Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Sebastian Beckmann <beckmann.sebastian@outlook.de>
To: "ffmpeg-devel@ffmpeg.org" <ffmpeg-devel@ffmpeg.org>
Subject: [FFmpeg-devel] [PATCH v3] avcodec/videotoolboxenc: Add CBR option to H264 and HEVC encoder
Date: Sun, 28 Aug 2022 20:27:14 +0000
Message-ID: <D9CE1DFE-778B-4486-BD46-4BE9DCA60112@outlook.de> (raw)
In-Reply-To: <CABmg8gUw0==9dBYpmXmG7k1Yc3tiOveEcSo13XFfB+WinBpFkQ@mail.gmail.com>

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

  reply	other threads:[~2022-08-28 20:27 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-26 11:42 [FFmpeg-devel] [PATCH] " 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   ` Sebastian Beckmann [this message]
2022-08-29 12:58     ` [FFmpeg-devel] [PATCH v3] " Rick Kern
2022-08-29 13:04       ` Sebastian Beckmann
2022-08-29 13:36         ` Rick Kern

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=D9CE1DFE-778B-4486-BD46-4BE9DCA60112@outlook.de \
    --to=beckmann.sebastian@outlook.de \
    --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