2010-01-23 18:17:57 +00:00
|
|
|
/*
|
|
|
|
* DXVA2 HW acceleration.
|
|
|
|
*
|
|
|
|
* copyright (c) 2010 Laurent Aimar
|
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* This file is part of Libav.
|
2010-01-23 18:17:57 +00:00
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2010-01-23 18:17:57 +00: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 17:35:10 +00:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2010-01-23 18:17:57 +00: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 17:35:10 +00:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2010-01-23 18:17:57 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2014-01-21 13:42:23 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "libavutil/log.h"
|
|
|
|
#include "libavutil/time.h"
|
|
|
|
|
|
|
|
#include "avcodec.h"
|
|
|
|
#include "mpegvideo.h"
|
2010-01-23 18:17:57 +00:00
|
|
|
#include "dxva2_internal.h"
|
|
|
|
|
2014-03-31 14:24:10 +00:00
|
|
|
void *ff_dxva2_get_surface(const AVFrame *frame)
|
2010-01-23 18:17:57 +00:00
|
|
|
{
|
2014-03-31 14:24:10 +00:00
|
|
|
return frame->data[3];
|
2010-01-23 18:17:57 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 08:52:47 +00:00
|
|
|
unsigned ff_dxva2_get_surface_index(const AVCodecContext *avctx,
|
|
|
|
const AVDXVAContext *ctx,
|
2014-03-31 14:24:10 +00:00
|
|
|
const AVFrame *frame)
|
2010-01-23 18:17:57 +00:00
|
|
|
{
|
2014-03-31 14:24:10 +00:00
|
|
|
void *surface = ff_dxva2_get_surface(frame);
|
2010-01-23 18:17:57 +00:00
|
|
|
unsigned i;
|
|
|
|
|
2015-05-11 08:52:47 +00:00
|
|
|
for (i = 0; i < DXVA_CONTEXT_COUNT(avctx, ctx); i++)
|
|
|
|
if (DXVA_CONTEXT_SURFACE(avctx, ctx, i) == surface)
|
2010-01-23 18:17:57 +00:00
|
|
|
return i;
|
|
|
|
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ff_dxva2_commit_buffer(AVCodecContext *avctx,
|
2015-05-11 08:52:47 +00:00
|
|
|
AVDXVAContext *ctx,
|
|
|
|
DECODER_BUFFER_DESC *dsc,
|
2010-01-23 18:17:57 +00:00
|
|
|
unsigned type, const void *data, unsigned size,
|
|
|
|
unsigned mb_count)
|
|
|
|
{
|
|
|
|
void *dxva_data;
|
|
|
|
unsigned dxva_size;
|
|
|
|
int result;
|
2014-01-19 14:42:36 +00:00
|
|
|
HRESULT hr;
|
2010-01-23 18:17:57 +00:00
|
|
|
|
2015-05-11 08:52:47 +00:00
|
|
|
#if CONFIG_D3D11VA
|
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
|
|
|
|
hr = ID3D11VideoContext_GetDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context,
|
|
|
|
D3D11VA_CONTEXT(ctx)->decoder,
|
|
|
|
type,
|
|
|
|
&dxva_size, &dxva_data);
|
|
|
|
#endif
|
|
|
|
#if CONFIG_DXVA2
|
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
|
|
|
|
hr = IDirectXVideoDecoder_GetBuffer(DXVA2_CONTEXT(ctx)->decoder, type,
|
|
|
|
&dxva_data, &dxva_size);
|
|
|
|
#endif
|
2014-01-19 14:42:36 +00:00
|
|
|
if (FAILED(hr)) {
|
2014-01-21 13:42:44 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Failed to get a buffer for %u: 0x%lx\n",
|
2014-01-19 14:42:36 +00:00
|
|
|
type, hr);
|
2010-01-23 18:17:57 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (size <= dxva_size) {
|
|
|
|
memcpy(dxva_data, data, size);
|
|
|
|
|
2015-05-11 08:52:47 +00:00
|
|
|
#if CONFIG_D3D11VA
|
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
|
|
|
|
D3D11_VIDEO_DECODER_BUFFER_DESC *dsc11 = dsc;
|
|
|
|
memset(dsc11, 0, sizeof(*dsc11));
|
|
|
|
dsc11->BufferType = type;
|
|
|
|
dsc11->DataSize = size;
|
|
|
|
dsc11->NumMBsInBuffer = mb_count;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if CONFIG_DXVA2
|
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
|
|
|
|
DXVA2_DecodeBufferDesc *dsc2 = dsc;
|
|
|
|
memset(dsc2, 0, sizeof(*dsc2));
|
|
|
|
dsc2->CompressedBufferType = type;
|
|
|
|
dsc2->DataSize = size;
|
|
|
|
dsc2->NumMBsInBuffer = mb_count;
|
|
|
|
}
|
|
|
|
#endif
|
2010-01-23 18:17:57 +00:00
|
|
|
|
|
|
|
result = 0;
|
|
|
|
} else {
|
2014-01-21 13:42:44 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Buffer for type %u was too small\n", type);
|
2010-01-23 18:17:57 +00:00
|
|
|
result = -1;
|
|
|
|
}
|
2014-01-19 14:42:36 +00:00
|
|
|
|
2015-05-11 08:52:47 +00:00
|
|
|
#if CONFIG_D3D11VA
|
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
|
|
|
|
hr = ID3D11VideoContext_ReleaseDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder, type);
|
|
|
|
#endif
|
|
|
|
#if CONFIG_DXVA2
|
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
|
|
|
|
hr = IDirectXVideoDecoder_ReleaseBuffer(DXVA2_CONTEXT(ctx)->decoder, type);
|
|
|
|
#endif
|
2014-01-19 14:42:36 +00:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
2014-01-21 13:42:44 +00:00
|
|
|
"Failed to release buffer type %u: 0x%lx\n",
|
2014-01-19 14:42:36 +00:00
|
|
|
type, hr);
|
2010-01-23 18:17:57 +00:00
|
|
|
result = -1;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-03-31 14:24:10 +00:00
|
|
|
int ff_dxva2_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
|
2010-01-23 18:17:57 +00:00
|
|
|
const void *pp, unsigned pp_size,
|
|
|
|
const void *qm, unsigned qm_size,
|
|
|
|
int (*commit_bs_si)(AVCodecContext *,
|
2015-05-11 08:52:47 +00:00
|
|
|
DECODER_BUFFER_DESC *bs,
|
|
|
|
DECODER_BUFFER_DESC *slice))
|
2010-01-23 18:17:57 +00:00
|
|
|
{
|
2015-05-11 08:52:47 +00:00
|
|
|
AVDXVAContext *ctx = avctx->hwaccel_context;
|
2010-01-23 18:17:57 +00:00
|
|
|
unsigned buffer_count = 0;
|
2015-05-11 08:52:47 +00:00
|
|
|
D3D11_VIDEO_DECODER_BUFFER_DESC buffer11[4];
|
|
|
|
DXVA2_DecodeBufferDesc buffer2[4];
|
|
|
|
DECODER_BUFFER_DESC *buffer,*buffer_slice;
|
2014-01-09 19:59:54 +00:00
|
|
|
int result, runs = 0;
|
|
|
|
HRESULT hr;
|
2015-05-11 08:52:47 +00:00
|
|
|
unsigned type;
|
2014-01-09 19:59:54 +00:00
|
|
|
|
|
|
|
do {
|
2015-05-11 08:52:47 +00:00
|
|
|
#if CONFIG_D3D11VA
|
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
|
|
|
|
hr = ID3D11VideoContext_DecoderBeginFrame(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder,
|
|
|
|
ff_dxva2_get_surface(frame),
|
|
|
|
0, NULL);
|
|
|
|
#endif
|
|
|
|
#if CONFIG_DXVA2
|
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
|
|
|
|
hr = IDirectXVideoDecoder_BeginFrame(DXVA2_CONTEXT(ctx)->decoder,
|
|
|
|
ff_dxva2_get_surface(frame),
|
|
|
|
NULL);
|
|
|
|
#endif
|
2014-01-09 19:59:54 +00:00
|
|
|
if (hr == E_PENDING)
|
|
|
|
av_usleep(2000);
|
|
|
|
} while (hr == E_PENDING && ++runs < 50);
|
2010-01-23 18:17:57 +00:00
|
|
|
|
2014-01-09 19:59:54 +00:00
|
|
|
if (FAILED(hr)) {
|
2014-01-21 13:42:44 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Failed to begin frame: 0x%lx\n", hr);
|
2010-01-23 18:17:57 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-11 08:52:47 +00:00
|
|
|
#if CONFIG_D3D11VA
|
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
|
|
|
|
buffer = &buffer11[buffer_count];
|
|
|
|
type = D3D11_VIDEO_DECODER_BUFFER_PICTURE_PARAMETERS;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if CONFIG_DXVA2
|
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
|
|
|
|
buffer = &buffer2[buffer_count];
|
|
|
|
type = DXVA2_PictureParametersBufferType;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
result = ff_dxva2_commit_buffer(avctx, ctx, buffer,
|
|
|
|
type,
|
2010-01-23 18:17:57 +00:00
|
|
|
pp, pp_size, 0);
|
|
|
|
if (result) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Failed to add picture parameter buffer\n");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
buffer_count++;
|
|
|
|
|
|
|
|
if (qm_size > 0) {
|
2015-05-11 08:52:47 +00:00
|
|
|
#if CONFIG_D3D11VA
|
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
|
|
|
|
buffer = &buffer11[buffer_count];
|
|
|
|
type = D3D11_VIDEO_DECODER_BUFFER_INVERSE_QUANTIZATION_MATRIX;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if CONFIG_DXVA2
|
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
|
|
|
|
buffer = &buffer2[buffer_count];
|
|
|
|
type = DXVA2_InverseQuantizationMatrixBufferType;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
result = ff_dxva2_commit_buffer(avctx, ctx, buffer,
|
|
|
|
type,
|
2010-01-23 18:17:57 +00:00
|
|
|
qm, qm_size, 0);
|
|
|
|
if (result) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Failed to add inverse quantization matrix buffer\n");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
buffer_count++;
|
|
|
|
}
|
|
|
|
|
2015-05-11 08:52:47 +00:00
|
|
|
#if CONFIG_D3D11VA
|
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
|
|
|
|
buffer = &buffer11[buffer_count + 0];
|
|
|
|
buffer_slice = &buffer11[buffer_count + 1];
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if CONFIG_DXVA2
|
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
|
|
|
|
buffer = &buffer2[buffer_count + 0];
|
|
|
|
buffer_slice = &buffer2[buffer_count + 1];
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-01-23 18:17:57 +00:00
|
|
|
result = commit_bs_si(avctx,
|
2015-05-11 08:52:47 +00:00
|
|
|
buffer,
|
|
|
|
buffer_slice);
|
2010-01-23 18:17:57 +00:00
|
|
|
if (result) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Failed to add bitstream or slice control buffer\n");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
buffer_count += 2;
|
|
|
|
|
|
|
|
/* TODO Film Grain when possible */
|
|
|
|
|
|
|
|
assert(buffer_count == 1 + (qm_size > 0) + 2);
|
|
|
|
|
2015-05-11 08:52:47 +00:00
|
|
|
#if CONFIG_D3D11VA
|
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
|
|
|
|
hr = ID3D11VideoContext_SubmitDecoderBuffers(D3D11VA_CONTEXT(ctx)->video_context,
|
|
|
|
D3D11VA_CONTEXT(ctx)->decoder,
|
|
|
|
buffer_count, buffer11);
|
|
|
|
#endif
|
|
|
|
#if CONFIG_DXVA2
|
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
|
|
|
|
DXVA2_DecodeExecuteParams exec = {
|
|
|
|
.NumCompBuffers = buffer_count,
|
|
|
|
.pCompressedBuffers = buffer2,
|
|
|
|
.pExtensionData = NULL,
|
|
|
|
};
|
|
|
|
hr = IDirectXVideoDecoder_Execute(DXVA2_CONTEXT(ctx)->decoder, &exec);
|
|
|
|
}
|
|
|
|
#endif
|
2014-01-19 14:42:36 +00:00
|
|
|
if (FAILED(hr)) {
|
2014-01-21 13:42:44 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Failed to execute: 0x%lx\n", hr);
|
2010-01-23 18:17:57 +00:00
|
|
|
result = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
2015-05-11 08:52:47 +00:00
|
|
|
#if CONFIG_D3D11VA
|
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
|
|
|
|
hr = ID3D11VideoContext_DecoderEndFrame(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder);
|
|
|
|
#endif
|
|
|
|
#if CONFIG_DXVA2
|
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
|
|
|
|
hr = IDirectXVideoDecoder_EndFrame(DXVA2_CONTEXT(ctx)->decoder, NULL);
|
|
|
|
#endif
|
2014-01-19 14:42:36 +00:00
|
|
|
if (FAILED(hr)) {
|
2014-01-21 13:42:44 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Failed to end frame: 0x%lx\n", hr);
|
2010-01-23 18:17:57 +00:00
|
|
|
result = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|