d3d11va: Use the proper decoding slice index
[libav.git] / libavcodec / dxva2.c
CommitLineData
85167c46
LA
1/*
2 * DXVA2 HW acceleration.
3 *
4 * copyright (c) 2010 Laurent Aimar
5 *
2912e87a 6 * This file is part of Libav.
85167c46 7 *
2912e87a 8 * Libav is free software; you can redistribute it and/or
85167c46
LA
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.
12 *
2912e87a 13 * Libav is distributed in the hope that it will be useful,
85167c46
LA
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.
17 *
18 * You should have received a copy of the GNU Lesser General Public
2912e87a 19 * License along with Libav; if not, write to the Free Software
85167c46
LA
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
766df7ca
DB
23#include <assert.h>
24#include <string.h>
25
26#include "libavutil/log.h"
27#include "libavutil/time.h"
28
29#include "avcodec.h"
85167c46
LA
30#include "dxva2_internal.h"
31
678e455f 32void *ff_dxva2_get_surface(const AVFrame *frame)
85167c46 33{
678e455f 34 return frame->data[3];
85167c46
LA
35}
36
d8039ef8
SL
37unsigned ff_dxva2_get_surface_index(const AVCodecContext *avctx,
38 const AVDXVAContext *ctx,
678e455f 39 const AVFrame *frame)
85167c46 40{
678e455f 41 void *surface = ff_dxva2_get_surface(frame);
85167c46
LA
42 unsigned i;
43
d8039ef8 44 for (i = 0; i < DXVA_CONTEXT_COUNT(avctx, ctx); i++)
be630b1e
SL
45#if CONFIG_D3D11VA
46 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD && ctx->d3d11va.surface[i] == surface) {
47 D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc;
48 ID3D11VideoDecoderOutputView_GetDesc(ctx->d3d11va.surface[i], &viewDesc);
49 return viewDesc.Texture2D.ArraySlice;
50 }
51#endif
52#if CONFIG_DXVA2
53 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD && ctx->dxva2.surface[i] == surface)
85167c46 54 return i;
be630b1e 55#endif
85167c46
LA
56
57 assert(0);
58 return 0;
59}
60
61int ff_dxva2_commit_buffer(AVCodecContext *avctx,
d8039ef8
SL
62 AVDXVAContext *ctx,
63 DECODER_BUFFER_DESC *dsc,
85167c46
LA
64 unsigned type, const void *data, unsigned size,
65 unsigned mb_count)
66{
67 void *dxva_data;
68 unsigned dxva_size;
69 int result;
9d80b1ae 70 HRESULT hr;
85167c46 71
d8039ef8
SL
72#if CONFIG_D3D11VA
73 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
74 hr = ID3D11VideoContext_GetDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context,
75 D3D11VA_CONTEXT(ctx)->decoder,
76 type,
77 &dxva_size, &dxva_data);
78#endif
79#if CONFIG_DXVA2
80 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
81 hr = IDirectXVideoDecoder_GetBuffer(DXVA2_CONTEXT(ctx)->decoder, type,
82 &dxva_data, &dxva_size);
83#endif
9d80b1ae 84 if (FAILED(hr)) {
ade4ecb4 85 av_log(avctx, AV_LOG_ERROR, "Failed to get a buffer for %u: 0x%lx\n",
9d80b1ae 86 type, hr);
85167c46
LA
87 return -1;
88 }
89 if (size <= dxva_size) {
90 memcpy(dxva_data, data, size);
91
d8039ef8
SL
92#if CONFIG_D3D11VA
93 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
94 D3D11_VIDEO_DECODER_BUFFER_DESC *dsc11 = dsc;
95 memset(dsc11, 0, sizeof(*dsc11));
96 dsc11->BufferType = type;
97 dsc11->DataSize = size;
98 dsc11->NumMBsInBuffer = mb_count;
99 }
100#endif
101#if CONFIG_DXVA2
102 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
103 DXVA2_DecodeBufferDesc *dsc2 = dsc;
104 memset(dsc2, 0, sizeof(*dsc2));
105 dsc2->CompressedBufferType = type;
106 dsc2->DataSize = size;
107 dsc2->NumMBsInBuffer = mb_count;
108 }
109#endif
85167c46
LA
110
111 result = 0;
112 } else {
ade4ecb4 113 av_log(avctx, AV_LOG_ERROR, "Buffer for type %u was too small\n", type);
85167c46
LA
114 result = -1;
115 }
9d80b1ae 116
d8039ef8
SL
117#if CONFIG_D3D11VA
118 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
119 hr = ID3D11VideoContext_ReleaseDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder, type);
120#endif
121#if CONFIG_DXVA2
122 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
123 hr = IDirectXVideoDecoder_ReleaseBuffer(DXVA2_CONTEXT(ctx)->decoder, type);
124#endif
9d80b1ae
SL
125 if (FAILED(hr)) {
126 av_log(avctx, AV_LOG_ERROR,
ade4ecb4 127 "Failed to release buffer type %u: 0x%lx\n",
9d80b1ae 128 type, hr);
85167c46
LA
129 result = -1;
130 }
131 return result;
132}
133
678e455f 134int ff_dxva2_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
85167c46
LA
135 const void *pp, unsigned pp_size,
136 const void *qm, unsigned qm_size,
137 int (*commit_bs_si)(AVCodecContext *,
d8039ef8
SL
138 DECODER_BUFFER_DESC *bs,
139 DECODER_BUFFER_DESC *slice))
85167c46 140{
d8039ef8 141 AVDXVAContext *ctx = avctx->hwaccel_context;
85167c46 142 unsigned buffer_count = 0;
172b2cee 143#if CONFIG_D3D11VA
d8039ef8 144 D3D11_VIDEO_DECODER_BUFFER_DESC buffer11[4];
172b2cee
MS
145#endif
146#if CONFIG_DXVA2
d8039ef8 147 DXVA2_DecodeBufferDesc buffer2[4];
172b2cee 148#endif
d8039ef8 149 DECODER_BUFFER_DESC *buffer,*buffer_slice;
5b2b23f2
SL
150 int result, runs = 0;
151 HRESULT hr;
d8039ef8 152 unsigned type;
5b2b23f2
SL
153
154 do {
d8039ef8 155#if CONFIG_D3D11VA
e3d4784e
SL
156 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
157 if (D3D11VA_CONTEXT(ctx)->context_mutex != INVALID_HANDLE_VALUE)
158 WaitForSingleObjectEx(D3D11VA_CONTEXT(ctx)->context_mutex, INFINITE, FALSE);
d8039ef8
SL
159 hr = ID3D11VideoContext_DecoderBeginFrame(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder,
160 ff_dxva2_get_surface(frame),
161 0, NULL);
e3d4784e 162 }
d8039ef8
SL
163#endif
164#if CONFIG_DXVA2
165 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
166 hr = IDirectXVideoDecoder_BeginFrame(DXVA2_CONTEXT(ctx)->decoder,
167 ff_dxva2_get_surface(frame),
168 NULL);
169#endif
99cf9433
SL
170 if (hr != E_PENDING || ++runs > 50)
171 break;
172#if CONFIG_D3D11VA
173 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
174 if (D3D11VA_CONTEXT(ctx)->context_mutex != INVALID_HANDLE_VALUE)
175 ReleaseMutex(D3D11VA_CONTEXT(ctx)->context_mutex);
176#endif
177 av_usleep(2000);
178 } while(1);
85167c46 179
5b2b23f2 180 if (FAILED(hr)) {
ade4ecb4 181 av_log(avctx, AV_LOG_ERROR, "Failed to begin frame: 0x%lx\n", hr);
e3d4784e
SL
182#if CONFIG_D3D11VA
183 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
184 if (D3D11VA_CONTEXT(ctx)->context_mutex != INVALID_HANDLE_VALUE)
185 ReleaseMutex(D3D11VA_CONTEXT(ctx)->context_mutex);
186#endif
85167c46
LA
187 return -1;
188 }
189
d8039ef8
SL
190#if CONFIG_D3D11VA
191 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
192 buffer = &buffer11[buffer_count];
193 type = D3D11_VIDEO_DECODER_BUFFER_PICTURE_PARAMETERS;
194 }
195#endif
196#if CONFIG_DXVA2
197 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
198 buffer = &buffer2[buffer_count];
199 type = DXVA2_PictureParametersBufferType;
200 }
201#endif
202 result = ff_dxva2_commit_buffer(avctx, ctx, buffer,
203 type,
85167c46
LA
204 pp, pp_size, 0);
205 if (result) {
206 av_log(avctx, AV_LOG_ERROR,
207 "Failed to add picture parameter buffer\n");
208 goto end;
209 }
210 buffer_count++;
211
212 if (qm_size > 0) {
d8039ef8
SL
213#if CONFIG_D3D11VA
214 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
215 buffer = &buffer11[buffer_count];
216 type = D3D11_VIDEO_DECODER_BUFFER_INVERSE_QUANTIZATION_MATRIX;
217 }
218#endif
219#if CONFIG_DXVA2
220 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
221 buffer = &buffer2[buffer_count];
222 type = DXVA2_InverseQuantizationMatrixBufferType;
223 }
224#endif
225 result = ff_dxva2_commit_buffer(avctx, ctx, buffer,
226 type,
85167c46
LA
227 qm, qm_size, 0);
228 if (result) {
229 av_log(avctx, AV_LOG_ERROR,
230 "Failed to add inverse quantization matrix buffer\n");
231 goto end;
232 }
233 buffer_count++;
234 }
235
d8039ef8
SL
236#if CONFIG_D3D11VA
237 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
238 buffer = &buffer11[buffer_count + 0];
239 buffer_slice = &buffer11[buffer_count + 1];
240 }
241#endif
242#if CONFIG_DXVA2
243 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
244 buffer = &buffer2[buffer_count + 0];
245 buffer_slice = &buffer2[buffer_count + 1];
246 }
247#endif
248
85167c46 249 result = commit_bs_si(avctx,
d8039ef8
SL
250 buffer,
251 buffer_slice);
85167c46
LA
252 if (result) {
253 av_log(avctx, AV_LOG_ERROR,
254 "Failed to add bitstream or slice control buffer\n");
255 goto end;
256 }
257 buffer_count += 2;
258
259 /* TODO Film Grain when possible */
260
261 assert(buffer_count == 1 + (qm_size > 0) + 2);
262
d8039ef8
SL
263#if CONFIG_D3D11VA
264 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
265 hr = ID3D11VideoContext_SubmitDecoderBuffers(D3D11VA_CONTEXT(ctx)->video_context,
266 D3D11VA_CONTEXT(ctx)->decoder,
267 buffer_count, buffer11);
268#endif
269#if CONFIG_DXVA2
270 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
271 DXVA2_DecodeExecuteParams exec = {
272 .NumCompBuffers = buffer_count,
273 .pCompressedBuffers = buffer2,
274 .pExtensionData = NULL,
275 };
276 hr = IDirectXVideoDecoder_Execute(DXVA2_CONTEXT(ctx)->decoder, &exec);
277 }
278#endif
9d80b1ae 279 if (FAILED(hr)) {
ade4ecb4 280 av_log(avctx, AV_LOG_ERROR, "Failed to execute: 0x%lx\n", hr);
85167c46
LA
281 result = -1;
282 }
283
284end:
d8039ef8 285#if CONFIG_D3D11VA
e3d4784e 286 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
d8039ef8 287 hr = ID3D11VideoContext_DecoderEndFrame(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder);
e3d4784e
SL
288 if (D3D11VA_CONTEXT(ctx)->context_mutex != INVALID_HANDLE_VALUE)
289 ReleaseMutex(D3D11VA_CONTEXT(ctx)->context_mutex);
290 }
d8039ef8
SL
291#endif
292#if CONFIG_DXVA2
293 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
294 hr = IDirectXVideoDecoder_EndFrame(DXVA2_CONTEXT(ctx)->decoder, NULL);
295#endif
9d80b1ae 296 if (FAILED(hr)) {
ade4ecb4 297 av_log(avctx, AV_LOG_ERROR, "Failed to end frame: 0x%lx\n", hr);
85167c46
LA
298 result = -1;
299 }
300
85167c46
LA
301 return result;
302}