[FFmpeg-devel] [PATCH v2] avcodec/videotoolboxenc: Add CBR option to H264 and HEVC encoder

Sebastian Beckmann beckmann.sebastian at outlook.de
Fri Aug 26 15:34:26 EEST 2022


Adds an option to use constant bitrate instead of average bitrate to the
videotoolbox encoders. This is enabled via -constant_bit_rate true.
macOS 13 is required for this option to work.

Signed-off-by: Sebastian Beckmann <beckmann.sebastian at outlook.de>
---
Changes the name of vtenc_constant_bit_rate_enabled to
vtenc_constant_bit_rate_available to clarify that this checks for
availablity of the feature.

libavcodec/videotoolboxenc.c | 37 +++++++++++++++++++++++++++++++++---
1 file changed, 34 insertions(+), 3 deletions(-)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 823e5ad94e..18d68360ef 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -101,6 +101,7 @@ static struct{
    CFStringRef kVTCompressionPropertyKey_RealTime;
    CFStringRef kVTCompressionPropertyKey_TargetQualityForAlpha;
    CFStringRef kVTCompressionPropertyKey_PrioritizeEncodingSpeedOverQuality;
+    CFStringRef kVTCompressionPropertyKey_ConstantBitRate;

    CFStringRef kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder;
    CFStringRef kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder;
@@ -164,6 +165,7 @@ static void loadVTEncSymbols(){
            "TargetQualityForAlpha");
    GET_SYM(kVTCompressionPropertyKey_PrioritizeEncodingSpeedOverQuality,
            "PrioritizeEncodingSpeedOverQuality");
+    GET_SYM(kVTCompressionPropertyKey_ConstantBitRate, "ConstantBitRate");

    GET_SYM(kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,
            "EnableHardwareAcceleratedVideoEncoder");
@@ -236,6 +238,7 @@ typedef struct VTEncContext {
    int realtime;
    int frames_before;
    int frames_after;
+    bool constant_bit_rate;

    int allow_sw;
    int require_sw;
@@ -1073,12 +1076,22 @@ static bool vtenc_qscale_enabled(void)
    return !TARGET_OS_IPHONE && TARGET_CPU_ARM64;
}

+// constant bit rate only on Macs with Apple Silicon running macOS 13 (Ventura) or newer
+static bool vtenc_constant_bit_rate_available(void)
+{
+    if (__builtin_available(macOS 13, *))
+        return !TARGET_OS_IPHONE && TARGET_CPU_ARM64;
+    else
+        return false;
+}
+
static int vtenc_create_encoder(AVCodecContext   *avctx,
                                CMVideoCodecType codec_type,
                                CFStringRef      profile_level,
                                CFNumberRef      gamma_level,
                                CFDictionaryRef  enc_info,
                                CFDictionaryRef  pixel_buffer_info,
+                                bool constant_bit_rate,
                                VTCompressionSessionRef *session)
{
    VTEncContext *vtctx = avctx->priv_data;
@@ -1122,6 +1135,11 @@ static int vtenc_create_encoder(AVCodecContext   *avctx,
        return AVERROR_EXTERNAL;
    }

+    if (constant_bit_rate && !vtenc_constant_bit_rate_available()) {
+        av_log(avctx, AV_LOG_ERROR, "Error: -constant_bit_rate true not available for encoder.\n");
+        return AVERROR_EXTERNAL;
+    }
+
    if (avctx->flags & AV_CODEC_FLAG_QSCALE) {
        quality = quality >= 100 ? 1.0 : quality / 100;
        quality_num = CFNumberCreate(kCFAllocatorDefault,
@@ -1139,9 +1157,16 @@ static int vtenc_create_encoder(AVCodecContext   *avctx,
                                      &bit_rate);
        if (!bit_rate_num) return AVERROR(ENOMEM);

-        status = VTSessionSetProperty(vtctx->session,
-                                      kVTCompressionPropertyKey_AverageBitRate,
-                                      bit_rate_num);
+        if (constant_bit_rate) {
+            status = VTSessionSetProperty(vtctx->session,
+                                          compat_keys.kVTCompressionPropertyKey_ConstantBitRate,
+                                          bit_rate_num);
+        } else {
+            status = VTSessionSetProperty(vtctx->session,
+                                          kVTCompressionPropertyKey_AverageBitRate,
+                                          bit_rate_num);
+        }
+
        CFRelease(bit_rate_num);
    }

@@ -1530,6 +1555,7 @@ static int vtenc_configure_encoder(AVCodecContext *avctx)
                                  gamma_level,
                                  enc_info,
                                  pixel_buffer_info,
+                                  vtctx->constant_bit_rate,
                                  &vtctx->session);

init_cleanup:
@@ -2532,6 +2558,7 @@ static int vtenc_populate_extradata(AVCodecContext   *avctx,
                                  gamma_level,
                                  enc_info,
                                  pixel_buffer_info,
+                                  vtctx->constant_bit_rate,
                                  &vtctx->session);
    if (status)
        goto pe_cleanup;
@@ -2727,6 +2754,8 @@ 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 },
+
    COMMON_OPTIONS
    { NULL },
};
@@ -2760,6 +2789,8 @@ static const AVOption hevc_options[] = {

    { "alpha_quality", "Compression quality for the alpha channel", OFFSET(alpha_quality), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0.0, 1.0, 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 },
+
    COMMON_OPTIONS
    { NULL },
};
-- 
2.37.0 (Apple Git-136)



More information about the ffmpeg-devel mailing list