]> git.sesse.net Git - ffmpeg/blob - libavcodec/videotoolbox.c
2ebe60fb5c0d0d4c09a0dee78c8070ec74fdeec1
[ffmpeg] / libavcodec / videotoolbox.c
1 /*
2  * Videotoolbox hardware acceleration
3  *
4  * copyright (c) 2012 Sebastien Zwickert
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "config.h"
24 #if CONFIG_VIDEOTOOLBOX
25 #  include "videotoolbox.h"
26 #else
27 #  include "vda.h"
28 #endif
29 #include "vda_vt_internal.h"
30 #include "libavutil/avutil.h"
31 #include "bytestream.h"
32 #include "h264dec.h"
33 #include "mpegvideo.h"
34
35 #ifndef kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder
36 #  define kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder CFSTR("RequireHardwareAcceleratedVideoDecoder")
37 #endif
38
39 #define VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING  12
40
41 static void videotoolbox_buffer_release(void *opaque, uint8_t *data)
42 {
43     CVPixelBufferRef cv_buffer = (CVImageBufferRef)data;
44     CVPixelBufferRelease(cv_buffer);
45 }
46
47 static int videotoolbox_buffer_copy(VTContext *vtctx,
48                                     const uint8_t *buffer,
49                                     uint32_t size)
50 {
51     void *tmp;
52
53     tmp = av_fast_realloc(vtctx->bitstream,
54                          &vtctx->allocated_size,
55                          size);
56
57     if (!tmp)
58         return AVERROR(ENOMEM);
59
60     vtctx->bitstream = tmp;
61     memcpy(vtctx->bitstream, buffer, size);
62     vtctx->bitstream_size = size;
63
64     return 0;
65 }
66
67 int ff_videotoolbox_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
68 {
69     frame->width  = avctx->width;
70     frame->height = avctx->height;
71     frame->format = avctx->pix_fmt;
72     frame->buf[0] = av_buffer_alloc(1);
73
74     if (!frame->buf[0])
75         return AVERROR(ENOMEM);
76
77     return 0;
78 }
79
80 #define AV_W8(p, v) *(p) = (v)
81
82 CFDataRef ff_videotoolbox_avcc_extradata_create(AVCodecContext *avctx)
83 {
84     H264Context *h     = avctx->priv_data;
85     CFDataRef data = NULL;
86     uint8_t *p;
87     int vt_extradata_size = 6 + 2 + h->ps.sps->data_size + 3 + h->ps.pps->data_size;
88     uint8_t *vt_extradata = av_malloc(vt_extradata_size);
89     if (!vt_extradata)
90         return NULL;
91
92     p = vt_extradata;
93
94     AV_W8(p + 0, 1); /* version */
95     AV_W8(p + 1, h->ps.sps->data[1]); /* profile */
96     AV_W8(p + 2, h->ps.sps->data[2]); /* profile compat */
97     AV_W8(p + 3, h->ps.sps->data[3]); /* 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);
101     memcpy(p + 8, h->ps.sps->data, h->ps.sps->data_size);
102     p += 8 + h->ps.sps->data_size;
103     AV_W8(p + 0, 1); /* number of pps */
104     AV_WB16(p + 1, h->ps.pps->data_size);
105     memcpy(p + 3, h->ps.pps->data, h->ps.pps->data_size);
106
107     p += 3 + h->ps.pps->data_size;
108     av_assert0(p - vt_extradata == vt_extradata_size);
109
110     data = CFDataCreate(kCFAllocatorDefault, vt_extradata, vt_extradata_size);
111     av_free(vt_extradata);
112     return data;
113 }
114
115 int ff_videotoolbox_buffer_create(VTContext *vtctx, AVFrame *frame)
116 {
117     av_buffer_unref(&frame->buf[0]);
118
119     frame->buf[0] = av_buffer_create((uint8_t*)vtctx->frame,
120                                      sizeof(vtctx->frame),
121                                      videotoolbox_buffer_release,
122                                      NULL,
123                                      AV_BUFFER_FLAG_READONLY);
124     if (!frame->buf[0]) {
125         return AVERROR(ENOMEM);
126     }
127
128     frame->data[3] = (uint8_t*)vtctx->frame;
129     vtctx->frame = NULL;
130
131     return 0;
132 }
133
134 int ff_videotoolbox_h264_start_frame(AVCodecContext *avctx,
135                                      const uint8_t *buffer,
136                                      uint32_t size)
137 {
138     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
139     H264Context *h  = avctx->priv_data;
140
141     vtctx->bitstream_size = 0;
142
143     if (h->is_avc == 1) {
144         return videotoolbox_buffer_copy(vtctx, buffer, size);
145     }
146
147     return 0;
148 }
149
150 int ff_videotoolbox_h264_decode_slice(AVCodecContext *avctx,
151                                       const uint8_t *buffer,
152                                       uint32_t size)
153 {
154     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
155     H264Context *h  = avctx->priv_data;
156     void *tmp;
157
158     if (h->is_avc == 1)
159         return 0;
160
161     tmp = av_fast_realloc(vtctx->bitstream,
162                           &vtctx->allocated_size,
163                           vtctx->bitstream_size+size+4);
164     if (!tmp)
165         return AVERROR(ENOMEM);
166
167     vtctx->bitstream = tmp;
168
169     AV_WB32(vtctx->bitstream + vtctx->bitstream_size, size);
170     memcpy(vtctx->bitstream + vtctx->bitstream_size + 4, buffer, size);
171
172     vtctx->bitstream_size += size + 4;
173
174     return 0;
175 }
176
177 int ff_videotoolbox_uninit(AVCodecContext *avctx)
178 {
179     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
180     if (vtctx) {
181         av_freep(&vtctx->bitstream);
182         if (vtctx->frame)
183             CVPixelBufferRelease(vtctx->frame);
184     }
185
186     return 0;
187 }
188
189 #if CONFIG_VIDEOTOOLBOX
190 static void videotoolbox_write_mp4_descr_length(PutByteContext *pb, int length)
191 {
192     int i;
193     uint8_t b;
194
195     for (i = 3; i >= 0; i--) {
196         b = (length >> (i * 7)) & 0x7F;
197         if (i != 0)
198             b |= 0x80;
199
200         bytestream2_put_byteu(pb, b);
201     }
202 }
203
204 static CFDataRef videotoolbox_esds_extradata_create(AVCodecContext *avctx)
205 {
206     CFDataRef data;
207     uint8_t *rw_extradata;
208     PutByteContext pb;
209     int full_size = 3 + 5 + 13 + 5 + avctx->extradata_size + 3;
210     // ES_DescrTag data + DecoderConfigDescrTag + data + DecSpecificInfoTag + size + SLConfigDescriptor
211     int config_size = 13 + 5 + avctx->extradata_size;
212     int s;
213
214     if (!(rw_extradata = av_mallocz(full_size + VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING)))
215         return NULL;
216
217     bytestream2_init_writer(&pb, rw_extradata, full_size + VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING);
218     bytestream2_put_byteu(&pb, 0);        // version
219     bytestream2_put_ne24(&pb, 0);         // flags
220
221     // elementary stream descriptor
222     bytestream2_put_byteu(&pb, 0x03);     // ES_DescrTag
223     videotoolbox_write_mp4_descr_length(&pb, full_size);
224     bytestream2_put_ne16(&pb, 0);         // esid
225     bytestream2_put_byteu(&pb, 0);        // stream priority (0-32)
226
227     // decoder configuration descriptor
228     bytestream2_put_byteu(&pb, 0x04);     // DecoderConfigDescrTag
229     videotoolbox_write_mp4_descr_length(&pb, config_size);
230     bytestream2_put_byteu(&pb, 32);       // object type indication. 32 = AV_CODEC_ID_MPEG4
231     bytestream2_put_byteu(&pb, 0x11);     // stream type
232     bytestream2_put_ne24(&pb, 0);         // buffer size
233     bytestream2_put_ne32(&pb, 0);         // max bitrate
234     bytestream2_put_ne32(&pb, 0);         // avg bitrate
235
236     // decoder specific descriptor
237     bytestream2_put_byteu(&pb, 0x05);     ///< DecSpecificInfoTag
238     videotoolbox_write_mp4_descr_length(&pb, avctx->extradata_size);
239
240     bytestream2_put_buffer(&pb, avctx->extradata, avctx->extradata_size);
241
242     // SLConfigDescriptor
243     bytestream2_put_byteu(&pb, 0x06);     // SLConfigDescrTag
244     bytestream2_put_byteu(&pb, 0x01);     // length
245     bytestream2_put_byteu(&pb, 0x02);     //
246
247     s = bytestream2_size_p(&pb);
248
249     data = CFDataCreate(kCFAllocatorDefault, rw_extradata, s);
250
251     av_freep(&rw_extradata);
252     return data;
253 }
254
255 static CMSampleBufferRef videotoolbox_sample_buffer_create(CMFormatDescriptionRef fmt_desc,
256                                                            void *buffer,
257                                                            int size)
258 {
259     OSStatus status;
260     CMBlockBufferRef  block_buf;
261     CMSampleBufferRef sample_buf;
262
263     block_buf  = NULL;
264     sample_buf = NULL;
265
266     status = CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault,// structureAllocator
267                                                 buffer,             // memoryBlock
268                                                 size,               // blockLength
269                                                 kCFAllocatorNull,   // blockAllocator
270                                                 NULL,               // customBlockSource
271                                                 0,                  // offsetToData
272                                                 size,               // dataLength
273                                                 0,                  // flags
274                                                 &block_buf);
275
276     if (!status) {
277         status = CMSampleBufferCreate(kCFAllocatorDefault,  // allocator
278                                       block_buf,            // dataBuffer
279                                       TRUE,                 // dataReady
280                                       0,                    // makeDataReadyCallback
281                                       0,                    // makeDataReadyRefcon
282                                       fmt_desc,             // formatDescription
283                                       1,                    // numSamples
284                                       0,                    // numSampleTimingEntries
285                                       NULL,                 // sampleTimingArray
286                                       0,                    // numSampleSizeEntries
287                                       NULL,                 // sampleSizeArray
288                                       &sample_buf);
289     }
290
291     if (block_buf)
292         CFRelease(block_buf);
293
294     return sample_buf;
295 }
296
297 static void videotoolbox_decoder_callback(void *opaque,
298                                           void *sourceFrameRefCon,
299                                           OSStatus status,
300                                           VTDecodeInfoFlags flags,
301                                           CVImageBufferRef image_buffer,
302                                           CMTime pts,
303                                           CMTime duration)
304 {
305     AVCodecContext *avctx = opaque;
306     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
307
308     if (vtctx->frame) {
309         CVPixelBufferRelease(vtctx->frame);
310         vtctx->frame = NULL;
311     }
312
313     if (!image_buffer) {
314         av_log(NULL, AV_LOG_DEBUG, "vt decoder cb: output image buffer is null\n");
315         return;
316     }
317
318     vtctx->frame = CVPixelBufferRetain(image_buffer);
319 }
320
321 static OSStatus videotoolbox_session_decode_frame(AVCodecContext *avctx)
322 {
323     OSStatus status;
324     CMSampleBufferRef sample_buf;
325     AVVideotoolboxContext *videotoolbox = avctx->hwaccel_context;
326     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
327
328     sample_buf = videotoolbox_sample_buffer_create(videotoolbox->cm_fmt_desc,
329                                                    vtctx->bitstream,
330                                                    vtctx->bitstream_size);
331
332     if (!sample_buf)
333         return -1;
334
335     status = VTDecompressionSessionDecodeFrame(videotoolbox->session,
336                                                sample_buf,
337                                                0,       // decodeFlags
338                                                NULL,    // sourceFrameRefCon
339                                                0);      // infoFlagsOut
340     if (status == noErr)
341         status = VTDecompressionSessionWaitForAsynchronousFrames(videotoolbox->session);
342
343     CFRelease(sample_buf);
344
345     return status;
346 }
347
348 static int videotoolbox_common_end_frame(AVCodecContext *avctx, AVFrame *frame)
349 {
350     int status;
351     AVVideotoolboxContext *videotoolbox = avctx->hwaccel_context;
352     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
353
354     av_buffer_unref(&frame->buf[0]);
355
356     if (!videotoolbox->session || !vtctx->bitstream)
357         return AVERROR_INVALIDDATA;
358
359     status = videotoolbox_session_decode_frame(avctx);
360
361     if (status) {
362         av_log(avctx, AV_LOG_ERROR, "Failed to decode frame (%d)\n", status);
363         return AVERROR_UNKNOWN;
364     }
365
366     if (!vtctx->frame)
367         return AVERROR_UNKNOWN;
368
369     return ff_videotoolbox_buffer_create(vtctx, frame);
370 }
371
372 static int videotoolbox_h264_end_frame(AVCodecContext *avctx)
373 {
374     H264Context *h = avctx->priv_data;
375     AVFrame *frame = h->cur_pic_ptr->f;
376
377     return videotoolbox_common_end_frame(avctx, frame);
378 }
379
380 static int videotoolbox_mpeg_start_frame(AVCodecContext *avctx,
381                                          const uint8_t *buffer,
382                                          uint32_t size)
383 {
384     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
385
386     return videotoolbox_buffer_copy(vtctx, buffer, size);
387 }
388
389 static int videotoolbox_mpeg_decode_slice(AVCodecContext *avctx,
390                                           const uint8_t *buffer,
391                                           uint32_t size)
392 {
393     return 0;
394 }
395
396 static int videotoolbox_mpeg_end_frame(AVCodecContext *avctx)
397 {
398     MpegEncContext *s = avctx->priv_data;
399     AVFrame *frame = s->current_picture_ptr->f;
400
401     return videotoolbox_common_end_frame(avctx, frame);
402 }
403
404 static CFDictionaryRef videotoolbox_decoder_config_create(CMVideoCodecType codec_type,
405                                                           AVCodecContext *avctx)
406 {
407     CFMutableDictionaryRef config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
408                                                                    0,
409                                                                    &kCFTypeDictionaryKeyCallBacks,
410                                                                    &kCFTypeDictionaryValueCallBacks);
411
412     CFDictionarySetValue(config_info,
413                          kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder,
414                          kCFBooleanTrue);
415
416     if (avctx->extradata_size) {
417         CFMutableDictionaryRef avc_info;
418         CFDataRef data = NULL;
419
420         avc_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
421                                              1,
422                                              &kCFTypeDictionaryKeyCallBacks,
423                                              &kCFTypeDictionaryValueCallBacks);
424
425         switch (codec_type) {
426         case kCMVideoCodecType_MPEG4Video :
427             data = videotoolbox_esds_extradata_create(avctx);
428             if (data)
429                 CFDictionarySetValue(avc_info, CFSTR("esds"), data);
430             break;
431         case kCMVideoCodecType_H264 :
432             data = ff_videotoolbox_avcc_extradata_create(avctx);
433             if (data)
434                 CFDictionarySetValue(avc_info, CFSTR("avcC"), data);
435             break;
436         default:
437             break;
438         }
439
440         CFDictionarySetValue(config_info,
441                 kCMFormatDescriptionExtension_SampleDescriptionExtensionAtoms,
442                 avc_info);
443
444         if (data)
445             CFRelease(data);
446
447         CFRelease(avc_info);
448     }
449     return config_info;
450 }
451
452 static CFDictionaryRef videotoolbox_buffer_attributes_create(int width,
453                                                              int height,
454                                                              OSType pix_fmt)
455 {
456     CFMutableDictionaryRef buffer_attributes;
457     CFMutableDictionaryRef io_surface_properties;
458     CFNumberRef cv_pix_fmt;
459     CFNumberRef w;
460     CFNumberRef h;
461
462     w = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &width);
463     h = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &height);
464     cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &pix_fmt);
465
466     buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
467                                                   4,
468                                                   &kCFTypeDictionaryKeyCallBacks,
469                                                   &kCFTypeDictionaryValueCallBacks);
470     io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
471                                                       0,
472                                                       &kCFTypeDictionaryKeyCallBacks,
473                                                       &kCFTypeDictionaryValueCallBacks);
474
475     if (pix_fmt)
476         CFDictionarySetValue(buffer_attributes, kCVPixelBufferPixelFormatTypeKey, cv_pix_fmt);
477     CFDictionarySetValue(buffer_attributes, kCVPixelBufferIOSurfacePropertiesKey, io_surface_properties);
478     CFDictionarySetValue(buffer_attributes, kCVPixelBufferWidthKey, w);
479     CFDictionarySetValue(buffer_attributes, kCVPixelBufferHeightKey, h);
480
481     CFRelease(io_surface_properties);
482     CFRelease(cv_pix_fmt);
483     CFRelease(w);
484     CFRelease(h);
485
486     return buffer_attributes;
487 }
488
489 static CMVideoFormatDescriptionRef videotoolbox_format_desc_create(CMVideoCodecType codec_type,
490                                                                    CFDictionaryRef decoder_spec,
491                                                                    int width,
492                                                                    int height)
493 {
494     CMFormatDescriptionRef cm_fmt_desc;
495     OSStatus status;
496
497     status = CMVideoFormatDescriptionCreate(kCFAllocatorDefault,
498                                             codec_type,
499                                             width,
500                                             height,
501                                             decoder_spec, // Dictionary of extension
502                                             &cm_fmt_desc);
503
504     if (status)
505         return NULL;
506
507     return cm_fmt_desc;
508 }
509
510 static int videotoolbox_default_init(AVCodecContext *avctx)
511 {
512     AVVideotoolboxContext *videotoolbox = avctx->hwaccel_context;
513     OSStatus status;
514     VTDecompressionOutputCallbackRecord decoder_cb;
515     CFDictionaryRef decoder_spec;
516     CFDictionaryRef buf_attr;
517
518     if (!videotoolbox) {
519         av_log(avctx, AV_LOG_ERROR, "hwaccel context is not set\n");
520         return -1;
521     }
522
523     switch( avctx->codec_id ) {
524     case AV_CODEC_ID_H263 :
525         videotoolbox->cm_codec_type = kCMVideoCodecType_H263;
526         break;
527     case AV_CODEC_ID_H264 :
528         videotoolbox->cm_codec_type = kCMVideoCodecType_H264;
529         break;
530     case AV_CODEC_ID_MPEG1VIDEO :
531         videotoolbox->cm_codec_type = kCMVideoCodecType_MPEG1Video;
532         break;
533     case AV_CODEC_ID_MPEG2VIDEO :
534         videotoolbox->cm_codec_type = kCMVideoCodecType_MPEG2Video;
535         break;
536     case AV_CODEC_ID_MPEG4 :
537         videotoolbox->cm_codec_type = kCMVideoCodecType_MPEG4Video;
538         break;
539     default :
540         break;
541     }
542
543     decoder_spec = videotoolbox_decoder_config_create(videotoolbox->cm_codec_type, avctx);
544
545     videotoolbox->cm_fmt_desc = videotoolbox_format_desc_create(videotoolbox->cm_codec_type,
546                                                                 decoder_spec,
547                                                                 avctx->width,
548                                                                 avctx->height);
549     if (!videotoolbox->cm_fmt_desc) {
550         if (decoder_spec)
551             CFRelease(decoder_spec);
552
553         av_log(avctx, AV_LOG_ERROR, "format description creation failed\n");
554         return -1;
555     }
556
557     buf_attr = videotoolbox_buffer_attributes_create(avctx->width,
558                                                      avctx->height,
559                                                      videotoolbox->cv_pix_fmt_type);
560
561     decoder_cb.decompressionOutputCallback = videotoolbox_decoder_callback;
562     decoder_cb.decompressionOutputRefCon   = avctx;
563
564     status = VTDecompressionSessionCreate(NULL,                      // allocator
565                                           videotoolbox->cm_fmt_desc, // videoFormatDescription
566                                           decoder_spec,              // videoDecoderSpecification
567                                           buf_attr,                  // destinationImageBufferAttributes
568                                           &decoder_cb,               // outputCallback
569                                           &videotoolbox->session);   // decompressionSessionOut
570
571     if (decoder_spec)
572         CFRelease(decoder_spec);
573     if (buf_attr)
574         CFRelease(buf_attr);
575
576     switch (status) {
577     case kVTVideoDecoderNotAvailableNowErr:
578     case kVTVideoDecoderUnsupportedDataFormatErr:
579         return AVERROR(ENOSYS);
580     case kVTVideoDecoderMalfunctionErr:
581         return AVERROR(EINVAL);
582     case kVTVideoDecoderBadDataErr :
583         return AVERROR_INVALIDDATA;
584     case 0:
585         return 0;
586     default:
587         return AVERROR_UNKNOWN;
588     }
589 }
590
591 static void videotoolbox_default_free(AVCodecContext *avctx)
592 {
593     AVVideotoolboxContext *videotoolbox = avctx->hwaccel_context;
594
595     if (videotoolbox) {
596         if (videotoolbox->cm_fmt_desc)
597             CFRelease(videotoolbox->cm_fmt_desc);
598
599         if (videotoolbox->session) {
600             VTDecompressionSessionInvalidate(videotoolbox->session);
601             CFRelease(videotoolbox->session);
602         }
603     }
604 }
605
606 AVHWAccel ff_h263_videotoolbox_hwaccel = {
607     .name           = "h263_videotoolbox",
608     .type           = AVMEDIA_TYPE_VIDEO,
609     .id             = AV_CODEC_ID_H263,
610     .pix_fmt        = AV_PIX_FMT_VIDEOTOOLBOX,
611     .alloc_frame    = ff_videotoolbox_alloc_frame,
612     .start_frame    = videotoolbox_mpeg_start_frame,
613     .decode_slice   = videotoolbox_mpeg_decode_slice,
614     .end_frame      = videotoolbox_mpeg_end_frame,
615     .uninit         = ff_videotoolbox_uninit,
616     .priv_data_size = sizeof(VTContext),
617 };
618
619 AVHWAccel ff_h264_videotoolbox_hwaccel = {
620     .name           = "h264_videotoolbox",
621     .type           = AVMEDIA_TYPE_VIDEO,
622     .id             = AV_CODEC_ID_H264,
623     .pix_fmt        = AV_PIX_FMT_VIDEOTOOLBOX,
624     .alloc_frame    = ff_videotoolbox_alloc_frame,
625     .start_frame    = ff_videotoolbox_h264_start_frame,
626     .decode_slice   = ff_videotoolbox_h264_decode_slice,
627     .end_frame      = videotoolbox_h264_end_frame,
628     .uninit         = ff_videotoolbox_uninit,
629     .priv_data_size = sizeof(VTContext),
630 };
631
632 AVHWAccel ff_mpeg1_videotoolbox_hwaccel = {
633     .name           = "mpeg1_videotoolbox",
634     .type           = AVMEDIA_TYPE_VIDEO,
635     .id             = AV_CODEC_ID_MPEG1VIDEO,
636     .pix_fmt        = AV_PIX_FMT_VIDEOTOOLBOX,
637     .alloc_frame    = ff_videotoolbox_alloc_frame,
638     .start_frame    = videotoolbox_mpeg_start_frame,
639     .decode_slice   = videotoolbox_mpeg_decode_slice,
640     .end_frame      = videotoolbox_mpeg_end_frame,
641     .uninit         = ff_videotoolbox_uninit,
642     .priv_data_size = sizeof(VTContext),
643 };
644
645 AVHWAccel ff_mpeg2_videotoolbox_hwaccel = {
646     .name           = "mpeg2_videotoolbox",
647     .type           = AVMEDIA_TYPE_VIDEO,
648     .id             = AV_CODEC_ID_MPEG2VIDEO,
649     .pix_fmt        = AV_PIX_FMT_VIDEOTOOLBOX,
650     .alloc_frame    = ff_videotoolbox_alloc_frame,
651     .start_frame    = videotoolbox_mpeg_start_frame,
652     .decode_slice   = videotoolbox_mpeg_decode_slice,
653     .end_frame      = videotoolbox_mpeg_end_frame,
654     .uninit         = ff_videotoolbox_uninit,
655     .priv_data_size = sizeof(VTContext),
656 };
657
658 AVHWAccel ff_mpeg4_videotoolbox_hwaccel = {
659     .name           = "mpeg4_videotoolbox",
660     .type           = AVMEDIA_TYPE_VIDEO,
661     .id             = AV_CODEC_ID_MPEG4,
662     .pix_fmt        = AV_PIX_FMT_VIDEOTOOLBOX,
663     .alloc_frame    = ff_videotoolbox_alloc_frame,
664     .start_frame    = videotoolbox_mpeg_start_frame,
665     .decode_slice   = videotoolbox_mpeg_decode_slice,
666     .end_frame      = videotoolbox_mpeg_end_frame,
667     .uninit         = ff_videotoolbox_uninit,
668     .priv_data_size = sizeof(VTContext),
669 };
670
671 AVVideotoolboxContext *av_videotoolbox_alloc_context(void)
672 {
673     AVVideotoolboxContext *ret = av_mallocz(sizeof(*ret));
674
675     if (ret) {
676         ret->output_callback = videotoolbox_decoder_callback;
677         ret->cv_pix_fmt_type = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
678     }
679
680     return ret;
681 }
682
683 int av_videotoolbox_default_init(AVCodecContext *avctx)
684 {
685     return av_videotoolbox_default_init2(avctx, NULL);
686 }
687
688 int av_videotoolbox_default_init2(AVCodecContext *avctx, AVVideotoolboxContext *vtctx)
689 {
690     avctx->hwaccel_context = vtctx ?: av_videotoolbox_alloc_context();
691     if (!avctx->hwaccel_context)
692         return AVERROR(ENOMEM);
693     return videotoolbox_default_init(avctx);
694 }
695
696 void av_videotoolbox_default_free(AVCodecContext *avctx)
697 {
698
699     videotoolbox_default_free(avctx);
700     av_freep(&avctx->hwaccel_context);
701 }
702 #endif /* CONFIG_VIDEOTOOLBOX */