d3d11va: Use the proper decoding slice index
[libav.git] / libavcodec / dxva2.c
1 /*
2 * DXVA2 HW acceleration.
3 *
4 * copyright (c) 2010 Laurent Aimar
5 *
6 * This file is part of Libav.
7 *
8 * Libav 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.
12 *
13 * Libav 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.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <assert.h>
24 #include <string.h>
25
26 #include "libavutil/log.h"
27 #include "libavutil/time.h"
28
29 #include "avcodec.h"
30 #include "dxva2_internal.h"
31
32 void *ff_dxva2_get_surface(const AVFrame *frame)
33 {
34 return frame->data[3];
35 }
36
37 unsigned ff_dxva2_get_surface_index(const AVCodecContext *avctx,
38 const AVDXVAContext *ctx,
39 const AVFrame *frame)
40 {
41 void *surface = ff_dxva2_get_surface(frame);
42 unsigned i;
43
44 for (i = 0; i < DXVA_CONTEXT_COUNT(avctx, ctx); i++)
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)
54 return i;
55 #endif
56
57 assert(0);
58 return 0;
59 }
60
61 int ff_dxva2_commit_buffer(AVCodecContext *avctx,
62 AVDXVAContext *ctx,
63 DECODER_BUFFER_DESC *dsc,
64 unsigned type, const void *data, unsigned size,
65 unsigned mb_count)
66 {
67 void *dxva_data;
68 unsigned dxva_size;
69 int result;
70 HRESULT hr;
71
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
84 if (FAILED(hr)) {
85 av_log(avctx, AV_LOG_ERROR, "Failed to get a buffer for %u: 0x%lx\n",
86 type, hr);
87 return -1;
88 }
89 if (size <= dxva_size) {
90 memcpy(dxva_data, data, size);
91
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
110
111 result = 0;
112 } else {
113 av_log(avctx, AV_LOG_ERROR, "Buffer for type %u was too small\n", type);
114 result = -1;
115 }
116
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
125 if (FAILED(hr)) {
126 av_log(avctx, AV_LOG_ERROR,
127 "Failed to release buffer type %u: 0x%lx\n",
128 type, hr);
129 result = -1;
130 }
131 return result;
132 }
133
134 int ff_dxva2_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
135 const void *pp, unsigned pp_size,
136 const void *qm, unsigned qm_size,
137 int (*commit_bs_si)(AVCodecContext *,
138 DECODER_BUFFER_DESC *bs,
139 DECODER_BUFFER_DESC *slice))
140 {
141 AVDXVAContext *ctx = avctx->hwaccel_context;
142 unsigned buffer_count = 0;
143 #if CONFIG_D3D11VA
144 D3D11_VIDEO_DECODER_BUFFER_DESC buffer11[4];
145 #endif
146 #if CONFIG_DXVA2
147 DXVA2_DecodeBufferDesc buffer2[4];
148 #endif
149 DECODER_BUFFER_DESC *buffer,*buffer_slice;
150 int result, runs = 0;
151 HRESULT hr;
152 unsigned type;
153
154 do {
155 #if CONFIG_D3D11VA
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);
159 hr = ID3D11VideoContext_DecoderBeginFrame(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder,
160 ff_dxva2_get_surface(frame),
161 0, NULL);
162 }
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
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);
179
180 if (FAILED(hr)) {
181 av_log(avctx, AV_LOG_ERROR, "Failed to begin frame: 0x%lx\n", hr);
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
187 return -1;
188 }
189
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,
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) {
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,
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
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
249 result = commit_bs_si(avctx,
250 buffer,
251 buffer_slice);
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
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
279 if (FAILED(hr)) {
280 av_log(avctx, AV_LOG_ERROR, "Failed to execute: 0x%lx\n", hr);
281 result = -1;
282 }
283
284 end:
285 #if CONFIG_D3D11VA
286 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
287 hr = ID3D11VideoContext_DecoderEndFrame(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder);
288 if (D3D11VA_CONTEXT(ctx)->context_mutex != INVALID_HANDLE_VALUE)
289 ReleaseMutex(D3D11VA_CONTEXT(ctx)->context_mutex);
290 }
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
296 if (FAILED(hr)) {
297 av_log(avctx, AV_LOG_ERROR, "Failed to end frame: 0x%lx\n", hr);
298 result = -1;
299 }
300
301 return result;
302 }