avcodec/aacdec_fixed: fix invalid shift in predict()
[ffmpeg.git] / 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 #  include "libavutil/hwcontext_videotoolbox.h"
27 #else
28 #  include "vda.h"
29 #endif
30 #include "vda_vt_internal.h"
31 #include "libavutil/avutil.h"
32 #include "libavutil/hwcontext.h"
33 #include "bytestream.h"
34 #include "h264dec.h"
35 #include "mpegvideo.h"
36 #include <TargetConditionals.h>
37
38 #ifndef kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder
39 #  define kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder CFSTR("RequireHardwareAcceleratedVideoDecoder")
40 #endif
41
42 #define VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING  12
43
44 static void videotoolbox_buffer_release(void *opaque, uint8_t *data)
45 {
46     CVPixelBufferRef cv_buffer = (CVImageBufferRef)data;
47     CVPixelBufferRelease(cv_buffer);
48 }
49
50 static int videotoolbox_buffer_copy(VTContext *vtctx,
51                                     const uint8_t *buffer,
52                                     uint32_t size)
53 {
54     void *tmp;
55
56     tmp = av_fast_realloc(vtctx->bitstream,
57                          &vtctx->allocated_size,
58                          size);
59
60     if (!tmp)
61         return AVERROR(ENOMEM);
62
63     vtctx->bitstream = tmp;
64     memcpy(vtctx->bitstream, buffer, size);
65     vtctx->bitstream_size = size;
66
67     return 0;
68 }
69
70 int ff_videotoolbox_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
71 {
72     frame->width  = avctx->width;
73     frame->height = avctx->height;
74     frame->format = avctx->pix_fmt;
75     frame->buf[0] = av_buffer_alloc(1);
76
77     if (!frame->buf[0])
78         return AVERROR(ENOMEM);
79
80     return 0;
81 }
82
83 #define AV_W8(p, v) *(p) = (v)
84
85 CFDataRef ff_videotoolbox_avcc_extradata_create(AVCodecContext *avctx)
86 {
87     H264Context *h     = avctx->priv_data;
88     CFDataRef data = NULL;
89     uint8_t *p;
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);
92     if (!vt_extradata)
93         return NULL;
94
95     p = vt_extradata;
96
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);
109
110     p += 3 + h->ps.pps->data_size;
111     av_assert0(p - vt_extradata == vt_extradata_size);
112
113     data = CFDataCreate(kCFAllocatorDefault, vt_extradata, vt_extradata_size);
114     av_free(vt_extradata);
115     return data;
116 }
117
118 int ff_videotoolbox_buffer_create(VTContext *vtctx, AVFrame *frame)
119 {
120     av_buffer_unref(&frame->buf[0]);
121
122     frame->buf[0] = av_buffer_create((uint8_t*)vtctx->frame,
123                                      sizeof(vtctx->frame),
124                                      videotoolbox_buffer_release,
125                                      NULL,
126                                      AV_BUFFER_FLAG_READONLY);
127     if (!frame->buf[0]) {
128         return AVERROR(ENOMEM);
129     }
130
131     frame->data[3] = (uint8_t*)vtctx->frame;
132     vtctx->frame = NULL;
133
134     return 0;
135 }
136
137 int ff_videotoolbox_h264_start_frame(AVCodecContext *avctx,
138                                      const uint8_t *buffer,
139                                      uint32_t size)
140 {
141     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
142     H264Context *h  = avctx->priv_data;
143
144     vtctx->bitstream_size = 0;
145
146     if (h->is_avc == 1) {
147         return videotoolbox_buffer_copy(vtctx, buffer, size);
148     }
149
150     return 0;
151 }
152
153 int ff_videotoolbox_h264_decode_slice(AVCodecContext *avctx,
154                                       const uint8_t *buffer,
155                                       uint32_t size)
156 {
157     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
158     H264Context *h  = avctx->priv_data;
159     void *tmp;
160
161     if (h->is_avc == 1)
162         return 0;
163
164     tmp = av_fast_realloc(vtctx->bitstream,
165                           &vtctx->allocated_size,
166                           vtctx->bitstream_size+size+4);
167     if (!tmp)
168         return AVERROR(ENOMEM);
169
170     vtctx->bitstream = tmp;
171
172     AV_WB32(vtctx->bitstream + vtctx->bitstream_size, size);
173     memcpy(vtctx->bitstream + vtctx->bitstream_size + 4, buffer, size);
174
175     vtctx->bitstream_size += size + 4;
176
177     return 0;
178 }
179
180 int ff_videotoolbox_uninit(AVCodecContext *avctx)
181 {
182     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
183     if (vtctx) {
184         av_freep(&vtctx->bitstream);
185         if (vtctx->frame)
186             CVPixelBufferRelease(vtctx->frame);
187     }
188
189     return 0;
190 }
191
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)
196 {
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;
201         if (vtctx->vt_ctx)
202             return vtctx->vt_ctx;
203     }
204     return avctx->hwaccel_context;
205 }
206
207 static int videotoolbox_buffer_create(AVCodecContext *avctx, AVFrame *frame)
208 {
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;
216     int ret;
217
218     ret = ff_videotoolbox_buffer_create(vtctx, frame);
219     if (ret < 0)
220         return ret;
221
222     // Old API code path.
223     if (!vtctx->cached_hw_frames_ctx)
224         return 0;
225
226     cached_frames = (AVHWFramesContext*)vtctx->cached_hw_frames_ctx->data;
227
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;
233         if (!hw_frames_ctx)
234             return AVERROR(ENOMEM);
235
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;
241
242         ret = av_hwframe_ctx_init(hw_frames_ctx);
243         if (ret < 0) {
244             av_buffer_unref(&hw_frames_ctx);
245             return ret;
246         }
247
248         av_buffer_unref(&vtctx->cached_hw_frames_ctx);
249         vtctx->cached_hw_frames_ctx = hw_frames_ctx;
250     }
251
252     av_buffer_unref(&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);
256
257     return 0;
258 }
259
260 static void videotoolbox_write_mp4_descr_length(PutByteContext *pb, int length)
261 {
262     int i;
263     uint8_t b;
264
265     for (i = 3; i >= 0; i--) {
266         b = (length >> (i * 7)) & 0x7F;
267         if (i != 0)
268             b |= 0x80;
269
270         bytestream2_put_byteu(pb, b);
271     }
272 }
273
274 static CFDataRef videotoolbox_esds_extradata_create(AVCodecContext *avctx)
275 {
276     CFDataRef data;
277     uint8_t *rw_extradata;
278     PutByteContext pb;
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;
282     int s;
283
284     if (!(rw_extradata = av_mallocz(full_size + VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING)))
285         return NULL;
286
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
290
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)
296
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
305
306     // decoder specific descriptor
307     bytestream2_put_byteu(&pb, 0x05);     ///< DecSpecificInfoTag
308     videotoolbox_write_mp4_descr_length(&pb, avctx->extradata_size);
309
310     bytestream2_put_buffer(&pb, avctx->extradata, avctx->extradata_size);
311
312     // SLConfigDescriptor
313     bytestream2_put_byteu(&pb, 0x06);     // SLConfigDescrTag
314     bytestream2_put_byteu(&pb, 0x01);     // length
315     bytestream2_put_byteu(&pb, 0x02);     //
316
317     s = bytestream2_size_p(&pb);
318
319     data = CFDataCreate(kCFAllocatorDefault, rw_extradata, s);
320
321     av_freep(&rw_extradata);
322     return data;
323 }
324
325 static CMSampleBufferRef videotoolbox_sample_buffer_create(CMFormatDescriptionRef fmt_desc,
326                                                            void *buffer,
327                                                            int size)
328 {
329     OSStatus status;
330     CMBlockBufferRef  block_buf;
331     CMSampleBufferRef sample_buf;
332
333     block_buf  = NULL;
334     sample_buf = NULL;
335
336     status = CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault,// structureAllocator
337                                                 buffer,             // memoryBlock
338                                                 size,               // blockLength
339                                                 kCFAllocatorNull,   // blockAllocator
340                                                 NULL,               // customBlockSource
341                                                 0,                  // offsetToData
342                                                 size,               // dataLength
343                                                 0,                  // flags
344                                                 &block_buf);
345
346     if (!status) {
347         status = CMSampleBufferCreate(kCFAllocatorDefault,  // allocator
348                                       block_buf,            // dataBuffer
349                                       TRUE,                 // dataReady
350                                       0,                    // makeDataReadyCallback
351                                       0,                    // makeDataReadyRefcon
352                                       fmt_desc,             // formatDescription
353                                       1,                    // numSamples
354                                       0,                    // numSampleTimingEntries
355                                       NULL,                 // sampleTimingArray
356                                       0,                    // numSampleSizeEntries
357                                       NULL,                 // sampleSizeArray
358                                       &sample_buf);
359     }
360
361     if (block_buf)
362         CFRelease(block_buf);
363
364     return sample_buf;
365 }
366
367 static void videotoolbox_decoder_callback(void *opaque,
368                                           void *sourceFrameRefCon,
369                                           OSStatus status,
370                                           VTDecodeInfoFlags flags,
371                                           CVImageBufferRef image_buffer,
372                                           CMTime pts,
373                                           CMTime duration)
374 {
375     AVCodecContext *avctx = opaque;
376     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
377
378     if (vtctx->frame) {
379         CVPixelBufferRelease(vtctx->frame);
380         vtctx->frame = NULL;
381     }
382
383     if (!image_buffer) {
384         av_log(NULL, AV_LOG_DEBUG, "vt decoder cb: output image buffer is null\n");
385         return;
386     }
387
388     vtctx->frame = CVPixelBufferRetain(image_buffer);
389 }
390
391 static OSStatus videotoolbox_session_decode_frame(AVCodecContext *avctx)
392 {
393     OSStatus status;
394     CMSampleBufferRef sample_buf;
395     AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
396     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
397
398     sample_buf = videotoolbox_sample_buffer_create(videotoolbox->cm_fmt_desc,
399                                                    vtctx->bitstream,
400                                                    vtctx->bitstream_size);
401
402     if (!sample_buf)
403         return -1;
404
405     status = VTDecompressionSessionDecodeFrame(videotoolbox->session,
406                                                sample_buf,
407                                                0,       // decodeFlags
408                                                NULL,    // sourceFrameRefCon
409                                                0);      // infoFlagsOut
410     if (status == noErr)
411         status = VTDecompressionSessionWaitForAsynchronousFrames(videotoolbox->session);
412
413     CFRelease(sample_buf);
414
415     return status;
416 }
417
418 static int videotoolbox_common_end_frame(AVCodecContext *avctx, AVFrame *frame)
419 {
420     int status;
421     AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
422     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
423
424     if (!videotoolbox->session || !vtctx->bitstream)
425         return AVERROR_INVALIDDATA;
426
427     status = videotoolbox_session_decode_frame(avctx);
428
429     if (status) {
430         av_log(avctx, AV_LOG_ERROR, "Failed to decode frame (%d)\n", status);
431         return AVERROR_UNKNOWN;
432     }
433
434     if (!vtctx->frame)
435         return AVERROR_UNKNOWN;
436
437     return videotoolbox_buffer_create(avctx, frame);
438 }
439
440 static int videotoolbox_h264_end_frame(AVCodecContext *avctx)
441 {
442     H264Context *h = avctx->priv_data;
443     AVFrame *frame = h->cur_pic_ptr->f;
444
445     return videotoolbox_common_end_frame(avctx, frame);
446 }
447
448 static int videotoolbox_mpeg_start_frame(AVCodecContext *avctx,
449                                          const uint8_t *buffer,
450                                          uint32_t size)
451 {
452     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
453
454     return videotoolbox_buffer_copy(vtctx, buffer, size);
455 }
456
457 static int videotoolbox_mpeg_decode_slice(AVCodecContext *avctx,
458                                           const uint8_t *buffer,
459                                           uint32_t size)
460 {
461     return 0;
462 }
463
464 static int videotoolbox_mpeg_end_frame(AVCodecContext *avctx)
465 {
466     MpegEncContext *s = avctx->priv_data;
467     AVFrame *frame = s->current_picture_ptr->f;
468
469     return videotoolbox_common_end_frame(avctx, frame);
470 }
471
472 static CFDictionaryRef videotoolbox_decoder_config_create(CMVideoCodecType codec_type,
473                                                           AVCodecContext *avctx)
474 {
475     CFMutableDictionaryRef config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
476                                                                    0,
477                                                                    &kCFTypeDictionaryKeyCallBacks,
478                                                                    &kCFTypeDictionaryValueCallBacks);
479
480     CFDictionarySetValue(config_info,
481                          kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder,
482                          kCFBooleanTrue);
483
484     if (avctx->extradata_size) {
485         CFMutableDictionaryRef avc_info;
486         CFDataRef data = NULL;
487
488         avc_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
489                                              1,
490                                              &kCFTypeDictionaryKeyCallBacks,
491                                              &kCFTypeDictionaryValueCallBacks);
492
493         switch (codec_type) {
494         case kCMVideoCodecType_MPEG4Video :
495             data = videotoolbox_esds_extradata_create(avctx);
496             if (data)
497                 CFDictionarySetValue(avc_info, CFSTR("esds"), data);
498             break;
499         case kCMVideoCodecType_H264 :
500             data = ff_videotoolbox_avcc_extradata_create(avctx);
501             if (data)
502                 CFDictionarySetValue(avc_info, CFSTR("avcC"), data);
503             break;
504         default:
505             break;
506         }
507
508         CFDictionarySetValue(config_info,
509                 kCMFormatDescriptionExtension_SampleDescriptionExtensionAtoms,
510                 avc_info);
511
512         if (data)
513             CFRelease(data);
514
515         CFRelease(avc_info);
516     }
517     return config_info;
518 }
519
520 static CFDictionaryRef videotoolbox_buffer_attributes_create(int width,
521                                                              int height,
522                                                              OSType pix_fmt)
523 {
524     CFMutableDictionaryRef buffer_attributes;
525     CFMutableDictionaryRef io_surface_properties;
526     CFNumberRef cv_pix_fmt;
527     CFNumberRef w;
528     CFNumberRef h;
529
530     w = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &width);
531     h = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &height);
532     cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &pix_fmt);
533
534     buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
535                                                   4,
536                                                   &kCFTypeDictionaryKeyCallBacks,
537                                                   &kCFTypeDictionaryValueCallBacks);
538     io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
539                                                       0,
540                                                       &kCFTypeDictionaryKeyCallBacks,
541                                                       &kCFTypeDictionaryValueCallBacks);
542
543     if (pix_fmt)
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);
548 #if TARGET_OS_IPHONE
549     CFDictionarySetValue(buffer_attributes, kCVPixelBufferOpenGLESCompatibilityKey, kCFBooleanTrue);
550 #else
551     CFDictionarySetValue(buffer_attributes, kCVPixelBufferIOSurfaceOpenGLTextureCompatibilityKey, kCFBooleanTrue);
552 #endif
553
554     CFRelease(io_surface_properties);
555     CFRelease(cv_pix_fmt);
556     CFRelease(w);
557     CFRelease(h);
558
559     return buffer_attributes;
560 }
561
562 static CMVideoFormatDescriptionRef videotoolbox_format_desc_create(CMVideoCodecType codec_type,
563                                                                    CFDictionaryRef decoder_spec,
564                                                                    int width,
565                                                                    int height)
566 {
567     CMFormatDescriptionRef cm_fmt_desc;
568     OSStatus status;
569
570     status = CMVideoFormatDescriptionCreate(kCFAllocatorDefault,
571                                             codec_type,
572                                             width,
573                                             height,
574                                             decoder_spec, // Dictionary of extension
575                                             &cm_fmt_desc);
576
577     if (status)
578         return NULL;
579
580     return cm_fmt_desc;
581 }
582
583 static int videotoolbox_default_init(AVCodecContext *avctx)
584 {
585     AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
586     OSStatus status;
587     VTDecompressionOutputCallbackRecord decoder_cb;
588     CFDictionaryRef decoder_spec;
589     CFDictionaryRef buf_attr;
590
591     if (!videotoolbox) {
592         av_log(avctx, AV_LOG_ERROR, "hwaccel context is not set\n");
593         return -1;
594     }
595
596     switch( avctx->codec_id ) {
597     case AV_CODEC_ID_H263 :
598         videotoolbox->cm_codec_type = kCMVideoCodecType_H263;
599         break;
600     case AV_CODEC_ID_H264 :
601         videotoolbox->cm_codec_type = kCMVideoCodecType_H264;
602         break;
603     case AV_CODEC_ID_MPEG1VIDEO :
604         videotoolbox->cm_codec_type = kCMVideoCodecType_MPEG1Video;
605         break;
606     case AV_CODEC_ID_MPEG2VIDEO :
607         videotoolbox->cm_codec_type = kCMVideoCodecType_MPEG2Video;
608         break;
609     case AV_CODEC_ID_MPEG4 :
610         videotoolbox->cm_codec_type = kCMVideoCodecType_MPEG4Video;
611         break;
612     default :
613         break;
614     }
615
616     decoder_spec = videotoolbox_decoder_config_create(videotoolbox->cm_codec_type, avctx);
617
618     videotoolbox->cm_fmt_desc = videotoolbox_format_desc_create(videotoolbox->cm_codec_type,
619                                                                 decoder_spec,
620                                                                 avctx->width,
621                                                                 avctx->height);
622     if (!videotoolbox->cm_fmt_desc) {
623         if (decoder_spec)
624             CFRelease(decoder_spec);
625
626         av_log(avctx, AV_LOG_ERROR, "format description creation failed\n");
627         return -1;
628     }
629
630     buf_attr = videotoolbox_buffer_attributes_create(avctx->width,
631                                                      avctx->height,
632                                                      videotoolbox->cv_pix_fmt_type);
633
634     decoder_cb.decompressionOutputCallback = videotoolbox_decoder_callback;
635     decoder_cb.decompressionOutputRefCon   = avctx;
636
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
643
644     if (decoder_spec)
645         CFRelease(decoder_spec);
646     if (buf_attr)
647         CFRelease(buf_attr);
648
649     switch (status) {
650     case kVTVideoDecoderNotAvailableNowErr:
651         av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox session not available.\n");
652         return AVERROR(ENOSYS);
653     case kVTVideoDecoderUnsupportedDataFormatErr:
654         av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox does not support this format.\n");
655         return AVERROR(ENOSYS);
656     case kVTVideoDecoderMalfunctionErr:
657         av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox malfunction.\n");
658         return AVERROR(EINVAL);
659     case kVTVideoDecoderBadDataErr :
660         av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox reported invalid data.\n");
661         return AVERROR_INVALIDDATA;
662     case 0:
663         return 0;
664     default:
665         av_log(avctx, AV_LOG_VERBOSE, "Unknown VideoToolbox session creation error %u\n", (unsigned)status);
666         return AVERROR_UNKNOWN;
667     }
668 }
669
670 static void videotoolbox_default_free(AVCodecContext *avctx)
671 {
672     AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
673
674     if (videotoolbox) {
675         if (videotoolbox->cm_fmt_desc)
676             CFRelease(videotoolbox->cm_fmt_desc);
677
678         if (videotoolbox->session) {
679             VTDecompressionSessionInvalidate(videotoolbox->session);
680             CFRelease(videotoolbox->session);
681         }
682     }
683 }
684
685 static int videotoolbox_uninit(AVCodecContext *avctx)
686 {
687     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
688     if (!vtctx)
689         return 0;
690
691     ff_videotoolbox_uninit(avctx);
692
693     if (vtctx->vt_ctx)
694         videotoolbox_default_free(avctx);
695
696     av_buffer_unref(&vtctx->cached_hw_frames_ctx);
697     av_freep(&vtctx->vt_ctx);
698
699     return 0;
700 }
701
702 static int videotoolbox_common_init(AVCodecContext *avctx)
703 {
704     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
705     AVHWFramesContext *hw_frames;
706     int err;
707
708     // Old API - do nothing.
709     if (avctx->hwaccel_context)
710         return 0;
711
712     if (!avctx->hw_frames_ctx && !avctx->hw_device_ctx) {
713         av_log(avctx, AV_LOG_ERROR,
714                "Either hw_frames_ctx or hw_device_ctx must be set.\n");
715         return AVERROR(EINVAL);
716     }
717
718     vtctx->vt_ctx = av_videotoolbox_alloc_context();
719     if (!vtctx->vt_ctx) {
720         err = AVERROR(ENOMEM);
721         goto fail;
722     }
723
724     if (avctx->hw_frames_ctx) {
725         hw_frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
726     } else {
727         avctx->hw_frames_ctx = av_hwframe_ctx_alloc(avctx->hw_device_ctx);
728         if (!avctx->hw_frames_ctx) {
729             err = AVERROR(ENOMEM);
730             goto fail;
731         }
732
733         hw_frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
734         hw_frames->format = AV_PIX_FMT_VIDEOTOOLBOX;
735         hw_frames->sw_format = AV_PIX_FMT_NV12; // same as av_videotoolbox_alloc_context()
736         hw_frames->width = avctx->width;
737         hw_frames->height = avctx->height;
738
739         err = av_hwframe_ctx_init(avctx->hw_frames_ctx);
740         if (err < 0) {
741             av_buffer_unref(&avctx->hw_frames_ctx);
742             goto fail;
743         }
744     }
745
746     vtctx->cached_hw_frames_ctx = av_buffer_ref(avctx->hw_frames_ctx);
747     if (!vtctx->cached_hw_frames_ctx) {
748         err = AVERROR(ENOMEM);
749         goto fail;
750     }
751
752     vtctx->vt_ctx->cv_pix_fmt_type =
753         av_map_videotoolbox_format_from_pixfmt(hw_frames->sw_format);
754     if (!vtctx->vt_ctx->cv_pix_fmt_type) {
755         av_log(avctx, AV_LOG_ERROR, "Unknown sw_format.\n");
756         err = AVERROR(EINVAL);
757         goto fail;
758     }
759
760     err = videotoolbox_default_init(avctx);
761     if (err < 0)
762         goto fail;
763
764     return 0;
765
766 fail:
767     videotoolbox_uninit(avctx);
768     return err;
769 }
770
771 AVHWAccel ff_h263_videotoolbox_hwaccel = {
772     .name           = "h263_videotoolbox",
773     .type           = AVMEDIA_TYPE_VIDEO,
774     .id             = AV_CODEC_ID_H263,
775     .pix_fmt        = AV_PIX_FMT_VIDEOTOOLBOX,
776     .alloc_frame    = ff_videotoolbox_alloc_frame,
777     .start_frame    = videotoolbox_mpeg_start_frame,
778     .decode_slice   = videotoolbox_mpeg_decode_slice,
779     .end_frame      = videotoolbox_mpeg_end_frame,
780     .init           = videotoolbox_common_init,
781     .uninit         = videotoolbox_uninit,
782     .priv_data_size = sizeof(VTContext),
783 };
784
785 AVHWAccel ff_h264_videotoolbox_hwaccel = {
786     .name           = "h264_videotoolbox",
787     .type           = AVMEDIA_TYPE_VIDEO,
788     .id             = AV_CODEC_ID_H264,
789     .pix_fmt        = AV_PIX_FMT_VIDEOTOOLBOX,
790     .alloc_frame    = ff_videotoolbox_alloc_frame,
791     .start_frame    = ff_videotoolbox_h264_start_frame,
792     .decode_slice   = ff_videotoolbox_h264_decode_slice,
793     .end_frame      = videotoolbox_h264_end_frame,
794     .init           = videotoolbox_common_init,
795     .uninit         = videotoolbox_uninit,
796     .priv_data_size = sizeof(VTContext),
797 };
798
799 AVHWAccel ff_mpeg1_videotoolbox_hwaccel = {
800     .name           = "mpeg1_videotoolbox",
801     .type           = AVMEDIA_TYPE_VIDEO,
802     .id             = AV_CODEC_ID_MPEG1VIDEO,
803     .pix_fmt        = AV_PIX_FMT_VIDEOTOOLBOX,
804     .alloc_frame    = ff_videotoolbox_alloc_frame,
805     .start_frame    = videotoolbox_mpeg_start_frame,
806     .decode_slice   = videotoolbox_mpeg_decode_slice,
807     .end_frame      = videotoolbox_mpeg_end_frame,
808     .init           = videotoolbox_common_init,
809     .uninit         = videotoolbox_uninit,
810     .priv_data_size = sizeof(VTContext),
811 };
812
813 AVHWAccel ff_mpeg2_videotoolbox_hwaccel = {
814     .name           = "mpeg2_videotoolbox",
815     .type           = AVMEDIA_TYPE_VIDEO,
816     .id             = AV_CODEC_ID_MPEG2VIDEO,
817     .pix_fmt        = AV_PIX_FMT_VIDEOTOOLBOX,
818     .alloc_frame    = ff_videotoolbox_alloc_frame,
819     .start_frame    = videotoolbox_mpeg_start_frame,
820     .decode_slice   = videotoolbox_mpeg_decode_slice,
821     .end_frame      = videotoolbox_mpeg_end_frame,
822     .init           = videotoolbox_common_init,
823     .uninit         = videotoolbox_uninit,
824     .priv_data_size = sizeof(VTContext),
825 };
826
827 AVHWAccel ff_mpeg4_videotoolbox_hwaccel = {
828     .name           = "mpeg4_videotoolbox",
829     .type           = AVMEDIA_TYPE_VIDEO,
830     .id             = AV_CODEC_ID_MPEG4,
831     .pix_fmt        = AV_PIX_FMT_VIDEOTOOLBOX,
832     .alloc_frame    = ff_videotoolbox_alloc_frame,
833     .start_frame    = videotoolbox_mpeg_start_frame,
834     .decode_slice   = videotoolbox_mpeg_decode_slice,
835     .end_frame      = videotoolbox_mpeg_end_frame,
836     .init           = videotoolbox_common_init,
837     .uninit         = videotoolbox_uninit,
838     .priv_data_size = sizeof(VTContext),
839 };
840
841 AVVideotoolboxContext *av_videotoolbox_alloc_context(void)
842 {
843     AVVideotoolboxContext *ret = av_mallocz(sizeof(*ret));
844
845     if (ret) {
846         ret->output_callback = videotoolbox_decoder_callback;
847         ret->cv_pix_fmt_type = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
848     }
849
850     return ret;
851 }
852
853 int av_videotoolbox_default_init(AVCodecContext *avctx)
854 {
855     return av_videotoolbox_default_init2(avctx, NULL);
856 }
857
858 int av_videotoolbox_default_init2(AVCodecContext *avctx, AVVideotoolboxContext *vtctx)
859 {
860     avctx->hwaccel_context = vtctx ?: av_videotoolbox_alloc_context();
861     if (!avctx->hwaccel_context)
862         return AVERROR(ENOMEM);
863     return videotoolbox_default_init(avctx);
864 }
865
866 void av_videotoolbox_default_free(AVCodecContext *avctx)
867 {
868
869     videotoolbox_default_free(avctx);
870     av_freep(&avctx->hwaccel_context);
871 }
872 #endif /* CONFIG_VIDEOTOOLBOX */