Statistics
| Branch: | Revision:

ffmpeg / libavcodec / vc1.c @ f92a6c66

History | View | Annotate | Download (155 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
#include "vdpau_internal.h"
39

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

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

    
48

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

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

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

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

    
114
    return 0;
115
}
116

    
117
/***********************************************************************/
118
/**
119
 * @defgroup vc1bitplane VC-1 Bitplane decoding
120
 * @see 8.7, p56
121
 * @{
122
 */
123

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

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

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

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

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

    
179
/** Decode a bitplane's bits
180
 * @param data bitplane where to store the decode bits
181
 * @param[out] raw_flag pointer to the flag indicating that this bitplane is not coded explicitly
182
 * @param v VC-1 context for bit reading and logging
183
 * @return Status
184
 * @todo FIXME: Optimize
185
 */
186
static int bitplane_decoding(uint8_t* data, int *raw_flag, VC1Context *v)
187
{
188
    GetBitContext *gb = &v->s.gb;
189

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

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

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

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

    
310
/** @} */ //Bitplane group
311

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

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

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

    
356
/**
357
 * VC-1 in-loop deblocking filter
358
 * @param src source block type
359
 * @param step distance between horizontally adjacent elements
360
 * @param stride distance between vertically adjacent elements
361
 * @param len edge length to filter (4 or 8 pixels)
362
 * @param pq block quantizer
363
 * @see 8.6
364
 */
365
static void vc1_loop_filter(uint8_t* src, int step, int stride, int len, int pq)
366
{
367
    int i;
368
    int filt3;
369

    
370
    for(i = 0; i < len; i += 4){
371
        filt3 = vc1_filter_line(src + 2*step, stride, pq);
372
        if(filt3){
373
            vc1_filter_line(src + 0*step, stride, pq);
374
            vc1_filter_line(src + 1*step, stride, pq);
375
            vc1_filter_line(src + 3*step, stride, pq);
376
        }
377
        src += step * 4;
378
    }
379
}
380

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

    
397
/***********************************************************************/
398
/** VOP Dquant decoding
399
 * @param v VC-1 Context
400
 */
401
static int vop_dquant_decoding(VC1Context *v)
402
{
403
    GetBitContext *gb = &v->s.gb;
404
    int pqdiff;
405

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

    
442
/** Put block onto picture
443
 */
444
static void vc1_put_block(VC1Context *v, DCTELEM block[6][64])
445
{
446
    uint8_t *Y;
447
    int ys, us, vs;
448
    DSPContext *dsp = &v->s.dsp;
449

    
450
    if(v->rangeredfrm) {
451
        int i, j, k;
452
        for(k = 0; k < 6; k++)
453
            for(j = 0; j < 8; j++)
454
                for(i = 0; i < 8; i++)
455
                    block[k][i + j*8] = ((block[k][i + j*8] - 128) << 1) + 128;
456

    
457
    }
458
    ys = v->s.current_picture.linesize[0];
459
    us = v->s.current_picture.linesize[1];
460
    vs = v->s.current_picture.linesize[2];
461
    Y = v->s.dest[0];
462

    
463
    dsp->put_pixels_clamped(block[0], Y, ys);
464
    dsp->put_pixels_clamped(block[1], Y + 8, ys);
465
    Y += ys * 8;
466
    dsp->put_pixels_clamped(block[2], Y, ys);
467
    dsp->put_pixels_clamped(block[3], Y + 8, ys);
468

    
469
    if(!(v->s.flags & CODEC_FLAG_GRAY)) {
470
        dsp->put_pixels_clamped(block[4], v->s.dest[1], us);
471
        dsp->put_pixels_clamped(block[5], v->s.dest[2], vs);
472
    }
473
}
474

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

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

    
487
    mx = s->mv[dir][0][0];
488
    my = s->mv[dir][0][1];
489

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

    
511
    src_x = s->mb_x * 16 + (mx >> 2);
512
    src_y = s->mb_y * 16 + (my >> 2);
513
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
514
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
515

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

    
528
    srcY += src_y * s->linesize + src_x;
529
    srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
530
    srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
531

    
532
    /* for grayscale we should not try to read from unknown area */
533
    if(s->flags & CODEC_FLAG_GRAY) {
534
        srcU = s->edge_emu_buffer + 18 * s->linesize;
535
        srcV = s->edge_emu_buffer + 18 * s->linesize;
536
    }
537

    
538
    if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
539
       || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3
540
       || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){
541
        uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
542

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

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

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

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

    
606
        if(!v->rnd)
607
            dsp->put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
608
        else
609
            dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
610
    }
611

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

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

    
636
    if(!v->s.last_picture.data[0])return;
637
    mx = s->mv[0][n][0];
638
    my = s->mv[0][n][1];
639
    srcY = s->last_picture.data[0];
640

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

    
643
    src_x = s->mb_x * 16 + (n&1) * 8 + (mx >> 2);
644
    src_y = s->mb_y * 16 + (n&2) * 4 + (my >> 2);
645

    
646
    if(v->profile != PROFILE_ADVANCED){
647
        src_x   = av_clip(  src_x, -16, s->mb_width  * 16);
648
        src_y   = av_clip(  src_y, -16, s->mb_height * 16);
649
    }else{
650
        src_x   = av_clip(  src_x, -17, s->avctx->coded_width);
651
        src_y   = av_clip(  src_y, -18, s->avctx->coded_height + 1);
652
    }
653

    
654
    srcY += src_y * s->linesize + src_x;
655

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

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

    
679
            src = srcY;
680
            for(j = 0; j < 9 + s->mspel*2; j++) {
681
                for(i = 0; i < 9 + s->mspel*2; i++) src[i] = v->luty[src[i]];
682
                src += s->linesize;
683
            }
684
        }
685
        srcY += s->mspel * (1 + s->linesize);
686
    }
687

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

    
700
static inline int median4(int a, int b, int c, int d)
701
{
702
    if(a < b) {
703
        if(c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
704
        else      return (FFMIN(b, c) + FFMAX(a, d)) / 2;
705
    } else {
706
        if(c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
707
        else      return (FFMIN(a, c) + FFMAX(b, d)) / 2;
708
    }
709
}
710

    
711

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

    
724
    if(!v->s.last_picture.data[0])return;
725
    if(s->flags & CODEC_FLAG_GRAY) return;
726

    
727
    for(i = 0; i < 4; i++) {
728
        mvx[i] = s->mv[0][i][0];
729
        mvy[i] = s->mv[0][i][1];
730
        intra[i] = v->mb_type[0][s->block_index[i]];
731
    }
732

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

    
769
    s->current_picture.motion_val[1][s->block_index[0]][0] = tx;
770
    s->current_picture.motion_val[1][s->block_index[0]][1] = ty;
771
    uvmx = (tx + ((tx&3) == 3)) >> 1;
772
    uvmy = (ty + ((ty&3) == 3)) >> 1;
773
    if(v->fastuvmc) {
774
        uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
775
        uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
776
    }
777

    
778
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
779
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
780

    
781
    if(v->profile != PROFILE_ADVANCED){
782
        uvsrc_x = av_clip(uvsrc_x,  -8, s->mb_width  *  8);
783
        uvsrc_y = av_clip(uvsrc_y,  -8, s->mb_height *  8);
784
    }else{
785
        uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
786
        uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
787
    }
788

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

    
801
        /* if we deal with range reduction we need to scale source blocks */
802
        if(v->rangeredfrm) {
803
            int i, j;
804
            uint8_t *src, *src2;
805

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

    
821
            src = srcU; src2 = srcV;
822
            for(j = 0; j < 9; j++) {
823
                for(i = 0; i < 9; i++) {
824
                    src[i] = v->lutuv[src[i]];
825
                    src2[i] = v->lutuv[src2[i]];
826
                }
827
                src += s->uvlinesize;
828
                src2 += s->uvlinesize;
829
            }
830
        }
831
    }
832

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

    
846
static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb);
847

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

    
859
    av_log(avctx, AV_LOG_DEBUG, "Header: %0X\n", show_bits(gb, 32));
860
    v->profile = get_bits(gb, 2);
861
    if (v->profile == PROFILE_COMPLEX)
862
    {
863
        av_log(avctx, AV_LOG_ERROR, "WMV3 Complex Profile is not fully supported\n");
864
    }
865

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

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

    
898
    v->res_x8 = get_bits1(gb); //reserved
899
    v->multires = get_bits1(gb);
900
    v->res_fasttx = get_bits1(gb);
901
    if (!v->res_fasttx)
902
    {
903
        v->s.dsp.vc1_inv_trans_8x8 = ff_simple_idct;
904
        v->s.dsp.vc1_inv_trans_8x4 = ff_simple_idct84_add;
905
        v->s.dsp.vc1_inv_trans_4x8 = ff_simple_idct48_add;
906
        v->s.dsp.vc1_inv_trans_4x4 = ff_simple_idct44_add;
907
    }
908

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

    
926
    v->res_transtab = get_bits1(gb);
927
    if (v->res_transtab)
928
    {
929
        av_log(avctx, AV_LOG_ERROR,
930
               "1 for reserved RES_TRANSTAB is forbidden\n");
931
        return -1;
932
    }
