GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/vc1_block.c Lines: 1848 1958 94.4 %
Date: 2021-04-22 14:24:15 Branches: 1637 2216 73.9 %

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) {
596

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

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

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

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

32424
        else if (c_avail && mb_pos)
782
32382
            q2 = s->current_picture.qscale_table[mb_pos - 1];
783
    } else {
784
19931
        if (n == 2)
785
4905
            q2 = q1;
786

15026
        else if (a_avail && mb_pos >= s->mb_stride)
787
15026
            q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
788
    }
789
790
    //AC Decoding
791
72684
    i = 1;
792
793
72684
    if (coded) {
794
65520
        int last = 0, skip, value;
795
        const uint8_t *zz_table;
796
        int k;
797
798
65520
        if (v->s.ac_pred) {
799

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

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

1708
            if (q2 && q1 != q2) {
883
840
                for (k = 1; k < 8; k++)
884
735
                    ac_val2[k] = (int)(ac_val2[k] * q2 * (unsigned)ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
885
            }
886
13664
            for (k = 1; k < 8; k++) {
887
11956
                block[k << sh] = ac_val2[k] * scale;
888

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

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

234820
    if (!a_avail && !c_avail) use_pred = 0;
970
234820
    ac_val = s->ac_val[0][s->block_index[n]];
971
234820
    ac_val2 = ac_val;
972
973
234820
    scale = quant * 2 + ((mquant < 0) ? 0 : v->halfpq);
974
975
234820
    if (dc_pred_dir) //left
976
113011
        ac_val -= 16;
977
    else //top
978
121809
        ac_val -= 16 * s->block_wrap[n];
979
980
234820
    q1 = s->current_picture.qscale_table[mb_pos];
981

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

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

234820
    if (dc_pred_dir && n == 1)
986
33871
        q2 = q1;
987

234820
    if (!dc_pred_dir && n == 2)
988
28266
        q2 = q1;
989
234820
    if (n == 3) q2 = q1;
990
991
234820
    if (coded) {
992
162495
        int last = 0, skip, value;
993
        int k;
994
995
1681789
        while (!last) {
996
1519294
            int ret = vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
997
1519294
            if (ret < 0)
998
                return ret;
999
1519294
            i += skip;
1000
1519294
            if (i > 63)
1001
                break;
1002
1519294
            if (v->fcm == PROGRESSIVE)
1003
925288
                block[v->zz_8x8[0][i++]] = value;
1004
            else {
1005

594006
                if (use_pred && (v->fcm == ILACE_FRAME)) {
1006
19188
                    if (!dc_pred_dir) // top
1007
8623
                        block[v->zz_8x8[2][i++]] = value;
1008
                    else // left
1009
10565
                        block[v->zz_8x8[3][i++]] = value;
1010
                } else {
1011
574818
                    block[v->zzi_8x8[i++]] = value;
1012
                }
1013
            }
1014
        }
1015
1016
        /* apply AC prediction if needed */
1017
162495
        if (use_pred) {
1018
            /* scale predictors if needed*/
1019
42460
            q1 = FFABS(q1) * 2 + ((q1 < 0) ? 0 : v->halfpq) - 1;
1020
42460
            if (q1 < 1)
1021
                return AVERROR_INVALIDDATA;
1022
42460
            if (q2)
1023
42460
                q2 = FFABS(q2) * 2 + ((q2 < 0) ? 0 : v->halfpq) - 1;
1024

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

6684
                if (q2 && q1 != q2) {
1070
256
                    for (k = 1; k < 8; k++)
1071
224
                        ac_val2[k] = (int)(ac_val2[k] * (unsigned)q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1072
                }
1073
            }
1074
        } else { // top
1075
38310
            if (use_pred) {
1076
2665
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1077
2665
                q1 = FFABS(q1) * 2 + ((q1 < 0) ? 0 : v->halfpq) - 1;
1078
2665
                if (q1 < 1)
1079
                    return AVERROR_INVALIDDATA;
1080
2665
                if (q2)
1081
2665
                    q2 = FFABS(q2) * 2 + ((q2 < 0) ? 0 : v->halfpq) - 1;
1082

2665
                if (q2 && q1 != q2) {
1083
104
                    for (k = 1; k < 8; k++)
1084
91
                        ac_val2[k + 8] = (int)(ac_val2[k + 8] * (unsigned)q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1085
                }
1086
            }
1087
        }
1088
1089
        /* apply AC prediction if needed */
1090
72325
        if (use_pred) {
1091
9349
            if (dc_pred_dir) { // left
1092
53472
                for (k = 1; k < 8; k++) {
1093
46788
                    block[k << v->left_blk_sh] = ac_val2[k] * scale;
1094

46788
                    if (!v->pquantizer && block[k << v->left_blk_sh])
1095
127
                        block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -quant : quant;
1096
                }
1097
            } else { // top
1098
21320
                for (k = 1; k < 8; k++) {
1099
18655
                    block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
1100

18655
                    if (!v->pquantizer && block[k << v->top_blk_sh])
1101
129
                        block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -quant : quant;
1102
                }
1103
            }
1104
9349
            i = 63;
1105
        }
1106
    }
1107
234820
    s->block_last_index[n] = i;
1108
1109
234820
    return 0;
1110
}
1111
1112
/** Decode P block
1113
 */
1114
703814
static int vc1_decode_p_block(VC1Context *v, int16_t block[64], int n,
1115
                              int mquant, int ttmb, int first_block,
1116
                              uint8_t *dst, int linesize, int skip_block,
1117
                              int *ttmb_out)
1118
{
1119
703814
    MpegEncContext *s = &v->s;
1120
703814
    GetBitContext *gb = &s->gb;
1121
    int i, j;
1122
703814
    int subblkpat = 0;
1123
    int scale, off, idx, last, skip, value;
1124
703814
    int ttblk = ttmb & 7;
1125
703814
    int pat = 0;
1126
703814
    int quant = FFABS(mquant);
1127
1128
703814
    s->bdsp.clear_block(block);
1129
1130
703814
    if (ttmb == -1) {
1131
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)];
1132
    }
1133
703814
    if (ttblk == TT_4X4) {
1134
77928
        subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
1135
    }
1136

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


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

242753
            || (!v->res_rtm_flag && !first_block))) {
1139
30481
        subblkpat = decode012(gb);
1140
30481
        if (subblkpat)
1141
11134
            subblkpat ^= 3; // swap decoded pattern bits
1142

30481
        if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
1143
5720
            ttblk = TT_8X4;
1144

30481
        if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
1145
3979
            ttblk = TT_4X8;
1146
    }
1147
703814
    scale = quant * 2 + ((mquant < 0) ? 0 : v->halfpq);
1148
1149
    // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
1150

703814
    if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
1151
38935
        subblkpat = 2 - (ttblk == TT_8X4_TOP);
1152
38935
        ttblk     = TT_8X4;
1153
    }
