[FFmpeg-devel] [PATCH v2 00/10] Added VideoToolbox H.264 Encoder.

2015-11-18 Thread kernrj
From: Rick Kern 

Updated patch with requested changes.

Rick Kern (10):
  Added VideoToolbox H.264 encoder.
  vtenc: Enable hardware encoding on OSX.
  vtenc: Removed trailing whitespace.
  vtenc: Fixed iOS compilation.
  vtenc: Check for min iOS/OSX versions.
  Better VideoToolbox check in configure.
  vtenc: check condition after conditional variable unblocks.
  vtenc: better allocation failure checking.
  vtenc: check for NULL CMSampleBufferRef in encoder callback.
  vtenc: Fixed mixed declarations and code.

 MAINTAINERS|1 +
 configure  |4 +
 libavcodec/Makefile|1 +
 libavcodec/allcodecs.c |1 +
 libavcodec/vtenc.c | 1212 
 5 files changed, 1219 insertions(+)
 create mode 100644 libavcodec/vtenc.c

-- 
2.4.9 (Apple Git-60)

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v2 01/10] Added VideoToolbox H.264 encoder.

2015-11-18 Thread kernrj
From: Rick Kern 

Enable with configure --enable-vtenc and encode using -codec:v vtenc_h264.

Signed-off-by: Rick Kern 
---
 MAINTAINERS|1 +
 configure  |   14 +
 libavcodec/Makefile|1 +
 libavcodec/allcodecs.c |1 +
 libavcodec/vtenc.c | 1089 
 5 files changed, 1106 insertions(+)
 create mode 100644 libavcodec/vtenc.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 3735742..28782d2 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -286,6 +286,7 @@ Codecs:
   vp8   David Conrad, Jason Garrett-Glaser, 
Ronald Bultje
   vp9   Ronald Bultje, Clément Bœsch
   vqavideo.cMike Melanson
+  vtenc.c   Rick Kern
   wavpack.c Kostya Shishkov
   wmaprodec.c   Sascha Sommer
   wmavoice.cRonald S. Bultje
diff --git a/configure b/configure
index 9a736ce..e465424 100755
--- a/configure
+++ b/configure
@@ -286,6 +286,7 @@ External library support:
   --disable-sdldisable sdl [autodetect]
   --disable-securetransport disable Secure Transport, needed for TLS support
on OSX if openssl and gnutls are not used 
[autodetect]
+  --enable-vtenc   enable VideoToolbox encoding support [no]
   --enable-x11grab enable X11 grabbing (legacy) [no]
   --disable-xlib   disable xlib [autodetect]
   --disable-zlib   disable zlib [autodetect]
@@ -1473,6 +1474,7 @@ EXTERNAL_LIBRARY_LIST="
 schannel
 sdl
 securetransport
+vtenc
 x11grab
 xlib
 zlib
@@ -2609,6 +2611,7 @@ libzvbi_teletext_decoder_deps="libzvbi"
 nvenc_encoder_deps="nvenc"
 nvenc_h264_encoder_deps="nvenc"
 nvenc_hevc_encoder_deps="nvenc"
+vtenc_h264_encoder_deps="vtenc"
 
 # demuxers / muxers
 ac3_demuxer_select="ac3_parser"
@@ -5471,6 +5474,17 @@ enabled openssl   && { check_lib openssl/ssl.h 
SSL_library_init -lssl -l
check_lib openssl/ssl.h SSL_library_init 
-lssl32 -leay32 ||
check_lib openssl/ssl.h SSL_library_init -lssl 
-lcrypto -lws2_32 -lgdi32 ||
die "ERROR: openssl not found"; }
+enabled vtenc && { { check_header VideoToolbox/VideoToolbox.h ||
+ die "ERROR: VideoToolbox/VideoToolbox.h not 
found."
+   } &&
+   { check_header "Availability.h" &&
+ { check_cpp_condition "Availability.h" 
"defined(__IPHONE_8_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0" ||
+   check_cpp_condition "Availability.h" 
"defined(__MAC_10_9) && __MAC_OS_X_VERSION_MAX_ALLOWED >= __MAC_10_9"
+  } ||
+  die "VideoToolbox requires Mac OSX 10.9+ or 
iOS 8.0+"
+   }
+ }
+
 enabled qtkit_indev  && { check_header_oc QTKit/QTKit.h || disable 
qtkit_indev; }
 
 if enabled gnutls; then
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 68a573f..afb38e4 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -116,6 +116,7 @@ OBJS-$(CONFIG_TEXTUREDSP)  += texturedsp.o
 OBJS-$(CONFIG_TEXTUREDSPENC)   += texturedspenc.o
 OBJS-$(CONFIG_TPELDSP) += tpeldsp.o
 OBJS-$(CONFIG_VIDEODSP)+= videodsp.o
