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 && profile == VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE) {
177 /* Run-time backward compatibility for libvdpau 0.8 and earlier */
178 profile = VDP_DECODER_PROFILE_H264_MAIN;
179 status = decoder_query_caps(vdctx->device, profile, &supported,
181 &max_width, &max_height);
184 if (status != VDP_STATUS_OK)
185 return vdpau_error(status);
187 if (supported != VDP_TRUE || max_level < level ||
188 max_width < width || max_height < height)
189 return AVERROR(ENOTSUP);
191 status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_CREATE,
193 if (status != VDP_STATUS_OK)
194 return vdpau_error(status);
198 status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_RENDER,
200 if (status != VDP_STATUS_OK)
201 return vdpau_error(status);
203 vdctx->render = func;
205 status = create(vdctx->device, profile, width, height, avctx->refs,
207 if (status == VDP_STATUS_OK) {
208 vdctx->width = avctx->coded_width;
209 vdctx->height = avctx->coded_height;
212 return vdpau_error(status);
215 int ff_vdpau_common_uninit(AVCodecContext *avctx)
217 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
218 VdpDecoderDestroy *destroy;
222 if (vdctx->device == VDP_INVALID_HANDLE)
223 return 0; /* Decoder created and destroyed by user */
224 if (vdctx->width == UINT32_MAX && vdctx->height == UINT32_MAX)
227 status = vdctx->get_proc_address(vdctx->device,
228 VDP_FUNC_ID_DECODER_DESTROY, &func);
229 if (status != VDP_STATUS_OK)
230 return vdpau_error(status);
234 status = destroy(vdctx->decoder);
235 return vdpau_error(status);
238 static int ff_vdpau_common_reinit(AVCodecContext *avctx)
240 VDPAUHWContext *hwctx = avctx->hwaccel_context;
241 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
243 if (vdctx->device == VDP_INVALID_HANDLE)
244 return 0; /* Decoder created by user */
245 if (avctx->coded_width == vdctx->width &&
246 avctx->coded_height == vdctx->height && !hwctx->reset)
249 avctx->hwaccel->uninit(avctx);
250 return avctx->hwaccel->init(avctx);
253 int ff_vdpau_common_start_frame(struct vdpau_picture_context *pic_ctx,
254 av_unused const uint8_t *buffer,
255 av_unused uint32_t size)
257 pic_ctx->bitstream_buffers_allocated = 0;
258 pic_ctx->bitstream_buffers_used = 0;
259 pic_ctx->bitstream_buffers = NULL;
263 int ff_vdpau_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
264 struct vdpau_picture_context *pic_ctx)
266 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
267 VdpVideoSurface surf = ff_vdpau_get_surface_id(frame);
271 val = ff_vdpau_common_reinit(avctx);
275 status = vdctx->render(vdctx->decoder, surf, (void *)&pic_ctx->info,
276 pic_ctx->bitstream_buffers_used,
277 pic_ctx->bitstream_buffers);
279 av_freep(&pic_ctx->bitstream_buffers);
280 return vdpau_error(status);
283 #if CONFIG_H263_VDPAU_HWACCEL || CONFIG_MPEG1_VDPAU_HWACCEL || \
284 CONFIG_MPEG2_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL || \
285 CONFIG_VC1_VDPAU_HWACCEL || CONFIG_WMV3_VDPAU_HWACCEL
286 int ff_vdpau_mpeg_end_frame(AVCodecContext *avctx)
288 MpegEncContext *s = avctx->priv_data;
289 Picture *pic = s->current_picture_ptr;
290 struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
293 val = ff_vdpau_common_end_frame(avctx, pic->f, pic_ctx);
297 ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
302 int ff_vdpau_add_buffer(struct vdpau_picture_context *pic_ctx,
303 const uint8_t *buf, uint32_t size)
305 VdpBitstreamBuffer *buffers = pic_ctx->bitstream_buffers;
307 buffers = av_fast_realloc(buffers, &pic_ctx->bitstream_buffers_allocated,
308 (pic_ctx->bitstream_buffers_used + 1) * sizeof(*buffers));
310 return AVERROR(ENOMEM);
312 pic_ctx->bitstream_buffers = buffers;
313 buffers += pic_ctx->bitstream_buffers_used++;
315 buffers->struct_version = VDP_BITSTREAM_BUFFER_VERSION;
316 buffers->bitstream = buf;
317 buffers->bitstream_bytes = size;
321 int av_vdpau_get_profile(AVCodecContext *avctx, VdpDecoderProfile *profile)
323 #define PROFILE(prof) \
325 *profile = VDP_DECODER_PROFILE_##prof; \
329 switch (avctx->codec_id) {
330 case AV_CODEC_ID_MPEG1VIDEO: PROFILE(MPEG1);
331 case AV_CODEC_ID_MPEG2VIDEO:
332 switch (avctx->profile) {
333 case FF_PROFILE_MPEG2_MAIN: PROFILE(MPEG2_MAIN);
334 case FF_PROFILE_MPEG2_SIMPLE: PROFILE(MPEG2_SIMPLE);
335 default: return AVERROR(EINVAL);
337 case AV_CODEC_ID_H263: PROFILE(MPEG4_PART2_ASP);
338 case AV_CODEC_ID_MPEG4:
339 switch (avctx->profile) {
340 case FF_PROFILE_MPEG4_SIMPLE: PROFILE(MPEG4_PART2_SP);
341 case FF_PROFILE_MPEG4_ADVANCED_SIMPLE: PROFILE(MPEG4_PART2_ASP);
342 default: return AVERROR(EINVAL);
344 case AV_CODEC_ID_H264:
345 switch (avctx->profile & ~FF_PROFILE_H264_INTRA) {
346 case FF_PROFILE_H264_BASELINE: PROFILE(H264_BASELINE);
347 case FF_PROFILE_H264_CONSTRAINED_BASELINE:
348 case FF_PROFILE_H264_MAIN: PROFILE(H264_MAIN);
349 case FF_PROFILE_H264_HIGH: PROFILE(H264_HIGH);
350 #ifdef VDP_DECODER_PROFILE_H264_EXTENDED
351 case FF_PROFILE_H264_EXTENDED: PROFILE(H264_EXTENDED);
353 default: return AVERROR(EINVAL);
355 case AV_CODEC_ID_WMV3:
356 case AV_CODEC_ID_VC1:
357 switch (avctx->profile) {
358 case FF_PROFILE_VC1_SIMPLE: PROFILE(VC1_SIMPLE);
359 case FF_PROFILE_VC1_MAIN: PROFILE(VC1_MAIN);
360 case FF_PROFILE_VC1_ADVANCED: PROFILE(VC1_ADVANCED);
361 default: return AVERROR(EINVAL);
364 return AVERROR(EINVAL);
368 AVVDPAUContext *av_vdpau_alloc_context(void)
370 return av_mallocz(sizeof(AVVDPAUContext));
373 int av_vdpau_bind_context(AVCodecContext *avctx, VdpDevice device,
374 VdpGetProcAddress *get_proc, unsigned flags)
376 VDPAUHWContext *hwctx;
378 if (flags & ~(AV_HWACCEL_FLAG_IGNORE_LEVEL|AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH))
379 return AVERROR(EINVAL);
381 if (av_reallocp(&avctx->hwaccel_context, sizeof(*hwctx)))
382 return AVERROR(ENOMEM);
384 hwctx = avctx->hwaccel_context;
386 memset(hwctx, 0, sizeof(*hwctx));
387 hwctx->context.decoder = VDP_INVALID_HANDLE;
388 hwctx->device = device;
389 hwctx->get_proc_address = get_proc;
390 hwctx->flags = flags;