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