2 * DXVA2 HW acceleration.
4 * copyright (c) 2010 Laurent Aimar
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "libavutil/log.h"
27 #include "libavutil/time.h"
30 #include "dxva2_internal.h"
32 void *ff_dxva2_get_surface(const AVFrame *frame)
34 return frame->data[3];
37 unsigned ff_dxva2_get_surface_index(const AVCodecContext *avctx,
38 const AVDXVAContext *ctx,
41 void *surface = ff_dxva2_get_surface(frame);
44 for (i = 0; i < DXVA_CONTEXT_COUNT(avctx, ctx); i++)
45 if (DXVA_CONTEXT_SURFACE(avctx, ctx, i) == surface)
52 int ff_dxva2_commit_buffer(AVCodecContext *avctx,
54 DECODER_BUFFER_DESC *dsc,
55 unsigned type, const void *data, unsigned size,
64 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
65 hr = ID3D11VideoContext_GetDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context,
66 D3D11VA_CONTEXT(ctx)->decoder,
68 &dxva_size, &dxva_data);
71 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
72 hr = IDirectXVideoDecoder_GetBuffer(DXVA2_CONTEXT(ctx)->decoder, type,
73 &dxva_data, &dxva_size);
76 av_log(avctx, AV_LOG_ERROR, "Failed to get a buffer for %u: 0x%lx\n",
80 if (size <= dxva_size) {
81 memcpy(dxva_data, data, size);
84 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
85 D3D11_VIDEO_DECODER_BUFFER_DESC *dsc11 = dsc;
86 memset(dsc11, 0, sizeof(*dsc11));
87 dsc11->BufferType = type;
88 dsc11->DataSize = size;
89 dsc11->NumMBsInBuffer = mb_count;
93 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
94 DXVA2_DecodeBufferDesc *dsc2 = dsc;
95 memset(dsc2, 0, sizeof(*dsc2));
96 dsc2->CompressedBufferType = type;
97 dsc2->DataSize = size;
98 dsc2->NumMBsInBuffer = mb_count;
104 av_log(avctx, AV_LOG_ERROR, "Buffer for type %u was too small\n", type);
109 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
110 hr = ID3D11VideoContext_ReleaseDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder, type);
113 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
114 hr = IDirectXVideoDecoder_ReleaseBuffer(DXVA2_CONTEXT(ctx)->decoder, type);
117 av_log(avctx, AV_LOG_ERROR,
118 "Failed to release buffer type %u: 0x%lx\n",
125 int ff_dxva2_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
126 const void *pp, unsigned pp_size,
127 const void *qm, unsigned qm_size,
128 int (*commit_bs_si)(AVCodecContext *,
129 DECODER_BUFFER_DESC *bs,
130 DECODER_BUFFER_DESC *slice))
132 AVDXVAContext *ctx = avctx->hwaccel_context;
133 unsigned buffer_count = 0;
135 D3D11_VIDEO_DECODER_BUFFER_DESC buffer11[4];
138 DXVA2_DecodeBufferDesc buffer2[4];
140 DECODER_BUFFER_DESC *buffer = NULL, *buffer_slice = NULL;
141 int result, runs = 0;
147 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
148 if (D3D11VA_CONTEXT(ctx)->context_mutex != INVALID_HANDLE_VALUE)
149 WaitForSingleObjectEx(D3D11VA_CONTEXT(ctx)->context_mutex, INFINITE, FALSE);
150 hr = ID3D11VideoContext_DecoderBeginFrame(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder,
151 ff_dxva2_get_surface(frame),
156 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
157 hr = IDirectXVideoDecoder_BeginFrame(DXVA2_CONTEXT(ctx)->decoder,
158 ff_dxva2_get_surface(frame),
163 } while (hr == E_PENDING && ++runs < 50);
166 av_log(avctx, AV_LOG_ERROR, "Failed to begin frame: 0x%lx\n", hr);
168 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
169 if (D3D11VA_CONTEXT(ctx)->context_mutex != INVALID_HANDLE_VALUE)
170 ReleaseMutex(D3D11VA_CONTEXT(ctx)->context_mutex);
176 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
177 buffer = &buffer11[buffer_count];
178 type = D3D11_VIDEO_DECODER_BUFFER_PICTURE_PARAMETERS;
182 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
183 buffer = &buffer2[buffer_count];
184 type = DXVA2_PictureParametersBufferType;
187 result = ff_dxva2_commit_buffer(avctx, ctx, buffer,
191 av_log(avctx, AV_LOG_ERROR,
192 "Failed to add picture parameter buffer\n");
199 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
200 buffer = &buffer11[buffer_count];
201 type = D3D11_VIDEO_DECODER_BUFFER_INVERSE_QUANTIZATION_MATRIX;
205 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
206 buffer = &buffer2[buffer_count];
207 type = DXVA2_InverseQuantizationMatrixBufferType;
210 result = ff_dxva2_commit_buffer(avctx, ctx, buffer,
214 av_log(avctx, AV_LOG_ERROR,
215 "Failed to add inverse quantization matrix buffer\n");
222 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
223 buffer = &buffer11[buffer_count + 0];
224 buffer_slice = &buffer11[buffer_count + 1];
228 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
229 buffer = &buffer2[buffer_count + 0];
230 buffer_slice = &buffer2[buffer_count + 1];
234 result = commit_bs_si(avctx,
238 av_log(avctx, AV_LOG_ERROR,
239 "Failed to add bitstream or slice control buffer\n");
244 /* TODO Film Grain when possible */
246 assert(buffer_count == 1 + (qm_size > 0) + 2);
249 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
250 hr = ID3D11VideoContext_SubmitDecoderBuffers(D3D11VA_CONTEXT(ctx)->video_context,
251 D3D11VA_CONTEXT(ctx)->decoder,
252 buffer_count, buffer11);
255 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
256 DXVA2_DecodeExecuteParams exec = {
257 .NumCompBuffers = buffer_count,
258 .pCompressedBuffers = buffer2,
259 .pExtensionData = NULL,
261 hr = IDirectXVideoDecoder_Execute(DXVA2_CONTEXT(ctx)->decoder, &exec);
265 av_log(avctx, AV_LOG_ERROR, "Failed to execute: 0x%lx\n", hr);
271 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
272 hr = ID3D11VideoContext_DecoderEndFrame(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder);
273 if (D3D11VA_CONTEXT(ctx)->context_mutex != INVALID_HANDLE_VALUE)
274 ReleaseMutex(D3D11VA_CONTEXT(ctx)->context_mutex);
278 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
279 hr = IDirectXVideoDecoder_EndFrame(DXVA2_CONTEXT(ctx)->decoder, NULL);
282 av_log(avctx, AV_LOG_ERROR, "Failed to end frame: 0x%lx\n", hr);