]> git.sesse.net Git - ffmpeg/blob - libavcodec/vda.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / 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     if (av_lockmgr_register(vda_lock_operation))
172         return -1;
173
174     vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_CREATE);
175
176     config_info = (CFDictionaryCreateMutable(kCFAllocatorDefault,
177                                              4,
178                                              &kCFTypeDictionaryKeyCallBacks,
179                                              &kCFTypeDictionaryValueCallBacks));
180
181     height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
182     width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
183     format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
184     avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
185
186     CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
187     CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
188     CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
189     CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
190
191     buffer_attributes = (CFDictionaryCreateMutable(kCFAllocatorDefault,
192                                                    2,
193                                                    &kCFTypeDictionaryKeyCallBacks,
194                                                    &kCFTypeDictionaryValueCallBacks));
195     io_surface_properties = (CFDictionaryCreateMutable(kCFAllocatorDefault,
196                                                        0,
197                                                        &kCFTypeDictionaryKeyCallBacks,
198                                                        &kCFTypeDictionaryValueCallBacks));
199     cv_pix_fmt  = CFNumberCreate(kCFAllocatorDefault,
200                                  kCFNumberSInt32Type,
201                                  &vda_ctx->cv_pix_fmt_type);
202     CFDictionarySetValue(buffer_attributes,
203                          kCVPixelBufferPixelFormatTypeKey,
204                          cv_pix_fmt);
205     CFDictionarySetValue(buffer_attributes,
206                          kCVPixelBufferIOSurfacePropertiesKey,
207                          io_surface_properties);
208
209     status = VDADecoderCreate( config_info,
210                                buffer_attributes,
211                                (VDADecoderOutputCallback *)vda_decoder_callback,
212                                (void *)vda_ctx,
213                                &vda_ctx->decoder );
214
215     CFRelease(height);
216     CFRelease(width);
217     CFRelease(format);
218     CFRelease(avc_data);
219     CFRelease(config_info);
220     CFRelease(io_surface_properties);
221     CFRelease(cv_pix_fmt);
222     CFRelease(buffer_attributes);
223
224     if (kVDADecoderNoErr != status)
225         return status;
226
227     return 0;
228 }
229
230 int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
231 {
232     OSStatus status = kVDADecoderNoErr;
233
234     if (vda_ctx->decoder)
235         status = VDADecoderDestroy(vda_ctx->decoder);
236
237     vda_clear_queue(vda_ctx);
238
239     if (vda_ctx->queue_mutex != NULL)
240         vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_DESTROY);
241
242     if (kVDADecoderNoErr != status)
243         return status;
244
245     return 0;
246 }
247
248 vda_frame *ff_vda_queue_pop(struct vda_context *vda_ctx)
249 {
250     vda_frame *top_frame;
251
252     if (!vda_ctx->queue)
253         return NULL;
254
255     vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_OBTAIN);
256     top_frame = vda_ctx->queue;
257     vda_ctx->queue = top_frame->next_frame;
258     vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_RELEASE);
259
260     return top_frame;
261 }
262
263 void ff_vda_release_vda_frame(vda_frame *frame)
264 {
265     if (frame != NULL)
266     {
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 = kVDADecoderNoErr;
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     if (kVDADecoderNoErr != status)
290         return status;
291
292     return 0;
293 }
294
295 /* @} */