Statistics
| Branch: | Revision:

ffmpeg / libavcodec / vc1.c @ 199436b9

History | View | Annotate | Download (153 KB)

1
/*
2
 * VC-1 and WMV3 decoder
3
 * Copyright (c) 2006-2007 Konstantin Shishkov
4
 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
5
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22

    
23
/**
24
 * @file vc1.c
25
 * VC-1 and WMV3 decoder
26
 *
27
 */
28
#include "dsputil.h"
29
#include "avcodec.h"
30
#include "mpegvideo.h"
31
#include "vc1.h"
32
#include "vc1data.h"
33
#include "vc1acdata.h"
34
#include "msmpeg4data.h"
35
#include "unary.h"
36
#include "simple_idct.h"
37
#include "mathops.h"
38

    
39
#undef NDEBUG
40
#include <assert.h>
41

    
42
#define MB_INTRA_VLC_BITS 9
43
#define DC_VLC_BITS 9
44
#define AC_VLC_BITS 9
45
static const uint16_t table_mb_intra[64][2];
46

    
47

    
48
/**
49
 * Init VC-1 specific tables and VC1Context members
50
 * @param v The VC1Context to initialize
51
 * @return Status
52
 */
53
static int vc1_init_common(VC1Context *v)
54
{
55
    static int done = 0;
56
    int i = 0;
57

    
58
    v->hrd_rate = v->hrd_buffer = NULL;
59

    
60
    /* VLC tables */
61
    if(!done)
62
    {
63
        done = 1;
64
        init_vlc(&ff_vc1_bfraction_vlc, VC1_BFRACTION_VLC_BITS, 23,
65
                 ff_vc1_bfraction_bits, 1, 1,
66
                 ff_vc1_bfraction_codes, 1, 1, 1);
67
        init_vlc(&ff_vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4,
68
                 ff_vc1_norm2_bits, 1, 1,
69
                 ff_vc1_norm2_codes, 1, 1, 1);
70
        init_vlc(&ff_vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 64,
71
                 ff_vc1_norm6_bits, 1, 1,
72
                 ff_vc1_norm6_codes, 2, 2, 1);
73
        init_vlc(&ff_vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7,
74
                 ff_vc1_imode_bits, 1, 1,
75
                 ff_vc1_imode_codes, 1, 1, 1);
76
        for (i=0; i<3; i++)
77
        {
78
            init_vlc(&ff_vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 16,
79
                     ff_vc1_ttmb_bits[i], 1, 1,
80
                     ff_vc1_ttmb_codes[i], 2, 2, 1);
81
            init_vlc(&ff_vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 8,
82
                     ff_vc1_ttblk_bits[i], 1, 1,
83
                     ff_vc1_ttblk_codes[i], 1, 1, 1);
84
            init_vlc(&ff_vc1_subblkpat_vlc[i], VC1_SUBBLKPAT_VLC_BITS, 15,
85
                     ff_vc1_subblkpat_bits[i], 1, 1,
86
                     ff_vc1_subblkpat_codes[i], 1, 1, 1);
87
        }
88
        for(i=0; i<4; i++)
89
        {
90
            init_vlc(&ff_vc1_4mv_block_pattern_vlc[i], VC1_4MV_BLOCK_PATTERN_VLC_BITS, 16,
91
                     ff_vc1_4mv_block_pattern_bits[i], 1, 1,
92
                     ff_vc1_4mv_block_pattern_codes[i], 1, 1, 1);
93
            init_vlc(&ff_vc1_cbpcy_p_vlc[i], VC1_CBPCY_P_VLC_BITS, 64,
94
                     ff_vc1_cbpcy_p_bits[i], 1, 1,
95
                     ff_vc1_cbpcy_p_codes[i], 2, 2, 1);
96
            init_vlc(&ff_vc1_mv_diff_vlc[i], VC1_MV_DIFF_VLC_BITS, 73,
97
                     ff_vc1_mv_diff_bits[i], 1, 1,
98
                     ff_vc1_mv_diff_codes[i], 2, 2, 1);
99
        }
100
        for(i=0; i<8; i++)
101
            init_vlc(&ff_vc1_ac_coeff_table[i], AC_VLC_BITS, vc1_ac_sizes[i],
102
                     &vc1_ac_tables[i][0][1], 8, 4,
103
                     &vc1_ac_tables[i][0][0], 8, 4, 1);
104
        init_vlc(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64,
105
                 &ff_msmp4_mb_i_table[0][1], 4, 2,
106
                 &ff_msmp4_mb_i_table[0][0], 4, 2, 1);
107
    }
108

    
109
    /* Other defaults */
110
    v->pq = -1;
111
    v->mvrange = 0; /* 7.1.1.18, p80 */
112

    
113
    return 0;
114
}
115

    
116
/***********************************************************************/
117
/**
118
 * @defgroup bitplane VC9 Bitplane decoding
119
 * @see 8.7, p56
120
 * @{
121
 */
122

    
123
/** @addtogroup bitplane
124
 * Imode types
125
 * @{
126
 */
127
enum Imode {
128
    IMODE_RAW,
129
    IMODE_NORM2,
130
    IMODE_DIFF2,
131
    IMODE_NORM6,
132
    IMODE_DIFF6,
133
    IMODE_ROWSKIP,
134
    IMODE_COLSKIP
135
};
136
/** @} */ //imode defines
137

    
138
/** Decode rows by checking if they are skipped
139
 * @param plane Buffer to store decoded bits
140
 * @param[in] width Width of this buffer
141
 * @param[in] height Height of this buffer
142
 * @param[in] stride of this buffer
143
 */
144
static void decode_rowskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
145
    int x, y;
146

    
147
    for (y=0; y<height; y++){
148
        if (!get_bits1(gb)) //rowskip
149
            memset(plane, 0, width);
150
        else
151
            for (x=0; x<width; x++)
152
                plane[x] = get_bits1(gb);
153
        plane += stride;
154
    }
155
}
156

    
157
/** Decode columns by checking if they are skipped
158
 * @param plane Buffer to store decoded bits
159
 * @param[in] width Width of this buffer
160
 * @param[in] height Height of this buffer
161
 * @param[in] stride of this buffer
162
 * @todo FIXME: Optimize
163
 */
164
static void decode_colskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
165
    int x, y;
166

    
167
    for (x=0; x<width; x++){
168
        if (!get_bits1(gb)) //colskip
169
            for (y=0; y<height; y++)
170
                plane[y*stride] = 0;
171
        else
172
            for (y=0; y<height; y++)
173
                plane[y*stride] = get_bits1(gb);
174
        plane ++;
175
    }
176
}
177

    
178
/** Decode a bitplane's bits
179
 * @param bp Bitplane where to store the decode bits
180
 * @param v VC-1 context for bit reading and logging
181
 * @return Status
182
 * @todo FIXME: Optimize
183
 */
184
static int bitplane_decoding(uint8_t* data, int *raw_flag, VC1Context *v)
185
{
186
    GetBitContext *gb = &v->s.gb;
187

    
188
    int imode, x, y, code, offset;
189
    uint8_t invert, *planep = data;
190
    int width, height, stride;
191

    
192
    width = v->s.mb_width;
193
    height = v->s.mb_height;
194
    stride = v->s.mb_stride;
195
    invert = get_bits1(gb);
196
    imode = get_vlc2(gb, ff_vc1_imode_vlc.table, VC1_IMODE_VLC_BITS, 1);
197

    
198
    *raw_flag = 0;
199
    switch (imode)
200
    {
201
    case IMODE_RAW:
202
        //Data is actually read in the MB layer (same for all tests == "raw")
203
        *raw_flag = 1; //invert ignored
204
        return invert;
205
    case IMODE_DIFF2:
206
    case IMODE_NORM2:
207
        if ((height * width) & 1)
208
        {
209
            *planep++ = get_bits1(gb);
210
            offset = 1;
211
        }
212
        else offset = 0;
213
        // decode bitplane as one long line
214
        for (y = offset; y < height * width; y += 2) {
215
            code = get_vlc2(gb, ff_vc1_norm2_vlc.table, VC1_NORM2_VLC_BITS, 1);
216
            *planep++ = code & 1;
217
            offset++;
218
            if(offset == width) {
219
                offset = 0;
220
                planep += stride - width;
221
            }
222
            *planep++ = code >> 1;
223
            offset++;
224
            if(offset == width) {
225
                offset = 0;
226
                planep += stride - width;
227
            }
228
        }
229
        break;
230
    case IMODE_DIFF6:
231
    case IMODE_NORM6:
232
        if(!(height % 3) && (width % 3)) { // use 2x3 decoding
233
            for(y = 0; y < height; y+= 3) {
234
                for(x = width & 1; x < width; x += 2) {
235
                    code = get_vlc2(gb, ff_vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
236
                    if(code < 0){
237
                        av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
238
                        return -1;
239
                    }
240
                    planep[x + 0] = (code >> 0) & 1;
241
                    planep[x + 1] = (code >> 1) & 1;
242
                    planep[x + 0 + stride] = (code >> 2) & 1;
243
                    planep[x + 1 + stride] = (code >> 3) & 1;
244
                    planep[x + 0 + stride * 2] = (code >> 4) & 1;
245
                    planep[x + 1 + stride * 2] = (code >> 5) & 1;
246
                }
247
                planep += stride * 3;
248
            }
249
            if(width & 1) decode_colskip(data, 1, height, stride, &v->s.gb);
250
        } else { // 3x2
251
            planep += (height & 1) * stride;
252
            for(y = height & 1; y < height; y += 2) {
253
                for(x = width % 3; x < width; x += 3) {
254
                    code = get_vlc2(gb, ff_vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
255
                    if(code < 0){
256
                        av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
257
                        return -1;
258
                    }
259
                    planep[x + 0] = (code >> 0) & 1;
260
                    planep[x + 1] = (code >> 1) & 1;
261
                    planep[x + 2] = (code >> 2) & 1;
262
                    planep[x + 0 + stride] = (code >> 3) & 1;
263
                    planep[x + 1 + stride] = (code >> 4) & 1;
264
                    planep[x + 2 + stride] = (code >> 5) & 1;
265
                }
266
                planep += stride * 2;
267
            }
268
            x = width % 3;
269
            if(x) decode_colskip(data  ,             x, height    , stride, &v->s.gb);
270
            if(height & 1) decode_rowskip(data+x, width - x, 1, stride, &v->s.gb);
271
        }
272
        break;
273
    case IMODE_ROWSKIP:
274
        decode_rowskip(data, width, height, stride, &v->s.gb);
275
        break;
276
    case IMODE_COLSKIP:
277
        decode_colskip(data, width, height, stride, &v->s.gb);
278
        break;
279
    default: break;
280
    }
281

    
282
    /* Applying diff operator */
283
    if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6)
284
    {
285
        planep = data;
286
        planep[0] ^= invert;
287
        for (x=1; x<width; x++)
288
            planep[x] ^= planep[x-1];
289
        for (y=1; y<height; y++)
290
        {
291
            planep += stride;
292
            planep[0] ^= planep[-stride];
293
            for (x=1; x<width; x++)
294
            {
295
                if (planep[x-1] != planep[x-stride]) planep[x] ^= invert;
296
                else                                 planep[x] ^= planep[x-1];
297
            }
298
        }
299
    }
300
    else if (invert)
301
    {
302
        planep = data;
303
        for (x=0; x<stride*height; x++) planep[x] = !planep[x]; //FIXME stride
304
    }
305
    return (imode<<1) + invert;
306
}
307

    
308
/** @} */ //Bitplane group
309

    
310
#define FILTSIGN(a) ((a) >= 0 ? 1 : -1)
311
/**
312
 * VC-1 in-loop deblocking filter for one line
313
 * @param src source block type
314
 * @param pq block quantizer
315
 * @return whether other 3 pairs should be filtered or not
316
 * @see 8.6
317
 */
318
static int av_always_inline vc1_filter_line(uint8_t* src, int stride, int pq){
319
    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
320

    
321
    int a0 = (2*(src[-2*stride] - src[ 1*stride]) - 5*(src[-1*stride] - src[ 0*stride]) + 4) >> 3;
322
    int a0_sign = a0 >> 31;        /* Store sign */
323
    a0 = (a0 ^ a0_sign) - a0_sign; /* a0 = FFABS(a0); */
324
    if(a0 < pq){
325
        int a1 = FFABS((2*(src[-4*stride] - src[-1*stride]) - 5*(src[-3*stride] - src[-2*stride]) + 4) >> 3);
326
        int a2 = FFABS((2*(src[ 0*stride] - src[ 3*stride]) - 5*(src[ 1*stride] - src[ 2*stride]) + 4) >> 3);
327
        if(a1 < a0 || a2 < a0){
328
            int clip = src[-1*stride] - src[ 0*stride];
329
            int clip_sign = clip >> 31;
330
            clip = ((clip ^ clip_sign) - clip_sign)>>1;
331
            if(clip){
332
                int a3 = FFMIN(a1, a2);
333
                int d = 5 * (a3 - a0);
334
                int d_sign = (d >> 31);
335
                d = ((d ^ d_sign) - d_sign) >> 3;
336
                d_sign ^= a0_sign;
337

    
338
                if( d_sign ^ clip_sign )
339
                    d = 0;
340
                else{
341
                    d = FFMIN(d, clip);
342
                    d = (d ^ d_sign) - d_sign;          /* Restore sign */
343
                    src[-1*stride] = cm[src[-1*stride] - d];
344
                    src[ 0*stride] = cm[src[ 0*stride] + d];
345
                }
346
                return 1;
347
            }
348
        }
349
    }
350
    return 0;
351
}
352

    
353
/**
354
 * VC-1 in-loop deblocking filter
355
 * @param src source block type
356
 * @param len edge length to filter (4 or 8 pixels)
357
 * @param pq block quantizer
358
 * @see 8.6
359
 */
360
static void vc1_loop_filter(uint8_t* src, int step, int stride, int len, int pq)
361
{
362
    int i;
363
    int filt3;
364

    
365
    for(i = 0; i < len; i += 4){
366
        filt3 = vc1_filter_line(src + 2*step, stride, pq);
367
        if(filt3){
368
            vc1_filter_line(src + 0*step, stride, pq);
369
            vc1_filter_line(src + 1*step, stride, pq);
370
            vc1_filter_line(src + 3*step, stride, pq);
371
        }
372
        src += step * 4;
373
    }
374
}
375

    
376
static void vc1_loop_filter_iblk(MpegEncContext *s, int pq)
377
{
378
    int i, j;
379
    if(!s->first_slice_line)
380
        vc1_loop_filter(s->dest[0], 1, s->linesize, 16, pq);
381
    vc1_loop_filter(s->dest[0] + 8*s->linesize, 1, s->linesize, 16, pq);
382
    for(i = !s->mb_x*8; i < 16; i += 8)
383
        vc1_loop_filter(s->dest[0] + i, s->linesize, 1, 16, pq);
384
    for(j = 0; j < 2; j++){
385
        if(!s->first_slice_line)
386
            vc1_loop_filter(s->dest[j+1], 1, s->uvlinesize, 8, pq);
387
        if(s->mb_x)
388
            vc1_loop_filter(s->dest[j+1], s->uvlinesize, 1, 8, pq);
389
    }
390
}
391

    
392
/***********************************************************************/
393
/** VOP Dquant decoding
394
 * @param v VC-1 Context
395
 */
396
static int vop_dquant_decoding(VC1Context *v)
397
{
398
    GetBitContext *gb = &v->s.gb;
399
    int pqdiff;
400

    
401
    //variable size
402
    if (v->dquant == 2)
403
    {
404
        pqdiff = get_bits(gb, 3);
405
        if (pqdiff == 7) v->altpq = get_bits(gb, 5);
406
        else v->altpq = v->pq + pqdiff + 1;
407
    }
408
    else
409
    {
410
        v->dquantfrm = get_bits1(gb);
411
        if ( v->dquantfrm )
412
        {
413
            v->dqprofile = get_bits(gb, 2);
414
            switch (v->dqprofile)
415
            {
416
            case DQPROFILE_SINGLE_EDGE:
417
            case DQPROFILE_DOUBLE_EDGES:
418
                v->dqsbedge = get_bits(gb, 2);
419
                break;
420
            case DQPROFILE_ALL_MBS:
421
                v->dqbilevel = get_bits1(gb);
422
                if(!v->dqbilevel)
423
                    v->halfpq = 0;
424
            default: break; //Forbidden ?
425
            }
426
            if (v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS)
427
            {
428
                pqdiff = get_bits(gb, 3);
429
                if (pqdiff == 7) v->altpq = get_bits(gb, 5);
430
                else v->altpq = v->pq + pqdiff + 1;
431
            }
432
        }
433
    }
434
    return 0;
435
}
436

    
437
/** Put block onto picture
438
 */
439
static void vc1_put_block(VC1Context *v, DCTELEM block[6][64])
440
{
441
    uint8_t *Y;
442
    int ys, us, vs;
443
    DSPContext *dsp = &v->s.dsp;
444

    
445
    if(v->rangeredfrm) {
446
        int i, j, k;
447
        for(k = 0; k < 6; k++)
448
            for(j = 0; j < 8; j++)
449
                for(i = 0; i < 8; i++)
450
                    block[k][i + j*8] = ((block[k][i + j*8] - 128) << 1) + 128;
451

    
452
    }
453
    ys = v->s.current_picture.linesize[0];
454
    us = v->s.current_picture.linesize[1];
455
    vs = v->s.current_picture.linesize[2];
456
    Y = v->s.dest[0];
457

    
458
    dsp->put_pixels_clamped(block[0], Y, ys);
459
    dsp->put_pixels_clamped(block[1], Y + 8, ys);
460
    Y += ys * 8;
461
    dsp->put_pixels_clamped(block[2], Y, ys);
462
    dsp->put_pixels_clamped(block[3], Y + 8, ys);
463

    
464
    if(!(v->s.flags & CODEC_FLAG_GRAY)) {
465
        dsp->put_pixels_clamped(block[4], v->s.dest[1], us);
466
        dsp->put_pixels_clamped(block[5], v->s.dest[2], vs);
467
    }
468
}
469

    
470
/** Do motion compensation over 1 macroblock
471
 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
472
 */
473
static void vc1_mc_1mv(VC1Context *v, int dir)
474
{
475
    MpegEncContext *s = &v->s;
476
    DSPContext *dsp = &v->s.dsp;
477
    uint8_t *srcY, *srcU, *srcV;
478
    int dxy, uvdxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
479

    
480
    if(!v->s.last_picture.data[0])return;
481

    
482
    mx = s->mv[dir][0][0];
483
    my = s->mv[dir][0][1];
484

    
485
    // store motion vectors for further use in B frames
486
    if(s->pict_type == FF_P_TYPE) {
487
        s->current_picture.motion_val[1][s->block_index[0]][0] = mx;
488
        s->current_picture.motion_val[1][s->block_index[0]][1] = my;
489
    }
490
    uvmx = (mx + ((mx & 3) == 3)) >> 1;
491
    uvmy = (my + ((my & 3) == 3)) >> 1;
492
    if(v->fastuvmc) {
493
        uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
494
        uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
495
    }
496
    if(!dir) {
497
        srcY = s->last_picture.data[0];
498
        srcU = s->last_picture.data[1];
499
        srcV = s->last_picture.data[2];
500
    } else {
501
        srcY = s->next_picture.data[0];
502
        srcU = s->next_picture.data[1];
503
        srcV = s->next_picture.data[2];
504
    }
505

    
506
    src_x = s->mb_x * 16 + (mx >> 2);
507
    src_y = s->mb_y * 16 + (my >> 2);
508
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
509
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
510

    
511
    if(v->profile != PROFILE_ADVANCED){
512
        src_x   = av_clip(  src_x, -16, s->mb_width  * 16);
513
        src_y   = av_clip(  src_y, -16, s->mb_height * 16);
514
        uvsrc_x = av_clip(uvsrc_x,  -8, s->mb_width  *  8);
515
        uvsrc_y = av_clip(uvsrc_y,  -8, s->mb_height *  8);
516
    }else{
517
        src_x   = av_clip(  src_x, -17, s->avctx->coded_width);
518
        src_y   = av_clip(  src_y, -18, s->avctx->coded_height + 1);
519
        uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
520
        uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
521
    }
522

    
523
    srcY += src_y * s->linesize + src_x;
524
    srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
525
    srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
526

    
527
    /* for grayscale we should not try to read from unknown area */
528
    if(s->flags & CODEC_FLAG_GRAY) {
529
        srcU = s->edge_emu_buffer + 18 * s->linesize;
530
        srcV = s->edge_emu_buffer + 18 * s->linesize;
531
    }
532

    
533
    if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
534
       || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3
535
       || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){
536
        uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
537

    
538
        srcY -= s->mspel * (1 + s->linesize);
539
        ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
540
                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
541
        srcY = s->edge_emu_buffer;
542
        ff_emulated_edge_mc(uvbuf     , srcU, s->uvlinesize, 8+1, 8+1,
543
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
544
        ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
545
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
546
        srcU = uvbuf;
547
        srcV = uvbuf + 16;
548
        /* if we deal with range reduction we need to scale source blocks */
549
        if(v->rangeredfrm) {
550
            int i, j;
551
            uint8_t *src, *src2;
552

    
553
            src = srcY;
554
            for(j = 0; j < 17 + s->mspel*2; j++) {
555
                for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
556
                src += s->linesize;
557
            }
558
            src = srcU; src2 = srcV;
559
            for(j = 0; j < 9; j++) {
560
                for(i = 0; i < 9; i++) {
561
                    src[i] = ((src[i] - 128) >> 1) + 128;
562
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
563
                }
564
                src += s->uvlinesize;
565
                src2 += s->uvlinesize;
566
            }
567
        }
568
        /* if we deal with intensity compensation we need to scale source blocks */
569
        if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
570
            int i, j;
571
            uint8_t *src, *src2;
572

    
573
            src = srcY;
574
            for(j = 0; j < 17 + s->mspel*2; j++) {
575
                for(i = 0; i < 17 + s->mspel*2; i++) src[i] = v->luty[src[i]];
576
                src += s->linesize;
577
            }
578
            src = srcU; src2 = srcV;
579
            for(j = 0; j < 9; j++) {
580
                for(i = 0; i < 9; i++) {
581
                    src[i] = v->lutuv[src[i]];
582
                    src2[i] = v->lutuv[src2[i]];
583
                }
584
                src += s->uvlinesize;
585
                src2 += s->uvlinesize;
586
            }
587
        }
588
        srcY += s->mspel * (1 + s->linesize);
589
    }
590

    
591
    if(s->mspel) {
592
        dxy = ((my & 3) << 2) | (mx & 3);
593
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0]    , srcY    , s->linesize, v->rnd);
594
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
595
        srcY += s->linesize * 8;
