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