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
31 #include "vdpau_internal.h"
34 * @addtogroup VDPAU_Decoding
39 static int vdpau_error(VdpStatus status)
44 case VDP_STATUS_NO_IMPLEMENTATION:
45 return AVERROR(ENOSYS);
46 case VDP_STATUS_DISPLAY_PREEMPTED:
48 case VDP_STATUS_INVALID_HANDLE:
49 return AVERROR(EBADF);
50 case VDP_STATUS_INVALID_POINTER:
51 return AVERROR(EFAULT);
52 case VDP_STATUS_RESOURCES:
53 return AVERROR(ENOBUFS);
54 case VDP_STATUS_HANDLE_DEVICE_MISMATCH:
55 return AVERROR(EXDEV);
56 case VDP_STATUS_ERROR:
59 return AVERROR(EINVAL);
63 int av_vdpau_get_surface_parameters(AVCodecContext *avctx,
65 uint32_t *width, uint32_t *height)
68 uint32_t w = avctx->coded_width;
69 uint32_t h = avctx->coded_height;
71 /* See <vdpau/vdpau.h> for per-type alignment constraints. */
72 switch (avctx->sw_pix_fmt) {
73 case AV_PIX_FMT_YUV420P:
74 case AV_PIX_FMT_YUVJ420P:
75 t = VDP_CHROMA_TYPE_420;
79 case AV_PIX_FMT_YUV422P:
80 case AV_PIX_FMT_YUVJ422P:
81 t = VDP_CHROMA_TYPE_422;
85 case AV_PIX_FMT_YUV444P:
86 case AV_PIX_FMT_YUVJ444P:
87 t = VDP_CHROMA_TYPE_444;
91 return AVERROR(ENOSYS);
103 int ff_vdpau_common_init(AVCodecContext *avctx, VdpDecoderProfile profile,
106 VDPAUHWContext *hwctx = avctx->hwaccel_context;
107 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
108 VdpVideoSurfaceQueryCapabilities *surface_query_caps;
109 VdpDecoderQueryCapabilities *decoder_query_caps;
110 VdpDecoderCreate *create;
114 uint32_t max_level, max_mb, max_width, max_height;
119 vdctx->width = UINT32_MAX;
120 vdctx->height = UINT32_MAX;
123 if (hwctx->context.decoder != VDP_INVALID_HANDLE) {
124 vdctx->decoder = hwctx->context.decoder;
125 vdctx->render = hwctx->context.render;
126 vdctx->device = VDP_INVALID_HANDLE;
127 return 0; /* Decoder created by user */
130 vdctx->device = hwctx->device;
131 vdctx->get_proc_address = hwctx->get_proc_address;
133 if (hwctx->flags & AV_HWACCEL_FLAG_IGNORE_LEVEL)
136 return AVERROR(ENOTSUP);
138 if (av_vdpau_get_surface_parameters(avctx, &type, &width, &height))
139 return AVERROR(ENOSYS);
141 if (!(hwctx->flags & AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH) &&
142 type != VDP_CHROMA_TYPE_420)
143 return AVERROR(ENOSYS);
145 status = vdctx->get_proc_address(vdctx->device,
146 VDP_FUNC_ID_VIDEO_SURFACE_QUERY_CAPABILITIES,
148 if (status != VDP_STATUS_OK)
149 return vdpau_error(status);
151 surface_query_caps = func;
153 status = surface_query_caps(vdctx->device, type, &supported,
154 &max_width, &max_height);
155 if (status != VDP_STATUS_OK)
156 return vdpau_error(status);
157 if (supported != VDP_TRUE ||
158 max_width < width || max_height < height)
159 return AVERROR(ENOTSUP);
161 status = vdctx->get_proc_address(vdctx->device,
162 VDP_FUNC_ID_DECODER_QUERY_CAPABILITIES,
164 if (status != VDP_STATUS_OK)
165 return vdpau_error(status);
167 decoder_query_caps = func;
169 status = decoder_query_caps(vdctx->device, profile, &supported, &max_level,
170 &max_mb, &max_width, &max_height);
171 #ifdef VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE
172 if ((status != VDP_STATUS_OK || supported != VDP_TRUE) && profile == VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE) {
173 profile = VDP_DECODER_PROFILE_H264_MAIN;
174 status = decoder_query_caps(vdctx->device, profile, &supported,
176 &max_width, &max_height);
179 if (status != VDP_STATUS_OK)
180 return vdpau_error(status);
182 if (supported != VDP_TRUE || max_level < level ||
183 max_width < width || max_height < height)
184 return AVERROR(ENOTSUP);
186 status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_CREATE,
188 if (status != VDP_STATUS_OK)
189 return vdpau_error(status);
193 status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_RENDER,
195 if (status != VDP_STATUS_OK)
196 return vdpau_error(status);
198 vdctx->render = func;
200 status = create(vdctx->device, profile, width, height, avctx->refs,
202 if (status == VDP_STATUS_OK) {
203 vdctx->width = avctx->coded_width;
204 vdctx->height = avctx->coded_height;
207 return vdpau_error(status);
210 int ff_vdpau_common_uninit(AVCodecContext *avctx)
212 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
213 VdpDecoderDestroy *destroy;
217 if (vdctx->device == VDP_INVALID_HANDLE)
218 return 0; /* Decoder created and destroyed by user */
219 if (vdctx->width == UINT32_MAX && vdctx->height == UINT32_MAX)
222 status = vdctx->get_proc_address(vdctx->device,
223 VDP_FUNC_ID_DECODER_DESTROY, &func);
224 if (status != VDP_STATUS_OK)
225 return vdpau_error(status);
229 status = destroy(vdctx->decoder);
230 return vdpau_error(status);
233 static int ff_vdpau_common_reinit(AVCodecContext *avctx)
235 VDPAUHWContext *hwctx = avctx->hwaccel_context;
236 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
238 if (vdctx->device == VDP_INVALID_HANDLE)
239 return 0; /* Decoder created by user */
240 if (avctx->coded_width == vdctx->width &&
241 avctx->coded_height == vdctx->height && !hwctx->reset)
244 avctx->hwaccel->uninit(avctx);
245 return avctx->hwaccel->init(avctx);
248 int ff_vdpau_common_start_frame(struct vdpau_picture_context *pic_ctx,
249 av_unused const uint8_t *buffer,
250 av_unused uint32_t size)
252 pic_ctx->bitstream_buffers_allocated = 0;
253 pic_ctx->bitstream_buffers_used = 0;
254 pic_ctx->bitstream_buffers = NULL;
258 int ff_vdpau_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
259 struct vdpau_picture_context *pic_ctx)
261 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
262 VdpVideoSurface surf = ff_vdpau_get_surface_id(frame);
266 val = ff_vdpau_common_reinit(avctx);
270 status = vdctx->render(vdctx->decoder, surf, (void *)&pic_ctx->info,
271 pic_ctx->bitstream_buffers_used,
272 pic_ctx->bitstream_buffers);
274 av_freep(&pic_ctx->bitstream_buffers);
275 return vdpau_error(status);
278 #if CONFIG_MPEG1_VDPAU_HWACCEL || \
279 CONFIG_MPEG2_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL || \
280 CONFIG_VC1_VDPAU_HWACCEL || CONFIG_WMV3_VDPAU_HWACCEL
281 int ff_vdpau_mpeg_end_frame(AVCodecContext *avctx)
283 MpegEncContext *s = avctx->priv_data;
284 Picture *pic = s->current_picture_ptr;
285 struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
288 val = ff_vdpau_common_end_frame(avctx, pic->f, pic_ctx);
292 ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
297 int ff_vdpau_add_buffer(struct vdpau_picture_context *pic_ctx,
298 const uint8_t *buf, uint32_t size)
300 VdpBitstreamBuffer *buffers = pic_ctx->bitstream_buffers;
302 buffers = av_fast_realloc(buffers, &pic_ctx->bitstream_buffers_allocated,
303 (pic_ctx->bitstream_buffers_used + 1) * sizeof(*buffers));
305 return AVERROR(ENOMEM);
307 pic_ctx->bitstream_buffers = buffers;
308 buffers += pic_ctx->bitstream_buffers_used++;
310 buffers->struct_version = VDP_BITSTREAM_BUFFER_VERSION;
311 buffers->bitstream = buf;
312 buffers->bitstream_bytes = size;
316 #if FF_API_VDPAU_PROFILE
317 int av_vdpau_get_profile(AVCodecContext *avctx, VdpDecoderProfile *profile)
319 #define PROFILE(prof) \
321 *profile = VDP_DECODER_PROFILE_##prof; \
325 switch (avctx->codec_id) {
326 case AV_CODEC_ID_MPEG1VIDEO: PROFILE(MPEG1);
327 case AV_CODEC_ID_MPEG2VIDEO:
328 switch (avctx->profile) {
329 case FF_PROFILE_MPEG2_MAIN: PROFILE(MPEG2_MAIN);
330 case FF_PROFILE_MPEG2_SIMPLE: PROFILE(MPEG2_SIMPLE);
331 default: return AVERROR(EINVAL);
333 case AV_CODEC_ID_H263: PROFILE(MPEG4_PART2_ASP);
334 case AV_CODEC_ID_MPEG4:
335 switch (avctx->profile) {
336 case FF_PROFILE_MPEG4_SIMPLE: PROFILE(MPEG4_PART2_SP);
337 case FF_PROFILE_MPEG4_ADVANCED_SIMPLE: PROFILE(MPEG4_PART2_ASP);
338 default: return AVERROR(EINVAL);
340 case AV_CODEC_ID_H264:
341 switch (avctx->profile & ~FF_PROFILE_H264_INTRA) {
342 case FF_PROFILE_H264_BASELINE: PROFILE(H264_BASELINE);
343 case FF_PROFILE_H264_CONSTRAINED_BASELINE:
344 case FF_PROFILE_H264_MAIN: PROFILE(H264_MAIN);
345 case FF_PROFILE_H264_HIGH: PROFILE(H264_HIGH);
346 #ifdef VDP_DECODER_PROFILE_H264_EXTENDED
347 case FF_PROFILE_H264_EXTENDED: PROFILE(H264_EXTENDED);
349 default: return AVERROR(EINVAL);
351 case AV_CODEC_ID_WMV3:
352 case AV_CODEC_ID_VC1:
353 switch (avctx->profile) {
354 case FF_PROFILE_VC1_SIMPLE: PROFILE(VC1_SIMPLE);
355 case FF_PROFILE_VC1_MAIN: PROFILE(VC1_MAIN);
356 case FF_PROFILE_VC1_ADVANCED: PROFILE(VC1_ADVANCED);
357 default: return AVERROR(EINVAL);
360 return AVERROR(EINVAL);
363 #endif /* FF_API_VDPAU_PROFILE */
365 AVVDPAUContext *av_vdpau_alloc_context(void)
367 return av_mallocz(sizeof(AVVDPAUContext));
370 int av_vdpau_bind_context(AVCodecContext *avctx, VdpDevice device,
371 VdpGetProcAddress *get_proc, unsigned flags)
373 VDPAUHWContext *hwctx;
375 if (flags & ~(AV_HWACCEL_FLAG_IGNORE_LEVEL|AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH))
376 return AVERROR(EINVAL);
378 if (av_reallocp(&avctx->hwaccel_context, sizeof(*hwctx)))
379 return AVERROR(ENOMEM);
381 hwctx = avctx->hwaccel_context;
383 memset(hwctx, 0, sizeof(*hwctx));
384 hwctx->context.decoder = VDP_INVALID_HANDLE;
385 hwctx->device = device;
386 hwctx->get_proc_address = get_proc;
387 hwctx->flags = flags;