596
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize    , srcY    , s->linesize, v->rnd);
597
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
598
    } else { // hpel mc - always used for luma
599
        dxy = (my & 2) | ((mx & 2) >> 1);
600

    
601
        if(!v->rnd)
602
            dsp->put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
603
        else
604
            dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
605
    }
606

    
607
    if(s->flags & CODEC_FLAG_GRAY) return;
608
    /* Chroma MC always uses qpel bilinear */
609
    uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
610
    uvmx = (uvmx&3)<<1;
611
    uvmy = (uvmy&3)<<1;
612
    if(!v->rnd){
613
        dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
614
        dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
615
    }else{
616
        dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
617
        dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
618
    }
619
}
620

    
621
/** Do motion compensation for 4-MV macroblock - luminance block
622
 */
623
static void vc1_mc_4mv_luma(VC1Context *v, int n)
624
{
625
    MpegEncContext *s = &v->s;
626
    DSPContext *dsp = &v->s.dsp;
627
    uint8_t *srcY;
628
    int dxy, mx, my, src_x, src_y;
629
    int off;
630

    
631
    if(!v->s.last_picture.data[0])return;
632
    mx = s->mv[0][n][0];
633
    my = s->mv[0][n][1];
634
    srcY = s->last_picture.data[0];
635

    
636
    off = s->linesize * 4 * (n&2) + (n&1) * 8;
637

    
638
    src_x = s->mb_x * 16 + (n&1) * 8 + (mx >> 2);
639
    src_y = s->mb_y * 16 + (n&2) * 4 + (my >> 2);
640

    
641
    if(v->profile != PROFILE_ADVANCED){
642
        src_x   = av_clip(  src_x, -16, s->mb_width  * 16);
643
        src_y   = av_clip(  src_y, -16, s->mb_height * 16);
644
    }else{
645
        src_x   = av_clip(  src_x, -17, s->avctx->coded_width);
646
        src_y   = av_clip(  src_y, -18, s->avctx->coded_height + 1);
647
    }
648

    
649
    srcY += src_y * s->linesize + src_x;
650

    
651
    if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
652
       || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 8 - s->mspel*2
653
       || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 8 - s->mspel*2){
654
        srcY -= s->mspel * (1 + s->linesize);
655
        ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 9+s->mspel*2, 9+s->mspel*2,
656
                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
657
        srcY = s->edge_emu_buffer;
658
        /* if we deal with range reduction we need to scale source blocks */
659
        if(v->rangeredfrm) {
660
            int i, j;
661
            uint8_t *src;
662

    
663
            src = srcY;
664
            for(j = 0; j < 9 + s->mspel*2; j++) {
665
                for(i = 0; i < 9 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
666
                src += s->linesize;
667
            }
668
        }
669
        /* if we deal with intensity compensation we need to scale source blocks */
670
        if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
671
            int i, j;
672
            uint8_t *src;
673

    
674
            src = srcY;
675
            for(j = 0; j < 9 + s->mspel*2; j++) {
676
                for(i = 0; i < 9 + s->mspel*2; i++) src[i] = v->luty[src[i]];
677
                src += s->linesize;
678
            }
679
        }
680
        srcY += s->mspel * (1 + s->linesize);
681
    }
682

    
683
    if(s->mspel) {
684
        dxy = ((my & 3) << 2) | (mx & 3);
685
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, v->rnd);
686
    } else { // hpel mc - always used for luma
687
        dxy = (my & 2) | ((mx & 2) >> 1);
688
        if(!v->rnd)
689
            dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
690
        else
691
            dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
692
    }
693
}
694

    
695
static inline int median4(int a, int b, int c, int d)
696
{
697
    if(a < b) {
698
        if(c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
699
        else      return (FFMIN(b, c) + FFMAX(a, d)) / 2;
700
    } else {
701
        if(c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
702
        else      return (FFMIN(a, c) + FFMAX(b, d)) / 2;
703
    }
704
}
705

    
706

    
707
/** Do motion compensation for 4-MV macroblock - both chroma blocks
708
 */
709
static void vc1_mc_4mv_chroma(VC1Context *v)
710
{
711
    MpegEncContext *s = &v->s;
712
    DSPContext *dsp = &v->s.dsp;
713
    uint8_t *srcU, *srcV;
714
    int uvdxy, uvmx, uvmy, uvsrc_x, uvsrc_y;
715
    int i, idx, tx = 0, ty = 0;
716
    int mvx[4], mvy[4], intra[4];
717
    static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
718

    
719
    if(!v->s.last_picture.data[0])return;
720
    if(s->flags & CODEC_FLAG_GRAY) return;
721

    
722
    for(i = 0; i < 4; i++) {
723
        mvx[i] = s->mv[0][i][0];
724
        mvy[i] = s->mv[0][i][1];
725
        intra[i] = v->mb_type[0][s->block_index[i]];
726
    }
727

    
728
    /* calculate chroma MV vector from four luma MVs */
729
    idx = (intra[3] << 3) | (intra[2] << 2) | (intra[1] << 1) | intra[0];
730
    if(!idx) { // all blocks are inter
731
        tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
732
        ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
733
    } else if(count[idx] == 1) { // 3 inter blocks
734
        switch(idx) {
735
        case 0x1:
736
            tx = mid_pred(mvx[1], mvx[2], mvx[3]);
737
            ty = mid_pred(mvy[1], mvy[2], mvy[3]);
738
            break;
739
        case 0x2:
740
            tx = mid_pred(mvx[0], mvx[2], mvx[3]);
741
            ty = mid_pred(mvy[0], mvy[2], mvy[3]);
742
            break;
743
        case 0x4:
744
            tx = mid_pred(mvx[0], mvx[1], mvx[3]);
745
            ty = mid_pred(mvy[0], mvy[1], mvy[3]);
746
            break;
747
        case 0x8:
748
            tx = mid_pred(mvx[0], mvx[1], mvx[2]);
749
            ty = mid_pred(mvy[0], mvy[1], mvy[2]);
750
            break;
751
        }
752
    } else if(count[idx] == 2) {
753
        int t1 = 0, t2 = 0;
754
        for(i=0; i<3;i++) if(!intra[i]) {t1 = i; break;}
755
        for(i= t1+1; i<4; i++)if(!intra[i]) {t2 = i; break;}
756
        tx = (mvx[t1] + mvx[t2]) / 2;
757
        ty = (mvy[t1] + mvy[t2]) / 2;
758
    } else {
759
        s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
760
        s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
761
        return; //no need to do MC for inter blocks
762
    }
763

    
764
    s->current_picture.motion_val[1][s->block_index[0]][0] = tx;
765
    s->current_picture.motion_val[1][s->block_index[0]][1] = ty;
766
    uvmx = (tx + ((tx&3) == 3)) >> 1;
767
    uvmy = (ty + ((ty&3) == 3)) >> 1;
768
    if(v->fastuvmc) {
769
        uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
770
        uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
771
    }
772

    
773
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
774
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
775

    
776
    if(v->profile != PROFILE_ADVANCED){
777
        uvsrc_x = av_clip(uvsrc_x,  -8, s->mb_width  *  8);
778
        uvsrc_y = av_clip(uvsrc_y,  -8, s->mb_height *  8);
779
    }else{
780
        uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
781
        uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
782
    }
783

    
784
    srcU = s->last_picture.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
785
    srcV = s->last_picture.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
786
    if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
787
       || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
788
       || (unsigned)uvsrc_y > (s->v_edge_pos >> 1) - 9){
789
        ff_emulated_edge_mc(s->edge_emu_buffer     , srcU, s->uvlinesize, 8+1, 8+1,
790
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
791
        ff_emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize, 8+1, 8+1,
792
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
793
        srcU = s->edge_emu_buffer;
794
        srcV = s->edge_emu_buffer + 16;
795

    
796
        /* if we deal with range reduction we need to scale source blocks */
797
        if(v->rangeredfrm) {
798
            int i, j;
799
            uint8_t *src, *src2;
800

    
801
            src = srcU; src2 = srcV;
802
            for(j = 0; j < 9; j++) {
803
                for(i = 0; i < 9; i++) {
804
                    src[i] = ((src[i] - 128) >> 1) + 128;
805
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
806
                }
807
                src += s->uvlinesize;
808
                src2 += s->uvlinesize;
809
            }
810
        }
811
        /* if we deal with intensity compensation we need to scale source blocks */
812
        if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
813
            int i, j;
814
            uint8_t *src, *src2;
815

    
816
            src = srcU; src2 = srcV;
817
            for(j = 0; j < 9; j++) {
818
                for(i = 0; i < 9; i++) {
819
                    src[i] = v->lutuv[src[i]];
820
                    src2[i] = v->lutuv[src2[i]];
821
                }
822
                src += s->uvlinesize;
823
                src2 += s->uvlinesize;
824
            }
825
        }
826
    }
827

    
828
    /* Chroma MC always uses qpel bilinear */
829
    uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
830
    uvmx = (uvmx&3)<<1;
831
    uvmy = (uvmy&3)<<1;
832
    if(!v->rnd){
833
        dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
834
        dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
835
    }else{
836
        dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
837
        dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
838
    }
839
}
840

    
841
static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb);
842

    
843
/**
844
 * Decode Simple/Main Profiles sequence header
845
 * @see Figure 7-8, p16-17
846
 * @param avctx Codec context
847
 * @param gb GetBit context initialized from Codec context extra_data
848
 * @return Status
849
 */
850
static int decode_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
851
{
852
    VC1Context *v = avctx->priv_data;
853

    
854
    av_log(avctx, AV_LOG_DEBUG, "Header: %0X\n", show_bits(gb, 32));
855
    v->profile = get_bits(gb, 2);
856
    if (v->profile == PROFILE_COMPLEX)
857
    {
858
        av_log(avctx, AV_LOG_ERROR, "WMV3 Complex Profile is not fully supported\n");
859
    }
860

    
861
    if (v->profile == PROFILE_ADVANCED)
862
    {
863
        v->zz_8x4 = ff_vc1_adv_progressive_8x4_zz;
864
        v->zz_4x8 = ff_vc1_adv_progressive_4x8_zz;
865
        return decode_sequence_header_adv(v, gb);
866
    }
867
    else
868
    {
869
        v->zz_8x4 = wmv2_scantableA;
870
        v->zz_4x8 = wmv2_scantableB;
871
        v->res_sm = get_bits(gb, 2); //reserved
872
        if (v->res_sm)
873
        {
874
            av_log(avctx, AV_LOG_ERROR,
875
                   "Reserved RES_SM=%i is forbidden\n", v->res_sm);
876
            return -1;
877
        }
878
    }
879

    
880
    // (fps-2)/4 (->30)
881
    v->frmrtq_postproc = get_bits(gb, 3); //common
882
    // (bitrate-32kbps)/64kbps
883
    v->bitrtq_postproc = get_bits(gb, 5); //common
884
    v->s.loop_filter = get_bits1(gb); //common
885
    if(v->s.loop_filter == 1 && v->profile == PROFILE_SIMPLE)
886
    {
887
        av_log(avctx, AV_LOG_ERROR,
888
               "LOOPFILTER shell not be enabled in simple profile\n");
889
    }
890
    if(v->s.avctx->skip_loop_filter >= AVDISCARD_ALL)
891
        v->s.loop_filter = 0;
892

    
893
    v->res_x8 = get_bits1(gb); //reserved
894
    v->multires = get_bits1(gb);
895
    v->res_fasttx = get_bits1(gb);
896
    if (!v->res_fasttx)
897
    {
898
        v->s.dsp.vc1_inv_trans_8x8 = ff_simple_idct;
899
        v->s.dsp.vc1_inv_trans_8x4 = ff_simple_idct84_add;
900
        v->s.dsp.vc1_inv_trans_4x8 = ff_simple_idct48_add;
901
        v->s.dsp.vc1_inv_trans_4x4 = ff_simple_idct44_add;
902
    }
903

    
904
    v->fastuvmc =  get_bits1(gb); //common
905
    if (!v->profile && !v->fastuvmc)
906
    {
907
        av_log(avctx, AV_LOG_ERROR,
908
               "FASTUVMC unavailable in Simple Profile\n");
909
        return -1;
910
    }
911
    v->extended_mv =  get_bits1(gb); //common
912
    if (!v->profile && v->extended_mv)
913
    {
914
        av_log(avctx, AV_LOG_ERROR,
915
               "Extended MVs unavailable in Simple Profile\n");
916
        return -1;
917
    }
918
    v->dquant =  get_bits(gb, 2); //common
919
    v->vstransform =  get_bits1(gb); //common
920

    
921
    v->res_transtab = get_bits1(gb);
922
    if (v->res_transtab)
923
    {
924
        av_log(avctx, AV_LOG_ERROR,
925
               "1 for reserved RES_TRANSTAB is forbidden\n");
926
        return -1;
927
    }
928

    
929
    v->overlap = get_bits1(gb); //common
930

    
931
    v->s.resync_marker = get_bits1(gb);
932
    v->rangered = get_bits1(gb);
933
    if (v->rangered && v->profile == PROFILE_SIMPLE)
934
    {
935
        av_log(avctx, AV_LOG_INFO,
936
               "RANGERED should be set to 0 in simple profile\n");
937
    }
938

    
939
    v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
940
    v->quantizer_mode = get_bits(gb, 2); //common
941

    
942
    v->finterpflag = get_bits1(gb); //common
943
    v->res_rtm_flag = get_bits1(gb); //reserved
944
    if (!v->res_rtm_flag)
945
    {
946
//            av_log(avctx, AV_LOG_ERROR,
947
//                   "0 for reserved RES_RTM_FLAG is forbidden\n");
948
        av_log(avctx, AV_LOG_ERROR,
949
               "Old WMV3 version detected, only I-frames will be decoded\n");
950
        //return -1;
951
    }
952
    //TODO: figure out what they mean (always 0x402F)
953
    if(!v->res_fasttx) skip_bits(gb, 16);
954
    av_log(avctx, AV_LOG_DEBUG,
955
               "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
956
               "LoopFilter=%i, MultiRes=%i, FastUVMC=%i, Extended MV=%i\n"
957
               "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
958
               "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
959
               v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
960
               v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
961
               v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
962
               v->dquant, v->quantizer_mode, avctx->max_b_frames
963
               );
964
    return 0;
965
}
966

    
967
static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb)
968
{
969
    v->res_rtm_flag = 1;
970
    v->level = get_bits(gb, 3);
971
    if(v->level >= 5)
972
    {
973
        av_log(v->s.avctx, AV_LOG_ERROR, "Reserved LEVEL %i\n",v->level);
974
    }
975
    v->chromaformat = get_bits(gb, 2);
976
    if (v->chromaformat != 1)
977
    {
978
        av_log(v->s.avctx, AV_LOG_ERROR,
979
               "Only 4:2:0 chroma format supported\n");
980
        return -1;
981
    }
982

    
983
    // (fps-2)/4 (->30)
984
    v->frmrtq_postproc = get_bits(gb, 3); //common
985
    // (bitrate-32kbps)/64kbps
986
    v->bitrtq_postproc = get_bits(gb, 5); //common
987
    v->postprocflag = get_bits1(gb); //common
988

    
989
    v->s.avctx->coded_width = (get_bits(gb, 12) + 1) << 1;
990
    v->s.avctx->coded_height = (get_bits(gb, 12) + 1) << 1;
991
    v->s.avctx->width = v->s.avctx->coded_width;
992
    v->s.avctx->height = v->s.avctx->coded_height;
993
    v->broadcast = get_bits1(gb);
994
    v->interlace = get_bits1(gb);
995
    v->tfcntrflag = get_bits1(gb);
996
    v->finterpflag = get_bits1(gb);
997
    skip_bits1(gb); // reserved
998

    
999
    v->s.h_edge_pos = v->s.avctx->coded_width;
1000
    v->s.v_edge_pos = v->s.avctx->coded_height;
1001

    
1002
    av_log(v->s.avctx, AV_LOG_DEBUG,
1003
               "Advanced Profile level %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
1004
               "LoopFilter=%i, ChromaFormat=%i, Pulldown=%i, Interlace: %i\n"
1005
               "TFCTRflag=%i, FINTERPflag=%i\n",
1006
               v->level, v->frmrtq_postproc, v->bitrtq_postproc,
1007
               v->s.loop_filter, v->chromaformat, v->broadcast, v->interlace,
1008
               v->tfcntrflag, v->finterpflag
1009
               );
1010

    
1011
    v->psf = get_bits1(gb);
1012
    if(v->psf) { //PsF, 6.1.13
1013
        av_log(v->s.avctx, AV_LOG_ERROR, "Progressive Segmented Frame mode: not supported (yet)\n");
1014
        return -1;
1015
    }
1016
    v->s.max_b_frames = v->s.avctx->max_b_frames = 7;
1017
    if(get_bits1(gb)) { //Display Info - decoding is not affected by it
1018
        int w, h, ar = 0;
1019
        av_log(v->s.avctx, AV_LOG_DEBUG, "Display extended info:\n");
1020
        v->s.avctx->coded_width  = w = get_bits(gb, 14) + 1;
1021
        v->s.avctx->coded_height = h = get_bits(gb, 14) + 1;
1022
        av_log(v->s.avctx, AV_LOG_DEBUG, "Display dimensions: %ix%i\n", w, h);
1023
        if(get_bits1(gb))
1024
            ar = get_bits(gb, 4);
1025
        if(ar && ar < 14){
1026
            v->s.avctx->sample_aspect_ratio = ff_vc1_pixel_aspect[ar];
1027
        }else if(ar == 15){
1028
            w = get_bits(gb, 8);
1029
            h = get_bits(gb, 8);
1030
            v->s.avctx->sample_aspect_ratio = (AVRational){w, h};
1031
        }
1032
        av_log(v->s.avctx, AV_LOG_DEBUG, "Aspect: %i:%i\n", v->s.avctx->sample_aspect_ratio.num, v->s.avctx->sample_aspect_ratio.den);
1033

    
1034
        if(get_bits1(gb)){ //framerate stuff
1035
            if(get_bits1(gb)) {
1036
                v->s.avctx->time_base.num = 32;
1037
                v->s.avctx->time_base.den = get_bits(gb, 16) + 1;
1038
            } else {
1039
                int nr, dr;
1040
                nr = get_bits(gb, 8);
1041
                dr = get_bits(gb, 4);
1042
                if(nr && nr < 8 && dr && dr < 3){
1043
                    v->s.avctx->time_base.num = ff_vc1_fps_dr[dr - 1];
1044
                    v->s.avctx->time_base.den = ff_vc1_fps_nr[nr - 1] * 1000;
1045
                }
1046
            }
1047
        }
1048

    
1049
        if(get_bits1(gb)){
1050
            v->color_prim = get_bits(gb, 8);
1051
            v->transfer_char = get_bits(gb, 8);
1052
            v->matrix_coef = get_bits(gb, 8);
1053
        }
1054
    }
1055

    
1056
    v->hrd_param_flag = get_bits1(gb);
1057
    if(v->hrd_param_flag) {
1058
        int i;
1059
        v->hrd_num_leaky_buckets = get_bits(gb, 5);
1060
        skip_bits(gb, 4); //bitrate exponent
1061
        skip_bits(gb, 4); //buffer size exponent
1062
        for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
1063
            skip_bits(gb, 16); //hrd_rate[n]
1064
            skip_bits(gb, 16); //hrd_buffer[n]
1065
        }
1066
    }
