2 * Videotoolbox hardware acceleration
4 * copyright (c) 2012 Sebastien Zwickert
6 * This file is part of FFmpeg.
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.
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.
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
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"
33 #include "mpegvideo.h"
34 #include <TargetConditionals.h>
36 #ifndef kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder
37 # define kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder CFSTR("RequireHardwareAcceleratedVideoDecoder")
39 #ifndef kVTVideoDecoderSpecification_EnableHardwareAcceleratedVideoDecoder
40 # define kVTVideoDecoderSpecification_EnableHardwareAcceleratedVideoDecoder CFSTR("EnableHardwareAcceleratedVideoDecoder")
43 #if !HAVE_KCMVIDEOCODECTYPE_HEVC
44 enum { kCMVideoCodecType_HEVC = 'hvc1' };
47 #define VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING 12
49 typedef struct VTHWFrame {
50 CVPixelBufferRef pixbuf;
51 AVBufferRef *hw_frames_ctx;
54 static void videotoolbox_buffer_release(void *opaque, uint8_t *data)
56 VTHWFrame *ref = (VTHWFrame *)data;
57 av_buffer_unref(&ref->hw_frames_ctx);
58 CVPixelBufferRelease(ref->pixbuf);
63 static int videotoolbox_buffer_copy(VTContext *vtctx,
64 const uint8_t *buffer,
69 tmp = av_fast_realloc(vtctx->bitstream,
70 &vtctx->allocated_size,
74 return AVERROR(ENOMEM);
76 vtctx->bitstream = tmp;
77 memcpy(vtctx->bitstream, buffer, size);
78 vtctx->bitstream_size = size;
83 static int videotoolbox_postproc_frame(void *avctx, AVFrame *frame)
85 VTHWFrame *ref = (VTHWFrame *)frame->buf[0]->data;
88 av_log(avctx, AV_LOG_ERROR, "No frame decoded?\n");
89 av_frame_unref(frame);
90 return AVERROR_EXTERNAL;
93 frame->data[3] = (uint8_t*)ref->pixbuf;
95 if (ref->hw_frames_ctx) {
96 av_buffer_unref(&frame->hw_frames_ctx);
97 frame->hw_frames_ctx = av_buffer_ref(ref->hw_frames_ctx);
98 if (!frame->hw_frames_ctx)
99 return AVERROR(ENOMEM);
105 int ff_videotoolbox_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
107 size_t size = sizeof(VTHWFrame);
108 uint8_t *data = NULL;
109 AVBufferRef *buf = NULL;
110 int ret = ff_attach_decode_data(frame);
111 FrameDecodeData *fdd;
115 data = av_mallocz(size);
117 return AVERROR(ENOMEM);
118 buf = av_buffer_create(data, size, videotoolbox_buffer_release, NULL, 0);
121 return AVERROR(ENOMEM);
125 fdd = (FrameDecodeData*)frame->private_ref->data;
126 fdd->post_process = videotoolbox_postproc_frame;
128 frame->width = avctx->width;
129 frame->height = avctx->height;
130 frame->format = avctx->pix_fmt;
135 #define AV_W8(p, v) *(p) = (v)
137 CFDataRef ff_videotoolbox_avcc_extradata_create(AVCodecContext *avctx)
139 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
140 H264Context *h = avctx->priv_data;
141 CFDataRef data = NULL;
143 int vt_extradata_size = 6 + 2 + h->ps.sps->data_size + 3 + h->ps.pps->data_size;
144 uint8_t *vt_extradata = av_malloc(vt_extradata_size);
150 AV_W8(p + 0, 1); /* version */
151 AV_W8(p + 1, h->ps.sps->data[1]); /* profile */
152 AV_W8(p + 2, h->ps.sps->data[2]); /* profile compat */
153 AV_W8(p + 3, h->ps.sps->data[3]); /* level */
154 AV_W8(p + 4, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 3 (11) */
155 AV_W8(p + 5, 0xe1); /* 3 bits reserved (111) + 5 bits number of sps (00001) */
156 AV_WB16(p + 6, h->ps.sps->data_size);
157 memcpy(p + 8, h->ps.sps->data, h->ps.sps->data_size);
158 p += 8 + h->ps.sps->data_size;
159 AV_W8(p + 0, 1); /* number of pps */
160 AV_WB16(p + 1, h->ps.pps->data_size);
161 memcpy(p + 3, h->ps.pps->data, h->ps.pps->data_size);
163 p += 3 + h->ps.pps->data_size;
164 av_assert0(p - vt_extradata == vt_extradata_size);
166 // save sps header (profile/level) used to create decoder session,
167 // so we can detect changes and recreate it.
169 memcpy(vtctx->sps, h->ps.sps->data + 1, 3);
171 data = CFDataCreate(kCFAllocatorDefault, vt_extradata, vt_extradata_size);
172 av_free(vt_extradata);
176 CFDataRef ff_videotoolbox_hvcc_extradata_create(AVCodecContext *avctx)
178 HEVCContext *h = avctx->priv_data;
179 const HEVCVPS *vps = (const HEVCVPS *)h->ps.vps_list[0]->data;
180 const HEVCSPS *sps = (const HEVCSPS *)h->ps.sps_list[0]->data;
182 const HEVCPPS *pps = h->ps.pps;
183 PTLCommon ptlc = vps->ptl.general_ptl;
185 uint8_t parallelismType;
186 CFDataRef data = NULL;
188 int vt_extradata_size = 23 + 5 + vps->data_size + 5 + sps->data_size + 3;
189 uint8_t *vt_extradata;
191 for (i = 0; i < HEVC_MAX_PPS_COUNT; i++) {
192 if (h->ps.pps_list[i]) {
193 const HEVCPPS *pps = (const HEVCPPS *)h->ps.pps_list[i]->data;
194 vt_extradata_size += 2 + pps->data_size;
199 vt_extradata = av_malloc(vt_extradata_size);
204 /* unsigned int(8) configurationVersion = 1; */
208 * unsigned int(2) general_profile_space;
209 * unsigned int(1) general_tier_flag;
210 * unsigned int(5) general_profile_idc;
212 AV_W8(p + 1, ptlc.profile_space << 6 |
213 ptlc.tier_flag << 5 |
216 /* unsigned int(32) general_profile_compatibility_flags; */
217 memcpy(p + 2, ptlc.profile_compatibility_flag, 4);
219 /* unsigned int(48) general_constraint_indicator_flags; */
220 AV_W8(p + 6, ptlc.progressive_source_flag << 7 |
221 ptlc.interlaced_source_flag << 6 |
222 ptlc.non_packed_constraint_flag << 5 |
223 ptlc.frame_only_constraint_flag << 4);
227 /* unsigned int(8) general_level_idc; */
228 AV_W8(p + 12, ptlc.level_idc);
231 * bit(4) reserved = ‘1111’b;
232 * unsigned int(12) min_spatial_segmentation_idc;
234 AV_W8(p + 13, 0xf0 | (vui.min_spatial_segmentation_idc >> 4));
235 AV_W8(p + 14, vui.min_spatial_segmentation_idc & 0xff);
238 * bit(6) reserved = ‘111111’b;
239 * unsigned int(2) parallelismType;
241 if (!vui.min_spatial_segmentation_idc)
243 else if (pps->entropy_coding_sync_enabled_flag && pps->tiles_enabled_flag)
245 else if (pps->entropy_coding_sync_enabled_flag)
247 else if (pps->tiles_enabled_flag)
251 AV_W8(p + 15, 0xfc | parallelismType);
254 * bit(6) reserved = ‘111111’b;
255 * unsigned int(2) chromaFormat;
257 AV_W8(p + 16, sps->chroma_format_idc | 0xfc);
260 * bit(5) reserved = ‘11111’b;
261 * unsigned int(3) bitDepthLumaMinus8;
263 AV_W8(p + 17, (sps->bit_depth - 8) | 0xfc);
266 * bit(5) reserved = ‘11111’b;
267 * unsigned int(3) bitDepthChromaMinus8;
269 AV_W8(p + 18, (sps->bit_depth_chroma - 8) | 0xfc);
271 /* bit(16) avgFrameRate; */
275 * bit(2) constantFrameRate;
276 * bit(3) numTemporalLayers;
277 * bit(1) temporalIdNested;
278 * unsigned int(2) lengthSizeMinusOne;
280 AV_W8(p + 21, 0 << 6 |
281 sps->max_sub_layers << 3 |
282 sps->temporal_id_nesting_flag << 2 |
285 /* unsigned int(8) numOfArrays; */
291 * bit(1) array_completeness;
292 * unsigned int(1) reserved = 0;
293 * unsigned int(6) NAL_unit_type;
296 HEVC_NAL_VPS & 0x3f);
297 /* unsigned int(16) numNalus; */
299 /* unsigned int(16) nalUnitLength; */
300 AV_WB16(p + 3, vps->data_size);
301 /* bit(8*nalUnitLength) nalUnit; */
302 memcpy(p + 5, vps->data, vps->data_size);
303 p += 5 + vps->data_size;
307 HEVC_NAL_SPS & 0x3f);
309 AV_WB16(p + 3, sps->data_size);
310 memcpy(p + 5, sps->data, sps->data_size);
311 p += 5 + sps->data_size;
315 HEVC_NAL_PPS & 0x3f);
316 AV_WB16(p + 1, num_pps);
318 for (i = 0; i < HEVC_MAX_PPS_COUNT; i++) {
319 if (h->ps.pps_list[i]) {
320 const HEVCPPS *pps = (const HEVCPPS *)h->ps.pps_list[i]->data;
321 AV_WB16(p, pps->data_size);
322 memcpy(p + 2, pps->data, pps->data_size);
323 p += 2 + pps->data_size;
327 av_assert0(p - vt_extradata == vt_extradata_size);
329 data = CFDataCreate(kCFAllocatorDefault, vt_extradata, vt_extradata_size);
330 av_free(vt_extradata);
334 int ff_videotoolbox_h264_start_frame(AVCodecContext *avctx,
335 const uint8_t *buffer,
338 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
339 H264Context *h = avctx->priv_data;
341 if (h->is_avc == 1) {
342 return videotoolbox_buffer_copy(vtctx, buffer, size);
348 static int videotoolbox_h264_decode_params(AVCodecContext *avctx,
350 const uint8_t *buffer,
353 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
354 H264Context *h = avctx->priv_data;
356 // save sps header (profile/level) used to create decoder session
358 memcpy(vtctx->sps, h->ps.sps->data + 1, 3);
360 if (type == H264_NAL_SPS) {
361 if (size > 4 && memcmp(vtctx->sps, buffer + 1, 3) != 0) {
362 vtctx->reconfig_needed = true;
363 memcpy(vtctx->sps, buffer + 1, 3);
367 // pass-through SPS/PPS changes to the decoder
368 return ff_videotoolbox_h264_decode_slice(avctx, buffer, size);
371 static int videotoolbox_common_decode_slice(AVCodecContext *avctx,
372 const uint8_t *buffer,
375 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
378 tmp = av_fast_realloc(vtctx->bitstream,
379 &vtctx->allocated_size,
380 vtctx->bitstream_size+size+4);
382 return AVERROR(ENOMEM);
384 vtctx->bitstream = tmp;
386 AV_WB32(vtctx->bitstream + vtctx->bitstream_size, size);
387 memcpy(vtctx->bitstream + vtctx->bitstream_size + 4, buffer, size);
389 vtctx->bitstream_size += size + 4;
394 int ff_videotoolbox_h264_decode_slice(AVCodecContext *avctx,
395 const uint8_t *buffer,
398 H264Context *h = avctx->priv_data;
403 return videotoolbox_common_decode_slice(avctx, buffer, size);
406 int ff_videotoolbox_uninit(AVCodecContext *avctx)
408 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
410 av_freep(&vtctx->bitstream);
412 CVPixelBufferRelease(vtctx->frame);
418 #if CONFIG_VIDEOTOOLBOX
419 // Return the AVVideotoolboxContext that matters currently. Where it comes from
420 // depends on the API used.
421 static AVVideotoolboxContext *videotoolbox_get_context(AVCodecContext *avctx)
423 // Somewhat tricky because the user can call av_videotoolbox_default_free()
424 // at any time, even when the codec is closed.
425 if (avctx->internal && avctx->internal->hwaccel_priv_data) {
426 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
428 return vtctx->vt_ctx;
430 return avctx->hwaccel_context;
433 static int videotoolbox_buffer_create(AVCodecContext *avctx, AVFrame *frame)
435 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
436 CVPixelBufferRef pixbuf = (CVPixelBufferRef)vtctx->frame;
437 OSType pixel_format = CVPixelBufferGetPixelFormatType(pixbuf);
438 enum AVPixelFormat sw_format = av_map_videotoolbox_format_to_pixfmt(pixel_format);
439 int width = CVPixelBufferGetWidth(pixbuf);
440 int height = CVPixelBufferGetHeight(pixbuf);
441 AVHWFramesContext *cached_frames;
445 if (!frame->buf[0] || frame->data[3]) {
446 av_log(avctx, AV_LOG_ERROR, "videotoolbox: invalid state\n");
447 av_frame_unref(frame);
448 return AVERROR_EXTERNAL;
451 ref = (VTHWFrame *)frame->buf[0]->data;
454 CVPixelBufferRelease(ref->pixbuf);
455 ref->pixbuf = vtctx->frame;
458 // Old API code path.
459 if (!vtctx->cached_hw_frames_ctx)
462 cached_frames = (AVHWFramesContext*)vtctx->cached_hw_frames_ctx->data;
464 if (cached_frames->sw_format != sw_format ||
465 cached_frames->width != width ||
466 cached_frames->height != height) {
467 AVBufferRef *hw_frames_ctx = av_hwframe_ctx_alloc(cached_frames->device_ref);
468 AVHWFramesContext *hw_frames;
470 return AVERROR(ENOMEM);
472 hw_frames = (AVHWFramesContext*)hw_frames_ctx->data;
473 hw_frames->format = cached_frames->format;
474 hw_frames->sw_format = sw_format;
475 hw_frames->width = width;
476 hw_frames->height = height;
478 ret = av_hwframe_ctx_init(hw_frames_ctx);
480 av_buffer_unref(&hw_frames_ctx);
484 av_buffer_unref(&vtctx->cached_hw_frames_ctx);
485 vtctx->cached_hw_frames_ctx = hw_frames_ctx;
488 av_buffer_unref(&ref->hw_frames_ctx);
489 ref->hw_frames_ctx = av_buffer_ref(vtctx->cached_hw_frames_ctx);
490 if (!ref->hw_frames_ctx)
491 return AVERROR(ENOMEM);
496 static void videotoolbox_write_mp4_descr_length(PutByteContext *pb, int length)
501 for (i = 3; i >= 0; i--) {
502 b = (length >> (i * 7)) & 0x7F;
506 bytestream2_put_byteu(pb, b);
510 static CFDataRef videotoolbox_esds_extradata_create(AVCodecContext *avctx)
513 uint8_t *rw_extradata;
515 int full_size = 3 + 5 + 13 + 5 + avctx->extradata_size + 3;
516 // ES_DescrTag data + DecoderConfigDescrTag + data + DecSpecificInfoTag + size + SLConfigDescriptor
517 int config_size = 13 + 5 + avctx->extradata_size;
520 if (!(rw_extradata = av_mallocz(full_size + VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING)))
523 bytestream2_init_writer(&pb, rw_extradata, full_size + VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING);
524 bytestream2_put_byteu(&pb, 0); // version
525 bytestream2_put_ne24(&pb, 0); // flags
527 // elementary stream descriptor
528 bytestream2_put_byteu(&pb, 0x03); // ES_DescrTag
529 videotoolbox_write_mp4_descr_length(&pb, full_size);
530 bytestream2_put_ne16(&pb, 0); // esid
531 bytestream2_put_byteu(&pb, 0); // stream priority (0-32)
533 // decoder configuration descriptor
534 bytestream2_put_byteu(&pb, 0x04); // DecoderConfigDescrTag
535 videotoolbox_write_mp4_descr_length(&pb, config_size);
536 bytestream2_put_byteu(&pb, 32); // object type indication. 32 = AV_CODEC_ID_MPEG4
537 bytestream2_put_byteu(&pb, 0x11); // stream type
538 bytestream2_put_ne24(&pb, 0); // buffer size
539 bytestream2_put_ne32(&pb, 0); // max bitrate
540 bytestream2_put_ne32(&pb, 0); // avg bitrate
542 // decoder specific descriptor
543 bytestream2_put_byteu(&pb, 0x05); ///< DecSpecificInfoTag
544 videotoolbox_write_mp4_descr_length(&pb, avctx->extradata_size);
546 bytestream2_put_buffer(&pb, avctx->extradata, avctx->extradata_size);
548 // SLConfigDescriptor
549 bytestream2_put_byteu(&pb, 0x06); // SLConfigDescrTag
550 bytestream2_put_byteu(&pb, 0x01); // length
551 bytestream2_put_byteu(&pb, 0x02); //
553 s = bytestream2_size_p(&pb);
555 data = CFDataCreate(kCFAllocatorDefault, rw_extradata, s);
557 av_freep(&rw_extradata);
561 static CMSampleBufferRef videotoolbox_sample_buffer_create(CMFormatDescriptionRef fmt_desc,
566 CMBlockBufferRef block_buf;
567 CMSampleBufferRef sample_buf;
572 status = CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault,// structureAllocator
573 buffer, // memoryBlock
575 kCFAllocatorNull, // blockAllocator
576 NULL, // customBlockSource
583 status = CMSampleBufferCreate(kCFAllocatorDefault, // allocator
584 block_buf, // dataBuffer
586 0, // makeDataReadyCallback
587 0, // makeDataReadyRefcon
588 fmt_desc, // formatDescription
590 0, // numSampleTimingEntries
591 NULL, // sampleTimingArray
592 0, // numSampleSizeEntries
593 NULL, // sampleSizeArray
598 CFRelease(block_buf);
603 static void videotoolbox_decoder_callback(void *opaque,
604 void *sourceFrameRefCon,
606 VTDecodeInfoFlags flags,
607 CVImageBufferRef image_buffer,
611 AVCodecContext *avctx = opaque;
612 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
615 CVPixelBufferRelease(vtctx->frame);
620 av_log(NULL, AV_LOG_DEBUG, "vt decoder cb: output image buffer is null\n");
624 vtctx->frame = CVPixelBufferRetain(image_buffer);
627 static OSStatus videotoolbox_session_decode_frame(AVCodecContext *avctx)
630 CMSampleBufferRef sample_buf;
631 AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
632 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
634 sample_buf = videotoolbox_sample_buffer_create(videotoolbox->cm_fmt_desc,
636 vtctx->bitstream_size);
641 status = VTDecompressionSessionDecodeFrame(videotoolbox->session,
644 NULL, // sourceFrameRefCon
647 status = VTDecompressionSessionWaitForAsynchronousFrames(videotoolbox->session);
649 CFRelease(sample_buf);
654 static CMVideoFormatDescriptionRef videotoolbox_format_desc_create(CMVideoCodecType codec_type,
655 CFDictionaryRef decoder_spec,
659 CMFormatDescriptionRef cm_fmt_desc;
662 status = CMVideoFormatDescriptionCreate(kCFAllocatorDefault,
666 decoder_spec, // Dictionary of extension
675 static CFDictionaryRef videotoolbox_buffer_attributes_create(int width,
679 CFMutableDictionaryRef buffer_attributes;
680 CFMutableDictionaryRef io_surface_properties;
681 CFNumberRef cv_pix_fmt;
685 w = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &width);
686 h = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &height);
687 cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &pix_fmt);
689 buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
691 &kCFTypeDictionaryKeyCallBacks,
692 &kCFTypeDictionaryValueCallBacks);
693 io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
695 &kCFTypeDictionaryKeyCallBacks,
696 &kCFTypeDictionaryValueCallBacks);
699 CFDictionarySetValue(buffer_attributes, kCVPixelBufferPixelFormatTypeKey, cv_pix_fmt);
700 CFDictionarySetValue(buffer_attributes, kCVPixelBufferIOSurfacePropertiesKey, io_surface_properties);
701 CFDictionarySetValue(buffer_attributes, kCVPixelBufferWidthKey, w);
702 CFDictionarySetValue(buffer_attributes, kCVPixelBufferHeightKey, h);
704 CFDictionarySetValue(buffer_attributes, kCVPixelBufferOpenGLESCompatibilityKey, kCFBooleanTrue);
706 CFDictionarySetValue(buffer_attributes, kCVPixelBufferIOSurfaceOpenGLTextureCompatibilityKey, kCFBooleanTrue);
709 CFRelease(io_surface_properties);
710 CFRelease(cv_pix_fmt);
714 return buffer_attributes;
717 static CFDictionaryRef videotoolbox_decoder_config_create(CMVideoCodecType codec_type,
718 AVCodecContext *avctx)
720 CFMutableDictionaryRef config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
722 &kCFTypeDictionaryKeyCallBacks,
723 &kCFTypeDictionaryValueCallBacks);
725 CFDictionarySetValue(config_info,
726 codec_type == kCMVideoCodecType_HEVC ?
727 kVTVideoDecoderSpecification_EnableHardwareAcceleratedVideoDecoder :
728 kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder,
731 CFMutableDictionaryRef avc_info;
732 CFDataRef data = NULL;
734 avc_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
736 &kCFTypeDictionaryKeyCallBacks,
737 &kCFTypeDictionaryValueCallBacks);
739 switch (codec_type) {
740 case kCMVideoCodecType_MPEG4Video :
741 if (avctx->extradata_size)
742 data = videotoolbox_esds_extradata_create(avctx);
744 CFDictionarySetValue(avc_info, CFSTR("esds"), data);
746 case kCMVideoCodecType_H264 :
747 data = ff_videotoolbox_avcc_extradata_create(avctx);
749 CFDictionarySetValue(avc_info, CFSTR("avcC"), data);
751 case kCMVideoCodecType_HEVC :
752 data = ff_videotoolbox_hvcc_extradata_create(avctx);
754 CFDictionarySetValue(avc_info, CFSTR("hvcC"), data);
760 CFDictionarySetValue(config_info,
761 kCMFormatDescriptionExtension_SampleDescriptionExtensionAtoms,
771 static int videotoolbox_start(AVCodecContext *avctx)
773 AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
775 VTDecompressionOutputCallbackRecord decoder_cb;
776 CFDictionaryRef decoder_spec;
777 CFDictionaryRef buf_attr;
780 av_log(avctx, AV_LOG_ERROR, "hwaccel context is not set\n");
784 switch( avctx->codec_id ) {
785 case AV_CODEC_ID_H263 :
786 videotoolbox->cm_codec_type = kCMVideoCodecType_H263;
788 case AV_CODEC_ID_H264 :
789 videotoolbox->cm_codec_type = kCMVideoCodecType_H264;
791 case AV_CODEC_ID_HEVC :
792 videotoolbox->cm_codec_type = kCMVideoCodecType_HEVC;
794 case AV_CODEC_ID_MPEG1VIDEO :
795 videotoolbox->cm_codec_type = kCMVideoCodecType_MPEG1Video;
797 case AV_CODEC_ID_MPEG2VIDEO :
798 videotoolbox->cm_codec_type = kCMVideoCodecType_MPEG2Video;
800 case AV_CODEC_ID_MPEG4 :
801 videotoolbox->cm_codec_type = kCMVideoCodecType_MPEG4Video;
807 decoder_spec = videotoolbox_decoder_config_create(videotoolbox->cm_codec_type, avctx);
810 av_log(avctx, AV_LOG_ERROR, "decoder specification creation failed\n");
814 videotoolbox->cm_fmt_desc = videotoolbox_format_desc_create(videotoolbox->cm_codec_type,
818 if (!videotoolbox->cm_fmt_desc) {
820 CFRelease(decoder_spec);
822 av_log(avctx, AV_LOG_ERROR, "format description creation failed\n");
826 buf_attr = videotoolbox_buffer_attributes_create(avctx->width,
828 videotoolbox->cv_pix_fmt_type);
830 decoder_cb.decompressionOutputCallback = videotoolbox_decoder_callback;
831 decoder_cb.decompressionOutputRefCon = avctx;
833 status = VTDecompressionSessionCreate(NULL, // allocator
834 videotoolbox->cm_fmt_desc, // videoFormatDescription
835 decoder_spec, // videoDecoderSpecification
836 buf_attr, // destinationImageBufferAttributes
837 &decoder_cb, // outputCallback
838 &videotoolbox->session); // decompressionSessionOut
841 CFRelease(decoder_spec);
846 case kVTVideoDecoderNotAvailableNowErr:
847 av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox session not available.\n");
848 return AVERROR(ENOSYS);
849 case kVTVideoDecoderUnsupportedDataFormatErr:
850 av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox does not support this format.\n");
851 return AVERROR(ENOSYS);
852 case kVTCouldNotFindVideoDecoderErr:
853 av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox decoder for this format not found.\n");
854 return AVERROR(ENOSYS);
855 case kVTVideoDecoderMalfunctionErr:
856 av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox malfunction.\n");
857 return AVERROR(EINVAL);
858 case kVTVideoDecoderBadDataErr:
859 av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox reported invalid data.\n");
860 return AVERROR_INVALIDDATA;
864 av_log(avctx, AV_LOG_VERBOSE, "Unknown VideoToolbox session creation error %d\n", (int)status);
865 return AVERROR_UNKNOWN;
869 static void videotoolbox_stop(AVCodecContext *avctx)
871 AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
875 if (videotoolbox->cm_fmt_desc) {
876 CFRelease(videotoolbox->cm_fmt_desc);
877 videotoolbox->cm_fmt_desc = NULL;
880 if (videotoolbox->session) {
881 VTDecompressionSessionInvalidate(videotoolbox->session);
882 CFRelease(videotoolbox->session);
883 videotoolbox->session = NULL;
887 static const char *videotoolbox_error_string(OSStatus status)
890 case kVTVideoDecoderBadDataErr:
892 case kVTVideoDecoderMalfunctionErr:
893 return "decoder malfunction";
894 case kVTInvalidSessionErr:
895 return "invalid session";
900 static int videotoolbox_common_end_frame(AVCodecContext *avctx, AVFrame *frame)
903 AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
904 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
906 if (vtctx->reconfig_needed == true) {
907 vtctx->reconfig_needed = false;
908 av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox decoder needs reconfig, restarting..\n");
909 videotoolbox_stop(avctx);
910 if (videotoolbox_start(avctx) != 0) {
911 return AVERROR_EXTERNAL;
915 if (!videotoolbox->session || !vtctx->bitstream || !vtctx->bitstream_size)
916 return AVERROR_INVALIDDATA;
918 status = videotoolbox_session_decode_frame(avctx);
919 if (status != noErr) {
920 if (status == kVTVideoDecoderMalfunctionErr || status == kVTInvalidSessionErr)
921 vtctx->reconfig_needed = true;
922 av_log(avctx, AV_LOG_ERROR, "Failed to decode frame (%s, %d)\n", videotoolbox_error_string(status), (int)status);
923 return AVERROR_UNKNOWN;
927 vtctx->reconfig_needed = true;
928 return AVERROR_UNKNOWN;
931 return videotoolbox_buffer_create(avctx, frame);
934 static int videotoolbox_h264_end_frame(AVCodecContext *avctx)
936 H264Context *h = avctx->priv_data;
937 AVFrame *frame = h->cur_pic_ptr->f;
938 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
939 int ret = videotoolbox_common_end_frame(avctx, frame);
940 vtctx->bitstream_size = 0;
944 static int videotoolbox_hevc_start_frame(AVCodecContext *avctx,
945 const uint8_t *buffer,
951 static int videotoolbox_hevc_decode_slice(AVCodecContext *avctx,
952 const uint8_t *buffer,
955 return videotoolbox_common_decode_slice(avctx, buffer, size);
959 static int videotoolbox_hevc_decode_params(AVCodecContext *avctx,
961 const uint8_t *buffer,
964 return videotoolbox_common_decode_slice(avctx, buffer, size);
967 static int videotoolbox_hevc_end_frame(AVCodecContext *avctx)
969 HEVCContext *h = avctx->priv_data;
970 AVFrame *frame = h->ref->frame;
971 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
972 int ret = videotoolbox_common_end_frame(avctx, frame);
973 vtctx->bitstream_size = 0;
977 static int videotoolbox_mpeg_start_frame(AVCodecContext *avctx,
978 const uint8_t *buffer,
981 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
983 return videotoolbox_buffer_copy(vtctx, buffer, size);
986 static int videotoolbox_mpeg_decode_slice(AVCodecContext *avctx,
987 const uint8_t *buffer,
993 static int videotoolbox_mpeg_end_frame(AVCodecContext *avctx)
995 MpegEncContext *s = avctx->priv_data;
996 AVFrame *frame = s->current_picture_ptr->f;
998 return videotoolbox_common_end_frame(avctx, frame);
1001 static int videotoolbox_uninit(AVCodecContext *avctx)
1003 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
1007 ff_videotoolbox_uninit(avctx);
1010 videotoolbox_stop(avctx);
1012 av_buffer_unref(&vtctx->cached_hw_frames_ctx);
1013 av_freep(&vtctx->vt_ctx);
1018 static int videotoolbox_common_init(AVCodecContext *avctx)
1020 VTContext *vtctx = avctx->internal->hwaccel_priv_data;
1021 AVHWFramesContext *hw_frames;
1024 // Old API - do nothing.
1025 if (avctx->hwaccel_context)
1028 if (!avctx->hw_frames_ctx && !avctx->hw_device_ctx) {
1029 av_log(avctx, AV_LOG_ERROR,
1030 "Either hw_frames_ctx or hw_device_ctx must be set.\n");
1031 return AVERROR(EINVAL);
1034 vtctx->vt_ctx = av_videotoolbox_alloc_context();
1035 if (!vtctx->vt_ctx) {
1036 err = AVERROR(ENOMEM);
1040 if (avctx->hw_frames_ctx) {
1041 hw_frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
1043 avctx->hw_frames_ctx = av_hwframe_ctx_alloc(avctx->hw_device_ctx);
1044 if (!avctx->hw_frames_ctx) {
1045 err = AVERROR(ENOMEM);
1049 hw_frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
1050 hw_frames->format = AV_PIX_FMT_VIDEOTOOLBOX;
1051 hw_frames->sw_format = AV_PIX_FMT_NV12; // same as av_videotoolbox_alloc_context()
1052 hw_frames->width = avctx->width;
1053 hw_frames->height = avctx->height;
1055 err = av_hwframe_ctx_init(avctx->hw_frames_ctx);
1057 av_buffer_unref(&avctx->hw_frames_ctx);
1062 vtctx->cached_hw_frames_ctx = av_buffer_ref(avctx->hw_frames_ctx);
1063 if (!vtctx->cached_hw_frames_ctx) {
1064 err = AVERROR(ENOMEM);
1068 vtctx->vt_ctx->cv_pix_fmt_type =
1069 av_map_videotoolbox_format_from_pixfmt(hw_frames->sw_format);
1070 if (!vtctx->vt_ctx->cv_pix_fmt_type) {
1071 av_log(avctx, AV_LOG_ERROR, "Unknown sw_format.\n");
1072 err = AVERROR(EINVAL);
1076 err = videotoolbox_start(avctx);
1083 videotoolbox_uninit(avctx);
1087 static int videotoolbox_frame_params(AVCodecContext *avctx,
1088 AVBufferRef *hw_frames_ctx)
1090 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)hw_frames_ctx->data;
1092 frames_ctx->format = AV_PIX_FMT_VIDEOTOOLBOX;
1093 frames_ctx->width = avctx->coded_width;
1094 frames_ctx->height = avctx->coded_height;
1095 frames_ctx->sw_format = AV_PIX_FMT_NV12;
1100 const AVHWAccel ff_h263_videotoolbox_hwaccel = {
1101 .name = "h263_videotoolbox",
1102 .type = AVMEDIA_TYPE_VIDEO,
1103 .id = AV_CODEC_ID_H263,
1104 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
1105 .alloc_frame = ff_videotoolbox_alloc_frame,
1106 .start_frame = videotoolbox_mpeg_start_frame,
1107 .decode_slice = videotoolbox_mpeg_decode_slice,
1108 .end_frame = videotoolbox_mpeg_end_frame,
1109 .frame_params = videotoolbox_frame_params,
1110 .init = videotoolbox_common_init,
1111 .uninit = videotoolbox_uninit,
1112 .priv_data_size = sizeof(VTContext),
1115 const AVHWAccel ff_hevc_videotoolbox_hwaccel = {
1116 .name = "hevc_videotoolbox",
1117 .type = AVMEDIA_TYPE_VIDEO,
1118 .id = AV_CODEC_ID_HEVC,
1119 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
1120 .alloc_frame = ff_videotoolbox_alloc_frame,
1121 .start_frame = videotoolbox_hevc_start_frame,
1122 .decode_slice = videotoolbox_hevc_decode_slice,
1123 .decode_params = videotoolbox_hevc_decode_params,
1124 .end_frame = videotoolbox_hevc_end_frame,
1125 .frame_params = videotoolbox_frame_params,
1126 .init = videotoolbox_common_init,
1127 .uninit = ff_videotoolbox_uninit,
1128 .priv_data_size = sizeof(VTContext),
1131 const AVHWAccel ff_h264_videotoolbox_hwaccel = {
1132 .name = "h264_videotoolbox",
1133 .type = AVMEDIA_TYPE_VIDEO,
1134 .id = AV_CODEC_ID_H264,
1135 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
1136 .alloc_frame = ff_videotoolbox_alloc_frame,
1137 .start_frame = ff_videotoolbox_h264_start_frame,
1138 .decode_slice = ff_videotoolbox_h264_decode_slice,
1139 .decode_params = videotoolbox_h264_decode_params,
1140 .end_frame = videotoolbox_h264_end_frame,
1141 .frame_params = videotoolbox_frame_params,
1142 .init = videotoolbox_common_init,
1143 .uninit = videotoolbox_uninit,
1144 .priv_data_size = sizeof(VTContext),
1147 const AVHWAccel ff_mpeg1_videotoolbox_hwaccel = {
1148 .name = "mpeg1_videotoolbox",
1149 .type = AVMEDIA_TYPE_VIDEO,
1150 .id = AV_CODEC_ID_MPEG1VIDEO,
1151 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
1152 .alloc_frame = ff_videotoolbox_alloc_frame,
1153 .start_frame = videotoolbox_mpeg_start_frame,
1154 .decode_slice = videotoolbox_mpeg_decode_slice,
1155 .end_frame = videotoolbox_mpeg_end_frame,
1156 .frame_params = videotoolbox_frame_params,
1157 .init = videotoolbox_common_init,
1158 .uninit = videotoolbox_uninit,
1159 .priv_data_size = sizeof(VTContext),
1162 const AVHWAccel ff_mpeg2_videotoolbox_hwaccel = {
1163 .name = "mpeg2_videotoolbox",
1164 .type = AVMEDIA_TYPE_VIDEO,
1165 .id = AV_CODEC_ID_MPEG2VIDEO,
1166 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
1167 .alloc_frame = ff_videotoolbox_alloc_frame,
1168 .start_frame = videotoolbox_mpeg_start_frame,
1169 .decode_slice = videotoolbox_mpeg_decode_slice,
1170 .end_frame = videotoolbox_mpeg_end_frame,
1171 .frame_params = videotoolbox_frame_params,
1172 .init = videotoolbox_common_init,
1173 .uninit = videotoolbox_uninit,
1174 .priv_data_size = sizeof(VTContext),
1177 const AVHWAccel ff_mpeg4_videotoolbox_hwaccel = {
1178 .name = "mpeg4_videotoolbox",
1179 .type = AVMEDIA_TYPE_VIDEO,
1180 .id = AV_CODEC_ID_MPEG4,
1181 .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
1182 .alloc_frame = ff_videotoolbox_alloc_frame,
1183 .start_frame = videotoolbox_mpeg_start_frame,
1184 .decode_slice = videotoolbox_mpeg_decode_slice,
1185 .end_frame = videotoolbox_mpeg_end_frame,
1186 .frame_params = videotoolbox_frame_params,
1187 .init = videotoolbox_common_init,
1188 .uninit = videotoolbox_uninit,
1189 .priv_data_size = sizeof(VTContext),
1192 AVVideotoolboxContext *av_videotoolbox_alloc_context(void)
1194 AVVideotoolboxContext *ret = av_mallocz(sizeof(*ret));
1197 ret->output_callback = videotoolbox_decoder_callback;
1198 ret->cv_pix_fmt_type = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
1204 int av_videotoolbox_default_init(AVCodecContext *avctx)
1206 return av_videotoolbox_default_init2(avctx, NULL);
1209 int av_videotoolbox_default_init2(AVCodecContext *avctx, AVVideotoolboxContext *vtctx)
1211 avctx->hwaccel_context = vtctx ?: av_videotoolbox_alloc_context();
1212 if (!avctx->hwaccel_context)
1213 return AVERROR(ENOMEM);
1214 return videotoolbox_start(avctx);
1217 void av_videotoolbox_default_free(AVCodecContext *avctx)
1220 videotoolbox_stop(avctx);
1221 av_freep(&avctx->hwaccel_context);
1223 #endif /* CONFIG_VIDEOTOOLBOX */