Libav
dxva2_vc1.c
Go to the documentation of this file.
1 /*
2  * DXVA2 WMV3/VC-1 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 #include "mpegutils.h"
25 #include "vc1.h"
26 #include "vc1data.h"
27 
28 struct dxva2_picture_context {
29  DXVA_PictureParameters pp;
30  DXVA_SliceInfo si;
31 
32  const uint8_t *bitstream;
33  unsigned bitstream_size;
34 };
35 
37  struct dxva_context *ctx, const VC1Context *v,
38  DXVA_PictureParameters *pp)
39 {
40  const MpegEncContext *s = &v->s;
41  const Picture *current_picture = s->current_picture_ptr;
42 
43  memset(pp, 0, sizeof(*pp));
44  pp->wDecodedPictureIndex =
45  pp->wDeblockedPictureIndex = ff_dxva2_get_surface_index(ctx, current_picture->f);
46  if (s->pict_type != AV_PICTURE_TYPE_I && !v->bi_type)
47  pp->wForwardRefPictureIndex = ff_dxva2_get_surface_index(ctx, s->last_picture.f);
48  else
49  pp->wForwardRefPictureIndex = 0xffff;
50  if (s->pict_type == AV_PICTURE_TYPE_B && !v->bi_type)
51  pp->wBackwardRefPictureIndex = ff_dxva2_get_surface_index(ctx, s->next_picture.f);
52  else
53  pp->wBackwardRefPictureIndex = 0xffff;
54  if (v->profile == PROFILE_ADVANCED) {
55  /* It is the cropped width/height -1 of the frame */
56  pp->wPicWidthInMBminus1 = avctx->width - 1;
57  pp->wPicHeightInMBminus1= avctx->height - 1;
58  } else {
59  /* It is the coded width/height in macroblock -1 of the frame */
60  pp->wPicWidthInMBminus1 = s->mb_width - 1;
61  pp->wPicHeightInMBminus1= s->mb_height - 1;
62  }
63  pp->bMacroblockWidthMinus1 = 15;
64  pp->bMacroblockHeightMinus1 = 15;
65  pp->bBlockWidthMinus1 = 7;
66  pp->bBlockHeightMinus1 = 7;
67  pp->bBPPminus1 = 7;
69  pp->bPicStructure |= 0x01;
71  pp->bPicStructure |= 0x02;
72  pp->bSecondField = v->interlace && v->fcm != ILACE_FIELD && !s->first_field;
73  pp->bPicIntra = s->pict_type == AV_PICTURE_TYPE_I || v->bi_type;
74  pp->bPicBackwardPrediction = s->pict_type == AV_PICTURE_TYPE_B && !v->bi_type;
75  pp->bBidirectionalAveragingMode = (1 << 7) |
76  ((ctx->cfg->ConfigIntraResidUnsigned != 0) << 6) |
77  ((ctx->cfg->ConfigResidDiffAccelerator != 0) << 5) |
78  ((v->lumscale != 32 || v->lumshift != 0) << 4) |
79  ((v->profile == PROFILE_ADVANCED) << 3);
80  pp->bMVprecisionAndChromaRelation = ((v->mv_mode == MV_PMODE_1MV_HPEL_BILIN) << 3) |
81  (1 << 2) |
82  (0 << 1) |
83  (!s->quarter_sample );
84  pp->bChromaFormat = v->chromaformat;
85  ctx->report_id++;
86  if (ctx->report_id >= (1 << 16))
87  ctx->report_id = 1;
88  pp->bPicScanFixed = ctx->report_id >> 8;
89  pp->bPicScanMethod = ctx->report_id & 0xff;
90  pp->bPicReadbackRequests = 0;
91  pp->bRcontrol = v->rnd;
92  pp->bPicSpatialResid8 = (v->panscanflag << 7) |
93  (v->refdist_flag << 6) |
94  (s->loop_filter << 5) |
95  (v->fastuvmc << 4) |
96  (v->extended_mv << 3) |
97  (v->dquant << 1) |
98  (v->vstransform );
99  pp->bPicOverflowBlocks = (v->quantizer_mode << 6) |
100  (v->multires << 5) |
101  (v->resync_marker << 4) |
102  (v->rangered << 3) |
103  (s->max_b_frames );
104  pp->bPicExtrapolation = (!v->interlace || v->fcm == PROGRESSIVE) ? 1 : 2;
105  pp->bPicDeblocked = ((!pp->bPicBackwardPrediction && v->overlap) << 6) |
106  ((v->profile != PROFILE_ADVANCED && v->rangeredfrm) << 5) |
107  (s->loop_filter << 1);
108  pp->bPicDeblockConfined = (v->postprocflag << 7) |
109  (v->broadcast << 6) |
110  (v->interlace << 5) |
111  (v->tfcntrflag << 4) |
112  (v->finterpflag << 3) |
113  ((s->pict_type != AV_PICTURE_TYPE_B) << 2) |
114  (v->psf << 1) |
115  (v->extended_dmv );
116  if (s->pict_type != AV_PICTURE_TYPE_I)
117  pp->bPic4MVallowed = v->mv_mode == MV_PMODE_MIXED_MV ||
120  if (v->profile == PROFILE_ADVANCED)
121  pp->bPicOBMC = (v->range_mapy_flag << 7) |
122  (v->range_mapy << 4) |
123  (v->range_mapuv_flag << 3) |
124  (v->range_mapuv );
125  pp->bPicBinPB = 0;
126  pp->bMV_RPS = 0;
127  pp->bReservedBits = 0;
128  if (s->picture_structure == PICT_FRAME) {
129  pp->wBitstreamFcodes = v->lumscale;
130  pp->wBitstreamPCEelements = v->lumshift;
131  } else {
132  /* Syntax: (top_field_param << 8) | bottom_field_param */
133  pp->wBitstreamFcodes = (v->lumscale << 8) | v->lumscale;
134  pp->wBitstreamPCEelements = (v->lumshift << 8) | v->lumshift;
135  }
136  pp->bBitstreamConcealmentNeed = 0;
137  pp->bBitstreamConcealmentMethod = 0;
138 }
139 
140 static void fill_slice(AVCodecContext *avctx, DXVA_SliceInfo *slice,
141  unsigned position, unsigned size)
142 {
143  const VC1Context *v = avctx->priv_data;
144  const MpegEncContext *s = &v->s;
145 
146  memset(slice, 0, sizeof(*slice));
147  slice->wHorizontalPosition = 0;
148  slice->wVerticalPosition = s->mb_y;
149  slice->dwSliceBitsInBuffer = 8 * size;
150  slice->dwSliceDataLocation = position;
151  slice->bStartCodeBitOffset = 0;
152  slice->bReservedBits = 0;
153  slice->wMBbitOffset = get_bits_count(&s->gb);
154  slice->wNumberMBsInSlice = s->mb_width * s->mb_height; /* XXX We assume 1 slice */
155  slice->wQuantizerScaleCode = v->pq;
156  slice->wBadSliceChopping = 0;
157 }
158 
160  DXVA2_DecodeBufferDesc *bs,
161  DXVA2_DecodeBufferDesc *sc)
162 {
163  const VC1Context *v = avctx->priv_data;
164  struct dxva_context *ctx = avctx->hwaccel_context;
165  const MpegEncContext *s = &v->s;
167 
168  DXVA_SliceInfo *slice = &ctx_pic->si;
169 
170  static const uint8_t start_code[] = { 0, 0, 1, 0x0d };
171  const unsigned start_code_size = avctx->codec_id == AV_CODEC_ID_VC1 ? sizeof(start_code) : 0;
172  const unsigned slice_size = slice->dwSliceBitsInBuffer / 8;
173  const unsigned padding = 128 - ((start_code_size + slice_size) & 127);
174  const unsigned data_size = start_code_size + slice_size + padding;
175 
176  uint8_t *dxva_data;
177  unsigned dxva_size;
178  int result;
179 
180  if (FAILED(IDirectXVideoDecoder_GetBuffer(ctx->decoder,
181  DXVA2_BitStreamDateBufferType,
182  &dxva_data, &dxva_size)))
183  return -1;
184 
185  result = data_size <= dxva_size ? 0 : -1;
186  if (!result) {
187  if (start_code_size > 0)
188  memcpy(dxva_data, start_code, start_code_size);
189  memcpy(dxva_data + start_code_size,
190  ctx_pic->bitstream + slice->dwSliceDataLocation, slice_size);
191  if (padding > 0)
192  memset(dxva_data + start_code_size + slice_size, 0, padding);
193  slice->dwSliceBitsInBuffer = 8 * data_size;
194  }
195  if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(ctx->decoder,
196  DXVA2_BitStreamDateBufferType)))
197  return -1;
198  if (result)
199  return result;
200 
201  memset(bs, 0, sizeof(*bs));
202  bs->CompressedBufferType = DXVA2_BitStreamDateBufferType;
203  bs->DataSize = data_size;
204  bs->NumMBsInBuffer = s->mb_width * s->mb_height;
205  assert((bs->DataSize & 127) == 0);
206 
207  return ff_dxva2_commit_buffer(avctx, ctx, sc,
208  DXVA2_SliceControlBufferType,
209  slice, sizeof(*slice), bs->NumMBsInBuffer);
210 }
211 
213  av_unused const uint8_t *buffer,
214  av_unused uint32_t size)
215 {
216  const VC1Context *v = avctx->priv_data;
217  struct dxva_context *ctx = avctx->hwaccel_context;
219 
220  if (!ctx->decoder || !ctx->cfg || ctx->surface_count <= 0)
221  return -1;
222  assert(ctx_pic);
223 
224  fill_picture_parameters(avctx, ctx, v, &ctx_pic->pp);
225 
226  ctx_pic->bitstream_size = 0;
227  ctx_pic->bitstream = NULL;
228  return 0;
229 }
230 
232  const uint8_t *buffer,
233  uint32_t size)
234 {
235  const VC1Context *v = avctx->priv_data;
236  const Picture *current_picture = v->s.current_picture_ptr;
237  struct dxva2_picture_context *ctx_pic = current_picture->hwaccel_picture_private;
238 
239  if (ctx_pic->bitstream_size > 0)
240  return -1;
241 
242  if (avctx->codec_id == AV_CODEC_ID_VC1 &&
243  size >= 4 && IS_MARKER(AV_RB32(buffer))) {
244  buffer += 4;
245  size -= 4;
246  }
247 
248  ctx_pic->bitstream_size = size;
249  ctx_pic->bitstream = buffer;
250 
251  fill_slice(avctx, &ctx_pic->si, 0, size);
252  return 0;
253 }
254 
256 {
257  VC1Context *v = avctx->priv_data;
259  int ret;
260 
261  if (ctx_pic->bitstream_size <= 0)
262  return -1;
263 
265  &ctx_pic->pp, sizeof(ctx_pic->pp),
266  NULL, 0,
268  if (!ret)
269  ff_mpeg_draw_horiz_band(&v->s, 0, avctx->height);
270  return ret;
271 }
272 
273 #if CONFIG_WMV3_DXVA2_HWACCEL
274 AVHWAccel ff_wmv3_dxva2_hwaccel = {
275  .name = "wmv3_dxva2",
276  .type = AVMEDIA_TYPE_VIDEO,
277  .id = AV_CODEC_ID_WMV3,
278  .pix_fmt = AV_PIX_FMT_DXVA2_VLD,
279  .start_frame = dxva2_vc1_start_frame,
280  .decode_slice = dxva2_vc1_decode_slice,
281  .end_frame = dxva2_vc1_end_frame,
282  .frame_priv_data_size = sizeof(struct dxva2_picture_context),
283 };
284 #endif
285 
287  .name = "vc1_dxva2",
288  .type = AVMEDIA_TYPE_VIDEO,
289  .id = AV_CODEC_ID_VC1,
290  .pix_fmt = AV_PIX_FMT_DXVA2_VLD,
291  .start_frame = dxva2_vc1_start_frame,
292  .decode_slice = dxva2_vc1_decode_slice,
293  .end_frame = dxva2_vc1_end_frame,
294  .frame_priv_data_size = sizeof(struct dxva2_picture_context),
295 };
in the bitstream is reported as 00b
Definition: vc1.h:173
The VC1 Context.
Definition: vc1.h:182
int size
DXVA_SliceInfo si
Definition: dxva2_vc1.c:30
int extended_mv
Ext MV in P/B (not in Simple)
Definition: vc1.h:231
int broadcast
TFF/RFF present.
Definition: vc1.h:209
uint8_t rangeredfrm
Frame decoding info for S/M profiles only.
Definition: vc1.h:310
DXVA_PicParams_H264 pp
Definition: dxva2_h264.c:29
int fastuvmc
Rounding of qpel vector to hpel ? (not in Simple)
Definition: vc1.h:230
unsigned surface_count
The number of surface in the surface array.
Definition: dxva2.h:67
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:2361
VC-1 tables.
int bi_type
Definition: vc1.h:389
uint8_t
int panscanflag
NUMPANSCANWIN, TOPLEFT{X,Y}, BOTRIGHT{X,Y} present.
Definition: vc1.h:212
int interlace
Progressive/interlaced (RPTFTM syntax element)
Definition: vc1.h:210
int profile
Sequence header data for all Profiles TODO: choose between ints, uint8_ts and monobit flags...
Definition: vc1.h:227
#define PICT_BOTTOM_FIELD
Definition: mpegutils.h:34
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:194
int refdist_flag
REFDIST syntax element present in II, IP, PI or PP field picture headers.
Definition: vc1.h:213
int mb_height
number of MBs horizontally & vertically
Definition: mpegvideo.h:255
static int dxva2_vc1_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: dxva2_vc1.c:231
int psf
Progressive Segmented Frame.
Definition: vc1.h:220
unsigned ff_dxva2_get_surface_index(const struct dxva_context *ctx, const AVFrame *frame)
Definition: dxva2.c:38
int overlap
overlapped transforms in use
Definition: vc1.h:234
in the bitstream is reported as 11b
Definition: vc1.h:175
#define PICT_TOP_FIELD
Definition: mpegutils.h:33
#define IS_MARKER(state, i, buf, buf_size)
Definition: dca_parser.c:37
int quarter_sample
1->qpel, 0->half pel ME/MC
Definition: mpegvideo.h:514
GetBitContext gb
Definition: mpegvideo.h:558
int resync_marker
could this stream contain resync markers
Definition: vc1.h:404
int postprocflag
Per-frame processing suggestion flag present.
Definition: vc1.h:208
int tfcntrflag
TFCNTR present.
Definition: vc1.h:211
int width
picture width / height.
Definition: avcodec.h:1224
uint8_t mv_mode
Frame decoding info for all profiles.
Definition: vc1.h:241
Picture * current_picture_ptr
pointer to the current picture
Definition: mpegvideo.h:310
Picture.
Definition: mpegvideo.h:99
void * hwaccel_picture_private
hardware accelerator private data
Definition: mpegvideo.h:131
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
uint8_t lumscale
Luma compensation parameters.
Definition: vc1.h:277
const DXVA2_ConfigPictureDecode * cfg
DXVA2 configuration used to create the decoder.
Definition: dxva2.h:62
static char buffer[20]
Definition: seek-test.c:31
uint8_t range_mapuv_flag
Definition: vc1.h:337
const char * name
Name of the hardware accelerated codec.
Definition: avcodec.h:2899
unsigned bitstream_size
Definition: dxva2_h264.c:35
static void fill_slice(AVCodecContext *avctx, DXVA_SliceInfo *slice, unsigned position, unsigned size)
Definition: dxva2_vc1.c:140
unsigned report_id
Private to the Libav AVHWAccel implementation.
Definition: dxva2.h:82
int first_field
is 1 for the first field of a field picture 0 otherwise
Definition: mpegvideo.h:590
int rangered
RANGEREDFRM (range reduction) syntax element present at frame level.
Definition: vc1.h:198
int finterpflag
INTERPFRM present.
Definition: vc1.h:236
NULL
Definition: eval.c:55
int chromaformat
2bits, 2=4:2:0, only defined
Definition: vc1.h:207
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:2444
static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx, DXVA2_DecodeBufferDesc *bs, DXVA2_DecodeBufferDesc *sc)
Definition: dxva2_vc1.c:159
enum AVCodecID codec_id
Definition: avcodec.h:1067
AVHWAccel.
Definition: avcodec.h:2893
int multires
frame-level RESPIC syntax element present
Definition: vc1.h:195
main external API structure.
Definition: avcodec.h:1050
#define av_unused
Definition: attributes.h:86
uint8_t range_mapy
Definition: vc1.h:338
int extended_dmv
Additional extended dmv range at P/B frame-level.
Definition: vc1.h:214
struct AVFrame * f
Definition: mpegvideo.h:100
int quantizer_mode
2bits, quantizer mode used for sequence, see QUANT_*
Definition: vc1.h:235
int max_b_frames
max number of b-frames for encoding
Definition: mpegvideo.h:240
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
Definition: mpegvideo.h:339
int vstransform
variable-size [48]x[48] transform type + info
Definition: vc1.h:233
void * priv_data
Definition: avcodec.h:1092
uint8_t range_mapuv
Definition: vc1.h:339
MpegEncContext s
Definition: vc1.h:183
MpegEncContext.
Definition: mpegvideo.h:204
static int dxva2_vc1_start_frame(AVCodecContext *avctx, av_unused const uint8_t *buffer, av_unused uint32_t size)
Definition: dxva2_vc1.c:212
const uint8_t * bitstream
Definition: dxva2_h264.c:34
uint8_t pq
Definition: vc1.h:246
enum FrameCodingMode fcm
Frame decoding info for Advanced profile.
Definition: vc1.h:316
Picture last_picture
copy of the previous picture structure.
Definition: mpegvideo.h:288
uint8_t lumshift
Definition: vc1.h:278
Bi-dir predicted.
Definition: avutil.h:255
#define AV_RB32(x)
Definition: intreadwrite.h:232
#define PICT_FRAME
Definition: mpegutils.h:35
int picture_structure
Definition: mpegvideo.h:570
int rnd
rounding control
Definition: vc1.h:306
static int dxva2_vc1_end_frame(AVCodecContext *avctx)
Definition: dxva2_vc1.c:255
Picture next_picture
copy of the next picture structure.
Definition: mpegvideo.h:294
uint8_t range_mapy_flag
Definition: vc1.h:336
AVHWAccel ff_vc1_dxva2_hwaccel
Definition: dxva2_vc1.c:286
int dquant
How qscale varies with MBs, 2bits (not in Simple)
Definition: vc1.h:232
uint8_t mv_mode2
Secondary MV coding mode (B frames)
Definition: vc1.h:242
static void fill_picture_parameters(AVCodecContext *avctx, struct dxva_context *ctx, const VC1Context *v, DXVA_PictureParameters *pp)
Definition: dxva2_vc1.c:36
IDirectXVideoDecoder * decoder
DXVA2 decoder object.
Definition: dxva2.h:57
This structure is used to provides the necessary configurations and data to the DXVA2 Libav HWAccel i...
Definition: dxva2.h:53
HW decoding through DXVA2, Picture.data[3] contains a LPDIRECT3DSURFACE9 pointer. ...
Definition: pixfmt.h:138