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 "h264dec.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;
59 
60  /* for backing-up fields set by user.
61  * we have to gain full control of such fields here */
63  enum AVPixelFormat (*get_format)(struct AVCodecContext *s, const enum AVPixelFormat * fmt);
66 
67 static enum AVPixelFormat get_format(struct AVCodecContext *avctx,
68  const enum AVPixelFormat *fmt)
69 {
70  return AV_PIX_FMT_VDA_VLD;
71 }
72 
73 typedef struct {
74  CVPixelBufferRef cv_buffer;
76 
77 static void release_buffer(void *opaque, uint8_t *data)
78 {
79  VDABufferContext *context = opaque;
80  CVPixelBufferUnlockBaseAddress(context->cv_buffer, 0);
81  CVPixelBufferRelease(context->cv_buffer);
82  av_free(context);
83 }
84 
85 static int get_buffer2(AVCodecContext *avctx, AVFrame *pic, int flag)
86 {
87  VDABufferContext *context = av_mallocz(sizeof(VDABufferContext));
89  if (!context || !buffer) {
90  av_free(context);
91  return AVERROR(ENOMEM);
92  }
93 
94  pic->buf[0] = buffer;
95  pic->data[0] = (void *)1;
96  return 0;
97 }
98 
99 static inline void set_context(AVCodecContext *avctx)
100 {
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;
108 }
109 
110 static inline void restore_context(AVCodecContext *avctx)
111 {
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;
116 }
117 
118 static int vdadec_decode(AVCodecContext *avctx,
119  void *data, int *got_frame, AVPacket *avpkt)
120 {
121  VDADecoderContext *ctx = avctx->priv_data;
122  AVFrame *pic = data;
123  int ret;
124 
125  set_context(avctx);
126  ret = ff_h264_decoder.decode(avctx, data, got_frame, avpkt);
127  restore_context(avctx);
128  if (*got_frame) {
129  AVBufferRef *buffer = pic->buf[0];
130  VDABufferContext *context = av_buffer_get_opaque(buffer);
131  CVPixelBufferRef cv_buffer = (CVPixelBufferRef)pic->data[3];
132 
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);
143  }
144  } else {
145  pic->data[0] = CVPixelBufferGetBaseAddress(cv_buffer);
146  pic->linesize[0] = CVPixelBufferGetBytesPerRow(cv_buffer);
147  }
148  }
149  avctx->pix_fmt = ctx->pix_fmt;
150 
151  return ret;
152 }
153 
155 {
156  VDADecoderContext *ctx = avctx->priv_data;
157  /* release buffers and decoder */
159  /* close H.264 decoder */
160  if (ctx->h264_initialized) {
161  set_context(avctx);
162  ff_h264_decoder.close(avctx);
163  restore_context(avctx);
164  }
165  return 0;
166 }
167 
169 {
170  VDADecoderContext *ctx = avctx->priv_data;
171  struct vda_context *vda_ctx = &ctx->vda_ctx;
172  OSStatus status;
173  int ret, i;
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  /* init vda */
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';
196  break;
197  case AV_PIX_FMT_YUYV422:
198  vda_ctx->cv_pix_fmt_type = 'yuvs';
199  break;
200  case AV_PIX_FMT_NV12:
201  vda_ctx->cv_pix_fmt_type = '420v';
202  break;
203  case AV_PIX_FMT_YUV420P:
204  vda_ctx->cv_pix_fmt_type = 'y420';
205  break;
206  default:
207  av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format: %d\n", avctx->pix_fmt);
208  goto failed;
209  }
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);
215  goto failed;
216  }
217 
218  /* init H.264 decoder */
219  set_context(avctx);
220  ret = ff_h264_decoder.init(avctx);
221  restore_context(avctx);
222  if (ret < 0) {
223  av_log(avctx, AV_LOG_ERROR, "Failed to open H.264 decoder.\n");
224  goto failed;
225  }
226  ctx->h264_initialized = 1;
227 
228  for (i = 0; i < MAX_SPS_COUNT; i++) {
229  const SPS *sps = ctx->h264ctx.ps.sps_list[i] ? (const SPS*)ctx->h264ctx.ps.sps_list[i]->data : NULL;
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");
234  goto failed;
235  }
236  }
237 
238  return 0;
239 
240 failed:
241  vdadec_close(avctx);
242  return -1;
243 }
244 
245 static void vdadec_flush(AVCodecContext *avctx)
246 {
247  set_context(avctx);
248  ff_h264_decoder.flush(avctx);
249  restore_context(avctx);
250 }
251 
252 AVCodec ff_h264_vda_decoder = {
253  .name = "h264_vda",
254  .type = AVMEDIA_TYPE_VIDEO,
255  .id = AV_CODEC_ID_H264,
256  .priv_data_size = sizeof(VDADecoderContext),
257  .init = vdadec_init,
258  .close = vdadec_close,
260  .capabilities = AV_CODEC_CAP_DELAY,
261  .flush = vdadec_flush,
262  .long_name = NULL_IF_CONFIG_SMALL("H.264 (VDA acceleration)"),
263 };
int chroma_format_idc
Definition: h264_ps.h:47
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:82
#define NULL
Definition: coverity.c:32
const struct AVCodec * codec
Definition: avcodec.h:1770
const char * s
Definition: avisynth_c.h:768
static enum AVPixelFormat pix_fmt
This structure describes decoded (raw) audio or video data.
Definition: frame.h:201
#define MAX_SPS_COUNT
Definition: h264_ps.h:37
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static void flush(AVCodecContext *avctx)
const char * fmt
Definition: avisynth_c.h:769
void(* flush)(AVCodecContext *)
Flush buffers.
Definition: avcodec.h:3845
AVBufferRef * sps_list[MAX_SPS_COUNT]
Definition: h264_ps.h:138
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:393
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
Sequence parameter set.
Definition: h264_ps.h:43
int flag
Definition: cpu.c:34
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1989
int(* get_buffer2)(struct AVCodecContext *s, AVFrame *frame, int flags)
Definition: vda_h264_dec.c:64
AVCodec ff_h264_vda_decoder
Definition: vda_h264_dec.c:252
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:222
int(* decode)(AVCodecContext *, void *outdata, int *outdata_size, AVPacket *avpkt)
Definition: avcodec.h:3822
H264Context.
Definition: h264dec.h:337
int format
The frame format.
Definition: vda.h:112
AVCodec.
Definition: avcodec.h:3739
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
OSType cv_pix_fmt_type
The pixel format for output image buffers.
Definition: vda.h:120
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: avcodec.h:1027
int use_ref_buffer
Use av_buffer to manage buffer.
Definition: vda.h:146
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
H264Context h264ctx
Definition: vda_h264_dec.c:55
uint8_t
#define av_cold
Definition: attributes.h:82
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:3094
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1876
static enum AVPixelFormat get_format(struct AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Definition: vda_h264_dec.c:67
static AVFrame * frame
static int flags
Definition: log.c:57
int width
The frame width.
Definition: vda.h:96
#define av_log(a,...)
static av_cold int vdadec_init(AVCodecContext *avctx)
Definition: vda_h264_dec.c:168
static enum AVPixelFormat vda_pixfmts[]
Definition: vda_h264_dec.c:46
int(* close)(AVCodecContext *)
Definition: avcodec.h:3823
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:90
const char * name
Name of the codec implementation.
Definition: avcodec.h:3746
AVBufferRef * av_buffer_create(uint8_t *data, int size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:28
GLsizei count
Definition: opengl_enc.c:109
This structure is used to provide the necessary configurations and data to the VDA FFmpeg HWAccel imp...
Definition: vda.h:65
enum AVPixelFormat(* get_format)(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
Definition: vda_h264_dec.c:63
static av_cold int vdadec_close(AVCodecContext *avctx)
Definition: vda_h264_dec.c:154
enum AVPixelFormat * pix_fmts
array of supported pixel formats, or NULL if unknown, array is terminated by -1
Definition: avcodec.h:3760
int width
picture width / height.
Definition: avcodec.h:1948
AVFormatContext * ctx
Definition: movenc.c:48
void * hwaccel_context
Definition: vda_h264_dec.c:62
H.264 / AVC / MPEG-4 part10 codec.
enum AVPixelFormat pix_fmt
Definition: vda_h264_dec.c:58
struct vda_context vda_ctx
Definition: vda_h264_dec.c:57
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:274
CVPixelBufferRef cv_buffer
Definition: vda_h264_dec.c:74
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:232
Public libavcodec VDA header.
int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
Destroy the video decoder.
Definition: vda_h264.c:251
main external API structure.
Definition: avcodec.h:1761
AVCodec ff_h264_decoder
Definition: h264dec.c:1059
uint8_t * data
The data buffer.
Definition: buffer.h:89
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition: pixfmt.h:63
static enum AVPixelFormat vda_pixfmts_prior_10_7[]
Definition: vda_h264_dec.c:40
int extradata_size
Definition: avcodec.h:1877
static void set_context(AVCodecContext *avctx)
Definition: vda_h264_dec.c:99
void * av_buffer_get_opaque(const AVBufferRef *buf)
Definition: buffer.c:141
enum AVPixelFormat(* get_format)(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
callback to negotiate the pixelFormat
Definition: avcodec.h:2039
int(* get_buffer2)(struct AVCodecContext *s, AVFrame *frame, int flags)
This callback is called at the beginning of each frame to get data buffer(s) for it.
Definition: avcodec.h:2678
int use_sync_decoding
Use the hardware decoder in synchronous mode.
Definition: vda.h:88
static void vdadec_flush(AVCodecContext *avctx)
Definition: vda_h264_dec.c:245
#define kCFCoreFoundationVersionNumber10_7
Definition: vda_h264_dec.c:35
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:215
A reference to a data buffer.
Definition: buffer.h:81
hardware decoding through VDA
Definition: pixfmt.h:179
int
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
static void restore_context(AVCodecContext *avctx)
Definition: vda_h264_dec.c:110
if(ret< 0)
Definition: vf_mcdeint.c:279
static void release_buffer(void *opaque, uint8_t *data)
Definition: vda_h264_dec.c:77
H264ParamSets ps
Definition: h264dec.h:455
int bit_depth_luma
bit_depth_luma_minus8 + 8
Definition: h264_ps.h:97
void * priv_data
Definition: avcodec.h:1803
#define av_free(p)
int ff_vda_create_decoder(struct vda_context *vda_ctx, uint8_t *extradata, int extradata_size)
Create the video decoder.
Definition: vda_h264.c:164
static int vdadec_decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: vda_h264_dec.c:118
static int get_buffer2(AVCodecContext *avctx, AVFrame *pic, int flag)
Definition: vda_h264_dec.c:85
int(* init)(AVCodecContext *)
Definition: avcodec.h:3807
int height
The frame height.
Definition: vda.h:104
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
This structure stores compressed data.
Definition: avcodec.h:1656
GLuint buffer
Definition: opengl_enc.c:102