[PATCH v2 00/12] videotoolbox encoder features and fixes
Sorry for the delay. The commit for the keyframes_only property was removed - this was already supported with -g 1. Rick Kern (12): lavc/videotoolboxenc: Use shared pixel buffer pool lavc/videotoolboxenc: Handle hwaccel format as input lavc/videotoolboxenc: Require hardware encoding lavc/videotoolboxenc: Add entropy setting lavc/videotoolboxenc: Fix AVCodecContext.has_b_frames usage. lavc/videotoolboxenc: Fix DTS lavc/videotoolboxenc: Handle out-of-memory and fix memory leak lavc/videotoolboxenc: Add realtime encoding property lavc/videotoolboxenc: add concatentation properties lavc/videotoolboxenc: Support for forced I-frames lavc/videotoolboxenc: Support pixel aspect ratio lavc/videotoolboxenc: Set colorimetry values configure | 5 +- libavcodec/videotoolboxenc.c | 682 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 619 insertions(+), 68 deletions(-) -- 2.7.4
This reduces the chance of a memcpy in the media server. Signed-off-by: Rick Kern <kernrj@gmail.com> --- libavcodec/videotoolboxenc.c | 224 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 180 insertions(+), 44 deletions(-) diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c index 3177074..b578260 100644 --- a/libavcodec/videotoolboxenc.c +++ b/libavcodec/videotoolboxenc.c @@ -29,6 +29,7 @@ #include "libavutil/atomic.h" #include "libavutil/avstring.h" #include "libavcodec/avcodec.h" +#include "libavutil/pixdesc.h" #include "internal.h" #include <pthread.h> @@ -74,6 +75,15 @@ typedef struct VTEncContext { bool warned_color_range; } VTEncContext; +/** + * NULL-safe release of *refPtr, and sets value to NULL. + */ +static void vt_release_num(CFNumberRef* refPtr){ + if(!*refPtr) return; + CFRelease(*refPtr); + *refPtr = NULL; +} + static void set_async_error(VTEncContext *vtctx, int err) { BufNode *info; @@ -477,9 +487,123 @@ static bool get_vt_profile_level(AVCodecContext *avctx, return true; } +static int get_cv_pixel_format( + AVCodecContext* avctx, + enum AVPixelFormat fmt, + enum AVColorRange range, + int* av_pixel_format, + int* range_guessed) +{ + if (range_guessed) *range_guessed = 0; + + switch (fmt) { + case AV_PIX_FMT_NV12: + switch (range) { + case AVCOL_RANGE_MPEG: + *av_pixel_format = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange; + break; + + case AVCOL_RANGE_JPEG: + *av_pixel_format = kCVPixelFormatType_420YpCbCr8BiPlanarFullRange; + break; + + default: + if (range_guessed) *range_guessed = 1; + *av_pixel_format = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange; + } + break; + + case AV_PIX_FMT_YUV420P: + switch (range) { + case AVCOL_RANGE_MPEG: + *av_pixel_format = kCVPixelFormatType_420YpCbCr8Planar; + break; + + case AVCOL_RANGE_JPEG: + *av_pixel_format = kCVPixelFormatType_420YpCbCr8PlanarFullRange; + break; + + default: + if(range_guessed) *range_guessed = 1; + *av_pixel_format = kCVPixelFormatType_420YpCbCr8Planar; + } + break; + + default: return AVERROR(EINVAL); + } + + return 0; +} + +static int create_cv_pixel_buffer_info(AVCodecContext* avctx, + CFMutableDictionaryRef* dict) +{ + CFNumberRef cv_color_format_num = NULL; + CFNumberRef width_num = NULL; + CFNumberRef height_num = NULL; + CFMutableDictionaryRef pixel_buffer_info = NULL; + int cv_color_format; + int status = get_cv_pixel_format(avctx, + avctx->pix_fmt, + avctx->color_range, + &cv_color_format, + NULL); + if (status) return status; + + pixel_buffer_info = CFDictionaryCreateMutable( + kCFAllocatorDefault, + 20, + &kCFCopyStringDictionaryKeyCallBacks, + &kCFTypeDictionaryValueCallBacks); + + if (!pixel_buffer_info) goto pbinfo_nomem; + + cv_color_format_num = CFNumberCreate(kCFAllocatorDefault, + kCFNumberSInt32Type, + &cv_color_format); + if (!cv_color_format_num) goto pbinfo_nomem; + + CFDictionarySetValue(pixel_buffer_info, + kCVPixelBufferPixelFormatTypeKey, + cv_color_format_num); + vt_release_num(&cv_color_format_num); + + width_num = CFNumberCreate(kCFAllocatorDefault, + kCFNumberSInt32Type, + &avctx->width); + if (!width_num) return AVERROR(ENOMEM); + + CFDictionarySetValue(pixel_buffer_info, + kCVPixelBufferWidthKey, + width_num); + vt_release_num(&width_num); + + height_num = CFNumberCreate(kCFAllocatorDefault, + kCFNumberSInt32Type, + &avctx->height); + if (!height_num) goto pbinfo_nomem; + + CFDictionarySetValue(pixel_buffer_info, + kCVPixelBufferHeightKey, + height_num); + vt_release_num(&height_num); + + *dict = pixel_buffer_info; + return 0; + +pbinfo_nomem: + vt_release_num(&cv_color_format_num); + vt_release_num(&width_num); + vt_release_num(&height_num); + if (pixel_buffer_info) CFRelease(pixel_buffer_info); + + return AVERROR(ENOMEM); +} + static av_cold int vtenc_init(AVCodecContext *avctx) { CFMutableDictionaryRef enc_info; + CFMutableDictionaryRef pixel_buffer_info; CMVideoCodecType codec_type; VTEncContext *vtctx = avctx->priv_data; CFStringRef profile_level; @@ -517,13 +641,19 @@ static av_cold int vtenc_init(AVCodecContext *avctx) CFDictionarySetValue(enc_info, kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder, kCFBooleanTrue); #endif + status = create_cv_pixel_buffer_info(avctx, &pixel_buffer_info); + if (status) { + CFRelease(enc_info); + return status; + } + status = VTCompressionSessionCreate( kCFAllocatorDefault, avctx->width, avctx->height, codec_type, enc_info, - NULL, + pixel_buffer_info, kCFAllocatorDefault, vtenc_output_callback, avctx, @@ -549,6 +679,7 @@ static av_cold int vtenc_init(AVCodecContext *avctx) } #endif + CFRelease(pixel_buffer_info); CFRelease(enc_info); if (status || !vtctx->session) { @@ -912,26 +1043,37 @@ static int get_cv_pixel_info( int av_format = frame->format; int av_color_range = av_frame_get_color_range(frame); int i; + int range_guessed; + int status; - switch (av_format) { - case AV_PIX_FMT_NV12: - switch (av_color_range) { - case AVCOL_RANGE_MPEG: - *color = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange; - break; + status = get_cv_pixel_format(avctx, av_format, av_color_range, color, &range_guessed); + if (status) { + av_log(avctx, + AV_LOG_ERROR, + "Could not get pixel format for color format '%s' range '%s'.\n", + av_get_pix_fmt_name(av_format), + av_color_range > AVCOL_RANGE_UNSPECIFIED && + av_color_range < AVCOL_RANGE_NB ? + av_color_range_name(av_color_range) : + "Unknown"); - case AVCOL_RANGE_JPEG: - *color = kCVPixelFormatType_420YpCbCr8BiPlanarFullRange; - break; + return AVERROR(EINVAL); + } - default: - if (!vtctx->warned_color_range) { - vtctx->warned_color_range = true; - av_log(avctx, AV_LOG_WARNING, "Color range not set for NV12. Using MPEG range.\n"); - } - *color = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange; + if (range_guessed) { + if (!vtctx->warned_color_range) { + vtctx->warned_color_range = true; + av_log(avctx, + AV_LOG_WARNING, + "Color range not set for %s. Using MPEG range.\n", + av_get_pix_fmt_name(av_format)); } + av_log(avctx, AV_LOG_WARNING, ""); + } + + switch (av_format) { + case AV_PIX_FMT_NV12: *plane_count = 2; widths [0] = avctx->width; @@ -944,23 +1086,6 @@ static int get_cv_pixel_info( break; case AV_PIX_FMT_YUV420P: - switch (av_color_range) { - case AVCOL_RANGE_MPEG: - *color = kCVPixelFormatType_420YpCbCr8Planar; - break; - - case AVCOL_RANGE_JPEG: - *color = kCVPixelFormatType_420YpCbCr8PlanarFullRange; - break; - - default: - if (!vtctx->warned_color_range) { - vtctx->warned_color_range = true; - av_log(avctx, AV_LOG_WARNING, "Color range not set for YUV 4:2:0. Using MPEG range.\n"); - } - *color = kCVPixelFormatType_420YpCbCr8Planar; - } - *plane_count = 3; widths [0] = avctx->width; @@ -976,7 +1101,15 @@ static int get_cv_pixel_info( strides[2] = frame ? frame->linesize[2] : (avctx->width + 1) / 2; break; - default: return AVERROR(EINVAL); + default: + av_log( + avctx, + AV_LOG_ERROR, + "Could not get frame format info for color %d range %d.\n", + av_format, + av_color_range); + + return AVERROR(EINVAL); } *contiguous_buf_size = 0; @@ -1111,6 +1244,8 @@ static int create_cv_pixel_buffer(AVCodecContext *avctx, size_t strides[AV_NUM_DATA_POINTERS]; int status; size_t contiguous_buf_size; + CVPixelBufferPoolRef pix_buf_pool; + VTEncContext* vtctx = avctx->priv_data; memset(widths, 0, sizeof(widths)); memset(heights, 0, sizeof(heights)); @@ -1141,16 +1276,19 @@ static int create_cv_pixel_buffer(AVCodecContext *avctx, } #if TARGET_OS_IPHONE - status = CVPixelBufferCreate( - kCFAllocatorDefault, - frame->width, - frame->height, - color, - NULL, - cv_img - ); + pix_buf_pool = VTCompressionSessionGetPixelBufferPool(vtctx->session); + if (!pix_buf_pool) { + av_log(avctx, AV_LOG_ERROR, "Could not get pixel buffer pool.\n"); + return AVERROR_EXTERNAL; + } + + status = CVPixelBufferPoolCreatePixelBuffer(NULL, + pix_buf_pool, + cv_img); + if (status) { + av_log(avctx, AV_LOG_ERROR, "Could not create pixel buffer from pool: %d.\n", status); return AVERROR_EXTERNAL; } @@ -1306,9 +1444,7 @@ static av_cold int vtenc_close(AVCodecContext *avctx) static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_NV12, -#if !TARGET_OS_IPHONE AV_PIX_FMT_YUV420P, -#endif AV_PIX_FMT_NONE }; -- 2.7.4
Rick Kern <kernrj <at> gmail.com> writes:
+ if(!*refPtr) return;
Is this needed (I don't know)? If yes, please make it two lines.
+ CFRelease(*refPtr); + *refPtr = NULL; + switch (fmt) { + case AV_PIX_FMT_NV12: + switch (range) { + case AVCOL_RANGE_MPEG: + *av_pixel_format = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange; + break; + + case AVCOL_RANGE_JPEG: + *av_pixel_format = kCVPixelFormatType_420YpCbCr8BiPlanarFullRange; + break; + + default: + if (range_guessed) *range_guessed = 1; + *av_pixel_format = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
You can rearrange this to make the patch smaller (and the default case easier to read). Same below.
static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_NV12, -#if !TARGET_OS_IPHONE AV_PIX_FMT_YUV420P, -#endif AV_PIX_FMT_NONE
Sorry: How is this related? Carl Eugen
On Apr 23, 2016, at 12:23 PM, Carl Eugen Hoyos <cehoyos@ag.or.at> wrote:
Rick Kern <kernrj <at> gmail.com> writes:
+ if(!*refPtr) return;
Is this needed (I don't know)? If yes, please make it two lines.
+ CFRelease(*refPtr); + *refPtr = NULL; + switch (fmt) { + case AV_PIX_FMT_NV12: + switch (range) { + case AVCOL_RANGE_MPEG: + *av_pixel_format = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange; + break; + + case AVCOL_RANGE_JPEG: + *av_pixel_format = kCVPixelFormatType_420YpCbCr8BiPlanarFullRange; + break; + + default: + if (range_guessed) *range_guessed = 1; + *av_pixel_format = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
You can rearrange this to make the patch smaller (and the default case easier to read). Same below.
static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_NV12, -#if !TARGET_OS_IPHONE AV_PIX_FMT_YUV420P, -#endif AV_PIX_FMT_NONE
Sorry: How is this related? Using a shared pixel buffer pool also sets up an internal color converter when needed.
Carl Eugen
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
Richard Kern <kernrj <at> gmail.com> writes:
static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_NV12, -#if !TARGET_OS_IPHONE AV_PIX_FMT_YUV420P, -#endif AV_PIX_FMT_NONE
Sorry: How is this related? Using a shared pixel buffer pool also sets up an internal
color converter when needed.
And is this generally wanted? (I assume so.) Carl Eugen
On Apr 26, 2016, at 8:14 AM, Carl Eugen Hoyos <cehoyos@ag.or.at> wrote:
Richard Kern <kernrj <at> gmail.com> writes:
static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_NV12, -#if !TARGET_OS_IPHONE AV_PIX_FMT_YUV420P, -#endif AV_PIX_FMT_NONE
Sorry: How is this related? Using a shared pixel buffer pool also sets up an internal
color converter when needed.
And is this generally wanted? (I assume so.) The pool? Probably. Using it reduces memcpy calls in the media server process.
I dug into this a little - yuv420p is supported natively (at least on some devices), so it shouldn’t have been excluded in the first place.
Carl Eugen
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
on iOS when enter background got Could not get pixel buffer pool. error How about videotoolbox run on background? On Wed, Apr 27, 2016 at 12:00 AM, Richard Kern <kernrj@gmail.com> wrote:
On Apr 26, 2016, at 8:14 AM, Carl Eugen Hoyos <cehoyos@ag.or.at> wrote:
Richard Kern <kernrj <at> gmail.com> writes:
static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_NV12, -#if !TARGET_OS_IPHONE AV_PIX_FMT_YUV420P, -#endif AV_PIX_FMT_NONE
Sorry: How is this related? Using a shared pixel buffer pool also sets up an internal
color converter when needed.
And is this generally wanted? (I assume so.) The pool? Probably. Using it reduces memcpy calls in the media server process.
I dug into this a little - yuv420p is supported natively (at least on some devices), so it shouldn’t have been excluded in the first place.
Carl Eugen
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
Handle AV_PIX_FMT_VIDEOTOOLBOX. This results in better energy usage and faster encoding, especially on iOS. When the buffer comes from the media server, no memcpy's are needed. Signed-off-by: Rick Kern <kernrj@gmail.com> --- libavcodec/videotoolboxenc.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c index b578260..7b52073 100644 --- a/libavcodec/videotoolboxenc.c +++ b/libavcodec/videotoolboxenc.c @@ -641,10 +641,14 @@ static av_cold int vtenc_init(AVCodecContext *avctx) CFDictionarySetValue(enc_info, kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder, kCFBooleanTrue); #endif - status = create_cv_pixel_buffer_info(avctx, &pixel_buffer_info); - if (status) { - CFRelease(enc_info); - return status; + if (avctx->pix_fmt != AV_PIX_FMT_VIDEOTOOLBOX) { + status = create_cv_pixel_buffer_info(avctx, &pixel_buffer_info); + if (status) { + CFRelease(enc_info); + return status; + } + } else { + pixel_buffer_info = NULL; } status = VTCompressionSessionCreate( @@ -679,7 +683,7 @@ static av_cold int vtenc_init(AVCodecContext *avctx) } #endif - CFRelease(pixel_buffer_info); + if (pixel_buffer_info) CFRelease(pixel_buffer_info); CFRelease(enc_info); if (status || !vtctx->session) { @@ -1247,6 +1251,17 @@ static int create_cv_pixel_buffer(AVCodecContext *avctx, CVPixelBufferPoolRef pix_buf_pool; VTEncContext* vtctx = avctx->priv_data; + + if (avctx->pix_fmt == AV_PIX_FMT_VIDEOTOOLBOX) { + av_assert0(frame->format == AV_PIX_FMT_VIDEOTOOLBOX); + + *cv_img = (CVPixelBufferRef)frame->data[3]; + av_assert0(*cv_img); + + CFRetain(*cv_img); + return 0; + } + memset(widths, 0, sizeof(widths)); memset(heights, 0, sizeof(heights)); memset(strides, 0, sizeof(strides)); @@ -1443,6 +1458,7 @@ static av_cold int vtenc_close(AVCodecContext *avctx) } static const enum AVPixelFormat pix_fmts[] = { + AV_PIX_FMT_VIDEOTOOLBOX, AV_PIX_FMT_NV12, AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE -- 2.7.4
Software encoding can be allowed by setting allow_sw to 1. Signed-off-by: Rick Kern <kernrj@gmail.com> --- libavcodec/videotoolboxenc.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c index 7b52073..ec69bb1 100644 --- a/libavcodec/videotoolboxenc.c +++ b/libavcodec/videotoolboxenc.c @@ -70,6 +70,10 @@ typedef struct VTEncContext { int64_t profile; int64_t level; +#if !TARGET_OS_IPHONE + int64_t allow_sw; +#endif + bool flushing; bool has_b_frames; bool warned_color_range; @@ -637,8 +641,11 @@ static av_cold int vtenc_init(AVCodecContext *avctx) if (!enc_info) return AVERROR(ENOMEM); #if !TARGET_OS_IPHONE - CFDictionarySetValue(enc_info, kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder, kCFBooleanTrue); - CFDictionarySetValue(enc_info, kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder, kCFBooleanTrue); + if (vtctx->allow_sw != 1) { + CFDictionarySetValue(enc_info, kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder, kCFBooleanTrue); + } else { + CFDictionarySetValue(enc_info, kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder, kCFBooleanTrue); + } #endif if (avctx->pix_fmt != AV_PIX_FMT_VIDEOTOOLBOX) { @@ -664,30 +671,18 @@ static av_cold int vtenc_init(AVCodecContext *avctx) &vtctx->session ); -#if !TARGET_OS_IPHONE - if (status != 0 || !vtctx->session) { - CFDictionaryRemoveValue(enc_info, kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder); - - status = VTCompressionSessionCreate( - kCFAllocatorDefault, - avctx->width, - avctx->height, - codec_type, - enc_info, - NULL, - kCFAllocatorDefault, - vtenc_output_callback, - avctx, - &vtctx->session - ); - } -#endif - if (pixel_buffer_info) CFRelease(pixel_buffer_info); CFRelease(enc_info); if (status || !vtctx->session) { av_log(avctx, AV_LOG_ERROR, "Error: cannot create compression session: %d\n", status); + +#if !TARGET_OS_IPHONE + if (!vtctx->allow_sw) { + av_log(avctx, AV_LOG_ERROR, "Try -allow_sw 1. The hardware encoder may be busy, or not supported.\n"); + } +#endif + return AVERROR_EXTERNAL; } @@ -1484,6 +1479,11 @@ static const AVOption options[] = { { "5.1", "Level 5.1", 0, AV_OPT_TYPE_CONST, { .i64 = 51 }, INT_MIN, INT_MAX, VE, "level" }, { "5.2", "Level 5.2", 0, AV_OPT_TYPE_CONST, { .i64 = 52 }, INT_MIN, INT_MAX, VE, "level" }, +#if !TARGET_OS_IPHONE + { "allow_sw", "Allow software encoding", OFFSET(allow_sw), AV_OPT_TYPE_BOOL, + { .i64 = -1 }, -1, 1, VE }, +#endif + { NULL }, }; -- 2.7.4
Rick Kern <kernrj <at> gmail.com> writes:
+#if !TARGET_OS_IPHONE + { "allow_sw", "Allow software encoding", OFFSET(allow_sw),
I have no strong opinion, but shouldn't the option always exist (but only work in some cases)? Carl Eugen
On Apr 23, 2016, at 12:25 PM, Carl Eugen Hoyos <cehoyos@ag.or.at> wrote:
Rick Kern <kernrj <at> gmail.com> writes:
+#if !TARGET_OS_IPHONE + { "allow_sw", "Allow software encoding", OFFSET(allow_sw),
I have no strong opinion, but shouldn't the option always exist (but only work in some cases)? iOS doesn’t support software encoding, and the dictionary keys to allow it aren’t part of the headers.
Carl Eugen
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
Richard Kern <kernrj <at> gmail.com> writes:
+#if !TARGET_OS_IPHONE + { "allow_sw", "Allow software encoding", OFFSET(allow_sw),
I have no strong opinion, but shouldn't the option always exist (but only work in some cases)? iOS doesn’t support software encoding, and the dictionary keys to allow it aren’t part of the headers.
I understand this but I suspect the FFmpeg option "allow_sw" should also exist on ios. Carl Eugen
Add an entropy setting to choose between CAVLC and CABAC. Signed-off-by: Rick Kern <kernrj@gmail.com> --- libavcodec/videotoolboxenc.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c index ec69bb1..07577b6 100644 --- a/libavcodec/videotoolboxenc.c +++ b/libavcodec/videotoolboxenc.c @@ -41,6 +41,12 @@ typedef enum VT_H264Profile { H264_PROF_COUNT } VT_H264Profile; +typedef enum VTH264Entropy{ + VT_ENTROPY_NOT_SET, + VT_CAVLC, + VT_CABAC +} VTH264Entropy; + static const uint8_t start_code[] = { 0, 0, 0, 1 }; typedef struct BufNode { @@ -69,6 +75,7 @@ typedef struct VTEncContext { int64_t profile; int64_t level; + int64_t entropy; #if !TARGET_OS_IPHONE int64_t allow_sw; @@ -627,6 +634,11 @@ static av_cold int vtenc_init(AVCodecContext *avctx) vtctx->has_b_frames = false; } + if (vtctx->entropy == VT_CABAC && vtctx->profile == H264_PROF_BASELINE) { + av_log(avctx, AV_LOG_WARNING, "CABAC entropy requires 'main' or 'high' profile, but baseline was requested. Encode will not use CABAC entropy.\n"); + vtctx->entropy = VT_ENTROPY_NOT_SET; + } + if (!get_vt_profile_level(avctx, &profile_level)) return AVERROR(EINVAL); vtctx->session = NULL; @@ -736,6 +748,21 @@ static av_cold int vtenc_init(AVCodecContext *avctx) } } + if (vtctx->entropy != VT_ENTROPY_NOT_SET) { + CFStringRef entropy = vtctx->entropy == VT_CABAC ? + kVTH264EntropyMode_CABAC: + kVTH264EntropyMode_CAVLC; + + status = VTSessionSetProperty(vtctx->session, + kVTCompressionPropertyKey_H264EntropyMode, + entropy); + + if (status) { + av_log(avctx, AV_LOG_ERROR, "Error setting entropy property: %d\n", status); + return AVERROR_EXTERNAL; + } + } + status = VTCompressionSessionPrepareToEncodeFrames(vtctx->session); if (status) { av_log(avctx, AV_LOG_ERROR, "Error: cannot prepare encoder: %d\n", status); @@ -1484,6 +1511,10 @@ static const AVOption options[] = { { .i64 = -1 }, -1, 1, VE }, #endif + { "entropy", "Entropy coding", OFFSET(entropy), AV_OPT_TYPE_INT, { .i64 = VT_ENTROPY_NOT_SET }, VT_ENTROPY_NOT_SET, VT_CABAC, VE, "entropy" }, + { "cavlc", "CAVLC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CAVLC }, INT_MIN, INT_MAX, VE, "entropy" }, + { "cabac", "CABAC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CABAC }, INT_MIN, INT_MAX, VE, "entropy" }, + { NULL }, }; -- 2.7.4
Rick Kern <kernrj <at> gmail.com> writes:
Add an entropy setting to choose between CAVLC and CABAC.
Am I correct that it is impossible to do this similar to libx264? Carl Eugen
On Sat, Apr 23, 2016 at 6:27 PM, Carl Eugen Hoyos <cehoyos@ag.or.at> wrote:
Rick Kern <kernrj <at> gmail.com> writes:
Add an entropy setting to choose between CAVLC and CABAC.
Am I correct that it is impossible to do this similar to libx264?
libx264 in avcodec uses "coder" with values "cabac"/"ac" or "cavlc"/"vlc", using the same option names might be beneficial. - Hendrik
Now set by the encoder, not used as an input parameter. Signed-off-by: Rick Kern <kernrj@gmail.com> --- libavcodec/videotoolboxenc.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c index 07577b6..6a9a2fe 100644 --- a/libavcodec/videotoolboxenc.c +++ b/libavcodec/videotoolboxenc.c @@ -620,6 +620,7 @@ static av_cold int vtenc_init(AVCodecContext *avctx) CFStringRef profile_level; SInt32 bit_rate = avctx->bit_rate; CFNumberRef bit_rate_num; + CFBooleanRef has_b_frames_cfbool; int status; codec_type = get_cm_codec_type(avctx->codec_id); @@ -628,7 +629,7 @@ static av_cold int vtenc_init(AVCodecContext *avctx) return AVERROR(EINVAL); } - vtctx->has_b_frames = avctx->has_b_frames || avctx->max_b_frames > 0; + vtctx->has_b_frames = avctx->max_b_frames > 0; if(vtctx->has_b_frames && vtctx->profile == H264_PROF_BASELINE){ av_log(avctx, AV_LOG_WARNING, "Cannot use B-frames with baseline profile. Output will not contain B-frames.\n"); vtctx->has_b_frames = false; @@ -773,6 +774,18 @@ static av_cold int vtenc_init(AVCodecContext *avctx) pthread_cond_init(&vtctx->cv_sample_sent, NULL); vtctx->dts_delta = vtctx->has_b_frames ? -1 : 0; + status = VTSessionCopyProperty(vtctx->session, + kVTCompressionPropertyKey_AllowFrameReordering, + kCFAllocatorDefault, + &has_b_frames_cfbool); + + if (!status) { + //Some devices don't output B-frames for main profile, even if requested. + vtctx->has_b_frames = CFBooleanGetValue(has_b_frames_cfbool); + CFRelease(has_b_frames_cfbool); + } + avctx->has_b_frames = vtctx->has_b_frames; + return 0; } -- 2.7.4
Some devices output an invalid DTS when B-frames aren't used. Using PTS for the DTS in this case. Signed-off-by: Rick Kern <kernrj@gmail.com> --- libavcodec/videotoolboxenc.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c index 6a9a2fe..5594e69 100644 --- a/libavcodec/videotoolboxenc.c +++ b/libavcodec/videotoolboxenc.c @@ -1056,6 +1056,15 @@ static int vtenc_cm_to_avpacket( pts = CMSampleBufferGetPresentationTimeStamp(sample_buffer); dts = CMSampleBufferGetDecodeTimeStamp (sample_buffer); + if (CMTIME_IS_INVALID(dts)) { + if (!vtctx->has_b_frames) { + dts = pts; + } else { + av_log(avctx, AV_LOG_ERROR, "DTS is invalid.\n"); + return AVERROR_EXTERNAL; + } + } + dts_delta = vtctx->dts_delta >= 0 ? vtctx->dts_delta : 0; time_base_num = avctx->time_base.num; pkt->pts = pts.value / time_base_num; -- 2.7.4
Signed-off-by: Rick Kern <kernrj@gmail.com> --- libavcodec/videotoolboxenc.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c index 5594e69..073b87a 100644 --- a/libavcodec/videotoolboxenc.c +++ b/libavcodec/videotoolboxenc.c @@ -728,9 +728,12 @@ static av_cold int vtenc_init(AVCodecContext *avctx) CFNumberRef interval = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &avctx->gop_size); + if (!interval) return AVERROR(ENOMEM); + status = VTSessionSetProperty(vtctx->session, kVTCompressionPropertyKey_MaxKeyFrameInterval, interval); + CFRelease(interval); if (status) { av_log(avctx, AV_LOG_ERROR, "Error setting 'max key-frame interval' property: %d\n", status); -- 2.7.4
Rick Kern <kernrj <at> gmail.com> writes:
+ if (!interval) return AVERROR(ENOMEM);
Most people here believe that a line break makes this more readable, feel free to ignore though. Carl Eugen
Hint to the encoder that encoding should be done in real-time, even at the expense of quality. Signed-off-by: Rick Kern <kernrj@gmail.com> --- libavcodec/videotoolboxenc.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c index 073b87a..a30a3ca 100644 --- a/libavcodec/videotoolboxenc.c +++ b/libavcodec/videotoolboxenc.c @@ -76,6 +76,7 @@ typedef struct VTEncContext { int64_t profile; int64_t level; int64_t entropy; + int64_t realtime; #if !TARGET_OS_IPHONE int64_t allow_sw; @@ -767,6 +768,16 @@ static av_cold int vtenc_init(AVCodecContext *avctx) } } + if (vtctx->realtime) { + status = VTSessionSetProperty(vtctx->session, + kVTCompressionPropertyKey_RealTime, + kCFBooleanTrue); + + if (status) { + av_log(avctx, AV_LOG_ERROR, "Error setting realtime property: %d\n", status); + } + } + status = VTCompressionSessionPrepareToEncodeFrames(vtctx->session); if (status) { av_log(avctx, AV_LOG_ERROR, "Error: cannot prepare encoder: %d\n", status); @@ -1540,6 +1551,9 @@ static const AVOption options[] = { { "cavlc", "CAVLC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CAVLC }, INT_MIN, INT_MAX, VE, "entropy" }, { "cabac", "CABAC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CABAC }, INT_MIN, INT_MAX, VE, "entropy" }, + { "realtime", "Hint that encoding should happen in real-time if not faster (e.g. capturing from camera).", + OFFSET(realtime), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE }, + { NULL }, }; -- 2.7.4
Add frames_before and frames_after as hints that there will be frames before or after the frames produced in this session. This may help with concatenation issues like bit rate spikes. Signed-off-by: Rick Kern <kernrj@gmail.com> --- libavcodec/videotoolboxenc.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c index a30a3ca..c630825 100644 --- a/libavcodec/videotoolboxenc.c +++ b/libavcodec/videotoolboxenc.c @@ -77,6 +77,8 @@ typedef struct VTEncContext { int64_t level; int64_t entropy; int64_t realtime; + int64_t frames_before; + int64_t frames_after; #if !TARGET_OS_IPHONE int64_t allow_sw; @@ -742,6 +744,30 @@ static av_cold int vtenc_init(AVCodecContext *avctx) } } + if (vtctx->frames_before == 1) { + status = VTSessionSetProperty(vtctx->session, + kVTCompressionPropertyKey_MoreFramesBeforeStart, + kCFBooleanTrue); + + if (status == kVTPropertyNotSupportedErr) { + av_log(avctx, AV_LOG_WARNING, "frames_before property is not supported on this device. Ignoring.\n"); + } else if (status) { + av_log(avctx, AV_LOG_ERROR, "Error setting frames_before property: %d\n", status); + } + } + + if (vtctx->frames_after == 1) { + status = VTSessionSetProperty(vtctx->session, + kVTCompressionPropertyKey_MoreFramesAfterEnd, + kCFBooleanTrue); + + if (status == kVTPropertyNotSupportedErr) { + av_log(avctx, AV_LOG_WARNING, "frames_after property is not supported on this device. Ignoring.\n"); + } else if (status) { + av_log(avctx, AV_LOG_ERROR, "Error setting frames_after property: %d\n", status); + } + } + if (!vtctx->has_b_frames) { status = VTSessionSetProperty(vtctx->session, kVTCompressionPropertyKey_AllowFrameReordering, @@ -1553,6 +1579,10 @@ static const AVOption options[] = { { "realtime", "Hint that encoding should happen in real-time if not faster (e.g. capturing from camera).", OFFSET(realtime), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE }, + { "frames_before", "Other frames will come before the frames in this session. This helps smooth concatenation issues.", + OFFSET(frames_before), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE }, + { "frames_after", "Other frames will come after the frames in this session. This helps smooth concatenation issues.", + OFFSET(frames_after), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE }, { NULL }, }; -- 2.7.4
Rick Kern <kernrj <at> gmail.com> writes:
+ OFFSET(frames_before), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },
I would have expected this to be "0, 1", no? Carl Eugen
Setting AVFrame.pic_type to AV_PICTURE_TYPE_I will force an I-frame. Signed-off-by: Rick Kern <kernrj@gmail.com> --- libavcodec/videotoolboxenc.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c index c630825..5935d09 100644 --- a/libavcodec/videotoolboxenc.c +++ b/libavcodec/videotoolboxenc.c @@ -1434,27 +1434,51 @@ static int create_cv_pixel_buffer(AVCodecContext *avctx, return 0; } +static int create_encoder_dict_h264(const AVFrame *frame, + CFDictionaryRef* dict_out) +{ + CFDictionaryRef dict = NULL; + if (frame->pict_type == AV_PICTURE_TYPE_I) { + const void *keys[] = { kVTEncodeFrameOptionKey_ForceKeyFrame }; + const void *vals[] = { kCFBooleanTrue }; + + dict = CFDictionaryCreate(NULL, keys, vals, 1, NULL, NULL); + if(!dict) return AVERROR(ENOMEM); + } + + *dict_out = dict; + return 0; +} + static int vtenc_send_frame(AVCodecContext *avctx, VTEncContext *vtctx, const AVFrame *frame) { CMTime time; + CFDictionaryRef frame_dict; CVPixelBufferRef cv_img = NULL; int status = create_cv_pixel_buffer(avctx, frame, &cv_img); if (status) return status; + status = create_encoder_dict_h264(frame, &frame_dict); + if (status) { + CFRelease(cv_img); + return status; + } + time = CMTimeMake(frame->pts * avctx->time_base.num, avctx->time_base.den); status = VTCompressionSessionEncodeFrame( vtctx->session, cv_img, time, kCMTimeInvalid, - NULL, + frame_dict, NULL, NULL ); + if (frame_dict) CFRelease(frame_dict); CFRelease(cv_img); if (status) { -- 2.7.4
Signed-off-by: Rick Kern <kernrj@gmail.com> --- libavcodec/videotoolboxenc.c | 63 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c index 5935d09..768405d 100644 --- a/libavcodec/videotoolboxenc.c +++ b/libavcodec/videotoolboxenc.c @@ -768,6 +768,69 @@ static av_cold int vtenc_init(AVCodecContext *avctx) } } + if (avctx->sample_aspect_ratio.num != 0) { + CFNumberRef num; + CFNumberRef den; + CFMutableDictionaryRef par; + AVRational *avpar = &avctx->sample_aspect_ratio; + + av_reduce(&avpar->num, &avpar->den, + avpar->num, avpar->den, + 0xFFFFFFFF); + + num = CFNumberCreate(kCFAllocatorDefault, + kCFNumberIntType, + &avpar->num); + + den = CFNumberCreate(kCFAllocatorDefault, + kCFNumberIntType, + &avpar->den); + + + + par = CFDictionaryCreateMutable(kCFAllocatorDefault, + 2, + &kCFCopyStringDictionaryKeyCallBacks, + &kCFTypeDictionaryValueCallBacks); + + if (!par || !num || !den) { + if (par) CFRelease(par); + if (num) CFRelease(num); + if (den) CFRelease(den); + + return AVERROR(ENOMEM); + } + + CFDictionarySetValue( + par, + kCMFormatDescriptionKey_PixelAspectRatioHorizontalSpacing, + num); + + CFDictionarySetValue( + par, + kCMFormatDescriptionKey_PixelAspectRatioVerticalSpacing, + den); + + status = VTSessionSetProperty(vtctx->session, + kVTCompressionPropertyKey_PixelAspectRatio, + par); + + CFRelease(par); + CFRelease(num); + CFRelease(den); + + if (status) { + av_log(avctx, + AV_LOG_ERROR, + "Error setting pixel aspect ratio to %d:%d: %d.\n", + avctx->sample_aspect_ratio.num, + avctx->sample_aspect_ratio.den, + status); + + return AVERROR_EXTERNAL; + } + } + if (!vtctx->has_b_frames) { status = VTSessionSetProperty(vtctx->session, kVTCompressionPropertyKey_AllowFrameReordering, -- 2.7.4
Rick Kern <kernrj <at> gmail.com> writes:
+ if (status) { + av_log(avctx, + AV_LOG_ERROR, + "Error setting pixel aspect ratio to %d:%d: %d.\n", + avctx->sample_aspect_ratio.num, + avctx->sample_aspect_ratio.den, + status); + + return AVERROR_EXTERNAL;
Why is it necessary to quit here? Especially since we don't know what aspects osx accepts. Carl Eugen
Signed-off-by: Rick Kern <kernrj@gmail.com> --- configure | 5 +- libavcodec/videotoolboxenc.c | 209 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 213 insertions(+), 1 deletion(-) diff --git a/configure b/configure index 97f374b..c484a10 100755 --- a/configure +++ b/configure @@ -2078,6 +2078,7 @@ CONFIG_EXTRA=" vp3dsp vp56dsp vp8dsp + vt_bt2020 wma_freqs wmv2dsp " @@ -2740,6 +2741,7 @@ videotoolbox_deps="VideoToolbox_VideoToolbox_h" videotoolbox_extralibs="-framework CoreFoundation -framework VideoToolbox -framework CoreMedia -framework CoreVideo" videotoolbox_encoder_deps="videotoolbox VTCompressionSessionPrepareToEncodeFrames" videotoolbox_encoder_suggest="vda_framework" +vt_bt2020_deps="kCVImageBufferColorPrimaries_ITU_R_2020" # demuxers / muxers ac3_demuxer_select="ac3_parser" @@ -3128,7 +3130,7 @@ enable audiotoolbox enable d3d11va dxva2 vaapi vda vdpau videotoolbox_hwaccel xvmc enable xlib -enable vda_framework videotoolbox videotoolbox_encoder +enable vda_framework videotoolbox videotoolbox_encoder vt_bt2020 # build settings SHFLAGS='-shared -Wl,-soname,$$(@F)' @@ -5441,6 +5443,7 @@ check_header vdpau/vdpau_x11.h check_header VideoDecodeAcceleration/VDADecoder.h check_header VideoToolbox/VideoToolbox.h check_func_headers VideoToolbox/VTCompressionSession.h VTCompressionSessionPrepareToEncodeFrames -framework VideoToolbox +check_func_headers CoreVideo/CVImageBuffer.h kCVImageBufferColorPrimaries_ITU_R_2020 -framework CoreVideo check_header windows.h check_header X11/extensions/XvMClib.h check_header asm/types.h diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c index 768405d..1201004 100644 --- a/libavcodec/videotoolboxenc.c +++ b/libavcodec/videotoolboxenc.c @@ -33,6 +33,12 @@ #include "internal.h" #include <pthread.h> +#if !CONFIG_VT_BT2020 +# define kCVImageBufferColorPrimaries_ITU_R_2020 CFSTR("ITU_R_2020") +# define kCVImageBufferTransferFunction_ITU_R_2020 CFSTR("ITU_R_2020") +# define kCVImageBufferYCbCrMatrix_ITU_R_2020 CFSTR("ITU_R_2020") +#endif + typedef enum VT_H264Profile { H264_PROF_AUTO, H264_PROF_BASELINE, @@ -58,6 +64,9 @@ typedef struct BufNode { typedef struct VTEncContext { AVClass *class; VTCompressionSessionRef session; + CFStringRef ycbcr_matrix; + CFStringRef color_primaries; + CFStringRef transfer_function; pthread_mutex_t lock; pthread_cond_t cv_sample_sent; @@ -549,6 +558,28 @@ static int get_cv_pixel_format( return 0; } +static void add_color_attr(AVCodecContext *avctx, CFMutableDictionaryRef dict) { + VTEncContext *vtctx = avctx->priv_data; + + if (vtctx->color_primaries) { + CFDictionarySetValue(dict, + kCVImageBufferColorPrimariesKey, + vtctx->color_primaries); + } + + if (vtctx->transfer_function) { + CFDictionarySetValue(dict, + kCVImageBufferTransferFunctionKey, + vtctx->transfer_function); + } + + if (vtctx->ycbcr_matrix) { + CFDictionarySetValue(dict, + kCVImageBufferYCbCrMatrixKey, + vtctx->ycbcr_matrix); + } +} + static int create_cv_pixel_buffer_info(AVCodecContext* avctx, CFMutableDictionaryRef* dict) { @@ -602,6 +633,8 @@ static int create_cv_pixel_buffer_info(AVCodecContext* avctx, height_num); vt_release_num(&height_num); + add_color_attr(avctx, pixel_buffer_info); + *dict = pixel_buffer_info; return 0; @@ -614,6 +647,110 @@ pbinfo_nomem: return AVERROR(ENOMEM); } +static int get_cv_color_primaries(AVCodecContext *avctx, + CFStringRef *primaries) +{ + enum AVColorPrimaries pri = avctx->color_primaries; + switch (pri) { + case AVCOL_PRI_UNSPECIFIED: + *primaries = NULL; + break; + + case AVCOL_PRI_BT709: + *primaries = kCVImageBufferColorPrimaries_ITU_R_709_2; + break; + + case AVCOL_PRI_BT2020: + *primaries = kCVImageBufferColorPrimaries_ITU_R_2020; + break; + + default: + av_log(avctx, AV_LOG_ERROR, "Color primaries %s is not supported.\n", av_color_primaries_name(pri)); + *primaries = NULL; + return -1; + } + + return 0; +} + +static int get_cv_transfer_function(AVCodecContext *avctx, + CFStringRef *transfer_fnc, + CFNumberRef *gamma_level) +{ + enum AVColorTransferCharacteristic trc = avctx->color_trc; + Float32 gamma; + *gamma_level = NULL; + + switch (trc) { + case AVCOL_TRC_UNSPECIFIED: + *transfer_fnc = NULL; + break; + + case AVCOL_TRC_BT709: + *transfer_fnc = kCVImageBufferTransferFunction_ITU_R_709_2; + break; + + case AVCOL_TRC_SMPTE240M: + *transfer_fnc = kCVImageBufferTransferFunction_SMPTE_240M_1995; + break; + + case AVCOL_TRC_GAMMA22: + gamma = 2.2; + *transfer_fnc = kCVImageBufferTransferFunction_UseGamma; + *gamma_level = CFNumberCreate(NULL, kCFNumberFloat32Type, &gamma); + break; + + case AVCOL_TRC_GAMMA28: + gamma = 2.8; + *transfer_fnc = kCVImageBufferTransferFunction_UseGamma; + *gamma_level = CFNumberCreate(NULL, kCFNumberFloat32Type, &gamma); + break; + + case AVCOL_TRC_BT2020_10: + case AVCOL_TRC_BT2020_12: + *transfer_fnc = kCVImageBufferTransferFunction_ITU_R_2020; + break; + + default: + av_log(avctx, AV_LOG_ERROR, "Transfer function %s is not supported.\n", av_color_transfer_name(trc)); + return -1; + } + + return 0; +} + +static int get_cv_ycbcr_matrix(AVCodecContext *avctx, CFStringRef *matrix) { + switch(avctx->colorspace) { + case AVCOL_SPC_BT709: + *matrix = kCVImageBufferYCbCrMatrix_ITU_R_709_2; + break; + + case AVCOL_SPC_UNSPECIFIED: + *matrix = NULL; + break; + + case AVCOL_SPC_BT470BG: + case AVCOL_SPC_SMPTE170M: + *matrix = kCVImageBufferYCbCrMatrix_ITU_R_601_4; + break; + + case AVCOL_SPC_SMPTE240M: + *matrix = kCVImageBufferYCbCrMatrix_SMPTE_240M_1995; + break; + + case AVCOL_SPC_BT2020_NCL: + *matrix = kCVImageBufferYCbCrMatrix_ITU_R_2020; + break; + + default: + av_log(avctx, AV_LOG_ERROR, "Color space %s is not supported.\n", av_color_space_name(avctx->colorspace)); + return -1; + } + + return 0; +} + + static av_cold int vtenc_init(AVCodecContext *avctx) { CFMutableDictionaryRef enc_info; @@ -624,6 +761,7 @@ static av_cold int vtenc_init(AVCodecContext *avctx) SInt32 bit_rate = avctx->bit_rate; CFNumberRef bit_rate_num; CFBooleanRef has_b_frames_cfbool; + CFNumberRef gamma_level; int status; codec_type = get_cm_codec_type(avctx->codec_id); @@ -831,6 +969,49 @@ static av_cold int vtenc_init(AVCodecContext *avctx) } } + status = get_cv_transfer_function(avctx, &vtctx->transfer_function, &gamma_level); + if (!status && vtctx->transfer_function) { + status = VTSessionSetProperty(vtctx->session, + kVTCompressionPropertyKey_TransferFunction, + vtctx->transfer_function); + + if (status) { + av_log(avctx, AV_LOG_ERROR, "Error setting transfer function: %d\n", status); + } + } + + status = get_cv_ycbcr_matrix(avctx, &vtctx->ycbcr_matrix); + if (!status && vtctx->ycbcr_matrix) { + status = VTSessionSetProperty(vtctx->session, + kVTCompressionPropertyKey_YCbCrMatrix, + vtctx->ycbcr_matrix); + + if (status) { + av_log(avctx, AV_LOG_ERROR, "Error setting ycbcr matrix: %d\n", status); + } + } + + status = get_cv_color_primaries(avctx, &vtctx->color_primaries); + if (!status && vtctx->color_primaries) { + status = VTSessionSetProperty(vtctx->session, + kVTCompressionPropertyKey_ColorPrimaries, + vtctx->color_primaries); + + if (status) { + av_log(avctx, AV_LOG_ERROR, "Error setting color primaries: %d\n", status); + } + } + + if (!status && gamma_level) { + status = VTSessionSetProperty(vtctx->session, + kCVImageBufferGammaLevelKey, + gamma_level); + + if (status) { + av_log(avctx, AV_LOG_ERROR, "Error setting gamma level: %d\n", status); + } + } + if (!vtctx->has_b_frames) { status = VTSessionSetProperty(vtctx->session, kVTCompressionPropertyKey_AllowFrameReordering, @@ -1395,9 +1576,18 @@ static int create_cv_pixel_buffer(AVCodecContext *avctx, size_t strides[AV_NUM_DATA_POINTERS]; int status; size_t contiguous_buf_size; +#if TARGET_OS_IPHONE CVPixelBufferPoolRef pix_buf_pool; VTEncContext* vtctx = avctx->priv_data; +#else + CFMutableDictionaryRef pix_buf_attachments = CFDictionaryCreateMutable( + kCFAllocatorDefault, + 10, + &kCFCopyStringDictionaryKeyCallBacks, + &kCFTypeDictionaryValueCallBacks); + if (!pix_buf_attachments) return AVERROR(ENOMEM); +#endif if (avctx->pix_fmt == AV_PIX_FMT_VIDEOTOOLBOX) { av_assert0(frame->format == AV_PIX_FMT_VIDEOTOOLBOX); @@ -1488,6 +1678,10 @@ static int create_cv_pixel_buffer(AVCodecContext *avctx, cv_img ); + add_color_attr(avctx, pix_buf_attachments); + CVBufferSetAttachments(*cv_img, pix_buf_attachments, kCVAttachmentMode_ShouldPropagate); + CFRelease(pix_buf_attachments); + if (status) { av_log(avctx, AV_LOG_ERROR, "Error: Could not create CVPixelBuffer: %d\n", status); return AVERROR_EXTERNAL; @@ -1625,6 +1819,21 @@ static av_cold int vtenc_close(AVCodecContext *avctx) CFRelease(vtctx->session); vtctx->session = NULL; + if (vtctx->color_primaries) { + CFRelease(vtctx->color_primaries); + vtctx->color_primaries = NULL; + } + + if (vtctx->transfer_function) { + CFRelease(vtctx->transfer_function); + vtctx->transfer_function = NULL; + } + + if (vtctx->ycbcr_matrix) { + CFRelease(vtctx->ycbcr_matrix); + vtctx->ycbcr_matrix = NULL; + } + return 0; } -- 2.7.4
Rick Kern <kernrj <at> gmail.com> writes:
+check_func_headers CoreVideo/CVImageBuffer.h kCVImageBufferColorPrimaries_ITU_R_2020
I would have expected "enabled videotoolbox && check_func_headers" which should also make the changes to the "enable vda..." above unneded, but I may miss something.
+ if (status) { + av_log(avctx, AV_LOG_ERROR, "Error setting gamma level: %d\n", status);
Sounds like a "WARNING" to me... Carl Eugen
participants (5)
-
Carl Eugen Hoyos -
crossle song -
Hendrik Leppkes -
Richard Kern -
Rick Kern