933

    
934
    v->overlap = get_bits1(gb); //common
935

    
936
    v->s.resync_marker = get_bits1(gb);
937
    v->rangered = get_bits1(gb);
938
    if (v->rangered && v->profile == PROFILE_SIMPLE)
939
    {
940
        av_log(avctx, AV_LOG_INFO,
941
               "RANGERED should be set to 0 in simple profile\n");
942
    }
943

    
944
    v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
945
    v->quantizer_mode = get_bits(gb, 2); //common
946

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

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

    
988
    // (fps-2)/4 (->30)
989
    v->frmrtq_postproc = get_bits(gb, 3); //common
990
    // (bitrate-32kbps)/64kbps
991
    v->bitrtq_postproc = get_bits(gb, 5); //common
992
    v->postprocflag = get_bits1(gb); //common
993

    
994
    v->s.avctx->coded_width = (get_bits(gb, 12) + 1) << 1;
995
    v->s.avctx->coded_height = (get_bits(gb, 12) + 1) << 1;
996
    v->s.avctx->width = v->s.avctx->coded_width;
997
    v->s.avctx->height = v->s.avctx->coded_height;
998
    v->broadcast = get_bits1(gb);
999
    v->interlace = get_bits1(gb);
1000
    v->tfcntrflag = get_bits1(gb);
1001
    v->finterpflag = get_bits1(gb);
1002
    skip_bits1(gb); // reserved
1003

    
1004
    v->s.h_edge_pos = v->s.avctx->coded_width;
1005
    v->s.v_edge_pos = v->s.avctx->coded_height;
1006

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

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

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

    
1054
        if(get_bits1(gb)){
1055
            v->color_prim = get_bits(gb, 8);
1056
            v->transfer_char = get_bits(gb, 8);
1057
            v->matrix_coef = get_bits(gb, 8);
1058
        }
1059
    }
1060

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

    
1075
static int decode_entry_point(AVCodecContext *avctx, GetBitContext *gb)
1076
{
1077
    VC1Context *v = avctx->priv_data;
1078
    int i;
1079

    
1080
    av_log(avctx, AV_LOG_DEBUG, "Entry point: %08X\n", show_bits_long(gb, 32));
1081
    v->broken_link = get_bits1(gb);
1082
    v->closed_entry = get_bits1(gb);
1083
    v->panscanflag = get_bits1(gb);
1084
    v->refdist_flag = get_bits1(gb);
1085
    v->s.loop_filter = get_bits1(gb);
1086
    v->fastuvmc = get_bits1(gb);
1087
    v->extended_mv = get_bits1(gb);
1088
    v->dquant = get_bits(gb, 2);
1089
    v->vstransform = get_bits1(gb);
1090
    v->overlap = get_bits1(gb);
1091
    v->quantizer_mode = get_bits(gb, 2);
1092

    
1093
    if(v->hrd_param_flag){
1094
        for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
1095
            skip_bits(gb, 8); //hrd_full[n]
1096
        }
1097
    }
1098

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

    
1114
    av_log(avctx, AV_LOG_DEBUG, "Entry point info:\n"
1115
        "BrokenLink=%i, ClosedEntry=%i, PanscanFlag=%i\n"
1116
        "RefDist=%i, Postproc=%i, FastUVMC=%i, ExtMV=%i\n"
1117
        "DQuant=%i, VSTransform=%i, Overlap=%i, Qmode=%i\n",
1118
        v->broken_link, v->closed_entry, v->panscanflag, v->refdist_flag, v->s.loop_filter,
1119
        v->fastuvmc, v->extended_mv, v->dquant, v->vstransform, v->overlap, v->quantizer_mode);
1120

    
1121
    return 0;
1122
}
1123

    
1124
static int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
1125
{
1126
    int pqindex, lowquant, status;
1127

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

    
1140
    v->bi_type = 0;
1141
    if(v->s.pict_type == FF_B_TYPE) {
1142
        v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
1143
        v->bfraction = ff_vc1_bfraction_lut[v->bfraction_lut_index];
1144
        if(v->bfraction == 0) {
1145
            v->s.pict_type = FF_BI_TYPE;
1146
        }
1147
    }
1148
    if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)
1149
        skip_bits(gb, 7); // skip buffer fullness
1150

    
1151
    /* calculate RND */
1152
    if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)
1153
        v->rnd = 1;
1154
    if(v->s.pict_type == FF_P_TYPE)
1155
        v->rnd ^= 1;
1156

    
1157
    /* Quantizer stuff */
1158
    pqindex = get_bits(gb, 5);
1159
    if(!pqindex) return -1;
1160
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1161
        v->pq = ff_vc1_pquant_table[0][pqindex];
1162
    else
1163
        v->pq = ff_vc1_pquant_table[1][pqindex];
1164

    
1165
    v->pquantizer = 1;
1166
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1167
        v->pquantizer = pqindex < 9;
1168
    if (v->quantizer_mode == QUANT_NON_UNIFORM)
1169
        v->pquantizer = 0;
1170
    v->pqindex = pqindex;
1171
    if (pqindex < 9) v->halfpq = get_bits1(gb);
1172
    else v->halfpq = 0;
1173
    if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
1174
        v->pquantizer = get_bits1(gb);
1175
    v->dquantfrm = 0;
1176
    if (v->extended_mv == 1) v->mvrange = get_unary(gb, 0, 3);
1177
    v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1178
    v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
1179
    v->range_x = 1 << (v->k_x - 1);
1180
    v->range_y = 1 << (v->k_y - 1);
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_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
1385
        v->bfraction = ff_vc1_bfraction_lut[v->bfraction_lut_index];
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_bits(gb, 2);
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 vc1block 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 overlap flag indicating that overlap filtering is used
2180
 * @param pq integer part of picture quantizer
2181
 * @param[in] n block index in the current MB
2182
 * @param dc_val_ptr Pointer to DC predictor
2183
 * @param dir_ptr Prediction direction for use in AC prediction
2184
 */
2185
static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2186
                              int16_t **dc_val_ptr, int *dir_ptr)
2187
{
2188
    int a, b, c, wrap, pred, scale;
2189
    int16_t *dc_val;
2190
    static const uint16_t dcpred[32] = {
2191
    -1, 1024,  512,  341,  256,  205,  171,  146,  128,
2192
         114,  102,   93,   85,   79,   73,   68,   64,
2193
          60,   57,   54,   51,   49,   47,   45,   43,
2194
          41,   39,   38,   37,   35,   34,   33
2195
    };
2196

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

    
2201
    wrap = s->block_wrap[n];
2202
    dc_val= s->dc_val[0] + s->block_index[n];
2203

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

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

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

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

    
2237

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

    
2258
    /* find prediction - wmv3_dc_scale always used here in fact */
2259
    if (n < 4)     scale = s->y_dc_scale;
2260
    else           scale = s->c_dc_scale;
2261

    
2262
    wrap = s->block_wrap[n];
2263
    dc_val= s->dc_val[0] + s->block_index[n];
2264

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

    
2292
    if(a_avail && c_avail) {
2293
        if(abs(a - b) <= abs(b - c)) {
2294
            pred = c;
2295
            *dir_ptr = 1;//left
2296
        } else {
2297
            pred = a;
2298
            *dir_ptr = 0;//top
2299
        }
2300
    } else if(a_avail) {
2301
        pred = a;
2302
        *dir_ptr = 0;//top
2303
    } else if(c_avail) {
2304
        pred = c;
2305
        *dir_ptr = 1;//left
2306
    } else {
2307
        pred = 0;
2308
        *dir_ptr = 1;//left
2309
    }
2310

    
2311
    /* update predictor */
2312
    *dc_val_ptr = &dc_val[0];
2313
    return pred;
2314
}
2315

    
2316
/** @} */ // Block group
2317

    
2318
/**
2319
 * @defgroup vc1_std_mb VC1 Macroblock-level functions in Simple/Main Profiles
2320
 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2321
 * @{
2322
 */
2323

    
2324
static inline int vc1_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
2325
{
2326
    int xy, wrap, pred, a, b, c;
2327

    
2328
    xy = s->block_index[n];
2329
    wrap = s->b8_stride;
2330

    
2331
    /* B C
2332
     * A X
2333
     */
2334
    a = s->coded_block[xy - 1       ];
2335
    b = s->coded_block[xy - 1 - wrap];
2336
    c = s->coded_block[xy     - wrap];
2337

    
2338
    if (b == c) {
2339
        pred = a;
2340
    } else {
2341
        pred = c;
2342
    }
2343

    
2344
    /* store value */
2345
    *coded_block_ptr = &s->coded_block[xy];
2346

    
2347
    return pred;
2348
}
2349

    
2350
/**
2351
 * Decode one AC coefficient
2352
 * @param v The VC1 context
2353
 * @param last Last coefficient
2354
 * @param skip How much zero coefficients to skip
2355
 * @param value Decoded AC coefficient value
2356
 * @param codingset set of VLC to decode data
2357
 * @see 8.1.3.4
2358
 */
