]> git.sesse.net Git - ffmpeg/blob - libavcodec/vda.c
Merge commit '0c00fd80ee4791bd70b634084307fc9f179e0412'
[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 <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     /* Each VCL NAL in the bistream sent to the decoder
153      * is preceded by a 4 bytes length header.
154      * Change the avcC atom header if needed, to signal headers of 4 bytes. */
155     if (extradata_size >= 4 && (extradata[4] & 0x03) != 0x03) {
156         uint8_t *rw_extradata;
157
158         if (!(rw_extradata = av_malloc(extradata_size)))
159             return AVERROR(ENOMEM);
160
161         memcpy(rw_extradata, extradata, extradata_size);
162
163         rw_extradata[4] |= 0x03;
164
165         avc_data = CFDataCreate(kCFAllocatorDefault, rw_extradata, extradata_size);
166
167         av_freep(&rw_extradata);
168     } else {
169         avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
170     }
171
172     config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
173                                             4,
174                                             &kCFTypeDictionaryKeyCallBacks,
175                                             &kCFTypeDictionaryValueCallBacks);
176
177     height   = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
178     width    = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
179     format   = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
180
181     CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
182     CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
183     CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
184     CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
185
186     buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
187                                                   2,
188                                                   &kCFTypeDictionaryKeyCallBacks,
189                                                   &kCFTypeDictionaryValueCallBacks);
190     io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
191                                                       0,
192                                                       &kCFTypeDictionaryKeyCallBacks,
193                                                       &kCFTypeDictionaryValueCallBacks);
194     cv_pix_fmt  = CFNumberCreate(kCFAllocatorDefault,
195                                  kCFNumberSInt32Type,
196                                  &vda_ctx->cv_pix_fmt_type);
197     CFDictionarySetValue(buffer_attributes,
198                          kCVPixelBufferPixelFormatTypeKey,
199                          cv_pix_fmt);
200     CFDictionarySetValue(buffer_attributes,
201                          kCVPixelBufferIOSurfacePropertiesKey,
202                          io_surface_properties);
203
204     status = VDADecoderCreate(config_info,
205                               buffer_attributes,
206                               (VDADecoderOutputCallback *)vda_decoder_callback,
207                               vda_ctx,
208                               &vda_ctx->decoder);
209
210     CFRelease(height);
211     CFRelease(width);
212     CFRelease(format);
213     CFRelease(avc_data);
214     CFRelease(config_info);
215     CFRelease(io_surface_properties);
216     CFRelease(cv_pix_fmt);
217     CFRelease(buffer_attributes);
218
219     if (kVDADecoderNoErr != status)
220         return status;
221
222     return 0;
223 }
224
225 int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
226 {
227     OSStatus status = kVDADecoderNoErr;
228
229     if (vda_ctx->decoder)
230         status = VDADecoderDestroy(vda_ctx->decoder);
231
232     vda_clear_queue(vda_ctx);
233
234     pthread_mutex_destroy(&vda_ctx->queue_mutex);
235
236     if (vda_ctx->bitstream)
237         av_freep(&vda_ctx->bitstream);
238
239     if (kVDADecoderNoErr != status)
240         return status;
241
242     return 0;
243 }
244
245 vda_frame *ff_vda_queue_pop(struct vda_context *vda_ctx)
246 {
247     vda_frame *top_frame;
248
249     if (!vda_ctx->queue)
250         return NULL;
251
252     pthread_mutex_lock(&vda_ctx->queue_mutex);
253     top_frame = vda_ctx->queue;
254     vda_ctx->queue = top_frame->next_frame;
255     pthread_mutex_unlock(&vda_ctx->queue_mutex);
256
257     return top_frame;
258 }
259
260 void ff_vda_release_vda_frame(vda_frame *frame)
261 {
262     if (frame) {
263         CVPixelBufferRelease(frame->cv_buffer);
264         av_freep(&frame);
265     }
266 }
267
268 int ff_vda_decoder_decode(struct vda_context *vda_ctx,
269                           uint8_t *bitstream,
270                           int bitstream_size,
271                           int64_t frame_pts)
272 {
273     OSStatus status = kVDADecoderNoErr;
274     CFDictionaryRef user_info;
275     CFDataRef coded_frame;
276
277     coded_frame = CFDataCreate(kCFAllocatorDefault, bitstream, bitstream_size);
278     user_info = vda_dictionary_with_pts(frame_pts);
279
280     status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, user_info);
281
282     CFRelease(user_info);
283     CFRelease(coded_frame);
284
285     if (kVDADecoderNoErr != status)
286         return status;
287
288     return 0;
289 }