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 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 "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++) {
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  (void **)&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 
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 
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 
250 {
251  struct MpegEncContext *s = avctx->priv_data;
252  struct dxva2_picture_context *ctx_pic =
254  int ret;
255 
256  if (ctx_pic->slice_count <= 0 || ctx_pic->bitstream_size <= 0)
257  return -1;
259  &ctx_pic->pp, sizeof(ctx_pic->pp),
260  &ctx_pic->qm, sizeof(ctx_pic->qm),
262  if (!ret)
263  ff_mpeg_draw_horiz_band(s, 0, avctx->height);
264  return ret;
265 }
266 
268  .name = "mpeg2_dxva2",
269  .type = AVMEDIA_TYPE_VIDEO,
271  .pix_fmt = AV_PIX_FMT_DXVA2_VLD,
272  .start_frame = dxva2_mpeg2_start_frame,
273  .decode_slice = dxva2_mpeg2_decode_slice,
274  .end_frame = dxva2_mpeg2_end_frame,
275  .priv_data_size = sizeof(struct dxva2_picture_context),
276 };
static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx, DXVA2_DecodeBufferDesc *bs, DXVA2_DecodeBufferDesc *sc)
Definition: dxva2_mpeg2.c:147
const char * s
Definition: avisynth_c.h:668
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
uint16_t chroma_intra_matrix[64]
Definition: mpegvideo.h:473
uint16_t chroma_inter_matrix[64]
Definition: mpegvideo.h:475
DXVA_QmatrixData qm
Definition: dxva2_mpeg2.c:28
static int dxva2_mpeg2_start_frame(AVCodecContext *avctx, av_unused const uint8_t *buffer, av_unused uint32_t size)
Definition: dxva2_mpeg2.c:206
DXVA_PictureParameters pp
Definition: dxva2_mpeg2.c:27
const DXVA2_ConfigPictureDecode * cfg
DXVA2 configuration used to create the decoder.
Definition: dxva2.h:68
unsigned surface_count
The number of surface in the surface array.
Definition: dxva2.h:73
DXVA_SliceInfo slice[MAX_SLICES]
Definition: dxva2_mpeg2.c:30
void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
Definition: mpegvideo.c:2996
uint8_t
void * hwaccel_context
Hardware accelerator context.
#define PICT_FRAME
Definition: mpegvideo.h:664
end end
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:666
int repeat_first_field
Definition: mpegvideo.h:673
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:193
static int dxva2_mpeg2_end_frame(AVCodecContext *avctx)
Definition: dxva2_mpeg2.c:249
uint8_t idct_permutation[64]
idct input permutation.
Definition: dsputil.h:249
int mb_height
number of MBs horizontally & vertically
Definition: mpegvideo.h:277
AVHWAccel ff_mpeg2_dxva2_hwaccel
Definition: dxva2_mpeg2.c:267
static int dxva2_mpeg2_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: dxva2_mpeg2.c:228
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
int size
int intra_vlc_format
Definition: mpegvideo.h:671
int ff_dxva2_common_end_frame(AVCodecContext *avctx, Picture *pic, 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
int progressive_frame
Definition: mpegvideo.h:682
int top_field_first
Definition: mpegvideo.h:668
const char * name
Name of the hardware accelerated codec.
ret
Definition: avfilter.c:821
Picture * current_picture_ptr
pointer to the current picture
Definition: mpegvideo.h:347
Picture.
Definition: mpegvideo.h:97
int alternate_scan
Definition: mpegvideo.h:672
void * hwaccel_picture_private
hardware accelerator private data
Definition: mpegvideo.h:132
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
int mpeg_f_code[2][2]
Definition: mpegvideo.h:659
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
preferred ID for MPEG-1/2 video decoding
int first_field
is 1 for the first field of a field picture 0 otherwise
Definition: mpegvideo.h:686
int frame_pred_frame_dct
Definition: mpegvideo.h:667
NULL
Definition: eval.c:55
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:474
int concealment_motion_vectors
Definition: mpegvideo.h:669
AVHWAccel.
main external API structure.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
int chroma_420_type
Definition: mpegvideo.h:674
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:273
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:265
synthesis window for stochastic i
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:379
DSPContext dsp
pointers for accelerated dsp functions
Definition: mpegvideo.h:391
HW decoding through DXVA2, Picture.data[3] contains a LPDIRECT3DSURFACE9 pointer. ...
Definition: pixfmt.h:135
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
Definition: mpegvideo.h:377
#define FAILED(hr)
Definition: windows2linux.h:48
MpegEncContext.
Definition: mpegvideo.h:241
const uint8_t * bitstream
Definition: dxva2_h264.c:33
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:115
#define MAX_SLICES
Definition: dxva2_mpeg2.c:25
Picture last_picture
copy of the previous picture structure.
Definition: mpegvideo.h:325
Bi-dir predicted.
Definition: avutil.h:218
the buffer and buffer reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFilterBuffer structures They must not be accessed but through references stored in AVFilterBufferRef structures Several references can point to the same buffer
int picture_structure
Definition: mpegvideo.h:660
Picture next_picture
copy of the next picture structure.
Definition: mpegvideo.h:331
uint16_t intra_matrix[64]
matrix transmitted in the bitstream
Definition: mpegvideo.h:472
IDirectXVideoDecoder * decoder
DXVA2 decoder object.
Definition: dxva2.h:63
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:114
This structure is used to provides the necessary configurations and data to the DXVA2 FFmpeg HWAccel ...
Definition: dxva2.h:59
DXVA_Qmatrix_H264 qm
Definition: dxva2_h264.c:29