2359
static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip, int *value, int codingset)
2360
{
2361
    GetBitContext *gb = &v->s.gb;
2362
    int index, escape, run = 0, level = 0, lst = 0;
2363

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

    
2412
    *last = lst;
2413
    *skip = run;
2414
    *value = level;
2415
}
2416

    
2417
/** Decode intra block in intra frames - should be faster than decode_intra_block
2418
 * @param v VC1Context
2419
 * @param block block to decode
2420
 * @param[in] n subblock index
2421
 * @param coded are AC coeffs present or not
2422
 * @param codingset set of VLC to decode data
2423
 */
2424
static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset)
2425
{
2426
    GetBitContext *gb = &v->s.gb;
2427
    MpegEncContext *s = &v->s;
2428
    int dc_pred_dir = 0; /* Direction of the DC prediction used */
2429
    int run_diff, i;
2430
    int16_t *dc_val;
2431
    int16_t *ac_val, *ac_val2;
2432
    int dcdiff;
2433

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

    
2464
    /* Prediction */
2465
    dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2466
    *dc_val = dcdiff;
2467

    
2468
    /* Store the quantized DC coeff, used for prediction */
2469
    if (n < 4) {
2470
        block[0] = dcdiff * s->y_dc_scale;
2471
    } else {
2472
        block[0] = dcdiff * s->c_dc_scale;
2473
    }
2474
    /* Skip ? */
2475
    run_diff = 0;
2476
    i = 0;
2477
    if (!coded) {
2478
        goto not_coded;
2479
    }
2480

    
2481
    //AC Decoding
2482
    i = 1;
2483

    
2484
    {
2485
        int last = 0, skip, value;
2486
        const int8_t *zz_table;
2487
        int scale;
2488
        int k;
2489

    
2490
        scale = v->pq * 2 + v->halfpq;
2491

    
2492
        if(v->s.ac_pred) {
2493
            if(!dc_pred_dir)
2494
                zz_table = wmv1_scantable[2];
2495
            else
2496
                zz_table = wmv1_scantable[3];
2497
        } else
2498
            zz_table = wmv1_scantable[1];
2499

    
2500
        ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2501
        ac_val2 = ac_val;
2502
        if(dc_pred_dir) //left
2503
            ac_val -= 16;
2504
        else //top
2505
            ac_val -= 16 * s->block_wrap[n];
2506

    
2507
        while (!last) {
2508
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2509
            i += skip;
2510
            if(i > 63)
2511
                break;
2512
            block[zz_table[i++]] = value;
2513
        }
2514

    
2515
        /* apply AC prediction if needed */
2516
        if(s->ac_pred) {
2517
            if(dc_pred_dir) { //left
2518
                for(k = 1; k < 8; k++)
2519
                    block[k << 3] += ac_val[k];
2520
            } else { //top
2521
                for(k = 1; k < 8; k++)
2522
                    block[k] += ac_val[k + 8];
2523
            }
2524
        }
2525
        /* save AC coeffs for further prediction */
2526
        for(k = 1; k < 8; k++) {
2527
            ac_val2[k] = block[k << 3];
2528
            ac_val2[k + 8] = block[k];
2529
        }
2530

    
2531
        /* scale AC coeffs */
2532
        for(k = 1; k < 64; k++)
2533
            if(block[k]) {
2534
                block[k] *= scale;
2535
                if(!v->pquantizer)
2536
                    block[k] += (block[k] < 0) ? -v->pq : v->pq;
2537
            }
2538

    
2539
        if(s->ac_pred) i = 63;
2540
    }
2541

    
2542
not_coded:
2543
    if(!coded) {
2544
        int k, scale;
2545
        ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2546
        ac_val2 = ac_val;
2547

    
2548
        scale = v->pq * 2 + v->halfpq;
2549
        memset(ac_val2, 0, 16 * 2);
2550
        if(dc_pred_dir) {//left
2551
            ac_val -= 16;
2552
            if(s->ac_pred)
2553
                memcpy(ac_val2, ac_val, 8 * 2);
2554
        } else {//top
2555
            ac_val -= 16 * s->block_wrap[n];
2556
            if(s->ac_pred)
2557
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2558
        }
2559

    
2560
        /* apply AC prediction if needed */
2561
        if(s->ac_pred) {
2562
            if(dc_pred_dir) { //left
2563
                for(k = 1; k < 8; k++) {
2564
                    block[k << 3] = ac_val[k] * scale;
2565
                    if(!v->pquantizer && block[k << 3])
2566
                        block[k << 3] += (block[k << 3] < 0) ? -v->pq : v->pq;
2567
                }
2568
            } else { //top
2569
                for(k = 1; k < 8; k++) {
2570
                    block[k] = ac_val[k + 8] * scale;
2571
                    if(!v->pquantizer && block[k])
2572
                        block[k] += (block[k] < 0) ? -v->pq : v->pq;
2573
                }
2574
            }
2575
            i = 63;
2576
        }
2577
    }
2578
    s->block_last_index[n] = i;
2579

    
2580
    return 0;
2581
}
2582

    
2583
/** Decode intra block in intra frames - should be faster than decode_intra_block
2584
 * @param v VC1Context
2585
 * @param block block to decode
2586
 * @param[in] n subblock number
2587
 * @param coded are AC coeffs present or not
2588
 * @param codingset set of VLC to decode data
2589
 * @param mquant quantizer value for this macroblock
2590
 */
2591
static int vc1_decode_i_block_adv(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset, int mquant)
2592
{
2593
    GetBitContext *gb = &v->s.gb;
2594
    MpegEncContext *s = &v->s;
2595
    int dc_pred_dir = 0; /* Direction of the DC prediction used */
2596
    int run_diff, i;
2597
    int16_t *dc_val;
2598
    int16_t *ac_val, *ac_val2;
2599
    int dcdiff;
2600
    int a_avail = v->a_avail, c_avail = v->c_avail;
2601
    int use_pred = s->ac_pred;
2602
    int scale;
2603
    int q1, q2 = 0;
2604
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2605

    
2606
    /* Get DC differential */
2607
    if (n < 4) {
2608
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2609
    } else {
2610
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2611
    }
2612
    if (dcdiff < 0){
2613
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2614
        return -1;
2615
    }
2616
    if (dcdiff)
2617
    {
2618
        if (dcdiff == 119 /* ESC index value */)
2619
        {
2620
            /* TODO: Optimize */
2621
            if (mquant == 1) dcdiff = get_bits(gb, 10);
2622
            else if (mquant == 2) dcdiff = get_bits(gb, 9);
2623
            else dcdiff = get_bits(gb, 8);
2624
        }
2625
        else
2626
        {
2627
            if (mquant == 1)
2628
                dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
2629
            else if (mquant == 2)
2630
                dcdiff = (dcdiff<<1) + get_bits1(gb)   - 1;
2631
        }
2632
        if (get_bits1(gb))
2633
            dcdiff = -dcdiff;
2634
    }
2635

    
2636
    /* Prediction */
2637
    dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
2638
    *dc_val = dcdiff;
2639

    
2640
    /* Store the quantized DC coeff, used for prediction */
2641
    if (n < 4) {
2642
        block[0] = dcdiff * s->y_dc_scale;
2643
    } else {
2644
        block[0] = dcdiff * s->c_dc_scale;
2645
    }
2646
    /* Skip ? */
2647
    run_diff = 0;
2648
    i = 0;
2649

    
2650
    //AC Decoding
2651
    i = 1;
2652

    
2653
    /* check if AC is needed at all */
2654
    if(!a_avail && !c_avail) use_pred = 0;
2655
    ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2656
    ac_val2 = ac_val;
2657

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

    
2660
    if(dc_pred_dir) //left
2661
        ac_val -= 16;
2662
    else //top
2663
        ac_val -= 16 * s->block_wrap[n];
2664

    
2665
    q1 = s->current_picture.qscale_table[mb_pos];
2666
    if(dc_pred_dir && c_avail && mb_pos) q2 = s->current_picture.qscale_table[mb_pos - 1];
2667
    if(!dc_pred_dir && a_avail && mb_pos >= s->mb_stride) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2668
    if(dc_pred_dir && n==1) q2 = q1;
2669
    if(!dc_pred_dir && n==2) q2 = q1;
2670
    if(n==3) q2 = q1;
2671

    
2672
    if(coded) {
2673
        int last = 0, skip, value;
2674
        const int8_t *zz_table;
2675
        int k;
2676

    
2677
        if(v->s.ac_pred) {
2678
            if(!dc_pred_dir)
2679
                zz_table = wmv1_scantable[2];
2680
            else
2681
                zz_table = wmv1_scantable[3];
2682
        } else
2683
            zz_table = wmv1_scantable[1];
2684

    
2685
        while (!last) {
2686
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2687
            i += skip;
2688
            if(i > 63)
2689
                break;
2690
            block[zz_table[i++]] = value;
2691
        }
2692

    
2693
        /* apply AC prediction if needed */
2694
        if(use_pred) {
2695
            /* scale predictors if needed*/
2696
            if(q2 && q1!=q2) {
2697
                q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2698
                q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2699

    
2700
                if(dc_pred_dir) { //left
2701
                    for(k = 1; k < 8; k++)
2702
                        block[k << 3] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2703
                } else { //top
2704
                    for(k = 1; k < 8; k++)
2705
                        block[k] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2706
                }
2707
            } else {
2708
                if(dc_pred_dir) { //left
2709
                    for(k = 1; k < 8; k++)
2710
                        block[k << 3] += ac_val[k];
2711
                } else { //top
2712
                    for(k = 1; k < 8; k++)
2713
                        block[k] += ac_val[k + 8];
2714
                }
2715
            }
2716
        }
2717
        /* save AC coeffs for further prediction */
2718
        for(k = 1; k < 8; k++) {
2719
            ac_val2[k] = block[k << 3];
2720
            ac_val2[k + 8] = block[k];
2721
        }
2722

    
2723
        /* scale AC coeffs */
2724
        for(k = 1; k < 64; k++)
2725
            if(block[k]) {
2726
                block[k] *= scale;
2727
                if(!v->pquantizer)
2728
                    block[k] += (block[k] < 0) ? -mquant : mquant;
2729
            }
2730

    
2731
        if(use_pred) i = 63;
2732
    } else { // no AC coeffs
2733
        int k;
2734

    
2735
        memset(ac_val2, 0, 16 * 2);
2736
        if(dc_pred_dir) {//left
2737
            if(use_pred) {
2738
                memcpy(ac_val2, ac_val, 8 * 2);
2739
                if(q2 && q1!=q2) {
2740
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2741
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2742
                    for(k = 1; k < 8; k++)
2743
                        ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2744
                }
2745
            }
2746
        } else {//top
2747
            if(use_pred) {
2748
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2749
                if(q2 && q1!=q2) {
2750
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2751
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2752
                    for(k = 1; k < 8; k++)
2753
                        ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2754
                }
2755
            }
2756
        }
2757

    
2758
        /* apply AC prediction if needed */
2759
        if(use_pred) {
2760
            if(dc_pred_dir) { //left
2761
                for(k = 1; k < 8; k++) {
2762
                    block[k << 3] = ac_val2[k] * scale;
2763
                    if(!v->pquantizer && block[k << 3])
2764
                        block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
2765
                }
2766
            } else { //top
2767
                for(k = 1; k < 8; k++) {
2768
                    block[k] = ac_val2[k + 8] * scale;
2769
                    if(!v->pquantizer && block[k])
2770
                        block[k] += (block[k] < 0) ? -mquant : mquant;
2771
                }
2772
            }
2773
            i = 63;
2774
        }
2775
    }
2776
    s->block_last_index[n] = i;
2777

    
2778
    return 0;
2779
}
2780

    
2781
/** Decode intra block in inter frames - more generic version than vc1_decode_i_block
2782
 * @param v VC1Context
2783
 * @param block block to decode
2784
 * @param[in] n subblock index
2785
 * @param coded are AC coeffs present or not
2786
 * @param mquant block quantizer
2787
 * @param codingset set of VLC to decode data
2788
 */