1067
    return 0;
1068
}
1069

    
1070
static int decode_entry_point(AVCodecContext *avctx, GetBitContext *gb)
1071
{
1072
    VC1Context *v = avctx->priv_data;
1073
    int i, blink, clentry;
1074

    
1075
    av_log(avctx, AV_LOG_DEBUG, "Entry point: %08X\n", show_bits_long(gb, 32));
1076
    blink = get_bits1(gb); // broken link
1077
    clentry = get_bits1(gb); // closed entry
1078
    v->panscanflag = get_bits1(gb);
1079
    v->refdist_flag = get_bits1(gb);
1080
    v->s.loop_filter = get_bits1(gb);
1081
    v->fastuvmc = get_bits1(gb);
1082
    v->extended_mv = get_bits1(gb);
1083
    v->dquant = get_bits(gb, 2);
1084
    v->vstransform = get_bits1(gb);
1085
    v->overlap = get_bits1(gb);
1086
    v->quantizer_mode = get_bits(gb, 2);
1087

    
1088
    if(v->hrd_param_flag){
1089
        for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
1090
            skip_bits(gb, 8); //hrd_full[n]
1091
        }
1092
    }
1093

    
1094
    if(get_bits1(gb)){
1095
        avctx->coded_width = (get_bits(gb, 12)+1)<<1;
1096
        avctx->coded_height = (get_bits(gb, 12)+1)<<1;
1097
    }
1098
    if(v->extended_mv)
1099
        v->extended_dmv = get_bits1(gb);
1100
    if((v->range_mapy_flag = get_bits1(gb))) {
1101
        av_log(avctx, AV_LOG_ERROR, "Luma scaling is not supported, expect wrong picture\n");
1102
        v->range_mapy = get_bits(gb, 3);
1103
    }
1104
    if((v->range_mapuv_flag = get_bits1(gb))) {
1105
        av_log(avctx, AV_LOG_ERROR, "Chroma scaling is not supported, expect wrong picture\n");
1106
        v->range_mapuv = get_bits(gb, 3);
1107
    }
1108

    
1109
    av_log(avctx, AV_LOG_DEBUG, "Entry point info:\n"
1110
        "BrokenLink=%i, ClosedEntry=%i, PanscanFlag=%i\n"
1111
        "RefDist=%i, Postproc=%i, FastUVMC=%i, ExtMV=%i\n"
1112
        "DQuant=%i, VSTransform=%i, Overlap=%i, Qmode=%i\n",
1113
        blink, clentry, v->panscanflag, v->refdist_flag, v->s.loop_filter,
1114
        v->fastuvmc, v->extended_mv, v->dquant, v->vstransform, v->overlap, v->quantizer_mode);
1115

    
1116
    return 0;
1117
}
1118

    
1119
static int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
1120
{
1121
    int pqindex, lowquant, status;
1122

    
1123
    if(v->finterpflag) v->interpfrm = get_bits1(gb);
1124
    skip_bits(gb, 2); //framecnt unused
1125
    v->rangeredfrm = 0;
1126
    if (v->rangered) v->rangeredfrm = get_bits1(gb);
1127
    v->s.pict_type = get_bits1(gb);
1128
    if (v->s.avctx->max_b_frames) {
1129
        if (!v->s.pict_type) {
1130
            if (get_bits1(gb)) v->s.pict_type = FF_I_TYPE;
1131
            else v->s.pict_type = FF_B_TYPE;
1132
        } else v->s.pict_type = FF_P_TYPE;
1133
    } else v->s.pict_type = v->s.pict_type ? FF_P_TYPE : FF_I_TYPE;
1134

    
1135
    v->bi_type = 0;
1136
    if(v->s.pict_type == FF_B_TYPE) {
1137
        v->bfraction = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
1138
        v->bfraction = ff_vc1_bfraction_lut[v->bfraction];
1139
        if(v->bfraction == 0) {
1140
            v->s.pict_type = FF_BI_TYPE;
1141
        }
1142
    }
1143
    if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)
1144
        skip_bits(gb, 7); // skip buffer fullness
1145

    
1146
    /* calculate RND */
1147
    if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)
1148
        v->rnd = 1;
1149
    if(v->s.pict_type == FF_P_TYPE)
1150
        v->rnd ^= 1;
1151

    
1152
    /* Quantizer stuff */
1153
    pqindex = get_bits(gb, 5);
1154
    if(!pqindex) return -1;
1155
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1156
        v->pq = ff_vc1_pquant_table[0][pqindex];
1157
    else
1158
        v->pq = ff_vc1_pquant_table[1][pqindex];
1159

    
1160
    v->pquantizer = 1;
1161
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1162
        v->pquantizer = pqindex < 9;
1163
    if (v->quantizer_mode == QUANT_NON_UNIFORM)
1164
        v->pquantizer = 0;
1165
    v->pqindex = pqindex;
1166
    if (pqindex < 9) v->halfpq = get_bits1(gb);
1167
    else v->halfpq = 0;
1168
    if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
1169
        v->pquantizer = get_bits1(gb);
1170
    v->dquantfrm = 0;
1171
    if (v->extended_mv == 1) v->mvrange = get_unary(gb, 0, 3);
1172
    v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1173
    v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
1174
    v->range_x = 1 << (v->k_x - 1);
1175
    v->range_y = 1 << (v->k_y - 1);
1176
    if (v->profile == PROFILE_ADVANCED)
1177
    {
1178
        if (v->postprocflag) v->postproc = get_bits1(gb);
1179
    }
1180
    else
1181
        if (v->multires && v->s.pict_type != FF_B_TYPE) v->respic = get_bits(gb, 2);
1182

    
1183
    if(v->res_x8 && (v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)){
1184
        v->x8_type = get_bits1(gb);
1185
    }else v->x8_type = 0;
1186
//av_log(v->s.avctx, AV_LOG_INFO, "%c Frame: QP=[%i]%i (+%i/2) %i\n",
1187
//        (v->s.pict_type == FF_P_TYPE) ? 'P' : ((v->s.pict_type == FF_I_TYPE) ? 'I' : 'B'), pqindex, v->pq, v->halfpq, v->rangeredfrm);
1188

    
1189
    if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_P_TYPE) v->use_ic = 0;
1190

    
1191
    switch(v->s.pict_type) {
1192
    case FF_P_TYPE:
1193
        if (v->pq < 5) v->tt_index = 0;
1194
        else if(v->pq < 13) v->tt_index = 1;
1195
        else v->tt_index = 2;
1196

    
1197
        lowquant = (v->pq > 12) ? 0 : 1;
1198
        v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
1199
        if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
1200
        {
1201
            int scale, shift, i;
1202
            v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
1203
            v->lumscale = get_bits(gb, 6);
1204
            v->lumshift = get_bits(gb, 6);
1205
            v->use_ic = 1;
1206
            /* fill lookup tables for intensity compensation */
1207
            if(!v->lumscale) {
1208
                scale = -64;
1209
                shift = (255 - v->lumshift * 2) << 6;
1210
                if(v->lumshift > 31)
1211
                    shift += 128 << 6;
1212
            } else {
1213
                scale = v->lumscale + 32;
1214
                if(v->lumshift > 31)
1215
                    shift = (v->lumshift - 64) << 6;
1216
                else
1217
                    shift = v->lumshift << 6;
1218
            }
1219
            for(i = 0; i < 256; i++) {
1220
                v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
1221
                v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
1222
            }
1223
        }
1224
        if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
1225
            v->s.quarter_sample = 0;
1226
        else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1227
            if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
1228
                v->s.quarter_sample = 0;
1229
            else
1230
                v->s.quarter_sample = 1;
1231
        } else
1232
            v->s.quarter_sample = 1;
1233
        v->s.mspel = !(v->mv_mode == MV_PMODE_1MV_HPEL_BILIN || (v->mv_mode == MV_PMODE_INTENSITY_COMP && v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN));
1234

    
1235
        if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
1236
                 v->mv_mode2 == MV_PMODE_MIXED_MV)
1237
                || v->mv_mode == MV_PMODE_MIXED_MV)
1238
        {
1239
            status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
1240
            if (status < 0) return -1;
1241
            av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
1242
                   "Imode: %i, Invert: %i\n", status>>1, status&1);
1243
        } else {
1244
            v->mv_type_is_raw = 0;
1245
            memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
1246
        }
1247
        status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1248
        if (status < 0) return -1;
1249
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1250
               "Imode: %i, Invert: %i\n", status>>1, status&1);
1251

    
1252
        /* Hopefully this is correct for P frames */
1253
        v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
1254
        v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1255

    
1256
        if (v->dquant)
1257
        {
1258
            av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1259
            vop_dquant_decoding(v);
1260
        }
1261

    
1262
        v->ttfrm = 0; //FIXME Is that so ?
1263
        if (v->vstransform)
1264
        {
1265
            v->ttmbf = get_bits1(gb);
1266
            if (v->ttmbf)
1267
            {
1268
                v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
1269
            }
1270
        } else {
1271
            v->ttmbf = 1;
1272
            v->ttfrm = TT_8X8;
1273
        }
1274
        break;
1275
    case FF_B_TYPE:
1276
        if (v->pq < 5) v->tt_index = 0;
1277
        else if(v->pq < 13) v->tt_index = 1;
1278
        else v->tt_index = 2;
1279

    
1280
        lowquant = (v->pq > 12) ? 0 : 1;
1281
        v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
1282
        v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
1283
        v->s.mspel = v->s.quarter_sample;
1284

    
1285
        status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
1286
        if (status < 0) return -1;
1287
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
1288
               "Imode: %i, Invert: %i\n", status>>1, status&1);
1289
        status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1290
        if (status < 0) return -1;
1291
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1292
               "Imode: %i, Invert: %i\n", status>>1, status&1);
1293

    
1294
        v->s.mv_table_index = get_bits(gb, 2);
1295
        v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1296

    
1297
        if (v->dquant)
1298
        {
1299
            av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1300
            vop_dquant_decoding(v);
1301
        }
1302

    
1303
        v->ttfrm = 0;
1304
        if (v->vstransform)
1305
        {
1306
            v->ttmbf = get_bits1(gb);
1307
            if (v->ttmbf)
1308
            {
1309
                v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
1310
            }
1311
        } else {
1312
            v->ttmbf = 1;
1313
            v->ttfrm = TT_8X8;
1314
        }
1315
        break;
1316
    }
1317

    
1318
    if(!v->x8_type)
1319
    {
1320
        /* AC Syntax */
1321
        v->c_ac_table_index = decode012(gb);
1322
        if (v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)
1323
        {
1324
            v->y_ac_table_index = decode012(gb);
1325
        }
1326
        /* DC Syntax */
1327
        v->s.dc_table_index = get_bits1(gb);
1328
    }
1329

    
1330
    if(v->s.pict_type == FF_BI_TYPE) {
1331
        v->s.pict_type = FF_B_TYPE;
1332
        v->bi_type = 1;
1333
    }
1334
    return 0;
1335
}
1336

    
1337
static int vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb)
1338
{
1339
    int pqindex, lowquant;
1340
    int status;
1341

    
1342
    v->p_frame_skipped = 0;
1343

    
1344
    if(v->interlace){
1345
        v->fcm = decode012(gb);
1346
        if(v->fcm) return -1; // interlaced frames/fields are not implemented
1347
    }
1348
    switch(get_unary(gb, 0, 4)) {
1349
    case 0:
1350
        v->s.pict_type = FF_P_TYPE;
1351
        break;
1352
    case 1:
1353
        v->s.pict_type = FF_B_TYPE;
1354
        break;
1355
    case 2:
1356
        v->s.pict_type = FF_I_TYPE;
1357
        break;
1358
    case 3:
1359
        v->s.pict_type = FF_BI_TYPE;
1360
        break;
1361
    case 4:
1362
        v->s.pict_type = FF_P_TYPE; // skipped pic
1363
        v->p_frame_skipped = 1;
1364
        return 0;
1365
    }
1366
    if(v->tfcntrflag)
1367
        skip_bits(gb, 8);
1368
    if(v->broadcast) {
1369
        if(!v->interlace || v->psf) {
1370
            v->rptfrm = get_bits(gb, 2);
1371
        } else {
1372
            v->tff = get_bits1(gb);
1373
            v->rptfrm = get_bits1(gb);
1374
        }
1375
    }
1376
    if(v->panscanflag) {
1377
        //...
1378
    }
1379
    v->rnd = get_bits1(gb);
1380
    if(v->interlace)
1381
        v->uvsamp = get_bits1(gb);
1382
    if(v->finterpflag) v->interpfrm = get_bits1(gb);
1383
    if(v->s.pict_type == FF_B_TYPE) {
1384
        v->bfraction = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
1385
        v->bfraction = ff_vc1_bfraction_lut[v->bfraction];
1386
        if(v->bfraction == 0) {
1387
            v->s.pict_type = FF_BI_TYPE; /* XXX: should not happen here */
1388
        }
1389
    }
1390
    pqindex = get_bits(gb, 5);
1391
    if(!pqindex) return -1;
1392
    v->pqindex = pqindex;
1393
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1394
        v->pq = ff_vc1_pquant_table[0][pqindex];
1395
    else
1396
        v->pq = ff_vc1_pquant_table[1][pqindex];
1397

    
1398
    v->pquantizer = 1;
1399
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1400
        v->pquantizer = pqindex < 9;
1401
    if (v->quantizer_mode == QUANT_NON_UNIFORM)
1402
        v->pquantizer = 0;
1403
    v->pqindex = pqindex;
1404
    if (pqindex < 9) v->halfpq = get_bits1(gb);
1405
    else v->halfpq = 0;
1406
    if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
1407
        v->pquantizer = get_bits1(gb);
1408
    if(v->postprocflag)
1409
        v->postproc = get_bits1(gb);
1410

    
1411
    if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_P_TYPE) v->use_ic = 0;
1412

    
1413
    switch(v->s.pict_type) {
1414
    case FF_I_TYPE:
1415
    case FF_BI_TYPE:
1416
        status = bitplane_decoding(v->acpred_plane, &v->acpred_is_raw, v);
1417
        if (status < 0) return -1;
1418
        av_log(v->s.avctx, AV_LOG_DEBUG, "ACPRED plane encoding: "
1419
                "Imode: %i, Invert: %i\n", status>>1, status&1);
1420
        v->condover = CONDOVER_NONE;
1421
        if(v->overlap && v->pq <= 8) {
1422
            v->condover = decode012(gb);
1423
            if(v->condover == CONDOVER_SELECT) {
1424
                status = bitplane_decoding(v->over_flags_plane, &v->overflg_is_raw, v);
1425
                if (status < 0) return -1;
1426
                av_log(v->s.avctx, AV_LOG_DEBUG, "CONDOVER plane encoding: "
1427
                        "Imode: %i, Invert: %i\n", status>>1, status&1);
1428
            }
1429
        }
1430
        break;
1431
    case FF_P_TYPE:
1432
        if (v->extended_mv) v->mvrange = get_unary(gb, 0, 3);
1433
        else v->mvrange = 0;
1434
        v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1435
        v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
1436
        v->range_x = 1 << (v->k_x - 1);
1437
        v->range_y = 1 << (v->k_y - 1);
1438

    
1439
        if (v->pq < 5) v->tt_index = 0;
1440
        else if(v->pq < 13) v->tt_index = 1;
1441
        else v->tt_index = 2;
1442

    
1443
        lowquant = (v->pq > 12) ? 0 : 1;
1444
        v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
1445
        if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
1446
        {
1447
            int scale, shift, i;
1448
            v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
1449
            v->lumscale = get_bits(gb, 6);
1450
            v->lumshift = get_bits(gb, 6);
1451
            /* fill lookup tables for intensity compensation */
1452
            if(!v->lumscale) {
1453
                scale = -64;
1454
                shift = (255 - v->lumshift * 2) << 6;
1455
                if(v->lumshift > 31)
1456
                    shift += 128 << 6;
1457
            } else {
1458
                scale = v->lumscale + 32;
1459
                if(v->lumshift > 31)
1460
                    shift = (v->lumshift - 64) << 6;
1461
                else
1462
                    shift = v->lumshift << 6;
1463
            }
1464
            for(i = 0; i < 256; i++) {
1465
                v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
1466
                v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
1467
            }
1468
            v->use_ic = 1;
1469
        }
1470
        if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
1471
            v->s.quarter_sample = 0;
1472
        else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1473
            if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
1474
                v->s.quarter_sample = 0;
1475
            else
1476
                v->s.quarter_sample = 1;
1477
        } else
1478
            v->s.quarter_sample = 1;
1479
        v->s.mspel = !(v->mv_mode == MV_PMODE_1MV_HPEL_BILIN || (v->mv_mode == MV_PMODE_INTENSITY_COMP && v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN));
1480

    
1481
        if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
1482
                 v->mv_mode2 == MV_PMODE_MIXED_MV)
1483
                || v->mv_mode == MV_PMODE_MIXED_MV)
1484
        {
1485
            status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
1486
            if (status < 0) return -1;
1487
            av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
1488
                   "Imode: %i, Invert: %i\n", status>>1, status&1);
1489
        } else {
1490
            v->mv_type_is_raw = 0;
1491
            memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
1492
        }
1493
        status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1494
        if (status < 0) return -1;
1495
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1496
               "Imode: %i, Invert: %i\n", status>>1, status&1);
1497

    
1498
        /* Hopefully this is correct for P frames */
1499
        v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
1500
        v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1501
        if (v->dquant)
1502
        {
1503
            av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1504
            vop_dquant_decoding(v);
1505
        }
1506

    
1507
        v->ttfrm = 0; //FIXME Is that so ?
1508
        if (v->vstransform)
1509
        {
1510
            v->ttmbf = get_bits1(gb);
1511
            if (v->ttmbf)
1512
            {
1513
                v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
1514
            }
1515
        } else {
1516
            v->ttmbf = 1;
1517
            v->ttfrm = TT_8X8;
1518
        }
1519
        break;
1520
    case FF_B_TYPE:
1521
        if (v->extended_mv) v->mvrange = get_unary(gb, 0, 3);
1522
        else v->mvrange = 0;
1523
        v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1524
        v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
1525
        v->range_x = 1 << (v->k_x - 1);
1526
        v->range_y = 1 << (v->k_y - 1);
1527

    
1528
        if (v->pq < 5) v->tt_index = 0;
1529
        else if(v->pq < 13) v->tt_index = 1;
1530
        else v->tt_index = 2;
1531

    
1532
        lowquant = (v->pq > 12) ? 0 : 1;
1533
        v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
1534
        v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
1535
        v->s.mspel = v->s.quarter_sample;
1536

    
1537
        status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
1538
        if (status < 0) return -1;
1539
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
1540
               "Imode: %i, Invert: %i\n", status>>1, status&1);
1541
        status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1542
        if (status < 0) return -1;
1543
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1544
               "Imode: %i, Invert: %i\n", status>>1, status&1);
1545

    
1546
        v->s.mv_table_index = get_bits(gb, 2);
1547
        v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1548

    
1549
        if (v->dquant)
1550
        {
1551
            av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1552
            vop_dquant_decoding(v);
1553
        }
1554

    
1555
        v->ttfrm = 0;
1556
        if (v->vstransform)
1557
        {
1558
            v->ttmbf = get_bits1(gb);
1559
            if (v->ttmbf)
1560
            {
1561
                v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
1562
            }
1563
        } else {
1564
            v->ttmbf = 1;
1565
            v->ttfrm = TT_8X8;
1566
        }
1567
        break;
1568
    }
1569

    
1570
    /* AC Syntax */
1571
    v->c_ac_table_index = decode012(gb);
1572
    if (v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)
1573
    {
1574
        v->y_ac_table_index = decode012(gb);
1575
    }
1576
    /* DC Syntax */
1577
    v->s.dc_table_index = get_bits1(gb);
1578
    if ((v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE) && v->dquant) {
1579
        av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1580
        vop_dquant_decoding(v);
1581
    }
