2 * DXVA2 WMV3/VC-1 HW acceleration.
4 * copyright (c) 2010 Laurent Aimar
6 * This file is part of FFmpeg.
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.
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.
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
23 #include "mpegutils.h"
27 // The headers above may include w32threads.h, which uses the original
28 // _WIN32_WINNT define, while dxva2_internal.h redefines it to target a
29 // potentially newer version.
30 #include "dxva2_internal.h"
32 struct dxva2_picture_context {
33 DXVA_PictureParameters pp;
36 const uint8_t *bitstream;
37 unsigned bitstream_size;
40 static void fill_picture_parameters(AVCodecContext *avctx,
41 AVDXVAContext *ctx, const VC1Context *v,
42 DXVA_PictureParameters *pp)
44 const MpegEncContext *s = &v->s;
45 const Picture *current_picture = s->current_picture_ptr;
48 // determine if intensity compensation is needed
49 if (s->pict_type == AV_PICTURE_TYPE_P) {
50 if ((v->fcm == ILACE_FRAME && v->intcomp) || (v->fcm != ILACE_FRAME && v->mv_mode == MV_PMODE_INTENSITY_COMP)) {
51 if (v->lumscale != 32 || v->lumshift != 0 || (s->picture_structure != PICT_FRAME && (v->lumscale2 != 32 || v->lumshift2 != 0)))
56 memset(pp, 0, sizeof(*pp));
57 pp->wDecodedPictureIndex =
58 pp->wDeblockedPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, current_picture->f);
59 if (s->pict_type != AV_PICTURE_TYPE_I && !v->bi_type)
60 pp->wForwardRefPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, s->last_picture.f);
62 pp->wForwardRefPictureIndex = 0xffff;
63 if (s->pict_type == AV_PICTURE_TYPE_B && !v->bi_type)
64 pp->wBackwardRefPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, s->next_picture.f);
66 pp->wBackwardRefPictureIndex = 0xffff;
67 if (v->profile == PROFILE_ADVANCED) {
68 /* It is the cropped width/height -1 of the frame */
69 pp->wPicWidthInMBminus1 = avctx->width - 1;
70 pp->wPicHeightInMBminus1= avctx->height - 1;
72 /* It is the coded width/height in macroblock -1 of the frame */
73 pp->wPicWidthInMBminus1 = s->mb_width - 1;
74 pp->wPicHeightInMBminus1= s->mb_height - 1;
76 pp->bMacroblockWidthMinus1 = 15;
77 pp->bMacroblockHeightMinus1 = 15;
78 pp->bBlockWidthMinus1 = 7;
79 pp->bBlockHeightMinus1 = 7;
81 if (s->picture_structure & PICT_TOP_FIELD)
82 pp->bPicStructure |= 0x01;
83 if (s->picture_structure & PICT_BOTTOM_FIELD)
84 pp->bPicStructure |= 0x02;
85 pp->bSecondField = v->interlace && v->fcm == ILACE_FIELD && v->second_field;
86 pp->bPicIntra = s->pict_type == AV_PICTURE_TYPE_I || v->bi_type;
87 pp->bPicBackwardPrediction = s->pict_type == AV_PICTURE_TYPE_B && !v->bi_type;
88 pp->bBidirectionalAveragingMode = (1 << 7) |
89 ((DXVA_CONTEXT_CFG_INTRARESID(avctx, ctx) != 0) << 6) |
90 ((DXVA_CONTEXT_CFG_RESIDACCEL(avctx, ctx) != 0) << 5) |
92 ((v->profile == PROFILE_ADVANCED) << 3);
93 pp->bMVprecisionAndChromaRelation = ((v->mv_mode == MV_PMODE_1MV_HPEL_BILIN) << 3) |
96 (!s->quarter_sample );
97 pp->bChromaFormat = v->chromaformat;
98 DXVA_CONTEXT_REPORT_ID(avctx, ctx)++;
99 if (DXVA_CONTEXT_REPORT_ID(avctx, ctx) >= (1 << 16))
100 DXVA_CONTEXT_REPORT_ID(avctx, ctx) = 1;
101 pp->bPicScanFixed = DXVA_CONTEXT_REPORT_ID(avctx, ctx) >> 8;
102 pp->bPicScanMethod = DXVA_CONTEXT_REPORT_ID(avctx, ctx) & 0xff;
103 pp->bPicReadbackRequests = 0;
104 pp->bRcontrol = v->rnd;
105 pp->bPicSpatialResid8 = (v->panscanflag << 7) |
106 (v->refdist_flag << 6) |
107 (s->loop_filter << 5) |
109 (v->extended_mv << 3) |
112 pp->bPicOverflowBlocks = (v->quantizer_mode << 6) |
114 (v->resync_marker << 4) |
117 pp->bPicExtrapolation = (!v->interlace || v->fcm == PROGRESSIVE) ? 1 : 2;
118 pp->bPicDeblocked = ((!pp->bPicBackwardPrediction && v->overlap) << 6) |
119 ((v->profile != PROFILE_ADVANCED && v->rangeredfrm) << 5) |
120 (s->loop_filter << 1);
121 pp->bPicDeblockConfined = (v->postprocflag << 7) |
122 (v->broadcast << 6) |
123 (v->interlace << 5) |
124 (v->tfcntrflag << 4) |
125 (v->finterpflag << 3) |
126 ((s->pict_type != AV_PICTURE_TYPE_B) << 2) |
129 if (s->pict_type != AV_PICTURE_TYPE_I)
130 pp->bPic4MVallowed = v->mv_mode == MV_PMODE_MIXED_MV ||
131 (v->mv_mode == MV_PMODE_INTENSITY_COMP &&
132 v->mv_mode2 == MV_PMODE_MIXED_MV);
133 if (v->profile == PROFILE_ADVANCED)
134 pp->bPicOBMC = (v->range_mapy_flag << 7) |
135 (v->range_mapy << 4) |
136 (v->range_mapuv_flag << 3) |
139 pp->bMV_RPS = (v->fcm == ILACE_FIELD && pp->bPicBackwardPrediction) ? v->refdist + 9 : 0;
140 pp->bReservedBits = v->pq;
141 if (s->picture_structure == PICT_FRAME) {
143 pp->wBitstreamFcodes = v->lumscale;
144 pp->wBitstreamPCEelements = v->lumshift;
146 pp->wBitstreamFcodes = 32;
147 pp->wBitstreamPCEelements = 0;
150 /* Syntax: (top_field_param << 8) | bottom_field_param */
152 pp->wBitstreamFcodes = (v->lumscale << 8) | v->lumscale2;
153 pp->wBitstreamPCEelements = (v->lumshift << 8) | v->lumshift2;
155 pp->wBitstreamFcodes = (32 << 8) | 32;
156 pp->wBitstreamPCEelements = 0;
159 pp->bBitstreamConcealmentNeed = 0;
160 pp->bBitstreamConcealmentMethod = 0;
163 static void fill_slice(AVCodecContext *avctx, DXVA_SliceInfo *slice,
164 unsigned position, unsigned size)
166 const VC1Context *v = avctx->priv_data;
167 const MpegEncContext *s = &v->s;
169 memset(slice, 0, sizeof(*slice));
170 slice->wHorizontalPosition = 0;
171 slice->wVerticalPosition = s->mb_y;
172 slice->dwSliceBitsInBuffer = 8 * size;
173 slice->dwSliceDataLocation = position;
174 slice->bStartCodeBitOffset = 0;
175 slice->bReservedBits = (s->pict_type == AV_PICTURE_TYPE_B && !v->bi_type) ? v->bfraction_lut_index + 9 : 0;
176 slice->wMBbitOffset = v->p_frame_skipped ? 0xffff : get_bits_count(&s->gb) + (avctx->codec_id == AV_CODEC_ID_VC1 ? 32 : 0);
177 slice->wNumberMBsInSlice = s->mb_width * s->mb_height; /* XXX We assume 1 slice */
178 slice->wQuantizerScaleCode = v->pq;
179 slice->wBadSliceChopping = 0;
182 static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx,
183 DECODER_BUFFER_DESC *bs,
184 DECODER_BUFFER_DESC *sc)
186 const VC1Context *v = avctx->priv_data;
187 AVDXVAContext *ctx = avctx->hwaccel_context;
188 const MpegEncContext *s = &v->s;
189 struct dxva2_picture_context *ctx_pic = s->current_picture_ptr->hwaccel_picture_private;
191 DXVA_SliceInfo *slice = &ctx_pic->si;
193 static const uint8_t start_code[] = { 0, 0, 1, 0x0d };
194 const unsigned start_code_size = avctx->codec_id == AV_CODEC_ID_VC1 ? sizeof(start_code) : 0;
195 const unsigned slice_size = slice->dwSliceBitsInBuffer / 8;
196 const unsigned padding = 128 - ((start_code_size + slice_size) & 127);
197 const unsigned data_size = start_code_size + slice_size + padding;
206 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
207 type = D3D11_VIDEO_DECODER_BUFFER_BITSTREAM;
208 if (FAILED(ID3D11VideoContext_GetDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context,
209 D3D11VA_CONTEXT(ctx)->decoder,
211 &dxva_size, &dxva_data_ptr)))
216 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
217 type = DXVA2_BitStreamDateBufferType;
218 if (FAILED(IDirectXVideoDecoder_GetBuffer(DXVA2_CONTEXT(ctx)->decoder,
220 &dxva_data_ptr, &dxva_size)))
225 dxva_data = dxva_data_ptr;
226 result = data_size <= dxva_size ? 0 : -1;
228 if (start_code_size > 0) {
229 memcpy(dxva_data, start_code, start_code_size);
233 memcpy(dxva_data + start_code_size,
234 ctx_pic->bitstream + slice->dwSliceDataLocation, slice_size);
236 memset(dxva_data + start_code_size + slice_size, 0, padding);
237 slice->dwSliceBitsInBuffer = 8 * data_size;
240 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
241 if (FAILED(ID3D11VideoContext_ReleaseDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder, type)))
245 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
246 if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(DXVA2_CONTEXT(ctx)->decoder, type)))
253 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
254 D3D11_VIDEO_DECODER_BUFFER_DESC *dsc11 = bs;
255 memset(dsc11, 0, sizeof(*dsc11));
256 dsc11->BufferType = type;
257 dsc11->DataSize = data_size;
258 dsc11->NumMBsInBuffer = s->mb_width * s->mb_height;
260 type = D3D11_VIDEO_DECODER_BUFFER_SLICE_CONTROL;
264 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
265 DXVA2_DecodeBufferDesc *dsc2 = bs;
266 memset(dsc2, 0, sizeof(*dsc2));
267 dsc2->CompressedBufferType = type;
268 dsc2->DataSize = data_size;
269 dsc2->NumMBsInBuffer = s->mb_width * s->mb_height;
271 type = DXVA2_SliceControlBufferType;
274 assert((data_size & 127) == 0);
276 return ff_dxva2_commit_buffer(avctx, ctx, sc,
278 slice, sizeof(*slice), s->mb_width * s->mb_height);
281 static int dxva2_vc1_start_frame(AVCodecContext *avctx,
282 av_unused const uint8_t *buffer,
283 av_unused uint32_t size)
285 const VC1Context *v = avctx->priv_data;
286 AVDXVAContext *ctx = avctx->hwaccel_context;
287 struct dxva2_picture_context *ctx_pic = v->s.current_picture_ptr->hwaccel_picture_private;
289 if (DXVA_CONTEXT_DECODER(avctx, ctx) == NULL ||
290 DXVA_CONTEXT_CFG(avctx, ctx) == NULL ||
291 DXVA_CONTEXT_COUNT(avctx, ctx) <= 0)
295 fill_picture_parameters(avctx, ctx, v, &ctx_pic->pp);
297 ctx_pic->bitstream_size = 0;
298 ctx_pic->bitstream = NULL;
302 static int dxva2_vc1_decode_slice(AVCodecContext *avctx,
303 const uint8_t *buffer,
306 const VC1Context *v = avctx->priv_data;
307 const Picture *current_picture = v->s.current_picture_ptr;
308 struct dxva2_picture_context *ctx_pic = current_picture->hwaccel_picture_private;
310 if (ctx_pic->bitstream_size > 0)
313 if (avctx->codec_id == AV_CODEC_ID_VC1 &&
314 size >= 4 && IS_MARKER(AV_RB32(buffer))) {
319 ctx_pic->bitstream_size = size;
320 ctx_pic->bitstream = buffer;
322 fill_slice(avctx, &ctx_pic->si, 0, size);
326 static int dxva2_vc1_end_frame(AVCodecContext *avctx)
328 VC1Context *v = avctx->priv_data;
329 struct dxva2_picture_context *ctx_pic = v->s.current_picture_ptr->hwaccel_picture_private;
332 if (ctx_pic->bitstream_size <= 0)
335 ret = ff_dxva2_common_end_frame(avctx, v->s.current_picture_ptr->f,
336 &ctx_pic->pp, sizeof(ctx_pic->pp),
338 commit_bitstream_and_slice_buffer);
340 ff_mpeg_draw_horiz_band(&v->s, 0, avctx->height);
344 #if CONFIG_WMV3_DXVA2_HWACCEL
345 AVHWAccel ff_wmv3_dxva2_hwaccel = {
346 .name = "wmv3_dxva2",
347 .type = AVMEDIA_TYPE_VIDEO,
348 .id = AV_CODEC_ID_WMV3,
349 .pix_fmt = AV_PIX_FMT_DXVA2_VLD,
350 .start_frame = dxva2_vc1_start_frame,
351 .decode_slice = dxva2_vc1_decode_slice,
352 .end_frame = dxva2_vc1_end_frame,
353 .frame_priv_data_size = sizeof(struct dxva2_picture_context),
357 #if CONFIG_VC1_DXVA2_HWACCEL
358 AVHWAccel ff_vc1_dxva2_hwaccel = {
360 .type = AVMEDIA_TYPE_VIDEO,
361 .id = AV_CODEC_ID_VC1,
362 .pix_fmt = AV_PIX_FMT_DXVA2_VLD,
363 .start_frame = dxva2_vc1_start_frame,
364 .decode_slice = dxva2_vc1_decode_slice,
365 .end_frame = dxva2_vc1_end_frame,
366 .frame_priv_data_size = sizeof(struct dxva2_picture_context),
370 #if CONFIG_WMV3_D3D11VA_HWACCEL
371 AVHWAccel ff_wmv3_d3d11va_hwaccel = {
372 .name = "wmv3_d3d11va",
373 .type = AVMEDIA_TYPE_VIDEO,
374 .id = AV_CODEC_ID_WMV3,
375 .pix_fmt = AV_PIX_FMT_D3D11VA_VLD,
376 .start_frame = dxva2_vc1_start_frame,
377 .decode_slice = dxva2_vc1_decode_slice,
378 .end_frame = dxva2_vc1_end_frame,
379 .frame_priv_data_size = sizeof(struct dxva2_picture_context),
383 #if CONFIG_VC1_D3D11VA_HWACCEL
384 AVHWAccel ff_vc1_d3d11va_hwaccel = {
385 .name = "vc1_d3d11va",
386 .type = AVMEDIA_TYPE_VIDEO,
387 .id = AV_CODEC_ID_VC1,
388 .pix_fmt = AV_PIX_FMT_D3D11VA_VLD,
389 .start_frame = dxva2_vc1_start_frame,
390 .decode_slice = dxva2_vc1_decode_slice,
391 .end_frame = dxva2_vc1_end_frame,
392 .frame_priv_data_size = sizeof(struct dxva2_picture_context),