History

lavc: Add new VDA hwaccel

It leverages the new hwaccel 1.2 features: - get_buffer2 is never called - the internal context is automatically initialized/deinitialized

Anton Khirnov committed 12 years ago

Commit 67afcef1 parent 31a46758 files · +414 −7Browse files

Unified · Split

8 files
  1. configure +2 −0
  2. libavcodec/Makefile +1 −0
  3. libavcodec/allcodecs.c +1 −0
  4. libavcodec/h264_slice.c +2 −0
  5. libavcodec/vda.c +72 −0
  6. libavcodec/vda.h +53 −0
  7. libavcodec/vda_h264.c +250 −7
  8. libavcodec/vda_internal.h +33 −0
configure+2−0
@@ -1896,6 +1896,8 @@ h264_vaapi_hwaccel_deps="vaapi" · 1895 unmodified lines
18961896 h264_vaapi_hwaccel_select="h264_decoder"
18971897 h264_vda_hwaccel_deps="vda"
18981898 h264_vda_hwaccel_select="h264_decoder"
1899+h264_vda_old_hwaccel_deps="vda"
1900+h264_vda_old_hwaccel_select="h264_decoder"
18991901 h264_vdpau_hwaccel_deps="vdpau"
19001902 h264_vdpau_hwaccel_select="h264_decoder"
19011903 mpeg1_vdpau_hwaccel_deps="vdpau"
libavcodec/Makefile+1−0
@@ -68,6 +68,7 @@ OBJS-$(CONFIG_RDFT) += rdft.o $(RDFT-OBJS-yes) · 67 unmodified lines
6868 OBJS-$(CONFIG_SINEWIN) += sinewin.o
6969 OBJS-$(CONFIG_TPELDSP) += tpeldsp.o
7070 OBJS-$(CONFIG_VAAPI) += vaapi.o
71+OBJS-$(CONFIG_VDA) += vda.o
7172 OBJS-$(CONFIG_VDPAU) += vdpau.o
7273 OBJS-$(CONFIG_VIDEODSP) += videodsp.o
7374 OBJS-$(CONFIG_VP3DSP) += vp3dsp.o
libavcodec/allcodecs.c+1−0
@@ -79,6 +79,7 @@ void avcodec_register_all(void) · 78 unmodified lines
7979 REGISTER_HWACCEL(H264_DXVA2, h264_dxva2);
8080 REGISTER_HWACCEL(H264_VAAPI, h264_vaapi);
8181 REGISTER_HWACCEL(H264_VDA, h264_vda);
82+ REGISTER_HWACCEL(H264_VDA_OLD, h264_vda_old);
8283 REGISTER_HWACCEL(H264_VDPAU, h264_vdpau);
8384 REGISTER_HWACCEL(MPEG1_VDPAU, mpeg1_vdpau);
8485 REGISTER_HWACCEL(MPEG2_DXVA2, mpeg2_dxva2);
libavcodec/h264_slice.c+2−0
@@ -153,6 +153,7 @@ static const enum AVPixelFormat h264_hwaccel_pixfmt_list_420[] = { · 152 unmodified lines
153153 #endif
154154 #if CONFIG_H264_VDA_HWACCEL
155155 AV_PIX_FMT_VDA_VLD,
156+ AV_PIX_FMT_VDA,
156157 #endif
157158 #if CONFIG_H264_VDPAU_HWACCEL
158159 AV_PIX_FMT_VDPAU,
@@ -170,6 +171,7 @@ static const enum AVPixelFormat h264_hwaccel_pixfmt_list_jpeg_420[] = { · 11 unmodified lines
170171 #endif
171172 #if CONFIG_H264_VDA_HWACCEL
172173 AV_PIX_FMT_VDA_VLD,
174+ AV_PIX_FMT_VDA,
173175 #endif
174176 #if CONFIG_H264_VDPAU_HWACCEL
175177 AV_PIX_FMT_VDPAU,
libavcodec/vda.c+72−0
@@ -0,0 +1,72 @@
1+/*
2+ * This file is part of Libav.
3+ *
4+ * Libav is free software; you can redistribute it and/or
5+ * modify it under the terms of the GNU Lesser General Public
6+ * License as published by the Free Software Foundation; either
7+ * version 2.1 of the License, or (at your option) any later version.
8+ *
9+ * Libav is distributed in the hope that it will be useful,
10+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+ * Lesser General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU Lesser General Public
15+ * License along with Libav; if not, write to the Free Software
16+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17+ */
18+
19+#include "config.h"
20+
21+#include "libavutil/mem.h"
22+
23+#include "vda.h"
24+#include "vda_internal.h"
25+
26+#if CONFIG_H264_VDA_HWACCEL
27+AVVDAContext *av_vda_alloc_context(void)
28+{
29+ AVVDAContext *ret = av_mallocz(sizeof(*ret));
30+
31+ if (ret)
32+ ret->output_callback = ff_vda_output_callback;
33+
34+ return ret;
35+}
36+
37+int av_vda_default_init(AVCodecContext *avctx)
38+{
39+ avctx->hwaccel_context = av_vda_alloc_context();
40+ if (!avctx->hwaccel_context)
41+ return AVERROR(ENOMEM);
42+ return ff_vda_default_init(avctx);
43+}
44+
45+void av_vda_default_free(AVCodecContext *avctx)
46+{
47+ ff_vda_default_free(avctx);
48+ av_freep(&avctx->hwaccel_context);
49+}
50+
51+void ff_vda_default_free(AVCodecContext *avctx)
52+{
53+ AVVDAContext *vda = avctx->hwaccel_context;
54+ if (vda && vda->decoder)
55+ VDADecoderDestroy(vda->decoder);
56+}
57+
58+#else
59+AVVDAContext *av_vda_alloc_context(void)
60+{
61+ return NULL;
62+}
63+
64+int av_vda_default_init(AVCodecContext *avctx)
65+{
66+ return AVERROR(ENOSYS);
67+}
68+
69+void av_vda_default_free(AVCodecContext *ctx)
70+{
71+}
72+#endif
libavcodec/vda.h+53−0
@@ -29,6 +29,7 @@ · 28 unmodified lines
2929 * Public libavcodec VDA header.
3030 */
3131
32+#include "libavcodec/avcodec.h"
3233 #include "libavcodec/version.h"
3334
3435 #include <stdint.h>
@@ -135,6 +136,58 @@ int ff_vda_create_decoder(struct vda_context *vda_ctx, · 100 unmodified lines
135136 /** Destroy the video decoder. */
136137 int ff_vda_destroy_decoder(struct vda_context *vda_ctx);
137138
139+/**
140+ * This struct holds all the information that needs to be passed
141+ * between the caller and libavcodec for initializing VDA decoding.
142+ * Its size is not a part of the public ABI, it must be allocated with
143+ * av_vda_alloc_context() and freed with av_free().
144+ */
145+typedef struct AVVDAContext {
146+ /**
147+ * VDA decoder object. Created and freed by the caller.
148+ */
149+ VDADecoder decoder;
150+
151+ /**
152+ * The output callback that must be passed to VDADecoderCreate.
153+ * Set by av_vda_alloc_context().
154+ */
155+ VDADecoderOutputCallback output_callback;
156+} AVVDAContext;
157+
158+/**
159+ * Allocate and initialize a VDA context.
160+ *
161+ * This function should be called from the get_format() callback when the caller
162+ * selects the AV_PIX_FMT_VDA format. The caller must then create the decoder
163+ * object (using the output callback provided by libavcodec) that will be used
164+ * for VDA-accelerated decoding.
165+ *
166+ * When decoding with VDA is finished, the caller must destroy the decoder
167+ * object and free the VDA context using av_free().
168+ *
169+ * @return the newly allocated context or NULL on failure
170+ */
171+AVVDAContext *av_vda_alloc_context(void);
172+
173+/**
174+ * This is a convenience function that creates and sets up the VDA context using
175+ * an internal implementation.
176+ *
177+ * @param avctx the corresponding codec context
178+ *
179+ * @return >= 0 on success, a negative AVERROR code on failure
180+ */
181+int av_vda_default_init(AVCodecContext *avctx);
182+
183+/**
184+ * This function must be called to free the VDA context initialized with
185+ * av_vda_default_init().
186+ *
187+ * @param avctx the corresponding codec context
188+ */
189+void av_vda_default_free(AVCodecContext *avctx);
190+
138191 /**
139192 * @}
140193 */
libavcodec/vda_h264.c+250−7
@@ -26,7 +26,9 @@ · 25 unmodified lines
2626
2727 #include "libavutil/avutil.h"
2828 #include "h264.h"
29+#include "internal.h"
2930 #include "vda.h"
31+#include "vda_internal.h"
3032
3133 typedef struct VDAContext {
3234 // The current bitstream buffer.
@@ -37,6 +39,8 @@ typedef struct VDAContext { · 4 unmodified lines
3739
3840 // The reference size used for fast reallocation.
3941 int allocated_size;
42+
43+ CVImageBufferRef frame;
4044 } VDAContext;
4145
4246 /* Decoder callback that adds the VDA frame to the queue in display order. */
@@ -78,7 +82,7 @@ static int vda_sync_decode(VDAContext *ctx, struct vda_context *vda_ctx) · 35 unmodified lines
7882 }
7983
8084
81−static int vda_h264_start_frame(AVCodecContext *avctx,
85+static int vda_old_h264_start_frame(AVCodecContext *avctx,
8286 av_unused const uint8_t *buffer,
8387 av_unused uint32_t size)
8488 {
@@ -93,7 +97,7 @@ static int vda_h264_start_frame(AVCodecContext *avctx, · 8 unmodified lines
9397 return 0;
9498 }
9599
96−static int vda_h264_decode_slice(AVCodecContext *avctx,
100+static int vda_old_h264_decode_slice(AVCodecContext *avctx,
97101 const uint8_t *buffer,
98102 uint32_t size)
99103 {
@@ -120,7 +124,7 @@ static int vda_h264_decode_slice(AVCodecContext *avctx, · 20 unmodified lines
120124 return 0;
121125 }
122126
123−static int vda_h264_end_frame(AVCodecContext *avctx)
127+static int vda_old_h264_end_frame(AVCodecContext *avctx)
124128 {
125129 H264Context *h = avctx->priv_data;
126130 VDAContext *vda = avctx->internal->hwaccel_priv_data;
@@ -208,7 +212,7 @@ int ff_vda_create_decoder(struct vda_context *vda_ctx, · 81 unmodified lines
208212
209213 status = VDADecoderCreate(config_info,
210214 buffer_attributes,
211− vda_decoder_callback,
215+ (VDADecoderOutputCallback *)vda_decoder_callback,
212216 vda_ctx,
213217 &vda_ctx->decoder);
214218
@@ -234,17 +238,256 @@ int ff_vda_destroy_decoder(struct vda_context *vda_ctx) · 19 unmodified lines
234238 return status;
235239 }
236240
237−static void vda_h264_uninit(AVCodecContext *avctx)
241+static int vda_h264_uninit(AVCodecContext *avctx)
238242 {
239− VDAContext *vda = avctx->internal->priv_data;
243+ VDAContext *vda = avctx->internal->hwaccel_priv_data;
240244 av_freep(&vda->bitstream);
245+ if (vda->frame)
246+ CVPixelBufferRelease(vda->frame);
247+ return 0;
241248 }
242249
243−AVHWAccel ff_h264_vda_hwaccel = {
250+AVHWAccel ff_h264_vda_old_hwaccel = {
244251 .name = "h264_vda",
245252 .type = AVMEDIA_TYPE_VIDEO,
246253 .id = AV_CODEC_ID_H264,
247254 .pix_fmt = AV_PIX_FMT_VDA_VLD,
255+ .start_frame = vda_old_h264_start_frame,
256+ .decode_slice = vda_old_h264_decode_slice,
257+ .end_frame = vda_old_h264_end_frame,
258+ .uninit = vda_h264_uninit,
259+ .priv_data_size = sizeof(VDAContext),
260+};
261+
262+void ff_vda_output_callback(void *opaque,
263+ CFDictionaryRef user_info,
264+ OSStatus status,
265+ uint32_t infoFlags,
266+ CVImageBufferRef image_buffer)
267+{
268+ AVCodecContext *ctx = opaque;
269+ VDAContext *vda = ctx->internal->hwaccel_priv_data;
270+
271+
272+ if (vda->frame) {
273+ CVPixelBufferRelease(vda->frame);
274+ vda->frame = NULL;
275+ }
276+
277+ if (!image_buffer)
278+ return;
279+
280+ vda->frame = CVPixelBufferRetain(image_buffer);
281+}
282+
283+static int vda_h264_start_frame(AVCodecContext *avctx,
284+ const uint8_t *buffer,
285+ uint32_t size)
286+{
287+ VDAContext *vda = avctx->internal->hwaccel_priv_data;
288+
289+ vda->bitstream_size = 0;
290+
291+ return 0;
292+}
293+
294+static int vda_h264_decode_slice(AVCodecContext *avctx,
295+ const uint8_t *buffer,
296+ uint32_t size)
297+{
298+ VDAContext *vda = avctx->internal->hwaccel_priv_data;
299+ void *tmp;
300+
301+ tmp = av_fast_realloc(vda->bitstream,
302+ &vda->allocated_size,
303+ vda->bitstream_size + size + 4);
304+ if (!tmp)
305+ return AVERROR(ENOMEM);
306+
307+ vda->bitstream = tmp;
308+
309+ AV_WB32(vda->bitstream + vda->bitstream_size, size);
310+ memcpy(vda->bitstream + vda->bitstream_size + 4, buffer, size);
311+
312+ vda->bitstream_size += size + 4;
313+
314+ return 0;
315+}
316+
317+static void release_buffer(void *opaque, uint8_t *data)
318+{
319+ CVImageBufferRef frame = (CVImageBufferRef)data;
320+ CVPixelBufferRelease(frame);
321+}
322+
323+static int vda_h264_end_frame(AVCodecContext *avctx)
324+{
325+ H264Context *h = avctx->priv_data;
326+ VDAContext *vda = avctx->internal->hwaccel_priv_data;
327+ AVVDAContext *vda_ctx = avctx->hwaccel_context;
328+ AVFrame *frame = &h->cur_pic_ptr->f;
329+ uint32_t flush_flags = 1 << 0; ///< kVDADecoderFlush_emitFrames
330+ CFDataRef coded_frame;
331+ OSStatus status;
332+
333+ if (!vda->bitstream_size)
334+ return AVERROR_INVALIDDATA;
335+
336+
337+ coded_frame = CFDataCreate(kCFAllocatorDefault,
338+ vda->bitstream,
339+ vda->bitstream_size);
340+
341+ status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, NULL);
342+
343+ if (status == kVDADecoderNoErr)
344+ status = VDADecoderFlush(vda_ctx->decoder, flush_flags);
345+
346+ CFRelease(coded_frame);
347+
348+ if (status != kVDADecoderNoErr) {
349+ av_log(avctx, AV_LOG_ERROR, "Failed to decode frame (%d)\n", status);
350+ return AVERROR_UNKNOWN;
351+ }
352+
353+ if (vda->frame) {
354+ av_buffer_unref(&frame->buf[0]);
355+
356+ frame->buf[0] = av_buffer_create((uint8_t*)vda->frame,
357+ sizeof(vda->frame),
358+ release_buffer, NULL,
359+ AV_BUFFER_FLAG_READONLY);
360+ if (!frame->buf)
361+ return AVERROR(ENOMEM);
362+
363+ frame->data[3] = (uint8_t*)vda->frame;
364+ vda->frame = NULL;
365+ }
366+
367+ return 0;
368+}
369+
370+int ff_vda_default_init(AVCodecContext *avctx)
371+{
372+ AVVDAContext *vda_ctx = avctx->hwaccel_context;
373+ OSStatus status = kVDADecoderNoErr;
374+ CFNumberRef height;
375+ CFNumberRef width;
376+ CFNumberRef format;
377+ CFDataRef avc_data;
378+ CFMutableDictionaryRef config_info;
379+ CFMutableDictionaryRef buffer_attributes;
380+ CFMutableDictionaryRef io_surface_properties;
381+ CFNumberRef cv_pix_fmt;
382+ int32_t fmt = 'avc1', pix_fmt = kCVPixelFormatType_422YpCbCr8;
383+
384+ // kCVPixelFormatType_420YpCbCr8Planar;
385+
386+ /* Each VCL NAL in the bistream sent to the decoder
387+ * is preceded by a 4 bytes length header.
388+ * Change the avcC atom header if needed, to signal headers of 4 bytes. */
389+ if (avctx->extradata_size >= 4 && (avctx->extradata[4] & 0x03) != 0x03) {
390+ uint8_t *rw_extradata;
391+
392+ if (!(rw_extradata = av_malloc(avctx->extradata_size)))
393+ return AVERROR(ENOMEM);
394+
395+ memcpy(rw_extradata, avctx->extradata, avctx->extradata_size);
396+
397+ rw_extradata[4] |= 0x03;
398+
399+ avc_data = CFDataCreate(kCFAllocatorDefault, rw_extradata, avctx->extradata_size);
400+
401+ av_freep(&rw_extradata);
402+ } else {
403+ avc_data = CFDataCreate(kCFAllocatorDefault,
404+ avctx->extradata, avctx->extradata_size);
405+ }
406+
407+ config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
408+ 4,
409+ &kCFTypeDictionaryKeyCallBacks,
410+ &kCFTypeDictionaryValueCallBacks);
411+
412+ height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &avctx->height);
413+ width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &avctx->width);
414+ format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &fmt);
415+ CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
416+ CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
417+ CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
418+ CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
419+
420+ buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
421+ 2,
422+ &kCFTypeDictionaryKeyCallBacks,
423+ &kCFTypeDictionaryValueCallBacks);
424+ io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
425+ 0,
426+ &kCFTypeDictionaryKeyCallBacks,
427+ &kCFTypeDictionaryValueCallBacks);
428+ cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
429+ kCFNumberSInt32Type,
430+ &pix_fmt);
431+
432+ CFDictionarySetValue(buffer_attributes,
433+ kCVPixelBufferPixelFormatTypeKey,
434+ cv_pix_fmt);
435+ CFDictionarySetValue(buffer_attributes,
436+ kCVPixelBufferIOSurfacePropertiesKey,
437+ io_surface_properties);
438+
439+ status = VDADecoderCreate(config_info,
440+ buffer_attributes,
441+ (VDADecoderOutputCallback *)ff_vda_output_callback,
442+ avctx,
443+ &vda_ctx->decoder);
444+
445+ CFRelease(format);
446+ CFRelease(height);
447+ CFRelease(width);
448+ CFRelease(avc_data);
449+ CFRelease(config_info);
450+ CFRelease(cv_pix_fmt);
451+ CFRelease(io_surface_properties);
452+ CFRelease(buffer_attributes);
453+
454+ if (status != kVDADecoderNoErr) {
455+ av_log(avctx, AV_LOG_ERROR, "Cannot initialize VDA %d\n", status);
456+ }
457+
458+ switch (status) {
459+ case kVDADecoderHardwareNotSupportedErr:
460+ case kVDADecoderFormatNotSupportedErr:
461+ return AVERROR(ENOSYS);
462+ case kVDADecoderConfigurationError:
463+ return AVERROR(EINVAL);
464+ case kVDADecoderDecoderFailedErr:
465+ return AVERROR_INVALIDDATA;
466+ case kVDADecoderNoErr:
467+ return 0;
468+ default:
469+ return AVERROR_UNKNOWN;
470+ }
471+}
472+
473+static int vda_h264_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
474+{
475+ frame->width = avctx->width;
476+ frame->height = avctx->height;
477+ frame->format = avctx->pix_fmt;
478+ frame->buf[0] = av_buffer_alloc(1);
479+
480+ if (!frame->buf[0])
481+ return AVERROR(ENOMEM);
482+ return 0;
483+}
484+
485+AVHWAccel ff_h264_vda_hwaccel = {
486+ .name = "h264_vda",
487+ .type = AVMEDIA_TYPE_VIDEO,
488+ .id = AV_CODEC_ID_H264,
489+ .pix_fmt = AV_PIX_FMT_VDA,
490+ .alloc_frame = vda_h264_alloc_frame,
248491 .start_frame = vda_h264_start_frame,
249492 .decode_slice = vda_h264_decode_slice,
250493 .end_frame = vda_h264_end_frame,
libavcodec/vda_internal.h+33−0
@@ -0,0 +1,33 @@
1+/*
2+ * This file is part of Libav.
3+ *
4+ * Libav is free software; you can redistribute it and/or
5+ * modify it under the terms of the GNU Lesser General Public
6+ * License as published by the Free Software Foundation; either
7+ * version 2.1 of the License, or (at your option) any later version.
8+ *
9+ * Libav is distributed in the hope that it will be useful,
10+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+ * Lesser General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU Lesser General Public
15+ * License along with Libav; if not, write to the Free Software
16+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17+ */
18+
19+#ifndef AVCODEC_VDA_INTERNAL_H
20+#define AVCODEC_VDA_INTERNAL_H
21+
22+#include "vda.h"
23+
24+void ff_vda_output_callback(void *vda_hw_ctx,
25+ CFDictionaryRef user_info,
26+ OSStatus status,
27+ uint32_t infoFlags,
28+ CVImageBufferRef image_buffer);
29+
30+int ff_vda_default_init(AVCodecContext *avctx);
31+void ff_vda_default_free(AVCodecContext *avctx);
32+
33+#endif /* AVCODEC_VDA_INTERNAL_H */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t