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