2789
static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n, int coded, int mquant, int codingset)
2790
{
2791
    GetBitContext *gb = &v->s.gb;
2792
    MpegEncContext *s = &v->s;
2793
    int dc_pred_dir = 0; /* Direction of the DC prediction used */
2794
    int run_diff, i;
2795
    int16_t *dc_val;
2796
    int16_t *ac_val, *ac_val2;
2797
    int dcdiff;
2798
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2799
    int a_avail = v->a_avail, c_avail = v->c_avail;
2800
    int use_pred = s->ac_pred;
2801
    int scale;
2802
    int q1, q2 = 0;
2803

    
2804
    /* XXX: Guard against dumb values of mquant */
2805
    mquant = (mquant < 1) ? 0 : ( (mquant>31) ? 31 : mquant );
2806

    
2807
    /* Set DC scale - y and c use the same */
2808
    s->y_dc_scale = s->y_dc_scale_table[mquant];
2809
    s->c_dc_scale = s->c_dc_scale_table[mquant];
2810

    
2811
    /* Get DC differential */
2812
    if (n < 4) {
2813
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2814
    } else {
2815
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2816
    }
2817
    if (dcdiff < 0){
2818
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2819
        return -1;
2820
    }
2821
    if (dcdiff)
2822
    {
2823
        if (dcdiff == 119 /* ESC index value */)
2824
        {
2825
            /* TODO: Optimize */
2826
            if (mquant == 1) dcdiff = get_bits(gb, 10);
2827
            else if (mquant == 2) dcdiff = get_bits(gb, 9);
2828
            else dcdiff = get_bits(gb, 8);
2829
        }
2830
        else
2831
        {
2832
            if (mquant == 1)
2833
                dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
2834
            else if (mquant == 2)
2835
                dcdiff = (dcdiff<<1) + get_bits1(gb)   - 1;
2836
        }
2837
        if (get_bits1(gb))
2838
            dcdiff = -dcdiff;
2839
    }
2840

    
2841
    /* Prediction */
2842
    dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
2843
    *dc_val = dcdiff;
2844

    
2845
    /* Store the quantized DC coeff, used for prediction */
2846

    
2847
    if (n < 4) {
2848
        block[0] = dcdiff * s->y_dc_scale;
2849
    } else {
2850
        block[0] = dcdiff * s->c_dc_scale;
2851
    }
2852
    /* Skip ? */
2853
    run_diff = 0;
2854
    i = 0;
2855

    
2856
    //AC Decoding
2857
    i = 1;
2858

    
2859
    /* check if AC is needed at all and adjust direction if needed */
2860
    if(!a_avail) dc_pred_dir = 1;
2861
    if(!c_avail) dc_pred_dir = 0;
2862
    if(!a_avail && !c_avail) use_pred = 0;
2863
    ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2864
    ac_val2 = ac_val;
2865

    
2866
    scale = mquant * 2 + v->halfpq;
2867

    
2868
    if(dc_pred_dir) //left
2869
        ac_val -= 16;
2870
    else //top
2871
        ac_val -= 16 * s->block_wrap[n];
2872

    
2873
    q1 = s->current_picture.qscale_table[mb_pos];
2874
    if(dc_pred_dir && c_avail && mb_pos) q2 = s->current_picture.qscale_table[mb_pos - 1];
2875
    if(!dc_pred_dir && a_avail && mb_pos >= s->mb_stride) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2876
    if(dc_pred_dir && n==1) q2 = q1;
2877
    if(!dc_pred_dir && n==2) q2 = q1;
2878
    if(n==3) q2 = q1;
2879

    
2880
    if(coded) {
2881
        int last = 0, skip, value;
2882
        const int8_t *zz_table;
2883
        int k;
2884

    
2885
        zz_table = wmv1_scantable[0];
2886

    
2887
        while (!last) {
2888
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2889
            i += skip;
2890
            if(i > 63)
2891
                break;
2892
            block[zz_table[i++]] = value;
2893
        }
2894

    
2895
        /* apply AC prediction if needed */
2896
        if(use_pred) {
2897
            /* scale predictors if needed*/
2898
            if(q2 && q1!=q2) {
2899
                q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2900
                q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2901

    
2902
                if(dc_pred_dir) { //left
2903
                    for(k = 1; k < 8; k++)
2904
                        block[k << 3] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2905
                } else { //top
2906
                    for(k = 1; k < 8; k++)
2907
                        block[k] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2908
                }
2909
            } else {
2910
                if(dc_pred_dir) { //left
2911
                    for(k = 1; k < 8; k++)
2912
                        block[k << 3] += ac_val[k];
2913
                } else { //top
2914
                    for(k = 1; k < 8; k++)
2915
                        block[k] += ac_val[k + 8];
2916
                }
2917
            }
2918
        }
2919
        /* save AC coeffs for further prediction */
2920
        for(k = 1; k < 8; k++) {
2921
            ac_val2[k] = block[k << 3];
2922
            ac_val2[k + 8] = block[k];
2923
        }
2924

    
2925
        /* scale AC coeffs */
2926
        for(k = 1; k < 64; k++)
2927
            if(block[k]) {
2928
                block[k] *= scale;
2929
                if(!v->pquantizer)
2930
                    block[k] += (block[k] < 0) ? -mquant : mquant;
2931
            }
2932

    
2933
        if(use_pred) i = 63;
2934
    } else { // no AC coeffs
2935
        int k;
2936

    
2937
        memset(ac_val2, 0, 16 * 2);
2938
        if(dc_pred_dir) {//left
2939
            if(use_pred) {
2940
                memcpy(ac_val2, ac_val, 8 * 2);
2941
                if(q2 && q1!=q2) {
2942
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2943
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2944
                    for(k = 1; k < 8; k++)
2945
                        ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2946
                }
2947
            }
2948
        } else {//top
2949
            if(use_pred) {
2950
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2951
                if(q2 && q1!=q2) {
2952
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2953
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2954
                    for(k = 1; k < 8; k++)
2955
                        ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2956
                }
2957
            }
2958
        }
2959

    
2960
        /* apply AC prediction if needed */
2961
        if(use_pred) {
2962
            if(dc_pred_dir) { //left
2963
                for(k = 1; k < 8; k++) {
2964
                    block[k << 3] = ac_val2[k] * scale;
2965
                    if(!v->pquantizer && block[k << 3])
2966
                        block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
2967
                }
2968
            } else { //top
2969
                for(k = 1; k < 8; k++) {
2970
                    block[k] = ac_val2[k + 8] * scale;
2971
                    if(!v->pquantizer && block[k])
2972
                        block[k] += (block[k] < 0) ? -mquant : mquant;
2973
                }
2974
            }
2975
            i = 63;
2976
        }
2977
    }
2978
    s->block_last_index[n] = i;
2979

    
2980
    return 0;
2981
}
2982

    
2983
/** Decode P block
2984
 */
