GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/vc1_block.c Lines: 1851 1967 94.1 %
Date: 2020-08-14 10:39:37 Branches: 1640 2222 73.8 %

Line Branch Exec Source
1
/*
2
 * VC-1 and WMV3 decoder
3
 * Copyright (c) 2011 Mashiat Sarker Shakkhar
4
 * Copyright (c) 2006-2007 Konstantin Shishkov
5
 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
6
 *
7
 * This file is part of FFmpeg.
8
 *
9
 * FFmpeg is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
11
 * License as published by the Free Software Foundation; either
12
 * version 2.1 of the License, or (at your option) any later version.
13
 *
14
 * FFmpeg is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public
20
 * License along with FFmpeg; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
 */
23
24
/**
25
 * @file
26
 * VC-1 and WMV3 block decoding routines
27
 */
28
29
#include "avcodec.h"
30
#include "mpegutils.h"
31
#include "mpegvideo.h"
32
#include "msmpeg4data.h"
33
#include "unary.h"
34
#include "vc1.h"
35
#include "vc1_pred.h"
36
#include "vc1acdata.h"
37
#include "vc1data.h"
38
39
#define MB_INTRA_VLC_BITS 9
40
#define DC_VLC_BITS 9
41
42
// offset tables for interlaced picture MVDATA decoding
43
static const uint8_t offset_table[2][9] = {
44
    {  0,  1,  2,  4,  8, 16, 32,  64, 128 },
45
    {  0,  1,  3,  7, 15, 31, 63, 127, 255 },
46
};
47
48
// mapping table for internal block representation
49
static const int block_map[6] = {0, 2, 1, 3, 4, 5};
50
51
/***********************************************************************/
52
/**
53
 * @name VC-1 Bitplane decoding
54
 * @see 8.7, p56
55
 * @{
56
 */
57
58
59
9768
static inline void init_block_index(VC1Context *v)
60
{
61
9768
    MpegEncContext *s = &v->s;
62
9768
    ff_init_block_index(s);
63

9768
    if (v->field_mode && !(v->second_field ^ v->tff)) {
64
654
        s->dest[0] += s->current_picture_ptr->f->linesize[0];
65
654
        s->dest[1] += s->current_picture_ptr->f->linesize[1];
66
654
        s->dest[2] += s->current_picture_ptr->f->linesize[2];
67
    }
68
9768
}
69
70
/** @} */ //Bitplane group
71
72
233713
static void vc1_put_blocks_clamped(VC1Context *v, int put_signed)
73
{
74
233713
    MpegEncContext *s = &v->s;
75
    uint8_t *dest;
76
233713
    int block_count = CONFIG_GRAY && (s->avctx->flags & AV_CODEC_FLAG_GRAY) ? 4 : 6;
77
233713
    int fieldtx = 0;
78
    int i;
79
80
    /* The put pixels loop is one MB row and one MB column behind the decoding
81
     * loop because we can only put pixels when overlap filtering is done. For
82
     * interlaced frame pictures, however, the put pixels loop is only one
83
     * column behind the decoding loop as interlaced frame pictures only need
84
     * horizontal overlap filtering. */
85

233713
    if (!s->first_slice_line && v->fcm != ILACE_FRAME) {
86
172200
        if (s->mb_x) {
87
1168776
            for (i = 0; i < block_count; i++) {
88

1669680
                if (i > 3 ? v->mb_type[0][s->block_index[i] - s->block_wrap[i] - 1] :
89
667872
                            v->mb_type[0][s->block_index[i] - 2 * s->block_wrap[i] - 2]) {
90
252055
                    dest = s->dest[0] + ((i & 2) - 4) * 4 * s->linesize + ((i & 1) - 2) * 8;
91
252055
                    if (put_signed)
92

227279
                        s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][block_map[i]],
93
55930
                                                          i > 3 ? s->dest[i - 3] - 8 * s->uvlinesize - 8 : dest,
94
                                                          i > 3 ? s->uvlinesize : s->linesize);
95
                    else
96

107608
                        s->idsp.put_pixels_clamped(v->block[v->topleft_blk_idx][block_map[i]],
97
26902
                                                   i > 3 ? s->dest[i - 3] - 8 * s->uvlinesize - 8 : dest,
98
                                                   i > 3 ? s->uvlinesize : s->linesize);
99
                }
100
            }
101
        }
102
172200
        if (s->mb_x == v->end_mb_x - 1) {
103
36624
            for (i = 0; i < block_count; i++) {
104

52320
                if (i > 3 ? v->mb_type[0][s->block_index[i] - s->block_wrap[i]] :
105
20928
                            v->mb_type[0][s->block_index[i] - 2 * s->block_wrap[i]]) {
106
9951
                    dest = s->dest[0] + ((i & 2) - 4) * 4 * s->linesize + (i & 1) * 8;
107
9951
                    if (put_signed)
108

6659
                        s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][block_map[i]],
109
1622
                                                          i > 3 ? s->dest[i - 3] - 8 * s->uvlinesize : dest,
110
                                                          i > 3 ? s->uvlinesize : s->linesize);
111
                    else
112

6552
                        s->idsp.put_pixels_clamped(v->block[v->top_blk_idx][block_map[i]],
113
1638
                                                   i > 3 ? s->dest[i - 3] - 8 * s->uvlinesize : dest,
114
                                                   i > 3 ? s->uvlinesize : s->linesize);
115
                }
116
            }
117
        }
118
    }
119

233713
    if (s->mb_y == s->end_mb_y - 1 || v->fcm == ILACE_FRAME) {
120
61513
        if (s->mb_x) {
121
60300
            if (v->fcm == ILACE_FRAME)
122
24276
                fieldtx = v->fieldtx_plane[s->mb_y * s->mb_stride + s->mb_x - 1];
123
422100
            for (i = 0; i < block_count; i++) {
124

603000
                if (i > 3 ? v->mb_type[0][s->block_index[i] - 1] :
125
241200
                            v->mb_type[0][s->block_index[i] - 2]) {
126
133453
                    if (fieldtx)
127
18414
                        dest = s->dest[0] + ((i & 2) >> 1) * s->linesize + ((i & 1) - 2) * 8;
128
                    else
129
115039
                        dest = s->dest[0] + (i & 2) * 4 * s->linesize + ((i & 1) - 2) * 8;
130
133453
                    if (put_signed)
131

253742
                        s->idsp.put_signed_pixels_clamped(v->block[v->left_blk_idx][block_map[i]],
132
41650
                                                          i > 3 ? s->dest[i - 3] - 8 : dest,
133
85221
                                                          i > 3 ? s->uvlinesize : s->linesize << fieldtx);
134
                    else
135

13164
                        s->idsp.put_pixels_clamped(v->block[v->left_blk_idx][block_map[i]],
136
2194
                                                   i > 3 ? s->dest[i - 3] - 8 : dest,
137
4388
                                                   i > 3 ? s->uvlinesize : s->linesize << fieldtx);
138
                }
139
            }
140
        }
141
61513
        if (s->mb_x == v->end_mb_x - 1) {
142
1213
            if (v->fcm == ILACE_FRAME)
143
204
                fieldtx = v->fieldtx_plane[s->mb_y * s->mb_stride + s->mb_x];
144
8491
            for (i = 0; i < block_count; i++) {
145
7278
                if (v->mb_type[0][s->block_index[i]]) {
146
2831
                    if (fieldtx)
147
162
                        dest = s->dest[0] + ((i & 2) >> 1) * s->linesize + (i & 1) * 8;
148
                    else
149
2669
                        dest = s->dest[0] + (i & 2) * 4 * s->linesize + (i & 1) * 8;
150
2831
                    if (put_signed)
151

4666
                        s->idsp.put_signed_pixels_clamped(v->block[v->cur_blk_idx][block_map[i]],
152
764
                                                          i > 3 ? s->dest[i - 3] : dest,
153
1569
                                                          i > 3 ? s->uvlinesize : s->linesize << fieldtx);
154
                    else
155

996
                        s->idsp.put_pixels_clamped(v->block[v->cur_blk_idx][block_map[i]],
156
166
                                                   i > 3 ? s->dest[i - 3] : dest,
157
332
                                                   i > 3 ? s->uvlinesize : s->linesize << fieldtx);
158
                }
159
            }
160
        }
161
    }
162
233713
}
163
164
#define inc_blk_idx(idx) do { \
165
        idx++; \
166
        if (idx >= v->n_allocated_blks) \
167
            idx = 0; \
168
    } while (0)
169
170
/***********************************************************************/
171
/**
172
 * @name VC-1 Block-level functions
173
 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
174
 * @{
175
 */
176
177
/**
178
 * @def GET_MQUANT
179
 * @brief Get macroblock-level quantizer scale
180
 */
181
#define GET_MQUANT()                                           \
182
    if (v->dquantfrm) {                                        \
183
        int edges = 0;                                         \
184
        if (v->dqprofile == DQPROFILE_ALL_MBS) {               \
185
            if (v->dqbilevel) {                                \
186
                mquant = (get_bits1(gb)) ? -v->altpq : v->pq;  \
187
            } else {                                           \
188
                mqdiff = get_bits(gb, 3);                      \
189
                if (mqdiff != 7)                               \
190
                    mquant = -v->pq - mqdiff;                  \
191
                else                                           \
192
                    mquant = -get_bits(gb, 5);                 \
193
            }                                                  \
194
        }                                                      \
195
        if (v->dqprofile == DQPROFILE_SINGLE_EDGE)             \
196
            edges = 1 << v->dqsbedge;                          \
197
        else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES)       \
198
            edges = (3 << v->dqsbedge) % 15;                   \
199
        else if (v->dqprofile == DQPROFILE_FOUR_EDGES)         \
200
            edges = 15;                                        \
201
        if ((edges&1) && !s->mb_x)                             \
202
            mquant = -v->altpq;                                \
203
        if ((edges&2) && !s->mb_y)                             \
204
            mquant = -v->altpq;                                \
205
        if ((edges&4) && s->mb_x == (s->mb_width - 1))         \
206
            mquant = -v->altpq;                                \
207
        if ((edges&8) &&                                       \
208
            s->mb_y == ((s->mb_height >> v->field_mode) - 1))  \
209
            mquant = -v->altpq;                                \
210
        if (!mquant || mquant > 31 || mquant < -31) {                          \
211
            av_log(v->s.avctx, AV_LOG_ERROR,                   \
212
                   "Overriding invalid mquant %d\n", mquant);  \
213
            mquant = 1;                                        \
214
        }                                                      \
215
    }
216
217
/**
218
 * @def GET_MVDATA(_dmv_x, _dmv_y)
219
 * @brief Get MV differentials
220
 * @see MVDATA decoding from 8.3.5.2, p(1)20
221
 * @param _dmv_x Horizontal differential for decoded MV
222
 * @param _dmv_y Vertical differential for decoded MV
223
 */
224
#define GET_MVDATA(_dmv_x, _dmv_y)                                      \
225
    index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
226
                         VC1_MV_DIFF_VLC_BITS, 2);                      \
227
    if (index > 36) {                                                   \
228
        mb_has_coeffs = 1;                                              \
229
        index -= 37;                                                    \
230
    } else                                                              \
231
        mb_has_coeffs = 0;                                              \
232
    s->mb_intra = 0;                                                    \
233
    if (!index) {                                                       \
234
        _dmv_x = _dmv_y = 0;                                            \
235
    } else if (index == 35) {                                           \
236
        _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample);          \
237
        _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample);          \
238
    } else if (index == 36) {                                           \
239
        _dmv_x = 0;                                                     \
240
        _dmv_y = 0;                                                     \
241
        s->mb_intra = 1;                                                \
242
    } else {                                                            \
243
        index1 = index % 6;                                             \
244
        _dmv_x = offset_table[1][index1];                               \
245
        val = size_table[index1] - (!s->quarter_sample && index1 == 5); \
246
        if (val > 0) {                                                  \
247
            val = get_bits(gb, val);                                    \
248
            sign = 0 - (val & 1);                                       \
249
            _dmv_x = (sign ^ ((val >> 1) + _dmv_x)) - sign;             \
250
        }                                                               \
251
                                                                        \
252
        index1 = index / 6;                                             \
253
        _dmv_y = offset_table[1][index1];                               \
254
        val = size_table[index1] - (!s->quarter_sample && index1 == 5); \
255
        if (val > 0) {                                                  \
256
            val = get_bits(gb, val);                                    \
257
            sign = 0 - (val & 1);                                       \
258
            _dmv_y = (sign ^ ((val >> 1) + _dmv_y)) - sign;             \
259
        }                                                               \
260
    }
261
262
118892
static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
263
                                                   int *dmv_y, int *pred_flag)
264
{
265
    int index, index1;
266
    int extend_x, extend_y;
267
118892
    GetBitContext *gb = &v->s.gb;
268
    int bits, esc;
269
    int val, sign;
270
271
118892
    if (v->numref) {
272
59109
        bits = VC1_2REF_MVDATA_VLC_BITS;
273
59109
        esc  = 125;
274
    } else {
275
59783
        bits = VC1_1REF_MVDATA_VLC_BITS;
276
59783
        esc  = 71;
277
    }
278
118892
    extend_x = v->dmvrange & 1;
279
118892
    extend_y = (v->dmvrange >> 1) & 1;
280
118892
    index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
281
118892
    if (index == esc) {
282
8896
        *dmv_x = get_bits(gb, v->k_x);
283
8896
        *dmv_y = get_bits(gb, v->k_y);
284
8896
        if (v->numref) {
285
414
            if (pred_flag)
286
414
                *pred_flag = *dmv_y & 1;
287
414
            *dmv_y = (*dmv_y + (*dmv_y & 1)) >> 1;
288
        }
289
    }
290
    else {
291
109996
        av_assert0(index < esc);
292
109996
        index1 = (index + 1) % 9;
293
109996
        if (index1 != 0) {
294
84682
            val    = get_bits(gb, index1 + extend_x);
295
84682
            sign   = 0 - (val & 1);
296
84682
            *dmv_x = (sign ^ ((val >> 1) + offset_table[extend_x][index1])) - sign;
297
        } else
298
25314
            *dmv_x = 0;
299
109996
        index1 = (index + 1) / 9;
300
109996
        if (index1 > v->numref) {
301
76703
            val    = get_bits(gb, (index1 >> v->numref) + extend_y);
302
76703
            sign   = 0 - (val & 1);
303
76703
            *dmv_y = (sign ^ ((val >> 1) + offset_table[extend_y][index1 >> v->numref])) - sign;
304
        } else
305
33293
            *dmv_y = 0;
306

109996
        if (v->numref && pred_flag)
307
58695
            *pred_flag = index1 & 1;
308
    }
309
118892
}
310
311
/** Reconstruct motion vector for B-frame and do motion compensation
312
 */
313
62854
static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
314
                            int direct, int mode)
315
{
316
62854
    if (direct) {
317
9339
        ff_vc1_mc_1mv(v, 0);
318
9339
        ff_vc1_interp_mc(v);
319
9339
        return;
320
    }
321
53515
    if (mode == BMV_TYPE_INTERPOLATED) {
322
12043
        ff_vc1_mc_1mv(v, 0);
323
12043
        ff_vc1_interp_mc(v);
324
12043
        return;
325
    }
326
327
41472
    ff_vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
328
}
329
330
/** Get predicted DC value for I-frames only
331
 * prediction dir: left=0, top=1
332
 * @param s MpegEncContext
333
 * @param overlap flag indicating that overlap filtering is used
334
 * @param pq integer part of picture quantizer
335
 * @param[in] n block index in the current MB
336
 * @param dc_val_ptr Pointer to DC predictor
337
 * @param dir_ptr Prediction direction for use in AC prediction
338
 */
339
92700
static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
340
                                int16_t **dc_val_ptr, int *dir_ptr)
