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>
29 #include "libavutil/avutil.h"
30 #include "vda_internal.h"
32 /* Helper to create a dictionary according to the given pts. */
33 static CFDictionaryRef vda_dictionary_with_pts(int64_t i_pts)
35 CFStringRef key = CFSTR("FF_VDA_DECODER_PTS_KEY");
36 CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &i_pts);
37 CFDictionaryRef user_info = CFDictionaryCreate(kCFAllocatorDefault,
39 (const void **)&value,
41 &kCFTypeDictionaryKeyCallBacks,
42 &kCFTypeDictionaryValueCallBacks);
47 /* Helper to retrieve the pts from the given dictionary. */
48 static int64_t vda_pts_from_dictionary(CFDictionaryRef user_info)
56 pts = CFDictionaryGetValue(user_info, CFSTR("FF_VDA_DECODER_PTS_KEY"));
59 CFNumberGetValue(pts, kCFNumberSInt64Type, &outValue);
64 /* Removes and releases all frames from the queue. */
65 static void vda_clear_queue(struct vda_context *vda_ctx)
69 pthread_mutex_lock(&vda_ctx->queue_mutex);
71 while (vda_ctx->queue) {
72 top_frame = vda_ctx->queue;
73 vda_ctx->queue = top_frame->next_frame;
74 ff_vda_release_vda_frame(top_frame);
77 pthread_mutex_unlock(&vda_ctx->queue_mutex);
81 /* Decoder callback that adds the vda frame to the queue in display order. */
82 static void vda_decoder_callback (void *vda_hw_ctx,
83 CFDictionaryRef user_info,
86 CVImageBufferRef image_buffer)
88 struct vda_context *vda_ctx = (struct vda_context*)vda_hw_ctx;
90 vda_frame *queue_walker;
95 if (vda_ctx->cv_pix_fmt_type != CVPixelBufferGetPixelFormatType(image_buffer))
98 new_frame = av_mallocz(sizeof(vda_frame));
102 new_frame->next_frame = NULL;
103 new_frame->cv_buffer = CVPixelBufferRetain(image_buffer);
104 new_frame->pts = vda_pts_from_dictionary(user_info);
106 pthread_mutex_lock(&vda_ctx->queue_mutex);
108 queue_walker = vda_ctx->queue;
110 if (!queue_walker || (new_frame->pts < queue_walker->pts)) {
111 /* we have an empty queue, or this frame earlier than the current queue head */
112 new_frame->next_frame = queue_walker;
113 vda_ctx->queue = new_frame;
115 /* walk the queue and insert this frame where it belongs in display order */
116 vda_frame *next_frame;
119 next_frame = queue_walker->next_frame;
121 if (!next_frame || (new_frame->pts < next_frame->pts)) {
122 new_frame->next_frame = next_frame;
123 queue_walker->next_frame = new_frame;
126 queue_walker = next_frame;
130 pthread_mutex_unlock(&vda_ctx->queue_mutex);
133 int ff_vda_create_decoder(struct vda_context *vda_ctx,
137 OSStatus status = kVDADecoderNoErr;
142 CFMutableDictionaryRef config_info;
143 CFMutableDictionaryRef buffer_attributes;
144 CFMutableDictionaryRef io_surface_properties;
145 CFNumberRef cv_pix_fmt;
147 vda_ctx->bitstream = NULL;
148 vda_ctx->ref_size = 0;
150 pthread_mutex_init(&vda_ctx->queue_mutex, NULL);
152 if (extradata[4]==0xFE) {
153 // convert 3 byte NAL sizes to 4 byte
157 config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
159 &kCFTypeDictionaryKeyCallBacks,
160 &kCFTypeDictionaryValueCallBacks);
162 height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
163 width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
164 format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
165 avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
167 CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
168 CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
169 CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
170 CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
172 buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
174 &kCFTypeDictionaryKeyCallBacks,
175 &kCFTypeDictionaryValueCallBacks);
176 io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
178 &kCFTypeDictionaryKeyCallBacks,
179 &kCFTypeDictionaryValueCallBacks);
180 cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
182 &vda_ctx->cv_pix_fmt_type);
183 CFDictionarySetValue(buffer_attributes,
184 kCVPixelBufferPixelFormatTypeKey,
186 CFDictionarySetValue(buffer_attributes,
187 kCVPixelBufferIOSurfacePropertiesKey,
188 io_surface_properties);
190 status = VDADecoderCreate(config_info,
192 (VDADecoderOutputCallback *)vda_decoder_callback,
200 CFRelease(config_info);
201 CFRelease(io_surface_properties);
202 CFRelease(cv_pix_fmt);
203 CFRelease(buffer_attributes);
205 if (kVDADecoderNoErr != status)
211 int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
213 OSStatus status = kVDADecoderNoErr;
215 if (vda_ctx->decoder)
216 status = VDADecoderDestroy(vda_ctx->decoder);
218 vda_clear_queue(vda_ctx);
220 pthread_mutex_destroy(&vda_ctx->queue_mutex);
222 if (vda_ctx->bitstream)
223 av_freep(&vda_ctx->bitstream);
225 if (kVDADecoderNoErr != status)
231 vda_frame *ff_vda_queue_pop(struct vda_context *vda_ctx)
233 vda_frame *top_frame;
238 pthread_mutex_lock(&vda_ctx->queue_mutex);
239 top_frame = vda_ctx->queue;
240 vda_ctx->queue = top_frame->next_frame;
241 pthread_mutex_unlock(&vda_ctx->queue_mutex);
246 void ff_vda_release_vda_frame(vda_frame *frame)
249 CVPixelBufferRelease(frame->cv_buffer);
254 int ff_vda_decoder_decode(struct vda_context *vda_ctx,
259 OSStatus status = kVDADecoderNoErr;
260 CFDictionaryRef user_info;
261 CFDataRef coded_frame;
263 coded_frame = CFDataCreate(kCFAllocatorDefault, bitstream, bitstream_size);
264 user_info = vda_dictionary_with_pts(frame_pts);
266 status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, user_info);
268 CFRelease(user_info);
269 CFRelease(coded_frame);
271 if (kVDADecoderNoErr != status)