From: Rick Kern <kernrj@gmail.com> To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] [PATCH] lavc/videotoolboxenc: support additional options Date: Sat, 20 May 2023 18:24:22 -0400 Message-ID: <20230520222422.68633-1-kernrj@gmail.com> (raw) Added support for more VideoToolbox encoder options: - qmin and qmax options are now used - max_slice_bytes: Max number of bytes per H.264 slice - max_ref_frames: Limit the number of reference frames - Disable open GOP when the cgop flag is set - power_efficient: Enable power-efficient mode --- libavcodec/videotoolboxenc.c | 177 ++++++++++++++++++++++++++++++++++- 1 file changed, 176 insertions(+), 1 deletion(-) diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c index b017c90c36..be29d13629 100644 --- a/libavcodec/videotoolboxenc.c +++ b/libavcodec/videotoolboxenc.c @@ -120,6 +120,11 @@ static struct{ CFStringRef kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder; CFStringRef kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder; CFStringRef kVTVideoEncoderSpecification_EnableLowLatencyRateControl; + CFStringRef kVTCompressionPropertyKey_AllowOpenGOP; + CFStringRef kVTCompressionPropertyKey_MaximizePowerEfficiency; + CFStringRef kVTCompressionPropertyKey_ReferenceBufferCount; + CFStringRef kVTCompressionPropertyKey_MaxAllowedFrameQP; + CFStringRef kVTCompressionPropertyKey_MinAllowedFrameQP; getParameterSetAtIndex CMVideoFormatDescriptionGetHEVCParameterSetAtIndex; } compat_keys; @@ -188,6 +193,13 @@ static void loadVTEncSymbols(void){ "RequireHardwareAcceleratedVideoEncoder"); GET_SYM(kVTVideoEncoderSpecification_EnableLowLatencyRateControl, "EnableLowLatencyRateControl"); + GET_SYM(kVTCompressionPropertyKey_AllowOpenGOP, "AllowOpenGOP"); + GET_SYM(kVTCompressionPropertyKey_MaximizePowerEfficiency, + "MaximizePowerEfficiency"); + GET_SYM(kVTCompressionPropertyKey_ReferenceBufferCount, + "ReferenceBufferCount"); + GET_SYM(kVTCompressionPropertyKey_MaxAllowedFrameQP, "MaxAllowedFrameQP"); + GET_SYM(kVTCompressionPropertyKey_MinAllowedFrameQP, "MinAllowedFrameQP"); } typedef enum VT_H264Profile { @@ -268,6 +280,10 @@ typedef struct VTEncContext { /* can't be bool type since AVOption will access it as int */ int a53_cc; + + int max_slice_bytes; + int power_efficient; + int max_ref_frames; } VTEncContext; static int vtenc_populate_extradata(AVCodecContext *avctx, @@ -1106,6 +1122,78 @@ static bool vtenc_qscale_enabled(void) return !TARGET_OS_IPHONE && TARGET_CPU_ARM64; } +static int set_encoder_int_property_or_log( + AVCodecContext* avctx, + CFStringRef key, + const char* print_option_name, + int value) { + int status; + VTEncContext *vtctx = avctx->priv_data; + CFNumberRef value_cfnum = CFNumberCreate( + kCFAllocatorDefault, + kCFNumberIntType, + &value); + + if (value_cfnum == NULL) { + return AVERROR(ENOMEM); + } + + status = VTSessionSetProperty(vtctx->session, key, value_cfnum); + if (status == kVTPropertyNotSupportedErr) { + av_log( + avctx, + AV_LOG_INFO, + "This device does not support the %s option. Value %d ignored.\n", + print_option_name, + value); + } else if (status != 0) { + av_log( + avctx, + AV_LOG_ERROR, + "Error setting %s=%d: Error %d\n", + print_option_name, + value, + status); + } + + CFRelease (value_cfnum); + + return 0; +} + +static int set_encoder_bool_property_or_log( + AVCodecContext* avctx, + CFStringRef key, + const char* print_option_name, + int value) { + int status; + VTEncContext *vtctx = avctx->priv_data; + + status = VTSessionSetProperty( + vtctx->session, + key, + value ? kCFBooleanTrue : kCFBooleanFalse); + + if (status == kVTPropertyNotSupportedErr) { + av_log( + avctx, + AV_LOG_INFO, + "This device does not support the %s option. Value %d ignored.\n", + print_option_name, + value); + } else if (status != 0) { + av_log( + avctx, + AV_LOG_ERROR, + "Error setting %s=%d: Error %d\n", + print_option_name, + value, + status); + } + + return 0; +} + static int vtenc_create_encoder(AVCodecContext *avctx, CMVideoCodecType codec_type, CFStringRef profile_level, @@ -1476,6 +1564,81 @@ static int vtenc_create_encoder(AVCodecContext *avctx, } } + if ((avctx->flags & AV_CODEC_FLAG_CLOSED_GOP) != 0) { + set_encoder_bool_property_or_log( + avctx, + compat_keys.kVTCompressionPropertyKey_AllowOpenGOP, + "AllowOpenGop", + 0); + + if (status) { + return status; + } + } + + if (avctx->qmin >= 0) { + status = set_encoder_int_property_or_log( + avctx, + compat_keys.kVTCompressionPropertyKey_MinAllowedFrameQP, + "qmin", + avctx->qmin); + + if (status != 0) { + return status; + } + } + + if (avctx->qmax >= 0) { + status = set_encoder_int_property_or_log( + avctx, + compat_keys.kVTCompressionPropertyKey_MaxAllowedFrameQP, + "qmax", + avctx->qmax); + + if (status != 0) { + return status; + } + } + + if (vtctx->max_slice_bytes >= 0 && avctx->codec_id == AV_CODEC_ID_H264) { + status = set_encoder_int_property_or_log( + avctx, + kVTCompressionPropertyKey_MaxH264SliceBytes, + "max_slice_bytes", + vtctx->max_slice_bytes); + + if (status != 0) { + return status; + } + } + + if (vtctx->power_efficient >= 0) { + status = VTSessionSetProperty( + vtctx->session, + compat_keys.kVTCompressionPropertyKey_MaximizePowerEfficiency, + vtctx->power_efficient ? kCFBooleanTrue : kCFBooleanFalse); + + if (status) { + av_log( + avctx, + AV_LOG_ERROR, + "Error setting power_efficient property: %d\n", + status); + } + } + + if (vtctx->max_ref_frames > 0) { + status = set_encoder_int_property_or_log( + avctx, + compat_keys.kVTCompressionPropertyKey_ReferenceBufferCount, + "max_ref_frames", + vtctx->max_ref_frames); + + if (status != 0) { + return status; + } + } + status = VTCompressionSessionPrepareToEncodeFrames(vtctx->session); if (status) { av_log(avctx, AV_LOG_ERROR, "Error: cannot prepare encoder: %d\n", status); @@ -2753,6 +2916,11 @@ static const enum AVPixelFormat prores_pix_fmts[] = { OFFSET(frames_after), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \ { "prio_speed", "prioritize encoding speed", OFFSET(prio_speed), AV_OPT_TYPE_BOOL, \ { .i64 = -1 }, -1, 1, VE }, \ + { "power_efficient", "Set to 1 to enable more power-efficient encoding if supported.", \ + OFFSET(power_efficient), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE }, \ + { "max_ref_frames", \ + "Sets the maximum number of reference frames. This only has an effect when the value is less than the maximum allowed by the profile/level.", \ + OFFSET(max_ref_frames), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE }, #define OFFSET(x) offsetof(VTEncContext, x) static const AVOption h264_options[] = { @@ -2783,7 +2951,14 @@ static const AVOption h264_options[] = { { "a53cc", "Use A53 Closed Captions (if available)", OFFSET(a53_cc), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, VE }, { "constant_bit_rate", "Require constant bit rate (macOS 13 or newer)", OFFSET(constant_bit_rate), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, - + { "max_slice_bytes", + "Set the maximum number of bytes in an H.264 slice.", + OFFSET(max_slice_bytes), + AV_OPT_TYPE_INT, + { .i64 = -1 }, + -1, + INT_MAX, + VE }, COMMON_OPTIONS { NULL }, }; -- 2.38.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
next reply other threads:[~2023-05-20 22:24 UTC|newest] Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top 2023-05-20 22:24 Rick Kern [this message] 2023-05-20 23:16 ` Marvin Scholz
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20230520222422.68633-1-kernrj@gmail.com \ --to=kernrj@gmail.com \ --cc=ffmpeg-devel@ffmpeg.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel This inbox may be cloned and mirrored by anyone: git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \ ffmpegdev@gitmailbox.com public-inbox-index ffmpegdev Example config snippet for mirrors. AGPL code for this site: git clone https://public-inbox.org/public-inbox.git