Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH] avcodec/videotoolboxenc: Add CBR option to H264 and HEVC encoder
@ 2022-08-26 11:42 Sebastian Beckmann
  2022-08-26 11:45 ` Diederick C. Niehorster
  2022-08-28 18:37 ` [FFmpeg-devel] [PATCH] " Rick Kern
  0 siblings, 2 replies; 8+ messages in thread
From: Sebastian Beckmann @ 2022-08-26 11:42 UTC (permalink / raw)
  To: ffmpeg-devel

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@outlook.de>
---
libavcodec/videotoolboxenc.c | 37 +++++++++++++++++++++++++++++++++---
1 file changed, 34 insertions(+), 3 deletions(-)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 823e5ad94e..9eb6fe09a2 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_enabled(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_enabled()) {
+        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)

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [FFmpeg-devel] [PATCH] avcodec/videotoolboxenc: Add CBR option to H264 and HEVC encoder
  2022-08-26 11:42 [FFmpeg-devel] [PATCH] avcodec/videotoolboxenc: Add CBR option to H264 and HEVC encoder Sebastian Beckmann
@ 2022-08-26 11:45 ` Diederick C. Niehorster
  2022-08-26 12:34   ` [FFmpeg-devel] [PATCH v2] " Sebastian Beckmann
  2022-08-28 18:37 ` [FFmpeg-devel] [PATCH] " Rick Kern
  1 sibling, 1 reply; 8+ messages in thread
From: Diederick C. Niehorster @ 2022-08-26 11:45 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

On Fri, Aug 26, 2022 at 1:42 PM Sebastian Beckmann
<beckmann.sebastian@outlook.de> wrote:
>
> 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@outlook.de>
> ---
> libavcodec/videotoolboxenc.c | 37 +++++++++++++++++++++++++++++++++---
> 1 file changed, 34 insertions(+), 3 deletions(-)
>
> diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
> index 823e5ad94e..9eb6fe09a2 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_enabled(void)
> +{
> +    if (__builtin_available(macOS 13, *))
> +        return !TARGET_OS_IPHONE && TARGET_CPU_ARM64;
> +    else
> +        return false;
> +}

vtenc_constant_bit_rate_available() may be a better name for this
function, its the user that enables it through the flag you added

> 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_enabled()) {
> +        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)
>
> _______________________________________________
> 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".
_______________________________________________
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".

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [FFmpeg-devel] [PATCH v2] avcodec/videotoolboxenc: Add CBR option to H264 and HEVC encoder
  2022-08-26 11:45 ` Diederick C. Niehorster
@ 2022-08-26 12:34   ` Sebastian Beckmann
  0 siblings, 0 replies; 8+ messages in thread
From: Sebastian Beckmann @ 2022-08-26 12:34 UTC (permalink / raw)
  To: ffmpeg-devel

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

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [FFmpeg-devel] [PATCH] avcodec/videotoolboxenc: Add CBR option to H264 and HEVC encoder
  2022-08-26 11:42 [FFmpeg-devel] [PATCH] avcodec/videotoolboxenc: Add CBR option to H264 and HEVC encoder Sebastian Beckmann
  2022-08-26 11:45 ` Diederick C. Niehorster
@ 2022-08-28 18:37 ` Rick Kern
  2022-08-28 20:27   ` [FFmpeg-devel] [PATCH v3] " Sebastian Beckmann
  1 sibling, 1 reply; 8+ messages in thread
From: Rick Kern @ 2022-08-28 18:37 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

On Fri, Aug 26, 2022 at 7:42 AM Sebastian Beckmann <
beckmann.sebastian@outlook.de> wrote:

> 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@outlook.de>
> ---
> libavcodec/videotoolboxenc.c | 37 +++++++++++++++++++++++++++++++++---
> 1 file changed, 34 insertions(+), 3 deletions(-)
>
> diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
> index 823e5ad94e..9eb6fe09a2 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_enabled(void)
>

