[FFmpeg-devel] [PATCH] libavcodec/videotoolboxenc.c: add option to hevc encoder to prioritize speed.

Thilo Borgmann thilo.borgmann at mail.de
Fri Apr 22 19:52:42 EEST 2022


Am 22.04.22 um 18:24 schrieb Andreas Rheinhardt:
> Simone Karin Lehmann:
>>
>>
>>> Am 14.04.2022 um 10:13 schrieb Thilo Borgmann <thilo.borgmann at mail.de>:
>>>
>>> Hi,
>>>
>>>>> diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
>>>>> index 418ff00b8d..ab0dad6cbc 100644
>>>>> --- a/libavcodec/videotoolboxenc.c
>>>>> +++ b/libavcodec/videotoolboxenc.c
>>>>> @@ -236,6 +236,7 @@ typedef struct VTEncContext {
>>>>>      int allow_sw;
>>>>>      int require_sw;
>>>>>      double alpha_quality;
>>>>> +    int64_t prio_speed;
>>>
>>> int64_t is definitely overkill.
>>
>> I just wanted to make it consistent with hevc_options[] and other variables of AV_OPT_TYPE_BOOL. these are noted as .i64.
> 
> AV_OPT_TYPE_BOOL expects an int target; the i64 is just the member used
> for initialization (it is used because an int64_t can hold all the
> values that make sense for a boolean -- it is therefore used for all
> AV_OPT_TYPE_INT as well).
> This also means that int64_t is not only overkill, but wrong (but it
> happens to work on little-endian architectures).

Thanks!


>> Anyway, I changed it to int.
>>
>>>
>>>>>
>>>>>      bool flushing;
>>>>>      int has_b_frames;
>>>>> @@ -1145,6 +1146,17 @@ static int vtenc_create_encoder(AVCodecContext   *avctx,
>>>>>          return AVERROR_EXTERNAL;
>>>>>      }
>>>>>
>>>>> +    // prioritize speed over quality
>>>>> +    if (vtctx->prio_speed) {
>>>>> +        status = VTSessionSetProperty(vtctx->session,
>>>>> +                                      kVTCompressionPropertyKey_PrioritizeEncodingSpeedOverQuality,
>>>>> +                                      kCFBooleanTrue);
>>>>> +        if (status) {
>>>>> +            av_log(avctx, AV_LOG_ERROR, "Error setting PrioritizeEncodingSpeedOverQuality property: %d\n", status);
>>>>> +            return AVERROR_EXTERNAL;
>>>>> +        }
>>>
>>> If its called to priotize speed over qual, why not just print a warning and continue without this option?
>>
>> You’re right. A warning seems much better. Changed this too.
>>
>> Here’s the modified patch.

For that version I get:

libavcodec/videotoolboxenc.c:1153:39: error: use of undeclared identifier 'kVTCompressionPropertyKey_PrioritizeEncodingSpeedOverQuality'
                                       kVTCompressionPropertyKey_PrioritizeEncodingSpeedOverQuality,
                                       ^

Should require some OSX version dependency via #if'ery somewhere sane. Like
#if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070

or similar.

-Thilo



>> Signed-off-by: Simone Karin Lehmann <simone at lisanet.de>
>> ---
>>   libavcodec/videotoolboxenc.c | 12 ++++++++++++
>>   1 file changed, 12 insertions(+)
>>
>> diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
>> index 270496b7a7..cf931569d1 100644
>> --- a/libavcodec/videotoolboxenc.c
>> +++ b/libavcodec/videotoolboxenc.c
>> @@ -237,6 +237,7 @@ typedef struct VTEncContext {
>>       int allow_sw;
>>       int require_sw;
>>       double alpha_quality;
>> +    int prio_speed;
>>   
>>       bool flushing;
>>       int has_b_frames;
>> @@ -1146,6 +1147,16 @@ static int vtenc_create_encoder(AVCodecContext   *avctx,
>>           return AVERROR_EXTERNAL;
>>       }
>>   
>> +    // prioritize speed over quality
>> +    if (vtctx->prio_speed) {
>> +        status = VTSessionSetProperty(vtctx->session,
>> +                                      kVTCompressionPropertyKey_PrioritizeEncodingSpeedOverQuality,
>> +                                      kCFBooleanTrue);
>> +        if (status) {
>> +            av_log(avctx, AV_LOG_WARNING, "PrioritizeEncodingSpeedOverQuality property is not supported on this device. Ignoring.\n");
>> +        }
>> +    }
>> +
>>       if ((vtctx->codec_id == AV_CODEC_ID_H264 || vtctx->codec_id == AV_CODEC_ID_HEVC)
>>               && max_rate > 0) {
>>           bytes_per_second_value = max_rate >> 3;
>> @@ -2745,6 +2756,7 @@ static const AVOption hevc_options[] = {
>>       { "main10",   "Main10 Profile",   0, AV_OPT_TYPE_CONST, { .i64 = HEVC_PROF_MAIN10 }, INT_MIN, INT_MAX, VE, "profile" },
>>   
>>       { "alpha_quality", "Compression quality for the alpha channel", OFFSET(alpha_quality), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0.0, 1.0, VE },
>> +    { "prio_speed", "prioritize encoding speed", OFFSET(prio_speed), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
>>   
>>       COMMON_OPTIONS
>>       { NULL },
> 
> _______________________________________________
> 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