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 ff_vdpau_common_init(AVCodecContext *avctx, VdpDecoderProfile profile,
70 VDPAUHWContext *hwctx = avctx->hwaccel_context;
71 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
72 VdpVideoSurfaceQueryCapabilities *surface_query_caps;
73 VdpDecoderQueryCapabilities *decoder_query_caps;
74 VdpDecoderCreate *create;
78 uint32_t max_level, max_mb, max_width, max_height;
79 /* See vdpau/vdpau.h for alignment constraints. */
80 uint32_t width = (avctx->coded_width + 1) & ~1;
81 uint32_t height = (avctx->coded_height + 3) & ~3;
83 vdctx->width = UINT32_MAX;
84 vdctx->height = UINT32_MAX;
87 if (hwctx->context.decoder != VDP_INVALID_HANDLE) {
88 vdctx->decoder = hwctx->context.decoder;
89 vdctx->render = hwctx->context.render;
90 vdctx->device = VDP_INVALID_HANDLE;
91 return 0; /* Decoder created by user */
94 vdctx->device = hwctx->device;
95 vdctx->get_proc_address = hwctx->get_proc_address;
97 if (hwctx->flags & AV_HWACCEL_FLAG_IGNORE_LEVEL)
100 return AVERROR(ENOTSUP);
102 status = vdctx->get_proc_address(vdctx->device,
103 VDP_FUNC_ID_VIDEO_SURFACE_QUERY_CAPABILITIES,
105 if (status != VDP_STATUS_OK)
106 return vdpau_error(status);
108 surface_query_caps = func;
110 status = surface_query_caps(vdctx->device, VDP_CHROMA_TYPE_420, &supported,
111 &max_width, &max_height);
112 if (status != VDP_STATUS_OK)
113 return vdpau_error(status);
114 if (supported != VDP_TRUE ||
115 max_width < width || max_height < height)
116 return AVERROR(ENOTSUP);
118 status = vdctx->get_proc_address(vdctx->device,
119 VDP_FUNC_ID_DECODER_QUERY_CAPABILITIES,
121 if (status != VDP_STATUS_OK)
122 return vdpau_error(status);
124 decoder_query_caps = func;
126 status = decoder_query_caps(vdctx->device, profile, &supported, &max_level,
127 &max_mb, &max_width, &max_height);
128 if (status != VDP_STATUS_OK)
129 return vdpau_error(status);
131 if (supported != VDP_TRUE || max_level < level ||
132 max_width < width || max_height < height)
133 return AVERROR(ENOTSUP);
135 status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_CREATE,
137 if (status != VDP_STATUS_OK)
138 return vdpau_error(status);
142 status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_RENDER,
144 if (status != VDP_STATUS_OK)
145 return vdpau_error(status);
147 vdctx->render = func;
149 status = create(vdctx->device, profile, width, height, avctx->refs,
151 if (status == VDP_STATUS_OK) {
152 vdctx->width = avctx->coded_width;
153 vdctx->height = avctx->coded_height;
156 return vdpau_error(status);
159 int ff_vdpau_common_uninit(AVCodecContext *avctx)
161 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
162 VdpDecoderDestroy *destroy;
166 if (vdctx->device == VDP_INVALID_HANDLE)
167 return 0; /* Decoder created and destroyed by user */
168 if (vdctx->width == UINT32_MAX && vdctx->height == UINT32_MAX)
171 status = vdctx->get_proc_address(vdctx->device,
172 VDP_FUNC_ID_DECODER_DESTROY, &func);
173 if (status != VDP_STATUS_OK)
174 return vdpau_error(status);
178 status = destroy(vdctx->decoder);
179 return vdpau_error(status);
182 static int ff_vdpau_common_reinit(AVCodecContext *avctx)
184 VDPAUHWContext *hwctx = avctx->hwaccel_context;
185 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
187 if (vdctx->device == VDP_INVALID_HANDLE)
188 return 0; /* Decoder created by user */
189 if (avctx->coded_width == vdctx->width &&
190 avctx->coded_height == vdctx->height && !hwctx->reset)
193 avctx->hwaccel->uninit(avctx);
194 return avctx->hwaccel->init(avctx);
197 int ff_vdpau_common_start_frame(struct vdpau_picture_context *pic_ctx,
198 av_unused const uint8_t *buffer,
199 av_unused uint32_t size)
201 pic_ctx->bitstream_buffers_allocated = 0;
202 pic_ctx->bitstream_buffers_used = 0;
203 pic_ctx->bitstream_buffers = NULL;
207 int ff_vdpau_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
208 struct vdpau_picture_context *pic_ctx)
210 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
211 VdpVideoSurface surf = ff_vdpau_get_surface_id(frame);
215 val = ff_vdpau_common_reinit(avctx);
219 status = vdctx->render(vdctx->decoder, surf, (void *)&pic_ctx->info,
220 pic_ctx->bitstream_buffers_used,
221 pic_ctx->bitstream_buffers);
223 av_freep(&pic_ctx->bitstream_buffers);
224 return vdpau_error(status);
227 #if CONFIG_H263_VDPAU_HWACCEL || CONFIG_MPEG1_VDPAU_HWACCEL || \
228 CONFIG_MPEG2_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL || \
229 CONFIG_VC1_VDPAU_HWACCEL || CONFIG_WMV3_VDPAU_HWACCEL
230 int ff_vdpau_mpeg_end_frame(AVCodecContext *avctx)
232 MpegEncContext *s = avctx->priv_data;
233 Picture *pic = s->current_picture_ptr;
234 struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
237 val = ff_vdpau_common_end_frame(avctx, pic->f, pic_ctx);
241 ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
246 int ff_vdpau_add_buffer(struct vdpau_picture_context *pic_ctx,
247 const uint8_t *buf, uint32_t size)
249 VdpBitstreamBuffer *buffers = pic_ctx->bitstream_buffers;
251 buffers = av_fast_realloc(buffers, &pic_ctx->bitstream_buffers_allocated,
252 (pic_ctx->bitstream_buffers_used + 1) * sizeof(*buffers));
254 return AVERROR(ENOMEM);
256 pic_ctx->bitstream_buffers = buffers;
257 buffers += pic_ctx->bitstream_buffers_used++;
259 buffers->struct_version = VDP_BITSTREAM_BUFFER_VERSION;
260 buffers->bitstream = buf;
261 buffers->bitstream_bytes = size;
265 int av_vdpau_get_profile(AVCodecContext *avctx, VdpDecoderProfile *profile)
267 #define PROFILE(prof) \
273 switch (avctx->codec_id) {
274 case AV_CODEC_ID_MPEG1VIDEO: PROFILE(VDP_DECODER_PROFILE_MPEG1);
275 case AV_CODEC_ID_MPEG2VIDEO:
276 switch (avctx->profile) {
277 case FF_PROFILE_MPEG2_MAIN: PROFILE(VDP_DECODER_PROFILE_MPEG2_MAIN);
278 case FF_PROFILE_MPEG2_SIMPLE: PROFILE(VDP_DECODER_PROFILE_MPEG2_SIMPLE);
279 default: return AVERROR(EINVAL);
281 case AV_CODEC_ID_H263: PROFILE(VDP_DECODER_PROFILE_MPEG4_PART2_ASP);
282 case AV_CODEC_ID_MPEG4:
283 switch (avctx->profile) {
284 case FF_PROFILE_MPEG4_SIMPLE: PROFILE(VDP_DECODER_PROFILE_MPEG4_PART2_SP);
285 case FF_PROFILE_MPEG4_ADVANCED_SIMPLE: PROFILE(VDP_DECODER_PROFILE_MPEG4_PART2_ASP);
286 default: return AVERROR(EINVAL);
288 case AV_CODEC_ID_H264:
289 switch (avctx->profile & ~FF_PROFILE_H264_INTRA) {
290 case FF_PROFILE_H264_BASELINE: PROFILE(VDP_DECODER_PROFILE_H264_BASELINE);
291 case FF_PROFILE_H264_CONSTRAINED_BASELINE:
292 case FF_PROFILE_H264_MAIN: PROFILE(VDP_DECODER_PROFILE_H264_MAIN);
293 case FF_PROFILE_H264_HIGH: PROFILE(VDP_DECODER_PROFILE_H264_HIGH);
294 default: return AVERROR(EINVAL);
296 case AV_CODEC_ID_WMV3:
297 case AV_CODEC_ID_VC1:
298 switch (avctx->profile) {
299 case FF_PROFILE_VC1_SIMPLE: PROFILE(VDP_DECODER_PROFILE_VC1_SIMPLE);
300 case FF_PROFILE_VC1_MAIN: PROFILE(VDP_DECODER_PROFILE_VC1_MAIN);
301 case FF_PROFILE_VC1_ADVANCED: PROFILE(VDP_DECODER_PROFILE_VC1_ADVANCED);
302 default: return AVERROR(EINVAL);
305 return AVERROR(EINVAL);
308 AVVDPAUContext *av_vdpau_alloc_context(void)
310 return av_mallocz(sizeof(AVVDPAUContext));
313 int av_vdpau_bind_context(AVCodecContext *avctx, VdpDevice device,
314 VdpGetProcAddress *get_proc, unsigned flags)
316 VDPAUHWContext *hwctx;
318 if (flags & ~AV_HWACCEL_FLAG_IGNORE_LEVEL)
319 return AVERROR(EINVAL);
321 if (av_reallocp(&avctx->hwaccel_context, sizeof(*hwctx)))
322 return AVERROR(ENOMEM);
324 hwctx = avctx->hwaccel_context;
326 memset(hwctx, 0, sizeof(*hwctx));
327 hwctx->context.decoder = VDP_INVALID_HANDLE;
328 hwctx->device = device;
329 hwctx->get_proc_address = get_proc;
330 hwctx->flags = flags;