FFmpeg  2.6.3
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 "libavutil/log.h"
24 #include "dxva2_internal.h"
25 #include "mpegutils.h"
26 
27 #define MAX_SLICES 1024
28 struct dxva2_picture_context {
29  DXVA_PictureParameters pp;
30  DXVA_QmatrixData qm;
31  unsigned slice_count;
32  DXVA_SliceInfo slice[MAX_SLICES];
33 
34  const uint8_t *bitstream;
35  unsigned bitstream_size;
36 };
37 
39  struct dxva_context *ctx,
40  const struct MpegEncContext *s,
41  DXVA_PictureParameters *pp)
42 {
43  const Picture *current_picture = s->current_picture_ptr;
44  int is_field = s->picture_structure != PICT_FRAME;
45 
46  memset(pp, 0, sizeof(*pp));
47  pp->wDecodedPictureIndex = ff_dxva2_get_surface_index(ctx, current_picture->f);
48  pp->wDeblockedPictureIndex = 0;
49  if (s->pict_type != AV_PICTURE_TYPE_I)
50  pp->wForwardRefPictureIndex = ff_dxva2_get_surface_index(ctx, s->last_picture.f);
51  else
52  pp->wForwardRefPictureIndex = 0xffff;
53  if (s->pict_type == AV_PICTURE_TYPE_B)
54  pp->wBackwardRefPictureIndex = ff_dxva2_get_surface_index(ctx, s->next_picture.f);
55  else
56  pp->wBackwardRefPictureIndex = 0xffff;
57  pp->wPicWidthInMBminus1 = s->mb_width - 1;
58  pp->wPicHeightInMBminus1 = (s->mb_height >> is_field) - 1;
59  pp->bMacroblockWidthMinus1 = 15;
60  pp->bMacroblockHeightMinus1 = 15;
61  pp->bBlockWidthMinus1 = 7;
62  pp->bBlockHeightMinus1 = 7;
63  pp->bBPPminus1 = 7;
64  pp->bPicStructure = s->picture_structure;
65  pp->bSecondField = is_field && !s->first_field;
66  pp->bPicIntra = s->pict_type == AV_PICTURE_TYPE_I;
67  pp->bPicBackwardPrediction = s->pict_type == AV_PICTURE_TYPE_B;
68  pp->bBidirectionalAveragingMode = 0;
69  pp->bMVprecisionAndChromaRelation= 0; /* FIXME */
70  pp->bChromaFormat = s->chroma_format;
71  pp->bPicScanFixed = 1;
72  pp->bPicScanMethod = s->alternate_scan ? 1 : 0;
73  pp->bPicReadbackRequests = 0;
74  pp->bRcontrol = 0;
75  pp->bPicSpatialResid8 = 0;
76  pp->bPicOverflowBlocks = 0;
77  pp->bPicExtrapolation = 0;
78  pp->bPicDeblocked = 0;
79  pp->bPicDeblockConfined = 0;
80  pp->bPic4MVallowed = 0;
81  pp->bPicOBMC = 0;
82  pp->bPicBinPB = 0;
83  pp->bMV_RPS = 0;
84  pp->bReservedBits = 0;
85  pp->wBitstreamFcodes = (s->mpeg_f_code[0][0] << 12) |
86  (s->mpeg_f_code[0][1] << 8) |
87  (s->mpeg_f_code[1][0] << 4) |
88  (s->mpeg_f_code[1][1] );
89  pp->wBitstreamPCEelements = (s->intra_dc_precision << 14) |
90  (s->picture_structure << 12) |
91  (s->top_field_first << 11) |
92  (s->frame_pred_frame_dct << 10) |
93  (s->concealment_motion_vectors << 9) |
94  (s->q_scale_type << 8) |
95  (s->intra_vlc_format << 7) |
96  (s->alternate_scan << 6) |
97  (s->repeat_first_field << 5) |
98  (s->chroma_420_type << 4) |
99  (s->progressive_frame << 3);
100  pp->bBitstreamConcealmentNeed = 0;
101  pp->bBitstreamConcealmentMethod = 0;
102 }
103 
105  struct dxva_context *ctx,
106  const struct MpegEncContext *s,
107  DXVA_QmatrixData *qm)
108 {
109  int i;
110  for (i = 0; i < 4; i++)
111  qm->bNewQmatrix[i] = 1;
112  for (i = 0; i < 64; i++) {
114  qm->Qmatrix[0][i] = s->intra_matrix[n];
115  qm->Qmatrix[1][i] = s->inter_matrix[n];
116  qm->Qmatrix[2][i] = s->chroma_intra_matrix[n];
117  qm->Qmatrix[3][i] = s->chroma_inter_matrix[n];
118  }
119 }
120 
121 static void fill_slice(AVCodecContext *avctx,
122  const struct MpegEncContext *s,
123  DXVA_SliceInfo *slice,
124  unsigned position,
125  const uint8_t *buffer, unsigned size)
126 {
127  int is_field = s->picture_structure != PICT_FRAME;
128  GetBitContext gb;
129 
130  memset(slice, 0, sizeof(*slice));
131  slice->wHorizontalPosition = s->mb_x;
132  slice->wVerticalPosition = s->mb_y >> is_field;
133  slice->dwSliceBitsInBuffer = 8 * size;
134  slice->dwSliceDataLocation = position;
135  slice->bStartCodeBitOffset = 0;
136  slice->bReservedBits = 0;
137  /* XXX We store the index of the first MB and it will be fixed later */
138  slice->wNumberMBsInSlice = (s->mb_y >> is_field) * s->mb_width + s->mb_x;
139  slice->wBadSliceChopping = 0;
140 
141  init_get_bits(&gb, &buffer[4], 8 * (size - 4));
142 
143  slice->wQuantizerScaleCode = get_bits(&gb, 5);
145 
146  slice->wMBbitOffset = 4 * 8 + get_bits_count(&gb);
147 }
149  DXVA2_DecodeBufferDesc *bs,
150  DXVA2_DecodeBufferDesc *sc)
151 {
152  const struct MpegEncContext *s = avctx->priv_data;
153  struct dxva_context *ctx = avctx->hwaccel_context;
154  struct dxva2_picture_context *ctx_pic =
156  const int is_field = s->picture_structure != PICT_FRAME;
157  const unsigned mb_count = s->mb_width * (s->mb_height >> is_field);
158  void *dxva_data_ptr;
159  uint8_t *dxva_data, *current, *end;
160  unsigned dxva_size;
161  unsigned i;
162 
163  if (FAILED(IDirectXVideoDecoder_GetBuffer(ctx->decoder,
164  DXVA2_BitStreamDateBufferType,
165  &dxva_data_ptr, &dxva_size)))
166  return -1;
167 
168  dxva_data = dxva_data_ptr;
169  current = dxva_data;
170  end = dxva_data + dxva_size;
171 
172  for (i = 0; i < ctx_pic->slice_count; i++) {
173  DXVA_SliceInfo *slice = &ctx_pic->slice[i];
174  unsigned position = slice->dwSliceDataLocation;
175  unsigned size = slice->dwSliceBitsInBuffer / 8;
176  if (size > end - current) {
177  av_log(avctx, AV_LOG_ERROR, "Failed to build bitstream");
178  break;
179  }
180  slice->dwSliceDataLocation = current - dxva_data;
181 
182  if (i < ctx_pic->slice_count - 1)
183  slice->wNumberMBsInSlice =
184  slice[1].wNumberMBsInSlice - slice[0].wNumberMBsInSlice;
185  else
186  slice->wNumberMBsInSlice =
187  mb_count - slice[0].wNumberMBsInSlice;
188 
189  memcpy(current, &ctx_pic->bitstream[position], size);
190  current += size;
191  }
192  if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(ctx->decoder,
193  DXVA2_BitStreamDateBufferType)))
194  return -1;
195  if (i < ctx_pic->slice_count)
196  return -1;
197 
198  memset(bs, 0, sizeof(*bs));
199  bs->CompressedBufferType = DXVA2_BitStreamDateBufferType;
200  bs->DataSize = current - dxva_data;
201  bs->NumMBsInBuffer = mb_count;
202 
203  return ff_dxva2_commit_buffer(avctx, ctx, sc,
204  DXVA2_SliceControlBufferType,
205  ctx_pic->slice,
206  ctx_pic->slice_count * sizeof(*ctx_pic->slice),
207  mb_count);
208 }
209 
211  av_unused const uint8_t *buffer,
212  av_unused uint32_t size)
213 {
214  const struct MpegEncContext *s = avctx->priv_data;
215  struct dxva_context *ctx = avctx->hwaccel_context;
216  struct dxva2_picture_context *ctx_pic =
218 
219  if (!ctx->decoder || !ctx->cfg || ctx->surface_count <= 0)
220  return -1;
221  assert(ctx_pic);
222 
223  fill_picture_parameters(avctx, ctx, s, &ctx_pic->pp);
224  fill_quantization_matrices(avctx, ctx, s, &ctx_pic->qm);
225 
226  ctx_pic->slice_count = 0;
227  ctx_pic->bitstream_size = 0;
228  ctx_pic->bitstream = NULL;
229  return 0;
230 }
231 
233  const uint8_t *buffer, uint32_t size)
234 {
235  const struct MpegEncContext *s = avctx->priv_data;
236  struct dxva2_picture_context *ctx_pic =
238  unsigned position;
239 
240  if (ctx_pic->slice_count >= MAX_SLICES) {
241  avpriv_request_sample(avctx, "%d slices in dxva2",
242  ctx_pic->slice_count);
243  return -1;
244  }
245  if (!ctx_pic->bitstream)
246  ctx_pic->bitstream = buffer;
247  ctx_pic->bitstream_size += size;
248 
249  position = buffer - ctx_pic->bitstream;
250  fill_slice(avctx, s, &ctx_pic->slice[ctx_pic->slice_count++], position,
251  buffer, size);
252  return 0;
253 }
254 
256 {
257  struct MpegEncContext *s = avctx->priv_data;
258  struct dxva2_picture_context *ctx_pic =
260  int ret;
261 
262  if (ctx_pic->slice_count <= 0 || ctx_pic->bitstream_size <= 0)
263  return -1;
265  &ctx_pic->pp, sizeof(ctx_pic->pp),
266  &ctx_pic->qm, sizeof(ctx_pic->qm),
268  if (!ret)
269  ff_mpeg_draw_horiz_band(s, 0, avctx->height);
270  return ret;
271 }
272 
274  .name = "mpeg2_dxva2",
275  .type = AVMEDIA_TYPE_VIDEO,
277  .pix_fmt = AV_PIX_FMT_DXVA2_VLD,
278  .start_frame = dxva2_mpeg2_start_frame,
279  .decode_slice = dxva2_mpeg2_decode_slice,
280  .end_frame = dxva2_mpeg2_end_frame,
281  .frame_priv_data_size = sizeof(struct dxva2_picture_context),
282 };
IDCTDSPContext idsp
Definition: mpegvideo.h:366
#define NULL
Definition: coverity.c:32
static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx, DXVA2_DecodeBufferDesc *bs, DXVA2_DecodeBufferDesc *sc)
Definition: dxva2_mpeg2.c:148
const char * s
Definition: avisynth_c.h:669
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:260
uint16_t chroma_intra_matrix[64]
Definition: mpegvideo.h:435
uint16_t chroma_inter_matrix[64]
Definition: mpegvideo.h:437
DXVA_QmatrixData qm
Definition: dxva2_mpeg2.c:30
static int dxva2_mpeg2_start_frame(AVCodecContext *avctx, av_unused const uint8_t *buffer, av_unused uint32_t size)
Definition: dxva2_mpeg2.c:210
DXVA_PictureParameters pp
Definition: dxva2_mpeg2.c:29
const DXVA2_ConfigPictureDecode * cfg
DXVA2 configuration used to create the decoder.
Definition: dxva2.h:66
unsigned surface_count
The number of surface in the surface array.
Definition: dxva2.h:71
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 *, DXVA2_DecodeBufferDesc *bs, DXVA2_DecodeBufferDesc *slice))
Definition: dxva2.c:94
DXVA_SliceInfo slice[MAX_SLICES]
Definition: dxva2_mpeg2.c:32
void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
Definition: mpegvideo.c:3296
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
uint8_t
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:2654
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:67
static void fill_picture_parameters(AVCodecContext *avctx, struct dxva_context *ctx, const struct MpegEncContext *s, DXVA_PictureParameters *pp)
Definition: dxva2_mpeg2.c:38
int intra_dc_precision
Definition: mpegvideo.h:591
int repeat_first_field
Definition: mpegvideo.h:599
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:212
static int dxva2_mpeg2_end_frame(AVCodecContext *avctx)
Definition: dxva2_mpeg2.c:255
int mb_height
number of MBs horizontally & vertically
Definition: mpegvideo.h:263
ptrdiff_t size
Definition: opengl_enc.c:101
#define av_log(a,...)
unsigned ff_dxva2_get_surface_index(const struct dxva_context *ctx, const AVFrame *frame)
Definition: dxva2.c:38
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:175
AVHWAccel ff_mpeg2_dxva2_hwaccel
Definition: dxva2_mpeg2.c:273
static int dxva2_mpeg2_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: dxva2_mpeg2.c:232
int intra_vlc_format
Definition: mpegvideo.h:596
int progressive_frame
Definition: mpegvideo.h:608
int top_field_first
Definition: mpegvideo.h:593
const char * name
Name of the hardware accelerated codec.
Definition: avcodec.h:3281
ret
Definition: avfilter.c:974
Picture * current_picture_ptr
pointer to the current picture
Definition: mpegvideo.h:318
Picture.
Definition: mpegvideo.h:102
int alternate_scan
Definition: mpegvideo.h:597
void * hwaccel_picture_private
hardware accelerator private data
Definition: mpegvideo.h:137
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:52
int n
Definition: avisynth_c.h:589
uint8_t idct_permutation[64]
IDCT input permutation.
Definition: idctdsp.h:94
int mpeg_f_code[2][2]
Definition: mpegvideo.h:586
unsigned bitstream_size
Definition: dxva2_h264.c:35
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:121
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:107
int first_field
is 1 for the first field of a field picture 0 otherwise
Definition: mpegvideo.h:611
int frame_pred_frame_dct
Definition: mpegvideo.h:592
uint16_t inter_matrix[64]
Definition: mpegvideo.h:436
int concealment_motion_vectors
Definition: mpegvideo.h:594
main external API structure.
Definition: avcodec.h:1239
int chroma_420_type
Definition: mpegvideo.h:600
struct AVFrame * f
Definition: mpegvideo.h:103
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:410
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:117
HW decoding through DXVA2, Picture.data[3] contains a LPDIRECT3DSURFACE9 pointer. ...
Definition: pixfmt.h:142
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
Definition: mpegvideo.h:348
#define FAILED(hr)
Definition: windows2linux.h:48
MpegEncContext.
Definition: mpegvideo.h:212
const uint8_t * bitstream
Definition: dxva2_h264.c:34
#define MAX_SLICES
Definition: dxva2_mpeg2.c:27
Picture last_picture
copy of the previous picture structure.
Definition: mpegvideo.h:296
Bi-dir predicted.
Definition: avutil.h:269
void * priv_data
Definition: avcodec.h:1281
#define PICT_FRAME
Definition: mpegutils.h:35
int picture_structure
Definition: mpegvideo.h:589
Picture next_picture
copy of the next picture structure.
Definition: mpegvideo.h:302
uint16_t intra_matrix[64]
matrix transmitted in the bitstream
Definition: mpegvideo.h:434
static int skip_1stop_8data_bits(GetBitContext *gb)
Definition: get_bits.h:593
IDirectXVideoDecoder * decoder
DXVA2 decoder object.
Definition: dxva2.h:61
static void fill_quantization_matrices(AVCodecContext *avctx, struct dxva_context *ctx, const struct MpegEncContext *s, DXVA_QmatrixData *qm)
Definition: dxva2_mpeg2.c:104
for(j=16;j >0;--j)
GLuint buffer
Definition: opengl_enc.c:102
#define av_unused
Definition: attributes.h:118
This structure is used to provides the necessary configurations and data to the DXVA2 FFmpeg HWAccel ...
Definition: dxva2.h:57
DXVA_Qmatrix_H264 qm
Definition: dxva2_h264.c:30