FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dxva2.c
Go to the documentation of this file.
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 "mpegvideo.h"
31 #include "dxva2_internal.h"
32 
34 {
35  return frame->data[3];
36 }
37 
39  const AVDXVAContext *ctx,
40  const AVFrame *frame)
41 {
42  void *surface = ff_dxva2_get_surface(frame);
43  unsigned i;
44 
45  for (i = 0; i < DXVA_CONTEXT_COUNT(avctx, ctx); i++)
46  if (DXVA_CONTEXT_SURFACE(avctx, ctx, i) == surface)
47  return i;
48 
49  assert(0);
50  return 0;
51 }
52 
54  AVDXVAContext *ctx,
56  unsigned type, const void *data, unsigned size,
57  unsigned mb_count)
58 {
59  void *dxva_data;
60  unsigned dxva_size;
61  int result;
62  HRESULT hr = 0;
63 
64 #if CONFIG_D3D11VA
65  if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
66  hr = ID3D11VideoContext_GetDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context,
67  D3D11VA_CONTEXT(ctx)->decoder,
68  type,
69  &dxva_size, &dxva_data);
70 #endif
71 #if CONFIG_DXVA2
72  if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
73  hr = IDirectXVideoDecoder_GetBuffer(DXVA2_CONTEXT(ctx)->decoder, type,
74  &dxva_data, &dxva_size);
75 #endif
76  if (FAILED(hr)) {
77  av_log(avctx, AV_LOG_ERROR, "Failed to get a buffer for %u: 0x%lx\n",
78  type, hr);
79  return -1;
80  }
81  if (size <= dxva_size) {
82  memcpy(dxva_data, data, size);
83 
84 #if CONFIG_D3D11VA
85  if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
86  D3D11_VIDEO_DECODER_BUFFER_DESC *dsc11 = dsc;
87  memset(dsc11, 0, sizeof(*dsc11));
88  dsc11->BufferType = type;
89  dsc11->DataSize = size;
90  dsc11->NumMBsInBuffer = mb_count;
91  }
92 #endif
93 #if CONFIG_DXVA2
94  if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
95  DXVA2_DecodeBufferDesc *dsc2 = dsc;
96  memset(dsc2, 0, sizeof(*dsc2));
97  dsc2->CompressedBufferType = type;
98  dsc2->DataSize = size;
99  dsc2->NumMBsInBuffer = mb_count;
100  }
101 #endif
102 
103  result = 0;
104  } else {
105  av_log(avctx, AV_LOG_ERROR, "Buffer for type %u was too small\n", type);
106  result = -1;
107  }
108 
109 #if CONFIG_D3D11VA
110  if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
111  hr = ID3D11VideoContext_ReleaseDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder, type);
112 #endif
113 #if CONFIG_DXVA2
114  if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
115  hr = IDirectXVideoDecoder_ReleaseBuffer(DXVA2_CONTEXT(ctx)->decoder, type);
116 #endif
117  if (FAILED(hr)) {
118  av_log(avctx, AV_LOG_ERROR,
119  "Failed to release buffer type %u: 0x%lx\n",
120  type, hr);
121  result = -1;
122  }
123  return result;
124 }
125 
127  const void *pp, unsigned pp_size,
128  const void *qm, unsigned qm_size,
129  int (*commit_bs_si)(AVCodecContext *,
131  DECODER_BUFFER_DESC *slice))
132 {
133  AVDXVAContext *ctx = avctx->hwaccel_context;
134  unsigned buffer_count = 0;
135 #if CONFIG_D3D11VA
136  D3D11_VIDEO_DECODER_BUFFER_DESC buffer11[4];
137 #endif
138 #if CONFIG_DXVA2
139  DXVA2_DecodeBufferDesc buffer2[4];
140 #endif
141  DECODER_BUFFER_DESC *buffer = NULL, *buffer_slice = NULL;
142  int result, runs = 0;
143  HRESULT hr;
144  unsigned type;
145 
146  do {
147 #if CONFIG_D3D11VA
148  if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
149  hr = ID3D11VideoContext_DecoderBeginFrame(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder,
150  ff_dxva2_get_surface(frame),
151  0, NULL);
152 #endif
153 #if CONFIG_DXVA2
154  if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
155  hr = IDirectXVideoDecoder_BeginFrame(DXVA2_CONTEXT(ctx)->decoder,
156  ff_dxva2_get_surface(frame),
157  NULL);
158 #endif
159  if (hr == E_PENDING)
160  av_usleep(2000);
161  } while (hr == E_PENDING && ++runs < 50);
162 
163  if (FAILED(hr)) {
164  av_log(avctx, AV_LOG_ERROR, "Failed to begin frame: 0x%lx\n", hr);
165  return -1;
166  }
167 
168 #if CONFIG_D3D11VA
169  if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
170  buffer = &buffer11[buffer_count];
171  type = D3D11_VIDEO_DECODER_BUFFER_PICTURE_PARAMETERS;
172  }
173 #endif
174 #if CONFIG_DXVA2
175  if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
176  buffer = &buffer2[buffer_count];
177  type = DXVA2_PictureParametersBufferType;
178  }
179 #endif
180  result = ff_dxva2_commit_buffer(avctx, ctx, buffer,
181  type,
182  pp, pp_size, 0);
183  if (result) {
184  av_log(avctx, AV_LOG_ERROR,
185  "Failed to add picture parameter buffer\n");
186  goto end;
187  }
188  buffer_count++;
189 
190  if (qm_size > 0) {
191 #if CONFIG_D3D11VA
192  if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
193  buffer = &buffer11[buffer_count];
194  type = D3D11_VIDEO_DECODER_BUFFER_INVERSE_QUANTIZATION_MATRIX;
195  }
196 #endif
197 #if CONFIG_DXVA2
198  if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
199  buffer = &buffer2[buffer_count];
200  type = DXVA2_InverseQuantizationMatrixBufferType;
201  }
202 #endif
203  result = ff_dxva2_commit_buffer(avctx, ctx, buffer,
204  type,
205  qm, qm_size, 0);
206  if (result) {
207  av_log(avctx, AV_LOG_ERROR,
208  "Failed to add inverse quantization matrix buffer\n");
209  goto end;
210  }
211  buffer_count++;
212  }
213 
214 #if CONFIG_D3D11VA
215  if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
216  buffer = &buffer11[buffer_count + 0];
217  buffer_slice = &buffer11[buffer_count + 1];
218  }
219 #endif
220 #if CONFIG_DXVA2
221  if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
222  buffer = &buffer2[buffer_count + 0];
223  buffer_slice = &buffer2[buffer_count + 1];
224  }
225 #endif
226 
227  result = commit_bs_si(avctx,
228  buffer,
229  buffer_slice);
230  if (result) {
231  av_log(avctx, AV_LOG_ERROR,
232  "Failed to add bitstream or slice control buffer\n");
233  goto end;
234  }
235  buffer_count += 2;
236 
237  /* TODO Film Grain when possible */
238 
239  assert(buffer_count == 1 + (qm_size > 0) + 2);
240 
241 #if CONFIG_D3D11VA
242  if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
243  hr = ID3D11VideoContext_SubmitDecoderBuffers(D3D11VA_CONTEXT(ctx)->video_context,
244  D3D11VA_CONTEXT(ctx)->decoder,
245  buffer_count, buffer11);
246 #endif
247 #if CONFIG_DXVA2
248  if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
249  DXVA2_DecodeExecuteParams exec = {
250  .NumCompBuffers = buffer_count,
251  .pCompressedBuffers = buffer2,
252  .pExtensionData = NULL,
253  };
254  hr = IDirectXVideoDecoder_Execute(DXVA2_CONTEXT(ctx)->decoder, &exec);
255  }
256 #endif
257  if (FAILED(hr)) {
258  av_log(avctx, AV_LOG_ERROR, "Failed to execute: 0x%lx\n", hr);
259  result = -1;
260  }
261 
262 end:
263 #if CONFIG_D3D11VA
264  if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
265  hr = ID3D11VideoContext_DecoderEndFrame(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder);
266 #endif
267 #if CONFIG_DXVA2
268  if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
269  hr = IDirectXVideoDecoder_EndFrame(DXVA2_CONTEXT(ctx)->decoder, NULL);
270 #endif
271  if (FAILED(hr)) {
272  av_log(avctx, AV_LOG_ERROR, "Failed to end frame: 0x%lx\n", hr);
273  result = -1;
274  }
275 
276  return result;
277 }
#define NULL
Definition: coverity.c:32
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1722
mpegvideo header.
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition: time.c:76
void * ff_dxva2_get_surface(const AVFrame *frame)
Definition: dxva2.c:33
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:2934
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
int ff_dxva2_common_end_frame(AVCodecContext *avctx, AVFrame *frame, const void *pp, unsigned pp_size, const void *qm, unsigned qm_size, int(*commit_bs_si)(AVCodecContext *, DECODER_BUFFER_DESC *bs, DECODER_BUFFER_DESC *slice))
Definition: dxva2.c:126
static AVFrame * frame
ptrdiff_t size
Definition: opengl_enc.c:101
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
Libavcodec external API header.
static const chunk_decoder decoder[8]
Definition: dfa.c:327
unsigned ff_dxva2_get_surface_index(const AVCodecContext *avctx, const AVDXVAContext *ctx, const AVFrame *frame)
Definition: dxva2.c:38
main external API structure.
Definition: avcodec.h:1502
int ff_dxva2_commit_buffer(AVCodecContext *avctx, AVDXVAContext *ctx, DECODER_BUFFER_DESC *dsc, unsigned type, const void *data, unsigned size, unsigned mb_count)
Definition: dxva2.c:53
GLint GLenum type
Definition: opengl_enc.c:105
void DECODER_BUFFER_DESC
HW decoding through DXVA2, Picture.data[3] contains a LPDIRECT3DSURFACE9 pointer. ...
Definition: pixfmt.h:149
DWORD HRESULT
#define FAILED(hr)
Definition: windows2linux.h:48
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
HW decoding through Direct3D11, Picture.data[3] contains a ID3D11VideoDecoderOutputView pointer...
Definition: pixfmt.h:268
GLuint buffer
Definition: opengl_enc.c:102