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
32 #include "vdpau_internal.h"
34 // XXX: at the time of adding this ifdefery, av_assert* wasn't use outside.
35 // When dropping it, make sure other av_assert* were not added since then.
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 AVVDPAUContext *av_alloc_vdpaucontext(void)
69 return av_vdpau_alloc_context();
72 MAKE_ACCESSORS(AVVDPAUContext, vdpau_hwaccel, AVVDPAU_Render2, render2)
74 int av_vdpau_get_surface_parameters(AVCodecContext *avctx,
76 uint32_t *width, uint32_t *height)
79 uint32_t w = avctx->coded_width;
80 uint32_t h = avctx->coded_height;
82 /* See <vdpau/vdpau.h> for per-type alignment constraints. */
83 switch (avctx->sw_pix_fmt) {
84 case AV_PIX_FMT_YUV420P:
85 case AV_PIX_FMT_YUVJ420P:
86 t = VDP_CHROMA_TYPE_420;
90 case AV_PIX_FMT_YUV422P:
91 case AV_PIX_FMT_YUVJ422P:
92 t = VDP_CHROMA_TYPE_422;
96 case AV_PIX_FMT_YUV444P:
97 case AV_PIX_FMT_YUVJ444P:
98 t = VDP_CHROMA_TYPE_444;
102 return AVERROR(ENOSYS);
114 int ff_vdpau_common_frame_params(AVCodecContext *avctx,
115 AVBufferRef *hw_frames_ctx)
117 AVHWFramesContext *hw_frames = (AVHWFramesContext*)hw_frames_ctx->data;
122 if (av_vdpau_get_surface_parameters(avctx, &type, &width, &height))
123 return AVERROR(EINVAL);
125 hw_frames->format = AV_PIX_FMT_VDPAU;
126 hw_frames->sw_format = avctx->sw_pix_fmt;
127 hw_frames->width = width;
128 hw_frames->height = height;
133 int ff_vdpau_common_init(AVCodecContext *avctx, VdpDecoderProfile profile,
136 VDPAUHWContext *hwctx = avctx->hwaccel_context;
137 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
138 VdpVideoSurfaceQueryCapabilities *surface_query_caps;
139 VdpDecoderQueryCapabilities *decoder_query_caps;
140 VdpDecoderCreate *create;
141 VdpGetInformationString *info;
142 const char *info_string;
146 uint32_t max_level, max_mb, max_width, max_height;
152 vdctx->width = UINT32_MAX;
153 vdctx->height = UINT32_MAX;
155 if (av_vdpau_get_surface_parameters(avctx, &type, &width, &height))
156 return AVERROR(ENOSYS);
161 if (hwctx->context.decoder != VDP_INVALID_HANDLE) {
162 vdctx->decoder = hwctx->context.decoder;
163 vdctx->render = hwctx->context.render;
164 vdctx->device = VDP_INVALID_HANDLE;
165 return 0; /* Decoder created by user */
168 vdctx->device = hwctx->device;
169 vdctx->get_proc_address = hwctx->get_proc_address;
171 if (hwctx->flags & AV_HWACCEL_FLAG_IGNORE_LEVEL)
174 if (!(hwctx->flags & AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH) &&
175 type != VDP_CHROMA_TYPE_420)
176 return AVERROR(ENOSYS);
178 AVHWFramesContext *frames_ctx;
179 AVVDPAUDeviceContext *dev_ctx;
181 ret = ff_decode_get_hw_frames_ctx(avctx, AV_HWDEVICE_TYPE_VDPAU);
185 frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
186 dev_ctx = frames_ctx->device_ctx->hwctx;
188 vdctx->device = dev_ctx->device;
189 vdctx->get_proc_address = dev_ctx->get_proc_address;
191 if (avctx->hwaccel_flags & AV_HWACCEL_FLAG_IGNORE_LEVEL)
196 return AVERROR(ENOTSUP);
198 status = vdctx->get_proc_address(vdctx->device,
199 VDP_FUNC_ID_GET_INFORMATION_STRING,
201 if (status != VDP_STATUS_OK)
202 return vdpau_error(status);
206 status = info(&info_string);
207 if (status != VDP_STATUS_OK)
208 return vdpau_error(status);
209 if (avctx->codec_id == AV_CODEC_ID_HEVC && strncmp(info_string, "NVIDIA ", 7) == 0 &&
210 !(avctx->hwaccel_flags & AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH)) {
211 int driver_version = 0;
212 sscanf(info_string, "NVIDIA VDPAU Driver Shared Library %d", &driver_version);
213 if (driver_version < 410) {
214 av_log(avctx, AV_LOG_VERBOSE, "HEVC with NVIDIA VDPAU drivers is buggy, skipping.\n");
215 return AVERROR(ENOTSUP);
219 status = vdctx->get_proc_address(vdctx->device,
220 VDP_FUNC_ID_VIDEO_SURFACE_QUERY_CAPABILITIES,
222 if (status != VDP_STATUS_OK)
223 return vdpau_error(status);
225 surface_query_caps = func;
227 status = surface_query_caps(vdctx->device, type, &supported,
228 &max_width, &max_height);
229 if (status != VDP_STATUS_OK)
230 return vdpau_error(status);
231 if (supported != VDP_TRUE ||
232 max_width < width || max_height < height)
233 return AVERROR(ENOTSUP);
235 status = vdctx->get_proc_address(vdctx->device,
236 VDP_FUNC_ID_DECODER_QUERY_CAPABILITIES,
238 if (status != VDP_STATUS_OK)
239 return vdpau_error(status);
241 decoder_query_caps = func;
243 status = decoder_query_caps(vdctx->device, profile, &supported, &max_level,
244 &max_mb, &max_width, &max_height);
245 #ifdef VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE
246 if ((status != VDP_STATUS_OK || supported != VDP_TRUE) && profile == VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE) {
247 profile = VDP_DECODER_PROFILE_H264_MAIN;
248 status = decoder_query_caps(vdctx->device, profile, &supported,
250 &max_width, &max_height);
253 if (status != VDP_STATUS_OK)
254 return vdpau_error(status);
256 if (supported != VDP_TRUE || max_level < level ||
257 max_width < width || max_height < height)
258 return AVERROR(ENOTSUP);
260 status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_CREATE,
262 if (status != VDP_STATUS_OK)
263 return vdpau_error(status);
267 status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_RENDER,
269 if (status != VDP_STATUS_OK)
270 return vdpau_error(status);
272 vdctx->render = func;
274 status = create(vdctx->device, profile, width, height, avctx->refs,
276 if (status == VDP_STATUS_OK) {
277 vdctx->width = avctx->coded_width;
278 vdctx->height = avctx->coded_height;
281 return vdpau_error(status);
284 int ff_vdpau_common_uninit(AVCodecContext *avctx)
286 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
287 VdpDecoderDestroy *destroy;
291 if (vdctx->device == VDP_INVALID_HANDLE)
292 return 0; /* Decoder created and destroyed by user */
293 if (vdctx->width == UINT32_MAX && vdctx->height == UINT32_MAX)
296 status = vdctx->get_proc_address(vdctx->device,
297 VDP_FUNC_ID_DECODER_DESTROY, &func);
298 if (status != VDP_STATUS_OK)
299 return vdpau_error(status);
303 status = destroy(vdctx->decoder);
304 return vdpau_error(status);
307 static int ff_vdpau_common_reinit(AVCodecContext *avctx)
309 VDPAUHWContext *hwctx = avctx->hwaccel_context;
310 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
312 if (vdctx->device == VDP_INVALID_HANDLE)
313 return 0; /* Decoder created by user */
314 if (avctx->coded_width == vdctx->width &&
315 avctx->coded_height == vdctx->height && (!hwctx || !hwctx->reset))
318 avctx->hwaccel->uninit(avctx);
319 return avctx->hwaccel->init(avctx);
322 int ff_vdpau_common_start_frame(struct vdpau_picture_context *pic_ctx,
323 av_unused const uint8_t *buffer,
324 av_unused uint32_t size)
326 pic_ctx->bitstream_buffers_allocated = 0;
327 pic_ctx->bitstream_buffers_used = 0;
328 pic_ctx->bitstream_buffers = NULL;
332 int ff_vdpau_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
333 struct vdpau_picture_context *pic_ctx)
335 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
336 AVVDPAUContext *hwctx = avctx->hwaccel_context;
337 VdpVideoSurface surf = ff_vdpau_get_surface_id(frame);
341 val = ff_vdpau_common_reinit(avctx);
345 if (hwctx && !hwctx->render && hwctx->render2) {
346 status = hwctx->render2(avctx, frame, (void *)&pic_ctx->info,
347 pic_ctx->bitstream_buffers_used, pic_ctx->bitstream_buffers);
349 status = vdctx->render(vdctx->decoder, surf, &pic_ctx->info,
350 pic_ctx->bitstream_buffers_used,
351 pic_ctx->bitstream_buffers);
353 av_freep(&pic_ctx->bitstream_buffers);
355 return vdpau_error(status);
358 #if CONFIG_MPEG1_VDPAU_HWACCEL || \
359 CONFIG_MPEG2_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL || \
360 CONFIG_VC1_VDPAU_HWACCEL || CONFIG_WMV3_VDPAU_HWACCEL
361 int ff_vdpau_mpeg_end_frame(AVCodecContext *avctx)
363 MpegEncContext *s = avctx->priv_data;
364 Picture *pic = s->current_picture_ptr;
365 struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
368 val = ff_vdpau_common_end_frame(avctx, pic->f, pic_ctx);
372 ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
377 int ff_vdpau_add_buffer(struct vdpau_picture_context *pic_ctx,
378 const uint8_t *buf, uint32_t size)
380 VdpBitstreamBuffer *buffers = pic_ctx->bitstream_buffers;
382 buffers = av_fast_realloc(buffers, &pic_ctx->bitstream_buffers_allocated,
383 (pic_ctx->bitstream_buffers_used + 1) * sizeof(*buffers));
385 return AVERROR(ENOMEM);
387 pic_ctx->bitstream_buffers = buffers;
388 buffers += pic_ctx->bitstream_buffers_used++;
390 buffers->struct_version = VDP_BITSTREAM_BUFFER_VERSION;
391 buffers->bitstream = buf;
392 buffers->bitstream_bytes = size;
396 #if FF_API_VDPAU_PROFILE
397 int av_vdpau_get_profile(AVCodecContext *avctx, VdpDecoderProfile *profile)
399 #define PROFILE(prof) \
401 *profile = VDP_DECODER_PROFILE_##prof; \
405 switch (avctx->codec_id) {
406 case AV_CODEC_ID_MPEG1VIDEO: PROFILE(MPEG1);
407 case AV_CODEC_ID_MPEG2VIDEO:
408 switch (avctx->profile) {
409 case FF_PROFILE_MPEG2_MAIN: PROFILE(MPEG2_MAIN);
410 case FF_PROFILE_MPEG2_SIMPLE: PROFILE(MPEG2_SIMPLE);
411 default: return AVERROR(EINVAL);
413 case AV_CODEC_ID_H263: PROFILE(MPEG4_PART2_ASP);
414 case AV_CODEC_ID_MPEG4:
415 switch (avctx->profile) {
416 case FF_PROFILE_MPEG4_SIMPLE: PROFILE(MPEG4_PART2_SP);
417 case FF_PROFILE_MPEG4_ADVANCED_SIMPLE: PROFILE(MPEG4_PART2_ASP);
418 default: return AVERROR(EINVAL);
420 case AV_CODEC_ID_H264:
421 switch (avctx->profile & ~FF_PROFILE_H264_INTRA) {
422 case FF_PROFILE_H264_BASELINE: PROFILE(H264_BASELINE);
423 case FF_PROFILE_H264_CONSTRAINED_BASELINE:
424 case FF_PROFILE_H264_MAIN: PROFILE(H264_MAIN);
425 case FF_PROFILE_H264_HIGH: PROFILE(H264_HIGH);
426 #ifdef VDP_DECODER_PROFILE_H264_EXTENDED
427 case FF_PROFILE_H264_EXTENDED: PROFILE(H264_EXTENDED);
429 default: return AVERROR(EINVAL);
431 case AV_CODEC_ID_WMV3:
432 case AV_CODEC_ID_VC1:
433 switch (avctx->profile) {
434 case FF_PROFILE_VC1_SIMPLE: PROFILE(VC1_SIMPLE);
435 case FF_PROFILE_VC1_MAIN: PROFILE(VC1_MAIN);
436 case FF_PROFILE_VC1_ADVANCED: PROFILE(VC1_ADVANCED);
437 default: return AVERROR(EINVAL);
440 return AVERROR(EINVAL);
443 #endif /* FF_API_VDPAU_PROFILE */
445 AVVDPAUContext *av_vdpau_alloc_context(void)
447 return av_mallocz(sizeof(VDPAUHWContext));
450 int av_vdpau_bind_context(AVCodecContext *avctx, VdpDevice device,
451 VdpGetProcAddress *get_proc, unsigned flags)
453 VDPAUHWContext *hwctx;
455 if (flags & ~(AV_HWACCEL_FLAG_IGNORE_LEVEL|AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH))
456 return AVERROR(EINVAL);
458 if (av_reallocp(&avctx->hwaccel_context, sizeof(*hwctx)))
459 return AVERROR(ENOMEM);
461 hwctx = avctx->hwaccel_context;
463 memset(hwctx, 0, sizeof(*hwctx));
464 hwctx->context.decoder = VDP_INVALID_HANDLE;
465 hwctx->device = device;
466 hwctx->get_proc_address = get_proc;
467 hwctx->flags = flags;