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;
122 if (av_vdpau_get_surface_parameters(avctx, &type, &width, &height))
123 return AVERROR(ENOSYS);
128 if (hwctx->context.decoder != VDP_INVALID_HANDLE) {
129 vdctx->decoder = hwctx->context.decoder;
130 vdctx->render = hwctx->context.render;
131 vdctx->device = VDP_INVALID_HANDLE;
132 return 0; /* Decoder created by user */
135 vdctx->device = hwctx->device;
136 vdctx->get_proc_address = hwctx->get_proc_address;
138 if (hwctx->flags & AV_HWACCEL_FLAG_IGNORE_LEVEL)
141 if (!(hwctx->flags & AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH) &&
142 type != VDP_CHROMA_TYPE_420)
143 return AVERROR(ENOSYS);
145 AVHWFramesContext *frames_ctx = NULL;
146 AVVDPAUDeviceContext *dev_ctx;
148 // We assume the hw_frames_ctx always survives until ff_vdpau_common_uninit
149 // is called. This holds true as the user is not allowed to touch
150 // hw_device_ctx, or hw_frames_ctx after get_format (and ff_get_format
151 // itself also uninits before unreffing hw_frames_ctx).
152 if (avctx->hw_frames_ctx) {
153 frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
154 } else if (avctx->hw_device_ctx) {
157 avctx->hw_frames_ctx = av_hwframe_ctx_alloc(avctx->hw_device_ctx);
158 if (!avctx->hw_frames_ctx)
159 return AVERROR(ENOMEM);
161 frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
162 frames_ctx->format = AV_PIX_FMT_VDPAU;
163 frames_ctx->sw_format = avctx->sw_pix_fmt;
164 frames_ctx->width = avctx->coded_width;
165 frames_ctx->height = avctx->coded_height;
167 ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
169 av_buffer_unref(&avctx->hw_frames_ctx);
175 av_log(avctx, AV_LOG_ERROR, "A hardware frames context is "
176 "required for VDPAU decoding.\n");
177 return AVERROR(EINVAL);
180 dev_ctx = frames_ctx->device_ctx->hwctx;
182 vdctx->device = dev_ctx->device;
183 vdctx->get_proc_address = dev_ctx->get_proc_address;
185 if (avctx->hwaccel_flags & AV_HWACCEL_FLAG_IGNORE_LEVEL)
190 return AVERROR(ENOTSUP);
192 status = vdctx->get_proc_address(vdctx->device,
193 VDP_FUNC_ID_VIDEO_SURFACE_QUERY_CAPABILITIES,
195 if (status != VDP_STATUS_OK)
196 return vdpau_error(status);
198 surface_query_caps = func;
200 status = surface_query_caps(vdctx->device, type, &supported,
201 &max_width, &max_height);
202 if (status != VDP_STATUS_OK)
203 return vdpau_error(status);
204 if (supported != VDP_TRUE ||
205 max_width < width || max_height < height)
206 return AVERROR(ENOTSUP);
208 status = vdctx->get_proc_address(vdctx->device,
209 VDP_FUNC_ID_DECODER_QUERY_CAPABILITIES,
211 if (status != VDP_STATUS_OK)
212 return vdpau_error(status);
214 decoder_query_caps = func;
216 status = decoder_query_caps(vdctx->device, profile, &supported, &max_level,
217 &max_mb, &max_width, &max_height);
218 #ifdef VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE
219 if ((status != VDP_STATUS_OK || supported != VDP_TRUE) && profile == VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE) {
220 profile = VDP_DECODER_PROFILE_H264_MAIN;
221 status = decoder_query_caps(vdctx->device, profile, &supported,
223 &max_width, &max_height);
226 if (status != VDP_STATUS_OK)
227 return vdpau_error(status);
229 if (supported != VDP_TRUE || max_level < level ||
230 max_width < width || max_height < height)
231 return AVERROR(ENOTSUP);
233 status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_CREATE,
235 if (status != VDP_STATUS_OK)
236 return vdpau_error(status);
240 status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_RENDER,
242 if (status != VDP_STATUS_OK)
243 return vdpau_error(status);
245 vdctx->render = func;
247 status = create(vdctx->device, profile, width, height, avctx->refs,
249 if (status == VDP_STATUS_OK) {
250 vdctx->width = avctx->coded_width;
251 vdctx->height = avctx->coded_height;
254 return vdpau_error(status);
257 int ff_vdpau_common_uninit(AVCodecContext *avctx)
259 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
260 VdpDecoderDestroy *destroy;
264 if (vdctx->device == VDP_INVALID_HANDLE)
265 return 0; /* Decoder created and destroyed by user */
266 if (vdctx->width == UINT32_MAX && vdctx->height == UINT32_MAX)
269 status = vdctx->get_proc_address(vdctx->device,
270 VDP_FUNC_ID_DECODER_DESTROY, &func);
271 if (status != VDP_STATUS_OK)
272 return vdpau_error(status);
276 status = destroy(vdctx->decoder);
277 return vdpau_error(status);
280 static int ff_vdpau_common_reinit(AVCodecContext *avctx)
282 VDPAUHWContext *hwctx = avctx->hwaccel_context;
283 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
285 if (vdctx->device == VDP_INVALID_HANDLE)
286 return 0; /* Decoder created by user */
287 if (avctx->coded_width == vdctx->width &&
288 avctx->coded_height == vdctx->height && (!hwctx || !hwctx->reset))
291 avctx->hwaccel->uninit(avctx);
292 return avctx->hwaccel->init(avctx);
295 int ff_vdpau_common_start_frame(struct vdpau_picture_context *pic_ctx,
296 av_unused const uint8_t *buffer,
297 av_unused uint32_t size)
299 pic_ctx->bitstream_buffers_allocated = 0;
300 pic_ctx->bitstream_buffers_used = 0;
301 pic_ctx->bitstream_buffers = NULL;
305 int ff_vdpau_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
306 struct vdpau_picture_context *pic_ctx)
308 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
309 VdpVideoSurface surf = ff_vdpau_get_surface_id(frame);
313 val = ff_vdpau_common_reinit(avctx);
317 status = vdctx->render(vdctx->decoder, surf, &pic_ctx->info,
318 pic_ctx->bitstream_buffers_used,
319 pic_ctx->bitstream_buffers);
321 av_freep(&pic_ctx->bitstream_buffers);
322 return vdpau_error(status);
325 #if CONFIG_MPEG1_VDPAU_HWACCEL || \
326 CONFIG_MPEG2_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL || \
327 CONFIG_VC1_VDPAU_HWACCEL || CONFIG_WMV3_VDPAU_HWACCEL
328 int ff_vdpau_mpeg_end_frame(AVCodecContext *avctx)
330 MpegEncContext *s = avctx->priv_data;
331 Picture *pic = s->current_picture_ptr;
332 struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
335 val = ff_vdpau_common_end_frame(avctx, pic->f, pic_ctx);
339 ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
344 int ff_vdpau_add_buffer(struct vdpau_picture_context *pic_ctx,
345 const uint8_t *buf, uint32_t size)
347 VdpBitstreamBuffer *buffers = pic_ctx->bitstream_buffers;
349 buffers = av_fast_realloc(buffers, &pic_ctx->bitstream_buffers_allocated,
350 (pic_ctx->bitstream_buffers_used + 1) * sizeof(*buffers));
352 return AVERROR(ENOMEM);
354 pic_ctx->bitstream_buffers = buffers;
355 buffers += pic_ctx->bitstream_buffers_used++;
357 buffers->struct_version = VDP_BITSTREAM_BUFFER_VERSION;
358 buffers->bitstream = buf;
359 buffers->bitstream_bytes = size;
363 #if FF_API_VDPAU_PROFILE
364 int av_vdpau_get_profile(AVCodecContext *avctx, VdpDecoderProfile *profile)
366 #define PROFILE(prof) \
368 *profile = VDP_DECODER_PROFILE_##prof; \
372 switch (avctx->codec_id) {
373 case AV_CODEC_ID_MPEG1VIDEO: PROFILE(MPEG1);
374 case AV_CODEC_ID_MPEG2VIDEO:
375 switch (avctx->profile) {
376 case FF_PROFILE_MPEG2_MAIN: PROFILE(MPEG2_MAIN);
377 case FF_PROFILE_MPEG2_SIMPLE: PROFILE(MPEG2_SIMPLE);
378 default: return AVERROR(EINVAL);
380 case AV_CODEC_ID_H263: PROFILE(MPEG4_PART2_ASP);
381 case AV_CODEC_ID_MPEG4:
382 switch (avctx->profile) {
383 case FF_PROFILE_MPEG4_SIMPLE: PROFILE(MPEG4_PART2_SP);
384 case FF_PROFILE_MPEG4_ADVANCED_SIMPLE: PROFILE(MPEG4_PART2_ASP);
385 default: return AVERROR(EINVAL);
387 case AV_CODEC_ID_H264:
388 switch (avctx->profile & ~FF_PROFILE_H264_INTRA) {
389 case FF_PROFILE_H264_BASELINE: PROFILE(H264_BASELINE);
390 case FF_PROFILE_H264_CONSTRAINED_BASELINE:
391 case FF_PROFILE_H264_MAIN: PROFILE(H264_MAIN);
392 case FF_PROFILE_H264_HIGH: PROFILE(H264_HIGH);
393 #ifdef VDP_DECODER_PROFILE_H264_EXTENDED
394 case FF_PROFILE_H264_EXTENDED: PROFILE(H264_EXTENDED);
396 default: return AVERROR(EINVAL);
398 case AV_CODEC_ID_WMV3:
399 case AV_CODEC_ID_VC1:
400 switch (avctx->profile) {
401 case FF_PROFILE_VC1_SIMPLE: PROFILE(VC1_SIMPLE);
402 case FF_PROFILE_VC1_MAIN: PROFILE(VC1_MAIN);
403 case FF_PROFILE_VC1_ADVANCED: PROFILE(VC1_ADVANCED);
404 default: return AVERROR(EINVAL);
407 return AVERROR(EINVAL);
410 #endif /* FF_API_VDPAU_PROFILE */
412 AVVDPAUContext *av_vdpau_alloc_context(void)
414 return av_mallocz(sizeof(AVVDPAUContext));
417 int av_vdpau_bind_context(AVCodecContext *avctx, VdpDevice device,
418 VdpGetProcAddress *get_proc, unsigned flags)
420 VDPAUHWContext *hwctx;
422 if (flags & ~(AV_HWACCEL_FLAG_IGNORE_LEVEL|AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH))
423 return AVERROR(EINVAL);
425 if (av_reallocp(&avctx->hwaccel_context, sizeof(*hwctx)))
426 return AVERROR(ENOMEM);
428 hwctx = avctx->hwaccel_context;
430 memset(hwctx, 0, sizeof(*hwctx));
431 hwctx->context.decoder = VDP_INVALID_HANDLE;
432 hwctx->device = device;
433 hwctx->get_proc_address = get_proc;
434 hwctx->flags = flags;