indeo: clear allocated band buffers
[ffmpeg.git] / libavcodec / vda.c
1 /*
2  * VDA hardware acceleration
3  *
4  * copyright (c) 2011 Sebastien Zwickert
5  *
6  * This file is part of Libav.
7  *
8  * Libav 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  * Libav 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 Libav; 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,
37                                                kCFNumberSInt64Type, &i_pts);
38     CFDictionaryRef user_info = CFDictionaryCreate(kCFAllocatorDefault,
39                                                    (const void **)&key,
40                                                    (const void **)&value,
41                                                    1,
42                                                    &kCFTypeDictionaryKeyCallBacks,
43                                                    &kCFTypeDictionaryValueCallBacks);
44     CFRelease(value);
45     return user_info;
46 }
47
48 /* helper to retrieve the pts from the given dictionary */
49 static int64_t vda_pts_from_dictionary(CFDictionaryRef user_info)
50 {
51     CFNumberRef pts;
52     int64_t outValue = 0;
53
54     if (!user_info)
55         return 0;
56
57     pts = CFDictionaryGetValue(user_info, CFSTR("FF_VDA_DECODER_PTS_KEY"));
58
59     if (pts)
60         CFNumberGetValue(pts, kCFNumberSInt64Type, &outValue);
61
62     return outValue;
63 }
64
65 /* Remove and release all frames from the queue. */
66 static void vda_clear_queue(struct vda_context *vda_ctx)
67 {
68     vda_frame *top_frame;
69
70     pthread_mutex_lock(&vda_ctx->queue_mutex);
71
72     while (vda_ctx->queue) {
73         top_frame      = vda_ctx->queue;
74         vda_ctx->queue = top_frame->next_frame;
75         ff_vda_release_vda_frame(top_frame);
76     }
77
78     pthread_mutex_unlock(&vda_ctx->queue_mutex);
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 = 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     if (!(new_frame = av_mallocz(sizeof(vda_frame))))
99         return;
100     new_frame->next_frame = NULL;
101     new_frame->cv_buffer  = CVPixelBufferRetain(image_buffer);
102     new_frame->pts        = vda_pts_from_dictionary(user_info);
103
104     pthread_mutex_lock(&vda_ctx->queue_mutex);
105
106     queue_walker = vda_ctx->queue;
107
108     if (!queue_walker || new_frame->pts < queue_walker->pts) {
109         /* we have an empty queue, or this frame earlier than the current queue head */
110         new_frame->next_frame = queue_walker;
111         vda_ctx->queue        = new_frame;
112     } else {
113         /* walk the queue and insert this frame where it belongs in display order */
114         vda_frame *next_frame;
115         while (1) {
116             next_frame = queue_walker->next_frame;
117             if (!next_frame || new_frame->pts < next_frame->pts) {
118                 new_frame->next_frame    = next_frame;
119                 queue_walker->next_frame = new_frame;
120                 break;
121             }
122             queue_walker = next_frame;
123         }
124     }
125
126     pthread_mutex_unlock(&vda_ctx->queue_mutex);
127 }
128
129 int ff_vda_create_decoder(struct vda_context *vda_ctx,
130                           uint8_t *extradata,
131                           int extradata_size)
132 {
133     OSStatus status = kVDADecoderNoErr;
134     CFNumberRef height;
135     CFNumberRef width;
136     CFNumberRef format;
137     CFDataRef avc_data;
138     CFMutableDictionaryRef config_info;
139     CFMutableDictionaryRef buffer_attributes;
140     CFMutableDictionaryRef io_surface_properties;
141     CFNumberRef cv_pix_fmt;
142
143     pthread_mutex_init(&vda_ctx->queue_mutex, NULL);
144
145     /* Each VCL NAL in the bistream sent to the decoder
146      * is preceeded by a 4 bytes length header.
147      * Change the avcC atom header if needed, to signal headers of 4 bytes. */
148     if (extradata_size >= 4 && (extradata[4] & 0x03) != 0x03) {
149         uint8_t *rw_extradata;
150
151         if (!(rw_extradata = av_malloc(extradata_size)))
152             return AVERROR(ENOMEM);
153
154         memcpy(rw_extradata, extradata, extradata_size);
155
156         rw_extradata[4] |= 0x03;
157
158         avc_data = CFDataCreate(kCFAllocatorDefault, rw_extradata, extradata_size);
159
160         av_freep(&rw_extradata);
161     } else {
162         avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
163     }
164
165     config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
166                                             4,
167                                             &kCFTypeDictionaryKeyCallBacks,
168                                             &kCFTypeDictionaryValueCallBacks);
169
170     height   = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
171     width    = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
172     format   = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
173
174     CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
175     CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
176     CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
177     CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
178
179     buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
180                                                   2,
181                                                   &kCFTypeDictionaryKeyCallBacks,
182                                                   &kCFTypeDictionaryValueCallBacks);
183     io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
184                                                       0,
185                                                       &kCFTypeDictionaryKeyCallBacks,
186                                                       &kCFTypeDictionaryValueCallBacks);
187     cv_pix_fmt      = CFNumberCreate(kCFAllocatorDefault,
188                                      kCFNumberSInt32Type,
189                                      &vda_ctx->cv_pix_fmt_type);
190     CFDictionarySetValue(buffer_attributes,
191                          kCVPixelBufferPixelFormatTypeKey,
192                          cv_pix_fmt);
193     CFDictionarySetValue(buffer_attributes,
194                          kCVPixelBufferIOSurfacePropertiesKey,
195                          io_surface_properties);
196
197     status = VDADecoderCreate(config_info,
198                               buffer_attributes,
199                               vda_decoder_callback,
200                               vda_ctx,
201                               &vda_ctx->decoder);
202
203     CFRelease(height);
204     CFRelease(width);
205     CFRelease(format);
206     CFRelease(avc_data);
207     CFRelease(config_info);
208     CFRelease(io_surface_properties);
209     CFRelease(cv_pix_fmt);
210     CFRelease(buffer_attributes);
211
212     if (kVDADecoderNoErr != status)
213         return status;
214
215     return 0;
216 }
217
218 int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
219 {
220     OSStatus status = kVDADecoderNoErr;
221
222     if (vda_ctx->decoder)
223         status = VDADecoderDestroy(vda_ctx->decoder);
224
225     vda_clear_queue(vda_ctx);
226
227     pthread_mutex_destroy(&vda_ctx->queue_mutex);
228
229     if (kVDADecoderNoErr != status)
230         return status;
231
232     return 0;
233 }
234
235 vda_frame *ff_vda_queue_pop(struct vda_context *vda_ctx)
236 {
237     vda_frame *top_frame;
238
239     if (!vda_ctx->queue)
240         return NULL;
241
242     pthread_mutex_lock(&vda_ctx->queue_mutex);
243     top_frame      = vda_ctx->queue;
244     vda_ctx->queue = top_frame->next_frame;
245     pthread_mutex_unlock(&vda_ctx->queue_mutex);
246
247     return top_frame;
248 }
249
250 void ff_vda_release_vda_frame(vda_frame *frame)
251 {
252     if (frame) {
253         CVPixelBufferRelease(frame->cv_buffer);
254         av_freep(&frame);
255     }
256 }
257
258 int ff_vda_decoder_decode(struct vda_context *vda_ctx,
259                           uint8_t *bitstream,
260                           int bitstream_size,
261                           int64_t frame_pts)
262 {
263     OSStatus status = kVDADecoderNoErr;
264     CFDictionaryRef user_info;
265     CFDataRef coded_frame;
266
267     coded_frame = CFDataCreate(kCFAllocatorDefault, bitstream, bitstream_size);
268     user_info   = vda_dictionary_with_pts(frame_pts);
269     status      = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, user_info);
270
271     CFRelease(user_info);
272     CFRelease(coded_frame);
273
274     if (kVDADecoderNoErr != status)
275         return status;
276
277     return 0;
278 }