341
{
342
    int a, b, c, wrap, pred, scale;
343
    int16_t *dc_val;
344
    static const uint16_t dcpred[32] = {
345
        -1, 1024,  512,  341,  256,  205,  171,  146,  128,
346
             114,  102,   93,   85,   79,   73,   68,   64,
347
              60,   57,   54,   51,   49,   47,   45,   43,
348
              41,   39,   38,   37,   35,   34,   33
349
    };
350
351
    /* find prediction - wmv3_dc_scale always used here in fact */
352
92700
    if (n < 4) scale = s->y_dc_scale;
353
30900
    else       scale = s->c_dc_scale;
354
355
92700
    wrap   = s->block_wrap[n];
356
92700
    dc_val = s->dc_val[0] + s->block_index[n];
357
358
    /* B A
359
     * C X
360
     */
361
92700
    c = dc_val[ - 1];
362
92700
    b = dc_val[ - 1 - wrap];
363
92700
    a = dc_val[ - wrap];
364
365

92700
    if (pq < 9 || !overlap) {
366
        /* Set outer values */
367

92700
        if (s->first_slice_line && (n != 2 && n != 3))
368
4720
            b = a = dcpred[scale];
369

92700
        if (s->mb_x == 0 && (n != 1 && n != 3))
370
3608
            b = c = dcpred[scale];
371
    } else {
372
        /* Set outer values */
373
        if (s->first_slice_line && (n != 2 && n != 3))
374
            b = a = 0;
375
        if (s->mb_x == 0 && (n != 1 && n != 3))
376
            b = c = 0;
377
    }
378
379
92700
    if (abs(a - b) <= abs(b - c)) {
380
62836
        pred     = c;
381
62836
        *dir_ptr = 1; // left
382
    } else {
383
29864
        pred     = a;
384
29864
        *dir_ptr = 0; // top
385
    }
386
387
    /* update predictor */
388
92700
    *dc_val_ptr = &dc_val[0];
389
92700
    return pred;
390
}
391
392
393
/** Get predicted DC value
394
 * prediction dir: left=0, top=1
395
 * @param s MpegEncContext
396
 * @param overlap flag indicating that overlap filtering is used
397
 * @param pq integer part of picture quantizer
398
 * @param[in] n block index in the current MB
399
 * @param a_avail flag indicating top block availability
400
 * @param c_avail flag indicating left block availability
401
 * @param dc_val_ptr Pointer to DC predictor
402
 * @param dir_ptr Prediction direction for use in AC prediction
403
 */
404
307504
static inline int ff_vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
405
                              int a_avail, int c_avail,
406
                              int16_t **dc_val_ptr, int *dir_ptr)
