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"
29 #include "vda_vt_internal.h"
30 #include "libavutil/avutil.h"
31 #include "bytestream.h"
33 #include "mpegvideo.h"
35 #ifndef kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder
36 # define kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder CFSTR("RequireHardwareAcceleratedVideoDecoder")
39 #define VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING 12
41 static void videotoolbox_buffer_release(void *opaque, uint8_t *data)
43 CVPixelBufferRef cv_buffer = (CVImageBufferRef)data;
44 CVPixelBufferRelease(cv_buffer);
47 static int videotoolbox_buffer_copy(VTContext *vtctx,
48 const uint8_t *buffer,
53 tmp = av_fast_realloc(vtctx->bitstream,
54 &vtctx->allocated_size,
58 return AVERROR(ENOMEM);
60 vtctx->bitstream = tmp;
61 memcpy(vtctx->bitstream, buffer, size);
62 vtctx->bitstream_size = size;
67 int ff_videotoolbox_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
69 frame->width = avctx->width;
70 frame->height = avctx->height;
71 frame->format = avctx->pix_fmt;
72 frame->buf[0] = av_buffer_alloc(1);
75 return AVERROR(ENOMEM);
80 #define AV_W8(p, v) *(p) = (v)
82 CFDataRef ff_videotoolbox_avcc_extradata_create(AVCodecContext *avctx)
84 H264Context *h = avctx->priv_data;
85 CFDataRef data = NULL;
87 int vt_extradata_size = 6 + 3 + h->ps.sps->data_size + 4 + h->ps.pps->data_size;
88 uint8_t *vt_extradata = av_malloc(vt_extradata_size);
94 AV_W8(p + 0, 1); /* version */
95 AV_W8(p + 1, h->ps.sps->data[0]); /* profile */
96 AV_W8(p + 2, h->ps.sps->data[1]); /* profile compat */
97 AV_W8(p + 3, h->ps.sps->data[2]); /* level */
98 AV_W8(p + 4, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 3 (11) */
99 AV_W8(p + 5, 0xe1); /* 3 bits reserved (111) + 5 bits number of sps (00001) */
100 AV_WB16(p + 6, h->ps.sps->data_size + 1);
101 AV_W8(p + 8, NAL_SPS | (3 << 5)); // NAL unit header
102 memcpy(p + 9, h->ps.sps->data, h->ps.sps->data_size);
103 p += 9 + h->ps.sps->data_size;
104 AV_W8(p + 0, 1); /* number of pps */
105 AV_WB16(p + 1, h->ps.pps->data_size + 1);
106 AV_W8(p + 3, NAL_PPS | (3 << 5)); // NAL unit header
107 memcpy(p + 4, h->ps.pps->data, h->ps.pps->data_size);
109 p += 4 + h->ps.pps->data_size;
110 av_assert0(p - vt_extradata == vt_extradata_size);
112 data = CFDataCreate(kCFAllocatorDefault, vt_extradata, vt_extradata_size);
113 av_free(vt_extradata);
117 int ff_videotoolbox_buffer_create(VTContext *vtctx, AVFrame *frame)
119 av_buffer_unref(&frame->buf[0]);
121 frame->buf[0] = av_buffer_create((uint8_t*)vtctx->frame,
122 sizeof(vtctx->frame),
123 videotoolbox_buffer_release,
125 AV_BUFFER_FLAG_READONLY);
126 if (!frame->buf[0]) {
127 return AVERROR(ENOMEM);
130 frame->data[3] = (uint8_t*)vtctx->frame;
136 int ff_videotoolbox_h264_start_frame(AVCodecContext *avctx,
137 const uint8_t *buffer,
140 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
141 H264Context *h = avctx->priv_data;
143 vtctx->bitstream_size = 0;
145 if (h->is_avc == 1) {
146 return videotoolbox_buffer_copy(vtctx, buffer, size);
152 int ff_videotoolbox_h264_decode_slice(AVCodecContext *avctx,
153 const uint8_t *buffer,
156 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
157 H264Context *h = avctx->priv_data;
163 tmp = av_fast_realloc(vtctx->bitstream,
164 &vtctx->allocated_size,
165 vtctx->bitstream_size+size+4);
167 return AVERROR(ENOMEM);
169 vtctx->bitstream = tmp;
171 AV_WB32(vtctx->bitstream + vtctx->bitstream_size, size);
172 memcpy(vtctx->bitstream + vtctx->bitstream_size + 4, buffer, size);
174 vtctx->bitstream_size += size + 4;
179 int ff_videotoolbox_uninit(AVCodecContext *avctx)
181 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
183 av_freep(&vtctx->bitstream);
185 CVPixelBufferRelease(vtctx->frame);
191 #if CONFIG_VIDEOTOOLBOX
192 static void videotoolbox_write_mp4_descr_length(PutByteContext *pb, int length)
197 for (i = 3; i >= 0; i--) {
198 b = (length >> (i * 7)) & 0x7F;
202 bytestream2_put_byteu(pb, b);
206 static CFDataRef videotoolbox_esds_extradata_create(AVCodecContext *avctx)
209 uint8_t *rw_extradata;
211 int full_size = 3 + 5 + 13 + 5 + avctx->extradata_size + 3;
212 // ES_DescrTag data + DecoderConfigDescrTag + data + DecSpecificInfoTag + size + SLConfigDescriptor
213 int config_size = 13 + 5 + avctx->extradata_size;
216 if (!(rw_extradata = av_mallocz(full_size + VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING)))
219 bytestream2_init_writer(&pb, rw_extradata, full_size + VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING);
220 bytestream2_put_byteu(&pb, 0); // version
221 bytestream2_put_ne24(&pb, 0); // flags
223 // elementary stream descriptor
224 bytestream2_put_byteu(&pb, 0x03); // ES_DescrTag
225 videotoolbox_write_mp4_descr_length(&pb, full_size);
226 bytestream2_put_ne16(&pb, 0); // esid
227 bytestream2_put_byteu(&pb, 0); // stream priority (0-32)
229 // decoder configuration descriptor
230 bytestream2_put_byteu(&pb, 0x04); // DecoderConfigDescrTag
231 videotoolbox_write_mp4_descr_length(&pb, config_size);
232 bytestream2_put_byteu(&pb, 32); // object type indication. 32 = AV_CODEC_ID_MPEG4
233 bytestream2_put_byteu(&pb, 0x11); // stream type
234 bytestream2_put_ne24(&pb, 0); // buffer size
235 bytestream2_put_ne32(&pb, 0); // max bitrate
236 bytestream2_put_ne32(&pb, 0); // avg bitrate
238 // decoder specific descriptor
239 bytestream2_put_byteu(&pb, 0x05); ///< DecSpecificInfoTag
240 videotoolbox_write_mp4_descr_length(&pb, avctx->extradata_size);
242 bytestream2_put_buffer(&pb, avctx->extradata, avctx->extradata_size);
244 // SLConfigDescriptor
245 bytestream2_put_byteu(&pb, 0x06); // SLConfigDescrTag
246 bytestream2_put_byteu(&pb, 0x01); // length
247 bytestream2_put_byteu(&pb, 0x02); //
249 s = bytestream2_size_p(&pb);
251 data = CFDataCreate(kCFAllocatorDefault, rw_extradata, s);
253 av_freep(&rw_extradata);
257 static CMSampleBufferRef videotoolbox_sample_buffer_create(CMFormatDescriptionRef fmt_desc,
262 CMBlockBufferRef block_buf;
263 CMSampleBufferRef sample_buf;
268 status = CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault,// structureAllocator
269 buffer, // memoryBlock
271 kCFAllocatorNull, // blockAllocator
272 NULL, // customBlockSource
279 status = CMSampleBufferCreate(kCFAllocatorDefault, // allocator
280 block_buf, // dataBuffer
282 0, // makeDataReadyCallback
283 0, // makeDataReadyRefcon
284 fmt_desc, // formatDescription
286 0, // numSampleTimingEntries
287 NULL, // sampleTimingArray
288 0, // numSampleSizeEntries
289 NULL, // sampleSizeArray
294 CFRelease(block_buf);
299 static void videotoolbox_decoder_callback(void *opaque,
300 void *sourceFrameRefCon,
302 VTDecodeInfoFlags flags,
303 CVImageBufferRef image_buffer,
307 AVCodecContext *avctx = opaque;
308 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
311 CVPixelBufferRelease(vtctx->frame);
316 av_log(NULL, AV_LOG_DEBUG, "vt decoder cb: output image buffer is null\n");
320 vtctx->frame = CVPixelBufferRetain(image_buffer);
323 static OSStatus videotoolbox_session_decode_frame(AVCodecContext *avctx)
326 CMSampleBufferRef sample_buf;
327 AVVideotoolboxContext *videotoolbox = avctx->hwaccel_context;
328 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
330 sample_buf = videotoolbox_sample_buffer_create(videotoolbox->cm_fmt_desc,
332 vtctx->bitstream_size);
337 status = VTDecompressionSessionDecodeFrame(videotoolbox->session,
340 NULL, // sourceFrameRefCon
343 status = VTDecompressionSessionWaitForAsynchronousFrames(videotoolbox->session);
345 CFRelease(sample_buf);
350 static int videotoolbox_common_end_frame(AVCodecContext *avctx, AVFrame *frame)
353 AVVideotoolboxContext *videotoolbox = avctx->hwaccel_context;
354 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
356 av_buffer_unref(&frame->buf[0]);
358 if (!videotoolbox->session || !vtctx->bitstream)
359 return AVERROR_INVALIDDATA;
361 status = videotoolbox_session_decode_frame(avctx);
364 av_log(avctx, AV_LOG_ERROR, "Failed to decode frame (%d)\n", status);
365 return AVERROR_UNKNOWN;
369 return AVERROR_UNKNOWN;
371 return ff_videotoolbox_buffer_create(vtctx, frame);
374 static int videotoolbox_h264_end_frame(AVCodecContext *avctx)
376 H264Context *h = avctx->priv_data;
377 AVFrame *frame = h->cur_pic_ptr->f;
379 return videotoolbox_common_end_frame(avctx, frame);
382 static int videotoolbox_mpeg_start_frame(AVCodecContext *avctx,
383 const uint8_t *buffer,
386 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
388 return videotoolbox_buffer_copy(vtctx, buffer, size);
391 static int videotoolbox_mpeg_decode_slice(AVCodecContext *avctx,
392 const uint8_t *buffer,
398 static int videotoolbox_mpeg_end_frame(AVCodecContext *avctx)
400 MpegEncContext *s = avctx->priv_data;
401 AVFrame *frame = s->current_picture_ptr->f;
403 return videotoolbox_common_end_frame(avctx, frame);
406 static CFDictionaryRef videotoolbox_decoder_config_create(CMVideoCodecType codec_type,
407 AVCodecContext *avctx)
409 CFMutableDictionaryRef config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
411 &kCFTypeDictionaryKeyCallBacks,
412 &kCFTypeDictionaryValueCallBacks);
414 CFDictionarySetValue(config_info,
415 kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder,
418 if (avctx->extradata_size) {
419 CFMutableDictionaryRef avc_info;
420 CFDataRef data = NULL;
422 avc_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
424 &kCFTypeDictionaryKeyCallBacks,
425 &kCFTypeDictionaryValueCallBacks);
427 switch (codec_type) {
428 case kCMVideoCodecType_MPEG4Video :
429 data = videotoolbox_esds_extradata_create(avctx);
431 CFDictionarySetValue(avc_info, CFSTR("esds"), data);
433 case kCMVideoCodecType_H264 :
434 data = ff_videotoolbox_avcc_extradata_create(avctx);
436 CFDictionarySetValue(avc_info, CFSTR("avcC"), data);
442 CFDictionarySetValue(config_info,
443 kCMFormatDescriptionExtension_SampleDescriptionExtensionAtoms,
454 static CFDictionaryRef videotoolbox_buffer_attributes_create(int width,
458 CFMutableDictionaryRef buffer_attributes;
459 CFMutableDictionaryRef io_surface_properties;
460 CFNumberRef cv_pix_fmt;
464 w = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &width);
465 h = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &height);
466 cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &pix_fmt);
468 buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
470 &kCFTypeDictionaryKeyCallBacks,
471 &kCFTypeDictionaryValueCallBacks);
472 io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
474 &kCFTypeDictionaryKeyCallBacks,
475 &kCFTypeDictionaryValueCallBacks);
477 CFDictionarySetValue(buffer_attributes, kCVPixelBufferPixelFormatTypeKey, cv_pix_fmt);
478 CFDictionarySetValue(buffer_attributes, kCVPixelBufferIOSurfacePropertiesKey, io_surface_properties);
479 CFDictionarySetValue(buffer_attributes, kCVPixelBufferWidthKey, w);
480 CFDictionarySetValue(buffer_attributes, kCVPixelBufferHeightKey, h);
482 CFRelease(io_surface_properties);
483 CFRelease(cv_pix_fmt);
487 return buffer_attributes;
490 static CMVideoFormatDescriptionRef videotoolbox_format_desc_create(AVCodecContext *avctx,
491 CMVideoCodecType codec_type,
492 CFDictionaryRef decoder_spec,
496 CMFormatDescriptionRef cm_fmt_desc = NULL;
498 H264Context *h = codec_type == kCMVideoCodecType_H264 ? avctx->priv_data : NULL;
500 if (h && h->ps.sps->data_size && h->ps.pps->data_size) {
502 const uint8_t **ps_data = av_malloc(sizeof(uint8_t*) * ps_count);
503 size_t *ps_sizes = av_malloc(sizeof(size_t) * ps_count);
505 ps_data[0] = h->ps.sps->data;
506 ps_sizes[0] = h->ps.sps->data_size;
508 ps_data[1] = h->ps.pps->data;
509 ps_sizes[1] = h->ps.pps->data_size;
511 status = CMVideoFormatDescriptionCreateFromH264ParameterSets(NULL,
521 av_log(avctx, AV_LOG_ERROR, "Error creating H.264 format description: %d\n", status);
525 status = CMVideoFormatDescriptionCreate(kCFAllocatorDefault,
529 decoder_spec, // Dictionary of extension
533 av_log(avctx, AV_LOG_ERROR, "Error creating format description: %d\n", status);
541 static int videotoolbox_default_init(AVCodecContext *avctx)
543 AVVideotoolboxContext *videotoolbox = avctx->hwaccel_context;
545 VTDecompressionOutputCallbackRecord decoder_cb;
546 CFDictionaryRef decoder_spec;
547 CFDictionaryRef buf_attr;
550 av_log(avctx, AV_LOG_ERROR, "hwaccel context is not set\n");
554 switch( avctx->codec_id ) {
555 case AV_CODEC_ID_H263 :
556 videotoolbox->cm_codec_type = kCMVideoCodecType_H263;
558 case AV_CODEC_ID_H264 :
559 videotoolbox->cm_codec_type = kCMVideoCodecType_H264;
561 case AV_CODEC_ID_MPEG1VIDEO :
562 videotoolbox->cm_codec_type = kCMVideoCodecType_MPEG1Video;
564 case AV_CODEC_ID_MPEG2VIDEO :
565 videotoolbox->cm_codec_type = kCMVideoCodecType_MPEG2Video;
567 case AV_CODEC_ID_MPEG4 :
568 videotoolbox->cm_codec_type = kCMVideoCodecType_MPEG4Video;
574 decoder_spec = videotoolbox_decoder_config_create(videotoolbox->cm_codec_type, avctx);
576 videotoolbox->cm_fmt_desc = videotoolbox_format_desc_create(avctx,
577 videotoolbox->cm_codec_type,
581 if (!videotoolbox->cm_fmt_desc) {
583 CFRelease(decoder_spec);
585 av_log(avctx, AV_LOG_ERROR, "format description creation failed\n");
589 buf_attr = videotoolbox_buffer_attributes_create(avctx->width,
591 videotoolbox->cv_pix_fmt_type);
593 decoder_cb.decompressionOutputCallback = videotoolbox_decoder_callback;
594 decoder_cb.decompressionOutputRefCon = avctx;
596 status = VTDecompressionSessionCreate(NULL, // allocator
597 videotoolbox->cm_fmt_desc, // videoFormatDescription
598 decoder_spec, // videoDecoderSpecification
599 buf_attr, // destinationImageBufferAttributes
600 &decoder_cb, // outputCallback
601 &videotoolbox->session); // decompressionSessionOut
604 CFRelease(decoder_spec);
609 case kVTVideoDecoderNotAvailableNowErr:
610 case kVTVideoDecoderUnsupportedDataFormatErr:
611 return AVERROR(ENOSYS);
612 case kVTVideoDecoderMalfunctionErr:
613 return AVERROR(EINVAL);
614 case kVTVideoDecoderBadDataErr :
615 return AVERROR_INVALIDDATA;
619 return AVERROR_UNKNOWN;
623 static void videotoolbox_default_free(AVCodecContext *avctx)
625 AVVideotoolboxContext *videotoolbox = avctx->hwaccel_context;
628 if (videotoolbox->cm_fmt_desc)
629 CFRelease(videotoolbox->cm_fmt_desc);
631 if (videotoolbox->session) {
632 VTDecompressionSessionInvalidate(videotoolbox->session);
633 CFRelease(videotoolbox->session);
638 AVHWAccel ff_h263_videotoolbox_hwaccel = {
639 .name = "h263_videotoolbox",
640 .type = AVMEDIA_TYPE_VIDEO,
641 .id = AV_CODEC_ID_H263,
642 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
643 .alloc_frame = ff_videotoolbox_alloc_frame,
644 .start_frame = videotoolbox_mpeg_start_frame,
645 .decode_slice = videotoolbox_mpeg_decode_slice,
646 .end_frame = videotoolbox_mpeg_end_frame,
647 .uninit = ff_videotoolbox_uninit,
648 .priv_data_size = sizeof(VTContext),
651 AVHWAccel ff_h264_videotoolbox_hwaccel = {
652 .name = "h264_videotoolbox",
653 .type = AVMEDIA_TYPE_VIDEO,
654 .id = AV_CODEC_ID_H264,
655 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
656 .alloc_frame = ff_videotoolbox_alloc_frame,
657 .start_frame = ff_videotoolbox_h264_start_frame,
658 .decode_slice = ff_videotoolbox_h264_decode_slice,
659 .end_frame = videotoolbox_h264_end_frame,
660 .uninit = ff_videotoolbox_uninit,
661 .priv_data_size = sizeof(VTContext),
664 AVHWAccel ff_mpeg1_videotoolbox_hwaccel = {
665 .name = "mpeg1_videotoolbox",
666 .type = AVMEDIA_TYPE_VIDEO,
667 .id = AV_CODEC_ID_MPEG1VIDEO,
668 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
669 .alloc_frame = ff_videotoolbox_alloc_frame,
670 .start_frame = videotoolbox_mpeg_start_frame,
671 .decode_slice = videotoolbox_mpeg_decode_slice,
672 .end_frame = videotoolbox_mpeg_end_frame,
673 .uninit = ff_videotoolbox_uninit,
674 .priv_data_size = sizeof(VTContext),
677 AVHWAccel ff_mpeg2_videotoolbox_hwaccel = {
678 .name = "mpeg2_videotoolbox",
679 .type = AVMEDIA_TYPE_VIDEO,
680 .id = AV_CODEC_ID_MPEG2VIDEO,
681 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
682 .alloc_frame = ff_videotoolbox_alloc_frame,
683 .start_frame = videotoolbox_mpeg_start_frame,
684 .decode_slice = videotoolbox_mpeg_decode_slice,
685 .end_frame = videotoolbox_mpeg_end_frame,
686 .uninit = ff_videotoolbox_uninit,
687 .priv_data_size = sizeof(VTContext),
690 AVHWAccel ff_mpeg4_videotoolbox_hwaccel = {
691 .name = "mpeg4_videotoolbox",
692 .type = AVMEDIA_TYPE_VIDEO,
693 .id = AV_CODEC_ID_MPEG4,
694 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
695 .alloc_frame = ff_videotoolbox_alloc_frame,
696 .start_frame = videotoolbox_mpeg_start_frame,
697 .decode_slice = videotoolbox_mpeg_decode_slice,
698 .end_frame = videotoolbox_mpeg_end_frame,
699 .uninit = ff_videotoolbox_uninit,
700 .priv_data_size = sizeof(VTContext),
703 AVVideotoolboxContext *av_videotoolbox_alloc_context(void)
705 AVVideotoolboxContext *ret = av_mallocz(sizeof(*ret));
708 ret->output_callback = videotoolbox_decoder_callback;
709 ret->cv_pix_fmt_type = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
715 int av_videotoolbox_default_init(AVCodecContext *avctx)
717 return av_videotoolbox_default_init2(avctx, NULL);
720 int av_videotoolbox_default_init2(AVCodecContext *avctx, AVVideotoolboxContext *vtctx)
722 avctx->hwaccel_context = vtctx ?: av_videotoolbox_alloc_context();
723 if (!avctx->hwaccel_context)
724 return AVERROR(ENOMEM);
725 return videotoolbox_default_init(avctx);
728 void av_videotoolbox_default_free(AVCodecContext *avctx)
731 videotoolbox_default_free(avctx);
732 av_freep(&avctx->hwaccel_context);
734 #endif /* CONFIG_VIDEOTOOLBOX */