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 FFmpeg.
9 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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"
33 // XXX: at the time of adding this ifdefery, av_assert* wasn't use outside.
34 // When dropping it, make sure other av_assert* were not added since then.
37 * @addtogroup VDPAU_Decoding
42 static int vdpau_error(VdpStatus status)
47 case VDP_STATUS_NO_IMPLEMENTATION:
48 return AVERROR(ENOSYS);
49 case VDP_STATUS_DISPLAY_PREEMPTED:
51 case VDP_STATUS_INVALID_HANDLE:
52 return AVERROR(EBADF);
53 case VDP_STATUS_INVALID_POINTER:
54 return AVERROR(EFAULT);
55 case VDP_STATUS_RESOURCES:
56 return AVERROR(ENOBUFS);
57 case VDP_STATUS_HANDLE_DEVICE_MISMATCH:
58 return AVERROR(EXDEV);
59 case VDP_STATUS_ERROR:
62 return AVERROR(EINVAL);
66 AVVDPAUContext *av_alloc_vdpaucontext(void)
68 return av_vdpau_alloc_context();
71 MAKE_ACCESSORS(AVVDPAUContext, vdpau_hwaccel, AVVDPAU_Render2, render2)
73 int av_vdpau_get_surface_parameters(AVCodecContext *avctx,
75 uint32_t *width, uint32_t *height)
78 uint32_t w = avctx->coded_width;
79 uint32_t h = avctx->coded_height;
81 /* See <vdpau/vdpau.h> for per-type alignment constraints. */
82 switch (avctx->sw_pix_fmt) {
83 case AV_PIX_FMT_YUV420P:
84 case AV_PIX_FMT_YUVJ420P:
85 t = VDP_CHROMA_TYPE_420;
89 case AV_PIX_FMT_YUV422P:
90 case AV_PIX_FMT_YUVJ422P:
91 t = VDP_CHROMA_TYPE_422;
95 case AV_PIX_FMT_YUV444P:
96 case AV_PIX_FMT_YUVJ444P:
97 t = VDP_CHROMA_TYPE_444;
101 return AVERROR(ENOSYS);
113 int ff_vdpau_common_init(AVCodecContext *avctx, VdpDecoderProfile profile,
116 VDPAUHWContext *hwctx = avctx->hwaccel_context;
117 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
118 VdpVideoSurfaceQueryCapabilities *surface_query_caps;
119 VdpDecoderQueryCapabilities *decoder_query_caps;
120 VdpDecoderCreate *create;
121 VdpGetInformationString *info;
122 const char *info_string;
126 uint32_t max_level, max_mb, max_width, max_height;
131 vdctx->width = UINT32_MAX;
132 vdctx->height = UINT32_MAX;
134 if (av_vdpau_get_surface_parameters(avctx, &type, &width, &height))
135 return AVERROR(ENOSYS);
140 if (hwctx->context.decoder != VDP_INVALID_HANDLE) {
141 vdctx->decoder = hwctx->context.decoder;
142 vdctx->render = hwctx->context.render;
143 vdctx->device = VDP_INVALID_HANDLE;
144 return 0; /* Decoder created by user */
147 vdctx->device = hwctx->device;
148 vdctx->get_proc_address = hwctx->get_proc_address;
150 if (hwctx->flags & AV_HWACCEL_FLAG_IGNORE_LEVEL)
153 if (!(hwctx->flags & AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH) &&
154 type != VDP_CHROMA_TYPE_420)
155 return AVERROR(ENOSYS);
157 AVHWFramesContext *frames_ctx = NULL;
158 AVVDPAUDeviceContext *dev_ctx;
160 // We assume the hw_frames_ctx always survives until ff_vdpau_common_uninit
161 // is called. This holds true as the user is not allowed to touch
162 // hw_device_ctx, or hw_frames_ctx after get_format (and ff_get_format
163 // itself also uninits before unreffing hw_frames_ctx).
164 if (avctx->hw_frames_ctx) {
165 frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
166 } else if (avctx->hw_device_ctx) {
169 avctx->hw_frames_ctx = av_hwframe_ctx_alloc(avctx->hw_device_ctx);
170 if (!avctx->hw_frames_ctx)
171 return AVERROR(ENOMEM);
173 frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
174 frames_ctx->format = AV_PIX_FMT_VDPAU;
175 frames_ctx->sw_format = avctx->sw_pix_fmt;
176 frames_ctx->width = avctx->coded_width;
177 frames_ctx->height = avctx->coded_height;
179 ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
181 av_buffer_unref(&avctx->hw_frames_ctx);
187 av_log(avctx, AV_LOG_ERROR, "A hardware frames context is "
188 "required for VDPAU decoding.\n");
189 return AVERROR(EINVAL);
192 dev_ctx = frames_ctx->device_ctx->hwctx;
194 vdctx->device = dev_ctx->device;
195 vdctx->get_proc_address = dev_ctx->get_proc_address;
197 if (avctx->hwaccel_flags & AV_HWACCEL_FLAG_IGNORE_LEVEL)
202 return AVERROR(ENOTSUP);
204 status = vdctx->get_proc_address(vdctx->device,
205 VDP_FUNC_ID_GET_INFORMATION_STRING,
207 if (status != VDP_STATUS_OK)
208 return vdpau_error(status);
212 status = info(&info_string);
213 if (status != VDP_STATUS_OK)
214 return vdpau_error(status);
215 if (avctx->codec_id == AV_CODEC_ID_HEVC && strncmp(info_string, "NVIDIA ", 7) == 0 &&
216 !(avctx->hwaccel_flags & AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH)) {
217 av_log(avctx, AV_LOG_VERBOSE, "HEVC with NVIDIA VDPAU drivers is buggy, skipping.\n");
218 return AVERROR(ENOTSUP);
221 status = vdctx->get_proc_address(vdctx->device,
222 VDP_FUNC_ID_VIDEO_SURFACE_QUERY_CAPABILITIES,
224 if (status != VDP_STATUS_OK)
225 return vdpau_error(status);
227 surface_query_caps = func;
229 status = surface_query_caps(vdctx->device, type, &supported,
230 &max_width, &max_height);
231 if (status != VDP_STATUS_OK)
232 return vdpau_error(status);
233 if (supported != VDP_TRUE ||
234 max_width < width || max_height < height)
235 return AVERROR(ENOTSUP);
237 status = vdctx->get_proc_address(vdctx->device,
238 VDP_FUNC_ID_DECODER_QUERY_CAPABILITIES,
240 if (status != VDP_STATUS_OK)
241 return vdpau_error(status);
243 decoder_query_caps = func;
245 status = decoder_query_caps(vdctx->device, profile, &supported, &max_level,
246 &max_mb, &max_width, &max_height);
247 #ifdef VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE
248 if ((status != VDP_STATUS_OK || supported != VDP_TRUE) && profile == VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE) {
249 profile = VDP_DECODER_PROFILE_H264_MAIN;
250 status = decoder_query_caps(vdctx->device, profile, &supported,
252 &max_width, &max_height);
255 if (status != VDP_STATUS_OK)
256 return vdpau_error(status);
258 if (supported != VDP_TRUE || max_level < level ||
259 max_width < width || max_height < height)
260 return AVERROR(ENOTSUP);
262 status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_CREATE,
264 if (status != VDP_STATUS_OK)
265 return vdpau_error(status);
269 status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_RENDER,
271 if (status != VDP_STATUS_OK)
272 return vdpau_error(status);
274 vdctx->render = func;
276 status = create(vdctx->device, profile, width, height, avctx->refs,
278 if (status == VDP_STATUS_OK) {
279 vdctx->width = avctx->coded_width;
280 vdctx->height = avctx->coded_height;
283 return vdpau_error(status);
286 int ff_vdpau_common_uninit(AVCodecContext *avctx)
288 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
289 VdpDecoderDestroy *destroy;
293 if (vdctx->device == VDP_INVALID_HANDLE)
294 return 0; /* Decoder created and destroyed by user */
295 if (vdctx->width == UINT32_MAX && vdctx->height == UINT32_MAX)
298 status = vdctx->get_proc_address(vdctx->device,
299 VDP_FUNC_ID_DECODER_DESTROY, &func);
300 if (status != VDP_STATUS_OK)
301 return vdpau_error(status);
305 status = destroy(vdctx->decoder);
306 return vdpau_error(status);
309 static int ff_vdpau_common_reinit(AVCodecContext *avctx)
311 VDPAUHWContext *hwctx = avctx->hwaccel_context;
312 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
314 if (vdctx->device == VDP_INVALID_HANDLE)
315 return 0; /* Decoder created by user */
316 if (avctx->coded_width == vdctx->width &&
317 avctx->coded_height == vdctx->height && (!hwctx || !hwctx->reset))
320 avctx->hwaccel->uninit(avctx);
321 return avctx->hwaccel->init(avctx);
324 int ff_vdpau_common_start_frame(struct vdpau_picture_context *pic_ctx,
325 av_unused const uint8_t *buffer,
326 av_unused uint32_t size)
328 pic_ctx->bitstream_buffers_allocated = 0;
329 pic_ctx->bitstream_buffers_used = 0;
330 pic_ctx->bitstream_buffers = NULL;
334 int ff_vdpau_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
335 struct vdpau_picture_context *pic_ctx)
337 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
338 AVVDPAUContext *hwctx = avctx->hwaccel_context;
339 VdpVideoSurface surf = ff_vdpau_get_surface_id(frame);
343 val = ff_vdpau_common_reinit(avctx);
347 if (hwctx && !hwctx->render && hwctx->render2) {
348 status = hwctx->render2(avctx, frame, (void *)&pic_ctx->info,
349 pic_ctx->bitstream_buffers_used, pic_ctx->bitstream_buffers);
351 status = vdctx->render(vdctx->decoder, surf, &pic_ctx->info,
352 pic_ctx->bitstream_buffers_used,
353 pic_ctx->bitstream_buffers);
355 av_freep(&pic_ctx->bitstream_buffers);
357 return vdpau_error(status);
360 #if CONFIG_MPEG1_VDPAU_HWACCEL || \
361 CONFIG_MPEG2_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL || \
362 CONFIG_VC1_VDPAU_HWACCEL || CONFIG_WMV3_VDPAU_HWACCEL
363 int ff_vdpau_mpeg_end_frame(AVCodecContext *avctx)
365 MpegEncContext *s = avctx->priv_data;
366 Picture *pic = s->current_picture_ptr;
367 struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
370 val = ff_vdpau_common_end_frame(avctx, pic->f, pic_ctx);
374 ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
379 int ff_vdpau_add_buffer(struct vdpau_picture_context *pic_ctx,
380 const uint8_t *buf, uint32_t size)
382 VdpBitstreamBuffer *buffers = pic_ctx->bitstream_buffers;
384 buffers = av_fast_realloc(buffers, &pic_ctx->bitstream_buffers_allocated,
385 (pic_ctx->bitstream_buffers_used + 1) * sizeof(*buffers));
387 return AVERROR(ENOMEM);
389 pic_ctx->bitstream_buffers = buffers;
390 buffers += pic_ctx->bitstream_buffers_used++;
392 buffers->struct_version = VDP_BITSTREAM_BUFFER_VERSION;
393 buffers->bitstream = buf;
394 buffers->bitstream_bytes = size;
398 #if FF_API_VDPAU_PROFILE
399 int av_vdpau_get_profile(AVCodecContext *avctx, VdpDecoderProfile *profile)
401 #define PROFILE(prof) \
403 *profile = VDP_DECODER_PROFILE_##prof; \
407 switch (avctx->codec_id) {
408 case AV_CODEC_ID_MPEG1VIDEO: PROFILE(MPEG1);
409 case AV_CODEC_ID_MPEG2VIDEO:
410 switch (avctx->profile) {
411 case FF_PROFILE_MPEG2_MAIN: PROFILE(MPEG2_MAIN);
412 case FF_PROFILE_MPEG2_SIMPLE: PROFILE(MPEG2_SIMPLE);
413 default: return AVERROR(EINVAL);
415 case AV_CODEC_ID_H263: PROFILE(MPEG4_PART2_ASP);
416 case AV_CODEC_ID_MPEG4:
417 switch (avctx->profile) {
418 case FF_PROFILE_MPEG4_SIMPLE: PROFILE(MPEG4_PART2_SP);
419 case FF_PROFILE_MPEG4_ADVANCED_SIMPLE: PROFILE(MPEG4_PART2_ASP);
420 default: return AVERROR(EINVAL);
422 case AV_CODEC_ID_H264:
423 switch (avctx->profile & ~FF_PROFILE_H264_INTRA) {
424 case FF_PROFILE_H264_BASELINE: PROFILE(H264_BASELINE);
425 case FF_PROFILE_H264_CONSTRAINED_BASELINE:
426 case FF_PROFILE_H264_MAIN: PROFILE(H264_MAIN);
427 case FF_PROFILE_H264_HIGH: PROFILE(H264_HIGH);
428 #ifdef VDP_DECODER_PROFILE_H264_EXTENDED
429 case FF_PROFILE_H264_EXTENDED: PROFILE(H264_EXTENDED);
431 default: return AVERROR(EINVAL);
433 case AV_CODEC_ID_WMV3:
434 case AV_CODEC_ID_VC1:
435 switch (avctx->profile) {
436 case FF_PROFILE_VC1_SIMPLE: PROFILE(VC1_SIMPLE);
437 case FF_PROFILE_VC1_MAIN: PROFILE(VC1_MAIN);
438 case FF_PROFILE_VC1_ADVANCED: PROFILE(VC1_ADVANCED);
439 default: return AVERROR(EINVAL);
442 return AVERROR(EINVAL);
445 #endif /* FF_API_VDPAU_PROFILE */
447 AVVDPAUContext *av_vdpau_alloc_context(void)
449 return av_mallocz(sizeof(VDPAUHWContext));
452 int av_vdpau_bind_context(AVCodecContext *avctx, VdpDevice device,
453 VdpGetProcAddress *get_proc, unsigned flags)
455 VDPAUHWContext *hwctx;
457 if (flags & ~(AV_HWACCEL_FLAG_IGNORE_LEVEL|AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH))
458 return AVERROR(EINVAL);
460 if (av_reallocp(&avctx->hwaccel_context, sizeof(*hwctx)))
461 return AVERROR(ENOMEM);
463 hwctx = avctx->hwaccel_context;
465 memset(hwctx, 0, sizeof(*hwctx));
466 hwctx->context.decoder = VDP_INVALID_HANDLE;
467 hwctx->device = device;
468 hwctx->get_proc_address = get_proc;
469 hwctx->flags = flags;