407
{
408
    int a, b, c, wrap, pred;
409
    int16_t *dc_val;
410
307504
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
411
307504
    int q1, q2 = 0;
412
    int dqscale_index;
413
414
    /* scale predictors if needed */
415
307504
    q1 = FFABS(s->current_picture.qscale_table[mb_pos]);
416
307504
    dqscale_index = s->y_dc_scale_table[q1] - 1;
417
307504
    if (dqscale_index < 0)
418
        return 0;
419
420
307504
    wrap = s->block_wrap[n];
421
307504
    dc_val = s->dc_val[0] + s->block_index[n];
422
423
    /* B A
424
     * C X
425
     */
426
307504
    c = dc_val[ - 1];
427
307504
    b = dc_val[ - 1 - wrap];
428
307504
    a = dc_val[ - wrap];
429
430

307504
    if (c_avail && (n != 1 && n != 3)) {
431
113632
        q2 = FFABS(s->current_picture.qscale_table[mb_pos - 1]);
432

113632
        if (q2 && q2 != q1)
433
5816
            c = (int)((unsigned)c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
434
    }
435

307504
    if (a_avail && (n != 2 && n != 3)) {
436
95301
        q2 = FFABS(s->current_picture.qscale_table[mb_pos - s->mb_stride]);
437

95301
        if (q2 && q2 != q1)
438
5480
            a = (int)((unsigned)a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
439
    }
440

307504
    if (a_avail && c_avail && (n != 3)) {
441
105964
        int off = mb_pos;
442
105964
        if (n != 1)
443
82210
            off--;
444
105964
        if (n != 2)
445
77634
            off -= s->mb_stride;
446
105964
        q2 = FFABS(s->current_picture.qscale_table[off]);
447

105964
        if (q2 && q2 != q1)
448
7525
            b = (int)((unsigned)b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
449
    }
450
451

307504
    if (c_avail && (!a_avail || abs(a - b) <= abs(b - c))) {
452
161106
        pred     = c;
453
161106
        *dir_ptr = 1; // left
454
146398
    } else if (a_avail) {
455
93264
        pred     = a;
456
93264
        *dir_ptr = 0; // top
457
    } else {
458
53134
        pred     = 0;
459
53134
        *dir_ptr = 1; // left
460
    }
461
462
    /* update predictor */
463
307504
    *dc_val_ptr = &dc_val[0];
464
307504
    return pred;
465
}
466
467
/** @} */ // Block group
468
469
/**
470
 * @name VC1 Macroblock-level functions in Simple/Main Profiles
471
 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
472
 * @{
473
 */
474
475
110256
static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
476
                                       uint8_t **coded_block_ptr)
477
{
478
    int xy, wrap, pred, a, b, c;
479
480
110256
    xy   = s->block_index[n];
481
110256
    wrap = s->b8_stride;
482
483
    /* B C
484
     * A X
485
     */
486
110256
    a = s->coded_block[xy - 1       ];
487
110256
    b = s->coded_block[xy - 1 - wrap];
488
110256
    c = s->coded_block[xy     - wrap];
489
490
110256
    if (b == c) {
491
97397
        pred = a;
492
    } else {
493
12859
        pred = c;
494
    }
495
496
    /* store value */
497
110256
    *coded_block_ptr = &s->coded_block[xy];
498
499
110256
    return pred;
500
}
501
502
/**
503
 * Decode one AC coefficient
504
 * @param v The VC1 context
505
 * @param last Last coefficient
506
 * @param skip How much zero coefficients to skip
507
 * @param value Decoded AC coefficient value
508
 * @param codingset set of VLC to decode data
509
 * @see 8.1.3.4
510
 */
511
7198422
static int vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
512
                                int *value, int codingset)
513
{
514
7198422
    GetBitContext *gb = &v->s.gb;
515
    int index, run, level, lst, sign;
516
517
7198422
    index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
518
7198422
    if (index < 0)
519
        return index;
520
7198422
    if (index != ff_vc1_ac_sizes[codingset] - 1) {
521
7138154
        run   = vc1_index_decode_table[codingset][index][0];
522
7138154
        level = vc1_index_decode_table[codingset][index][1];
523

7138154
        lst   = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
524
7138154
        sign  = get_bits1(gb);
525
    } else {
526
60268
        int escape = decode210(gb);
527
60268
        if (escape != 2) {
528
52988
            index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
529
52988
            if (index >= ff_vc1_ac_sizes[codingset] - 1U)
530
                return AVERROR_INVALIDDATA;
531
52988
            run   = vc1_index_decode_table[codingset][index][0];
532
52988
            level = vc1_index_decode_table[codingset][index][1];
533
52988
            lst   = index >= vc1_last_decode_table[codingset];
534
52988
            if (escape == 0) {
535
26409
                if (lst)
536
10794
                    level += vc1_last_delta_level_table[codingset][run];
537
                else
538
15615
                    level += vc1_delta_level_table[codingset][run];
539
            } else {
540
26579
                if (lst)
541
9525
                    run += vc1_last_delta_run_table[codingset][level] + 1;
542
                else
543
17054
                    run += vc1_delta_run_table[codingset][level] + 1;
544
            }
545
52988
            sign = get_bits1(gb);
546
        } else {
547
7280
            lst = get_bits1(gb);
548
7280
            if (v->s.esc3_level_length == 0) {
549

374
                if (v->pq < 8 || v->dquantfrm) { // table 59
550
316
                    v->s.esc3_level_length = get_bits(gb, 3);
551
316
                    if (!v->s.esc3_level_length)
552
22
                        v->s.esc3_level_length = get_bits(gb, 2) + 8;
553
                } else { // table 60
554
58
                    v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
555
                }
556
374
                v->s.esc3_run_length = 3 + get_bits(gb, 2);
557
            }
558
7280
            run   = get_bits(gb, v->s.esc3_run_length);
559
7280
            sign  = get_bits1(gb);
560
7280
            level = get_bits(gb, v->s.esc3_level_length);
561
        }
562
    }
563
564
7198422
    *last  = lst;
565
7198422
    *skip  = run;
566
7198422
    *value = (level ^ -sign) + sign;
567
568
7198422
    return 0;
569
}
570
571
/** Decode intra block in intra frames - should be faster than decode_intra_block
572
 * @param v VC1Context
573
 * @param block block to decode
574
 * @param[in] n subblock index
575
 * @param coded are AC coeffs present or not
576
 * @param codingset set of VLC to decode data
577
 */
578
92700
static int vc1_decode_i_block(VC1Context *v, int16_t block[64], int n,
579
                              int coded, int codingset)
580
{
581
92700
    GetBitContext *gb = &v->s.gb;
582
92700
    MpegEncContext *s = &v->s;
583
92700
    int dc_pred_dir = 0; /* Direction of the DC prediction used */
584
    int i;
585
    int16_t *dc_val;
586
    int16_t *ac_val, *ac_val2;
587
    int dcdiff, scale;
588
589
    /* Get DC differential */
590
92700
    if (n < 4) {
591
61800
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
592
    } else {
593
30900
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
594
    }
595
92700
    if (dcdiff < 0) {
596
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
597
        return -1;
598
    }
599
92700
    if (dcdiff) {
600

67998
        const int m = (v->pq == 1 || v->pq == 2) ? 3 - v->pq : 0;
601
67998
        if (dcdiff == 119 /* ESC index value */) {
602
1
            dcdiff = get_bits(gb, 8 + m);
603
        } else {
604
67997
            if (m)
605
15121
                dcdiff = (dcdiff << m) + get_bits(gb, m) - ((1 << m) - 1);
606
        }
607
67998
        if (get_bits1(gb))
608
35364
            dcdiff = -dcdiff;
609
    }
610
611
    /* Prediction */
612
92700
    dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
613
92700
    *dc_val = dcdiff;
614
615
    /* Store the quantized DC coeff, used for prediction */
616
92700
    if (n < 4)
617
61800
        scale = s->y_dc_scale;
618
    else
619
30900
        scale = s->c_dc_scale;
620
92700
    block[0] = dcdiff * scale;
621
622
92700
    ac_val  = s->ac_val[0][s->block_index[n]];
623
92700
    ac_val2 = ac_val;
624
92700
    if (dc_pred_dir) // left
625
62836
        ac_val -= 16;
626
    else // top
627
29864
        ac_val -= 16 * s->block_wrap[n];
628
629
92700
    scale = v->pq * 2 + v->halfpq;
630
631
    //AC Decoding
632
92700
    i = !!coded;
633
634
92700
    if (coded) {
635
42724
        int last = 0, skip, value;
636
        const uint8_t *zz_table;
637
        int k;
638
639
42724
        if (v->s.ac_pred) {
640
9549
            if (!dc_pred_dir)
641
3134
                zz_table = v->zz_8x8[2];
642
            else
643
6415
                zz_table = v->zz_8x8[3];
644
        } else
645
33175
            zz_table = v->zz_8x8[1];
646
647
587287
        while (!last) {
648
544563
            int ret = vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
649
544563
            if (ret < 0)
650
                return ret;
651
544563
            i += skip;
652
544563
            if (i > 63)
653
                break;
654
544563
            block[zz_table[i++]] = value;
655
        }
656
657
        /* apply AC prediction if needed */
658
42724
        if (s->ac_pred) {
659
            int sh;
660
9549
            if (dc_pred_dir) { // left
661
6415
                sh = v->left_blk_sh;
662
            } else { // top
663
3134
                sh = v->top_blk_sh;
664
3134
                ac_val += 8;
665
            }
666
76392
            for (k = 1; k < 8; k++)
667
66843
                block[k << sh] += ac_val[k];
668
        }
669
        /* save AC coeffs for further prediction */
670
341792
        for (k = 1; k < 8; k++) {
671
299068
            ac_val2[k]     = block[k << v->left_blk_sh];
672
299068
            ac_val2[k + 8] = block[k << v->top_blk_sh];
673
        }
674
675
        /* scale AC coeffs */
676
2734336
        for (k = 1; k < 64; k++)
677
2691612
            if (block[k]) {
678
544598
                block[k] *= scale;
679
544598
                if (!v->pquantizer)
680
66373
                    block[k] += (block[k] < 0) ? -v->pq : v->pq;
681
            }
682
683
    } else {
684
        int k;
685
686
49976
        memset(ac_val2, 0, 16 * 2);
687
688
        /* apply AC prediction if needed */
689
49976
        if (s->ac_pred) {
690
            int sh;
691
3825
            if (dc_pred_dir) { //left
692
2909
                sh = v->left_blk_sh;
693
            } else { // top
694
916
                sh = v->top_blk_sh;
695
916
                ac_val  += 8;
696
916
                ac_val2 += 8;
697
            }
698
3825
            memcpy(ac_val2, ac_val, 8 * 2);
699
30600
            for (k = 1; k < 8; k++) {
700
26775
                block[k << sh] = ac_val[k] * scale;
701

26775
                if (!v->pquantizer && block[k << sh])
702
583
                    block[k << sh] += (block[k << sh] < 0) ? -v->pq : v->pq;
703
            }
704
        }
705
    }
706
92700
    if (s->ac_pred) i = 63;
707
92700
    s->block_last_index[n] = i;
708
709
92700
    return 0;
710
}
711
712
/** Decode intra block in intra frames - should be faster than decode_intra_block
713
 * @param v VC1Context
714
 * @param block block to decode
715
 * @param[in] n subblock number
716
 * @param coded are AC coeffs present or not
717
 * @param codingset set of VLC to decode data
718
 * @param mquant quantizer value for this macroblock
719
 */
720
72684
static int vc1_decode_i_block_adv(VC1Context *v, int16_t block[64], int n,
721
                                  int coded, int codingset, int mquant)
722
{
723
72684
    GetBitContext *gb = &v->s.gb;
724
72684
    MpegEncContext *s = &v->s;
725
72684
    int dc_pred_dir = 0; /* Direction of the DC prediction used */
726
    int i;
727
72684
    int16_t *dc_val = NULL;
728
    int16_t *ac_val, *ac_val2;
729
    int dcdiff;
730
72684
    int a_avail = v->a_avail, c_avail = v->c_avail;
731
72684
    int use_pred = s->ac_pred;
732
    int scale;
733
72684
    int q1, q2 = 0;
734
72684
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
735
72684
    int quant = FFABS(mquant);
736
737
    /* Get DC differential */
738
72684
    if (n < 4) {
739
48456
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
740
    } else {
741
24228
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
742
    }
743
72684
    if (dcdiff < 0) {
744
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
745
        return -1;
746
    }
747
72684
    if (dcdiff) {
748

50133
        const int m = (quant == 1 || quant == 2) ? 3 - quant : 0;
749
50133
        if (dcdiff == 119 /* ESC index value */) {
750
1
            dcdiff = get_bits(gb, 8 + m);
751
        } else {
752
50132
            if (m)
753
6545
                dcdiff = (dcdiff << m) + get_bits(gb, m) - ((1 << m) - 1);
754
        }
755
50133
        if (get_bits1(gb))
756
25831
            dcdiff = -dcdiff;
757
    }
758
759
    /* Prediction */
760
72684
    dcdiff += ff_vc1_pred_dc(&v->s, v->overlap, quant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
761
72684
    *dc_val = dcdiff;
762
763
    /* Store the quantized DC coeff, used for prediction */
764
72684
    if (n < 4)
765
48456
        scale = s->y_dc_scale;
766
    else
767
24228
        scale = s->c_dc_scale;
768
72684
    block[0] = dcdiff * scale;
769
770
    /* check if AC is needed at all */
771

72684
    if (!a_avail && !c_avail)
772
42
        use_pred = 0;
773
774
72684
    scale = quant * 2 + ((mquant < 0) ? 0 : v->halfpq);
775
776
72684
    ac_val  = s->ac_val[0][s->block_index[n]];
777
72684
    ac_val2 = ac_val;
778
72684
    if (dc_pred_dir) // left
779
48137
        ac_val -= 16;
780
    else // top
781
24547
        ac_val -= 16 * s->block_wrap[n];
782
783
72684
    q1 = s->current_picture.qscale_table[mb_pos];
784
72684
    if (n == 3)
785
12114
        q2 = q1;
786
60570
    else if (dc_pred_dir) {
787
40639
        if (n == 1)
788
8215
            q2 = q1;
789

32424
        else if (c_avail && mb_pos)
790
32382
            q2 = s->current_picture.qscale_table[mb_pos - 1];
791
    } else {
792
19931
        if (n == 2)
793
4905
            q2 = q1;
794

15026
        else if (a_avail && mb_pos >= s->mb_stride)
795
15026
            q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
796
    }
797
798
    //AC Decoding
799
72684
    i = 1;
800
801
72684
    if (coded) {
802
65520
        int last = 0, skip, value;
803
        const uint8_t *zz_table;
804
        int k;
805
806
65520
        if (v->s.ac_pred) {
807

16184
            if (!use_pred && v->fcm == ILACE_FRAME) {
808
3
                zz_table = v->zzi_8x8;
809
            } else {
810
16181
                if (!dc_pred_dir) // top
811
5968
                    zz_table = v->zz_8x8[2];
812
                else // left
813
10213
                    zz_table = v->zz_8x8[3];
814
            }
815
        } else {
816
49336
            if (v->fcm != ILACE_FRAME)
817
12456
                zz_table = v->zz_8x8[1];
818
            else
819
36880
                zz_table = v->zzi_8x8;
820
        }
821
822
845925
        while (!last) {
823
780405
            int ret = vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
824
780405
            if (ret < 0)
825
                return ret;
826
780405
            i += skip;
827
780405
            if (i > 63)
828
                break;
829
780405
            block[zz_table[i++]] = value;
830
        }
831
832
        /* apply AC prediction if needed */
833
65520
        if (use_pred) {
834
            int sh;
835
16172
            if (dc_pred_dir) { // left
836
10204
                sh = v->left_blk_sh;
837
            } else { // top
838
5968
                sh = v->top_blk_sh;
839
5968
                ac_val += 8;
840
            }
841
            /* scale predictors if needed*/
842
16172
            q1 = FFABS(q1) * 2 + ((q1 < 0) ? 0 : v->halfpq) - 1;
843
16172
            if (q1 < 1)
844
                return AVERROR_INVALIDDATA;
845
16172
            if (q2)
846
16172
                q2 = FFABS(q2) * 2 + ((q2 < 0) ? 0 : v->halfpq) - 1;
847

16172
            if (q2 && q1 != q2) {
848
8136
                for (k = 1; k < 8; k++)
849
7119
                    block[k << sh] += (int)(ac_val[k] * (unsigned)q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
850
            } else {
851
121240
                for (k = 1; k < 8; k++)
852
106085
                    block[k << sh] += ac_val[k];
853
            }
854
        }
855
        /* save AC coeffs for further prediction */
856
524160
        for (k = 1; k < 8; k++) {
857
458640
            ac_val2[k    ] = block[k << v->left_blk_sh];
858
458640
            ac_val2[k + 8] = block[k << v->top_blk_sh];
859
        }
860
861
        /* scale AC coeffs */
862
4193280
        for (k = 1; k < 64; k++)
863
4127760
            if (block[k]) {
864
779370
                block[k] *= scale;
865
779370
                if (!v->pquantizer)
866
                    block[k] += (block[k] < 0) ? -quant : quant;
867
            }
868
869
    } else { // no AC coeffs
870
        int k;
871
872
7164
        memset(ac_val2, 0, 16 * 2);
873
874
        /* apply AC prediction if needed */
875
7164
        if (use_pred) {
876
            int sh;
877
1708
            if (dc_pred_dir) { // left
878
1468
                sh = v->left_blk_sh;
879
            } else { // top
880
240
                sh = v->top_blk_sh;
881
240
                ac_val  += 8;
882
240
                ac_val2 += 8;
883
            }
884
1708
            memcpy(ac_val2, ac_val, 8 * 2);
885
1708
            q1 = FFABS(q1) * 2 + ((q1 < 0) ? 0 : v->halfpq) - 1;
886
1708
            if (q1 < 1)
887
                return AVERROR_INVALIDDATA;
888
1708
            if (q2)
889
1708
                q2 = FFABS(q2) * 2 + ((q2 < 0) ? 0 : v->halfpq) - 1;
890

1708
            if (q2 && q1 != q2) {
891
840
                for (k = 1; k < 8; k++)
892
735
                    ac_val2[k] = (int)(ac_val2[k] * q2 * (unsigned)ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
893
            }
894
13664
            for (k = 1; k < 8; k++) {
895
11956
                block[k << sh] = ac_val2[k] * scale;
896

11956
                if (!v->pquantizer && block[k << sh])
897
                    block[k << sh] += (block[k << sh] < 0) ? -quant : quant;
898
            }
899
        }
900
    }
901
72684
    if (use_pred) i = 63;
902
72684
    s->block_last_index[n] = i;
903
904
72684
    return 0;
905
}
906
907
/** Decode intra block in inter frames - more generic version than vc1_decode_i_block
908
 * @param v VC1Context
909
 * @param block block to decode
910
 * @param[in] n subblock index
911
 * @param coded are AC coeffs present or not
912
 * @param mquant block quantizer
913
 * @param codingset set of VLC to decode data
914
 */
915
234820
static int vc1_decode_intra_block(VC1Context *v, int16_t block[64], int n,
916
                                  int coded, int mquant, int codingset)
917
{
918
234820
    GetBitContext *gb = &v->s.gb;
919
234820
    MpegEncContext *s = &v->s;
920
234820
    int dc_pred_dir = 0; /* Direction of the DC prediction used */
921
    int i;
922
234820
    int16_t *dc_val = NULL;
923
    int16_t *ac_val, *ac_val2;
924
    int dcdiff;
925
234820
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
926
234820
    int a_avail = v->a_avail, c_avail = v->c_avail;
927
234820
    int use_pred = s->ac_pred;
928
    int scale;
929
234820
    int q1, q2 = 0;
930
234820
    int quant = FFABS(mquant);
931
932
234820
    s->bdsp.clear_block(block);
933
934
    /* XXX: Guard against dumb values of mquant */
935
234820
    quant = av_clip_uintp2(quant, 5);
936
937
    /* Set DC scale - y and c use the same */
938
234820
    s->y_dc_scale = s->y_dc_scale_table[quant];
939
234820
    s->c_dc_scale = s->c_dc_scale_table[quant];
940
941
    /* Get DC differential */
942
234820
    if (n < 4) {
943
158444
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
944
    } else {
945
76376
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
946
    }
947
234820
    if (dcdiff < 0) {
948
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
949
        return -1;
950
    }
951
234820
    if (dcdiff) {
952

189282
        const int m = (quant == 1 || quant == 2) ? 3 - quant : 0;
953
189282
        if (dcdiff == 119 /* ESC index value */) {
954
3
            dcdiff = get_bits(gb, 8 + m);
955
        } else {
956
189279
            if (m)
957
18284
                dcdiff = (dcdiff << m) + get_bits(gb, m) - ((1 << m) - 1);
958
        }
959
189282
        if (get_bits1(gb))
960
106032
            dcdiff = -dcdiff;
961
    }
962
963
    /* Prediction */
964
234820
    dcdiff += ff_vc1_pred_dc(&v->s, v->overlap, quant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
965
234820
    *dc_val = dcdiff;
966
967
    /* Store the quantized DC coeff, used for prediction */
968
969
234820
    if (n < 4) {
970
158444
        block[0] = dcdiff * s->y_dc_scale;
971
    } else {
972
76376
        block[0] = dcdiff * s->c_dc_scale;
973
    }
974
975
    //AC Decoding
976
234820
    i = 1;
977
978
    /* check if AC is needed at all and adjust direction if needed */
979
234820
    if (!a_avail) dc_pred_dir = 1;
980
234820
    if (!c_avail) dc_pred_dir = 0;
981

234820
    if (!a_avail && !c_avail) use_pred = 0;
982
234820
    ac_val = s->ac_val[0][s->block_index[n]];
983
234820
    ac_val2 = ac_val;
984
985
234820
    scale = quant * 2 + ((mquant < 0) ? 0 : v->halfpq);
986
987
234820
    if (dc_pred_dir) //left
988
113011
        ac_val -= 16;
989
    else //top
990
121809
        ac_val -= 16 * s->block_wrap[n];
991
992
234820
    q1 = s->current_picture.qscale_table[mb_pos];
993

234820
    if (dc_pred_dir && c_avail && mb_pos)
994
112932
        q2 = s->current_picture.qscale_table[mb_pos - 1];
995

234820
    if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
996
67195
        q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
997

234820
    if (dc_pred_dir && n == 1)
998
33871
        q2 = q1;
999

234820
    if (!dc_pred_dir && n == 2)
1000
28266
        q2 = q1;
1001
234820
    if (n == 3) q2 = q1;
1002
1003
234820
    if (coded) {
1004
162495
        int last = 0, skip, value;
1005
        int k;
1006
1007
1681789
        while (!last) {
1008
1519294
            int ret = vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1009
1519294
            if (ret < 0)
1010
                return ret;
1011
1519294
            i += skip;
1012
1519294
            if (i > 63)
1013
                break;
1014
1519294
            if (v->fcm == PROGRESSIVE)
1015
925288
                block[v->zz_8x8[0][i++]] = value;
1016
            else {
1017

594006
                if (use_pred && (v->fcm == ILACE_FRAME)) {
1018
19188
                    if (!dc_pred_dir) // top
1019
8623
                        block[v->zz_8x8[2][i++]] = value;
1020
                    else // left
1021
10565
                        block[v->zz_8x8[3][i++]] = value;
1022
                } else {
1023
574818
                    block[v->zzi_8x8[i++]] = value;
1024
                }
1025
            }
1026
        }
1027
1028
        /* apply AC prediction if needed */
1029
162495
        if (use_pred) {
1030
            /* scale predictors if needed*/
1031
42460
            q1 = FFABS(q1) * 2 + ((q1 < 0) ? 0 : v->halfpq) - 1;
1032
42460
            if (q1 < 1)
1033
                return AVERROR_INVALIDDATA;
1034
42460
            if (q2)
1035
42460
                q2 = FFABS(q2) * 2 + ((q2 < 0) ? 0 : v->halfpq) - 1;
1036

42460
            if (q2 && q1 != q2) {
1037
354
                if (dc_pred_dir) { // left
1038
1880
                    for (k = 1; k < 8; k++)
1039
1645
                        block[k << v->left_blk_sh] += (int)(ac_val[k] * q2 * (unsigned)ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1040
                } else { //top
1041
952
                    for (k = 1; k < 8; k++)
1042
833
                        block[k << v->top_blk_sh] += (int)(ac_val[k + 8] * q2 * (unsigned)ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1043
                }
1044
            } else {
1045
42106
                if (dc_pred_dir) { // left
1046
202440
                    for (k = 1; k < 8; k++)
1047
177135
                        block[k << v->left_blk_sh] += ac_val[k];
1048
                } else { // top
1049
134408
                    for (k = 1; k < 8; k++)
1050
117607
                        block[k << v->top_blk_sh] += ac_val[k + 8];
1051
                }
1052
            }
1053
        }
1054
        /* save AC coeffs for further prediction */
1055
1299960
        for (k = 1; k < 8; k++) {
1056
1137465
            ac_val2[k    ] = block[k << v->left_blk_sh];
1057
1137465
            ac_val2[k + 8] = block[k << v->top_blk_sh];
1058
        }
1059
1060
        /* scale AC coeffs */
1061
10399680
        for (k = 1; k < 64; k++)
1062
10237185
            if (block[k]) {
1063
1522244
                block[k] *= scale;
1064
1522244
                if (!v->pquantizer)
1065
28424
                    block[k] += (block[k] < 0) ? -quant : quant;
1066
            }
1067
1068
162495
        if (use_pred) i = 63;
1069
    } else { // no AC coeffs
1070
        int k;
1071
1072
72325
        memset(ac_val2, 0, 16 * 2);
1073
72325
        if (dc_pred_dir) { // left
1074
34015
            if (use_pred) {
1075
6684
                memcpy(ac_val2, ac_val, 8 * 2);
1076
6684
                q1 = FFABS(q1) * 2 + ((q1 < 0) ? 0 : v->halfpq) - 1;
1077
6684
                if (q1 < 1)
1078
                    return AVERROR_INVALIDDATA;
1079
6684
                if (q2)
1080
6684
                    q2 = FFABS(q2) * 2 + ((q2 < 0) ? 0 : v->halfpq) - 1;
1081

6684
                if (q2 && q1 != q2) {
1082
256
                    for (k = 1; k < 8; k++)
1083
224
                        ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1084
                }
1085
            }
1086
        } else { // top
1087
38310
            if (use_pred) {
1088
2665
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1089
2665
                q1 = FFABS(q1) * 2 + ((q1 < 0) ? 0 : v->halfpq) - 1;
1090
2665
                if (q1 < 1)
1091
                    return AVERROR_INVALIDDATA;
1092
2665
                if (q2)
1093
2665
                    q2 = FFABS(q2) * 2 + ((q2 < 0) ? 0 : v->halfpq) - 1;
1094

2665
                if (q2 && q1 != q2) {
1095
104
                    for (k = 1; k < 8; k++)
1096
91
                        ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1097
                }
1098
            }
1099
        }
1100
1101
        /* apply AC prediction if needed */
1102
72325
        if (use_pred) {
1103
9349
            if (dc_pred_dir) { // left
1104
53472
                for (k = 1; k < 8; k++) {
1105
46788
                    block[k << v->left_blk_sh] = ac_val2[k] * scale;
1106

46788
                    if (!v->pquantizer && block[k << v->left_blk_sh])
1107
127
                        block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -quant : quant;
1108
                }
1109
            } else { // top
1110
21320
                for (k = 1; k < 8; k++) {
1111
18655
                    block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
1112

18655
                    if (!v->pquantizer && block[k << v->top_blk_sh])
1113
129
                        block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -quant : quant;
1114
                }
1115
            }
1116
9349
            i = 63;
1117
        }
1118
    }
1119
234820
    s->block_last_index[n] = i;
1120
1121
234820
    return 0;
1122
}
1123
1124
/** Decode P block
1125
 */
1126
703814
static int vc1_decode_p_block(VC1Context *v, int16_t block[64], int n,
1127
                              int mquant, int ttmb, int first_block,
1128
                              uint8_t *dst, int linesize, int skip_block,
1129
                              int *ttmb_out)
1130
{
1131
703814
    MpegEncContext *s = &v->s;
1132
703814
    GetBitContext *gb = &s->gb;
1133
    int i, j;
1134
703814
    int subblkpat = 0;
1135
    int scale, off, idx, last, skip, value;
1136
703814
    int ttblk = ttmb & 7;
1137
703814
    int pat = 0;
1138
703814
    int quant = FFABS(mquant);
1139
1140
703814
    s->bdsp.clear_block(block);
1141
1142
703814
    if (ttmb == -1) {
1143
327570
        ttblk = ff_vc1_ttblk_to_tt[v->tt_index][get_vlc2(gb, ff_vc1_ttblk_vlc[v->tt_index].table, VC1_TTBLK_VLC_BITS, 1)];
1144
    }
1145
703814
    if (ttblk == TT_4X4) {
1146
77928
        subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
1147
    }
1148

703814
    if ((ttblk != TT_8X8 && ttblk != TT_4X4)
1149


273234
        && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
1150

242753
            || (!v->res_rtm_flag && !first_block))) {
1151
30481
        subblkpat = decode012(gb);
1152
30481
        if (subblkpat)
1153
11134
            subblkpat ^= 3; // swap decoded pattern bits
1154

30481
        if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
1155
5720
            ttblk = TT_8X4;
1156

30481
        if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
1157
3979
            ttblk = TT_4X8;
1158
    }
1159
703814
    scale = quant * 2 + ((mquant < 0) ? 0 : v->halfpq);
1160
1161
    // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
1162

703814
    if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
1163
38935
        subblkpat = 2 - (ttblk == TT_8X4_TOP);
1164
38935
        ttblk     = TT_8X4;
1165
    }
1166

703814
    if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
1167
47634
        subblkpat = 2 - (ttblk == TT_4X8_LEFT);
1168
47634
        ttblk     = TT_4X8;
1169
    }
1170

703814
    switch (ttblk) {
1171
352652
    case TT_8X8:
1172
352652
        pat  = 0xF;
1173
352652
        i    = 0;
1174
352652
        last = 0;
1175
2426985
        while (!last) {
1176
2074333
            int ret = vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1177
2074333
            if (ret < 0)
1178
                return ret;
1179
2074333
            i += skip;
1180
2074333
            if (i > 63)
1181
                break;
1182
2074333
            if (!v->fcm)
1183
934770
                idx = v->zz_8x8[0][i++];
1184
            else
1185
1139563
                idx = v->zzi_8x8[i++];
1186
2074333
            block[idx] = value * scale;
1187
2074333
            if (!v->pquantizer)
1188
201139
                block[idx] += (block[idx] < 0) ? -quant : quant;
1189
        }
1190
352652
        if (!skip_block) {
1191
352652
            if (i == 1)
1192
48583
                v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
1193
            else {
1194
304069
                v->vc1dsp.vc1_inv_trans_8x8(block);
1195
304069
                s->idsp.add_pixels_clamped(block, dst, linesize);
1196
            }
1197
        }
1198
352652
        break;
1199
77928
    case TT_4X4:
1200
77928
        pat = ~subblkpat & 0xF;
1201
389640
        for (j = 0; j < 4; j++) {
1202
311712
            last = subblkpat & (1 << (3 - j));
1203
311712
            i    = 0;
1204
311712
            off  = (j & 1) * 4 + (j & 2) * 16;
1205
818368
            while (!last) {
1206
506656
                int ret = vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1207
506656
                if (ret < 0)
1208
                    return ret;
1209
506656
                i += skip;
1210
506656
                if (i > 15)
1211
                    break;
1212
506656
                if (!v->fcm)
1213
283472
                    idx = ff_vc1_simple_progressive_4x4_zz[i++];
1214
                else
1215
223184
                    idx = ff_vc1_adv_interlaced_4x4_zz[i++];
1216
506656
                block[idx + off] = value * scale;
1217
506656
                if (!v->pquantizer)
1218
9718
                    block[idx + off] += (block[idx + off] < 0) ? -quant : quant;
1219
            }
1220

311712
            if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
1221
190131
                if (i == 1)
1222
21184
                    v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
1223
                else
1224
168947
                    v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) *  2 * linesize, linesize, block + off);
1225
            }
1226
        }
1227
77928
        break;
1228
134020
    case TT_8X4:
1229
134020
        pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
1230
402060
        for (j = 0; j < 2; j++) {
1231
268040
            last = subblkpat & (1 << (1 - j));
1232
268040
            i    = 0;
1233
268040
            off  = j * 32;
1234
1164631
            while (!last) {
1235
896591
                int ret = vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1236
896591
                if (ret < 0)
1237
                    return ret;
1238
896591
                i += skip;
1239
896591
                if (i > 31)
1240
                    break;
1241
896591
                if (!v->fcm)
1242
470573
                    idx = v->zz_8x4[i++] + off;
1243
                else
1244
426018
                    idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
1245
896591
                block[idx] = value * scale;
1246
896591
                if (!v->pquantizer)
1247
9538
                    block[idx] += (block[idx] < 0) ? -quant : quant;
1248
            }
1249

268040
            if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
1250
222413
                if (i == 1)
1251
15798
                    v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
1252
                else
1253
206615
                    v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
1254
            }
1255
        }
1256
134020
        break;
1257
139214
    case TT_4X8:
1258
139214
        pat = ~(subblkpat * 5) & 0xF;
1259
417642
        for (j = 0; j < 2; j++) {
1260
278428
            last = subblkpat & (1 << (1 - j));
1261
278428
            i    = 0;
1262
278428
            off  = j * 4;
1263
1155008
            while (!last) {
1264
876580
                int ret = vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1265
876580
                if (ret < 0)
1266
                    return ret;
1267
876580
                i += skip;
1268
876580
                if (i > 31)
1269
                    break;
1270
876580
                if (!v->fcm)
1271
431900
                    idx = v->zz_4x8[i++] + off;
1272
                else
1273
444680
                    idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
1274
876580
                block[idx] = value * scale;
1275
876580
                if (!v->pquantizer)
1276
10845
                    block[idx] += (block[idx] < 0) ? -quant : quant;
1277
            }
1278

278428
            if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
1279
226352
                if (i == 1)
1280
17898
                    v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
1281
                else
1282
208454
                    v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
1283
            }
1284
        }
1285
139214
        break;
1286
    }
