[FFmpeg-devel] [PATCH] lavc/videotoolboxenc: support additional options

Marvin Scholz epirat07 at gmail.com
Sun May 21 02:16:08 EEST 2023



On 21 May 2023, at 0:24, Rick Kern wrote:

> 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) {

Nit: These should probably be aligned with the opening brace,
like other functions do in this file.

> +    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) {

Nit: These should probably be aligned with the opening brace,
like other functions do in this file.

> +    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;
> +}

You could avoid the repetition here by having a separate function that
takes a CFTypeRef and does the set + error handling + log part.

> +
>  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 },

Nit: This should be formatted like the other entries, IMO.

>      COMMON_OPTIONS
>      { NULL },
>  };
> -- 
> 2.38.1
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request at ffmpeg.org with subject "unsubscribe".


More information about the ffmpeg-devel mailing list