1582

    
1583
    v->bi_type = 0;
1584
    if(v->s.pict_type == FF_BI_TYPE) {
1585
        v->s.pict_type = FF_B_TYPE;
1586
        v->bi_type = 1;
1587
    }
1588
    return 0;
1589
}
1590

    
1591
/***********************************************************************/
1592
/**
1593
 * @defgroup block VC-1 Block-level functions
1594
 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1595
 * @{
1596
 */
1597

    
1598
/**
1599
 * @def GET_MQUANT
1600
 * @brief Get macroblock-level quantizer scale
1601
 */
1602
#define GET_MQUANT()                                           \
1603
  if (v->dquantfrm)                                            \
1604
  {                                                            \
1605
    int edges = 0;                                             \
1606
    if (v->dqprofile == DQPROFILE_ALL_MBS)                     \
1607
    {                                                          \
1608
      if (v->dqbilevel)                                        \
1609
      {                                                        \
1610
        mquant = (get_bits1(gb)) ? v->altpq : v->pq;           \
1611
      }                                                        \
1612
      else                                                     \
1613
      {                                                        \
1614
        mqdiff = get_bits(gb, 3);                              \
1615
        if (mqdiff != 7) mquant = v->pq + mqdiff;              \
1616
        else mquant = get_bits(gb, 5);                         \
1617
      }                                                        \
1618
    }                                                          \
1619
    if(v->dqprofile == DQPROFILE_SINGLE_EDGE)                  \
1620
        edges = 1 << v->dqsbedge;                              \
1621
    else if(v->dqprofile == DQPROFILE_DOUBLE_EDGES)            \
1622
        edges = (3 << v->dqsbedge) % 15;                       \
1623
    else if(v->dqprofile == DQPROFILE_FOUR_EDGES)              \
1624
        edges = 15;                                            \
1625
    if((edges&1) && !s->mb_x)                                  \
1626
        mquant = v->altpq;                                     \
1627
    if((edges&2) && s->first_slice_line)                       \
1628
        mquant = v->altpq;                                     \
1629
    if((edges&4) && s->mb_x == (s->mb_width - 1))              \
1630
        mquant = v->altpq;                                     \
1631
    if((edges&8) && s->mb_y == (s->mb_height - 1))             \
1632
        mquant = v->altpq;                                     \
1633
  }
1634

    
1635
/**
1636
 * @def GET_MVDATA(_dmv_x, _dmv_y)
1637
 * @brief Get MV differentials
1638
 * @see MVDATA decoding from 8.3.5.2, p(1)20
1639
 * @param _dmv_x Horizontal differential for decoded MV
1640
 * @param _dmv_y Vertical differential for decoded MV
1641
 */
1642
#define GET_MVDATA(_dmv_x, _dmv_y)                                  \
1643
  index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table,\
1644
                       VC1_MV_DIFF_VLC_BITS, 2);                    \
1645
  if (index > 36)                                                   \
1646
  {                                                                 \
1647
    mb_has_coeffs = 1;                                              \
1648
    index -= 37;                                                    \
1649
  }                                                                 \
1650
  else mb_has_coeffs = 0;                                           \
1651
  s->mb_intra = 0;                                                  \
1652
  if (!index) { _dmv_x = _dmv_y = 0; }                              \
1653
  else if (index == 35)                                             \
1654
  {                                                                 \
1655
    _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample);          \
1656
    _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample);          \
1657
  }                                                                 \
1658
  else if (index == 36)                                             \
1659
  {                                                                 \
1660
    _dmv_x = 0;                                                     \
1661
    _dmv_y = 0;                                                     \
1662
    s->mb_intra = 1;                                                \
1663
  }                                                                 \
1664
  else                                                              \
1665
  {                                                                 \
1666
    index1 = index%6;                                               \
1667
    if (!s->quarter_sample && index1 == 5) val = 1;                 \
1668
    else                                   val = 0;                 \
1669
    if(size_table[index1] - val > 0)                                \
1670
        val = get_bits(gb, size_table[index1] - val);               \
1671
    else                                   val = 0;                 \
1672
    sign = 0 - (val&1);                                             \
1673
    _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign;     \
1674
                                                                    \
1675
    index1 = index/6;                                               \
1676
    if (!s->quarter_sample && index1 == 5) val = 1;                 \
1677
    else                                   val = 0;                 \
1678
    if(size_table[index1] - val > 0)                                \
1679
        val = get_bits(gb, size_table[index1] - val);               \
1680
    else                                   val = 0;                 \
1681
    sign = 0 - (val&1);                                             \
1682
    _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign;     \
1683
  }
1684

    
1685
/** Predict and set motion vector
1686
 */
1687
static inline void vc1_pred_mv(MpegEncContext *s, int n, int dmv_x, int dmv_y, int mv1, int r_x, int r_y, uint8_t* is_intra)
1688
{
1689
    int xy, wrap, off = 0;
1690
    int16_t *A, *B, *C;
1691
    int px, py;
1692
    int sum;
1693

    
1694
    /* scale MV difference to be quad-pel */
1695
    dmv_x <<= 1 - s->quarter_sample;
1696
    dmv_y <<= 1 - s->quarter_sample;
1697

    
1698
    wrap = s->b8_stride;
1699
    xy = s->block_index[n];
1700

    
1701
    if(s->mb_intra){
1702
        s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
1703
        s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
1704
        s->current_picture.motion_val[1][xy][0] = 0;
1705
        s->current_picture.motion_val[1][xy][1] = 0;
1706
        if(mv1) { /* duplicate motion data for 1-MV block */
1707
            s->current_picture.motion_val[0][xy + 1][0] = 0;
1708
            s->current_picture.motion_val[0][xy + 1][1] = 0;
1709
            s->current_picture.motion_val[0][xy + wrap][0] = 0;
1710
            s->current_picture.motion_val[0][xy + wrap][1] = 0;
1711
            s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
1712
            s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
1713
            s->current_picture.motion_val[1][xy + 1][0] = 0;
1714
            s->current_picture.motion_val[1][xy + 1][1] = 0;
1715
            s->current_picture.motion_val[1][xy + wrap][0] = 0;
1716
            s->current_picture.motion_val[1][xy + wrap][1] = 0;
1717
            s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
1718
            s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
1719
        }
1720
        return;
1721
    }
1722

    
1723
    C = s->current_picture.motion_val[0][xy - 1];
1724
    A = s->current_picture.motion_val[0][xy - wrap];
1725
    if(mv1)
1726
        off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1727
    else {
1728
        //in 4-MV mode different blocks have different B predictor position
1729
        switch(n){
1730
        case 0:
1731
            off = (s->mb_x > 0) ? -1 : 1;
1732
            break;
1733
        case 1:
1734
            off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1735
            break;
1736
        case 2:
1737
            off = 1;
1738
            break;
1739
        case 3:
1740
            off = -1;
1741
        }
1742
    }
1743
    B = s->current_picture.motion_val[0][xy - wrap + off];
1744

    
1745
    if(!s->first_slice_line || (n==2 || n==3)) { // predictor A is not out of bounds
1746
        if(s->mb_width == 1) {
1747
            px = A[0];
1748
            py = A[1];
1749
        } else {
1750
            px = mid_pred(A[0], B[0], C[0]);
1751
            py = mid_pred(A[1], B[1], C[1]);
1752
        }
1753
    } else if(s->mb_x || (n==1 || n==3)) { // predictor C is not out of bounds
1754
        px = C[0];
1755
        py = C[1];
1756
    } else {
1757
        px = py = 0;
1758
    }
1759
    /* Pullback MV as specified in 8.3.5.3.4 */
1760
    {
1761
        int qx, qy, X, Y;
1762
        qx = (s->mb_x << 6) + ((n==1 || n==3) ? 32 : 0);
1763
        qy = (s->mb_y << 6) + ((n==2 || n==3) ? 32 : 0);
1764
        X = (s->mb_width << 6) - 4;
1765
        Y = (s->mb_height << 6) - 4;
1766
        if(mv1) {
1767
            if(qx + px < -60) px = -60 - qx;
1768
            if(qy + py < -60) py = -60 - qy;
1769
        } else {
1770
            if(qx + px < -28) px = -28 - qx;
1771
            if(qy + py < -28) py = -28 - qy;
1772
        }
1773
        if(qx + px > X) px = X - qx;
1774
        if(qy + py > Y) py = Y - qy;
1775
    }
1776
    /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
1777
    if((!s->first_slice_line || (n==2 || n==3)) && (s->mb_x || (n==1 || n==3))) {
1778
        if(is_intra[xy - wrap])
1779
            sum = FFABS(px) + FFABS(py);
1780
        else
1781
            sum = FFABS(px - A[0]) + FFABS(py - A[1]);
1782
        if(sum > 32) {
1783
            if(get_bits1(&s->gb)) {
1784
                px = A[0];
1785
                py = A[1];
1786
            } else {
1787
                px = C[0];
1788
                py = C[1];
1789
            }
1790
        } else {
1791
            if(is_intra[xy - 1])
1792
                sum = FFABS(px) + FFABS(py);
1793
            else
1794
                sum = FFABS(px - C[0]) + FFABS(py - C[1]);
1795
            if(sum > 32) {
1796
                if(get_bits1(&s->gb)) {
1797
                    px = A[0];
1798
                    py = A[1];
1799
                } else {
1800
                    px = C[0];
1801
                    py = C[1];
1802
                }
1803
            }
1804
        }
1805
    }
1806
    /* store MV using signed modulus of MV range defined in 4.11 */
1807
    s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1808
    s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
1809
    if(mv1) { /* duplicate motion data for 1-MV block */
1810
        s->current_picture.motion_val[0][xy + 1][0] = s->current_picture.motion_val[0][xy][0];
1811
        s->current_picture.motion_val[0][xy + 1][1] = s->current_picture.motion_val[0][xy][1];
1812
        s->current_picture.motion_val[0][xy + wrap][0] = s->current_picture.motion_val[0][xy][0];
1813
        s->current_picture.motion_val[0][xy + wrap][1] = s->current_picture.motion_val[0][xy][1];
1814
        s->current_picture.motion_val[0][xy + wrap + 1][0] = s->current_picture.motion_val[0][xy][0];
1815
        s->current_picture.motion_val[0][xy + wrap + 1][1] = s->current_picture.motion_val[0][xy][1];
1816
    }
1817
}
1818

    
1819
/** Motion compensation for direct or interpolated blocks in B-frames
1820
 */
1821
static void vc1_interp_mc(VC1Context *v)
1822
{
1823
    MpegEncContext *s = &v->s;
1824
    DSPContext *dsp = &v->s.dsp;
1825
    uint8_t *srcY, *srcU, *srcV;
1826
    int dxy, uvdxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1827

    
1828
    if(!v->s.next_picture.data[0])return;
1829

    
1830
    mx = s->mv[1][0][0];
1831
    my = s->mv[1][0][1];
1832
    uvmx = (mx + ((mx & 3) == 3)) >> 1;
1833
    uvmy = (my + ((my & 3) == 3)) >> 1;
1834
    if(v->fastuvmc) {
1835
        uvmx = uvmx + ((uvmx<0)?-(uvmx&1):(uvmx&1));
1836
        uvmy = uvmy + ((uvmy<0)?-(uvmy&1):(uvmy&1));
1837
    }
1838
    srcY = s->next_picture.data[0];
1839
    srcU = s->next_picture.data[1];
1840
    srcV = s->next_picture.data[2];
1841

    
1842
    src_x = s->mb_x * 16 + (mx >> 2);
1843
    src_y = s->mb_y * 16 + (my >> 2);
1844
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1845
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1846

    
1847
    if(v->profile != PROFILE_ADVANCED){
1848
        src_x   = av_clip(  src_x, -16, s->mb_width  * 16);
1849
        src_y   = av_clip(  src_y, -16, s->mb_height * 16);
1850
        uvsrc_x = av_clip(uvsrc_x,  -8, s->mb_width  *  8);
1851
        uvsrc_y = av_clip(uvsrc_y,  -8, s->mb_height *  8);
1852
    }else{
1853
        src_x   = av_clip(  src_x, -17, s->avctx->coded_width);
1854
        src_y   = av_clip(  src_y, -18, s->avctx->coded_height + 1);
1855
        uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
1856
        uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
1857
    }
1858

    
1859
    srcY += src_y * s->linesize + src_x;
1860
    srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1861
    srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1862

    
1863
    /* for grayscale we should not try to read from unknown area */
1864
    if(s->flags & CODEC_FLAG_GRAY) {
1865
        srcU = s->edge_emu_buffer + 18 * s->linesize;
1866
        srcV = s->edge_emu_buffer + 18 * s->linesize;
1867
    }
1868

    
1869
    if(v->rangeredfrm
1870
       || (unsigned)src_x > s->h_edge_pos - (mx&3) - 16
1871
       || (unsigned)src_y > s->v_edge_pos - (my&3) - 16){
1872
        uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
1873

    
1874
        srcY -= s->mspel * (1 + s->linesize);
1875
        ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
1876
                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
1877
        srcY = s->edge_emu_buffer;
1878
        ff_emulated_edge_mc(uvbuf     , srcU, s->uvlinesize, 8+1, 8+1,
1879
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
1880
        ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
1881
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
1882
        srcU = uvbuf;
1883
        srcV = uvbuf + 16;
1884
        /* if we deal with range reduction we need to scale source blocks */
1885
        if(v->rangeredfrm) {
1886
            int i, j;
1887
            uint8_t *src, *src2;
1888

    
1889
            src = srcY;
1890
            for(j = 0; j < 17 + s->mspel*2; j++) {
1891
                for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
1892
                src += s->linesize;
1893
            }
1894
            src = srcU; src2 = srcV;
1895
            for(j = 0; j < 9; j++) {
1896
                for(i = 0; i < 9; i++) {
1897
                    src[i] = ((src[i] - 128) >> 1) + 128;
1898
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
1899
                }
1900
                src += s->uvlinesize;
1901
                src2 += s->uvlinesize;
1902
            }
1903
        }
1904
        srcY += s->mspel * (1 + s->linesize);
1905
    }
1906

    
1907
    mx >>= 1;
1908
    my >>= 1;
1909
    dxy = ((my & 1) << 1) | (mx & 1);
1910

    
1911
    dsp->avg_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
1912

    
1913
    if(s->flags & CODEC_FLAG_GRAY) return;
1914
    /* Chroma MC always uses qpel blilinear */
1915
    uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
1916
    uvmx = (uvmx&3)<<1;
1917
    uvmy = (uvmy&3)<<1;
1918
    dsp->avg_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
1919
    dsp->avg_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
1920
}
1921

    
1922
static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
1923
{
1924
    int n = bfrac;
1925

    
1926
#if B_FRACTION_DEN==256
1927
    if(inv)
1928
        n -= 256;
1929
    if(!qs)
1930
        return 2 * ((value * n + 255) >> 9);
1931
    return (value * n + 128) >> 8;
1932
#else
1933
    if(inv)
1934
        n -= B_FRACTION_DEN;
1935
    if(!qs)
1936
        return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
1937
    return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
1938
#endif
1939
}
1940

    
1941
/** Reconstruct motion vector for B-frame and do motion compensation
1942
 */
1943
static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mode)
1944
{
1945
    if(v->use_ic) {
1946
        v->mv_mode2 = v->mv_mode;
1947
        v->mv_mode = MV_PMODE_INTENSITY_COMP;
1948
    }
1949
    if(direct) {
1950
        vc1_mc_1mv(v, 0);
1951
        vc1_interp_mc(v);
1952
        if(v->use_ic) v->mv_mode = v->mv_mode2;
1953
        return;
1954
    }
1955
    if(mode == BMV_TYPE_INTERPOLATED) {
1956
        vc1_mc_1mv(v, 0);
1957
        vc1_interp_mc(v);
1958
        if(v->use_ic) v->mv_mode = v->mv_mode2;
1959
        return;
1960
    }
1961

    
1962
    if(v->use_ic && (mode == BMV_TYPE_BACKWARD)) v->mv_mode = v->mv_mode2;
1963
    vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
1964
    if(v->use_ic) v->mv_mode = v->mv_mode2;
1965
}
1966

    
1967
static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mvtype)
1968
{
1969
    MpegEncContext *s = &v->s;
1970
    int xy, wrap, off = 0;
1971
    int16_t *A, *B, *C;
1972
    int px, py;
1973
    int sum;
1974
    int r_x, r_y;
1975
    const uint8_t *is_intra = v->mb_type[0];
1976

    
1977
    r_x = v->range_x;
1978
    r_y = v->range_y;
1979
    /* scale MV difference to be quad-pel */
1980
    dmv_x[0] <<= 1 - s->quarter_sample;
1981
    dmv_y[0] <<= 1 - s->quarter_sample;
1982
    dmv_x[1] <<= 1 - s->quarter_sample;
1983
    dmv_y[1] <<= 1 - s->quarter_sample;
1984

    
1985
    wrap = s->b8_stride;
1986
    xy = s->block_index[0];
1987

    
1988
    if(s->mb_intra) {
1989
        s->current_picture.motion_val[0][xy][0] =
1990
        s->current_picture.motion_val[0][xy][1] =
1991
        s->current_picture.motion_val[1][xy][0] =
1992
        s->current_picture.motion_val[1][xy][1] = 0;
1993
        return;
1994
    }
1995
    s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
1996
    s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
1997
    s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
1998
    s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
1999

    
2000
    /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2001
    s->mv[0][0][0] = av_clip(s->mv[0][0][0], -60 - (s->mb_x << 6), (s->mb_width  << 6) - 4 - (s->mb_x << 6));
2002
    s->mv[0][0][1] = av_clip(s->mv[0][0][1], -60 - (s->mb_y << 6), (s->mb_height << 6) - 4 - (s->mb_y << 6));
2003
    s->mv[1][0][0] = av_clip(s->mv[1][0][0], -60 - (s->mb_x << 6), (s->mb_width  << 6) - 4 - (s->mb_x << 6));
2004
    s->mv[1][0][1] = av_clip(s->mv[1][0][1], -60 - (s->mb_y << 6), (s->mb_height << 6) - 4 - (s->mb_y << 6));
2005
    if(direct) {
2006
        s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
2007
        s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
2008
        s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
2009
        s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
2010
        return;
2011
    }
2012

    
2013
    if((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2014
        C = s->current_picture.motion_val[0][xy - 2];
2015
        A = s->current_picture.motion_val[0][xy - wrap*2];
2016
        off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2017
        B = s->current_picture.motion_val[0][xy - wrap*2 + off];
2018

    
2019
        if(!s->mb_x) C[0] = C[1] = 0;
2020
        if(!s->first_slice_line) { // predictor A is not out of bounds
2021
            if(s->mb_width == 1) {
2022
                px = A[0];
2023
                py = A[1];
2024
            } else {
2025
                px = mid_pred(A[0], B[0], C[0]);
2026
                py = mid_pred(A[1], B[1], C[1]);
2027
            }
2028
        } else if(s->mb_x) { // predictor C is not out of bounds
2029
            px = C[0];
2030
            py = C[1];
2031
        } else {
2032
            px = py = 0;
2033
        }
2034
        /* Pullback MV as specified in 8.3.5.3.4 */
2035
        {
2036
            int qx, qy, X, Y;
2037
            if(v->profile < PROFILE_ADVANCED) {
2038
                qx = (s->mb_x << 5);
2039
                qy = (s->mb_y << 5);
2040
                X = (s->mb_width << 5) - 4;
2041
                Y = (s->mb_height << 5) - 4;
2042
                if(qx + px < -28) px = -28 - qx;
2043
                if(qy + py < -28) py = -28 - qy;
2044
                if(qx + px > X) px = X - qx;
2045
                if(qy + py > Y) py = Y - qy;
2046
            } else {
2047
                qx = (s->mb_x << 6);
2048
                qy = (s->mb_y << 6);
2049
                X = (s->mb_width << 6) - 4;
2050
                Y = (s->mb_height << 6) - 4;
2051
                if(qx + px < -60) px = -60 - qx;
2052
                if(qy + py < -60) py = -60 - qy;
2053
                if(qx + px > X) px = X - qx;
2054
                if(qy + py > Y) py = Y - qy;
2055
            }
2056
        }
2057
        /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2058
        if(0 && !s->first_slice_line && s->mb_x) {
2059
            if(is_intra[xy - wrap])
2060
                sum = FFABS(px) + FFABS(py);
2061
            else
2062
                sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2063
            if(sum > 32) {
2064
                if(get_bits1(&s->gb)) {
2065
                    px = A[0];
2066
                    py = A[1];
2067
                } else {
2068
                    px = C[0];
2069
                    py = C[1];
2070
                }
2071
            } else {
2072
                if(is_intra[xy - 2])
2073
                    sum = FFABS(px) + FFABS(py);
2074
                else
2075
                    sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2076
                if(sum > 32) {
2077
                    if(get_bits1(&s->gb)) {
2078
                        px = A[0];
2079
                        py = A[1];
2080
                    } else {
2081
                        px = C[0];
2082
                        py = C[1];
2083
                    }
2084
                }
2085
            }
2086
        }
2087
        /* store MV using signed modulus of MV range defined in 4.11 */
2088
        s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2089
        s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2090
    }
2091
    if((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2092
        C = s->current_picture.motion_val[1][xy - 2];
2093
        A = s->current_picture.motion_val[1][xy - wrap*2];
2094
        off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2095
        B = s->current_picture.motion_val[1][xy - wrap*2 + off];
2096

    
2097
        if(!s->mb_x) C[0] = C[1] = 0;
2098
        if(!s->first_slice_line) { // predictor A is not out of bounds
2099
            if(s->mb_width == 1) {
2100
                px = A[0];
2101
                py = A[1];
2102
            } else {
2103
                px = mid_pred(A[0], B[0], C[0]);
2104
                py = mid_pred(A[1], B[1], C[1]);
2105
            }
2106
        } else if(s->mb_x) { // predictor C is not out of bounds
2107
            px = C[0];
2108
            py = C[1];
2109
        } else {
2110
            px = py = 0;
2111
        }
2112
        /* Pullback MV as specified in 8.3.5.3.4 */
2113
        {
2114
            int qx, qy, X, Y;
2115
            if(v->profile < PROFILE_ADVANCED) {
2116
                qx = (s->mb_x << 5);
2117
                qy = (s->mb_y << 5);
2118
                X = (s->mb_width << 5) - 4;
2119
                Y = (s->mb_height << 5) - 4;
2120
                if(qx + px < -28) px = -28 - qx;
2121
                if(qy + py < -28) py = -28 - qy;
2122
                if(qx + px > X) px = X - qx;
2123
                if(qy + py > Y) py = Y - qy;
2124
            } else {
2125
                qx = (s->mb_x << 6);
2126
                qy = (s->mb_y << 6);
2127
                X = (s->mb_width << 6) - 4;
2128
                Y = (s->mb_height << 6) - 4;
2129
                if(qx + px < -60) px = -60 - qx;
2130
                if(qy + py < -60) py = -60 - qy;
2131
                if(qx + px > X) px = X - qx;
2132
                if(qy + py > Y) py = Y - qy;
2133
            }
2134
        }
2135
        /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2136
        if(0 && !s->first_slice_line && s->mb_x) {
2137
            if(is_intra[xy - wrap])
2138
                sum = FFABS(px) + FFABS(py);
2139
            else
2140
                sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2141
            if(sum > 32) {
2142
                if(get_bits1(&s->gb)) {
2143
                    px = A[0];
2144
                    py = A[1];
2145
                } else {
2146
                    px = C[0];
2147
                    py = C[1];
2148
                }
2149
            } else {
2150
                if(is_intra[xy - 2])
2151
                    sum = FFABS(px) + FFABS(py);
2152
                else
2153
                    sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2154
                if(sum > 32) {
2155
                    if(get_bits1(&s->gb)) {
2156
                        px = A[0];
2157
                        py = A[1];
2158
                    } else {
2159
                        px = C[0];
2160
                        py = C[1];
2161
                    }
2162
                }
2163
            }
2164
        }
2165
        /* store MV using signed modulus of MV range defined in 4.11 */
2166

    
2167
        s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2168
        s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2169
    }
2170
    s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
2171
    s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
2172
    s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
2173
    s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
2174
}
2175

    
2176
/** Get predicted DC value for I-frames only
2177
 * prediction dir: left=0, top=1
2178
 * @param s MpegEncContext
2179
 * @param[in] n block index in the current MB
2180
 * @param dc_val_ptr Pointer to DC predictor
2181
 * @param dir_ptr Prediction direction for use in AC prediction
2182
 */
2183
static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2184
                              int16_t **dc_val_ptr, int *dir_ptr)
2185
{
2186
    int a, b, c, wrap, pred, scale;
2187
    int16_t *dc_val;
2188
    static const uint16_t dcpred[32] = {
2189
    -1, 1024,  512,  341,  256,  205,  171,  146,  128,
2190
         114,  102,   93,   85,   79,   73,   68,   64,
2191
          60,   57,   54,   51,   49,   47,   45,   43,
2192
          41,   39,   38,   37,   35,   34,   33
2193
    };
2194

    
2195
    /* find prediction - wmv3_dc_scale always used here in fact */
2196
    if (n < 4)     scale = s->y_dc_scale;
2197
    else           scale = s->c_dc_scale;
2198

    
2199
    wrap = s->block_wrap[n];
2200
    dc_val= s->dc_val[0] + s->block_index[n];
2201

    
2202
    /* B A
2203
     * C X
2204
     */
2205
    c = dc_val[ - 1];
2206
    b = dc_val[ - 1 - wrap];
2207
    a = dc_val[ - wrap];
2208

    
2209
    if (pq < 9 || !overlap)
2210
    {
2211
        /* Set outer values */
2212
        if (s->first_slice_line && (n!=2 && n!=3)) b=a=dcpred[scale];
2213
        if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=dcpred[scale];
2214
    }
2215
    else
2216
    {
2217
        /* Set outer values */
2218
        if (s->first_slice_line && (n!=2 && n!=3)) b=a=0;
2219
        if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=0;
2220
    }
2221

    
2222
    if (abs(a - b) <= abs(b - c)) {
2223
        pred = c;
2224
        *dir_ptr = 1;//left
2225
    } else {
2226
        pred = a;
2227
        *dir_ptr = 0;//top
2228
    }
2229

    
2230
    /* update predictor */
2231
    *dc_val_ptr = &dc_val[0];
2232
    return pred;
2233
}
2234

    
2235

    
2236
/** Get predicted DC value
2237
 * prediction dir: left=0, top=1
2238
 * @param s MpegEncContext
2239
 * @param[in] n block index in the current MB
2240
 * @param dc_val_ptr Pointer to DC predictor
2241
 * @param dir_ptr Prediction direction for use in AC prediction
2242
 */
2243
static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2244
                              int a_avail, int c_avail,
2245
                              int16_t **dc_val_ptr, int *dir_ptr)