2985
static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n, int mquant, int ttmb, int first_block,
2986
                              uint8_t *dst, int linesize, int skip_block, int apply_filter, int cbp_top, int cbp_left)
2987
{
2988
    MpegEncContext *s = &v->s;
2989
    GetBitContext *gb = &s->gb;
2990
    int i, j;
2991
    int subblkpat = 0;
2992
    int scale, off, idx, last, skip, value;
2993
    int ttblk = ttmb & 7;
2994
    int pat = 0;
2995

    
2996
    if(ttmb == -1) {
2997
        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)];
2998
    }
2999
    if(ttblk == TT_4X4) {
3000
        subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
3001
    }
3002
    if((ttblk != TT_8X8 && ttblk != TT_4X4) && (v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))) {
3003
        subblkpat = decode012(gb);
3004
        if(subblkpat) subblkpat ^= 3; //swap decoded pattern bits
3005
        if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) ttblk = TT_8X4;
3006
        if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) ttblk = TT_4X8;
3007
    }
3008
    scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
3009

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

    
3122
/** @} */ // Macroblock group
3123

    
3124
static const int size_table  [6] = { 0, 2, 3, 4,  5,  8 };
3125
static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3126

    
3127
/** Decode one P-frame MB (in Simple/Main profile)
3128
 */
3129
static int vc1_decode_p_mb(VC1Context *v)
3130
{
3131
    MpegEncContext *s = &v->s;
3132
    GetBitContext *gb = &s->gb;
3133
    int i, j;
3134
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3135
    int cbp; /* cbp decoding stuff */
3136
    int mqdiff, mquant; /* MB quantization */
3137
    int ttmb = v->ttfrm; /* MB Transform type */
3138

    
3139
    int mb_has_coeffs = 1; /* last_flag */
3140
    int dmv_x, dmv_y; /* Differential MV components */
3141
    int index, index1; /* LUT indexes */
3142
    int val, sign; /* temp values */
3143
    int first_block = 1;
3144
    int dst_idx, off;
3145
    int skipped, fourmv;
3146
    int block_cbp = 0, pat;
3147
    int apply_loop_filter;
3148

    
3149
    mquant = v->pq; /* Loosy initialization */
3150

    
3151
    if (v->mv_type_is_raw)
3152
        fourmv = get_bits1(gb);
3153
    else
3154
        fourmv = v->mv_type_mb_plane[mb_pos];
3155
    if (v->skip_is_raw)
3156
        skipped = get_bits1(gb);
3157
    else
3158
        skipped = v->s.mbskip_table[mb_pos];
3159

    
3160
    s->dsp.clear_blocks(s->block[0]);
3161

    
3162
    apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY);
3163
    if (!fourmv) /* 1MV mode */
3164
    {
3165
        if (!skipped)
3166
        {
3167
            GET_MVDATA(dmv_x, dmv_y);
3168

    
3169
            if (s->mb_intra) {
3170
                s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
3171
                s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
3172
            }
3173
            s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
3174
            vc1_pred_mv(s, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
3175

    
3176
            /* FIXME Set DC val for inter block ? */
3177
            if (s->mb_intra && !mb_has_coeffs)
3178
            {
3179
                GET_MQUANT();
3180
                s->ac_pred = get_bits1(gb);
3181
                cbp = 0;
3182
            }
3183
            else if (mb_has_coeffs)
3184
            {
3185
                if (s->mb_intra) s->ac_pred = get_bits1(gb);
3186
                cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3187
                GET_MQUANT();
3188
            }
3189
            else
3190
            {
3191
                mquant = v->pq;
3192
                cbp = 0;
3193
            }
3194
            s->current_picture.qscale_table[mb_pos] = mquant;
3195

    
3196
            if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3197
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
3198
                                VC1_TTMB_VLC_BITS, 2);
3199
            if(!s->mb_intra) vc1_mc_1mv(v, 0);
3200
            dst_idx = 0;
3201
            for (i=0; i<6; i++)
3202
            {
3203
                s->dc_val[0][s->block_index[i]] = 0;
3204
                dst_idx += i >> 2;
3205
                val = ((cbp >> (5 - i)) & 1);
3206
                off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3207
                v->mb_type[0][s->block_index[i]] = s->mb_intra;
3208
                if(s->mb_intra) {
3209
                    /* check if prediction blocks A and C are available */
3210
                    v->a_avail = v->c_avail = 0;
3211
                    if(i == 2 || i == 3 || !s->first_slice_line)
3212
                        v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3213
                    if(i == 1 || i == 3 || s->mb_x)
3214
                        v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3215

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

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

    
3417
    /* Should never happen */
3418
    return -1;
3419
}
3420

    
3421
/** Decode one B-frame MB (in Main profile)
3422
 */
3423
static void vc1_decode_b_mb(VC1Context *v)
3424
{
3425
    MpegEncContext *s = &v->s;
3426
    GetBitContext *gb = &s->gb;
3427
    int i, j;
3428
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3429
    int cbp = 0; /* cbp decoding stuff */
3430
    int mqdiff, mquant; /* MB quantization */
3431
    int ttmb = v->ttfrm; /* MB Transform type */
3432
    int mb_has_coeffs = 0; /* last_flag */
3433
    int index, index1; /* LUT indexes */
3434
    int val, sign; /* temp values */
3435
    int first_block = 1;
3436
    int dst_idx, off;
3437
    int skipped, direct;
3438
    int dmv_x[2], dmv_y[2];
3439
    int bmvtype = BMV_TYPE_BACKWARD;
3440

    
3441
    mquant = v->pq; /* Loosy initialization */
3442
    s->mb_intra = 0;
3443

    
3444
    if (v->dmb_is_raw)
3445
        direct = get_bits1(gb);
3446
    else
3447
        direct = v->direct_mb_plane[mb_pos];
3448
    if (v->skip_is_raw)
3449
        skipped = get_bits1(gb);
3450
    else
3451
        skipped = v->s.mbskip_table[mb_pos];
3452

    
3453
    s->dsp.clear_blocks(s->block[0]);
3454
    dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
3455
    for(i = 0; i < 6; i++) {
3456
        v->mb_type[0][s->block_index[i]] = 0;
3457
        s->dc_val[0][s->block_index[i]] = 0;
3458
    }
3459
    s->current_picture.qscale_table[mb_pos] = 0;
3460

    
3461
    if (!direct) {
3462
        if (!skipped) {
3463
            GET_MVDATA(dmv_x[0], dmv_y[0]);
3464
            dmv_x[1] = dmv_x[0];
3465
            dmv_y[1] = dmv_y[0];
3466
        }
3467
        if(skipped || !s->mb_intra) {
3468
            bmvtype = decode012(gb);
3469
            switch(bmvtype) {
3470
            case 0:
3471
                bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
3472
                break;
3473
            case 1:
3474
                bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
3475
                break;
3476
            case 2:
3477
                bmvtype = BMV_TYPE_INTERPOLATED;
3478
                dmv_x[0] = dmv_y[0] = 0;
3479
            }
3480
        }
3481
    }
3482
    for(i = 0; i < 6; i++)
3483
        v->mb_type[0][s->block_index[i]] = s->mb_intra;
3484

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

    
3554
            vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
3555
            if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3556
            s->dsp.vc1_inv_trans_8x8(s->block[i]);
3557
            if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
3558
            s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
3559
        } else if(val) {
3560
            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);
3561
            if(!v->ttmbf && ttmb < 8) ttmb = -1;
3562
            first_block = 0;
3563
        }
3564
    }
3565
}
3566

    
3567
/** Decode blocks of I-frame
3568
 */
