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;
67 static int get_buffer(AVCodecContext *avctx, AVFrame *pic)
69 pic->type = FF_BUFFER_TYPE_USER;
70 pic->data[0] = (void *)1;
74 static void release_buffer(AVCodecContext *avctx, AVFrame *pic)
78 CVPixelBufferRef cv_buffer = (CVPixelBufferRef)pic->data[3];
79 CVPixelBufferUnlockBaseAddress(cv_buffer, 0);
80 CVPixelBufferRelease(cv_buffer);
82 for (i = 0; i < 4; i++)
86 static int vdadec_decode(AVCodecContext *avctx,
87 void *data, int *got_frame, AVPacket *avpkt)
89 VDADecoderContext *ctx = avctx->priv_data;
93 ret = ff_h264_decoder.decode(avctx, data, got_frame, avpkt);
95 CVPixelBufferRef cv_buffer = (CVPixelBufferRef)pic->data[3];
96 CVPixelBufferLockBaseAddress(cv_buffer, 0);
97 pic->format = ctx->pix_fmt;
98 if (CVPixelBufferIsPlanar(cv_buffer)) {
99 int i, count = CVPixelBufferGetPlaneCount(cv_buffer);
100 av_assert0(count < 4);
101 for (i = 0; i < count; i++) {
102 pic->data[i] = CVPixelBufferGetBaseAddressOfPlane(cv_buffer, i);
103 pic->linesize[i] = CVPixelBufferGetBytesPerRowOfPlane(cv_buffer, i);
106 pic->data[0] = CVPixelBufferGetBaseAddress(cv_buffer);
107 pic->linesize[0] = CVPixelBufferGetBytesPerRow(cv_buffer);
110 avctx->pix_fmt = ctx->pix_fmt;
115 static av_cold int vdadec_close(AVCodecContext *avctx)
117 VDADecoderContext *ctx = avctx->priv_data;
118 /* release buffers and decoder */
119 ff_vda_destroy_decoder(&ctx->vda_ctx);
120 /* close H.264 decoder */
121 if (ctx->h264_initialized)
122 ff_h264_decoder.close(avctx);
126 static av_cold int check_format(AVCodecContext *avctx)
128 AVCodecParserContext *parser;
135 /* init parser & parse file */
136 parser = av_parser_init(avctx->codec->id);
138 av_log(avctx, AV_LOG_ERROR, "Failed to open H.264 parser.\n");
141 parser->flags = PARSER_FLAG_COMPLETE_FRAMES;
142 index = av_parser_parse2(parser, avctx, &pout, &psize, NULL, 0, 0, 0, 0);
144 av_log(avctx, AV_LOG_ERROR, "Failed to parse this file.\n");
148 /* check if support */
149 h = parser->priv_data;
150 switch (h->sps.bit_depth_luma) {
152 if (!CHROMA444 && !CHROMA422) {
153 // only this will H.264 decoder switch to hwaccel
158 av_log(avctx, AV_LOG_ERROR, "Unsupported file.\n");
162 av_parser_close(parser);
168 static av_cold int vdadec_init(AVCodecContext *avctx)
170 VDADecoderContext *ctx = avctx->priv_data;
171 struct vda_context *vda_ctx = &ctx->vda_ctx;
175 ctx->h264_initialized = 0;
177 /* init pix_fmts of codec */
178 if (!ff_h264_vda_decoder.pix_fmts) {
179 if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber10_7)
180 ff_h264_vda_decoder.pix_fmts = vda_pixfmts_prior_10_7;
182 ff_h264_vda_decoder.pix_fmts = vda_pixfmts;
185 /* check if VDA supports this file */
186 if (check_format(avctx) < 0)
190 memset(vda_ctx, 0, sizeof(struct vda_context));
191 vda_ctx->width = avctx->width;
192 vda_ctx->height = avctx->height;
193 vda_ctx->format = 'avc1';
194 vda_ctx->use_sync_decoding = 1;
195 ctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
196 switch (ctx->pix_fmt) {
197 case AV_PIX_FMT_UYVY422:
198 vda_ctx->cv_pix_fmt_type = '2vuy';
200 case AV_PIX_FMT_YUYV422:
201 vda_ctx->cv_pix_fmt_type = 'yuvs';
203 case AV_PIX_FMT_NV12:
204 vda_ctx->cv_pix_fmt_type = '420v';
206 case AV_PIX_FMT_YUV420P:
207 vda_ctx->cv_pix_fmt_type = 'y420';
210 av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format: %d\n", avctx->pix_fmt);
213 status = ff_vda_create_decoder(vda_ctx,
214 avctx->extradata, avctx->extradata_size);
215 if (status != kVDADecoderNoErr) {
216 av_log(avctx, AV_LOG_ERROR,
217 "Failed to init VDA decoder: %d.\n", status);
220 avctx->hwaccel_context = vda_ctx;
222 /* changes callback functions */
223 avctx->get_format = get_format;
224 avctx->get_buffer = get_buffer;
225 avctx->release_buffer = release_buffer;
227 /* init H.264 decoder */
228 ret = ff_h264_decoder.init(avctx);
230 av_log(avctx, AV_LOG_ERROR, "Failed to open H.264 decoder.\n");
233 ctx->h264_initialized = 1;
242 static void vdadec_flush(AVCodecContext *avctx)
244 return ff_h264_decoder.flush(avctx);
247 AVCodec ff_h264_vda_decoder = {
249 .type = AVMEDIA_TYPE_VIDEO,
250 .id = AV_CODEC_ID_H264,
251 .priv_data_size = sizeof(VDADecoderContext),
253 .close = vdadec_close,
254 .decode = vdadec_decode,
255 .capabilities = CODEC_CAP_DELAY,
256 .flush = vdadec_flush,
257 .long_name = NULL_IF_CONFIG_SMALL("H.264 (VDA acceleration)"),