1287
703814
    if (ttmb_out)
1288
646349
        *ttmb_out |= ttblk << (n * 4);
1289
703814
    return pat;
1290
}
1291
1292
/** @} */ // Macroblock group
1293
1294
static const uint8_t size_table[6] = { 0, 2, 3, 4,  5,  8 };
1295
1296
/** Decode one P-frame MB
1297
 */
1298
152584
static int vc1_decode_p_mb(VC1Context *v)
1299
{
1300
152584
    MpegEncContext *s = &v->s;
1301
152584
    GetBitContext *gb = &s->gb;
1302
    int i, j;
1303
152584
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1304
    int cbp; /* cbp decoding stuff */
1305
    int mqdiff, mquant; /* MB quantization */
1306
152584
    int ttmb = v->ttfrm; /* MB Transform type */
1307
1308
152584
    int mb_has_coeffs = 1; /* last_flag */
1309
    int dmv_x, dmv_y; /* Differential MV components */
1310
    int index, index1; /* LUT indexes */
1311
    int val, sign; /* temp values */
1312
152584
    int first_block = 1;
1313
    int dst_idx, off;
1314
    int skipped, fourmv;
1315
152584
    int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
1316
1317
152584
    mquant = v->pq; /* lossy initialization */
1318
1319
152584
    if (v->mv_type_is_raw)
1320
58770
        fourmv = get_bits1(gb);
1321
    else
1322
93814
        fourmv = v->mv_type_mb_plane[mb_pos];
1323
152584
    if (v->skip_is_raw)
1324
58470
        skipped = get_bits1(gb);
1325
    else
1326
94114
        skipped = v->s.mbskip_table[mb_pos];
1327
1328
152584
    if (!fourmv) { /* 1MV mode */
1329
126008
        if (!skipped) {
1330





113891
            GET_MVDATA(dmv_x, dmv_y);
1331
1332
113891
            if (s->mb_intra) {
1333
24985
                s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
1334
24985
                s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
1335
            }
1336
113891
            s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
1337
113891
            ff_vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1338
1339
            /* FIXME Set DC val for inter block ? */
1340

113891
            if (s->mb_intra && !mb_has_coeffs) {
1341









2748
                GET_MQUANT();
1342
2748
                s->ac_pred = get_bits1(gb);
1343
2748
                cbp        = 0;
1344
111143
            } else if (mb_has_coeffs) {
1345
92366
                if (s->mb_intra)
1346
22237
                    s->ac_pred = get_bits1(gb);
1347
92366
                cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1348









92366
                GET_MQUANT();
1349
            } else {
1350
18777
                mquant = v->pq;
1351
18777
                cbp    = 0;
1352
            }
1353
113891
            s->current_picture.qscale_table[mb_pos] = mquant;
1354
1355

113891
            if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
1356
42234
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
1357
                                VC1_TTMB_VLC_BITS, 2);
1358
113891
            if (!s->mb_intra) ff_vc1_mc_1mv(v, 0);
1359
113891
            dst_idx = 0;
1360
797237
            for (i = 0; i < 6; i++) {
1361
683346
                s->dc_val[0][s->block_index[i]] = 0;
1362
683346
                dst_idx += i >> 2;
1363
683346
                val = ((cbp >> (5 - i)) & 1);
1364
683346
                off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1365
683346
                v->mb_type[0][s->block_index[i]] = s->mb_intra;
1366
683346
                if (s->mb_intra) {
1367
                    /* check if prediction blocks A and C are available */
1368
149910
                    v->a_avail = v->c_avail = 0;
1369

149910
                    if (i == 2 || i == 3 || !s->first_slice_line)
1370
110526
                        v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1371

149910
                    if (i == 1 || i == 3 || s->mb_x)
1372
147294
                        v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1373
1374
149910
                    vc1_decode_intra_block(v, v->block[v->cur_blk_idx][block_map[i]], i, val, mquant,
1375
149910
                                           (i & 4) ? v->codingset2 : v->codingset);
1376
                    if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1377
                        continue;
1378
149910
                    v->vc1dsp.vc1_inv_trans_8x8(v->block[v->cur_blk_idx][block_map[i]]);
1379
149910
                    if (v->rangeredfrm)
1380
                        for (j = 0; j < 64; j++)
1381
                            v->block[v->cur_blk_idx][block_map[i]][j] *= 2;
1382
149910
                    block_cbp   |= 0xF << (i << 2);
1383
149910
                    block_intra |= 1 << i;
1384
533436
                } else if (val) {
1385
658374
                    pat = vc1_decode_p_block(v, v->block[v->cur_blk_idx][block_map[i]], i, mquant, ttmb, first_block,
1386
219458
                                             s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
1387
                                             CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
1388
219458
                    if (pat < 0)
1389
                        return pat;
1390
219458
                    block_cbp |= pat << (i << 2);
1391

219458
                    if (!v->ttmbf && ttmb < 8)
1392
87522
                        ttmb = -1;
1393
219458
                    first_block = 0;
1394
                }
1395
            }
1396
        } else { // skipped
1397
12117
            s->mb_intra = 0;
1398
84819
            for (i = 0; i < 6; i++) {
1399
72702
                v->mb_type[0][s->block_index[i]] = 0;
1400
72702
                s->dc_val[0][s->block_index[i]]  = 0;
1401
            }
1402
12117
            s->current_picture.mb_type[mb_pos]      = MB_TYPE_SKIP;
1403
12117
            s->current_picture.qscale_table[mb_pos] = 0;
1404
12117
            ff_vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1405
12117
            ff_vc1_mc_1mv(v, 0);
1406
        }
1407
    } else { // 4MV mode
1408
26576
        if (!skipped /* unskipped MB */) {
1409
22855
            int intra_count = 0, coded_inter = 0;
1410
            int is_intra[6], is_coded[6];
1411
            /* Get CBPCY */
1412
22855
            cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1413
159985
            for (i = 0; i < 6; i++) {
1414
137130
                val = ((cbp >> (5 - i)) & 1);
1415
137130
                s->dc_val[0][s->block_index[i]] = 0;
1416
137130
                s->mb_intra                     = 0;
1417
137130
                if (i < 4) {
1418
91420
                    dmv_x = dmv_y = 0;
1419
91420
                    s->mb_intra   = 0;
1420
91420
                    mb_has_coeffs = 0;
1421
91420
                    if (val) {
1422





71997
                        GET_MVDATA(dmv_x, dmv_y);
1423
                    }
1424
91420
                    ff_vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1425
91420
                    if (!s->mb_intra)
1426
80856
                        ff_vc1_mc_4mv_luma(v, i, 0, 0);
1427
91420
                    intra_count += s->mb_intra;
1428
91420
                    is_intra[i]  = s->mb_intra;
1429
91420
                    is_coded[i]  = mb_has_coeffs;
1430
                }
1431
137130
                if (i & 4) {
1432
45710
                    is_intra[i] = (intra_count >= 3);
1433
45710
                    is_coded[i] = val;
1434
                }
1435
137130
                if (i == 4)
1436
22855
                    ff_vc1_mc_4mv_chroma(v, 0);
1437
137130
                v->mb_type[0][s->block_index[i]] = is_intra[i];
1438
137130
                if (!coded_inter)
1439
52317
                    coded_inter = !is_intra[i] & is_coded[i];
1440
            }
1441
            // if there are no coded blocks then don't do anything more
1442
22855
            dst_idx = 0;
1443

22855
            if (!intra_count && !coded_inter)
1444
2699
                goto end;
1445









20156
            GET_MQUANT();
1446
20156
            s->current_picture.qscale_table[mb_pos] = mquant;
1447
            /* test if block is intra and has pred */
1448
            {
1449
20156
                int intrapred = 0;
1450
122744
                for (i = 0; i < 6; i++)
1451
106282
                    if (is_intra[i]) {
1452


7257
                        if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
1453


5463
                            || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
1454
3694
                            intrapred = 1;
1455
3694
                            break;
1456
                        }
1457
                    }
1458
20156
                if (intrapred)
1459
3694
                    s->ac_pred = get_bits1(gb);
1460
                else
1461
16462
                    s->ac_pred = 0;
1462
            }
1463

20156
            if (!v->ttmbf && coded_inter)
1464
18220
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1465
141092
            for (i = 0; i < 6; i++) {
1466
120936
                dst_idx    += i >> 2;
1467
120936
                off         = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1468
120936
                s->mb_intra = is_intra[i];
1469
120936
                if (is_intra[i]) {
1470
                    /* check if prediction blocks A and C are available */
1471
13000
                    v->a_avail = v->c_avail = 0;
1472

13000
                    if (i == 2 || i == 3 || !s->first_slice_line)
1473
10021
                        v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1474

13000
                    if (i == 1 || i == 3 || s->mb_x)
1475
12804
                        v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1476
1477
13000
                    vc1_decode_intra_block(v, v->block[v->cur_blk_idx][block_map[i]], i, is_coded[i], mquant,
1478
13000
                                           (i & 4) ? v->codingset2 : v->codingset);
1479
                    if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1480
                        continue;
1481
13000
                    v->vc1dsp.vc1_inv_trans_8x8(v->block[v->cur_blk_idx][block_map[i]]);
1482
13000
                    if (v->rangeredfrm)
1483
                        for (j = 0; j < 64; j++)
1484
                            v->block[v->cur_blk_idx][block_map[i]][j] *= 2;
1485
13000
                    block_cbp   |= 0xF << (i << 2);
1486
13000
                    block_intra |= 1 << i;
1487
107936
                } else if (is_coded[i]) {
1488
200115
                    pat = vc1_decode_p_block(v, v->block[v->cur_blk_idx][block_map[i]], i, mquant, ttmb,
1489
66705
                                             first_block, s->dest[dst_idx] + off,
1490
66705
                                             (i & 4) ? s->uvlinesize : s->linesize,
1491
                                             CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY),
1492
                                             &block_tt);
1493
66705
                    if (pat < 0)
1494
                        return pat;
1495
66705
                    block_cbp |= pat << (i << 2);
1496

66705
                    if (!v->ttmbf && ttmb < 8)
1497
44004
                        ttmb = -1;
1498
66705
                    first_block = 0;
1499
                }
1500
            }
1501
        } else { // skipped MB
1502
3721
            s->mb_intra                               = 0;
1503
3721
            s->current_picture.qscale_table[mb_pos] = 0;
1504
26047
            for (i = 0; i < 6; i++) {
1505
22326
                v->mb_type[0][s->block_index[i]] = 0;
1506
22326
                s->dc_val[0][s->block_index[i]]  = 0;
1507
            }
1508
18605
            for (i = 0; i < 4; i++) {
1509
14884
                ff_vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1510
14884
                ff_vc1_mc_4mv_luma(v, i, 0, 0);
1511
            }
1512
3721
            ff_vc1_mc_4mv_chroma(v, 0);
1513
3721
            s->current_picture.qscale_table[mb_pos] = 0;
1514
        }
1515
    }
1516
152584
end:
1517

152584
    if (v->overlap && v->pq >= 9)
1518
        ff_vc1_p_overlap_filter(v);
1519
152584
    vc1_put_blocks_clamped(v, 1);
1520
1521
152584
    v->cbp[s->mb_x]      = block_cbp;
1522
152584
    v->ttblk[s->mb_x]    = block_tt;
1523
152584
    v->is_intra[s->mb_x] = block_intra;
1524
1525
152584
    return 0;
