|
| 1 | +From a9770e475398405ad4e91cb73369bf0e6a929e9f Mon Sep 17 00:00:00 2001 |
| 2 | +From: qianlongxu < [email protected]> |
| 3 | +Date: Mon, 13 Oct 2025 14:17:36 +0800 |
| 4 | +Subject: [PATCH] add AV1 hardware acceleration |
| 5 | + |
| 6 | +--- |
| 7 | + configure | 4 ++ |
| 8 | + libavcodec/Makefile | 1 + |
| 9 | + libavcodec/av1dec.c | 10 ++++ |
| 10 | + libavcodec/hwaccels.h | 1 + |
| 11 | + libavcodec/videotoolbox.c | 14 +++++ |
| 12 | + libavcodec/videotoolbox_av1.c | 99 +++++++++++++++++++++++++++++++++++ |
| 13 | + libavcodec/vt_internal.h | 1 + |
| 14 | + 7 files changed, 130 insertions(+) |
| 15 | + create mode 100644 libavcodec/videotoolbox_av1.c |
| 16 | + |
| 17 | +diff --git a/configure b/configure |
| 18 | +index fc5e423..9c56a4c 100755 |
| 19 | +--- a/configure |
| 20 | ++++ b/configure |
| 21 | +@@ -2463,6 +2463,7 @@ TYPES_LIST=" |
| 22 | + kCMVideoCodecType_HEVC |
| 23 | + kCMVideoCodecType_HEVCWithAlpha |
| 24 | + kCMVideoCodecType_VP9 |
| 25 | ++ kCMVideoCodecType_AV1 |
| 26 | + kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange |
| 27 | + kCVPixelFormatType_422YpCbCr8BiPlanarVideoRange |
| 28 | + kCVPixelFormatType_422YpCbCr10BiPlanarVideoRange |
| 29 | +@@ -3168,6 +3169,8 @@ av1_vaapi_hwaccel_deps="vaapi VADecPictureParameterBufferAV1_bit_depth_idx" |
| 30 | + av1_vaapi_hwaccel_select="av1_decoder" |
| 31 | + av1_vdpau_hwaccel_deps="vdpau VdpPictureInfoAV1" |
| 32 | + av1_vdpau_hwaccel_select="av1_decoder" |
| 33 | ++av1_videotoolbox_hwaccel_deps="videotoolbox" |
| 34 | ++av1_videotoolbox_hwaccel_select="av1_decoder" |
| 35 | + av1_vulkan_hwaccel_deps="vulkan" |
| 36 | + av1_vulkan_hwaccel_select="av1_decoder" |
| 37 | + h263_vaapi_hwaccel_deps="vaapi" |
| 38 | +@@ -6700,6 +6703,7 @@ enabled videotoolbox && { |
| 39 | + check_func_headers CoreMedia/CMFormatDescription.h kCMVideoCodecType_HEVC "-framework CoreMedia" |
| 40 | + check_func_headers CoreMedia/CMFormatDescription.h kCMVideoCodecType_HEVCWithAlpha "-framework CoreMedia" |
| 41 | + check_func_headers CoreMedia/CMFormatDescription.h kCMVideoCodecType_VP9 "-framework CoreMedia" |
| 42 | ++ check_func_headers CoreMedia/CMFormatDescription.h kCMVideoCodecType_AV1 "-framework CoreMedia" |
| 43 | + check_func_headers CoreVideo/CVPixelBuffer.h kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange "-framework CoreVideo" |
| 44 | + check_func_headers CoreVideo/CVPixelBuffer.h kCVPixelFormatType_422YpCbCr8BiPlanarVideoRange "-framework CoreVideo" |
| 45 | + check_func_headers CoreVideo/CVPixelBuffer.h kCVPixelFormatType_422YpCbCr10BiPlanarVideoRange "-framework CoreVideo" |
| 46 | +diff --git a/libavcodec/Makefile b/libavcodec/Makefile |
| 47 | +index 441d4ee..d74c06b 100644 |
| 48 | +--- a/libavcodec/Makefile |
| 49 | ++++ b/libavcodec/Makefile |
| 50 | +@@ -1010,6 +1010,7 @@ OBJS-$(CONFIG_AV1_D3D12VA_HWACCEL) += dxva2_av1.o d3d12va_av1.o |
| 51 | + OBJS-$(CONFIG_AV1_NVDEC_HWACCEL) += nvdec_av1.o |
| 52 | + OBJS-$(CONFIG_AV1_VAAPI_HWACCEL) += vaapi_av1.o |
| 53 | + OBJS-$(CONFIG_AV1_VDPAU_HWACCEL) += vdpau_av1.o |
| 54 | ++OBJS-$(CONFIG_AV1_VIDEOTOOLBOX_HWACCEL) += videotoolbox_av1.o |
| 55 | + OBJS-$(CONFIG_AV1_VULKAN_HWACCEL) += vulkan_decode.o vulkan_av1.o |
| 56 | + OBJS-$(CONFIG_H263_VAAPI_HWACCEL) += vaapi_mpeg4.o |
| 57 | + OBJS-$(CONFIG_H263_VIDEOTOOLBOX_HWACCEL) += videotoolbox.o |
| 58 | +diff --git a/libavcodec/av1dec.c b/libavcodec/av1dec.c |
| 59 | +index 1d5b9ef..0fad09a 100644 |
| 60 | +--- a/libavcodec/av1dec.c |
| 61 | ++++ b/libavcodec/av1dec.c |
| 62 | +@@ -541,6 +541,7 @@ static int get_pixel_format(AVCodecContext *avctx) |
| 63 | + CONFIG_AV1_NVDEC_HWACCEL + \ |
| 64 | + CONFIG_AV1_VAAPI_HWACCEL + \ |
| 65 | + CONFIG_AV1_VDPAU_HWACCEL + \ |
| 66 | ++ CONFIG_AV1_VIDEOTOOLBOX_HWACCEL + \ |
| 67 | + CONFIG_AV1_VULKAN_HWACCEL) |
| 68 | + enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmtp = pix_fmts; |
| 69 | + |
| 70 | +@@ -568,6 +569,9 @@ static int get_pixel_format(AVCodecContext *avctx) |
| 71 | + #if CONFIG_AV1_VDPAU_HWACCEL |
| 72 | + *fmtp++ = AV_PIX_FMT_VDPAU; |
| 73 | + #endif |
| 74 | ++#if CONFIG_AV1_VIDEOTOOLBOX_HWACCEL |
| 75 | ++ *fmtp++ = AV_PIX_FMT_VIDEOTOOLBOX; |
| 76 | ++#endif |
| 77 | + #if CONFIG_AV1_VULKAN_HWACCEL |
| 78 | + *fmtp++ = AV_PIX_FMT_VULKAN; |
| 79 | + #endif |
| 80 | +@@ -592,6 +596,9 @@ static int get_pixel_format(AVCodecContext *avctx) |
| 81 | + #if CONFIG_AV1_VDPAU_HWACCEL |
| 82 | + *fmtp++ = AV_PIX_FMT_VDPAU; |
| 83 | + #endif |
| 84 | ++#if CONFIG_AV1_VIDEOTOOLBOX_HWACCEL |
| 85 | ++ *fmtp++ = AV_PIX_FMT_VIDEOTOOLBOX; |
| 86 | ++#endif |
| 87 | + #if CONFIG_AV1_VULKAN_HWACCEL |
| 88 | + *fmtp++ = AV_PIX_FMT_VULKAN; |
| 89 | + #endif |
| 90 | +@@ -1594,6 +1601,9 @@ const FFCodec ff_av1_decoder = { |
| 91 | + #if CONFIG_AV1_VDPAU_HWACCEL |
| 92 | + HWACCEL_VDPAU(av1), |
| 93 | + #endif |
| 94 | ++#if CONFIG_AV1_VIDEOTOOLBOX_HWACCEL |
| 95 | ++ HWACCEL_VIDEOTOOLBOX(av1), |
| 96 | ++#endif |
| 97 | + #if CONFIG_AV1_VULKAN_HWACCEL |
| 98 | + HWACCEL_VULKAN(av1), |
| 99 | + #endif |
| 100 | +diff --git a/libavcodec/hwaccels.h b/libavcodec/hwaccels.h |
| 101 | +index 5171e4c..2b9bdc8 100644 |
| 102 | +--- a/libavcodec/hwaccels.h |
| 103 | ++++ b/libavcodec/hwaccels.h |
| 104 | +@@ -26,6 +26,7 @@ extern const struct FFHWAccel ff_av1_dxva2_hwaccel; |
| 105 | + extern const struct FFHWAccel ff_av1_nvdec_hwaccel; |
| 106 | + extern const struct FFHWAccel ff_av1_vaapi_hwaccel; |
| 107 | + extern const struct FFHWAccel ff_av1_vdpau_hwaccel; |
| 108 | ++extern const struct FFHWAccel ff_av1_videotoolbox_hwaccel; |
| 109 | + extern const struct FFHWAccel ff_av1_vulkan_hwaccel; |
| 110 | + extern const struct FFHWAccel ff_h263_vaapi_hwaccel; |
| 111 | + extern const struct FFHWAccel ff_h263_videotoolbox_hwaccel; |
| 112 | +diff --git a/libavcodec/videotoolbox.c b/libavcodec/videotoolbox.c |
| 113 | +index 505483e..392d7fb 100644 |
| 114 | +--- a/libavcodec/videotoolbox.c |
| 115 | ++++ b/libavcodec/videotoolbox.c |
| 116 | +@@ -56,6 +56,10 @@ enum { kCMVideoCodecType_HEVC = 'hvc1' }; |
| 117 | + enum { kCMVideoCodecType_VP9 = 'vp09' }; |
| 118 | + #endif |
| 119 | + |
| 120 | ++#if !HAVE_KCMVIDEOCODECTYPE_AV1 |
| 121 | ++enum { kCMVideoCodecType_AV1 = 'av01' }; |
| 122 | ++#endif |
| 123 | ++ |
| 124 | + #define VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING 12 |
| 125 | + |
| 126 | + typedef struct VTHWFrame { |
| 127 | +@@ -846,6 +850,13 @@ static CFDictionaryRef videotoolbox_decoder_config_create(CMVideoCodecType codec |
| 128 | + if (data) |
| 129 | + CFDictionarySetValue(avc_info, CFSTR("vpcC"), data); |
| 130 | + break; |
| 131 | ++#endif |
| 132 | ++#if CONFIG_AV1_VIDEOTOOLBOX_HWACCEL |
| 133 | ++ case kCMVideoCodecType_AV1 : |
| 134 | ++ data = ff_videotoolbox_av1c_extradata_create(avctx); |
| 135 | ++ if (data) |
| 136 | ++ CFDictionarySetValue(avc_info, CFSTR("av1C"), data); |
| 137 | ++ break; |
| 138 | + #endif |
| 139 | + default: |
| 140 | + break; |
| 141 | +@@ -912,6 +923,9 @@ static int videotoolbox_start(AVCodecContext *avctx) |
| 142 | + case AV_CODEC_ID_VP9 : |
| 143 | + videotoolbox->cm_codec_type = kCMVideoCodecType_VP9; |
| 144 | + break; |
| 145 | ++ case AV_CODEC_ID_AV1 : |
| 146 | ++ videotoolbox->cm_codec_type = kCMVideoCodecType_AV1; |
| 147 | ++ break; |
| 148 | + default : |
| 149 | + break; |
| 150 | + } |
| 151 | +diff --git a/libavcodec/videotoolbox_av1.c b/libavcodec/videotoolbox_av1.c |
| 152 | +new file mode 100644 |
| 153 | +index 0000000..4bf0c73 |
| 154 | +--- /dev/null |
| 155 | ++++ b/libavcodec/videotoolbox_av1.c |
| 156 | +@@ -0,0 +1,99 @@ |
| 157 | ++/* |
| 158 | ++ * Videotoolbox hardware acceleration for AV1 |
| 159 | ++ * Copyright (c) 2023 Jan Ekström |
| 160 | ++ * |
| 161 | ++ * This file is part of FFmpeg. |
| 162 | ++ * |
| 163 | ++ * FFmpeg is free software; you can redistribute it and/or |
| 164 | ++ * modify it under the terms of the GNU Lesser General Public |
| 165 | ++ * License as published by the Free Software Foundation; either |
| 166 | ++ * version 2.1 of the License, or (at your option) any later version. |
| 167 | ++ * |
| 168 | ++ * FFmpeg is distributed in the hope that it will be useful, |
| 169 | ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 170 | ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 171 | ++ * Lesser General Public License for more details. |
| 172 | ++ * |
| 173 | ++ * You should have received a copy of the GNU Lesser General Public |
| 174 | ++ * License along with FFmpeg; if not, write to the Free Software |
| 175 | ++ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 176 | ++ */ |
| 177 | ++ |
| 178 | ++#include "libavformat/avio.h" |
| 179 | ++#include "libavformat/avio_internal.h" |
| 180 | ++#define CALLED_FROM_AVCODEC 1 |
| 181 | ++#include "libavformat/av1.c" |
| 182 | ++#undef CALLED_FROM_AVCODEC |
| 183 | ++ |
| 184 | ++#include "libavutil/avutil.h" |
| 185 | ++#include "libavutil/frame.h" |
| 186 | ++#include "libavutil/pixfmt.h" |
| 187 | ++ |
| 188 | ++#include "av1dec.h" |
| 189 | ++#include "codec_id.h" |
| 190 | ++#include "hwaccel_internal.h" |
| 191 | ++#include "internal.h" |
| 192 | ++#include "vt_internal.h" |
| 193 | ++ |
| 194 | ++CFDataRef ff_videotoolbox_av1c_extradata_create(AVCodecContext *avctx) |
| 195 | ++{ |
| 196 | ++ AVIOContext *pb; |
| 197 | ++ uint8_t *buf; |
| 198 | ++ CFDataRef data = NULL; |
| 199 | ++ int buf_size = 0; |
| 200 | ++ int ret = avio_open_dyn_buf(&pb); |
| 201 | ++ if (ret < 0) |
| 202 | ++ return NULL; |
| 203 | ++ |
| 204 | ++ ret = ff_isom_write_av1c(pb, avctx->extradata, avctx->extradata_size, 1); |
| 205 | ++ if (ret < 0) |
| 206 | ++ goto fail; |
| 207 | ++ |
| 208 | ++ buf_size = avio_get_dyn_buf(pb, &buf); |
| 209 | ++ if (buf_size) |
| 210 | ++ data = CFDataCreate(kCFAllocatorDefault, buf, buf_size); |
| 211 | ++ |
| 212 | ++fail: |
| 213 | ++ ffio_free_dyn_buf(&pb); |
| 214 | ++ |
| 215 | ++ return data; |
| 216 | ++} |
| 217 | ++ |
| 218 | ++static int videotoolbox_av1_start_frame(AVCodecContext *avctx, |
| 219 | ++ const uint8_t *buffer, |
| 220 | ++ uint32_t size) |
| 221 | ++{ |
| 222 | ++ return 0; |
| 223 | ++} |
| 224 | ++ |
| 225 | ++static int videotoolbox_av1_decode_slice(AVCodecContext *avctx, |
| 226 | ++ const uint8_t *buffer, |
| 227 | ++ uint32_t size) |
| 228 | ++{ |
| 229 | ++ VTContext *vtctx = avctx->internal->hwaccel_priv_data; |
| 230 | ++ |
| 231 | ++ return ff_videotoolbox_buffer_copy(vtctx, buffer, size); |
| 232 | ++} |
| 233 | ++ |
| 234 | ++static int videotoolbox_av1_end_frame(AVCodecContext *avctx) |
| 235 | ++{ |
| 236 | ++ const AV1DecContext *s = avctx->priv_data; |
| 237 | ++ AVFrame *frame = s->cur_frame.f; |
| 238 | ++ |
| 239 | ++ return ff_videotoolbox_common_end_frame(avctx, frame); |
| 240 | ++} |
| 241 | ++ |
| 242 | ++const FFHWAccel ff_av1_videotoolbox_hwaccel = { |
| 243 | ++ .p.name = "av1_videotoolbox", |
| 244 | ++ .p.type = AVMEDIA_TYPE_VIDEO, |
| 245 | ++ .p.id = AV_CODEC_ID_AV1, |
| 246 | ++ .p.pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX, |
| 247 | ++ .alloc_frame = ff_videotoolbox_alloc_frame, |
| 248 | ++ .start_frame = videotoolbox_av1_start_frame, |
| 249 | ++ .decode_slice = videotoolbox_av1_decode_slice, |
| 250 | ++ .end_frame = videotoolbox_av1_end_frame, |
| 251 | ++ .frame_params = ff_videotoolbox_frame_params, |
| 252 | ++ .init = ff_videotoolbox_common_init, |
| 253 | ++ .uninit = ff_videotoolbox_uninit, |
| 254 | ++ .priv_data_size = sizeof(VTContext), |
| 255 | ++}; |
| 256 | +\ No newline at end of file |
| 257 | +diff --git a/libavcodec/vt_internal.h b/libavcodec/vt_internal.h |
| 258 | +index 9502d7c..427348f 100644 |
| 259 | +--- a/libavcodec/vt_internal.h |
| 260 | ++++ b/libavcodec/vt_internal.h |
| 261 | +@@ -64,6 +64,7 @@ int ff_videotoolbox_h264_decode_slice(AVCodecContext *avctx, |
| 262 | + const uint8_t *buffer, |
| 263 | + uint32_t size); |
| 264 | + int ff_videotoolbox_common_end_frame(AVCodecContext *avctx, AVFrame *frame); |
| 265 | ++CFDataRef ff_videotoolbox_av1c_extradata_create(AVCodecContext *avctx); |
| 266 | + CFDataRef ff_videotoolbox_avcc_extradata_create(AVCodecContext *avctx); |
| 267 | + CFDataRef ff_videotoolbox_hvcc_extradata_create(AVCodecContext *avctx); |
| 268 | + CFDataRef ff_videotoolbox_vpcc_extradata_create(AVCodecContext *avctx); |
| 269 | +-- |
| 270 | +2.39.5 (Apple Git-154) |
| 271 | + |
0 commit comments