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 "dxva2_internal.h"
27 struct dxva2_picture_context {
28 DXVA_PictureParameters pp;
31 const uint8_t *bitstream;
32 unsigned bitstream_size;
35 static void fill_picture_parameters(AVCodecContext *avctx,
36 struct dxva_context *ctx, const VC1Context *v,
37 DXVA_PictureParameters *pp)
39 const MpegEncContext *s = &v->s;
40 const Picture *current_picture = s->current_picture_ptr;
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)))
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);
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);
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;
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;
71 pp->bMacroblockWidthMinus1 = 15;
72 pp->bMacroblockHeightMinus1 = 15;
73 pp->bBlockWidthMinus1 = 7;
74 pp->bBlockHeightMinus1 = 7;
76 if (s->picture_structure & PICT_TOP_FIELD)
77 pp->bPicStructure |= 0x01;
78 if (s->picture_structure & PICT_BOTTOM_FIELD)
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) |
87 ((v->profile == PROFILE_ADVANCED) << 3);
88 pp->bMVprecisionAndChromaRelation = ((v->mv_mode == MV_PMODE_1MV_HPEL_BILIN) << 3) |
91 (!s->quarter_sample );
92 pp->bChromaFormat = v->chromaformat;
94 if (ctx->report_id >= (1 << 16))
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) |
104 (v->extended_mv << 3) |
107 pp->bPicOverflowBlocks = (v->quantizer_mode << 6) |
109 (v->resync_marker << 4) |
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) |
124 if (s->pict_type != AV_PICTURE_TYPE_I)
125 pp->bPic4MVallowed = v->mv_mode == MV_PMODE_MIXED_MV ||
126 (v->mv_mode == MV_PMODE_INTENSITY_COMP &&
127 v->mv_mode2 == 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) |
134 pp->bMV_RPS = (v->fcm == ILACE_FIELD && pp->bPicBackwardPrediction) ? v->refdist + 9 : 0;
135 pp->bReservedBits = v->pq;
136 if (s->picture_structure == PICT_FRAME) {
138 pp->wBitstreamFcodes = v->lumscale;
139 pp->wBitstreamPCEelements = v->lumshift;
141 pp->wBitstreamFcodes = 32;
142 pp->wBitstreamPCEelements = 0;
145 /* Syntax: (top_field_param << 8) | bottom_field_param */
147 pp->wBitstreamFcodes = (v->lumscale << 8) | v->lumscale2;
148 pp->wBitstreamPCEelements = (v->lumshift << 8) | v->lumshift2;
150 pp->wBitstreamFcodes = (32 << 8) | 32;
151 pp->wBitstreamPCEelements = 0;
154 pp->bBitstreamConcealmentNeed = 0;
155 pp->bBitstreamConcealmentMethod = 0;
158 static void fill_slice(AVCodecContext *avctx, DXVA_SliceInfo *slice,
159 unsigned position, unsigned size)
161 const VC1Context *v = avctx->priv_data;
162 const MpegEncContext *s = &v->s;
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) + (avctx->codec_id == AV_CODEC_ID_VC1 ? 32 : 0);
172 slice->wNumberMBsInSlice = s->mb_width * s->mb_height; /* XXX We assume 1 slice */
173 slice->wQuantizerScaleCode = v->pq;
174 slice->wBadSliceChopping = 0;
177 static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx,
178 DXVA2_DecodeBufferDesc *bs,
179 DXVA2_DecodeBufferDesc *sc)
181 const VC1Context *v = avctx->priv_data;
182 struct dxva_context *ctx = avctx->hwaccel_context;
183 const MpegEncContext *s = &v->s;
184 struct dxva2_picture_context *ctx_pic = s->current_picture_ptr->hwaccel_picture_private;
186 DXVA_SliceInfo *slice = &ctx_pic->si;
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;
198 if (FAILED(IDirectXVideoDecoder_GetBuffer(ctx->decoder,
199 DXVA2_BitStreamDateBufferType,
200 (void **)&dxva_data, &dxva_size)))
203 result = data_size <= dxva_size ? 0 : -1;
205 if (start_code_size > 0) {
206 memcpy(dxva_data, start_code, start_code_size);
210 memcpy(dxva_data + start_code_size,
211 ctx_pic->bitstream + slice->dwSliceDataLocation, slice_size);
213 memset(dxva_data + start_code_size + slice_size, 0, padding);
214 slice->dwSliceBitsInBuffer = 8 * data_size;
216 if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(ctx->decoder,
217 DXVA2_BitStreamDateBufferType)))
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);
228 return ff_dxva2_commit_buffer(avctx, ctx, sc,
229 DXVA2_SliceControlBufferType,
230 slice, sizeof(*slice), bs->NumMBsInBuffer);
233 static int dxva2_vc1_start_frame(AVCodecContext *avctx,
234 av_unused const uint8_t *buffer,
235 av_unused uint32_t size)
237 const VC1Context *v = avctx->priv_data;
238 struct dxva_context *ctx = avctx->hwaccel_context;
239 struct dxva2_picture_context *ctx_pic = v->s.current_picture_ptr->hwaccel_picture_private;
241 if (!ctx->decoder || !ctx->cfg || ctx->surface_count <= 0)
245 fill_picture_parameters(avctx, ctx, v, &ctx_pic->pp);
247 ctx_pic->bitstream_size = 0;
248 ctx_pic->bitstream = NULL;
252 static int dxva2_vc1_decode_slice(AVCodecContext *avctx,
253 const uint8_t *buffer,
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;
260 if (ctx_pic->bitstream_size > 0)
263 if (avctx->codec_id == AV_CODEC_ID_VC1 &&
264 size >= 4 && IS_MARKER(AV_RB32(buffer))) {
269 ctx_pic->bitstream_size = size;
270 ctx_pic->bitstream = buffer;
272 fill_slice(avctx, &ctx_pic->si, 0, size);
276 static int dxva2_vc1_end_frame(AVCodecContext *avctx)
278 VC1Context *v = avctx->priv_data;
279 struct dxva2_picture_context *ctx_pic = v->s.current_picture_ptr->hwaccel_picture_private;
282 if (ctx_pic->bitstream_size <= 0)
285 ret = ff_dxva2_common_end_frame(avctx, v->s.current_picture_ptr,
286 &ctx_pic->pp, sizeof(ctx_pic->pp),
288 commit_bitstream_and_slice_buffer);
290 ff_mpeg_draw_horiz_band(&v->s, 0, avctx->height);
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),
307 AVHWAccel ff_vc1_dxva2_hwaccel = {
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),