OSDN Git Service

avcodec/vp9block: fix runtime error: signed integer overflow: 196675 * 20670 cannot...
[android-x86/external-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_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);
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     case kVTVideoDecoderUnsupportedDataFormatErr:
652         return AVERROR(ENOSYS);
653     case kVTVideoDecoderMalfunctionErr:
654         return AVERROR(EINVAL);
655     case kVTVideoDecoderBadDataErr :
656         return AVERROR_INVALIDDATA;
657     case 0:
658         return 0;
659     default:
660         return AVERROR_UNKNOWN;
661     }
662 }
663
664 static void videotoolbox_default_free(AVCodecContext *avctx)
665 {
666     AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
667
668     if (videotoolbox) {
669         if (videotoolbox->cm_fmt_desc)
670             CFRelease(videotoolbox->cm_fmt_desc);
671
672         if (videotoolbox->session) {
673             VTDecompressionSessionInvalidate(videotoolbox->session);
674             CFRelease(videotoolbox->session);
675         }
676     }
677 }
678
679 static int videotoolbox_uninit(AVCodecContext *avctx)
680 {
681     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
682     if (!vtctx)
683         return 0;
684
685     ff_videotoolbox_uninit(avctx);
686
687     if (vtctx->vt_ctx)
688         videotoolbox_default_free(avctx);
689
690     av_buffer_unref(&vtctx->cached_hw_frames_ctx);
691     av_freep(&vtctx->vt_ctx);
692
693     return 0;
694 }
695
696 static int videotoolbox_common_init(AVCodecContext *avctx)
697 {
698     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
699     AVHWFramesContext *hw_frames;
700     int err;
701
702     // Old API - do nothing.
703     if (avctx->hwaccel_context)
704         return 0;
705
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);
710     }
711
712     vtctx->vt_ctx = av_videotoolbox_alloc_context();
713     if (!vtctx->vt_ctx) {
714         err = AVERROR(ENOMEM);
715         goto fail;
716     }
717
718     if (avctx->hw_frames_ctx) {
719         hw_frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
720     } else {
721         avctx->hw_frames_ctx = av_hwframe_ctx_alloc(avctx->hw_device_ctx);
722         if (!avctx->hw_frames_ctx) {
723             err = AVERROR(ENOMEM);
724             goto fail;
725         }
726
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;
732
733         err = av_hwframe_ctx_init(avctx->hw_frames_ctx);
734         if (err < 0) {
735             av_buffer_unref(&avctx->hw_frames_ctx);
736             goto fail;
737         }
738     }
739
740     vtctx->cached_hw_frames_ctx = av_buffer_ref(avctx->hw_frames_ctx);
741     if (!vtctx->cached_hw_frames_ctx) {
742         err = AVERROR(ENOMEM);
743         goto fail;
744     }
745
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);
751         goto fail;
752     }
753
754     err = videotoolbox_default_init(avctx);
755     if (err < 0)
756         goto fail;
757
758     return 0;
759
760 fail:
761     videotoolbox_uninit(avctx);
762     return err;
763 }
764
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),
777 };
778
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),
791 };
792
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),
805 };
806
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),
819 };
820
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),
833 };
834
835 AVVideotoolboxContext *av_videotoolbox_alloc_context(void)
836 {
837     AVVideotoolboxContext *ret = av_mallocz(sizeof(*ret));
838
839     if (ret) {
840         ret->output_callback = videotoolbox_decoder_callback;
841         ret->cv_pix_fmt_type = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
842     }
843
844     return ret;
845 }
846
847 int av_videotoolbox_default_init(AVCodecContext *avctx)
848 {
849     return av_videotoolbox_default_init2(avctx, NULL);
850 }
851
852 int av_videotoolbox_default_init2(AVCodecContext *avctx, AVVideotoolboxContext *vtctx)
853 {
854     avctx->hwaccel_context = vtctx ?: av_videotoolbox_alloc_context();
855     if (!avctx->hwaccel_context)
856         return AVERROR(ENOMEM);
857     return videotoolbox_default_init(avctx);
858 }
859
860 void av_videotoolbox_default_free(AVCodecContext *avctx)
861 {
862
863     videotoolbox_default_free(avctx);
864     av_freep(&avctx->hwaccel_context);
865 }
866 #endif /* CONFIG_VIDEOTOOLBOX */