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