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")
40 #if !HAVE_KCMVIDEOCODECTYPE_HEVC
41 enum { kCMVideoCodecType_HEVC = 'hvc1' };
44 #define VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING 12
46 static void videotoolbox_buffer_release(void *opaque, uint8_t *data)
48 CVPixelBufferRef cv_buffer = (CVImageBufferRef)data;
49 CVPixelBufferRelease(cv_buffer);
52 static int videotoolbox_buffer_copy(VTContext *vtctx,
53 const uint8_t *buffer,
58 tmp = av_fast_realloc(vtctx->bitstream,
59 &vtctx->allocated_size,
63 return AVERROR(ENOMEM);
65 vtctx->bitstream = tmp;
66 memcpy(vtctx->bitstream, buffer, size);
67 vtctx->bitstream_size = size;
72 int ff_videotoolbox_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
74 int ret = ff_attach_decode_data(frame);
78 frame->width = avctx->width;
79 frame->height = avctx->height;
80 frame->format = avctx->pix_fmt;
81 frame->buf[0] = av_buffer_alloc(1);
84 return AVERROR(ENOMEM);
89 #define AV_W8(p, v) *(p) = (v)
91 CFDataRef ff_videotoolbox_avcc_extradata_create(AVCodecContext *avctx)
93 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
94 H264Context *h = avctx->priv_data;
95 CFDataRef data = NULL;
97 int vt_extradata_size = 6 + 2 + h->ps.sps->data_size + 3 + h->ps.pps->data_size;
98 uint8_t *vt_extradata = av_malloc(vt_extradata_size);
104 AV_W8(p + 0, 1); /* version */
105 AV_W8(p + 1, h->ps.sps->data[1]); /* profile */
106 AV_W8(p + 2, h->ps.sps->data[2]); /* profile compat */
107 AV_W8(p + 3, h->ps.sps->data[3]); /* level */
108 AV_W8(p + 4, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 3 (11) */
109 AV_W8(p + 5, 0xe1); /* 3 bits reserved (111) + 5 bits number of sps (00001) */
110 AV_WB16(p + 6, h->ps.sps->data_size);
111 memcpy(p + 8, h->ps.sps->data, h->ps.sps->data_size);
112 p += 8 + h->ps.sps->data_size;
113 AV_W8(p + 0, 1); /* number of pps */
114 AV_WB16(p + 1, h->ps.pps->data_size);
115 memcpy(p + 3, h->ps.pps->data, h->ps.pps->data_size);
117 p += 3 + h->ps.pps->data_size;
118 av_assert0(p - vt_extradata == vt_extradata_size);
120 // save sps header (profile/level) used to create decoder session,
121 // so we can detect changes and recreate it.
123 memcpy(vtctx->sps, h->ps.sps->data + 1, 3);
125 data = CFDataCreate(kCFAllocatorDefault, vt_extradata, vt_extradata_size);
126 av_free(vt_extradata);
130 CFDataRef ff_videotoolbox_hvcc_extradata_create(AVCodecContext *avctx)
132 HEVCContext *h = avctx->priv_data;
133 const HEVCVPS *vps = (const HEVCVPS *)h->ps.vps_list[0]->data;
134 const HEVCSPS *sps = (const HEVCSPS *)h->ps.sps_list[0]->data;
136 const HEVCPPS *pps = h->ps.pps;
137 PTLCommon ptlc = vps->ptl.general_ptl;
139 uint8_t parallelismType;
140 CFDataRef data = NULL;
142 int vt_extradata_size = 23 + 5 + vps->data_size + 5 + sps->data_size + 3;
143 uint8_t *vt_extradata;
145 for (i = 0; i < HEVC_MAX_PPS_COUNT; i++) {
146 if (h->ps.pps_list[i]) {
147 const HEVCPPS *pps = (const HEVCPPS *)h->ps.pps_list[i]->data;
148 vt_extradata_size += 2 + pps->data_size;
153 vt_extradata = av_malloc(vt_extradata_size);
158 /* unsigned int(8) configurationVersion = 1; */
162 * unsigned int(2) general_profile_space;
163 * unsigned int(1) general_tier_flag;
164 * unsigned int(5) general_profile_idc;
166 AV_W8(p + 1, ptlc.profile_space << 6 |
167 ptlc.tier_flag << 5 |
170 /* unsigned int(32) general_profile_compatibility_flags; */
171 memcpy(p + 2, ptlc.profile_compatibility_flag, 4);
173 /* unsigned int(48) general_constraint_indicator_flags; */
174 AV_W8(p + 6, ptlc.progressive_source_flag << 7 |
175 ptlc.interlaced_source_flag << 6 |
176 ptlc.non_packed_constraint_flag << 5 |
177 ptlc.frame_only_constraint_flag << 4);
181 /* unsigned int(8) general_level_idc; */
182 AV_W8(p + 12, ptlc.level_idc);
185 * bit(4) reserved = ‘1111’b;
186 * unsigned int(12) min_spatial_segmentation_idc;
188 AV_W8(p + 13, 0xf0 | (vui.min_spatial_segmentation_idc >> 4));
189 AV_W8(p + 14, vui.min_spatial_segmentation_idc & 0xff);
192 * bit(6) reserved = ‘111111’b;
193 * unsigned int(2) parallelismType;
195 if (!vui.min_spatial_segmentation_idc)
197 else if (pps->entropy_coding_sync_enabled_flag && pps->tiles_enabled_flag)
199 else if (pps->entropy_coding_sync_enabled_flag)
201 else if (pps->tiles_enabled_flag)
205 AV_W8(p + 15, 0xfc | parallelismType);
208 * bit(6) reserved = ‘111111’b;
209 * unsigned int(2) chromaFormat;
211 AV_W8(p + 16, sps->chroma_format_idc | 0xfc);
214 * bit(5) reserved = ‘11111’b;
215 * unsigned int(3) bitDepthLumaMinus8;
217 AV_W8(p + 17, (sps->bit_depth - 8) | 0xfc);
220 * bit(5) reserved = ‘11111’b;
221 * unsigned int(3) bitDepthChromaMinus8;
223 AV_W8(p + 18, (sps->bit_depth_chroma - 8) | 0xfc);
225 /* bit(16) avgFrameRate; */
229 * bit(2) constantFrameRate;
230 * bit(3) numTemporalLayers;
231 * bit(1) temporalIdNested;
232 * unsigned int(2) lengthSizeMinusOne;
234 AV_W8(p + 21, 0 << 6 |
235 sps->max_sub_layers << 3 |
236 sps->temporal_id_nesting_flag << 2 |
239 /* unsigned int(8) numOfArrays; */
245 * bit(1) array_completeness;
246 * unsigned int(1) reserved = 0;
247 * unsigned int(6) NAL_unit_type;
250 HEVC_NAL_VPS & 0x3f);
251 /* unsigned int(16) numNalus; */
253 /* unsigned int(16) nalUnitLength; */
254 AV_WB16(p + 3, vps->data_size);
255 /* bit(8*nalUnitLength) nalUnit; */
256 memcpy(p + 5, vps->data, vps->data_size);
257 p += 5 + vps->data_size;
261 HEVC_NAL_SPS & 0x3f);
263 AV_WB16(p + 3, sps->data_size);
264 memcpy(p + 5, sps->data, sps->data_size);
265 p += 5 + sps->data_size;
269 HEVC_NAL_PPS & 0x3f);
270 AV_WB16(p + 1, num_pps);
272 for (i = 0; i < HEVC_MAX_PPS_COUNT; i++) {
273 if (h->ps.pps_list[i]) {
274 const HEVCPPS *pps = (const HEVCPPS *)h->ps.pps_list[i]->data;
275 AV_WB16(p, pps->data_size);
276 memcpy(p + 2, pps->data, pps->data_size);
277 p += 2 + pps->data_size;
281 av_assert0(p - vt_extradata == vt_extradata_size);
283 data = CFDataCreate(kCFAllocatorDefault, vt_extradata, vt_extradata_size);
284 av_free(vt_extradata);
288 int ff_videotoolbox_buffer_create(VTContext *vtctx, AVFrame *frame)
290 av_buffer_unref(&frame->buf[0]);
292 frame->buf[0] = av_buffer_create((uint8_t*)vtctx->frame,
293 sizeof(vtctx->frame),
294 videotoolbox_buffer_release,
296 AV_BUFFER_FLAG_READONLY);
297 if (!frame->buf[0]) {
298 return AVERROR(ENOMEM);
301 frame->data[3] = (uint8_t*)vtctx->frame;
307 int ff_videotoolbox_h264_start_frame(AVCodecContext *avctx,
308 const uint8_t *buffer,
311 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
312 H264Context *h = avctx->priv_data;
314 if (h->is_avc == 1) {
315 return videotoolbox_buffer_copy(vtctx, buffer, size);
321 static int videotoolbox_h264_decode_params(AVCodecContext *avctx,
323 const uint8_t *buffer,
326 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
327 H264Context *h = avctx->priv_data;
329 // save sps header (profile/level) used to create decoder session
331 memcpy(vtctx->sps, h->ps.sps->data + 1, 3);
333 if (type == H264_NAL_SPS) {
334 if (size > 4 && memcmp(vtctx->sps, buffer + 1, 3) != 0) {
335 vtctx->reconfig_needed = true;
336 memcpy(vtctx->sps, buffer + 1, 3);
340 // pass-through SPS/PPS changes to the decoder
341 return ff_videotoolbox_h264_decode_slice(avctx, buffer, size);
344 int ff_videotoolbox_h264_decode_slice(AVCodecContext *avctx,
345 const uint8_t *buffer,
348 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
349 H264Context *h = avctx->priv_data;
355 tmp = av_fast_realloc(vtctx->bitstream,
356 &vtctx->allocated_size,
357 vtctx->bitstream_size+size+4);
359 return AVERROR(ENOMEM);
361 vtctx->bitstream = tmp;
363 AV_WB32(vtctx->bitstream + vtctx->bitstream_size, size);
364 memcpy(vtctx->bitstream + vtctx->bitstream_size + 4, buffer, size);
366 vtctx->bitstream_size += size + 4;
371 int ff_videotoolbox_uninit(AVCodecContext *avctx)
373 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
375 av_freep(&vtctx->bitstream);
377 CVPixelBufferRelease(vtctx->frame);
383 #if CONFIG_VIDEOTOOLBOX
384 // Return the AVVideotoolboxContext that matters currently. Where it comes from
385 // depends on the API used.
386 static AVVideotoolboxContext *videotoolbox_get_context(AVCodecContext *avctx)
388 // Somewhat tricky because the user can call av_videotoolbox_default_free()
389 // at any time, even when the codec is closed.
390 if (avctx->internal && avctx->internal->hwaccel_priv_data) {
391 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
393 return vtctx->vt_ctx;
395 return avctx->hwaccel_context;
398 static int videotoolbox_buffer_create(AVCodecContext *avctx, AVFrame *frame)
400 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
401 CVPixelBufferRef pixbuf = (CVPixelBufferRef)vtctx->frame;
402 OSType pixel_format = CVPixelBufferGetPixelFormatType(pixbuf);
403 enum AVPixelFormat sw_format = av_map_videotoolbox_format_to_pixfmt(pixel_format);
404 int width = CVPixelBufferGetWidth(pixbuf);
405 int height = CVPixelBufferGetHeight(pixbuf);
406 AVHWFramesContext *cached_frames;
409 ret = ff_videotoolbox_buffer_create(vtctx, frame);
413 // Old API code path.
414 if (!vtctx->cached_hw_frames_ctx)
417 cached_frames = (AVHWFramesContext*)vtctx->cached_hw_frames_ctx->data;
419 if (cached_frames->sw_format != sw_format ||
420 cached_frames->width != width ||
421 cached_frames->height != height) {
422 AVBufferRef *hw_frames_ctx = av_hwframe_ctx_alloc(cached_frames->device_ref);
423 AVHWFramesContext *hw_frames;
425 return AVERROR(ENOMEM);
427 hw_frames = (AVHWFramesContext*)hw_frames_ctx->data;
428 hw_frames->format = cached_frames->format;
429 hw_frames->sw_format = sw_format;
430 hw_frames->width = width;
431 hw_frames->height = height;
433 ret = av_hwframe_ctx_init(hw_frames_ctx);
435 av_buffer_unref(&hw_frames_ctx);
439 av_buffer_unref(&vtctx->cached_hw_frames_ctx);
440 vtctx->cached_hw_frames_ctx = hw_frames_ctx;
443 av_buffer_unref(&frame->hw_frames_ctx);
444 frame->hw_frames_ctx = av_buffer_ref(vtctx->cached_hw_frames_ctx);
445 if (!frame->hw_frames_ctx)
446 return AVERROR(ENOMEM);
451 static void videotoolbox_write_mp4_descr_length(PutByteContext *pb, int length)
456 for (i = 3; i >= 0; i--) {
457 b = (length >> (i * 7)) & 0x7F;
461 bytestream2_put_byteu(pb, b);
465 static CFDataRef videotoolbox_esds_extradata_create(AVCodecContext *avctx)
468 uint8_t *rw_extradata;
470 int full_size = 3 + 5 + 13 + 5 + avctx->extradata_size + 3;
471 // ES_DescrTag data + DecoderConfigDescrTag + data + DecSpecificInfoTag + size + SLConfigDescriptor
472 int config_size = 13 + 5 + avctx->extradata_size;
475 if (!(rw_extradata = av_mallocz(full_size + VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING)))
478 bytestream2_init_writer(&pb, rw_extradata, full_size + VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING);
479 bytestream2_put_byteu(&pb, 0); // version
480 bytestream2_put_ne24(&pb, 0); // flags
482 // elementary stream descriptor
483 bytestream2_put_byteu(&pb, 0x03); // ES_DescrTag
484 videotoolbox_write_mp4_descr_length(&pb, full_size);
485 bytestream2_put_ne16(&pb, 0); // esid
486 bytestream2_put_byteu(&pb, 0); // stream priority (0-32)
488 // decoder configuration descriptor
489 bytestream2_put_byteu(&pb, 0x04); // DecoderConfigDescrTag
490 videotoolbox_write_mp4_descr_length(&pb, config_size);
491 bytestream2_put_byteu(&pb, 32); // object type indication. 32 = AV_CODEC_ID_MPEG4
492 bytestream2_put_byteu(&pb, 0x11); // stream type
493 bytestream2_put_ne24(&pb, 0); // buffer size
494 bytestream2_put_ne32(&pb, 0); // max bitrate
495 bytestream2_put_ne32(&pb, 0); // avg bitrate
497 // decoder specific descriptor
498 bytestream2_put_byteu(&pb, 0x05); ///< DecSpecificInfoTag
499 videotoolbox_write_mp4_descr_length(&pb, avctx->extradata_size);
501 bytestream2_put_buffer(&pb, avctx->extradata, avctx->extradata_size);
503 // SLConfigDescriptor
504 bytestream2_put_byteu(&pb, 0x06); // SLConfigDescrTag
505 bytestream2_put_byteu(&pb, 0x01); // length
506 bytestream2_put_byteu(&pb, 0x02); //
508 s = bytestream2_size_p(&pb);
510 data = CFDataCreate(kCFAllocatorDefault, rw_extradata, s);
512 av_freep(&rw_extradata);
516 static CMSampleBufferRef videotoolbox_sample_buffer_create(CMFormatDescriptionRef fmt_desc,
521 CMBlockBufferRef block_buf;
522 CMSampleBufferRef sample_buf;
527 status = CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault,// structureAllocator
528 buffer, // memoryBlock
530 kCFAllocatorNull, // blockAllocator
531 NULL, // customBlockSource
538 status = CMSampleBufferCreate(kCFAllocatorDefault, // allocator
539 block_buf, // dataBuffer
541 0, // makeDataReadyCallback
542 0, // makeDataReadyRefcon
543 fmt_desc, // formatDescription
545 0, // numSampleTimingEntries
546 NULL, // sampleTimingArray
547 0, // numSampleSizeEntries
548 NULL, // sampleSizeArray
553 CFRelease(block_buf);
558 static void videotoolbox_decoder_callback(void *opaque,
559 void *sourceFrameRefCon,
561 VTDecodeInfoFlags flags,
562 CVImageBufferRef image_buffer,
566 AVCodecContext *avctx = opaque;
567 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
570 CVPixelBufferRelease(vtctx->frame);
575 av_log(NULL, AV_LOG_DEBUG, "vt decoder cb: output image buffer is null\n");
579 vtctx->frame = CVPixelBufferRetain(image_buffer);
582 static OSStatus videotoolbox_session_decode_frame(AVCodecContext *avctx)
585 CMSampleBufferRef sample_buf;
586 AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
587 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
589 sample_buf = videotoolbox_sample_buffer_create(videotoolbox->cm_fmt_desc,
591 vtctx->bitstream_size);
596 status = VTDecompressionSessionDecodeFrame(videotoolbox->session,
599 NULL, // sourceFrameRefCon
602 status = VTDecompressionSessionWaitForAsynchronousFrames(videotoolbox->session);
604 CFRelease(sample_buf);
609 static CMVideoFormatDescriptionRef videotoolbox_format_desc_create(CMVideoCodecType codec_type,
610 CFDictionaryRef decoder_spec,
614 CMFormatDescriptionRef cm_fmt_desc;
617 status = CMVideoFormatDescriptionCreate(kCFAllocatorDefault,
621 decoder_spec, // Dictionary of extension
630 static CFDictionaryRef videotoolbox_buffer_attributes_create(int width,
634 CFMutableDictionaryRef buffer_attributes;
635 CFMutableDictionaryRef io_surface_properties;
636 CFNumberRef cv_pix_fmt;
640 w = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &width);
641 h = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &height);
642 cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &pix_fmt);
644 buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
646 &kCFTypeDictionaryKeyCallBacks,
647 &kCFTypeDictionaryValueCallBacks);
648 io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
650 &kCFTypeDictionaryKeyCallBacks,
651 &kCFTypeDictionaryValueCallBacks);
654 CFDictionarySetValue(buffer_attributes, kCVPixelBufferPixelFormatTypeKey, cv_pix_fmt);
655 CFDictionarySetValue(buffer_attributes, kCVPixelBufferIOSurfacePropertiesKey, io_surface_properties);
656 CFDictionarySetValue(buffer_attributes, kCVPixelBufferWidthKey, w);
657 CFDictionarySetValue(buffer_attributes, kCVPixelBufferHeightKey, h);
659 CFDictionarySetValue(buffer_attributes, kCVPixelBufferOpenGLESCompatibilityKey, kCFBooleanTrue);
661 CFDictionarySetValue(buffer_attributes, kCVPixelBufferIOSurfaceOpenGLTextureCompatibilityKey, kCFBooleanTrue);
664 CFRelease(io_surface_properties);
665 CFRelease(cv_pix_fmt);
669 return buffer_attributes;
672 static CFDictionaryRef videotoolbox_decoder_config_create(CMVideoCodecType codec_type,
673 AVCodecContext *avctx)
675 CFMutableDictionaryRef config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
677 &kCFTypeDictionaryKeyCallBacks,
678 &kCFTypeDictionaryValueCallBacks);
680 CFDictionarySetValue(config_info,
681 kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder,
684 CFMutableDictionaryRef avc_info;
685 CFDataRef data = NULL;
687 avc_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
689 &kCFTypeDictionaryKeyCallBacks,
690 &kCFTypeDictionaryValueCallBacks);
692 switch (codec_type) {
693 case kCMVideoCodecType_MPEG4Video :
694 if (avctx->extradata_size)
695 data = videotoolbox_esds_extradata_create(avctx);
697 CFDictionarySetValue(avc_info, CFSTR("esds"), data);
699 case kCMVideoCodecType_H264 :
700 data = ff_videotoolbox_avcc_extradata_create(avctx);
702 CFDictionarySetValue(avc_info, CFSTR("avcC"), data);
704 case kCMVideoCodecType_HEVC :
705 data = ff_videotoolbox_hvcc_extradata_create(avctx);
707 CFDictionarySetValue(avc_info, CFSTR("hvcC"), data);
713 CFDictionarySetValue(config_info,
714 kCMFormatDescriptionExtension_SampleDescriptionExtensionAtoms,
724 static int videotoolbox_start(AVCodecContext *avctx)
726 AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
728 VTDecompressionOutputCallbackRecord decoder_cb;
729 CFDictionaryRef decoder_spec;
730 CFDictionaryRef buf_attr;
733 av_log(avctx, AV_LOG_ERROR, "hwaccel context is not set\n");
737 switch( avctx->codec_id ) {
738 case AV_CODEC_ID_H263 :
739 videotoolbox->cm_codec_type = kCMVideoCodecType_H263;
741 case AV_CODEC_ID_H264 :
742 videotoolbox->cm_codec_type = kCMVideoCodecType_H264;
744 case AV_CODEC_ID_HEVC :
745 videotoolbox->cm_codec_type = kCMVideoCodecType_HEVC;
747 case AV_CODEC_ID_MPEG1VIDEO :
748 videotoolbox->cm_codec_type = kCMVideoCodecType_MPEG1Video;
750 case AV_CODEC_ID_MPEG2VIDEO :
751 videotoolbox->cm_codec_type = kCMVideoCodecType_MPEG2Video;
753 case AV_CODEC_ID_MPEG4 :
754 videotoolbox->cm_codec_type = kCMVideoCodecType_MPEG4Video;
760 decoder_spec = videotoolbox_decoder_config_create(videotoolbox->cm_codec_type, avctx);
763 av_log(avctx, AV_LOG_ERROR, "decoder specification creation failed\n");
767 videotoolbox->cm_fmt_desc = videotoolbox_format_desc_create(videotoolbox->cm_codec_type,
771 if (!videotoolbox->cm_fmt_desc) {
773 CFRelease(decoder_spec);
775 av_log(avctx, AV_LOG_ERROR, "format description creation failed\n");
779 buf_attr = videotoolbox_buffer_attributes_create(avctx->width,
781 videotoolbox->cv_pix_fmt_type);
783 decoder_cb.decompressionOutputCallback = videotoolbox_decoder_callback;
784 decoder_cb.decompressionOutputRefCon = avctx;
786 status = VTDecompressionSessionCreate(NULL, // allocator
787 videotoolbox->cm_fmt_desc, // videoFormatDescription
788 decoder_spec, // videoDecoderSpecification
789 buf_attr, // destinationImageBufferAttributes
790 &decoder_cb, // outputCallback
791 &videotoolbox->session); // decompressionSessionOut
794 CFRelease(decoder_spec);
799 case kVTVideoDecoderNotAvailableNowErr:
800 av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox session not available.\n");
801 return AVERROR(ENOSYS);
802 case kVTVideoDecoderUnsupportedDataFormatErr:
803 av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox does not support this format.\n");
804 return AVERROR(ENOSYS);
805 case kVTVideoDecoderMalfunctionErr:
806 av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox malfunction.\n");
807 return AVERROR(EINVAL);
808 case kVTVideoDecoderBadDataErr:
809 av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox reported invalid data.\n");
810 return AVERROR_INVALIDDATA;
814 av_log(avctx, AV_LOG_VERBOSE, "Unknown VideoToolbox session creation error %u\n", (unsigned)status);
815 return AVERROR_UNKNOWN;
819 static void videotoolbox_stop(AVCodecContext *avctx)
821 AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
825 if (videotoolbox->cm_fmt_desc) {
826 CFRelease(videotoolbox->cm_fmt_desc);
827 videotoolbox->cm_fmt_desc = NULL;
830 if (videotoolbox->session) {
831 VTDecompressionSessionInvalidate(videotoolbox->session);
832 CFRelease(videotoolbox->session);
833 videotoolbox->session = NULL;
837 static const char *videotoolbox_error_string(OSStatus status)
840 case kVTVideoDecoderBadDataErr:
842 case kVTVideoDecoderMalfunctionErr:
843 return "decoder malfunction";
844 case kVTInvalidSessionErr:
845 return "invalid session";
850 static int videotoolbox_common_end_frame(AVCodecContext *avctx, AVFrame *frame)
853 AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
854 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
856 if (vtctx->reconfig_needed == true) {
857 vtctx->reconfig_needed = false;
858 av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox decoder needs reconfig, restarting..\n");
859 videotoolbox_stop(avctx);
860 if (videotoolbox_start(avctx) != 0) {
861 return AVERROR_EXTERNAL;
865 if (!videotoolbox->session || !vtctx->bitstream || !vtctx->bitstream_size)
866 return AVERROR_INVALIDDATA;
868 status = videotoolbox_session_decode_frame(avctx);
869 if (status != noErr) {
870 if (status == kVTVideoDecoderMalfunctionErr || status == kVTInvalidSessionErr)
871 vtctx->reconfig_needed = true;
872 av_log(avctx, AV_LOG_ERROR, "Failed to decode frame (%s, %d)\n", videotoolbox_error_string(status), (int)status);
873 return AVERROR_UNKNOWN;
877 vtctx->reconfig_needed = true;
878 return AVERROR_UNKNOWN;
881 return videotoolbox_buffer_create(avctx, frame);
884 static int videotoolbox_h264_end_frame(AVCodecContext *avctx)
886 H264Context *h = avctx->priv_data;
887 AVFrame *frame = h->cur_pic_ptr->f;
888 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
889 int ret = videotoolbox_common_end_frame(avctx, frame);
890 vtctx->bitstream_size = 0;
894 static int videotoolbox_hevc_decode_params(AVCodecContext *avctx,
896 const uint8_t *buffer,
899 return ff_videotoolbox_h264_decode_slice(avctx, buffer, size);
902 static int videotoolbox_hevc_end_frame(AVCodecContext *avctx)
904 HEVCContext *h = avctx->priv_data;
905 AVFrame *frame = h->ref->frame;
906 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
909 ret = videotoolbox_common_end_frame(avctx, frame);
910 vtctx->bitstream_size = 0;
914 static int videotoolbox_mpeg_start_frame(AVCodecContext *avctx,
915 const uint8_t *buffer,
918 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
920 return videotoolbox_buffer_copy(vtctx, buffer, size);
923 static int videotoolbox_mpeg_decode_slice(AVCodecContext *avctx,
924 const uint8_t *buffer,
930 static int videotoolbox_mpeg_end_frame(AVCodecContext *avctx)
932 MpegEncContext *s = avctx->priv_data;
933 AVFrame *frame = s->current_picture_ptr->f;
935 return videotoolbox_common_end_frame(avctx, frame);
938 static int videotoolbox_uninit(AVCodecContext *avctx)
940 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
944 ff_videotoolbox_uninit(avctx);
947 videotoolbox_stop(avctx);
949 av_buffer_unref(&vtctx->cached_hw_frames_ctx);
950 av_freep(&vtctx->vt_ctx);
955 static int videotoolbox_common_init(AVCodecContext *avctx)
957 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
958 AVHWFramesContext *hw_frames;
961 // Old API - do nothing.
962 if (avctx->hwaccel_context)
965 if (!avctx->hw_frames_ctx && !avctx->hw_device_ctx) {
966 av_log(avctx, AV_LOG_ERROR,
967 "Either hw_frames_ctx or hw_device_ctx must be set.\n");
968 return AVERROR(EINVAL);
971 vtctx->vt_ctx = av_videotoolbox_alloc_context();
972 if (!vtctx->vt_ctx) {
973 err = AVERROR(ENOMEM);
977 if (avctx->hw_frames_ctx) {
978 hw_frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
980 avctx->hw_frames_ctx = av_hwframe_ctx_alloc(avctx->hw_device_ctx);
981 if (!avctx->hw_frames_ctx) {
982 err = AVERROR(ENOMEM);
986 hw_frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
987 hw_frames->format = AV_PIX_FMT_VIDEOTOOLBOX;
988 hw_frames->sw_format = AV_PIX_FMT_NV12; // same as av_videotoolbox_alloc_context()
989 hw_frames->width = avctx->width;
990 hw_frames->height = avctx->height;
992 err = av_hwframe_ctx_init(avctx->hw_frames_ctx);
994 av_buffer_unref(&avctx->hw_frames_ctx);
999 vtctx->cached_hw_frames_ctx = av_buffer_ref(avctx->hw_frames_ctx);
1000 if (!vtctx->cached_hw_frames_ctx) {
1001 err = AVERROR(ENOMEM);
1005 vtctx->vt_ctx->cv_pix_fmt_type =
1006 av_map_videotoolbox_format_from_pixfmt(hw_frames->sw_format);
1007 if (!vtctx->vt_ctx->cv_pix_fmt_type) {
1008 av_log(avctx, AV_LOG_ERROR, "Unknown sw_format.\n");
1009 err = AVERROR(EINVAL);
1013 err = videotoolbox_start(avctx);
1020 videotoolbox_uninit(avctx);
1024 static int videotoolbox_frame_params(AVCodecContext *avctx,
1025 AVBufferRef *hw_frames_ctx)
1027 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)hw_frames_ctx->data;
1029 frames_ctx->format = AV_PIX_FMT_VIDEOTOOLBOX;
1030 frames_ctx->width = avctx->coded_width;
1031 frames_ctx->height = avctx->coded_height;
1032 frames_ctx->sw_format = AV_PIX_FMT_NV12;
1037 const AVHWAccel ff_h263_videotoolbox_hwaccel = {
1038 .name = "h263_videotoolbox",
1039 .type = AVMEDIA_TYPE_VIDEO,
1040 .id = AV_CODEC_ID_H263,
1041 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
1042 .alloc_frame = ff_videotoolbox_alloc_frame,
1043 .start_frame = videotoolbox_mpeg_start_frame,
1044 .decode_slice = videotoolbox_mpeg_decode_slice,
1045 .end_frame = videotoolbox_mpeg_end_frame,
1046 .frame_params = videotoolbox_frame_params,
1047 .init = videotoolbox_common_init,
1048 .uninit = videotoolbox_uninit,
1049 .priv_data_size = sizeof(VTContext),
1052 const AVHWAccel ff_hevc_videotoolbox_hwaccel = {
1053 .name = "hevc_videotoolbox",
1054 .type = AVMEDIA_TYPE_VIDEO,
1055 .id = AV_CODEC_ID_HEVC,
1056 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
1057 .alloc_frame = ff_videotoolbox_alloc_frame,
1058 .start_frame = ff_videotoolbox_h264_start_frame,
1059 .decode_slice = ff_videotoolbox_h264_decode_slice,
1060 .decode_params = videotoolbox_hevc_decode_params,
1061 .end_frame = videotoolbox_hevc_end_frame,
1062 .frame_params = videotoolbox_frame_params,
1063 .init = videotoolbox_common_init,
1064 .uninit = ff_videotoolbox_uninit,
1065 .priv_data_size = sizeof(VTContext),
1068 const AVHWAccel ff_h264_videotoolbox_hwaccel = {
1069 .name = "h264_videotoolbox",
1070 .type = AVMEDIA_TYPE_VIDEO,
1071 .id = AV_CODEC_ID_H264,
1072 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
1073 .alloc_frame = ff_videotoolbox_alloc_frame,
1074 .start_frame = ff_videotoolbox_h264_start_frame,
1075 .decode_slice = ff_videotoolbox_h264_decode_slice,
1076 .decode_params = videotoolbox_h264_decode_params,
1077 .end_frame = videotoolbox_h264_end_frame,
1078 .frame_params = videotoolbox_frame_params,
1079 .init = videotoolbox_common_init,
1080 .uninit = videotoolbox_uninit,
1081 .priv_data_size = sizeof(VTContext),
1084 const AVHWAccel ff_mpeg1_videotoolbox_hwaccel = {
1085 .name = "mpeg1_videotoolbox",
1086 .type = AVMEDIA_TYPE_VIDEO,
1087 .id = AV_CODEC_ID_MPEG1VIDEO,
1088 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
1089 .alloc_frame = ff_videotoolbox_alloc_frame,
1090 .start_frame = videotoolbox_mpeg_start_frame,
1091 .decode_slice = videotoolbox_mpeg_decode_slice,
1092 .end_frame = videotoolbox_mpeg_end_frame,
1093 .frame_params = videotoolbox_frame_params,
1094 .init = videotoolbox_common_init,
1095 .uninit = videotoolbox_uninit,
1096 .priv_data_size = sizeof(VTContext),
1099 const AVHWAccel ff_mpeg2_videotoolbox_hwaccel = {
1100 .name = "mpeg2_videotoolbox",
1101 .type = AVMEDIA_TYPE_VIDEO,
1102 .id = AV_CODEC_ID_MPEG2VIDEO,
1103 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
1104 .alloc_frame = ff_videotoolbox_alloc_frame,
1105 .start_frame = videotoolbox_mpeg_start_frame,
1106 .decode_slice = videotoolbox_mpeg_decode_slice,
1107 .end_frame = videotoolbox_mpeg_end_frame,
1108 .frame_params = videotoolbox_frame_params,
1109 .init = videotoolbox_common_init,
1110 .uninit = videotoolbox_uninit,
1111 .priv_data_size = sizeof(VTContext),
1114 const AVHWAccel ff_mpeg4_videotoolbox_hwaccel = {
1115 .name = "mpeg4_videotoolbox",
1116 .type = AVMEDIA_TYPE_VIDEO,
1117 .id = AV_CODEC_ID_MPEG4,
1118 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
1119 .alloc_frame = ff_videotoolbox_alloc_frame,
1120 .start_frame = videotoolbox_mpeg_start_frame,
1121 .decode_slice = videotoolbox_mpeg_decode_slice,
1122 .end_frame = videotoolbox_mpeg_end_frame,
1123 .frame_params = videotoolbox_frame_params,
1124 .init = videotoolbox_common_init,
1125 .uninit = videotoolbox_uninit,
1126 .priv_data_size = sizeof(VTContext),
1129 AVVideotoolboxContext *av_videotoolbox_alloc_context(void)
1131 AVVideotoolboxContext *ret = av_mallocz(sizeof(*ret));
1134 ret->output_callback = videotoolbox_decoder_callback;
1135 ret->cv_pix_fmt_type = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
1141 int av_videotoolbox_default_init(AVCodecContext *avctx)
1143 return av_videotoolbox_default_init2(avctx, NULL);
1146 int av_videotoolbox_default_init2(AVCodecContext *avctx, AVVideotoolboxContext *vtctx)
1148 avctx->hwaccel_context = vtctx ?: av_videotoolbox_alloc_context();
1149 if (!avctx->hwaccel_context)
1150 return AVERROR(ENOMEM);
1151 return videotoolbox_start(avctx);
1154 void av_videotoolbox_default_free(AVCodecContext *avctx)
1157 videotoolbox_stop(avctx);
1158 av_freep(&avctx->hwaccel_context);
1160 #endif /* CONFIG_VIDEOTOOLBOX */