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 2145446517 for ; Sat, 20 May 2023 23:16:20 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4414568C125; Sun, 21 May 2023 02:16:18 +0300 (EEST) Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.47]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E9EA668B157 for ; Sun, 21 May 2023 02:16:11 +0300 (EEST) Received: by mail-ej1-f47.google.com with SMTP id a640c23a62f3a-96fbc74fbf1so38515166b.1 for ; Sat, 20 May 2023 16:16:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684624571; x=1687216571; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=9ei4d4bZZG6Nhtx9SSZWDzM+q4HXxoVeYwLHvASYjF4=; b=CQO+Ew4psmpYZZ4t8LcFPZjB8xcvt5yHKDB1lxZ1txekvgL9uT4RYaN621jU2JTCPs izeBWqi2OBzDIOyZJzMJqVXsx0Ip5YzrtDteVGDvKRF2yTWvx8uDyjvCMFOiZawnWFW8 vZqibi+3KigQH0cpXewKiPJETDrp/rIg7Kc2ER/etUDzboeUcZ93A/QVL3pZGk3utd4H z/caKEKQId9zA91Qwt03iiMm9Rj81fz1wLeq3h2pKXfvwGyRjIPIfsnrcMYoXzsESAkj A8zyYwP6qWvEs6mqN0l6lNWe6Zyc8hZKSGa4AtcExFpsZ5w0M4oP3bc8UInlo/Q6yJ5H dG1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684624571; x=1687216571; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9ei4d4bZZG6Nhtx9SSZWDzM+q4HXxoVeYwLHvASYjF4=; b=NTXKI7Q1bhgU6Iw10ppPTWNRJFQyGwI+s1MXQTmMi1RX+13fQyRG/FZSqvG+OBi21Z cVbT849xQ7akaXbEbflGc0MkjAv9+3Fl5DtfAxm1A6fWTbvqIlMGIP3wG/LYLO+uKeyS sg5tofy/B83D8Qmw4tjYufKxhZVmkDkrwVNitA76oTYF+judBZ1qB8I2TwRCgR9DIxME u9l82h02/FOLrWth8qrfrVg/9Jn+5tsbx2TRZA+eohy1nkX+KdyU+yz7UBEWDL50ndQr Sf9sD5PbIOKL00YQhZxj0Ex++PCF+cA3R2/klVWXqh3D7rEBSUo5QlmVszn4JQLeXLfs Xl8w== X-Gm-Message-State: AC+VfDwd2tFCxSZqgCAvzYO8vLZyA8nllmobWNkZ/0fLgc/TgTCegNSa qHIZq+GNwW8IOvXu2g/4aRXVi1wlnw0= X-Google-Smtp-Source: ACHHUZ6q1rxVCfer4GgjQleururgAGjgj1Y4ZvY215oND667OTIy25V0ZWuZGxAcvCQOOh7U7j8lUQ== X-Received: by 2002:a17:907:97ca:b0:94b:958c:8827 with SMTP id js10-20020a17090797ca00b0094b958c8827mr7169760ejc.56.1684624571052; Sat, 20 May 2023 16:16:11 -0700 (PDT) Received: from [192.168.1.104] (84-112-104-25.cable.dynamic.surfer.at. [84.112.104.25]) by smtp.gmail.com with ESMTPSA id r15-20020aa7c14f000000b004af6c5f1805sm1243226edp.52.2023.05.20.16.16.09 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 20 May 2023 16:16:10 -0700 (PDT) From: Marvin Scholz To: FFmpeg development discussions and patches Date: Sun, 21 May 2023 01:16:08 +0200 X-Mailer: MailMate (1.14r5964) Message-ID: <17C1176A-56F6-47EE-990E-E2A6935A7B1A@gmail.com> In-Reply-To: <20230520222422.68633-1-kernrj@gmail.com> References: <20230520222422.68633-1-kernrj@gmail.com> MIME-Version: 1.0 Subject: Re: [FFmpeg-devel] [PATCH] lavc/videotoolboxenc: support additional options 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 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@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".