2 * Videotoolbox hardware acceleration
4 * copyright (c) 2012 Sebastien Zwickert
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #if CONFIG_VIDEOTOOLBOX
25 # include "videotoolbox.h"
26 # include "libavutil/hwcontext_videotoolbox.h"
30 #include "vda_vt_internal.h"
31 #include "libavutil/avutil.h"
32 #include "libavutil/hwcontext.h"
33 #include "bytestream.h"
36 #include "mpegvideo.h"
37 #include <TargetConditionals.h>
39 #ifndef kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder
40 # define kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder CFSTR("RequireHardwareAcceleratedVideoDecoder")
43 #if !HAVE_KCMVIDEOCODECTYPE_HEVC
44 enum { kCMVideoCodecType_HEVC = 'hvc1' };
47 #define VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING 12
49 static void videotoolbox_buffer_release(void *opaque, uint8_t *data)
51 CVPixelBufferRef cv_buffer = (CVImageBufferRef)data;
52 CVPixelBufferRelease(cv_buffer);
55 static int videotoolbox_buffer_copy(VTContext *vtctx,
56 const uint8_t *buffer,
61 tmp = av_fast_realloc(vtctx->bitstream,
62 &vtctx->allocated_size,
66 return AVERROR(ENOMEM);
68 vtctx->bitstream = tmp;
69 memcpy(vtctx->bitstream, buffer, size);
70 vtctx->bitstream_size = size;
75 int ff_videotoolbox_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
77 frame->width = avctx->width;
78 frame->height = avctx->height;
79 frame->format = avctx->pix_fmt;
80 frame->buf[0] = av_buffer_alloc(1);
83 return AVERROR(ENOMEM);
88 #define AV_W8(p, v) *(p) = (v)
90 CFDataRef ff_videotoolbox_avcc_extradata_create(AVCodecContext *avctx)
92 H264Context *h = avctx->priv_data;
93 CFDataRef data = NULL;
95 int vt_extradata_size = 6 + 2 + h->ps.sps->data_size + 3 + h->ps.pps->data_size;
96 uint8_t *vt_extradata = av_malloc(vt_extradata_size);
102 AV_W8(p + 0, 1); /* version */
103 AV_W8(p + 1, h->ps.sps->data[1]); /* profile */
104 AV_W8(p + 2, h->ps.sps->data[2]); /* profile compat */
105 AV_W8(p + 3, h->ps.sps->data[3]); /* level */
106 AV_W8(p + 4, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 3 (11) */
107 AV_W8(p + 5, 0xe1); /* 3 bits reserved (111) + 5 bits number of sps (00001) */
108 AV_WB16(p + 6, h->ps.sps->data_size);
109 memcpy(p + 8, h->ps.sps->data, h->ps.sps->data_size);
110 p += 8 + h->ps.sps->data_size;
111 AV_W8(p + 0, 1); /* number of pps */
112 AV_WB16(p + 1, h->ps.pps->data_size);
113 memcpy(p + 3, h->ps.pps->data, h->ps.pps->data_size);
115 p += 3 + h->ps.pps->data_size;
116 av_assert0(p - vt_extradata == vt_extradata_size);
118 data = CFDataCreate(kCFAllocatorDefault, vt_extradata, vt_extradata_size);
119 av_free(vt_extradata);
123 CFDataRef ff_videotoolbox_hvcc_extradata_create(AVCodecContext *avctx)
125 HEVCContext *h = avctx->priv_data;
126 const HEVCVPS *vps = (const HEVCVPS *)h->ps.vps_list[0]->data;
127 const HEVCSPS *sps = (const HEVCSPS *)h->ps.sps_list[0]->data;
129 const HEVCPPS *pps = h->ps.pps;
130 PTLCommon ptlc = vps->ptl.general_ptl;
132 uint8_t parallelismType;
133 CFDataRef data = NULL;
135 int vt_extradata_size = 23 + 5 + vps->data_size + 5 + sps->data_size + 3;
136 uint8_t *vt_extradata;
138 for (i = 0; i < MAX_PPS_COUNT; i++) {
139 if (h->ps.pps_list[i]) {
140 const HEVCPPS *pps = (const HEVCPPS *)h->ps.pps_list[i]->data;
141 vt_extradata_size += 2 + pps->data_size;
146 vt_extradata = av_malloc(vt_extradata_size);
151 /* unsigned int(8) configurationVersion = 1; */
155 * unsigned int(2) general_profile_space;
156 * unsigned int(1) general_tier_flag;
157 * unsigned int(5) general_profile_idc;
159 AV_W8(p + 1, ptlc.profile_space << 6 |
160 ptlc.tier_flag << 5 |
163 /* unsigned int(32) general_profile_compatibility_flags; */
164 memcpy(p + 2, ptlc.profile_compatibility_flag, 4);
166 /* unsigned int(48) general_constraint_indicator_flags; */
167 AV_W8(p + 6, ptlc.progressive_source_flag << 7 |
168 ptlc.interlaced_source_flag << 6 |
169 ptlc.non_packed_constraint_flag << 5 |
170 ptlc.frame_only_constraint_flag << 4);
174 /* unsigned int(8) general_level_idc; */
175 AV_W8(p + 12, ptlc.level_idc);
178 * bit(4) reserved = ‘1111’b;
179 * unsigned int(12) min_spatial_segmentation_idc;
181 AV_W8(p + 13, 0xf0 | (vui.min_spatial_segmentation_idc >> 4));
182 AV_W8(p + 14, vui.min_spatial_segmentation_idc & 0xff);
185 * bit(6) reserved = ‘111111’b;
186 * unsigned int(2) parallelismType;
188 if (!vui.min_spatial_segmentation_idc)
190 else if (pps->entropy_coding_sync_enabled_flag && pps->tiles_enabled_flag)
192 else if (pps->entropy_coding_sync_enabled_flag)
194 else if (pps->tiles_enabled_flag)
198 AV_W8(p + 15, 0xfc | parallelismType);
201 * bit(6) reserved = ‘111111’b;
202 * unsigned int(2) chromaFormat;
204 AV_W8(p + 16, sps->chroma_format_idc | 0xfc);
207 * bit(5) reserved = ‘11111’b;
208 * unsigned int(3) bitDepthLumaMinus8;
210 AV_W8(p + 17, (sps->bit_depth - 8) | 0xfc);
213 * bit(5) reserved = ‘11111’b;
214 * unsigned int(3) bitDepthChromaMinus8;
216 AV_W8(p + 18, (sps->bit_depth_chroma - 8) | 0xfc);
218 /* bit(16) avgFrameRate; */
222 * bit(2) constantFrameRate;
223 * bit(3) numTemporalLayers;
224 * bit(1) temporalIdNested;
225 * unsigned int(2) lengthSizeMinusOne;
227 AV_W8(p + 21, 0 << 6 |
228 sps->max_sub_layers << 3 |
229 sps->temporal_id_nesting_flag << 2 |
232 /* unsigned int(8) numOfArrays; */
238 * bit(1) array_completeness;
239 * unsigned int(1) reserved = 0;
240 * unsigned int(6) NAL_unit_type;
243 HEVC_NAL_VPS & 0x3f);
244 /* unsigned int(16) numNalus; */
246 /* unsigned int(16) nalUnitLength; */
247 AV_WB16(p + 3, vps->data_size);
248 /* bit(8*nalUnitLength) nalUnit; */
249 memcpy(p + 5, vps->data, vps->data_size);
250 p += 5 + vps->data_size;
254 HEVC_NAL_SPS & 0x3f);
256 AV_WB16(p + 3, sps->data_size);
257 memcpy(p + 5, sps->data, sps->data_size);
258 p += 5 + sps->data_size;
262 HEVC_NAL_PPS & 0x3f);
263 AV_WB16(p + 1, num_pps);
265 for (i = 0; i < MAX_PPS_COUNT; i++) {
266 if (h->ps.pps_list[i]) {
267 const HEVCPPS *pps = (const HEVCPPS *)h->ps.pps_list[i]->data;
268 AV_WB16(p, pps->data_size);
269 memcpy(p + 2, pps->data, pps->data_size);
270 p += 2 + pps->data_size;
274 av_assert0(p - vt_extradata == vt_extradata_size);
276 data = CFDataCreate(kCFAllocatorDefault, vt_extradata, vt_extradata_size);
277 av_free(vt_extradata);
281 int ff_videotoolbox_buffer_create(VTContext *vtctx, AVFrame *frame)
283 av_buffer_unref(&frame->buf[0]);
285 frame->buf[0] = av_buffer_create((uint8_t*)vtctx->frame,
286 sizeof(vtctx->frame),
287 videotoolbox_buffer_release,
289 AV_BUFFER_FLAG_READONLY);
290 if (!frame->buf[0]) {
291 return AVERROR(ENOMEM);
294 frame->data[3] = (uint8_t*)vtctx->frame;
300 int ff_videotoolbox_h264_start_frame(AVCodecContext *avctx,
301 const uint8_t *buffer,
304 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
305 H264Context *h = avctx->priv_data;
307 vtctx->bitstream_size = 0;
309 if (h->is_avc == 1) {
310 return videotoolbox_buffer_copy(vtctx, buffer, size);
316 int ff_videotoolbox_h264_decode_slice(AVCodecContext *avctx,
317 const uint8_t *buffer,
320 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
321 H264Context *h = avctx->priv_data;
327 tmp = av_fast_realloc(vtctx->bitstream,
328 &vtctx->allocated_size,
329 vtctx->bitstream_size+size+4);
331 return AVERROR(ENOMEM);
333 vtctx->bitstream = tmp;
335 AV_WB32(vtctx->bitstream + vtctx->bitstream_size, size);
336 memcpy(vtctx->bitstream + vtctx->bitstream_size + 4, buffer, size);
338 vtctx->bitstream_size += size + 4;
343 int ff_videotoolbox_uninit(AVCodecContext *avctx)
345 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
347 av_freep(&vtctx->bitstream);
349 CVPixelBufferRelease(vtctx->frame);
355 #if CONFIG_VIDEOTOOLBOX
356 // Return the AVVideotoolboxContext that matters currently. Where it comes from
357 // depends on the API used.
358 static AVVideotoolboxContext *videotoolbox_get_context(AVCodecContext *avctx)
360 // Somewhat tricky because the user can call av_videotoolbox_default_free()
361 // at any time, even when the codec is closed.
362 if (avctx->internal && avctx->internal->hwaccel_priv_data) {
363 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
365 return vtctx->vt_ctx;
367 return avctx->hwaccel_context;
370 static int videotoolbox_buffer_create(AVCodecContext *avctx, AVFrame *frame)
372 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
373 CVPixelBufferRef pixbuf = (CVPixelBufferRef)vtctx->frame;
374 OSType pixel_format = CVPixelBufferGetPixelFormatType(pixbuf);
375 enum AVPixelFormat sw_format = av_map_videotoolbox_format_to_pixfmt(pixel_format);
376 int width = CVPixelBufferGetWidth(pixbuf);
377 int height = CVPixelBufferGetHeight(pixbuf);
378 AVHWFramesContext *cached_frames;
381 ret = ff_videotoolbox_buffer_create(vtctx, frame);
385 // Old API code path.
386 if (!vtctx->cached_hw_frames_ctx)
389 cached_frames = (AVHWFramesContext*)vtctx->cached_hw_frames_ctx->data;
391 if (cached_frames->sw_format != sw_format ||
392 cached_frames->width != width ||
393 cached_frames->height != height) {
394 AVBufferRef *hw_frames_ctx = av_hwframe_ctx_alloc(cached_frames->device_ref);
395 AVHWFramesContext *hw_frames;
397 return AVERROR(ENOMEM);
399 hw_frames = (AVHWFramesContext*)hw_frames_ctx->data;
400 hw_frames->format = cached_frames->format;
401 hw_frames->sw_format = sw_format;
402 hw_frames->width = width;
403 hw_frames->height = height;
405 ret = av_hwframe_ctx_init(hw_frames_ctx);
407 av_buffer_unref(&hw_frames_ctx);
411 av_buffer_unref(&vtctx->cached_hw_frames_ctx);
412 vtctx->cached_hw_frames_ctx = hw_frames_ctx;
415 av_buffer_unref(&frame->hw_frames_ctx);
416 frame->hw_frames_ctx = av_buffer_ref(vtctx->cached_hw_frames_ctx);
417 if (!frame->hw_frames_ctx)
418 return AVERROR(ENOMEM);
423 static void videotoolbox_write_mp4_descr_length(PutByteContext *pb, int length)
428 for (i = 3; i >= 0; i--) {
429 b = (length >> (i * 7)) & 0x7F;
433 bytestream2_put_byteu(pb, b);
437 static CFDataRef videotoolbox_esds_extradata_create(AVCodecContext *avctx)
440 uint8_t *rw_extradata;
442 int full_size = 3 + 5 + 13 + 5 + avctx->extradata_size + 3;
443 // ES_DescrTag data + DecoderConfigDescrTag + data + DecSpecificInfoTag + size + SLConfigDescriptor
444 int config_size = 13 + 5 + avctx->extradata_size;
447 if (!(rw_extradata = av_mallocz(full_size + VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING)))
450 bytestream2_init_writer(&pb, rw_extradata, full_size + VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING);
451 bytestream2_put_byteu(&pb, 0); // version
452 bytestream2_put_ne24(&pb, 0); // flags
454 // elementary stream descriptor
455 bytestream2_put_byteu(&pb, 0x03); // ES_DescrTag
456 videotoolbox_write_mp4_descr_length(&pb, full_size);
457 bytestream2_put_ne16(&pb, 0); // esid
458 bytestream2_put_byteu(&pb, 0); // stream priority (0-32)
460 // decoder configuration descriptor
461 bytestream2_put_byteu(&pb, 0x04); // DecoderConfigDescrTag
462 videotoolbox_write_mp4_descr_length(&pb, config_size);
463 bytestream2_put_byteu(&pb, 32); // object type indication. 32 = AV_CODEC_ID_MPEG4
464 bytestream2_put_byteu(&pb, 0x11); // stream type
465 bytestream2_put_ne24(&pb, 0); // buffer size
466 bytestream2_put_ne32(&pb, 0); // max bitrate
467 bytestream2_put_ne32(&pb, 0); // avg bitrate
469 // decoder specific descriptor
470 bytestream2_put_byteu(&pb, 0x05); ///< DecSpecificInfoTag
471 videotoolbox_write_mp4_descr_length(&pb, avctx->extradata_size);
473 bytestream2_put_buffer(&pb, avctx->extradata, avctx->extradata_size);
475 // SLConfigDescriptor
476 bytestream2_put_byteu(&pb, 0x06); // SLConfigDescrTag
477 bytestream2_put_byteu(&pb, 0x01); // length
478 bytestream2_put_byteu(&pb, 0x02); //
480 s = bytestream2_size_p(&pb);
482 data = CFDataCreate(kCFAllocatorDefault, rw_extradata, s);
484 av_freep(&rw_extradata);
488 static CMSampleBufferRef videotoolbox_sample_buffer_create(CMFormatDescriptionRef fmt_desc,
493 CMBlockBufferRef block_buf;
494 CMSampleBufferRef sample_buf;
499 status = CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault,// structureAllocator
500 buffer, // memoryBlock
502 kCFAllocatorNull, // blockAllocator
503 NULL, // customBlockSource
510 status = CMSampleBufferCreate(kCFAllocatorDefault, // allocator
511 block_buf, // dataBuffer
513 0, // makeDataReadyCallback
514 0, // makeDataReadyRefcon
515 fmt_desc, // formatDescription
517 0, // numSampleTimingEntries
518 NULL, // sampleTimingArray
519 0, // numSampleSizeEntries
520 NULL, // sampleSizeArray
525 CFRelease(block_buf);
530 static void videotoolbox_decoder_callback(void *opaque,
531 void *sourceFrameRefCon,
533 VTDecodeInfoFlags flags,
534 CVImageBufferRef image_buffer,
538 AVCodecContext *avctx = opaque;
539 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
542 CVPixelBufferRelease(vtctx->frame);
547 av_log(NULL, AV_LOG_DEBUG, "vt decoder cb: output image buffer is null\n");
551 vtctx->frame = CVPixelBufferRetain(image_buffer);
554 static OSStatus videotoolbox_session_decode_frame(AVCodecContext *avctx)
557 CMSampleBufferRef sample_buf;
558 AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
559 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
561 sample_buf = videotoolbox_sample_buffer_create(videotoolbox->cm_fmt_desc,
563 vtctx->bitstream_size);
568 status = VTDecompressionSessionDecodeFrame(videotoolbox->session,
571 NULL, // sourceFrameRefCon
574 status = VTDecompressionSessionWaitForAsynchronousFrames(videotoolbox->session);
576 CFRelease(sample_buf);
581 static int videotoolbox_common_end_frame(AVCodecContext *avctx, AVFrame *frame)
584 AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
585 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
587 if (!videotoolbox->session || !vtctx->bitstream)
588 return AVERROR_INVALIDDATA;
590 status = videotoolbox_session_decode_frame(avctx);
593 av_log(avctx, AV_LOG_ERROR, "Failed to decode frame (%d)\n", status);
594 return AVERROR_UNKNOWN;
598 return AVERROR_UNKNOWN;
600 return videotoolbox_buffer_create(avctx, frame);
603 static int videotoolbox_h264_end_frame(AVCodecContext *avctx)
605 H264Context *h = avctx->priv_data;
606 AVFrame *frame = h->cur_pic_ptr->f;
608 return videotoolbox_common_end_frame(avctx, frame);
611 static int videotoolbox_hevc_end_frame(AVCodecContext *avctx)
613 HEVCContext *h = avctx->priv_data;
614 AVFrame *frame = h->ref->frame;
615 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
618 ret = videotoolbox_common_end_frame(avctx, frame);
619 vtctx->bitstream_size = 0;
623 static int videotoolbox_mpeg_start_frame(AVCodecContext *avctx,
624 const uint8_t *buffer,
627 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
629 return videotoolbox_buffer_copy(vtctx, buffer, size);
632 static int videotoolbox_mpeg_decode_slice(AVCodecContext *avctx,
633 const uint8_t *buffer,
639 static int videotoolbox_mpeg_end_frame(AVCodecContext *avctx)
641 MpegEncContext *s = avctx->priv_data;
642 AVFrame *frame = s->current_picture_ptr->f;
644 return videotoolbox_common_end_frame(avctx, frame);
647 static CFDictionaryRef videotoolbox_decoder_config_create(CMVideoCodecType codec_type,
648 AVCodecContext *avctx)
650 CFMutableDictionaryRef config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
652 &kCFTypeDictionaryKeyCallBacks,
653 &kCFTypeDictionaryValueCallBacks);
655 CFDictionarySetValue(config_info,
656 kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder,
659 if (avctx->extradata_size) {
660 CFMutableDictionaryRef avc_info;
661 CFDataRef data = NULL;
663 avc_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
665 &kCFTypeDictionaryKeyCallBacks,
666 &kCFTypeDictionaryValueCallBacks);
668 switch (codec_type) {
669 case kCMVideoCodecType_MPEG4Video :
670 data = videotoolbox_esds_extradata_create(avctx);
672 CFDictionarySetValue(avc_info, CFSTR("esds"), data);
674 case kCMVideoCodecType_H264 :
675 data = ff_videotoolbox_avcc_extradata_create(avctx);
677 CFDictionarySetValue(avc_info, CFSTR("avcC"), data);
679 case kCMVideoCodecType_HEVC :
680 data = ff_videotoolbox_hvcc_extradata_create(avctx);
682 CFDictionarySetValue(avc_info, CFSTR("hvcC"), data);
688 CFDictionarySetValue(config_info,
689 kCMFormatDescriptionExtension_SampleDescriptionExtensionAtoms,
700 static CFDictionaryRef videotoolbox_buffer_attributes_create(int width,
704 CFMutableDictionaryRef buffer_attributes;
705 CFMutableDictionaryRef io_surface_properties;
706 CFNumberRef cv_pix_fmt;
710 w = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &width);
711 h = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &height);
712 cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &pix_fmt);
714 buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
716 &kCFTypeDictionaryKeyCallBacks,
717 &kCFTypeDictionaryValueCallBacks);
718 io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
720 &kCFTypeDictionaryKeyCallBacks,
721 &kCFTypeDictionaryValueCallBacks);
724 CFDictionarySetValue(buffer_attributes, kCVPixelBufferPixelFormatTypeKey, cv_pix_fmt);
725 CFDictionarySetValue(buffer_attributes, kCVPixelBufferIOSurfacePropertiesKey, io_surface_properties);
726 CFDictionarySetValue(buffer_attributes, kCVPixelBufferWidthKey, w);
727 CFDictionarySetValue(buffer_attributes, kCVPixelBufferHeightKey, h);
729 CFDictionarySetValue(buffer_attributes, kCVPixelBufferOpenGLESCompatibilityKey, kCFBooleanTrue);
731 CFDictionarySetValue(buffer_attributes, kCVPixelBufferIOSurfaceOpenGLTextureCompatibilityKey, kCFBooleanTrue);
734 CFRelease(io_surface_properties);
735 CFRelease(cv_pix_fmt);
739 return buffer_attributes;
742 static CMVideoFormatDescriptionRef videotoolbox_format_desc_create(CMVideoCodecType codec_type,
743 CFDictionaryRef decoder_spec,
747 CMFormatDescriptionRef cm_fmt_desc;
750 status = CMVideoFormatDescriptionCreate(kCFAllocatorDefault,
754 decoder_spec, // Dictionary of extension
763 static int videotoolbox_default_init(AVCodecContext *avctx)
765 AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
767 VTDecompressionOutputCallbackRecord decoder_cb;
768 CFDictionaryRef decoder_spec;
769 CFDictionaryRef buf_attr;
772 av_log(avctx, AV_LOG_ERROR, "hwaccel context is not set\n");
776 switch( avctx->codec_id ) {
777 case AV_CODEC_ID_H263 :
778 videotoolbox->cm_codec_type = kCMVideoCodecType_H263;
780 case AV_CODEC_ID_H264 :
781 videotoolbox->cm_codec_type = kCMVideoCodecType_H264;
783 case AV_CODEC_ID_HEVC :
784 videotoolbox->cm_codec_type = kCMVideoCodecType_HEVC;
786 case AV_CODEC_ID_MPEG1VIDEO :
787 videotoolbox->cm_codec_type = kCMVideoCodecType_MPEG1Video;
789 case AV_CODEC_ID_MPEG2VIDEO :
790 videotoolbox->cm_codec_type = kCMVideoCodecType_MPEG2Video;
792 case AV_CODEC_ID_MPEG4 :
793 videotoolbox->cm_codec_type = kCMVideoCodecType_MPEG4Video;
799 decoder_spec = videotoolbox_decoder_config_create(videotoolbox->cm_codec_type, avctx);
801 videotoolbox->cm_fmt_desc = videotoolbox_format_desc_create(videotoolbox->cm_codec_type,
805 if (!videotoolbox->cm_fmt_desc) {
807 CFRelease(decoder_spec);
809 av_log(avctx, AV_LOG_ERROR, "format description creation failed\n");
813 buf_attr = videotoolbox_buffer_attributes_create(avctx->width,
815 videotoolbox->cv_pix_fmt_type);
817 decoder_cb.decompressionOutputCallback = videotoolbox_decoder_callback;
818 decoder_cb.decompressionOutputRefCon = avctx;
820 status = VTDecompressionSessionCreate(NULL, // allocator
821 videotoolbox->cm_fmt_desc, // videoFormatDescription
822 decoder_spec, // videoDecoderSpecification
823 buf_attr, // destinationImageBufferAttributes
824 &decoder_cb, // outputCallback
825 &videotoolbox->session); // decompressionSessionOut
828 CFRelease(decoder_spec);
833 case kVTVideoDecoderNotAvailableNowErr:
834 av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox session not available.\n");
835 return AVERROR(ENOSYS);
836 case kVTVideoDecoderUnsupportedDataFormatErr:
837 av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox does not support this format.\n");
838 return AVERROR(ENOSYS);
839 case kVTVideoDecoderMalfunctionErr:
840 av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox malfunction.\n");
841 return AVERROR(EINVAL);
842 case kVTVideoDecoderBadDataErr :
843 av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox reported invalid data.\n");
844 return AVERROR_INVALIDDATA;
848 av_log(avctx, AV_LOG_VERBOSE, "Unknown VideoToolbox session creation error %u\n", (unsigned)status);
849 return AVERROR_UNKNOWN;
853 static void videotoolbox_default_free(AVCodecContext *avctx)
855 AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
858 if (videotoolbox->cm_fmt_desc)
859 CFRelease(videotoolbox->cm_fmt_desc);
861 if (videotoolbox->session) {
862 VTDecompressionSessionInvalidate(videotoolbox->session);
863 CFRelease(videotoolbox->session);
868 static int videotoolbox_uninit(AVCodecContext *avctx)
870 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
874 ff_videotoolbox_uninit(avctx);
877 videotoolbox_default_free(avctx);
879 av_buffer_unref(&vtctx->cached_hw_frames_ctx);
880 av_freep(&vtctx->vt_ctx);
885 static int videotoolbox_common_init(AVCodecContext *avctx)
887 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
888 AVHWFramesContext *hw_frames;
891 // Old API - do nothing.
892 if (avctx->hwaccel_context)
895 if (!avctx->hw_frames_ctx && !avctx->hw_device_ctx) {
896 av_log(avctx, AV_LOG_ERROR,
897 "Either hw_frames_ctx or hw_device_ctx must be set.\n");
898 return AVERROR(EINVAL);
901 vtctx->vt_ctx = av_videotoolbox_alloc_context();
902 if (!vtctx->vt_ctx) {
903 err = AVERROR(ENOMEM);
907 if (avctx->hw_frames_ctx) {
908 hw_frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
910 avctx->hw_frames_ctx = av_hwframe_ctx_alloc(avctx->hw_device_ctx);
911 if (!avctx->hw_frames_ctx) {
912 err = AVERROR(ENOMEM);
916 hw_frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
917 hw_frames->format = AV_PIX_FMT_VIDEOTOOLBOX;
918 hw_frames->sw_format = AV_PIX_FMT_NV12; // same as av_videotoolbox_alloc_context()
919 hw_frames->width = avctx->width;
920 hw_frames->height = avctx->height;
922 err = av_hwframe_ctx_init(avctx->hw_frames_ctx);
924 av_buffer_unref(&avctx->hw_frames_ctx);
929 vtctx->cached_hw_frames_ctx = av_buffer_ref(avctx->hw_frames_ctx);
930 if (!vtctx->cached_hw_frames_ctx) {
931 err = AVERROR(ENOMEM);
935 vtctx->vt_ctx->cv_pix_fmt_type =
936 av_map_videotoolbox_format_from_pixfmt(hw_frames->sw_format);
937 if (!vtctx->vt_ctx->cv_pix_fmt_type) {
938 av_log(avctx, AV_LOG_ERROR, "Unknown sw_format.\n");
939 err = AVERROR(EINVAL);
943 err = videotoolbox_default_init(avctx);
950 videotoolbox_uninit(avctx);
954 AVHWAccel ff_h263_videotoolbox_hwaccel = {
955 .name = "h263_videotoolbox",
956 .type = AVMEDIA_TYPE_VIDEO,
957 .id = AV_CODEC_ID_H263,
958 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
959 .alloc_frame = ff_videotoolbox_alloc_frame,
960 .start_frame = videotoolbox_mpeg_start_frame,
961 .decode_slice = videotoolbox_mpeg_decode_slice,
962 .end_frame = videotoolbox_mpeg_end_frame,
963 .init = videotoolbox_common_init,
964 .uninit = videotoolbox_uninit,
965 .priv_data_size = sizeof(VTContext),
968 AVHWAccel ff_hevc_videotoolbox_hwaccel = {
969 .name = "hevc_videotoolbox",
970 .type = AVMEDIA_TYPE_VIDEO,
971 .id = AV_CODEC_ID_HEVC,
972 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
973 .alloc_frame = ff_videotoolbox_alloc_frame,
974 .start_frame = ff_videotoolbox_h264_start_frame,
975 .decode_slice = ff_videotoolbox_h264_decode_slice,
976 .end_frame = videotoolbox_hevc_end_frame,
977 .init = videotoolbox_common_init,
978 .uninit = ff_videotoolbox_uninit,
979 .priv_data_size = sizeof(VTContext),
982 AVHWAccel ff_h264_videotoolbox_hwaccel = {
983 .name = "h264_videotoolbox",
984 .type = AVMEDIA_TYPE_VIDEO,
985 .id = AV_CODEC_ID_H264,
986 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
987 .alloc_frame = ff_videotoolbox_alloc_frame,
988 .start_frame = ff_videotoolbox_h264_start_frame,
989 .decode_slice = ff_videotoolbox_h264_decode_slice,
990 .end_frame = videotoolbox_h264_end_frame,
991 .init = videotoolbox_common_init,
992 .uninit = videotoolbox_uninit,
993 .priv_data_size = sizeof(VTContext),
996 AVHWAccel ff_mpeg1_videotoolbox_hwaccel = {
997 .name = "mpeg1_videotoolbox",
998 .type = AVMEDIA_TYPE_VIDEO,
999 .id = AV_CODEC_ID_MPEG1VIDEO,
1000 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
1001 .alloc_frame = ff_videotoolbox_alloc_frame,
1002 .start_frame = videotoolbox_mpeg_start_frame,
1003 .decode_slice = videotoolbox_mpeg_decode_slice,
1004 .end_frame = videotoolbox_mpeg_end_frame,
1005 .init = videotoolbox_common_init,
1006 .uninit = videotoolbox_uninit,
1007 .priv_data_size = sizeof(VTContext),
1010 AVHWAccel ff_mpeg2_videotoolbox_hwaccel = {
1011 .name = "mpeg2_videotoolbox",
1012 .type = AVMEDIA_TYPE_VIDEO,
1013 .id = AV_CODEC_ID_MPEG2VIDEO,
1014 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
1015 .alloc_frame = ff_videotoolbox_alloc_frame,
1016 .start_frame = videotoolbox_mpeg_start_frame,
1017 .decode_slice = videotoolbox_mpeg_decode_slice,
1018 .end_frame = videotoolbox_mpeg_end_frame,
1019 .init = videotoolbox_common_init,
1020 .uninit = videotoolbox_uninit,
1021 .priv_data_size = sizeof(VTContext),
1024 AVHWAccel ff_mpeg4_videotoolbox_hwaccel = {
1025 .name = "mpeg4_videotoolbox",
1026 .type = AVMEDIA_TYPE_VIDEO,
1027 .id = AV_CODEC_ID_MPEG4,
1028 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
1029 .alloc_frame = ff_videotoolbox_alloc_frame,
1030 .start_frame = videotoolbox_mpeg_start_frame,
1031 .decode_slice = videotoolbox_mpeg_decode_slice,
1032 .end_frame = videotoolbox_mpeg_end_frame,
1033 .init = videotoolbox_common_init,
1034 .uninit = videotoolbox_uninit,
1035 .priv_data_size = sizeof(VTContext),
1038 AVVideotoolboxContext *av_videotoolbox_alloc_context(void)
1040 AVVideotoolboxContext *ret = av_mallocz(sizeof(*ret));
1043 ret->output_callback = videotoolbox_decoder_callback;
1044 ret->cv_pix_fmt_type = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
1050 int av_videotoolbox_default_init(AVCodecContext *avctx)
1052 return av_videotoolbox_default_init2(avctx, NULL);
1055 int av_videotoolbox_default_init2(AVCodecContext *avctx, AVVideotoolboxContext *vtctx)
1057 avctx->hwaccel_context = vtctx ?: av_videotoolbox_alloc_context();
1058 if (!avctx->hwaccel_context)
1059 return AVERROR(ENOMEM);
1060 return videotoolbox_default_init(avctx);
1063 void av_videotoolbox_default_free(AVCodecContext *avctx)
1066 videotoolbox_default_free(avctx);
1067 av_freep(&avctx->hwaccel_context);
1069 #endif /* CONFIG_VIDEOTOOLBOX */