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++) {
46 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD && ctx->d3d11va.surface[i] == surface)
48 D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc;
49 ID3D11VideoDecoderOutputView_GetDesc(ctx->d3d11va.surface[i], &viewDesc);
50 return viewDesc.Texture2D.ArraySlice;
54 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD && ctx->dxva2.surface[i] == surface)
63 int ff_dxva2_commit_buffer(AVCodecContext *avctx,
65 DECODER_BUFFER_DESC *dsc,
66 unsigned type, const void *data, unsigned size,
75 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
76 hr = ID3D11VideoContext_GetDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context,
77 D3D11VA_CONTEXT(ctx)->decoder,
79 &dxva_size, &dxva_data);
82 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
83 hr = IDirectXVideoDecoder_GetBuffer(DXVA2_CONTEXT(ctx)->decoder, type,
84 &dxva_data, &dxva_size);
87 av_log(avctx, AV_LOG_ERROR, "Failed to get a buffer for %u: 0x%lx\n",
91 if (size <= dxva_size) {
92 memcpy(dxva_data, data, size);
95 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
96 D3D11_VIDEO_DECODER_BUFFER_DESC *dsc11 = dsc;
97 memset(dsc11, 0, sizeof(*dsc11));
98 dsc11->BufferType = type;
99 dsc11->DataSize = size;
100 dsc11->NumMBsInBuffer = mb_count;
104 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
105 DXVA2_DecodeBufferDesc *dsc2 = dsc;
106 memset(dsc2, 0, sizeof(*dsc2));
107 dsc2->CompressedBufferType = type;
108 dsc2->DataSize = size;
109 dsc2->NumMBsInBuffer = mb_count;
115 av_log(avctx, AV_LOG_ERROR, "Buffer for type %u was too small\n", type);
120 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
121 hr = ID3D11VideoContext_ReleaseDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder, type);
124 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
125 hr = IDirectXVideoDecoder_ReleaseBuffer(DXVA2_CONTEXT(ctx)->decoder, type);
128 av_log(avctx, AV_LOG_ERROR,
129 "Failed to release buffer type %u: 0x%lx\n",
136 int ff_dxva2_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
137 const void *pp, unsigned pp_size,
138 const void *qm, unsigned qm_size,
139 int (*commit_bs_si)(AVCodecContext *,
140 DECODER_BUFFER_DESC *bs,
141 DECODER_BUFFER_DESC *slice))
143 AVDXVAContext *ctx = avctx->hwaccel_context;
144 unsigned buffer_count = 0;
146 D3D11_VIDEO_DECODER_BUFFER_DESC buffer11[4];
149 DXVA2_DecodeBufferDesc buffer2[4];
151 DECODER_BUFFER_DESC *buffer = NULL, *buffer_slice = NULL;
152 int result, runs = 0;
158 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
159 if (D3D11VA_CONTEXT(ctx)->context_mutex != INVALID_HANDLE_VALUE)
160 WaitForSingleObjectEx(D3D11VA_CONTEXT(ctx)->context_mutex, INFINITE, FALSE);
161 hr = ID3D11VideoContext_DecoderBeginFrame(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder,
162 ff_dxva2_get_surface(frame),
167 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
168 hr = IDirectXVideoDecoder_BeginFrame(DXVA2_CONTEXT(ctx)->decoder,
169 ff_dxva2_get_surface(frame),
172 if (hr != E_PENDING || ++runs > 50)
175 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
176 if (D3D11VA_CONTEXT(ctx)->context_mutex != INVALID_HANDLE_VALUE)
177 ReleaseMutex(D3D11VA_CONTEXT(ctx)->context_mutex);
183 av_log(avctx, AV_LOG_ERROR, "Failed to begin frame: 0x%lx\n", hr);
185 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
186 if (D3D11VA_CONTEXT(ctx)->context_mutex != INVALID_HANDLE_VALUE)
187 ReleaseMutex(D3D11VA_CONTEXT(ctx)->context_mutex);
193 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
194 buffer = &buffer11[buffer_count];
195 type = D3D11_VIDEO_DECODER_BUFFER_PICTURE_PARAMETERS;
199 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
200 buffer = &buffer2[buffer_count];
201 type = DXVA2_PictureParametersBufferType;
204 result = ff_dxva2_commit_buffer(avctx, ctx, buffer,
208 av_log(avctx, AV_LOG_ERROR,
209 "Failed to add picture parameter buffer\n");
216 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
217 buffer = &buffer11[buffer_count];
218 type = D3D11_VIDEO_DECODER_BUFFER_INVERSE_QUANTIZATION_MATRIX;
222 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
223 buffer = &buffer2[buffer_count];
224 type = DXVA2_InverseQuantizationMatrixBufferType;
227 result = ff_dxva2_commit_buffer(avctx, ctx, buffer,
231 av_log(avctx, AV_LOG_ERROR,
232 "Failed to add inverse quantization matrix buffer\n");
239 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
240 buffer = &buffer11[buffer_count + 0];
241 buffer_slice = &buffer11[buffer_count + 1];
245 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
246 buffer = &buffer2[buffer_count + 0];
247 buffer_slice = &buffer2[buffer_count + 1];
251 result = commit_bs_si(avctx,
255 av_log(avctx, AV_LOG_ERROR,
256 "Failed to add bitstream or slice control buffer\n");
261 /* TODO Film Grain when possible */
263 assert(buffer_count == 1 + (qm_size > 0) + 2);
266 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
267 hr = ID3D11VideoContext_SubmitDecoderBuffers(D3D11VA_CONTEXT(ctx)->video_context,
268 D3D11VA_CONTEXT(ctx)->decoder,
269 buffer_count, buffer11);
272 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
273 DXVA2_DecodeExecuteParams exec = {
274 .NumCompBuffers = buffer_count,
275 .pCompressedBuffers = buffer2,
276 .pExtensionData = NULL,
278 hr = IDirectXVideoDecoder_Execute(DXVA2_CONTEXT(ctx)->decoder, &exec);
282 av_log(avctx, AV_LOG_ERROR, "Failed to execute: 0x%lx\n", hr);
288 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
289 hr = ID3D11VideoContext_DecoderEndFrame(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder);
290 if (D3D11VA_CONTEXT(ctx)->context_mutex != INVALID_HANDLE_VALUE)
291 ReleaseMutex(D3D11VA_CONTEXT(ctx)->context_mutex);
295 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
296 hr = IDirectXVideoDecoder_EndFrame(DXVA2_CONTEXT(ctx)->decoder, NULL);
299 av_log(avctx, AV_LOG_ERROR, "Failed to end frame: 0x%lx\n", hr);