73f9fb4109088160bdcf5c79ff95193652c8aa2d
[libav.git] / libavcodec / dxva2_vc1.c
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
36 static void fill_picture_parameters(AVCodecContext *avctx,
37 AVDXVAContext *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(avctx, ctx, current_picture->f);
46 if (s->pict_type != AV_PICTURE_TYPE_I && !v->bi_type)
47 pp->wForwardRefPictureIndex = ff_dxva2_get_surface_index(avctx, 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(avctx, 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;
68 if (s->picture_structure & PICT_TOP_FIELD)
69 pp->bPicStructure |= 0x01;
70 if (s->picture_structure & PICT_BOTTOM_FIELD)
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 ((DXVA_CONTEXT_CFG_INTRARESID(avctx, ctx) != 0) << 6) |
77 ((DXVA_CONTEXT_CFG_RESIDACCEL(avctx, ctx) != 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 DXVA_CONTEXT_REPORT_ID(avctx, ctx)++;
86 if (DXVA_CONTEXT_REPORT_ID(avctx, ctx) >= (1 << 16))
87 DXVA_CONTEXT_REPORT_ID(avctx, ctx) = 1;
88 pp->bPicScanFixed = DXVA_CONTEXT_REPORT_ID(avctx, ctx) >> 8;
89 pp->bPicScanMethod = DXVA_CONTEXT_REPORT_ID(avctx, ctx) & 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 ||
118 (v->mv_mode == MV_PMODE_INTENSITY_COMP &&
119 v->mv_mode2 == 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
159 static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx,
160 DECODER_BUFFER_DESC *bs,
161 DECODER_BUFFER_DESC *sc)
162 {
163 const VC1Context *v = avctx->priv_data;
164 AVDXVAContext *ctx = avctx->hwaccel_context;
165 const MpegEncContext *s = &v->s;
166 struct dxva2_picture_context *ctx_pic = s->current_picture_ptr->hwaccel_picture_private;
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 void *dxva_data_ptr;
177 uint8_t *dxva_data;
178 unsigned dxva_size;
179 int result;
180 unsigned type;
181
182 #if CONFIG_D3D11VA
183 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
184 type = D3D11_VIDEO_DECODER_BUFFER_BITSTREAM;
185 if (FAILED(ID3D11VideoContext_GetDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context,
186 D3D11VA_CONTEXT(ctx)->decoder,
187 type,
188 &dxva_size, &dxva_data_ptr)))
189 return -1;
190 }
191 #endif
192 #if CONFIG_DXVA2
193 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
194 type = DXVA2_BitStreamDateBufferType;
195 if (FAILED(IDirectXVideoDecoder_GetBuffer(DXVA2_CONTEXT(ctx)->decoder,
196 type,
197 &dxva_data_ptr, &dxva_size)))
198 return -1;
199 }
200 #endif
201
202 dxva_data = dxva_data_ptr;
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 memcpy(dxva_data + start_code_size,
208 ctx_pic->bitstream + slice->dwSliceDataLocation, slice_size);
209 if (padding > 0)
210 memset(dxva_data + start_code_size + slice_size, 0, padding);
211 slice->dwSliceBitsInBuffer = 8 * data_size;
212 }
213 #if CONFIG_D3D11VA
214 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
215 if (FAILED(ID3D11VideoContext_ReleaseDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder, type)))
216 return -1;
217 #endif
218 #if CONFIG_DXVA2
219 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
220 if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(DXVA2_CONTEXT(ctx)->decoder, type)))
221 return -1;
222 #endif
223 if (result)
224 return result;
225
226 #if CONFIG_D3D11VA
227 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
228 D3D11_VIDEO_DECODER_BUFFER_DESC *dsc11 = bs;
229 memset(dsc11, 0, sizeof(*dsc11));
230 dsc11->BufferType = type;
231 dsc11->DataSize = data_size;
232 dsc11->NumMBsInBuffer = s->mb_width * s->mb_height;
233
234 type = D3D11_VIDEO_DECODER_BUFFER_SLICE_CONTROL;
235 }
236 #endif
237 #if CONFIG_DXVA2
238 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
239 DXVA2_DecodeBufferDesc *dsc2 = bs;
240 memset(dsc2, 0, sizeof(*dsc2));
241 dsc2->CompressedBufferType = type;
242 dsc2->DataSize = data_size;
243 dsc2->NumMBsInBuffer = s->mb_width * s->mb_height;
244
245 type = DXVA2_SliceControlBufferType;
246 }
247 #endif
248 assert((data_size & 127) == 0);
249
250 return ff_dxva2_commit_buffer(avctx, ctx, sc,
251 type,
252 slice, sizeof(*slice), s->mb_width * s->mb_height);
253 }
254
255 static int dxva2_vc1_start_frame(AVCodecContext *avctx,
256 av_unused const uint8_t *buffer,
257 av_unused uint32_t size)
258 {
259 const VC1Context *v = avctx->priv_data;
260 AVDXVAContext *ctx = avctx->hwaccel_context;
261 struct dxva2_picture_context *ctx_pic = v->s.current_picture_ptr->hwaccel_picture_private;
262
263 if (DXVA_CONTEXT_DECODER(avctx, ctx) == NULL ||
264 DXVA_CONTEXT_CFG(avctx, ctx) == NULL ||
265 DXVA_CONTEXT_COUNT(avctx, ctx) <= 0)
266 return -1;
267 assert(ctx_pic);
268
269 fill_picture_parameters(avctx, ctx, v, &ctx_pic->pp);
270
271 ctx_pic->bitstream_size = 0;
272 ctx_pic->bitstream = NULL;
273 return 0;
274 }
275
276 static int dxva2_vc1_decode_slice(AVCodecContext *avctx,
277 const uint8_t *buffer,
278 uint32_t size)
279 {
280 const VC1Context *v = avctx->priv_data;
281 const Picture *current_picture = v->s.current_picture_ptr;
282 struct dxva2_picture_context *ctx_pic = current_picture->hwaccel_picture_private;
283
284 if (ctx_pic->bitstream_size > 0)
285 return -1;
286
287 if (avctx->codec_id == AV_CODEC_ID_VC1 &&
288 size >= 4 && IS_MARKER(AV_RB32(buffer))) {
289 buffer += 4;
290 size -= 4;
291 }
292
293 ctx_pic->bitstream_size = size;
294 ctx_pic->bitstream = buffer;
295
296 fill_slice(avctx, &ctx_pic->si, 0, size);
297 return 0;
298 }
299
300 static int dxva2_vc1_end_frame(AVCodecContext *avctx)
301 {
302 VC1Context *v = avctx->priv_data;
303 struct dxva2_picture_context *ctx_pic = v->s.current_picture_ptr->hwaccel_picture_private;
304 int ret;
305
306 if (ctx_pic->bitstream_size <= 0)
307 return -1;
308
309 ret = ff_dxva2_common_end_frame(avctx, v->s.current_picture_ptr->f,
310 &ctx_pic->pp, sizeof(ctx_pic->pp),
311 NULL, 0,
312 commit_bitstream_and_slice_buffer);
313 if (!ret)
314 ff_mpeg_draw_horiz_band(&v->s, 0, avctx->height);
315 return ret;
316 }
317
318 #if CONFIG_WMV3_DXVA2_HWACCEL
319 AVHWAccel ff_wmv3_dxva2_hwaccel = {
320 .name = "wmv3_dxva2",
321 .type = AVMEDIA_TYPE_VIDEO,
322 .id = AV_CODEC_ID_WMV3,
323 .pix_fmt = AV_PIX_FMT_DXVA2_VLD,
324 .start_frame = dxva2_vc1_start_frame,
325 .decode_slice = dxva2_vc1_decode_slice,
326 .end_frame = dxva2_vc1_end_frame,
327 .frame_priv_data_size = sizeof(struct dxva2_picture_context),
328 };
329 #endif
330
331 #if CONFIG_VC1_DXVA2_HWACCEL
332 AVHWAccel ff_vc1_dxva2_hwaccel = {
333 .name = "vc1_dxva2",
334 .type = AVMEDIA_TYPE_VIDEO,
335 .id = AV_CODEC_ID_VC1,
336 .pix_fmt = AV_PIX_FMT_DXVA2_VLD,
337 .start_frame = dxva2_vc1_start_frame,
338 .decode_slice = dxva2_vc1_decode_slice,
339 .end_frame = dxva2_vc1_end_frame,
340 .frame_priv_data_size = sizeof(struct dxva2_picture_context),
341 };
342 #endif
343
344 #if CONFIG_WMV3_D3D11VA_HWACCEL
345 AVHWAccel ff_wmv3_d3d11va_hwaccel = {
346 .name = "wmv3_d3d11va",
347 .type = AVMEDIA_TYPE_VIDEO,
348 .id = AV_CODEC_ID_WMV3,
349 .pix_fmt = AV_PIX_FMT_D3D11VA_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),
354 };
355 #endif
356
357 #if CONFIG_VC1_D3D11VA_HWACCEL
358 AVHWAccel ff_vc1_d3d11va_hwaccel = {
359 .name = "vc1_d3d11va",
360 .type = AVMEDIA_TYPE_VIDEO,
361 .id = AV_CODEC_ID_VC1,
362 .pix_fmt = AV_PIX_FMT_D3D11VA_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),
367 };
368 #endif