1154

703814
    if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
1155
47634
        subblkpat = 2 - (ttblk == TT_4X8_LEFT);
1156
47634
        ttblk     = TT_4X8;
1157
    }
1158

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

311712
            if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
1209
190131
                if (i == 1)
1210
21184
                    v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
1211
                else
1212
168947
                    v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) *  2 * linesize, linesize, block + off);
1213
            }
1214
        }
1215
77928
        break;
1216
134020
    case TT_8X4:
1217
134020
        pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
1218
402060
        for (j = 0; j < 2; j++) {
1219
268040
            last = subblkpat & (1 << (1 - j));
1220
268040
            i    = 0;
1221
268040
            off  = j * 32;
1222
1164631
            while (!last) {
1223
896591
                int ret = vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1224
896591
                if (ret < 0)
1225
                    return ret;
1226
896591
                i += skip;
1227
896591
                if (i > 31)
1228
                    break;
1229
896591
                if (!v->fcm)
1230
470573
                    idx = v->zz_8x4[i++] + off;
1231
                else
1232
426018
                    idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
1233
896591
                block[idx] = value * scale;
1234
896591
                if (!v->pquantizer)
1235
9538
                    block[idx] += (block[idx] < 0) ? -quant : quant;
1236
            }
1237

268040
            if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
1238
222413
                if (i == 1)
1239
15798
                    v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
1240
                else
1241
206615
                    v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
1242
            }
1243
        }
1244
134020
        break;
1245
139214
    case TT_4X8:
1246
139214
        pat = ~(subblkpat * 5) & 0xF;
1247
417642
        for (j = 0; j < 2; j++) {
1248
278428
            last = subblkpat & (1 << (1 - j));
1249
278428
            i    = 0;
1250
278428
            off  = j * 4;
1251
1155008
            while (!last) {
1252
876580
                int ret = vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1253
876580
                if (ret < 0)
1254
                    return ret;
1255
876580
                i += skip;
1256
876580
                if (i > 31)
1257
                    break;
1258
876580
                if (!v->fcm)
1259
431900
                    idx = v->zz_4x8[i++] + off;
1260
                else
1261
444680
                    idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
1262
876580
                block[idx] = value * scale;
1263
876580
                if (!v->pquantizer)
1264
10845
                    block[idx] += (block[idx] < 0) ? -quant : quant;
1265
            }
1266

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





113891
            GET_MVDATA(dmv_x, dmv_y);
1319
1320
113891
            if (s->mb_intra) {
1321
24985
                s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
1322
24985
                s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
1323
            }
1324
113891
            s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
1325
113891
            ff_vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1326
1327
            /* FIXME Set DC val for inter block ? */
1328

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









2748
                GET_MQUANT();
1330
2748
                s->ac_pred = get_bits1(gb);
1331
2748
                cbp        = 0;
1332
111143
            } else if (mb_has_coeffs) {
1333
92366
                if (s->mb_intra)
1334
22237
                    s->ac_pred = get_bits1(gb);
1335
92366
                cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1336









92366
                GET_MQUANT();
1337
            } else {
1338
18777
                mquant = v->pq;
1339
18777
                cbp    = 0;
1340
            }
1341
113891
            s->current_picture.qscale_table[mb_pos] = mquant;
1342
1343

113891
            if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
1344
42234
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
1345
                                VC1_TTMB_VLC_BITS, 2);
1346
113891
            if (!s->mb_intra) ff_vc1_mc_1mv(v, 0);
1347
113891
            dst_idx = 0;