2246
{
2247
    int a, b, c, wrap, pred, scale;
2248
    int16_t *dc_val;
2249
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2250
    int q1, q2 = 0;
2251

    
2252
    /* find prediction - wmv3_dc_scale always used here in fact */
2253
    if (n < 4)     scale = s->y_dc_scale;
2254
    else           scale = s->c_dc_scale;
2255

    
2256
    wrap = s->block_wrap[n];
2257
    dc_val= s->dc_val[0] + s->block_index[n];
2258

    
2259
    /* B A
2260
     * C X
2261
     */
2262
    c = dc_val[ - 1];
2263
    b = dc_val[ - 1 - wrap];
2264
    a = dc_val[ - wrap];
2265
    /* scale predictors if needed */
2266
    q1 = s->current_picture.qscale_table[mb_pos];
2267
    if(c_avail && (n!= 1 && n!=3)) {
2268
        q2 = s->current_picture.qscale_table[mb_pos - 1];
2269
        if(q2 && q2 != q1)
2270
            c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
2271
    }
2272
    if(a_avail && (n!= 2 && n!=3)) {
2273
        q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2274
        if(q2 && q2 != q1)
2275
            a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
2276
    }
2277
    if(a_avail && c_avail && (n!=3)) {
2278
        int off = mb_pos;
2279
        if(n != 1) off--;
2280
        if(n != 2) off -= s->mb_stride;
2281
        q2 = s->current_picture.qscale_table[off];
2282
        if(q2 && q2 != q1)
2283
            b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
2284
    }
2285

    
2286
    if(a_avail && c_avail) {
2287
        if(abs(a - b) <= abs(b - c)) {
2288
            pred = c;
2289
            *dir_ptr = 1;//left
2290
        } else {
2291
            pred = a;
2292
            *dir_ptr = 0;//top
2293
        }
2294
    } else if(a_avail) {
2295
        pred = a;
2296
        *dir_ptr = 0;//top
2297
    } else if(c_avail) {
2298
        pred = c;
2299
        *dir_ptr = 1;//left
2300
    } else {
2301
        pred = 0;
2302
        *dir_ptr = 1;//left
2303
    }
2304

    
2305
    /* update predictor */
2306
    *dc_val_ptr = &dc_val[0];
2307
    return pred;
2308
}
2309

    
2310

    
2311
/**
2312
 * @defgroup std_mb VC1 Macroblock-level functions in Simple/Main Profiles
2313
 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2314
 * @{
2315
 */
2316

    
2317
static inline int vc1_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
2318
{
2319
    int xy, wrap, pred, a, b, c;
2320

    
2321
    xy = s->block_index[n];
2322
    wrap = s->b8_stride;
2323

    
2324
    /* B C
2325
     * A X
2326
     */
2327
    a = s->coded_block[xy - 1       ];
2328
    b = s->coded_block[xy - 1 - wrap];
2329
    c = s->coded_block[xy     - wrap];
2330

    
2331
    if (b == c) {
2332
        pred = a;
2333
    } else {
2334
        pred = c;
2335
    }
2336

    
2337
    /* store value */
2338
    *coded_block_ptr = &s->coded_block[xy];
2339

    
2340
    return pred;
2341
}
2342

    
2343
/**
2344
 * Decode one AC coefficient
2345
 * @param v The VC1 context
2346
 * @param last Last coefficient
2347
 * @param skip How much zero coefficients to skip
2348
 * @param value Decoded AC coefficient value
2349
 * @see 8.1.3.4
2350
 */
2351
static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip, int *value, int codingset)
2352
{
2353
    GetBitContext *gb = &v->s.gb;
2354
    int index, escape, run = 0, level = 0, lst = 0;
2355

    
2356
    index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2357
    if (index != vc1_ac_sizes[codingset] - 1) {
2358
        run = vc1_index_decode_table[codingset][index][0];
2359
        level = vc1_index_decode_table[codingset][index][1];
2360
        lst = index >= vc1_last_decode_table[codingset];
2361
        if(get_bits1(gb))
2362
            level = -level;
2363
    } else {
2364
        escape = decode210(gb);
2365
        if (escape != 2) {
2366
            index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2367
            run = vc1_index_decode_table[codingset][index][0];
2368
            level = vc1_index_decode_table[codingset][index][1];
2369
            lst = index >= vc1_last_decode_table[codingset];
2370
            if(escape == 0) {
2371
                if(lst)
2372
                    level += vc1_last_delta_level_table[codingset][run];
2373
                else
2374
                    level += vc1_delta_level_table[codingset][run];
2375
            } else {
2376
                if(lst)
2377
                    run += vc1_last_delta_run_table[codingset][level] + 1;
2378
                else
2379
                    run += vc1_delta_run_table[codingset][level] + 1;
2380
            }
2381
            if(get_bits1(gb))
2382
                level = -level;
2383
        } else {
2384
            int sign;
2385
            lst = get_bits1(gb);
2386
            if(v->s.esc3_level_length == 0) {
2387
                if(v->pq < 8 || v->dquantfrm) { // table 59
2388
                    v->s.esc3_level_length = get_bits(gb, 3);
2389
                    if(!v->s.esc3_level_length)
2390
                        v->s.esc3_level_length = get_bits(gb, 2) + 8;
2391
                } else { //table 60
2392
                    v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
2393
                }
2394
                v->s.esc3_run_length = 3 + get_bits(gb, 2);
2395
            }
2396
            run = get_bits(gb, v->s.esc3_run_length);
2397
            sign = get_bits1(gb);
2398
            level = get_bits(gb, v->s.esc3_level_length);
2399
            if(sign)
2400
                level = -level;
2401
        }
2402
    }
2403

    
2404
    *last = lst;
2405
    *skip = run;
2406
    *value = level;
2407
}
2408

    
2409
/** Decode intra block in intra frames - should be faster than decode_intra_block
2410
 * @param v VC1Context
2411
 * @param block block to decode
2412
 * @param coded are AC coeffs present or not
2413
 * @param codingset set of VLC to decode data
2414
 */
2415
static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset)
2416
{
2417
    GetBitContext *gb = &v->s.gb;
2418
    MpegEncContext *s = &v->s;
2419
    int dc_pred_dir = 0; /* Direction of the DC prediction used */
2420
    int run_diff, i;
2421
    int16_t *dc_val;
2422
    int16_t *ac_val, *ac_val2;
2423
    int dcdiff;
2424

    
2425
    /* Get DC differential */
2426
    if (n < 4) {
2427
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2428
    } else {
2429
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2430
    }
2431
    if (dcdiff < 0){
2432
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2433
        return -1;
2434
    }
2435
    if (dcdiff)
2436
    {
2437
        if (dcdiff == 119 /* ESC index value */)
2438
        {
2439
            /* TODO: Optimize */
2440
            if (v->pq == 1) dcdiff = get_bits(gb, 10);
2441
            else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2442
            else dcdiff = get_bits(gb, 8);
2443
        }
2444
        else
2445
        {
2446
            if (v->pq == 1)
2447
                dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
2448
            else if (v->pq == 2)
2449
                dcdiff = (dcdiff<<1) + get_bits1(gb)   - 1;
2450
        }
2451
        if (get_bits1(gb))
2452
            dcdiff = -dcdiff;
2453
    }
2454

    
2455
    /* Prediction */
2456
    dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2457
    *dc_val = dcdiff;
2458

    
2459
    /* Store the quantized DC coeff, used for prediction */
2460
    if (n < 4) {
2461
        block[0] = dcdiff * s->y_dc_scale;
2462
    } else {
2463
        block[0] = dcdiff * s->c_dc_scale;
2464
    }
2465
    /* Skip ? */
2466
    run_diff = 0;
2467
    i = 0;
2468
    if (!coded) {
2469
        goto not_coded;
2470
    }
2471

    
2472
    //AC Decoding
2473
    i = 1;
2474

    
2475
    {
2476
        int last = 0, skip, value;
2477
        const int8_t *zz_table;
2478
        int scale;
2479
        int k;
2480

    
2481
        scale = v->pq * 2 + v->halfpq;
2482

    
2483
        if(v->s.ac_pred) {
2484
            if(!dc_pred_dir)
2485
                zz_table = wmv1_scantable[2];
2486
            else
2487
                zz_table = wmv1_scantable[3];
2488
        } else
2489
            zz_table = wmv1_scantable[1];
2490

    
2491
        ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2492
        ac_val2 = ac_val;
2493
        if(dc_pred_dir) //left
2494
            ac_val -= 16;
2495
        else //top
2496
            ac_val -= 16 * s->block_wrap[n];
2497

    
2498
        while (!last) {
2499
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2500
            i += skip;
2501
            if(i > 63)
2502
                break;
2503
            block[zz_table[i++]] = value;
2504
        }
2505

    
2506
        /* apply AC prediction if needed */
2507
        if(s->ac_pred) {
2508
            if(dc_pred_dir) { //left
2509
                for(k = 1; k < 8; k++)
2510
                    block[k << 3] += ac_val[k];
2511
            } else { //top
2512
                for(k = 1; k < 8; k++)
2513
                    block[k] += ac_val[k + 8];
2514
            }
2515
        }
2516
        /* save AC coeffs for further prediction */
2517
        for(k = 1; k < 8; k++) {
2518
            ac_val2[k] = block[k << 3];
2519
            ac_val2[k + 8] = block[k];
2520
        }
2521

    
2522
        /* scale AC coeffs */
2523
        for(k = 1; k < 64; k++)
2524
            if(block[k]) {
2525
                block[k] *= scale;
2526
                if(!v->pquantizer)
2527
                    block[k] += (block[k] < 0) ? -v->pq : v->pq;
2528
            }
2529

    
2530
        if(s->ac_pred) i = 63;
2531
    }
2532

    
2533
not_coded:
2534
    if(!coded) {
2535
        int k, scale;
2536
        ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2537
        ac_val2 = ac_val;
2538

    
2539
        scale = v->pq * 2 + v->halfpq;
2540
        memset(ac_val2, 0, 16 * 2);
2541
        if(dc_pred_dir) {//left
2542
            ac_val -= 16;
2543
            if(s->ac_pred)
2544
                memcpy(ac_val2, ac_val, 8 * 2);
2545
        } else {//top
2546
            ac_val -= 16 * s->block_wrap[n];
2547
            if(s->ac_pred)
2548
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2549
        }
2550

    
2551
        /* apply AC prediction if needed */
2552
        if(s->ac_pred) {
2553
            if(dc_pred_dir) { //left
2554
                for(k = 1; k < 8; k++) {
2555
                    block[k << 3] = ac_val[k] * scale;
2556
                    if(!v->pquantizer && block[k << 3])
2557
                        block[k << 3] += (block[k << 3] < 0) ? -v->pq : v->pq;
2558
                }
2559
            } else { //top
2560
                for(k = 1; k < 8; k++) {
2561
                    block[k] = ac_val[k + 8] * scale;
2562
                    if(!v->pquantizer && block[k])
2563
                        block[k] += (block[k] < 0) ? -v->pq : v->pq;
2564
                }
2565
            }
2566
            i = 63;
2567
        }
2568
    }
2569
    s->block_last_index[n] = i;
2570

    
2571
    return 0;
2572
}
2573

    
2574
/** Decode intra block in intra frames - should be faster than decode_intra_block
2575
 * @param v VC1Context
2576
 * @param block block to decode
2577
 * @param coded are AC coeffs present or not
2578
 * @param codingset set of VLC to decode data
2579
 */
