FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 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 #include "vc1.h"
25 #include "vc1data.h"
26 
27 struct dxva2_picture_context {
28  DXVA_PictureParameters pp;
29  DXVA_SliceInfo si;
30 
31  const uint8_t *bitstream;
32  unsigned bitstream_size;
33 };
34 
36  struct dxva_context *ctx, const VC1Context *v,
37  DXVA_PictureParameters *pp)
38 {
39  const MpegEncContext *s = &v->s;
40  const Picture *current_picture = s->current_picture_ptr;
41  int intcomp = 0;
42 
43  // determine if intensity compensation is needed
44  if (s->pict_type == AV_PICTURE_TYPE_P) {
45  if ((v->fcm == ILACE_FRAME && v->intcomp) || (v->fcm != ILACE_FRAME && v->mv_mode == MV_PMODE_INTENSITY_COMP)) {
46  if (v->lumscale != 32 || v->lumshift != 0 || (s->picture_structure != PICT_FRAME && (v->lumscale2 != 32 && v->lumshift2 != 0)))
47  intcomp = 1;
48  }
49  }
50 
51  memset(pp, 0, sizeof(*pp));
52  pp->wDecodedPictureIndex =
53  pp->wDeblockedPictureIndex = ff_dxva2_get_surface_index(ctx, current_picture);
54  if (s->pict_type != AV_PICTURE_TYPE_I && !v->bi_type)
55  pp->wForwardRefPictureIndex = ff_dxva2_get_surface_index(ctx, &s->last_picture);
56  else
57  pp->wForwardRefPictureIndex = 0xffff;
58  if (s->pict_type == AV_PICTURE_TYPE_B && !v->bi_type)
59  pp->wBackwardRefPictureIndex = ff_dxva2_get_surface_index(ctx, &s->next_picture);
60  else
61  pp->wBackwardRefPictureIndex = 0xffff;
62  if (v->profile == PROFILE_ADVANCED) {
63  /* It is the cropped width/height -1 of the frame */
64  pp->wPicWidthInMBminus1 = avctx->width - 1;
65  pp->wPicHeightInMBminus1= avctx->height - 1;
66  } else {
67  /* It is the coded width/height in macroblock -1 of the frame */
68  pp->wPicWidthInMBminus1 = s->mb_width - 1;
69  pp->wPicHeightInMBminus1= s->mb_height - 1;
70  }
71  pp->bMacroblockWidthMinus1 = 15;
72  pp->bMacroblockHeightMinus1 = 15;
73  pp->bBlockWidthMinus1 = 7;
74  pp->bBlockHeightMinus1 = 7;
75  pp->bBPPminus1 = 7;
77  pp->bPicStructure |= 0x01;
79  pp->bPicStructure |= 0x02;
80  pp->bSecondField = v->interlace && v->fcm == ILACE_FIELD && v->second_field;
81  pp->bPicIntra = s->pict_type == AV_PICTURE_TYPE_I || v->bi_type;
82  pp->bPicBackwardPrediction = s->pict_type == AV_PICTURE_TYPE_B && !v->bi_type;
83  pp->bBidirectionalAveragingMode = (1 << 7) |
84  ((ctx->cfg->ConfigIntraResidUnsigned != 0) << 6) |
85  ((ctx->cfg->ConfigResidDiffAccelerator != 0) << 5) |
86  (intcomp << 4) |
87  ((v->profile == PROFILE_ADVANCED) << 3);
88  pp->bMVprecisionAndChromaRelation = ((v->mv_mode == MV_PMODE_1MV_HPEL_BILIN) << 3) |
89  (1 << 2) |
90  (0 << 1) |
91  (!s->quarter_sample );
92  pp->bChromaFormat = v->chromaformat;
93  ctx->report_id++;
94  if (ctx->report_id >= (1 << 16))
95  ctx->report_id = 1;
96  pp->bPicScanFixed = ctx->report_id >> 8;
97  pp->bPicScanMethod = ctx->report_id & 0xff;
98  pp->bPicReadbackRequests = 0;
99  pp->bRcontrol = v->rnd;
100  pp->bPicSpatialResid8 = (v->panscanflag << 7) |
101  (v->refdist_flag << 6) |
102  (s->loop_filter << 5) |
103  (v->fastuvmc << 4) |
104  (v->extended_mv << 3) |
105  (v->dquant << 1) |
106  (v->vstransform );
107  pp->bPicOverflowBlocks = (v->quantizer_mode << 6) |
108  (v->multires << 5) |
109  (v->resync_marker << 4) |
110  (v->rangered << 3) |
111  (s->max_b_frames );
112  pp->bPicExtrapolation = (!v->interlace || v->fcm == PROGRESSIVE) ? 1 : 2;
113  pp->bPicDeblocked = ((!pp->bPicBackwardPrediction && v->overlap) << 6) |
114  ((v->profile != PROFILE_ADVANCED && v->rangeredfrm) << 5) |
115  (s->loop_filter << 1);
116  pp->bPicDeblockConfined = (v->postprocflag << 7) |
117  (v->broadcast << 6) |
118  (v->interlace << 5) |
119  (v->tfcntrflag << 4) |
120  (v->finterpflag << 3) |
121  ((s->pict_type != AV_PICTURE_TYPE_B) << 2) |
122  (v->psf << 1) |
123  (v->extended_dmv );
124  if (s->pict_type != AV_PICTURE_TYPE_I)
125  pp->bPic4MVallowed = v->mv_mode == MV_PMODE_MIXED_MV ||
128  if (v->profile == PROFILE_ADVANCED)
129  pp->bPicOBMC = (v->range_mapy_flag << 7) |
130  (v->range_mapy << 4) |
131  (v->range_mapuv_flag << 3) |
132  (v->range_mapuv );
133  pp->bPicBinPB = 0;
134  pp->bMV_RPS = (v->fcm == ILACE_FIELD && pp->bPicBackwardPrediction) ? v->refdist + 9 : 0;
135  pp->bReservedBits = v->pq;
137  if (intcomp) {
138  pp->wBitstreamFcodes = v->lumscale;
139  pp->wBitstreamPCEelements = v->lumshift;
140  } else {
141  pp->wBitstreamFcodes = 32;
142  pp->wBitstreamPCEelements = 0;
143  }
144  } else {
145  /* Syntax: (top_field_param << 8) | bottom_field_param */
146  if (intcomp) {
147  pp->wBitstreamFcodes = (v->lumscale << 8) | v->lumscale2;
148  pp->wBitstreamPCEelements = (v->lumshift << 8) | v->lumshift2;
149  } else {
150  pp->wBitstreamFcodes = (32 << 8) | 32;
151  pp->wBitstreamPCEelements = 0;
152  }
153  }
154  pp->bBitstreamConcealmentNeed = 0;
155  pp->bBitstreamConcealmentMethod = 0;
156 }
157 
158 static void fill_slice(AVCodecContext *avctx, DXVA_SliceInfo *slice,
159  unsigned position, unsigned size)
160 {
161  const VC1Context *v = avctx->priv_data;
162  const MpegEncContext *s = &v->s;
163 
164  memset(slice, 0, sizeof(*slice));
165  slice->wHorizontalPosition = 0;
166  slice->wVerticalPosition = s->mb_y;
167  slice->dwSliceBitsInBuffer = 8 * size;
168  slice->dwSliceDataLocation = position;
169  slice->bStartCodeBitOffset = 0;
170  slice->bReservedBits = (s->pict_type == AV_PICTURE_TYPE_B && !v->bi_type) ? v->bfraction_lut_index + 9 : 0;
171  slice->wMBbitOffset = v->p_frame_skipped ? 0xffff : get_bits_count(&s->gb);
172  slice->wNumberMBsInSlice = s->mb_width * s->mb_height; /* XXX We assume 1 slice */
173  slice->wQuantizerScaleCode = v->pq;
174  slice->wBadSliceChopping = 0;
175 }
176 
178  DXVA2_DecodeBufferDesc *bs,
179  DXVA2_DecodeBufferDesc *sc)
180 {
181  const VC1Context *v = avctx->priv_data;
182  struct dxva_context *ctx = avctx->hwaccel_context;
183  const MpegEncContext *s = &v->s;
185 
186  DXVA_SliceInfo *slice = &ctx_pic->si;
187 
188  static const uint8_t start_code[] = { 0, 0, 1, 0x0d };
189  const unsigned start_code_size = avctx->codec_id == AV_CODEC_ID_VC1 ? sizeof(start_code) : 0;
190  const unsigned slice_size = slice->dwSliceBitsInBuffer / 8;
191  const unsigned padding = 128 - ((start_code_size + slice_size) & 127);
192  const unsigned data_size = start_code_size + slice_size + padding;
193 
194  uint8_t *dxva_data;
195  unsigned dxva_size;
196  int result;
197 
198  if (FAILED(IDirectXVideoDecoder_GetBuffer(ctx->decoder,
199  DXVA2_BitStreamDateBufferType,
200  (void **)&dxva_data, &dxva_size)))
201  return -1;
202 
203  result = data_size <= dxva_size ? 0 : -1;
204  if (!result) {
205  if (start_code_size > 0) {
206  memcpy(dxva_data, start_code, start_code_size);
207  if (v->second_field)
208  dxva_data[3] = 0x0c;
209  }
210  memcpy(dxva_data + start_code_size,
211  ctx_pic->bitstream + slice->dwSliceDataLocation, slice_size);
212  if (padding > 0)
213  memset(dxva_data + start_code_size + slice_size, 0, padding);
214  slice->dwSliceBitsInBuffer = 8 * data_size;
215  }
216  if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(ctx->decoder,
217  DXVA2_BitStreamDateBufferType)))
218  return -1;
219  if (result)
220  return result;
221 
222  memset(bs, 0, sizeof(*bs));
223  bs->CompressedBufferType = DXVA2_BitStreamDateBufferType;
224  bs->DataSize = data_size;
225  bs->NumMBsInBuffer = s->mb_width * s->mb_height;
226  assert((bs->DataSize & 127) == 0);
227 
228  return ff_dxva2_commit_buffer(avctx, ctx, sc,
229  DXVA2_SliceControlBufferType,
230  slice, sizeof(*slice), bs->NumMBsInBuffer);
231 }
232 
234  av_unused const uint8_t *buffer,
235  av_unused uint32_t size)
236 {
237  const VC1Context *v = avctx->priv_data;
238  struct dxva_context *ctx = avctx->hwaccel_context;
240 
241  if (!ctx->decoder || !ctx->cfg || ctx->surface_count <= 0)
242  return -1;
243  assert(ctx_pic);
244 
245  fill_picture_parameters(avctx, ctx, v, &ctx_pic->pp);
246 
247  ctx_pic->bitstream_size = 0;
248  ctx_pic->bitstream = NULL;
249  return 0;
250 }
251 
253  const uint8_t *buffer,
254  uint32_t size)
255 {
256  const VC1Context *v = avctx->priv_data;
257  const Picture *current_picture = v->s.current_picture_ptr;
258  struct dxva2_picture_context *ctx_pic = current_picture->hwaccel_picture_private;
259 
260  if (ctx_pic->bitstream_size > 0)
261  return -1;
262 
263  if (avctx->codec_id == AV_CODEC_ID_VC1 &&
264  size >= 4 && IS_MARKER(AV_RB32(buffer))) {
265  buffer += 4;
266  size -= 4;
267  }
268 
269  ctx_pic->bitstream_size = size;
270  ctx_pic->bitstream = buffer;
271 
272  fill_slice(avctx, &ctx_pic->si, 0, size);
273  return 0;
274 }
275 
277 {
278  VC1Context *v = avctx->priv_data;
280  int ret;
281 
282  if (ctx_pic->bitstream_size <= 0)
283  return -1;
284 
286  &ctx_pic->pp, sizeof(ctx_pic->pp),
287  NULL, 0,
289  if (!ret)
290  ff_mpeg_draw_horiz_band(&v->s, 0, avctx->height);
291  return ret;
292 }
293 
294 #if CONFIG_WMV3_DXVA2_HWACCEL
295 AVHWAccel ff_wmv3_dxva2_hwaccel = {
296  .name = "wmv3_dxva2",
297  .type = AVMEDIA_TYPE_VIDEO,
298  .id = AV_CODEC_ID_WMV3,
299  .pix_fmt = AV_PIX_FMT_DXVA2_VLD,
300  .start_frame = dxva2_vc1_start_frame,
301  .decode_slice = dxva2_vc1_decode_slice,
302  .end_frame = dxva2_vc1_end_frame,
303  .priv_data_size = sizeof(struct dxva2_picture_context),
304 };
305 #endif
306 
308  .name = "vc1_dxva2",
309  .type = AVMEDIA_TYPE_VIDEO,
310  .id = AV_CODEC_ID_VC1,
311  .pix_fmt = AV_PIX_FMT_DXVA2_VLD,
312  .start_frame = dxva2_vc1_start_frame,
313  .decode_slice = dxva2_vc1_decode_slice,
314  .end_frame = dxva2_vc1_end_frame,
315  .priv_data_size = sizeof(struct dxva2_picture_context),
316 };