1348
797237
            for (i = 0; i < 6; i++) {
1349
683346
                s->dc_val[0][s->block_index[i]] = 0;
1350
683346
                dst_idx += i >> 2;
1351
683346
                val = ((cbp >> (5 - i)) & 1);
1352
683346
                off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1353
683346
                v->mb_type[0][s->block_index[i]] = s->mb_intra;
1354
683346
                if (s->mb_intra) {
1355
                    /* check if prediction blocks A and C are available */
1356
149910
                    v->a_avail = v->c_avail = 0;
1357

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

149910
                    if (i == 1 || i == 3 || s->mb_x)
1360
147294
                        v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1361
1362
149910
                    vc1_decode_intra_block(v, v->block[v->cur_blk_idx][block_map[i]], i, val, mquant,
1363
149910
                                           (i & 4) ? v->codingset2 : v->codingset);
1364
                    if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1365
                        continue;
1366
149910
                    v->vc1dsp.vc1_inv_trans_8x8(v->block[v->cur_blk_idx][block_map[i]]);
1367
149910
                    if (v->rangeredfrm)
1368
                        for (j = 0; j < 64; j++)
1369
                            v->block[v->cur_blk_idx][block_map[i]][j] *= 2;
1370
149910
                    block_cbp   |= 0xF << (i << 2);
1371
149910
                    block_intra |= 1 << i;
1372
533436
                } else if (val) {
1373
658374
                    pat = vc1_decode_p_block(v, v->block[v->cur_blk_idx][block_map[i]], i, mquant, ttmb, first_block,
1374
219458
                                             s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
1375
                                             CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
1376
219458
                    if (pat < 0)
1377
                        return pat;
1378
219458
                    block_cbp |= pat << (i << 2);
1379

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





71997
                        GET_MVDATA(dmv_x, dmv_y);
1411
                    }
1412
91420
                    ff_vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1413
91420
                    if (!s->mb_intra)
1414
80856
                        ff_vc1_mc_4mv_luma(v, i, 0, 0);
1415
91420
                    intra_count += s->mb_intra;
1416
91420
                    is_intra[i]  = s->mb_intra;
1417
91420
                    is_coded[i]  = mb_has_coeffs;
1418
                }
1419
137130
                if (i & 4) {
1420
45710
                    is_intra[i] = (intra_count >= 3);
1421
45710
                    is_coded[i] = val;
1422
                }
1423
137130
                if (i == 4)
1424
22855
                    ff_vc1_mc_4mv_chroma(v, 0);
1425
137130
                v->mb_type[0][s->block_index[i]] = is_intra[i];
1426
137130
                if (!coded_inter)
1427
52317
                    coded_inter = !is_intra[i] & is_coded[i];
1428
            }
1429
            // if there are no coded blocks then don't do anything more
1430
22855
            dst_idx = 0;
1431

22855
            if (!intra_count && !coded_inter)
1432
2699
                goto end;
1433









20156
            GET_MQUANT();
1434
20156
            s->current_picture.qscale_table[mb_pos] = mquant;
1435
            /* test if block is intra and has pred */
1436
            {
1437
20156
                int intrapred = 0;
1438
122744
                for (i = 0; i < 6; i++)
1439
106282
                    if (is_intra[i]) {
1440


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


5463
                            || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
1442
3694
                            intrapred = 1;
1443
3694
                            break;
1444
                        }
1445
                    }
1446
20156
                if (intrapred)
1447
3694
                    s->ac_pred = get_bits1(gb);
1448
                else
1449
16462
                    s->ac_pred = 0;
1450
            }
1451

20156
            if (!v->ttmbf && coded_inter)
1452
18220
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1453
141092
            for (i = 0; i < 6; i++) {
1454
120936
                dst_idx    += i >> 2;
1455
120936
                off         = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1456
120936
                s->mb_intra = is_intra[i];
1457
120936
                if (is_intra[i]) {
1458
                    /* check if prediction blocks A and C are available */
1459
13000
                    v->a_avail = v->c_avail = 0;
1460

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

13000
                    if (i == 1 || i == 3 || s->mb_x)
1463
12804
                        v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1464
1465
13000
                    vc1_decode_intra_block(v, v->block[v->cur_blk_idx][block_map[i]], i, is_coded[i], mquant,
1466
13000
                                           (i & 4) ? v->codingset2 : v->codingset);
1467
                    if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1468
                        continue;
1469
13000
                    v->vc1dsp.vc1_inv_trans_8x8(v->block[v->cur_blk_idx][block_map[i]]);
1470
13000
                    if (v->rangeredfrm)
1471
                        for (j = 0; j < 64; j++)
1472
                            v->block[v->cur_blk_idx][block_map[i]][j] *= 2;
1473
13000
                    block_cbp   |= 0xF << (i << 2);
1474
13000
                    block_intra |= 1 << i;
1475
107936
                } else if (is_coded[i]) {
1476
200115
                    pat = vc1_decode_p_block(v, v->block[v->cur_blk_idx][block_map[i]], i, mquant, ttmb,
1477
66705
                                             first_block, s->dest[dst_idx] + off,
1478
66705
                                             (i & 4) ? s->uvlinesize : s->linesize,
1479
                                             CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY),
1480
                                             &block_tt);
1481
66705
                    if (pat < 0)
1482
                        return pat;
1483
66705
                    block_cbp |= pat << (i << 2);
1484

66705
                    if (!v->ttmbf && ttmb < 8)
1485
44004
                        ttmb = -1;
1486
66705
                    first_block = 0;
1487
                }
1488
            }
1489
        } else { // skipped MB
1490
3721
            s->mb_intra                               = 0;
1491
3721
            s->current_picture.qscale_table[mb_pos] = 0;
1492
26047
            for (i = 0; i < 6; i++) {
1493
22326
                v->mb_type[0][s->block_index[i]] = 0;
1494
22326
                s->dc_val[0][s->block_index[i]]  = 0;
1495
            }
1496
18605
            for (i = 0; i < 4; i++) {
1497
14884
                ff_vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1498
14884
                ff_vc1_mc_4mv_luma(v, i, 0, 0);
1499
            }
1500
3721
            ff_vc1_mc_4mv_chroma(v, 0);
1501
3721
            s->current_picture.qscale_table[mb_pos] = 0;
1502
        }
1503
    }
1504
152584
end:
1505

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

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









1676
            GET_MQUANT();
1593
1676
            s->current_picture.qscale_table[mb_pos] = mquant;
