From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id A8F6F43C5E for ; Sun, 28 Aug 2022 18:38:01 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BBC7F68B989; Sun, 28 Aug 2022 21:37:58 +0300 (EEST) Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A119468B8D7 for ; Sun, 28 Aug 2022 21:37:52 +0300 (EEST) Received: by mail-pf1-f176.google.com with SMTP id 199so6194675pfz.2 for ; Sun, 28 Aug 2022 11:37:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc; bh=PzGv/zXoOOGpUm/IsXAN3whHCbxCbWhiC64xTubvk8I=; b=NDxdCltWWIJodEKl9VS9rBxKlgN33ONyO6dOqaVyKpwfslVijStTj8LXFv3WtQFolo 1mkHWXoolcS6ZrIIQxpGjOWmqWFB9HQjvyv54Jz4y78BDHlNFYCgKoZR/t5EuvYycdbi noabDheuceCn4dy86c8FrZn754ukQFYLSrsyBFfaTUkx6dKz3R73VEvk5vuxuTh5p/L0 pW6X3nghmXzFcxBnUhuJ6TMw8QqI/tLDV3CI07jDTw1Yikd984GqJUK5f716MNYlbqKa iESkPQkHYQ98tu+Tpmg3m0NnmuDAPu05R6PYm1RQ+8VvoHSboaGMJLa7nc1dkwEmdKgB 17RA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc; bh=PzGv/zXoOOGpUm/IsXAN3whHCbxCbWhiC64xTubvk8I=; b=Tp6RwphlEve3c8oTL7472Zptwb5kxl/FmAixL3K5uZG2ojouzPHupYxbTIqej0pvLL kmX8XawFQght6D6IR8cBQ8/gPupNfHUE0ru+ncoqqAHCKMiUnWWFys/djoC1kK5wowtr zM1EQFLAbbg4zYGCLKp1lbD52jfT12fcZqiCnFl3/CU7dNPf+xMDNs9doPXsb4Mp9Kzk XlBqSZlE19nk1ePXDbiJcfOadXwHYkGZ00WrzIPCWGDoYH+3nCy6+oDiWbAOvF5DjpHp q6nyW3S9cUc0yXu1acfuCr3bIENgrW9CuZLSJQ0l2lT8iveIkQQnjYD2yljU71T64V0C kzkQ== X-Gm-Message-State: ACgBeo2U5hqIzhwa3CxulaqLoUNWdH/lUEQGzwhS6FXgA979z7ZRlB+U YA3lZbk7P15rtTcwzRgDBSm10ivAJNq3RcOKHBskFSdKskY= X-Google-Smtp-Source: AA6agR4ed673K9NIbCqGlTsqaINHGQzg0lfmWJzrpIDALVsyYUf83CvfOyaYL8c/rBJajTSqpccYTngA9Rd0wzU84Fk= X-Received: by 2002:aa7:8712:0:b0:537:db6d:af66 with SMTP id b18-20020aa78712000000b00537db6daf66mr10141832pfo.55.1661711869890; Sun, 28 Aug 2022 11:37:49 -0700 (PDT) MIME-Version: 1.0 References: <333BB5A1-3A9E-4DCA-9782-4DA302C124F6@outlook.de> In-Reply-To: <333BB5A1-3A9E-4DCA-9782-4DA302C124F6@outlook.de> From: Rick Kern Date: Sun, 28 Aug 2022 14:37:38 -0400 Message-ID: To: FFmpeg development discussions and patches X-Content-Filtered-By: Mailman/MimeDel 2.1.29 Subject: Re: [FFmpeg-devel] [PATCH] avcodec/videotoolboxenc: Add CBR option to H264 and HEVC encoder X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: 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 > --- > 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".