20
20
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
#include <CoreFoundation/CFNumber.h>
24
#include <CoreFoundation/CFData.h>
25
#include <CoreFoundation/CFString.h>
27
#include "libavutil/avutil.h"
26
#include "vda_internal.h"
28
/* This structure is used to store the bitstream of the current frame. */
29
struct vda_picture_context {
32
#include <CoreFoundation/CFDictionary.h>
34
/* helper to create a dictionary according to the given pts */
35
static CFDictionaryRef vda_dictionary_with_pts(int64_t i_pts)
37
CFStringRef key = CFSTR("FF_VDA_DECODER_PTS_KEY");
38
CFNumberRef value = CFNumberCreate(kCFAllocatorDefault,
39
kCFNumberSInt64Type, &i_pts);
40
CFDictionaryRef user_info = CFDictionaryCreate(kCFAllocatorDefault,
42
(const void **)&value,
44
&kCFTypeDictionaryKeyCallBacks,
45
&kCFTypeDictionaryValueCallBacks);
50
/* helper to retrieve the pts from the given dictionary */
51
static int64_t vda_pts_from_dictionary(CFDictionaryRef user_info)
59
pts = CFDictionaryGetValue(user_info, CFSTR("FF_VDA_DECODER_PTS_KEY"));
62
CFNumberGetValue(pts, kCFNumberSInt64Type, &outValue);
67
/* Remove and release all frames from the queue. */
68
static void vda_clear_queue(struct vda_context *vda_ctx)
72
pthread_mutex_lock(&vda_ctx->queue_mutex);
74
while (vda_ctx->queue) {
75
top_frame = vda_ctx->queue;
76
vda_ctx->queue = top_frame->next_frame;
77
ff_vda_release_vda_frame(top_frame);
80
pthread_mutex_unlock(&vda_ctx->queue_mutex);
83
static int vda_decoder_decode(struct vda_context *vda_ctx,
88
OSStatus status = kVDADecoderNoErr;
89
CFDictionaryRef user_info;
90
CFDataRef coded_frame;
92
coded_frame = CFDataCreate(kCFAllocatorDefault, bitstream, bitstream_size);
93
user_info = vda_dictionary_with_pts(frame_pts);
94
status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, user_info);
97
CFRelease(coded_frame);
102
vda_frame *ff_vda_queue_pop(struct vda_context *vda_ctx)
104
vda_frame *top_frame;
109
pthread_mutex_lock(&vda_ctx->queue_mutex);
110
top_frame = vda_ctx->queue;
111
vda_ctx->queue = top_frame->next_frame;
112
pthread_mutex_unlock(&vda_ctx->queue_mutex);
117
void ff_vda_release_vda_frame(vda_frame *frame)
120
CVPixelBufferRelease(frame->cv_buffer);
126
/* Decoder callback that adds the VDA frame to the queue in display order. */
127
static void vda_decoder_callback(void *vda_hw_ctx,
128
CFDictionaryRef user_info,
131
CVImageBufferRef image_buffer)
133
struct vda_context *vda_ctx = vda_hw_ctx;
138
if (vda_ctx->cv_pix_fmt_type != CVPixelBufferGetPixelFormatType(image_buffer))
141
if (vda_ctx->use_sync_decoding) {
142
vda_ctx->cv_buffer = CVPixelBufferRetain(image_buffer);
144
vda_frame *new_frame;
145
vda_frame *queue_walker;
147
if (!(new_frame = av_mallocz(sizeof(*new_frame))))
149
new_frame->next_frame = NULL;
150
new_frame->cv_buffer = CVPixelBufferRetain(image_buffer);
151
new_frame->pts = vda_pts_from_dictionary(user_info);
153
pthread_mutex_lock(&vda_ctx->queue_mutex);
155
queue_walker = vda_ctx->queue;
157
if (!queue_walker || new_frame->pts < queue_walker->pts) {
158
/* we have an empty queue, or this frame earlier than the current queue head */
159
new_frame->next_frame = queue_walker;
160
vda_ctx->queue = new_frame;
162
/* walk the queue and insert this frame where it belongs in display order */
163
vda_frame *next_frame;
165
next_frame = queue_walker->next_frame;
166
if (!next_frame || new_frame->pts < next_frame->pts) {
167
new_frame->next_frame = next_frame;
168
queue_walker->next_frame = new_frame;
171
queue_walker = next_frame;
175
pthread_mutex_unlock(&vda_ctx->queue_mutex);
179
static int vda_sync_decode(struct vda_context *vda_ctx)
182
CFDataRef coded_frame;
183
uint32_t flush_flags = 1 << 0; ///< kVDADecoderFlush_emitFrames
185
coded_frame = CFDataCreate(kCFAllocatorDefault,
186
vda_ctx->priv_bitstream,
187
vda_ctx->priv_bitstream_size);
189
status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, NULL);
191
if (kVDADecoderNoErr == status)
192
status = VDADecoderFlush(vda_ctx->decoder, flush_flags);
194
CFRelease(coded_frame);
34
200
static int start_frame(AVCodecContext *avctx,
35
201
av_unused const uint8_t *buffer,
36
202
av_unused uint32_t size)
38
const H264Context *h = avctx->priv_data;
39
204
struct vda_context *vda_ctx = avctx->hwaccel_context;
40
struct vda_picture_context *pic_ctx = h->s.current_picture_ptr->f.hwaccel_picture_private;
42
206
if (!vda_ctx->decoder)
45
pic_ctx->bitstream = NULL;
46
pic_ctx->bitstream_size = 0;
209
vda_ctx->priv_bitstream_size = 0;
52
215
const uint8_t *buffer,
55
H264Context *h = avctx->priv_data;
56
218
struct vda_context *vda_ctx = avctx->hwaccel_context;
57
struct vda_picture_context *pic_ctx = h->s.current_picture_ptr->f.hwaccel_picture_private;
60
221
if (!vda_ctx->decoder)
63
tmp = av_realloc(pic_ctx->bitstream, pic_ctx->bitstream_size+size+4);
224
tmp = av_fast_realloc(vda_ctx->priv_bitstream,
225
&vda_ctx->priv_allocated_size,
226
vda_ctx->priv_bitstream_size + size + 4);
65
228
return AVERROR(ENOMEM);
67
pic_ctx->bitstream = tmp;
69
AV_WB32(pic_ctx->bitstream + pic_ctx->bitstream_size, size);
70
memcpy(pic_ctx->bitstream + pic_ctx->bitstream_size + 4, buffer, size);
72
pic_ctx->bitstream_size += size + 4;
230
vda_ctx->priv_bitstream = tmp;
232
AV_WB32(vda_ctx->priv_bitstream + vda_ctx->priv_bitstream_size, size);
233
memcpy(vda_ctx->priv_bitstream + vda_ctx->priv_bitstream_size + 4, buffer, size);
235
vda_ctx->priv_bitstream_size += size + 4;
79
242
H264Context *h = avctx->priv_data;
80
243
struct vda_context *vda_ctx = avctx->hwaccel_context;
81
struct vda_picture_context *pic_ctx = h->s.current_picture_ptr->f.hwaccel_picture_private;
82
244
AVFrame *frame = &h->s.current_picture_ptr->f;
85
if (!vda_ctx->decoder || !pic_ctx->bitstream)
247
if (!vda_ctx->decoder || !vda_ctx->priv_bitstream)
88
status = ff_vda_decoder_decode(vda_ctx, pic_ctx->bitstream,
89
pic_ctx->bitstream_size,
90
frame->reordered_opaque);
250
if (vda_ctx->use_sync_decoding) {
251
status = vda_sync_decode(vda_ctx);
252
frame->data[3] = (void*)vda_ctx->cv_buffer;
254
status = vda_decoder_decode(vda_ctx, vda_ctx->priv_bitstream,
255
vda_ctx->priv_bitstream_size,
256
frame->reordered_opaque);
93
260
av_log(avctx, AV_LOG_ERROR, "Failed to decode frame (%d)\n", status);
95
av_freep(&pic_ctx->bitstream);
265
int ff_vda_create_decoder(struct vda_context *vda_ctx,
269
OSStatus status = kVDADecoderNoErr;
274
CFMutableDictionaryRef config_info;
275
CFMutableDictionaryRef buffer_attributes;
276
CFMutableDictionaryRef io_surface_properties;
277
CFNumberRef cv_pix_fmt;
280
pthread_mutex_init(&vda_ctx->queue_mutex, NULL);
283
/* Each VCL NAL in the bistream sent to the decoder
284
* is preceded by a 4 bytes length header.
285
* Change the avcC atom header if needed, to signal headers of 4 bytes. */
286
if (extradata_size >= 4 && (extradata[4] & 0x03) != 0x03) {
287
uint8_t *rw_extradata;
289
if (!(rw_extradata = av_malloc(extradata_size)))
290
return AVERROR(ENOMEM);
292
memcpy(rw_extradata, extradata, extradata_size);
294
rw_extradata[4] |= 0x03;
296
avc_data = CFDataCreate(kCFAllocatorDefault, rw_extradata, extradata_size);
298
av_freep(&rw_extradata);
300
avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
303
config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
305
&kCFTypeDictionaryKeyCallBacks,
306
&kCFTypeDictionaryValueCallBacks);
308
height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
309
width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
310
format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
312
CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
313
CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
314
CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
315
CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
317
buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
319
&kCFTypeDictionaryKeyCallBacks,
320
&kCFTypeDictionaryValueCallBacks);
321
io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
323
&kCFTypeDictionaryKeyCallBacks,
324
&kCFTypeDictionaryValueCallBacks);
325
cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
327
&vda_ctx->cv_pix_fmt_type);
328
CFDictionarySetValue(buffer_attributes,
329
kCVPixelBufferPixelFormatTypeKey,
331
CFDictionarySetValue(buffer_attributes,
332
kCVPixelBufferIOSurfacePropertiesKey,
333
io_surface_properties);
335
status = VDADecoderCreate(config_info,
337
vda_decoder_callback,
345
CFRelease(config_info);
346
CFRelease(io_surface_properties);
347
CFRelease(cv_pix_fmt);
348
CFRelease(buffer_attributes);
353
int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
355
OSStatus status = kVDADecoderNoErr;
357
if (vda_ctx->decoder)
358
status = VDADecoderDestroy(vda_ctx->decoder);
361
vda_clear_queue(vda_ctx);
362
pthread_mutex_destroy(&vda_ctx->queue_mutex);
365
av_freep(&vda_ctx->priv_bitstream);