Libav
dxva2_mpeg2.c
Go to the documentation of this file.
1 /*
2  * MPEG-2 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 "libavutil/log.h"
24 #include "mpegutils.h"
25 #include "mpegvideo.h"
26 
27 // The headers above may include w32threads.h, which uses the original
28 // _WIN32_WINNT define, while dxva2_internal.h redefines it to target a
29 // potentially newer version.
30 #include "dxva2_internal.h"
31 
32 #define MAX_SLICES 1024
33 struct dxva2_picture_context {
34  DXVA_PictureParameters pp;
35  DXVA_QmatrixData qm;
36  unsigned slice_count;
37  DXVA_SliceInfo slice[MAX_SLICES];
38 
39  const uint8_t *bitstream;
40  unsigned bitstream_size;
41 };
42 
45  const struct MpegEncContext *s,
46  DXVA_PictureParameters *pp)
47 {
48  const Picture *current_picture = s->current_picture_ptr;
49  int is_field = s->picture_structure != PICT_FRAME;
50 
51  memset(pp, 0, sizeof(*pp));
52  pp->wDecodedPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, current_picture->f);
53  pp->wDeblockedPictureIndex = 0;
54  if (s->pict_type != AV_PICTURE_TYPE_I)
55  pp->wForwardRefPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, s->last_picture.f);
56  else
57  pp->wForwardRefPictureIndex = 0xffff;
58  if (s->pict_type == AV_PICTURE_TYPE_B)
59  pp->wBackwardRefPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, s->next_picture.f);
60  else
61  pp->wBackwardRefPictureIndex = 0xffff;
62  pp->wPicWidthInMBminus1 = s->mb_width - 1;
63  pp->wPicHeightInMBminus1 = (s->mb_height >> is_field) - 1;
64  pp->bMacroblockWidthMinus1 = 15;
65  pp->bMacroblockHeightMinus1 = 15;
66  pp->bBlockWidthMinus1 = 7;
67  pp->bBlockHeightMinus1 = 7;
68  pp->bBPPminus1 = 7;
69  pp->bPicStructure = s->picture_structure;
70  pp->bSecondField = is_field && !s->first_field;
71  pp->bPicIntra = s->pict_type == AV_PICTURE_TYPE_I;
72  pp->bPicBackwardPrediction = s->pict_type == AV_PICTURE_TYPE_B;
73  pp->bBidirectionalAveragingMode = 0;
74  pp->bMVprecisionAndChromaRelation= 0; /* FIXME */
75  pp->bChromaFormat = s->chroma_format;
76  pp->bPicScanFixed = 1;
77  pp->bPicScanMethod = s->alternate_scan ? 1 : 0;
78  pp->bPicReadbackRequests = 0;
79  pp->bRcontrol = 0;
80  pp->bPicSpatialResid8 = 0;
81  pp->bPicOverflowBlocks = 0;
82  pp->bPicExtrapolation = 0;
83  pp->bPicDeblocked = 0;
84  pp->bPicDeblockConfined = 0;
85  pp->bPic4MVallowed = 0;
86  pp->bPicOBMC = 0;
87  pp->bPicBinPB = 0;
88  pp->bMV_RPS = 0;
89  pp->bReservedBits = 0;
90  pp->wBitstreamFcodes = (s->mpeg_f_code[0][0] << 12) |
91  (s->mpeg_f_code[0][1] << 8) |
92  (s->mpeg_f_code[1][0] << 4) |
93  (s->mpeg_f_code[1][1] );
94  pp->wBitstreamPCEelements = (s->intra_dc_precision << 14) |
95  (s->picture_structure << 12) |
96  (s->top_field_first << 11) |
97  (s->frame_pred_frame_dct << 10) |
98  (s->concealment_motion_vectors << 9) |
99  (s->q_scale_type << 8) |
100  (s->intra_vlc_format << 7) |
101  (s->alternate_scan << 6) |
102  (s->repeat_first_field << 5) |
103  (s->chroma_420_type << 4) |
104  (s->progressive_frame << 3);
105  pp->bBitstreamConcealmentNeed = 0;
106  pp->bBitstreamConcealmentMethod = 0;
107 }
108 
111  const struct MpegEncContext *s,
112  DXVA_QmatrixData *qm)
113 {
114  int i;
115  for (i = 0; i < 4; i++)
116  qm->bNewQmatrix[i] = 1;
117  for (i = 0; i < 64; i++) {
118  int n = s->idsp.idct_permutation[ff_zigzag_direct[i]];
119  qm->Qmatrix[0][i] = s->intra_matrix[n];;
120  qm->Qmatrix[1][i] = s->inter_matrix[n];;
121  qm->Qmatrix[2][i] = s->chroma_intra_matrix[n];;
122  qm->Qmatrix[3][i] = s->chroma_inter_matrix[n];;
123  }
124 }
125 
126 static void fill_slice(AVCodecContext *avctx,
127  const struct MpegEncContext *s,
128  DXVA_SliceInfo *slice,
129  unsigned position,
130  const uint8_t *buffer, unsigned size)
131 {
132  int is_field = s->picture_structure != PICT_FRAME;
133  GetBitContext gb;
134 
135  memset(slice, 0, sizeof(*slice));
136  slice->wHorizontalPosition = s->mb_x;
137  slice->wVerticalPosition = s->mb_y >> is_field;
138  slice->dwSliceBitsInBuffer = 8 * size;
139  slice->dwSliceDataLocation = position;
140  slice->bStartCodeBitOffset = 0;
141  slice->bReservedBits = 0;
142  /* XXX We store the index of the first MB and it will be fixed later */
143  slice->wNumberMBsInSlice = (s->mb_y >> is_field) * s->mb_width + s->mb_x;
144  slice->wBadSliceChopping = 0;
145 
146  init_get_bits(&gb, &buffer[4], 8 * (size - 4));
147 
148  slice->wQuantizerScaleCode = get_bits(&gb, 5);
149  while (get_bits1(&gb))
150  skip_bits(&gb, 8);
151 
152  slice->wMBbitOffset = 4 * 8 + get_bits_count(&gb);
153 }
157 {
158  const struct MpegEncContext *s = avctx->priv_data;
160  struct dxva2_picture_context *ctx_pic =
162  const int is_field = s->picture_structure != PICT_FRAME;
163  const unsigned mb_count = s->mb_width * (s->mb_height >> is_field);
164  void *dxva_data_ptr;
165  uint8_t *dxva_data, *current, *end;
166  unsigned dxva_size;
167  unsigned i;
168  unsigned type;
169 
170 #if CONFIG_D3D11VA
171  if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
172  type = D3D11_VIDEO_DECODER_BUFFER_BITSTREAM;
173  if (FAILED(ID3D11VideoContext_GetDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context,
174  D3D11VA_CONTEXT(ctx)->decoder,
175  type,
176  &dxva_size, &dxva_data_ptr)))
177  return -1;
178  }
179 #endif
180 #if CONFIG_DXVA2
181  if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
182  type = DXVA2_BitStreamDateBufferType;
183  if (FAILED(IDirectXVideoDecoder_GetBuffer(DXVA2_CONTEXT(ctx)->decoder,
184  type,
185  &dxva_data_ptr, &dxva_size)))
186  return -1;
187  }
188 #endif
189 
190  dxva_data = dxva_data_ptr;
191  current = dxva_data;
192  end = dxva_data + dxva_size;
193 
194  for (i = 0; i < ctx_pic->slice_count; i++) {
195  DXVA_SliceInfo *slice = &ctx_pic->slice[i];
196  unsigned position = slice->dwSliceDataLocation;
197  unsigned size = slice->dwSliceBitsInBuffer / 8;
198  if (size > end - current) {
199  av_log(avctx, AV_LOG_ERROR, "Failed to build bitstream");
200  break;
201  }
202  slice->dwSliceDataLocation = current - dxva_data;
203 
204  if (i < ctx_pic->slice_count - 1)
205  slice->wNumberMBsInSlice =
206  slice[1].wNumberMBsInSlice - slice[0].wNumberMBsInSlice;
207  else
208  slice->wNumberMBsInSlice =
209  mb_count - slice[0].wNumberMBsInSlice;
210 
211  memcpy(current, &ctx_pic->bitstream[position], size);
212  current += size;
213  }
214 #if CONFIG_D3D11VA
215  if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
216  if (FAILED(ID3D11VideoContext_ReleaseDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder, type)))
217  return -1;
218 #endif
219 #if CONFIG_DXVA2
220  if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
221  if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(DXVA2_CONTEXT(ctx)->decoder, type)))
222  return -1;
223 #endif
224  if (i < ctx_pic->slice_count)
225  return -1;
226 
227 #if CONFIG_D3D11VA
228  if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
229  D3D11_VIDEO_DECODER_BUFFER_DESC *dsc11 = bs;
230  memset(dsc11, 0, sizeof(*dsc11));
231  dsc11->BufferType = type;
232  dsc11->DataSize = current - dxva_data;
233  dsc11->NumMBsInBuffer = mb_count;
234 
235  type = D3D11_VIDEO_DECODER_BUFFER_SLICE_CONTROL;
236  }
237 #endif
238 #if CONFIG_DXVA2
239  if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
240  DXVA2_DecodeBufferDesc *dsc2 = bs;
241  memset(dsc2, 0, sizeof(*dsc2));
242  dsc2->CompressedBufferType = type;
243  dsc2->DataSize = current - dxva_data;
244  dsc2->NumMBsInBuffer = mb_count;
245 
246  type = DXVA2_SliceControlBufferType;
247  }
248 #endif
249 
250  return ff_dxva2_commit_buffer(avctx, ctx, sc,
251  type,
252  ctx_pic->slice,
253  ctx_pic->slice_count * sizeof(*ctx_pic->slice),
254  mb_count);
255 }
256 
258  av_unused const uint8_t *buffer,
259  av_unused uint32_t size)
260 {
261  const struct MpegEncContext *s = avctx->priv_data;
263  struct dxva2_picture_context *ctx_pic =
265 
266  if (DXVA_CONTEXT_DECODER(avctx, ctx) == NULL ||
267  DXVA_CONTEXT_CFG(avctx, ctx) == NULL ||
268  DXVA_CONTEXT_COUNT(avctx, ctx) <= 0)
269  return -1;
270  assert(ctx_pic);
271 
272  fill_picture_parameters(avctx, ctx, s, &ctx_pic->pp);
273  fill_quantization_matrices(avctx, ctx, s, &ctx_pic->qm);
274 
275  ctx_pic->slice_count = 0;
276  ctx_pic->bitstream_size = 0;
277  ctx_pic->bitstream = NULL;
278  return 0;
279 }
280 
282  const uint8_t *buffer, uint32_t size)
283 {
284  const struct MpegEncContext *s = avctx->priv_data;
285  struct dxva2_picture_context *ctx_pic =
287  unsigned position;
288 
289  if (ctx_pic->slice_count >= MAX_SLICES) {
290  avpriv_request_sample(avctx, "%d slices in dxva2",
291  ctx_pic->slice_count);
292  return -1;
293  }
294  if (!ctx_pic->bitstream)
295  ctx_pic->bitstream = buffer;
296  ctx_pic->bitstream_size += size;
297 
298  position = buffer - ctx_pic->bitstream;
299  fill_slice(avctx, s, &ctx_pic->slice[ctx_pic->slice_count++], position,
300  buffer, size);
301  return 0;
302 }
303 
305 {
306  struct MpegEncContext *s = avctx->priv_data;
307  struct dxva2_picture_context *ctx_pic =
309  int ret;
310 
311  if (ctx_pic->slice_count <= 0 || ctx_pic->bitstream_size <= 0)
312  return -1;
314  &ctx_pic->pp, sizeof(ctx_pic->pp),
315  &ctx_pic->qm, sizeof(ctx_pic->qm),
317  if (!ret)
318  ff_mpeg_draw_horiz_band(s, 0, avctx->height);
319  return ret;
320 }
321 
322 #if CONFIG_MPEG2_DXVA2_HWACCEL
323 AVHWAccel ff_mpeg2_dxva2_hwaccel = {
324  .name = "mpeg2_dxva2",
325  .type = AVMEDIA_TYPE_VIDEO,
327  .pix_fmt = AV_PIX_FMT_DXVA2_VLD,
328  .start_frame = dxva2_mpeg2_start_frame,
329  .decode_slice = dxva2_mpeg2_decode_slice,
330  .end_frame = dxva2_mpeg2_end_frame,
331  .frame_priv_data_size = sizeof(struct dxva2_picture_context),
332 };
333 #endif
334 
335 #if CONFIG_MPEG2_D3D11VA_HWACCEL
336 AVHWAccel ff_mpeg2_d3d11va_hwaccel = {
337  .name = "mpeg2_d3d11va",
338  .type = AVMEDIA_TYPE_VIDEO,
340  .pix_fmt = AV_PIX_FMT_D3D11VA_VLD,
341  .start_frame = dxva2_mpeg2_start_frame,
342  .decode_slice = dxva2_mpeg2_decode_slice,
343  .end_frame = dxva2_mpeg2_end_frame,
344  .frame_priv_data_size = sizeof(struct dxva2_picture_context),
345 };
346 #endif
IDCTDSPContext idsp
Definition: mpegvideo.h:221
int size
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:228
uint16_t chroma_intra_matrix[64]
Definition: mpegvideo.h:295
uint16_t chroma_inter_matrix[64]
Definition: mpegvideo.h:297
DXVA_QmatrixData qm
Definition: dxva2_mpeg2.c:35
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1621
static int dxva2_mpeg2_start_frame(AVCodecContext *avctx, av_unused const uint8_t *buffer, av_unused uint32_t size)
Definition: dxva2_mpeg2.c:257
mpegvideo header.
DXVA_PictureParameters pp
Definition: dxva2_mpeg2.c:34
static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx, DECODER_BUFFER_DESC *bs, DECODER_BUFFER_DESC *sc)
Definition: dxva2_mpeg2.c:154
DXVA_SliceInfo slice[MAX_SLICES]
Definition: dxva2_mpeg2.c:37
void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
Definition: mpegvideo.c:1737
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
static char buffer[20]
Definition: seek.c:32
uint8_t
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:2708
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:125
int intra_dc_precision
Definition: mpegvideo.h:446
int repeat_first_field
Definition: mpegvideo.h:454
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:182
static int dxva2_mpeg2_end_frame(AVCodecContext *avctx)
Definition: dxva2_mpeg2.c:304
int mb_height
number of MBs horizontally & vertically
Definition: mpegvideo.h:124
static void fill_picture_parameters(AVCodecContext *avctx, AVDXVAContext *ctx, const struct MpegEncContext *s, DXVA_PictureParameters *pp)
Definition: dxva2_mpeg2.c:43
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
static int dxva2_mpeg2_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: dxva2_mpeg2.c:281
int intra_vlc_format
Definition: mpegvideo.h:452
int progressive_frame
Definition: mpegvideo.h:462
int top_field_first
Definition: mpegvideo.h:448
const char * name
Name of the hardware accelerated codec.
Definition: avcodec.h:3244
static const chunk_decoder decoder[8]
Definition: dfa.c:322
Picture * current_picture_ptr
pointer to the current picture
Definition: mpegvideo.h:179
Picture.
Definition: mpegpicture.h:45
int alternate_scan
Definition: mpegvideo.h:453
void * hwaccel_picture_private
Hardware accelerator private data.
Definition: mpegpicture.h:74
AVFormatContext * ctx
Definition: movenc.c:48
uint8_t idct_permutation[64]
IDCT input permutation.
Definition: idctdsp.h:94
int mpeg_f_code[2][2]
Definition: mpegvideo.h:440
static void fill_quantization_matrices(AVCodecContext *avctx, AVDXVAContext *ctx, const struct MpegEncContext *s, DXVA_QmatrixData *qm)
Definition: dxva2_mpeg2.c:109
unsigned bitstream_size
Definition: dxva2_h264.c:40
static void fill_slice(AVCodecContext *avctx, const struct MpegEncContext *s, DXVA_SliceInfo *slice, unsigned position, const uint8_t *buffer, unsigned size)
Definition: dxva2_mpeg2.c:126
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:198
int first_field
is 1 for the first field of a field picture 0 otherwise
Definition: mpegvideo.h:465
int frame_pred_frame_dct
Definition: mpegvideo.h:447
NULL
Definition: eval.c:55
uint16_t inter_matrix[64]
Definition: mpegvideo.h:296
int concealment_motion_vectors
Definition: mpegvideo.h:449
unsigned ff_dxva2_get_surface_index(const AVCodecContext *avctx, const AVDXVAContext *ctx, const AVFrame *frame)
Definition: dxva2.c:37
main external API structure.
Definition: avcodec.h:1409
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:52
int chroma_420_type
Definition: mpegvideo.h:455
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:267
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:259
struct AVFrame * f
Definition: mpegpicture.h:46
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:362
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:115
void DECODER_BUFFER_DESC
HW decoding through DXVA2, Picture.data[3] contains a LPDIRECT3DSURFACE9 pointer. ...
Definition: pixfmt.h:137
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
Definition: mpegvideo.h:206
MpegEncContext.
Definition: mpegvideo.h:76
const uint8_t * bitstream
Definition: dxva2_h264.c:39
#define MAX_SLICES
Definition: dxva2_mpeg2.c:32
Picture last_picture
copy of the previous picture structure.
Definition: mpegvideo.h:157
Bi-dir predicted.
Definition: avutil.h:262
void * priv_data
Definition: avcodec.h:1451
#define PICT_FRAME
Definition: mpegutils.h:39
int picture_structure
Definition: mpegvideo.h:443
Picture next_picture
copy of the next picture structure.
Definition: mpegvideo.h:163
uint16_t intra_matrix[64]
matrix transmitted in the bitstream
Definition: mpegvideo.h:294
HW decoding through Direct3D11, Picture.data[3] contains a ID3D11VideoDecoderOutputView pointer...
Definition: pixfmt.h:222
for(j=16;j >0;--j)
#define av_unused
Definition: attributes.h:86
DXVA_Qmatrix_H264 qm
Definition: dxva2_h264.c:35