2 * Copyright (c) 2012, Xidorn Quan
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * H.264 decoder via VDA
24 * @author Xidorn Quan <quanxunzhen@gmail.com>
28 #include <CoreFoundation/CoreFoundation.h>
34 #ifndef kCFCoreFoundationVersionNumber10_7
35 #define kCFCoreFoundationVersionNumber10_7 635.00
38 extern AVCodec ff_h264_decoder, ff_h264_vda_decoder;
40 static const enum AVPixelFormat vda_pixfmts_prior_10_7[] = {
46 static const enum AVPixelFormat vda_pixfmts[] = {
57 struct vda_context vda_ctx;
58 enum AVPixelFormat pix_fmt;
61 static enum AVPixelFormat get_format(struct AVCodecContext *avctx,
62 const enum AVPixelFormat *fmt)
64 return AV_PIX_FMT_VDA_VLD;
68 CVPixelBufferRef cv_buffer;
71 static void release_buffer(void *opaque, uint8_t *data)
73 VDABufferContext *context = opaque;
74 CVPixelBufferUnlockBaseAddress(context->cv_buffer, 0);
75 CVPixelBufferRelease(context->cv_buffer);
79 static int get_buffer2(AVCodecContext *avctx, AVFrame *pic, int flag)
81 VDABufferContext *context = av_mallocz(sizeof(VDABufferContext));
82 AVBufferRef *buffer = av_buffer_create(NULL, 0, release_buffer, context, 0);
83 if (!context || !buffer) {
85 return AVERROR(ENOMEM);
89 pic->data[0] = (void *)1;
93 static int vdadec_decode(AVCodecContext *avctx,
94 void *data, int *got_frame, AVPacket *avpkt)
96 VDADecoderContext *ctx = avctx->priv_data;
100 ret = ff_h264_decoder.decode(avctx, data, got_frame, avpkt);
102 AVBufferRef *buffer = pic->buf[0];
103 VDABufferContext *context = av_buffer_get_opaque(buffer);
104 CVPixelBufferRef cv_buffer = (CVPixelBufferRef)pic->data[3];
106 CVPixelBufferRetain(cv_buffer);
107 CVPixelBufferLockBaseAddress(cv_buffer, 0);
108 context->cv_buffer = cv_buffer;
109 pic->format = ctx->pix_fmt;
110 if (CVPixelBufferIsPlanar(cv_buffer)) {
111 int i, count = CVPixelBufferGetPlaneCount(cv_buffer);
112 av_assert0(count < 4);
113 for (i = 0; i < count; i++) {
114 pic->data[i] = CVPixelBufferGetBaseAddressOfPlane(cv_buffer, i);
115 pic->linesize[i] = CVPixelBufferGetBytesPerRowOfPlane(cv_buffer, i);
118 pic->data[0] = CVPixelBufferGetBaseAddress(cv_buffer);
119 pic->linesize[0] = CVPixelBufferGetBytesPerRow(cv_buffer);
122 avctx->pix_fmt = ctx->pix_fmt;
127 static av_cold int vdadec_close(AVCodecContext *avctx)
129 VDADecoderContext *ctx = avctx->priv_data;
130 /* release buffers and decoder */
131 ff_vda_destroy_decoder(&ctx->vda_ctx);
132 /* close H.264 decoder */
133 if (ctx->h264_initialized)
134 ff_h264_decoder.close(avctx);
138 static av_cold int check_format(AVCodecContext *avctx)
140 AVCodecParserContext *parser;
147 /* init parser & parse file */
148 parser = av_parser_init(avctx->codec->id);
150 av_log(avctx, AV_LOG_ERROR, "Failed to open H.264 parser.\n");
153 parser->flags = PARSER_FLAG_COMPLETE_FRAMES;
154 index = av_parser_parse2(parser, avctx, &pout, &psize, NULL, 0, 0, 0, 0);
156 av_log(avctx, AV_LOG_ERROR, "Failed to parse this file.\n");
160 /* check if support */
161 h = parser->priv_data;
162 switch (h->sps.bit_depth_luma) {
164 if (!CHROMA444(h) && !CHROMA422(h)) {
165 // only this will H.264 decoder switch to hwaccel
170 av_log(avctx, AV_LOG_ERROR, "Unsupported file.\n");
174 av_parser_close(parser);
180 static av_cold int vdadec_init(AVCodecContext *avctx)
182 VDADecoderContext *ctx = avctx->priv_data;
183 struct vda_context *vda_ctx = &ctx->vda_ctx;
187 ctx->h264_initialized = 0;
189 /* init pix_fmts of codec */
190 if (!ff_h264_vda_decoder.pix_fmts) {
191 if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber10_7)
192 ff_h264_vda_decoder.pix_fmts = vda_pixfmts_prior_10_7;
194 ff_h264_vda_decoder.pix_fmts = vda_pixfmts;
197 /* check if VDA supports this file */
198 if (check_format(avctx) < 0)
202 memset(vda_ctx, 0, sizeof(struct vda_context));
203 vda_ctx->width = avctx->width;
204 vda_ctx->height = avctx->height;
205 vda_ctx->format = 'avc1';
206 vda_ctx->use_sync_decoding = 1;
207 vda_ctx->use_ref_buffer = 1;
208 ctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
209 switch (ctx->pix_fmt) {
210 case AV_PIX_FMT_UYVY422:
211 vda_ctx->cv_pix_fmt_type = '2vuy';
213 case AV_PIX_FMT_YUYV422:
214 vda_ctx->cv_pix_fmt_type = 'yuvs';
216 case AV_PIX_FMT_NV12:
217 vda_ctx->cv_pix_fmt_type = '420v';
219 case AV_PIX_FMT_YUV420P:
220 vda_ctx->cv_pix_fmt_type = 'y420';
223 av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format: %d\n", avctx->pix_fmt);
226 status = ff_vda_create_decoder(vda_ctx,
227 avctx->extradata, avctx->extradata_size);
228 if (status != kVDADecoderNoErr) {
229 av_log(avctx, AV_LOG_ERROR,
230 "Failed to init VDA decoder: %d.\n", status);
233 avctx->hwaccel_context = vda_ctx;
235 /* changes callback functions */
236 avctx->get_format = get_format;
237 avctx->get_buffer2 = get_buffer2;
238 #if FF_API_GET_BUFFER
239 // force the old get_buffer to be empty
240 avctx->get_buffer = NULL;
243 /* init H.264 decoder */
244 ret = ff_h264_decoder.init(avctx);
246 av_log(avctx, AV_LOG_ERROR, "Failed to open H.264 decoder.\n");
249 ctx->h264_initialized = 1;
258 static void vdadec_flush(AVCodecContext *avctx)
260 return ff_h264_decoder.flush(avctx);
263 AVCodec ff_h264_vda_decoder = {
265 .type = AVMEDIA_TYPE_VIDEO,
266 .id = AV_CODEC_ID_H264,
267 .priv_data_size = sizeof(VDADecoderContext),
269 .close = vdadec_close,
270 .decode = vdadec_decode,
271 .capabilities = CODEC_CAP_DELAY,
272 .flush = vdadec_flush,
273 .long_name = NULL_IF_CONFIG_SMALL("H.264 (VDA acceleration)"),