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 "dxva2_internal.h"
24 
25 #define MAX_SLICES (SLICE_MAX_START_CODE - SLICE_MIN_START_CODE + 1)
26 struct dxva2_picture_context {
27  DXVA_PictureParameters pp;
28  DXVA_QmatrixData qm;
29  unsigned slice_count;
30  DXVA_SliceInfo slice[MAX_SLICES];
31 
32  const uint8_t *bitstream;
33  unsigned bitstream_size;
34 };
35 
37  struct dxva_context *ctx,
38  const struct MpegEncContext *s,
39  DXVA_PictureParameters *pp)
40 {
41  const Picture *current_picture = s->current_picture_ptr;
42  int is_field = s->picture_structure != PICT_FRAME;
43 
44  memset(pp, 0, sizeof(*pp));
45  pp->wDecodedPictureIndex = ff_dxva2_get_surface_index(ctx, current_picture);
46  pp->wDeblockedPictureIndex = 0;
47  if (s->pict_type != AV_PICTURE_TYPE_I)
48  pp->wForwardRefPictureIndex = ff_dxva2_get_surface_index(ctx, &s->last_picture);
49  else
50  pp->wForwardRefPictureIndex = 0xffff;
51  if (s->pict_type == AV_PICTURE_TYPE_B)
52  pp->wBackwardRefPictureIndex = ff_dxva2_get_surface_index(ctx, &s->next_picture);
53  else
54  pp->wBackwardRefPictureIndex = 0xffff;
55  pp->wPicWidthInMBminus1 = s->mb_width - 1;
56  pp->wPicHeightInMBminus1 = (s->mb_height >> is_field) - 1;
57  pp->bMacroblockWidthMinus1 = 15;
58  pp->bMacroblockHeightMinus1 = 15;
59  pp->bBlockWidthMinus1 = 7;
60  pp->bBlockHeightMinus1 = 7;
61  pp->bBPPminus1 = 7;
62  pp->bPicStructure = s->picture_structure;
63  pp->bSecondField = is_field && !s->first_field;
64  pp->bPicIntra = s->pict_type == AV_PICTURE_TYPE_I;
65  pp->bPicBackwardPrediction = s->pict_type == AV_PICTURE_TYPE_B;
66  pp->bBidirectionalAveragingMode = 0;
67  pp->bMVprecisionAndChromaRelation= 0; /* FIXME */
68  pp->bChromaFormat = s->chroma_format;
69  pp->bPicScanFixed = 1;
70  pp->bPicScanMethod = s->alternate_scan ? 1 : 0;
71  pp->bPicReadbackRequests = 0;
72  pp->bRcontrol = 0;
73  pp->bPicSpatialResid8 = 0;
74  pp->bPicOverflowBlocks = 0;
75  pp->bPicExtrapolation = 0;
76  pp->bPicDeblocked = 0;
77  pp->bPicDeblockConfined = 0;
78  pp->bPic4MVallowed = 0;
79  pp->bPicOBMC = 0;
80  pp->bPicBinPB = 0;
81  pp->bMV_RPS = 0;
82  pp->bReservedBits = 0;
83  pp->wBitstreamFcodes = (s->mpeg_f_code[0][0] << 12) |
84  (s->mpeg_f_code[0][1] << 8) |
85  (s->mpeg_f_code[1][0] << 4) |
86  (s->mpeg_f_code[1][1] );
87  pp->wBitstreamPCEelements = (s->intra_dc_precision << 14) |
88  (s->picture_structure << 12) |
89  (s->top_field_first << 11) |
90  (s->frame_pred_frame_dct << 10) |
91  (s->concealment_motion_vectors << 9) |
92  (s->q_scale_type << 8) |
93  (s->intra_vlc_format << 7) |
94  (s->alternate_scan << 6) |
95  (s->repeat_first_field << 5) |
96  (s->chroma_420_type << 4) |
97  (s->progressive_frame << 3);
98  pp->bBitstreamConcealmentNeed = 0;
99  pp->bBitstreamConcealmentMethod = 0;
100 }
101 
103  struct dxva_context *ctx,
104  const struct MpegEncContext *s,
105  DXVA_QmatrixData *qm)
106 {
107  int i;
108  for (i = 0; i < 4; i++)
109  qm->bNewQmatrix[i] = 1;
110  for (i = 0; i < 64; i++) {
111  int n = s->dsp.idct_permutation[ff_zigzag_direct[i]];
112  qm->Qmatrix[0][i] = s->intra_matrix[n];;
113  qm->Qmatrix[1][i] = s->inter_matrix[n];;
114  qm->Qmatrix[2][i] = s->chroma_intra_matrix[n];;
115  qm->Qmatrix[3][i] = s->chroma_inter_matrix[n];;
116  }
117 }
118 
119 static void fill_slice(AVCodecContext *avctx,
120  const struct MpegEncContext *s,
121  DXVA_SliceInfo *slice,
122  unsigned position,
123  const uint8_t *buffer, unsigned size)
124 {
125  int is_field = s->picture_structure != PICT_FRAME;
126  GetBitContext gb;
127 
128  memset(slice, 0, sizeof(*slice));
129  slice->wHorizontalPosition = s->mb_x;
130  slice->wVerticalPosition = s->mb_y >> is_field;
131  slice->dwSliceBitsInBuffer = 8 * size;
132  slice->dwSliceDataLocation = position;
133  slice->bStartCodeBitOffset = 0;
134  slice->bReservedBits = 0;
135  /* XXX We store the index of the first MB and it will be fixed later */
136  slice->wNumberMBsInSlice = (s->mb_y >> is_field) * s->mb_width + s->mb_x;
137  slice->wBadSliceChopping = 0;
138 
139  init_get_bits(&gb, &buffer[4], 8 * (size - 4));
140 
141  slice->wQuantizerScaleCode = get_bits(&gb, 5);
142  while (get_bits1(&gb))
143  skip_bits(&gb, 8);
144 
145  slice->wMBbitOffset = 4 * 8 + get_bits_count(&gb);
146 }
148  DXVA2_DecodeBufferDesc *bs,
149  DXVA2_DecodeBufferDesc *sc)
150 {
151  const struct MpegEncContext *s = avctx->priv_data;
152  struct dxva_context *ctx = avctx->hwaccel_context;
153  struct dxva2_picture_context *ctx_pic =
155  const int is_field = s->picture_structure != PICT_FRAME;
156  const unsigned mb_count = s->mb_width * (s->mb_height >> is_field);
157  uint8_t *dxva_data, *current, *end;
158  unsigned dxva_size;
159  unsigned i;
160 
161  if (FAILED(IDirectXVideoDecoder_GetBuffer(ctx->decoder,
162  DXVA2_BitStreamDateBufferType,
163  &dxva_data, &dxva_size)))
164  return -1;
165  current = dxva_data;
166  end = dxva_data + dxva_size;
167 
168  for (i = 0; i < ctx_pic->slice_count; i++) {
169  DXVA_SliceInfo *slice = &ctx_pic->slice[i];
170  unsigned position = slice->dwSliceDataLocation;
171  unsigned size = slice->dwSliceBitsInBuffer / 8;
172  if (size > end - current) {
173  av_log(avctx, AV_LOG_ERROR, "Failed to build bitstream");
174  break;
175  }
176  slice->dwSliceDataLocation = current - dxva_data;
177 
178  if (i < ctx_pic->slice_count - 1)
179  slice->wNumberMBsInSlice =
180  slice[1].wNumberMBsInSlice - slice[0].wNumberMBsInSlice;
181  else
182  slice->wNumberMBsInSlice =
183  mb_count - slice[0].wNumberMBsInSlice;
184 
185  memcpy(current, &ctx_pic->bitstream[position], size);
186  current += size;
187  }
188  if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(ctx->decoder,
189  DXVA2_BitStreamDateBufferType)))
190  return -1;
191  if (i < ctx_pic->slice_count)
192  return -1;
193 
194  memset(bs, 0, sizeof(*bs));
195  bs->CompressedBufferType = DXVA2_BitStreamDateBufferType;
196  bs->DataSize = current - dxva_data;
197  bs->NumMBsInBuffer = mb_count;
198 
199  return ff_dxva2_commit_buffer(avctx, ctx, sc,
200  DXVA2_SliceControlBufferType,
201  ctx_pic->slice,
202  ctx_pic->slice_count * sizeof(*ctx_pic->slice),
203  mb_count);
204 }
205 
206 static int start_frame(AVCodecContext *avctx,
207  av_unused const uint8_t *buffer,
208  av_unused uint32_t size)
209 {
210  const struct MpegEncContext *s = avctx->priv_data;
211  struct dxva_context *ctx = avctx->hwaccel_context;
212  struct dxva2_picture_context *ctx_pic =
214 
215  if (!ctx->decoder || !ctx->cfg || ctx->surface_count <= 0)
216  return -1;
217  assert(ctx_pic);
218 
219  fill_picture_parameters(avctx, ctx, s, &ctx_pic->pp);
220  fill_quantization_matrices(avctx, ctx, s, &ctx_pic->qm);
221 
222  ctx_pic->slice_count = 0;
223  ctx_pic->bitstream_size = 0;
224  ctx_pic->bitstream = NULL;
225  return 0;
226 }
227 
228 static int decode_slice(AVCodecContext *avctx,
229  const uint8_t *buffer, uint32_t size)
230 {
231  const struct MpegEncContext *s = avctx->priv_data;
232  struct dxva2_picture_context *ctx_pic =
234  unsigned position;
235 
236  if (ctx_pic->slice_count >= MAX_SLICES)
237  return -1;
238 
239  if (!ctx_pic->bitstream)
240  ctx_pic->bitstream = buffer;
241  ctx_pic->bitstream_size += size;
242 
243  position = buffer - ctx_pic->bitstream;
244  fill_slice(avctx, s, &ctx_pic->slice[ctx_pic->slice_count++], position,
245  buffer, size);
246  return 0;
247 }
248 
249 static int end_frame(AVCodecContext *avctx)
250 {
251  struct MpegEncContext *s = avctx->priv_data;
252  struct dxva2_picture_context *ctx_pic =
254 
255  if (ctx_pic->slice_count <= 0 || ctx_pic->bitstream_size <= 0)
256  return -1;
257  return ff_dxva2_common_end_frame(avctx, s,
258  &ctx_pic->pp, sizeof(ctx_pic->pp),
259  &ctx_pic->qm, sizeof(ctx_pic->qm),
261 }
262 
264  .name = "mpeg2_dxva2",
265  .type = AVMEDIA_TYPE_VIDEO,
266  .id = CODEC_ID_MPEG2VIDEO,
267  .pix_fmt = PIX_FMT_DXVA2_VLD,
268  .start_frame = start_frame,
269  .decode_slice = decode_slice,
270  .end_frame = end_frame,
271  .priv_data_size = sizeof(struct dxva2_picture_context),
272 };
273 
static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx, DXVA2_DecodeBufferDesc *bs, DXVA2_DecodeBufferDesc *sc)
Definition: dxva2_mpeg2.c:147
const uint8_t ff_zigzag_direct[64]
Definition: dsputil.c:61
int size
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:237
int ff_dxva2_common_end_frame(AVCodecContext *avctx, MpegEncContext *s, const void *pp, unsigned pp_size, const void *qm, unsigned qm_size, int(*commit_bs_si)(AVCodecContext *, DXVA2_DecodeBufferDesc *bs, DXVA2_DecodeBufferDesc *slice))
Definition: dxva2.c:79
uint16_t chroma_intra_matrix[64]
Definition: mpegvideo.h:422
void * hwaccel_picture_private
hardware accelerator private data (Libav-allocated)
Definition: avcodec.h:1230
uint16_t chroma_inter_matrix[64]
Definition: mpegvideo.h:424
DXVA_QmatrixData qm
Definition: dxva2_mpeg2.c:28
DXVA_PictureParameters pp
Definition: dxva2_mpeg2.c:27
const DXVA2_ConfigPictureDecode * cfg
DXVA2 configuration used to create the decoder.
Definition: dxva2.h:48
unsigned surface_count
The number of surface in the surface array.
Definition: dxva2.h:53
DXVA_SliceInfo slice[MAX_SLICES]
Definition: dxva2_mpeg2.c:30
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:2919
#define PICT_FRAME
Definition: mpegvideo.h:618
static void fill_picture_parameters(AVCodecContext *avctx, struct dxva_context *ctx, const struct MpegEncContext *s, DXVA_PictureParameters *pp)
Definition: dxva2_mpeg2.c:36
int intra_dc_precision
Definition: mpegvideo.h:620
int repeat_first_field
Definition: mpegvideo.h:627
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:192
uint8_t idct_permutation[64]
idct input permutation.
Definition: dsputil.h:505
int mb_height
number of MBs horizontally & vertically
Definition: mpegvideo.h:237
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:88
HW decoding through DXVA2, Picture.data[3] contains a LPDIRECT3DSURFACE9 pointer. ...
Definition: pixfmt.h:131
AVHWAccel ff_mpeg2_dxva2_hwaccel
Definition: dxva2_mpeg2.c:263
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
int intra_vlc_format
Definition: mpegvideo.h:625
static int decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: dxva2_mpeg2.c:228
int progressive_frame
Definition: mpegvideo.h:636
int top_field_first
Definition: mpegvideo.h:622
const char * name
Name of the hardware accelerated codec.
Definition: avcodec.h:3282
Picture * current_picture_ptr
pointer to the current picture
Definition: mpegvideo.h:299
Picture.
Definition: mpegvideo.h:85
int alternate_scan
Definition: mpegvideo.h:626
int ff_dxva2_commit_buffer(AVCodecContext *avctx, struct dxva_context *ctx, DXVA2_DecodeBufferDesc *dsc, unsigned type, const void *data, unsigned size, unsigned mb_count)
Definition: dxva2.c:44
static char buffer[20]
Definition: seek-test.c:34
int mpeg_f_code[2][2]
Definition: mpegvideo.h:613
unsigned bitstream_size
Definition: dxva2_h264.c:34
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:119
int first_field
is 1 for the first field of a field picture 0 otherwise
Definition: mpegvideo.h:640
int frame_pred_frame_dct
Definition: mpegvideo.h:621
static int start_frame(AVCodecContext *avctx, av_unused const uint8_t *buffer, av_unused uint32_t size)
Definition: dxva2_mpeg2.c:206
NULL
Definition: eval.c:50
unsigned ff_dxva2_get_surface_index(const struct dxva_context *ctx, const Picture *picture)
Definition: dxva2.c:30
uint16_t inter_matrix[64]
Definition: mpegvideo.h:423
int concealment_motion_vectors
Definition: mpegvideo.h:623
AVHWAccel.
Definition: avcodec.h:3276
main external API structure.
Definition: avcodec.h:1329
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
int chroma_420_type
Definition: mpegvideo.h:628
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:268
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:260
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:354
DSPContext dsp
pointers for accelerated dsp functions
Definition: mpegvideo.h:343
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
Definition: mpegvideo.h:331
MpegEncContext.
Definition: mpegvideo.h:201
const uint8_t * bitstream
Definition: dxva2_h264.c:33
#define MAX_SLICES
Definition: dxva2_mpeg2.c:25
Picture last_picture
copy of the previous picture structure.
Definition: mpegvideo.h:277
Bi-dir predicted.
Definition: avutil.h:298
void * priv_data
Definition: avcodec.h:1531
int picture_structure
Definition: mpegvideo.h:614
static int end_frame(AVCodecContext *avctx)
Definition: dxva2_mpeg2.c:249
Picture next_picture
copy of the next picture structure.
Definition: mpegvideo.h:283
struct AVFrame f
Definition: mpegvideo.h:86
uint16_t intra_matrix[64]
matrix transmitted in the bitstream
Definition: mpegvideo.h:421
IDirectXVideoDecoder * decoder
DXVA2 decoder object.
Definition: dxva2.h:43
static void fill_quantization_matrices(AVCodecContext *avctx, struct dxva_context *ctx, const struct MpegEncContext *s, DXVA_QmatrixData *qm)
Definition: dxva2_mpeg2.c:102
for(j=16;j >0;--j)
#define av_unused
Definition: attributes.h:95
This structure is used to provides the necessary configurations and data to the DXVA2 Libav HWAccel i...
Definition: dxva2.h:39
DXVA_Qmatrix_H264 qm
Definition: dxva2_h264.c:29