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"
34 #include <TargetConditionals.h>
36 #ifndef kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder
37 # define kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder CFSTR("RequireHardwareAcceleratedVideoDecoder")
40 #define VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING 12
42 static void videotoolbox_buffer_release(void *opaque, uint8_t *data)
44 CVPixelBufferRef cv_buffer = (CVImageBufferRef)data;
45 CVPixelBufferRelease(cv_buffer);
48 static int videotoolbox_buffer_copy(VTContext *vtctx,
49 const uint8_t *buffer,
54 tmp = av_fast_realloc(vtctx->bitstream,
55 &vtctx->allocated_size,
59 return AVERROR(ENOMEM);
61 vtctx->bitstream = tmp;
62 memcpy(vtctx->bitstream, buffer, size);
63 vtctx->bitstream_size = size;
68 int ff_videotoolbox_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
70 frame->width = avctx->width;
71 frame->height = avctx->height;
72 frame->format = avctx->pix_fmt;
73 frame->buf[0] = av_buffer_alloc(1);
76 return AVERROR(ENOMEM);
81 #define AV_W8(p, v) *(p) = (v)
83 CFDataRef ff_videotoolbox_avcc_extradata_create(AVCodecContext *avctx)
85 H264Context *h = avctx->priv_data;
86 CFDataRef data = NULL;
88 int vt_extradata_size = 6 + 2 + h->ps.sps->data_size + 3 + h->ps.pps->data_size;
89 uint8_t *vt_extradata = av_malloc(vt_extradata_size);
95 AV_W8(p + 0, 1); /* version */
96 AV_W8(p + 1, h->ps.sps->data[1]); /* profile */
97 AV_W8(p + 2, h->ps.sps->data[2]); /* profile compat */
98 AV_W8(p + 3, h->ps.sps->data[3]); /* level */
99 AV_W8(p + 4, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 3 (11) */
100 AV_W8(p + 5, 0xe1); /* 3 bits reserved (111) + 5 bits number of sps (00001) */
101 AV_WB16(p + 6, h->ps.sps->data_size);
102 memcpy(p + 8, h->ps.sps->data, h->ps.sps->data_size);
103 p += 8 + h->ps.sps->data_size;
104 AV_W8(p + 0, 1); /* number of pps */
105 AV_WB16(p + 1, h->ps.pps->data_size);
106 memcpy(p + 3, h->ps.pps->data, h->ps.pps->data_size);
108 p += 3 + h->ps.pps->data_size;
109 av_assert0(p - vt_extradata == vt_extradata_size);
111 data = CFDataCreate(kCFAllocatorDefault, vt_extradata, vt_extradata_size);
112 av_free(vt_extradata);
116 int ff_videotoolbox_buffer_create(VTContext *vtctx, AVFrame *frame)
118 av_buffer_unref(&frame->buf[0]);
120 frame->buf[0] = av_buffer_create((uint8_t*)vtctx->frame,
121 sizeof(vtctx->frame),
122 videotoolbox_buffer_release,
124 AV_BUFFER_FLAG_READONLY);
125 if (!frame->buf[0]) {
126 return AVERROR(ENOMEM);
129 frame->data[3] = (uint8_t*)vtctx->frame;
135 int ff_videotoolbox_h264_start_frame(AVCodecContext *avctx,
136 const uint8_t *buffer,
139 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
140 H264Context *h = avctx->priv_data;
142 vtctx->bitstream_size = 0;
144 if (h->is_avc == 1) {
145 return videotoolbox_buffer_copy(vtctx, buffer, size);
151 int ff_videotoolbox_h264_decode_slice(AVCodecContext *avctx,
152 const uint8_t *buffer,
155 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
156 H264Context *h = avctx->priv_data;
162 tmp = av_fast_realloc(vtctx->bitstream,
163 &vtctx->allocated_size,
164 vtctx->bitstream_size+size+4);
166 return AVERROR(ENOMEM);
168 vtctx->bitstream = tmp;
170 AV_WB32(vtctx->bitstream + vtctx->bitstream_size, size);
171 memcpy(vtctx->bitstream + vtctx->bitstream_size + 4, buffer, size);
173 vtctx->bitstream_size += size + 4;
178 int ff_videotoolbox_uninit(AVCodecContext *avctx)
180 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
182 av_freep(&vtctx->bitstream);
184 CVPixelBufferRelease(vtctx->frame);
190 #if CONFIG_VIDEOTOOLBOX
191 static void videotoolbox_write_mp4_descr_length(PutByteContext *pb, int length)
196 for (i = 3; i >= 0; i--) {
197 b = (length >> (i * 7)) & 0x7F;
201 bytestream2_put_byteu(pb, b);
205 static CFDataRef videotoolbox_esds_extradata_create(AVCodecContext *avctx)
208 uint8_t *rw_extradata;
210 int full_size = 3 + 5 + 13 + 5 + avctx->extradata_size + 3;
211 // ES_DescrTag data + DecoderConfigDescrTag + data + DecSpecificInfoTag + size + SLConfigDescriptor
212 int config_size = 13 + 5 + avctx->extradata_size;
215 if (!(rw_extradata = av_mallocz(full_size + VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING)))
218 bytestream2_init_writer(&pb, rw_extradata, full_size + VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING);
219 bytestream2_put_byteu(&pb, 0); // version
220 bytestream2_put_ne24(&pb, 0); // flags
222 // elementary stream descriptor
223 bytestream2_put_byteu(&pb, 0x03); // ES_DescrTag
224 videotoolbox_write_mp4_descr_length(&pb, full_size);
225 bytestream2_put_ne16(&pb, 0); // esid
226 bytestream2_put_byteu(&pb, 0); // stream priority (0-32)
228 // decoder configuration descriptor
229 bytestream2_put_byteu(&pb, 0x04); // DecoderConfigDescrTag
230 videotoolbox_write_mp4_descr_length(&pb, config_size);
231 bytestream2_put_byteu(&pb, 32); // object type indication. 32 = AV_CODEC_ID_MPEG4
232 bytestream2_put_byteu(&pb, 0x11); // stream type
233 bytestream2_put_ne24(&pb, 0); // buffer size
234 bytestream2_put_ne32(&pb, 0); // max bitrate
235 bytestream2_put_ne32(&pb, 0); // avg bitrate
237 // decoder specific descriptor
238 bytestream2_put_byteu(&pb, 0x05); ///< DecSpecificInfoTag
239 videotoolbox_write_mp4_descr_length(&pb, avctx->extradata_size);
241 bytestream2_put_buffer(&pb, avctx->extradata, avctx->extradata_size);
243 // SLConfigDescriptor
244 bytestream2_put_byteu(&pb, 0x06); // SLConfigDescrTag
245 bytestream2_put_byteu(&pb, 0x01); // length
246 bytestream2_put_byteu(&pb, 0x02); //
248 s = bytestream2_size_p(&pb);
250 data = CFDataCreate(kCFAllocatorDefault, rw_extradata, s);
252 av_freep(&rw_extradata);
256 static CMSampleBufferRef videotoolbox_sample_buffer_create(CMFormatDescriptionRef fmt_desc,
261 CMBlockBufferRef block_buf;
262 CMSampleBufferRef sample_buf;
267 status = CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault,// structureAllocator
268 buffer, // memoryBlock
270 kCFAllocatorNull, // blockAllocator
271 NULL, // customBlockSource
278 status = CMSampleBufferCreate(kCFAllocatorDefault, // allocator
279 block_buf, // dataBuffer
281 0, // makeDataReadyCallback
282 0, // makeDataReadyRefcon
283 fmt_desc, // formatDescription
285 0, // numSampleTimingEntries
286 NULL, // sampleTimingArray
287 0, // numSampleSizeEntries
288 NULL, // sampleSizeArray
293 CFRelease(block_buf);
298 static void videotoolbox_decoder_callback(void *opaque,
299 void *sourceFrameRefCon,
301 VTDecodeInfoFlags flags,
302 CVImageBufferRef image_buffer,
306 AVCodecContext *avctx = opaque;
307 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
310 CVPixelBufferRelease(vtctx->frame);
315 av_log(NULL, AV_LOG_DEBUG, "vt decoder cb: output image buffer is null\n");
319 vtctx->frame = CVPixelBufferRetain(image_buffer);
322 static OSStatus videotoolbox_session_decode_frame(AVCodecContext *avctx)
325 CMSampleBufferRef sample_buf;
326 AVVideotoolboxContext *videotoolbox = avctx->hwaccel_context;
327 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
329 sample_buf = videotoolbox_sample_buffer_create(videotoolbox->cm_fmt_desc,
331 vtctx->bitstream_size);
336 status = VTDecompressionSessionDecodeFrame(videotoolbox->session,
339 NULL, // sourceFrameRefCon
342 status = VTDecompressionSessionWaitForAsynchronousFrames(videotoolbox->session);
344 CFRelease(sample_buf);
349 static int videotoolbox_common_end_frame(AVCodecContext *avctx, AVFrame *frame)
352 AVVideotoolboxContext *videotoolbox = avctx->hwaccel_context;
353 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
355 if (!videotoolbox->session || !vtctx->bitstream)
356 return AVERROR_INVALIDDATA;
358 status = videotoolbox_session_decode_frame(avctx);
361 av_log(avctx, AV_LOG_ERROR, "Failed to decode frame (%d)\n", status);
362 return AVERROR_UNKNOWN;
366 return AVERROR_UNKNOWN;
368 return ff_videotoolbox_buffer_create(vtctx, frame);
371 static int videotoolbox_h264_end_frame(AVCodecContext *avctx)
373 H264Context *h = avctx->priv_data;
374 AVFrame *frame = h->cur_pic_ptr->f;
376 return videotoolbox_common_end_frame(avctx, frame);
379 static int videotoolbox_mpeg_start_frame(AVCodecContext *avctx,
380 const uint8_t *buffer,
383 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
385 return videotoolbox_buffer_copy(vtctx, buffer, size);
388 static int videotoolbox_mpeg_decode_slice(AVCodecContext *avctx,
389 const uint8_t *buffer,
395 static int videotoolbox_mpeg_end_frame(AVCodecContext *avctx)
397 MpegEncContext *s = avctx->priv_data;
398 AVFrame *frame = s->current_picture_ptr->f;
400 return videotoolbox_common_end_frame(avctx, frame);
403 static CFDictionaryRef videotoolbox_decoder_config_create(CMVideoCodecType codec_type,
404 AVCodecContext *avctx)
406 CFMutableDictionaryRef config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
408 &kCFTypeDictionaryKeyCallBacks,
409 &kCFTypeDictionaryValueCallBacks);
411 CFDictionarySetValue(config_info,
412 kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder,
415 if (avctx->extradata_size) {
416 CFMutableDictionaryRef avc_info;
417 CFDataRef data = NULL;
419 avc_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
421 &kCFTypeDictionaryKeyCallBacks,
422 &kCFTypeDictionaryValueCallBacks);
424 switch (codec_type) {
425 case kCMVideoCodecType_MPEG4Video :
426 data = videotoolbox_esds_extradata_create(avctx);
428 CFDictionarySetValue(avc_info, CFSTR("esds"), data);
430 case kCMVideoCodecType_H264 :
431 data = ff_videotoolbox_avcc_extradata_create(avctx);
433 CFDictionarySetValue(avc_info, CFSTR("avcC"), data);
439 CFDictionarySetValue(config_info,
440 kCMFormatDescriptionExtension_SampleDescriptionExtensionAtoms,
451 static CFDictionaryRef videotoolbox_buffer_attributes_create(int width,
455 CFMutableDictionaryRef buffer_attributes;
456 CFMutableDictionaryRef io_surface_properties;
457 CFNumberRef cv_pix_fmt;
461 w = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &width);
462 h = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &height);
463 cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &pix_fmt);
465 buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
467 &kCFTypeDictionaryKeyCallBacks,
468 &kCFTypeDictionaryValueCallBacks);
469 io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
471 &kCFTypeDictionaryKeyCallBacks,
472 &kCFTypeDictionaryValueCallBacks);
475 CFDictionarySetValue(buffer_attributes, kCVPixelBufferPixelFormatTypeKey, cv_pix_fmt);
476 CFDictionarySetValue(buffer_attributes, kCVPixelBufferIOSurfacePropertiesKey, io_surface_properties);
477 CFDictionarySetValue(buffer_attributes, kCVPixelBufferWidthKey, w);
478 CFDictionarySetValue(buffer_attributes, kCVPixelBufferHeightKey, h);
480 CFDictionarySetValue(buffer_attributes, kCVPixelBufferOpenGLESCompatibilityKey, kCFBooleanTrue);
482 CFDictionarySetValue(buffer_attributes, kCVPixelBufferIOSurfaceOpenGLTextureCompatibilityKey, kCFBooleanTrue);
485 CFRelease(io_surface_properties);
486 CFRelease(cv_pix_fmt);
490 return buffer_attributes;
493 static CMVideoFormatDescriptionRef videotoolbox_format_desc_create(CMVideoCodecType codec_type,
494 CFDictionaryRef decoder_spec,
498 CMFormatDescriptionRef cm_fmt_desc;
501 status = CMVideoFormatDescriptionCreate(kCFAllocatorDefault,
505 decoder_spec, // Dictionary of extension
514 static int videotoolbox_default_init(AVCodecContext *avctx)
516 AVVideotoolboxContext *videotoolbox = avctx->hwaccel_context;
518 VTDecompressionOutputCallbackRecord decoder_cb;
519 CFDictionaryRef decoder_spec;
520 CFDictionaryRef buf_attr;
523 av_log(avctx, AV_LOG_ERROR, "hwaccel context is not set\n");
527 switch( avctx->codec_id ) {
528 case AV_CODEC_ID_H263 :
529 videotoolbox->cm_codec_type = kCMVideoCodecType_H263;
531 case AV_CODEC_ID_H264 :
532 videotoolbox->cm_codec_type = kCMVideoCodecType_H264;
534 case AV_CODEC_ID_MPEG1VIDEO :
535 videotoolbox->cm_codec_type = kCMVideoCodecType_MPEG1Video;
537 case AV_CODEC_ID_MPEG2VIDEO :
538 videotoolbox->cm_codec_type = kCMVideoCodecType_MPEG2Video;
540 case AV_CODEC_ID_MPEG4 :
541 videotoolbox->cm_codec_type = kCMVideoCodecType_MPEG4Video;
547 decoder_spec = videotoolbox_decoder_config_create(videotoolbox->cm_codec_type, avctx);
549 videotoolbox->cm_fmt_desc = videotoolbox_format_desc_create(videotoolbox->cm_codec_type,
553 if (!videotoolbox->cm_fmt_desc) {
555 CFRelease(decoder_spec);
557 av_log(avctx, AV_LOG_ERROR, "format description creation failed\n");
561 buf_attr = videotoolbox_buffer_attributes_create(avctx->width,
563 videotoolbox->cv_pix_fmt_type);
565 decoder_cb.decompressionOutputCallback = videotoolbox_decoder_callback;
566 decoder_cb.decompressionOutputRefCon = avctx;
568 status = VTDecompressionSessionCreate(NULL, // allocator
569 videotoolbox->cm_fmt_desc, // videoFormatDescription
570 decoder_spec, // videoDecoderSpecification
571 buf_attr, // destinationImageBufferAttributes
572 &decoder_cb, // outputCallback
573 &videotoolbox->session); // decompressionSessionOut
576 CFRelease(decoder_spec);
581 case kVTVideoDecoderNotAvailableNowErr:
582 case kVTVideoDecoderUnsupportedDataFormatErr:
583 return AVERROR(ENOSYS);
584 case kVTVideoDecoderMalfunctionErr:
585 return AVERROR(EINVAL);
586 case kVTVideoDecoderBadDataErr :
587 return AVERROR_INVALIDDATA;
591 return AVERROR_UNKNOWN;
595 static void videotoolbox_default_free(AVCodecContext *avctx)
597 AVVideotoolboxContext *videotoolbox = avctx->hwaccel_context;
600 if (videotoolbox->cm_fmt_desc)
601 CFRelease(videotoolbox->cm_fmt_desc);
603 if (videotoolbox->session) {
604 VTDecompressionSessionInvalidate(videotoolbox->session);
605 CFRelease(videotoolbox->session);
610 AVHWAccel ff_h263_videotoolbox_hwaccel = {
611 .name = "h263_videotoolbox",
612 .type = AVMEDIA_TYPE_VIDEO,
613 .id = AV_CODEC_ID_H263,
614 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
615 .alloc_frame = ff_videotoolbox_alloc_frame,
616 .start_frame = videotoolbox_mpeg_start_frame,
617 .decode_slice = videotoolbox_mpeg_decode_slice,
618 .end_frame = videotoolbox_mpeg_end_frame,
619 .uninit = ff_videotoolbox_uninit,
620 .priv_data_size = sizeof(VTContext),
623 AVHWAccel ff_h264_videotoolbox_hwaccel = {
624 .name = "h264_videotoolbox",
625 .type = AVMEDIA_TYPE_VIDEO,
626 .id = AV_CODEC_ID_H264,
627 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
628 .alloc_frame = ff_videotoolbox_alloc_frame,
629 .start_frame = ff_videotoolbox_h264_start_frame,
630 .decode_slice = ff_videotoolbox_h264_decode_slice,
631 .end_frame = videotoolbox_h264_end_frame,
632 .uninit = ff_videotoolbox_uninit,
633 .priv_data_size = sizeof(VTContext),
636 AVHWAccel ff_mpeg1_videotoolbox_hwaccel = {
637 .name = "mpeg1_videotoolbox",
638 .type = AVMEDIA_TYPE_VIDEO,
639 .id = AV_CODEC_ID_MPEG1VIDEO,
640 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
641 .alloc_frame = ff_videotoolbox_alloc_frame,
642 .start_frame = videotoolbox_mpeg_start_frame,
643 .decode_slice = videotoolbox_mpeg_decode_slice,
644 .end_frame = videotoolbox_mpeg_end_frame,
645 .uninit = ff_videotoolbox_uninit,
646 .priv_data_size = sizeof(VTContext),
649 AVHWAccel ff_mpeg2_videotoolbox_hwaccel = {
650 .name = "mpeg2_videotoolbox",
651 .type = AVMEDIA_TYPE_VIDEO,
652 .id = AV_CODEC_ID_MPEG2VIDEO,
653 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
654 .alloc_frame = ff_videotoolbox_alloc_frame,
655 .start_frame = videotoolbox_mpeg_start_frame,
656 .decode_slice = videotoolbox_mpeg_decode_slice,
657 .end_frame = videotoolbox_mpeg_end_frame,
658 .uninit = ff_videotoolbox_uninit,
659 .priv_data_size = sizeof(VTContext),
662 AVHWAccel ff_mpeg4_videotoolbox_hwaccel = {
663 .name = "mpeg4_videotoolbox",
664 .type = AVMEDIA_TYPE_VIDEO,
665 .id = AV_CODEC_ID_MPEG4,
666 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
667 .alloc_frame = ff_videotoolbox_alloc_frame,
668 .start_frame = videotoolbox_mpeg_start_frame,
669 .decode_slice = videotoolbox_mpeg_decode_slice,
670 .end_frame = videotoolbox_mpeg_end_frame,
671 .uninit = ff_videotoolbox_uninit,
672 .priv_data_size = sizeof(VTContext),
675 AVVideotoolboxContext *av_videotoolbox_alloc_context(void)
677 AVVideotoolboxContext *ret = av_mallocz(sizeof(*ret));
680 ret->output_callback = videotoolbox_decoder_callback;
681 ret->cv_pix_fmt_type = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
687 int av_videotoolbox_default_init(AVCodecContext *avctx)
689 return av_videotoolbox_default_init2(avctx, NULL);
692 int av_videotoolbox_default_init2(AVCodecContext *avctx, AVVideotoolboxContext *vtctx)
694 avctx->hwaccel_context = vtctx ?: av_videotoolbox_alloc_context();
695 if (!avctx->hwaccel_context)
696 return AVERROR(ENOMEM);
697 return videotoolbox_default_init(avctx);
700 void av_videotoolbox_default_free(AVCodecContext *avctx)
703 videotoolbox_default_free(avctx);
704 av_freep(&avctx->hwaccel_context);
706 #endif /* CONFIG_VIDEOTOOLBOX */