2 * VDA H264 HW acceleration.
4 * copyright (c) 2011 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
23 #include <CoreFoundation/CFDictionary.h>
24 #include <CoreFoundation/CFNumber.h>
25 #include <CoreFoundation/CFData.h>
28 #include "libavutil/avutil.h"
32 CVPixelBufferRef cv_buffer;
35 /* Decoder callback that adds the vda frame to the queue in display order. */
36 static void vda_decoder_callback (void *vda_hw_ctx,
37 CFDictionaryRef user_info,
40 CVImageBufferRef image_buffer)
42 struct vda_context *vda_ctx = vda_hw_ctx;
47 if (vda_ctx->cv_pix_fmt_type != CVPixelBufferGetPixelFormatType(image_buffer))
50 vda_ctx->cv_buffer = CVPixelBufferRetain(image_buffer);
53 static int vda_sync_decode(struct vda_context *vda_ctx)
56 CFDataRef coded_frame;
57 uint32_t flush_flags = 1 << 0; ///< kVDADecoderFlush_emitFrames
59 coded_frame = CFDataCreate(kCFAllocatorDefault,
60 vda_ctx->priv_bitstream,
61 vda_ctx->priv_bitstream_size);
63 status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, NULL);
65 if (kVDADecoderNoErr == status)
66 status = VDADecoderFlush(vda_ctx->decoder, flush_flags);
68 CFRelease(coded_frame);
74 static int vda_h264_start_frame(AVCodecContext *avctx,
75 av_unused const uint8_t *buffer,
76 av_unused uint32_t size)
78 struct vda_context *vda_ctx = avctx->hwaccel_context;
80 if (!vda_ctx->decoder)
83 vda_ctx->priv_bitstream_size = 0;
88 static int vda_h264_decode_slice(AVCodecContext *avctx,
89 const uint8_t *buffer,
92 struct vda_context *vda_ctx = avctx->hwaccel_context;
95 if (!vda_ctx->decoder)
98 tmp = av_fast_realloc(vda_ctx->priv_bitstream,
99 &vda_ctx->priv_allocated_size,
100 vda_ctx->priv_bitstream_size + size + 4);
102 return AVERROR(ENOMEM);
104 vda_ctx->priv_bitstream = tmp;
106 AV_WB32(vda_ctx->priv_bitstream + vda_ctx->priv_bitstream_size, size);
107 memcpy(vda_ctx->priv_bitstream + vda_ctx->priv_bitstream_size + 4, buffer, size);
109 vda_ctx->priv_bitstream_size += size + 4;
114 static void vda_h264_release_buffer(void *opaque, uint8_t *data)
116 struct vda_buffer *context = opaque;
117 CVPixelBufferRelease(context->cv_buffer);
121 static int vda_h264_end_frame(AVCodecContext *avctx)
123 H264Context *h = avctx->priv_data;
124 struct vda_context *vda_ctx = avctx->hwaccel_context;
125 AVFrame *frame = &h->cur_pic_ptr->f;
126 struct vda_buffer *context;
130 if (!vda_ctx->decoder || !vda_ctx->priv_bitstream)
133 status = vda_sync_decode(vda_ctx);
134 frame->data[3] = (void*)vda_ctx->cv_buffer;
137 av_log(avctx, AV_LOG_ERROR, "Failed to decode frame (%d)\n", status);
139 if (!vda_ctx->use_ref_buffer || status)
142 context = av_mallocz(sizeof(*context));
143 buffer = av_buffer_create(NULL, 0, vda_h264_release_buffer, context, 0);
144 if (!context || !buffer) {
145 CVPixelBufferRelease(vda_ctx->cv_buffer);
150 context->cv_buffer = vda_ctx->cv_buffer;
151 frame->buf[3] = buffer;
156 int ff_vda_create_decoder(struct vda_context *vda_ctx,
165 CFMutableDictionaryRef config_info;
166 CFMutableDictionaryRef buffer_attributes;
167 CFMutableDictionaryRef io_surface_properties;
168 CFNumberRef cv_pix_fmt;
170 vda_ctx->priv_bitstream = NULL;
171 vda_ctx->priv_allocated_size = 0;
173 /* Each VCL NAL in the bitstream sent to the decoder
174 * is preceded by a 4 bytes length header.
175 * Change the avcC atom header if needed, to signal headers of 4 bytes. */
176 if (extradata_size >= 4 && (extradata[4] & 0x03) != 0x03) {
177 uint8_t *rw_extradata;
179 if (!(rw_extradata = av_malloc(extradata_size)))
180 return AVERROR(ENOMEM);
182 memcpy(rw_extradata, extradata, extradata_size);
184 rw_extradata[4] |= 0x03;
186 avc_data = CFDataCreate(kCFAllocatorDefault, rw_extradata, extradata_size);
188 av_freep(&rw_extradata);
190 avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
193 config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
195 &kCFTypeDictionaryKeyCallBacks,
196 &kCFTypeDictionaryValueCallBacks);
198 height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
199 width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
200 format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
202 CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
203 CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
204 CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
205 CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
207 buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
209 &kCFTypeDictionaryKeyCallBacks,
210 &kCFTypeDictionaryValueCallBacks);
211 io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
213 &kCFTypeDictionaryKeyCallBacks,
214 &kCFTypeDictionaryValueCallBacks);
215 cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
217 &vda_ctx->cv_pix_fmt_type);
218 CFDictionarySetValue(buffer_attributes,
219 kCVPixelBufferPixelFormatTypeKey,
221 CFDictionarySetValue(buffer_attributes,
222 kCVPixelBufferIOSurfacePropertiesKey,
223 io_surface_properties);
225 status = VDADecoderCreate(config_info,
227 vda_decoder_callback,
235 CFRelease(config_info);
236 CFRelease(io_surface_properties);
237 CFRelease(cv_pix_fmt);
238 CFRelease(buffer_attributes);
243 int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
245 OSStatus status = kVDADecoderNoErr;
247 if (vda_ctx->decoder)
248 status = VDADecoderDestroy(vda_ctx->decoder);
250 av_freep(&vda_ctx->priv_bitstream);
255 AVHWAccel ff_h264_vda_hwaccel = {
257 .type = AVMEDIA_TYPE_VIDEO,
258 .id = AV_CODEC_ID_H264,
259 .pix_fmt = AV_PIX_FMT_VDA_VLD,
260 .start_frame = vda_h264_start_frame,
261 .decode_slice = vda_h264_decode_slice,
262 .end_frame = vda_h264_end_frame,