2580
static int vc1_decode_i_block_adv(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset, int mquant)
2581
{
2582
    GetBitContext *gb = &v->s.gb;
2583
    MpegEncContext *s = &v->s;
2584
    int dc_pred_dir = 0; /* Direction of the DC prediction used */
2585
    int run_diff, i;
2586
    int16_t *dc_val;
2587
    int16_t *ac_val, *ac_val2;
2588
    int dcdiff;
2589
    int a_avail = v->a_avail, c_avail = v->c_avail;
2590
    int use_pred = s->ac_pred;
2591
    int scale;
2592
    int q1, q2 = 0;
2593
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2594

    
2595
    /* Get DC differential */
2596
    if (n < 4) {
2597
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2598
    } else {
2599
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2600
    }
2601
    if (dcdiff < 0){
2602
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2603
        return -1;
2604
    }
2605
    if (dcdiff)
2606
    {
2607
        if (dcdiff == 119 /* ESC index value */)
2608
        {
2609
            /* TODO: Optimize */
2610
            if (mquant == 1) dcdiff = get_bits(gb, 10);
2611
            else if (mquant == 2) dcdiff = get_bits(gb, 9);
2612
            else dcdiff = get_bits(gb, 8);
2613
        }
2614
        else
2615
        {
2616
            if (mquant == 1)
2617
                dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
2618
            else if (mquant == 2)
2619
                dcdiff = (dcdiff<<1) + get_bits1(gb)   - 1;
2620
        }
2621
        if (get_bits1(gb))
2622
            dcdiff = -dcdiff;
2623
    }
2624

    
2625
    /* Prediction */
2626
    dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
2627
    *dc_val = dcdiff;
2628

    
2629
    /* Store the quantized DC coeff, used for prediction */
2630
    if (n < 4) {
2631
        block[0] = dcdiff * s->y_dc_scale;
2632
    } else {
2633
        block[0] = dcdiff * s->c_dc_scale;
2634
    }
2635
    /* Skip ? */
2636
    run_diff = 0;
2637
    i = 0;
2638

    
2639
    //AC Decoding
2640
    i = 1;
2641

    
2642
    /* check if AC is needed at all */
2643
    if(!a_avail && !c_avail) use_pred = 0;
2644
    ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2645
    ac_val2 = ac_val;
2646

    
2647
    scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
2648

    
2649
    if(dc_pred_dir) //left
2650
        ac_val -= 16;
2651
    else //top
2652
        ac_val -= 16 * s->block_wrap[n];
2653

    
2654
    q1 = s->current_picture.qscale_table[mb_pos];
2655
    if(dc_pred_dir && c_avail && mb_pos) q2 = s->current_picture.qscale_table[mb_pos - 1];
2656
    if(!dc_pred_dir && a_avail && mb_pos >= s->mb_stride) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2657
    if(dc_pred_dir && n==1) q2 = q1;
2658
    if(!dc_pred_dir && n==2) q2 = q1;
2659
    if(n==3) q2 = q1;
2660

    
2661
    if(coded) {
2662
        int last = 0, skip, value;
2663
        const int8_t *zz_table;
2664
        int k;
2665

    
2666
        if(v->s.ac_pred) {
2667
            if(!dc_pred_dir)
2668
                zz_table = wmv1_scantable[2];
2669
            else
2670
                zz_table = wmv1_scantable[3];
2671
        } else
2672
            zz_table = wmv1_scantable[1];
2673

    
2674
        while (!last) {
2675
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2676
            i += skip;
2677
            if(i > 63)
2678
                break;
2679
            block[zz_table[i++]] = value;
2680
        }
2681

    
2682
        /* apply AC prediction if needed */
2683
        if(use_pred) {
2684
            /* scale predictors if needed*/
2685
            if(q2 && q1!=q2) {
2686
                q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2687
                q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2688

    
2689
                if(dc_pred_dir) { //left
2690
                    for(k = 1; k < 8; k++)
2691
                        block[k << 3] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2692
                } else { //top
2693
                    for(k = 1; k < 8; k++)
2694
                        block[k] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2695
                }
2696
            } else {
2697
                if(dc_pred_dir) { //left
2698
                    for(k = 1; k < 8; k++)
2699
                        block[k << 3] += ac_val[k];
2700
                } else { //top
2701
                    for(k = 1; k < 8; k++)
2702
                        block[k] += ac_val[k + 8];
2703
                }
2704
            }
2705
        }
2706
        /* save AC coeffs for further prediction */
2707
        for(k = 1; k < 8; k++) {
2708
            ac_val2[k] = block[k << 3];
2709
            ac_val2[k + 8] = block[k];
2710
        }
2711

    
2712
        /* scale AC coeffs */
2713
        for(k = 1; k < 64; k++)
2714
            if(block[k]) {
2715
                block[k] *= scale;
2716
                if(!v->pquantizer)
2717
                    block[k] += (block[k] < 0) ? -mquant : mquant;
2718
            }
2719

    
2720
        if(use_pred) i = 63;
2721
    } else { // no AC coeffs
2722
        int k;
2723

    
2724
        memset(ac_val2, 0, 16 * 2);
2725
        if(dc_pred_dir) {//left
2726
            if(use_pred) {
2727
                memcpy(ac_val2, ac_val, 8 * 2);
2728
                if(q2 && q1!=q2) {
2729
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2730
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2731
                    for(k = 1; k < 8; k++)
2732
                        ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2733
                }
2734
            }
2735
        } else {//top
2736
            if(use_pred) {
2737
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2738
                if(q2 && q1!=q2) {
2739
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2740
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2741
                    for(k = 1; k < 8; k++)
2742
                        ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2743
                }
2744
            }
2745
        }
2746

    
2747
        /* apply AC prediction if needed */
2748
        if(use_pred) {
2749
            if(dc_pred_dir) { //left
2750
                for(k = 1; k < 8; k++) {
2751
                    block[k << 3] = ac_val2[k] * scale;
2752
                    if(!v->pquantizer && block[k << 3])
2753
                        block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
2754
                }
2755
            } else { //top
2756
                for(k = 1; k < 8; k++) {
2757
                    block[k] = ac_val2[k + 8] * scale;
2758
                    if(!v->pquantizer && block[k])
2759
                        block[k] += (block[k] < 0) ? -mquant : mquant;
2760
                }
2761
            }
2762
            i = 63;
2763
        }
2764
    }
2765
    s->block_last_index[n] = i;
2766

    
2767
    return 0;
2768
}
2769

    
2770
/** Decode intra block in inter frames - more generic version than vc1_decode_i_block
2771
 * @param v VC1Context
2772
 * @param block block to decode
2773
 * @param coded are AC coeffs present or not
2774
 * @param mquant block quantizer
2775
 * @param codingset set of VLC to decode data
2776
 */
2777
static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n, int coded, int mquant, int codingset)
2778
{
2779
    GetBitContext *gb = &v->s.gb;
2780
    MpegEncContext *s = &v->s;
2781
    int dc_pred_dir = 0; /* Direction of the DC prediction used */
2782
    int run_diff, i;
2783
    int16_t *dc_val;
2784
    int16_t *ac_val, *ac_val2;
2785
    int dcdiff;
2786
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2787
    int a_avail = v->a_avail, c_avail = v->c_avail;
2788
    int use_pred = s->ac_pred;
2789
    int scale;
2790
    int q1, q2 = 0;
2791

    
2792
    /* XXX: Guard against dumb values of mquant */
2793
    mquant = (mquant < 1) ? 0 : ( (mquant>31) ? 31 : mquant );
2794

    
2795
    /* Set DC scale - y and c use the same */
2796
    s->y_dc_scale = s->y_dc_scale_table[mquant];
2797
    s->c_dc_scale = s->c_dc_scale_table[mquant];
2798

    
2799
    /* Get DC differential */
2800
    if (n < 4) {
2801
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2802
    } else {
2803
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2804
    }
2805
    if (dcdiff < 0){
2806
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2807
        return -1;
2808
    }
2809
    if (dcdiff)
2810
    {
2811
        if (dcdiff == 119 /* ESC index value */)
2812
        {
2813
            /* TODO: Optimize */
2814
            if (mquant == 1) dcdiff = get_bits(gb, 10);
2815
            else if (mquant == 2) dcdiff = get_bits(gb, 9);
2816
            else dcdiff = get_bits(gb, 8);
2817
        }
2818
        else
2819
        {
2820
            if (mquant == 1)
2821
                dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
2822
            else if (mquant == 2)
2823
                dcdiff = (dcdiff<<1) + get_bits1(gb)   - 1;
2824
        }
2825
        if (get_bits1(gb))
2826
            dcdiff = -dcdiff;
2827
    }
2828

    
2829
    /* Prediction */
2830
    dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
2831
    *dc_val = dcdiff;
2832

    
2833
    /* Store the quantized DC coeff, used for prediction */
2834

    
2835
    if (n < 4) {
2836
        block[0] = dcdiff * s->y_dc_scale;
2837
    } else {
2838
        block[0] = dcdiff * s->c_dc_scale;
2839
    }
2840
    /* Skip ? */
2841
    run_diff = 0;
2842
    i = 0;
2843

    
2844
    //AC Decoding
2845
    i = 1;
2846

    
2847
    /* check if AC is needed at all and adjust direction if needed */
2848
    if(!a_avail) dc_pred_dir = 1;
2849
    if(!c_avail) dc_pred_dir = 0;
2850
    if(!a_avail && !c_avail) use_pred = 0;
2851
    ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2852
    ac_val2 = ac_val;
2853

    
2854
    scale = mquant * 2 + v->halfpq;
2855

    
2856
    if(dc_pred_dir) //left
2857
        ac_val -= 16;
2858
    else //top
2859
        ac_val -= 16 * s->block_wrap[n];
2860

    
2861
    q1 = s->current_picture.qscale_table[mb_pos];
2862
    if(dc_pred_dir && c_avail && mb_pos) q2 = s->current_picture.qscale_table[mb_pos - 1];
2863
    if(!dc_pred_dir && a_avail && mb_pos >= s->mb_stride) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2864
    if(dc_pred_dir && n==1) q2 = q1;
2865
    if(!dc_pred_dir && n==2) q2 = q1;
2866
    if(n==3) q2 = q1;
2867

    
2868
    if(coded) {
2869
        int last = 0, skip, value;
2870
        const int8_t *zz_table;
2871
        int k;
2872

    
2873
        zz_table = wmv1_scantable[0];
2874

    
2875
        while (!last) {
2876
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2877
            i += skip;
2878
            if(i > 63)
2879
                break;
2880
            block[zz_table[i++]] = value;
2881
        }
2882

    
2883
        /* apply AC prediction if needed */
2884
        if(use_pred) {
2885
            /* scale predictors if needed*/
2886
            if(q2 && q1!=q2) {
2887
                q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2888
                q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2889

    
2890
                if(dc_pred_dir) { //left
2891
                    for(k = 1; k < 8; k++)
2892
                        block[k << 3] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2893
                } else { //top
2894
                    for(k = 1; k < 8; k++)
2895
                        block[k] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2896
                }
2897
            } else {
2898
                if(dc_pred_dir) { //left
2899
                    for(k = 1; k < 8; k++)
2900
                        block[k << 3] += ac_val[k];
2901
                } else { //top
2902
                    for(k = 1; k < 8; k++)
2903
                        block[k] += ac_val[k + 8];
2904
                }
2905
            }
2906
        }
2907
        /* save AC coeffs for further prediction */
2908
        for(k = 1; k < 8; k++) {
2909
            ac_val2[k] = block[k << 3];
2910
            ac_val2[k + 8] = block[k];
2911
        }
2912

    
2913
        /* scale AC coeffs */
2914
        for(k = 1; k < 64; k++)
2915
            if(block[k]) {
2916
                block[k] *= scale;
2917
                if(!v->pquantizer)
2918
                    block[k] += (block[k] < 0) ? -mquant : mquant;
2919
            }
2920

    
2921
        if(use_pred) i = 63;
2922
    } else { // no AC coeffs
2923
        int k;
2924

    
2925
        memset(ac_val2, 0, 16 * 2);
2926
        if(dc_pred_dir) {//left
2927
            if(use_pred) {
2928
                memcpy(ac_val2, ac_val, 8 * 2);
2929
                if(q2 && q1!=q2) {
2930
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2931
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2932
                    for(k = 1; k < 8; k++)
2933
                        ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2934
                }
2935
            }
2936
        } else {//top
2937
            if(use_pred) {
2938
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2939
                if(q2 && q1!=q2) {
2940
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2941
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2942
                    for(k = 1; k < 8; k++)
2943
                        ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2944
                }
2945
            }
2946
        }
2947

    
2948
        /* apply AC prediction if needed */
2949
        if(use_pred) {
2950
            if(dc_pred_dir) { //left
2951
                for(k = 1; k < 8; k++) {
2952
                    block[k << 3] = ac_val2[k] * scale;
2953
                    if(!v->pquantizer && block[k << 3])
2954
                        block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
2955
                }
2956
            } else { //top
2957
                for(k = 1; k < 8; k++) {
2958
                    block[k] = ac_val2[k + 8] * scale;
2959
                    if(!v->pquantizer && block[k])
2960
                        block[k] += (block[k] < 0) ? -mquant : mquant;
2961
                }
2962
            }
2963
            i = 63;
2964
        }
2965
    }
2966
    s->block_last_index[n] = i;
2967

    
2968
    return 0;
2969
}
2970

    
2971
/** Decode P block
2972
 */
2973
static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n, int mquant, int ttmb, int first_block,
2974
                              uint8_t *dst, int linesize, int skip_block, int apply_filter, int cbp_top, int cbp_left)
2975
{
2976
    MpegEncContext *s = &v->s;
2977
    GetBitContext *gb = &s->gb;
2978
    int i, j;
2979
    int subblkpat = 0;
2980
    int scale, off, idx, last, skip, value;
2981
    int ttblk = ttmb & 7;
2982
    int pat = 0;
2983

    
2984
    if(ttmb == -1) {
2985
        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)];
2986
    }
2987
    if(ttblk == TT_4X4) {
2988
        subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
2989
    }
2990
    if((ttblk != TT_8X8 && ttblk != TT_4X4) && (v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))) {
2991
        subblkpat = decode012(gb);
2992
        if(subblkpat) subblkpat ^= 3; //swap decoded pattern bits
2993
        if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) ttblk = TT_8X4;
2994
        if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) ttblk = TT_4X8;
2995
    }
2996
    scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
2997

    
2998
    // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
2999
    if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
3000
        subblkpat = 2 - (ttblk == TT_8X4_TOP);
3001
        ttblk = TT_8X4;
3002
    }
3003
    if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
3004
        subblkpat = 2 - (ttblk == TT_4X8_LEFT);
3005
        ttblk = TT_4X8;
3006
    }
3007
    switch(ttblk) {
3008
    case TT_8X8:
3009
        pat = 0xF;
3010
        i = 0;
3011
        last = 0;
3012
        while (!last) {
3013
            vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3014
            i += skip;
3015
            if(i > 63)
3016
                break;
3017
            idx = wmv1_scantable[0][i++];
3018
            block[idx] = value * scale;
3019
            if(!v->pquantizer)
3020
                block[idx] += (block[idx] < 0) ? -mquant : mquant;
3021
        }
3022
        if(!skip_block){
3023
            s->dsp.vc1_inv_trans_8x8(block);
3024
            s->dsp.add_pixels_clamped(block, dst, linesize);
3025
            if(apply_filter && cbp_top  & 0xC)
3026
                vc1_loop_filter(dst, 1, linesize, 8, mquant);
3027
            if(apply_filter && cbp_left & 0xA)
3028
                vc1_loop_filter(dst, linesize, 1, 8, mquant);
3029
        }
3030
        break;
3031
    case TT_4X4:
3032
        pat = ~subblkpat & 0xF;
3033
        for(j = 0; j < 4; j++) {
3034
            last = subblkpat & (1 << (3 - j));
3035
            i = 0;
3036
            off = (j & 1) * 4 + (j & 2) * 16;
3037
            while (!last) {
3038
                vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3039
                i += skip;
3040
                if(i > 15)
3041
                    break;
3042
                idx = ff_vc1_simple_progressive_4x4_zz[i++];
3043
                block[idx + off] = value * scale;
3044
                if(!v->pquantizer)
3045
                    block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
3046
            }
3047
            if(!(subblkpat & (1 << (3 - j))) && !skip_block){
3048
                s->dsp.vc1_inv_trans_4x4(dst + (j&1)*4 + (j&2)*2*linesize, linesize, block + off);
3049
                if(apply_filter && (j&2 ? pat & (1<<(j-2)) : (cbp_top & (1 << (j + 2)))))
3050
                    vc1_loop_filter(dst + (j&1)*4 + (j&2)*2*linesize, 1, linesize, 4, mquant);
3051
                if(apply_filter && (j&1 ? pat & (1<<(j-1)) : (cbp_left & (1 << (j + 1)))))
3052
                    vc1_loop_filter(dst + (j&1)*4 + (j&2)*2*linesize, linesize, 1, 4, mquant);
3053
            }
3054
        }
3055
        break;
3056
    case TT_8X4:
3057
        pat = ~((subblkpat & 2)*6 + (subblkpat & 1)*3) & 0xF;
3058
        for(j = 0; j < 2; j++) {
3059
            last = subblkpat & (1 << (1 - j));
3060
            i = 0;
3061
            off = j * 32;
3062
            while (!last) {
3063
                vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3064
                i += skip;
3065
                if(i > 31)
3066
                    break;
3067
                idx = v->zz_8x4[i++]+off;
3068
                block[idx] = value * scale;
3069
                if(!v->pquantizer)
3070
                    block[idx] += (block[idx] < 0) ? -mquant : mquant;
3071
            }
3072
            if(!(subblkpat & (1 << (1 - j))) && !skip_block){
3073
                s->dsp.vc1_inv_trans_8x4(dst + j*4*linesize, linesize, block + off);
3074
                if(apply_filter && j ? pat & 0x3 : (cbp_top & 0xC))
3075
                    vc1_loop_filter(dst + j*4*linesize, 1, linesize, 8, mquant);
3076
                if(apply_filter && cbp_left & (2 << j))
3077
                    vc1_loop_filter(dst + j*4*linesize, linesize, 1, 4, mquant);
3078
            }
3079
        }
3080
        break;
3081
    case TT_4X8:
3082
        pat = ~(subblkpat*5) & 0xF;
3083
        for(j = 0; j < 2; j++) {
3084
            last = subblkpat & (1 << (1 - j));
3085
            i = 0;
3086
            off = j * 4;
3087
            while (!last) {
3088
                vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3089
                i += skip;
3090
                if(i > 31)
3091
                    break;
3092
                idx = v->zz_4x8[i++]+off;
3093
                block[idx] = value * scale;
3094
                if(!v->pquantizer)
3095
                    block[idx] += (block[idx] < 0) ? -mquant : mquant;
3096
            }
3097
            if(!(subblkpat & (1 << (1 - j))) && !skip_block){
3098
                s->dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
3099
                if(apply_filter && cbp_top & (2 << j))
3100
                    vc1_loop_filter(dst + j*4, 1, linesize, 4, mquant);
3101
                if(apply_filter && j ? pat & 0x5 : (cbp_left & 0xA))
3102
                    vc1_loop_filter(dst + j*4, linesize, 1, 8, mquant);
3103
            }
3104
        }
3105
        break;
3106
    }
3107
    return pat;
3108
}
3109

    
3110

    
3111
/** Decode one P-frame MB (in Simple/Main profile)
3112
 */
3113
static int vc1_decode_p_mb(VC1Context *v)
3114
{
3115
    MpegEncContext *s = &v->s;
3116
    GetBitContext *gb = &s->gb;
3117
    int i, j;
3118
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3119
    int cbp; /* cbp decoding stuff */
3120
    int mqdiff, mquant; /* MB quantization */
3121
    int ttmb = v->ttfrm; /* MB Transform type */
3122

    
3123
    static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
3124
      offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3125
    int mb_has_coeffs = 1; /* last_flag */
3126
    int dmv_x, dmv_y; /* Differential MV components */
3127
    int index, index1; /* LUT indexes */
3128
    int val, sign; /* temp values */
3129
    int first_block = 1;
3130
    int dst_idx, off;
3131
    int skipped, fourmv;
3132
    int block_cbp = 0, pat;
3133
    int apply_loop_filter;
3134

    
3135
    mquant = v->pq; /* Loosy initialization */
3136

    
3137
    if (v->mv_type_is_raw)
3138
        fourmv = get_bits1(gb);
3139
    else
3140
        fourmv = v->mv_type_mb_plane[mb_pos];
3141
    if (v->skip_is_raw)
3142
        skipped = get_bits1(gb);
3143
    else
3144
        skipped = v->s.mbskip_table[mb_pos];
3145

    
3146
    s->dsp.clear_blocks(s->block[0]);
3147

    
3148
    apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY);
3149
    if (!fourmv) /* 1MV mode */
3150
    {
3151
        if (!skipped)
3152
        {
3153
            GET_MVDATA(dmv_x, dmv_y);
3154

    
3155
            if (s->mb_intra) {
3156
                s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
3157
                s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
3158
            }
3159
            s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
3160
            vc1_pred_mv(s, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
3161

    
3162
            /* FIXME Set DC val for inter block ? */
3163
            if (s->mb_intra && !mb_has_coeffs)
3164
            {
3165
                GET_MQUANT();
3166
                s->ac_pred = get_bits1(gb);
3167
                cbp = 0;
3168
            }
3169
            else if (mb_has_coeffs)
3170
            {
3171
                if (s->mb_intra) s->ac_pred = get_bits1(gb);
3172
                cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3173
                GET_MQUANT();
3174
            }
3175
            else
3176
            {
3177
                mquant = v->pq;
3178
                cbp = 0;
3179
            }
3180
            s->current_picture.qscale_table[mb_pos] = mquant;
3181

    
3182
            if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3183
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
3184
                                VC1_TTMB_VLC_BITS, 2);
3185
            if(!s->mb_intra) vc1_mc_1mv(v, 0);
3186
            dst_idx = 0;
3187
            for (i=0; i<6; i++)
3188
            {
3189
                s->dc_val[0][s->block_index[i]] = 0;
3190
                dst_idx += i >> 2;
3191
                val = ((cbp >> (5 - i)) & 1);
3192
                off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3193
                v->mb_type[0][s->block_index[i]] = s->mb_intra;
3194
                if(s->mb_intra) {
3195
                    /* check if prediction blocks A and C are available */
3196
                    v->a_avail = v->c_avail = 0;
3197
                    if(i == 2 || i == 3 || !s->first_slice_line)
3198
                        v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3199
                    if(i == 1 || i == 3 || s->mb_x)
3200
                        v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3201

    
3202
                    vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
3203
                    if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3204
                    s->dsp.vc1_inv_trans_8x8(s->block[i]);
3205
                    if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
3206
                    s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
3207
                    if(v->pq >= 9 && v->overlap) {
3208
                        if(v->c_avail)
3209
                            s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
3210
                        if(v->a_avail)
3211
                            s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
3212
                    }
3213
                    if(apply_loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){
3214
                        int left_cbp, top_cbp;
3215
                        if(i & 4){
3216
                            left_cbp = v->cbp[s->mb_x - 1]            >> (i * 4);
3217
                            top_cbp  = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
3218
                        }else{
3219
                            left_cbp = (i & 1) ? (cbp >> ((i-1)*4)) : (v->cbp[s->mb_x - 1]           >> ((i+1)*4));
3220
                            top_cbp  = (i & 2) ? (cbp >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));
3221
                        }
3222
                        if(left_cbp & 0xC)
3223
                            vc1_loop_filter(s->dest[dst_idx] + off, 1, i & 4 ? s->uvlinesize : s->linesize, 8, mquant);
3224
                        if(top_cbp  & 0xA)
3225
                            vc1_loop_filter(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, 1, 8, mquant);
3226
                    }
3227
                    block_cbp |= 0xF << (i << 2);
3228
                } else if(val) {
3229
                    int left_cbp = 0, top_cbp = 0, filter = 0;
3230
                    if(apply_loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){
3231
                        filter = 1;
3232
                        if(i & 4){
3233
                            left_cbp = v->cbp[s->mb_x - 1]            >> (i * 4);
3234
                            top_cbp  = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
3235
                        }else{
3236
                            left_cbp = (i & 1) ? (cbp >> ((i-1)*4)) : (v->cbp[s->mb_x - 1]           >> ((i+1)*4));
3237
                            top_cbp  = (i & 2) ? (cbp >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));
3238
                        }
3239
                        if(left_cbp & 0xC)
3240
                            vc1_loop_filter(s->dest[dst_idx] + off, 1, i & 4 ? s->uvlinesize : s->linesize, 8, mquant);
3241
                        if(top_cbp  & 0xA)
3242
                            vc1_loop_filter(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, 1, 8, mquant);
3243
                    }
3244
                    pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize, (i&4) && (s->flags & CODEC_FLAG_GRAY), filter, left_cbp, top_cbp);
3245
                    block_cbp |= pat << (i << 2);
3246
                    if(!v->ttmbf && ttmb < 8) ttmb = -1;
3247
                    first_block = 0;
3248
                }