1594
            /* Set DC scale - y and c use the same (not sure if necessary here) */
1595
1676
            s->y_dc_scale = s->y_dc_scale_table[FFABS(mquant)];
1596
1676
            s->c_dc_scale = s->c_dc_scale_table[FFABS(mquant)];
1597
1676
            dst_idx = 0;
1598
11732
            for (i = 0; i < 6; i++) {
1599
10056
                v->a_avail = v->c_avail          = 0;
1600
10056
                v->mb_type[0][s->block_index[i]] = 1;
1601
10056
                s->dc_val[0][s->block_index[i]]  = 0;
1602
10056
                dst_idx += i >> 2;
1603
10056
                val = ((cbp >> (5 - i)) & 1);
1604

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

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









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

14631
            if (!v->ttmbf && cbp)
1679
13330
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1680
102417
            for (i = 0; i < 6; i++) {
1681
87786
                s->dc_val[0][s->block_index[i]] = 0;
1682
87786
                dst_idx += i >> 2;
1683
87786
                val = ((cbp >> (5 - i)) & 1);
1684
87786
                if (!fieldtx)
1685
70050
                    off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1686
                else
1687

17736
                    off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
1688
87786
                if (val) {
1689
190881
                    pat = vc1_decode_p_block(v, v->block[v->cur_blk_idx][block_map[i]], i, mquant, ttmb,
1690
63627
                                             first_block, s->dest[dst_idx] + off,
1691
63627
                                             (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
1692
                                             CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
1693
63627
                    if (pat < 0)
1694
                        return pat;
1695
63627
                    block_cbp |= pat << (i << 2);
1696

63627
                    if (!v->ttmbf && ttmb < 8)
1697
44047
                        ttmb = -1;
1698
63627
                    first_block = 0;
1699
                }
1700
            }
1701
        }
1702
    } else { // skipped
1703
13
        s->mb_intra = v->is_intra[s->mb_x] = 0;
1704
91
        for (i = 0; i < 6; i++) {
1705
78
            v->mb_type[0][s->block_index[i]] = 0;
1706
78
            s->dc_val[0][s->block_index[i]] = 0;
1707
        }
1708
13
        s->current_picture.mb_type[mb_pos]      = MB_TYPE_SKIP;
1709
13
        s->current_picture.qscale_table[mb_pos] = 0;
1710
13
        v->blk_mv_type[s->block_index[0]] = 0;
1711
13
        v->blk_mv_type[s->block_index[1]] = 0;
1712
13
        v->blk_mv_type[s->block_index[2]] = 0;
1713
13
        v->blk_mv_type[s->block_index[3]] = 0;
1714
13
        ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
1715
13
        ff_vc1_mc_1mv(v, 0);
1716
13
        v->fieldtx_plane[mb_pos] = 0;
1717
    }
1718

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









9990
        GET_MQUANT();
1757
9990
        s->current_picture.qscale_table[mb_pos] = mquant;
1758
        /* Set DC scale - y and c use the same (not sure if necessary here) */
1759
9990
        s->y_dc_scale = s->y_dc_scale_table[FFABS(mquant)];
1760
9990
        s->c_dc_scale = s->c_dc_scale_table[FFABS(mquant)];
1761
9990
        v->s.ac_pred  = v->acpred_plane[mb_pos] = get_bits1(gb);
1762
9990
        mb_has_coeffs = idx_mbmode & 1;
1763
9990
        if (mb_has_coeffs)
1764
9243
            cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
1765
9990
        dst_idx = 0;
1766
69930
        for (i = 0; i < 6; i++) {
1767
59940
            v->a_avail = v->c_avail          = 0;
1768
59940
            v->mb_type[0][s->block_index[i]] = 1;
1769
59940
            s->dc_val[0][s->block_index[i]]  = 0;
1770
59940
            dst_idx += i >> 2;
1771
59940
            val = ((cbp >> (5 - i)) & 1);
1772

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

59940
            if (i == 1 || i == 3 || s->mb_x)
1775
59200
                v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1776
1777
59940
            vc1_decode_intra_block(v, v->block[v->cur_blk_idx][block_map[i]], i, val, mquant,
1778
59940
                                   (i & 4) ? v->codingset2 : v->codingset);
1779
            if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1780
                continue;
1781
59940
            v->vc1dsp.vc1_inv_trans_8x8(v->block[v->cur_blk_idx][block_map[i]]);
1782
59940
            off  = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1783
59940
            block_cbp |= 0xf << (i << 2);
1784
        }
1785
    } else {
1786
27255
        s->mb_intra = v->is_intra[s->mb_x] = 0;
1787
27255
        s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
1788
190785
        for (i = 0; i < 6; i++)
1789
163530
            v->mb_type[0][s->block_index[i]] = 0;
1790
27255
        if (idx_mbmode <= 5) { // 1-MV
1791
20405
            dmv_x = dmv_y = pred_flag = 0;
1792
20405
            if (idx_mbmode & 1) {
1793
15790
                get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
1794
            }
1795
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);
1796
20405
            ff_vc1_mc_1mv(v, 0);
1797
20405
            mb_has_coeffs = !(idx_mbmode & 2);
1798
        } else { // 4-MV
1799
6850
            v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
1800
34250
            for (i = 0; i < 4; i++) {
1801
27400
                dmv_x = dmv_y = pred_flag = 0;
1802
27400
                if (v->fourmvbp & (8 >> i))
1803
8342
                    get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
1804
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);
1805
27400
                ff_vc1_mc_4mv_luma(v, i, 0, 0);
1806
            }
1807
6850
            ff_vc1_mc_4mv_chroma(v, 0);
