FFmpeg
 All Data Structures 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 static int get_buffer(AVCodecContext *avctx, AVFrame *pic)
68 {
70  pic->data[0] = (void *)1;
71  return 0;
72 }
73 
74 static void release_buffer(AVCodecContext *avctx, AVFrame *pic)
75 {
76  int i;
77 
78  CVPixelBufferRef cv_buffer = (CVPixelBufferRef)pic->data[3];
79  CVPixelBufferUnlockBaseAddress(cv_buffer, 0);
80  CVPixelBufferRelease(cv_buffer);
81 
82  for (i = 0; i < 4; i++)
83  pic->data[i] = NULL;
84 }
85 
86 static int vdadec_decode(AVCodecContext *avctx,
87  void *data, int *got_frame, AVPacket *avpkt)
88 {
89  VDADecoderContext *ctx = avctx->priv_data;
90  AVFrame *pic = data;
91  int ret;
92 
93  ret = ff_h264_decoder.decode(avctx, data, got_frame, avpkt);
94  if (*got_frame) {
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);
104  }
105  } else {
106  pic->data[0] = CVPixelBufferGetBaseAddress(cv_buffer);
107  pic->linesize[0] = CVPixelBufferGetBytesPerRow(cv_buffer);
108  }
109  }
110  avctx->pix_fmt = ctx->pix_fmt;
111 
112  return ret;
113 }
114 
116 {
117  VDADecoderContext *ctx = avctx->priv_data;
118  /* release buffers and decoder */
120  /* close H.264 decoder */
121  if (ctx->h264_initialized)
122  ff_h264_decoder.close(avctx);
123  return 0;
124 }
125 
127 {
128  AVCodecParserContext *parser;
129  uint8_t *pout;
130  int psize;
131  int index;
132  H264Context *h;
133  int ret = -1;
134 
135  /* init parser & parse file */
136  parser = av_parser_init(avctx->codec->id);
137  if (!parser) {
138  av_log(avctx, AV_LOG_ERROR, "Failed to open H.264 parser.\n");
139  goto final;
140  }
142  index = av_parser_parse2(parser, avctx, &pout, &psize, NULL, 0, 0, 0, 0);
143  if (index < 0) {
144  av_log(avctx, AV_LOG_ERROR, "Failed to parse this file.\n");
145  goto release_parser;
146  }
147 
148  /* check if support */
149  h = parser->priv_data;
150  switch (h->sps.bit_depth_luma) {
151  case 8:
152  if (!CHROMA444 && !CHROMA422) {
153  // only this will H.264 decoder switch to hwaccel
154  ret = 0;
155  break;
156  }
157  default:
158  av_log(avctx, AV_LOG_ERROR, "Unsupported file.\n");
159  }
160 
161 release_parser:
162  av_parser_close(parser);
163 
164 final:
165  return ret;
166 }
167 
169 {
170  VDADecoderContext *ctx = avctx->priv_data;
171  struct vda_context *vda_ctx = &ctx->vda_ctx;
172  OSStatus status;
173  int ret;
174 
175  ctx->h264_initialized = 0;
176 
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;
181  else
182  ff_h264_vda_decoder.pix_fmts = vda_pixfmts;
183  }
184 
185  /* check if VDA supports this file */
186  if (check_format(avctx) < 0)
187  goto failed;
188 
189  /* init vda */
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';
199  break;
200  case AV_PIX_FMT_YUYV422:
201  vda_ctx->cv_pix_fmt_type = 'yuvs';
202  break;
203  case AV_PIX_FMT_NV12:
204  vda_ctx->cv_pix_fmt_type = '420v';
205  break;
206  case AV_PIX_FMT_YUV420P:
207  vda_ctx->cv_pix_fmt_type = 'y420';
208  break;
209  default:
210  av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format: %d\n", avctx->pix_fmt);
211  goto failed;
212  }
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);
218  goto failed;
219  }
220  avctx->hwaccel_context = vda_ctx;
221 
222  /* changes callback functions */
223  avctx->get_format = get_format;
224  avctx->get_buffer = get_buffer;
226 
227  /* init H.264 decoder */
228  ret = ff_h264_decoder.init(avctx);
229  if (ret < 0) {
230  av_log(avctx, AV_LOG_ERROR, "Failed to open H.264 decoder.\n");
231  goto failed;
232  }
233  ctx->h264_initialized = 1;
234 
235  return 0;
236 
237 failed:
238  vdadec_close(avctx);
239  return -1;
240 }
241 
242 static void vdadec_flush(AVCodecContext *avctx)
243 {
244  return ff_h264_decoder.flush(avctx);
245 }
246 
247 AVCodec ff_h264_vda_decoder = {
248  .name = "h264_vda",
249  .type = AVMEDIA_TYPE_VIDEO,
250  .id = AV_CODEC_ID_H264,
251  .priv_data_size = sizeof(VDADecoderContext),
252  .init = vdadec_init,
253  .close = vdadec_close,
255  .capabilities = CODEC_CAP_DELAY,
256  .flush = vdadec_flush,
257  .long_name = NULL_IF_CONFIG_SMALL("H.264 (VDA acceleration)"),
258 };