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 if (vda_ctx->use_sync_decoding) {
47 vda_ctx->cv_buffer = CVPixelBufferRetain(image_buffer);
50 vda_frame *queue_walker;
52 if (!(new_frame = av_mallocz(sizeof(*new_frame))))
54 new_frame->next_frame = NULL;
55 new_frame->cv_buffer = CVPixelBufferRetain(image_buffer);
56 new_frame->pts = vda_pts_from_dictionary(user_info);
58 pthread_mutex_lock(&vda_ctx->queue_mutex);
60 queue_walker = vda_ctx->queue;
62 if (!queue_walker || new_frame->pts < queue_walker->pts) {
63 /* we have an empty queue, or this frame earlier than the current queue head */
64 new_frame->next_frame = queue_walker;
65 vda_ctx->queue = new_frame;
67 /* walk the queue and insert this frame where it belongs in display order */
68 vda_frame *next_frame;
70 next_frame = queue_walker->next_frame;
71 if (!next_frame || new_frame->pts < next_frame->pts) {
72 new_frame->next_frame = next_frame;
73 queue_walker->next_frame = new_frame;
76 queue_walker = next_frame;
80 pthread_mutex_unlock(&vda_ctx->queue_mutex);
84 static int vda_sync_decode(struct vda_context *vda_ctx)
87 CFDataRef coded_frame;
88 uint32_t flush_flags = 1 << 0; ///< kVDADecoderFlush_emitFrames
90 coded_frame = CFDataCreate(kCFAllocatorDefault,
91 vda_ctx->priv_bitstream,
92 vda_ctx->priv_bitstream_size);
94 status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, NULL);
96 if (kVDADecoderNoErr == status)
97 status = VDADecoderFlush(vda_ctx->decoder, flush_flags);
99 CFRelease(coded_frame);
105 static int vda_h264_start_frame(AVCodecContext *avctx,
106 av_unused const uint8_t *buffer,
107 av_unused uint32_t size)
109 struct vda_context *vda_ctx = avctx->hwaccel_context;
111 if (!vda_ctx->decoder)
114 vda_ctx->priv_bitstream_size = 0;
119 static int vda_h264_decode_slice(AVCodecContext *avctx,
120 const uint8_t *buffer,
123 struct vda_context *vda_ctx = avctx->hwaccel_context;
126 if (!vda_ctx->decoder)
129 tmp = av_fast_realloc(vda_ctx->priv_bitstream,
130 &vda_ctx->priv_allocated_size,
131 vda_ctx->priv_bitstream_size + size + 4);
133 return AVERROR(ENOMEM);
135 vda_ctx->priv_bitstream = tmp;
137 AV_WB32(vda_ctx->priv_bitstream + vda_ctx->priv_bitstream_size, size);
138 memcpy(vda_ctx->priv_bitstream + vda_ctx->priv_bitstream_size + 4, buffer, size);
140 vda_ctx->priv_bitstream_size += size + 4;
145 static int vda_h264_end_frame(AVCodecContext *avctx)
147 H264Context *h = avctx->priv_data;
148 struct vda_context *vda_ctx = avctx->hwaccel_context;
149 AVFrame *frame = &h->cur_pic_ptr->f;
152 if (!vda_ctx->decoder || !vda_ctx->priv_bitstream)
155 if (vda_ctx->use_sync_decoding) {
156 status = vda_sync_decode(vda_ctx);
157 frame->data[3] = (void*)vda_ctx->cv_buffer;
159 status = vda_decoder_decode(vda_ctx, vda_ctx->priv_bitstream,
160 vda_ctx->priv_bitstream_size,
161 frame->reordered_opaque);
165 av_log(avctx, AV_LOG_ERROR, "Failed to decode frame (%d)\n", status);
170 int ff_vda_create_decoder(struct vda_context *vda_ctx,
174 OSStatus status = kVDADecoderNoErr;
179 CFMutableDictionaryRef config_info;
180 CFMutableDictionaryRef buffer_attributes;
181 CFMutableDictionaryRef io_surface_properties;
182 CFNumberRef cv_pix_fmt;
184 /* Each VCL NAL in the bistream sent to the decoder
185 * is preceded by a 4 bytes length header.
186 * Change the avcC atom header if needed, to signal headers of 4 bytes. */
187 if (extradata_size >= 4 && (extradata[4] & 0x03) != 0x03) {
188 uint8_t *rw_extradata;
190 if (!(rw_extradata = av_malloc(extradata_size)))
191 return AVERROR(ENOMEM);
193 memcpy(rw_extradata, extradata, extradata_size);
195 rw_extradata[4] |= 0x03;
197 avc_data = CFDataCreate(kCFAllocatorDefault, rw_extradata, extradata_size);
199 av_freep(&rw_extradata);
201 avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
204 config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
206 &kCFTypeDictionaryKeyCallBacks,
207 &kCFTypeDictionaryValueCallBacks);
209 height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
210 width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
211 format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
213 CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
214 CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
215 CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
216 CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
218 buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
220 &kCFTypeDictionaryKeyCallBacks,
221 &kCFTypeDictionaryValueCallBacks);
222 io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
224 &kCFTypeDictionaryKeyCallBacks,
225 &kCFTypeDictionaryValueCallBacks);
226 cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
228 &vda_ctx->cv_pix_fmt_type);
229 CFDictionarySetValue(buffer_attributes,
230 kCVPixelBufferPixelFormatTypeKey,
232 CFDictionarySetValue(buffer_attributes,
233 kCVPixelBufferIOSurfacePropertiesKey,
234 io_surface_properties);
236 status = VDADecoderCreate(config_info,
238 vda_decoder_callback,
246 CFRelease(config_info);
247 CFRelease(io_surface_properties);
248 CFRelease(cv_pix_fmt);
249 CFRelease(buffer_attributes);
254 int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
256 OSStatus status = kVDADecoderNoErr;
258 if (vda_ctx->decoder)
259 status = VDADecoderDestroy(vda_ctx->decoder);
261 av_freep(&vda_ctx->priv_bitstream);
266 AVHWAccel ff_h264_vda_hwaccel = {
268 .type = AVMEDIA_TYPE_VIDEO,
269 .id = AV_CODEC_ID_H264,
270 .pix_fmt = AV_PIX_FMT_VDA_VLD,
271 .start_frame = vda_h264_start_frame,
272 .decode_slice = vda_h264_decode_slice,
273 .end_frame = vda_h264_end_frame,