3569
static void vc1_decode_i_blocks(VC1Context *v)
3570
{
3571
    int k, j;
3572
    MpegEncContext *s = &v->s;
3573
    int cbp, val;
3574
    uint8_t *coded_val;
3575
    int mb_pos;
3576

    
3577
    /* select codingmode used for VLC tables selection */
3578
    switch(v->y_ac_table_index){
3579
    case 0:
3580
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
3581
        break;
3582
    case 1:
3583
        v->codingset = CS_HIGH_MOT_INTRA;
3584
        break;
3585
    case 2:
3586
        v->codingset = CS_MID_RATE_INTRA;
3587
        break;
3588
    }
3589

    
3590
    switch(v->c_ac_table_index){
3591
    case 0:
3592
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
3593
        break;
3594
    case 1:
3595
        v->codingset2 = CS_HIGH_MOT_INTER;
3596
        break;
3597
    case 2:
3598
        v->codingset2 = CS_MID_RATE_INTER;
3599
        break;
3600
    }
3601

    
3602
    /* Set DC scale - y and c use the same */
3603
    s->y_dc_scale = s->y_dc_scale_table[v->pq];
3604
    s->c_dc_scale = s->c_dc_scale_table[v->pq];
3605

    
3606
    //do frame decode
3607
    s->mb_x = s->mb_y = 0;
3608
    s->mb_intra = 1;
3609
    s->first_slice_line = 1;
3610
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3611
        for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
3612
            ff_init_block_index(s);
3613
            ff_update_block_index(s);
3614
            s->dsp.clear_blocks(s->block[0]);
3615
            mb_pos = s->mb_x + s->mb_y * s->mb_width;
3616
            s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
3617
            s->current_picture.qscale_table[mb_pos] = v->pq;
3618
            s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
3619
            s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
3620

    
3621
            // do actual MB decoding and displaying
3622
            cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
3623
            v->s.ac_pred = get_bits1(&v->s.gb);
3624

    
3625
            for(k = 0; k < 6; k++) {
3626
                val = ((cbp >> (5 - k)) & 1);
3627

    
3628
                if (k < 4) {
3629
                    int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
3630
                    val = val ^ pred;
3631
                    *coded_val = val;
3632
                }
3633
                cbp |= val << (5 - k);
3634

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

    
3637
                s->dsp.vc1_inv_trans_8x8(s->block[k]);
3638
                if(v->pq >= 9 && v->overlap) {
3639
                    for(j = 0; j < 64; j++) s->block[k][j] += 128;
3640
                }
3641
            }
3642

    
3643
            vc1_put_block(v, s->block);
3644
            if(v->pq >= 9 && v->overlap) {
3645
                if(s->mb_x) {
3646
                    s->dsp.vc1_h_overlap(s->dest[0], s->linesize);
3647
                    s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
3648
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
3649
                        s->dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
3650
                        s->dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
3651
                    }
3652
                }
3653
                s->dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
3654
                s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
3655
                if(!s->first_slice_line) {
3656
                    s->dsp.vc1_v_overlap(s->dest[0], s->linesize);
3657
                    s->dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
3658
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
3659
                        s->dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
3660
                        s->dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
3661
                    }
3662
                }
3663
                s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
3664
                s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
3665
            }
3666
            if(v->s.loop_filter) vc1_loop_filter_iblk(s, s->current_picture.qscale_table[mb_pos]);
3667

    
3668
            if(get_bits_count(&s->gb) > v->bits) {
3669
                ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
3670
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
3671
                return;
3672
            }
3673
        }
3674
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3675
        s->first_slice_line = 0;
3676
    }
3677
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3678
}
3679

    
3680
/** Decode blocks of I-frame for advanced profile
3681
 */
3682
static void vc1_decode_i_blocks_adv(VC1Context *v)
3683
{
3684
    int k, j;
3685
    MpegEncContext *s = &v->s;
3686
    int cbp, val;
3687
    uint8_t *coded_val;
3688
    int mb_pos;
3689
    int mquant = v->pq;
3690
    int mqdiff;
3691
    int overlap;
3692
    GetBitContext *gb = &s->gb;
3693

    
3694
    /* select codingmode used for VLC tables selection */
3695
    switch(v->y_ac_table_index){
3696
    case 0:
3697
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
3698
        break;
3699
    case 1:
3700
        v->codingset = CS_HIGH_MOT_INTRA;
3701
        break;
3702
    case 2:
3703
        v->codingset = CS_MID_RATE_INTRA;
3704
        break;
3705
    }
3706

    
3707
    switch(v->c_ac_table_index){
3708
    case 0:
3709
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
3710
        break;
3711
    case 1:
3712
        v->codingset2 = CS_HIGH_MOT_INTER;
3713
        break;
3714
    case 2:
3715
        v->codingset2 = CS_MID_RATE_INTER;
3716
        break;
3717
    }
3718

    
3719
    //do frame decode
3720
    s->mb_x = s->mb_y = 0;
3721
    s->mb_intra = 1;
3722
    s->first_slice_line = 1;
3723
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3724
        for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
3725
            ff_init_block_index(s);
3726
            ff_update_block_index(s);
3727
            s->dsp.clear_blocks(s->block[0]);
3728
            mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3729
            s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
3730
            s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
3731
            s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
3732

    
3733
            // do actual MB decoding and displaying
3734
            cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
3735
            if(v->acpred_is_raw)
3736
                v->s.ac_pred = get_bits1(&v->s.gb);
3737
            else
3738
                v->s.ac_pred = v->acpred_plane[mb_pos];
3739

    
3740
            if(v->condover == CONDOVER_SELECT) {
3741
                if(v->overflg_is_raw)
3742
                    overlap = get_bits1(&v->s.gb);
3743
                else
3744
                    overlap = v->over_flags_plane[mb_pos];
3745
            } else
3746
                overlap = (v->condover == CONDOVER_ALL);
3747

    
3748
            GET_MQUANT();
3749

    
3750
            s->current_picture.qscale_table[mb_pos] = mquant;
3751
            /* Set DC scale - y and c use the same */
3752
            s->y_dc_scale = s->y_dc_scale_table[mquant];
3753
            s->c_dc_scale = s->c_dc_scale_table[mquant];
3754

    
3755
            for(k = 0; k < 6; k++) {
3756
                val = ((cbp >> (5 - k)) & 1);
3757

    
3758
                if (k < 4) {
3759
                    int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
3760
                    val = val ^ pred;
3761
                    *coded_val = val;
3762
                }
3763
                cbp |= val << (5 - k);
3764

    
3765
                v->a_avail = !s->first_slice_line || (k==2 || k==3);
3766
                v->c_avail = !!s->mb_x || (k==1 || k==3);
3767

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

    
3770
                s->dsp.vc1_inv_trans_8x8(s->block[k]);
3771
                for(j = 0; j < 64; j++) s->block[k][j] += 128;
3772
            }
3773

    
3774
            vc1_put_block(v, s->block);
3775
            if(overlap) {
3776
                if(s->mb_x) {
3777
                    s->dsp.vc1_h_overlap(s->dest[0], s->linesize);
3778
                    s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
3779
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
3780
                        s->dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
3781
                        s->dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
3782
                    }
3783
                }
3784
                s->dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
3785
                s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
3786
                if(!s->first_slice_line) {
3787
                    s->dsp.vc1_v_overlap(s->dest[0], s->linesize);
3788
                    s->dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
3789
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
3790
                        s->dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
3791
                        s->dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
3792
                    }
3793
                }
3794
                s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
3795
                s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
3796
            }
3797
            if(v->s.loop_filter) vc1_loop_filter_iblk(s, s->current_picture.qscale_table[mb_pos]);
3798

    
3799
            if(get_bits_count(&s->gb) > v->bits) {
3800
                ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
3801
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
3802
                return;
3803
            }
3804
        }
3805
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3806
        s->first_slice_line = 0;
3807
    }
3808
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3809
}
3810

    
3811
static void vc1_decode_p_blocks(VC1Context *v)
3812
{
3813
    MpegEncContext *s = &v->s;
3814

    
3815
    /* select codingmode used for VLC tables selection */
3816
    switch(v->c_ac_table_index){
3817
    case 0:
3818
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
3819
        break;
3820
    case 1:
3821
        v->codingset = CS_HIGH_MOT_INTRA;
3822
        break;
3823
    case 2:
3824
        v->codingset = CS_MID_RATE_INTRA;
3825
        break;
3826
    }
3827

    
3828
    switch(v->c_ac_table_index){
3829
    case 0:
3830
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
3831
        break;
3832
    case 1:
3833
        v->codingset2 = CS_HIGH_MOT_INTER;
3834
        break;
3835
    case 2:
3836
        v->codingset2 = CS_MID_RATE_INTER;
3837
        break;
3838
    }
3839

    
3840
    s->first_slice_line = 1;
3841
    memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
3842
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3843
        for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
3844
            ff_init_block_index(s);
3845
            ff_update_block_index(s);
3846
            s->dsp.clear_blocks(s->block[0]);
3847

    
3848
            vc1_decode_p_mb(v);
3849
            if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
3850
                ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
3851
                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);
3852
                return;
3853
            }
3854
        }
3855
        memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0])*s->mb_stride);
3856
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3857
        s->first_slice_line = 0;
3858
    }
3859
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3860
}
3861

    
3862
static void vc1_decode_b_blocks(VC1Context *v)
3863
{
3864
    MpegEncContext *s = &v->s;
3865

    
3866
    /* select codingmode used for VLC tables selection */
3867
    switch(v->c_ac_table_index){
3868
    case 0:
3869
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
3870
        break;
3871
    case 1:
3872
        v->codingset = CS_HIGH_MOT_INTRA;
3873
        break;
3874
    case 2:
3875
        v->codingset = CS_MID_RATE_INTRA;
3876
        break;
3877
    }
3878

    
3879
    switch(v->c_ac_table_index){
3880
    case 0:
3881
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
3882
        break;
3883
    case 1:
3884
        v->codingset2 = CS_HIGH_MOT_INTER;
3885
        break;
3886
    case 2:
3887
        v->codingset2 = CS_MID_RATE_INTER;
3888
        break;
3889
    }
3890

    
3891
    s->first_slice_line = 1;
3892
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3893
        for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
3894
            ff_init_block_index(s);
3895
            ff_update_block_index(s);
3896
            s->dsp.clear_blocks(s->block[0]);
3897

    
3898
            vc1_decode_b_mb(v);
3899
            if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
3900
                ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
3901
                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);