3249
            }
3250
        }
3251
        else //Skipped
3252
        {
3253
            s->mb_intra = 0;
3254
            for(i = 0; i < 6; i++) {
3255
                v->mb_type[0][s->block_index[i]] = 0;
3256
                s->dc_val[0][s->block_index[i]] = 0;
3257
            }
3258
            s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
3259
            s->current_picture.qscale_table[mb_pos] = 0;
3260
            vc1_pred_mv(s, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);
3261
            vc1_mc_1mv(v, 0);
3262
            return 0;
3263
        }
3264
    } //1MV mode
3265
    else //4MV mode
3266
    {
3267
        if (!skipped /* unskipped MB */)
3268
        {
3269
            int intra_count = 0, coded_inter = 0;
3270
            int is_intra[6], is_coded[6];
3271
            /* Get CBPCY */
3272
            cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3273
            for (i=0; i<6; i++)
3274
            {
3275
                val = ((cbp >> (5 - i)) & 1);
3276
                s->dc_val[0][s->block_index[i]] = 0;
3277
                s->mb_intra = 0;
3278
                if(i < 4) {
3279
                    dmv_x = dmv_y = 0;
3280
                    s->mb_intra = 0;
3281
                    mb_has_coeffs = 0;
3282
                    if(val) {
3283
                        GET_MVDATA(dmv_x, dmv_y);
3284
                    }
3285
                    vc1_pred_mv(s, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);
3286
                    if(!s->mb_intra) vc1_mc_4mv_luma(v, i);
3287
                    intra_count += s->mb_intra;
3288
                    is_intra[i] = s->mb_intra;
3289
                    is_coded[i] = mb_has_coeffs;
3290
                }
3291
                if(i&4){
3292
                    is_intra[i] = (intra_count >= 3);
3293
                    is_coded[i] = val;
3294
                }
3295
                if(i == 4) vc1_mc_4mv_chroma(v);
3296
                v->mb_type[0][s->block_index[i]] = is_intra[i];
3297
                if(!coded_inter) coded_inter = !is_intra[i] & is_coded[i];
3298
            }
3299
            // if there are no coded blocks then don't do anything more
3300
            if(!intra_count && !coded_inter) return 0;
3301
            dst_idx = 0;
3302
            GET_MQUANT();
3303
            s->current_picture.qscale_table[mb_pos] = mquant;
3304
            /* test if block is intra and has pred */
3305
            {
3306
                int intrapred = 0;
3307
                for(i=0; i<6; i++)
3308
                    if(is_intra[i]) {
3309
                        if(((!s->first_slice_line || (i==2 || i==3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
3310
                            || ((s->mb_x || (i==1 || i==3)) && v->mb_type[0][s->block_index[i] - 1])) {
3311
                            intrapred = 1;
3312
                            break;
3313
                        }
3314
                    }
3315
                if(intrapred)s->ac_pred = get_bits1(gb);
3316
                else s->ac_pred = 0;
3317
            }
3318
            if (!v->ttmbf && coded_inter)
3319
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3320
            for (i=0; i<6; i++)
3321
            {
3322
                dst_idx += i >> 2;
3323
                off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3324
                s->mb_intra = is_intra[i];
3325
                if (is_intra[i]) {
3326
                    /* check if prediction blocks A and C are available */
3327
                    v->a_avail = v->c_avail = 0;
3328
                    if(i == 2 || i == 3 || !s->first_slice_line)
3329
                        v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3330
                    if(i == 1 || i == 3 || s->mb_x)
3331
                        v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3332

    
3333
                    vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant, (i&4)?v->codingset2:v->codingset);
3334
                    if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3335
                    s->dsp.vc1_inv_trans_8x8(s->block[i]);
3336
                    if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
3337
                    s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
3338
                    if(v->pq >= 9 && v->overlap) {
3339
                        if(v->c_avail)
3340
                            s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
3341
                        if(v->a_avail)
3342
                            s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
3343
                    }
3344
                    if(v->s.loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){
3345
                        int left_cbp, top_cbp;
3346
                        if(i & 4){
3347
                            left_cbp = v->cbp[s->mb_x - 1]            >> (i * 4);
3348
                            top_cbp  = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
3349
                        }else{
3350
                            left_cbp = (i & 1) ? (cbp >> ((i-1)*4)) : (v->cbp[s->mb_x - 1]           >> ((i+1)*4));
3351
                            top_cbp  = (i & 2) ? (cbp >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));
3352
                        }
3353
                        if(left_cbp & 0xC)
3354
                            vc1_loop_filter(s->dest[dst_idx] + off, 1, i & 4 ? s->uvlinesize : s->linesize, 8, mquant);
3355
                        if(top_cbp  & 0xA)
3356
                            vc1_loop_filter(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, 1, 8, mquant);
3357
                    }
3358
                    block_cbp |= 0xF << (i << 2);
3359
                } else if(is_coded[i]) {
3360
                    int left_cbp = 0, top_cbp = 0, filter = 0;
3361
                    if(v->s.loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){
3362
                        filter = 1;
3363
                        if(i & 4){
3364
                            left_cbp = v->cbp[s->mb_x - 1]            >> (i * 4);
3365
                            top_cbp  = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
3366
                        }else{
3367
                            left_cbp = (i & 1) ? (cbp >> ((i-1)*4)) : (v->cbp[s->mb_x - 1]           >> ((i+1)*4));
3368
                            top_cbp  = (i & 2) ? (cbp >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));
3369
                        }
3370
                        if(left_cbp & 0xC)
3371
                            vc1_loop_filter(s->dest[dst_idx] + off, 1, i & 4 ? s->uvlinesize : s->linesize, 8, mquant);
3372
                        if(top_cbp  & 0xA)
3373
                            vc1_loop_filter(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, 1, 8, mquant);
3374
                    }
3375
                    pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize, (i&4) && (s->flags & CODEC_FLAG_GRAY), filter, left_cbp, top_cbp);
3376
                    block_cbp |= pat << (i << 2);
3377
                    if(!v->ttmbf && ttmb < 8) ttmb = -1;
3378
                    first_block = 0;
3379
                }
3380
            }
3381
            return 0;
3382
        }
3383
        else //Skipped MB
3384
        {
3385
            s->mb_intra = 0;
3386
            s->current_picture.qscale_table[mb_pos] = 0;
3387
            for (i=0; i<6; i++) {
3388
                v->mb_type[0][s->block_index[i]] = 0;
3389
                s->dc_val[0][s->block_index[i]] = 0;
3390
            }
3391
            for (i=0; i<4; i++)
3392
            {
3393
                vc1_pred_mv(s, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0]);
3394
                vc1_mc_4mv_luma(v, i);
3395
            }
3396
            vc1_mc_4mv_chroma(v);
3397
            s->current_picture.qscale_table[mb_pos] = 0;
3398
            return 0;
3399
        }
3400
    }
3401
    v->cbp[s->mb_x] = block_cbp;
3402

    
3403
    /* Should never happen */
3404
    return -1;
3405
}
3406

    
3407
/** Decode one B-frame MB (in Main profile)
3408
 */
3409
static void vc1_decode_b_mb(VC1Context *v)
3410
{
3411
    MpegEncContext *s = &v->s;
3412
    GetBitContext *gb = &s->gb;
3413
    int i, j;
3414
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3415
    int cbp = 0; /* cbp decoding stuff */
3416
    int mqdiff, mquant; /* MB quantization */
3417
    int ttmb = v->ttfrm; /* MB Transform type */
3418

    
3419
    static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
3420
      offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3421
    int mb_has_coeffs = 0; /* last_flag */
3422
    int index, index1; /* LUT indexes */
3423
    int val, sign; /* temp values */
3424
    int first_block = 1;
3425
    int dst_idx, off;
3426
    int skipped, direct;
3427
    int dmv_x[2], dmv_y[2];
3428
    int bmvtype = BMV_TYPE_BACKWARD;
3429

    
3430
    mquant = v->pq; /* Loosy initialization */
3431
    s->mb_intra = 0;
3432

    
3433
    if (v->dmb_is_raw)
3434
        direct = get_bits1(gb);
3435
    else
3436
        direct = v->direct_mb_plane[mb_pos];
3437
    if (v->skip_is_raw)
3438
        skipped = get_bits1(gb);
3439
    else
3440
        skipped = v->s.mbskip_table[mb_pos];
3441

    
3442
    s->dsp.clear_blocks(s->block[0]);
3443
    dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
3444
    for(i = 0; i < 6; i++) {
3445
        v->mb_type[0][s->block_index[i]] = 0;
3446
        s->dc_val[0][s->block_index[i]] = 0;
3447
    }
3448
    s->current_picture.qscale_table[mb_pos] = 0;
3449

    
3450
    if (!direct) {
3451
        if (!skipped) {
3452
            GET_MVDATA(dmv_x[0], dmv_y[0]);
3453
            dmv_x[1] = dmv_x[0];
3454
            dmv_y[1] = dmv_y[0];
3455
        }
3456
        if(skipped || !s->mb_intra) {
3457
            bmvtype = decode012(gb);
3458
            switch(bmvtype) {
3459
            case 0:
3460
                bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
3461
                break;
3462
            case 1:
3463
                bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
3464
                break;
3465
            case 2:
3466
                bmvtype = BMV_TYPE_INTERPOLATED;
3467
                dmv_x[0] = dmv_y[0] = 0;
3468
            }
3469
        }
3470
    }
3471
    for(i = 0; i < 6; i++)
3472
        v->mb_type[0][s->block_index[i]] = s->mb_intra;
3473

    
3474
    if (skipped) {
3475
        if(direct) bmvtype = BMV_TYPE_INTERPOLATED;
3476
        vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
3477
        vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3478
        return;
3479
    }
3480
    if (direct) {
3481
        cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3482
        GET_MQUANT();
3483
        s->mb_intra = 0;
3484
        mb_has_coeffs = 0;
3485
        s->current_picture.qscale_table[mb_pos] = mquant;
3486
        if(!v->ttmbf)
3487
            ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3488
        dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
3489
        vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
3490
        vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3491
    } else {
3492
        if(!mb_has_coeffs && !s->mb_intra) {
3493
            /* no coded blocks - effectively skipped */
3494
            vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
3495
            vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3496
            return;
3497
        }
3498
        if(s->mb_intra && !mb_has_coeffs) {
3499
            GET_MQUANT();
3500
            s->current_picture.qscale_table[mb_pos] = mquant;
3501
            s->ac_pred = get_bits1(gb);
3502
            cbp = 0;
3503
            vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
3504
        } else {
3505
            if(bmvtype == BMV_TYPE_INTERPOLATED) {
3506
                GET_MVDATA(dmv_x[0], dmv_y[0]);
3507
                if(!mb_has_coeffs) {
3508
                    /* interpolated skipped block */
3509
                    vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
3510
                    vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3511
                    return;
3512
                }
3513
            }
3514
            vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
3515
            if(!s->mb_intra) {
3516
                vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3517
            }
3518
            if(s->mb_intra)
3519
                s->ac_pred = get_bits1(gb);
3520
            cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3521
            GET_MQUANT();
3522
            s->current_picture.qscale_table[mb_pos] = mquant;
3523
            if(!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3524
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3525
        }
3526
    }
3527
    dst_idx = 0;
3528
    for (i=0; i<6; i++)
3529
    {
3530
        s->dc_val[0][s->block_index[i]] = 0;
3531
        dst_idx += i >> 2;
3532
        val = ((cbp >> (5 - i)) & 1);
3533
        off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3534
        v->mb_type[0][s->block_index[i]] = s->mb_intra;
3535
        if(s->mb_intra) {
3536
            /* check if prediction blocks A and C are available */
3537
            v->a_avail = v->c_avail = 0;
3538
            if(i == 2 || i == 3 || !s->first_slice_line)
3539
                v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3540
            if(i == 1 || i == 3 || s->mb_x)
3541
                v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3542

    
3543
            vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
3544
            if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3545
            s->dsp.vc1_inv_trans_8x8(s->block[i]);
3546
            if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
3547
            s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
3548
        } else if(val) {
3549
            vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize, (i&4) && (s->flags & CODEC_FLAG_GRAY), 0, 0, 0);
3550
            if(!v->ttmbf && ttmb < 8) ttmb = -1;
3551
            first_block = 0;
3552
        }
3553
    }
3554
}
3555

    
3556
/** Decode blocks of I-frame
3557
 */
3558
static void vc1_decode_i_blocks(VC1Context *v)
3559
{
3560
    int k, j;
3561
    MpegEncContext *s = &v->s;
3562
    int cbp, val;
3563
    uint8_t *coded_val;
3564
    int mb_pos;
3565

    
3566
    /* select codingmode used for VLC tables selection */
3567
    switch(v->y_ac_table_index){
3568
    case 0:
3569
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
3570
        break;
3571
    case 1:
3572
        v->codingset = CS_HIGH_MOT_INTRA;
3573
        break;
3574
    case 2:
3575
        v->codingset = CS_MID_RATE_INTRA;
3576
        break;
3577
    }
3578

    
3579
    switch(v->c_ac_table_index){
3580
    case 0:
3581
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
3582
        break;
3583
    case 1:
3584
        v->codingset2 = CS_HIGH_MOT_INTER;
3585
        break;
3586
    case 2:
3587
        v->codingset2 = CS_MID_RATE_INTER;
3588
        break;
3589
    }
3590

    
3591
    /* Set DC scale - y and c use the same */
3592
    s->y_dc_scale = s->y_dc_scale_table[v->pq];
3593
    s->c_dc_scale = s->c_dc_scale_table[v->pq];
3594

    
3595
    //do frame decode
3596
    s->mb_x = s->mb_y = 0;
3597
    s->mb_intra = 1;
3598
    s->first_slice_line = 1;
3599
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3600
        for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
3601
            ff_init_block_index(s);
3602
            ff_update_block_index(s);
3603
            s->dsp.clear_blocks(s->block[0]);
3604
            mb_pos = s->mb_x + s->mb_y * s->mb_width;
3605
            s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
3606
            s->current_picture.qscale_table[mb_pos] = v->pq;
3607
            s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
3608
            s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
3609

    
3610
            // do actual MB decoding and displaying
3611
            cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
3612
            v->s.ac_pred = get_bits1(&v->s.gb);
3613

    
3614
            for(k = 0; k < 6; k++) {
3615
                val = ((cbp >> (5 - k)) & 1);
3616

    
3617
                if (k < 4) {
3618
                    int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
3619
                    val = val ^ pred;
3620
                    *coded_val = val;
3621
                }
3622
                cbp |= val << (5 - k);
3623

    
3624
                vc1_decode_i_block(v, s->block[k], k, val, (k<4)? v->codingset : v->codingset2);
3625

    
3626
                s->dsp.vc1_inv_trans_8x8(s->block[k]);
3627
                if(v->pq >= 9 && v->overlap) {
3628
                    for(j = 0; j < 64; j++) s->block[k][j] += 128;
3629
                }
3630
            }
3631

    
3632
            vc1_put_block(v, s->block);
3633
            if(v->pq >= 9 && v->overlap) {
3634
                if(s->mb_x) {
3635
                    s->dsp.vc1_h_overlap(s->dest[0], s->linesize);
3636
                    s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
3637
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
3638
                        s->dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
3639
                        s->dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
3640
                    }
3641
                }
3642
                s->dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
3643
                s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
3644
                if(!s->first_slice_line) {
3645
                    s->dsp.vc1_v_overlap(s->dest[0], s->linesize);
3646
                    s->dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
3647
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
3648
                        s->dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
3649
                        s->dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
3650
                    }
3651
                }
3652
                s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
3653
                s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
3654
            }
3655
            if(v->s.loop_filter) vc1_loop_filter_iblk(s, s->current_picture.qscale_table[mb_pos]);
3656

    
3657
            if(get_bits_count(&s->gb) > v->bits) {
3658
                ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
3659
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
3660
                return;
3661
            }
3662
        }
3663
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3664
        s->first_slice_line = 0;
3665
    }
3666
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3667
}
3668

    
3669
/** Decode blocks of I-frame for advanced profile
3670
 */
3671
static void vc1_decode_i_blocks_adv(VC1Context *v)
3672
{
3673
    int k, j;
3674
    MpegEncContext *s = &v->s;
3675
    int cbp, val;
3676
    uint8_t *coded_val;
3677
    int mb_pos;
3678
    int mquant = v->pq;
3679
    int mqdiff;
3680
    int overlap;
3681
    GetBitContext *gb = &s->gb;
3682

    
3683
    /* select codingmode used for VLC tables selection */
3684
    switch(v->y_ac_table_index){
3685
    case 0:
3686
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
3687
        break;
3688
    case 1:
3689
        v->codingset = CS_HIGH_MOT_INTRA;
3690
        break;
3691
    case 2:
3692
        v->codingset = CS_MID_RATE_INTRA;
3693
        break;
3694
    }
3695

    
3696
    switch(v->c_ac_table_index){
3697
    case 0:
3698
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
3699
        break;
3700
    case 1:
3701
        v->codingset2 = CS_HIGH_MOT_INTER;
3702
        break;
3703
    case 2:
3704
        v->codingset2 = CS_MID_RATE_INTER;
3705
        break;
3706
    }
3707

    
3708
    //do frame decode
3709
    s->mb_x = s->mb_y = 0;
3710
    s->mb_intra = 1;
3711
    s->first_slice_line = 1;
3712
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3713
        for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
3714
            ff_init_block_index(s);
3715
            ff_update_block_index(s);
3716
            s->dsp.clear_blocks(s->block[0]);
3717
            mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3718
            s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
3719
            s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
3720
            s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
3721

    
3722
            // do actual MB decoding and displaying
3723
            cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
3724
            if(v->acpred_is_raw)
3725
                v->s.ac_pred = get_bits1(&v->s.gb);
3726
            else
3727
                v->s.ac_pred = v->acpred_plane[mb_pos];
3728

    
3729
            if(v->condover == CONDOVER_SELECT) {
3730
                if(v->overflg_is_raw)
3731
                    overlap = get_bits1(&v->s.gb);
3732
                else
3733
                    overlap = v->over_flags_plane[mb_pos];
3734
            } else
3735
                overlap = (v->condover == CONDOVER_ALL);
3736

    
3737
            GET_MQUANT();
3738

    
3739
            s->current_picture.qscale_table[mb_pos] = mquant;
3740
            /* Set DC scale - y and c use the same */
3741
            s->y_dc_scale = s->y_dc_scale_table[mquant];
3742
            s->c_dc_scale = s->c_dc_scale_table[mquant];
3743

    
3744
            for(k = 0; k < 6; k++) {
3745
                val = ((cbp >> (5 - k)) & 1);
3746

    
3747
                if (k < 4) {
3748
                    int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
3749
                    val = val ^ pred;
3750
                    *coded_val = val;
3751
                }
3752
                cbp |= val << (5 - k);
3753

    
3754
                v->a_avail = !s->first_slice_line || (k==2 || k==3);
3755
                v->c_avail = !!s->mb_x || (k==1 || k==3);
3756

    
3757
                vc1_decode_i_block_adv(v, s->block[k], k, val, (k<4)? v->codingset : v->codingset2, mquant);
3758

    
3759
                s->dsp.vc1_inv_trans_8x8(s->block[k]);
3760
                for(j = 0; j < 64; j++) s->block[k][j] += 128;
3761
            }
3762

    
3763
            vc1_put_block(v, s->block);
3764
            if(overlap) {
3765
                if(s->mb_x) {
3766
                    s->dsp.vc1_h_overlap(s->dest[0], s->linesize);
3767
                    s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
3768
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
3769
                        s->dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
3770
                        s->dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
3771
                    }
3772
                }
3773
                s->dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
3774
                s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
3775
                if(!s->first_slice_line) {
3776
                    s->dsp.vc1_v_overlap(s->dest[0], s->linesize);
3777
                    s->dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
3778
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
3779
                        s->dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
3780
                        s->dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
3781
                    }
3782
                }
