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 "vda_internal.h"
26 * \addtogroup VDA_Decoding
31 /* Mutex manager callback. */
32 static int vda_lock_operation(void **mtx, enum AVLockOp op)
37 *mtx = av_malloc(sizeof(pthread_mutex_t));
40 return !!pthread_mutex_init(*mtx, NULL);
42 return !!pthread_mutex_lock(*mtx);
44 return !!pthread_mutex_unlock(*mtx);
46 pthread_mutex_destroy(*mtx);
53 /* Helper to create a dictionary according to the given pts. */
54 static CFDictionaryRef vda_dictionary_with_pts(int64_t i_pts)
56 CFStringRef key = CFSTR("FF_VDA_DECODER_PTS_KEY");
57 CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &i_pts);
58 CFDictionaryRef user_info = CFDictionaryCreate( kCFAllocatorDefault,
60 (const void **)&value,
62 &kCFTypeDictionaryKeyCallBacks,
63 &kCFTypeDictionaryValueCallBacks);
68 /* Helper to retrieve the pts from the given dictionary. */
69 static int64_t vda_pts_from_dictionary(CFDictionaryRef user_info)
74 if (NULL == user_info)
77 pts = CFDictionaryGetValue(user_info, CFSTR("FF_VDA_DECODER_PTS_KEY"));
80 CFNumberGetValue(pts, kCFNumberSInt64Type, &outValue);
85 /* Removes and releases all frames from the queue. */
86 static void vda_clear_queue(struct vda_context *vda_ctx)
90 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_OBTAIN);
92 while (vda_ctx->queue != NULL)
94 top_frame = vda_ctx->queue;
95 vda_ctx->queue = top_frame->next_frame;
96 ff_vda_release_vda_frame(top_frame);
99 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_RELEASE);
103 /* Decoder callback that adds the vda frame to the queue in display order. */
104 static void vda_decoder_callback (void *vda_hw_ctx,
105 CFDictionaryRef user_info,
108 CVImageBufferRef image_buffer)
110 struct vda_context *vda_ctx = (struct vda_context*)vda_hw_ctx;
111 vda_frame *new_frame;
112 vda_frame *queue_walker;
114 if (NULL == image_buffer)
117 if (vda_ctx->cv_pix_fmt_type != CVPixelBufferGetPixelFormatType(image_buffer))
120 new_frame = (vda_frame *)av_mallocz(sizeof(vda_frame));
121 new_frame->next_frame = NULL;
122 new_frame->cv_buffer = CVPixelBufferRetain(image_buffer);
123 new_frame->pts = vda_pts_from_dictionary(user_info);
125 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_OBTAIN);
127 queue_walker = vda_ctx->queue;
129 if (!queue_walker || (new_frame->pts < queue_walker->pts))
131 /* we have an empty queue, or this frame earlier than the current queue head */
132 new_frame->next_frame = queue_walker;
133 vda_ctx->queue = new_frame;
137 /* walk the queue and insert this frame where it belongs in display order */
138 vda_frame *next_frame;
142 next_frame = queue_walker->next_frame;
144 if (!next_frame || (new_frame->pts < next_frame->pts))
146 new_frame->next_frame = next_frame;
147 queue_walker->next_frame = new_frame;
150 queue_walker = next_frame;
154 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_RELEASE);
157 int ff_vda_create_decoder(struct vda_context *vda_ctx,
161 OSStatus status = kVDADecoderNoErr;
166 CFMutableDictionaryRef config_info;
167 CFMutableDictionaryRef buffer_attributes;
168 CFMutableDictionaryRef io_surface_properties;
169 CFNumberRef cv_pix_fmt;
171 if (av_lockmgr_register(vda_lock_operation))
174 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_CREATE);
176 config_info = (CFDictionaryCreateMutable(kCFAllocatorDefault,
178 &kCFTypeDictionaryKeyCallBacks,
179 &kCFTypeDictionaryValueCallBacks));
181 height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
182 width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
183 format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
184 avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
186 CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
187 CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
188 CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
189 CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
191 buffer_attributes = (CFDictionaryCreateMutable(kCFAllocatorDefault,
193 &kCFTypeDictionaryKeyCallBacks,
194 &kCFTypeDictionaryValueCallBacks));
195 io_surface_properties = (CFDictionaryCreateMutable(kCFAllocatorDefault,
197 &kCFTypeDictionaryKeyCallBacks,
198 &kCFTypeDictionaryValueCallBacks));
199 cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
201 &vda_ctx->cv_pix_fmt_type);
202 CFDictionarySetValue(buffer_attributes,
203 kCVPixelBufferPixelFormatTypeKey,
205 CFDictionarySetValue(buffer_attributes,
206 kCVPixelBufferIOSurfacePropertiesKey,
207 io_surface_properties);
209 status = VDADecoderCreate( config_info,
211 (VDADecoderOutputCallback *)vda_decoder_callback,
219 CFRelease(config_info);
220 CFRelease(io_surface_properties);
221 CFRelease(cv_pix_fmt);
222 CFRelease(buffer_attributes);
224 if (kVDADecoderNoErr != status)
230 int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
232 OSStatus status = kVDADecoderNoErr;
234 if (vda_ctx->decoder)
235 status = VDADecoderDestroy(vda_ctx->decoder);
237 vda_clear_queue(vda_ctx);
239 if (vda_ctx->queue_mutex != NULL)
240 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_DESTROY);
242 if (kVDADecoderNoErr != status)
248 vda_frame *ff_vda_queue_pop(struct vda_context *vda_ctx)
250 vda_frame *top_frame;
255 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_OBTAIN);
256 top_frame = vda_ctx->queue;
257 vda_ctx->queue = top_frame->next_frame;
258 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_RELEASE);
263 void ff_vda_release_vda_frame(vda_frame *frame)
267 CVPixelBufferRelease(frame->cv_buffer);
272 int ff_vda_decoder_decode(struct vda_context *vda_ctx,
277 OSStatus status = kVDADecoderNoErr;
278 CFDictionaryRef user_info;
279 CFDataRef coded_frame;
281 coded_frame = CFDataCreate(kCFAllocatorDefault, bitstream, bitstream_size);
282 user_info = vda_dictionary_with_pts(frame_pts);
284 status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, user_info);
286 CFRelease(user_info);
287 CFRelease(coded_frame);
289 if (kVDADecoderNoErr != status)