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 #include "videotoolbox.h"
25 #include "libavutil/hwcontext_videotoolbox.h"
26 #include "vt_internal.h"
27 #include "libavutil/avutil.h"
28 #include "libavutil/hwcontext.h"
29 #include "bytestream.h"
33 #include "mpegvideo.h"
34 #include <TargetConditionals.h>
36 #ifndef kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder
37 # define kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder CFSTR("RequireHardwareAcceleratedVideoDecoder")
39 #ifndef kVTVideoDecoderSpecification_EnableHardwareAcceleratedVideoDecoder
40 # define kVTVideoDecoderSpecification_EnableHardwareAcceleratedVideoDecoder CFSTR("EnableHardwareAcceleratedVideoDecoder")
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 = *(CVPixelBufferRef *)data;
52 CVPixelBufferRelease(cv_buffer);
57 static int videotoolbox_buffer_copy(VTContext *vtctx,
58 const uint8_t *buffer,
63 tmp = av_fast_realloc(vtctx->bitstream,
64 &vtctx->allocated_size,
68 return AVERROR(ENOMEM);
70 vtctx->bitstream = tmp;
71 memcpy(vtctx->bitstream, buffer, size);
72 vtctx->bitstream_size = size;
77 static int videotoolbox_postproc_frame(void *avctx, AVFrame *frame)
79 CVPixelBufferRef ref = *(CVPixelBufferRef *)frame->buf[0]->data;
82 av_log(avctx, AV_LOG_ERROR, "No frame decoded?\n");
83 av_frame_unref(frame);
84 return AVERROR_EXTERNAL;
87 frame->data[3] = (uint8_t*)ref;
92 int ff_videotoolbox_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
94 size_t size = sizeof(CVPixelBufferRef);
96 AVBufferRef *buf = NULL;
97 int ret = ff_attach_decode_data(frame);
102 data = av_mallocz(size);
104 return AVERROR(ENOMEM);
105 buf = av_buffer_create(data, size, videotoolbox_buffer_release, NULL, 0);
108 return AVERROR(ENOMEM);
112 fdd = (FrameDecodeData*)frame->private_ref->data;
113 fdd->post_process = videotoolbox_postproc_frame;
115 frame->width = avctx->width;
116 frame->height = avctx->height;
117 frame->format = avctx->pix_fmt;
122 #define AV_W8(p, v) *(p) = (v)
124 CFDataRef ff_videotoolbox_avcc_extradata_create(AVCodecContext *avctx)
126 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
127 H264Context *h = avctx->priv_data;
128 CFDataRef data = NULL;
130 int vt_extradata_size = 6 + 2 + h->ps.sps->data_size + 3 + h->ps.pps->data_size;
131 uint8_t *vt_extradata = av_malloc(vt_extradata_size);
137 AV_W8(p + 0, 1); /* version */
138 AV_W8(p + 1, h->ps.sps->data[1]); /* profile */
139 AV_W8(p + 2, h->ps.sps->data[2]); /* profile compat */
140 AV_W8(p + 3, h->ps.sps->data[3]); /* level */
141 AV_W8(p + 4, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 3 (11) */
142 AV_W8(p + 5, 0xe1); /* 3 bits reserved (111) + 5 bits number of sps (00001) */
143 AV_WB16(p + 6, h->ps.sps->data_size);
144 memcpy(p + 8, h->ps.sps->data, h->ps.sps->data_size);
145 p += 8 + h->ps.sps->data_size;
146 AV_W8(p + 0, 1); /* number of pps */
147 AV_WB16(p + 1, h->ps.pps->data_size);
148 memcpy(p + 3, h->ps.pps->data, h->ps.pps->data_size);
150 p += 3 + h->ps.pps->data_size;
151 av_assert0(p - vt_extradata == vt_extradata_size);
153 // save sps header (profile/level) used to create decoder session,
154 // so we can detect changes and recreate it.
156 memcpy(vtctx->sps, h->ps.sps->data + 1, 3);
158 data = CFDataCreate(kCFAllocatorDefault, vt_extradata, vt_extradata_size);
159 av_free(vt_extradata);
163 CFDataRef ff_videotoolbox_hvcc_extradata_create(AVCodecContext *avctx)
165 HEVCContext *h = avctx->priv_data;
166 const HEVCVPS *vps = (const HEVCVPS *)h->ps.vps_list[0]->data;
167 const HEVCSPS *sps = (const HEVCSPS *)h->ps.sps_list[0]->data;
169 const HEVCPPS *pps = h->ps.pps;
170 PTLCommon ptlc = vps->ptl.general_ptl;
172 uint8_t parallelismType;
173 CFDataRef data = NULL;
175 int vt_extradata_size = 23 + 5 + vps->data_size + 5 + sps->data_size + 3;
176 uint8_t *vt_extradata;
178 for (i = 0; i < HEVC_MAX_PPS_COUNT; i++) {
179 if (h->ps.pps_list[i]) {
180 const HEVCPPS *pps = (const HEVCPPS *)h->ps.pps_list[i]->data;
181 vt_extradata_size += 2 + pps->data_size;
186 vt_extradata = av_malloc(vt_extradata_size);
191 /* unsigned int(8) configurationVersion = 1; */
195 * unsigned int(2) general_profile_space;
196 * unsigned int(1) general_tier_flag;
197 * unsigned int(5) general_profile_idc;
199 AV_W8(p + 1, ptlc.profile_space << 6 |
200 ptlc.tier_flag << 5 |
203 /* unsigned int(32) general_profile_compatibility_flags; */
204 memcpy(p + 2, ptlc.profile_compatibility_flag, 4);
206 /* unsigned int(48) general_constraint_indicator_flags; */
207 AV_W8(p + 6, ptlc.progressive_source_flag << 7 |
208 ptlc.interlaced_source_flag << 6 |
209 ptlc.non_packed_constraint_flag << 5 |
210 ptlc.frame_only_constraint_flag << 4);
214 /* unsigned int(8) general_level_idc; */
215 AV_W8(p + 12, ptlc.level_idc);
218 * bit(4) reserved = ‘1111’b;
219 * unsigned int(12) min_spatial_segmentation_idc;
221 AV_W8(p + 13, 0xf0 | (vui.min_spatial_segmentation_idc >> 4));
222 AV_W8(p + 14, vui.min_spatial_segmentation_idc & 0xff);
225 * bit(6) reserved = ‘111111’b;
226 * unsigned int(2) parallelismType;
228 if (!vui.min_spatial_segmentation_idc)
230 else if (pps->entropy_coding_sync_enabled_flag && pps->tiles_enabled_flag)
232 else if (pps->entropy_coding_sync_enabled_flag)
234 else if (pps->tiles_enabled_flag)
238 AV_W8(p + 15, 0xfc | parallelismType);
241 * bit(6) reserved = ‘111111’b;
242 * unsigned int(2) chromaFormat;
244 AV_W8(p + 16, sps->chroma_format_idc | 0xfc);
247 * bit(5) reserved = ‘11111’b;
248 * unsigned int(3) bitDepthLumaMinus8;
250 AV_W8(p + 17, (sps->bit_depth - 8) | 0xfc);
253 * bit(5) reserved = ‘11111’b;
254 * unsigned int(3) bitDepthChromaMinus8;
256 AV_W8(p + 18, (sps->bit_depth_chroma - 8) | 0xfc);
258 /* bit(16) avgFrameRate; */
262 * bit(2) constantFrameRate;
263 * bit(3) numTemporalLayers;
264 * bit(1) temporalIdNested;
265 * unsigned int(2) lengthSizeMinusOne;
267 AV_W8(p + 21, 0 << 6 |
268 sps->max_sub_layers << 3 |
269 sps->temporal_id_nesting_flag << 2 |
272 /* unsigned int(8) numOfArrays; */
278 * bit(1) array_completeness;
279 * unsigned int(1) reserved = 0;
280 * unsigned int(6) NAL_unit_type;
283 HEVC_NAL_VPS & 0x3f);
284 /* unsigned int(16) numNalus; */
286 /* unsigned int(16) nalUnitLength; */
287 AV_WB16(p + 3, vps->data_size);
288 /* bit(8*nalUnitLength) nalUnit; */
289 memcpy(p + 5, vps->data, vps->data_size);
290 p += 5 + vps->data_size;
294 HEVC_NAL_SPS & 0x3f);
296 AV_WB16(p + 3, sps->data_size);
297 memcpy(p + 5, sps->data, sps->data_size);
298 p += 5 + sps->data_size;
302 HEVC_NAL_PPS & 0x3f);
303 AV_WB16(p + 1, num_pps);
305 for (i = 0; i < HEVC_MAX_PPS_COUNT; i++) {
306 if (h->ps.pps_list[i]) {
307 const HEVCPPS *pps = (const HEVCPPS *)h->ps.pps_list[i]->data;
308 AV_WB16(p, pps->data_size);
309 memcpy(p + 2, pps->data, pps->data_size);
310 p += 2 + pps->data_size;
314 av_assert0(p - vt_extradata == vt_extradata_size);
316 data = CFDataCreate(kCFAllocatorDefault, vt_extradata, vt_extradata_size);
317 av_free(vt_extradata);
321 static int videotoolbox_set_frame(AVCodecContext *avctx, AVFrame *frame)
323 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
324 if (!frame->buf[0] || frame->data[3]) {
325 av_log(avctx, AV_LOG_ERROR, "videotoolbox: invalid state\n");
326 av_frame_unref(frame);
327 return AVERROR_EXTERNAL;
330 CVPixelBufferRef *ref = (CVPixelBufferRef *)frame->buf[0]->data;
333 CVPixelBufferRelease(*ref);
341 int ff_videotoolbox_h264_start_frame(AVCodecContext *avctx,
342 const uint8_t *buffer,
345 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
346 H264Context *h = avctx->priv_data;
348 if (h->is_avc == 1) {
349 return videotoolbox_buffer_copy(vtctx, buffer, size);
355 static int videotoolbox_h264_decode_params(AVCodecContext *avctx,
357 const uint8_t *buffer,
360 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
361 H264Context *h = avctx->priv_data;
363 // save sps header (profile/level) used to create decoder session
365 memcpy(vtctx->sps, h->ps.sps->data + 1, 3);
367 if (type == H264_NAL_SPS) {
368 if (size > 4 && memcmp(vtctx->sps, buffer + 1, 3) != 0) {
369 vtctx->reconfig_needed = true;
370 memcpy(vtctx->sps, buffer + 1, 3);
374 // pass-through SPS/PPS changes to the decoder
375 return ff_videotoolbox_h264_decode_slice(avctx, buffer, size);
378 int ff_videotoolbox_h264_decode_slice(AVCodecContext *avctx,
379 const uint8_t *buffer,
382 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
383 H264Context *h = avctx->priv_data;
389 tmp = av_fast_realloc(vtctx->bitstream,
390 &vtctx->allocated_size,
391 vtctx->bitstream_size+size+4);
393 return AVERROR(ENOMEM);
395 vtctx->bitstream = tmp;
397 AV_WB32(vtctx->bitstream + vtctx->bitstream_size, size);
398 memcpy(vtctx->bitstream + vtctx->bitstream_size + 4, buffer, size);
400 vtctx->bitstream_size += size + 4;
405 int ff_videotoolbox_uninit(AVCodecContext *avctx)
407 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
409 av_freep(&vtctx->bitstream);
411 CVPixelBufferRelease(vtctx->frame);
417 #if CONFIG_VIDEOTOOLBOX
418 // Return the AVVideotoolboxContext that matters currently. Where it comes from
419 // depends on the API used.
420 static AVVideotoolboxContext *videotoolbox_get_context(AVCodecContext *avctx)
422 // Somewhat tricky because the user can call av_videotoolbox_default_free()
423 // at any time, even when the codec is closed.
424 if (avctx->internal && avctx->internal->hwaccel_priv_data) {
425 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
427 return vtctx->vt_ctx;
429 return avctx->hwaccel_context;
432 static int videotoolbox_buffer_create(AVCodecContext *avctx, AVFrame *frame)
434 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
435 CVPixelBufferRef pixbuf = (CVPixelBufferRef)vtctx->frame;
436 OSType pixel_format = CVPixelBufferGetPixelFormatType(pixbuf);
437 enum AVPixelFormat sw_format = av_map_videotoolbox_format_to_pixfmt(pixel_format);
438 int width = CVPixelBufferGetWidth(pixbuf);
439 int height = CVPixelBufferGetHeight(pixbuf);
440 AVHWFramesContext *cached_frames;
443 ret = videotoolbox_set_frame(avctx, frame);
447 // Old API code path.
448 if (!vtctx->cached_hw_frames_ctx)
451 cached_frames = (AVHWFramesContext*)vtctx->cached_hw_frames_ctx->data;
453 if (cached_frames->sw_format != sw_format ||
454 cached_frames->width != width ||
455 cached_frames->height != height) {
456 AVBufferRef *hw_frames_ctx = av_hwframe_ctx_alloc(cached_frames->device_ref);
457 AVHWFramesContext *hw_frames;
459 return AVERROR(ENOMEM);
461 hw_frames = (AVHWFramesContext*)hw_frames_ctx->data;
462 hw_frames->format = cached_frames->format;
463 hw_frames->sw_format = sw_format;
464 hw_frames->width = width;
465 hw_frames->height = height;
467 ret = av_hwframe_ctx_init(hw_frames_ctx);
469 av_buffer_unref(&hw_frames_ctx);
473 av_buffer_unref(&vtctx->cached_hw_frames_ctx);
474 vtctx->cached_hw_frames_ctx = hw_frames_ctx;
477 av_buffer_unref(&frame->hw_frames_ctx);
478 frame->hw_frames_ctx = av_buffer_ref(vtctx->cached_hw_frames_ctx);
479 if (!frame->hw_frames_ctx)
480 return AVERROR(ENOMEM);
485 static void videotoolbox_write_mp4_descr_length(PutByteContext *pb, int length)
490 for (i = 3; i >= 0; i--) {
491 b = (length >> (i * 7)) & 0x7F;
495 bytestream2_put_byteu(pb, b);
499 static CFDataRef videotoolbox_esds_extradata_create(AVCodecContext *avctx)
502 uint8_t *rw_extradata;
504 int full_size = 3 + 5 + 13 + 5 + avctx->extradata_size + 3;
505 // ES_DescrTag data + DecoderConfigDescrTag + data + DecSpecificInfoTag + size + SLConfigDescriptor
506 int config_size = 13 + 5 + avctx->extradata_size;
509 if (!(rw_extradata = av_mallocz(full_size + VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING)))
512 bytestream2_init_writer(&pb, rw_extradata, full_size + VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING);
513 bytestream2_put_byteu(&pb, 0); // version
514 bytestream2_put_ne24(&pb, 0); // flags
516 // elementary stream descriptor
517 bytestream2_put_byteu(&pb, 0x03); // ES_DescrTag
518 videotoolbox_write_mp4_descr_length(&pb, full_size);
519 bytestream2_put_ne16(&pb, 0); // esid
520 bytestream2_put_byteu(&pb, 0); // stream priority (0-32)
522 // decoder configuration descriptor
523 bytestream2_put_byteu(&pb, 0x04); // DecoderConfigDescrTag
524 videotoolbox_write_mp4_descr_length(&pb, config_size);
525 bytestream2_put_byteu(&pb, 32); // object type indication. 32 = AV_CODEC_ID_MPEG4
526 bytestream2_put_byteu(&pb, 0x11); // stream type
527 bytestream2_put_ne24(&pb, 0); // buffer size
528 bytestream2_put_ne32(&pb, 0); // max bitrate
529 bytestream2_put_ne32(&pb, 0); // avg bitrate
531 // decoder specific descriptor
532 bytestream2_put_byteu(&pb, 0x05); ///< DecSpecificInfoTag
533 videotoolbox_write_mp4_descr_length(&pb, avctx->extradata_size);
535 bytestream2_put_buffer(&pb, avctx->extradata, avctx->extradata_size);
537 // SLConfigDescriptor
538 bytestream2_put_byteu(&pb, 0x06); // SLConfigDescrTag
539 bytestream2_put_byteu(&pb, 0x01); // length
540 bytestream2_put_byteu(&pb, 0x02); //
542 s = bytestream2_size_p(&pb);
544 data = CFDataCreate(kCFAllocatorDefault, rw_extradata, s);
546 av_freep(&rw_extradata);
550 static CMSampleBufferRef videotoolbox_sample_buffer_create(CMFormatDescriptionRef fmt_desc,
555 CMBlockBufferRef block_buf;
556 CMSampleBufferRef sample_buf;
561 status = CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault,// structureAllocator
562 buffer, // memoryBlock
564 kCFAllocatorNull, // blockAllocator
565 NULL, // customBlockSource
572 status = CMSampleBufferCreate(kCFAllocatorDefault, // allocator
573 block_buf, // dataBuffer
575 0, // makeDataReadyCallback
576 0, // makeDataReadyRefcon
577 fmt_desc, // formatDescription
579 0, // numSampleTimingEntries
580 NULL, // sampleTimingArray
581 0, // numSampleSizeEntries
582 NULL, // sampleSizeArray
587 CFRelease(block_buf);
592 static void videotoolbox_decoder_callback(void *opaque,
593 void *sourceFrameRefCon,
595 VTDecodeInfoFlags flags,
596 CVImageBufferRef image_buffer,
600 AVCodecContext *avctx = opaque;
601 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
604 CVPixelBufferRelease(vtctx->frame);
609 av_log(NULL, AV_LOG_DEBUG, "vt decoder cb: output image buffer is null\n");
613 vtctx->frame = CVPixelBufferRetain(image_buffer);
616 static OSStatus videotoolbox_session_decode_frame(AVCodecContext *avctx)
619 CMSampleBufferRef sample_buf;
620 AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
621 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
623 sample_buf = videotoolbox_sample_buffer_create(videotoolbox->cm_fmt_desc,
625 vtctx->bitstream_size);
630 status = VTDecompressionSessionDecodeFrame(videotoolbox->session,
633 NULL, // sourceFrameRefCon
636 status = VTDecompressionSessionWaitForAsynchronousFrames(videotoolbox->session);
638 CFRelease(sample_buf);
643 static CMVideoFormatDescriptionRef videotoolbox_format_desc_create(CMVideoCodecType codec_type,
644 CFDictionaryRef decoder_spec,
648 CMFormatDescriptionRef cm_fmt_desc;
651 status = CMVideoFormatDescriptionCreate(kCFAllocatorDefault,
655 decoder_spec, // Dictionary of extension
664 static CFDictionaryRef videotoolbox_buffer_attributes_create(int width,
668 CFMutableDictionaryRef buffer_attributes;
669 CFMutableDictionaryRef io_surface_properties;
670 CFNumberRef cv_pix_fmt;
674 w = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &width);
675 h = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &height);
676 cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &pix_fmt);
678 buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
680 &kCFTypeDictionaryKeyCallBacks,
681 &kCFTypeDictionaryValueCallBacks);
682 io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
684 &kCFTypeDictionaryKeyCallBacks,
685 &kCFTypeDictionaryValueCallBacks);
688 CFDictionarySetValue(buffer_attributes, kCVPixelBufferPixelFormatTypeKey, cv_pix_fmt);
689 CFDictionarySetValue(buffer_attributes, kCVPixelBufferIOSurfacePropertiesKey, io_surface_properties);
690 CFDictionarySetValue(buffer_attributes, kCVPixelBufferWidthKey, w);
691 CFDictionarySetValue(buffer_attributes, kCVPixelBufferHeightKey, h);
693 CFDictionarySetValue(buffer_attributes, kCVPixelBufferOpenGLESCompatibilityKey, kCFBooleanTrue);
695 CFDictionarySetValue(buffer_attributes, kCVPixelBufferIOSurfaceOpenGLTextureCompatibilityKey, kCFBooleanTrue);
698 CFRelease(io_surface_properties);
699 CFRelease(cv_pix_fmt);
703 return buffer_attributes;
706 static CFDictionaryRef videotoolbox_decoder_config_create(CMVideoCodecType codec_type,
707 AVCodecContext *avctx)
709 CFMutableDictionaryRef config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
711 &kCFTypeDictionaryKeyCallBacks,
712 &kCFTypeDictionaryValueCallBacks);
714 CFDictionarySetValue(config_info,
715 codec_type == kCMVideoCodecType_HEVC ?
716 kVTVideoDecoderSpecification_EnableHardwareAcceleratedVideoDecoder :
717 kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder,
720 CFMutableDictionaryRef avc_info;
721 CFDataRef data = NULL;
723 avc_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
725 &kCFTypeDictionaryKeyCallBacks,
726 &kCFTypeDictionaryValueCallBacks);
728 switch (codec_type) {
729 case kCMVideoCodecType_MPEG4Video :
730 if (avctx->extradata_size)
731 data = videotoolbox_esds_extradata_create(avctx);
733 CFDictionarySetValue(avc_info, CFSTR("esds"), data);
735 case kCMVideoCodecType_H264 :
736 data = ff_videotoolbox_avcc_extradata_create(avctx);
738 CFDictionarySetValue(avc_info, CFSTR("avcC"), data);
740 case kCMVideoCodecType_HEVC :
741 data = ff_videotoolbox_hvcc_extradata_create(avctx);
743 CFDictionarySetValue(avc_info, CFSTR("hvcC"), data);
749 CFDictionarySetValue(config_info,
750 kCMFormatDescriptionExtension_SampleDescriptionExtensionAtoms,
760 static int videotoolbox_start(AVCodecContext *avctx)
762 AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
764 VTDecompressionOutputCallbackRecord decoder_cb;
765 CFDictionaryRef decoder_spec;
766 CFDictionaryRef buf_attr;
769 av_log(avctx, AV_LOG_ERROR, "hwaccel context is not set\n");
773 switch( avctx->codec_id ) {
774 case AV_CODEC_ID_H263 :
775 videotoolbox->cm_codec_type = kCMVideoCodecType_H263;
777 case AV_CODEC_ID_H264 :
778 videotoolbox->cm_codec_type = kCMVideoCodecType_H264;
780 case AV_CODEC_ID_HEVC :
781 videotoolbox->cm_codec_type = kCMVideoCodecType_HEVC;
783 case AV_CODEC_ID_MPEG1VIDEO :
784 videotoolbox->cm_codec_type = kCMVideoCodecType_MPEG1Video;
786 case AV_CODEC_ID_MPEG2VIDEO :
787 videotoolbox->cm_codec_type = kCMVideoCodecType_MPEG2Video;
789 case AV_CODEC_ID_MPEG4 :
790 videotoolbox->cm_codec_type = kCMVideoCodecType_MPEG4Video;
796 decoder_spec = videotoolbox_decoder_config_create(videotoolbox->cm_codec_type, avctx);
799 av_log(avctx, AV_LOG_ERROR, "decoder specification creation failed\n");
803 videotoolbox->cm_fmt_desc = videotoolbox_format_desc_create(videotoolbox->cm_codec_type,
807 if (!videotoolbox->cm_fmt_desc) {
809 CFRelease(decoder_spec);
811 av_log(avctx, AV_LOG_ERROR, "format description creation failed\n");
815 buf_attr = videotoolbox_buffer_attributes_create(avctx->width,
817 videotoolbox->cv_pix_fmt_type);
819 decoder_cb.decompressionOutputCallback = videotoolbox_decoder_callback;
820 decoder_cb.decompressionOutputRefCon = avctx;
822 status = VTDecompressionSessionCreate(NULL, // allocator
823 videotoolbox->cm_fmt_desc, // videoFormatDescription
824 decoder_spec, // videoDecoderSpecification
825 buf_attr, // destinationImageBufferAttributes
826 &decoder_cb, // outputCallback
827 &videotoolbox->session); // decompressionSessionOut
830 CFRelease(decoder_spec);
835 case kVTVideoDecoderNotAvailableNowErr:
836 av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox session not available.\n");
837 return AVERROR(ENOSYS);
838 case kVTVideoDecoderUnsupportedDataFormatErr:
839 av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox does not support this format.\n");
840 return AVERROR(ENOSYS);
841 case kVTCouldNotFindVideoDecoderErr:
842 av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox decoder for this format not found.\n");
843 return AVERROR(ENOSYS);
844 case kVTVideoDecoderMalfunctionErr:
845 av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox malfunction.\n");
846 return AVERROR(EINVAL);
847 case kVTVideoDecoderBadDataErr:
848 av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox reported invalid data.\n");
849 return AVERROR_INVALIDDATA;
853 av_log(avctx, AV_LOG_VERBOSE, "Unknown VideoToolbox session creation error %d\n", (int)status);
854 return AVERROR_UNKNOWN;
858 static void videotoolbox_stop(AVCodecContext *avctx)
860 AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
864 if (videotoolbox->cm_fmt_desc) {
865 CFRelease(videotoolbox->cm_fmt_desc);
866 videotoolbox->cm_fmt_desc = NULL;
869 if (videotoolbox->session) {
870 VTDecompressionSessionInvalidate(videotoolbox->session);
871 CFRelease(videotoolbox->session);
872 videotoolbox->session = NULL;
876 static const char *videotoolbox_error_string(OSStatus status)
879 case kVTVideoDecoderBadDataErr:
881 case kVTVideoDecoderMalfunctionErr:
882 return "decoder malfunction";
883 case kVTInvalidSessionErr:
884 return "invalid session";
889 static int videotoolbox_common_end_frame(AVCodecContext *avctx, AVFrame *frame)
892 AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
893 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
895 if (vtctx->reconfig_needed == true) {
896 vtctx->reconfig_needed = false;
897 av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox decoder needs reconfig, restarting..\n");
898 videotoolbox_stop(avctx);
899 if (videotoolbox_start(avctx) != 0) {
900 return AVERROR_EXTERNAL;
904 if (!videotoolbox->session || !vtctx->bitstream || !vtctx->bitstream_size)
905 return AVERROR_INVALIDDATA;
907 status = videotoolbox_session_decode_frame(avctx);
908 if (status != noErr) {
909 if (status == kVTVideoDecoderMalfunctionErr || status == kVTInvalidSessionErr)
910 vtctx->reconfig_needed = true;
911 av_log(avctx, AV_LOG_ERROR, "Failed to decode frame (%s, %d)\n", videotoolbox_error_string(status), (int)status);
912 return AVERROR_UNKNOWN;
916 vtctx->reconfig_needed = true;
917 return AVERROR_UNKNOWN;
920 return videotoolbox_buffer_create(avctx, frame);
923 static int videotoolbox_h264_end_frame(AVCodecContext *avctx)
925 H264Context *h = avctx->priv_data;
926 AVFrame *frame = h->cur_pic_ptr->f;
927 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
928 int ret = videotoolbox_common_end_frame(avctx, frame);
929 vtctx->bitstream_size = 0;
933 static int videotoolbox_hevc_decode_params(AVCodecContext *avctx,
935 const uint8_t *buffer,
938 return ff_videotoolbox_h264_decode_slice(avctx, buffer, size);
941 static int videotoolbox_hevc_end_frame(AVCodecContext *avctx)
943 HEVCContext *h = avctx->priv_data;
944 AVFrame *frame = h->ref->frame;
945 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
948 ret = videotoolbox_common_end_frame(avctx, frame);
949 vtctx->bitstream_size = 0;
953 static int videotoolbox_mpeg_start_frame(AVCodecContext *avctx,
954 const uint8_t *buffer,
957 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
959 return videotoolbox_buffer_copy(vtctx, buffer, size);
962 static int videotoolbox_mpeg_decode_slice(AVCodecContext *avctx,
963 const uint8_t *buffer,
969 static int videotoolbox_mpeg_end_frame(AVCodecContext *avctx)
971 MpegEncContext *s = avctx->priv_data;
972 AVFrame *frame = s->current_picture_ptr->f;
974 return videotoolbox_common_end_frame(avctx, frame);
977 static int videotoolbox_uninit(AVCodecContext *avctx)
979 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
983 ff_videotoolbox_uninit(avctx);
986 videotoolbox_stop(avctx);
988 av_buffer_unref(&vtctx->cached_hw_frames_ctx);
989 av_freep(&vtctx->vt_ctx);
994 static int videotoolbox_common_init(AVCodecContext *avctx)
996 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
997 AVHWFramesContext *hw_frames;
1000 // Old API - do nothing.
1001 if (avctx->hwaccel_context)
1004 if (!avctx->hw_frames_ctx && !avctx->hw_device_ctx) {
1005 av_log(avctx, AV_LOG_ERROR,
1006 "Either hw_frames_ctx or hw_device_ctx must be set.\n");
1007 return AVERROR(EINVAL);
1010 vtctx->vt_ctx = av_videotoolbox_alloc_context();
1011 if (!vtctx->vt_ctx) {
1012 err = AVERROR(ENOMEM);
1016 if (avctx->hw_frames_ctx) {
1017 hw_frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
1019 avctx->hw_frames_ctx = av_hwframe_ctx_alloc(avctx->hw_device_ctx);
1020 if (!avctx->hw_frames_ctx) {
1021 err = AVERROR(ENOMEM);
1025 hw_frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
1026 hw_frames->format = AV_PIX_FMT_VIDEOTOOLBOX;
1027 hw_frames->sw_format = AV_PIX_FMT_NV12; // same as av_videotoolbox_alloc_context()
1028 hw_frames->width = avctx->width;
1029 hw_frames->height = avctx->height;
1031 err = av_hwframe_ctx_init(avctx->hw_frames_ctx);
1033 av_buffer_unref(&avctx->hw_frames_ctx);
1038 vtctx->cached_hw_frames_ctx = av_buffer_ref(avctx->hw_frames_ctx);
1039 if (!vtctx->cached_hw_frames_ctx) {
1040 err = AVERROR(ENOMEM);
1044 vtctx->vt_ctx->cv_pix_fmt_type =
1045 av_map_videotoolbox_format_from_pixfmt(hw_frames->sw_format);
1046 if (!vtctx->vt_ctx->cv_pix_fmt_type) {
1047 av_log(avctx, AV_LOG_ERROR, "Unknown sw_format.\n");
1048 err = AVERROR(EINVAL);
1052 err = videotoolbox_start(avctx);
1059 videotoolbox_uninit(avctx);
1063 static int videotoolbox_frame_params(AVCodecContext *avctx,
1064 AVBufferRef *hw_frames_ctx)
1066 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)hw_frames_ctx->data;
1068 frames_ctx->format = AV_PIX_FMT_VIDEOTOOLBOX;
1069 frames_ctx->width = avctx->coded_width;
1070 frames_ctx->height = avctx->coded_height;
1071 frames_ctx->sw_format = AV_PIX_FMT_NV12;
1076 const AVHWAccel ff_h263_videotoolbox_hwaccel = {
1077 .name = "h263_videotoolbox",
1078 .type = AVMEDIA_TYPE_VIDEO,
1079 .id = AV_CODEC_ID_H263,
1080 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
1081 .alloc_frame = ff_videotoolbox_alloc_frame,
1082 .start_frame = videotoolbox_mpeg_start_frame,
1083 .decode_slice = videotoolbox_mpeg_decode_slice,
1084 .end_frame = videotoolbox_mpeg_end_frame,
1085 .frame_params = videotoolbox_frame_params,
1086 .init = videotoolbox_common_init,
1087 .uninit = videotoolbox_uninit,
1088 .priv_data_size = sizeof(VTContext),
1091 const AVHWAccel ff_hevc_videotoolbox_hwaccel = {
1092 .name = "hevc_videotoolbox",
1093 .type = AVMEDIA_TYPE_VIDEO,
1094 .id = AV_CODEC_ID_HEVC,
1095 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
1096 .alloc_frame = ff_videotoolbox_alloc_frame,
1097 .start_frame = ff_videotoolbox_h264_start_frame,
1098 .decode_slice = ff_videotoolbox_h264_decode_slice,
1099 .decode_params = videotoolbox_hevc_decode_params,
1100 .end_frame = videotoolbox_hevc_end_frame,
1101 .frame_params = videotoolbox_frame_params,
1102 .init = videotoolbox_common_init,
1103 .uninit = ff_videotoolbox_uninit,
1104 .priv_data_size = sizeof(VTContext),
1107 const AVHWAccel ff_h264_videotoolbox_hwaccel = {
1108 .name = "h264_videotoolbox",
1109 .type = AVMEDIA_TYPE_VIDEO,
1110 .id = AV_CODEC_ID_H264,
1111 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
1112 .alloc_frame = ff_videotoolbox_alloc_frame,
1113 .start_frame = ff_videotoolbox_h264_start_frame,
1114 .decode_slice = ff_videotoolbox_h264_decode_slice,
1115 .decode_params = videotoolbox_h264_decode_params,
1116 .end_frame = videotoolbox_h264_end_frame,
1117 .frame_params = videotoolbox_frame_params,
1118 .init = videotoolbox_common_init,
1119 .uninit = videotoolbox_uninit,
1120 .priv_data_size = sizeof(VTContext),
1123 const AVHWAccel ff_mpeg1_videotoolbox_hwaccel = {
1124 .name = "mpeg1_videotoolbox",
1125 .type = AVMEDIA_TYPE_VIDEO,
1126 .id = AV_CODEC_ID_MPEG1VIDEO,
1127 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
1128 .alloc_frame = ff_videotoolbox_alloc_frame,
1129 .start_frame = videotoolbox_mpeg_start_frame,
1130 .decode_slice = videotoolbox_mpeg_decode_slice,
1131 .end_frame = videotoolbox_mpeg_end_frame,
1132 .frame_params = videotoolbox_frame_params,
1133 .init = videotoolbox_common_init,
1134 .uninit = videotoolbox_uninit,
1135 .priv_data_size = sizeof(VTContext),
1138 const AVHWAccel ff_mpeg2_videotoolbox_hwaccel = {
1139 .name = "mpeg2_videotoolbox",
1140 .type = AVMEDIA_TYPE_VIDEO,
1141 .id = AV_CODEC_ID_MPEG2VIDEO,
1142 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
1143 .alloc_frame = ff_videotoolbox_alloc_frame,
1144 .start_frame = videotoolbox_mpeg_start_frame,
1145 .decode_slice = videotoolbox_mpeg_decode_slice,
1146 .end_frame = videotoolbox_mpeg_end_frame,
1147 .frame_params = videotoolbox_frame_params,
1148 .init = videotoolbox_common_init,
1149 .uninit = videotoolbox_uninit,
1150 .priv_data_size = sizeof(VTContext),
1153 const AVHWAccel ff_mpeg4_videotoolbox_hwaccel = {
1154 .name = "mpeg4_videotoolbox",
1155 .type = AVMEDIA_TYPE_VIDEO,
1156 .id = AV_CODEC_ID_MPEG4,
1157 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
1158 .alloc_frame = ff_videotoolbox_alloc_frame,
1159 .start_frame = videotoolbox_mpeg_start_frame,
1160 .decode_slice = videotoolbox_mpeg_decode_slice,
1161 .end_frame = videotoolbox_mpeg_end_frame,
1162 .frame_params = videotoolbox_frame_params,
1163 .init = videotoolbox_common_init,
1164 .uninit = videotoolbox_uninit,
1165 .priv_data_size = sizeof(VTContext),
1168 AVVideotoolboxContext *av_videotoolbox_alloc_context(void)
1170 AVVideotoolboxContext *ret = av_mallocz(sizeof(*ret));
1173 ret->output_callback = videotoolbox_decoder_callback;
1174 ret->cv_pix_fmt_type = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
1180 int av_videotoolbox_default_init(AVCodecContext *avctx)
1182 return av_videotoolbox_default_init2(avctx, NULL);
1185 int av_videotoolbox_default_init2(AVCodecContext *avctx, AVVideotoolboxContext *vtctx)
1187 avctx->hwaccel_context = vtctx ?: av_videotoolbox_alloc_context();
1188 if (!avctx->hwaccel_context)
1189 return AVERROR(ENOMEM);
1190 return videotoolbox_start(avctx);
1193 void av_videotoolbox_default_free(AVCodecContext *avctx)
1196 videotoolbox_stop(avctx);
1197 av_freep(&avctx->hwaccel_context);
1199 #endif /* CONFIG_VIDEOTOOLBOX */