Browse code

VC-1/WMV3 DXVA2 implementation

It allows VLD VC-1/WMV3 decoding using DXVA2 (GPU assisted decoding API under
VISTA and Windows 7).
It is implemented by using AVHWAccel API.

Originally committed as revision 21424 to svn://svn.ffmpeg.org/ffmpeg/trunk

Laurent Aimar authored on 2010/01/25 02:27:00
Showing 4 changed files
... ...
@@ -1234,6 +1234,8 @@ tiff_encoder_suggest="zlib"
1234 1234
 truehd_decoder_select="mlp_decoder"
1235 1235
 tscc_decoder_select="zlib"
1236 1236
 vc1_decoder_select="h263_decoder"
1237
+vc1_dxva2_hwaccel_deps="dxva2api_h"
1238
+vc1_dxva2_hwaccel_select="dxva2 vc1_decoder"
1237 1239
 vc1_vaapi_hwaccel_deps="va_va_h"
1238 1240
 vc1_vaapi_hwaccel_select="vaapi vc1_decoder"
1239 1241
 vc1_vdpau_decoder_deps="vdpau_vdpau_h vdpau_vdpau_x11_h"
... ...
@@ -1251,6 +1253,7 @@ wmv1_encoder_select="h263_encoder"
1251 1251
 wmv2_decoder_select="h263_decoder"
1252 1252
 wmv2_encoder_select="h263_encoder"
1253 1253
 wmv3_decoder_select="vc1_decoder"
1254
+wmv3_dxva2_hwaccel_select="vc1_dxva2_hwaccel"
1254 1255
 wmv3_vaapi_hwaccel_select="vc1_vaapi_hwaccel"
1255 1256
 wmv3_vdpau_decoder_select="vc1_vdpau_decoder"
1256 1257
 zlib_decoder_select="zlib"
... ...
@@ -315,6 +315,7 @@ OBJS-$(CONFIG_VB_DECODER)              += vb.o
315 315
 OBJS-$(CONFIG_VC1_DECODER)             += vc1dec.o vc1.o vc1data.o vc1dsp.o \
316 316
                                           msmpeg4.o msmpeg4data.o           \
317 317
                                           intrax8.o intrax8dsp.o
318
+OBJS-$(CONFIG_VC1_DXVA2_HWACCEL)       += dxva2_vc1.o
318 319
 OBJS-$(CONFIG_VC1_VAAPI_HWACCEL)       += vaapi_vc1.o
319 320
 OBJS-$(CONFIG_VCR1_DECODER)            += vcr1.o
320 321
 OBJS-$(CONFIG_VCR1_ENCODER)            += vcr1.o
... ...
@@ -59,7 +59,9 @@ void avcodec_register_all(void)
59 59
     REGISTER_HWACCEL (H264_VAAPI, h264_vaapi);
60 60
     REGISTER_HWACCEL (MPEG2_VAAPI, mpeg2_vaapi);
61 61
     REGISTER_HWACCEL (MPEG4_VAAPI, mpeg4_vaapi);
62
+    REGISTER_HWACCEL (VC1_DXVA2, vc1_dxva2);
62 63
     REGISTER_HWACCEL (VC1_VAAPI, vc1_vaapi);
64
+    REGISTER_HWACCEL (WMV3_DXVA2, wmv3_dxva2);
63 65
     REGISTER_HWACCEL (WMV3_VAAPI, wmv3_vaapi);
64 66
 
65 67
     /* video codecs */