+OBJS-$(CONFIG_VTENC)   += vtenc.o
 OBJS-$(CONFIG_VP3DSP)  += vp3dsp.o
 OBJS-$(CONFIG_VP56DSP) += vp56dsp.o
 OBJS-$(CONFIG_VP8DSP)  += vp8dsp.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 9f60d7c..6465df0 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -605,6 +605,7 @@ void avcodec_register_all(void)
 REGISTER_ENCODER(HEVC_QSV,  hevc_qsv);
 REGISTER_ENCODER(LIBKVAZAAR,libkvazaar);
 REGISTER_ENCODER(MPEG2_QSV, mpeg2_qsv);
+REGISTER_ENCODER(VTENC_H264,vtenc_h264);
 
 /* parsers */
 REGISTER_PARSER(AAC,aac);
diff --git a/libavcodec/vtenc.c b/libavcodec/vtenc.c
new file mode 100644
index 000..06c5360
--- /dev/null
+++ b/libavcodec/vtenc.c
@@ -0,0 +1,1089 @@
+/*
+ * copyright (c) 2015 Rick Kern 
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Publ

[FFmpeg-devel] [PATCH v2 02/10] vtenc: Enable hardware encoding on OSX.

2015-11-18 Thread kernrj
From: Rick Kern 

Use correct dictionary key/value to enable hardware encoding on OSX.

Signed-off-by: Rick Kern 
---
 libavcodec/vtenc.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libavcodec/vtenc.c b/libavcodec/vtenc.c
index 06c5360..f497c38 100644
--- a/libavcodec/vtenc.c
+++ b/libavcodec/vtenc.c
@@ -500,6 +500,7 @@ static av_cold int vtenc_init(AVCodecContext* avctx){
 );
 
 CFDictionarySetValue(enc_info, 
kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder, 
kCFBooleanTrue);
+CFDictionarySetValue(enc_info, 
kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder, 
kCFBooleanTrue);
 
 status = VTCompressionSessionCreate(
 kCFAllocatorDefault,
-- 
2.4.9 (Apple Git-60)

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v2 03/10] vtenc: Removed trailing whitespace.

2015-11-18 Thread kernrj
From: Rick Kern 

Signed-off-by: Rick Kern 
---
 libavcodec/vtenc.c | 308 ++---
 1 file changed, 154 insertions(+), 154 deletions(-)

diff --git a/libavcodec/vtenc.c b/libavcodec/vtenc.c
index f497c38..b363492 100644
--- a/libavcodec/vtenc.c
+++ b/libavcodec/vtenc.c
@@ -44,20 +44,20 @@ typedef struct BufNode{
 typedef struct VTEncContext{
 AVClass* class;
 VTCompressionSessionRef session;
-
+
 pthread_mutex_t lock;
 pthread_cond_t cv_sample_sent;
 int async_error;
-
+
 BufNode* q_head;
 BufNode* q_tail;
-
+
 int64_t frame_ct_out;
 int64_t frame_ct_in;
-
+
 int64_t first_pts;
 int64_t dts_delta;
-
+
 char* profile;
 char* level;
 
@@ -68,62 +68,62 @@ typedef struct VTEncContext{
 
 static void set_async_error(VTEncContext* vtctx, int err){
 pthread_mutex_lock(&vtctx->lock);
-
+
 vtctx->async_error = err;
-
+
 BufNode* info = vtctx->q_head;
 vtctx->q_head = vtctx->q_tail = NULL;
-
+
 while(info){
 CFRelease(info->cm_buffer);
-
+
 BufNode* next = info->next;
 free(info);
 info = next;
 }
-
+
 pthread_mutex_unlock(&vtctx->lock);
 }
 
 static int vtenc_q_pop(VTEncContext* vtctx, bool wait, CMSampleBufferRef* buf){
 pthread_mutex_lock(&vtctx->lock);
-
+
 if(vtctx->async_error){
 pthread_mutex_unlock(&vtctx->lock);
 return vtctx->async_error;
 }
-
+
 if(vtctx->flushing && vtctx->frame_ct_in == vtctx->frame_ct_out){
 *buf = NULL;
-
+
 pthread_mutex_unlock(&vtctx->lock);
 return 0;
 }
-
+
 if(!vtctx->q_head && wait){
 pthread_cond_wait(&vtctx->cv_sample_sent, &vtctx->lock);
 }
-
+
 if(!vtctx->q_head){
 pthread_mutex_unlock(&vtctx->lock);
 *buf = NULL;
 return 0;
 }
-
+
 BufNode* info = vtctx->q_head;
 vtctx->q_head = vtctx->q_head->next;
 if(!vtctx->q_head){
 vtctx->q_tail = NULL;
 }
-
+
 pthread_mutex_unlock(&vtctx->lock);
-
+
 CMSampleBufferRef buffer = info->cm_buffer;
 free(info);
-
+
 vtctx->frame_ct_out++;
 *buf = buffer;
-
+
 return 0;
 }
 
@@ -133,19 +133,19 @@ static void vtenc_q_push(VTEncContext* vtctx, 
CMSampleBufferRef buffer){
 CFRetain(buffer);
 info->cm_buffer = buffer;
 info->next = NULL;
-
+
 pthread_mutex_lock(&vtctx->lock);
 pthread_cond_signal(&vtctx->cv_sample_sent);
-
+
 if(!vtctx->q_head){
 vtctx->q_head = vtctx->q_tail = info;
 pthread_mutex_unlock(&vtctx->lock);
 return;
 }
-
+
 vtctx->q_tail->next = info;
 vtctx->q_tail = info;
-
+
 pthread_mutex_unlock(&vtctx->lock);
 }
 
@@ -177,13 +177,13 @@ static int get_params_info(
 {
 size_t total_size = 0;
 size_t ps_count;
-
+
 OSStatus status = 
CMVideoFormatDescriptionGetH264ParameterSetAtIndex(vid_fmt, 0, NULL, NULL, 
&ps_count, NULL);
 if(status){
 av_log(avctx, AV_LOG_ERROR, "Error getting parameter set count: %d\n", 
status);
 return AVERROR_EXTERNAL;
 }
-
+
 for(size_t i = 0; i < ps_count; i++){
 const uint8_t* ps;
 size_t ps_size;
@@ -192,7 +192,7 @@ static int get_params_info(
 av_log(avctx, AV_LOG_ERROR, "Error getting parameter set size for 
index %zd: %d\n", i, status);
 return AVERROR_EXTERNAL;
 }
-
+
 total_size += ps_size + sizeof(start_code);
 }
 
@@ -212,7 +212,7 @@ static int copy_param_sets(
 av_log(avctx, AV_LOG_ERROR, "Error getting parameter set count for 
copying: %d\n", status);
 return AVERROR_EXTERNAL;
 }
-
+
 size_t offset = 0;
 for(size_t i = 0; i < ps_count; i++){
 const uint8_t* ps;
@@ -222,20 +222,20 @@ static int copy_param_sets(
 av_log(avctx, AV_LOG_ERROR, "Error getting parameter set data for 
index %zd: %d\n", i, status);
 return AVERROR_EXTERNAL;
 }
-
+
 size_t next_offset = offset + sizeof(start_code) + ps_size;
 if(dst_size < next_offset){
 av_log(avctx, AV_LOG_ERROR, "Error: buffer too small for parameter 
sets.\n");
 return AVERROR_BUFFER_TOO_SMALL;
 }
-
+
 memcpy(dst + offset, start_code, sizeof(start_code));
 offset += sizeof(start_code);
-
+
 memcpy(dst + offset, ps, ps_size);
 offset = next_offset;
 }
-
+
 return 0;
 }
 
@@ -246,7 +246,7 @@ static int set_extradata(AVCodecContext* avctx, 
CMSampleBufferRef sample_buffer)
 av_log(avctx, AV_LOG_ERROR, "No video format.\n");
 return AVERROR_EXTERNAL;
 }
-
+
 size_t total_size;
 int status;
 status = get_params_info(avctx, vid_fmt, &total_size);
@@ -254,19 +254,19 @@ static int set_extradata(AVCodecContext* avctx, 
CMSampleBufferRef sample_buffer

[FFmpeg-devel] [PATCH v2 05/10] vtenc: Check for min iOS/OSX versions.

2015-11-18 Thread kernrj
From: Rick Kern 

configure was checking for the SDK version being used to compile. Now it 
compares against the minimum deployment target.

Signed-off-by: Rick Kern 
---
 configure | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/configure b/configure
index e465424..718f285 100755
--- a/configure
+++ b/configure
@@ -5477,9 +5477,9 @@ enabled openssl   && { check_lib openssl/ssl.h 
SSL_library_init -lssl -l
 enabled vtenc && { { check_header VideoToolbox/VideoToolbox.h ||
  die "ERROR: VideoToolbox/VideoToolbox.h not 
found."
} &&
-   { check_header "Availability.h" &&
- { check_cpp_condition "Availability.h" 
"defined(__IPHONE_8_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0" ||
-   check_cpp_condition "Availability.h" 
"defined(__MAC_10_9) && __MAC_OS_X_VERSION_MAX_ALLOWED >= __MAC_10_9"
+   { { check_header "Availability.h" || die 
"Availability.h missing"; }&&
+ { check_cpp_condition "Availability.h" 
"defined(__IPHONE_8_0) && __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_8_0" ||
+   check_cpp_condition "Availability.h" 
"defined(__MAC_10_9) && __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_9"
   } ||
   die "VideoToolbox requires Mac OSX 10.9+ or 
iOS 8.0+"
}
-- 
2.4.9 (Apple Git-60)

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v2 04/10] vtenc: Fixed iOS compilation.

2015-11-18 Thread kernrj
From: Rick Kern 

Removed dictionary keys that aren't supported on iOS builds.

Signed-off-by: Rick Kern 
---
 libavcodec/vtenc.c | 4 
 1 file changed, 4 insertions(+)

diff --git a/libavcodec/vtenc.c b/libavcodec/vtenc.c
index b363492..7132411 100644
--- a/libavcodec/vtenc.c
+++ b/libavcodec/vtenc.c
@@ -499,8 +499,10 @@ static av_cold int vtenc_init(AVCodecContext* avctx){
 &kCFTypeDictionaryValueCallBacks
 );
 
+#if !TARGET_OS_IPHONE
 CFDictionarySetValue(enc_info, 
kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder, 
kCFBooleanTrue);
 CFDictionarySetValue(enc_info, 
kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder, 
kCFBooleanTrue);
+#endif
 
 status = VTCompressionSessionCreate(
 kCFAllocatorDefault,
@@ -515,6 +517,7 @@ static av_cold int vtenc_init(AVCodecContext* avctx){
 &vtctx->session
 );
 
+#if !TARGET_OS_IPHONE
 if(status != 0 || !vtctx->session){
 CFDictionaryRemoveValue(enc_info, 
kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder);
 
@@ -531,6 +534,7 @@ static av_cold int vtenc_init(AVCodecContext* avctx){
 &vtctx->session
 );
 }
+#endif
 
 if(status || !vtctx->session){
 av_log(avctx, AV_LOG_ERROR, "Error: cannot create compression session: 
%d\n", status);
-- 
2.4.9 (Apple Git-60)

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v2 08/10] vtenc: better allocation failure checking.

2015-11-18 Thread kernrj
From: Rick Kern 

Some memory allocations were unchecked.

Signed-off-by: Rick Kern 
---
 libavcodec/vtenc.c | 11 +--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/libavcodec/vtenc.c b/libavcodec/vtenc.c
index f0d8289..583244c 100644
--- a/libavcodec/vtenc.c
+++ b/libavcodec/vtenc.c
@@ -118,17 +118,20 @@ static int vtenc_q_pop(VTEncContext* vtctx, bool wait, 
CMSampleBufferRef* buf){
 
 pthread_mutex_unlock(&vtctx->lock);
 
-CMSampleBufferRef buffer = info->cm_buffer;
+*buf = info->cm_buffer;
 free(info);
 
 vtctx->frame_ct_out++;
-*buf = buffer;
 
 return 0;
 }
 
 static void vtenc_q_push(VTEncContext* vtctx, CMSampleBufferRef buffer){
 BufNode* info = (BufNode*)malloc(sizeof(BufNode));
+if(!info){
+set_async_error(vtctx, AVERROR(ENOMEM));
+return;
+}
 
 CFRetain(buffer);
 info->cm_buffer = buffer;
@@ -499,6 +502,8 @@ static av_cold int vtenc_init(AVCodecContext* avctx){
 &kCFTypeDictionaryValueCallBacks
 );
 
+if(!enc_info) return AVERROR(ENOMEM);
+
 #if !TARGET_OS_IPHONE
 CFDictionarySetValue(enc_info, 
kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder, 
kCFBooleanTrue);
 CFDictionarySetValue(enc_info, 
kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder, 
kCFBooleanTrue);
@@ -543,6 +548,8 @@ static av_cold int vtenc_init(AVCodecContext* avctx){
 
 SInt32 bit_rate = avctx->bit_rate;
 CFNumberRef bit_rate_num = CFNumberCreate(kCFAllocatorDefault, 
kCFNumberSInt32Type, &bit_rate);
+if(!bit_rate_num) return AVERROR(ENOMEM);
+
 status = VTSessionSetProperty(vtctx->session, 
kVTCompressionPropertyKey_AverageBitRate, bit_rate_num);
 CFRelease(bit_rate_num);
 
-- 
2.4.9 (Apple Git-60)

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v2 07/10] vtenc: check condition after conditional variable unblocks.

2015-11-18 Thread kernrj
From: Rick Kern 

Condition was not being verified after conditional variable unblocked.

Signed-off-by: Rick Kern 
---
 libavcodec/vtenc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavcodec/vtenc.c b/libavcodec/vtenc.c
index 7132411..f0d8289 100644
--- a/libavcodec/vtenc.c
+++ b/libavcodec/vtenc.c
@@ -100,7 +100,7 @@ static int vtenc_q_pop(VTEncContext* vtctx, bool wait, 
CMSampleBufferRef* buf){
 return 0;
 }
 
-if(!vtctx->q_head && wait){
+while(!vtctx->q_head && !vtctx->async_error && wait){
 pthread_cond_wait(&vtctx->cv_sample_sent, &vtctx->lock);
 }
 
-- 
2.4.9 (Apple Git-60)

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v2 06/10] Better VideoToolbox check in configure.

2015-11-18 Thread kernrj
From: Rick Kern 

---
 configure | 12 +---
 1 file changed, 1 insertion(+), 11 deletions(-)

diff --git a/configure b/configure
index 718f285..f99fbd9 100755
--- a/configure
+++ b/configure
@@ -5474,17 +5474,7 @@ enabled openssl   && { check_lib openssl/ssl.h 
SSL_library_init -lssl -l
check_lib openssl/ssl.h SSL_library_init 
-lssl32 -leay32 ||
check_lib openssl/ssl.h SSL_library_init -lssl 
-lcrypto -lws2_32 -lgdi32 ||
die "ERROR: openssl not found"; }
-enabled vtenc && { { check_header VideoToolbox/VideoToolbox.h ||
- die "ERROR: VideoToolbox/VideoToolbox.h not 
found."
-   } &&
-   { { check_header "Availability.h" || die 
"Availability.h missing"; }&&
- { check_cpp_condition "Availability.h" 
"defined(__IPHONE_8_0) && __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_8_0" ||
-   check_cpp_condition "Availability.h" 
"defined(__MAC_10_9) && __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_9"
-  } ||
-  die "VideoToolbox requires Mac OSX 10.9+ or 
iOS 8.0+"
-   }
- }
-
+enabled vtenc && require VideoToolbox 
VideoToolbox/VTCompressionSession.h VTCompressionSessionPrepareToEncodeFrames 
-framework VideoToolbox
 enabled qtkit_indev  && { check_header_oc QTKit/QTKit.h || disable 
qtkit_indev; }
 
 if enabled gnutls; then
-- 
2.4.9 (Apple Git-60)

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v2 09/10] vtenc: check for NULL CMSampleBufferRef in encoder callback.

2015-11-18 Thread kernrj
From: Rick Kern 

The CMSampleBufferRef passed into the encoder can be NULL. Added check.

Signed-off-by: Rick Kern 
---
 libavcodec/vtenc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libavcodec/vtenc.c b/libavcodec/vtenc.c
index 583244c..78a918b 100644
--- a/libavcodec/vtenc.c
+++ b/libavcodec/vtenc.c
@@ -285,11 +285,11 @@ static av_cold void vtenc_output_callback(
 VTEncContext*   vtctx = (VTEncContext*)avctx->priv_data;
 
 if(vtctx->async_error){
-CFRelease(sample_buffer);
+if(sample_buffer) CFRelease(sample_buffer);
 return;
 }
 
-if(status){
+if(status || !sample_buffer){
 av_log(avctx, AV_LOG_ERROR, "Error encoding frame: %d\n", status);
 set_async_error(vtctx, AVERROR_EXTERNAL);
 return;
-- 
2.4.9 (Apple Git-60)

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH v2 10/10] vtenc: Fixed mixed declarations and code.

2015-11-18 Thread kernrj
From: Rick Kern 

Moved all declarations to the top of their block.

Signed-off-by: Rick Kern 
---
 libavcodec/vtenc.c | 409 ++---
 1 file changed, 260 insertions(+), 149 deletions(-)

diff --git a/libavcodec/vtenc.c b/libavcodec/vtenc.c
index 78a918b..a8cc340 100644
--- a/libavcodec/vtenc.c
+++ b/libavcodec/vtenc.c
@@ -67,17 +67,18 @@ typedef struct VTEncContext{
 } VTEncContext;
 
 static void set_async_error(VTEncContext* vtctx, int err){
+BufNode* info;
+
 pthread_mutex_lock(&vtctx->lock);
 
 vtctx->async_error = err;
 
-BufNode* info = vtctx->q_head;
+info = vtctx->q_head;
 vtctx->q_head = vtctx->q_tail = NULL;
 
 while(info){
-CFRelease(info->cm_buffer);
-
 BufNode* next = info->next;
+CFRelease(info->cm_buffer);
 free(info);
 info = next;
 }
@@ -86,6 +87,8 @@ static void set_async_error(VTEncContext* vtctx, int err){
 }
 
 static int vtenc_q_pop(VTEncContext* vtctx, bool wait, CMSampleBufferRef* buf){
+BufNode* info;
+
 pthread_mutex_lock(&vtctx->lock);
 
 if(vtctx->async_error){
@@ -110,7 +113,7 @@ static int vtenc_q_pop(VTEncContext* vtctx, bool wait, 
CMSampleBufferRef* buf){
 return 0;
 }
 
-BufNode* info = vtctx->q_head;
+info = vtctx->q_head;
 vtctx->q_head = vtctx->q_head->next;
 if(!vtctx->q_head){
 vtctx->q_tail = NULL;
@@ -141,12 +144,12 @@ static void vtenc_q_push(VTEncContext* vtctx, 
CMSampleBufferRef buffer){
 pthread_cond_signal(&vtctx->cv_sample_sent);
 
 if(!vtctx->q_head){
-vtctx->q_head = vtctx->q_tail = info;
-pthread_mutex_unlock(&vtctx->lock);
-return;
+vtctx->q_head = info;
+}
+else{
+vtctx->q_tail->next = info;
 }
 
-vtctx->q_tail->next = info;
 vtctx->q_tail = info;
 
 pthread_mutex_unlock(&vtctx->lock);
@@ -172,22 +175,21 @@ static void vtenc_free_block(void* opaque, uint8_t* data){
  *In all cases, *dst_size is set to the number of bytes used 
starting
  *at *dst.
  */
