lavc: remove disabled FF_API_VDA_ASYNC cruft
[ffmpeg.git] / libavcodec / vda_h264.c
1 /*
2  * VDA H.264 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 <CoreFoundation/CFNumber.h>
24 #include <CoreFoundation/CFData.h>
25 #include <CoreFoundation/CFString.h>
26
27 #include "libavutil/avutil.h"
28 #include "h264.h"
29 #include "vda.h"
30
31 /* Decoder callback that adds the VDA frame to the queue in display order. */
32 static void vda_decoder_callback(void *vda_hw_ctx,
33                                  CFDictionaryRef user_info,
34                                  OSStatus status,
35                                  uint32_t infoFlags,
36                                  CVImageBufferRef image_buffer)
37 {
38     struct vda_context *vda_ctx = vda_hw_ctx;
39
40     if (!image_buffer)
41         return;
42
43     if (vda_ctx->cv_pix_fmt_type != CVPixelBufferGetPixelFormatType(image_buffer))
44         return;
45
46     if (vda_ctx->use_sync_decoding) {
47         vda_ctx->cv_buffer = CVPixelBufferRetain(image_buffer);
48     } else {
49         vda_frame *new_frame;
50         vda_frame *queue_walker;
51
52         if (!(new_frame = av_mallocz(sizeof(*new_frame))))
53             return;
54         new_frame->next_frame = NULL;
55         new_frame->cv_buffer  = CVPixelBufferRetain(image_buffer);
56         new_frame->pts        = vda_pts_from_dictionary(user_info);
57
58         pthread_mutex_lock(&vda_ctx->queue_mutex);
59
60         queue_walker = vda_ctx->queue;
61
62         if (!queue_walker || new_frame->pts < queue_walker->pts) {
63             /* we have an empty queue, or this frame earlier than the current queue head */
64             new_frame->next_frame = queue_walker;
65             vda_ctx->queue        = new_frame;
66         } else {
67             /* walk the queue and insert this frame where it belongs in display order */
68             vda_frame *next_frame;
69             while (1) {
70                 next_frame = queue_walker->next_frame;
71                 if (!next_frame || new_frame->pts < next_frame->pts) {
72                     new_frame->next_frame    = next_frame;
73                     queue_walker->next_frame = new_frame;
74                     break;
75                 }
76                 queue_walker = next_frame;
77             }
78         }
79
80         pthread_mutex_unlock(&vda_ctx->queue_mutex);
81     }
82 }
83
84 static int vda_sync_decode(struct vda_context *vda_ctx)
85 {
86     OSStatus status;
87     CFDataRef coded_frame;
88     uint32_t flush_flags = 1 << 0; ///< kVDADecoderFlush_emitFrames
89
90     coded_frame = CFDataCreate(kCFAllocatorDefault,
91                                vda_ctx->priv_bitstream,
92                                vda_ctx->priv_bitstream_size);
93
94     status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, NULL);
95
96     if (kVDADecoderNoErr == status)
97         status = VDADecoderFlush(vda_ctx->decoder, flush_flags);
98
99     CFRelease(coded_frame);
100
101     return status;
102 }
103
104
105 static int vda_h264_start_frame(AVCodecContext *avctx,
106                                 av_unused const uint8_t *buffer,
107                                 av_unused uint32_t size)
108 {
109     struct vda_context *vda_ctx         = avctx->hwaccel_context;
110
111     if (!vda_ctx->decoder)
112         return -1;
113
114     vda_ctx->priv_bitstream_size = 0;
115
116     return 0;
117 }
118
119 static int vda_h264_decode_slice(AVCodecContext *avctx,
120                                  const uint8_t *buffer,
121                                  uint32_t size)
122 {
123     struct vda_context *vda_ctx         = avctx->hwaccel_context;
124     void *tmp;
125
126     if (!vda_ctx->decoder)
127         return -1;
128
129     tmp = av_fast_realloc(vda_ctx->priv_bitstream,
130                           &vda_ctx->priv_allocated_size,
131                           vda_ctx->priv_bitstream_size + size + 4);
132     if (!tmp)
133         return AVERROR(ENOMEM);
134
135     vda_ctx->priv_bitstream = tmp;
136
137     AV_WB32(vda_ctx->priv_bitstream + vda_ctx->priv_bitstream_size, size);
138     memcpy(vda_ctx->priv_bitstream + vda_ctx->priv_bitstream_size + 4, buffer, size);
139
140     vda_ctx->priv_bitstream_size += size + 4;
141
142     return 0;
143 }
144
145 static int vda_h264_end_frame(AVCodecContext *avctx)
146 {
147     H264Context *h                      = avctx->priv_data;
148     struct vda_context *vda_ctx         = avctx->hwaccel_context;
149     AVFrame *frame                      = &h->cur_pic_ptr->f;
150     int status;
151
152     if (!vda_ctx->decoder || !vda_ctx->priv_bitstream)
153         return -1;
154
155     if (vda_ctx->use_sync_decoding) {
156         status = vda_sync_decode(vda_ctx);
157         frame->data[3] = (void*)vda_ctx->cv_buffer;
158     } else {
159         status = vda_decoder_decode(vda_ctx, vda_ctx->priv_bitstream,
160                                     vda_ctx->priv_bitstream_size,
161                                     frame->reordered_opaque);
162     }
163
164     if (status)
165         av_log(avctx, AV_LOG_ERROR, "Failed to decode frame (%d)\n", status);
166
167     return status;
168 }
169
170 int ff_vda_create_decoder(struct vda_context *vda_ctx,
171                           uint8_t *extradata,
172                           int extradata_size)
173 {
174     OSStatus status = kVDADecoderNoErr;
175     CFNumberRef height;
176     CFNumberRef width;
177     CFNumberRef format;
178     CFDataRef avc_data;
179     CFMutableDictionaryRef config_info;
180     CFMutableDictionaryRef buffer_attributes;
181     CFMutableDictionaryRef io_surface_properties;
182     CFNumberRef cv_pix_fmt;
183
184     /* Each VCL NAL in the bistream sent to the decoder
185      * is preceded by a 4 bytes length header.
186      * Change the avcC atom header if needed, to signal headers of 4 bytes. */
187     if (extradata_size >= 4 && (extradata[4] & 0x03) != 0x03) {
188         uint8_t *rw_extradata;
189
190         if (!(rw_extradata = av_malloc(extradata_size)))
191             return AVERROR(ENOMEM);
192
193         memcpy(rw_extradata, extradata, extradata_size);
194
195         rw_extradata[4] |= 0x03;
196
197         avc_data = CFDataCreate(kCFAllocatorDefault, rw_extradata, extradata_size);
198
199         av_freep(&rw_extradata);
200     } else {
201         avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
202     }
203
204     config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
205                                             4,
206                                             &kCFTypeDictionaryKeyCallBacks,
207                                             &kCFTypeDictionaryValueCallBacks);
208
209     height   = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
210     width    = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
211     format   = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
212
213     CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
214     CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
215     CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
216     CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
217
218     buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
219                                                   2,
220                                                   &kCFTypeDictionaryKeyCallBacks,
221                                                   &kCFTypeDictionaryValueCallBacks);
222     io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
223                                                       0,
224                                                       &kCFTypeDictionaryKeyCallBacks,
225                                                       &kCFTypeDictionaryValueCallBacks);
226     cv_pix_fmt      = CFNumberCreate(kCFAllocatorDefault,
227                                      kCFNumberSInt32Type,
228                                      &vda_ctx->cv_pix_fmt_type);
229     CFDictionarySetValue(buffer_attributes,
230                          kCVPixelBufferPixelFormatTypeKey,
231                          cv_pix_fmt);
232     CFDictionarySetValue(buffer_attributes,
233                          kCVPixelBufferIOSurfacePropertiesKey,
234                          io_surface_properties);
235
236     status = VDADecoderCreate(config_info,
237                               buffer_attributes,
238                               vda_decoder_callback,
239                               vda_ctx,
240                               &vda_ctx->decoder);
241
242     CFRelease(height);
243     CFRelease(width);
244     CFRelease(format);
245     CFRelease(avc_data);
246     CFRelease(config_info);
247     CFRelease(io_surface_properties);
248     CFRelease(cv_pix_fmt);
249     CFRelease(buffer_attributes);
250
251     return status;
252 }
253
254 int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
255 {
256     OSStatus status = kVDADecoderNoErr;
257
258     if (vda_ctx->decoder)
259         status = VDADecoderDestroy(vda_ctx->decoder);
260
261     av_freep(&vda_ctx->priv_bitstream);
262
263     return status;
264 }
265
266 AVHWAccel ff_h264_vda_hwaccel = {
267     .name           = "h264_vda",
268     .type           = AVMEDIA_TYPE_VIDEO,
269     .id             = AV_CODEC_ID_H264,
270     .pix_fmt        = AV_PIX_FMT_VDA_VLD,
271     .start_frame    = vda_h264_start_frame,
272     .decode_slice   = vda_h264_decode_slice,
273     .end_frame      = vda_h264_end_frame,
274 };