1808
6850
            mb_has_coeffs = idx_mbmode & 1;
1809
        }
1810
27255
        if (mb_has_coeffs)
1811
20418
            cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1812
27255
        if (cbp) {
1813









20418
            GET_MQUANT();
1814
        }
1815
27255
        s->current_picture.qscale_table[mb_pos] = mquant;
1816

27255
        if (!v->ttmbf && cbp) {
1817
20418
            ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1818
        }
1819
27255
        dst_idx = 0;
1820
190785
        for (i = 0; i < 6; i++) {
1821
163530
            s->dc_val[0][s->block_index[i]] = 0;
1822
163530
            dst_idx += i >> 2;
1823
163530
            val = ((cbp >> (5 - i)) & 1);
1824
163530
            off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
1825
163530
            if (val) {
1826
268404
                pat = vc1_decode_p_block(v, v->block[v->cur_blk_idx][block_map[i]], i, mquant, ttmb,
1827
89468
                                         first_block, s->dest[dst_idx] + off,
1828
89468
                                         (i & 4) ? s->uvlinesize : s->linesize,
1829
                                         CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY),
1830
                                         &block_tt);
1831
89468
                if (pat < 0)
1832
                    return pat;
1833
89468
                block_cbp |= pat << (i << 2);
1834

89468
                if (!v->ttmbf && ttmb < 8)
1835
58173
                    ttmb = -1;
1836
89468
                first_block = 0;
1837
            }
1838
        }
1839
    }
1840

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





15906
            GET_MVDATA(dmv_x[0], dmv_y[0]);
1892
15906
            dmv_x[1] = dmv_x[0];
1893
15906
            dmv_y[1] = dmv_y[0];
1894
        }
1895

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

18201
            switch (bmvtype) {
1898
8021
            case 0:
1899
8021
                bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
1900
8021
                break;
1901
4023
            case 1:
1902
4023
                bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
1903
4023
                break;
1904
6157
            case 2:
1905
6157
                bmvtype  = BMV_TYPE_INTERPOLATED;
1906
6157
                dmv_x[0] = dmv_y[0] = 0;
1907
            }
1908
2739
        }
1909
    }
1910
146580
    for (i = 0; i < 6; i++)
1911
125640
        v->mb_type[0][s->block_index[i]] = s->mb_intra;
1912
1913
20940
    if (skipped) {
1914
3578
        if (direct)
1915
1113
            bmvtype = BMV_TYPE_INTERPOLATED;
1916
3578
        ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1917
3578
        vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1918
3578
        return 0;
1919
    }
1920
17362
    if (direct) {
1921
1456
        cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1922









1456
        GET_MQUANT();
1923
1456
        s->mb_intra = 0;
1924
1456
        s->current_picture.qscale_table[mb_pos] = mquant;
1925
1456
        if (!v->ttmbf)
1926
1456
            ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1927
1456
        dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
1928
1456
        ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1929
1456
        vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1930
    } else {
1931

15906
        if (!mb_has_coeffs && !s->mb_intra) {
1932
            /* no coded blocks - effectively skipped */
1933
1035
            ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1934
1035
            vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1935
1035
            return 0;
1936
        }
1937

14871
        if (s->mb_intra && !mb_has_coeffs) {
1938
            GET_MQUANT();
1939
            s->current_picture.qscale_table[mb_pos] = mquant;
1940
            s->ac_pred = get_bits1(gb);
1941
            cbp = 0;
1942
            ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1943
        } else {
1944
14871
            if (bmvtype == BMV_TYPE_INTERPOLATED) {
1945





5289
                GET_MVDATA(dmv_x[0], dmv_y[0]);
1946
5289
                if (!mb_has_coeffs) {
1947
                    /* interpolated skipped block */
1948
475
                    ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1949
475
                    vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1950
475
                    return 0;
1951
                }
1952
            }
1953
14396
            ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1954
14396
            if (!s->mb_intra) {
1955
14226
                vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1956
            }
1957
14396
            if (s->mb_intra)
1958
170
                s->ac_pred = get_bits1(gb);
1959
14396
            cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1960









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

14396
            if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
1963
14226
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1964
        }
1965
    }
1966
15852
    dst_idx = 0;
1967
110964
    for (i = 0; i < 6; i++) {
1968
95112
        s->dc_val[0][s->block_index[i]] = 0;
1969
95112
        dst_idx += i >> 2;
1970
95112
        val = ((cbp >> (5 - i)) & 1);
1971
95112
        off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1972
95112
        v->mb_type[0][s->block_index[i]] = s->mb_intra;
1973
95112
        if (s->mb_intra) {
1974
            /* check if prediction blocks A and C are available */
1975
1020
            v->a_avail = v->c_avail = 0;
1976

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

1020
            if (i == 1 || i == 3 || s->mb_x)
1979
992
                v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1980
1981
1020
            vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1982
1020
                                   (i & 4) ? v->codingset2 : v->codingset);
1983
            if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1984
                continue;
1985
1020
            v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1986
1020
            if (v->rangeredfrm)
1987
                for (j = 0; j < 64; j++)
1988
                    s->block[i][j] *= 2;
1989
1020
            s->idsp.put_signed_pixels_clamped(s->block[i],
1990
1020
                                              s->dest[dst_idx] + off,
1991
1020
                                              i & 4 ? s->uvlinesize
1992
                                                    : s->linesize);
1993
94092
        } else if (val) {
1994
172395
            int pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
1995
57465
                                         first_block, s->dest[dst_idx] + off,
1996
57465
                                         (i & 4) ? s->uvlinesize : s->linesize,
1997
                                         CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), NULL);
