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
24 #include <CoreFoundation/CFDictionary.h>
25 #include <CoreFoundation/CFNumber.h>
26 #include <CoreFoundation/CFData.h>
27 #include <CoreFoundation/CFString.h>
30 #include "vda_internal.h"
33 * \addtogroup VDA_Decoding
38 /* Mutex manager callback. */
39 static int vda_lock_operation(void **mtx, enum AVLockOp op)
43 *mtx = av_malloc(sizeof(pthread_mutex_t));
46 return !!pthread_mutex_init(*mtx, NULL);
48 return !!pthread_mutex_lock(*mtx);
50 return !!pthread_mutex_unlock(*mtx);
52 pthread_mutex_destroy(*mtx);
59 /* Helper to create a dictionary according to the given pts. */
60 static CFDictionaryRef vda_dictionary_with_pts(int64_t i_pts)
62 CFStringRef key = CFSTR("FF_VDA_DECODER_PTS_KEY");
63 CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &i_pts);
64 CFDictionaryRef user_info = CFDictionaryCreate(kCFAllocatorDefault,
66 (const void **)&value,
68 &kCFTypeDictionaryKeyCallBacks,
69 &kCFTypeDictionaryValueCallBacks);
74 /* Helper to retrieve the pts from the given dictionary. */
75 static int64_t vda_pts_from_dictionary(CFDictionaryRef user_info)
83 pts = CFDictionaryGetValue(user_info, CFSTR("FF_VDA_DECODER_PTS_KEY"));
86 CFNumberGetValue(pts, kCFNumberSInt64Type, &outValue);
91 /* Removes and releases all frames from the queue. */
92 static void vda_clear_queue(struct vda_context *vda_ctx)
96 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_OBTAIN);
98 while (vda_ctx->queue) {
99 top_frame = vda_ctx->queue;
100 vda_ctx->queue = top_frame->next_frame;
101 ff_vda_release_vda_frame(top_frame);
104 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_RELEASE);
108 /* Decoder callback that adds the vda frame to the queue in display order. */
109 static void vda_decoder_callback (void *vda_hw_ctx,
110 CFDictionaryRef user_info,
113 CVImageBufferRef image_buffer)
115 struct vda_context *vda_ctx = (struct vda_context*)vda_hw_ctx;
116 vda_frame *new_frame;
117 vda_frame *queue_walker;
122 if (vda_ctx->cv_pix_fmt_type != CVPixelBufferGetPixelFormatType(image_buffer))
125 new_frame = av_mallocz(sizeof(vda_frame));
129 new_frame->next_frame = NULL;
130 new_frame->cv_buffer = CVPixelBufferRetain(image_buffer);
131 new_frame->pts = vda_pts_from_dictionary(user_info);
133 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_OBTAIN);
135 queue_walker = vda_ctx->queue;
137 if (!queue_walker || (new_frame->pts < queue_walker->pts)) {
138 /* we have an empty queue, or this frame earlier than the current queue head */
139 new_frame->next_frame = queue_walker;
140 vda_ctx->queue = new_frame;
142 /* walk the queue and insert this frame where it belongs in display order */
143 vda_frame *next_frame;
146 next_frame = queue_walker->next_frame;
148 if (!next_frame || (new_frame->pts < next_frame->pts)) {
149 new_frame->next_frame = next_frame;
150 queue_walker->next_frame = new_frame;
153 queue_walker = next_frame;
157 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_RELEASE);
160 int ff_vda_create_decoder(struct vda_context *vda_ctx,
164 OSStatus status = kVDADecoderNoErr;
169 CFMutableDictionaryRef config_info;
170 CFMutableDictionaryRef buffer_attributes;
171 CFMutableDictionaryRef io_surface_properties;
172 CFNumberRef cv_pix_fmt;
174 vda_ctx->bitstream = NULL;
175 vda_ctx->ref_size = 0;
177 if (av_lockmgr_register(vda_lock_operation))
180 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_CREATE);
182 config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
184 &kCFTypeDictionaryKeyCallBacks,
185 &kCFTypeDictionaryValueCallBacks);
187 height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
188 width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
189 format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
190 avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
192 CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
193 CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
194 CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
195 CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
197 buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
199 &kCFTypeDictionaryKeyCallBacks,
200 &kCFTypeDictionaryValueCallBacks);
201 io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
203 &kCFTypeDictionaryKeyCallBacks,
204 &kCFTypeDictionaryValueCallBacks);
205 cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
207 &vda_ctx->cv_pix_fmt_type);
208 CFDictionarySetValue(buffer_attributes,
209 kCVPixelBufferPixelFormatTypeKey,
211 CFDictionarySetValue(buffer_attributes,
212 kCVPixelBufferIOSurfacePropertiesKey,
213 io_surface_properties);
215 status = VDADecoderCreate(config_info,
217 (VDADecoderOutputCallback *)vda_decoder_callback,
225 CFRelease(config_info);
226 CFRelease(io_surface_properties);
227 CFRelease(cv_pix_fmt);
228 CFRelease(buffer_attributes);
230 if (kVDADecoderNoErr != status)
236 int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
238 OSStatus status = kVDADecoderNoErr;
240 if (vda_ctx->decoder)
241 status = VDADecoderDestroy(vda_ctx->decoder);
243 vda_clear_queue(vda_ctx);
245 if (vda_ctx->queue_mutex)
246 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_DESTROY);
248 if (vda_ctx->bitstream)
249 av_freep(&vda_ctx->bitstream);
251 if (kVDADecoderNoErr != status)
257 vda_frame *ff_vda_queue_pop(struct vda_context *vda_ctx)
259 vda_frame *top_frame;
264 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_OBTAIN);
265 top_frame = vda_ctx->queue;
266 vda_ctx->queue = top_frame->next_frame;
267 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_RELEASE);
272 void ff_vda_release_vda_frame(vda_frame *frame)
275 CVPixelBufferRelease(frame->cv_buffer);
280 int ff_vda_decoder_decode(struct vda_context *vda_ctx,
285 OSStatus status = kVDADecoderNoErr;
286 CFDictionaryRef user_info;
287 CFDataRef coded_frame;
289 coded_frame = CFDataCreate(kCFAllocatorDefault, bitstream, bitstream_size);
290 user_info = vda_dictionary_with_pts(frame_pts);
292 status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, user_info);
294 CFRelease(user_info);
295 CFRelease(coded_frame);
297 if (kVDADecoderNoErr != status)