2 * Videotoolbox hardware acceleration
4 * copyright (c) 2012 Sebastien Zwickert
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #if CONFIG_VIDEOTOOLBOX
25 # include "videotoolbox.h"
26 # include "libavutil/hwcontext_videotoolbox.h"
30 #include "vda_vt_internal.h"
31 #include "libavutil/avutil.h"
32 #include "libavutil/hwcontext.h"
33 #include "bytestream.h"
35 #include "mpegvideo.h"
36 #include <TargetConditionals.h>
38 #ifndef kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder
39 # define kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder CFSTR("RequireHardwareAcceleratedVideoDecoder")
42 #define VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING 12
44 static void videotoolbox_buffer_release(void *opaque, uint8_t *data)
46 CVPixelBufferRef cv_buffer = (CVImageBufferRef)data;
47 CVPixelBufferRelease(cv_buffer);
50 static int videotoolbox_buffer_copy(VTContext *vtctx,
51 const uint8_t *buffer,
56 tmp = av_fast_realloc(vtctx->bitstream,
57 &vtctx->allocated_size,
61 return AVERROR(ENOMEM);
63 vtctx->bitstream = tmp;
64 memcpy(vtctx->bitstream, buffer, size);
65 vtctx->bitstream_size = size;
70 int ff_videotoolbox_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
72 frame->width = avctx->width;
73 frame->height = avctx->height;
74 frame->format = avctx->pix_fmt;
75 frame->buf[0] = av_buffer_alloc(1);
78 return AVERROR(ENOMEM);
83 #define AV_W8(p, v) *(p) = (v)
85 CFDataRef ff_videotoolbox_avcc_extradata_create(AVCodecContext *avctx)
87 H264Context *h = avctx->priv_data;
88 CFDataRef data = NULL;
90 int vt_extradata_size = 6 + 2 + h->ps.sps->data_size + 3 + h->ps.pps->data_size;
91 uint8_t *vt_extradata = av_malloc(vt_extradata_size);
97 AV_W8(p + 0, 1); /* version */
98 AV_W8(p + 1, h->ps.sps->data[1]); /* profile */
99 AV_W8(p + 2, h->ps.sps->data[2]); /* profile compat */
100 AV_W8(p + 3, h->ps.sps->data[3]); /* level */
101 AV_W8(p + 4, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 3 (11) */
102 AV_W8(p + 5, 0xe1); /* 3 bits reserved (111) + 5 bits number of sps (00001) */
103 AV_WB16(p + 6, h->ps.sps->data_size);
104 memcpy(p + 8, h->ps.sps->data, h->ps.sps->data_size);
105 p += 8 + h->ps.sps->data_size;
106 AV_W8(p + 0, 1); /* number of pps */
107 AV_WB16(p + 1, h->ps.pps->data_size);
108 memcpy(p + 3, h->ps.pps->data, h->ps.pps->data_size);
110 p += 3 + h->ps.pps->data_size;
111 av_assert0(p - vt_extradata == vt_extradata_size);
113 data = CFDataCreate(kCFAllocatorDefault, vt_extradata, vt_extradata_size);
114 av_free(vt_extradata);
118 int ff_videotoolbox_buffer_create(VTContext *vtctx, AVFrame *frame)
120 av_buffer_unref(&frame->buf[0]);
122 frame->buf[0] = av_buffer_create((uint8_t*)vtctx->frame,
123 sizeof(vtctx->frame),
124 videotoolbox_buffer_release,
126 AV_BUFFER_FLAG_READONLY);
127 if (!frame->buf[0]) {
128 return AVERROR(ENOMEM);
131 frame->data[3] = (uint8_t*)vtctx->frame;
137 int ff_videotoolbox_h264_start_frame(AVCodecContext *avctx,
138 const uint8_t *buffer,
141 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
142 H264Context *h = avctx->priv_data;
144 vtctx->bitstream_size = 0;
146 if (h->is_avc == 1) {
147 return videotoolbox_buffer_copy(vtctx, buffer, size);
153 int ff_videotoolbox_h264_decode_slice(AVCodecContext *avctx,
154 const uint8_t *buffer,
157 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
158 H264Context *h = avctx->priv_data;
164 tmp = av_fast_realloc(vtctx->bitstream,
165 &vtctx->allocated_size,
166 vtctx->bitstream_size+size+4);
168 return AVERROR(ENOMEM);
170 vtctx->bitstream = tmp;
172 AV_WB32(vtctx->bitstream + vtctx->bitstream_size, size);
173 memcpy(vtctx->bitstream + vtctx->bitstream_size + 4, buffer, size);
175 vtctx->bitstream_size += size + 4;
180 int ff_videotoolbox_uninit(AVCodecContext *avctx)
182 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
184 av_freep(&vtctx->bitstream);
186 CVPixelBufferRelease(vtctx->frame);
192 #if CONFIG_VIDEOTOOLBOX
193 // Return the AVVideotoolboxContext that matters currently. Where it comes from
194 // depends on the API used.
195 static AVVideotoolboxContext *videotoolbox_get_context(AVCodecContext *avctx)
197 // Somewhat tricky because the user can call av_videotoolbox_default_free()
198 // at any time, even when the codec is closed.
199 if (avctx->internal && avctx->internal->hwaccel_priv_data) {
200 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
202 return vtctx->vt_ctx;
204 return avctx->hwaccel_context;
207 static int videotoolbox_buffer_create(AVCodecContext *avctx, AVFrame *frame)
209 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
210 CVPixelBufferRef pixbuf = (CVPixelBufferRef)vtctx->frame;
211 OSType pixel_format = CVPixelBufferGetPixelFormatType(pixbuf);
212 enum AVPixelFormat sw_format = av_map_videotoolbox_format_to_pixfmt(pixel_format);
213 int width = CVPixelBufferGetWidth(pixbuf);
214 int height = CVPixelBufferGetHeight(pixbuf);
215 AVHWFramesContext *cached_frames;
218 ret = ff_videotoolbox_buffer_create(vtctx, frame);
222 // Old API code path.
223 if (!vtctx->cached_hw_frames_ctx)
226 cached_frames = (AVHWFramesContext*)vtctx->cached_hw_frames_ctx->data;
228 if (cached_frames->sw_format != sw_format ||
229 cached_frames->width != width ||
230 cached_frames->height != height) {
231 AVBufferRef *hw_frames_ctx = av_hwframe_ctx_alloc(cached_frames->device_ref);
232 AVHWFramesContext *hw_frames;
234 return AVERROR(ENOMEM);
236 hw_frames = (AVHWFramesContext*)hw_frames_ctx->data;
237 hw_frames->format = cached_frames->format;
238 hw_frames->sw_format = sw_format;
239 hw_frames->width = width;
240 hw_frames->height = height;
242 ret = av_hwframe_ctx_init(hw_frames_ctx);
244 av_buffer_unref(&hw_frames_ctx);
248 av_buffer_unref(&vtctx->cached_hw_frames_ctx);
249 vtctx->cached_hw_frames_ctx = hw_frames_ctx;
252 av_assert0(!frame->hw_frames_ctx);
253 frame->hw_frames_ctx = av_buffer_ref(vtctx->cached_hw_frames_ctx);
254 if (!frame->hw_frames_ctx)
255 return AVERROR(ENOMEM);
260 static void videotoolbox_write_mp4_descr_length(PutByteContext *pb, int length)
265 for (i = 3; i >= 0; i--) {
266 b = (length >> (i * 7)) & 0x7F;
270 bytestream2_put_byteu(pb, b);
274 static CFDataRef videotoolbox_esds_extradata_create(AVCodecContext *avctx)
277 uint8_t *rw_extradata;
279 int full_size = 3 + 5 + 13 + 5 + avctx->extradata_size + 3;
280 // ES_DescrTag data + DecoderConfigDescrTag + data + DecSpecificInfoTag + size + SLConfigDescriptor
281 int config_size = 13 + 5 + avctx->extradata_size;
284 if (!(rw_extradata = av_mallocz(full_size + VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING)))
287 bytestream2_init_writer(&pb, rw_extradata, full_size + VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING);
288 bytestream2_put_byteu(&pb, 0); // version
289 bytestream2_put_ne24(&pb, 0); // flags
291 // elementary stream descriptor
292 bytestream2_put_byteu(&pb, 0x03); // ES_DescrTag
293 videotoolbox_write_mp4_descr_length(&pb, full_size);
294 bytestream2_put_ne16(&pb, 0); // esid
295 bytestream2_put_byteu(&pb, 0); // stream priority (0-32)
297 // decoder configuration descriptor
298 bytestream2_put_byteu(&pb, 0x04); // DecoderConfigDescrTag
299 videotoolbox_write_mp4_descr_length(&pb, config_size);
300 bytestream2_put_byteu(&pb, 32); // object type indication. 32 = AV_CODEC_ID_MPEG4
301 bytestream2_put_byteu(&pb, 0x11); // stream type
302 bytestream2_put_ne24(&pb, 0); // buffer size
303 bytestream2_put_ne32(&pb, 0); // max bitrate
304 bytestream2_put_ne32(&pb, 0); // avg bitrate
306 // decoder specific descriptor
307 bytestream2_put_byteu(&pb, 0x05); ///< DecSpecificInfoTag
308 videotoolbox_write_mp4_descr_length(&pb, avctx->extradata_size);
310 bytestream2_put_buffer(&pb, avctx->extradata, avctx->extradata_size);
312 // SLConfigDescriptor
313 bytestream2_put_byteu(&pb, 0x06); // SLConfigDescrTag
314 bytestream2_put_byteu(&pb, 0x01); // length
315 bytestream2_put_byteu(&pb, 0x02); //
317 s = bytestream2_size_p(&pb);
319 data = CFDataCreate(kCFAllocatorDefault, rw_extradata, s);
321 av_freep(&rw_extradata);
325 static CMSampleBufferRef videotoolbox_sample_buffer_create(CMFormatDescriptionRef fmt_desc,
330 CMBlockBufferRef block_buf;
331 CMSampleBufferRef sample_buf;
336 status = CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault,// structureAllocator
337 buffer, // memoryBlock
339 kCFAllocatorNull, // blockAllocator
340 NULL, // customBlockSource
347 status = CMSampleBufferCreate(kCFAllocatorDefault, // allocator
348 block_buf, // dataBuffer
350 0, // makeDataReadyCallback
351 0, // makeDataReadyRefcon
352 fmt_desc, // formatDescription
354 0, // numSampleTimingEntries
355 NULL, // sampleTimingArray
356 0, // numSampleSizeEntries
357 NULL, // sampleSizeArray
362 CFRelease(block_buf);
367 static void videotoolbox_decoder_callback(void *opaque,
368 void *sourceFrameRefCon,
370 VTDecodeInfoFlags flags,
371 CVImageBufferRef image_buffer,
375 AVCodecContext *avctx = opaque;
376 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
379 CVPixelBufferRelease(vtctx->frame);
384 av_log(NULL, AV_LOG_DEBUG, "vt decoder cb: output image buffer is null\n");
388 vtctx->frame = CVPixelBufferRetain(image_buffer);
391 static OSStatus videotoolbox_session_decode_frame(AVCodecContext *avctx)
394 CMSampleBufferRef sample_buf;
395 AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
396 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
398 sample_buf = videotoolbox_sample_buffer_create(videotoolbox->cm_fmt_desc,
400 vtctx->bitstream_size);
405 status = VTDecompressionSessionDecodeFrame(videotoolbox->session,
408 NULL, // sourceFrameRefCon
411 status = VTDecompressionSessionWaitForAsynchronousFrames(videotoolbox->session);
413 CFRelease(sample_buf);
418 static int videotoolbox_common_end_frame(AVCodecContext *avctx, AVFrame *frame)
421 AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
422 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
424 if (!videotoolbox->session || !vtctx->bitstream)
425 return AVERROR_INVALIDDATA;
427 status = videotoolbox_session_decode_frame(avctx);
430 av_log(avctx, AV_LOG_ERROR, "Failed to decode frame (%d)\n", status);
431 return AVERROR_UNKNOWN;
435 return AVERROR_UNKNOWN;
437 return videotoolbox_buffer_create(avctx, frame);
440 static int videotoolbox_h264_end_frame(AVCodecContext *avctx)
442 H264Context *h = avctx->priv_data;
443 AVFrame *frame = h->cur_pic_ptr->f;
445 return videotoolbox_common_end_frame(avctx, frame);
448 static int videotoolbox_mpeg_start_frame(AVCodecContext *avctx,
449 const uint8_t *buffer,
452 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
454 return videotoolbox_buffer_copy(vtctx, buffer, size);
457 static int videotoolbox_mpeg_decode_slice(AVCodecContext *avctx,
458 const uint8_t *buffer,
464 static int videotoolbox_mpeg_end_frame(AVCodecContext *avctx)
466 MpegEncContext *s = avctx->priv_data;
467 AVFrame *frame = s->current_picture_ptr->f;
469 return videotoolbox_common_end_frame(avctx, frame);
472 static CFDictionaryRef videotoolbox_decoder_config_create(CMVideoCodecType codec_type,
473 AVCodecContext *avctx)
475 CFMutableDictionaryRef config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
477 &kCFTypeDictionaryKeyCallBacks,
478 &kCFTypeDictionaryValueCallBacks);
480 CFDictionarySetValue(config_info,
481 kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder,
484 if (avctx->extradata_size) {
485 CFMutableDictionaryRef avc_info;
486 CFDataRef data = NULL;
488 avc_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
490 &kCFTypeDictionaryKeyCallBacks,
491 &kCFTypeDictionaryValueCallBacks);
493 switch (codec_type) {
494 case kCMVideoCodecType_MPEG4Video :
495 data = videotoolbox_esds_extradata_create(avctx);
497 CFDictionarySetValue(avc_info, CFSTR("esds"), data);
499 case kCMVideoCodecType_H264 :
500 data = ff_videotoolbox_avcc_extradata_create(avctx);
502 CFDictionarySetValue(avc_info, CFSTR("avcC"), data);
508 CFDictionarySetValue(config_info,
509 kCMFormatDescriptionExtension_SampleDescriptionExtensionAtoms,
520 static CFDictionaryRef videotoolbox_buffer_attributes_create(int width,
524 CFMutableDictionaryRef buffer_attributes;
525 CFMutableDictionaryRef io_surface_properties;
526 CFNumberRef cv_pix_fmt;
530 w = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &width);
531 h = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &height);
532 cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &pix_fmt);
534 buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
536 &kCFTypeDictionaryKeyCallBacks,
537 &kCFTypeDictionaryValueCallBacks);
538 io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
540 &kCFTypeDictionaryKeyCallBacks,
541 &kCFTypeDictionaryValueCallBacks);
544 CFDictionarySetValue(buffer_attributes, kCVPixelBufferPixelFormatTypeKey, cv_pix_fmt);
545 CFDictionarySetValue(buffer_attributes, kCVPixelBufferIOSurfacePropertiesKey, io_surface_properties);
546 CFDictionarySetValue(buffer_attributes, kCVPixelBufferWidthKey, w);
547 CFDictionarySetValue(buffer_attributes, kCVPixelBufferHeightKey, h);
549 CFDictionarySetValue(buffer_attributes, kCVPixelBufferOpenGLESCompatibilityKey, kCFBooleanTrue);
551 CFDictionarySetValue(buffer_attributes, kCVPixelBufferIOSurfaceOpenGLTextureCompatibilityKey, kCFBooleanTrue);
554 CFRelease(io_surface_properties);
555 CFRelease(cv_pix_fmt);
559 return buffer_attributes;
562 static CMVideoFormatDescriptionRef videotoolbox_format_desc_create(CMVideoCodecType codec_type,
563 CFDictionaryRef decoder_spec,
567 CMFormatDescriptionRef cm_fmt_desc;
570 status = CMVideoFormatDescriptionCreate(kCFAllocatorDefault,
574 decoder_spec, // Dictionary of extension
583 static int videotoolbox_default_init(AVCodecContext *avctx)
585 AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
587 VTDecompressionOutputCallbackRecord decoder_cb;
588 CFDictionaryRef decoder_spec;
589 CFDictionaryRef buf_attr;
592 av_log(avctx, AV_LOG_ERROR, "hwaccel context is not set\n");
596 switch( avctx->codec_id ) {
597 case AV_CODEC_ID_H263 :
598 videotoolbox->cm_codec_type = kCMVideoCodecType_H263;
600 case AV_CODEC_ID_H264 :
601 videotoolbox->cm_codec_type = kCMVideoCodecType_H264;
603 case AV_CODEC_ID_MPEG1VIDEO :
604 videotoolbox->cm_codec_type = kCMVideoCodecType_MPEG1Video;
606 case AV_CODEC_ID_MPEG2VIDEO :
607 videotoolbox->cm_codec_type = kCMVideoCodecType_MPEG2Video;
609 case AV_CODEC_ID_MPEG4 :
610 videotoolbox->cm_codec_type = kCMVideoCodecType_MPEG4Video;
616 decoder_spec = videotoolbox_decoder_config_create(videotoolbox->cm_codec_type, avctx);
618 videotoolbox->cm_fmt_desc = videotoolbox_format_desc_create(videotoolbox->cm_codec_type,
622 if (!videotoolbox->cm_fmt_desc) {
624 CFRelease(decoder_spec);
626 av_log(avctx, AV_LOG_ERROR, "format description creation failed\n");
630 buf_attr = videotoolbox_buffer_attributes_create(avctx->width,
632 videotoolbox->cv_pix_fmt_type);
634 decoder_cb.decompressionOutputCallback = videotoolbox_decoder_callback;
635 decoder_cb.decompressionOutputRefCon = avctx;
637 status = VTDecompressionSessionCreate(NULL, // allocator
638 videotoolbox->cm_fmt_desc, // videoFormatDescription
639 decoder_spec, // videoDecoderSpecification
640 buf_attr, // destinationImageBufferAttributes
641 &decoder_cb, // outputCallback
642 &videotoolbox->session); // decompressionSessionOut
645 CFRelease(decoder_spec);
650 case kVTVideoDecoderNotAvailableNowErr:
651 case kVTVideoDecoderUnsupportedDataFormatErr:
652 return AVERROR(ENOSYS);
653 case kVTVideoDecoderMalfunctionErr:
654 return AVERROR(EINVAL);
655 case kVTVideoDecoderBadDataErr :
656 return AVERROR_INVALIDDATA;
660 return AVERROR_UNKNOWN;
664 static void videotoolbox_default_free(AVCodecContext *avctx)
666 AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
669 if (videotoolbox->cm_fmt_desc)
670 CFRelease(videotoolbox->cm_fmt_desc);
672 if (videotoolbox->session) {
673 VTDecompressionSessionInvalidate(videotoolbox->session);
674 CFRelease(videotoolbox->session);
679 static int videotoolbox_uninit(AVCodecContext *avctx)
681 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
685 ff_videotoolbox_uninit(avctx);
688 videotoolbox_default_free(avctx);
690 av_buffer_unref(&vtctx->cached_hw_frames_ctx);
691 av_freep(&vtctx->vt_ctx);
696 static int videotoolbox_common_init(AVCodecContext *avctx)
698 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
699 AVHWFramesContext *hw_frames;
702 // Old API - do nothing.
703 if (avctx->hwaccel_context)
706 if (!avctx->hw_frames_ctx && !avctx->hw_device_ctx) {
707 av_log(avctx, AV_LOG_ERROR,
708 "Either hw_frames_ctx or hw_device_ctx must be set.\n");
709 return AVERROR(EINVAL);
712 vtctx->vt_ctx = av_videotoolbox_alloc_context();
713 if (!vtctx->vt_ctx) {
714 err = AVERROR(ENOMEM);
718 if (avctx->hw_frames_ctx) {
719 hw_frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
721 avctx->hw_frames_ctx = av_hwframe_ctx_alloc(avctx->hw_device_ctx);
722 if (!avctx->hw_frames_ctx) {
723 err = AVERROR(ENOMEM);
727 hw_frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
728 hw_frames->format = AV_PIX_FMT_VIDEOTOOLBOX;
729 hw_frames->sw_format = AV_PIX_FMT_NV12; // same as av_videotoolbox_alloc_context()
730 hw_frames->width = avctx->width;
731 hw_frames->height = avctx->height;
733 err = av_hwframe_ctx_init(avctx->hw_frames_ctx);
735 av_buffer_unref(&avctx->hw_frames_ctx);
740 vtctx->cached_hw_frames_ctx = av_buffer_ref(avctx->hw_frames_ctx);
741 if (!vtctx->cached_hw_frames_ctx) {
742 err = AVERROR(ENOMEM);
746 vtctx->vt_ctx->cv_pix_fmt_type =
747 av_map_videotoolbox_format_from_pixfmt(hw_frames->sw_format);
748 if (!vtctx->vt_ctx->cv_pix_fmt_type) {
749 av_log(avctx, AV_LOG_ERROR, "Unknown sw_format.\n");
750 err = AVERROR(EINVAL);
754 err = videotoolbox_default_init(avctx);
761 videotoolbox_uninit(avctx);
765 AVHWAccel ff_h263_videotoolbox_hwaccel = {
766 .name = "h263_videotoolbox",
767 .type = AVMEDIA_TYPE_VIDEO,
768 .id = AV_CODEC_ID_H263,
769 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
770 .alloc_frame = ff_videotoolbox_alloc_frame,
771 .start_frame = videotoolbox_mpeg_start_frame,
772 .decode_slice = videotoolbox_mpeg_decode_slice,
773 .end_frame = videotoolbox_mpeg_end_frame,
774 .init = videotoolbox_common_init,
775 .uninit = videotoolbox_uninit,
776 .priv_data_size = sizeof(VTContext),
779 AVHWAccel ff_h264_videotoolbox_hwaccel = {
780 .name = "h264_videotoolbox",
781 .type = AVMEDIA_TYPE_VIDEO,
782 .id = AV_CODEC_ID_H264,
783 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
784 .alloc_frame = ff_videotoolbox_alloc_frame,
785 .start_frame = ff_videotoolbox_h264_start_frame,
786 .decode_slice = ff_videotoolbox_h264_decode_slice,
787 .end_frame = videotoolbox_h264_end_frame,
788 .init = videotoolbox_common_init,
789 .uninit = videotoolbox_uninit,
790 .priv_data_size = sizeof(VTContext),
793 AVHWAccel ff_mpeg1_videotoolbox_hwaccel = {
794 .name = "mpeg1_videotoolbox",
795 .type = AVMEDIA_TYPE_VIDEO,
796 .id = AV_CODEC_ID_MPEG1VIDEO,
797 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
798 .alloc_frame = ff_videotoolbox_alloc_frame,
799 .start_frame = videotoolbox_mpeg_start_frame,
800 .decode_slice = videotoolbox_mpeg_decode_slice,
801 .end_frame = videotoolbox_mpeg_end_frame,
802 .init = videotoolbox_common_init,
803 .uninit = videotoolbox_uninit,
804 .priv_data_size = sizeof(VTContext),
807 AVHWAccel ff_mpeg2_videotoolbox_hwaccel = {
808 .name = "mpeg2_videotoolbox",
809 .type = AVMEDIA_TYPE_VIDEO,
810 .id = AV_CODEC_ID_MPEG2VIDEO,
811 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
812 .alloc_frame = ff_videotoolbox_alloc_frame,
813 .start_frame = videotoolbox_mpeg_start_frame,
814 .decode_slice = videotoolbox_mpeg_decode_slice,
815 .end_frame = videotoolbox_mpeg_end_frame,
816 .init = videotoolbox_common_init,
817 .uninit = videotoolbox_uninit,
818 .priv_data_size = sizeof(VTContext),
821 AVHWAccel ff_mpeg4_videotoolbox_hwaccel = {
822 .name = "mpeg4_videotoolbox",
823 .type = AVMEDIA_TYPE_VIDEO,
824 .id = AV_CODEC_ID_MPEG4,
825 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
826 .alloc_frame = ff_videotoolbox_alloc_frame,
827 .start_frame = videotoolbox_mpeg_start_frame,
828 .decode_slice = videotoolbox_mpeg_decode_slice,
829 .end_frame = videotoolbox_mpeg_end_frame,
830 .init = videotoolbox_common_init,
831 .uninit = videotoolbox_uninit,
832 .priv_data_size = sizeof(VTContext),
835 AVVideotoolboxContext *av_videotoolbox_alloc_context(void)
837 AVVideotoolboxContext *ret = av_mallocz(sizeof(*ret));
840 ret->output_callback = videotoolbox_decoder_callback;
841 ret->cv_pix_fmt_type = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
847 int av_videotoolbox_default_init(AVCodecContext *avctx)
849 return av_videotoolbox_default_init2(avctx, NULL);
852 int av_videotoolbox_default_init2(AVCodecContext *avctx, AVVideotoolboxContext *vtctx)
854 avctx->hwaccel_context = vtctx ?: av_videotoolbox_alloc_context();
855 if (!avctx->hwaccel_context)
856 return AVERROR(ENOMEM);
857 return videotoolbox_default_init(avctx);
860 void av_videotoolbox_default_free(AVCodecContext *avctx)
863 videotoolbox_default_free(avctx);
864 av_freep(&avctx->hwaccel_context);
866 #endif /* CONFIG_VIDEOTOOLBOX */