2009-01-05 00:55:27 +01:00
|
|
|
/*
|
|
|
|
* Video Decode and Presentation API for UNIX (VDPAU) is used for
|
2010-03-15 00:52:45 +01:00
|
|
|
* HW decode acceleration for MPEG-1/2, MPEG-4 ASP, H.264 and VC-1.
|
2009-01-05 00:55:27 +01:00
|
|
|
*
|
2009-01-19 16:46:40 +01:00
|
|
|
* Copyright (c) 2008 NVIDIA
|
2009-01-05 00:55:27 +01:00
|
|
|
*
|
2011-03-18 18:35:10 +01:00
|
|
|
* This file is part of Libav.
|
2009-01-05 00:55:27 +01:00
|
|
|
*
|
2011-03-18 18:35:10 +01:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2009-01-05 00:55:27 +01:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2011-03-18 18:35:10 +01:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2009-01-05 00:55:27 +01:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2011-03-18 18:35:10 +01:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2009-01-05 00:55:27 +01:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <limits.h>
|
2014-10-04 15:55:04 +02:00
|
|
|
#include "libavutil/avassert.h"
|
2009-01-05 00:55:27 +01:00
|
|
|
#include "avcodec.h"
|
2014-10-04 15:55:04 +02:00
|
|
|
#include "internal.h"
|
2009-01-05 00:55:27 +01:00
|
|
|
#include "h264.h"
|
2009-01-20 10:28:36 +01:00
|
|
|
#include "vc1.h"
|
2009-01-05 00:55:27 +01:00
|
|
|
|
|
|
|
#undef NDEBUG
|
|
|
|
#include <assert.h>
|
|
|
|
|
2009-02-14 14:37:18 +01:00
|
|
|
#include "vdpau.h"
|
2009-01-05 00:55:27 +01:00
|
|
|
#include "vdpau_internal.h"
|
|
|
|
|
|
|
|
/**
|
2011-06-23 22:41:54 +02:00
|
|
|
* @addtogroup VDPAU_Decoding
|
2009-01-05 00:55:27 +01:00
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2014-10-04 15:55:02 +02:00
|
|
|
static int vdpau_error(VdpStatus status)
|
|
|
|
{
|
|
|
|
switch (status) {
|
|
|
|
case VDP_STATUS_OK:
|
|
|
|
return 0;
|
|
|
|
case VDP_STATUS_NO_IMPLEMENTATION:
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
case VDP_STATUS_DISPLAY_PREEMPTED:
|
|
|
|
return AVERROR(EIO);
|
|
|
|
case VDP_STATUS_INVALID_HANDLE:
|
|
|
|
return AVERROR(EBADF);
|
|
|
|
case VDP_STATUS_INVALID_POINTER:
|
|
|
|
return AVERROR(EFAULT);
|
|
|
|
case VDP_STATUS_RESOURCES:
|
|
|
|
return AVERROR(ENOBUFS);
|
|
|
|
case VDP_STATUS_HANDLE_DEVICE_MISMATCH:
|
|
|
|
return AVERROR(EXDEV);
|
|
|
|
case VDP_STATUS_ERROR:
|
|
|
|
return AVERROR(EIO);
|
|
|
|
default:
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-04 15:55:04 +02:00
|
|
|
int ff_vdpau_common_init(AVCodecContext *avctx, VdpDecoderProfile profile,
|
|
|
|
int level)
|
|
|
|
{
|
|
|
|
VDPAUHWContext *hwctx = avctx->hwaccel_context;
|
|
|
|
VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
|
2014-10-04 15:55:08 +02:00
|
|
|
VdpVideoSurfaceQueryCapabilities *surface_query_caps;
|
|
|
|
VdpDecoderQueryCapabilities *decoder_query_caps;
|
2014-10-04 15:55:04 +02:00
|
|
|
VdpDecoderCreate *create;
|
|
|
|
void *func;
|
|
|
|
VdpStatus status;
|
2014-10-04 15:55:08 +02:00
|
|
|
VdpBool supported;
|
|
|
|
uint32_t max_level, max_mb, max_width, max_height;
|
2014-10-04 15:55:04 +02:00
|
|
|
/* See vdpau/vdpau.h for alignment constraints. */
|
|
|
|
uint32_t width = (avctx->coded_width + 1) & ~1;
|
|
|
|
uint32_t height = (avctx->coded_height + 3) & ~3;
|
|
|
|
|
2014-10-04 15:55:06 +02:00
|
|
|
vdctx->width = UINT32_MAX;
|
|
|
|
vdctx->height = UINT32_MAX;
|
2014-10-04 15:55:07 +02:00
|
|
|
hwctx->reset = 0;
|
2014-10-04 15:55:06 +02:00
|
|
|
|
2014-10-04 15:55:04 +02:00
|
|
|
if (hwctx->context.decoder != VDP_INVALID_HANDLE) {
|
|
|
|
vdctx->decoder = hwctx->context.decoder;
|
|
|
|
vdctx->render = hwctx->context.render;
|
|
|
|
vdctx->device = VDP_INVALID_HANDLE;
|
|
|
|
return 0; /* Decoder created by user */
|
|
|
|
}
|
|
|
|
|
|
|
|
vdctx->device = hwctx->device;
|
|
|
|
vdctx->get_proc_address = hwctx->get_proc_address;
|
|
|
|
|
2014-10-14 11:59:54 +02:00
|
|
|
if (hwctx->flags & AV_HWACCEL_FLAG_IGNORE_LEVEL)
|
|
|
|
level = 0;
|
|
|
|
else if (level < 0)
|
2014-10-04 15:55:08 +02:00
|
|
|
return AVERROR(ENOTSUP);
|
|
|
|
|
|
|
|
status = vdctx->get_proc_address(vdctx->device,
|
|
|
|
VDP_FUNC_ID_VIDEO_SURFACE_QUERY_CAPABILITIES,
|
|
|
|
&func);
|
|
|
|
if (status != VDP_STATUS_OK)
|
|
|
|
return vdpau_error(status);
|
|
|
|
else
|
|
|
|
surface_query_caps = func;
|
|
|
|
|
|
|
|
status = surface_query_caps(vdctx->device, VDP_CHROMA_TYPE_420, &supported,
|
|
|
|
&max_width, &max_height);
|
|
|
|
if (status != VDP_STATUS_OK)
|
|
|
|
return vdpau_error(status);
|
|
|
|
if (supported != VDP_TRUE ||
|
|
|
|
max_width < width || max_height < height)
|
|
|
|
return AVERROR(ENOTSUP);
|
|
|
|
|
|
|
|
status = vdctx->get_proc_address(vdctx->device,
|
|
|
|
VDP_FUNC_ID_DECODER_QUERY_CAPABILITIES,
|
|
|
|
&func);
|
|
|
|
if (status != VDP_STATUS_OK)
|
|
|
|
return vdpau_error(status);
|
|
|
|
else
|
|
|
|
decoder_query_caps = func;
|
|
|
|
|
|
|
|
status = decoder_query_caps(vdctx->device, profile, &supported, &max_level,
|
|
|
|
&max_mb, &max_width, &max_height);
|
|
|
|
if (status != VDP_STATUS_OK)
|
|
|
|
return vdpau_error(status);
|
|
|
|
|
|
|
|
if (supported != VDP_TRUE || max_level < level ||
|
|
|
|
max_width < width || max_height < height)
|
|
|
|
return AVERROR(ENOTSUP);
|
|
|
|
|
2014-10-04 15:55:04 +02:00
|
|
|
status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_CREATE,
|
|
|
|
&func);
|
|
|
|
if (status != VDP_STATUS_OK)
|
|
|
|
return vdpau_error(status);
|
|
|
|
else
|
|
|
|
create = func;
|
|
|
|
|
|
|
|
status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_RENDER,
|
|
|
|
&func);
|
|
|
|
if (status != VDP_STATUS_OK)
|
|
|
|
return vdpau_error(status);
|
|
|
|
else
|
|
|
|
vdctx->render = func;
|
|
|
|
|
|
|
|
status = create(vdctx->device, profile, width, height, avctx->refs,
|
|
|
|
&vdctx->decoder);
|
2014-10-04 15:55:06 +02:00
|
|
|
if (status == VDP_STATUS_OK) {
|
|
|
|
vdctx->width = avctx->coded_width;
|
|
|
|
vdctx->height = avctx->coded_height;
|
|
|
|
}
|
|
|
|
|
2014-10-04 15:55:04 +02:00
|
|
|
return vdpau_error(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ff_vdpau_common_uninit(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
|
|
|
|
VdpDecoderDestroy *destroy;
|
|
|
|
void *func;
|
|
|
|
VdpStatus status;
|
|
|
|
|
|
|
|
if (vdctx->device == VDP_INVALID_HANDLE)
|
|
|
|
return 0; /* Decoder created and destroyed by user */
|
2014-10-04 15:55:06 +02:00
|
|
|
if (vdctx->width == UINT32_MAX && vdctx->height == UINT32_MAX)
|
|
|
|
return 0;
|
2014-10-04 15:55:04 +02:00
|
|
|
|
|
|
|
status = vdctx->get_proc_address(vdctx->device,
|
|
|
|
VDP_FUNC_ID_DECODER_DESTROY, &func);
|
|
|
|
if (status != VDP_STATUS_OK)
|
|
|
|
return vdpau_error(status);
|
|
|
|
else
|
|
|
|
destroy = func;
|
|
|
|
|
|
|
|
status = destroy(vdctx->decoder);
|
|
|
|
return vdpau_error(status);
|
|
|
|
}
|
|
|
|
|
2014-10-04 15:55:06 +02:00
|
|
|
static int ff_vdpau_common_reinit(AVCodecContext *avctx)
|
|
|
|
{
|
2014-10-04 15:55:07 +02:00
|
|
|
VDPAUHWContext *hwctx = avctx->hwaccel_context;
|
2014-10-04 15:55:06 +02:00
|
|
|
VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
|
|
|
|
|
|
|
|
if (vdctx->device == VDP_INVALID_HANDLE)
|
|
|
|
return 0; /* Decoder created by user */
|
|
|
|
if (avctx->coded_width == vdctx->width &&
|
2014-10-04 15:55:07 +02:00
|
|
|
avctx->coded_height == vdctx->height && !hwctx->reset)
|
2014-10-04 15:55:06 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
avctx->hwaccel->uninit(avctx);
|
|
|
|
return avctx->hwaccel->init(avctx);
|
|
|
|
}
|
|
|
|
|
2014-03-20 08:53:16 +01:00
|
|
|
int ff_vdpau_common_start_frame(struct vdpau_picture_context *pic_ctx,
|
2013-01-12 16:53:43 +01:00
|
|
|
av_unused const uint8_t *buffer,
|
|
|
|
av_unused uint32_t size)
|
|
|
|
{
|
2013-07-25 21:30:20 +02:00
|
|
|
pic_ctx->bitstream_buffers_allocated = 0;
|
|
|
|
pic_ctx->bitstream_buffers_used = 0;
|
|
|
|
pic_ctx->bitstream_buffers = NULL;
|
2013-01-12 16:53:43 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-04 15:55:03 +02:00
|
|
|
int ff_vdpau_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
|
|
|
|
struct vdpau_picture_context *pic_ctx)
|
|
|
|
{
|
2014-10-04 15:55:05 +02:00
|
|
|
VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
|
2014-10-04 15:55:03 +02:00
|
|
|
VdpVideoSurface surf = ff_vdpau_get_surface_id(frame);
|
|
|
|
VdpStatus status;
|
2014-10-04 15:55:06 +02:00
|
|
|
int val;
|
|
|
|
|
|
|
|
val = ff_vdpau_common_reinit(avctx);
|
|
|
|
if (val < 0)
|
|
|
|
return val;
|
2014-10-04 15:55:03 +02:00
|
|
|
|
2014-10-04 15:55:05 +02:00
|
|
|
status = vdctx->render(vdctx->decoder, surf, (void *)&pic_ctx->info,
|
2014-10-04 15:55:03 +02:00
|
|
|
pic_ctx->bitstream_buffers_used,
|
|
|
|
pic_ctx->bitstream_buffers);
|
|
|
|
|
|
|
|
av_freep(&pic_ctx->bitstream_buffers);
|
|
|
|
return vdpau_error(status);
|
|
|
|
}
|
|
|
|
|
2013-03-26 22:47:29 +01:00
|
|
|
#if CONFIG_H263_VDPAU_HWACCEL || CONFIG_MPEG1_VDPAU_HWACCEL || \
|
|
|
|
CONFIG_MPEG2_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL || \
|
|
|
|
CONFIG_VC1_VDPAU_HWACCEL || CONFIG_WMV3_VDPAU_HWACCEL
|
2013-02-03 11:10:05 +01:00
|
|
|
int ff_vdpau_mpeg_end_frame(AVCodecContext *avctx)
|
2013-01-12 16:53:43 +01:00
|
|
|
{
|
2013-02-03 11:10:05 +01:00
|
|
|
MpegEncContext *s = avctx->priv_data;
|
2013-07-25 21:30:20 +02:00
|
|
|
Picture *pic = s->current_picture_ptr;
|
|
|
|
struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
|
2014-10-04 15:55:03 +02:00
|
|
|
int val;
|
2013-01-12 16:53:43 +01:00
|
|
|
|
2014-10-04 15:55:03 +02:00
|
|
|
val = ff_vdpau_common_end_frame(avctx, pic->f, pic_ctx);
|
|
|
|
if (val < 0)
|
|
|
|
return val;
|
2013-01-12 16:53:43 +01:00
|
|
|
|
2013-02-03 11:10:05 +01:00
|
|
|
ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
|
2013-01-12 16:53:43 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2013-03-26 22:47:29 +01:00
|
|
|
#endif
|
2013-01-12 16:53:43 +01:00
|
|
|
|
2014-03-20 08:53:16 +01:00
|
|
|
int ff_vdpau_add_buffer(struct vdpau_picture_context *pic_ctx,
|
|
|
|
const uint8_t *buf, uint32_t size)
|
2013-01-12 16:53:43 +01:00
|
|
|
{
|
2013-07-25 21:30:20 +02:00
|
|
|
VdpBitstreamBuffer *buffers = pic_ctx->bitstream_buffers;
|
2013-01-12 16:53:43 +01:00
|
|
|
|
2013-07-25 21:30:20 +02:00
|
|
|
buffers = av_fast_realloc(buffers, &pic_ctx->bitstream_buffers_allocated,
|
|
|
|
(pic_ctx->bitstream_buffers_used + 1) * sizeof(*buffers));
|
2013-01-12 16:53:43 +01:00
|
|
|
if (!buffers)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2013-07-25 21:30:20 +02:00
|
|
|
pic_ctx->bitstream_buffers = buffers;
|
|
|
|
buffers += pic_ctx->bitstream_buffers_used++;
|
2013-01-12 16:53:43 +01:00
|
|
|
|
|
|
|
buffers->struct_version = VDP_BITSTREAM_BUFFER_VERSION;
|
|
|
|
buffers->bitstream = buf;
|
|
|
|
buffers->bitstream_bytes = size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-01 18:37:47 +01:00
|
|
|
int av_vdpau_get_profile(AVCodecContext *avctx, VdpDecoderProfile *profile)
|
|
|
|
{
|
|
|
|
#define PROFILE(prof) \
|
|
|
|
do { \
|
|
|
|
*profile = prof; \
|
|
|
|
return 0; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
switch (avctx->codec_id) {
|
|
|
|
case AV_CODEC_ID_MPEG1VIDEO: PROFILE(VDP_DECODER_PROFILE_MPEG1);
|
|
|
|
case AV_CODEC_ID_MPEG2VIDEO:
|
|
|
|
switch (avctx->profile) {
|
|
|
|
case FF_PROFILE_MPEG2_MAIN: PROFILE(VDP_DECODER_PROFILE_MPEG2_MAIN);
|
|
|
|
case FF_PROFILE_MPEG2_SIMPLE: PROFILE(VDP_DECODER_PROFILE_MPEG2_SIMPLE);
|
|
|
|
default: return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
case AV_CODEC_ID_H263: PROFILE(VDP_DECODER_PROFILE_MPEG4_PART2_ASP);
|
|
|
|
case AV_CODEC_ID_MPEG4:
|
|
|
|
switch (avctx->profile) {
|
|
|
|
case FF_PROFILE_MPEG4_SIMPLE: PROFILE(VDP_DECODER_PROFILE_MPEG4_PART2_SP);
|
|
|
|
case FF_PROFILE_MPEG4_ADVANCED_SIMPLE: PROFILE(VDP_DECODER_PROFILE_MPEG4_PART2_ASP);
|
|
|
|
default: return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
case AV_CODEC_ID_H264:
|
2014-03-04 20:13:09 +01:00
|
|
|
switch (avctx->profile & ~FF_PROFILE_H264_INTRA) {
|
2013-11-01 18:37:47 +01:00
|
|
|
case FF_PROFILE_H264_CONSTRAINED_BASELINE:
|
|
|
|
case FF_PROFILE_H264_BASELINE: PROFILE(VDP_DECODER_PROFILE_H264_BASELINE);
|
|
|
|
case FF_PROFILE_H264_MAIN: PROFILE(VDP_DECODER_PROFILE_H264_MAIN);
|
|
|
|
case FF_PROFILE_H264_HIGH: PROFILE(VDP_DECODER_PROFILE_H264_HIGH);
|
|
|
|
default: return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
case AV_CODEC_ID_WMV3:
|
|
|
|
case AV_CODEC_ID_VC1:
|
|
|
|
switch (avctx->profile) {
|
|
|
|
case FF_PROFILE_VC1_SIMPLE: PROFILE(VDP_DECODER_PROFILE_VC1_SIMPLE);
|
|
|
|
case FF_PROFILE_VC1_MAIN: PROFILE(VDP_DECODER_PROFILE_VC1_MAIN);
|
|
|
|
case FF_PROFILE_VC1_ADVANCED: PROFILE(VDP_DECODER_PROFILE_VC1_ADVANCED);
|
|
|
|
default: return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2013-11-02 22:01:14 +01:00
|
|
|
AVVDPAUContext *av_vdpau_alloc_context(void)
|
|
|
|
{
|
|
|
|
return av_mallocz(sizeof(AVVDPAUContext));
|
|
|
|
}
|
|
|
|
|
2014-10-04 15:55:07 +02:00
|
|
|
int av_vdpau_bind_context(AVCodecContext *avctx, VdpDevice device,
|
|
|
|
VdpGetProcAddress *get_proc, unsigned flags)
|
|
|
|
{
|
|
|
|
VDPAUHWContext *hwctx;
|
|
|
|
|
2014-10-14 11:59:54 +02:00
|
|
|
if (flags & ~AV_HWACCEL_FLAG_IGNORE_LEVEL)
|
2014-10-14 11:59:53 +02:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
2014-10-04 15:55:07 +02:00
|
|
|
if (av_reallocp(&avctx->hwaccel_context, sizeof(*hwctx)))
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
hwctx = avctx->hwaccel_context;
|
|
|
|
|
|
|
|
memset(hwctx, 0, sizeof(*hwctx));
|
|
|
|
hwctx->context.decoder = VDP_INVALID_HANDLE;
|
|
|
|
hwctx->device = device;
|
|
|
|
hwctx->get_proc_address = get_proc;
|
2014-10-14 11:59:54 +02:00
|
|
|
hwctx->flags = flags;
|
2014-10-04 15:55:07 +02:00
|
|
|
hwctx->reset = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-01-05 00:55:27 +01:00
|
|
|
/* @}*/
|