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;
60 /* for backing-up fields set by user.
61 * we have to gain full control of such fields here */
62 void *hwaccel_context;
63 enum AVPixelFormat (*get_format)(struct AVCodecContext *s, const enum AVPixelFormat * fmt);
64 int (*get_buffer2)(struct AVCodecContext *s, AVFrame *frame, int flags);
67 static enum AVPixelFormat get_format(struct AVCodecContext *avctx,
68 const enum AVPixelFormat *fmt)
70 return AV_PIX_FMT_VDA_VLD;
74 CVPixelBufferRef cv_buffer;
77 static void release_buffer(void *opaque, uint8_t *data)
79 VDABufferContext *context = opaque;
80 CVPixelBufferUnlockBaseAddress(context->cv_buffer, 0);
81 CVPixelBufferRelease(context->cv_buffer);
85 static int get_buffer2(AVCodecContext *avctx, AVFrame *pic, int flag)
87 VDABufferContext *context = av_mallocz(sizeof(VDABufferContext));
88 AVBufferRef *buffer = av_buffer_create(NULL, 0, release_buffer, context, 0);
89 if (!context || !buffer) {
91 return AVERROR(ENOMEM);
95 pic->data[0] = (void *)1;
99 static inline void set_context(AVCodecContext *avctx)
101 VDADecoderContext *ctx = avctx->priv_data;
102 ctx->hwaccel_context = avctx->hwaccel_context;
103 avctx->hwaccel_context = &ctx->vda_ctx;
104 ctx->get_format = avctx->get_format;
105 avctx->get_format = get_format;
106 ctx->get_buffer2 = avctx->get_buffer2;
107 avctx->get_buffer2 = get_buffer2;
110 static inline void restore_context(AVCodecContext *avctx)
112 VDADecoderContext *ctx = avctx->priv_data;
113 avctx->hwaccel_context = ctx->hwaccel_context;
114 avctx->get_format = ctx->get_format;
115 avctx->get_buffer2 = ctx->get_buffer2;
118 static int vdadec_decode(AVCodecContext *avctx,
119 void *data, int *got_frame, AVPacket *avpkt)
121 VDADecoderContext *ctx = avctx->priv_data;
126 ret = ff_h264_decoder.decode(avctx, data, got_frame, avpkt);
127 restore_context(avctx);
129 AVBufferRef *buffer = pic->buf[0];
130 VDABufferContext *context = av_buffer_get_opaque(buffer);
131 CVPixelBufferRef cv_buffer = (CVPixelBufferRef)pic->data[3];
133 CVPixelBufferRetain(cv_buffer);
134 CVPixelBufferLockBaseAddress(cv_buffer, 0);
135 context->cv_buffer = cv_buffer;
136 pic->format = ctx->pix_fmt;
137 if (CVPixelBufferIsPlanar(cv_buffer)) {
138 int i, count = CVPixelBufferGetPlaneCount(cv_buffer);
139 av_assert0(count < 4);
140 for (i = 0; i < count; i++) {
141 pic->data[i] = CVPixelBufferGetBaseAddressOfPlane(cv_buffer, i);
142 pic->linesize[i] = CVPixelBufferGetBytesPerRowOfPlane(cv_buffer, i);
145 pic->data[0] = CVPixelBufferGetBaseAddress(cv_buffer);
146 pic->linesize[0] = CVPixelBufferGetBytesPerRow(cv_buffer);
149 avctx->pix_fmt = ctx->pix_fmt;
154 static av_cold int vdadec_close(AVCodecContext *avctx)
156 VDADecoderContext *ctx = avctx->priv_data;
157 /* release buffers and decoder */
158 ff_vda_destroy_decoder(&ctx->vda_ctx);
159 /* close H.264 decoder */
160 if (ctx->h264_initialized) {
162 ff_h264_decoder.close(avctx);
163 restore_context(avctx);
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;
186 memset(vda_ctx, 0, sizeof(struct vda_context));
187 vda_ctx->width = avctx->width;
188 vda_ctx->height = avctx->height;
189 vda_ctx->format = 'avc1';
190 vda_ctx->use_sync_decoding = 1;
191 vda_ctx->use_ref_buffer = 1;
192 ctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
193 switch (ctx->pix_fmt) {
194 case AV_PIX_FMT_UYVY422:
195 vda_ctx->cv_pix_fmt_type = '2vuy';
197 case AV_PIX_FMT_YUYV422:
198 vda_ctx->cv_pix_fmt_type = 'yuvs';
200 case AV_PIX_FMT_NV12:
201 vda_ctx->cv_pix_fmt_type = '420v';
203 case AV_PIX_FMT_YUV420P:
204 vda_ctx->cv_pix_fmt_type = 'y420';
207 av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format: %d\n", avctx->pix_fmt);
210 status = ff_vda_create_decoder(vda_ctx,
211 avctx->extradata, avctx->extradata_size);
212 if (status != kVDADecoderNoErr) {
213 av_log(avctx, AV_LOG_ERROR,
214 "Failed to init VDA decoder: %d.\n", status);
218 /* init H.264 decoder */
220 ret = ff_h264_decoder.init(avctx);
221 restore_context(avctx);
223 av_log(avctx, AV_LOG_ERROR, "Failed to open H.264 decoder.\n");
226 ctx->h264_initialized = 1;
228 for (i = 0; i < MAX_SPS_COUNT; i++) {
229 SPS *sps = ctx->h264ctx.sps_buffers[i];
230 if (sps && (sps->bit_depth_luma != 8 ||
231 sps->chroma_format_idc == 2 ||
232 sps->chroma_format_idc == 3)) {
233 av_log(avctx, AV_LOG_ERROR, "Format is not supported.\n");
245 static void vdadec_flush(AVCodecContext *avctx)
248 ff_h264_decoder.flush(avctx);
249 restore_context(avctx);
252 AVCodec ff_h264_vda_decoder = {
254 .type = AVMEDIA_TYPE_VIDEO,
255 .id = AV_CODEC_ID_H264,
256 .priv_data_size = sizeof(VDADecoderContext),
258 .close = vdadec_close,
259 .decode = vdadec_decode,
260 .capabilities = AV_CODEC_CAP_DELAY,
261 .flush = vdadec_flush,
262 .long_name = NULL_IF_CONFIG_SMALL("H.264 (VDA acceleration)"),