It's possible that Apple adds support for CBR on iOS or Intel-based Macs in
the future, so it would be more future-proof to avoid hard-coding here.
Then you could always try to set the property, and print an error message
if it returns an error.  See PrioritizeEncodingSpeedOverQuality for an
example.

I see the same hard-coded logic is used in vtenc_qscale_enabled(). Thanks
for following the patterns in this file, because that's usually the right
thing to do. However, the qscale issue slipped through the cracks and also
needs to be changed.


> +{
> +    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_enabled()) {
> +        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);
>

For example, you could check status here, and output a message about no CBR
support.


> +        } 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)
>
> _______________________________________________
> 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".
>
_______________________________________________
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".

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [FFmpeg-devel] [PATCH v3] avcodec/videotoolboxenc: Add CBR option to H264 and HEVC encoder
  2022-08-28 18:37 ` [FFmpeg-devel] [PATCH] " Rick Kern
@ 2022-08-28 20:27   ` Sebastian Beckmann
  2022-08-29 12:58     ` Rick Kern
  0 siblings, 1 reply; 8+ messages in thread
From: Sebastian Beckmann @ 2022-08-28 20:27 UTC (permalink / raw)
  To: ffmpeg-devel

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@outlook.de>
---
Removed the hard-coded check for Apple Silicon CPUs.

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

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 823e5ad94e..9de86cc4d9 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;
@@ -1079,6 +1082,7 @@ static int vtenc_create_encoder(AVCodecContext   *avctx,
                                CFNumberRef      gamma_level,
                                CFDictionaryRef  enc_info,
                                CFDictionaryRef  pixel_buffer_info,
+                                bool constant_bit_rate,
                                VTCompressionSessionRef *session)
{
    VTEncContext *vtctx = avctx->priv_data;
@@ -1139,9 +1143,20 @@ 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);
+            if (status == kVTPropertyNotSupportedErr) {
+                av_log(avctx, AV_LOG_ERROR, "Error: -constant_bit_rate true is not supported by the encoder.\n");
+                return AVERROR_EXTERNAL;
+            }
+        } else {
+            status = VTSessionSetProperty(vtctx->session,
+                                          kVTCompressionPropertyKey_AverageBitRate,
+                                          bit_rate_num);
+        }
+
        CFRelease(bit_rate_num);
    }

@@ -1530,6 +1545,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 +2548,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 +2744,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 +2779,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)

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [FFmpeg-devel] [PATCH v3] avcodec/videotoolboxenc: Add CBR option to H264 and HEVC encoder
  2022-08-28 20:27   ` [FFmpeg-devel] [PATCH v3] " Sebastian Beckmann
@ 2022-08-29 12:58     ` Rick Kern
  2022-08-29 13:04       ` Sebastian Beckmann
  0 siblings, 1 reply; 8+ messages in thread
From: Rick Kern @ 2022-08-29 12:58 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

On Sun, Aug 28, 2022 at 4:27 PM Sebastian Beckmann <
beckmann.sebastian@outlook.de> wrote:

> 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@outlook.de>
> ---
> Removed the hard-coded check for Apple Silicon CPUs.
>
> libavcodec/videotoolboxenc.c | 27 ++++++++++++++++++++++++---
> 1 file changed, 24 insertions(+), 3 deletions(-)
>
> diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
> index 823e5ad94e..9de86cc4d9 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;
> @@ -1079,6 +1082,7 @@ static int vtenc_create_encoder(AVCodecContext
>  *avctx,
>                                 CFNumberRef      gamma_level,
>                                 CFDictionaryRef  enc_info,
>                                 CFDictionaryRef  pixel_buffer_info,
> +                                bool constant_bit_rate,
>                                 VTCompressionSessionRef *session)
> {
>     VTEncContext *vtctx = avctx->priv_data;
> @@ -1139,9 +1143,20 @@ 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);
> +            if (status == kVTPropertyNotSupportedErr) {
> +                av_log(avctx, AV_LOG_ERROR, "Error: -constant_bit_rate
> true is not supported by the encoder.\n");
> +                return AVERROR_EXTERNAL;
> +            }
> +        } else {
> +            status = VTSessionSetProperty(vtctx->session,
> +
> kVTCompressionPropertyKey_AverageBitRate,
> +                                          bit_rate_num);
> +        }
> +
>         CFRelease(bit_rate_num);
>     }
>
> @@ -1530,6 +1545,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 +2548,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 +2744,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 +2779,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)
>

