7e915a4e83b797d9f6bbaffb4804df038ffaee93
[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 #include "videotoolbox.h"
25 #include "libavutil/hwcontext_videotoolbox.h"
26 #include "vt_internal.h"
27 #include "libavutil/avutil.h"
28 #include "libavutil/hwcontext.h"
29 #include "bytestream.h"
30 #include "h264dec.h"
31 #include "hevcdec.h"
32 #include "mpegvideo.h"
33 #include <TargetConditionals.h>
34
35 #ifndef kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder
36 #  define kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder CFSTR("RequireHardwareAcceleratedVideoDecoder")
37 #endif
38
39 #if !HAVE_KCMVIDEOCODECTYPE_HEVC
40 enum { kCMVideoCodecType_HEVC = 'hvc1' };
41 #endif
42
43 #define VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING  12
44
45 static void videotoolbox_stop(AVCodecContext *avctx);
46 static int videotoolbox_start(AVCodecContext *avctx);
47
48 static void videotoolbox_buffer_release(void *opaque, uint8_t *data)
49 {
50     CVPixelBufferRef cv_buffer = (CVImageBufferRef)data;
51     CVPixelBufferRelease(cv_buffer);
52 }
53
54 static int videotoolbox_buffer_copy(VTContext *vtctx,
55                                     const uint8_t *buffer,
56                                     uint32_t size)
57 {
58     void *tmp;
59
60     tmp = av_fast_realloc(vtctx->bitstream,
61                          &vtctx->allocated_size,
62                          size);
63
64     if (!tmp)
65         return AVERROR(ENOMEM);
66
67     vtctx->bitstream = tmp;
68     memcpy(vtctx->bitstream, buffer, size);
69     vtctx->bitstream_size = size;
70
71     return 0;
72 }
73
74 int ff_videotoolbox_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
75 {
76     frame->width  = avctx->width;
77     frame->height = avctx->height;
78     frame->format = avctx->pix_fmt;
79     frame->buf[0] = av_buffer_alloc(1);
80
81     if (!frame->buf[0])
82         return AVERROR(ENOMEM);
83
84     return 0;
85 }
86
87 #define AV_W8(p, v) *(p) = (v)
88
89 CFDataRef ff_videotoolbox_avcc_extradata_create(AVCodecContext *avctx)
90 {
91     H264Context *h     = avctx->priv_data;
92     CFDataRef data = NULL;
93     uint8_t *p;
94     int vt_extradata_size = 6 + 2 + h->ps.sps->data_size + 3 + h->ps.pps->data_size;
95     uint8_t *vt_extradata = av_malloc(vt_extradata_size);
96     if (!vt_extradata)
97         return NULL;
98
99     p = vt_extradata;
100
101     AV_W8(p + 0, 1); /* version */
102     AV_W8(p + 1, h->ps.sps->data[1]); /* profile */
103     AV_W8(p + 2, h->ps.sps->data[2]); /* profile compat */
104     AV_W8(p + 3, h->ps.sps->data[3]); /* level */
105     AV_W8(p + 4, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 3 (11) */
106     AV_W8(p + 5, 0xe1); /* 3 bits reserved (111) + 5 bits number of sps (00001) */
107     AV_WB16(p + 6, h->ps.sps->data_size);
108     memcpy(p + 8, h->ps.sps->data, h->ps.sps->data_size);
109     p += 8 + h->ps.sps->data_size;
110     AV_W8(p + 0, 1); /* number of pps */
111     AV_WB16(p + 1, h->ps.pps->data_size);
112     memcpy(p + 3, h->ps.pps->data, h->ps.pps->data_size);
113
114     p += 3 + h->ps.pps->data_size;
115     av_assert0(p - vt_extradata == vt_extradata_size);
116
117     data = CFDataCreate(kCFAllocatorDefault, vt_extradata, vt_extradata_size);
118     av_free(vt_extradata);
119     return data;
120 }
121
122 CFDataRef ff_videotoolbox_hvcc_extradata_create(AVCodecContext *avctx)
123 {
124     HEVCContext *h = avctx->priv_data;
125     const HEVCVPS *vps = (const HEVCVPS *)h->ps.vps_list[0]->data;
126     const HEVCSPS *sps = (const HEVCSPS *)h->ps.sps_list[0]->data;
127     int i, num_pps = 0;
128     const HEVCPPS *pps = h->ps.pps;
129     PTLCommon ptlc = vps->ptl.general_ptl;
130     VUI vui = sps->vui;
131     uint8_t parallelismType;
132     CFDataRef data = NULL;
133     uint8_t *p;
134     int vt_extradata_size = 23 + 5 + vps->data_size + 5 + sps->data_size + 3;
135     uint8_t *vt_extradata;
136
137     for (i = 0; i < HEVC_MAX_PPS_COUNT; i++) {
138         if (h->ps.pps_list[i]) {
139             const HEVCPPS *pps = (const HEVCPPS *)h->ps.pps_list[i]->data;
140             vt_extradata_size += 2 + pps->data_size;
141             num_pps++;
142         }
143     }
144
145     vt_extradata = av_malloc(vt_extradata_size);
146     if (!vt_extradata)
147         return NULL;
148     p = vt_extradata;
149
150     /* unsigned int(8) configurationVersion = 1; */
151     AV_W8(p + 0, 1);
152
153     /*
154      * unsigned int(2) general_profile_space;
155      * unsigned int(1) general_tier_flag;
156      * unsigned int(5) general_profile_idc;
157      */
158     AV_W8(p + 1, ptlc.profile_space << 6 |
159                  ptlc.tier_flag     << 5 |
160                  ptlc.profile_idc);
161
162     /* unsigned int(32) general_profile_compatibility_flags; */
163     memcpy(p + 2, ptlc.profile_compatibility_flag, 4);
164
165     /* unsigned int(48) general_constraint_indicator_flags; */
166     AV_W8(p + 6, ptlc.progressive_source_flag    << 7 |
167                  ptlc.interlaced_source_flag     << 6 |
168                  ptlc.non_packed_constraint_flag << 5 |
169                  ptlc.frame_only_constraint_flag << 4);
170     AV_W8(p + 7, 0);
171     AV_WN32(p + 8, 0);
172
173     /* unsigned int(8) general_level_idc; */
174     AV_W8(p + 12, ptlc.level_idc);
175
176     /*
177      * bit(4) reserved = ‘1111’b;
178      * unsigned int(12) min_spatial_segmentation_idc;
179      */
180     AV_W8(p + 13, 0xf0 | (vui.min_spatial_segmentation_idc >> 4));
181     AV_W8(p + 14, vui.min_spatial_segmentation_idc & 0xff);
182
183     /*
184      * bit(6) reserved = ‘111111’b;
185      * unsigned int(2) parallelismType;
186      */
187     if (!vui.min_spatial_segmentation_idc)
188         parallelismType = 0;
189     else if (pps->entropy_coding_sync_enabled_flag && pps->tiles_enabled_flag)
190         parallelismType = 0;
191     else if (pps->entropy_coding_sync_enabled_flag)
192         parallelismType = 3;
193     else if (pps->tiles_enabled_flag)
194         parallelismType = 2;
195     else
196         parallelismType = 1;
197     AV_W8(p + 15, 0xfc | parallelismType);
198
199     /*
200      * bit(6) reserved = ‘111111’b;
201      * unsigned int(2) chromaFormat;
202      */
203     AV_W8(p + 16, sps->chroma_format_idc | 0xfc);
204
205     /*
206      * bit(5) reserved = ‘11111’b;
207      * unsigned int(3) bitDepthLumaMinus8;
208      */
209     AV_W8(p + 17, (sps->bit_depth - 8) | 0xfc);
210
211     /*
212      * bit(5) reserved = ‘11111’b;
213      * unsigned int(3) bitDepthChromaMinus8;
214      */
215     AV_W8(p + 18, (sps->bit_depth_chroma - 8) | 0xfc);
216
217     /* bit(16) avgFrameRate; */
218     AV_WB16(p + 19, 0);
219
220     /*
221      * bit(2) constantFrameRate;
222      * bit(3) numTemporalLayers;
223      * bit(1) temporalIdNested;
224      * unsigned int(2) lengthSizeMinusOne;
225      */
226     AV_W8(p + 21, 0                             << 6 |
227                   sps->max_sub_layers           << 3 |
228                   sps->temporal_id_nesting_flag << 2 |
229                   3);
230
231     /* unsigned int(8) numOfArrays; */
232     AV_W8(p + 22, 3);
233
234     p += 23;
235     /* vps */
236     /*
237      * bit(1) array_completeness;
238      * unsigned int(1) reserved = 0;
239      * unsigned int(6) NAL_unit_type;
240      */
241     AV_W8(p, 1 << 7 |
242              HEVC_NAL_VPS & 0x3f);
243     /* unsigned int(16) numNalus; */
244     AV_WB16(p + 1, 1);
245     /* unsigned int(16) nalUnitLength; */
246     AV_WB16(p + 3, vps->data_size);
247     /* bit(8*nalUnitLength) nalUnit; */
248     memcpy(p + 5, vps->data, vps->data_size);
249     p += 5 + vps->data_size;
250
251     /* sps */
252     AV_W8(p, 1 << 7 |
253              HEVC_NAL_SPS & 0x3f);
254     AV_WB16(p + 1, 1);
255     AV_WB16(p + 3, sps->data_size);
256     memcpy(p + 5, sps->data, sps->data_size);
257     p += 5 + sps->data_size;
258
259     /* pps */
260     AV_W8(p, 1 << 7 |
261              HEVC_NAL_PPS & 0x3f);
262     AV_WB16(p + 1, num_pps);
263     p += 3;
264     for (i = 0; i < HEVC_MAX_PPS_COUNT; i++) {
265         if (h->ps.pps_list[i]) {
266             const HEVCPPS *pps = (const HEVCPPS *)h->ps.pps_list[i]->data;
267             AV_WB16(p, pps->data_size);
268             memcpy(p + 2, pps->data, pps->data_size);
269             p += 2 + pps->data_size;
270         }
271     }
272
273     av_assert0(p - vt_extradata == vt_extradata_size);
274
275     data = CFDataCreate(kCFAllocatorDefault, vt_extradata, vt_extradata_size);
276     av_free(vt_extradata);
277     return data;
278 }
279
280 int ff_videotoolbox_buffer_create(VTContext *vtctx, AVFrame *frame)
281 {
282     av_buffer_unref(&frame->buf[0]);
283
284     frame->buf[0] = av_buffer_create((uint8_t*)vtctx->frame,
285                                      sizeof(vtctx->frame),
286                                      videotoolbox_buffer_release,
287                                      NULL,
288                                      AV_BUFFER_FLAG_READONLY);
289     if (!frame->buf[0]) {
290         return AVERROR(ENOMEM);
291     }
292
293     frame->data[3] = (uint8_t*)vtctx->frame;
294     vtctx->frame = NULL;
295
296     return 0;
297 }
298
299 int ff_videotoolbox_h264_start_frame(AVCodecContext *avctx,
300                                      const uint8_t *buffer,
301                                      uint32_t size)
302 {
303     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
304     H264Context *h  = avctx->priv_data;
305
306     if (h->is_avc == 1) {
307         return videotoolbox_buffer_copy(vtctx, buffer, size);
308     }
309
310     return 0;
311 }
312
313 static int videotoolbox_h264_decode_params(AVCodecContext *avctx,
314                                            int type,
315                                            const uint8_t *buffer,
316                                            uint32_t size)
317 {
318     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
319
320     if (type == H264_NAL_SPS) {
321         if (!vtctx->sps || vtctx->sps_len != size || memcmp(buffer, vtctx->sps, size) != 0) {
322             vtctx->sps = av_fast_realloc(vtctx->sps, &vtctx->sps_capa, size);
323             if (vtctx->sps)
324                 memcpy(vtctx->sps, buffer, size);
325             vtctx->reconfig_needed = true;
326             vtctx->sps_len = size;
327         }
328     }
329
330     // pass-through new PPS to the decoder
331     return ff_videotoolbox_h264_decode_slice(avctx, buffer, size);
332 }
333
334 int ff_videotoolbox_h264_decode_slice(AVCodecContext *avctx,
335                                       const uint8_t *buffer,
336                                       uint32_t size)
337 {
338     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
339     H264Context *h  = avctx->priv_data;
340     void *tmp;
341
342     if (h->is_avc == 1)
343         return 0;
344
345     tmp = av_fast_realloc(vtctx->bitstream,
346                           &vtctx->allocated_size,
347                           vtctx->bitstream_size+size+4);
348     if (!tmp)
349         return AVERROR(ENOMEM);
350
351     vtctx->bitstream = tmp;
352
353     AV_WB32(vtctx->bitstream + vtctx->bitstream_size, size);
354     memcpy(vtctx->bitstream + vtctx->bitstream_size + 4, buffer, size);
355
356     vtctx->bitstream_size += size + 4;
357
358     return 0;
359 }
360
361 int ff_videotoolbox_uninit(AVCodecContext *avctx)
362 {
363     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
364     if (vtctx) {
365         av_freep(&vtctx->bitstream);
366         av_freep(&vtctx->sps);
367         if (vtctx->frame)
368             CVPixelBufferRelease(vtctx->frame);
369     }
370
371     return 0;
372 }
373
374 #if CONFIG_VIDEOTOOLBOX
375 // Return the AVVideotoolboxContext that matters currently. Where it comes from
376 // depends on the API used.
377 static AVVideotoolboxContext *videotoolbox_get_context(AVCodecContext *avctx)
378 {
379     // Somewhat tricky because the user can call av_videotoolbox_default_free()
380     // at any time, even when the codec is closed.
381     if (avctx->internal && avctx->internal->hwaccel_priv_data) {
382         VTContext *vtctx = avctx->internal->hwaccel_priv_data;
383         if (vtctx->vt_ctx)
384             return vtctx->vt_ctx;
385     }
386     return avctx->hwaccel_context;
387 }
388
389 static int videotoolbox_buffer_create(AVCodecContext *avctx, AVFrame *frame)
390 {
391     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
392     CVPixelBufferRef pixbuf = (CVPixelBufferRef)vtctx->frame;
393     OSType pixel_format = CVPixelBufferGetPixelFormatType(pixbuf);
394     enum AVPixelFormat sw_format = av_map_videotoolbox_format_to_pixfmt(pixel_format);
395     int width = CVPixelBufferGetWidth(pixbuf);
396     int height = CVPixelBufferGetHeight(pixbuf);
397     AVHWFramesContext *cached_frames;
398     int ret;
399
400     ret = ff_videotoolbox_buffer_create(vtctx, frame);
401     if (ret < 0)
402         return ret;
403
404     // Old API code path.
405     if (!vtctx->cached_hw_frames_ctx)
406         return 0;
407
408     cached_frames = (AVHWFramesContext*)vtctx->cached_hw_frames_ctx->data;
409
410     if (cached_frames->sw_format != sw_format ||
411         cached_frames->width != width ||
412         cached_frames->height != height) {
413         AVBufferRef *hw_frames_ctx = av_hwframe_ctx_alloc(cached_frames->device_ref);
414         AVHWFramesContext *hw_frames;
415         if (!hw_frames_ctx)
416             return AVERROR(ENOMEM);
417
418         hw_frames = (AVHWFramesContext*)hw_frames_ctx->data;
419         hw_frames->format = cached_frames->format;
420         hw_frames->sw_format = sw_format;
421         hw_frames->width = width;
422         hw_frames->height = height;
423
424         ret = av_hwframe_ctx_init(hw_frames_ctx);
425         if (ret < 0) {
426             av_buffer_unref(&hw_frames_ctx);
427             return ret;
428         }
429
430         av_buffer_unref(&vtctx->cached_hw_frames_ctx);
431         vtctx->cached_hw_frames_ctx = hw_frames_ctx;
432     }
433
434     av_buffer_unref(&frame->hw_frames_ctx);
435     frame->hw_frames_ctx = av_buffer_ref(vtctx->cached_hw_frames_ctx);
436     if (!frame->hw_frames_ctx)
437         return AVERROR(ENOMEM);
438
439     return 0;
440 }
441
442 static void videotoolbox_write_mp4_descr_length(PutByteContext *pb, int length)
443 {
444     int i;
445     uint8_t b;
446
447     for (i = 3; i >= 0; i--) {
448         b = (length >> (i * 7)) & 0x7F;
449         if (i != 0)
450             b |= 0x80;
451
452         bytestream2_put_byteu(pb, b);
453     }
454 }
455
456 static CFDataRef videotoolbox_esds_extradata_create(AVCodecContext *avctx)
457 {
458     CFDataRef data;
459     uint8_t *rw_extradata;
460     PutByteContext pb;
461     int full_size = 3 + 5 + 13 + 5 + avctx->extradata_size + 3;
462     // ES_DescrTag data + DecoderConfigDescrTag + data + DecSpecificInfoTag + size + SLConfigDescriptor
463     int config_size = 13 + 5 + avctx->extradata_size;
464     int s;
465
466     if (!(rw_extradata = av_mallocz(full_size + VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING)))
467         return NULL;
468
469     bytestream2_init_writer(&pb, rw_extradata, full_size + VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING);
470     bytestream2_put_byteu(&pb, 0);        // version
471     bytestream2_put_ne24(&pb, 0);         // flags
472
473     // elementary stream descriptor
474     bytestream2_put_byteu(&pb, 0x03);     // ES_DescrTag
475     videotoolbox_write_mp4_descr_length(&pb, full_size);
476     bytestream2_put_ne16(&pb, 0);         // esid
477     bytestream2_put_byteu(&pb, 0);        // stream priority (0-32)
478
479     // decoder configuration descriptor
480     bytestream2_put_byteu(&pb, 0x04);     // DecoderConfigDescrTag
481     videotoolbox_write_mp4_descr_length(&pb, config_size);
482     bytestream2_put_byteu(&pb, 32);       // object type indication. 32 = AV_CODEC_ID_MPEG4
483     bytestream2_put_byteu(&pb, 0x11);     // stream type
484     bytestream2_put_ne24(&pb, 0);         // buffer size
485     bytestream2_put_ne32(&pb, 0);         // max bitrate
486     bytestream2_put_ne32(&pb, 0);         // avg bitrate
487
488     // decoder specific descriptor
489     bytestream2_put_byteu(&pb, 0x05);     ///< DecSpecificInfoTag
490     videotoolbox_write_mp4_descr_length(&pb, avctx->extradata_size);
491
492     bytestream2_put_buffer(&pb, avctx->extradata, avctx->extradata_size);
493
494     // SLConfigDescriptor
495     bytestream2_put_byteu(&pb, 0x06);     // SLConfigDescrTag
496     bytestream2_put_byteu(&pb, 0x01);     // length
497     bytestream2_put_byteu(&pb, 0x02);     //
498
499     s = bytestream2_size_p(&pb);
500
501     data = CFDataCreate(kCFAllocatorDefault, rw_extradata, s);
502
503     av_freep(&rw_extradata);
504     return data;
505 }
506
507 static CMSampleBufferRef videotoolbox_sample_buffer_create(CMFormatDescriptionRef fmt_desc,
508                                                            void *buffer,
509                                                            int size)
510 {
511     OSStatus status;
512     CMBlockBufferRef  block_buf;
513     CMSampleBufferRef sample_buf;
514
515     block_buf  = NULL;
516     sample_buf = NULL;
517
518     status = CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault,// structureAllocator
519                                                 buffer,             // memoryBlock
520                                                 size,               // blockLength
521                                                 kCFAllocatorNull,   // blockAllocator
522                                                 NULL,               // customBlockSource
523                                                 0,                  // offsetToData
524                                                 size,               // dataLength
525                                                 0,                  // flags
526                                                 &block_buf);
527
528     if (!status) {
529         status = CMSampleBufferCreate(kCFAllocatorDefault,  // allocator
530                                       block_buf,            // dataBuffer
531                                       TRUE,                 // dataReady
532                                       0,                    // makeDataReadyCallback
533                                       0,                    // makeDataReadyRefcon
534                                       fmt_desc,             // formatDescription
535                                       1,                    // numSamples
536                                       0,                    // numSampleTimingEntries
537                                       NULL,                 // sampleTimingArray
538                                       0,                    // numSampleSizeEntries
539                                       NULL,                 // sampleSizeArray
540                                       &sample_buf);
541     }
542
543     if (block_buf)
544         CFRelease(block_buf);
545
546     return sample_buf;
547 }
548
549 static void videotoolbox_decoder_callback(void *opaque,
550                                           void *sourceFrameRefCon,
551                                           OSStatus status,
552                                           VTDecodeInfoFlags flags,
553                                           CVImageBufferRef image_buffer,
554                                           CMTime pts,
555                                           CMTime duration)
556 {
557     AVCodecContext *avctx = opaque;
558     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
559
560     if (vtctx->frame) {
561         CVPixelBufferRelease(vtctx->frame);
562         vtctx->frame = NULL;
563     }
564
565     if (!image_buffer) {
566         av_log(NULL, AV_LOG_DEBUG, "vt decoder cb: output image buffer is null\n");
567         return;
568     }
569
570     vtctx->frame = CVPixelBufferRetain(image_buffer);
571 }
572
573 static OSStatus videotoolbox_session_decode_frame(AVCodecContext *avctx)
574 {
575     OSStatus status;
576     CMSampleBufferRef sample_buf;
577     AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
578     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
579
580     sample_buf = videotoolbox_sample_buffer_create(videotoolbox->cm_fmt_desc,
581                                                    vtctx->bitstream,
582                                                    vtctx->bitstream_size);
583
584     if (!sample_buf)
585         return -1;
586
587     status = VTDecompressionSessionDecodeFrame(videotoolbox->session,
588                                                sample_buf,
589                                                0,       // decodeFlags
590                                                NULL,    // sourceFrameRefCon
591                                                0);      // infoFlagsOut
592     if (status == noErr)
593         status = VTDecompressionSessionWaitForAsynchronousFrames(videotoolbox->session);
594
595     CFRelease(sample_buf);
596
597     return status;
598 }
599
600 static const char *videotoolbox_error_string(OSStatus status)
601 {
602     switch (status) {
603         case kVTVideoDecoderBadDataErr:
604             return "bad data";
605         case kVTVideoDecoderMalfunctionErr:
606             return "decoder malfunction";
607         case kVTInvalidSessionErr:
608             return "invalid session";
609     }
610     return "unknown";
611 }
612
613 static int videotoolbox_common_end_frame(AVCodecContext *avctx, AVFrame *frame)
614 {
615     OSStatus status;
616     AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
617     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
618
619     if (vtctx->reconfig_needed == true) {
620         vtctx->reconfig_needed = false;
621         av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox decoder needs reconfig, restarting..\n");
622         videotoolbox_stop(avctx);
623         if (videotoolbox_start(avctx) != 0) {
624             return AVERROR_EXTERNAL;
625         }
626     }
627
628     if (!videotoolbox->session || !vtctx->bitstream || !vtctx->bitstream_size)
629         return AVERROR_INVALIDDATA;
630
631     status = videotoolbox_session_decode_frame(avctx);
632     if (status != noErr) {
633         if (status == kVTVideoDecoderMalfunctionErr || status == kVTInvalidSessionErr)
634             vtctx->reconfig_needed = true;
635         av_log(avctx, AV_LOG_ERROR, "Failed to decode frame (%s, %d)\n", videotoolbox_error_string(status), (int)status);
636         return AVERROR_UNKNOWN;
637     }
638
639     if (!vtctx->frame) {
640         vtctx->reconfig_needed = true;
641         return AVERROR_UNKNOWN;
642     }
643
644     return videotoolbox_buffer_create(avctx, frame);
645 }
646
647 static int videotoolbox_h264_end_frame(AVCodecContext *avctx)
648 {
649     H264Context *h = avctx->priv_data;
650     AVFrame *frame = h->cur_pic_ptr->f;
651     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
652     int ret = videotoolbox_common_end_frame(avctx, frame);
653     vtctx->bitstream_size = 0;
654     return ret;
655 }
656
657 static int videotoolbox_hevc_end_frame(AVCodecContext *avctx)
658 {
659     HEVCContext *h = avctx->priv_data;
660     AVFrame *frame = h->ref->frame;
661     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
662     int ret;
663
664     ret = videotoolbox_common_end_frame(avctx, frame);
665     vtctx->bitstream_size = 0;
666     return ret;
667 }
668
669 static int videotoolbox_mpeg_start_frame(AVCodecContext *avctx,
670                                          const uint8_t *buffer,
671                                          uint32_t size)
672 {
673     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
674
675     return videotoolbox_buffer_copy(vtctx, buffer, size);
676 }
677
678 static int videotoolbox_mpeg_decode_slice(AVCodecContext *avctx,
679                                           const uint8_t *buffer,
680                                           uint32_t size)
681 {
682     return 0;
683 }
684
685 static int videotoolbox_mpeg_end_frame(AVCodecContext *avctx)
686 {
687     MpegEncContext *s = avctx->priv_data;
688     AVFrame *frame = s->current_picture_ptr->f;
689
690     return videotoolbox_common_end_frame(avctx, frame);
691 }
692
693 static CFDictionaryRef videotoolbox_decoder_config_create(CMVideoCodecType codec_type,
694                                                           AVCodecContext *avctx)
695 {
696     CFMutableDictionaryRef config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
697                                                                    0,
698                                                                    &kCFTypeDictionaryKeyCallBacks,
699                                                                    &kCFTypeDictionaryValueCallBacks);
700
701     CFDictionarySetValue(config_info,
702                          kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder,
703                          kCFBooleanTrue);
704
705     if (1) {
706         CFMutableDictionaryRef avc_info;
707         CFDataRef data = NULL;
708
709         avc_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
710                                              1,
711                                              &kCFTypeDictionaryKeyCallBacks,
712                                              &kCFTypeDictionaryValueCallBacks);
713
714         switch (codec_type) {
715         case kCMVideoCodecType_MPEG4Video :
716             if (avctx->extradata_size)
717                 data = videotoolbox_esds_extradata_create(avctx);
718             if (data)
719                 CFDictionarySetValue(avc_info, CFSTR("esds"), data);
720             break;
721         case kCMVideoCodecType_H264 :
722             data = ff_videotoolbox_avcc_extradata_create(avctx);
723             if (data)
724                 CFDictionarySetValue(avc_info, CFSTR("avcC"), data);
725             break;
726         case kCMVideoCodecType_HEVC :
727             data = ff_videotoolbox_hvcc_extradata_create(avctx);
728             if (data)
729                 CFDictionarySetValue(avc_info, CFSTR("hvcC"), data);
730             break;
731         default:
732             break;
733         }
734
735         CFDictionarySetValue(config_info,
736                 kCMFormatDescriptionExtension_SampleDescriptionExtensionAtoms,
737                 avc_info);
738
739         if (data)
740             CFRelease(data);
741
742         CFRelease(avc_info);
743     }
744     return config_info;
745 }
746
747 static CFDictionaryRef videotoolbox_buffer_attributes_create(int width,
748                                                              int height,
749                                                              OSType pix_fmt)
750 {
751     CFMutableDictionaryRef buffer_attributes;
752     CFMutableDictionaryRef io_surface_properties;
753     CFNumberRef cv_pix_fmt;
754     CFNumberRef w;
755     CFNumberRef h;
756
757     w = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &width);
758     h = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &height);
759     cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &pix_fmt);
760
761     buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
762                                                   4,
763                                                   &kCFTypeDictionaryKeyCallBacks,
764                                                   &kCFTypeDictionaryValueCallBacks);
765     io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
766                                                       0,
767                                                       &kCFTypeDictionaryKeyCallBacks,
768                                                       &kCFTypeDictionaryValueCallBacks);
769
770     if (pix_fmt)
771         CFDictionarySetValue(buffer_attributes, kCVPixelBufferPixelFormatTypeKey, cv_pix_fmt);
772     CFDictionarySetValue(buffer_attributes, kCVPixelBufferIOSurfacePropertiesKey, io_surface_properties);
773     CFDictionarySetValue(buffer_attributes, kCVPixelBufferWidthKey, w);
774     CFDictionarySetValue(buffer_attributes, kCVPixelBufferHeightKey, h);
775 #if TARGET_OS_IPHONE
776     CFDictionarySetValue(buffer_attributes, kCVPixelBufferOpenGLESCompatibilityKey, kCFBooleanTrue);
777 #else
778     CFDictionarySetValue(buffer_attributes, kCVPixelBufferIOSurfaceOpenGLTextureCompatibilityKey, kCFBooleanTrue);
779 #endif
780
781     CFRelease(io_surface_properties);
782     CFRelease(cv_pix_fmt);
783     CFRelease(w);
784     CFRelease(h);
785
786     return buffer_attributes;
787 }
788
789 static CMVideoFormatDescriptionRef videotoolbox_format_desc_create(CMVideoCodecType codec_type,
790                                                                    CFDictionaryRef decoder_spec,
791                                                                    int width,
792                                                                    int height)
793 {
794     CMFormatDescriptionRef cm_fmt_desc;
795     OSStatus status;
796
797     status = CMVideoFormatDescriptionCreate(kCFAllocatorDefault,
798                                             codec_type,
799                                             width,
800                                             height,
801                                             decoder_spec, // Dictionary of extension
802                                             &cm_fmt_desc);
803
804     if (status)
805         return NULL;
806
807     return cm_fmt_desc;
808 }
809
810 static int videotoolbox_start(AVCodecContext *avctx)
811 {
812     AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
813     OSStatus status;
814     VTDecompressionOutputCallbackRecord decoder_cb;
815     CFDictionaryRef decoder_spec;
816     CFDictionaryRef buf_attr;
817
818     if (!videotoolbox) {
819         av_log(avctx, AV_LOG_ERROR, "hwaccel context is not set\n");
820         return -1;
821     }
822
823     switch( avctx->codec_id ) {
824     case AV_CODEC_ID_H263 :
825         videotoolbox->cm_codec_type = kCMVideoCodecType_H263;
826         break;
827     case AV_CODEC_ID_H264 :
828         videotoolbox->cm_codec_type = kCMVideoCodecType_H264;
829         break;
830     case AV_CODEC_ID_HEVC :
831         videotoolbox->cm_codec_type = kCMVideoCodecType_HEVC;
832         break;
833     case AV_CODEC_ID_MPEG1VIDEO :
834         videotoolbox->cm_codec_type = kCMVideoCodecType_MPEG1Video;
835         break;
836     case AV_CODEC_ID_MPEG2VIDEO :
837         videotoolbox->cm_codec_type = kCMVideoCodecType_MPEG2Video;
838         break;
839     case AV_CODEC_ID_MPEG4 :
840         videotoolbox->cm_codec_type = kCMVideoCodecType_MPEG4Video;
841         break;
842     default :
843         break;
844     }
845
846     decoder_spec = videotoolbox_decoder_config_create(videotoolbox->cm_codec_type, avctx);
847
848     if (!decoder_spec) {
849         av_log(avctx, AV_LOG_ERROR, "decoder specification creation failed\n");
850         return -1;
851     }
852
853     videotoolbox->cm_fmt_desc = videotoolbox_format_desc_create(videotoolbox->cm_codec_type,
854                                                                 decoder_spec,
855                                                                 avctx->width,
856                                                                 avctx->height);
857     if (!videotoolbox->cm_fmt_desc) {
858         if (decoder_spec)
859             CFRelease(decoder_spec);
860
861         av_log(avctx, AV_LOG_ERROR, "format description creation failed\n");
862         return -1;
863     }
864
865     buf_attr = videotoolbox_buffer_attributes_create(avctx->width,
866                                                      avctx->height,
867                                                      videotoolbox->cv_pix_fmt_type);
868
869     decoder_cb.decompressionOutputCallback = videotoolbox_decoder_callback;
870     decoder_cb.decompressionOutputRefCon   = avctx;
871
872     status = VTDecompressionSessionCreate(NULL,                      // allocator
873                                           videotoolbox->cm_fmt_desc, // videoFormatDescription
874                                           decoder_spec,              // videoDecoderSpecification
875                                           buf_attr,                  // destinationImageBufferAttributes
876                                           &decoder_cb,               // outputCallback
877                                           &videotoolbox->session);   // decompressionSessionOut
878
879     if (decoder_spec)
880         CFRelease(decoder_spec);
881     if (buf_attr)
882         CFRelease(buf_attr);
883
884     switch (status) {
885     case kVTVideoDecoderNotAvailableNowErr:
886         av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox session not available.\n");
887         return AVERROR(ENOSYS);
888     case kVTVideoDecoderUnsupportedDataFormatErr:
889         av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox does not support this format.\n");
890         return AVERROR(ENOSYS);
891     case kVTVideoDecoderMalfunctionErr:
892         av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox malfunction.\n");
893         return AVERROR(EINVAL);
894     case kVTVideoDecoderBadDataErr :
895         av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox reported invalid data.\n");
896         return AVERROR_INVALIDDATA;
897     case 0:
898         return 0;
899     default:
900         av_log(avctx, AV_LOG_VERBOSE, "Unknown VideoToolbox session creation error %u\n", (unsigned)status);
901         return AVERROR_UNKNOWN;
902     }
903 }
904
905 static void videotoolbox_stop(AVCodecContext *avctx)
906 {
907     AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
908     if (!videotoolbox)
909         return;
910
911     if (videotoolbox->cm_fmt_desc) {
912         CFRelease(videotoolbox->cm_fmt_desc);
913         videotoolbox->cm_fmt_desc = NULL;
914     }
915
916     if (videotoolbox->session) {
917         VTDecompressionSessionInvalidate(videotoolbox->session);
918         CFRelease(videotoolbox->session);
919         videotoolbox->session = NULL;
920     }
921 }
922
923 static int videotoolbox_uninit(AVCodecContext *avctx)
924 {
925     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
926     if (!vtctx)
927         return 0;
928
929     ff_videotoolbox_uninit(avctx);
930
931     if (vtctx->vt_ctx)
932         videotoolbox_stop(avctx);
933
934     av_buffer_unref(&vtctx->cached_hw_frames_ctx);
935     av_freep(&vtctx->vt_ctx);
936
937     return 0;
938 }
939
940 static int videotoolbox_common_init(AVCodecContext *avctx)
941 {
942     VTContext *vtctx = avctx->internal->hwaccel_priv_data;
943     AVHWFramesContext *hw_frames;
944     int err;
945
946     // Old API - do nothing.
947     if (avctx->hwaccel_context)
948         return 0;
949
950     if (!avctx->hw_frames_ctx && !avctx->hw_device_ctx) {
951         av_log(avctx, AV_LOG_ERROR,
952                "Either hw_frames_ctx or hw_device_ctx must be set.\n");
953         return AVERROR(EINVAL);
954     }
955
956     vtctx->vt_ctx = av_videotoolbox_alloc_context();
957     if (!vtctx->vt_ctx) {
958         err = AVERROR(ENOMEM);
959         goto fail;
960     }
961
962     if (avctx->hw_frames_ctx) {
963         hw_frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
964     } else {
965         avctx->hw_frames_ctx = av_hwframe_ctx_alloc(avctx->hw_device_ctx);
966         if (!avctx->hw_frames_ctx) {
967             err = AVERROR(ENOMEM);
968             goto fail;
969         }
970
971         hw_frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
972         hw_frames->format = AV_PIX_FMT_VIDEOTOOLBOX;
973         hw_frames->sw_format = AV_PIX_FMT_NV12; // same as av_videotoolbox_alloc_context()
974         hw_frames->width = avctx->width;
975         hw_frames->height = avctx->height;
976
977         err = av_hwframe_ctx_init(avctx->hw_frames_ctx);
978         if (err < 0) {
979             av_buffer_unref(&avctx->hw_frames_ctx);
980             goto fail;
981         }
982     }
983
984     vtctx->cached_hw_frames_ctx = av_buffer_ref(avctx->hw_frames_ctx);
985     if (!vtctx->cached_hw_frames_ctx) {
986         err = AVERROR(ENOMEM);
987         goto fail;
988     }
989
990     vtctx->vt_ctx->cv_pix_fmt_type =
991         av_map_videotoolbox_format_from_pixfmt(hw_frames->sw_format);
992     if (!vtctx->vt_ctx->cv_pix_fmt_type) {
993         av_log(avctx, AV_LOG_ERROR, "Unknown sw_format.\n");
994         err = AVERROR(EINVAL);
995         goto fail;
996     }
997
998     err = videotoolbox_start(avctx);
999     if (err < 0)
1000         goto fail;
1001
1002     return 0;
1003
1004 fail:
1005     videotoolbox_uninit(avctx);
1006     return err;
1007 }
1008
1009 static int videotoolbox_frame_params(AVCodecContext *avctx,
1010                                      AVBufferRef *hw_frames_ctx)
1011 {
1012     AVHWFramesContext *frames_ctx = (AVHWFramesContext*)hw_frames_ctx->data;
1013
1014     frames_ctx->format            = AV_PIX_FMT_VIDEOTOOLBOX;
1015     frames_ctx->width             = avctx->coded_width;
1016     frames_ctx->height            = avctx->coded_height;
1017     frames_ctx->sw_format         = AV_PIX_FMT_NV12;
1018
1019     return 0;
1020 }
1021
1022 AVHWAccel ff_h263_videotoolbox_hwaccel = {
1023     .name           = "h263_videotoolbox",
1024     .type           = AVMEDIA_TYPE_VIDEO,
1025     .id             = AV_CODEC_ID_H263,
1026     .pix_fmt        = AV_PIX_FMT_VIDEOTOOLBOX,
1027     .alloc_frame    = ff_videotoolbox_alloc_frame,
1028     .start_frame    = videotoolbox_mpeg_start_frame,
1029     .decode_slice   = videotoolbox_mpeg_decode_slice,
1030     .end_frame      = videotoolbox_mpeg_end_frame,
1031     .frame_params   = videotoolbox_frame_params,
1032     .init           = videotoolbox_common_init,
1033     .uninit         = videotoolbox_uninit,
1034     .priv_data_size = sizeof(VTContext),
1035 };
1036
1037 AVHWAccel ff_hevc_videotoolbox_hwaccel = {
1038     .name           = "hevc_videotoolbox",
1039     .type           = AVMEDIA_TYPE_VIDEO,
1040     .id             = AV_CODEC_ID_HEVC,
1041     .pix_fmt        = AV_PIX_FMT_VIDEOTOOLBOX,
1042     .alloc_frame    = ff_videotoolbox_alloc_frame,
1043     .start_frame    = ff_videotoolbox_h264_start_frame,
1044     .decode_slice   = ff_videotoolbox_h264_decode_slice,
1045     .end_frame      = videotoolbox_hevc_end_frame,
1046     .frame_params   = videotoolbox_frame_params,
1047     .init           = videotoolbox_common_init,
1048     .uninit         = ff_videotoolbox_uninit,
1049     .priv_data_size = sizeof(VTContext),
1050 };
1051
1052 AVHWAccel ff_h264_videotoolbox_hwaccel = {
1053     .name           = "h264_videotoolbox",
1054     .type           = AVMEDIA_TYPE_VIDEO,
1055     .id             = AV_CODEC_ID_H264,
1056     .pix_fmt        = AV_PIX_FMT_VIDEOTOOLBOX,
1057     .alloc_frame    = ff_videotoolbox_alloc_frame,
1058     .start_frame    = ff_videotoolbox_h264_start_frame,
1059     .decode_slice   = ff_videotoolbox_h264_decode_slice,
1060     .decode_params  = videotoolbox_h264_decode_params,
1061     .end_frame      = videotoolbox_h264_end_frame,
1062     .frame_params   = videotoolbox_frame_params,
1063     .init           = videotoolbox_common_init,
1064     .uninit         = videotoolbox_uninit,
1065     .priv_data_size = sizeof(VTContext),
1066 };
1067
1068 AVHWAccel ff_mpeg1_videotoolbox_hwaccel = {
1069     .name           = "mpeg1_videotoolbox",
1070     .type           = AVMEDIA_TYPE_VIDEO,
1071     .id             = AV_CODEC_ID_MPEG1VIDEO,
1072     .pix_fmt        = AV_PIX_FMT_VIDEOTOOLBOX,
1073     .alloc_frame    = ff_videotoolbox_alloc_frame,
1074     .start_frame    = videotoolbox_mpeg_start_frame,
1075     .decode_slice   = videotoolbox_mpeg_decode_slice,
1076     .end_frame      = videotoolbox_mpeg_end_frame,
1077     .frame_params   = videotoolbox_frame_params,
1078     .init           = videotoolbox_common_init,
1079     .uninit         = videotoolbox_uninit,
1080     .priv_data_size = sizeof(VTContext),
1081 };
1082
1083 AVHWAccel ff_mpeg2_videotoolbox_hwaccel = {
1084     .name           = "mpeg2_videotoolbox",
1085     .type           = AVMEDIA_TYPE_VIDEO,
1086     .id             = AV_CODEC_ID_MPEG2VIDEO,
1087     .pix_fmt        = AV_PIX_FMT_VIDEOTOOLBOX,
1088     .alloc_frame    = ff_videotoolbox_alloc_frame,
1089     .start_frame    = videotoolbox_mpeg_start_frame,
1090     .decode_slice   = videotoolbox_mpeg_decode_slice,
1091     .end_frame      = videotoolbox_mpeg_end_frame,
1092     .frame_params   = videotoolbox_frame_params,
1093     .init           = videotoolbox_common_init,
1094     .uninit         = videotoolbox_uninit,
1095     .priv_data_size = sizeof(VTContext),
1096 };
1097
1098 AVHWAccel ff_mpeg4_videotoolbox_hwaccel = {
1099     .name           = "mpeg4_videotoolbox",
1100     .type           = AVMEDIA_TYPE_VIDEO,
1101     .id             = AV_CODEC_ID_MPEG4,
1102     .pix_fmt        = AV_PIX_FMT_VIDEOTOOLBOX,
1103     .alloc_frame    = ff_videotoolbox_alloc_frame,
1104     .start_frame    = videotoolbox_mpeg_start_frame,
1105     .decode_slice   = videotoolbox_mpeg_decode_slice,
1106     .end_frame      = videotoolbox_mpeg_end_frame,
1107     .frame_params   = videotoolbox_frame_params,
1108     .init           = videotoolbox_common_init,
1109     .uninit         = videotoolbox_uninit,
1110     .priv_data_size = sizeof(VTContext),
1111 };
1112
1113 AVVideotoolboxContext *av_videotoolbox_alloc_context(void)
1114 {
1115     AVVideotoolboxContext *ret = av_mallocz(sizeof(*ret));
1116
1117     if (ret) {
1118         ret->output_callback = videotoolbox_decoder_callback;
1119         ret->cv_pix_fmt_type = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
1120     }
1121
1122     return ret;
1123 }
1124
1125 int av_videotoolbox_default_init(AVCodecContext *avctx)
1126 {
1127     return av_videotoolbox_default_init2(avctx, NULL);
1128 }
1129
1130 int av_videotoolbox_default_init2(AVCodecContext *avctx, AVVideotoolboxContext *vtctx)
1131 {
1132     avctx->hwaccel_context = vtctx ?: av_videotoolbox_alloc_context();
1133     if (!avctx->hwaccel_context)
1134         return AVERROR(ENOMEM);
1135     return videotoolbox_start(avctx);
1136 }
1137
1138 void av_videotoolbox_default_free(AVCodecContext *avctx)
1139 {
1140
1141     videotoolbox_stop(avctx);
1142     av_freep(&avctx->hwaccel_context);
1143 }
1144 #endif /* CONFIG_VIDEOTOOLBOX */