vda: better frame allocation.
[ffmpeg.git] / libavcodec / vda_h264.c
1 /*
2  * VDA H264 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 "h264.h"
29 #include "vda.h"
30
31 #if FF_API_VDA_ASYNC
32 #include <CoreFoundation/CFString.h>
33
34 /* Helper to create a dictionary according to the given pts. */
35 static CFDictionaryRef vda_dictionary_with_pts(int64_t i_pts)
36 {
37     CFStringRef key = CFSTR("FF_VDA_DECODER_PTS_KEY");
38     CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, 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 /* Removes and releases 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
82 static int vda_decoder_decode(struct vda_context *vda_ctx,
83                               uint8_t *bitstream,
84                               int bitstream_size,
85                               int64_t frame_pts)
86 {
87     OSStatus status;
88     CFDictionaryRef user_info;
89     CFDataRef coded_frame;
90
91     coded_frame = CFDataCreate(kCFAllocatorDefault, bitstream, bitstream_size);
92     user_info = vda_dictionary_with_pts(frame_pts);
93
94     status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, user_info);
95
96     CFRelease(user_info);
97     CFRelease(coded_frame);
98
99     return status;
100 }
101
102 vda_frame *ff_vda_queue_pop(struct vda_context *vda_ctx)
103 {
104     vda_frame *top_frame;
105
106     if (!vda_ctx->queue)
107         return NULL;
108
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);
113
114     return top_frame;
115 }
116
117 void ff_vda_release_vda_frame(vda_frame *frame)
118 {
119     if (frame) {
120         CVPixelBufferRelease(frame->cv_buffer);
121         av_freep(&frame);
122     }
123 }
124 #endif
125
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,
129                                   OSStatus status,
130                                   uint32_t infoFlags,
131                                   CVImageBufferRef image_buffer)
132 {
133     struct vda_context *vda_ctx = vda_hw_ctx;
134
135     if (!image_buffer)
136         return;
137
138     if (vda_ctx->cv_pix_fmt_type != CVPixelBufferGetPixelFormatType(image_buffer))
139         return;
140
141     if (vda_ctx->use_sync_decoding) {
142         vda_ctx->cv_buffer = CVPixelBufferRetain(image_buffer);
143     } else {
144         vda_frame *new_frame;
145         vda_frame *queue_walker;
146
147         if (!(new_frame = av_mallocz(sizeof(*new_frame))))
148             return;
149
150         new_frame->next_frame = NULL;
151         new_frame->cv_buffer = CVPixelBufferRetain(image_buffer);
152         new_frame->pts = vda_pts_from_dictionary(user_info);
153
154         pthread_mutex_lock(&vda_ctx->queue_mutex);
155
156         queue_walker = vda_ctx->queue;
157
158         if (!queue_walker || (new_frame->pts < queue_walker->pts)) {
159             /* we have an empty queue, or this frame earlier than the current queue head */
160             new_frame->next_frame = queue_walker;
161             vda_ctx->queue = new_frame;
162         } else {
163             /* walk the queue and insert this frame where it belongs in display order */
164             vda_frame *next_frame;
165
166             while (1) {
167                 next_frame = queue_walker->next_frame;
168
169                 if (!next_frame || (new_frame->pts < next_frame->pts)) {
170                     new_frame->next_frame = next_frame;
171                     queue_walker->next_frame = new_frame;
172                     break;
173                 }
174                 queue_walker = next_frame;
175             }
176         }
177
178         pthread_mutex_unlock(&vda_ctx->queue_mutex);
179     }
180 }
181
182 static int vda_sync_decode(struct vda_context *vda_ctx)
183 {
184     OSStatus status;
185     CFDataRef coded_frame;
186     uint32_t flush_flags = 1 << 0; ///< kVDADecoderFlush_emitFrames
187
188     coded_frame = CFDataCreate(kCFAllocatorDefault,
189                                vda_ctx->priv_bitstream,
190                                vda_ctx->priv_bitstream_size);
191
192     status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, NULL);
193
194     if (kVDADecoderNoErr == status)
195         status = VDADecoderFlush(vda_ctx->decoder, flush_flags);
196
197     CFRelease(coded_frame);
198
199     return status;
200 }
201
202 static int start_frame(AVCodecContext *avctx,
203                        av_unused const uint8_t *buffer,
204                        av_unused uint32_t size)
205 {
206     struct vda_context *vda_ctx = avctx->hwaccel_context;
207
208     if (!vda_ctx->decoder)
209         return -1;
210
211     vda_ctx->priv_bitstream_size = 0;
212
213     return 0;
214 }
215
216 static int decode_slice(AVCodecContext *avctx,
217                         const uint8_t *buffer,
218                         uint32_t size)
219 {
220     struct vda_context *vda_ctx = avctx->hwaccel_context;
221     void *tmp;
222
223     if (!vda_ctx->decoder)
224         return -1;
225
226     tmp = av_fast_realloc(vda_ctx->priv_bitstream,
227                           &vda_ctx->priv_allocated_size,
228                           vda_ctx->priv_bitstream_size + size + 4);
229     if (!tmp)
230         return AVERROR(ENOMEM);
231
232     vda_ctx->priv_bitstream = tmp;
233
234     AV_WB32(vda_ctx->priv_bitstream + vda_ctx->priv_bitstream_size, size);
235     memcpy(vda_ctx->priv_bitstream + vda_ctx->priv_bitstream_size + 4, buffer, size);
236
237     vda_ctx->priv_bitstream_size += size + 4;
238
239     return 0;
240 }
241
242 static int end_frame(AVCodecContext *avctx)
243 {
244     H264Context *h                      = avctx->priv_data;
245     struct vda_context *vda_ctx         = avctx->hwaccel_context;
246     AVFrame *frame                      = &h->s.current_picture_ptr->f;
247     int status;
248
249     if (!vda_ctx->decoder || !vda_ctx->priv_bitstream)
250         return -1;
251
252     if (vda_ctx->use_sync_decoding) {
253         status = vda_sync_decode(vda_ctx);
254         frame->data[3] = (void*)vda_ctx->cv_buffer;
255     } else {
256         status = vda_decoder_decode(vda_ctx, vda_ctx->priv_bitstream,
257                                     vda_ctx->priv_bitstream_size,
258                                     frame->reordered_opaque);
259     }
260
261     if (status)
262         av_log(avctx, AV_LOG_ERROR, "Failed to decode frame (%d)\n", status);
263
264     return status;
265 }
266
267 int ff_vda_create_decoder(struct vda_context *vda_ctx,
268                           uint8_t *extradata,
269                           int extradata_size)
270 {
271     OSStatus status;
272     CFNumberRef height;
273     CFNumberRef width;
274     CFNumberRef format;
275     CFDataRef avc_data;
276     CFMutableDictionaryRef config_info;
277     CFMutableDictionaryRef buffer_attributes;
278     CFMutableDictionaryRef io_surface_properties;
279     CFNumberRef cv_pix_fmt;
280
281     vda_ctx->priv_bitstream = NULL;
282     vda_ctx->priv_allocated_size = 0;
283
284 #if FF_API_VDA_ASYNC
285     pthread_mutex_init(&vda_ctx->queue_mutex, NULL);
286 #endif
287
288     /* Each VCL NAL in the bistream sent to the decoder
289      * is preceded by a 4 bytes length header.
290      * Change the avcC atom header if needed, to signal headers of 4 bytes. */
291     if (extradata_size >= 4 && (extradata[4] & 0x03) != 0x03) {
292         uint8_t *rw_extradata;
293
294         if (!(rw_extradata = av_malloc(extradata_size)))
295             return AVERROR(ENOMEM);
296
297         memcpy(rw_extradata, extradata, extradata_size);
298
299         rw_extradata[4] |= 0x03;
300
301         avc_data = CFDataCreate(kCFAllocatorDefault, rw_extradata, extradata_size);
302
303         av_freep(&rw_extradata);
304     } else {
305         avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
306     }
307
308     config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
309                                             4,
310                                             &kCFTypeDictionaryKeyCallBacks,
311                                             &kCFTypeDictionaryValueCallBacks);
312
313     height   = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
314     width    = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
315     format   = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
316
317     CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
318     CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
319     CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
320     CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
321
322     buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
323                                                   2,
324                                                   &kCFTypeDictionaryKeyCallBacks,
325                                                   &kCFTypeDictionaryValueCallBacks);
326     io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
327                                                       0,
328                                                       &kCFTypeDictionaryKeyCallBacks,
329                                                       &kCFTypeDictionaryValueCallBacks);
330     cv_pix_fmt  = CFNumberCreate(kCFAllocatorDefault,
331                                  kCFNumberSInt32Type,
332                                  &vda_ctx->cv_pix_fmt_type);
333     CFDictionarySetValue(buffer_attributes,
334                          kCVPixelBufferPixelFormatTypeKey,
335                          cv_pix_fmt);
336     CFDictionarySetValue(buffer_attributes,
337                          kCVPixelBufferIOSurfacePropertiesKey,
338                          io_surface_properties);
339
340     status = VDADecoderCreate(config_info,
341                               buffer_attributes,
342                               vda_decoder_callback,
343                               vda_ctx,
344                               &vda_ctx->decoder);
345
346     CFRelease(height);
347     CFRelease(width);
348     CFRelease(format);
349     CFRelease(avc_data);
350     CFRelease(config_info);
351     CFRelease(io_surface_properties);
352     CFRelease(cv_pix_fmt);
353     CFRelease(buffer_attributes);
354
355     return status;
356 }
357
358 int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
359 {
360     OSStatus status = kVDADecoderNoErr;
361
362     if (vda_ctx->decoder)
363         status = VDADecoderDestroy(vda_ctx->decoder);
364
365 #if FF_API_VDA_ASYNC
366     vda_clear_queue(vda_ctx);
367     pthread_mutex_destroy(&vda_ctx->queue_mutex);
368 #endif
369     av_freep(&vda_ctx->priv_bitstream);
370
371     return status;
372 }
373
374 AVHWAccel ff_h264_vda_hwaccel = {
375     .name           = "h264_vda",
376     .type           = AVMEDIA_TYPE_VIDEO,
377     .id             = AV_CODEC_ID_H264,
378     .pix_fmt        = PIX_FMT_VDA_VLD,
379     .start_frame    = start_frame,
380     .decode_slice   = decode_slice,
381     .end_frame      = end_frame,
382 };