FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vda_h264_dec.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Xidorn Quan
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 /**
22  * @file
23  * H.264 decoder via VDA
24  * @author Xidorn Quan <quanxunzhen@gmail.com>
25  */
26 
27 #include <string.h>
28 #include <CoreFoundation/CoreFoundation.h>
29 
30 #include "vda.h"
31 #include "h264.h"
32 #include "avcodec.h"
33 
34 #ifndef kCFCoreFoundationVersionNumber10_7
35 #define kCFCoreFoundationVersionNumber10_7 635.00
36 #endif
37 
39 
40 static const enum AVPixelFormat vda_pixfmts_prior_10_7[] = {
44 };
45 
46 static const enum AVPixelFormat vda_pixfmts[] = {
52 };
53 
54 typedef struct {
57  struct vda_context vda_ctx;
60 
61 static enum AVPixelFormat get_format(struct AVCodecContext *avctx,
62  const enum AVPixelFormat *fmt)
63 {
64  return AV_PIX_FMT_VDA_VLD;
65 }
66 
67 typedef struct {
68  CVPixelBufferRef cv_buffer;
70 
71 static void release_buffer(void *opaque, uint8_t *data)
72 {
73  VDABufferContext *context = opaque;
74  CVPixelBufferUnlockBaseAddress(context->cv_buffer, 0);
75  CVPixelBufferRelease(context->cv_buffer);
76  av_free(context);
77 }
78 
79 static int get_buffer2(AVCodecContext *avctx, AVFrame *pic, int flag)
80 {
81  VDABufferContext *context = av_mallocz(sizeof(VDABufferContext));
82  AVBufferRef *buffer = av_buffer_create(NULL, 0, release_buffer, context, 0);
83  if (!context || !buffer) {
84  av_free(context);
85  return AVERROR(ENOMEM);
86  }
87 
88  pic->buf[0] = buffer;
89  pic->data[0] = (void *)1;
90  return 0;
91 }
92 
93 static int vdadec_decode(AVCodecContext *avctx,
94  void *data, int *got_frame, AVPacket *avpkt)
95 {
96  VDADecoderContext *ctx = avctx->priv_data;
97  AVFrame *pic = data;
98  int ret;
99 
100  ret = ff_h264_decoder.decode(avctx, data, got_frame, avpkt);
101  if (*got_frame) {
102  AVBufferRef *buffer = pic->buf[0];
103  VDABufferContext *context = av_buffer_get_opaque(buffer);
104  CVPixelBufferRef cv_buffer = (CVPixelBufferRef)pic->data[3];
105 
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);
116  }
117  } else {
118  pic->data[0] = CVPixelBufferGetBaseAddress(cv_buffer);
119  pic->linesize[0] = CVPixelBufferGetBytesPerRow(cv_buffer);
120  }
121  }
122  avctx->pix_fmt = ctx->pix_fmt;
123 
124  return ret;
125 }
126 
128 {
129  VDADecoderContext *ctx = avctx->priv_data;
130  /* release buffers and decoder */
132  /* close H.264 decoder */
133  if (ctx->h264_initialized)
134  ff_h264_decoder.close(avctx);
135  return 0;
136 }
137 
139 {
140  VDADecoderContext *ctx = avctx->priv_data;
141  struct vda_context *vda_ctx = &ctx->vda_ctx;
142  OSStatus status;
143  int ret;
144 
145  ctx->h264_initialized = 0;
146 
147  /* init pix_fmts of codec */
148  if (!ff_h264_vda_decoder.pix_fmts) {
149  if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber10_7)
150  ff_h264_vda_decoder.pix_fmts = vda_pixfmts_prior_10_7;
151  else
152  ff_h264_vda_decoder.pix_fmts = vda_pixfmts;
153  }
154 
155  /* init vda */
156  memset(vda_ctx, 0, sizeof(struct vda_context));
157  vda_ctx->width = avctx->width;
158  vda_ctx->height = avctx->height;
159  vda_ctx->format = 'avc1';
160  vda_ctx->use_sync_decoding = 1;
161  vda_ctx->use_ref_buffer = 1;
162  ctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
163  switch (ctx->pix_fmt) {
164  case AV_PIX_FMT_UYVY422:
165  vda_ctx->cv_pix_fmt_type = '2vuy';
166  break;
167  case AV_PIX_FMT_YUYV422:
168  vda_ctx->cv_pix_fmt_type = 'yuvs';
169  break;
170  case AV_PIX_FMT_NV12:
171  vda_ctx->cv_pix_fmt_type = '420v';
172  break;
173  case AV_PIX_FMT_YUV420P:
174  vda_ctx->cv_pix_fmt_type = 'y420';
175  break;
176  default:
177  av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format: %d\n", avctx->pix_fmt);
178  goto failed;
179  }
180  status = ff_vda_create_decoder(vda_ctx,
181  avctx->extradata, avctx->extradata_size);
182  if (status != kVDADecoderNoErr) {
183  av_log(avctx, AV_LOG_ERROR,
184  "Failed to init VDA decoder: %d.\n", status);
185  goto failed;
186  }
187  avctx->hwaccel_context = vda_ctx;
188 
189  /* changes callback functions */
190  avctx->get_format = get_format;
191  avctx->get_buffer2 = get_buffer2;
192 #if FF_API_GET_BUFFER
193  // force the old get_buffer to be empty
194  avctx->get_buffer = NULL;
195 #endif
196 
197  /* init H.264 decoder */
198  ret = ff_h264_decoder.init(avctx);
199  if (ret < 0) {
200  av_log(avctx, AV_LOG_ERROR, "Failed to open H.264 decoder.\n");
201  goto failed;
202  }
203  ctx->h264_initialized = 1;
204 
205  return 0;
206 
207 failed:
208  vdadec_close(avctx);
209  return -1;
210 }
211 
212 static void vdadec_flush(AVCodecContext *avctx)
213 {
214  return ff_h264_decoder.flush(avctx);
215 }
216 
217 AVCodec ff_h264_vda_decoder = {
218  .name = "h264_vda",
219  .type = AVMEDIA_TYPE_VIDEO,
220  .id = AV_CODEC_ID_H264,
221  .priv_data_size = sizeof(VDADecoderContext),
222  .init = vdadec_init,
223  .close = vdadec_close,
225  .capabilities = CODEC_CAP_DELAY,
226  .flush = vdadec_flush,
227  .long_name = NULL_IF_CONFIG_SMALL("H.264 (VDA acceleration)"),
228 };