2 * Video Decode and Presentation API for UNIX (VDPAU) is used for
3 * HW decode acceleration for MPEG-1/2, MPEG-4 ASP, H.264 and VC-1.
5 * Copyright (c) 2008 NVIDIA
7 * This file is part of Libav.
9 * Libav is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * Libav is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with Libav; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "libavutil/avassert.h"
35 #include "vdpau_internal.h"
38 * @addtogroup VDPAU_Decoding
43 static int vdpau_error(VdpStatus status)
48 case VDP_STATUS_NO_IMPLEMENTATION:
49 return AVERROR(ENOSYS);
50 case VDP_STATUS_DISPLAY_PREEMPTED:
52 case VDP_STATUS_INVALID_HANDLE:
53 return AVERROR(EBADF);
54 case VDP_STATUS_INVALID_POINTER:
55 return AVERROR(EFAULT);
56 case VDP_STATUS_RESOURCES:
57 return AVERROR(ENOBUFS);
58 case VDP_STATUS_HANDLE_DEVICE_MISMATCH:
59 return AVERROR(EXDEV);
60 case VDP_STATUS_ERROR:
63 return AVERROR(EINVAL);
67 int av_vdpau_get_surface_parameters(AVCodecContext *avctx,
69 uint32_t *width, uint32_t *height)
72 uint32_t w = avctx->coded_width;
73 uint32_t h = avctx->coded_height;
75 /* See <vdpau/vdpau.h> for per-type alignment constraints. */
76 switch (avctx->sw_pix_fmt) {
77 case AV_PIX_FMT_YUV420P:
78 case AV_PIX_FMT_YUVJ420P:
79 t = VDP_CHROMA_TYPE_420;
83 case AV_PIX_FMT_YUV422P:
84 case AV_PIX_FMT_YUVJ422P:
85 t = VDP_CHROMA_TYPE_422;
89 case AV_PIX_FMT_YUV444P:
90 case AV_PIX_FMT_YUVJ444P:
91 t = VDP_CHROMA_TYPE_444;
95 return AVERROR(ENOSYS);
107 int ff_vdpau_common_init(AVCodecContext *avctx, VdpDecoderProfile profile,
110 VDPAUHWContext *hwctx = avctx->hwaccel_context;
111 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
112 VdpVideoSurfaceQueryCapabilities *surface_query_caps;
113 VdpDecoderQueryCapabilities *decoder_query_caps;
114 VdpDecoderCreate *create;
118 uint32_t max_level, max_mb, max_width, max_height;
123 vdctx->width = UINT32_MAX;
124 vdctx->height = UINT32_MAX;
127 if (hwctx->context.decoder != VDP_INVALID_HANDLE) {
128 vdctx->decoder = hwctx->context.decoder;
129 vdctx->render = hwctx->context.render;
130 vdctx->device = VDP_INVALID_HANDLE;
131 return 0; /* Decoder created by user */
134 vdctx->device = hwctx->device;
135 vdctx->get_proc_address = hwctx->get_proc_address;
137 if (hwctx->flags & AV_HWACCEL_FLAG_IGNORE_LEVEL)
140 return AVERROR(ENOTSUP);
142 if (av_vdpau_get_surface_parameters(avctx, &type, &width, &height))
143 return AVERROR(ENOSYS);
145 if (!(hwctx->flags & AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH) &&
146 type != VDP_CHROMA_TYPE_420)
147 return AVERROR(ENOSYS);
149 status = vdctx->get_proc_address(vdctx->device,
150 VDP_FUNC_ID_VIDEO_SURFACE_QUERY_CAPABILITIES,
152 if (status != VDP_STATUS_OK)
153 return vdpau_error(status);
155 surface_query_caps = func;
157 status = surface_query_caps(vdctx->device, type, &supported,
158 &max_width, &max_height);
159 if (status != VDP_STATUS_OK)
160 return vdpau_error(status);
161 if (supported != VDP_TRUE ||
162 max_width < width || max_height < height)
163 return AVERROR(ENOTSUP);
165 status = vdctx->get_proc_address(vdctx->device,
166 VDP_FUNC_ID_DECODER_QUERY_CAPABILITIES,
168 if (status != VDP_STATUS_OK)
169 return vdpau_error(status);
171 decoder_query_caps = func;
173 status = decoder_query_caps(vdctx->device, profile, &supported, &max_level,
174 &max_mb, &max_width, &max_height);
175 #ifdef VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE
176 if ((status != VDP_STATUS_OK || supported != VDP_TRUE) && profile == VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE) {
177 profile = VDP_DECODER_PROFILE_H264_MAIN;
178 status = decoder_query_caps(vdctx->device, profile, &supported,
180 &max_width, &max_height);
183 if (status != VDP_STATUS_OK)
184 return vdpau_error(status);
186 if (supported != VDP_TRUE || max_level < level ||
187 max_width < width || max_height < height)
188 return AVERROR(ENOTSUP);
190 status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_CREATE,
192 if (status != VDP_STATUS_OK)
193 return vdpau_error(status);
197 status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_RENDER,
199 if (status != VDP_STATUS_OK)
200 return vdpau_error(status);
202 vdctx->render = func;
204 status = create(vdctx->device, profile, width, height, avctx->refs,
206 if (status == VDP_STATUS_OK) {
207 vdctx->width = avctx->coded_width;
208 vdctx->height = avctx->coded_height;
211 return vdpau_error(status);
214 int ff_vdpau_common_uninit(AVCodecContext *avctx)
216 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
217 VdpDecoderDestroy *destroy;
221 if (vdctx->device == VDP_INVALID_HANDLE)
222 return 0; /* Decoder created and destroyed by user */
223 if (vdctx->width == UINT32_MAX && vdctx->height == UINT32_MAX)
226 status = vdctx->get_proc_address(vdctx->device,
227 VDP_FUNC_ID_DECODER_DESTROY, &func);
228 if (status != VDP_STATUS_OK)
229 return vdpau_error(status);
233 status = destroy(vdctx->decoder);
234 return vdpau_error(status);
237 static int ff_vdpau_common_reinit(AVCodecContext *avctx)
239 VDPAUHWContext *hwctx = avctx->hwaccel_context;
240 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
242 if (vdctx->device == VDP_INVALID_HANDLE)
243 return 0; /* Decoder created by user */
244 if (avctx->coded_width == vdctx->width &&
245 avctx->coded_height == vdctx->height && !hwctx->reset)
248 avctx->hwaccel->uninit(avctx);
249 return avctx->hwaccel->init(avctx);
252 int ff_vdpau_common_start_frame(struct vdpau_picture_context *pic_ctx,
253 av_unused const uint8_t *buffer,
254 av_unused uint32_t size)
256 pic_ctx->bitstream_buffers_allocated = 0;
257 pic_ctx->bitstream_buffers_used = 0;
258 pic_ctx->bitstream_buffers = NULL;
262 int ff_vdpau_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
263 struct vdpau_picture_context *pic_ctx)
265 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
266 VdpVideoSurface surf = ff_vdpau_get_surface_id(frame);
270 val = ff_vdpau_common_reinit(avctx);
274 status = vdctx->render(vdctx->decoder, surf, (void *)&pic_ctx->info,
275 pic_ctx->bitstream_buffers_used,
276 pic_ctx->bitstream_buffers);
278 av_freep(&pic_ctx->bitstream_buffers);
279 return vdpau_error(status);
282 #if CONFIG_MPEG1_VDPAU_HWACCEL || \
283 CONFIG_MPEG2_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL || \
284 CONFIG_VC1_VDPAU_HWACCEL || CONFIG_WMV3_VDPAU_HWACCEL
285 int ff_vdpau_mpeg_end_frame(AVCodecContext *avctx)
287 MpegEncContext *s = avctx->priv_data;
288 Picture *pic = s->current_picture_ptr;
289 struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
292 val = ff_vdpau_common_end_frame(avctx, pic->f, pic_ctx);
296 ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
301 int ff_vdpau_add_buffer(struct vdpau_picture_context *pic_ctx,
302 const uint8_t *buf, uint32_t size)
304 VdpBitstreamBuffer *buffers = pic_ctx->bitstream_buffers;
306 buffers = av_fast_realloc(buffers, &pic_ctx->bitstream_buffers_allocated,
307 (pic_ctx->bitstream_buffers_used + 1) * sizeof(*buffers));
309 return AVERROR(ENOMEM);
311 pic_ctx->bitstream_buffers = buffers;
312 buffers += pic_ctx->bitstream_buffers_used++;
314 buffers->struct_version = VDP_BITSTREAM_BUFFER_VERSION;
315 buffers->bitstream = buf;
316 buffers->bitstream_bytes = size;
320 int av_vdpau_get_profile(AVCodecContext *avctx, VdpDecoderProfile *profile)
322 #define PROFILE(prof) \
324 *profile = VDP_DECODER_PROFILE_##prof; \
328 switch (avctx->codec_id) {
329 case AV_CODEC_ID_MPEG1VIDEO: PROFILE(MPEG1);
330 case AV_CODEC_ID_MPEG2VIDEO:
331 switch (avctx->profile) {
332 case FF_PROFILE_MPEG2_MAIN: PROFILE(MPEG2_MAIN);
333 case FF_PROFILE_MPEG2_SIMPLE: PROFILE(MPEG2_SIMPLE);
334 default: return AVERROR(EINVAL);
336 case AV_CODEC_ID_H263: PROFILE(MPEG4_PART2_ASP);
337 case AV_CODEC_ID_MPEG4:
338 switch (avctx->profile) {
339 case FF_PROFILE_MPEG4_SIMPLE: PROFILE(MPEG4_PART2_SP);
340 case FF_PROFILE_MPEG4_ADVANCED_SIMPLE: PROFILE(MPEG4_PART2_ASP);
341 default: return AVERROR(EINVAL);
343 case AV_CODEC_ID_H264:
344 switch (avctx->profile & ~FF_PROFILE_H264_INTRA) {
345 case FF_PROFILE_H264_BASELINE: PROFILE(H264_BASELINE);
346 case FF_PROFILE_H264_CONSTRAINED_BASELINE:
347 case FF_PROFILE_H264_MAIN: PROFILE(H264_MAIN);
348 case FF_PROFILE_H264_HIGH: PROFILE(H264_HIGH);
349 #ifdef VDP_DECODER_PROFILE_H264_EXTENDED
350 case FF_PROFILE_H264_EXTENDED: PROFILE(H264_EXTENDED);
352 default: return AVERROR(EINVAL);
354 case AV_CODEC_ID_WMV3:
355 case AV_CODEC_ID_VC1:
356 switch (avctx->profile) {
357 case FF_PROFILE_VC1_SIMPLE: PROFILE(VC1_SIMPLE);
358 case FF_PROFILE_VC1_MAIN: PROFILE(VC1_MAIN);
359 case FF_PROFILE_VC1_ADVANCED: PROFILE(VC1_ADVANCED);
360 default: return AVERROR(EINVAL);
363 return AVERROR(EINVAL);
367 AVVDPAUContext *av_vdpau_alloc_context(void)
369 return av_mallocz(sizeof(AVVDPAUContext));
372 int av_vdpau_bind_context(AVCodecContext *avctx, VdpDevice device,
373 VdpGetProcAddress *get_proc, unsigned flags)
375 VDPAUHWContext *hwctx;
377 if (flags & ~(AV_HWACCEL_FLAG_IGNORE_LEVEL|AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH))
378 return AVERROR(EINVAL);
380 if (av_reallocp(&avctx->hwaccel_context, sizeof(*hwctx)))
381 return AVERROR(ENOMEM);
383 hwctx = avctx->hwaccel_context;
385 memset(hwctx, 0, sizeof(*hwctx));
386 hwctx->context.decoder = VDP_INVALID_HANDLE;
387 hwctx->device = device;
388 hwctx->get_proc_address = get_proc;
389 hwctx->flags = flags;