2 * VDA 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"
28 #include "vda_internal.h"
31 #include <CoreFoundation/CFDictionary.h>
33 /* helper to create a dictionary according to the given pts */
34 static CFDictionaryRef vda_dictionary_with_pts(int64_t i_pts)
36 CFStringRef key = CFSTR("FF_VDA_DECODER_PTS_KEY");
37 CFNumberRef value = CFNumberCreate(kCFAllocatorDefault,
38 kCFNumberSInt64Type, &i_pts);
39 CFDictionaryRef user_info = CFDictionaryCreate(kCFAllocatorDefault,
41 (const void **)&value,
43 &kCFTypeDictionaryKeyCallBacks,
44 &kCFTypeDictionaryValueCallBacks);
49 /* helper to retrieve the pts from the given dictionary */
50 static int64_t vda_pts_from_dictionary(CFDictionaryRef user_info)
58 pts = CFDictionaryGetValue(user_info, CFSTR("FF_VDA_DECODER_PTS_KEY"));
61 CFNumberGetValue(pts, kCFNumberSInt64Type, &outValue);
66 /* Remove and release all frames from the queue. */
67 static void vda_clear_queue(struct vda_context *vda_ctx)
71 pthread_mutex_lock(&vda_ctx->queue_mutex);
73 while (vda_ctx->queue) {
74 top_frame = vda_ctx->queue;
75 vda_ctx->queue = top_frame->next_frame;
76 ff_vda_release_vda_frame(top_frame);
79 pthread_mutex_unlock(&vda_ctx->queue_mutex);
83 /* Decoder callback that adds the VDA frame to the queue in display order. */
84 static void vda_decoder_callback(void *vda_hw_ctx,
85 CFDictionaryRef user_info,
88 CVImageBufferRef image_buffer)
90 struct vda_context *vda_ctx = vda_hw_ctx;
95 if (vda_ctx->cv_pix_fmt_type != CVPixelBufferGetPixelFormatType(image_buffer))
98 if (vda_ctx->use_sync_decoding) {
99 vda_ctx->cv_buffer = CVPixelBufferRetain(image_buffer);
101 vda_frame *new_frame;
102 vda_frame *queue_walker;
104 if (!(new_frame = av_mallocz(sizeof(vda_frame))))
106 new_frame->next_frame = NULL;
107 new_frame->cv_buffer = CVPixelBufferRetain(image_buffer);
108 new_frame->pts = vda_pts_from_dictionary(user_info);
110 pthread_mutex_lock(&vda_ctx->queue_mutex);
112 queue_walker = vda_ctx->queue;
114 if (!queue_walker || new_frame->pts < queue_walker->pts) {
115 /* we have an empty queue, or this frame earlier than the current queue head */
116 new_frame->next_frame = queue_walker;
117 vda_ctx->queue = new_frame;
119 /* walk the queue and insert this frame where it belongs in display order */
120 vda_frame *next_frame;
122 next_frame = queue_walker->next_frame;
123 if (!next_frame || new_frame->pts < next_frame->pts) {
124 new_frame->next_frame = next_frame;
125 queue_walker->next_frame = new_frame;
128 queue_walker = next_frame;
132 pthread_mutex_unlock(&vda_ctx->queue_mutex);
136 int ff_vda_create_decoder(struct vda_context *vda_ctx,
140 OSStatus status = kVDADecoderNoErr;
145 CFMutableDictionaryRef config_info;
146 CFMutableDictionaryRef buffer_attributes;
147 CFMutableDictionaryRef io_surface_properties;
148 CFNumberRef cv_pix_fmt;
151 pthread_mutex_init(&vda_ctx->queue_mutex, NULL);
154 /* Each VCL NAL in the bistream sent to the decoder
155 * is preceeded by a 4 bytes length header.
156 * Change the avcC atom header if needed, to signal headers of 4 bytes. */
157 if (extradata_size >= 4 && (extradata[4] & 0x03) != 0x03) {
158 uint8_t *rw_extradata;
160 if (!(rw_extradata = av_malloc(extradata_size)))
161 return AVERROR(ENOMEM);
163 memcpy(rw_extradata, extradata, extradata_size);
165 rw_extradata[4] |= 0x03;
167 avc_data = CFDataCreate(kCFAllocatorDefault, rw_extradata, extradata_size);
169 av_freep(&rw_extradata);
171 avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
174 config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
176 &kCFTypeDictionaryKeyCallBacks,
177 &kCFTypeDictionaryValueCallBacks);
179 height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
180 width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
181 format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
183 CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
184 CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
185 CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
186 CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
188 buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
190 &kCFTypeDictionaryKeyCallBacks,
191 &kCFTypeDictionaryValueCallBacks);
192 io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
194 &kCFTypeDictionaryKeyCallBacks,
195 &kCFTypeDictionaryValueCallBacks);
196 cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
198 &vda_ctx->cv_pix_fmt_type);
199 CFDictionarySetValue(buffer_attributes,
200 kCVPixelBufferPixelFormatTypeKey,
202 CFDictionarySetValue(buffer_attributes,
203 kCVPixelBufferIOSurfacePropertiesKey,
204 io_surface_properties);
206 status = VDADecoderCreate(config_info,
208 vda_decoder_callback,
216 CFRelease(config_info);
217 CFRelease(io_surface_properties);
218 CFRelease(cv_pix_fmt);
219 CFRelease(buffer_attributes);
224 int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
226 OSStatus status = kVDADecoderNoErr;
228 if (vda_ctx->decoder)
229 status = VDADecoderDestroy(vda_ctx->decoder);
232 vda_clear_queue(vda_ctx);
233 pthread_mutex_destroy(&vda_ctx->queue_mutex);
236 av_freep(&vda_ctx->priv_bitstream);
242 vda_frame *ff_vda_queue_pop(struct vda_context *vda_ctx)
244 vda_frame *top_frame;
249 pthread_mutex_lock(&vda_ctx->queue_mutex);
250 top_frame = vda_ctx->queue;
251 vda_ctx->queue = top_frame->next_frame;
252 pthread_mutex_unlock(&vda_ctx->queue_mutex);
257 void ff_vda_release_vda_frame(vda_frame *frame)
260 CVPixelBufferRelease(frame->cv_buffer);
265 int ff_vda_decoder_decode(struct vda_context *vda_ctx,
270 OSStatus status = kVDADecoderNoErr;
271 CFDictionaryRef user_info;
272 CFDataRef coded_frame;
274 coded_frame = CFDataCreate(kCFAllocatorDefault, bitstream, bitstream_size);
275 user_info = vda_dictionary_with_pts(frame_pts);
276 status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, user_info);
278 CFRelease(user_info);
279 CFRelease(coded_frame);
285 int ff_vda_sync_decode(struct vda_context *vda_ctx)
288 CFDataRef coded_frame;
289 uint32_t flush_flags = 1 << 0; ///< kVDADecoderFlush_emitFrames
291 coded_frame = CFDataCreate(kCFAllocatorDefault,
292 vda_ctx->priv_bitstream,
293 vda_ctx->priv_bitstream_size);
295 status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, NULL);
297 if (kVDADecoderNoErr == status)
298 status = VDADecoderFlush(vda_ctx->decoder, flush_flags);
300 CFRelease(coded_frame);