66 68
new file mode 100644
... ...
@@ -0,0 +1,291 @@
0
+/*
1
+ * DXVA2 WMV3/VC-1 HW acceleration.
2
+ *
3
+ * copyright (c) 2010 Laurent Aimar
4
+ *
5
+ * This file is part of FFmpeg.
6
+ *
7
+ * FFmpeg is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * FFmpeg is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with FFmpeg; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+#include "dxva2_internal.h"
23
+#include "vc1.h"
24
+#include "vc1data.h"
25
+
26
+struct dxva2_picture_context {
27
+    DXVA_PictureParameters pp;
28
+    DXVA_SliceInfo         si;
29
+
30
+    const uint8_t          *bitstream;
31
+    unsigned               bitstream_size;
32
+};
33
+
34
+static void fill_picture_parameters(AVCodecContext *avctx,
35
+                                    struct dxva_context *ctx, const VC1Context *v,
36
+                                    DXVA_PictureParameters *pp)
37
+{
38
+    const MpegEncContext *s = &v->s;
39
+    const Picture *current_picture = s->current_picture_ptr;
40
+
41
+    memset(pp, 0, sizeof(*pp));
42
+    pp->wDecodedPictureIndex    =
43
+    pp->wDeblockedPictureIndex  = ff_dxva2_get_surface_index(ctx, current_picture);
44
+    if (s->pict_type != FF_I_TYPE)
45
+        pp->wForwardRefPictureIndex = ff_dxva2_get_surface_index(ctx, &s->last_picture);
46
+    else
47
+        pp->wForwardRefPictureIndex = 0xffff;
48
+    if (s->pict_type == FF_B_TYPE)
49
+        pp->wBackwardRefPictureIndex = ff_dxva2_get_surface_index(ctx, &s->next_picture);
50
+    else
51
+        pp->wBackwardRefPictureIndex = 0xffff;
52
+    if (v->profile == PROFILE_ADVANCED) {
53
+        /* It is the cropped width/height -1 of the frame */
54
+        pp->wPicWidthInMBminus1 = avctx->width  - 1;
55
+        pp->wPicHeightInMBminus1= avctx->height - 1;
56
+    } else {
57
+        /* It is the coded width/height in macroblock -1 of the frame */
58
+        pp->wPicWidthInMBminus1 = s->mb_width  - 1;
59
+        pp->wPicHeightInMBminus1= s->mb_height - 1;
60
+    }
61
+    pp->bMacroblockWidthMinus1  = 15;
62
+    pp->bMacroblockHeightMinus1 = 15;
63
+    pp->bBlockWidthMinus1       = 7;
64
+    pp->bBlockHeightMinus1      = 7;
65
+    pp->bBPPminus1              = 7;
66
+    if (s->picture_structure & PICT_TOP_FIELD)
67
+        pp->bPicStructure      |= 0x01;
68
+    if (s->picture_structure & PICT_BOTTOM_FIELD)
69
+        pp->bPicStructure      |= 0x02;
70
+    pp->bSecondField            = v->interlace && v->fcm != 0x03 && !s->first_field;
71
+    pp->bPicIntra               = s->pict_type == FF_I_TYPE;
72
+    pp->bPicBackwardPrediction  = s->pict_type == FF_B_TYPE;
73
+    pp->bBidirectionalAveragingMode = (1                                           << 7) |
74
+                                      ((ctx->cfg->ConfigIntraResidUnsigned != 0)   << 6) |
75
+                                      ((ctx->cfg->ConfigResidDiffAccelerator != 0) << 5) |
76
+                                      ((v->lumscale != 32 || v->lumshift != 0)     << 4) |
77
+                                      ((v->profile == PROFILE_ADVANCED)            << 3);
78
+    pp->bMVprecisionAndChromaRelation = ((v->mv_mode == MV_PMODE_1MV_HPEL_BILIN) << 3) |
79
+                                        (1                                       << 2) |
80
+                                        (0                                       << 1) |
81
+                                        (!s->quarter_sample                          );
82
+    pp->bChromaFormat           = v->chromaformat;
83
+    ctx->report_id++;
84
+    if (ctx->report_id >= (1 << 16))
85
+        ctx->report_id = 1;
86
+    pp->bPicScanFixed           = ctx->report_id >> 8;
87
+    pp->bPicScanMethod          = ctx->report_id & 0xff;
88
+    pp->bPicReadbackRequests    = 0;
89
+    pp->bRcontrol               = v->rnd;
90
+    pp->bPicSpatialResid8       = (v->panscanflag  << 7) |
91
+                                  (v->refdist_flag << 6) |
92
+                                  (s->loop_filter  << 5) |
93
+                                  (v->fastuvmc     << 4) |
94
+                                  (v->extended_mv  << 3) |
95
+                                  (v->dquant       << 1) |
96
+                                  (v->vstransform      );
97
+    pp->bPicOverflowBlocks      = (v->quantizer_mode << 6) |
98
+                                  (v->multires       << 5) |
99
+                                  (s->resync_marker  << 4) |
100
+                                  (v->rangered       << 3) |
101
+                                  (s->max_b_frames       );
102
+    pp->bPicExtrapolation       = (!v->interlace || v->fcm == 0x00) ? 1 : 2;
103
+    pp->bPicDeblocked           = ((v->profile != PROFILE_ADVANCED && v->rangeredfrm) << 5) |
104
+                                  (s->loop_filter                                     << 1);
105
+    pp->bPicDeblockConfined     = (v->postprocflag             << 7) |
106
+                                  (v->broadcast                << 6) |
107
+                                  (v->interlace                << 5) |
108
+                                  (v->tfcntrflag               << 4) |
109
+                                  (v->finterpflag              << 3) |
110
+                                  ((s->pict_type != FF_B_TYPE) << 2) |
111
+                                  (v->psf                      << 1) |
112
+                                  (v->extended_dmv                 );
113
+    if (s->pict_type != FF_I_TYPE)
114
+        pp->bPic4MVallowed      = v->mv_mode == MV_PMODE_MIXED_MV ||
115
+                                  (v->mv_mode == MV_PMODE_INTENSITY_COMP &&
116
+                                   v->mv_mode2 == MV_PMODE_MIXED_MV);
117
+    if (v->profile == PROFILE_ADVANCED)
118
+        pp->bPicOBMC            = (v->range_mapy_flag  << 7) |
119
+                                  (v->range_mapy       << 4) |
120
+                                  (v->range_mapuv_flag << 3) |
121
+                                  (v->range_mapuv          );
122
+    pp->bPicBinPB               = 0;
123
+    pp->bMV_RPS                 = 0;
124
+    pp->bReservedBits           = 0;
125
+    if (s->picture_structure == PICT_FRAME) {
126
+        pp->wBitstreamFcodes        = v->lumscale;
127
+        pp->wBitstreamPCEelements   = v->lumshift;
128
+    } else {
129
+        /* Syntax: (top_field_param << 8) | bottom_field_param */
130
+        pp->wBitstreamFcodes        = (v->lumscale << 8) | v->lumscale;
131
+        pp->wBitstreamPCEelements   = (v->lumshift << 8) | v->lumshift;
132
+    }
133
+    pp->bBitstreamConcealmentNeed   = 0;
134
+    pp->bBitstreamConcealmentMethod = 0;
135
+}
136
+
137
+static void fill_slice(AVCodecContext *avctx, DXVA_SliceInfo *slice,
138
+                       unsigned position, unsigned size)
139
+{
140
+    const VC1Context *v = avctx->priv_data;
141
+    const MpegEncContext *s = &v->s;
142
+
143
+    memset(slice, 0, sizeof(*slice));
144
+    slice->wHorizontalPosition = 0;
145
+    slice->wVerticalPosition   = s->mb_y;
146
+    slice->dwSliceBitsInBuffer = 8 * size;
147
+    slice->dwSliceDataLocation = position;
148
+    slice->bStartCodeBitOffset = 0;
149
+    slice->bReservedBits       = 0;
150
+    slice->wMBbitOffset        = get_bits_count(&s->gb);
151
+    slice->wNumberMBsInSlice   = s->mb_width * s->mb_height; /* XXX We assume 1 slice */
152
+    slice->wQuantizerScaleCode = v->pq;
153
+    slice->wBadSliceChopping   = 0;
154
+}
155
+
156
+static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx,
157
+                                             DXVA2_DecodeBufferDesc *bs,
158
+                                             DXVA2_DecodeBufferDesc *sc)
159
+{
160
+    const VC1Context *v = avctx->priv_data;
161
+    struct dxva_context *ctx = avctx->hwaccel_context;
162
+    const MpegEncContext *s = &v->s;
163
+    struct dxva2_picture_context *ctx_pic = s->current_picture_ptr->hwaccel_picture_private;
164
+
165
+    DXVA_SliceInfo *slice = &ctx_pic->si;
166
+
167
+    static const uint8_t start_code[] = { 0, 0, 1, 0x0d };
168
+    const unsigned start_code_size = avctx->codec_id == CODEC_ID_VC1 ? sizeof(start_code) : 0;
169
+    const unsigned slice_size = slice->dwSliceBitsInBuffer / 8;
170
+    const unsigned padding = 128 - ((start_code_size + slice_size) & 127);
171
+    const unsigned data_size = start_code_size + slice_size + padding;
172
+
173
+    uint8_t  *dxva_data;
174
+    unsigned dxva_size;
175
+    int result;
176
+
177
+    if (FAILED(IDirectXVideoDecoder_GetBuffer(ctx->decoder,
178
+                                              DXVA2_BitStreamDateBufferType,
179
+                                              &dxva_data, &dxva_size)))
180
+        return -1;
181
+
182
+    result = data_size <= dxva_size ? 0 : -1;
183
+    if (!result) {
184
+        if (start_code_size > 0)
185
+            memcpy(dxva_data, start_code, start_code_size);
186
+        memcpy(dxva_data + start_code_size,
187
+               ctx_pic->bitstream + slice->dwSliceDataLocation, slice_size);
188
+        if (padding > 0)
189
+            memset(dxva_data + start_code_size + slice_size, 0, padding);
190
+        slice->dwSliceBitsInBuffer = 8 * data_size;
191
+    }
192
+    if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(ctx->decoder,
193
+                                                  DXVA2_BitStreamDateBufferType)))
194
+        return -1;
195
+    if (result)
196
+        return result;
197
+
198
+    memset(bs, 0, sizeof(*bs));
199
+    bs->CompressedBufferType = DXVA2_BitStreamDateBufferType;
200
+    bs->DataSize             = data_size;
201
+    bs->NumMBsInBuffer       = s->mb_width * s->mb_height;
202
+    assert((bs->DataSize & 127) == 0);
203
+
204
+    return ff_dxva2_commit_buffer(avctx, ctx, sc,
205
+                                  DXVA2_SliceControlBufferType,
206
+                                  slice, sizeof(*slice), bs->NumMBsInBuffer);
207
+}
208
+
209
+static int start_frame(AVCodecContext *avctx,
210
+                       av_unused const uint8_t *buffer,
211
+                       av_unused uint32_t size)
212
+{
213
+    const VC1Context *v = avctx->priv_data;
214
+    struct dxva_context *ctx = avctx->hwaccel_context;
215
+    struct dxva2_picture_context *ctx_pic = v->s.current_picture_ptr->hwaccel_picture_private;
216
+
217
+    if (!ctx->decoder || !ctx->cfg || ctx->surface_count <= 0)
218
+        return -1;
219
+    assert(ctx_pic);
220
+
221
+    fill_picture_parameters(avctx, ctx, v, &ctx_pic->pp);
222
+
223
+    ctx_pic->bitstream_size = 0;
224
+    ctx_pic->bitstream      = NULL;
225
+    return 0;
226
+}
227
+
228
+static int decode_slice(AVCodecContext *avctx,
229
+                        const uint8_t *buffer, uint32_t size)
230
+{
231
+    const VC1Context *v = avctx->priv_data;
232
+    const Picture *current_picture = v->s.current_picture_ptr;
233
+    struct dxva2_picture_context *ctx_pic = current_picture->hwaccel_picture_private;
234
+
235
+    if (ctx_pic->bitstream_size > 0)
236
+        return -1;
237
+
238
+    if (avctx->codec_id == CODEC_ID_VC1 &&
239
+        size >= 4 && IS_MARKER(AV_RB32(buffer))) {
240
+        buffer += 4;
241
+        size   -= 4;
242
+    }
243
+
244
+    ctx_pic->bitstream_size = size;
245
+    ctx_pic->bitstream      = buffer;
246
+
247
+    fill_slice(avctx, &ctx_pic->si, 0, size);
248
+    return 0;
249
+}
250
+
251
+static int end_frame(AVCodecContext *avctx)
252
+{
253
+    VC1Context *v = avctx->priv_data;
254
+    struct dxva2_picture_context *ctx_pic = v->s.current_picture_ptr->hwaccel_picture_private;
255
+
256
+    if (ctx_pic->bitstream_size <= 0)
257
+        return -1;
258
+
259
+    return ff_dxva2_common_end_frame(avctx, &v->s,
260
+                                     &ctx_pic->pp, sizeof(ctx_pic->pp),
261
+                                     NULL, 0,
262
+                                     commit_bitstream_and_slice_buffer);
263
+}
264
+
265
+#if CONFIG_WMV3_DXVA2_HWACCEL
266
+AVHWAccel wmv3_dxva2_hwaccel = {
267
+    .name           = "wmv3_dxva2",
268
+    .type           = CODEC_TYPE_VIDEO,
269
+    .id             = CODEC_ID_WMV3,
270
+    .pix_fmt        = PIX_FMT_DXVA2_VLD,
271
+    .capabilities   = 0,
272
+    .start_frame    = start_frame,
273
+    .decode_slice   = decode_slice,
274
+    .end_frame      = end_frame,
275
+    .priv_data_size = sizeof(struct dxva2_picture_context),
276
+};
277
+#endif
278
+
279
+AVHWAccel vc1_dxva2_hwaccel = {
280
+    .name           = "vc1_dxva2",
281
+    .type           = CODEC_TYPE_VIDEO,
282
+    .id             = CODEC_ID_VC1,
283
+    .pix_fmt        = PIX_FMT_DXVA2_VLD,
284
+    .capabilities   = 0,
285
+    .start_frame    = start_frame,
286
+    .decode_slice   = decode_slice,
287
+    .end_frame      = end_frame,
288
+    .priv_data_size = sizeof(struct dxva2_picture_context),
289
+};
290
+