[FFmpeg-devel] [PATCH v2] lavc/videotoolboxenc: Add spatial_aq option

Martin Storsjö martin at martin.st
Tue Feb 11 11:28:59 EET 2025


On Tue, 4 Feb 2025, Martin Storsjö wrote:

> On Mon, 16 Dec 2024, Dennis Sädtler via ffmpeg-devel wrote:
>
>> From: Dennis Sädtler <dennis at obsproject.com>
>> 
>> Added in macOS 15 "Sequoia".
>> 
>> Signed-off-by: Dennis Sädtler <dennis at obsproject.com>
>> ---
>> Fixed line-endings, otherwise identical to v1.
>> 
>> libavcodec/videotoolboxenc.c | 12 ++++++++++++
>> 1 file changed, 12 insertions(+)
>> 
>> diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
>> index da7b291b03..fb2de7b960 100644
>> --- a/libavcodec/videotoolboxenc.c
>> +++ b/libavcodec/videotoolboxenc.c
>> @@ -121,6 +121,7 @@ static struct{
>>     CFStringRef 
>> kVTCompressionPropertyKey_PrioritizeEncodingSpeedOverQuality;
>>     CFStringRef kVTCompressionPropertyKey_ConstantBitRate;
>>     CFStringRef kVTCompressionPropertyKey_EncoderID;
>> +    CFStringRef kVTCompressionPropertyKey_SpatialAdaptiveQPLevel;
>>
>>     CFStringRef 
>> kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder;
>>     CFStringRef 
>> kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder;
>> @@ -208,6 +209,7 @@ static void loadVTEncSymbols(void){
>>             "ReferenceBufferCount");
>>     GET_SYM(kVTCompressionPropertyKey_MaxAllowedFrameQP, 
>> "MaxAllowedFrameQP");
>>     GET_SYM(kVTCompressionPropertyKey_MinAllowedFrameQP, 
>> "MinAllowedFrameQP");
>> +    GET_SYM(kVTCompressionPropertyKey_SpatialAdaptiveQPLevel, 
>> "SpatialAdaptiveQPLevel");
>> }
>> 
>> #define H264_PROFILE_CONSTRAINED_HIGH (AV_PROFILE_H264_HIGH | 
>> AV_PROFILE_H264_CONSTRAINED)
>> @@ -279,6 +281,7 @@ typedef struct VTEncContext {
>>     int max_slice_bytes;
>>     int power_efficient;
>>     int max_ref_frames;
>> +    int spatialaq;
>> } VTEncContext;
>> 
>> static void vtenc_free_buf_node(BufNode *info)
>> @@ -1599,6 +1602,13 @@ static int vtenc_create_encoder(AVCodecContext 
>> *avctx,
>>         }
>>     }
>> 
>> +    if (vtctx->spatialaq >= 0) {
>> +        set_encoder_int_property_or_log(avctx,
>> + 
>> compat_keys.kVTCompressionPropertyKey_SpatialAdaptiveQPLevel,
>> +                                        "spatialaq",
>> +                                        vtctx->spatialaq ? 
>> kVTQPModulationLevel_Default : kVTQPModulationLevel_Disable);
>
> These constants aren't available if building with an older SDK, so this would 
> break building in such cases.
>
> For other similar cases of compile time constants that may or may not be 
> available, we have configure checks and provide the constants ourselves - 
> that seems to be reasonable here too.
>
> With the following changes on top, it seems to build successfully even for 
> older versions:
>
>
> diff --git a/configure b/configure
> index 32fbe58126..020c2af843 100755
> --- a/configure
> +++ b/configure
> @@ -2490,6 +2490,7 @@ TYPES_LIST="
>     kCVImageBufferColorPrimaries_ITU_R_2020
>     kCVImageBufferTransferFunction_ITU_R_2020
>     kCVImageBufferTransferFunction_SMPTE_ST_428_1
> +    kVTQPModulationLevel_Default
>     socklen_t
>     struct_addrinfo
>     struct_group_source_req
> @@ -6748,6 +6749,7 @@ enabled videotoolbox && {
>     check_func_headers CoreVideo/CVImageBuffer.h 
> kCVImageBufferColorPrimaries_ITU_R_2020 "-framework CoreVideo"
>     check_func_headers CoreVideo/CVImageBuffer.h 
> kCVImageBufferTransferFunction_ITU_R_2020 "-framework CoreVideo"
>     check_func_headers CoreVideo/CVImageBuffer.h 
> kCVImageBufferTransferFunction_SMPTE_ST_428_1 "-framework CoreVideo"
> +    check_func_headers VideoToolbox/VTCompressionProperties.h 
> kVTQPModulationLevel_Default "-framework CoreVideo"
> }
>
> enabled metal && test_cmd $metalcc -v || disable metal
> diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
> index fb2de7b960..950a29d9fa 100644
> --- a/libavcodec/videotoolboxenc.c
> +++ b/libavcodec/videotoolboxenc.c
> @@ -54,6 +54,11 @@ enum { kCVPixelFormatType_420YpCbCr10BiPlanarFullRange = 
> 'xf20' };
> enum { kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange = 'x420' };
> #endif
>
> +#if !HAVE_KVTQPMODULATIONLEVEL_DEFAULT
> +enum { kVTQPModulationLevel_Default = -1 };
> +enum { kVTQPModulationLevel_Disable = 0 };
> +#endif
> +
> #ifndef TARGET_CPU_ARM64
> #   define TARGET_CPU_ARM64 0
> #endif
>
>
> If you don't mind these changes, I could push the patch with these changes 
> squashed in.

Will push with these changes.

// Martin


More information about the ffmpeg-devel mailing list