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
config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
147
&kCFTypeDictionaryKeyCallBacks,
148
&kCFTypeDictionaryValueCallBacks);
150
height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
151
width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
152
format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
153
avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
155
CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
156
CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
157
CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
158
CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
160
buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
162
&kCFTypeDictionaryKeyCallBacks,
163
&kCFTypeDictionaryValueCallBacks);
164
io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
166
&kCFTypeDictionaryKeyCallBacks,
167
&kCFTypeDictionaryValueCallBacks);
168
cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
170
&vda_ctx->cv_pix_fmt_type);
171
CFDictionarySetValue(buffer_attributes,
172
kCVPixelBufferPixelFormatTypeKey,
174
CFDictionarySetValue(buffer_attributes,
175
kCVPixelBufferIOSurfacePropertiesKey,
176
io_surface_properties);
178
status = VDADecoderCreate(config_info,
180
vda_decoder_callback,
188
CFRelease(config_info);
189
CFRelease(io_surface_properties);
190
CFRelease(cv_pix_fmt);
191
CFRelease(buffer_attributes);
193
if (kVDADecoderNoErr != status)
199
int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
201
OSStatus status = kVDADecoderNoErr;
203
if (vda_ctx->decoder)
204
status = VDADecoderDestroy(vda_ctx->decoder);
206
vda_clear_queue(vda_ctx);
208
pthread_mutex_destroy(&vda_ctx->queue_mutex);
210
if (kVDADecoderNoErr != status)
216
vda_frame *ff_vda_queue_pop(struct vda_context *vda_ctx)
218
vda_frame *top_frame;
223
pthread_mutex_lock(&vda_ctx->queue_mutex);
224
top_frame = vda_ctx->queue;
225
vda_ctx->queue = top_frame->next_frame;
226
pthread_mutex_unlock(&vda_ctx->queue_mutex);
231
void ff_vda_release_vda_frame(vda_frame *frame)
234
CVPixelBufferRelease(frame->cv_buffer);
239
int ff_vda_decoder_decode(struct vda_context *vda_ctx,
244
OSStatus status = kVDADecoderNoErr;
245
CFDictionaryRef user_info;
246
CFDataRef coded_frame;
248
coded_frame = CFDataCreate(kCFAllocatorDefault, bitstream, bitstream_size);
249
user_info = vda_dictionary_with_pts(frame_pts);
250
status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, user_info);
252
CFRelease(user_info);
253
CFRelease(coded_frame);
255
if (kVDADecoderNoErr != status)