3783
                s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
3784
                s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
3785
            }
3786
            if(v->s.loop_filter) vc1_loop_filter_iblk(s, s->current_picture.qscale_table[mb_pos]);
3787

    
3788
            if(get_bits_count(&s->gb) > v->bits) {
3789
                ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
3790
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
3791
                return;
3792
            }
3793
        }
3794
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3795
        s->first_slice_line = 0;
3796
    }
3797
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3798
}
3799

    
3800
static void vc1_decode_p_blocks(VC1Context *v)
3801
{
3802
    MpegEncContext *s = &v->s;
3803

    
3804
    /* select codingmode used for VLC tables selection */
3805
    switch(v->c_ac_table_index){
3806
    case 0:
3807
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
3808
        break;
3809
    case 1:
3810
        v->codingset = CS_HIGH_MOT_INTRA;
3811
        break;
3812
    case 2:
3813
        v->codingset = CS_MID_RATE_INTRA;
3814
        break;
3815
    }
3816

    
3817
    switch(v->c_ac_table_index){
3818
    case 0:
3819
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
3820
        break;
3821
    case 1:
3822
        v->codingset2 = CS_HIGH_MOT_INTER;
3823
        break;
3824
    case 2:
3825
        v->codingset2 = CS_MID_RATE_INTER;
3826
        break;
3827
    }
3828

    
3829
    s->first_slice_line = 1;
3830
    memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
3831
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3832
        for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
3833
            ff_init_block_index(s);
3834
            ff_update_block_index(s);
3835
            s->dsp.clear_blocks(s->block[0]);
3836

    
3837
            vc1_decode_p_mb(v);
3838
            if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
3839
                ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
3840
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n", get_bits_count(&s->gb), v->bits,s->mb_x,s->mb_y);
3841
                return;
3842
            }
3843
        }
3844
        memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0])*s->mb_stride);
3845
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3846
        s->first_slice_line = 0;
3847
    }
3848
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3849
}
3850

    
3851
static void vc1_decode_b_blocks(VC1Context *v)
3852
{
3853
    MpegEncContext *s = &v->s;
3854

    
3855
    /* select codingmode used for VLC tables selection */
3856
    switch(v->c_ac_table_index){
3857
    case 0:
3858
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
3859
        break;
3860
    case 1:
3861
        v->codingset = CS_HIGH_MOT_INTRA;
3862
        break;
3863
    case 2:
3864
        v->codingset = CS_MID_RATE_INTRA;
3865
        break;
3866
    }
3867

    
3868
    switch(v->c_ac_table_index){
3869
    case 0:
3870
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
3871
        break;
3872
    case 1:
3873
        v->codingset2 = CS_HIGH_MOT_INTER;
3874
        break;
3875
    case 2:
3876
        v->codingset2 = CS_MID_RATE_INTER;
3877
        break;
3878
    }
3879

    
3880
    s->first_slice_line = 1;
3881
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3882
        for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
3883
            ff_init_block_index(s);
3884
            ff_update_block_index(s);
3885
            s->dsp.clear_blocks(s->block[0]);
3886

    
3887
            vc1_decode_b_mb(v);
3888
            if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
3889
                ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
3890
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n", get_bits_count(&s->gb), v->bits,s->mb_x,s->mb_y);
3891
                return;
3892
            }
3893
            if(v->s.loop_filter) vc1_loop_filter_iblk(s, s->current_picture.qscale_table[s->mb_x + s->mb_y *s->mb_stride]);
3894
        }
3895
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3896
        s->first_slice_line = 0;
3897
    }
3898
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3899
}
3900

    
3901
static void vc1_decode_skip_blocks(VC1Context *v)
3902
{
3903
    MpegEncContext *s = &v->s;
3904

    
3905
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3906
    s->first_slice_line = 1;
3907
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3908
        s->mb_x = 0;
3909
        ff_init_block_index(s);
3910
        ff_update_block_index(s);
3911
        memcpy(s->dest[0], s->last_picture.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
3912
        memcpy(s->dest[1], s->last_picture.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
3913
        memcpy(s->dest[2], s->last_picture.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
3914
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3915
        s->first_slice_line = 0;
3916
    }
3917
    s->pict_type = FF_P_TYPE;
3918
}
3919

    
3920
static void vc1_decode_blocks(VC1Context *v)
3921
{
3922

    
3923
    v->s.esc3_level_length = 0;
3924
    if(v->x8_type){
3925
        ff_intrax8_decode_picture(&v->x8, 2*v->pq+v->halfpq, v->pq*(!v->pquantizer) );
3926
    }else{
3927

    
3928
        switch(v->s.pict_type) {
3929
        case FF_I_TYPE:
3930
            if(v->profile == PROFILE_ADVANCED)
3931
                vc1_decode_i_blocks_adv(v);
3932
            else
3933
                vc1_decode_i_blocks(v);
3934
            break;
3935
        case FF_P_TYPE:
3936
            if(v->p_frame_skipped)
3937
                vc1_decode_skip_blocks(v);
3938
            else
3939
                vc1_decode_p_blocks(v);
3940
            break;
3941
        case FF_B_TYPE:
3942
            if(v->bi_type){
3943
                if(v->profile == PROFILE_ADVANCED)
3944
                    vc1_decode_i_blocks_adv(v);
3945
                else
3946
                    vc1_decode_i_blocks(v);
3947
            }else
3948
                vc1_decode_b_blocks(v);
3949
            break;
3950
        }
3951
    }
3952
}
3953

    
3954
/** Find VC-1 marker in buffer
3955
 * @return position where next marker starts or end of buffer if no marker found
3956
 */
3957
static av_always_inline const uint8_t* find_next_marker(const uint8_t *src, const uint8_t *end)
3958
{
3959
    uint32_t mrk = 0xFFFFFFFF;
3960

    
3961
    if(end-src < 4) return end;
3962
    while(src < end){
3963
        mrk = (mrk << 8) | *src++;
3964
        if(IS_MARKER(mrk))
3965
            return src-4;
3966
    }
3967
    return end;
3968
}
3969

    
3970
static av_always_inline int vc1_unescape_buffer(const uint8_t *src, int size, uint8_t *dst)
3971
{
3972
    int dsize = 0, i;
3973

    
3974
    if(size < 4){
3975
        for(dsize = 0; dsize < size; dsize++) *dst++ = *src++;
3976
        return size;
3977
    }
3978
    for(i = 0; i < size; i++, src++) {
3979
        if(src[0] == 3 && i >= 2 && !src[-1] && !src[-2] && i < size-1 && src[1] < 4) {
3980
            dst[dsize++] = src[1];
3981
            src++;
3982
            i++;
3983
        } else
3984
            dst[dsize++] = *src;
3985
    }
3986
    return dsize;
3987
}
3988

    
3989
/** Initialize a VC1/WMV3 decoder
3990
 * @todo TODO: Handle VC-1 IDUs (Transport level?)
3991
 * @todo TODO: Decypher remaining bits in extra_data
3992
 */
3993
static av_cold int vc1_decode_init(AVCodecContext *avctx)
3994
{
3995
    VC1Context *v = avctx->priv_data;
3996
    MpegEncContext *s = &v->s;
3997
    GetBitContext gb;
3998

    
3999
    if (!avctx->extradata_size || !avctx->extradata) return -1;
4000
    if (!(avctx->flags & CODEC_FLAG_GRAY))
4001
        avctx->pix_fmt = PIX_FMT_YUV420P;
4002
    else
4003
        avctx->pix_fmt = PIX_FMT_GRAY8;
4004
    v->s.avctx = avctx;
4005
    avctx->flags |= CODEC_FLAG_EMU_EDGE;
4006
    v->s.flags |= CODEC_FLAG_EMU_EDGE;
4007

    
4008
    if(avctx->idct_algo==FF_IDCT_AUTO){
4009
        avctx->idct_algo=FF_IDCT_WMV2;
4010
    }
4011

    
4012
    if(ff_h263_decode_init(avctx) < 0)
4013
        return -1;
4014
    if (vc1_init_common(v) < 0) return -1;
4015

    
4016
    avctx->coded_width = avctx->width;
4017
    avctx->coded_height = avctx->height;
4018
    if (avctx->codec_id == CODEC_ID_WMV3)
4019
    {
4020
        int count = 0;
4021

    
4022
        // looks like WMV3 has a sequence header stored in the extradata
4023
        // advanced sequence header may be before the first frame
4024
        // the last byte of the extradata is a version number, 1 for the
4025
        // samples we can decode
4026

    
4027
        init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
4028

    
4029
        if (decode_sequence_header(avctx, &gb) < 0)
4030
          return -1;
4031

    
4032
        count = avctx->extradata_size*8 - get_bits_count(&gb);
4033
        if (count>0)
4034
        {
4035
            av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
4036
                   count, get_bits(&gb, count));
4037
        }
4038
        else if (count < 0)
4039
        {
4040
            av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
4041
        }
4042
    } else { // VC1/WVC1
4043
        const uint8_t *start = avctx->extradata;
4044
        uint8_t *end = avctx->extradata + avctx->extradata_size;
4045
        const uint8_t *next;
4046
        int size, buf2_size;
4047
        uint8_t *buf2 = NULL;
4048
        int seq_initialized = 0, ep_initialized = 0;
4049

    
4050
        if(avctx->extradata_size < 16) {
4051
            av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
4052
            return -1;
4053
        }
4054

    
4055
        buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
4056
        if(start[0]) start++; // in WVC1 extradata first byte is its size
4057
        next = start;
4058
        for(; next < end; start = next){
4059
            next = find_next_marker(start + 4, end);
4060
            size = next - start - 4;
4061
            if(size <= 0) continue;
4062
            buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
4063
            init_get_bits(&gb, buf2, buf2_size * 8);
4064
            switch(AV_RB32(start)){
4065
            case VC1_CODE_SEQHDR:
4066
                if(decode_sequence_header(avctx, &gb) < 0){
4067
                    av_free(buf2);
4068
                    return -1;
4069
                }
4070
                seq_initialized = 1;
4071
                break;
4072
            case VC1_CODE_ENTRYPOINT:
4073
                if(decode_entry_point(avctx, &gb) < 0){
4074
                    av_free(buf2);
4075
                    return -1;
4076
                }
4077
                ep_initialized = 1;
4078
                break;
4079
            }
4080
        }
4081
        av_free(buf2);
4082
        if(!seq_initialized || !ep_initialized){
4083
            av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
4084
            return -1;
4085
        }
4086
    }
4087
    avctx->has_b_frames= !!(avctx->max_b_frames);
4088
    s->low_delay = !avctx->has_b_frames;
4089

    
4090
    s->mb_width = (avctx->coded_width+15)>>4;
4091
    s->mb_height = (avctx->coded_height+15)>>4;
4092

    
4093
    /* Allocate mb bitplanes */
4094
    v->mv_type_mb_plane = av_malloc(s->mb_stride * s->mb_height);
4095
    v->direct_mb_plane = av_malloc(s->mb_stride * s->mb_height);
4096
    v->acpred_plane = av_malloc(s->mb_stride * s->mb_height);
4097
    v->over_flags_plane = av_malloc(s->mb_stride * s->mb_height);
4098

    
4099
    v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
4100
    v->cbp = v->cbp_base + s->mb_stride;
4101

    
4102
    /* allocate block type info in that way so it could be used with s->block_index[] */
4103
    v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
4104
    v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
4105
    v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
4106
    v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
4107

    
4108
    /* Init coded blocks info */
4109
    if (v->profile == PROFILE_ADVANCED)
4110
    {
4111
//        if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
4112
//            return -1;
4113
//        if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
4114
//            return -1;
4115
    }
4116

    
4117
    ff_intrax8_common_init(&v->x8,s);
4118
    return 0;
4119
}
4120

    
4121

    
4122
/** Decode a VC1/WMV3 frame
4123
 * @todo TODO: Handle VC-1 IDUs (Transport level?)
4124
 */
4125
static int vc1_decode_frame(AVCodecContext *avctx,
4126
                            void *data, int *data_size,
4127
                            const uint8_t *buf, int buf_size)
4128
{
4129
    VC1Context *v = avctx->priv_data;
4130
    MpegEncContext *s = &v->s;
4131
    AVFrame *pict = data;
4132
    uint8_t *buf2 = NULL;
4133

    
4134
    /* no supplementary picture */
4135
    if (buf_size == 0) {
4136
        /* special case for last picture */
4137
        if (s->low_delay==0 && s->next_picture_ptr) {
4138
            *pict= *(AVFrame*)s->next_picture_ptr;
4139
            s->next_picture_ptr= NULL;
4140

    
4141
            *data_size = sizeof(AVFrame);
4142
        }
4143

    
4144
        return 0;
4145
    }
4146

    
4147
    /* We need to set current_picture_ptr before reading the header,
4148
     * otherwise we cannot store anything in there. */
4149
    if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
4150
        int i= ff_find_unused_picture(s, 0);
4151
        s->current_picture_ptr= &s->picture[i];
4152
    }
4153

    
4154
    //for advanced profile we may need to parse and unescape data
4155
    if (avctx->codec_id == CODEC_ID_VC1) {
4156
        int buf_size2 = 0;
4157
        buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
4158

    
4159
        if(IS_MARKER(AV_RB32(buf))){ /* frame starts with marker and needs to be parsed */
4160
            const uint8_t *start, *end, *next;
4161
            int size;
4162

    
4163
            next = buf;
4164
            for(start = buf, end = buf + buf_size; next < end; start = next){
4165
                next = find_next_marker(start + 4, end);
4166
                size = next - start - 4;
4167
                if(size <= 0) continue;
4168
                switch(AV_RB32(start)){
4169
                case VC1_CODE_FRAME:
4170
                    buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
4171
                    break;
4172
                case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
4173
                    buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
4174
                    init_get_bits(&s->gb, buf2, buf_size2*8);
4175
                    decode_entry_point(avctx, &s->gb);
4176
                    break;
4177
                case VC1_CODE_SLICE:
4178
                    av_log(avctx, AV_LOG_ERROR, "Sliced decoding is not implemented (yet)\n");
4179
                    av_free(buf2);
4180
                    return -1;
4181
                }
4182
            }
4183
        }else if(v->interlace && ((buf[0] & 0xC0) == 0xC0)){ /* WVC1 interlaced stores both fields divided by marker */
4184
            const uint8_t *divider;
4185

    
4186
            divider = find_next_marker(buf, buf + buf_size);
4187
            if((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD){
4188
                av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
4189
                av_free(buf2);
4190
                return -1;
4191
            }
4192

    
4193
            buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
4194
            // TODO
4195
            av_free(buf2);return -1;
4196
        }else{
4197
            buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
4198
        }
4199
        init_get_bits(&s->gb, buf2, buf_size2*8);
4200
    } else
4201
        init_get_bits(&s->gb, buf, buf_size*8);
4202
    // do parse frame header
4203
    if(v->profile < PROFILE_ADVANCED) {
4204
        if(vc1_parse_frame_header(v, &s->gb) == -1) {
4205
            av_free(buf2);
4206
            return -1;
4207
        }
4208
    } else {
4209
        if(vc1_parse_frame_header_adv(v, &s->gb) == -1) {
4210
            av_free(buf2);
4211
            return -1;
4212
        }
4213
    }
4214

    
4215
    if(s->pict_type != FF_I_TYPE && !v->res_rtm_flag){
4216
        av_free(buf2);
4217
        return -1;
4218
    }
4219

    
4220
    // for hurry_up==5
4221
    s->current_picture.pict_type= s->pict_type;
4222
    s->current_picture.key_frame= s->pict_type == FF_I_TYPE;
4223

    
4224
    /* skip B-frames if we don't have reference frames */
4225
    if(s->last_picture_ptr==NULL && (s->pict_type==FF_B_TYPE || s->dropable)){
4226
        av_free(buf2);
4227
        return -1;//buf_size;
4228
    }
4229
    /* skip b frames if we are in a hurry */
4230
    if(avctx->hurry_up && s->pict_type==FF_B_TYPE) return -1;//buf_size;
4231
    if(   (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==FF_B_TYPE)
4232
       || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=FF_I_TYPE)
4233
       ||  avctx->skip_frame >= AVDISCARD_ALL) {
4234
        av_free(buf2);
4235
        return buf_size;
4236
    }
4237
    /* skip everything if we are in a hurry>=5 */
4238
    if(avctx->hurry_up>=5) {
4239
        av_free(buf2);
4240
        return -1;//buf_size;
4241
    }
4242

    
4243
    if(s->next_p_frame_damaged){
4244
        if(s->pict_type==FF_B_TYPE)
4245
            return buf_size;
4246
        else
4247
            s->next_p_frame_damaged=0;
4248
    }
4249

    
4250
    if(MPV_frame_start(s, avctx) < 0) {
4251
        av_free(buf2);
4252
        return -1;
4253
    }
4254

    
4255
    s->me.qpel_put= s->dsp.put_qpel_pixels_tab;
4256
    s->me.qpel_avg= s->dsp.avg_qpel_pixels_tab;
4257

    
4258
    ff_er_frame_start(s);
4259

    
4260
    v->bits = buf_size * 8;
4261
    vc1_decode_blocks(v);
4262
//av_log(s->avctx, AV_LOG_INFO, "Consumed %i/%i bits\n", get_bits_count(&s->gb), buf_size*8);
4263
//  if(get_bits_count(&s->gb) > buf_size * 8)
4264
//      return -1;
4265
    ff_er_frame_end(s);
4266

    
4267
    MPV_frame_end(s);
4268

    
4269
assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
4270
assert(s->current_picture.pict_type == s->pict_type);
4271
    if (s->pict_type == FF_B_TYPE || s->low_delay) {
4272
        *pict= *(AVFrame*)s->current_picture_ptr;
4273
    } else if (s->last_picture_ptr != NULL) {
4274
        *pict= *(AVFrame*)s->last_picture_ptr;
4275
    }
4276

    
4277
    if(s->last_picture_ptr || s->low_delay){
4278
        *data_size = sizeof(AVFrame);
4279
        ff_print_debug_info(s, pict);
4280
    }
4281

    
4282
    /* Return the Picture timestamp as the frame number */
4283
    /* we subtract 1 because it is added on utils.c     */
4284
    avctx->frame_number = s->picture_number - 1;
4285

    
4286
    av_free(buf2);
4287
    return buf_size;
4288
}
4289

    
4290

    
4291
/** Close a VC1/WMV3 decoder
4292
 * @warning Initial try at using MpegEncContext stuff
4293
 */
4294
static av_cold int vc1_decode_end(AVCodecContext *avctx)
4295
{
4296
    VC1Context *v = avctx->priv_data;
4297

    
4298
    av_freep(&v->hrd_rate);
4299
    av_freep(&v->hrd_buffer);
4300
    MPV_common_end(&v->s);
4301
    av_freep(&v->mv_type_mb_plane);
4302
    av_freep(&v->direct_mb_plane);
4303
    av_freep(&v->acpred_plane);
4304
    av_freep(&v->over_flags_plane);
4305
    av_freep(&v->mb_type_base);
4306
    av_freep(&v->cbp_base);
4307
    ff_intrax8_common_end(&v->x8);
4308
    return 0;
4309
}
4310

    
4311

    
4312
AVCodec vc1_decoder = {
4313
    "vc1",
4314
    CODEC_TYPE_VIDEO,
4315
    CODEC_ID_VC1,
4316
    sizeof(VC1Context),
4317
    vc1_decode_init,
4318
    NULL,
4319
    vc1_decode_end,
4320
    vc1_decode_frame,
4321
    CODEC_CAP_DELAY,
4322
    NULL,
4323
    .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
4324
};
4325

    
4326
AVCodec wmv3_decoder = {
4327
    "wmv3",
4328
    CODEC_TYPE_VIDEO,
4329
    CODEC_ID_WMV3,
4330
    sizeof(VC1Context),
4331
    vc1_decode_init,
4332
    NULL,
4333
    vc1_decode_end,
4334
    vc1_decode_frame,
4335
    CODEC_CAP_DELAY,
4336
    NULL,
4337
    .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
4338
};