-
-static int get_params_info(
+static int get_params_size(
 AVCodecContext* avctx,
 CMVideoFormatDescriptionRef vid_fmt,
 size_t* size)
 {
 size_t total_size = 0;
 size_t ps_count;
-
+size_t i;
 OSStatus status = 
CMVideoFormatDescriptionGetH264ParameterSetAtIndex(vid_fmt, 0, NULL, NULL, 
&ps_count, NULL);
 if(status){
 av_log(avctx, AV_LOG_ERROR, "Error getting parameter set count: %d\n", 
status);
 return AVERROR_EXTERNAL;
 }
 
-for(size_t i = 0; i < ps_count; i++){
+for(i = 0; i < ps_count; i++){
 const uint8_t* ps;
 size_t ps_size;
 status = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(vid_fmt, 
i, &ps, &ps_size, NULL, NULL);
@@ -211,22 +213,26 @@ static int copy_param_sets(
 {
 size_t ps_count;
 OSStatus status = 
CMVideoFormatDescriptionGetH264ParameterSetAtIndex(vid_fmt, 0, NULL, NULL, 
&ps_count, NULL);
+size_t offset = 0;
+size_t i;
+
 if(status){
 av_log(avctx, AV_LOG_ERROR, "Error getting parameter set count for 
copying: %d\n", status);
 return AVERROR_EXTERNAL;
 }
 
-size_t offset = 0;
-for(size_t i = 0; i < ps_count; i++){
+for(i = 0; i < ps_count; i++){
 const uint8_t* ps;
 size_t ps_size;
+size_t next_offset;
+
 status = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(vid_fmt, 
i, &ps, &ps_size, NULL, NULL);
 if(status){
 av_log(avctx, AV_LOG_ERROR, "Error getting parameter set data for 
index %zd: %d\n", i, status);
 return AVERROR_EXTERNAL;
 }
 
-size_t next_offset = offset + sizeof(start_code) + ps_size;
+next_offset = offset + sizeof(start_code) + ps_size;
 if(dst_size < next_offset){
 av_log(avctx, AV_LOG_ERROR, "Error: buffer too small for parameter 
sets.\n");
 return AVERROR_BUFFER_TOO_SMALL;
@@ -244,15 +250,16 @@ static int copy_param_sets(
 
 static int set_extradata(AVCodecContext* avctx, CMSampleBufferRef 
sample_buffer){
 CMVideoFormatDescriptionRef vid_fmt;
+size_t total_size;
+int status;
+
 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
 if(!vid_fmt){
 av_log(avctx, AV_LOG_ERROR, "No video format.\n");
 return AVERROR_EXTERNAL;
 }
 
-size_t total_size;
-int status;
-status = get_params_info(avctx, vid_fmt, &total_size);
+status = get_params_size(avctx, vid_fmt, &total_size);
 if(status){
 av_log(avctx, AV_LOG_ERROR, "Could not get parameter sets.\n");
 return status;
@@ -280,9 +287,8 @@ static av_cold void vtenc_output_callback(
 VTEncodeInfoFlags flags,
 CM_NULLABLE CMSampleBufferRef sample_buffer)
 {
-av_assert0(ctx);
 AVCodecContext* avctx = (AVCodecContext*)ctx;
-VTEncContext*   vtctx = (VTEncContext*)avctx->priv_data;
+VTEncContext*   vtctx