1526
}
1527
1528
/* Decode one macroblock in an interlaced frame p picture */
1529
1530
16320
static int vc1_decode_p_mb_intfr(VC1Context *v)
1531
{
1532
16320
    MpegEncContext *s = &v->s;
1533
16320
    GetBitContext *gb = &s->gb;
1534
    int i;
1535
16320
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1536
16320
    int cbp = 0; /* cbp decoding stuff */
1537
    int mqdiff, mquant; /* MB quantization */
1538
16320
    int ttmb = v->ttfrm; /* MB Transform type */
1539
1540
16320
    int mb_has_coeffs = 1; /* last_flag */
1541
    int dmv_x, dmv_y; /* Differential MV components */
1542
    int val; /* temp value */
1543
16320
    int first_block = 1;
1544
    int dst_idx, off;
1545
16320
    int skipped, fourmv = 0, twomv = 0;
1546
16320
    int block_cbp = 0, pat, block_tt = 0;
1547
16320
    int idx_mbmode = 0, mvbp;
1548
    int fieldtx;
1549
1550
16320
    mquant = v->pq; /* Lossy initialization */
1551
1552
16320
    if (v->skip_is_raw)
1553
        skipped = get_bits1(gb);
1554
    else
1555
16320
        skipped = v->s.mbskip_table[mb_pos];
1556
16320
    if (!skipped) {
1557
16307
        if (v->fourmvswitch)
1558
16307
            idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
1559
        else
1560
            idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
1561

16307
        switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
1562
        /* store the motion vector type in a flag (useful later) */
1563
704
        case MV_PMODE_INTFR_4MV:
1564
704
            fourmv = 1;
1565
704
            v->blk_mv_type[s->block_index[0]] = 0;
1566
704
            v->blk_mv_type[s->block_index[1]] = 0;
1567
704
            v->blk_mv_type[s->block_index[2]] = 0;
1568
704
            v->blk_mv_type[s->block_index[3]] = 0;
1569
704
            break;
1570
1084
        case MV_PMODE_INTFR_4MV_FIELD:
1571
1084
            fourmv = 1;
1572
1084
            v->blk_mv_type[s->block_index[0]] = 1;
1573
1084
            v->blk_mv_type[s->block_index[1]] = 1;
1574
1084
            v->blk_mv_type[s->block_index[2]] = 1;
1575
1084
            v->blk_mv_type[s->block_index[3]] = 1;
1576
1084
            break;
1577
4797
        case MV_PMODE_INTFR_2MV_FIELD:
1578
4797
            twomv = 1;
1579
4797
            v->blk_mv_type[s->block_index[0]] = 1;
1580
4797
            v->blk_mv_type[s->block_index[1]] = 1;
1581
4797
            v->blk_mv_type[s->block_index[2]] = 1;
1582
4797
            v->blk_mv_type[s->block_index[3]] = 1;
1583
4797
            break;
1584
8046
        case MV_PMODE_INTFR_1MV:
1585
8046
            v->blk_mv_type[s->block_index[0]] = 0;
1586
8046
            v->blk_mv_type[s->block_index[1]] = 0;
1587
8046
            v->blk_mv_type[s->block_index[2]] = 0;
1588
8046
            v->blk_mv_type[s->block_index[3]] = 0;
1589
8046
            break;
1590
        }
1591
16307
        if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
1592
8380
            for (i = 0; i < 4; i++) {
1593
6704
                s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
1594
6704
                s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
1595
            }
1596
1676
            v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
1597
1676
            s->mb_intra          = 1;
1598
1676
            s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
1599
1676
            fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
1600
1676
            mb_has_coeffs = get_bits1(gb);
1601
1676
            if (mb_has_coeffs)
1602
1615
                cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1603
1676
            v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
1604









1676
            GET_MQUANT();
1605
1676
            s->current_picture.qscale_table[mb_pos] = mquant;
1606
            /* Set DC scale - y and c use the same (not sure if necessary here) */
1607
1676
            s->y_dc_scale = s->y_dc_scale_table[FFABS(mquant)];
1608
1676
            s->c_dc_scale = s->c_dc_scale_table[FFABS(mquant)];
1609
1676
            dst_idx = 0;
1610
11732
            for (i = 0; i < 6; i++) {
1611
10056
                v->a_avail = v->c_avail          = 0;
1612
10056
                v->mb_type[0][s->block_index[i]] = 1;
1613
10056
                s->dc_val[0][s->block_index[i]]  = 0;
1614
10056
                dst_idx += i >> 2;
1615
10056
                val = ((cbp >> (5 - i)) & 1);
1616

10056
                if (i == 2 || i == 3 || !s->first_slice_line)
1617
9840
                    v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1618

10056
                if (i == 1 || i == 3 || s->mb_x)
1619
10000
                    v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1620
1621
10056
                vc1_decode_intra_block(v, v->block[v->cur_blk_idx][block_map[i]], i, val, mquant,
1622
10056
                                       (i & 4) ? v->codingset2 : v->codingset);
1623
                if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1624
                    continue;
1625
10056
                v->vc1dsp.vc1_inv_trans_8x8(v->block[v->cur_blk_idx][block_map[i]]);
1626
10056
                if (i < 4)
1627
6704
                    off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
1628
                else
1629
3352
                    off = 0;
1630
10056
                block_cbp |= 0xf << (i << 2);
1631
            }
1632
1633
        } else { // inter MB
1634
14631
            mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
1635
14631
            if (mb_has_coeffs)
1636
13330
                cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1637
14631
            if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
1638
4797
                v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
1639
            } else {
1640
9834
                if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
1641
9130
                    || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
1642
1788
                    v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
1643
                }
1644
            }
1645
14631
            s->mb_intra = v->is_intra[s->mb_x] = 0;
1646
102417
            for (i = 0; i < 6; i++)
1647
87786
                v->mb_type[0][s->block_index[i]] = 0;
1648
14631
            fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
1649
            /* for all motion vector read MVDATA and motion compensate each block */
1650
14631
            dst_idx = 0;
1651
14631
            if (fourmv) {
1652
1788
                mvbp = v->fourmvbp;
1653
8940
                for (i = 0; i < 4; i++) {
1654
7152
                    dmv_x = dmv_y = 0;
1655
7152
                    if (mvbp & (8 >> i))
1656
5798
                        get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1657
7152
                    ff_vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0);
1658
7152
                    ff_vc1_mc_4mv_luma(v, i, 0, 0);
1659
                }
1660
1788
                ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
1661
12843
            } else if (twomv) {
1662
4797
                mvbp  = v->twomvbp;
1663
4797
                dmv_x = dmv_y = 0;
1664
4797
                if (mvbp & 2) {
1665
4513
                    get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1666
                }
1667
4797
                ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
1668
4797
                ff_vc1_mc_4mv_luma(v, 0, 0, 0);
1669
4797
                ff_vc1_mc_4mv_luma(v, 1, 0, 0);
1670
4797
                dmv_x = dmv_y = 0;
1671
4797
                if (mvbp & 1) {
1672
4517
                    get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1673
                }
1674
4797
                ff_vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
1675
4797
                ff_vc1_mc_4mv_luma(v, 2, 0, 0);
1676
4797
                ff_vc1_mc_4mv_luma(v, 3, 0, 0);
1677
4797
                ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
1678
            } else {
1679
8046
                mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
1680
8046
                dmv_x = dmv_y = 0;
1681
8046
                if (mvbp) {
1682
7892
                    get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1683
                }
1684
8046
                ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
1685
8046
                ff_vc1_mc_1mv(v, 0);
1686
            }
1687
14631
            if (cbp)
1688









13330
                GET_MQUANT();  // p. 227
1689
14631
            s->current_picture.qscale_table[mb_pos] = mquant;
1690

14631
            if (!v->ttmbf && cbp)
1691
13330
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1692
102417
            for (i = 0; i < 6; i++) {
1693
87786
                s->dc_val[0][s->block_index[i]] = 0;
1694
87786
                dst_idx += i >> 2;
1695
87786
                val = ((cbp >> (5 - i)) & 1);
1696
87786
                if (!fieldtx)
1697
70050
                    off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1698
                else
1699

17736
                    off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
1700
87786
                if (val) {
1701
190881
                    pat = vc1_decode_p_block(v, v->block[v->cur_blk_idx][block_map[i]], i, mquant, ttmb,
1702
63627
                                             first_block, s->dest[dst_idx] + off,
1703
63627
                                             (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
1704
                                             CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
1705
63627
                    if (pat < 0)
1706
                        return pat;
1707
63627
                    block_cbp |= pat << (i << 2);
1708

63627
                    if (!v->ttmbf && ttmb < 8)
1709
44047
                        ttmb = -1;
1710
63627
                    first_block = 0;
1711
                }
1712
            }
1713
        }
1714
    } else { // skipped
1715
13
        s->mb_intra = v->is_intra[s->mb_x] = 0;
1716
91
        for (i = 0; i < 6; i++) {
1717
78
            v->mb_type[0][s->block_index[i]] = 0;
1718
78
            s->dc_val[0][s->block_index[i]] = 0;
1719
        }
1720
13
        s->current_picture.mb_type[mb_pos]      = MB_TYPE_SKIP;
1721
13
        s->current_picture.qscale_table[mb_pos] = 0;
1722
13
        v->blk_mv_type[s->block_index[0]] = 0;
1723
13
        v->blk_mv_type[s->block_index[1]] = 0;
1724
13
        v->blk_mv_type[s->block_index[2]] = 0;
1725
13
        v->blk_mv_type[s->block_index[3]] = 0;
1726
13
        ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
1727
13
        ff_vc1_mc_1mv(v, 0);
1728
13
        v->fieldtx_plane[mb_pos] = 0;
1729
    }
1730

16320
    if (v->overlap && v->pq >= 9)
1731
        ff_vc1_p_overlap_filter(v);
1732
16320
    vc1_put_blocks_clamped(v, 1);
1733
1734
16320
    v->cbp[s->mb_x]      = block_cbp;
1735
16320
    v->ttblk[s->mb_x]    = block_tt;
1736
1737
16320
    return 0;
1738
}
1739
1740
37245
static int vc1_decode_p_mb_intfi(VC1Context *v)
1741
{
1742
37245
    MpegEncContext *s = &v->s;
1743
37245
    GetBitContext *gb = &s->gb;
1744
    int i;
1745
37245
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1746
37245
    int cbp = 0; /* cbp decoding stuff */
1747
    int mqdiff, mquant; /* MB quantization */
1748
37245
    int ttmb = v->ttfrm; /* MB Transform type */
1749
1750
37245
    int mb_has_coeffs = 1; /* last_flag */
1751
    int dmv_x, dmv_y; /* Differential MV components */
1752
    int val; /* temp values */
1753
37245
    int first_block = 1;
1754
    int dst_idx, off;
1755
37245
    int pred_flag = 0;
1756
37245
    int block_cbp = 0, pat, block_tt = 0;
1757
37245
    int idx_mbmode = 0;
1758
1759
37245
    mquant = v->pq; /* Lossy initialization */
1760
1761
37245
    idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
1762
37245
    if (idx_mbmode <= 1) { // intra MB
1763
9990
        v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
1764
9990
        s->mb_intra          = 1;
1765
9990
        s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
1766
9990
        s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
1767
9990
        s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
1768









9990
        GET_MQUANT();
1769
9990
        s->current_picture.qscale_table[mb_pos] = mquant;
1770
        /* Set DC scale - y and c use the same (not sure if necessary here) */
1771
9990
        s->y_dc_scale = s->y_dc_scale_table[FFABS(mquant)];
1772
9990
        s->c_dc_scale = s->c_dc_scale_table[FFABS(mquant)];
1773
9990
        v->s.ac_pred  = v->acpred_plane[mb_pos] = get_bits1(gb);
1774
9990
        mb_has_coeffs = idx_mbmode & 1;
1775
9990
        if (mb_has_coeffs)
1776
9243
            cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
1777
9990
        dst_idx = 0;
1778
69930
        for (i = 0; i < 6; i++) {
1779
59940
            v->a_avail = v->c_avail          = 0;
1780
59940
            v->mb_type[0][s->block_index[i]] = 1;
1781
59940
            s->dc_val[0][s->block_index[i]]  = 0;
1782
59940
            dst_idx += i >> 2;
1783
59940
            val = ((cbp >> (5 - i)) & 1);
1784

59940
            if (i == 2 || i == 3 || !s->first_slice_line)
1785
57452
                v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1786

59940
            if (i == 1 || i == 3 || s->mb_x)
1787
59200
                v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1788
1789
59940
            vc1_decode_intra_block(v, v->block[v->cur_blk_idx][block_map[i]], i, val, mquant,
1790
59940
                                   (i & 4) ? v->codingset2 : v->codingset);
1791
            if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1792
                continue;
1793
59940
            v->vc1dsp.vc1_inv_trans_8x8(v->block[v->cur_blk_idx][block_map[i]]);
1794
59940
            off  = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1795
59940
            block_cbp |= 0xf << (i << 2);
1796
        }
1797
    } else {
1798
27255
        s->mb_intra = v->is_intra[s->mb_x] = 0;
1799
27255
        s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
1800
190785
        for (i = 0; i < 6; i++)
1801
163530
            v->mb_type[0][s->block_index[i]] = 0;
1802
27255
        if (idx_mbmode <= 5) { // 1-MV
1803
20405
            dmv_x = dmv_y = pred_flag = 0;
1804
20405
            if (idx_mbmode & 1) {
1805
15790
                get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
1806
            }
1807
20405
            ff_vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
1808
20405
            ff_vc1_mc_1mv(v, 0);
1809
20405
            mb_has_coeffs = !(idx_mbmode & 2);
1810
        } else { // 4-MV
1811
6850
            v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
1812
34250
            for (i = 0; i < 4; i++) {
1813
27400
                dmv_x = dmv_y = pred_flag = 0;
1814
27400
                if (v->fourmvbp & (8 >> i))
1815
8342
                    get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
1816
27400
                ff_vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
1817
27400
                ff_vc1_mc_4mv_luma(v, i, 0, 0);
1818
            }
1819
6850
            ff_vc1_mc_4mv_chroma(v, 0);
1820
6850
            mb_has_coeffs = idx_mbmode & 1;
1821
        }
1822
27255
        if (mb_has_coeffs)
1823
20418
            cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1824
27255
        if (cbp) {
1825









20418
            GET_MQUANT();
1826
        }
1827
27255
        s->current_picture.qscale_table[mb_pos] = mquant;
1828

27255
        if (!v->ttmbf && cbp) {
1829
20418
            ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1830
        }
1831
27255
        dst_idx = 0;
1832
190785
        for (i = 0; i < 6; i++) {
1833
163530
            s->dc_val[0][s->block_index[i]] = 0;
1834
163530
            dst_idx += i >> 2;
1835
163530
            val = ((cbp >> (5 - i)) & 1);
1836
163530
            off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
1837
163530
            if (val) {
1838
268404
                pat = vc1_decode_p_block(v, v->block[v->cur_blk_idx][block_map[i]], i, mquant, ttmb,
1839
89468
                                         first_block, s->dest[dst_idx] + off,
1840
89468
                                         (i & 4) ? s->uvlinesize : s->linesize,
1841
                                         CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY),
1842
                                         &block_tt);
1843
89468
                if (pat < 0)
1844
                    return pat;
1845
89468
                block_cbp |= pat << (i << 2);
1846

89468
                if (!v->ttmbf && ttmb < 8)
1847
58173
                    ttmb = -1;
1848
89468
                first_block = 0;
1849
            }
1850
        }
1851
    }
1852

37245
    if (v->overlap && v->pq >= 9)
1853
        ff_vc1_p_overlap_filter(v);
1854
37245
    vc1_put_blocks_clamped(v, 1);
1855
1856
37245
    v->cbp[s->mb_x]      = block_cbp;
1857
37245
    v->ttblk[s->mb_x]    = block_tt;
1858
1859
37245
    return 0;
1860
}
1861
1862
/** Decode one B-frame MB (in Main profile)
1863
 */
1864
20940
static int vc1_decode_b_mb(VC1Context *v)
1865
{
1866
20940
    MpegEncContext *s = &v->s;
1867
20940
    GetBitContext *gb = &s->gb;
1868
    int i, j;
1869
20940
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1870
20940
    int cbp = 0; /* cbp decoding stuff */
1871
    int mqdiff, mquant; /* MB quantization */
1872
20940
    int ttmb = v->ttfrm; /* MB Transform type */
1873
20940
    int mb_has_coeffs = 0; /* last_flag */
1874
    int index, index1; /* LUT indexes */
1875
    int val, sign; /* temp values */
1876
20940
    int first_block = 1;
1877
    int dst_idx, off;
1878
    int skipped, direct;
1879
    int dmv_x[2], dmv_y[2];
1880
20940
    int bmvtype = BMV_TYPE_BACKWARD;
1881
1882
20940
    mquant      = v->pq; /* lossy initialization */
1883
20940
    s->mb_intra = 0;
1884
1885
20940
    if (v->dmb_is_raw)
1886
        direct = get_bits1(gb);
1887
    else
1888
20940
        direct = v->direct_mb_plane[mb_pos];
1889
20940
    if (v->skip_is_raw)
1890
        skipped = get_bits1(gb);
1891
    else
1892
20940
        skipped = v->s.mbskip_table[mb_pos];
1893
1894
20940
    dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
1895
146580
    for (i = 0; i < 6; i++) {
1896
125640
        v->mb_type[0][s->block_index[i]] = 0;
1897
125640
        s->dc_val[0][s->block_index[i]]  = 0;
1898
    }
1899
20940
    s->current_picture.qscale_table[mb_pos] = 0;
1900
1901
20940
    if (!direct) {
1902
18371
        if (!skipped) {
1903





15906
            GET_MVDATA(dmv_x[0], dmv_y[0]);
1904
15906
            dmv_x[1] = dmv_x[0];
1905
15906
            dmv_y[1] = dmv_y[0];
1906
        }
1907

18371
        if (skipped || !s->mb_intra) {
1908
18201
            bmvtype = decode012(gb);
1909

18201
            switch (bmvtype) {
1910
8021
            case 0:
1911
8021
                bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
1912
8021
                break;
1913
4023
            case 1:
1914
4023
                bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
1915
4023
                break;
1916
6157
            case 2:
1917
6157
                bmvtype  = BMV_TYPE_INTERPOLATED;
1918
6157
                dmv_x[0] = dmv_y[0] = 0;
1919
            }
1920
2739
        }
1921
    }
1922
146580
    for (i = 0; i < 6; i++)
1923
125640
        v->mb_type[0][s->block_index[i]] = s->mb_intra;
1924
1925
20940
    if (skipped) {
1926
3578
        if (direct)
1927
1113
            bmvtype = BMV_TYPE_INTERPOLATED;
1928
3578
        ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1929
3578
        vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1930
3578
        return 0;
1931
    }
1932
17362
    if (direct) {
1933
1456
        cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1934









1456
        GET_MQUANT();
1935
1456
        s->mb_intra = 0;
1936
1456
        s->current_picture.qscale_table[mb_pos] = mquant;
1937
1456
        if (!v->ttmbf)
1938
1456
            ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1939
1456
        dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
1940
1456
        ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1941
1456
        vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1942
    } else {
1943

15906
        if (!mb_has_coeffs && !s->mb_intra) {
1944
            /* no coded blocks - effectively skipped */
1945
1035
            ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1946
1035
            vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1947
1035
            return 0;
1948
        }
1949

14871
        if (s->mb_intra && !mb_has_coeffs) {
1950
            GET_MQUANT();
1951
            s->current_picture.qscale_table[mb_pos] = mquant;
1952
            s->ac_pred = get_bits1(gb);
1953
            cbp = 0;
1954
            ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1955
        } else {
1956
14871
            if (bmvtype == BMV_TYPE_INTERPOLATED) {
1957





5289
                GET_MVDATA(dmv_x[0], dmv_y[0]);
1958
5289
                if (!mb_has_coeffs) {
1959
                    /* interpolated skipped block */
1960
475
                    ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1961
475
                    vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1962
475
                    return 0;
1963
                }
1964
            }
1965
14396
            ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1966
14396
            if (!s->mb_intra) {
1967
14226
                vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1968
            }
1969
14396
            if (s->mb_intra)
1970
170
                s->ac_pred = get_bits1(gb);
1971
14396
            cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1972









14396
            GET_MQUANT();
1973
14396
            s->current_picture.qscale_table[mb_pos] = mquant;
1974

14396
            if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
1975
14226
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1976
        }
1977
    }
1978
15852
    dst_idx = 0;
1979
110964
    for (i = 0; i < 6; i++) {
1980
95112
        s->dc_val[0][s->block_index[i]] = 0;
1981
95112
        dst_idx += i >> 2;
1982
95112
        val = ((cbp >> (5 - i)) & 1);
1983
95112
        off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1984
95112
        v->mb_type[0][s->block_index[i]] = s->mb_intra;
1985
95112
        if (s->mb_intra) {
1986
            /* check if prediction blocks A and C are available */
1987
1020
            v->a_avail = v->c_avail = 0;
1988

1020
            if (i == 2 || i == 3 || !s->first_slice_line)
1989
996
                v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1990

1020
            if (i == 1 || i == 3 || s->mb_x)
1991
992
                v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1992
1993
1020
            vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1994
1020
                                   (i & 4) ? v->codingset2 : v->codingset);
1995
            if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1996
                continue;
1997
1020
            v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1998
1020
            if (v->rangeredfrm)
1999
                for (j = 0; j < 64; j++)
2000
                    s->block[i][j] *= 2;
2001
1020
            s->idsp.put_signed_pixels_clamped(s->block[i],
2002
1020
                                              s->dest[dst_idx] + off,
2003
1020
                                              i & 4 ? s->uvlinesize
2004
                                                    : s->linesize);
2005
94092
        } else if (val) {
2006
172395
            int pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2007
57465
                                         first_block, s->dest[dst_idx] + off,
2008
57465
                                         (i & 4) ? s->uvlinesize : s->linesize,
2009
                                         CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), NULL);
