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