vda: support synchronous decoding.
[ffmpeg.git] / libavcodec / vda.c
1 /*
2  * VDA HW acceleration.
3  *
4  * copyright (c) 2011 Sebastien Zwickert
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include <CoreFoundation/CFDictionary.h>
24 #include <CoreFoundation/CFNumber.h>
25 #include <CoreFoundation/CFData.h>
26
27 #include "libavutil/avutil.h"
28 #include "vda_internal.h"
29
30 #if FF_API_VDA_ASYNC
31 #include <CoreFoundation/CFString.h>
32
33 /* Helper to create a dictionary according to the given pts. */
34 static CFDictionaryRef vda_dictionary_with_pts(int64_t i_pts)
35 {
36     CFStringRef key = CFSTR("FF_VDA_DECODER_PTS_KEY");
37     CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &i_pts);
38     CFDictionaryRef user_info = CFDictionaryCreate(kCFAllocatorDefault,
39                                                    (const void **)&key,
40                                                    (const void **)&value,
41                                                    1,
42                                                    &kCFTypeDictionaryKeyCallBacks,
43                                                    &kCFTypeDictionaryValueCallBacks);
44     CFRelease(value);
45     return user_info;
46 }
47
48 /* Helper to retrieve the pts from the given dictionary. */
49 static int64_t vda_pts_from_dictionary(CFDictionaryRef user_info)
50 {
51     CFNumberRef pts;
52     int64_t outValue = 0;
53
54     if (!user_info)
55         return 0;
56
57     pts = CFDictionaryGetValue(user_info, CFSTR("FF_VDA_DECODER_PTS_KEY"));
58
59     if (pts)
60         CFNumberGetValue(pts, kCFNumberSInt64Type, &outValue);
61
62     return outValue;
63 }
64
65 /* Removes and releases all frames from the queue. */
66 static void vda_clear_queue(struct vda_context *vda_ctx)
67 {
68     vda_frame *top_frame;
69
70     pthread_mutex_lock(&vda_ctx->queue_mutex);
71
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);
76     }
77
78     pthread_mutex_unlock(&vda_ctx->queue_mutex);
79 }
80 #endif
81
82 /* Decoder callback that adds the vda frame to the queue in display order. */
83 static void vda_decoder_callback (void *vda_hw_ctx,
84                                   CFDictionaryRef user_info,
85                                   OSStatus status,
86                                   uint32_t infoFlags,
87                                   CVImageBufferRef image_buffer)
88 {
89     struct vda_context *vda_ctx = (struct vda_context*)vda_hw_ctx;
90
91     if (!image_buffer)
92         return;
93
94     if (vda_ctx->cv_pix_fmt_type != CVPixelBufferGetPixelFormatType(image_buffer))
95         return;
96
97     if (vda_ctx->use_sync_decoding) {
98         vda_ctx->cv_buffer = CVPixelBufferRetain(image_buffer);
99     }
100     else {
101         vda_frame *new_frame;
102         vda_frame *queue_walker;
103
104         new_frame = av_mallocz(sizeof(vda_frame));
105         if (!new_frame)
106             return;
107
108         new_frame->next_frame = NULL;
109         new_frame->cv_buffer = CVPixelBufferRetain(image_buffer);
110         new_frame->pts = vda_pts_from_dictionary(user_info);
111
112         pthread_mutex_lock(&vda_ctx->queue_mutex);
113
114         queue_walker = vda_ctx->queue;
115
116         if (!queue_walker || (new_frame->pts < queue_walker->pts)) {
117             /* we have an empty queue, or this frame earlier than the current queue head */
118             new_frame->next_frame = queue_walker;
119             vda_ctx->queue = new_frame;
120         } else {
121             /* walk the queue and insert this frame where it belongs in display order */
122             vda_frame *next_frame;
123
124             while (1) {
125                 next_frame = queue_walker->next_frame;
126
127                 if (!next_frame || (new_frame->pts < next_frame->pts)) {
128                     new_frame->next_frame = next_frame;
129                     queue_walker->next_frame = new_frame;
130                     break;
131                 }
132                 queue_walker = next_frame;
133             }
134         }
135
136         pthread_mutex_unlock(&vda_ctx->queue_mutex);
137     }
138 }
139
140 int ff_vda_create_decoder(struct vda_context *vda_ctx,
141                           uint8_t *extradata,
142                           int extradata_size)
143 {
144     OSStatus status;
145     CFNumberRef height;
146     CFNumberRef width;
147     CFNumberRef format;
148     CFDataRef avc_data;
149     CFMutableDictionaryRef config_info;
150     CFMutableDictionaryRef buffer_attributes;
151     CFMutableDictionaryRef io_surface_properties;
152     CFNumberRef cv_pix_fmt;
153
154     vda_ctx->bitstream = NULL;
155     vda_ctx->ref_size = 0;
156
157 #if FF_API_VDA_ASYNC
158     pthread_mutex_init(&vda_ctx->queue_mutex, NULL);
159 #endif
160
161     /* Each VCL NAL in the bistream sent to the decoder
162      * is preceded by a 4 bytes length header.
163      * Change the avcC atom header if needed, to signal headers of 4 bytes. */
164     if (extradata_size >= 4 && (extradata[4] & 0x03) != 0x03) {
165         uint8_t *rw_extradata;
166
167         if (!(rw_extradata = av_malloc(extradata_size)))
168             return AVERROR(ENOMEM);
169
170         memcpy(rw_extradata, extradata, extradata_size);
171
172         rw_extradata[4] |= 0x03;
173
174         avc_data = CFDataCreate(kCFAllocatorDefault, rw_extradata, extradata_size);
175
176         av_freep(&rw_extradata);
177     } else {
178         avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
179     }
180
181     config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
182                                             4,
183                                             &kCFTypeDictionaryKeyCallBacks,
184                                             &kCFTypeDictionaryValueCallBacks);
185
186     height   = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
187     width    = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
188     format   = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
189
190     CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
191     CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
192     CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
193     CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
194
195     buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
196                                                   2,
197                                                   &kCFTypeDictionaryKeyCallBacks,
198                                                   &kCFTypeDictionaryValueCallBacks);
199     io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
200                                                       0,
201                                                       &kCFTypeDictionaryKeyCallBacks,
202                                                       &kCFTypeDictionaryValueCallBacks);
203     cv_pix_fmt  = CFNumberCreate(kCFAllocatorDefault,
204                                  kCFNumberSInt32Type,
205                                  &vda_ctx->cv_pix_fmt_type);
206     CFDictionarySetValue(buffer_attributes,
207                          kCVPixelBufferPixelFormatTypeKey,
208                          cv_pix_fmt);
209     CFDictionarySetValue(buffer_attributes,
210                          kCVPixelBufferIOSurfacePropertiesKey,
211                          io_surface_properties);
212
213     status = VDADecoderCreate(config_info,
214                               buffer_attributes,
215                               (VDADecoderOutputCallback *)vda_decoder_callback,
216                               vda_ctx,
217                               &vda_ctx->decoder);
218
219     CFRelease(height);
220     CFRelease(width);
221     CFRelease(format);
222     CFRelease(avc_data);
223     CFRelease(config_info);
224     CFRelease(io_surface_properties);
225     CFRelease(cv_pix_fmt);
226     CFRelease(buffer_attributes);
227
228     return status;
229 }
230
231 int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
232 {
233     OSStatus status = kVDADecoderNoErr;
234
235     if (vda_ctx->decoder)
236         status = VDADecoderDestroy(vda_ctx->decoder);
237
238 #if FF_API_VDA_ASYNC
239     vda_clear_queue(vda_ctx);
240     pthread_mutex_destroy(&vda_ctx->queue_mutex);
241 #endif
242     if (vda_ctx->bitstream)
243         av_freep(&vda_ctx->bitstream);
244
245     return status;
246 }
247
248 #if FF_API_VDA_ASYNC
249 vda_frame *ff_vda_queue_pop(struct vda_context *vda_ctx)
250 {
251     vda_frame *top_frame;
252
253     if (!vda_ctx->queue)
254         return NULL;
255
256     pthread_mutex_lock(&vda_ctx->queue_mutex);
257     top_frame = vda_ctx->queue;
258     vda_ctx->queue = top_frame->next_frame;
259     pthread_mutex_unlock(&vda_ctx->queue_mutex);
260
261     return top_frame;
262 }
263
264 void ff_vda_release_vda_frame(vda_frame *frame)
265 {
266     if (frame) {
267         CVPixelBufferRelease(frame->cv_buffer);
268         av_freep(&frame);
269     }
270 }
271
272 int ff_vda_decoder_decode(struct vda_context *vda_ctx,
273                           uint8_t *bitstream,
274                           int bitstream_size,
275                           int64_t frame_pts)
276 {
277     OSStatus status;
278     CFDictionaryRef user_info;
279     CFDataRef coded_frame;
280
281     coded_frame = CFDataCreate(kCFAllocatorDefault, bitstream, bitstream_size);
282     user_info = vda_dictionary_with_pts(frame_pts);
283
284     status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, user_info);
285
286     CFRelease(user_info);
287     CFRelease(coded_frame);
288
289     return status;
290 }
291 #endif
292
293 int ff_vda_sync_decode(struct vda_context *vda_ctx)
294 {
295     OSStatus status;
296     CFDataRef coded_frame;
297     uint32_t flush_flags = 1 << 0; ///< kVDADecoderFlush_emitFrames
298
299     coded_frame = CFDataCreate(kCFAllocatorDefault,
300                                vda_ctx->bitstream,
301                                vda_ctx->bitstream_size);
302
303     status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, NULL);
304
305     if (kVDADecoderNoErr == status)
306         status = VDADecoderFlush(vda_ctx->decoder, flush_flags);
307
308     CFRelease(coded_frame);
309
310     return status;
311 }