2 * VDA hardware acceleration
4 * copyright (c) 2011 Sebastien Zwickert
6 * This file is part of Libav.
8 * Libav 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 * Libav 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 Libav; 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,
37 kCFNumberSInt64Type, &i_pts);
38 CFDictionaryRef user_info = CFDictionaryCreate(kCFAllocatorDefault,
40 (const void **)&value,
42 &kCFTypeDictionaryKeyCallBacks,
43 &kCFTypeDictionaryValueCallBacks);
48 /* helper to retrieve the pts from the given dictionary */
49 static int64_t vda_pts_from_dictionary(CFDictionaryRef user_info)
57 pts = CFDictionaryGetValue(user_info, CFSTR("FF_VDA_DECODER_PTS_KEY"));
60 CFNumberGetValue(pts, kCFNumberSInt64Type, &outValue);
65 /* Remove and release all frames from the queue. */
66 static void vda_clear_queue(struct vda_context *vda_ctx)
70 pthread_mutex_lock(&vda_ctx->queue_mutex);
72 while (vda_ctx->queue) {
73 top_frame = vda_ctx->queue;
74 vda_ctx->queue = top_frame->next_frame;
75 ff_vda_release_vda_frame(top_frame);
78 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 = vda_hw_ctx;
90 vda_frame *queue_walker;
95 if (vda_ctx->cv_pix_fmt_type != CVPixelBufferGetPixelFormatType(image_buffer))
98 if (!(new_frame = av_mallocz(sizeof(vda_frame))))
100 new_frame->next_frame = NULL;
101 new_frame->cv_buffer = CVPixelBufferRetain(image_buffer);
102 new_frame->pts = vda_pts_from_dictionary(user_info);
104 pthread_mutex_lock(&vda_ctx->queue_mutex);
106 queue_walker = vda_ctx->queue;
108 if (!queue_walker || new_frame->pts < queue_walker->pts) {
109 /* we have an empty queue, or this frame earlier than the current queue head */
110 new_frame->next_frame = queue_walker;
111 vda_ctx->queue = new_frame;
113 /* walk the queue and insert this frame where it belongs in display order */
114 vda_frame *next_frame;
116 next_frame = queue_walker->next_frame;
117 if (!next_frame || new_frame->pts < next_frame->pts) {
118 new_frame->next_frame = next_frame;
119 queue_walker->next_frame = new_frame;
122 queue_walker = next_frame;
126 pthread_mutex_unlock(&vda_ctx->queue_mutex);
129 int ff_vda_create_decoder(struct vda_context *vda_ctx,
133 OSStatus status = kVDADecoderNoErr;
138 CFMutableDictionaryRef config_info;
139 CFMutableDictionaryRef buffer_attributes;
140 CFMutableDictionaryRef io_surface_properties;
141 CFNumberRef cv_pix_fmt;
143 pthread_mutex_init(&vda_ctx->queue_mutex, NULL);
145 /* Each VCL NAL in the bistream sent to the decoder
146 * is preceeded by a 4 bytes length header.
147 * Change the avcC atom header if needed, to signal headers of 4 bytes. */
148 if (extradata_size >= 4 && (extradata[4] & 0x03) != 0x03) {
149 uint8_t *rw_extradata;
151 if (!(rw_extradata = av_malloc(extradata_size)))
152 return AVERROR(ENOMEM);
154 memcpy(rw_extradata, extradata, extradata_size);
156 rw_extradata[4] |= 0x03;
158 avc_data = CFDataCreate(kCFAllocatorDefault, rw_extradata, extradata_size);
160 av_freep(&rw_extradata);
162 avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
165 config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
167 &kCFTypeDictionaryKeyCallBacks,
168 &kCFTypeDictionaryValueCallBacks);
170 height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
171 width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
172 format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
174 CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
175 CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
176 CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
177 CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
179 buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
181 &kCFTypeDictionaryKeyCallBacks,
182 &kCFTypeDictionaryValueCallBacks);
183 io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
185 &kCFTypeDictionaryKeyCallBacks,
186 &kCFTypeDictionaryValueCallBacks);
187 cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
189 &vda_ctx->cv_pix_fmt_type);
190 CFDictionarySetValue(buffer_attributes,
191 kCVPixelBufferPixelFormatTypeKey,
193 CFDictionarySetValue(buffer_attributes,
194 kCVPixelBufferIOSurfacePropertiesKey,
195 io_surface_properties);
197 status = VDADecoderCreate(config_info,
199 vda_decoder_callback,
207 CFRelease(config_info);
208 CFRelease(io_surface_properties);
209 CFRelease(cv_pix_fmt);
210 CFRelease(buffer_attributes);
212 if (kVDADecoderNoErr != status)
218 int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
220 OSStatus status = kVDADecoderNoErr;
222 if (vda_ctx->decoder)
223 status = VDADecoderDestroy(vda_ctx->decoder);
225 vda_clear_queue(vda_ctx);
227 pthread_mutex_destroy(&vda_ctx->queue_mutex);
229 if (kVDADecoderNoErr != status)
235 vda_frame *ff_vda_queue_pop(struct vda_context *vda_ctx)
237 vda_frame *top_frame;
242 pthread_mutex_lock(&vda_ctx->queue_mutex);
243 top_frame = vda_ctx->queue;
244 vda_ctx->queue = top_frame->next_frame;
245 pthread_mutex_unlock(&vda_ctx->queue_mutex);
250 void ff_vda_release_vda_frame(vda_frame *frame)
253 CVPixelBufferRelease(frame->cv_buffer);
258 int ff_vda_decoder_decode(struct vda_context *vda_ctx,
263 OSStatus status = kVDADecoderNoErr;
264 CFDictionaryRef user_info;
265 CFDataRef coded_frame;
267 coded_frame = CFDataCreate(kCFAllocatorDefault, bitstream, bitstream_size);
268 user_info = vda_dictionary_with_pts(frame_pts);
269 status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, user_info);
271 CFRelease(user_info);
272 CFRelease(coded_frame);
274 if (kVDADecoderNoErr != status)