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