24 #include "config_components.h"
41 #include <Availability.h>
42 #include <AvailabilityMacros.h>
43 #include <TargetConditionals.h>
45 #ifndef kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder
46 # define kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder CFSTR("RequireHardwareAcceleratedVideoDecoder")
48 #ifndef kVTVideoDecoderSpecification_EnableHardwareAcceleratedVideoDecoder
49 # define kVTVideoDecoderSpecification_EnableHardwareAcceleratedVideoDecoder CFSTR("EnableHardwareAcceleratedVideoDecoder")
52 #if !HAVE_KCMVIDEOCODECTYPE_HEVC
56 #if !HAVE_KCMVIDEOCODECTYPE_VP9
60 #if !HAVE_KCMVIDEOCODECTYPE_AV1
64 #define VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING 12
75 CVPixelBufferRelease(
ref->pixbuf);
131 frame->crop_right = 0;
132 frame->crop_left = 0;
134 frame->crop_bottom = 0;
139 frame->data[3] = (uint8_t*)
ref->pixbuf;
141 if (
ref->hw_frames_ctx) {
144 if (!
frame->hw_frames_ctx)
171 fdd =
frame->private_ref;
181 #define AV_W8(p, v) *(p) = (v)
189 for (
i = 0;
i < src_size;
i++) {
190 if (
i + 2 < src_size &&
192 src[
i + 1] == 0x00 &&
193 src[
i + 2] <= 0x03) {
220 int vt_extradata_size;
221 uint8_t *vt_extradata;
223 vt_extradata_size = 6 + 2 + sps_size + 3 + pps_size;
224 vt_extradata =
av_malloc(vt_extradata_size);
232 AV_W8(
p + 1,
h->ps.sps->data[1]);
233 AV_W8(
p + 2,
h->ps.sps->data[2]);
234 AV_W8(
p + 3,
h->ps.sps->data[3]);
250 memcpy(vtctx->
sps,
h->ps.sps->data + 1, 3);
252 data = CFDataCreate(kCFAllocatorDefault, vt_extradata, vt_extradata_size);
260 int i, num_vps = 0, num_sps = 0, num_pps = 0;
266 uint8_t parallelismType;
269 int vt_extradata_size = 23 + 3 + 3 + 3;
270 uint8_t *vt_extradata;
272 #define COUNT_SIZE_PS(T, t) \
273 for (i = 0; i < HEVC_MAX_##T##PS_COUNT; i++) { \
274 if (h->ps.t##ps_list[i]) { \
275 const HEVC##T##PS *lps = h->ps.t##ps_list[i]; \
276 vt_extradata_size += 2 + escape_ps(NULL, lps->data, lps->data_size); \
285 vt_extradata =
av_malloc(vt_extradata_size);
303 for (
i = 0;
i < 4;
i++) {
338 else if (
pps->entropy_coding_sync_enabled_flag &&
pps->tiles_enabled_flag)
340 else if (
pps->entropy_coding_sync_enabled_flag)
342 else if (
pps->tiles_enabled_flag)
346 AV_W8(
p + 15, 0xfc | parallelismType);
352 AV_W8(
p + 16,
sps->chroma_format_idc | 0xfc);
358 AV_W8(
p + 17, (
sps->bit_depth - 8) | 0xf8);
364 AV_W8(
p + 18, (
sps->bit_depth_chroma - 8) | 0xf8);
376 sps->max_sub_layers << 3 |
377 sps->temporal_id_nesting << 2 |
385 #define APPEND_PS(T, t) \
392 HEVC_NAL_##T##PS & 0x3f); \
394 AV_WB16(p + 1, num_##t##ps); \
396 for (i = 0; i < HEVC_MAX_##T##PS_COUNT; i++) { \
397 if (h->ps.t##ps_list[i]) { \
398 const HEVC##T##PS *lps = h->ps.t##ps_list[i]; \
399 int size = escape_ps(p + 2, lps->data, lps->data_size); \
413 data = CFDataCreate(kCFAllocatorDefault, vt_extradata, vt_extradata_size);
426 if (
h->is_avc == 1) {
443 memcpy(vtctx->
sps,
h->ps.sps->data + 1, 3);
491 #if CONFIG_VIDEOTOOLBOX
518 VTDecompressionSessionInvalidate(videotoolbox->
session);
519 CFRelease(videotoolbox->
session);
532 CVPixelBufferRelease(vtctx->
frame);
535 videotoolbox_stop(avctx);
546 CVPixelBufferRef pixbuf = (CVPixelBufferRef)vtctx->
frame;
547 OSType pixel_format = CVPixelBufferGetPixelFormatType(pixbuf);
549 int width = CVPixelBufferGetWidth(pixbuf);
550 int height = CVPixelBufferGetHeight(pixbuf);
564 CVPixelBufferRelease(
ref->pixbuf);
588 hw_ctx = hw_frames->
hwctx;
603 if (!
ref->hw_frames_ctx)
609 static void videotoolbox_write_mp4_descr_length(
PutByteContext *pb,
int length)
614 for (
i = 3;
i >= 0;
i--) {
615 b = (length >> (
i * 7)) & 0x7F;
619 bytestream2_put_byteu(pb,
b);
623 static CFDataRef videotoolbox_esds_extradata_create(
AVCodecContext *avctx)
626 uint8_t *rw_extradata;
637 bytestream2_put_byteu(&pb, 0);
641 bytestream2_put_byteu(&pb, 0x03);
642 videotoolbox_write_mp4_descr_length(&pb, full_size);
644 bytestream2_put_byteu(&pb, 0);
647 bytestream2_put_byteu(&pb, 0x04);
648 videotoolbox_write_mp4_descr_length(&pb, config_size);
649 bytestream2_put_byteu(&pb, 32);
650 bytestream2_put_byteu(&pb, 0x11);
656 bytestream2_put_byteu(&pb, 0x05);
662 bytestream2_put_byteu(&pb, 0x06);
663 bytestream2_put_byteu(&pb, 0x01);
664 bytestream2_put_byteu(&pb, 0x02);
668 data = CFDataCreate(kCFAllocatorDefault, rw_extradata,
s);
674 static CMSampleBufferRef videotoolbox_sample_buffer_create(CMFormatDescriptionRef fmt_desc,
679 CMBlockBufferRef block_buf;
680 CMSampleBufferRef sample_buf;
685 status = CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault,
696 status = CMSampleBufferCreate(kCFAllocatorDefault,
711 CFRelease(block_buf);
716 static void videotoolbox_decoder_callback(
void *opaque,
717 void *sourceFrameRefCon,
719 VTDecodeInfoFlags
flags,
720 CVImageBufferRef image_buffer,
727 CVPixelBufferRelease(vtctx->
frame);
737 "vt decoder cb: output image buffer is null: %i, reconfig %d\n",
742 vtctx->
frame = CVPixelBufferRetain(image_buffer);
745 static OSStatus videotoolbox_session_decode_frame(
AVCodecContext *avctx)
748 CMSampleBufferRef sample_buf;
752 sample_buf = videotoolbox_sample_buffer_create(videotoolbox->
cm_fmt_desc,
759 status = VTDecompressionSessionDecodeFrame(videotoolbox->
session,
765 status = VTDecompressionSessionWaitForAsynchronousFrames(videotoolbox->
session);
767 CFRelease(sample_buf);
772 static CMVideoFormatDescriptionRef videotoolbox_format_desc_create(CMVideoCodecType
codec_type,
773 CFDictionaryRef decoder_spec,
777 CMFormatDescriptionRef cm_fmt_desc;
780 status = CMVideoFormatDescriptionCreate(kCFAllocatorDefault,
793 static CFDictionaryRef videotoolbox_buffer_attributes_create(
int width,
797 CFMutableDictionaryRef buffer_attributes;
798 CFMutableDictionaryRef io_surface_properties;
799 CFNumberRef cv_pix_fmt;
803 w = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &
width);
804 h = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &
height);
805 cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &
pix_fmt);
807 buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
809 &kCFTypeDictionaryKeyCallBacks,
810 &kCFTypeDictionaryValueCallBacks);
811 io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
813 &kCFTypeDictionaryKeyCallBacks,
814 &kCFTypeDictionaryValueCallBacks);
817 CFDictionarySetValue(buffer_attributes, kCVPixelBufferPixelFormatTypeKey, cv_pix_fmt);
818 CFDictionarySetValue(buffer_attributes, kCVPixelBufferIOSurfacePropertiesKey, io_surface_properties);
819 CFDictionarySetValue(buffer_attributes, kCVPixelBufferWidthKey,
w);
820 CFDictionarySetValue(buffer_attributes, kCVPixelBufferHeightKey,
h);
822 CFDictionarySetValue(buffer_attributes, kCVPixelBufferOpenGLESCompatibilityKey, kCFBooleanTrue);
824 CFDictionarySetValue(buffer_attributes, kCVPixelBufferIOSurfaceOpenGLTextureCompatibilityKey, kCFBooleanTrue);
827 CFRelease(io_surface_properties);
828 CFRelease(cv_pix_fmt);
832 return buffer_attributes;
835 static CFDictionaryRef videotoolbox_decoder_config_create(CMVideoCodecType
codec_type,
838 CFMutableDictionaryRef avc_info;
841 CFMutableDictionaryRef config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
843 &kCFTypeDictionaryKeyCallBacks,
844 &kCFTypeDictionaryValueCallBacks);
846 CFDictionarySetValue(config_info,
852 avc_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
854 &kCFTypeDictionaryKeyCallBacks,
855 &kCFTypeDictionaryValueCallBacks);
858 case kCMVideoCodecType_MPEG4Video :
860 data = videotoolbox_esds_extradata_create(avctx);
862 CFDictionarySetValue(avc_info, CFSTR(
"esds"),
data);
864 case kCMVideoCodecType_H264 :
867 CFDictionarySetValue(avc_info, CFSTR(
"avcC"),
data);
872 CFDictionarySetValue(avc_info, CFSTR(
"hvcC"),
data);
874 #if CONFIG_VP9_VIDEOTOOLBOX_HWACCEL
878 CFDictionarySetValue(avc_info, CFSTR(
"vpcC"),
data);
881 #if CONFIG_AV1_VIDEOTOOLBOX_HWACCEL
885 CFDictionarySetValue(avc_info, CFSTR(
"av1C"),
data);
892 CFDictionarySetValue(config_info,
893 kCMFormatDescriptionExtension_SampleDescriptionExtensionAtoms,
907 VTDecompressionOutputCallbackRecord decoder_cb;
908 CFDictionaryRef decoder_spec;
909 CFDictionaryRef buf_attr;
940 case MKTAG(
'a',
'p',
'c',
'o'):
941 case
MKTAG(
'a',
'p',
'c',
's'):
942 case
MKTAG(
'a',
'p',
'c',
'n'):
943 case
MKTAG(
'a',
'p',
'c',
'h'):
944 case
MKTAG(
'a',
'p',
'4',
'h'):
945 case
MKTAG(
'a',
'p',
'4',
'x'):
946 videotoolbox->cm_codec_type =
av_bswap32(avctx->codec_tag);
960 #if defined(MAC_OS_X_VERSION_10_9) && !TARGET_OS_IPHONE && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_9) && AV_HAS_BUILTIN(__builtin_available)
962 if (__builtin_available(macOS 10.9, *)) {
963 VTRegisterProfessionalVideoWorkflowVideoDecoders();
968 #if defined(MAC_OS_VERSION_11_0) && !TARGET_OS_IPHONE && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_VERSION_11_0) && AV_HAS_BUILTIN(__builtin_available)
969 if (__builtin_available(macOS 11.0, *)) {
970 VTRegisterSupplementalVideoDecoderIfAvailable(videotoolbox->
cm_codec_type);
974 decoder_spec = videotoolbox_decoder_config_create(videotoolbox->
cm_codec_type, avctx);
987 CFRelease(decoder_spec);
993 buf_attr = videotoolbox_buffer_attributes_create(avctx->
width,
997 decoder_cb.decompressionOutputCallback = videotoolbox_decoder_callback;
1008 CFRelease(decoder_spec);
1010 CFRelease(buf_attr);
1013 case kVTVideoDecoderNotAvailableNowErr:
1016 case kVTVideoDecoderUnsupportedDataFormatErr:
1019 case kVTCouldNotFindVideoDecoderErr:
1022 case kVTVideoDecoderMalfunctionErr:
1025 case kVTVideoDecoderBadDataErr:
1036 static const char *videotoolbox_error_string(OSStatus
status)
1039 case kVTVideoDecoderBadDataErr:
1041 case kVTVideoDecoderMalfunctionErr:
1042 return "decoder malfunction";
1043 case kVTInvalidSessionErr:
1044 return "invalid session";
1058 videotoolbox_stop(avctx);
1059 if (videotoolbox_start(avctx) != 0) {
1067 status = videotoolbox_session_decode_frame(avctx);
1069 if (
status == kVTVideoDecoderMalfunctionErr ||
status == kVTInvalidSessionErr)
1078 return videotoolbox_buffer_create(avctx,
frame);
1099 frame->crop_right = 0;
1100 frame->crop_left = 0;
1101 frame->crop_top = 0;
1102 frame->crop_bottom = 0;
1115 static int videotoolbox_hevc_decode_params(
AVCodecContext *avctx,
1160 static int videotoolbox_prores_start_frame(
AVCodecContext *avctx,
1169 if (!
ctx->first_field)
1175 static int videotoolbox_prores_decode_slice(
AVCodecContext *avctx,
1187 if (!
ctx->first_field)
1204 #if HAVE_KCVPIXELFORMATTYPE_444YPCBCR16BIPLANARVIDEORANGE
1209 #if HAVE_KCVPIXELFORMATTYPE_444YPCBCR10BIPLANARVIDEORANGE
1211 #if HAVE_KCVPIXELFORMATTYPE_444YPCBCR8BIPLANARVIDEORANGE
1218 #if HAVE_KCVPIXELFORMATTYPE_422YPCBCR10BIPLANARVIDEORANGE
1220 #if HAVE_KCVPIXELFORMATTYPE_422YPCBCR8BIPLANARVIDEORANGE
1227 #if HAVE_KCVPIXELFORMATTYPE_420YPCBCR10BIPLANARVIDEORANGE
1243 if (cv_pix_fmt_type == 0) {
1244 cv_pix_fmt_type = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
1246 ret->cv_pix_fmt_type = cv_pix_fmt_type;
1264 return videotoolbox_start(avctx);
1268 "Either hw_frames_ctx or hw_device_ctx must be set.\n");
1289 hw_frames->
sw_format = videotoolbox_best_pixel_format(avctx);
1292 hw_ctx = hw_frames->
hwctx;
1315 "Failed to map underlying FFmpeg pixel format %s (%s range) to "
1316 "a VideoToolbox format!\n",
1317 attempted_format ? attempted_format->
name :
"<unknown>",
1323 err = videotoolbox_start(avctx);
1342 frames_ctx->
sw_format = videotoolbox_best_pixel_format(avctx);
1348 .
p.
name =
"h263_videotoolbox",
1353 .start_frame = videotoolbox_mpeg_start_frame,
1354 .decode_slice = videotoolbox_mpeg_decode_slice,
1355 .end_frame = videotoolbox_mpeg_end_frame,
1363 .
p.
name =
"hevc_videotoolbox",
1368 .start_frame = videotoolbox_hevc_start_frame,
1369 .decode_slice = videotoolbox_hevc_decode_slice,
1370 .decode_params = videotoolbox_hevc_decode_params,
1371 .end_frame = videotoolbox_hevc_end_frame,
1379 .
p.
name =
"h264_videotoolbox",
1387 .end_frame = videotoolbox_h264_end_frame,
1395 .
p.
name =
"mpeg1_videotoolbox",
1400 .start_frame = videotoolbox_mpeg_start_frame,
1401 .decode_slice = videotoolbox_mpeg_decode_slice,
1402 .end_frame = videotoolbox_mpeg_end_frame,
1410 .
p.
name =
"mpeg2_videotoolbox",
1415 .start_frame = videotoolbox_mpeg_start_frame,
1416 .decode_slice = videotoolbox_mpeg_decode_slice,
1417 .end_frame = videotoolbox_mpeg_end_frame,
1425 .
p.
name =
"mpeg4_videotoolbox",
1430 .start_frame = videotoolbox_mpeg_start_frame,
1431 .decode_slice = videotoolbox_mpeg_decode_slice,
1432 .end_frame = videotoolbox_mpeg_end_frame,
1440 .
p.
name =
"prores_videotoolbox",
1445 .start_frame = videotoolbox_prores_start_frame,
1446 .decode_slice = videotoolbox_prores_decode_slice,
1447 .end_frame = videotoolbox_prores_end_frame,