2010
57465
            if (pat < 0)
2011
                return pat;
2012

57465
            if (!v->ttmbf && ttmb < 8)
2013
54531
                ttmb = -1;
2014
57465
            first_block = 0;
2015
        }
2016
    }
2017
15852
    return 0;
2018
}
2019
2020
/** Decode one B-frame MB (in interlaced field B picture)
2021
 */
2022
51540
static int vc1_decode_b_mb_intfi(VC1Context *v)
2023
{
2024
51540
    MpegEncContext *s = &v->s;
2025
51540
    GetBitContext *gb = &s->gb;
2026
    int i, j;
2027
51540
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2028
51540
    int cbp = 0; /* cbp decoding stuff */
2029
    int mqdiff, mquant; /* MB quantization */
2030
51540
    int ttmb = v->ttfrm; /* MB Transform type */
2031
51540
    int mb_has_coeffs = 0; /* last_flag */
2032
    int val; /* temp value */
2033
51540
    int first_block = 1;
2034
    int dst_idx, off;
2035
    int fwd;
2036
    int dmv_x[2], dmv_y[2], pred_flag[2];
2037
51540
    int bmvtype = BMV_TYPE_BACKWARD;
2038
51540
    int block_cbp = 0, pat, block_tt = 0;
2039
    int idx_mbmode;
2040
2041
51540
    mquant      = v->pq; /* Lossy initialization */
2042
51540
    s->mb_intra = 0;
2043
2044
51540
    idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
2045
51540
    if (idx_mbmode <= 1) { // intra MB
2046
48
        v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
2047
48
        s->mb_intra          = 1;
2048
48
        s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2049
48
        s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2050
48
        s->current_picture.mb_type[mb_pos + v->mb_off]         = MB_TYPE_INTRA;
2051









48
        GET_MQUANT();
2052
48
        s->current_picture.qscale_table[mb_pos] = mquant;
2053
        /* Set DC scale - y and c use the same (not sure if necessary here) */
2054
48
        s->y_dc_scale = s->y_dc_scale_table[FFABS(mquant)];
2055
48
        s->c_dc_scale = s->c_dc_scale_table[FFABS(mquant)];
2056
48
        v->s.ac_pred  = v->acpred_plane[mb_pos] = get_bits1(gb);
2057
48
        mb_has_coeffs = idx_mbmode & 1;
2058
48
        if (mb_has_coeffs)
2059
33
            cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
2060
48
        dst_idx = 0;
2061
336
        for (i = 0; i < 6; i++) {
2062
288
            v->a_avail = v->c_avail          = 0;
2063
288
            v->mb_type[0][s->block_index[i]] = 1;
2064
288
            s->dc_val[0][s->block_index[i]]  = 0;
2065
288
            dst_idx += i >> 2;
2066
288
            val = ((cbp >> (5 - i)) & 1);
2067

288
            if (i == 2 || i == 3 || !s->first_slice_line)
2068
288
                v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2069

288
            if (i == 1 || i == 3 || s->mb_x)
2070
284
                v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2071
2072
288
            vc1_decode_intra_block(v, s->block[i], i, val, mquant,
2073
288
                                   (i & 4) ? v->codingset2 : v->codingset);
2074
            if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2075
                continue;
2076
288
            v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2077
288
            if (v->rangeredfrm)
2078
                for (j = 0; j < 64; j++)
2079
                    s->block[i][j] <<= 1;
2080
288
            off  = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2081
288
            s->idsp.put_signed_pixels_clamped(s->block[i],
2082
288
                                              s->dest[dst_idx] + off,
2083
288
                                              (i & 4) ? s->uvlinesize
2084
                                                      : s->linesize);
2085
        }
2086
    } else {
2087
51492
        s->mb_intra = v->is_intra[s->mb_x] = 0;
2088
51492
        s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
2089
360444
        for (i = 0; i < 6; i++)
2090
308952
            v->mb_type[0][s->block_index[i]] = 0;
2091
51492
        if (v->fmb_is_raw)
2092
675
            fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
2093
        else
2094
50817
            fwd = v->forward_mb_plane[mb_pos];
2095
51492
        if (idx_mbmode <= 5) { // 1-MV
2096
42084
            int interpmvp = 0;
2097
42084
            dmv_x[0]     = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
2098
42084
            pred_flag[0] = pred_flag[1] = 0;
2099
42084
            if (fwd)
2100
15503
                bmvtype = BMV_TYPE_FORWARD;
2101
            else {
2102
26581
                bmvtype = decode012(gb);
2103

26581
                switch (bmvtype) {
2104
13925
                case 0:
2105
13925
                    bmvtype = BMV_TYPE_BACKWARD;
2106
13925
                    break;
2107
6770
                case 1:
2108
6770
                    bmvtype = BMV_TYPE_DIRECT;
2109
6770
                    break;
2110
5886
                case 2:
2111
5886
                    bmvtype   = BMV_TYPE_INTERPOLATED;
2112
5886
                    interpmvp = get_bits1(gb);
2113
                }
2114
42084
            }
2115
42084
            v->bmvtype = bmvtype;
2116

42084
            if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
2117
20242
                get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
2118
            }
2119
42084
            if (interpmvp) {
2120
4685
                get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
2121
            }
2122
42084
            if (bmvtype == BMV_TYPE_DIRECT) {
2123
6770
                dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
2124
6770
                dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
2125
6770
                if (!s->next_picture_ptr->field_picture) {
2126
                    av_log(s->avctx, AV_LOG_ERROR, "Mixed field/frame direct mode not supported\n");
2127
                    return AVERROR_INVALIDDATA;
2128
                }
2129
            }
2130
42084
            ff_vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
2131
42084
            vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
2132
42084
            mb_has_coeffs = !(idx_mbmode & 2);
2133
        } else { // 4-MV
2134
9408
            if (fwd)
2135
4892
                bmvtype = BMV_TYPE_FORWARD;
2136
9408
            v->bmvtype  = bmvtype;
2137
9408
            v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
2138
47040
            for (i = 0; i < 4; i++) {
2139
37632
                dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
2140
37632
                dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
2141
37632
                if (v->fourmvbp & (8 >> i)) {
2142
10050
                    get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
2143
10050
                                             &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
2144
10050
                                         &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
2145
                }
2146
37632
                ff_vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
2147
37632
                ff_vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD, 0);
2148
            }
2149
9408
            ff_vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
2150
9408
            mb_has_coeffs = idx_mbmode & 1;
2151
        }
2152
51492
        if (mb_has_coeffs)
2153
30972
            cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2154
51492
        if (cbp) {
2155









30972
            GET_MQUANT();
2156
        }
2157
51492
        s->current_picture.qscale_table[mb_pos] = mquant;
2158

51492
        if (!v->ttmbf && cbp) {
2159
30972
            ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2160
        }
2161
51492
        dst_idx = 0;
2162
360444
        for (i = 0; i < 6; i++) {
2163
308952
            s->dc_val[0][s->block_index[i]] = 0;
2164
308952
            dst_idx += i >> 2;
2165
308952
            val = ((cbp >> (5 - i)) & 1);
2166
308952
            off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
2167
308952
            if (val) {
2168
274410
                pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2169
91470
                                         first_block, s->dest[dst_idx] + off,
2170
91470
                                         (i & 4) ? s->uvlinesize : s->linesize,
2171
                                         CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
2172
91470
                if (pat < 0)
2173
                    return pat;
2174
91470
                block_cbp |= pat << (i << 2);
2175

91470
                if (!v->ttmbf && ttmb < 8)
2176
69969
                    ttmb = -1;
2177
91470
                first_block = 0;
2178
            }
2179
        }
2180
    }
2181
51540
    v->cbp[s->mb_x]      = block_cbp;
2182
51540
    v->ttblk[s->mb_x]    = block_tt;
2183
2184
51540
    return 0;
2185
}
2186
2187
/** Decode one B-frame MB (in interlaced frame B picture)
2188
 */
2189
32640
static int vc1_decode_b_mb_intfr(VC1Context *v)
2190
{
2191
32640
    MpegEncContext *s = &v->s;
2192
32640
    GetBitContext *gb = &s->gb;
2193
    int i, j;
2194
32640
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2195
32640
    int cbp = 0; /* cbp decoding stuff */
2196
    int mqdiff, mquant; /* MB quantization */
2197
32640
    int ttmb = v->ttfrm; /* MB Transform type */
2198
32640
    int mvsw = 0; /* motion vector switch */
2199
32640
    int mb_has_coeffs = 1; /* last_flag */
2200
    int dmv_x, dmv_y; /* Differential MV components */
2201
    int val; /* temp value */
2202
32640
    int first_block = 1;
2203
    int dst_idx, off;
2204
32640
    int skipped, direct, twomv = 0;
2205
32640
    int block_cbp = 0, pat, block_tt = 0;
2206
32640
    int idx_mbmode = 0, mvbp;
2207
    int stride_y, fieldtx;
2208
32640
    int bmvtype = BMV_TYPE_BACKWARD;
2209
    int dir, dir2;
2210
2211
32640
    mquant = v->pq; /* Lossy initialization */
2212
32640
    s->mb_intra = 0;
2213
32640
    if (v->skip_is_raw)
2214
        skipped = get_bits1(gb);
2215
    else
2216
32640
        skipped = v->s.mbskip_table[mb_pos];
2217
2218
32640
    if (!skipped) {
2219
31098
        idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2);
2220
31098
        if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
2221
17446
            twomv = 1;
2222
17446
            v->blk_mv_type[s->block_index[0]] = 1;
2223
17446
            v->blk_mv_type[s->block_index[1]] = 1;
2224
17446
            v->blk_mv_type[s->block_index[2]] = 1;
2225
17446
            v->blk_mv_type[s->block_index[3]] = 1;
2226
        } else {
2227
13652
            v->blk_mv_type[s->block_index[0]] = 0;
2228
13652
            v->blk_mv_type[s->block_index[1]] = 0;
2229
13652
            v->blk_mv_type[s->block_index[2]] = 0;
2230
13652
            v->blk_mv_type[s->block_index[3]] = 0;
2231
        }
2232
    }
2233
2234
32640
    if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
2235
505
        for (i = 0; i < 4; i++) {
2236
404
            s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = 0;
2237
404
            s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = 0;
2238
404
            s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
2239
404
            s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
2240
        }
2241
101
        v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
2242
101
        s->mb_intra          = 1;
2243
101
        s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2244
101
        fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
2245
101
        mb_has_coeffs = get_bits1(gb);
2246
101
        if (mb_has_coeffs)
2247
101
            cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2248
101
        v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
2249









101
        GET_MQUANT();
2250
101
        s->current_picture.qscale_table[mb_pos] = mquant;
2251
        /* Set DC scale - y and c use the same (not sure if necessary here) */
2252
101
        s->y_dc_scale = s->y_dc_scale_table[FFABS(mquant)];
2253
101
        s->c_dc_scale = s->c_dc_scale_table[FFABS(mquant)];
2254
101
        dst_idx = 0;
2255
707
        for (i = 0; i < 6; i++) {
2256
606
            v->a_avail = v->c_avail          = 0;
2257
606
            v->mb_type[0][s->block_index[i]] = 1;
2258
606
            s->dc_val[0][s->block_index[i]]  = 0;
2259
606
            dst_idx += i >> 2;
2260
606
            val = ((cbp >> (5 - i)) & 1);
2261

606
            if (i == 2 || i == 3 || !s->first_slice_line)
2262
574
                v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2263

606
            if (i == 1 || i == 3 || s->mb_x)
2264
594
                v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2265
2266
606
            vc1_decode_intra_block(v, s->block[i], i, val, mquant,
2267
606
                                   (i & 4) ? v->codingset2 : v->codingset);
2268
            if (CONFIG_GRAY && i > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2269
                continue;
2270
606
            v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2271
606
            if (i < 4) {
2272
404
                stride_y = s->linesize << fieldtx;
2273
404
                off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
2274
            } else {
2275
202
                stride_y = s->uvlinesize;
2276
202
                off = 0;
2277
            }
2278
606
            s->idsp.put_signed_pixels_clamped(s->block[i],
2279
606
                                              s->dest[dst_idx] + off,
2280
                                              stride_y);
2281
        }
2282
    } else {
2283
32539
        s->mb_intra = v->is_intra[s->mb_x] = 0;
2284
2285
32539
        if (v->dmb_is_raw)
2286
            direct = get_bits1(gb);
2287
        else
2288
32539
            direct = v->direct_mb_plane[mb_pos];
2289
2290
32539
        if (direct) {
2291
8474
            if (s->next_picture_ptr->field_picture)
2292
                av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
2293
8474
            s->mv[0][0][0] = s->current_picture.motion_val[0][s->block_index[0]][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][0], v->bfraction, 0, s->quarter_sample);
2294
8474
            s->mv[0][0][1] = s->current_picture.motion_val[0][s->block_index[0]][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][1], v->bfraction, 0, s->quarter_sample);
2295
8474
            s->mv[1][0][0] = s->current_picture.motion_val[1][s->block_index[0]][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][0], v->bfraction, 1, s->quarter_sample);
2296
8474
            s->mv[1][0][1] = s->current_picture.motion_val[1][s->block_index[0]][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][1], v->bfraction, 1, s->quarter_sample);
2297
2298
8474
            if (twomv) {
2299
4360
                s->mv[0][2][0] = s->current_picture.motion_val[0][s->block_index[2]][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[2]][0], v->bfraction, 0, s->quarter_sample);
2300
4360
                s->mv[0][2][1] = s->current_picture.motion_val[0][s->block_index[2]][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[2]][1], v->bfraction, 0, s->quarter_sample);
2301
4360
                s->mv[1][2][0] = s->current_picture.motion_val[1][s->block_index[2]][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[2]][0], v->bfraction, 1, s->quarter_sample);
2302
4360
                s->mv[1][2][1] = s->current_picture.motion_val[1][s->block_index[2]][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[2]][1], v->bfraction, 1, s->quarter_sample);
2303
2304
13080
                for (i = 1; i < 4; i += 2) {
2305
8720
                    s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][i-1][0];
2306
8720
                    s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][i-1][1];
2307
8720
                    s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][i-1][0];
