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;
42 memset(pp, 0, sizeof(*pp));
43 pp->wDecodedPictureIndex =
44 pp->wDeblockedPictureIndex = ff_dxva2_get_surface_index(ctx, current_picture);
45 if (s->pict_type != AV_PICTURE_TYPE_I && !v->bi_type)
46 pp->wForwardRefPictureIndex = ff_dxva2_get_surface_index(ctx, &s->last_picture);
48 pp->wForwardRefPictureIndex = 0xffff;
49 if (s->pict_type == AV_PICTURE_TYPE_B && !v->bi_type)
50 pp->wBackwardRefPictureIndex = ff_dxva2_get_surface_index(ctx, &s->next_picture);
52 pp->wBackwardRefPictureIndex = 0xffff;
53 if (v->profile == PROFILE_ADVANCED) {
54 /* It is the cropped width/height -1 of the frame */
55 pp->wPicWidthInMBminus1 = avctx->width - 1;
56 pp->wPicHeightInMBminus1= avctx->height - 1;
58 /* It is the coded width/height in macroblock -1 of the frame */
59 pp->wPicWidthInMBminus1 = s->mb_width - 1;
60 pp->wPicHeightInMBminus1= s->mb_height - 1;
62 pp->bMacroblockWidthMinus1 = 15;
63 pp->bMacroblockHeightMinus1 = 15;
64 pp->bBlockWidthMinus1 = 7;
65 pp->bBlockHeightMinus1 = 7;
67 if (s->picture_structure & PICT_TOP_FIELD)
68 pp->bPicStructure |= 0x01;
69 if (s->picture_structure & PICT_BOTTOM_FIELD)
70 pp->bPicStructure |= 0x02;
71 pp->bSecondField = v->interlace && v->fcm == ILACE_FIELD && v->second_field;
72 pp->bPicIntra = s->pict_type == AV_PICTURE_TYPE_I || v->bi_type;
73 pp->bPicBackwardPrediction = s->pict_type == AV_PICTURE_TYPE_B && !v->bi_type;
74 pp->bBidirectionalAveragingMode = (1 << 7) |
75 ((ctx->cfg->ConfigIntraResidUnsigned != 0) << 6) |
76 ((ctx->cfg->ConfigResidDiffAccelerator != 0) << 5) |
77 ((v->lumscale != 32 || v->lumshift != 0) << 4) |
78 ((v->profile == PROFILE_ADVANCED) << 3);
79 pp->bMVprecisionAndChromaRelation = ((v->mv_mode == MV_PMODE_1MV_HPEL_BILIN) << 3) |
82 (!s->quarter_sample );
83 pp->bChromaFormat = v->chromaformat;
85 if (ctx->report_id >= (1 << 16))
87 pp->bPicScanFixed = ctx->report_id >> 8;
88 pp->bPicScanMethod = ctx->report_id & 0xff;
89 pp->bPicReadbackRequests = 0;
90 pp->bRcontrol = v->rnd;
91 pp->bPicSpatialResid8 = (v->panscanflag << 7) |
92 (v->refdist_flag << 6) |
93 (s->loop_filter << 5) |
95 (v->extended_mv << 3) |
98 pp->bPicOverflowBlocks = (v->quantizer_mode << 6) |
100 (s->resync_marker << 4) |
103 pp->bPicExtrapolation = (!v->interlace || v->fcm == PROGRESSIVE) ? 1 : 2;
104 pp->bPicDeblocked = ((!pp->bPicBackwardPrediction && v->overlap) << 6) |
105 ((v->profile != PROFILE_ADVANCED && v->rangeredfrm) << 5) |
106 (s->loop_filter << 1);
107 pp->bPicDeblockConfined = (v->postprocflag << 7) |
108 (v->broadcast << 6) |
109 (v->interlace << 5) |
110 (v->tfcntrflag << 4) |
111 (v->finterpflag << 3) |
112 ((s->pict_type != AV_PICTURE_TYPE_B) << 2) |
115 if (s->pict_type != AV_PICTURE_TYPE_I)
116 pp->bPic4MVallowed = v->mv_mode == MV_PMODE_MIXED_MV ||
117 (v->mv_mode == MV_PMODE_INTENSITY_COMP &&
118 v->mv_mode2 == MV_PMODE_MIXED_MV);
119 if (v->profile == PROFILE_ADVANCED)
120 pp->bPicOBMC = (v->range_mapy_flag << 7) |
121 (v->range_mapy << 4) |
122 (v->range_mapuv_flag << 3) |
126 pp->bReservedBits = 0;
127 if (s->picture_structure == PICT_FRAME) {
128 pp->wBitstreamFcodes = v->lumscale;
129 pp->wBitstreamPCEelements = v->lumshift;
131 /* Syntax: (top_field_param << 8) | bottom_field_param */
132 pp->wBitstreamFcodes = (v->lumscale << 8) | v->lumscale;
133 pp->wBitstreamPCEelements = (v->lumshift << 8) | v->lumshift;
135 pp->bBitstreamConcealmentNeed = 0;
136 pp->bBitstreamConcealmentMethod = 0;
139 static void fill_slice(AVCodecContext *avctx, DXVA_SliceInfo *slice,
140 unsigned position, unsigned size)
142 const VC1Context *v = avctx->priv_data;
143 const MpegEncContext *s = &v->s;
145 memset(slice, 0, sizeof(*slice));
146 slice->wHorizontalPosition = 0;
147 slice->wVerticalPosition = s->mb_y;
148 slice->dwSliceBitsInBuffer = 8 * size;
149 slice->dwSliceDataLocation = position;
150 slice->bStartCodeBitOffset = 0;
151 slice->bReservedBits = 0;
152 slice->wMBbitOffset = get_bits_count(&s->gb);
153 slice->wNumberMBsInSlice = s->mb_width * s->mb_height; /* XXX We assume 1 slice */
154 slice->wQuantizerScaleCode = v->pq;
155 slice->wBadSliceChopping = 0;
158 static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx,
159 DXVA2_DecodeBufferDesc *bs,
160 DXVA2_DecodeBufferDesc *sc)
162 const VC1Context *v = avctx->priv_data;
163 struct dxva_context *ctx = avctx->hwaccel_context;
164 const MpegEncContext *s = &v->s;
165 struct dxva2_picture_context *ctx_pic = s->current_picture_ptr->hwaccel_picture_private;
167 DXVA_SliceInfo *slice = &ctx_pic->si;
169 static const uint8_t start_code[] = { 0, 0, 1, 0x0d };
170 const unsigned start_code_size = avctx->codec_id == AV_CODEC_ID_VC1 ? sizeof(start_code) : 0;
171 const unsigned slice_size = slice->dwSliceBitsInBuffer / 8;
172 const unsigned padding = 128 - ((start_code_size + slice_size) & 127);
173 const unsigned data_size = start_code_size + slice_size + padding;
179 if (FAILED(IDirectXVideoDecoder_GetBuffer(ctx->decoder,
180 DXVA2_BitStreamDateBufferType,
181 (void **)&dxva_data, &dxva_size)))
184 result = data_size <= dxva_size ? 0 : -1;
186 if (start_code_size > 0) {
187 memcpy(dxva_data, start_code, start_code_size);
191 memcpy(dxva_data + start_code_size,
192 ctx_pic->bitstream + slice->dwSliceDataLocation, slice_size);
194 memset(dxva_data + start_code_size + slice_size, 0, padding);
195 slice->dwSliceBitsInBuffer = 8 * data_size;
197 if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(ctx->decoder,
198 DXVA2_BitStreamDateBufferType)))
203 memset(bs, 0, sizeof(*bs));
204 bs->CompressedBufferType = DXVA2_BitStreamDateBufferType;
205 bs->DataSize = data_size;
206 bs->NumMBsInBuffer = s->mb_width * s->mb_height;
207 assert((bs->DataSize & 127) == 0);
209 return ff_dxva2_commit_buffer(avctx, ctx, sc,
210 DXVA2_SliceControlBufferType,
211 slice, sizeof(*slice), bs->NumMBsInBuffer);
214 static int dxva2_vc1_start_frame(AVCodecContext *avctx,
215 av_unused const uint8_t *buffer,
216 av_unused uint32_t size)
218 const VC1Context *v = avctx->priv_data;
219 struct dxva_context *ctx = avctx->hwaccel_context;
220 struct dxva2_picture_context *ctx_pic = v->s.current_picture_ptr->hwaccel_picture_private;
222 if (!ctx->decoder || !ctx->cfg || ctx->surface_count <= 0)
226 fill_picture_parameters(avctx, ctx, v, &ctx_pic->pp);
228 ctx_pic->bitstream_size = 0;
229 ctx_pic->bitstream = NULL;
233 static int dxva2_vc1_decode_slice(AVCodecContext *avctx,
234 const uint8_t *buffer,
237 const VC1Context *v = avctx->priv_data;
238 const Picture *current_picture = v->s.current_picture_ptr;
239 struct dxva2_picture_context *ctx_pic = current_picture->hwaccel_picture_private;
241 if (ctx_pic->bitstream_size > 0)
244 if (avctx->codec_id == AV_CODEC_ID_VC1 &&
245 size >= 4 && IS_MARKER(AV_RB32(buffer))) {
250 ctx_pic->bitstream_size = size;
251 ctx_pic->bitstream = buffer;
253 fill_slice(avctx, &ctx_pic->si, 0, size);
257 static int dxva2_vc1_end_frame(AVCodecContext *avctx)
259 VC1Context *v = avctx->priv_data;
260 struct dxva2_picture_context *ctx_pic = v->s.current_picture_ptr->hwaccel_picture_private;
263 if (ctx_pic->bitstream_size <= 0)
266 ret = ff_dxva2_common_end_frame(avctx, v->s.current_picture_ptr,
267 &ctx_pic->pp, sizeof(ctx_pic->pp),
269 commit_bitstream_and_slice_buffer);
271 ff_mpeg_draw_horiz_band(&v->s, 0, avctx->height);
275 #if CONFIG_WMV3_DXVA2_HWACCEL
276 AVHWAccel ff_wmv3_dxva2_hwaccel = {
277 .name = "wmv3_dxva2",
278 .type = AVMEDIA_TYPE_VIDEO,
279 .id = AV_CODEC_ID_WMV3,
280 .pix_fmt = AV_PIX_FMT_DXVA2_VLD,
281 .start_frame = dxva2_vc1_start_frame,
282 .decode_slice = dxva2_vc1_decode_slice,
283 .end_frame = dxva2_vc1_end_frame,
284 .priv_data_size = sizeof(struct dxva2_picture_context),
288 AVHWAccel ff_vc1_dxva2_hwaccel = {
290 .type = AVMEDIA_TYPE_VIDEO,
291 .id = AV_CODEC_ID_VC1,
292 .pix_fmt = AV_PIX_FMT_DXVA2_VLD,
293 .start_frame = dxva2_vc1_start_frame,
294 .decode_slice = dxva2_vc1_decode_slice,
295 .end_frame = dxva2_vc1_end_frame,
296 .priv_data_size = sizeof(struct dxva2_picture_context),