dxva2: Log errors verbosely
[ffmpeg.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 "dxva2_internal.h"
24
25 void *ff_dxva2_get_surface(const Picture *picture)
26 {
27     return picture->f.data[3];
28 }
29
30 unsigned ff_dxva2_get_surface_index(const struct dxva_context *ctx,
31                                     const Picture *picture)
32 {
33     void *surface = ff_dxva2_get_surface(picture);
34     unsigned i;
35
36     for (i = 0; i < ctx->surface_count; i++)
37         if (ctx->surface[i] == surface)
38             return i;
39
40     assert(0);
41     return 0;
42 }
43
44 int ff_dxva2_commit_buffer(AVCodecContext *avctx,
45                            struct dxva_context *ctx,
46                            DXVA2_DecodeBufferDesc *dsc,
47                            unsigned type, const void *data, unsigned size,
48                            unsigned mb_count)
49 {
50     void     *dxva_data;
51     unsigned dxva_size;
52     int      result;
53     HRESULT hr;
54
55     hr = IDirectXVideoDecoder_GetBuffer(ctx->decoder, type,
56                                         &dxva_data, &dxva_size);
57     if (FAILED(hr)) {
58         av_log(avctx, AV_LOG_ERROR, "Failed to get a buffer for %d: 0x%x\n",
59                type, hr);
60         return -1;
61     }
62     if (size <= dxva_size) {
63         memcpy(dxva_data, data, size);
64
65         memset(dsc, 0, sizeof(*dsc));
66         dsc->CompressedBufferType = type;
67         dsc->DataSize             = size;
68         dsc->NumMBsInBuffer       = mb_count;
69
70         result = 0;
71     } else {
72         av_log(avctx, AV_LOG_ERROR, "Buffer for type %d was too small\n", type);
73         result = -1;
74     }
75
76     hr = IDirectXVideoDecoder_ReleaseBuffer(ctx->decoder, type);
77     if (FAILED(hr)) {
78         av_log(avctx, AV_LOG_ERROR,
79                "Failed to release buffer type %d: 0x%x\n",
80                type, hr);
81         result = -1;
82     }
83     return result;
84 }
85
86 int ff_dxva2_common_end_frame(AVCodecContext *avctx, Picture *pic,
87                               const void *pp, unsigned pp_size,
88                               const void *qm, unsigned qm_size,
89                               int (*commit_bs_si)(AVCodecContext *,
90                                                   DXVA2_DecodeBufferDesc *bs,
91                                                   DXVA2_DecodeBufferDesc *slice))
92 {
93     struct dxva_context *ctx = avctx->hwaccel_context;
94     unsigned               buffer_count = 0;
95     DXVA2_DecodeBufferDesc buffer[4];
96     DXVA2_DecodeExecuteParams exec = { 0 };
97     int result, runs = 0;
98     HRESULT hr;
99
100     do {
101         hr = IDirectXVideoDecoder_BeginFrame(ctx->decoder,
102                                              ff_dxva2_get_surface(pic),
103                                              NULL);
104         if (hr == E_PENDING)
105             av_usleep(2000);
106     } while (hr == E_PENDING && ++runs < 50);
107
108     if (FAILED(hr)) {
109         av_log(avctx, AV_LOG_ERROR, "Failed to begin frame: 0x%x\n", hr);
110         return -1;
111     }
112
113     result = ff_dxva2_commit_buffer(avctx, ctx, &buffer[buffer_count],
114                                     DXVA2_PictureParametersBufferType,
115                                     pp, pp_size, 0);
116     if (result) {
117         av_log(avctx, AV_LOG_ERROR,
118                "Failed to add picture parameter buffer\n");
119         goto end;
120     }
121     buffer_count++;
122
123     if (qm_size > 0) {
124         result = ff_dxva2_commit_buffer(avctx, ctx, &buffer[buffer_count],
125                                         DXVA2_InverseQuantizationMatrixBufferType,
126                                         qm, qm_size, 0);
127         if (result) {
128             av_log(avctx, AV_LOG_ERROR,
129                    "Failed to add inverse quantization matrix buffer\n");
130             goto end;
131         }
132         buffer_count++;
133     }
134
135     result = commit_bs_si(avctx,
136                           &buffer[buffer_count + 0],
137                           &buffer[buffer_count + 1]);
138     if (result) {
139         av_log(avctx, AV_LOG_ERROR,
140                "Failed to add bitstream or slice control buffer\n");
141         goto end;
142     }
143     buffer_count += 2;
144
145     /* TODO Film Grain when possible */
146
147     assert(buffer_count == 1 + (qm_size > 0) + 2);
148
149     exec.NumCompBuffers      = buffer_count;
150     exec.pCompressedBuffers  = buffer;
151     exec.pExtensionData      = NULL;
152     hr = IDirectXVideoDecoder_Execute(ctx->decoder, &exec);
153     if (FAILED(hr)) {
154         av_log(avctx, AV_LOG_ERROR, "Failed to execute: 0x%x\n", hr);
155         result = -1;
156     }
157
158 end:
159     hr = IDirectXVideoDecoder_EndFrame(ctx->decoder, NULL);
160     if (FAILED(hr)) {
161         av_log(avctx, AV_LOG_ERROR, "Failed to end frame: 0x%x\n", hr);
162         result = -1;
163     }
164
165     return result;
166 }