1998
57465
            if (pat < 0)
1999
                return pat;
2000

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









48
        GET_MQUANT();
2040
48
        s->current_picture.qscale_table[mb_pos] = mquant;
2041
        /* Set DC scale - y and c use the same (not sure if necessary here) */
2042
48
        s->y_dc_scale = s->y_dc_scale_table[FFABS(mquant)];
2043
48
        s->c_dc_scale = s->c_dc_scale_table[FFABS(mquant)];
2044
48
        v->s.ac_pred  = v->acpred_plane[mb_pos] = get_bits1(gb);
2045
48
        mb_has_coeffs = idx_mbmode & 1;
2046
48
        if (mb_has_coeffs)
2047
33
            cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
2048
48
        dst_idx = 0;
2049
336
        for (i = 0; i < 6; i++) {
2050
288
            v->a_avail = v->c_avail          = 0;
2051
288
            v->mb_type[0][s->block_index[i]] = 1;
2052
288
            s->dc_val[0][s->block_index[i]]  = 0;
2053
288
            dst_idx += i >> 2;
2054
288
            val = ((cbp >> (5 - i)) & 1);
2055

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

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

26581
                switch (bmvtype) {
2092
13925
                case 0:
2093
13925
                    bmvtype = BMV_TYPE_BACKWARD;
2094
13925
                    break;
2095
6770
                case 1:
2096
6770
                    bmvtype = BMV_TYPE_DIRECT;
2097
6770
                    break;
2098
5886
                case 2:
2099
5886
                    bmvtype   = BMV_TYPE_INTERPOLATED;
2100
5886
                    interpmvp = get_bits1(gb);
2101
                }
2102
42084
            }
2103
42084
            v->bmvtype = bmvtype;
2104

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









30972
            GET_MQUANT();
2144
        }
2145
51492
        s->current_picture.qscale_table[mb_pos] = mquant;
2146

51492
        if (!v->ttmbf && cbp) {
2147
30972
            ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2148
        }
2149
51492
        dst_idx = 0;
2150
360444
        for (i = 0; i < 6; i++) {
2151
308952
            s->dc_val[0][s->block_index[i]] = 0;
2152
308952
            dst_idx += i >> 2;
2153
308952
            val = ((cbp >> (5 - i)) & 1);
2154
308952
            off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
2155
308952
            if (val) {
2156
274410
                pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2157
91470
                                         first_block, s->dest[dst_idx] + off,
2158
91470
                                         (i & 4) ? s->uvlinesize : s->linesize,
2159
                                         CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
2160
91470
                if (pat < 0)
2161
                    return pat;
2162
91470
                block_cbp |= pat << (i << 2);
2163

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









101
        GET_MQUANT();
2238
101
        s->current_picture.qscale_table[mb_pos] = mquant;
2239
        /* Set DC scale - y and c use the same (not sure if necessary here) */
2240
101
        s->y_dc_scale = s->y_dc_scale_table[FFABS(mquant)];
2241
101
        s->c_dc_scale = s->c_dc_scale_table[FFABS(mquant)];
2242
101
        dst_idx = 0;
2243
707
        for (i = 0; i < 6; i++) {
2244
606
            v->a_avail = v->c_avail          = 0;
2245
606
            v->mb_type[0][s->block_index[i]] = 1;
2246
606
            s->dc_val[0][s->block_index[i]]  = 0;
2247
606
            dst_idx += i >> 2;
2248
606
            val = ((cbp >> (5 - i)) & 1);
2249

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

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

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

24065
                switch (bmvtype) {
2312
14341
                case 0:
2313
14341
                    bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
2314
14341
                    break;
2315
6232
                case 1:
2316
6232
                    bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
2317
6232
                    break;
2318
3492
                case 2:
2319
3492
                    bmvtype  = BMV_TYPE_INTERPOLATED;
2320
                }
2321
            }
2322
2323

24065
            if (twomv && bmvtype != BMV_TYPE_INTERPOLATED)
2324
12364
                mvsw = get_bits1(gb);
2325
        }
2326
2327
32539
        if (!skipped) { // inter MB
2328
30997
            mb_has_coeffs = ff_vc1_mbmode_intfrp[0][idx_mbmode][3];
2329
30997
            if (mb_has_coeffs)
2330
26859
                cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2331
30997
            if (!direct) {
2332

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

23128
                } else if (bmvtype == BMV_TYPE_INTERPOLATED || twomv) {
2335
15125
                    v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
2336
                }
2337
            }
2338
2339
216979
            for (i = 0; i < 6; i++)
2340
185982
                v->mb_type[0][s->block_index[i]] = 0;
2341
30997
            fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[0][idx_mbmode][1];
2342
            /* for all motion vector read MVDATA and motion compensate each block */
2343
30997
            dst_idx = 0;
2344
30997
            if (direct) {
2345
7147
                if (twomv) {
2346
21800
                    for (i = 0; i < 4; i++) {
2347
17440
                        ff_vc1_mc_4mv_luma(v, i, 0, 0);
2348
17440
                        ff_vc1_mc_4mv_luma(v, i, 1, 1);
2349
                    }
2350
4360
                    ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
2351
4360
                    ff_vc1_mc_4mv_chroma4(v, 1, 1, 1);
2352
                } else {
2353
2787
                    ff_vc1_mc_1mv(v, 0);
2354
2787
                    ff_vc1_interp_mc(v);
2355
                }
2356

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

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









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

30997
            if (!v->ttmbf && cbp)
2445
26859
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2446
216979
            for (i = 0; i < 6; i++) {
2447
185982
                s->dc_val[0][s->block_index[i]] = 0;
2448
185982
                dst_idx += i >> 2;
2449
185982
                val = ((cbp >> (5 - i)) & 1);
2450
185982
                if (!fieldtx)
2451
84336
                    off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2452
                else
2453

101646
                    off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
2454
185982
                if (val) {
2455
346863
                    pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2456
115621
                                             first_block, s->dest[dst_idx] + off,
2457
115621
                                             (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
2458
                                             CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
2459
115621
                    if (pat < 0)
2460
                        return pat;
2461
115621
                    block_cbp |= pat << (i << 2);
2462

115621
                    if (!v->ttmbf && ttmb < 8)
2463
92324
                        ttmb = -1;
2464
115621
                    first_block = 0;
2465
                }
2466
            }
2467
2468
        } else { // skipped
2469
1542
            dir = 0;
2470
10794
            for (i = 0; i < 6; i++) {
2471
9252
                v->mb_type[0][s->block_index[i]] = 0;
2472
9252
                s->dc_val[0][s->block_index[i]] = 0;
2473
            }
2474
1542
            s->current_picture.mb_type[mb_pos]      = MB_TYPE_SKIP;
2475
1542
            s->current_picture.qscale_table[mb_pos] = 0;
2476
1542
            v->blk_mv_type[s->block_index[0]] = 0;
2477
1542
            v->blk_mv_type[s->block_index[1]] = 0;
2478
1542
            v->blk_mv_type[s->block_index[2]] = 0;
2479
1542
            v->blk_mv_type[s->block_index[3]] = 0;
2480
2481
1542
            if (!direct) {
2482
215
                if (bmvtype == BMV_TYPE_INTERPOLATED) {
2483
9
                    ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
2484
9
                    ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 1);
2485
                } else {
2486
206
                    dir = bmvtype == BMV_TYPE_BACKWARD;
2487
206
                    ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], dir);
2488
206
                    if (mvsw) {
2489
                        int dir2 = dir;
2490
                        if (mvsw)
2491
                            dir2 = !dir;
2492
                        for (i = 0; i < 2; i++) {
2493
                            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];
2494
                            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];
2495
                            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];
2496
                            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];
