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