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