FFmpeg  2.1.1
vda_h264.c
Go to the documentation of this file.
1 /*
2  * VDA H264 HW acceleration.
3  *
4  * copyright (c) 2011 Sebastien Zwickert
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include <CoreFoundation/CFDictionary.h>
24 #include <CoreFoundation/CFNumber.h>
25 #include <CoreFoundation/CFData.h>
26 
27 #include "vda.h"
28 #include "libavutil/avutil.h"
29 #include "h264.h"
30 
31 struct vda_buffer {
32  CVPixelBufferRef cv_buffer;
33 };
34 
35 /* Decoder callback that adds the vda frame to the queue in display order. */
36 static void vda_decoder_callback (void *vda_hw_ctx,
37  CFDictionaryRef user_info,
38  OSStatus status,
39  uint32_t infoFlags,
40  CVImageBufferRef image_buffer)
41 {
42  struct vda_context *vda_ctx = vda_hw_ctx;
43 
44  if (!image_buffer)
45  return;
46 
47  if (vda_ctx->cv_pix_fmt_type != CVPixelBufferGetPixelFormatType(image_buffer))
48  return;
49 
50  vda_ctx->cv_buffer = CVPixelBufferRetain(image_buffer);
51 }
52 
53 static int vda_sync_decode(struct vda_context *vda_ctx)
54 {
55  OSStatus status;
56  CFDataRef coded_frame;
57  uint32_t flush_flags = 1 << 0; ///< kVDADecoderFlush_emitFrames
58 
59  coded_frame = CFDataCreate(kCFAllocatorDefault,
60  vda_ctx->priv_bitstream,
61  vda_ctx->priv_bitstream_size);
62 
63  status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, NULL);
64 
65  if (kVDADecoderNoErr == status)
66  status = VDADecoderFlush(vda_ctx->decoder, flush_flags);
67 
68  CFRelease(coded_frame);
69 
70  return status;
71 }
72 
73 
75  av_unused const uint8_t *buffer,
76  av_unused uint32_t size)
77 {
78  struct vda_context *vda_ctx = avctx->hwaccel_context;
79 
80  if (!vda_ctx->decoder)
81  return -1;
82 
83  vda_ctx->priv_bitstream_size = 0;
84 
85  return 0;
86 }
87 
89  const uint8_t *buffer,
90  uint32_t size)
91 {
92  struct vda_context *vda_ctx = avctx->hwaccel_context;
93  void *tmp;
94 
95  if (!vda_ctx->decoder)
96  return -1;
97 
98  tmp = av_fast_realloc(vda_ctx->priv_bitstream,
99  &vda_ctx->priv_allocated_size,
100  vda_ctx->priv_bitstream_size + size + 4);
101  if (!tmp)
102  return AVERROR(ENOMEM);
103 
104  vda_ctx->priv_bitstream = tmp;
105 
106  AV_WB32(vda_ctx->priv_bitstream + vda_ctx->priv_bitstream_size, size);
107  memcpy(vda_ctx->priv_bitstream + vda_ctx->priv_bitstream_size + 4, buffer, size);
108 
109  vda_ctx->priv_bitstream_size += size + 4;
110 
111  return 0;
112 }
113 
114 static void vda_h264_release_buffer(void *opaque, uint8_t *data)
115 {
116  struct vda_buffer *context = opaque;
117  CVPixelBufferRelease(context->cv_buffer);
118  av_free(context);
119 }
120 
122 {
123  H264Context *h = avctx->priv_data;
124  struct vda_context *vda_ctx = avctx->hwaccel_context;
125  AVFrame *frame = &h->cur_pic_ptr->f;
126  struct vda_buffer *context;
128  int status;
129 
130  if (!vda_ctx->decoder || !vda_ctx->priv_bitstream)
131  return -1;
132 
133  status = vda_sync_decode(vda_ctx);
134  frame->data[3] = (void*)vda_ctx->cv_buffer;
135 
136  if (status)
137  av_log(avctx, AV_LOG_ERROR, "Failed to decode frame (%d)\n", status);
138 
139  if (!vda_ctx->use_ref_buffer || status)
140  return status;
141 
142  context = av_mallocz(sizeof(*context));
143  buffer = av_buffer_create(NULL, 0, vda_h264_release_buffer, context, 0);
144  if (!context || !buffer) {
145  CVPixelBufferRelease(vda_ctx->cv_buffer);
146  av_free(context);
147  return -1;
148  }
149 
150  context->cv_buffer = vda_ctx->cv_buffer;
151  frame->buf[3] = buffer;
152 
153  return status;
154 }
155 
156 int ff_vda_create_decoder(struct vda_context *vda_ctx,
157  uint8_t *extradata,
158  int extradata_size)
159 {
160  OSStatus status;
161  CFNumberRef height;
162  CFNumberRef width;
163  CFNumberRef format;
164  CFDataRef avc_data;
165  CFMutableDictionaryRef config_info;
166  CFMutableDictionaryRef buffer_attributes;
167  CFMutableDictionaryRef io_surface_properties;
168  CFNumberRef cv_pix_fmt;
169 
170  vda_ctx->priv_bitstream = NULL;
171  vda_ctx->priv_allocated_size = 0;
172 
173  /* Each VCL NAL in the bitstream sent to the decoder
174  * is preceded by a 4 bytes length header.
175  * Change the avcC atom header if needed, to signal headers of 4 bytes. */
176  if (extradata_size >= 4 && (extradata[4] & 0x03) != 0x03) {
177  uint8_t *rw_extradata;
178 
179  if (!(rw_extradata = av_malloc(extradata_size)))
180  return AVERROR(ENOMEM);
181 
182  memcpy(rw_extradata, extradata, extradata_size);
183 
184  rw_extradata[4] |= 0x03;
185 
186  avc_data = CFDataCreate(kCFAllocatorDefault, rw_extradata, extradata_size);
187 
188  av_freep(&rw_extradata);
189  } else {
190  avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
191  }
192 
193  config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
194  4,
195  &kCFTypeDictionaryKeyCallBacks,
196  &kCFTypeDictionaryValueCallBacks);
197 
198  height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
199  width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
200  format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
201 
202  CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
203  CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
204  CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
205  CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
206 
207  buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
208  2,
209  &kCFTypeDictionaryKeyCallBacks,
210  &kCFTypeDictionaryValueCallBacks);
211  io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
212  0,
213  &kCFTypeDictionaryKeyCallBacks,
214  &kCFTypeDictionaryValueCallBacks);
215  cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
216  kCFNumberSInt32Type,
217  &vda_ctx->cv_pix_fmt_type);
218  CFDictionarySetValue(buffer_attributes,
219  kCVPixelBufferPixelFormatTypeKey,
220  cv_pix_fmt);
221  CFDictionarySetValue(buffer_attributes,
222  kCVPixelBufferIOSurfacePropertiesKey,
223  io_surface_properties);
224 
225  status = VDADecoderCreate(config_info,
226  buffer_attributes,
228  vda_ctx,
229  &vda_ctx->decoder);
230 
231  CFRelease(height);
232  CFRelease(width);
233  CFRelease(format);
234  CFRelease(avc_data);
235  CFRelease(config_info);
236  CFRelease(io_surface_properties);
237  CFRelease(cv_pix_fmt);
238  CFRelease(buffer_attributes);
239 
240  return status;
241 }
242 
244 {
245  OSStatus status = kVDADecoderNoErr;
246 
247  if (vda_ctx->decoder)
248  status = VDADecoderDestroy(vda_ctx->decoder);
249 
250  av_freep(&vda_ctx->priv_bitstream);
251 
252  return status;
253 }
254 
256  .name = "h264_vda",
257  .type = AVMEDIA_TYPE_VIDEO,
258  .id = AV_CODEC_ID_H264,
259  .pix_fmt = AV_PIX_FMT_VDA_VLD,
260  .start_frame = vda_h264_start_frame,
261  .decode_slice = vda_h264_decode_slice,
262  .end_frame = vda_h264_end_frame,
263 };
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
static int vda_sync_decode(struct vda_context *vda_ctx)
Definition: vda_h264.c:53
static int vda_h264_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: vda_h264.c:88
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
H264Context.
Definition: h264.h:286
int format
The frame format.
Definition: vda.h:104
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
if((e=av_dict_get(options,"", NULL, AV_DICT_IGNORE_SUFFIX)))
Definition: avfilter.c:965
OSType cv_pix_fmt_type
The pixel format for output image buffers.
Definition: vda.h:112
int use_ref_buffer
Use av_buffer to manage buffer.
Definition: vda.h:147
uint8_t
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:27
const char data[16]
Definition: mxf.c:68
#define AV_WB32(p, darg)
Definition: intreadwrite.h:265
int width
The frame width.
Definition: vda.h:88
CVPixelBufferRef cv_buffer
The Core Video pixel buffer that contains the current image data.
Definition: vda.h:72
static AVFrame * frame
Definition: demuxing.c:51
H.264 / AVC / MPEG4 part10 codec.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:219
static int vda_h264_start_frame(AVCodecContext *avctx, av_unused const uint8_t *buffer, av_unused uint32_t size)
Definition: vda_h264.c:74
VDADecoder decoder
VDA decoder object.
Definition: vda.h:64
Public libavcodec VDA header.
This structure is used to provide the necessary configurations and data to the VDA FFmpeg HWAccel imp...
Definition: vda.h:57
#define av_unused
Disable warnings about deprecated features This is useful for sections of code kept for backward comp...
Definition: avcodec.h:697
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
static char buffer[20]
Definition: seek-test.c:31
static void vda_h264_release_buffer(void *opaque, uint8_t *data)
Definition: vda_h264.c:114
const char * name
Name of the hardware accelerated codec.
Definition: avcodec.h:3027
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given block if it is not large enough, otherwise do nothing.
Definition: utils.c:120
static int width
Definition: utils.c:158
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:2513
AVHWAccel.
Definition: avcodec.h:3021
int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
Destroy the video decoder.
Definition: vda_h264.c:243
main external API structure.
Definition: avcodec.h:1146
static void vda_decoder_callback(void *vda_hw_ctx, CFDictionaryRef user_info, OSStatus status, uint32_t infoFlags, CVImageBufferRef image_buffer)
Definition: vda_h264.c:36
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:366
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
AVHWAccel ff_h264_vda_hwaccel
Definition: vda_h264.c:255
void * priv_data
Definition: avcodec.h:1182
uint8_t * priv_bitstream
The current bitstream buffer.
Definition: vda.h:120
hardware decoding through VDA
Definition: avcodec.h:4632
A reference to a data buffer.
Definition: buffer.h:81
CVPixelBufferRef cv_buffer
Definition: vda_h264.c:32
static int vda_h264_end_frame(AVCodecContext *avctx)
Definition: vda_h264.c:121
Picture * cur_pic_ptr
Definition: h264.h:299
int ff_vda_create_decoder(struct vda_context *vda_ctx, uint8_t *extradata, int extradata_size)
Create the video decoder.
Definition: vda_h264.c:156
#define AVERROR(e)
struct AVFrame f
Definition: mpegvideo.h:98
int priv_allocated_size
The reference size used for fast reallocation.
Definition: vda.h:136
int height
The frame height.
Definition: vda.h:96
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:107
int priv_bitstream_size
The current size of the bitstream.
Definition: vda.h:128
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:241