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);
561 hw_ctx = hw_frames->
hwctx;
576 if (!
ref->hw_frames_ctx)
582 static void videotoolbox_write_mp4_descr_length(
PutByteContext *pb,
int length)
587 for (
i = 3;
i >= 0;
i--) {
588 b = (length >> (
i * 7)) & 0x7F;
592 bytestream2_put_byteu(pb,
b);
596 static CFDataRef videotoolbox_esds_extradata_create(
AVCodecContext *avctx)
599 uint8_t *rw_extradata;
610 bytestream2_put_byteu(&pb, 0);
614 bytestream2_put_byteu(&pb, 0x03);
615 videotoolbox_write_mp4_descr_length(&pb, full_size);
617 bytestream2_put_byteu(&pb, 0);
620 bytestream2_put_byteu(&pb, 0x04);
621 videotoolbox_write_mp4_descr_length(&pb, config_size);
622 bytestream2_put_byteu(&pb, 32);
623 bytestream2_put_byteu(&pb, 0x11);
629 bytestream2_put_byteu(&pb, 0x05);
635 bytestream2_put_byteu(&pb, 0x06);
636 bytestream2_put_byteu(&pb, 0x01);
637 bytestream2_put_byteu(&pb, 0x02);
641 data = CFDataCreate(kCFAllocatorDefault, rw_extradata,
s);
647 static CMSampleBufferRef videotoolbox_sample_buffer_create(CMFormatDescriptionRef fmt_desc,
652 CMBlockBufferRef block_buf;
653 CMSampleBufferRef sample_buf;
658 status = CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault,
669 status = CMSampleBufferCreate(kCFAllocatorDefault,
684 CFRelease(block_buf);
689 static void videotoolbox_decoder_callback(
void *opaque,
690 void *sourceFrameRefCon,
692 VTDecodeInfoFlags
flags,
693 CVImageBufferRef image_buffer,
700 CVPixelBufferRelease(vtctx->
frame);
706 "vt decoder cb: output image buffer is null: %i\n",
status);
710 vtctx->
frame = CVPixelBufferRetain(image_buffer);
713 static OSStatus videotoolbox_session_decode_frame(
AVCodecContext *avctx)
716 CMSampleBufferRef sample_buf;
720 sample_buf = videotoolbox_sample_buffer_create(videotoolbox->
cm_fmt_desc,
727 status = VTDecompressionSessionDecodeFrame(videotoolbox->
session,
733 status = VTDecompressionSessionWaitForAsynchronousFrames(videotoolbox->
session);
735 CFRelease(sample_buf);
740 static CMVideoFormatDescriptionRef videotoolbox_format_desc_create(CMVideoCodecType
codec_type,
741 CFDictionaryRef decoder_spec,
745 CMFormatDescriptionRef cm_fmt_desc;
748 status = CMVideoFormatDescriptionCreate(kCFAllocatorDefault,
761 static CFDictionaryRef videotoolbox_buffer_attributes_create(
int width,
765 CFMutableDictionaryRef buffer_attributes;
766 CFMutableDictionaryRef io_surface_properties;
767 CFNumberRef cv_pix_fmt;
771 w = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &
width);
772 h = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &
height);
773 cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &
pix_fmt);
775 buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
777 &kCFTypeDictionaryKeyCallBacks,
778 &kCFTypeDictionaryValueCallBacks);
779 io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
781 &kCFTypeDictionaryKeyCallBacks,
782 &kCFTypeDictionaryValueCallBacks);
785 CFDictionarySetValue(buffer_attributes, kCVPixelBufferPixelFormatTypeKey, cv_pix_fmt);
786 CFDictionarySetValue(buffer_attributes, kCVPixelBufferIOSurfacePropertiesKey, io_surface_properties);
787 CFDictionarySetValue(buffer_attributes, kCVPixelBufferWidthKey,
w);
788 CFDictionarySetValue(buffer_attributes, kCVPixelBufferHeightKey,
h);
790 CFDictionarySetValue(buffer_attributes, kCVPixelBufferOpenGLESCompatibilityKey, kCFBooleanTrue);
792 CFDictionarySetValue(buffer_attributes, kCVPixelBufferIOSurfaceOpenGLTextureCompatibilityKey, kCFBooleanTrue);
795 CFRelease(io_surface_properties);
796 CFRelease(cv_pix_fmt);
800 return buffer_attributes;
803 static CFDictionaryRef videotoolbox_decoder_config_create(CMVideoCodecType
codec_type,
806 CFMutableDictionaryRef avc_info;
809 CFMutableDictionaryRef config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
811 &kCFTypeDictionaryKeyCallBacks,
812 &kCFTypeDictionaryValueCallBacks);
814 CFDictionarySetValue(config_info,
820 avc_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
822 &kCFTypeDictionaryKeyCallBacks,
823 &kCFTypeDictionaryValueCallBacks);
826 case kCMVideoCodecType_MPEG4Video :
828 data = videotoolbox_esds_extradata_create(avctx);
830 CFDictionarySetValue(avc_info, CFSTR(
"esds"),
data);
832 case kCMVideoCodecType_H264 :
835 CFDictionarySetValue(avc_info, CFSTR(
"avcC"),
data);
840 CFDictionarySetValue(avc_info, CFSTR(
"hvcC"),
data);
842 #if CONFIG_VP9_VIDEOTOOLBOX_HWACCEL
846 CFDictionarySetValue(avc_info, CFSTR(
"vpcC"),
data);
853 CFDictionarySetValue(config_info,
854 kCMFormatDescriptionExtension_SampleDescriptionExtensionAtoms,
868 VTDecompressionOutputCallbackRecord decoder_cb;
869 CFDictionaryRef decoder_spec;
870 CFDictionaryRef buf_attr;
901 case MKTAG(
'a',
'p',
'c',
'o'):
902 case
MKTAG(
'a',
'p',
'c',
's'):
903 case
MKTAG(
'a',
'p',
'c',
'n'):
904 case
MKTAG(
'a',
'p',
'c',
'h'):
905 case
MKTAG(
'a',
'p',
'4',
'h'):
906 case
MKTAG(
'a',
'p',
'4',
'x'):
907 videotoolbox->cm_codec_type =
av_bswap32(avctx->codec_tag);
918 #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)
920 if (__builtin_available(macOS 10.9, *)) {
921 VTRegisterProfessionalVideoWorkflowVideoDecoders();
926 #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)
927 if (__builtin_available(macOS 11.0, *)) {
928 VTRegisterSupplementalVideoDecoderIfAvailable(videotoolbox->
cm_codec_type);
932 decoder_spec = videotoolbox_decoder_config_create(videotoolbox->
cm_codec_type, avctx);
945 CFRelease(decoder_spec);
951 buf_attr = videotoolbox_buffer_attributes_create(avctx->
width,
955 decoder_cb.decompressionOutputCallback = videotoolbox_decoder_callback;
966 CFRelease(decoder_spec);
971 case kVTVideoDecoderNotAvailableNowErr:
974 case kVTVideoDecoderUnsupportedDataFormatErr:
977 case kVTCouldNotFindVideoDecoderErr:
980 case kVTVideoDecoderMalfunctionErr:
983 case kVTVideoDecoderBadDataErr:
994 static const char *videotoolbox_error_string(OSStatus
status)
997 case kVTVideoDecoderBadDataErr:
999 case kVTVideoDecoderMalfunctionErr:
1000 return "decoder malfunction";
1001 case kVTInvalidSessionErr:
1002 return "invalid session";
1016 videotoolbox_stop(avctx);
1017 if (videotoolbox_start(avctx) != 0) {
1025 status = videotoolbox_session_decode_frame(avctx);
1027 if (
status == kVTVideoDecoderMalfunctionErr ||
status == kVTInvalidSessionErr)
1033 if (!vtctx->
frame) {
1038 return videotoolbox_buffer_create(avctx,
frame);
1066 static int videotoolbox_hevc_decode_params(
AVCodecContext *avctx,
1081 h->output_frame->crop_right = 0;
1082 h->output_frame->crop_left = 0;
1083 h->output_frame->crop_top = 0;
1084 h->output_frame->crop_bottom = 0;
1115 static int videotoolbox_prores_start_frame(
AVCodecContext *avctx,
1122 static int videotoolbox_prores_decode_slice(
AVCodecContext *avctx,
1151 #if HAVE_KCVPIXELFORMATTYPE_444YPCBCR16BIPLANARVIDEORANGE
1156 #if HAVE_KCVPIXELFORMATTYPE_444YPCBCR10BIPLANARVIDEORANGE
1158 #if HAVE_KCVPIXELFORMATTYPE_444YPCBCR8BIPLANARVIDEORANGE
1165 #if HAVE_KCVPIXELFORMATTYPE_422YPCBCR10BIPLANARVIDEORANGE
1167 #if HAVE_KCVPIXELFORMATTYPE_422YPCBCR8BIPLANARVIDEORANGE
1174 #if HAVE_KCVPIXELFORMATTYPE_420YPCBCR10BIPLANARVIDEORANGE
1190 if (cv_pix_fmt_type == 0) {
1191 cv_pix_fmt_type = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
1193 ret->cv_pix_fmt_type = cv_pix_fmt_type;
1211 return videotoolbox_start(avctx);
1215 "Either hw_frames_ctx or hw_device_ctx must be set.\n");
1236 hw_frames->
sw_format = videotoolbox_best_pixel_format(avctx);
1239 hw_ctx = hw_frames->
hwctx;
1262 "Failed to map underlying FFmpeg pixel format %s (%s range) to "
1263 "a VideoToolbox format!\n",
1264 attempted_format ? attempted_format->
name :
"<unknown>",
1270 err = videotoolbox_start(avctx);
1289 frames_ctx->
sw_format = videotoolbox_best_pixel_format(avctx);
1295 .
p.
name =
"h263_videotoolbox",
1300 .start_frame = videotoolbox_mpeg_start_frame,
1301 .decode_slice = videotoolbox_mpeg_decode_slice,
1302 .end_frame = videotoolbox_mpeg_end_frame,
1310 .
p.
name =
"hevc_videotoolbox",
1315 .start_frame = videotoolbox_hevc_start_frame,
1316 .decode_slice = videotoolbox_hevc_decode_slice,
1317 .decode_params = videotoolbox_hevc_decode_params,
1318 .end_frame = videotoolbox_hevc_end_frame,
1326 .
p.
name =
"h264_videotoolbox",
1334 .end_frame = videotoolbox_h264_end_frame,
1342 .
p.
name =
"mpeg1_videotoolbox",
1347 .start_frame = videotoolbox_mpeg_start_frame,
1348 .decode_slice = videotoolbox_mpeg_decode_slice,
1349 .end_frame = videotoolbox_mpeg_end_frame,
1357 .
p.
name =
"mpeg2_videotoolbox",
1362 .start_frame = videotoolbox_mpeg_start_frame,
1363 .decode_slice = videotoolbox_mpeg_decode_slice,
1364 .end_frame = videotoolbox_mpeg_end_frame,
1372 .
p.
name =
"mpeg4_videotoolbox",
1377 .start_frame = videotoolbox_mpeg_start_frame,
1378 .decode_slice = videotoolbox_mpeg_decode_slice,
1379 .end_frame = videotoolbox_mpeg_end_frame,
1387 .
p.
name =
"prores_videotoolbox",
1392 .start_frame = videotoolbox_prores_start_frame,
1393 .decode_slice = videotoolbox_prores_decode_slice,
1394 .end_frame = videotoolbox_prores_end_frame,