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)
44 *mtx = av_malloc(sizeof(pthread_mutex_t));
47 return !!pthread_mutex_init(*mtx, NULL);
49 return !!pthread_mutex_lock(*mtx);
51 return !!pthread_mutex_unlock(*mtx);
53 pthread_mutex_destroy(*mtx);
60 /* Helper to create a dictionary according to the given pts. */
61 static CFDictionaryRef vda_dictionary_with_pts(int64_t i_pts)
63 CFStringRef key = CFSTR("FF_VDA_DECODER_PTS_KEY");
64 CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &i_pts);
65 CFDictionaryRef user_info = CFDictionaryCreate( kCFAllocatorDefault,
67 (const void **)&value,
69 &kCFTypeDictionaryKeyCallBacks,
70 &kCFTypeDictionaryValueCallBacks);
75 /* Helper to retrieve the pts from the given dictionary. */
76 static int64_t vda_pts_from_dictionary(CFDictionaryRef user_info)
84 pts = CFDictionaryGetValue(user_info, CFSTR("FF_VDA_DECODER_PTS_KEY"));
87 CFNumberGetValue(pts, kCFNumberSInt64Type, &outValue);
92 /* Removes and releases all frames from the queue. */
93 static void vda_clear_queue(struct vda_context *vda_ctx)
97 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_OBTAIN);
99 while (vda_ctx->queue)
101 top_frame = vda_ctx->queue;
102 vda_ctx->queue = top_frame->next_frame;
103 ff_vda_release_vda_frame(top_frame);
106 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_RELEASE);
110 /* Decoder callback that adds the vda frame to the queue in display order. */
111 static void vda_decoder_callback (void *vda_hw_ctx,
112 CFDictionaryRef user_info,
115 CVImageBufferRef image_buffer)
117 struct vda_context *vda_ctx = (struct vda_context*)vda_hw_ctx;
118 vda_frame *new_frame;
119 vda_frame *queue_walker;
124 if (vda_ctx->cv_pix_fmt_type != CVPixelBufferGetPixelFormatType(image_buffer))
127 new_frame = av_mallocz(sizeof(vda_frame));
131 new_frame->next_frame = NULL;
132 new_frame->cv_buffer = CVPixelBufferRetain(image_buffer);
133 new_frame->pts = vda_pts_from_dictionary(user_info);
135 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_OBTAIN);
137 queue_walker = vda_ctx->queue;
139 if (!queue_walker || (new_frame->pts < queue_walker->pts))
141 /* we have an empty queue, or this frame earlier than the current queue head */
142 new_frame->next_frame = queue_walker;
143 vda_ctx->queue = new_frame;
147 /* walk the queue and insert this frame where it belongs in display order */
148 vda_frame *next_frame;
152 next_frame = queue_walker->next_frame;
154 if (!next_frame || (new_frame->pts < next_frame->pts))
156 new_frame->next_frame = next_frame;
157 queue_walker->next_frame = new_frame;
160 queue_walker = next_frame;
164 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_RELEASE);
167 int ff_vda_create_decoder(struct vda_context *vda_ctx,
171 OSStatus status = kVDADecoderNoErr;
176 CFMutableDictionaryRef config_info;
177 CFMutableDictionaryRef buffer_attributes;
178 CFMutableDictionaryRef io_surface_properties;
179 CFNumberRef cv_pix_fmt;
181 vda_ctx->bitstream = NULL;
182 vda_ctx->ref_size = 0;
184 if (av_lockmgr_register(vda_lock_operation))
187 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_CREATE);
189 config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
191 &kCFTypeDictionaryKeyCallBacks,
192 &kCFTypeDictionaryValueCallBacks);
194 height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
195 width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
196 format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
197 avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
199 CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
200 CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
201 CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
202 CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
204 buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
206 &kCFTypeDictionaryKeyCallBacks,
207 &kCFTypeDictionaryValueCallBacks);
208 io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
210 &kCFTypeDictionaryKeyCallBacks,
211 &kCFTypeDictionaryValueCallBacks);
212 cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
214 &vda_ctx->cv_pix_fmt_type);
215 CFDictionarySetValue(buffer_attributes,
216 kCVPixelBufferPixelFormatTypeKey,
218 CFDictionarySetValue(buffer_attributes,
219 kCVPixelBufferIOSurfacePropertiesKey,
220 io_surface_properties);
222 status = VDADecoderCreate( config_info,
224 (VDADecoderOutputCallback *)vda_decoder_callback,
232 CFRelease(config_info);
233 CFRelease(io_surface_properties);
234 CFRelease(cv_pix_fmt);
235 CFRelease(buffer_attributes);
237 if (kVDADecoderNoErr != status)
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 vda_clear_queue(vda_ctx);
252 if (vda_ctx->queue_mutex)
253 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_DESTROY);
255 if (vda_ctx->bitstream)
256 av_freep(&vda_ctx->bitstream);
258 if (kVDADecoderNoErr != status)
264 vda_frame *ff_vda_queue_pop(struct vda_context *vda_ctx)
266 vda_frame *top_frame;
271 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_OBTAIN);
272 top_frame = vda_ctx->queue;
273 vda_ctx->queue = top_frame->next_frame;
274 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_RELEASE);
279 void ff_vda_release_vda_frame(vda_frame *frame)
283 CVPixelBufferRelease(frame->cv_buffer);
288 int ff_vda_decoder_decode(struct vda_context *vda_ctx,
293 OSStatus status = kVDADecoderNoErr;
294 CFDictionaryRef user_info;
295 CFDataRef coded_frame;
297 coded_frame = CFDataCreate(kCFAllocatorDefault, bitstream, bitstream_size);
298 user_info = vda_dictionary_with_pts(frame_pts);
300 status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, user_info);
302 CFRelease(user_info);
303 CFRelease(coded_frame);
305 if (kVDADecoderNoErr != status)