2308
8720
                    s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][i-1][1];
2309
                }
2310
            } else {
2311
16456
                for (i = 1; i < 4; i++) {
2312
12342
                    s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][0][0];
2313
12342
                    s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][0][1];
2314
12342
                    s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][0][0];
2315
12342
                    s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][0][1];
2316
                }
2317
            }
2318
        }
2319
2320
32539
        if (!direct) {
2321

24065
            if (skipped || !s->mb_intra) {
2322
24065
                bmvtype = decode012(gb);
2323

24065
                switch (bmvtype) {
2324
14341
                case 0:
2325
14341
                    bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
2326
14341
                    break;
2327
6232
                case 1:
2328
6232
                    bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
2329
6232
                    break;
2330
3492
                case 2:
2331
3492
                    bmvtype  = BMV_TYPE_INTERPOLATED;
2332
                }
2333
            }
2334
2335

24065
            if (twomv && bmvtype != BMV_TYPE_INTERPOLATED)
2336
12364
                mvsw = get_bits1(gb);
2337
        }
2338
2339
32539
        if (!skipped) { // inter MB
2340
30997
            mb_has_coeffs = ff_vc1_mbmode_intfrp[0][idx_mbmode][3];
2341
30997
            if (mb_has_coeffs)
2342
26859
                cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2343
30997
            if (!direct) {
2344

23850
                if (bmvtype == BMV_TYPE_INTERPOLATED && twomv) {
2345
722
                    v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
2346

23128
                } else if (bmvtype == BMV_TYPE_INTERPOLATED || twomv) {
2347
15125
                    v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
2348
                }
2349
            }
2350
2351
216979
            for (i = 0; i < 6; i++)
2352
185982
                v->mb_type[0][s->block_index[i]] = 0;
2353
30997
            fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[0][idx_mbmode][1];
2354
            /* for all motion vector read MVDATA and motion compensate each block */
2355
30997
            dst_idx = 0;
2356
30997
            if (direct) {
2357
7147
                if (twomv) {
2358
21800
                    for (i = 0; i < 4; i++) {
2359
17440
                        ff_vc1_mc_4mv_luma(v, i, 0, 0);
2360
17440
                        ff_vc1_mc_4mv_luma(v, i, 1, 1);
2361
                    }
2362
4360
                    ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
2363
4360
                    ff_vc1_mc_4mv_chroma4(v, 1, 1, 1);
2364
                } else {
2365
2787
                    ff_vc1_mc_1mv(v, 0);
2366
2787
                    ff_vc1_interp_mc(v);
2367
                }
2368

23850
            } else if (twomv && bmvtype == BMV_TYPE_INTERPOLATED) {
2369
722
                mvbp = v->fourmvbp;
2370
3610
                for (i = 0; i < 4; i++) {
2371

2888
                    dir = i==1 || i==3;
2372
2888
                    dmv_x = dmv_y = 0;
2373
2888
                    val = ((mvbp >> (3 - i)) & 1);
2374
2888
                    if (val)
2375
2477
                        get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2376
2888
                    j = i > 1 ? 2 : 0;
2377
2888
                    ff_vc1_pred_mv_intfr(v, j, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
2378
2888
                    ff_vc1_mc_4mv_luma(v, j, dir, dir);
2379
2888
                    ff_vc1_mc_4mv_luma(v, j+1, dir, dir);
2380
                }
2381
2382
722
                ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
2383
722
                ff_vc1_mc_4mv_chroma4(v, 1, 1, 1);
2384
23128
            } else if (bmvtype == BMV_TYPE_INTERPOLATED) {
2385
2761
                mvbp = v->twomvbp;
2386
2761
                dmv_x = dmv_y = 0;
2387
2761
                if (mvbp & 2)
2388
2691
                    get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2389
2390
2761
                ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
2391
2761
                ff_vc1_mc_1mv(v, 0);
2392
2393
2761
                dmv_x = dmv_y = 0;
2394
2761
                if (mvbp & 1)
2395
2648
                    get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2396
2397
2761
                ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 1);
2398
2761
                ff_vc1_interp_mc(v);
2399
20367
            } else if (twomv) {
2400
12364
                dir = bmvtype == BMV_TYPE_BACKWARD;
2401
12364
                dir2 = dir;
2402
12364
                if (mvsw)
2403
5853
                    dir2 = !dir;
2404
12364
                mvbp = v->twomvbp;
2405
12364
                dmv_x = dmv_y = 0;
2406
12364
                if (mvbp & 2)
2407
10627
                    get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2408
12364
                ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
2409
2410
12364
                dmv_x = dmv_y = 0;
2411
12364
                if (mvbp & 1)
2412
10929
                    get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2413
12364
                ff_vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir2);
2414
2415
12364
                if (mvsw) {
2416
17559
                    for (i = 0; i < 2; i++) {
2417
11706
                        s->mv[dir][i+2][0] = s->mv[dir][i][0] = s->current_picture.motion_val[dir][s->block_index[i+2]][0] = s->current_picture.motion_val[dir][s->block_index[i]][0];
2418
11706
                        s->mv[dir][i+2][1] = s->mv[dir][i][1] = s->current_picture.motion_val[dir][s->block_index[i+2]][1] = s->current_picture.motion_val[dir][s->block_index[i]][1];
2419
11706
                        s->mv[dir2][i+2][0] = s->mv[dir2][i][0] = s->current_picture.motion_val[dir2][s->block_index[i]][0] = s->current_picture.motion_val[dir2][s->block_index[i+2]][0];
2420
11706
                        s->mv[dir2][i+2][1] = s->mv[dir2][i][1] = s->current_picture.motion_val[dir2][s->block_index[i]][1] = s->current_picture.motion_val[dir2][s->block_index[i+2]][1];
2421
                    }
2422
                } else {
2423
6511
                    ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
2424
6511
                    ff_vc1_pred_mv_intfr(v, 2, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
2425
                }
2426
2427
12364
                ff_vc1_mc_4mv_luma(v, 0, dir, 0);
2428
12364
                ff_vc1_mc_4mv_luma(v, 1, dir, 0);
2429
12364
                ff_vc1_mc_4mv_luma(v, 2, dir2, 0);
2430
12364
                ff_vc1_mc_4mv_luma(v, 3, dir2, 0);
2431
12364
                ff_vc1_mc_4mv_chroma4(v, dir, dir2, 0);
2432
            } else {
2433
8003
                dir = bmvtype == BMV_TYPE_BACKWARD;
2434
2435
8003
                mvbp = ff_vc1_mbmode_intfrp[0][idx_mbmode][2];
2436
8003
                dmv_x = dmv_y = 0;
2437
8003
                if (mvbp)
2438
7691
                    get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2439
2440
8003
                ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], dir);
2441
8003
                v->blk_mv_type[s->block_index[0]] = 1;
2442
8003
                v->blk_mv_type[s->block_index[1]] = 1;
2443
8003
                v->blk_mv_type[s->block_index[2]] = 1;
2444
8003
                v->blk_mv_type[s->block_index[3]] = 1;
2445
8003
                ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
2446
24009
                for (i = 0; i < 2; i++) {
2447
16006
                    s->mv[!dir][i+2][0] = s->mv[!dir][i][0] = s->current_picture.motion_val[!dir][s->block_index[i+2]][0] = s->current_picture.motion_val[!dir][s->block_index[i]][0];
2448
16006
                    s->mv[!dir][i+2][1] = s->mv[!dir][i][1] = s->current_picture.motion_val[!dir][s->block_index[i+2]][1] = s->current_picture.motion_val[!dir][s->block_index[i]][1];
2449
                }
2450
8003
                ff_vc1_mc_1mv(v, dir);
2451
            }
2452
2453
30997
            if (cbp)
2454









26859
                GET_MQUANT();  // p. 227
2455
30997
            s->current_picture.qscale_table[mb_pos] = mquant;
2456

30997
            if (!v->ttmbf && cbp)
2457
26859
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2458
216979
            for (i = 0; i < 6; i++) {
2459
185982
                s->dc_val[0][s->block_index[i]] = 0;
2460
185982
                dst_idx += i >> 2;
2461
185982
                val = ((cbp >> (5 - i)) & 1);
2462
185982
                if (!fieldtx)
2463
84336
                    off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2464
                else
2465

101646
                    off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
2466
185982
                if (val) {
2467
346863
                    pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2468
115621
                                             first_block, s->dest[dst_idx] + off,
2469
115621
                                             (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
2470
                                             CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
2471
115621
                    if (pat < 0)
2472
                        return pat;
2473
115621
                    block_cbp |= pat << (i << 2);
2474

115621
                    if (!v->ttmbf && ttmb < 8)
2475
92324
                        ttmb = -1;
2476
115621
                    first_block = 0;
2477
                }
2478
            }
2479
2480
        } else { // skipped
2481
1542
            dir = 0;
2482
10794
            for (i = 0; i < 6; i++) {
2483
9252
                v->mb_type[0][s->block_index[i]] = 0;
2484
9252
                s->dc_val[0][s->block_index[i]] = 0;
2485
            }
2486
1542
            s->current_picture.mb_type[mb_pos]      = MB_TYPE_SKIP;
2487
1542
            s->current_picture.qscale_table[mb_pos] = 0;
2488
1542
            v->blk_mv_type[s->block_index[0]] = 0;
2489
1542
            v->blk_mv_type[s->block_index[1]] = 0;
2490
1542
            v->blk_mv_type[s->block_index[2]] = 0;
2491
1542
            v->blk_mv_type[s->block_index[3]] = 0;
2492
2493
1542
            if (!direct) {
2494
215
                if (bmvtype == BMV_TYPE_INTERPOLATED) {
2495
9
                    ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
2496
9
                    ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 1);
2497
                } else {
2498
206
                    dir = bmvtype == BMV_TYPE_BACKWARD;
2499
206
                    ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], dir);
2500
206
                    if (mvsw) {
2501
                        int dir2 = dir;
2502
                        if (mvsw)
2503
                            dir2 = !dir;
2504
                        for (i = 0; i < 2; i++) {
2505
                            s->mv[dir][i+2][0] = s->mv[dir][i][0] = s->current_picture.motion_val[dir][s->block_index[i+2]][0] = s->current_picture.motion_val[dir][s->block_index[i]][0];
2506
                            s->mv[dir][i+2][1] = s->mv[dir][i][1] = s->current_picture.motion_val[dir][s->block_index[i+2]][1] = s->current_picture.motion_val[dir][s->block_index[i]][1];
2507
                            s->mv[dir2][i+2][0] = s->mv[dir2][i][0] = s->current_picture.motion_val[dir2][s->block_index[i]][0] = s->current_picture.motion_val[dir2][s->block_index[i+2]][0];
2508
                            s->mv[dir2][i+2][1] = s->mv[dir2][i][1] = s->current_picture.motion_val[dir2][s->block_index[i]][1] = s->current_picture.motion_val[dir2][s->block_index[i+2]][1];
2509
                        }
2510
                    } else {
2511
206
                        v->blk_mv_type[s->block_index[0]] = 1;
2512
206
                        v->blk_mv_type[s->block_index[1]] = 1;
2513
206
                        v->blk_mv_type[s->block_index[2]] = 1;
2514
206
                        v->blk_mv_type[s->block_index[3]] = 1;
2515
206
                        ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
2516
618
                        for (i = 0; i < 2; i++) {
2517
412
                            s->mv[!dir][i+2][0] = s->mv[!dir][i][0] = s->current_picture.motion_val[!dir][s->block_index[i+2]][0] = s->current_picture.motion_val[!dir][s->block_index[i]][0];
2518
412
                            s->mv[!dir][i+2][1] = s->mv[!dir][i][1] = s->current_picture.motion_val[!dir][s->block_index[i+2]][1] = s->current_picture.motion_val[!dir][s->block_index[i]][1];
2519
                        }
2520
                    }
2521
                }
2522
            }
2523
2524
1542
            ff_vc1_mc_1mv(v, dir);
2525

1542
            if (direct || bmvtype == BMV_TYPE_INTERPOLATED) {
2526
1336
                ff_vc1_interp_mc(v);
2527
            }
2528
1542
            v->fieldtx_plane[mb_pos] = 0;
2529
        }
2530
    }
2531
32640
    v->cbp[s->mb_x]      = block_cbp;
2532
32640
    v->ttblk[s->mb_x]    = block_tt;
2533
2534
32640
    return 0;
2535
}
2536
2537
/** Decode blocks of I-frame
2538
 */
