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 /* Each VCL NAL in the bistream sent to the decoder
153 * is preceded by a 4 bytes length header.
154 * Change the avcC atom header if needed, to signal headers of 4 bytes. */
155 if (extradata_size >= 4 && (extradata[4] & 0x03) != 0x03) {
156 uint8_t *rw_extradata;
158 if (!(rw_extradata = av_malloc(extradata_size)))
159 return AVERROR(ENOMEM);
161 memcpy(rw_extradata, extradata, extradata_size);
163 rw_extradata[4] |= 0x03;
165 avc_data = CFDataCreate(kCFAllocatorDefault, rw_extradata, extradata_size);
167 av_freep(&rw_extradata);
169 avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
172 config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
174 &kCFTypeDictionaryKeyCallBacks,
175 &kCFTypeDictionaryValueCallBacks);
177 height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
178 width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
179 format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
181 CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
182 CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
183 CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
184 CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
186 buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
188 &kCFTypeDictionaryKeyCallBacks,
189 &kCFTypeDictionaryValueCallBacks);
190 io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
192 &kCFTypeDictionaryKeyCallBacks,
193 &kCFTypeDictionaryValueCallBacks);
194 cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
196 &vda_ctx->cv_pix_fmt_type);
197 CFDictionarySetValue(buffer_attributes,
198 kCVPixelBufferPixelFormatTypeKey,
200 CFDictionarySetValue(buffer_attributes,
201 kCVPixelBufferIOSurfacePropertiesKey,
202 io_surface_properties);
204 status = VDADecoderCreate(config_info,
206 (VDADecoderOutputCallback *)vda_decoder_callback,
214 CFRelease(config_info);
215 CFRelease(io_surface_properties);
216 CFRelease(cv_pix_fmt);
217 CFRelease(buffer_attributes);
219 if (kVDADecoderNoErr != status)
225 int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
227 OSStatus status = kVDADecoderNoErr;
229 if (vda_ctx->decoder)
230 status = VDADecoderDestroy(vda_ctx->decoder);
232 vda_clear_queue(vda_ctx);
234 pthread_mutex_destroy(&vda_ctx->queue_mutex);
236 if (vda_ctx->bitstream)
237 av_freep(&vda_ctx->bitstream);
239 if (kVDADecoderNoErr != status)
245 vda_frame *ff_vda_queue_pop(struct vda_context *vda_ctx)
247 vda_frame *top_frame;
252 pthread_mutex_lock(&vda_ctx->queue_mutex);
253 top_frame = vda_ctx->queue;
254 vda_ctx->queue = top_frame->next_frame;
255 pthread_mutex_unlock(&vda_ctx->queue_mutex);
260 void ff_vda_release_vda_frame(vda_frame *frame)
263 CVPixelBufferRelease(frame->cv_buffer);
268 int ff_vda_decoder_decode(struct vda_context *vda_ctx,
273 OSStatus status = kVDADecoderNoErr;
274 CFDictionaryRef user_info;
275 CFDataRef coded_frame;
277 coded_frame = CFDataCreate(kCFAllocatorDefault, bitstream, bitstream_size);
278 user_info = vda_dictionary_with_pts(frame_pts);
280 status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, user_info);
282 CFRelease(user_info);
283 CFRelease(coded_frame);
285 if (kVDADecoderNoErr != status)