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),
161 if (hr != E_PENDING || ++runs > 50)
164 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
165 if (D3D11VA_CONTEXT(ctx)->context_mutex != INVALID_HANDLE_VALUE)
166 ReleaseMutex(D3D11VA_CONTEXT(ctx)->context_mutex);
172 av_log(avctx, AV_LOG_ERROR, "Failed to begin frame: 0x%lx\n", hr);
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 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
183 buffer = &buffer11[buffer_count];
184 type = D3D11_VIDEO_DECODER_BUFFER_PICTURE_PARAMETERS;
188 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
189 buffer = &buffer2[buffer_count];
190 type = DXVA2_PictureParametersBufferType;
193 result = ff_dxva2_commit_buffer(avctx, ctx, buffer,
197 av_log(avctx, AV_LOG_ERROR,
198 "Failed to add picture parameter buffer\n");
205 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
206 buffer = &buffer11[buffer_count];
207 type = D3D11_VIDEO_DECODER_BUFFER_INVERSE_QUANTIZATION_MATRIX;
211 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
212 buffer = &buffer2[buffer_count];
213 type = DXVA2_InverseQuantizationMatrixBufferType;
216 result = ff_dxva2_commit_buffer(avctx, ctx, buffer,
220 av_log(avctx, AV_LOG_ERROR,
221 "Failed to add inverse quantization matrix buffer\n");
228 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
229 buffer = &buffer11[buffer_count + 0];
230 buffer_slice = &buffer11[buffer_count + 1];
234 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
235 buffer = &buffer2[buffer_count + 0];
236 buffer_slice = &buffer2[buffer_count + 1];
240 result = commit_bs_si(avctx,
244 av_log(avctx, AV_LOG_ERROR,
245 "Failed to add bitstream or slice control buffer\n");
250 /* TODO Film Grain when possible */
252 assert(buffer_count == 1 + (qm_size > 0) + 2);
255 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
256 hr = ID3D11VideoContext_SubmitDecoderBuffers(D3D11VA_CONTEXT(ctx)->video_context,
257 D3D11VA_CONTEXT(ctx)->decoder,
258 buffer_count, buffer11);
261 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
262 DXVA2_DecodeExecuteParams exec = {
263 .NumCompBuffers = buffer_count,
264 .pCompressedBuffers = buffer2,
265 .pExtensionData = NULL,
267 hr = IDirectXVideoDecoder_Execute(DXVA2_CONTEXT(ctx)->decoder, &exec);
271 av_log(avctx, AV_LOG_ERROR, "Failed to execute: 0x%lx\n", hr);
277 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
278 hr = ID3D11VideoContext_DecoderEndFrame(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder);
279 if (D3D11VA_CONTEXT(ctx)->context_mutex != INVALID_HANDLE_VALUE)
280 ReleaseMutex(D3D11VA_CONTEXT(ctx)->context_mutex);
284 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
285 hr = IDirectXVideoDecoder_EndFrame(DXVA2_CONTEXT(ctx)->decoder, NULL);
288 av_log(avctx, AV_LOG_ERROR, "Failed to end frame: 0x%lx\n", hr);