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
32 #include "vdpau_internal.h"
35 * @addtogroup VDPAU_Decoding
40 static int vdpau_error(VdpStatus status)
45 case VDP_STATUS_NO_IMPLEMENTATION:
46 return AVERROR(ENOSYS);
47 case VDP_STATUS_DISPLAY_PREEMPTED:
49 case VDP_STATUS_INVALID_HANDLE:
50 return AVERROR(EBADF);
51 case VDP_STATUS_INVALID_POINTER:
52 return AVERROR(EFAULT);
53 case VDP_STATUS_RESOURCES:
54 return AVERROR(ENOBUFS);
55 case VDP_STATUS_HANDLE_DEVICE_MISMATCH:
56 return AVERROR(EXDEV);
57 case VDP_STATUS_ERROR:
60 return AVERROR(EINVAL);
64 int av_vdpau_get_surface_parameters(AVCodecContext *avctx,
66 uint32_t *width, uint32_t *height)
69 uint32_t w = avctx->coded_width;
70 uint32_t h = avctx->coded_height;
72 /* See <vdpau/vdpau.h> for per-type alignment constraints. */
73 switch (avctx->sw_pix_fmt) {
74 case AV_PIX_FMT_YUV420P:
75 case AV_PIX_FMT_YUVJ420P:
76 t = VDP_CHROMA_TYPE_420;
80 case AV_PIX_FMT_YUV422P:
81 case AV_PIX_FMT_YUVJ422P:
82 t = VDP_CHROMA_TYPE_422;
86 case AV_PIX_FMT_YUV444P:
87 case AV_PIX_FMT_YUVJ444P:
88 t = VDP_CHROMA_TYPE_444;
92 return AVERROR(ENOSYS);
104 int ff_vdpau_common_frame_params(AVCodecContext *avctx,
105 AVBufferRef *hw_frames_ctx)
107 AVHWFramesContext *hw_frames = (AVHWFramesContext*)hw_frames_ctx->data;
112 if (av_vdpau_get_surface_parameters(avctx, &type, &width, &height))
113 return AVERROR(EINVAL);
115 hw_frames->format = AV_PIX_FMT_VDPAU;
116 hw_frames->sw_format = avctx->sw_pix_fmt;
117 hw_frames->width = width;
118 hw_frames->height = height;
123 int ff_vdpau_common_init(AVCodecContext *avctx, VdpDecoderProfile profile,
126 VDPAUHWContext *hwctx = avctx->hwaccel_context;
127 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
128 VdpVideoSurfaceQueryCapabilities *surface_query_caps;
129 VdpDecoderQueryCapabilities *decoder_query_caps;
130 VdpDecoderCreate *create;
134 uint32_t max_level, max_mb, max_width, max_height;
140 vdctx->width = UINT32_MAX;
141 vdctx->height = UINT32_MAX;
143 if (av_vdpau_get_surface_parameters(avctx, &type, &width, &height))
144 return AVERROR(ENOSYS);
149 if (hwctx->context.decoder != VDP_INVALID_HANDLE) {
150 vdctx->decoder = hwctx->context.decoder;
151 vdctx->render = hwctx->context.render;
152 vdctx->device = VDP_INVALID_HANDLE;
153 return 0; /* Decoder created by user */
156 vdctx->device = hwctx->device;
157 vdctx->get_proc_address = hwctx->get_proc_address;
159 if (hwctx->flags & AV_HWACCEL_FLAG_IGNORE_LEVEL)
162 if (!(hwctx->flags & AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH) &&
163 type != VDP_CHROMA_TYPE_420)
164 return AVERROR(ENOSYS);
166 AVHWFramesContext *frames_ctx;
167 AVVDPAUDeviceContext *dev_ctx;
169 ret = ff_decode_get_hw_frames_ctx(avctx, AV_HWDEVICE_TYPE_VDPAU);
173 frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
174 dev_ctx = frames_ctx->device_ctx->hwctx;
176 vdctx->device = dev_ctx->device;
177 vdctx->get_proc_address = dev_ctx->get_proc_address;
179 if (avctx->hwaccel_flags & AV_HWACCEL_FLAG_IGNORE_LEVEL)
184 return AVERROR(ENOTSUP);
186 status = vdctx->get_proc_address(vdctx->device,
187 VDP_FUNC_ID_VIDEO_SURFACE_QUERY_CAPABILITIES,
189 if (status != VDP_STATUS_OK)
190 return vdpau_error(status);
192 surface_query_caps = func;
194 status = surface_query_caps(vdctx->device, type, &supported,
195 &max_width, &max_height);
196 if (status != VDP_STATUS_OK)
197 return vdpau_error(status);
198 if (supported != VDP_TRUE ||
199 max_width < width || max_height < height)
200 return AVERROR(ENOTSUP);
202 status = vdctx->get_proc_address(vdctx->device,
203 VDP_FUNC_ID_DECODER_QUERY_CAPABILITIES,
205 if (status != VDP_STATUS_OK)
206 return vdpau_error(status);
208 decoder_query_caps = func;
210 status = decoder_query_caps(vdctx->device, profile, &supported, &max_level,
211 &max_mb, &max_width, &max_height);
212 #ifdef VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE
213 if ((status != VDP_STATUS_OK || supported != VDP_TRUE) && profile == VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE) {
214 profile = VDP_DECODER_PROFILE_H264_MAIN;
215 status = decoder_query_caps(vdctx->device, profile, &supported,
217 &max_width, &max_height);
220 if (status != VDP_STATUS_OK)
221 return vdpau_error(status);
223 if (supported != VDP_TRUE || max_level < level ||
224 max_width < width || max_height < height)
225 return AVERROR(ENOTSUP);
227 status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_CREATE,
229 if (status != VDP_STATUS_OK)
230 return vdpau_error(status);
234 status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_RENDER,
236 if (status != VDP_STATUS_OK)
237 return vdpau_error(status);
239 vdctx->render = func;
241 status = create(vdctx->device, profile, width, height, avctx->refs,
243 if (status == VDP_STATUS_OK) {
244 vdctx->width = avctx->coded_width;
245 vdctx->height = avctx->coded_height;
248 return vdpau_error(status);
251 int ff_vdpau_common_uninit(AVCodecContext *avctx)
253 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
254 VdpDecoderDestroy *destroy;
258 if (vdctx->device == VDP_INVALID_HANDLE)
259 return 0; /* Decoder created and destroyed by user */
260 if (vdctx->width == UINT32_MAX && vdctx->height == UINT32_MAX)
263 status = vdctx->get_proc_address(vdctx->device,
264 VDP_FUNC_ID_DECODER_DESTROY, &func);
265 if (status != VDP_STATUS_OK)
266 return vdpau_error(status);
270 status = destroy(vdctx->decoder);
271 return vdpau_error(status);
274 static int ff_vdpau_common_reinit(AVCodecContext *avctx)
276 VDPAUHWContext *hwctx = avctx->hwaccel_context;
277 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
279 if (vdctx->device == VDP_INVALID_HANDLE)
280 return 0; /* Decoder created by user */
281 if (avctx->coded_width == vdctx->width &&
282 avctx->coded_height == vdctx->height && (!hwctx || !hwctx->reset))
285 avctx->hwaccel->uninit(avctx);
286 return avctx->hwaccel->init(avctx);
289 int ff_vdpau_common_start_frame(struct vdpau_picture_context *pic_ctx,
290 av_unused const uint8_t *buffer,
291 av_unused uint32_t size)
293 pic_ctx->bitstream_buffers_allocated = 0;
294 pic_ctx->bitstream_buffers_used = 0;
295 pic_ctx->bitstream_buffers = NULL;
299 int ff_vdpau_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
300 struct vdpau_picture_context *pic_ctx)
302 VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
303 VdpVideoSurface surf = ff_vdpau_get_surface_id(frame);
307 val = ff_vdpau_common_reinit(avctx);
311 status = vdctx->render(vdctx->decoder, surf, &pic_ctx->info,
312 pic_ctx->bitstream_buffers_used,
313 pic_ctx->bitstream_buffers);
315 av_freep(&pic_ctx->bitstream_buffers);
316 return vdpau_error(status);
319 #if CONFIG_MPEG1_VDPAU_HWACCEL || \
320 CONFIG_MPEG2_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL || \
321 CONFIG_VC1_VDPAU_HWACCEL || CONFIG_WMV3_VDPAU_HWACCEL
322 int ff_vdpau_mpeg_end_frame(AVCodecContext *avctx)
324 MpegEncContext *s = avctx->priv_data;
325 Picture *pic = s->current_picture_ptr;
326 struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
329 val = ff_vdpau_common_end_frame(avctx, pic->f, pic_ctx);
333 ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
338 int ff_vdpau_add_buffer(struct vdpau_picture_context *pic_ctx,
339 const uint8_t *buf, uint32_t size)
341 VdpBitstreamBuffer *buffers = pic_ctx->bitstream_buffers;
343 buffers = av_fast_realloc(buffers, &pic_ctx->bitstream_buffers_allocated,
344 (pic_ctx->bitstream_buffers_used + 1) * sizeof(*buffers));
346 return AVERROR(ENOMEM);
348 pic_ctx->bitstream_buffers = buffers;
349 buffers += pic_ctx->bitstream_buffers_used++;
351 buffers->struct_version = VDP_BITSTREAM_BUFFER_VERSION;
352 buffers->bitstream = buf;
353 buffers->bitstream_bytes = size;
357 #if FF_API_VDPAU_PROFILE
358 int av_vdpau_get_profile(AVCodecContext *avctx, VdpDecoderProfile *profile)
360 #define PROFILE(prof) \
362 *profile = VDP_DECODER_PROFILE_##prof; \
366 switch (avctx->codec_id) {
367 case AV_CODEC_ID_MPEG1VIDEO: PROFILE(MPEG1);
368 case AV_CODEC_ID_MPEG2VIDEO:
369 switch (avctx->profile) {
370 case FF_PROFILE_MPEG2_MAIN: PROFILE(MPEG2_MAIN);
371 case FF_PROFILE_MPEG2_SIMPLE: PROFILE(MPEG2_SIMPLE);
372 default: return AVERROR(EINVAL);
374 case AV_CODEC_ID_H263: PROFILE(MPEG4_PART2_ASP);
375 case AV_CODEC_ID_MPEG4:
376 switch (avctx->profile) {
377 case FF_PROFILE_MPEG4_SIMPLE: PROFILE(MPEG4_PART2_SP);
378 case FF_PROFILE_MPEG4_ADVANCED_SIMPLE: PROFILE(MPEG4_PART2_ASP);
379 default: return AVERROR(EINVAL);
381 case AV_CODEC_ID_H264:
382 switch (avctx->profile & ~FF_PROFILE_H264_INTRA) {
383 case FF_PROFILE_H264_BASELINE: PROFILE(H264_BASELINE);
384 case FF_PROFILE_H264_CONSTRAINED_BASELINE:
385 case FF_PROFILE_H264_MAIN: PROFILE(H264_MAIN);
386 case FF_PROFILE_H264_HIGH: PROFILE(H264_HIGH);
387 #ifdef VDP_DECODER_PROFILE_H264_EXTENDED
388 case FF_PROFILE_H264_EXTENDED: PROFILE(H264_EXTENDED);
390 default: return AVERROR(EINVAL);
392 case AV_CODEC_ID_WMV3:
393 case AV_CODEC_ID_VC1:
394 switch (avctx->profile) {
395 case FF_PROFILE_VC1_SIMPLE: PROFILE(VC1_SIMPLE);
396 case FF_PROFILE_VC1_MAIN: PROFILE(VC1_MAIN);
397 case FF_PROFILE_VC1_ADVANCED: PROFILE(VC1_ADVANCED);
398 default: return AVERROR(EINVAL);
401 return AVERROR(EINVAL);
404 #endif /* FF_API_VDPAU_PROFILE */
406 AVVDPAUContext *av_vdpau_alloc_context(void)
408 return av_mallocz(sizeof(AVVDPAUContext));
411 int av_vdpau_bind_context(AVCodecContext *avctx, VdpDevice device,
412 VdpGetProcAddress *get_proc, unsigned flags)
414 VDPAUHWContext *hwctx;
416 if (flags & ~(AV_HWACCEL_FLAG_IGNORE_LEVEL|AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH))
417 return AVERROR(EINVAL);
419 if (av_reallocp(&avctx->hwaccel_context, sizeof(*hwctx)))
420 return AVERROR(ENOMEM);
422 hwctx = avctx->hwaccel_context;
424 memset(hwctx, 0, sizeof(*hwctx));
425 hwctx->context.decoder = VDP_INVALID_HANDLE;
426 hwctx->device = device;
427 hwctx->get_proc_address = get_proc;
428 hwctx->flags = flags;