2 * VDA H.264 hardware acceleration
4 * copyright (c) 2011 Sebastien Zwickert
6 * This file is part of Libav.
8 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <CoreFoundation/CFNumber.h>
24 #include <CoreFoundation/CFData.h>
25 #include <CoreFoundation/CFString.h>
27 #include "libavutil/avutil.h"
31 /* Decoder callback that adds the VDA frame to the queue in display order. */
32 static void vda_decoder_callback(void *vda_hw_ctx,
33 CFDictionaryRef user_info,
36 CVImageBufferRef image_buffer)
38 struct vda_context *vda_ctx = vda_hw_ctx;
43 if (vda_ctx->cv_pix_fmt_type != CVPixelBufferGetPixelFormatType(image_buffer))
46 vda_ctx->cv_buffer = CVPixelBufferRetain(image_buffer);
49 static int vda_sync_decode(struct vda_context *vda_ctx)
52 CFDataRef coded_frame;
53 uint32_t flush_flags = 1 << 0; ///< kVDADecoderFlush_emitFrames
55 coded_frame = CFDataCreate(kCFAllocatorDefault,
56 vda_ctx->priv_bitstream,
57 vda_ctx->priv_bitstream_size);
59 status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, NULL);
61 if (kVDADecoderNoErr == status)
62 status = VDADecoderFlush(vda_ctx->decoder, flush_flags);
64 CFRelease(coded_frame);
70 static int vda_h264_start_frame(AVCodecContext *avctx,
71 av_unused const uint8_t *buffer,
72 av_unused uint32_t size)
74 struct vda_context *vda_ctx = avctx->hwaccel_context;
76 if (!vda_ctx->decoder)
79 vda_ctx->priv_bitstream_size = 0;
84 static int vda_h264_decode_slice(AVCodecContext *avctx,
85 const uint8_t *buffer,
88 struct vda_context *vda_ctx = avctx->hwaccel_context;
91 if (!vda_ctx->decoder)
94 tmp = av_fast_realloc(vda_ctx->priv_bitstream,
95 &vda_ctx->priv_allocated_size,
96 vda_ctx->priv_bitstream_size + size + 4);
98 return AVERROR(ENOMEM);
100 vda_ctx->priv_bitstream = tmp;
102 AV_WB32(vda_ctx->priv_bitstream + vda_ctx->priv_bitstream_size, size);
103 memcpy(vda_ctx->priv_bitstream + vda_ctx->priv_bitstream_size + 4, buffer, size);
105 vda_ctx->priv_bitstream_size += size + 4;
110 static int vda_h264_end_frame(AVCodecContext *avctx)
112 H264Context *h = avctx->priv_data;
113 struct vda_context *vda_ctx = avctx->hwaccel_context;
114 AVFrame *frame = &h->cur_pic_ptr->f;
117 if (!vda_ctx->decoder || !vda_ctx->priv_bitstream)
120 status = vda_sync_decode(vda_ctx);
121 frame->data[3] = (void*)vda_ctx->cv_buffer;
124 av_log(avctx, AV_LOG_ERROR, "Failed to decode frame (%d)\n", status);
129 int ff_vda_create_decoder(struct vda_context *vda_ctx,
133 OSStatus status = kVDADecoderNoErr;
138 CFMutableDictionaryRef config_info;
139 CFMutableDictionaryRef buffer_attributes;
140 CFMutableDictionaryRef io_surface_properties;
141 CFNumberRef cv_pix_fmt;
143 /* Each VCL NAL in the bistream sent to the decoder
144 * is preceded by a 4 bytes length header.
145 * Change the avcC atom header if needed, to signal headers of 4 bytes. */
146 if (extradata_size >= 4 && (extradata[4] & 0x03) != 0x03) {
147 uint8_t *rw_extradata;
149 if (!(rw_extradata = av_malloc(extradata_size)))
150 return AVERROR(ENOMEM);
152 memcpy(rw_extradata, extradata, extradata_size);
154 rw_extradata[4] |= 0x03;
156 avc_data = CFDataCreate(kCFAllocatorDefault, rw_extradata, extradata_size);
158 av_freep(&rw_extradata);
160 avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
163 config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
165 &kCFTypeDictionaryKeyCallBacks,
166 &kCFTypeDictionaryValueCallBacks);
168 height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
169 width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
170 format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
172 CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
173 CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
174 CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
175 CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
177 buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
179 &kCFTypeDictionaryKeyCallBacks,
180 &kCFTypeDictionaryValueCallBacks);
181 io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
183 &kCFTypeDictionaryKeyCallBacks,
184 &kCFTypeDictionaryValueCallBacks);
185 cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
187 &vda_ctx->cv_pix_fmt_type);
188 CFDictionarySetValue(buffer_attributes,
189 kCVPixelBufferPixelFormatTypeKey,
191 CFDictionarySetValue(buffer_attributes,
192 kCVPixelBufferIOSurfacePropertiesKey,
193 io_surface_properties);
195 status = VDADecoderCreate(config_info,
197 vda_decoder_callback,
205 CFRelease(config_info);
206 CFRelease(io_surface_properties);
207 CFRelease(cv_pix_fmt);
208 CFRelease(buffer_attributes);
213 int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
215 OSStatus status = kVDADecoderNoErr;
217 if (vda_ctx->decoder)
218 status = VDADecoderDestroy(vda_ctx->decoder);
220 av_freep(&vda_ctx->priv_bitstream);
225 AVHWAccel ff_h264_vda_hwaccel = {
227 .type = AVMEDIA_TYPE_VIDEO,
228 .id = AV_CODEC_ID_H264,
229 .pix_fmt = AV_PIX_FMT_VDA_VLD,
230 .start_frame = vda_h264_start_frame,
231 .decode_slice = vda_h264_decode_slice,
232 .end_frame = vda_h264_end_frame,