23 #include <CoreFoundation/CFDictionary.h>
24 #include <CoreFoundation/CFNumber.h>
25 #include <CoreFoundation/CFData.h>
32 #include <CoreFoundation/CFString.h>
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, kCFNumberSInt64Type, &i_pts);
39 CFDictionaryRef user_info = CFDictionaryCreate(kCFAllocatorDefault,
41 (
const void **)&value,
43 &kCFTypeDictionaryKeyCallBacks,
44 &kCFTypeDictionaryValueCallBacks);
50 static int64_t vda_pts_from_dictionary(CFDictionaryRef user_info)
58 pts = CFDictionaryGetValue(user_info, CFSTR(
"FF_VDA_DECODER_PTS_KEY"));
61 CFNumberGetValue(pts, kCFNumberSInt64Type, &outValue);
67 static void vda_clear_queue(
struct vda_context *vda_ctx)
73 while (vda_ctx->queue) {
74 top_frame = vda_ctx->queue;
75 vda_ctx->queue = top_frame->next_frame;
76 ff_vda_release_vda_frame(top_frame);
82 static int vda_decoder_decode(
struct vda_context *vda_ctx,
88 CFDictionaryRef user_info;
89 CFDataRef coded_frame;
91 coded_frame = CFDataCreate(kCFAllocatorDefault, bitstream, bitstream_size);
92 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;
110 top_frame = vda_ctx->queue;
111 vda_ctx->queue = top_frame->next_frame;
117 void ff_vda_release_vda_frame(vda_frame *
frame)
120 CVPixelBufferRelease(frame->cv_buffer);
128 CFDictionaryRef user_info,
131 CVImageBufferRef image_buffer)
138 if (vda_ctx->
cv_pix_fmt_type != CVPixelBufferGetPixelFormatType(image_buffer))
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))))
150 new_frame->next_frame =
NULL;
151 new_frame->cv_buffer = CVPixelBufferRetain(image_buffer);
152 new_frame->pts = vda_pts_from_dictionary(user_info);
156 queue_walker = vda_ctx->queue;
158 if (!queue_walker || (new_frame->pts < queue_walker->pts)) {
160 new_frame->next_frame = queue_walker;
161 vda_ctx->queue = new_frame;
164 vda_frame *next_frame;
167 next_frame = queue_walker->next_frame;
169 if (!next_frame || (new_frame->pts < next_frame->pts)) {
170 new_frame->next_frame = next_frame;
171 queue_walker->next_frame = new_frame;
174 queue_walker = next_frame;
185 CFDataRef coded_frame;
186 uint32_t flush_flags = 1 << 0;
188 coded_frame = CFDataCreate(kCFAllocatorDefault,
192 status = VDADecoderDecode(vda_ctx->
decoder, 0, coded_frame,
NULL);
194 if (kVDADecoderNoErr == status)
195 status = VDADecoderFlush(vda_ctx->
decoder, flush_flags);
197 CFRelease(coded_frame);
277 CFMutableDictionaryRef config_info;
278 CFMutableDictionaryRef buffer_attributes;
279 CFMutableDictionaryRef io_surface_properties;
280 CFNumberRef cv_pix_fmt;
292 if (extradata_size >= 4 && (extradata[4] & 0x03) != 0x03) {
295 if (!(rw_extradata =
av_malloc(extradata_size)))
298 memcpy(rw_extradata, extradata, extradata_size);
300 rw_extradata[4] |= 0x03;
302 avc_data = CFDataCreate(kCFAllocatorDefault, rw_extradata, extradata_size);
306 avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
309 config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
311 &kCFTypeDictionaryKeyCallBacks,
312 &kCFTypeDictionaryValueCallBacks);
314 height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->
height);
315 width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->
width);
316 format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->
format);
318 CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
319 CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
320 CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
321 CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
323 buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
325 &kCFTypeDictionaryKeyCallBacks,
326 &kCFTypeDictionaryValueCallBacks);
327 io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
329 &kCFTypeDictionaryKeyCallBacks,
330 &kCFTypeDictionaryValueCallBacks);
331 cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
334 CFDictionarySetValue(buffer_attributes,
335 kCVPixelBufferPixelFormatTypeKey,
337 CFDictionarySetValue(buffer_attributes,
338 kCVPixelBufferIOSurfacePropertiesKey,
339 io_surface_properties);
341 status = VDADecoderCreate(config_info,
351 CFRelease(config_info);
352 CFRelease(io_surface_properties);
353 CFRelease(cv_pix_fmt);
354 CFRelease(buffer_attributes);
361 OSStatus
status = kVDADecoderNoErr;
364 status = VDADecoderDestroy(vda_ctx->
decoder);
367 vda_clear_queue(vda_ctx);