The patch doesn't apply. Can you create a .patch file with git format-patch
and send the file as an attachment? Sometimes email clients/providers
corrupt inline patches.

The error message is:

git am ~/Downloads/\[FFmpeg-devel\]\ \[PATCH\ v3\]\
avcodec_videotoolboxenc_\ Add\ CBR\ option\ to\ H264\ and\ HEVC\ encoder.eml

Applying: avcodec/videotoolboxenc: Add CBR option to H264 and HEVC encoder

error: corrupt patch at line 41

Patch failed at 0001 avcodec/videotoolboxenc: Add CBR option to H264 and
HEVC encoder

hint: Use 'git am --show-current-patch' to see the failed patch

When you have resolved this problem, run "git am --continue".

If you prefer to skip this patch, run "git am --skip" instead.

To restore the original branch and stop patching, run "git am --abort".




>
> _______________________________________________
> 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".
>
_______________________________________________
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".

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [FFmpeg-devel] [PATCH v3] avcodec/videotoolboxenc: Add CBR option to H264 and HEVC encoder
  2022-08-29 12:58     ` Rick Kern
@ 2022-08-29 13:04       ` Sebastian Beckmann
  2022-08-29 13:36         ` Rick Kern
  0 siblings, 1 reply; 8+ messages in thread
From: Sebastian Beckmann @ 2022-08-29 13:04 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

[-- Attachment #1: Type: text/plain, Size: 183 bytes --]


> The patch doesn't apply. Can you create a .patch file with git format-patch
> and send the file as an attachment? Sometimes email clients/providers
> corrupt inline patches.


[-- Attachment #2: v3-0001-avcodec-videotoolboxenc-Add-CBR-option-to-H264-an.patch --]
[-- Type: application/octet-stream, Size: 5006 bytes --]

[-- Attachment #3: Type: text/plain, Size: 251 bytes --]

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [FFmpeg-devel] [PATCH v3] avcodec/videotoolboxenc: Add CBR option to H264 and HEVC encoder
  2022-08-29 13:04       ` Sebastian Beckmann
@ 2022-08-29 13:36         ` Rick Kern
  0 siblings, 0 replies; 8+ messages in thread
From: Rick Kern @ 2022-08-29 13:36 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

On Mon, Aug 29, 2022 at 9:05 AM Sebastian Beckmann <
beckmann.sebastian@outlook.de> wrote:

>
> > The patch doesn't apply. Can you create a .patch file with git
> format-patch
> > and send the file as an attachment? Sometimes email clients/providers
> > corrupt inline patches.
>

Thanks, pushed.


>
> _______________________________________________
> 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".
>
_______________________________________________
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".

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2022-08-29 13:37 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-26 11:42 [FFmpeg-devel] [PATCH] avcodec/videotoolboxenc: Add CBR option to H264 and HEVC encoder Sebastian Beckmann
2022-08-26 11:45 ` Diederick C. Niehorster
2022-08-26 12:34   ` [FFmpeg-devel] [PATCH v2] " Sebastian Beckmann
2022-08-28 18:37 ` [FFmpeg-devel] [PATCH] " Rick Kern
2022-08-28 20:27   ` [FFmpeg-devel] [PATCH v3] " Sebastian Beckmann
2022-08-29 12:58     ` Rick Kern
2022-08-29 13:04       ` Sebastian Beckmann
2022-08-29 13:36         ` Rick Kern

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