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 case AV_PIX_FMT_YUV420P10:
87 case AV_PIX_FMT_YUV420P12:
88 t = VDP_CHROMA_TYPE_420;
92 case AV_PIX_FMT_YUV422P:
93 case AV_PIX_FMT_YUVJ422P:
94 t = VDP_CHROMA_TYPE_422;
98 case AV_PIX_FMT_YUV444P:
99 case AV_PIX_FMT_YUVJ444P:
100 case AV_PIX_FMT_YUV444P10:
101 case AV_PIX_FMT_YUV444P12:
102 t = VDP_CHROMA_TYPE_444;
106 return AVERROR(ENOSYS);
118 int ff_vdpau_common_frame_params(AVCodecContext *avctx,
119 AVBufferRef *hw_frames_ctx)
121 AVHWFramesContext *hw_frames = (AVHWFramesContext*)hw_frames_ctx->data;
126 if (av_vdpau_get_surface_parameters(avctx, &type, &width, &height))
127 return AVERROR(EINVAL);
129 hw_frames->format = AV_PIX_FMT_VDPAU;
130 hw_frames->sw_format = avctx->sw_pix_fmt;
131 hw_frames->width = width;
132 hw_frames->height = height;
137 int ff_vdpau_common_init(AVCodecContext *avctx, VdpDecoderProfile profile,
140 VDPAUHWContext *hwctx = avctx->hwaccel_context;
141 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
142 VdpVideoSurfaceQueryCapabilities *surface_query_caps;
143 VdpDecoderQueryCapabilities *decoder_query_caps;
144 VdpDecoderCreate *create;
145 VdpGetInformationString *info;
146 const char *info_string;
150 uint32_t max_level, max_mb, max_width, max_height;
156 vdctx->width = UINT32_MAX;
157 vdctx->height = UINT32_MAX;
159 if (av_vdpau_get_surface_parameters(avctx, &type, &width, &height))
160 return AVERROR(ENOSYS);
165 if (hwctx->context.decoder != VDP_INVALID_HANDLE) {
166 vdctx->decoder = hwctx->context.decoder;
167 vdctx->render = hwctx->context.render;
168 vdctx->device = VDP_INVALID_HANDLE;
169 return 0; /* Decoder created by user */
172 vdctx->device = hwctx->device;
173 vdctx->get_proc_address = hwctx->get_proc_address;
175 if (hwctx->flags & AV_HWACCEL_FLAG_IGNORE_LEVEL)
178 if (!(hwctx->flags & AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH) &&
179 type != VDP_CHROMA_TYPE_420)
180 return AVERROR(ENOSYS);
182 AVHWFramesContext *frames_ctx;
183 AVVDPAUDeviceContext *dev_ctx;
185 ret = ff_decode_get_hw_frames_ctx(avctx, AV_HWDEVICE_TYPE_VDPAU);
189 frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
190 dev_ctx = frames_ctx->device_ctx->hwctx;
192 vdctx->device = dev_ctx->device;
193 vdctx->get_proc_address = dev_ctx->get_proc_address;
195 if (avctx->hwaccel_flags & AV_HWACCEL_FLAG_IGNORE_LEVEL)
200 return AVERROR(ENOTSUP);
202 status = vdctx->get_proc_address(vdctx->device,
203 VDP_FUNC_ID_GET_INFORMATION_STRING,
205 if (status != VDP_STATUS_OK)
206 return vdpau_error(status);
210 status = info(&info_string);
211 if (status != VDP_STATUS_OK)
212 return vdpau_error(status);
213 if (avctx->codec_id == AV_CODEC_ID_HEVC && strncmp(info_string, "NVIDIA ", 7) == 0 &&
214 !(avctx->hwaccel_flags & AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH)) {
215 int driver_version = 0;
216 sscanf(info_string, "NVIDIA VDPAU Driver Shared Library %d", &driver_version);
217 if (driver_version < 410) {
218 av_log(avctx, AV_LOG_VERBOSE, "HEVC with NVIDIA VDPAU drivers is buggy, skipping.\n");
219 return AVERROR(ENOTSUP);
223 status = vdctx->get_proc_address(vdctx->device,
224 VDP_FUNC_ID_VIDEO_SURFACE_QUERY_CAPABILITIES,
226 if (status != VDP_STATUS_OK)
227 return vdpau_error(status);
229 surface_query_caps = func;
231 status = surface_query_caps(vdctx->device, type, &supported,
232 &max_width, &max_height);
233 if (status != VDP_STATUS_OK)
234 return vdpau_error(status);
235 if (supported != VDP_TRUE ||
236 max_width < width || max_height < height)
237 return AVERROR(ENOTSUP);
239 status = vdctx->get_proc_address(vdctx->device,
240 VDP_FUNC_ID_DECODER_QUERY_CAPABILITIES,
242 if (status != VDP_STATUS_OK)
243 return vdpau_error(status);
245 decoder_query_caps = func;
247 status = decoder_query_caps(vdctx->device, profile, &supported, &max_level,
248 &max_mb, &max_width, &max_height);
249 #ifdef VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE
250 if ((status != VDP_STATUS_OK || supported != VDP_TRUE) && profile == VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE) {
251 profile = VDP_DECODER_PROFILE_H264_MAIN;
252 status = decoder_query_caps(vdctx->device, profile, &supported,
254 &max_width, &max_height);
257 if (status != VDP_STATUS_OK)
258 return vdpau_error(status);
260 if (supported != VDP_TRUE || max_level < level ||
261 max_width < width || max_height < height)
262 return AVERROR(ENOTSUP);
264 status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_CREATE,
266 if (status != VDP_STATUS_OK)
267 return vdpau_error(status);
271 status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_RENDER,
273 if (status != VDP_STATUS_OK)
274 return vdpau_error(status);
276 vdctx->render = func;
278 status = create(vdctx->device, profile, width, height, avctx->refs,
280 if (status == VDP_STATUS_OK) {
281 vdctx->width = avctx->coded_width;
282 vdctx->height = avctx->coded_height;
285 return vdpau_error(status);
288 int ff_vdpau_common_uninit(AVCodecContext *avctx)
290 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
291 VdpDecoderDestroy *destroy;
295 if (vdctx->device == VDP_INVALID_HANDLE)
296 return 0; /* Decoder created and destroyed by user */
297 if (vdctx->width == UINT32_MAX && vdctx->height == UINT32_MAX)
300 status = vdctx->get_proc_address(vdctx->device,
301 VDP_FUNC_ID_DECODER_DESTROY, &func);
302 if (status != VDP_STATUS_OK)
303 return vdpau_error(status);
307 status = destroy(vdctx->decoder);
308 return vdpau_error(status);
311 static int ff_vdpau_common_reinit(AVCodecContext *avctx)
313 VDPAUHWContext *hwctx = avctx->hwaccel_context;
314 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
316 if (vdctx->device == VDP_INVALID_HANDLE)
317 return 0; /* Decoder created by user */
318 if (avctx->coded_width == vdctx->width &&
319 avctx->coded_height == vdctx->height && (!hwctx || !hwctx->reset))
322 avctx->hwaccel->uninit(avctx);
323 return avctx->hwaccel->init(avctx);
326 int ff_vdpau_common_start_frame(struct vdpau_picture_context *pic_ctx,
327 av_unused const uint8_t *buffer,
328 av_unused uint32_t size)
330 pic_ctx->bitstream_buffers_allocated = 0;
331 pic_ctx->bitstream_buffers_used = 0;
332 pic_ctx->bitstream_buffers = NULL;
336 int ff_vdpau_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
337 struct vdpau_picture_context *pic_ctx)
339 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
340 AVVDPAUContext *hwctx = avctx->hwaccel_context;
341 VdpVideoSurface surf = ff_vdpau_get_surface_id(frame);
345 val = ff_vdpau_common_reinit(avctx);
349 if (hwctx && !hwctx->render && hwctx->render2) {
350 status = hwctx->render2(avctx, frame, (void *)&pic_ctx->info,
351 pic_ctx->bitstream_buffers_used, pic_ctx->bitstream_buffers);
353 status = vdctx->render(vdctx->decoder, surf, &pic_ctx->info,
354 pic_ctx->bitstream_buffers_used,
355 pic_ctx->bitstream_buffers);
357 av_freep(&pic_ctx->bitstream_buffers);
359 return vdpau_error(status);
362 #if CONFIG_MPEG1_VDPAU_HWACCEL || \
363 CONFIG_MPEG2_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL || \
364 CONFIG_VC1_VDPAU_HWACCEL || CONFIG_WMV3_VDPAU_HWACCEL
365 int ff_vdpau_mpeg_end_frame(AVCodecContext *avctx)
367 MpegEncContext *s = avctx->priv_data;
368 Picture *pic = s->current_picture_ptr;
369 struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
372 val = ff_vdpau_common_end_frame(avctx, pic->f, pic_ctx);
376 ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
381 int ff_vdpau_add_buffer(struct vdpau_picture_context *pic_ctx,
382 const uint8_t *buf, uint32_t size)
384 VdpBitstreamBuffer *buffers = pic_ctx->bitstream_buffers;
386 buffers = av_fast_realloc(buffers, &pic_ctx->bitstream_buffers_allocated,
387 (pic_ctx->bitstream_buffers_used + 1) * sizeof(*buffers));
389 return AVERROR(ENOMEM);
391 pic_ctx->bitstream_buffers = buffers;
392 buffers += pic_ctx->bitstream_buffers_used++;
394 buffers->struct_version = VDP_BITSTREAM_BUFFER_VERSION;
395 buffers->bitstream = buf;
396 buffers->bitstream_bytes = size;
400 #if FF_API_VDPAU_PROFILE
401 int av_vdpau_get_profile(AVCodecContext *avctx, VdpDecoderProfile *profile)
403 #define PROFILE(prof) \
405 *profile = VDP_DECODER_PROFILE_##prof; \
409 switch (avctx->codec_id) {
410 case AV_CODEC_ID_MPEG1VIDEO: PROFILE(MPEG1);
411 case AV_CODEC_ID_MPEG2VIDEO:
412 switch (avctx->profile) {
413 case FF_PROFILE_MPEG2_MAIN: PROFILE(MPEG2_MAIN);
414 case FF_PROFILE_MPEG2_SIMPLE: PROFILE(MPEG2_SIMPLE);
415 default: return AVERROR(EINVAL);
417 case AV_CODEC_ID_H263: PROFILE(MPEG4_PART2_ASP);
418 case AV_CODEC_ID_MPEG4:
419 switch (avctx->profile) {
420 case FF_PROFILE_MPEG4_SIMPLE: PROFILE(MPEG4_PART2_SP);
421 case FF_PROFILE_MPEG4_ADVANCED_SIMPLE: PROFILE(MPEG4_PART2_ASP);
422 default: return AVERROR(EINVAL);
424 case AV_CODEC_ID_H264:
425 switch (avctx->profile & ~FF_PROFILE_H264_INTRA) {
426 case FF_PROFILE_H264_BASELINE: PROFILE(H264_BASELINE);
427 case FF_PROFILE_H264_CONSTRAINED_BASELINE:
428 case FF_PROFILE_H264_MAIN: PROFILE(H264_MAIN);
429 case FF_PROFILE_H264_HIGH: PROFILE(H264_HIGH);
430 #ifdef VDP_DECODER_PROFILE_H264_EXTENDED
431 case FF_PROFILE_H264_EXTENDED: PROFILE(H264_EXTENDED);
433 default: return AVERROR(EINVAL);
435 case AV_CODEC_ID_WMV3:
436 case AV_CODEC_ID_VC1:
437 switch (avctx->profile) {
438 case FF_PROFILE_VC1_SIMPLE: PROFILE(VC1_SIMPLE);
439 case FF_PROFILE_VC1_MAIN: PROFILE(VC1_MAIN);
440 case FF_PROFILE_VC1_ADVANCED: PROFILE(VC1_ADVANCED);
441 default: return AVERROR(EINVAL);
444 return AVERROR(EINVAL);
447 #endif /* FF_API_VDPAU_PROFILE */
449 AVVDPAUContext *av_vdpau_alloc_context(void)
451 return av_mallocz(sizeof(VDPAUHWContext));
454 int av_vdpau_bind_context(AVCodecContext *avctx, VdpDevice device,
455 VdpGetProcAddress *get_proc, unsigned flags)
457 VDPAUHWContext *hwctx;
459 if (flags & ~(AV_HWACCEL_FLAG_IGNORE_LEVEL|AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH))
460 return AVERROR(EINVAL);
462 if (av_reallocp(&avctx->hwaccel_context, sizeof(*hwctx)))
463 return AVERROR(ENOMEM);
465 hwctx = avctx->hwaccel_context;
467 memset(hwctx, 0, sizeof(*hwctx));
468 hwctx->context.decoder = VDP_INVALID_HANDLE;
469 hwctx->device = device;
470 hwctx->get_proc_address = get_proc;
471 hwctx->flags = flags;