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