2497
                        }
2498
                    } else {
2499
206
                        v->blk_mv_type[s->block_index[0]] = 1;
2500
206
                        v->blk_mv_type[s->block_index[1]] = 1;
2501
206
                        v->blk_mv_type[s->block_index[2]] = 1;
2502
206
                        v->blk_mv_type[s->block_index[3]] = 1;
2503
206
                        ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
2504
618
                        for (i = 0; i < 2; i++) {
2505
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];
2506
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];
2507
                        }
2508
                    }
2509
                }
2510
            }
2511
2512
1542
            ff_vc1_mc_1mv(v, dir);
2513

1542
            if (direct || bmvtype == BMV_TYPE_INTERPOLATED) {
2514
1336
                ff_vc1_interp_mc(v);
2515
            }
2516
1542
            v->fieldtx_plane[mb_pos] = 0;
2517
        }
2518
    }
2519
32640
    v->cbp[s->mb_x]      = block_cbp;
2520
32640
    v->ttblk[s->mb_x]    = block_tt;
2521
2522
32640
    return 0;
2523
}
2524
2525
/** Decode blocks of I-frame
2526
 */
2527
83
static void vc1_decode_i_blocks(VC1Context *v)
2528
{
2529
    int k, j;
2530
83
    MpegEncContext *s = &v->s;
2531
    int cbp, val;
2532
    uint8_t *coded_val;
2533
    int mb_pos;
2534
2535
    /* select coding mode used for VLC tables selection */
2536

83
    switch (v->y_ac_table_index) {
2537
5
    case 0:
2538
5
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2539
5
        break;
2540
70
    case 1:
2541
70
        v->codingset = CS_HIGH_MOT_INTRA;
2542
70
        break;
2543
8
    case 2:
2544
8
        v->codingset = CS_MID_RATE_INTRA;
2545
8
        break;
2546
    }
2547
2548

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

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

14
    switch (v->y_ac_table_index) {
2668
1
    case 0:
2669
1
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2670
1
        break;
2671
2
    case 1:
2672
2
        v->codingset = CS_HIGH_MOT_INTRA;
2673
2
        break;
2674
11
    case 2:
2675
11
        v->codingset = CS_MID_RATE_INTRA;
2676
11
        break;
2677
    }
2678
2679

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

12114
            if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
2731
                v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
2732
2733









12114
            GET_MQUANT();
2734
2735
12114
            s->current_picture.qscale_table[mb_pos] = mquant;
2736
            /* Set DC scale - y and c use the same */
2737
12114
            s->y_dc_scale = s->y_dc_scale_table[FFABS(mquant)];
2738
12114
            s->c_dc_scale = s->c_dc_scale_table[FFABS(mquant)];
2739
2740
84798
            for (k = 0; k < 6; k++) {
2741
72684
                v->mb_type[0][s->block_index[k]] = 1;
2742
2743
72684
                val = ((cbp >> (5 - k)) & 1);
2744
2745
72684
                if (k < 4) {
2746
48456
                    int pred   = vc1_coded_block_pred(&v->s, k, &coded_val);
2747
48456
                    val        = val ^ pred;
2748
48456
                    *coded_val = val;
2749
                }
2750
72684
                cbp |= val << (5 - k);
2751
2752

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

72684
                v->c_avail = !!s->mb_x || (k == 1 || k == 3);
2754
2755
72684
                vc1_decode_i_block_adv(v, v->block[v->cur_blk_idx][block_map[k]], k, val,
2756
                                       (k < 4) ? v->codingset : v->codingset2, mquant);
2757
2758
                if (CONFIG_GRAY && k > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2759
                    continue;
2760
72684
                v->vc1dsp.vc1_inv_trans_8x8(v->block[v->cur_blk_idx][block_map[k]]);
2761
            }
2762
2763

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

915
    switch (v->c_ac_table_index) {
2802
268
    case 0:
2803
268
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2804
268
        break;
2805
391
    case 1:
2806
391
        v->codingset = CS_HIGH_MOT_INTRA;
2807
391
        break;
2808
256
    case 2:
2809
256
        v->codingset = CS_MID_RATE_INTRA;
2810
256
        break;
2811
    }
2812
2813

915
    switch (v->c_ac_table_index) {
2814
268
    case 0:
2815
268
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2816
268
        break;
2817
391
    case 1:
2818
391
        v->codingset2 = CS_HIGH_MOT_INTER;
2819
391
        break;
2820
256
    case 2:
2821
256
        v->codingset2 = CS_MID_RATE_INTER;
2822
256
        break;
2823
    }
2824
2825

915
    apply_loop_filter   = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY);
2826
915
    s->first_slice_line = 1;
2827
915
    memset(v->cbp_base, 0, sizeof(v->cbp_base[0]) * 3 * s->mb_stride);
2828
6277
    for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2829
5362
        s->mb_x = 0;
2830
5362
        init_block_index(v);
2831
211511
        for (; s->mb_x < s->mb_width; s->mb_x++) {
2832
206149
            ff_update_block_index(s);
2833
2834


206149
            if (v->fcm == ILACE_FIELD || (v->fcm == PROGRESSIVE && v->mv_type_is_raw) || v->skip_is_raw)
2835
96015
                if (get_bits_left(&v->s.gb) <= 1) {
2836
                    ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2837
                    return;
2838
                }
2839
2840
206149
            if (v->fcm == ILACE_FIELD) {
2841
37245
                vc1_decode_p_mb_intfi(v);
2842
37245
                if (apply_loop_filter)
2843
37245
                    ff_vc1_p_loop_filter(v);
2844
168904
            } else if (v->fcm == ILACE_FRAME) {
2845
16320
                vc1_decode_p_mb_intfr(v);
2846
16320
                if (apply_loop_filter)
2847
16320
                    ff_vc1_p_intfr_loop_filter(v);
2848
            } else {
2849
152584
                vc1_decode_p_mb(v);
2850
152584
                if (apply_loop_filter)
2851
93304
                    ff_vc1_p_loop_filter(v);
2852
            }
2853

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

109
    switch (v->c_ac_table_index) {
2893
14
    case 0:
2894
14
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2895
14
        break;
2896
16
    case 1:
2897
16
        v->codingset = CS_HIGH_MOT_INTRA;
2898
16
        break;
2899
79
    case 2:
2900
79
        v->codingset = CS_MID_RATE_INTRA;
2901
79
        break;
2902
    }
2903
2904

109
    switch (v->c_ac_table_index) {
2905
14
    case 0:
2906
14
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2907
14
        break;
2908
16
    case 1:
2909
16
        v->codingset2 = CS_HIGH_MOT_INTER;
2910
16
        break;
2911
79
    case 2:
2912
79
        v->codingset2 = CS_MID_RATE_INTER;
2913
79
        break;
2914
    }
2915
2916
109
    s->first_slice_line = 1;
2917
1809
    for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2918
1700
        s->mb_x = 0;
2919
1700
        init_block_index(v);
2920
106820
        for (; s->mb_x < s->mb_width; s->mb_x++) {
2921
105120
            ff_update_block_index(s);
2922
2923

105120
            if (v->fcm == ILACE_FIELD || v->skip_is_raw || v->dmb_is_raw)
2924
51540
                if (get_bits_left(&v->s.gb) <= 1) {
2925
                    ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2926
                    return;
2927
                }
2928
2929
105120
            if (v->fcm == ILACE_FIELD) {
2930
51540
                vc1_decode_b_mb_intfi(v);
2931
51540
                if (v->s.loop_filter)
2932
51540
                    ff_vc1_b_intfi_loop_filter(v);
2933
53580
            } else if (v->fcm == ILACE_FRAME) {
2934
32640
                vc1_decode_b_mb_intfr(v);
2935
32640
                if (v->s.loop_filter)
2936
32640
                    ff_vc1_p_intfr_loop_filter(v);
2937
            } else {
2938
20940
                vc1_decode_b_mb(v);
2939
20940
                if (v->s.loop_filter)
2940
6090
                    ff_vc1_i_loop_filter(v);
2941
            }
2942

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

1188
        switch (v->s.pict_type) {
3012
97
        case AV_PICTURE_TYPE_I:
3013
97
            if (v->profile == PROFILE_ADVANCED)
3014
14
                vc1_decode_i_blocks_adv(v);
3015
            else
3016
83
                vc1_decode_i_blocks(v);
3017
97
            break;
3018
982
        case AV_PICTURE_TYPE_P:
3019
982
            if (v->p_frame_skipped)
3020
67
                vc1_decode_skip_blocks(v);
3021
            else
3022
915
                vc1_decode_p_blocks(v);
3023
982
            break;
3024
109
        case AV_PICTURE_TYPE_B:
3025
109
            if (v->bi_type) {
3026
                if (v->profile == PROFILE_ADVANCED)
3027
                    vc1_decode_i_blocks_adv(v);
3028
                else
3029
                    vc1_decode_i_blocks(v);
3030
            } else
3031
109
                vc1_decode_b_blocks(v);
3032
109
            break;
3033
        }
3034
1188
    }
3035
1188
}