3902
                return;
3903
            }
3904
            if(v->s.loop_filter) vc1_loop_filter_iblk(s, s->current_picture.qscale_table[s->mb_x + s->mb_y *s->mb_stride]);
3905
        }
3906
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3907
        s->first_slice_line = 0;
3908
    }
3909
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3910
}
3911

    
3912
static void vc1_decode_skip_blocks(VC1Context *v)
3913
{
3914
    MpegEncContext *s = &v->s;
3915

    
3916
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3917
    s->first_slice_line = 1;
3918
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3919
        s->mb_x = 0;
3920
        ff_init_block_index(s);
3921
        ff_update_block_index(s);
3922
        memcpy(s->dest[0], s->last_picture.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
3923
        memcpy(s->dest[1], s->last_picture.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
3924
        memcpy(s->dest[2], s->last_picture.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
3925
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3926
        s->first_slice_line = 0;
3927
    }
3928
    s->pict_type = FF_P_TYPE;
3929
}
3930

    
3931
static void vc1_decode_blocks(VC1Context *v)
3932
{
3933

    
3934
    v->s.esc3_level_length = 0;
3935
    if(v->x8_type){
3936
        ff_intrax8_decode_picture(&v->x8, 2*v->pq+v->halfpq, v->pq*(!v->pquantizer) );
3937
    }else{
3938

    
3939
        switch(v->s.pict_type) {
3940
        case FF_I_TYPE:
3941
            if(v->profile == PROFILE_ADVANCED)
3942
                vc1_decode_i_blocks_adv(v);
3943
            else
3944
                vc1_decode_i_blocks(v);
3945
            break;
3946
        case FF_P_TYPE:
3947
            if(v->p_frame_skipped)
3948
                vc1_decode_skip_blocks(v);
3949
            else
3950
                vc1_decode_p_blocks(v);
3951
            break;
3952
        case FF_B_TYPE:
3953
            if(v->bi_type){
3954
                if(v->profile == PROFILE_ADVANCED)
3955
                    vc1_decode_i_blocks_adv(v);
3956
                else
3957
                    vc1_decode_i_blocks(v);
3958
            }else
3959
                vc1_decode_b_blocks(v);
3960
            break;
3961
        }
3962
    }
3963
}
3964

    
3965
/** Find VC-1 marker in buffer
3966
 * @return position where next marker starts or end of buffer if no marker found
3967
 */
3968
static av_always_inline const uint8_t* find_next_marker(const uint8_t *src, const uint8_t *end)
3969
{
3970
    uint32_t mrk = 0xFFFFFFFF;
3971

    
3972
    if(end-src < 4) return end;
3973
    while(src < end){
3974
        mrk = (mrk << 8) | *src++;
3975
        if(IS_MARKER(mrk))
3976
            return src-4;
3977
    }
3978
    return end;
3979
}
3980

    
3981
static av_always_inline int vc1_unescape_buffer(const uint8_t *src, int size, uint8_t *dst)
3982
{
3983
    int dsize = 0, i;
3984

    
3985
    if(size < 4){
3986
        for(dsize = 0; dsize < size; dsize++) *dst++ = *src++;
3987
        return size;
3988
    }
3989
    for(i = 0; i < size; i++, src++) {
3990
        if(src[0] == 3 && i >= 2 && !src[-1] && !src[-2] && i < size-1 && src[1] < 4) {
3991
            dst[dsize++] = src[1];
3992
            src++;
3993
            i++;
3994
        } else
3995
            dst[dsize++] = *src;
3996
    }
3997
    return dsize;
3998
}
3999

    
4000
/** Initialize a VC1/WMV3 decoder
4001
 * @todo TODO: Handle VC-1 IDUs (Transport level?)
4002
 * @todo TODO: Decypher remaining bits in extra_data
4003
 */
4004
static av_cold int vc1_decode_init(AVCodecContext *avctx)
4005
{
4006
    VC1Context *v = avctx->priv_data;
4007
    MpegEncContext *s = &v->s;
4008
    GetBitContext gb;
4009

    
4010
    if (!avctx->extradata_size || !avctx->extradata) return -1;
4011
    if (!(avctx->flags & CODEC_FLAG_GRAY))
4012
        avctx->pix_fmt = PIX_FMT_YUV420P;
4013
    else
4014
        avctx->pix_fmt = PIX_FMT_GRAY8;
4015
    v->s.avctx = avctx;
4016
    avctx->flags |= CODEC_FLAG_EMU_EDGE;
4017
    v->s.flags |= CODEC_FLAG_EMU_EDGE;
4018

    
4019
    if(avctx->idct_algo==FF_IDCT_AUTO){
4020
        avctx->idct_algo=FF_IDCT_WMV2;
4021
    }
4022

    
4023
    if(ff_h263_decode_init(avctx) < 0)
4024
        return -1;
4025
    if (vc1_init_common(v) < 0) return -1;
4026

    
4027
    avctx->coded_width = avctx->width;
4028
    avctx->coded_height = avctx->height;
4029
    if (avctx->codec_id == CODEC_ID_WMV3)
4030
    {
4031
        int count = 0;
4032

    
4033
        // looks like WMV3 has a sequence header stored in the extradata
4034
        // advanced sequence header may be before the first frame
4035
        // the last byte of the extradata is a version number, 1 for the
4036
        // samples we can decode
4037

    
4038
        init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
4039

    
4040
        if (decode_sequence_header(avctx, &gb) < 0)
4041
          return -1;
4042

    
4043
        count = avctx->extradata_size*8 - get_bits_count(&gb);
4044
        if (count>0)
4045
        {
4046
            av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
4047
                   count, get_bits(&gb, count));
4048
        }
4049
        else if (count < 0)
4050
        {
4051
            av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
4052
        }
4053
    } else { // VC1/WVC1
4054
        const uint8_t *start = avctx->extradata;
4055
        uint8_t *end = avctx->extradata + avctx->extradata_size;
4056
        const uint8_t *next;
4057
        int size, buf2_size;
4058
        uint8_t *buf2 = NULL;
4059
        int seq_initialized = 0, ep_initialized = 0;
4060

    
4061
        if(avctx->extradata_size < 16) {
4062
            av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
4063
            return -1;
4064
        }
4065

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

    
4101
    s->mb_width = (avctx->coded_width+15)>>4;
4102
    s->mb_height = (avctx->coded_height+15)>>4;
4103

    
4104
    /* Allocate mb bitplanes */
4105
    v->mv_type_mb_plane = av_malloc(s->mb_stride * s->mb_height);
4106
    v->direct_mb_plane = av_malloc(s->mb_stride * s->mb_height);
4107
    v->acpred_plane = av_malloc(s->mb_stride * s->mb_height);
4108
    v->over_flags_plane = av_malloc(s->mb_stride * s->mb_height);
4109

    
4110
    v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
4111
    v->cbp = v->cbp_base + s->mb_stride;
4112

    
4113
    /* allocate block type info in that way so it could be used with s->block_index[] */
4114
    v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
4115
    v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
4116
    v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
4117
    v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
4118

    
4119
    /* Init coded blocks info */
4120
    if (v->profile == PROFILE_ADVANCED)
4121
    {
4122
//        if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
4123
//            return -1;
4124
//        if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
4125
//            return -1;
4126
    }
4127

    
4128
    ff_intrax8_common_init(&v->x8,s);
4129
    return 0;
4130
}
4131

    
4132

    
4133
/** Decode a VC1/WMV3 frame
4134
 * @todo TODO: Handle VC-1 IDUs (Transport level?)
4135
 */
4136
static int vc1_decode_frame(AVCodecContext *avctx,
4137
                            void *data, int *data_size,
4138
                            const uint8_t *buf, int buf_size)
4139
{
4140
    VC1Context *v = avctx->priv_data;
4141
    MpegEncContext *s = &v->s;
4142
    AVFrame *pict = data;
4143
    uint8_t *buf2 = NULL;
4144
    const uint8_t *buf_vdpau = buf;
4145

    
4146
    /* no supplementary picture */
4147
    if (buf_size == 0) {
4148
        /* special case for last picture */
4149
        if (s->low_delay==0 && s->next_picture_ptr) {
4150
            *pict= *(AVFrame*)s->next_picture_ptr;
4151
            s->next_picture_ptr= NULL;
4152

    
4153
            *data_size = sizeof(AVFrame);
4154
        }
4155

    
4156
        return 0;
4157
    }
4158

    
4159
    /* We need to set current_picture_ptr before reading the header,
4160
     * otherwise we cannot store anything in there. */
4161
    if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
4162
        int i= ff_find_unused_picture(s, 0);
4163
        s->current_picture_ptr= &s->picture[i];
4164
    }
4165

    
4166
    if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
4167
        if (v->profile < PROFILE_ADVANCED)
4168
            avctx->pix_fmt = PIX_FMT_VDPAU_WMV3;
4169
        else
4170
            avctx->pix_fmt = PIX_FMT_VDPAU_VC1;
4171
    }
4172

    
4173
    //for advanced profile we may need to parse and unescape data
4174
    if (avctx->codec_id == CODEC_ID_VC1) {
4175
        int buf_size2 = 0;
4176
        buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
4177

    
4178
        if(IS_MARKER(AV_RB32(buf))){ /* frame starts with marker and needs to be parsed */
4179
            const uint8_t *start, *end, *next;
4180
            int size;
4181

    
4182
            next = buf;
4183
            for(start = buf, end = buf + buf_size; next < end; start = next){
4184
                next = find_next_marker(start + 4, end);
4185
                size = next - start - 4;
4186
                if(size <= 0) continue;
4187
                switch(AV_RB32(start)){
4188
                case VC1_CODE_FRAME:
4189
                    if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
4190
                        buf_vdpau = start;
4191
                    buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
4192
                    break;
4193
                case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
4194
                    buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
4195
                    init_get_bits(&s->gb, buf2, buf_size2*8);
4196
                    decode_entry_point(avctx, &s->gb);
4197
                    break;
4198
                case VC1_CODE_SLICE:
4199
                    av_log(avctx, AV_LOG_ERROR, "Sliced decoding is not implemented (yet)\n");
4200
                    av_free(buf2);
4201
                    return -1;
4202
                }
4203
            }
4204
        }else if(v->interlace && ((buf[0] & 0xC0) == 0xC0)){ /* WVC1 interlaced stores both fields divided by marker */
4205
            const uint8_t *divider;
4206

    
4207
            divider = find_next_marker(buf, buf + buf_size);
4208
            if((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD){
4209
                av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
4210
                av_free(buf2);
4211
                return -1;
4212
            }
4213

    
4214
            buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
4215
            // TODO
4216
            av_free(buf2);return -1;
4217
        }else{
4218
            buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
4219
        }
4220
        init_get_bits(&s->gb, buf2, buf_size2*8);
4221
    } else
4222
        init_get_bits(&s->gb, buf, buf_size*8);
4223
    // do parse frame header
4224
    if(v->profile < PROFILE_ADVANCED) {
4225
        if(vc1_parse_frame_header(v, &s->gb) == -1) {
4226
            av_free(buf2);
4227
            return -1;
4228
        }
4229
    } else {
4230
        if(vc1_parse_frame_header_adv(v, &s->gb) == -1) {
4231
            av_free(buf2);
4232
            return -1;
4233
        }
4234
    }
4235

    
4236
    if(s->pict_type != FF_I_TYPE && !v->res_rtm_flag){
4237
        av_free(buf2);
4238
        return -1;
4239
    }
4240

    
4241
    // for hurry_up==5
4242
    s->current_picture.pict_type= s->pict_type;
4243
    s->current_picture.key_frame= s->pict_type == FF_I_TYPE;
4244

    
4245
    /* skip B-frames if we don't have reference frames */
4246
    if(s->last_picture_ptr==NULL && (s->pict_type==FF_B_TYPE || s->dropable)){
4247
        av_free(buf2);
4248
        return -1;//buf_size;
4249
    }
4250
    /* skip b frames if we are in a hurry */
4251
    if(avctx->hurry_up && s->pict_type==FF_B_TYPE) return -1;//buf_size;
4252
    if(   (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==FF_B_TYPE)
4253
       || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=FF_I_TYPE)
4254
       ||  avctx->skip_frame >= AVDISCARD_ALL) {
4255
        av_free(buf2);
4256
        return buf_size;
4257
    }
4258
    /* skip everything if we are in a hurry>=5 */
4259
    if(avctx->hurry_up>=5) {
4260
        av_free(buf2);
4261
        return -1;//buf_size;
4262
    }
4263

    
4264
    if(s->next_p_frame_damaged){
4265
        if(s->pict_type==FF_B_TYPE)
4266
            return buf_size;
4267
        else
4268
            s->next_p_frame_damaged=0;
4269
    }
4270

    
4271
    if(MPV_frame_start(s, avctx) < 0) {
4272
        av_free(buf2);
4273
        return -1;
4274
    }
4275

    
4276
    s->me.qpel_put= s->dsp.put_qpel_pixels_tab;
4277
    s->me.qpel_avg= s->dsp.avg_qpel_pixels_tab;
4278

    
4279
    if ((CONFIG_VC1_VDPAU_DECODER || CONFIG_WMV3_VDPAU_DECODER)
4280
        &&s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
4281
        ff_vdpau_vc1_decode_picture(s, buf_vdpau, (buf + buf_size) - buf_vdpau);
4282
    else {
4283
        ff_er_frame_start(s);
4284

    
4285
        v->bits = buf_size * 8;
4286
        vc1_decode_blocks(v);
4287
//av_log(s->avctx, AV_LOG_INFO, "Consumed %i/%i bits\n", get_bits_count(&s->gb), buf_size*8);
4288
//  if(get_bits_count(&s->gb) > buf_size * 8)
4289
//      return -1;
4290
        ff_er_frame_end(s);
4291
    }
4292

    
4293
    MPV_frame_end(s);
4294

    
4295
assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
4296
assert(s->current_picture.pict_type == s->pict_type);
4297
    if (s->pict_type == FF_B_TYPE || s->low_delay) {
4298
        *pict= *(AVFrame*)s->current_picture_ptr;
4299
    } else if (s->last_picture_ptr != NULL) {
4300
        *pict= *(AVFrame*)s->last_picture_ptr;
4301
    }
4302

    
4303
    if(s->last_picture_ptr || s->low_delay){
4304
        *data_size = sizeof(AVFrame);
4305
        ff_print_debug_info(s, pict);
4306
    }
4307

    
4308
    /* Return the Picture timestamp as the frame number */
4309
    /* we subtract 1 because it is added on utils.c     */
4310
    avctx->frame_number = s->picture_number - 1;
4311

    
4312
    av_free(buf2);
4313
    return buf_size;
4314
}
4315

    
4316

    
4317
/** Close a VC1/WMV3 decoder
4318
 * @warning Initial try at using MpegEncContext stuff
4319
 */
4320
static av_cold int vc1_decode_end(AVCodecContext *avctx)
4321
{
4322
    VC1Context *v = avctx->priv_data;
4323

    
4324
    av_freep(&v->hrd_rate);
4325
    av_freep(&v->hrd_buffer);
4326
    MPV_common_end(&v->s);
4327
    av_freep(&v->mv_type_mb_plane);
4328
    av_freep(&v->direct_mb_plane);
4329
    av_freep(&v->acpred_plane);
4330
    av_freep(&v->over_flags_plane);
4331
    av_freep(&v->mb_type_base);
4332
    av_freep(&v->cbp_base);
4333
    ff_intrax8_common_end(&v->x8);
4334
    return 0;
4335
}
4336

    
4337

    
4338
AVCodec vc1_decoder = {
4339
    "vc1",
4340
    CODEC_TYPE_VIDEO,
4341
    CODEC_ID_VC1,
4342
    sizeof(VC1Context),
4343
    vc1_decode_init,
4344
    NULL,
4345
    vc1_decode_end,
4346
    vc1_decode_frame,
4347
    CODEC_CAP_DELAY,
4348
    NULL,
4349
    .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
4350
};
4351

    
4352
AVCodec wmv3_decoder = {
4353
    "wmv3",
4354
    CODEC_TYPE_VIDEO,
4355
    CODEC_ID_WMV3,
4356
    sizeof(VC1Context),
4357
    vc1_decode_init,
4358
    NULL,
4359
    vc1_decode_end,
4360
    vc1_decode_frame,
4361
    CODEC_CAP_DELAY,
4362
    NULL,
4363
    .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
4364
};
4365

    
4366
#if CONFIG_WMV3_VDPAU_DECODER
4367
AVCodec wmv3_vdpau_decoder = {
4368
    "wmv3_vdpau",
4369
    CODEC_TYPE_VIDEO,
4370
    CODEC_ID_WMV3,
4371
    sizeof(VC1Context),
4372
    vc1_decode_init,
4373
    NULL,
4374
    vc1_decode_end,
4375
    vc1_decode_frame,
4376
    CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
4377
    NULL,
4378
    .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
4379
};
4380
#endif
4381

    
4382
#if CONFIG_VC1_VDPAU_DECODER
4383
AVCodec vc1_vdpau_decoder = {
4384
    "vc1_vdpau",
4385
    CODEC_TYPE_VIDEO,
4386
    CODEC_ID_VC1,
4387
    sizeof(VC1Context),
4388
    vc1_decode_init,
4389
    NULL,
4390
    vc1_decode_end,
4391
    vc1_decode_frame,
4392
    CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
4393
    NULL,
4394
    .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
4395
};
4396
#endif