2539
83
static void vc1_decode_i_blocks(VC1Context *v)
2540
{
2541
    int k, j;
2542
83
    MpegEncContext *s = &v->s;
2543
    int cbp, val;
2544
    uint8_t *coded_val;
2545
    int mb_pos;
2546
2547
    /* select coding mode used for VLC tables selection */
2548

83
    switch (v->y_ac_table_index) {
2549
5
    case 0:
2550
5
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2551
5
        break;
2552
70
    case 1:
2553
70
        v->codingset = CS_HIGH_MOT_INTRA;
2554
70
        break;
2555
8
    case 2:
2556
8
        v->codingset = CS_MID_RATE_INTRA;
2557
8
        break;
2558
    }
2559
2560

83
    switch (v->c_ac_table_index) {
2561
77
    case 0:
2562
77
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2563
77
        break;
2564
5
    case 1:
2565
5
        v->codingset2 = CS_HIGH_MOT_INTER;
2566
5
        break;
2567
1
    case 2:
2568
1
        v->codingset2 = CS_MID_RATE_INTER;
2569
1
        break;
2570
    }
2571
2572
    /* Set DC scale - y and c use the same */
2573
83
    s->y_dc_scale = s->y_dc_scale_table[v->pq];
2574
83
    s->c_dc_scale = s->c_dc_scale_table[v->pq];
2575
2576
    //do frame decode
2577
83
    s->mb_x = s->mb_y = 0;
2578
83
    s->mb_intra         = 1;
2579
83
    s->first_slice_line = 1;
2580
985
    for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2581
902
        s->mb_x = 0;
2582
902
        init_block_index(v);
2583
16352
        for (; s->mb_x < v->end_mb_x; s->mb_x++) {
2584
15450
            ff_update_block_index(s);
2585
15450
            s->bdsp.clear_blocks(v->block[v->cur_blk_idx][0]);
2586
15450
            mb_pos = s->mb_x + s->mb_y * s->mb_width;
2587
15450
            s->current_picture.mb_type[mb_pos]                     = MB_TYPE_INTRA;
2588
15450
            s->current_picture.qscale_table[mb_pos]                = v->pq;
2589
77250
            for (int i = 0; i < 4; i++) {
2590
61800
                s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
2591
61800
                s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
2592
            }
2593
2594
            // do actual MB decoding and displaying
2595
15450
            cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2596
15450
            v->s.ac_pred = get_bits1(&v->s.gb);
2597
2598
108150
            for (k = 0; k < 6; k++) {
2599
92700
                v->mb_type[0][s->block_index[k]] = 1;
2600
2601
92700
                val = ((cbp >> (5 - k)) & 1);
2602
2603
92700
                if (k < 4) {
2604
61800
                    int pred   = vc1_coded_block_pred(&v->s, k, &coded_val);
2605
61800
                    val        = val ^ pred;
2606
61800
                    *coded_val = val;
2607
                }
2608
92700
                cbp |= val << (5 - k);
2609
2610
92700
                vc1_decode_i_block(v, v->block[v->cur_blk_idx][block_map[k]], k, val, (k < 4) ? v->codingset : v->codingset2);
2611
2612
                if (CONFIG_GRAY && k > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2613
                    continue;
2614
92700
                v->vc1dsp.vc1_inv_trans_8x8(v->block[v->cur_blk_idx][block_map[k]]);
2615
            }
2616
2617

15450
            if (v->overlap && v->pq >= 9) {
2618
                ff_vc1_i_overlap_filter(v);
2619
                if (v->rangeredfrm)
2620
                    for (k = 0; k < 6; k++)
2621
                        for (j = 0; j < 64; j++)
2622
                            v->block[v->cur_blk_idx][block_map[k]][j] *= 2;
2623
                vc1_put_blocks_clamped(v, 1);
2624
            } else {
2625
15450
                if (v->rangeredfrm)
2626
29680
                    for (k = 0; k < 6; k++)
2627
1653600
                        for (j = 0; j < 64; j++)
2628
1628160
                            v->block[v->cur_blk_idx][block_map[k]][j] = (v->block[v->cur_blk_idx][block_map[k]][j] - 64) * 2;
2629
15450
                vc1_put_blocks_clamped(v, 0);
2630
            }
2631
2632
15450
            if (v->s.loop_filter)
2633
12480
                ff_vc1_i_loop_filter(v);
2634
2635
15450
            if (get_bits_left(&s->gb) < 0) {
2636
                ff_er_add_slice(&s->er, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
2637
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
2638
                       get_bits_count(&s->gb), s->gb.size_in_bits);
2639
                return;
2640
            }
2641
2642
15450
            v->topleft_blk_idx = (v->topleft_blk_idx + 1) % (v->end_mb_x + 2);
2643
15450
            v->top_blk_idx = (v->top_blk_idx + 1) % (v->end_mb_x + 2);
2644
15450
            v->left_blk_idx = (v->left_blk_idx + 1) % (v->end_mb_x + 2);
2645
15450
            v->cur_blk_idx = (v->cur_blk_idx + 1) % (v->end_mb_x + 2);
2646
        }
2647
902
        if (!v->s.loop_filter)
2648
66
            ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2649
836
        else if (s->mb_y)
2650
755
            ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2651
2652
902
        s->first_slice_line = 0;
2653
    }
2654
83
    if (v->s.loop_filter)
2655
81
        ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2656
2657
    /* This is intentionally mb_height and not end_mb_y - unlike in advanced
2658
     * profile, these only differ are when decoding MSS2 rectangles. */
2659
83
    ff_er_add_slice(&s->er, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
2660
}
2661
2662
/** Decode blocks of I-frame for advanced profile
2663
 */
2664
14
static int vc1_decode_i_blocks_adv(VC1Context *v)
2665
{
2666
    int k;
2667
14
    MpegEncContext *s = &v->s;
2668
    int cbp, val;
2669
    uint8_t *coded_val;
2670
    int mb_pos;
2671
    int mquant;
2672
    int mqdiff;
2673
14
    GetBitContext *gb = &s->gb;
2674
2675
14
    if (get_bits_left(gb) <= 1)
2676
        return AVERROR_INVALIDDATA;
2677
2678
    /* select coding mode used for VLC tables selection */
2679

14
    switch (v->y_ac_table_index) {
2680
1
    case 0:
2681
1
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2682
1
        break;
2683
2
    case 1:
2684
2
        v->codingset = CS_HIGH_MOT_INTRA;
2685
2
        break;
2686
11
    case 2:
2687
11
        v->codingset = CS_MID_RATE_INTRA;
2688
11
        break;
2689
    }
2690
2691

14
    switch (v->c_ac_table_index) {
2692
12
    case 0:
2693
12
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2694
12
        break;
2695
    case 1:
2696
        v->codingset2 = CS_HIGH_MOT_INTER;
2697
        break;
2698
2
    case 2:
2699
2
        v->codingset2 = CS_MID_RATE_INTER;
2700
2
        break;
2701
    }
2702
2703
    // do frame decode
2704
14
    s->mb_x             = s->mb_y = 0;
2705
14
    s->mb_intra         = 1;
2706
14
    s->first_slice_line = 1;
2707
14
    s->mb_y             = s->start_mb_y;
2708
14
    if (s->start_mb_y) {
2709
6
        s->mb_x = 0;
2710
6
        init_block_index(v);
2711
6
        memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
2712
6
               (1 + s->b8_stride) * sizeof(*s->coded_block));
2713
    }
2714
195
    for (; s->mb_y < s->end_mb_y; s->mb_y++) {
2715
181
        s->mb_x = 0;
2716
181
        init_block_index(v);
2717
12295
        for (;s->mb_x < s->mb_width; s->mb_x++) {
2718
12114
            mquant = v->pq;
2719
12114
            ff_update_block_index(s);
2720
12114
            s->bdsp.clear_blocks(v->block[v->cur_blk_idx][0]);
2721
12114
            mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2722
12114
            s->current_picture.mb_type[mb_pos + v->mb_off]                         = MB_TYPE_INTRA;
2723
60570
            for (int i = 0; i < 4; i++) {
2724
48456
                s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][0] = 0;
2725
48456
                s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][1] = 0;
2726
            }
2727
2728
            // do actual MB decoding and displaying
2729
12114
            if (v->fieldtx_is_raw)
2730
                v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
2731
12114
            if (get_bits_left(&v->s.gb) <= 1) {
2732
                ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2733
                return 0;
2734
            }
2735
2736
12114
            cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2737
12114
            if (v->acpred_is_raw)
2738
2670
                v->s.ac_pred = get_bits1(&v->s.gb);
2739
            else
2740
9444
                v->s.ac_pred = v->acpred_plane[mb_pos];
2741
2742

12114
            if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
2743
                v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
2744
2745









12114
            GET_MQUANT();
2746
2747
12114
            s->current_picture.qscale_table[mb_pos] = mquant;
2748
            /* Set DC scale - y and c use the same */
2749
12114
            s->y_dc_scale = s->y_dc_scale_table[FFABS(mquant)];
2750
12114
            s->c_dc_scale = s->c_dc_scale_table[FFABS(mquant)];
2751
2752
84798
            for (k = 0; k < 6; k++) {
2753
72684
                v->mb_type[0][s->block_index[k]] = 1;
2754
2755
72684
                val = ((cbp >> (5 - k)) & 1);
2756
2757
72684
                if (k < 4) {
2758
48456
                    int pred   = vc1_coded_block_pred(&v->s, k, &coded_val);
2759
48456
                    val        = val ^ pred;
2760
48456
                    *coded_val = val;
2761
                }
2762
72684
                cbp |= val << (5 - k);
2763
2764

72684
                v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
2765

72684
                v->c_avail = !!s->mb_x || (k == 1 || k == 3);
2766
2767
72684
                vc1_decode_i_block_adv(v, v->block[v->cur_blk_idx][block_map[k]], k, val,
2768
                                       (k < 4) ? v->codingset : v->codingset2, mquant);
2769
2770
                if (CONFIG_GRAY && k > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2771
                    continue;
2772
72684
                v->vc1dsp.vc1_inv_trans_8x8(v->block[v->cur_blk_idx][block_map[k]]);
2773
            }
2774
2775

12114
            if (v->overlap && (v->pq >= 9 || v->condover != CONDOVER_NONE))
2776
399
                ff_vc1_i_overlap_filter(v);
2777
12114
            vc1_put_blocks_clamped(v, 1);
2778
12114
            if (v->s.loop_filter)
2779
11814
                ff_vc1_i_loop_filter(v);
2780
2781
12114
            if (get_bits_left(&s->gb) < 0) {
2782
                // TODO: may need modification to handle slice coding
2783
                ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2784
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
2785
                       get_bits_count(&s->gb), s->gb.size_in_bits);
2786
                return 0;
2787
            }
2788
12114
            inc_blk_idx(v->topleft_blk_idx);
2789
12114
            inc_blk_idx(v->top_blk_idx);
2790
12114
            inc_blk_idx(v->left_blk_idx);
2791
12114
            inc_blk_idx(v->cur_blk_idx);
2792
        }
2793
181
        if (!v->s.loop_filter)
2794
15
            ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2795
166
        else if (s->mb_y)
2796
159
            ff_mpeg_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
2797
181
        s->first_slice_line = 0;
2798
    }
2799
2800
14
    if (v->s.loop_filter)
2801
13
        ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2802
14
    ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2803
14
                    (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2804
14
    return 0;
2805
}
2806
2807
915
static void vc1_decode_p_blocks(VC1Context *v)
2808
{
2809
915
    MpegEncContext *s = &v->s;
2810
    int apply_loop_filter;
2811
2812
    /* select coding mode used for VLC tables selection */
2813

915
    switch (v->c_ac_table_index) {
2814
268
    case 0:
2815
268
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2816
268
        break;
2817
391
    case 1:
2818
391
        v->codingset = CS_HIGH_MOT_INTRA;
2819
391
        break;
2820
256
    case 2:
2821
256
        v->codingset = CS_MID_RATE_INTRA;
2822
256
        break;
2823
    }
2824
2825

915
    switch (v->c_ac_table_index) {
2826
268
    case 0:
2827
268
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2828
268
        break;
2829
391
    case 1:
2830
391
        v->codingset2 = CS_HIGH_MOT_INTER;
2831
391
        break;
2832
256
    case 2:
2833
256
        v->codingset2 = CS_MID_RATE_INTER;
2834
256
        break;
2835
    }
2836
2837

915
    apply_loop_filter   = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY);
2838
915
    s->first_slice_line = 1;
2839
915
    memset(v->cbp_base, 0, sizeof(v->cbp_base[0]) * 3 * s->mb_stride);
2840
6277
    for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2841
5362
        s->mb_x = 0;
2842
5362
        init_block_index(v);
2843
211511
        for (; s->mb_x < s->mb_width; s->mb_x++) {
2844
206149
            ff_update_block_index(s);
2845
2846


206149
            if (v->fcm == ILACE_FIELD || (v->fcm == PROGRESSIVE && v->mv_type_is_raw) || v->skip_is_raw)
2847
96015
                if (get_bits_left(&v->s.gb) <= 1) {
2848
                    ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2849
                    return;
2850
                }
2851
2852
206149
            if (v->fcm == ILACE_FIELD) {
2853
37245
                vc1_decode_p_mb_intfi(v);
2854
37245
                if (apply_loop_filter)
2855
37245
                    ff_vc1_p_loop_filter(v);
2856
168904
            } else if (v->fcm == ILACE_FRAME) {
2857
16320
                vc1_decode_p_mb_intfr(v);
2858
16320
                if (apply_loop_filter)
2859
16320
                    ff_vc1_p_intfr_loop_filter(v);
2860
            } else {
2861
152584
                vc1_decode_p_mb(v);
2862
152584
                if (apply_loop_filter)
2863
93304
                    ff_vc1_p_loop_filter(v);
2864
            }
2865

206149
            if (get_bits_left(&s->gb) < 0 || get_bits_count(&s->gb) < 0) {
2866
                // TODO: may need modification to handle slice coding
2867
                ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2868
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
2869
                       get_bits_count(&s->gb), s->gb.size_in_bits, s->mb_x, s->mb_y);
2870
                return;
2871
            }
2872
206149
            inc_blk_idx(v->topleft_blk_idx);
2873
206149
            inc_blk_idx(v->top_blk_idx);
2874
206149
            inc_blk_idx(v->left_blk_idx);
2875
206149
            inc_blk_idx(v->cur_blk_idx);
2876
        }
2877
5362
        memmove(v->cbp_base,
2878
5362
                v->cbp - s->mb_stride,
2879
5362
                sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
2880
5362
        memmove(v->ttblk_base,
2881
5362
                v->ttblk - s->mb_stride,
2882
5362
                sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
2883
5362
        memmove(v->is_intra_base,
2884
5362
                v->is_intra - s->mb_stride,
2885
5362
                sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
2886
5362
        memmove(v->luma_mv_base,
2887
5362
                v->luma_mv - s->mb_stride,
2888
5362
                sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
2889
5362
        if (s->mb_y != s->start_mb_y)
2890
4447
            ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2891
5362
        s->first_slice_line = 0;
2892
    }
2893
915
    if (s->end_mb_y >= s->start_mb_y)
2894
915
        ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2895
915
    ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2896
915
                    (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2897
}
2898
2899
109
static void vc1_decode_b_blocks(VC1Context *v)
2900
{
2901
109
    MpegEncContext *s = &v->s;
2902
2903
    /* select coding mode used for VLC tables selection */
2904

109
    switch (v->c_ac_table_index) {
2905
14
    case 0:
2906
14
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2907
14
        break;
2908
16
    case 1:
2909
16
        v->codingset = CS_HIGH_MOT_INTRA;
2910
16
        break;
2911
79
    case 2:
2912
79
        v->codingset = CS_MID_RATE_INTRA;
2913
79
        break;
2914
    }
2915
2916

109
    switch (v->c_ac_table_index) {
2917
14
    case 0:
2918
14
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2919
14
        break;
2920
16
    case 1:
2921
16
        v->codingset2 = CS_HIGH_MOT_INTER;
2922
16
        break;
2923
79
    case 2:
2924
79
        v->codingset2 = CS_MID_RATE_INTER;
2925
79
        break;
2926
    }
2927
2928
109
    s->first_slice_line = 1;
2929
1809
    for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2930
1700
        s->mb_x = 0;
2931
1700
        init_block_index(v);
2932
106820
        for (; s->mb_x < s->mb_width; s->mb_x++) {
2933
105120
            ff_update_block_index(s);
2934
2935

105120
            if (v->fcm == ILACE_FIELD || v->skip_is_raw || v->dmb_is_raw)
2936
51540
                if (get_bits_left(&v->s.gb) <= 1) {
2937
                    ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2938
                    return;
2939
                }
2940
2941
105120
            if (v->fcm == ILACE_FIELD) {
2942
51540
                vc1_decode_b_mb_intfi(v);
2943
51540
                if (v->s.loop_filter)
2944
51540
                    ff_vc1_b_intfi_loop_filter(v);
2945
53580
            } else if (v->fcm == ILACE_FRAME) {
2946
32640
                vc1_decode_b_mb_intfr(v);
2947
32640
                if (v->s.loop_filter)
2948
32640
                    ff_vc1_p_intfr_loop_filter(v);
2949
            } else {
2950
20940
                vc1_decode_b_mb(v);
2951
20940
                if (v->s.loop_filter)
2952
6090
                    ff_vc1_i_loop_filter(v);
2953
            }
2954

105120
            if (get_bits_left(&s->gb) < 0 || get_bits_count(&s->gb) < 0) {
2955
                // TODO: may need modification to handle slice coding
2956
                ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2957
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
2958
                       get_bits_count(&s->gb), s->gb.size_in_bits, s->mb_x, s->mb_y);
2959
                return;
2960
            }
2961
        }
2962
1700
        memmove(v->cbp_base,
2963
1700
                v->cbp - s->mb_stride,
2964
1700
                sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
2965
1700
        memmove(v->ttblk_base,
2966
1700
                v->ttblk - s->mb_stride,
2967
1700
                sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
2968
1700
        memmove(v->is_intra_base,
2969
1700
                v->is_intra - s->mb_stride,
2970
1700
                sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
2971
1700
        if (!v->s.loop_filter)
2972
330
            ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2973
1370
        else if (s->mb_y)
2974
1272
            ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2975
1700
        s->first_slice_line = 0;
2976
    }
2977
109
    if (v->s.loop_filter)
2978
98
        ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2979
109
    ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2980
109
                    (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2981
}
2982
2983
67
static void vc1_decode_skip_blocks(VC1Context *v)
2984
{
2985
67
    MpegEncContext *s = &v->s;
2986
2987
67
    if (!v->s.last_picture.f->data[0])
2988
        return;
2989
2990
67
    ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
2991
67
    s->first_slice_line = 1;
2992
1684
    for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2993
1617
        s->mb_x = 0;
2994
1617
        init_block_index(v);
2995
1617
        ff_update_block_index(s);
2996
1617
        memcpy(s->dest[0], s->last_picture.f->data[0] + s->mb_y * 16 * s->linesize,   s->linesize   * 16);
2997
1617
        memcpy(s->dest[1], s->last_picture.f->data[1] + s->mb_y *  8 * s->uvlinesize, s->uvlinesize *  8);
2998
1617
        memcpy(s->dest[2], s->last_picture.f->data[2] + s->mb_y *  8 * s->uvlinesize, s->uvlinesize *  8);
2999
1617
        ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
3000
1617
        s->first_slice_line = 0;
3001
    }
3002
67
    s->pict_type = AV_PICTURE_TYPE_P;
3003
}
3004
3005
1188
void ff_vc1_decode_blocks(VC1Context *v)
3006
{
3007
3008
1188
    v->s.esc3_level_length = 0;
3009
1188
    if (v->x8_type) {
3010
        ff_intrax8_decode_picture(&v->x8, &v->s.current_picture,
3011
                                  &v->s.gb, &v->s.mb_x, &v->s.mb_y,
3012
                                  2 * v->pq + v->halfpq, v->pq * !v->pquantizer,
3013
                                  v->s.loop_filter, v->s.low_delay);
3014
3015
        ff_er_add_slice(&v->s.er, 0, 0,
3016
                        (v->s.mb_x >> 1) - 1, (v->s.mb_y >> 1) - 1,
3017
                        ER_MB_END);
3018
    } else {
3019
1188
        v->cur_blk_idx     =  0;
3020
1188
        v->left_blk_idx    = -1;
3021
1188
        v->topleft_blk_idx =  1;
3022
1188
        v->top_blk_idx     =  2;
3023

1188
        switch (v->s.pict_type) {
3024
97
        case AV_PICTURE_TYPE_I:
3025
97
            if (v->profile == PROFILE_ADVANCED)
3026
14
                vc1_decode_i_blocks_adv(v);
3027
            else
3028
83
                vc1_decode_i_blocks(v);
3029
97
            break;
3030
982
        case AV_PICTURE_TYPE_P:
3031
982
            if (v->p_frame_skipped)
3032
67
                vc1_decode_skip_blocks(v);
3033
            else
3034
915
                vc1_decode_p_blocks(v);
3035
982
            break;
3036