Merge commit 'e3d4784eb31b3ea4a97f2d4c698a75fab9bf3d86'
[ffmpeg.git] / libavcodec / dxva2.c
1 /*
2  * DXVA2 HW acceleration.
3  *
4  * copyright (c) 2010 Laurent Aimar
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
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 (DXVA_CONTEXT_SURFACE(avctx, ctx, i) == surface)
46             return i;
47
48     assert(0);
49     return 0;
50 }
51
52 int ff_dxva2_commit_buffer(AVCodecContext *avctx,
53                            AVDXVAContext *ctx,
54                            DECODER_BUFFER_DESC *dsc,
55                            unsigned type, const void *data, unsigned size,
56                            unsigned mb_count)
57 {
58     void     *dxva_data;
59     unsigned dxva_size;
60     int      result;
61     HRESULT hr = 0;
62
63 #if CONFIG_D3D11VA
64     if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
65         hr = ID3D11VideoContext_GetDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context,
66                                                  D3D11VA_CONTEXT(ctx)->decoder,
67                                                  type,
68                                                  &dxva_size, &dxva_data);
69 #endif
70 #if CONFIG_DXVA2
71     if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
72         hr = IDirectXVideoDecoder_GetBuffer(DXVA2_CONTEXT(ctx)->decoder, type,
73                                             &dxva_data, &dxva_size);
74 #endif
75     if (FAILED(hr)) {
76         av_log(avctx, AV_LOG_ERROR, "Failed to get a buffer for %u: 0x%lx\n",
77                type, hr);
78         return -1;
79     }
80     if (size <= dxva_size) {
81         memcpy(dxva_data, data, size);
82
83 #if CONFIG_D3D11VA
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;
90         }
91 #endif
92 #if CONFIG_DXVA2
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;
99         }
100 #endif
101
102         result = 0;
103     } else {
104         av_log(avctx, AV_LOG_ERROR, "Buffer for type %u was too small\n", type);
105         result = -1;
106     }
107
108 #if CONFIG_D3D11VA
109     if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
110         hr = ID3D11VideoContext_ReleaseDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder, type);
111 #endif
112 #if CONFIG_DXVA2
113     if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
114         hr = IDirectXVideoDecoder_ReleaseBuffer(DXVA2_CONTEXT(ctx)->decoder, type);
115 #endif
116     if (FAILED(hr)) {
117         av_log(avctx, AV_LOG_ERROR,
118                "Failed to release buffer type %u: 0x%lx\n",
119                type, hr);
120         result = -1;
121     }
122     return result;
123 }
124
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))
131 {
132     AVDXVAContext *ctx = avctx->hwaccel_context;
133     unsigned               buffer_count = 0;
134 #if CONFIG_D3D11VA
135     D3D11_VIDEO_DECODER_BUFFER_DESC buffer11[4];
136 #endif
137 #if CONFIG_DXVA2
138     DXVA2_DecodeBufferDesc          buffer2[4];
139 #endif
140     DECODER_BUFFER_DESC             *buffer = NULL, *buffer_slice = NULL;
141     int result, runs = 0;
142     HRESULT hr;
143     unsigned type;
144
145     do {
146 #if CONFIG_D3D11VA
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),
152                                                       0, NULL);
153         }
154 #endif
155 #if CONFIG_DXVA2
156         if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
157             hr = IDirectXVideoDecoder_BeginFrame(DXVA2_CONTEXT(ctx)->decoder,
158                                                  ff_dxva2_get_surface(frame),
159                                                  NULL);
160 #endif
161         if (hr == E_PENDING)
162             av_usleep(2000);
163     } while (hr == E_PENDING && ++runs < 50);
164
165     if (FAILED(hr)) {
166         av_log(avctx, AV_LOG_ERROR, "Failed to begin frame: 0x%lx\n", hr);
167 #if CONFIG_D3D11VA
168         if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
169             if (D3D11VA_CONTEXT(ctx)->context_mutex != INVALID_HANDLE_VALUE)
170                 ReleaseMutex(D3D11VA_CONTEXT(ctx)->context_mutex);
171 #endif
172         return -1;
173     }
174
175 #if CONFIG_D3D11VA
176     if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
177         buffer = &buffer11[buffer_count];
178         type = D3D11_VIDEO_DECODER_BUFFER_PICTURE_PARAMETERS;
179     }
180 #endif
181 #if CONFIG_DXVA2
182     if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
183         buffer = &buffer2[buffer_count];
184         type = DXVA2_PictureParametersBufferType;
185     }
186 #endif
187     result = ff_dxva2_commit_buffer(avctx, ctx, buffer,
188                                     type,
189                                     pp, pp_size, 0);
190     if (result) {
191         av_log(avctx, AV_LOG_ERROR,
192                "Failed to add picture parameter buffer\n");
193         goto end;
194     }
195     buffer_count++;
196
197     if (qm_size > 0) {
198 #if CONFIG_D3D11VA
199         if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
200             buffer = &buffer11[buffer_count];
201             type = D3D11_VIDEO_DECODER_BUFFER_INVERSE_QUANTIZATION_MATRIX;
202         }
203 #endif
204 #if CONFIG_DXVA2
205         if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
206             buffer = &buffer2[buffer_count];
207             type = DXVA2_InverseQuantizationMatrixBufferType;
208         }
209 #endif
210         result = ff_dxva2_commit_buffer(avctx, ctx, buffer,
211                                         type,
212                                         qm, qm_size, 0);
213         if (result) {
214             av_log(avctx, AV_LOG_ERROR,
215                    "Failed to add inverse quantization matrix buffer\n");
216             goto end;
217         }
218         buffer_count++;
219     }
220
221 #if CONFIG_D3D11VA
222     if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
223         buffer       = &buffer11[buffer_count + 0];
224         buffer_slice = &buffer11[buffer_count + 1];
225     }
226 #endif
227 #if CONFIG_DXVA2
228     if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
229         buffer       = &buffer2[buffer_count + 0];
230         buffer_slice = &buffer2[buffer_count + 1];
231     }
232 #endif
233
234     result = commit_bs_si(avctx,
235                           buffer,
236                           buffer_slice);
237     if (result) {
238         av_log(avctx, AV_LOG_ERROR,
239                "Failed to add bitstream or slice control buffer\n");
240         goto end;
241     }
242     buffer_count += 2;
243
244     /* TODO Film Grain when possible */
245
246     assert(buffer_count == 1 + (qm_size > 0) + 2);
247
248 #if CONFIG_D3D11VA
249     if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
250         hr = ID3D11VideoContext_SubmitDecoderBuffers(D3D11VA_CONTEXT(ctx)->video_context,
251                                                      D3D11VA_CONTEXT(ctx)->decoder,
252                                                      buffer_count, buffer11);
253 #endif
254 #if CONFIG_DXVA2
255     if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
256         DXVA2_DecodeExecuteParams exec = {
257             .NumCompBuffers     = buffer_count,
258             .pCompressedBuffers = buffer2,
259             .pExtensionData     = NULL,
260         };
261         hr = IDirectXVideoDecoder_Execute(DXVA2_CONTEXT(ctx)->decoder, &exec);
262     }
263 #endif
264     if (FAILED(hr)) {
265         av_log(avctx, AV_LOG_ERROR, "Failed to execute: 0x%lx\n", hr);
266         result = -1;
267     }
268
269 end:
270 #if CONFIG_D3D11VA
271     if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
272         hr = ID3D11VideoContext_DecoderEndFrame(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder);
273         if (D3D11VA_CONTEXT(ctx)->context_mutex != INVALID_HANDLE_VALUE)
274             ReleaseMutex(D3D11VA_CONTEXT(ctx)->context_mutex);
275     }
276 #endif
277 #if CONFIG_DXVA2
278     if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
279         hr = IDirectXVideoDecoder_EndFrame(DXVA2_CONTEXT(ctx)->decoder, NULL);
280 #endif
281     if (FAILED(hr)) {
282         av_log(avctx, AV_LOG_ERROR, "Failed to end frame: 0x%lx\n", hr);
283         result = -1;
284     }
285
286     return result;
287 }