24 #include "config_components.h"
39 #include <Availability.h>
40 #include <AvailabilityMacros.h>
41 #include <TargetConditionals.h>
43 #ifndef kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder
44 # define kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder CFSTR("RequireHardwareAcceleratedVideoDecoder")
46 #ifndef kVTVideoDecoderSpecification_EnableHardwareAcceleratedVideoDecoder
47 # define kVTVideoDecoderSpecification_EnableHardwareAcceleratedVideoDecoder CFSTR("EnableHardwareAcceleratedVideoDecoder")
50 #if !HAVE_KCMVIDEOCODECTYPE_HEVC
54 #if !HAVE_KCMVIDEOCODECTYPE_VP9
58 #define VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING 12
69 CVPixelBufferRelease(
ref->pixbuf);
115 if (
ref->hw_frames_ctx) {
155 #define AV_W8(p, v) *(p) = (v)
163 for (
i = 0;
i < src_size;
i++) {
164 if (
i + 2 < src_size &&
166 src[
i + 1] == 0x00 &&
167 src[
i + 2] <= 0x03) {
194 int vt_extradata_size;
195 uint8_t *vt_extradata;
197 vt_extradata_size = 6 + 2 + sps_size + 3 + pps_size;
198 vt_extradata =
av_malloc(vt_extradata_size);
206 AV_W8(p + 1,
h->ps.sps->data[1]);
207 AV_W8(p + 2,
h->ps.sps->data[2]);
208 AV_W8(p + 3,
h->ps.sps->data[3]);
213 p +=
escape_ps(p,
h->ps.sps->data,
h->ps.sps->data_size);
217 p +=
escape_ps(p,
h->ps.pps->data,
h->ps.pps->data_size);
219 av_assert0(p - vt_extradata == vt_extradata_size);
224 memcpy(vtctx->
sps,
h->ps.sps->data + 1, 3);
226 data = CFDataCreate(kCFAllocatorDefault, vt_extradata, vt_extradata_size);
234 int i, num_vps = 0, num_sps = 0, num_pps = 0;
240 uint8_t parallelismType;
243 int vt_extradata_size = 23 + 3 + 3 + 3;
244 uint8_t *vt_extradata;
246 #define COUNT_SIZE_PS(T, t) \
247 for (i = 0; i < HEVC_MAX_##T##PS_COUNT; i++) { \
248 if (h->ps.t##ps_list[i]) { \
249 const HEVC##T##PS *lps = h->ps.t##ps_list[i]; \
250 vt_extradata_size += 2 + escape_ps(NULL, lps->data, lps->data_size); \
259 vt_extradata =
av_malloc(vt_extradata_size);
277 for (
i = 0;
i < 4;
i++) {
312 else if (
pps->entropy_coding_sync_enabled_flag &&
pps->tiles_enabled_flag)
314 else if (
pps->entropy_coding_sync_enabled_flag)
316 else if (
pps->tiles_enabled_flag)
320 AV_W8(p + 15, 0xfc | parallelismType);
326 AV_W8(p + 16,
sps->chroma_format_idc | 0xfc);
332 AV_W8(p + 17, (
sps->bit_depth - 8) | 0xf8);
338 AV_W8(p + 18, (
sps->bit_depth_chroma - 8) | 0xf8);
349 AV_W8(p + 21, 0 << 6 |
350 sps->max_sub_layers << 3 |
351 sps->temporal_id_nesting_flag << 2 |
359 #define APPEND_PS(T, t) \
366 HEVC_NAL_##T##PS & 0x3f); \
368 AV_WB16(p + 1, num_##t##ps); \
370 for (i = 0; i < HEVC_MAX_##T##PS_COUNT; i++) { \
371 if (h->ps.t##ps_list[i]) { \
372 const HEVC##T##PS *lps = h->ps.t##ps_list[i]; \
373 int size = escape_ps(p + 2, lps->data, lps->data_size); \
385 av_assert0(p - vt_extradata == vt_extradata_size);
387 data = CFDataCreate(kCFAllocatorDefault, vt_extradata, vt_extradata_size);
399 if (
h->is_avc == 1) {
416 memcpy(vtctx->
sps,
h->ps.sps->data + 1, 3);
464 #if CONFIG_VIDEOTOOLBOX
491 VTDecompressionSessionInvalidate(videotoolbox->
session);
492 CFRelease(videotoolbox->
session);
505 CVPixelBufferRelease(vtctx->
frame);
508 videotoolbox_stop(avctx);
519 CVPixelBufferRef pixbuf = (CVPixelBufferRef)vtctx->
frame;
520 OSType pixel_format = CVPixelBufferGetPixelFormatType(pixbuf);
522 int width = CVPixelBufferGetWidth(pixbuf);
523 int height = CVPixelBufferGetHeight(pixbuf);
537 CVPixelBufferRelease(
ref->pixbuf);
573 if (!
ref->hw_frames_ctx)
579 static void videotoolbox_write_mp4_descr_length(
PutByteContext *pb,
int length)
584 for (
i = 3;
i >= 0;
i--) {
585 b = (length >> (
i * 7)) & 0x7F;
589 bytestream2_put_byteu(pb,
b);
593 static CFDataRef videotoolbox_esds_extradata_create(
AVCodecContext *avctx)
596 uint8_t *rw_extradata;
607 bytestream2_put_byteu(&pb, 0);
611 bytestream2_put_byteu(&pb, 0x03);
612 videotoolbox_write_mp4_descr_length(&pb, full_size);
614 bytestream2_put_byteu(&pb, 0);
617 bytestream2_put_byteu(&pb, 0x04);
618 videotoolbox_write_mp4_descr_length(&pb, config_size);
619 bytestream2_put_byteu(&pb, 32);
620 bytestream2_put_byteu(&pb, 0x11);
626 bytestream2_put_byteu(&pb, 0x05);
632 bytestream2_put_byteu(&pb, 0x06);
633 bytestream2_put_byteu(&pb, 0x01);
634 bytestream2_put_byteu(&pb, 0x02);
638 data = CFDataCreate(kCFAllocatorDefault, rw_extradata,
s);
644 static CMSampleBufferRef videotoolbox_sample_buffer_create(CMFormatDescriptionRef fmt_desc,
649 CMBlockBufferRef block_buf;
650 CMSampleBufferRef sample_buf;
655 status = CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault,
666 status = CMSampleBufferCreate(kCFAllocatorDefault,
681 CFRelease(block_buf);
686 static void videotoolbox_decoder_callback(
void *opaque,
687 void *sourceFrameRefCon,
689 VTDecodeInfoFlags
flags,
690 CVImageBufferRef image_buffer,
697 CVPixelBufferRelease(vtctx->
frame);
703 "vt decoder cb: output image buffer is null: %i\n",
status);
707 vtctx->
frame = CVPixelBufferRetain(image_buffer);
710 static OSStatus videotoolbox_session_decode_frame(
AVCodecContext *avctx)
713 CMSampleBufferRef sample_buf;
717 sample_buf = videotoolbox_sample_buffer_create(videotoolbox->
cm_fmt_desc,
724 status = VTDecompressionSessionDecodeFrame(videotoolbox->
session,
730 status = VTDecompressionSessionWaitForAsynchronousFrames(videotoolbox->
session);
732 CFRelease(sample_buf);
737 static CMVideoFormatDescriptionRef videotoolbox_format_desc_create(CMVideoCodecType
codec_type,
738 CFDictionaryRef decoder_spec,
742 CMFormatDescriptionRef cm_fmt_desc;
745 status = CMVideoFormatDescriptionCreate(kCFAllocatorDefault,
758 static CFDictionaryRef videotoolbox_buffer_attributes_create(
int width,
762 CFMutableDictionaryRef buffer_attributes;
763 CFMutableDictionaryRef io_surface_properties;
764 CFNumberRef cv_pix_fmt;
768 w = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &
width);
769 h = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &
height);
770 cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &
pix_fmt);
772 buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
774 &kCFTypeDictionaryKeyCallBacks,
775 &kCFTypeDictionaryValueCallBacks);
776 io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
778 &kCFTypeDictionaryKeyCallBacks,
779 &kCFTypeDictionaryValueCallBacks);
782 CFDictionarySetValue(buffer_attributes, kCVPixelBufferPixelFormatTypeKey, cv_pix_fmt);
783 CFDictionarySetValue(buffer_attributes, kCVPixelBufferIOSurfacePropertiesKey, io_surface_properties);
784 CFDictionarySetValue(buffer_attributes, kCVPixelBufferWidthKey,
w);
785 CFDictionarySetValue(buffer_attributes, kCVPixelBufferHeightKey,
h);
787 CFDictionarySetValue(buffer_attributes, kCVPixelBufferOpenGLESCompatibilityKey, kCFBooleanTrue);
789 CFDictionarySetValue(buffer_attributes, kCVPixelBufferIOSurfaceOpenGLTextureCompatibilityKey, kCFBooleanTrue);
792 CFRelease(io_surface_properties);
793 CFRelease(cv_pix_fmt);
797 return buffer_attributes;
800 static CFDictionaryRef videotoolbox_decoder_config_create(CMVideoCodecType
codec_type,
803 CFMutableDictionaryRef avc_info;
806 CFMutableDictionaryRef config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
808 &kCFTypeDictionaryKeyCallBacks,
809 &kCFTypeDictionaryValueCallBacks);
811 CFDictionarySetValue(config_info,
817 avc_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
819 &kCFTypeDictionaryKeyCallBacks,
820 &kCFTypeDictionaryValueCallBacks);
823 case kCMVideoCodecType_MPEG4Video :
825 data = videotoolbox_esds_extradata_create(avctx);
827 CFDictionarySetValue(avc_info, CFSTR(
"esds"),
data);
829 case kCMVideoCodecType_H264 :
832 CFDictionarySetValue(avc_info, CFSTR(
"avcC"),
data);
837 CFDictionarySetValue(avc_info, CFSTR(
"hvcC"),
data);
839 #if CONFIG_VP9_VIDEOTOOLBOX_HWACCEL
843 CFDictionarySetValue(avc_info, CFSTR(
"vpcC"),
data);
850 CFDictionarySetValue(config_info,
851 kCMFormatDescriptionExtension_SampleDescriptionExtensionAtoms,
865 VTDecompressionOutputCallbackRecord decoder_cb;
866 CFDictionaryRef decoder_spec;
867 CFDictionaryRef buf_attr;
898 case MKTAG(
'a',
'p',
'c',
'o'):
899 case
MKTAG(
'a',
'p',
'c',
's'):
900 case
MKTAG(
'a',
'p',
'c',
'n'):
901 case
MKTAG(
'a',
'p',
'c',
'h'):
902 case
MKTAG(
'a',
'p',
'4',
'h'):
903 case
MKTAG(
'a',
'p',
'4',
'x'):
904 videotoolbox->cm_codec_type =
av_bswap32(avctx->codec_tag);
915 #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)
917 if (__builtin_available(macOS 10.9, *)) {
918 VTRegisterProfessionalVideoWorkflowVideoDecoders();
923 #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)
924 if (__builtin_available(macOS 11.0, *)) {
925 VTRegisterSupplementalVideoDecoderIfAvailable(videotoolbox->
cm_codec_type);
929 decoder_spec = videotoolbox_decoder_config_create(videotoolbox->
cm_codec_type, avctx);
942 CFRelease(decoder_spec);
948 buf_attr = videotoolbox_buffer_attributes_create(avctx->
width,
952 decoder_cb.decompressionOutputCallback = videotoolbox_decoder_callback;
963 CFRelease(decoder_spec);
968 case kVTVideoDecoderNotAvailableNowErr:
971 case kVTVideoDecoderUnsupportedDataFormatErr:
974 case kVTCouldNotFindVideoDecoderErr:
977 case kVTVideoDecoderMalfunctionErr:
980 case kVTVideoDecoderBadDataErr:
991 static const char *videotoolbox_error_string(OSStatus
status)
994 case kVTVideoDecoderBadDataErr:
996 case kVTVideoDecoderMalfunctionErr:
997 return "decoder malfunction";
998 case kVTInvalidSessionErr:
999 return "invalid session";
1013 videotoolbox_stop(avctx);
1014 if (videotoolbox_start(avctx) != 0) {
1022 status = videotoolbox_session_decode_frame(avctx);
1024 if (
status == kVTVideoDecoderMalfunctionErr ||
status == kVTInvalidSessionErr)
1030 if (!vtctx->
frame) {
1035 return videotoolbox_buffer_create(avctx,
frame);
1063 static int videotoolbox_hevc_decode_params(
AVCodecContext *avctx,
1078 h->output_frame->crop_right = 0;
1079 h->output_frame->crop_left = 0;
1080 h->output_frame->crop_top = 0;
1081 h->output_frame->crop_bottom = 0;
1112 static int videotoolbox_prores_start_frame(
AVCodecContext *avctx,
1119 static int videotoolbox_prores_decode_slice(
AVCodecContext *avctx,
1148 #if HAVE_KCVPIXELFORMATTYPE_444YPCBCR16BIPLANARVIDEORANGE
1153 #if HAVE_KCVPIXELFORMATTYPE_444YPCBCR10BIPLANARVIDEORANGE
1155 #if HAVE_KCVPIXELFORMATTYPE_444YPCBCR8BIPLANARVIDEORANGE
1162 #if HAVE_KCVPIXELFORMATTYPE_422YPCBCR10BIPLANARVIDEORANGE
1164 #if HAVE_KCVPIXELFORMATTYPE_422YPCBCR8BIPLANARVIDEORANGE
1171 #if HAVE_KCVPIXELFORMATTYPE_420YPCBCR10BIPLANARVIDEORANGE
1187 if (cv_pix_fmt_type == 0) {
1188 cv_pix_fmt_type = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
1190 ret->cv_pix_fmt_type = cv_pix_fmt_type;
1207 return videotoolbox_start(avctx);
1211 "Either hw_frames_ctx or hw_device_ctx must be set.\n");
1232 hw_frames->
sw_format = videotoolbox_best_pixel_format(avctx);
1256 "Failed to map underlying FFmpeg pixel format %s (%s range) to "
1257 "a VideoToolbox format!\n",
1258 attempted_format ? attempted_format->
name :
"<unknown>",
1264 err = videotoolbox_start(avctx);
1283 frames_ctx->
sw_format = videotoolbox_best_pixel_format(avctx);
1289 .
p.
name =
"h263_videotoolbox",
1294 .start_frame = videotoolbox_mpeg_start_frame,
1295 .decode_slice = videotoolbox_mpeg_decode_slice,
1296 .end_frame = videotoolbox_mpeg_end_frame,
1304 .
p.
name =
"hevc_videotoolbox",
1309 .start_frame = videotoolbox_hevc_start_frame,
1310 .decode_slice = videotoolbox_hevc_decode_slice,
1311 .decode_params = videotoolbox_hevc_decode_params,
1312 .end_frame = videotoolbox_hevc_end_frame,
1320 .
p.
name =
"h264_videotoolbox",
1328 .end_frame = videotoolbox_h264_end_frame,
1336 .
p.
name =
"mpeg1_videotoolbox",
1341 .start_frame = videotoolbox_mpeg_start_frame,
1342 .decode_slice = videotoolbox_mpeg_decode_slice,
1343 .end_frame = videotoolbox_mpeg_end_frame,
1351 .
p.
name =
"mpeg2_videotoolbox",
1356 .start_frame = videotoolbox_mpeg_start_frame,
1357 .decode_slice = videotoolbox_mpeg_decode_slice,
1358 .end_frame = videotoolbox_mpeg_end_frame,
1366 .
p.
name =
"mpeg4_videotoolbox",
1371 .start_frame = videotoolbox_mpeg_start_frame,
1372 .decode_slice = videotoolbox_mpeg_decode_slice,
1373 .end_frame = videotoolbox_mpeg_end_frame,
1381 .
p.
name =
"prores_videotoolbox",
1386 .start_frame = videotoolbox_prores_start_frame,
1387 .decode_slice = videotoolbox_prores_decode_slice,
1388 .end_frame = videotoolbox_prores_end_frame,
1397 #if FF_API_VT_HWACCEL_CONTEXT
1400 return videotoolbox_alloc_context_with_pix_fmt(
AV_PIX_FMT_NONE,
false);
1405 return av_videotoolbox_default_init2(avctx,
NULL);
1421 videotoolbox_stop(avctx);