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 static void *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 = get_surface(frame);
45 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
46 D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc;
47 ID3D11VideoDecoderOutputView_GetDesc((ID3D11VideoDecoderOutputView*) surface, &viewDesc);
48 return viewDesc.Texture2D.ArraySlice;
52 for (i = 0; i < DXVA_CONTEXT_COUNT(avctx, ctx); i++) {
53 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD && ctx->dxva2.surface[i] == surface)
62 int ff_dxva2_commit_buffer(AVCodecContext *avctx,
64 DECODER_BUFFER_DESC *dsc,
65 unsigned type, const void *data, unsigned size,
74 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
75 hr = ID3D11VideoContext_GetDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context,
76 D3D11VA_CONTEXT(ctx)->decoder,
78 &dxva_size, &dxva_data);
81 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
82 hr = IDirectXVideoDecoder_GetBuffer(DXVA2_CONTEXT(ctx)->decoder, type,
83 &dxva_data, &dxva_size);
86 av_log(avctx, AV_LOG_ERROR, "Failed to get a buffer for %u: 0x%lx\n",
90 if (size <= dxva_size) {
91 memcpy(dxva_data, data, size);
94 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
95 D3D11_VIDEO_DECODER_BUFFER_DESC *dsc11 = dsc;
96 memset(dsc11, 0, sizeof(*dsc11));
97 dsc11->BufferType = type;
98 dsc11->DataSize = size;
99 dsc11->NumMBsInBuffer = mb_count;
103 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
104 DXVA2_DecodeBufferDesc *dsc2 = dsc;
105 memset(dsc2, 0, sizeof(*dsc2));
106 dsc2->CompressedBufferType = type;
107 dsc2->DataSize = size;
108 dsc2->NumMBsInBuffer = mb_count;
114 av_log(avctx, AV_LOG_ERROR, "Buffer for type %u was too small\n", type);
119 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
120 hr = ID3D11VideoContext_ReleaseDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder, type);
123 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
124 hr = IDirectXVideoDecoder_ReleaseBuffer(DXVA2_CONTEXT(ctx)->decoder, type);
127 av_log(avctx, AV_LOG_ERROR,
128 "Failed to release buffer type %u: 0x%lx\n",
135 int ff_dxva2_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
136 const void *pp, unsigned pp_size,
137 const void *qm, unsigned qm_size,
138 int (*commit_bs_si)(AVCodecContext *,
139 DECODER_BUFFER_DESC *bs,
140 DECODER_BUFFER_DESC *slice))
142 AVDXVAContext *ctx = avctx->hwaccel_context;
143 unsigned buffer_count = 0;
145 D3D11_VIDEO_DECODER_BUFFER_DESC buffer11[4];
148 DXVA2_DecodeBufferDesc buffer2[4];
150 DECODER_BUFFER_DESC *buffer = NULL, *buffer_slice = NULL;
151 int result, runs = 0;
157 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
158 if (D3D11VA_CONTEXT(ctx)->context_mutex != INVALID_HANDLE_VALUE)
159 WaitForSingleObjectEx(D3D11VA_CONTEXT(ctx)->context_mutex, INFINITE, FALSE);
160 hr = ID3D11VideoContext_DecoderBeginFrame(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder,
166 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
167 hr = IDirectXVideoDecoder_BeginFrame(DXVA2_CONTEXT(ctx)->decoder,
171 if (hr != E_PENDING || ++runs > 50)
174 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
175 if (D3D11VA_CONTEXT(ctx)->context_mutex != INVALID_HANDLE_VALUE)
176 ReleaseMutex(D3D11VA_CONTEXT(ctx)->context_mutex);
182 av_log(avctx, AV_LOG_ERROR, "Failed to begin frame: 0x%lx\n", hr);
184 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
185 if (D3D11VA_CONTEXT(ctx)->context_mutex != INVALID_HANDLE_VALUE)
186 ReleaseMutex(D3D11VA_CONTEXT(ctx)->context_mutex);
192 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
193 buffer = &buffer11[buffer_count];
194 type = D3D11_VIDEO_DECODER_BUFFER_PICTURE_PARAMETERS;
198 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
199 buffer = &buffer2[buffer_count];
200 type = DXVA2_PictureParametersBufferType;
203 result = ff_dxva2_commit_buffer(avctx, ctx, buffer,
207 av_log(avctx, AV_LOG_ERROR,
208 "Failed to add picture parameter buffer\n");
215 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
216 buffer = &buffer11[buffer_count];
217 type = D3D11_VIDEO_DECODER_BUFFER_INVERSE_QUANTIZATION_MATRIX;
221 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
222 buffer = &buffer2[buffer_count];
223 type = DXVA2_InverseQuantizationMatrixBufferType;
226 result = ff_dxva2_commit_buffer(avctx, ctx, buffer,
230 av_log(avctx, AV_LOG_ERROR,
231 "Failed to add inverse quantization matrix buffer\n");
238 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
239 buffer = &buffer11[buffer_count + 0];
240 buffer_slice = &buffer11[buffer_count + 1];
244 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
245 buffer = &buffer2[buffer_count + 0];
246 buffer_slice = &buffer2[buffer_count + 1];
250 result = commit_bs_si(avctx,
254 av_log(avctx, AV_LOG_ERROR,
255 "Failed to add bitstream or slice control buffer\n");
260 /* TODO Film Grain when possible */
262 assert(buffer_count == 1 + (qm_size > 0) + 2);
265 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
266 hr = ID3D11VideoContext_SubmitDecoderBuffers(D3D11VA_CONTEXT(ctx)->video_context,
267 D3D11VA_CONTEXT(ctx)->decoder,
268 buffer_count, buffer11);
271 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
272 DXVA2_DecodeExecuteParams exec = {
273 .NumCompBuffers = buffer_count,
274 .pCompressedBuffers = buffer2,
275 .pExtensionData = NULL,
277 hr = IDirectXVideoDecoder_Execute(DXVA2_CONTEXT(ctx)->decoder, &exec);
281 av_log(avctx, AV_LOG_ERROR, "Failed to execute: 0x%lx\n", hr);
287 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
288 hr = ID3D11VideoContext_DecoderEndFrame(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder);
289 if (D3D11VA_CONTEXT(ctx)->context_mutex != INVALID_HANDLE_VALUE)
290 ReleaseMutex(D3D11VA_CONTEXT(ctx)->context_mutex);
294 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
295 hr = IDirectXVideoDecoder_EndFrame(DXVA2_CONTEXT(ctx)->decoder, NULL);
298 av_log(avctx, AV_LOG_ERROR, "Failed to end frame: 0x%lx\n", hr);