Statistics
| Branch: | Revision:

ffmpeg / libavcodec / vc1.c @ 3c2ddb59

History | View | Annotate | Download (143 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

    
36
#undef NDEBUG
37
#include <assert.h>
38

    
39
#define MB_INTRA_VLC_BITS 9
40
#define DC_VLC_BITS 9
41
#define AC_VLC_BITS 9
42
static const uint16_t table_mb_intra[64][2];
43

    
44

    
45
static inline int decode210(GetBitContext *gb){
46
    if (get_bits1(gb))
47
        return 0;
48
    else
49
        return 2 - get_bits1(gb);
50
}
51

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

    
62
    v->hrd_rate = v->hrd_buffer = NULL;
63

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

    
113
    /* Other defaults */
114
    v->pq = -1;
115
    v->mvrange = 0; /* 7.1.1.18, p80 */
116

    
117
    return 0;
118
}
119

    
120
/***********************************************************************/
121
/**
122
 * @defgroup bitplane VC9 Bitplane decoding
123
 * @see 8.7, p56
124
 * @{
125
 */
126

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

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

    
151
    for (y=0; y<height; y++){
152
        if (!get_bits(gb, 1)) //rowskip
153
            memset(plane, 0, width);
154
        else
155
            for (x=0; x<width; x++)
156
                plane[x] = get_bits(gb, 1);
157
        plane += stride;
158
    }
159
}
160

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

    
171
    for (x=0; x<width; x++){
172
        if (!get_bits(gb, 1)) //colskip
173
            for (y=0; y<height; y++)
174
                plane[y*stride] = 0;
175
        else
176
            for (y=0; y<height; y++)
177
                plane[y*stride] = get_bits(gb, 1);
178
        plane ++;
179
    }
180
}
181

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

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

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

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

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

    
312
/** @} */ //Bitplane group
313

    
314
/***********************************************************************/
315
/** VOP Dquant decoding
316
 * @param v VC-1 Context
317
 */
318
static int vop_dquant_decoding(VC1Context *v)
319
{
320
    GetBitContext *gb = &v->s.gb;
321
    int pqdiff;
322

    
323
    //variable size
324
    if (v->dquant == 2)
325
    {
326
        pqdiff = get_bits(gb, 3);
327
        if (pqdiff == 7) v->altpq = get_bits(gb, 5);
328
        else v->altpq = v->pq + pqdiff + 1;
329
    }
330
    else
331
    {
332
        v->dquantfrm = get_bits(gb, 1);
333
        if ( v->dquantfrm )
334
        {
335
            v->dqprofile = get_bits(gb, 2);
336
            switch (v->dqprofile)
337
            {
338
            case DQPROFILE_SINGLE_EDGE:
339
            case DQPROFILE_DOUBLE_EDGES:
340
                v->dqsbedge = get_bits(gb, 2);
341
                break;
342
            case DQPROFILE_ALL_MBS:
343
                v->dqbilevel = get_bits(gb, 1);
344
            default: break; //Forbidden ?
345
            }
346
            if (v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS)
347
            {
348
                pqdiff = get_bits(gb, 3);
349
                if (pqdiff == 7) v->altpq = get_bits(gb, 5);
350
                else v->altpq = v->pq + pqdiff + 1;
351
            }
352
        }
353
    }
354
    return 0;
355
}
356

    
357
/** Put block onto picture
358
 */
359
static void vc1_put_block(VC1Context *v, DCTELEM block[6][64])
360
{
361
    uint8_t *Y;
362
    int ys, us, vs;
363
    DSPContext *dsp = &v->s.dsp;
364

    
365
    if(v->rangeredfrm) {
366
        int i, j, k;
367
        for(k = 0; k < 6; k++)
368
            for(j = 0; j < 8; j++)
369
                for(i = 0; i < 8; i++)
370
                    block[k][i + j*8] = ((block[k][i + j*8] - 128) << 1) + 128;
371

    
372
    }
373
    ys = v->s.current_picture.linesize[0];
374
    us = v->s.current_picture.linesize[1];
375
    vs = v->s.current_picture.linesize[2];
376
    Y = v->s.dest[0];
377

    
378
    dsp->put_pixels_clamped(block[0], Y, ys);
379
    dsp->put_pixels_clamped(block[1], Y + 8, ys);
380
    Y += ys * 8;
381
    dsp->put_pixels_clamped(block[2], Y, ys);
382
    dsp->put_pixels_clamped(block[3], Y + 8, ys);
383

    
384
    if(!(v->s.flags & CODEC_FLAG_GRAY)) {
385
        dsp->put_pixels_clamped(block[4], v->s.dest[1], us);
386
        dsp->put_pixels_clamped(block[5], v->s.dest[2], vs);
387
    }
388
}
389

    
390
/** Do motion compensation over 1 macroblock
391
 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
392
 */
393
static void vc1_mc_1mv(VC1Context *v, int dir)
394
{
395
    MpegEncContext *s = &v->s;
396
    DSPContext *dsp = &v->s.dsp;
397
    uint8_t *srcY, *srcU, *srcV;
398
    int dxy, uvdxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
399

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

    
402
    mx = s->mv[dir][0][0];
403
    my = s->mv[dir][0][1];
404

    
405
    // store motion vectors for further use in B frames
406
    if(s->pict_type == P_TYPE) {
407
        s->current_picture.motion_val[1][s->block_index[0]][0] = mx;
408
        s->current_picture.motion_val[1][s->block_index[0]][1] = my;
409
    }
410
    uvmx = (mx + ((mx & 3) == 3)) >> 1;
411
    uvmy = (my + ((my & 3) == 3)) >> 1;
412
    if(v->fastuvmc) {
413
        uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
414
        uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
415
    }
416
    if(!dir) {
417
        srcY = s->last_picture.data[0];
418
        srcU = s->last_picture.data[1];
419
        srcV = s->last_picture.data[2];
420
    } else {
421
        srcY = s->next_picture.data[0];
422
        srcU = s->next_picture.data[1];
423
        srcV = s->next_picture.data[2];
424
    }
425

    
426
    src_x = s->mb_x * 16 + (mx >> 2);
427
    src_y = s->mb_y * 16 + (my >> 2);
428
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
429
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
430

    
431
    if(v->profile != PROFILE_ADVANCED){
432
        src_x   = av_clip(  src_x, -16, s->mb_width  * 16);
433
        src_y   = av_clip(  src_y, -16, s->mb_height * 16);
434
        uvsrc_x = av_clip(uvsrc_x,  -8, s->mb_width  *  8);
435
        uvsrc_y = av_clip(uvsrc_y,  -8, s->mb_height *  8);
436
    }else{
437
        src_x   = av_clip(  src_x, -17, s->avctx->coded_width);
438
        src_y   = av_clip(  src_y, -18, s->avctx->coded_height + 1);
439
        uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
440
        uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
441
    }
442

    
443
    srcY += src_y * s->linesize + src_x;
444
    srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
445
    srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
446

    
447
    /* for grayscale we should not try to read from unknown area */
448
    if(s->flags & CODEC_FLAG_GRAY) {
449
        srcU = s->edge_emu_buffer + 18 * s->linesize;
450
        srcV = s->edge_emu_buffer + 18 * s->linesize;
451
    }
452

    
453
    if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
454
       || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3
455
       || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){
456
        uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
457

    
458
        srcY -= s->mspel * (1 + s->linesize);
459
        ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
460
                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
461
        srcY = s->edge_emu_buffer;
462
        ff_emulated_edge_mc(uvbuf     , srcU, s->uvlinesize, 8+1, 8+1,
463
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
464
        ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
465
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
466
        srcU = uvbuf;
467
        srcV = uvbuf + 16;
468
        /* if we deal with range reduction we need to scale source blocks */
469
        if(v->rangeredfrm) {
470
            int i, j;
471
            uint8_t *src, *src2;
472

    
473
            src = srcY;
474
            for(j = 0; j < 17 + s->mspel*2; j++) {
475
                for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
476
                src += s->linesize;
477
            }
478
            src = srcU; src2 = srcV;
479
            for(j = 0; j < 9; j++) {
480
                for(i = 0; i < 9; i++) {
481
                    src[i] = ((src[i] - 128) >> 1) + 128;
482
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
483
                }
484
                src += s->uvlinesize;
485
                src2 += s->uvlinesize;
486
            }
487
        }
488
        /* if we deal with intensity compensation we need to scale source blocks */
489
        if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
490
            int i, j;
491
            uint8_t *src, *src2;
492

    
493
            src = srcY;
494
            for(j = 0; j < 17 + s->mspel*2; j++) {
495
                for(i = 0; i < 17 + s->mspel*2; i++) src[i] = v->luty[src[i]];
496
                src += s->linesize;
497
            }
498
            src = srcU; src2 = srcV;
499
            for(j = 0; j < 9; j++) {
500
                for(i = 0; i < 9; i++) {
501
                    src[i] = v->lutuv[src[i]];
502
                    src2[i] = v->lutuv[src2[i]];
503
                }
504
                src += s->uvlinesize;
505
                src2 += s->uvlinesize;
506
            }
507
        }
508
        srcY += s->mspel * (1 + s->linesize);
509
    }
510

    
511
    if(s->mspel) {
512
        dxy = ((my & 3) << 2) | (mx & 3);
513
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0]    , srcY    , s->linesize, v->rnd);
514
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
515
        srcY += s->linesize * 8;
516
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize    , srcY    , s->linesize, v->rnd);
517
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
518
    } else { // hpel mc - always used for luma
519
        dxy = (my & 2) | ((mx & 2) >> 1);
520

    
521
        if(!v->rnd)
522
            dsp->put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
523
        else
524
            dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
525
    }
526

    
527
    if(s->flags & CODEC_FLAG_GRAY) return;
528
    /* Chroma MC always uses qpel bilinear */
529
    uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
530
    uvmx = (uvmx&3)<<1;
531
    uvmy = (uvmy&3)<<1;
532
    if(!v->rnd){
533
        dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
534
        dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
535
    }else{
536
        dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
537
        dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
538
    }
539
}
540

    
541
/** Do motion compensation for 4-MV macroblock - luminance block
542
 */
543
static void vc1_mc_4mv_luma(VC1Context *v, int n)
544
{
545
    MpegEncContext *s = &v->s;
546
    DSPContext *dsp = &v->s.dsp;
547
    uint8_t *srcY;
548
    int dxy, mx, my, src_x, src_y;
549
    int off;
550

    
551
    if(!v->s.last_picture.data[0])return;
552
    mx = s->mv[0][n][0];
553
    my = s->mv[0][n][1];
554
    srcY = s->last_picture.data[0];
555

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

    
558
    src_x = s->mb_x * 16 + (n&1) * 8 + (mx >> 2);
559
    src_y = s->mb_y * 16 + (n&2) * 4 + (my >> 2);
560

    
561
    if(v->profile != PROFILE_ADVANCED){
562
        src_x   = av_clip(  src_x, -16, s->mb_width  * 16);
563
        src_y   = av_clip(  src_y, -16, s->mb_height * 16);
564
    }else{
565
        src_x   = av_clip(  src_x, -17, s->avctx->coded_width);
566
        src_y   = av_clip(  src_y, -18, s->avctx->coded_height + 1);
567
    }
568

    
569
    srcY += src_y * s->linesize + src_x;
570

    
571
    if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
572
       || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 8 - s->mspel*2
573
       || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 8 - s->mspel*2){
574
        srcY -= s->mspel * (1 + s->linesize);
575
        ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 9+s->mspel*2, 9+s->mspel*2,
576
                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
577
        srcY = s->edge_emu_buffer;
578
        /* if we deal with range reduction we need to scale source blocks */
579
        if(v->rangeredfrm) {
580
            int i, j;
581
            uint8_t *src;
582

    
583
            src = srcY;
584
            for(j = 0; j < 9 + s->mspel*2; j++) {
585
                for(i = 0; i < 9 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
586
                src += s->linesize;
587
            }
588
        }
589
        /* if we deal with intensity compensation we need to scale source blocks */
590
        if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
591
            int i, j;
592
            uint8_t *src;
593

    
594
            src = srcY;
595
            for(j = 0; j < 9 + s->mspel*2; j++) {
596
                for(i = 0; i < 9 + s->mspel*2; i++) src[i] = v->luty[src[i]];
597
                src += s->linesize;
598
            }
599
        }
600
        srcY += s->mspel * (1 + s->linesize);
601
    }
602

    
603
    if(s->mspel) {
604
        dxy = ((my & 3) << 2) | (mx & 3);
605
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, v->rnd);
606
    } else { // hpel mc - always used for luma
607
        dxy = (my & 2) | ((mx & 2) >> 1);
608
        if(!v->rnd)
609
            dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
610
        else
611
            dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
612
    }
613
}
614

    
615
static inline int median4(int a, int b, int c, int d)
616
{
617
    if(a < b) {
618
        if(c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
619
        else      return (FFMIN(b, c) + FFMAX(a, d)) / 2;
620
    } else {
621
        if(c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
622
        else      return (FFMIN(a, c) + FFMAX(b, d)) / 2;
623
    }
624
}
625

    
626

    
627
/** Do motion compensation for 4-MV macroblock - both chroma blocks
628
 */
629
static void vc1_mc_4mv_chroma(VC1Context *v)
630
{
631
    MpegEncContext *s = &v->s;
632
    DSPContext *dsp = &v->s.dsp;
633
    uint8_t *srcU, *srcV;
634
    int uvdxy, uvmx, uvmy, uvsrc_x, uvsrc_y;
635
    int i, idx, tx = 0, ty = 0;
636
    int mvx[4], mvy[4], intra[4];
637
    static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
638

    
639
    if(!v->s.last_picture.data[0])return;
640
    if(s->flags & CODEC_FLAG_GRAY) return;
641

    
642
    for(i = 0; i < 4; i++) {
643
        mvx[i] = s->mv[0][i][0];
644
        mvy[i] = s->mv[0][i][1];
645
        intra[i] = v->mb_type[0][s->block_index[i]];
646
    }
647

    
648
    /* calculate chroma MV vector from four luma MVs */
649
    idx = (intra[3] << 3) | (intra[2] << 2) | (intra[1] << 1) | intra[0];
650
    if(!idx) { // all blocks are inter
651
        tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
652
        ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
653
    } else if(count[idx] == 1) { // 3 inter blocks
654
        switch(idx) {
655
        case 0x1:
656
            tx = mid_pred(mvx[1], mvx[2], mvx[3]);
657
            ty = mid_pred(mvy[1], mvy[2], mvy[3]);
658
            break;
659
        case 0x2:
660
            tx = mid_pred(mvx[0], mvx[2], mvx[3]);
661
            ty = mid_pred(mvy[0], mvy[2], mvy[3]);
662
            break;
663
        case 0x4:
664
            tx = mid_pred(mvx[0], mvx[1], mvx[3]);
665
            ty = mid_pred(mvy[0], mvy[1], mvy[3]);
666
            break;
667
        case 0x8:
668
            tx = mid_pred(mvx[0], mvx[1], mvx[2]);
669
            ty = mid_pred(mvy[0], mvy[1], mvy[2]);
670
            break;
671
        }
672
    } else if(count[idx] == 2) {
673
        int t1 = 0, t2 = 0;
674
        for(i=0; i<3;i++) if(!intra[i]) {t1 = i; break;}
675
        for(i= t1+1; i<4; i++)if(!intra[i]) {t2 = i; break;}
676
        tx = (mvx[t1] + mvx[t2]) / 2;
677
        ty = (mvy[t1] + mvy[t2]) / 2;
678
    } else {
679
        s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
680
        s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
681
        return; //no need to do MC for inter blocks
682
    }
683

    
684
    s->current_picture.motion_val[1][s->block_index[0]][0] = tx;
685
    s->current_picture.motion_val[1][s->block_index[0]][1] = ty;
686
    uvmx = (tx + ((tx&3) == 3)) >> 1;
687
    uvmy = (ty + ((ty&3) == 3)) >> 1;
688
    if(v->fastuvmc) {
689
        uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
690
        uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
691
    }
692

    
693
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
694
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
695

    
696
    if(v->profile != PROFILE_ADVANCED){
697
        uvsrc_x = av_clip(uvsrc_x,  -8, s->mb_width  *  8);
698
        uvsrc_y = av_clip(uvsrc_y,  -8, s->mb_height *  8);
699
    }else{
700
        uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
701
        uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
702
    }
703

    
704
    srcU = s->last_picture.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
705
    srcV = s->last_picture.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
706
    if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
707
       || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
708
       || (unsigned)uvsrc_y > (s->v_edge_pos >> 1) - 9){
709
        ff_emulated_edge_mc(s->edge_emu_buffer     , srcU, s->uvlinesize, 8+1, 8+1,
710
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
711
        ff_emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize, 8+1, 8+1,
712
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
713
        srcU = s->edge_emu_buffer;
714
        srcV = s->edge_emu_buffer + 16;
715

    
716
        /* if we deal with range reduction we need to scale source blocks */
717
        if(v->rangeredfrm) {
718
            int i, j;
719
            uint8_t *src, *src2;
720

    
721
            src = srcU; src2 = srcV;
722
            for(j = 0; j < 9; j++) {
723
                for(i = 0; i < 9; i++) {
724
                    src[i] = ((src[i] - 128) >> 1) + 128;
725
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
726
                }
727
                src += s->uvlinesize;
728
                src2 += s->uvlinesize;
729
            }
730
        }
731
        /* if we deal with intensity compensation we need to scale source blocks */
732
        if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
733
            int i, j;
734
            uint8_t *src, *src2;
735

    
736
            src = srcU; src2 = srcV;
737
            for(j = 0; j < 9; j++) {
738
                for(i = 0; i < 9; i++) {
739
                    src[i] = v->lutuv[src[i]];
740
                    src2[i] = v->lutuv[src2[i]];
741
                }
742
                src += s->uvlinesize;
743
                src2 += s->uvlinesize;
744
            }
745
        }
746
    }
747

    
748
    /* Chroma MC always uses qpel bilinear */
749
    uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
750
    uvmx = (uvmx&3)<<1;
751
    uvmy = (uvmy&3)<<1;
752
    if(!v->rnd){
753
        dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
754
        dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
755
    }else{
756
        dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
757
        dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
758
    }
759
}
760

    
761
static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb);
762

    
763
/**
764
 * Decode Simple/Main Profiles sequence header
765
 * @see Figure 7-8, p16-17
766
 * @param avctx Codec context
767
 * @param gb GetBit context initialized from Codec context extra_data
768
 * @return Status
769
 */
770
static int decode_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
771
{
772
    VC1Context *v = avctx->priv_data;
773

    
774
    av_log(avctx, AV_LOG_DEBUG, "Header: %0X\n", show_bits(gb, 32));
775
    v->profile = get_bits(gb, 2);
776
    if (v->profile == PROFILE_COMPLEX)
777
    {
778
        av_log(avctx, AV_LOG_ERROR, "WMV3 Complex Profile is not fully supported\n");
779
    }
780

    
781
    if (v->profile == PROFILE_ADVANCED)
782
    {
783
        return decode_sequence_header_adv(v, gb);
784
    }
785
    else
786
    {
787
        v->res_sm = get_bits(gb, 2); //reserved
788
        if (v->res_sm)
789
        {
790
            av_log(avctx, AV_LOG_ERROR,
791
                   "Reserved RES_SM=%i is forbidden\n", v->res_sm);
792
            return -1;
793
        }
794
    }
795

    
796
    // (fps-2)/4 (->30)
797
    v->frmrtq_postproc = get_bits(gb, 3); //common
798
    // (bitrate-32kbps)/64kbps
799
    v->bitrtq_postproc = get_bits(gb, 5); //common
800
    v->s.loop_filter = get_bits(gb, 1); //common
801
    if(v->s.loop_filter == 1 && v->profile == PROFILE_SIMPLE)
802
    {
803
        av_log(avctx, AV_LOG_ERROR,
804
               "LOOPFILTER shell not be enabled in simple profile\n");
805
    }
806

    
807
    v->res_x8 = get_bits(gb, 1); //reserved
808
    if (v->res_x8)
809
    {
810
        av_log(avctx, AV_LOG_ERROR,
811
               "1 for reserved RES_X8 is forbidden\n");
812
        //return -1;
813
    }
814
    v->multires = get_bits(gb, 1);
815
    v->res_fasttx = get_bits(gb, 1);
816
    if (!v->res_fasttx)
817
    {
818
        av_log(avctx, AV_LOG_ERROR,
819
               "0 for reserved RES_FASTTX is forbidden\n");
820
        //return -1;
821
    }
822

    
823
    v->fastuvmc =  get_bits(gb, 1); //common
824
    if (!v->profile && !v->fastuvmc)
825
    {
826
        av_log(avctx, AV_LOG_ERROR,
827
               "FASTUVMC unavailable in Simple Profile\n");
828
        return -1;
829
    }
830
    v->extended_mv =  get_bits(gb, 1); //common
831
    if (!v->profile && v->extended_mv)
832
    {
833
        av_log(avctx, AV_LOG_ERROR,
834
               "Extended MVs unavailable in Simple Profile\n");
835
        return -1;
836
    }
837
    v->dquant =  get_bits(gb, 2); //common
838
    v->vstransform =  get_bits(gb, 1); //common
839

    
840
    v->res_transtab = get_bits(gb, 1);
841
    if (v->res_transtab)
842
    {
843
        av_log(avctx, AV_LOG_ERROR,
844
               "1 for reserved RES_TRANSTAB is forbidden\n");
845
        return -1;
846
    }
847

    
848
    v->overlap = get_bits(gb, 1); //common
849

    
850
    v->s.resync_marker = get_bits(gb, 1);
851
    v->rangered = get_bits(gb, 1);
852
    if (v->rangered && v->profile == PROFILE_SIMPLE)
853
    {
854
        av_log(avctx, AV_LOG_INFO,
855
               "RANGERED should be set to 0 in simple profile\n");
856
    }
857

    
858
    v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
859
    v->quantizer_mode = get_bits(gb, 2); //common
860

    
861
    v->finterpflag = get_bits(gb, 1); //common
862
    v->res_rtm_flag = get_bits(gb, 1); //reserved
863
    if (!v->res_rtm_flag)
864
    {
865
//            av_log(avctx, AV_LOG_ERROR,
866
//                   "0 for reserved RES_RTM_FLAG is forbidden\n");
867
        av_log(avctx, AV_LOG_ERROR,
868
               "Old WMV3 version detected, only I-frames will be decoded\n");
869
        //return -1;
870
    }
871
    //TODO: figure out what they mean (always 0x402F)
872
    if(!v->res_fasttx) skip_bits(gb, 16);
873
    av_log(avctx, AV_LOG_DEBUG,
874
               "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
875
               "LoopFilter=%i, MultiRes=%i, FastUVMC=%i, Extended MV=%i\n"
876
               "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
877
               "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
878
               v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
879
               v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
880
               v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
881
               v->dquant, v->quantizer_mode, avctx->max_b_frames
882
               );
883
    return 0;
884
}
885

    
886
static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb)
887
{
888
    v->res_rtm_flag = 1;
889
    v->level = get_bits(gb, 3);
890
    if(v->level >= 5)
891
    {
892
        av_log(v->s.avctx, AV_LOG_ERROR, "Reserved LEVEL %i\n",v->level);
893
    }
894
    v->chromaformat = get_bits(gb, 2);
895
    if (v->chromaformat != 1)
896
    {
897
        av_log(v->s.avctx, AV_LOG_ERROR,
898
               "Only 4:2:0 chroma format supported\n");
899
        return -1;
900
    }
901

    
902
    // (fps-2)/4 (->30)
903
    v->frmrtq_postproc = get_bits(gb, 3); //common
904
    // (bitrate-32kbps)/64kbps
905
    v->bitrtq_postproc = get_bits(gb, 5); //common
906
    v->postprocflag = get_bits(gb, 1); //common
907

    
908
    v->s.avctx->coded_width = (get_bits(gb, 12) + 1) << 1;
909
    v->s.avctx->coded_height = (get_bits(gb, 12) + 1) << 1;
910
    v->s.avctx->width = v->s.avctx->coded_width;
911
    v->s.avctx->height = v->s.avctx->coded_height;
912
    v->broadcast = get_bits1(gb);
913
    v->interlace = get_bits1(gb);
914
    v->tfcntrflag = get_bits1(gb);
915
    v->finterpflag = get_bits1(gb);
916
    get_bits1(gb); // reserved
917

    
918
    v->s.h_edge_pos = v->s.avctx->coded_width;
919
    v->s.v_edge_pos = v->s.avctx->coded_height;
920

    
921
    av_log(v->s.avctx, AV_LOG_DEBUG,
922
               "Advanced Profile level %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
923
               "LoopFilter=%i, ChromaFormat=%i, Pulldown=%i, Interlace: %i\n"
924
               "TFCTRflag=%i, FINTERPflag=%i\n",
925
               v->level, v->frmrtq_postproc, v->bitrtq_postproc,
926
               v->s.loop_filter, v->chromaformat, v->broadcast, v->interlace,
927
               v->tfcntrflag, v->finterpflag
928
               );
929

    
930
    v->psf = get_bits1(gb);
931
    if(v->psf) { //PsF, 6.1.13
932
        av_log(v->s.avctx, AV_LOG_ERROR, "Progressive Segmented Frame mode: not supported (yet)\n");
933
        return -1;
934
    }
935
    v->s.max_b_frames = v->s.avctx->max_b_frames = 7;
936
    if(get_bits1(gb)) { //Display Info - decoding is not affected by it
937
        int w, h, ar = 0;
938
        av_log(v->s.avctx, AV_LOG_DEBUG, "Display extended info:\n");
939
        v->s.avctx->width  = v->s.width  = w = get_bits(gb, 14) + 1;
940
        v->s.avctx->height = v->s.height = h = get_bits(gb, 14) + 1;
941
        av_log(v->s.avctx, AV_LOG_DEBUG, "Display dimensions: %ix%i\n", w, h);
942
        if(get_bits1(gb))
943
            ar = get_bits(gb, 4);
944
        if(ar && ar < 14){
945
            v->s.avctx->sample_aspect_ratio = ff_vc1_pixel_aspect[ar];
946
        }else if(ar == 15){
947
            w = get_bits(gb, 8);
948
            h = get_bits(gb, 8);
949
            v->s.avctx->sample_aspect_ratio = (AVRational){w, h};
950
        }
951

    
952
        if(get_bits1(gb)){ //framerate stuff
953
            if(get_bits1(gb)) {
954
                v->s.avctx->time_base.num = 32;
955
                v->s.avctx->time_base.den = get_bits(gb, 16) + 1;
956
            } else {
957
                int nr, dr;
958
                nr = get_bits(gb, 8);
959
                dr = get_bits(gb, 4);
960
                if(nr && nr < 8 && dr && dr < 3){
961
                    v->s.avctx->time_base.num = ff_vc1_fps_dr[dr - 1];
962
                    v->s.avctx->time_base.den = ff_vc1_fps_nr[nr - 1] * 1000;
963
                }
964
            }
965
        }
966

    
967
        if(get_bits1(gb)){
968
            v->color_prim = get_bits(gb, 8);
969
            v->transfer_char = get_bits(gb, 8);
970
            v->matrix_coef = get_bits(gb, 8);
971
        }
972
    }
973

    
974
    v->hrd_param_flag = get_bits1(gb);
975
    if(v->hrd_param_flag) {
976
        int i;
977
        v->hrd_num_leaky_buckets = get_bits(gb, 5);
978
        get_bits(gb, 4); //bitrate exponent
979
        get_bits(gb, 4); //buffer size exponent
980
        for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
981
            get_bits(gb, 16); //hrd_rate[n]
982
            get_bits(gb, 16); //hrd_buffer[n]
983
        }
984
    }
985
    return 0;
986
}
987

    
988
static int decode_entry_point(AVCodecContext *avctx, GetBitContext *gb)
989
{
990
    VC1Context *v = avctx->priv_data;
991
    int i, blink, clentry, refdist;
992

    
993
    av_log(avctx, AV_LOG_DEBUG, "Entry point: %08X\n", show_bits_long(gb, 32));
994
    blink = get_bits1(gb); // broken link
995
    clentry = get_bits1(gb); // closed entry
996
    v->panscanflag = get_bits1(gb);
997
    refdist = get_bits1(gb); // refdist flag
998
    v->s.loop_filter = get_bits1(gb);
999
    v->fastuvmc = get_bits1(gb);
1000
    v->extended_mv = get_bits1(gb);
1001
    v->dquant = get_bits(gb, 2);
1002
    v->vstransform = get_bits1(gb);
1003
    v->overlap = get_bits1(gb);
1004
    v->quantizer_mode = get_bits(gb, 2);
1005

    
1006
    if(v->hrd_param_flag){
1007
        for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
1008
            get_bits(gb, 8); //hrd_full[n]
1009
        }
1010
    }
1011

    
1012
    if(get_bits1(gb)){
1013
        avctx->coded_width = (get_bits(gb, 12)+1)<<1;
1014
        avctx->coded_height = (get_bits(gb, 12)+1)<<1;
1015
    }
1016
    if(v->extended_mv)
1017
        v->extended_dmv = get_bits1(gb);
1018
    if(get_bits1(gb)) {
1019
        av_log(avctx, AV_LOG_ERROR, "Luma scaling is not supported, expect wrong picture\n");
1020
        skip_bits(gb, 3); // Y range, ignored for now
1021
    }
1022
    if(get_bits1(gb)) {
1023
        av_log(avctx, AV_LOG_ERROR, "Chroma scaling is not supported, expect wrong picture\n");
1024
        skip_bits(gb, 3); // UV range, ignored for now
1025
    }
1026

    
1027
    av_log(avctx, AV_LOG_DEBUG, "Entry point info:\n"
1028
        "BrokenLink=%i, ClosedEntry=%i, PanscanFlag=%i\n"
1029
        "RefDist=%i, Postproc=%i, FastUVMC=%i, ExtMV=%i\n"
1030
        "DQuant=%i, VSTransform=%i, Overlap=%i, Qmode=%i\n",
1031
        blink, clentry, v->panscanflag, refdist, v->s.loop_filter,
1032
        v->fastuvmc, v->extended_mv, v->dquant, v->vstransform, v->overlap, v->quantizer_mode);
1033

    
1034
    return 0;
1035
}
1036

    
1037
static int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
1038
{
1039
    int pqindex, lowquant, status;
1040

    
1041
    if(v->finterpflag) v->interpfrm = get_bits(gb, 1);
1042
    skip_bits(gb, 2); //framecnt unused
1043
    v->rangeredfrm = 0;
1044
    if (v->rangered) v->rangeredfrm = get_bits(gb, 1);
1045
    v->s.pict_type = get_bits(gb, 1);
1046
    if (v->s.avctx->max_b_frames) {
1047
        if (!v->s.pict_type) {
1048
            if (get_bits(gb, 1)) v->s.pict_type = I_TYPE;
1049
            else v->s.pict_type = B_TYPE;
1050
        } else v->s.pict_type = P_TYPE;
1051
    } else v->s.pict_type = v->s.pict_type ? P_TYPE : I_TYPE;
1052

    
1053
    v->bi_type = 0;
1054
    if(v->s.pict_type == B_TYPE) {
1055
        v->bfraction = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
1056
        v->bfraction = ff_vc1_bfraction_lut[v->bfraction];
1057
        if(v->bfraction == 0) {
1058
            v->s.pict_type = BI_TYPE;
1059
        }
1060
    }
1061
    if(v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
1062
        get_bits(gb, 7); // skip buffer fullness
1063

    
1064
    /* calculate RND */
1065
    if(v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
1066
        v->rnd = 1;
1067
    if(v->s.pict_type == P_TYPE)
1068
        v->rnd ^= 1;
1069

    
1070
    /* Quantizer stuff */
1071
    pqindex = get_bits(gb, 5);
1072
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1073
        v->pq = ff_vc1_pquant_table[0][pqindex];
1074
    else
1075
        v->pq = ff_vc1_pquant_table[1][pqindex];
1076

    
1077
    v->pquantizer = 1;
1078
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1079
        v->pquantizer = pqindex < 9;
1080
    if (v->quantizer_mode == QUANT_NON_UNIFORM)
1081
        v->pquantizer = 0;
1082
    v->pqindex = pqindex;
1083
    if (pqindex < 9) v->halfpq = get_bits(gb, 1);
1084
    else v->halfpq = 0;
1085
    if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
1086
        v->pquantizer = get_bits(gb, 1);
1087
    v->dquantfrm = 0;
1088
    if (v->extended_mv == 1) v->mvrange = get_unary(gb, 0, 3);
1089
    v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1090
    v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
1091
    v->range_x = 1 << (v->k_x - 1);
1092
    v->range_y = 1 << (v->k_y - 1);
1093
    if (v->profile == PROFILE_ADVANCED)
1094
    {
1095
        if (v->postprocflag) v->postproc = get_bits(gb, 1);
1096
    }
1097
    else
1098
        if (v->multires && v->s.pict_type != B_TYPE) v->respic = get_bits(gb, 2);
1099

    
1100
    if(v->res_x8 && (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)){
1101
        if(get_bits1(gb))return -1;
1102
    }
1103
//av_log(v->s.avctx, AV_LOG_INFO, "%c Frame: QP=[%i]%i (+%i/2) %i\n",
1104
//        (v->s.pict_type == P_TYPE) ? 'P' : ((v->s.pict_type == I_TYPE) ? 'I' : 'B'), pqindex, v->pq, v->halfpq, v->rangeredfrm);
1105

    
1106
    if(v->s.pict_type == I_TYPE || v->s.pict_type == P_TYPE) v->use_ic = 0;
1107

    
1108
    switch(v->s.pict_type) {
1109
    case P_TYPE:
1110
        if (v->pq < 5) v->tt_index = 0;
1111
        else if(v->pq < 13) v->tt_index = 1;
1112
        else v->tt_index = 2;
1113

    
1114
        lowquant = (v->pq > 12) ? 0 : 1;
1115
        v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
1116
        if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
1117
        {
1118
            int scale, shift, i;
1119
            v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
1120
            v->lumscale = get_bits(gb, 6);
1121
            v->lumshift = get_bits(gb, 6);
1122
            v->use_ic = 1;
1123
            /* fill lookup tables for intensity compensation */
1124
            if(!v->lumscale) {
1125
                scale = -64;
1126
                shift = (255 - v->lumshift * 2) << 6;
1127
                if(v->lumshift > 31)
1128
                    shift += 128 << 6;
1129
            } else {
1130
                scale = v->lumscale + 32;
1131
                if(v->lumshift > 31)
1132
                    shift = (v->lumshift - 64) << 6;
1133
                else
1134
                    shift = v->lumshift << 6;
1135
            }
1136
            for(i = 0; i < 256; i++) {
1137
                v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
1138
                v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
1139
            }
1140
        }
1141
        if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
1142
            v->s.quarter_sample = 0;
1143
        else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1144
            if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
1145
                v->s.quarter_sample = 0;
1146
            else
1147
                v->s.quarter_sample = 1;
1148
        } else
1149
            v->s.quarter_sample = 1;
1150
        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));
1151

    
1152
        if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
1153
                 v->mv_mode2 == MV_PMODE_MIXED_MV)
1154
                || v->mv_mode == MV_PMODE_MIXED_MV)
1155
        {
1156
            status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
1157
            if (status < 0) return -1;
1158
            av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
1159
                   "Imode: %i, Invert: %i\n", status>>1, status&1);
1160
        } else {
1161
            v->mv_type_is_raw = 0;
1162
            memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
1163
        }
1164
        status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1165
        if (status < 0) return -1;
1166
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1167
               "Imode: %i, Invert: %i\n", status>>1, status&1);
1168

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

    
1173
        if (v->dquant)
1174
        {
1175
            av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1176
            vop_dquant_decoding(v);
1177
        }
1178

    
1179
        v->ttfrm = 0; //FIXME Is that so ?
1180
        if (v->vstransform)
1181
        {
1182
            v->ttmbf = get_bits(gb, 1);
1183
            if (v->ttmbf)
1184
            {
1185
                v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
1186
            }
1187
        } else {
1188
            v->ttmbf = 1;
1189
            v->ttfrm = TT_8X8;
1190
        }
1191
        break;
1192
    case B_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 = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
1199
        v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
1200
        v->s.mspel = v->s.quarter_sample;
1201

    
1202
        status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
1203
        if (status < 0) return -1;
1204
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
1205
               "Imode: %i, Invert: %i\n", status>>1, status&1);
1206
        status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1207
        if (status < 0) return -1;
1208
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1209
               "Imode: %i, Invert: %i\n", status>>1, status&1);
1210

    
1211
        v->s.mv_table_index = get_bits(gb, 2);
1212
        v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1213

    
1214
        if (v->dquant)
1215
        {
1216
            av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1217
            vop_dquant_decoding(v);
1218
        }
1219

    
1220
        v->ttfrm = 0;
1221
        if (v->vstransform)
1222
        {
1223
            v->ttmbf = get_bits(gb, 1);
1224
            if (v->ttmbf)
1225
            {
1226
                v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
1227
            }
1228
        } else {
1229
            v->ttmbf = 1;
1230
            v->ttfrm = TT_8X8;
1231
        }
1232
        break;
1233
    }
1234

    
1235
    /* AC Syntax */
1236
    v->c_ac_table_index = decode012(gb);
1237
    if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
1238
    {
1239
        v->y_ac_table_index = decode012(gb);
1240
    }
1241
    /* DC Syntax */
1242
    v->s.dc_table_index = get_bits(gb, 1);
1243

    
1244
    if(v->s.pict_type == BI_TYPE) {
1245
        v->s.pict_type = B_TYPE;
1246
        v->bi_type = 1;
1247
    }
1248
    return 0;
1249
}
1250

    
1251
static int vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb)
1252
{
1253
    int pqindex, lowquant;
1254
    int status;
1255

    
1256
    v->p_frame_skipped = 0;
1257

    
1258
    if(v->interlace){
1259
        v->fcm = decode012(gb);
1260
        if(v->fcm) return -1; // interlaced frames/fields are not implemented
1261
    }
1262
    switch(get_unary(gb, 0, 4)) {
1263
    case 0:
1264
        v->s.pict_type = P_TYPE;
1265
        break;
1266
    case 1:
1267
        v->s.pict_type = B_TYPE;
1268
        break;
1269
    case 2:
1270
        v->s.pict_type = I_TYPE;
1271
        break;
1272
    case 3:
1273
        v->s.pict_type = BI_TYPE;
1274
        break;
1275
    case 4:
1276
        v->s.pict_type = P_TYPE; // skipped pic
1277
        v->p_frame_skipped = 1;
1278
        return 0;
1279
    }
1280
    if(v->tfcntrflag)
1281
        get_bits(gb, 8);
1282
    if(v->broadcast) {
1283
        if(!v->interlace || v->psf) {
1284
            v->rptfrm = get_bits(gb, 2);
1285
        } else {
1286
            v->tff = get_bits1(gb);
1287
            v->rptfrm = get_bits1(gb);
1288
        }
1289
    }
1290
    if(v->panscanflag) {
1291
        //...
1292
    }
1293
    v->rnd = get_bits1(gb);
1294
    if(v->interlace)
1295
        v->uvsamp = get_bits1(gb);
1296
    if(v->finterpflag) v->interpfrm = get_bits(gb, 1);
1297
    if(v->s.pict_type == B_TYPE) {
1298
        v->bfraction = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
1299
        v->bfraction = ff_vc1_bfraction_lut[v->bfraction];
1300
        if(v->bfraction == 0) {
1301
            v->s.pict_type = BI_TYPE; /* XXX: should not happen here */
1302
        }
1303
    }
1304
    pqindex = get_bits(gb, 5);
1305
    v->pqindex = pqindex;
1306
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1307
        v->pq = ff_vc1_pquant_table[0][pqindex];
1308
    else
1309
        v->pq = ff_vc1_pquant_table[1][pqindex];
1310

    
1311
    v->pquantizer = 1;
1312
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1313
        v->pquantizer = pqindex < 9;
1314
    if (v->quantizer_mode == QUANT_NON_UNIFORM)
1315
        v->pquantizer = 0;
1316
    v->pqindex = pqindex;
1317
    if (pqindex < 9) v->halfpq = get_bits(gb, 1);
1318
    else v->halfpq = 0;
1319
    if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
1320
        v->pquantizer = get_bits(gb, 1);
1321

    
1322
    if(v->s.pict_type == I_TYPE || v->s.pict_type == P_TYPE) v->use_ic = 0;
1323

    
1324
    switch(v->s.pict_type) {
1325
    case I_TYPE:
1326
    case BI_TYPE:
1327
        status = bitplane_decoding(v->acpred_plane, &v->acpred_is_raw, v);
1328
        if (status < 0) return -1;
1329
        av_log(v->s.avctx, AV_LOG_DEBUG, "ACPRED plane encoding: "
1330
                "Imode: %i, Invert: %i\n", status>>1, status&1);
1331
        v->condover = CONDOVER_NONE;
1332
        if(v->overlap && v->pq <= 8) {
1333
            v->condover = decode012(gb);
1334
            if(v->condover == CONDOVER_SELECT) {
1335
                status = bitplane_decoding(v->over_flags_plane, &v->overflg_is_raw, v);
1336
                if (status < 0) return -1;
1337
                av_log(v->s.avctx, AV_LOG_DEBUG, "CONDOVER plane encoding: "
1338
                        "Imode: %i, Invert: %i\n", status>>1, status&1);
1339
            }
1340
        }
1341
        break;
1342
    case P_TYPE:
1343
        if(v->postprocflag)
1344
            v->postproc = get_bits1(gb);
1345
        if (v->extended_mv) v->mvrange = get_unary(gb, 0, 3);
1346
        else v->mvrange = 0;
1347
        v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1348
        v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
1349
        v->range_x = 1 << (v->k_x - 1);
1350
        v->range_y = 1 << (v->k_y - 1);
1351

    
1352
        if (v->pq < 5) v->tt_index = 0;
1353
        else if(v->pq < 13) v->tt_index = 1;
1354
        else v->tt_index = 2;
1355

    
1356
        lowquant = (v->pq > 12) ? 0 : 1;
1357
        v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
1358
        if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
1359
        {
1360
            int scale, shift, i;
1361
            v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
1362
            v->lumscale = get_bits(gb, 6);
1363
            v->lumshift = get_bits(gb, 6);
1364
            /* fill lookup tables for intensity compensation */
1365
            if(!v->lumscale) {
1366
                scale = -64;
1367
                shift = (255 - v->lumshift * 2) << 6;
1368
                if(v->lumshift > 31)
1369
                    shift += 128 << 6;
1370
            } else {
1371
                scale = v->lumscale + 32;
1372
                if(v->lumshift > 31)
1373
                    shift = (v->lumshift - 64) << 6;
1374
                else
1375
                    shift = v->lumshift << 6;
1376
            }
1377
            for(i = 0; i < 256; i++) {
1378
                v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
1379
                v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
1380
            }
1381
            v->use_ic = 1;
1382
        }
1383
        if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
1384
            v->s.quarter_sample = 0;
1385
        else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1386
            if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
1387
                v->s.quarter_sample = 0;
1388
            else
1389
                v->s.quarter_sample = 1;
1390
        } else
1391
            v->s.quarter_sample = 1;
1392
        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));
1393

    
1394
        if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
1395
                 v->mv_mode2 == MV_PMODE_MIXED_MV)
1396
                || v->mv_mode == MV_PMODE_MIXED_MV)
1397
        {
1398
            status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
1399
            if (status < 0) return -1;
1400
            av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
1401
                   "Imode: %i, Invert: %i\n", status>>1, status&1);
1402
        } else {
1403
            v->mv_type_is_raw = 0;
1404
            memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
1405
        }
1406
        status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1407
        if (status < 0) return -1;
1408
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1409
               "Imode: %i, Invert: %i\n", status>>1, status&1);
1410

    
1411
        /* Hopefully this is correct for P frames */
1412
        v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
1413
        v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1414
        if (v->dquant)
1415
        {
1416
            av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1417
            vop_dquant_decoding(v);
1418
        }
1419

    
1420
        v->ttfrm = 0; //FIXME Is that so ?
1421
        if (v->vstransform)
1422
        {
1423
            v->ttmbf = get_bits(gb, 1);
1424
            if (v->ttmbf)
1425
            {
1426
                v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
1427
            }
1428
        } else {
1429
            v->ttmbf = 1;
1430
            v->ttfrm = TT_8X8;
1431
        }
1432
        break;
1433
    case B_TYPE:
1434
        if(v->postprocflag)
1435
            v->postproc = get_bits1(gb);
1436
        if (v->extended_mv) v->mvrange = get_unary(gb, 0, 3);
1437
        else v->mvrange = 0;
1438
        v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1439
        v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
1440
        v->range_x = 1 << (v->k_x - 1);
1441
        v->range_y = 1 << (v->k_y - 1);
1442

    
1443
        if (v->pq < 5) v->tt_index = 0;
1444
        else if(v->pq < 13) v->tt_index = 1;
1445
        else v->tt_index = 2;
1446

    
1447
        lowquant = (v->pq > 12) ? 0 : 1;
1448
        v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
1449
        v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
1450
        v->s.mspel = v->s.quarter_sample;
1451

    
1452
        status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
1453
        if (status < 0) return -1;
1454
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
1455
               "Imode: %i, Invert: %i\n", status>>1, status&1);
1456
        status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1457
        if (status < 0) return -1;
1458
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1459
               "Imode: %i, Invert: %i\n", status>>1, status&1);
1460

    
1461
        v->s.mv_table_index = get_bits(gb, 2);
1462
        v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1463

    
1464
        if (v->dquant)
1465
        {
1466
            av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1467
            vop_dquant_decoding(v);
1468
        }
1469

    
1470
        v->ttfrm = 0;
1471
        if (v->vstransform)
1472
        {
1473
            v->ttmbf = get_bits(gb, 1);
1474
            if (v->ttmbf)
1475
            {
1476
                v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
1477
            }
1478
        } else {
1479
            v->ttmbf = 1;
1480
            v->ttfrm = TT_8X8;
1481
        }
1482
        break;
1483
    }
1484

    
1485
    /* AC Syntax */
1486
    v->c_ac_table_index = decode012(gb);
1487
    if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
1488
    {
1489
        v->y_ac_table_index = decode012(gb);
1490
    }
1491
    /* DC Syntax */
1492
    v->s.dc_table_index = get_bits(gb, 1);
1493
    if ((v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE) && v->dquant) {
1494
        av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1495
        vop_dquant_decoding(v);
1496
    }
1497

    
1498
    v->bi_type = 0;
1499
    if(v->s.pict_type == BI_TYPE) {
1500
        v->s.pict_type = B_TYPE;
1501
        v->bi_type = 1;
1502
    }
1503
    return 0;
1504
}
1505

    
1506
/***********************************************************************/
1507
/**
1508
 * @defgroup block VC-1 Block-level functions
1509
 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1510
 * @{
1511
 */
1512

    
1513
/**
1514
 * @def GET_MQUANT
1515
 * @brief Get macroblock-level quantizer scale
1516
 */
1517
#define GET_MQUANT()                                           \
1518
  if (v->dquantfrm)                                            \
1519
  {                                                            \
1520
    int edges = 0;                                             \
1521
    if (v->dqprofile == DQPROFILE_ALL_MBS)                     \
1522
    {                                                          \
1523
      if (v->dqbilevel)                                        \
1524
      {                                                        \
1525
        mquant = (get_bits(gb, 1)) ? v->altpq : v->pq;         \
1526
      }                                                        \
1527
      else                                                     \
1528
      {                                                        \
1529
        mqdiff = get_bits(gb, 3);                              \
1530
        if (mqdiff != 7) mquant = v->pq + mqdiff;              \
1531
        else mquant = get_bits(gb, 5);                         \
1532
      }                                                        \
1533
    }                                                          \
1534
    if(v->dqprofile == DQPROFILE_SINGLE_EDGE)                  \
1535
        edges = 1 << v->dqsbedge;                              \
1536
    else if(v->dqprofile == DQPROFILE_DOUBLE_EDGES)            \
1537
        edges = (3 << v->dqsbedge) % 15;                       \
1538
    else if(v->dqprofile == DQPROFILE_FOUR_EDGES)              \
1539
        edges = 15;                                            \
1540
    if((edges&1) && !s->mb_x)                                  \
1541
        mquant = v->altpq;                                     \
1542
    if((edges&2) && s->first_slice_line)                       \
1543
        mquant = v->altpq;                                     \
1544
    if((edges&4) && s->mb_x == (s->mb_width - 1))              \
1545
        mquant = v->altpq;                                     \
1546
    if((edges&8) && s->mb_y == (s->mb_height - 1))             \
1547
        mquant = v->altpq;                                     \
1548
  }
1549

    
1550
/**
1551
 * @def GET_MVDATA(_dmv_x, _dmv_y)
1552
 * @brief Get MV differentials
1553
 * @see MVDATA decoding from 8.3.5.2, p(1)20
1554
 * @param _dmv_x Horizontal differential for decoded MV
1555
 * @param _dmv_y Vertical differential for decoded MV
1556
 */
1557
#define GET_MVDATA(_dmv_x, _dmv_y)                                  \
1558
  index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table,\
1559
                       VC1_MV_DIFF_VLC_BITS, 2);                    \
1560
  if (index > 36)                                                   \
1561
  {                                                                 \
1562
    mb_has_coeffs = 1;                                              \
1563
    index -= 37;                                                    \
1564
  }                                                                 \
1565
  else mb_has_coeffs = 0;                                           \
1566
  s->mb_intra = 0;                                                  \
1567
  if (!index) { _dmv_x = _dmv_y = 0; }                              \
1568
  else if (index == 35)                                             \
1569
  {                                                                 \
1570
    _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample);          \
1571
    _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample);          \
1572
  }                                                                 \
1573
  else if (index == 36)                                             \
1574
  {                                                                 \
1575
    _dmv_x = 0;                                                     \
1576
    _dmv_y = 0;                                                     \
1577
    s->mb_intra = 1;                                                \
1578
  }                                                                 \
1579
  else                                                              \
1580
  {                                                                 \
1581
    index1 = index%6;                                               \
1582
    if (!s->quarter_sample && index1 == 5) val = 1;                 \
1583
    else                                   val = 0;                 \
1584
    if(size_table[index1] - val > 0)                                \
1585
        val = get_bits(gb, size_table[index1] - val);               \
1586
    else                                   val = 0;                 \
1587
    sign = 0 - (val&1);                                             \
1588
    _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign;     \
1589
                                                                    \
1590
    index1 = index/6;                                               \
1591
    if (!s->quarter_sample && index1 == 5) val = 1;                 \
1592
    else                                   val = 0;                 \
1593
    if(size_table[index1] - val > 0)                                \
1594
        val = get_bits(gb, size_table[index1] - val);               \
1595
    else                                   val = 0;                 \
1596
    sign = 0 - (val&1);                                             \
1597
    _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign;     \
1598
  }
1599

    
1600
/** Predict and set motion vector
1601
 */
1602
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)
1603
{
1604
    int xy, wrap, off = 0;
1605
    int16_t *A, *B, *C;
1606
    int px, py;
1607
    int sum;
1608

    
1609
    /* scale MV difference to be quad-pel */
1610
    dmv_x <<= 1 - s->quarter_sample;
1611
    dmv_y <<= 1 - s->quarter_sample;
1612

    
1613
    wrap = s->b8_stride;
1614
    xy = s->block_index[n];
1615

    
1616
    if(s->mb_intra){
1617
        s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
1618
        s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
1619
        s->current_picture.motion_val[1][xy][0] = 0;
1620
        s->current_picture.motion_val[1][xy][1] = 0;
1621
        if(mv1) { /* duplicate motion data for 1-MV block */
1622
            s->current_picture.motion_val[0][xy + 1][0] = 0;
1623
            s->current_picture.motion_val[0][xy + 1][1] = 0;
1624
            s->current_picture.motion_val[0][xy + wrap][0] = 0;
1625
            s->current_picture.motion_val[0][xy + wrap][1] = 0;
1626
            s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
1627
            s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
1628
            s->current_picture.motion_val[1][xy + 1][0] = 0;
1629
            s->current_picture.motion_val[1][xy + 1][1] = 0;
1630
            s->current_picture.motion_val[1][xy + wrap][0] = 0;
1631
            s->current_picture.motion_val[1][xy + wrap][1] = 0;
1632
            s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
1633
            s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
1634
        }
1635
        return;
1636
    }
1637

    
1638
    C = s->current_picture.motion_val[0][xy - 1];
1639
    A = s->current_picture.motion_val[0][xy - wrap];
1640
    if(mv1)
1641
        off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1642
    else {
1643
        //in 4-MV mode different blocks have different B predictor position
1644
        switch(n){
1645
        case 0:
1646
            off = (s->mb_x > 0) ? -1 : 1;
1647
            break;
1648
        case 1:
1649
            off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1650
            break;
1651
        case 2:
1652
            off = 1;
1653
            break;
1654
        case 3:
1655
            off = -1;
1656
        }
1657
    }
1658
    B = s->current_picture.motion_val[0][xy - wrap + off];
1659

    
1660
    if(!s->first_slice_line || (n==2 || n==3)) { // predictor A is not out of bounds
1661
        if(s->mb_width == 1) {
1662
            px = A[0];
1663
            py = A[1];
1664
        } else {
1665
            px = mid_pred(A[0], B[0], C[0]);
1666
            py = mid_pred(A[1], B[1], C[1]);
1667
        }
1668
    } else if(s->mb_x || (n==1 || n==3)) { // predictor C is not out of bounds
1669
        px = C[0];
1670
        py = C[1];
1671
    } else {
1672
        px = py = 0;
1673
    }
1674
    /* Pullback MV as specified in 8.3.5.3.4 */
1675
    {
1676
        int qx, qy, X, Y;
1677
        qx = (s->mb_x << 6) + ((n==1 || n==3) ? 32 : 0);
1678
        qy = (s->mb_y << 6) + ((n==2 || n==3) ? 32 : 0);
1679
        X = (s->mb_width << 6) - 4;
1680
        Y = (s->mb_height << 6) - 4;
1681
        if(mv1) {
1682
            if(qx + px < -60) px = -60 - qx;
1683
            if(qy + py < -60) py = -60 - qy;
1684
        } else {
1685
            if(qx + px < -28) px = -28 - qx;
1686
            if(qy + py < -28) py = -28 - qy;
1687
        }
1688
        if(qx + px > X) px = X - qx;
1689
        if(qy + py > Y) py = Y - qy;
1690
    }
1691
    /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
1692
    if((!s->first_slice_line || (n==2 || n==3)) && (s->mb_x || (n==1 || n==3))) {
1693
        if(is_intra[xy - wrap])
1694
            sum = FFABS(px) + FFABS(py);
1695
        else
1696
            sum = FFABS(px - A[0]) + FFABS(py - A[1]);
1697
        if(sum > 32) {
1698
            if(get_bits1(&s->gb)) {
1699
                px = A[0];
1700
                py = A[1];
1701
            } else {
1702
                px = C[0];
1703
                py = C[1];
1704
            }
1705
        } else {
1706
            if(is_intra[xy - 1])
1707
                sum = FFABS(px) + FFABS(py);
1708
            else
1709
                sum = FFABS(px - C[0]) + FFABS(py - C[1]);
1710
            if(sum > 32) {
1711
                if(get_bits1(&s->gb)) {
1712
                    px = A[0];
1713
                    py = A[1];
1714
                } else {
1715
                    px = C[0];
1716
                    py = C[1];
1717
                }
1718
            }
1719
        }
1720
    }
1721
    /* store MV using signed modulus of MV range defined in 4.11 */
1722
    s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1723
    s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
1724
    if(mv1) { /* duplicate motion data for 1-MV block */
1725
        s->current_picture.motion_val[0][xy + 1][0] = s->current_picture.motion_val[0][xy][0];
1726
        s->current_picture.motion_val[0][xy + 1][1] = s->current_picture.motion_val[0][xy][1];
1727
        s->current_picture.motion_val[0][xy + wrap][0] = s->current_picture.motion_val[0][xy][0];
1728
        s->current_picture.motion_val[0][xy + wrap][1] = s->current_picture.motion_val[0][xy][1];
1729
        s->current_picture.motion_val[0][xy + wrap + 1][0] = s->current_picture.motion_val[0][xy][0];
1730
        s->current_picture.motion_val[0][xy + wrap + 1][1] = s->current_picture.motion_val[0][xy][1];
1731
    }
1732
}
1733

    
1734
/** Motion compensation for direct or interpolated blocks in B-frames
1735
 */
1736
static void vc1_interp_mc(VC1Context *v)
1737
{
1738
    MpegEncContext *s = &v->s;
1739
    DSPContext *dsp = &v->s.dsp;
1740
    uint8_t *srcY, *srcU, *srcV;
1741
    int dxy, uvdxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1742

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

    
1745
    mx = s->mv[1][0][0];
1746
    my = s->mv[1][0][1];
1747
    uvmx = (mx + ((mx & 3) == 3)) >> 1;
1748
    uvmy = (my + ((my & 3) == 3)) >> 1;
1749
    if(v->fastuvmc) {
1750
        uvmx = uvmx + ((uvmx<0)?-(uvmx&1):(uvmx&1));
1751
        uvmy = uvmy + ((uvmy<0)?-(uvmy&1):(uvmy&1));
1752
    }
1753
    srcY = s->next_picture.data[0];
1754
    srcU = s->next_picture.data[1];
1755
    srcV = s->next_picture.data[2];
1756

    
1757
    src_x = s->mb_x * 16 + (mx >> 2);
1758
    src_y = s->mb_y * 16 + (my >> 2);
1759
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1760
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1761

    
1762
    if(v->profile != PROFILE_ADVANCED){
1763
        src_x   = av_clip(  src_x, -16, s->mb_width  * 16);
1764
        src_y   = av_clip(  src_y, -16, s->mb_height * 16);
1765
        uvsrc_x = av_clip(uvsrc_x,  -8, s->mb_width  *  8);
1766
        uvsrc_y = av_clip(uvsrc_y,  -8, s->mb_height *  8);
1767
    }else{
1768
        src_x   = av_clip(  src_x, -17, s->avctx->coded_width);
1769
        src_y   = av_clip(  src_y, -18, s->avctx->coded_height + 1);
1770
        uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
1771
        uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
1772
    }
1773

    
1774
    srcY += src_y * s->linesize + src_x;
1775
    srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1776
    srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1777

    
1778
    /* for grayscale we should not try to read from unknown area */
1779
    if(s->flags & CODEC_FLAG_GRAY) {
1780
        srcU = s->edge_emu_buffer + 18 * s->linesize;
1781
        srcV = s->edge_emu_buffer + 18 * s->linesize;
1782
    }
1783

    
1784
    if(v->rangeredfrm
1785
       || (unsigned)src_x > s->h_edge_pos - (mx&3) - 16
1786
       || (unsigned)src_y > s->v_edge_pos - (my&3) - 16){
1787
        uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
1788

    
1789
        srcY -= s->mspel * (1 + s->linesize);
1790
        ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
1791
                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
1792
        srcY = s->edge_emu_buffer;
1793
        ff_emulated_edge_mc(uvbuf     , srcU, s->uvlinesize, 8+1, 8+1,
1794
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
1795
        ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
1796
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
1797
        srcU = uvbuf;
1798
        srcV = uvbuf + 16;
1799
        /* if we deal with range reduction we need to scale source blocks */
1800
        if(v->rangeredfrm) {
1801
            int i, j;
1802
            uint8_t *src, *src2;
1803

    
1804
            src = srcY;
1805
            for(j = 0; j < 17 + s->mspel*2; j++) {
1806
                for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
1807
                src += s->linesize;
1808
            }
1809
            src = srcU; src2 = srcV;
1810
            for(j = 0; j < 9; j++) {
1811
                for(i = 0; i < 9; i++) {
1812
                    src[i] = ((src[i] - 128) >> 1) + 128;
1813
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
1814
                }
1815
                src += s->uvlinesize;
1816
                src2 += s->uvlinesize;
1817
            }
1818
        }
1819
        srcY += s->mspel * (1 + s->linesize);
1820
    }
1821

    
1822
    mx >>= 1;
1823
    my >>= 1;
1824
    dxy = ((my & 1) << 1) | (mx & 1);
1825

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

    
1828
    if(s->flags & CODEC_FLAG_GRAY) return;
1829
    /* Chroma MC always uses qpel blilinear */
1830
    uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
1831
    uvmx = (uvmx&3)<<1;
1832
    uvmy = (uvmy&3)<<1;
1833
    dsp->avg_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
1834
    dsp->avg_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
1835
}
1836

    
1837
static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
1838
{
1839
    int n = bfrac;
1840

    
1841
#if B_FRACTION_DEN==256
1842
    if(inv)
1843
        n -= 256;
1844
    if(!qs)
1845
        return 2 * ((value * n + 255) >> 9);
1846
    return (value * n + 128) >> 8;
1847
#else
1848
    if(inv)
1849
        n -= B_FRACTION_DEN;
1850
    if(!qs)
1851
        return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
1852
    return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
1853
#endif
1854
}
1855

    
1856
/** Reconstruct motion vector for B-frame and do motion compensation
1857
 */
1858
static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mode)
1859
{
1860
    if(v->use_ic) {
1861
        v->mv_mode2 = v->mv_mode;
1862
        v->mv_mode = MV_PMODE_INTENSITY_COMP;
1863
    }
1864
    if(direct) {
1865
        vc1_mc_1mv(v, 0);
1866
        vc1_interp_mc(v);
1867
        if(v->use_ic) v->mv_mode = v->mv_mode2;
1868
        return;
1869
    }
1870
    if(mode == BMV_TYPE_INTERPOLATED) {
1871
        vc1_mc_1mv(v, 0);
1872
        vc1_interp_mc(v);
1873
        if(v->use_ic) v->mv_mode = v->mv_mode2;
1874
        return;
1875
    }
1876

    
1877
    if(v->use_ic && (mode == BMV_TYPE_BACKWARD)) v->mv_mode = v->mv_mode2;
1878
    vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
1879
    if(v->use_ic) v->mv_mode = v->mv_mode2;
1880
}
1881

    
1882
static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mvtype)
1883
{
1884
    MpegEncContext *s = &v->s;
1885
    int xy, wrap, off = 0;
1886
    int16_t *A, *B, *C;
1887
    int px, py;
1888
    int sum;
1889
    int r_x, r_y;
1890
    const uint8_t *is_intra = v->mb_type[0];
1891

    
1892
    r_x = v->range_x;
1893
    r_y = v->range_y;
1894
    /* scale MV difference to be quad-pel */
1895
    dmv_x[0] <<= 1 - s->quarter_sample;
1896
    dmv_y[0] <<= 1 - s->quarter_sample;
1897
    dmv_x[1] <<= 1 - s->quarter_sample;
1898
    dmv_y[1] <<= 1 - s->quarter_sample;
1899

    
1900
    wrap = s->b8_stride;
1901
    xy = s->block_index[0];
1902

    
1903
    if(s->mb_intra) {
1904
        s->current_picture.motion_val[0][xy][0] =
1905
        s->current_picture.motion_val[0][xy][1] =
1906
        s->current_picture.motion_val[1][xy][0] =
1907
        s->current_picture.motion_val[1][xy][1] = 0;
1908
        return;
1909
    }
1910
    s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
1911
    s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
1912
    s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
1913
    s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
1914

    
1915
    /* Pullback predicted motion vectors as specified in 8.4.5.4 */
1916
    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));
1917
    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));
1918
    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));
1919
    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));
1920
    if(direct) {
1921
        s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
1922
        s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
1923
        s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
1924
        s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
1925
        return;
1926
    }
1927

    
1928
    if((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
1929
        C = s->current_picture.motion_val[0][xy - 2];
1930
        A = s->current_picture.motion_val[0][xy - wrap*2];
1931
        off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1932
        B = s->current_picture.motion_val[0][xy - wrap*2 + off];
1933

    
1934
        if(!s->mb_x) C[0] = C[1] = 0;
1935
        if(!s->first_slice_line) { // predictor A is not out of bounds
1936
            if(s->mb_width == 1) {
1937
                px = A[0];
1938
                py = A[1];
1939
            } else {
1940
                px = mid_pred(A[0], B[0], C[0]);
1941
                py = mid_pred(A[1], B[1], C[1]);
1942
            }
1943
        } else if(s->mb_x) { // predictor C is not out of bounds
1944
            px = C[0];
1945
            py = C[1];
1946
        } else {
1947
            px = py = 0;
1948
        }
1949
        /* Pullback MV as specified in 8.3.5.3.4 */
1950
        {
1951
            int qx, qy, X, Y;
1952
            if(v->profile < PROFILE_ADVANCED) {
1953
                qx = (s->mb_x << 5);
1954
                qy = (s->mb_y << 5);
1955
                X = (s->mb_width << 5) - 4;
1956
                Y = (s->mb_height << 5) - 4;
1957
                if(qx + px < -28) px = -28 - qx;
1958
                if(qy + py < -28) py = -28 - qy;
1959
                if(qx + px > X) px = X - qx;
1960
                if(qy + py > Y) py = Y - qy;
1961
            } else {
1962
                qx = (s->mb_x << 6);
1963
                qy = (s->mb_y << 6);
1964
                X = (s->mb_width << 6) - 4;
1965
                Y = (s->mb_height << 6) - 4;
1966
                if(qx + px < -60) px = -60 - qx;
1967
                if(qy + py < -60) py = -60 - qy;
1968
                if(qx + px > X) px = X - qx;
1969
                if(qy + py > Y) py = Y - qy;
1970
            }
1971
        }
1972
        /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
1973
        if(0 && !s->first_slice_line && s->mb_x) {
1974
            if(is_intra[xy - wrap])
1975
                sum = FFABS(px) + FFABS(py);
1976
            else
1977
                sum = FFABS(px - A[0]) + FFABS(py - A[1]);
1978
            if(sum > 32) {
1979
                if(get_bits1(&s->gb)) {
1980
                    px = A[0];
1981
                    py = A[1];
1982
                } else {
1983
                    px = C[0];
1984
                    py = C[1];
1985
                }
1986
            } else {
1987
                if(is_intra[xy - 2])
1988
                    sum = FFABS(px) + FFABS(py);
1989
                else
1990
                    sum = FFABS(px - C[0]) + FFABS(py - C[1]);
1991
                if(sum > 32) {
1992
                    if(get_bits1(&s->gb)) {
1993
                        px = A[0];
1994
                        py = A[1];
1995
                    } else {
1996
                        px = C[0];
1997
                        py = C[1];
1998
                    }
1999
                }
2000
            }
2001
        }
2002
        /* store MV using signed modulus of MV range defined in 4.11 */
2003
        s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2004
        s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2005
    }
2006
    if((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2007
        C = s->current_picture.motion_val[1][xy - 2];
2008
        A = s->current_picture.motion_val[1][xy - wrap*2];
2009
        off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2010
        B = s->current_picture.motion_val[1][xy - wrap*2 + off];
2011

    
2012
        if(!s->mb_x) C[0] = C[1] = 0;
2013
        if(!s->first_slice_line) { // predictor A is not out of bounds
2014
            if(s->mb_width == 1) {
2015
                px = A[0];
2016
                py = A[1];
2017
            } else {
2018
                px = mid_pred(A[0], B[0], C[0]);
2019
                py = mid_pred(A[1], B[1], C[1]);
2020
            }
2021
        } else if(s->mb_x) { // predictor C is not out of bounds
2022
            px = C[0];
2023
            py = C[1];
2024
        } else {
2025
            px = py = 0;
2026
        }
2027
        /* Pullback MV as specified in 8.3.5.3.4 */
2028
        {
2029
            int qx, qy, X, Y;
2030
            if(v->profile < PROFILE_ADVANCED) {
2031
                qx = (s->mb_x << 5);
2032
                qy = (s->mb_y << 5);
2033
                X = (s->mb_width << 5) - 4;
2034
                Y = (s->mb_height << 5) - 4;
2035
                if(qx + px < -28) px = -28 - qx;
2036
                if(qy + py < -28) py = -28 - qy;
2037
                if(qx + px > X) px = X - qx;
2038
                if(qy + py > Y) py = Y - qy;
2039
            } else {
2040
                qx = (s->mb_x << 6);
2041
                qy = (s->mb_y << 6);
2042
                X = (s->mb_width << 6) - 4;
2043
                Y = (s->mb_height << 6) - 4;
2044
                if(qx + px < -60) px = -60 - qx;
2045
                if(qy + py < -60) py = -60 - qy;
2046
                if(qx + px > X) px = X - qx;
2047
                if(qy + py > Y) py = Y - qy;
2048
            }
2049
        }
2050
        /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2051
        if(0 && !s->first_slice_line && s->mb_x) {
2052
            if(is_intra[xy - wrap])
2053
                sum = FFABS(px) + FFABS(py);
2054
            else
2055
                sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2056
            if(sum > 32) {
2057
                if(get_bits1(&s->gb)) {
2058
                    px = A[0];
2059
                    py = A[1];
2060
                } else {
2061
                    px = C[0];
2062
                    py = C[1];
2063
                }
2064
            } else {
2065
                if(is_intra[xy - 2])
2066
                    sum = FFABS(px) + FFABS(py);
2067
                else
2068
                    sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2069
                if(sum > 32) {
2070
                    if(get_bits1(&s->gb)) {
2071
                        px = A[0];
2072
                        py = A[1];
2073
                    } else {
2074
                        px = C[0];
2075
                        py = C[1];
2076
                    }
2077
                }
2078
            }
2079
        }
2080
        /* store MV using signed modulus of MV range defined in 4.11 */
2081

    
2082
        s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2083
        s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2084
    }
2085
    s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
2086
    s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
2087
    s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
2088
    s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
2089
}
2090

    
2091
/** Get predicted DC value for I-frames only
2092
 * prediction dir: left=0, top=1
2093
 * @param s MpegEncContext
2094
 * @param[in] n block index in the current MB
2095
 * @param dc_val_ptr Pointer to DC predictor
2096
 * @param dir_ptr Prediction direction for use in AC prediction
2097
 */
2098
static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2099
                              int16_t **dc_val_ptr, int *dir_ptr)
2100
{
2101
    int a, b, c, wrap, pred, scale;
2102
    int16_t *dc_val;
2103
    static const uint16_t dcpred[32] = {
2104
    -1, 1024,  512,  341,  256,  205,  171,  146,  128,
2105
         114,  102,   93,   85,   79,   73,   68,   64,
2106
          60,   57,   54,   51,   49,   47,   45,   43,
2107
          41,   39,   38,   37,   35,   34,   33
2108
    };
2109

    
2110
    /* find prediction - wmv3_dc_scale always used here in fact */
2111
    if (n < 4)     scale = s->y_dc_scale;
2112
    else           scale = s->c_dc_scale;
2113

    
2114
    wrap = s->block_wrap[n];
2115
    dc_val= s->dc_val[0] + s->block_index[n];
2116

    
2117
    /* B A
2118
     * C X
2119
     */
2120
    c = dc_val[ - 1];
2121
    b = dc_val[ - 1 - wrap];
2122
    a = dc_val[ - wrap];
2123

    
2124
    if (pq < 9 || !overlap)
2125
    {
2126
        /* Set outer values */
2127
        if (s->first_slice_line && (n!=2 && n!=3)) b=a=dcpred[scale];
2128
        if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=dcpred[scale];
2129
    }
2130
    else
2131
    {
2132
        /* Set outer values */
2133
        if (s->first_slice_line && (n!=2 && n!=3)) b=a=0;
2134
        if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=0;
2135
    }
2136

    
2137
    if (abs(a - b) <= abs(b - c)) {
2138
        pred = c;
2139
        *dir_ptr = 1;//left
2140
    } else {
2141
        pred = a;
2142
        *dir_ptr = 0;//top
2143
    }
2144

    
2145
    /* update predictor */
2146
    *dc_val_ptr = &dc_val[0];
2147
    return pred;
2148
}
2149

    
2150

    
2151
/** Get predicted DC value
2152
 * prediction dir: left=0, top=1
2153
 * @param s MpegEncContext
2154
 * @param[in] n block index in the current MB
2155
 * @param dc_val_ptr Pointer to DC predictor
2156
 * @param dir_ptr Prediction direction for use in AC prediction
2157
 */
2158
static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2159
                              int a_avail, int c_avail,
2160
                              int16_t **dc_val_ptr, int *dir_ptr)
2161
{
2162
    int a, b, c, wrap, pred, scale;
2163
    int16_t *dc_val;
2164
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2165
    int q1, q2 = 0;
2166

    
2167
    /* find prediction - wmv3_dc_scale always used here in fact */
2168
    if (n < 4)     scale = s->y_dc_scale;
2169
    else           scale = s->c_dc_scale;
2170

    
2171
    wrap = s->block_wrap[n];
2172
    dc_val= s->dc_val[0] + s->block_index[n];
2173

    
2174
    /* B A
2175
     * C X
2176
     */
2177
    c = dc_val[ - 1];
2178
    b = dc_val[ - 1 - wrap];
2179
    a = dc_val[ - wrap];
2180
    /* scale predictors if needed */
2181
    q1 = s->current_picture.qscale_table[mb_pos];
2182
    if(c_avail && (n!= 1 && n!=3)) {
2183
        q2 = s->current_picture.qscale_table[mb_pos - 1];
2184
        if(q2 && q2 != q1)
2185
            c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
2186
    }
2187
    if(a_avail && (n!= 2 && n!=3)) {
2188
        q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2189
        if(q2 && q2 != q1)
2190
            a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
2191
    }
2192
    if(a_avail && c_avail && (n!=3)) {
2193
        int off = mb_pos;
2194
        if(n != 1) off--;
2195
        if(n != 2) off -= s->mb_stride;
2196
        q2 = s->current_picture.qscale_table[off];
2197
        if(q2 && q2 != q1)
2198
            b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
2199
    }
2200

    
2201
    if(a_avail && c_avail) {
2202
        if(abs(a - b) <= abs(b - c)) {
2203
            pred = c;
2204
            *dir_ptr = 1;//left
2205
        } else {
2206
            pred = a;
2207
            *dir_ptr = 0;//top
2208
        }
2209
    } else if(a_avail) {
2210
        pred = a;
2211
        *dir_ptr = 0;//top
2212
    } else if(c_avail) {
2213
        pred = c;
2214
        *dir_ptr = 1;//left
2215
    } else {
2216
        pred = 0;
2217
        *dir_ptr = 1;//left
2218
    }
2219

    
2220
    /* update predictor */
2221
    *dc_val_ptr = &dc_val[0];
2222
    return pred;
2223
}
2224

    
2225

    
2226
/**
2227
 * @defgroup std_mb VC1 Macroblock-level functions in Simple/Main Profiles
2228
 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2229
 * @{
2230
 */
2231

    
2232
static inline int vc1_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
2233
{
2234
    int xy, wrap, pred, a, b, c;
2235

    
2236
    xy = s->block_index[n];
2237
    wrap = s->b8_stride;
2238

    
2239
    /* B C
2240
     * A X
2241
     */
2242
    a = s->coded_block[xy - 1       ];
2243
    b = s->coded_block[xy - 1 - wrap];
2244
    c = s->coded_block[xy     - wrap];
2245

    
2246
    if (b == c) {
2247
        pred = a;
2248
    } else {
2249
        pred = c;
2250
    }
2251

    
2252
    /* store value */
2253
    *coded_block_ptr = &s->coded_block[xy];
2254

    
2255
    return pred;
2256
}
2257

    
2258
/**
2259
 * Decode one AC coefficient
2260
 * @param v The VC1 context
2261
 * @param last Last coefficient
2262
 * @param skip How much zero coefficients to skip
2263
 * @param value Decoded AC coefficient value
2264
 * @see 8.1.3.4
2265
 */
2266
static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip, int *value, int codingset)
2267
{
2268
    GetBitContext *gb = &v->s.gb;
2269
    int index, escape, run = 0, level = 0, lst = 0;
2270

    
2271
    index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2272
    if (index != vc1_ac_sizes[codingset] - 1) {
2273
        run = vc1_index_decode_table[codingset][index][0];
2274
        level = vc1_index_decode_table[codingset][index][1];
2275
        lst = index >= vc1_last_decode_table[codingset];
2276
        if(get_bits(gb, 1))
2277
            level = -level;
2278
    } else {
2279
        escape = decode210(gb);
2280
        if (escape != 2) {
2281
            index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2282
            run = vc1_index_decode_table[codingset][index][0];
2283
            level = vc1_index_decode_table[codingset][index][1];
2284
            lst = index >= vc1_last_decode_table[codingset];
2285
            if(escape == 0) {
2286
                if(lst)
2287
                    level += vc1_last_delta_level_table[codingset][run];
2288
                else
2289
                    level += vc1_delta_level_table[codingset][run];
2290
            } else {
2291
                if(lst)
2292
                    run += vc1_last_delta_run_table[codingset][level] + 1;
2293
                else
2294
                    run += vc1_delta_run_table[codingset][level] + 1;
2295
            }
2296
            if(get_bits(gb, 1))
2297
                level = -level;
2298
        } else {
2299
            int sign;
2300
            lst = get_bits(gb, 1);
2301
            if(v->s.esc3_level_length == 0) {
2302
                if(v->pq < 8 || v->dquantfrm) { // table 59
2303
                    v->s.esc3_level_length = get_bits(gb, 3);
2304
                    if(!v->s.esc3_level_length)
2305
                        v->s.esc3_level_length = get_bits(gb, 2) + 8;
2306
                } else { //table 60
2307
                    v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
2308
                }
2309
                v->s.esc3_run_length = 3 + get_bits(gb, 2);
2310
            }
2311
            run = get_bits(gb, v->s.esc3_run_length);
2312
            sign = get_bits(gb, 1);
2313
            level = get_bits(gb, v->s.esc3_level_length);
2314
            if(sign)
2315
                level = -level;
2316
        }
2317
    }
2318

    
2319
    *last = lst;
2320
    *skip = run;
2321
    *value = level;
2322
}
2323

    
2324
/** Decode intra block in intra frames - should be faster than decode_intra_block
2325
 * @param v VC1Context
2326
 * @param block block to decode
2327
 * @param coded are AC coeffs present or not
2328
 * @param codingset set of VLC to decode data
2329
 */
2330
static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset)
2331
{
2332
    GetBitContext *gb = &v->s.gb;
2333
    MpegEncContext *s = &v->s;
2334
    int dc_pred_dir = 0; /* Direction of the DC prediction used */
2335
    int run_diff, i;
2336
    int16_t *dc_val;
2337
    int16_t *ac_val, *ac_val2;
2338
    int dcdiff;
2339

    
2340
    /* Get DC differential */
2341
    if (n < 4) {
2342
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2343
    } else {
2344
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2345
    }
2346
    if (dcdiff < 0){
2347
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2348
        return -1;
2349
    }
2350
    if (dcdiff)
2351
    {
2352
        if (dcdiff == 119 /* ESC index value */)
2353
        {
2354
            /* TODO: Optimize */
2355
            if (v->pq == 1) dcdiff = get_bits(gb, 10);
2356
            else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2357
            else dcdiff = get_bits(gb, 8);
2358
        }
2359
        else
2360
        {
2361
            if (v->pq == 1)
2362
                dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
2363
            else if (v->pq == 2)
2364
                dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
2365
        }
2366
        if (get_bits(gb, 1))
2367
            dcdiff = -dcdiff;
2368
    }
2369

    
2370
    /* Prediction */
2371
    dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2372
    *dc_val = dcdiff;
2373

    
2374
    /* Store the quantized DC coeff, used for prediction */
2375
    if (n < 4) {
2376
        block[0] = dcdiff * s->y_dc_scale;
2377
    } else {
2378
        block[0] = dcdiff * s->c_dc_scale;
2379
    }
2380
    /* Skip ? */
2381
    run_diff = 0;
2382
    i = 0;
2383
    if (!coded) {
2384
        goto not_coded;
2385
    }
2386

    
2387
    //AC Decoding
2388
    i = 1;
2389

    
2390
    {
2391
        int last = 0, skip, value;
2392
        const int8_t *zz_table;
2393
        int scale;
2394
        int k;
2395

    
2396
        scale = v->pq * 2 + v->halfpq;
2397

    
2398
        if(v->s.ac_pred) {
2399
            if(!dc_pred_dir)
2400
                zz_table = ff_vc1_horizontal_zz;
2401
            else
2402
                zz_table = ff_vc1_vertical_zz;
2403
        } else
2404
            zz_table = ff_vc1_normal_zz;
2405

    
2406
        ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2407
        ac_val2 = ac_val;
2408
        if(dc_pred_dir) //left
2409
            ac_val -= 16;
2410
        else //top
2411
            ac_val -= 16 * s->block_wrap[n];
2412

    
2413
        while (!last) {
2414
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2415
            i += skip;
2416
            if(i > 63)
2417
                break;
2418
            block[zz_table[i++]] = value;
2419
        }
2420

    
2421
        /* apply AC prediction if needed */
2422
        if(s->ac_pred) {
2423
            if(dc_pred_dir) { //left
2424
                for(k = 1; k < 8; k++)
2425
                    block[k << 3] += ac_val[k];
2426
            } else { //top
2427
                for(k = 1; k < 8; k++)
2428
                    block[k] += ac_val[k + 8];
2429
            }
2430
        }
2431
        /* save AC coeffs for further prediction */
2432
        for(k = 1; k < 8; k++) {
2433
            ac_val2[k] = block[k << 3];
2434
            ac_val2[k + 8] = block[k];
2435
        }
2436

    
2437
        /* scale AC coeffs */
2438
        for(k = 1; k < 64; k++)
2439
            if(block[k]) {
2440
                block[k] *= scale;
2441
                if(!v->pquantizer)
2442
                    block[k] += (block[k] < 0) ? -v->pq : v->pq;
2443
            }
2444

    
2445
        if(s->ac_pred) i = 63;
2446
    }
2447

    
2448
not_coded:
2449
    if(!coded) {
2450
        int k, scale;
2451
        ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2452
        ac_val2 = ac_val;
2453

    
2454
        scale = v->pq * 2 + v->halfpq;
2455
        memset(ac_val2, 0, 16 * 2);
2456
        if(dc_pred_dir) {//left
2457
            ac_val -= 16;
2458
            if(s->ac_pred)
2459
                memcpy(ac_val2, ac_val, 8 * 2);
2460
        } else {//top
2461
            ac_val -= 16 * s->block_wrap[n];
2462
            if(s->ac_pred)
2463
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2464
        }
2465

    
2466
        /* apply AC prediction if needed */
2467
        if(s->ac_pred) {
2468
            if(dc_pred_dir) { //left
2469
                for(k = 1; k < 8; k++) {
2470
                    block[k << 3] = ac_val[k] * scale;
2471
                    if(!v->pquantizer && block[k << 3])
2472
                        block[k << 3] += (block[k << 3] < 0) ? -v->pq : v->pq;
2473
                }
2474
            } else { //top
2475
                for(k = 1; k < 8; k++) {
2476
                    block[k] = ac_val[k + 8] * scale;
2477
                    if(!v->pquantizer && block[k])
2478
                        block[k] += (block[k] < 0) ? -v->pq : v->pq;
2479
                }
2480
            }
2481
            i = 63;
2482
        }
2483
    }
2484
    s->block_last_index[n] = i;
2485

    
2486
    return 0;
2487
}
2488

    
2489
/** Decode intra block in intra frames - should be faster than decode_intra_block
2490
 * @param v VC1Context
2491
 * @param block block to decode
2492
 * @param coded are AC coeffs present or not
2493
 * @param codingset set of VLC to decode data
2494
 */
2495
static int vc1_decode_i_block_adv(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset, int mquant)
2496
{
2497
    GetBitContext *gb = &v->s.gb;
2498
    MpegEncContext *s = &v->s;
2499
    int dc_pred_dir = 0; /* Direction of the DC prediction used */
2500
    int run_diff, i;
2501
    int16_t *dc_val;
2502
    int16_t *ac_val, *ac_val2;
2503
    int dcdiff;
2504
    int a_avail = v->a_avail, c_avail = v->c_avail;
2505
    int use_pred = s->ac_pred;
2506
    int scale;
2507
    int q1, q2 = 0;
2508
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2509

    
2510
    /* Get DC differential */
2511
    if (n < 4) {
2512
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2513
    } else {
2514
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2515
    }
2516
    if (dcdiff < 0){
2517
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2518
        return -1;
2519
    }
2520
    if (dcdiff)
2521
    {
2522
        if (dcdiff == 119 /* ESC index value */)
2523
        {
2524
            /* TODO: Optimize */
2525
            if (mquant == 1) dcdiff = get_bits(gb, 10);
2526
            else if (mquant == 2) dcdiff = get_bits(gb, 9);
2527
            else dcdiff = get_bits(gb, 8);
2528
        }
2529
        else
2530
        {
2531
            if (mquant == 1)
2532
                dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
2533
            else if (mquant == 2)
2534
                dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
2535
        }
2536
        if (get_bits(gb, 1))
2537
            dcdiff = -dcdiff;
2538
    }
2539

    
2540
    /* Prediction */
2541
    dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
2542
    *dc_val = dcdiff;
2543

    
2544
    /* Store the quantized DC coeff, used for prediction */
2545
    if (n < 4) {
2546
        block[0] = dcdiff * s->y_dc_scale;
2547
    } else {
2548
        block[0] = dcdiff * s->c_dc_scale;
2549
    }
2550
    /* Skip ? */
2551
    run_diff = 0;
2552
    i = 0;
2553

    
2554
    //AC Decoding
2555
    i = 1;
2556

    
2557
    /* check if AC is needed at all */
2558
    if(!a_avail && !c_avail) use_pred = 0;
2559
    ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2560
    ac_val2 = ac_val;
2561

    
2562
    scale = mquant * 2 + v->halfpq;
2563

    
2564
    if(dc_pred_dir) //left
2565
        ac_val -= 16;
2566
    else //top
2567
        ac_val -= 16 * s->block_wrap[n];
2568

    
2569
    q1 = s->current_picture.qscale_table[mb_pos];
2570
    if(dc_pred_dir && c_avail && mb_pos) q2 = s->current_picture.qscale_table[mb_pos - 1];
2571
    if(!dc_pred_dir && a_avail && mb_pos >= s->mb_stride) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2572
    if(dc_pred_dir && n==1) q2 = q1;
2573
    if(!dc_pred_dir && n==2) q2 = q1;
2574
    if(n==3) q2 = q1;
2575

    
2576
    if(coded) {
2577
        int last = 0, skip, value;
2578
        const int8_t *zz_table;
2579
        int k;
2580

    
2581
        if(v->s.ac_pred) {
2582
            if(!dc_pred_dir)
2583
                zz_table = ff_vc1_horizontal_zz;
2584
            else
2585
                zz_table = ff_vc1_vertical_zz;
2586
        } else
2587
            zz_table = ff_vc1_normal_zz;
2588

    
2589
        while (!last) {
2590
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2591
            i += skip;
2592
            if(i > 63)
2593
                break;
2594
            block[zz_table[i++]] = value;
2595
        }
2596

    
2597
        /* apply AC prediction if needed */
2598
        if(use_pred) {
2599
            /* scale predictors if needed*/
2600
            if(q2 && q1!=q2) {
2601
                q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2602
                q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2603

    
2604
                if(dc_pred_dir) { //left
2605
                    for(k = 1; k < 8; k++)
2606
                        block[k << 3] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2607
                } else { //top
2608
                    for(k = 1; k < 8; k++)
2609
                        block[k] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2610
                }
2611
            } else {
2612
                if(dc_pred_dir) { //left
2613
                    for(k = 1; k < 8; k++)
2614
                        block[k << 3] += ac_val[k];
2615
                } else { //top
2616
                    for(k = 1; k < 8; k++)
2617
                        block[k] += ac_val[k + 8];
2618
                }
2619
            }
2620
        }
2621
        /* save AC coeffs for further prediction */
2622
        for(k = 1; k < 8; k++) {
2623
            ac_val2[k] = block[k << 3];
2624
            ac_val2[k + 8] = block[k];
2625
        }
2626

    
2627
        /* scale AC coeffs */
2628
        for(k = 1; k < 64; k++)
2629
            if(block[k]) {
2630
                block[k] *= scale;
2631
                if(!v->pquantizer)
2632
                    block[k] += (block[k] < 0) ? -mquant : mquant;
2633
            }
2634

    
2635
        if(use_pred) i = 63;
2636
    } else { // no AC coeffs
2637
        int k;
2638

    
2639
        memset(ac_val2, 0, 16 * 2);
2640
        if(dc_pred_dir) {//left
2641
            if(use_pred) {
2642
                memcpy(ac_val2, ac_val, 8 * 2);
2643
                if(q2 && q1!=q2) {
2644
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2645
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2646
                    for(k = 1; k < 8; k++)
2647
                        ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2648
                }
2649
            }
2650
        } else {//top
2651
            if(use_pred) {
2652
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2653
                if(q2 && q1!=q2) {
2654
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2655
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2656
                    for(k = 1; k < 8; k++)
2657
                        ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2658
                }
2659
            }
2660
        }
2661

    
2662
        /* apply AC prediction if needed */
2663
        if(use_pred) {
2664
            if(dc_pred_dir) { //left
2665
                for(k = 1; k < 8; k++) {
2666
                    block[k << 3] = ac_val2[k] * scale;
2667
                    if(!v->pquantizer && block[k << 3])
2668
                        block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
2669
                }
2670
            } else { //top
2671
                for(k = 1; k < 8; k++) {
2672
                    block[k] = ac_val2[k + 8] * scale;
2673
                    if(!v->pquantizer && block[k])
2674
                        block[k] += (block[k] < 0) ? -mquant : mquant;
2675
                }
2676
            }
2677
            i = 63;
2678
        }
2679
    }
2680
    s->block_last_index[n] = i;
2681

    
2682
    return 0;
2683
}
2684

    
2685
/** Decode intra block in inter frames - more generic version than vc1_decode_i_block
2686
 * @param v VC1Context
2687
 * @param block block to decode
2688
 * @param coded are AC coeffs present or not
2689
 * @param mquant block quantizer
2690
 * @param codingset set of VLC to decode data
2691
 */
2692
static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n, int coded, int mquant, int codingset)
2693
{
2694
    GetBitContext *gb = &v->s.gb;
2695
    MpegEncContext *s = &v->s;
2696
    int dc_pred_dir = 0; /* Direction of the DC prediction used */
2697
    int run_diff, i;
2698
    int16_t *dc_val;
2699
    int16_t *ac_val, *ac_val2;
2700
    int dcdiff;
2701
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2702
    int a_avail = v->a_avail, c_avail = v->c_avail;
2703
    int use_pred = s->ac_pred;
2704
    int scale;
2705
    int q1, q2 = 0;
2706

    
2707
    /* XXX: Guard against dumb values of mquant */
2708
    mquant = (mquant < 1) ? 0 : ( (mquant>31) ? 31 : mquant );
2709

    
2710
    /* Set DC scale - y and c use the same */
2711
    s->y_dc_scale = s->y_dc_scale_table[mquant];
2712
    s->c_dc_scale = s->c_dc_scale_table[mquant];
2713

    
2714
    /* Get DC differential */
2715
    if (n < 4) {
2716
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2717
    } else {
2718
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2719
    }
2720
    if (dcdiff < 0){
2721
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2722
        return -1;
2723
    }
2724
    if (dcdiff)
2725
    {
2726
        if (dcdiff == 119 /* ESC index value */)
2727
        {
2728
            /* TODO: Optimize */
2729
            if (mquant == 1) dcdiff = get_bits(gb, 10);
2730
            else if (mquant == 2) dcdiff = get_bits(gb, 9);
2731
            else dcdiff = get_bits(gb, 8);
2732
        }
2733
        else
2734
        {
2735
            if (mquant == 1)
2736
                dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
2737
            else if (mquant == 2)
2738
                dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
2739
        }
2740
        if (get_bits(gb, 1))
2741
            dcdiff = -dcdiff;
2742
    }
2743

    
2744
    /* Prediction */
2745
    dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
2746
    *dc_val = dcdiff;
2747

    
2748
    /* Store the quantized DC coeff, used for prediction */
2749

    
2750
    if (n < 4) {
2751
        block[0] = dcdiff * s->y_dc_scale;
2752
    } else {
2753
        block[0] = dcdiff * s->c_dc_scale;
2754
    }
2755
    /* Skip ? */
2756
    run_diff = 0;
2757
    i = 0;
2758

    
2759
    //AC Decoding
2760
    i = 1;
2761

    
2762
    /* check if AC is needed at all and adjust direction if needed */
2763
    if(!a_avail) dc_pred_dir = 1;
2764
    if(!c_avail) dc_pred_dir = 0;
2765
    if(!a_avail && !c_avail) use_pred = 0;
2766
    ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2767
    ac_val2 = ac_val;
2768

    
2769
    scale = mquant * 2 + v->halfpq;
2770

    
2771
    if(dc_pred_dir) //left
2772
        ac_val -= 16;
2773
    else //top
2774
        ac_val -= 16 * s->block_wrap[n];
2775

    
2776
    q1 = s->current_picture.qscale_table[mb_pos];
2777
    if(dc_pred_dir && c_avail && mb_pos) q2 = s->current_picture.qscale_table[mb_pos - 1];
2778
    if(!dc_pred_dir && a_avail && mb_pos >= s->mb_stride) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2779
    if(dc_pred_dir && n==1) q2 = q1;
2780
    if(!dc_pred_dir && n==2) q2 = q1;
2781
    if(n==3) q2 = q1;
2782

    
2783
    if(coded) {
2784
        int last = 0, skip, value;
2785
        const int8_t *zz_table;
2786
        int k;
2787

    
2788
        zz_table = ff_vc1_simple_progressive_8x8_zz;
2789

    
2790
        while (!last) {
2791
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2792
            i += skip;
2793
            if(i > 63)
2794
                break;
2795
            block[zz_table[i++]] = value;
2796
        }
2797

    
2798
        /* apply AC prediction if needed */
2799
        if(use_pred) {
2800
            /* scale predictors if needed*/
2801
            if(q2 && q1!=q2) {
2802
                q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2803
                q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2804

    
2805
                if(dc_pred_dir) { //left
2806
                    for(k = 1; k < 8; k++)
2807
                        block[k << 3] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2808
                } else { //top
2809
                    for(k = 1; k < 8; k++)
2810
                        block[k] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2811
                }
2812
            } else {
2813
                if(dc_pred_dir) { //left
2814
                    for(k = 1; k < 8; k++)
2815
                        block[k << 3] += ac_val[k];
2816
                } else { //top
2817
                    for(k = 1; k < 8; k++)
2818
                        block[k] += ac_val[k + 8];
2819
                }
2820
            }
2821
        }
2822
        /* save AC coeffs for further prediction */
2823
        for(k = 1; k < 8; k++) {
2824
            ac_val2[k] = block[k << 3];
2825
            ac_val2[k + 8] = block[k];
2826
        }
2827

    
2828
        /* scale AC coeffs */
2829
        for(k = 1; k < 64; k++)
2830
            if(block[k]) {
2831
                block[k] *= scale;
2832
                if(!v->pquantizer)
2833
                    block[k] += (block[k] < 0) ? -mquant : mquant;
2834
            }
2835

    
2836
        if(use_pred) i = 63;
2837
    } else { // no AC coeffs
2838
        int k;
2839

    
2840
        memset(ac_val2, 0, 16 * 2);
2841
        if(dc_pred_dir) {//left
2842
            if(use_pred) {
2843
                memcpy(ac_val2, ac_val, 8 * 2);
2844
                if(q2 && q1!=q2) {
2845
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2846
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2847
                    for(k = 1; k < 8; k++)
2848
                        ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2849
                }
2850
            }
2851
        } else {//top
2852
            if(use_pred) {
2853
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2854
                if(q2 && q1!=q2) {
2855
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2856
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2857
                    for(k = 1; k < 8; k++)
2858
                        ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2859
                }
2860
            }
2861
        }
2862

    
2863
        /* apply AC prediction if needed */
2864
        if(use_pred) {
2865
            if(dc_pred_dir) { //left
2866
                for(k = 1; k < 8; k++) {
2867
                    block[k << 3] = ac_val2[k] * scale;
2868
                    if(!v->pquantizer && block[k << 3])
2869
                        block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
2870
                }
2871
            } else { //top
2872
                for(k = 1; k < 8; k++) {
2873
                    block[k] = ac_val2[k + 8] * scale;
2874
                    if(!v->pquantizer && block[k])
2875
                        block[k] += (block[k] < 0) ? -mquant : mquant;
2876
                }
2877
            }
2878
            i = 63;
2879
        }
2880
    }
2881
    s->block_last_index[n] = i;
2882

    
2883
    return 0;
2884
}
2885

    
2886
/** Decode P block
2887
 */
2888
static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n, int mquant, int ttmb, int first_block)
2889
{
2890
    MpegEncContext *s = &v->s;
2891
    GetBitContext *gb = &s->gb;
2892
    int i, j;
2893
    int subblkpat = 0;
2894
    int scale, off, idx, last, skip, value;
2895
    int ttblk = ttmb & 7;
2896

    
2897
    if(ttmb == -1) {
2898
        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)];
2899
    }
2900
    if(ttblk == TT_4X4) {
2901
        subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
2902
    }
2903
    if((ttblk != TT_8X8 && ttblk != TT_4X4) && (v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))) {
2904
        subblkpat = decode012(gb);
2905
        if(subblkpat) subblkpat ^= 3; //swap decoded pattern bits
2906
        if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) ttblk = TT_8X4;
2907
        if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) ttblk = TT_4X8;
2908
    }
2909
    scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
2910

    
2911
    // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
2912
    if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
2913
        subblkpat = 2 - (ttblk == TT_8X4_TOP);
2914
        ttblk = TT_8X4;
2915
    }
2916
    if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
2917
        subblkpat = 2 - (ttblk == TT_4X8_LEFT);
2918
        ttblk = TT_4X8;
2919
    }
2920
    switch(ttblk) {
2921
    case TT_8X8:
2922
        i = 0;
2923
        last = 0;
2924
        while (!last) {
2925
            vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2926
            i += skip;
2927
            if(i > 63)
2928
                break;
2929
            idx = ff_vc1_simple_progressive_8x8_zz[i++];
2930
            block[idx] = value * scale;
2931
            if(!v->pquantizer)
2932
                block[idx] += (block[idx] < 0) ? -mquant : mquant;
2933
        }
2934
        s->dsp.vc1_inv_trans_8x8(block);
2935
        break;
2936
    case TT_4X4:
2937
        for(j = 0; j < 4; j++) {
2938
            last = subblkpat & (1 << (3 - j));
2939
            i = 0;
2940
            off = (j & 1) * 4 + (j & 2) * 16;
2941
            while (!last) {
2942
                vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2943
                i += skip;
2944
                if(i > 15)
2945
                    break;
2946
                idx = ff_vc1_simple_progressive_4x4_zz[i++];
2947
                block[idx + off] = value * scale;
2948
                if(!v->pquantizer)
2949
                    block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
2950
            }
2951
            if(!(subblkpat & (1 << (3 - j))))
2952
                s->dsp.vc1_inv_trans_4x4(block, j);
2953
        }
2954
        break;
2955
    case TT_8X4:
2956
        for(j = 0; j < 2; j++) {
2957
            last = subblkpat & (1 << (1 - j));
2958
            i = 0;
2959
            off = j * 32;
2960
            while (!last) {
2961
                vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2962
                i += skip;
2963
                if(i > 31)
2964
                    break;
2965
                if(v->profile < PROFILE_ADVANCED)
2966
                    idx = ff_vc1_simple_progressive_8x4_zz[i++];
2967
                else
2968
                    idx = ff_vc1_adv_progressive_8x4_zz[i++];
2969
                block[idx + off] = value * scale;
2970
                if(!v->pquantizer)
2971
                    block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
2972
            }
2973
            if(!(subblkpat & (1 << (1 - j))))
2974
                s->dsp.vc1_inv_trans_8x4(block, j);
2975
        }
2976
        break;
2977
    case TT_4X8:
2978
        for(j = 0; j < 2; j++) {
2979
            last = subblkpat & (1 << (1 - j));
2980
            i = 0;
2981
            off = j * 4;
2982
            while (!last) {
2983
                vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2984
                i += skip;
2985
                if(i > 31)
2986
                    break;
2987
                if(v->profile < PROFILE_ADVANCED)
2988
                    idx = ff_vc1_simple_progressive_4x8_zz[i++];
2989
                else
2990
                    idx = ff_vc1_adv_progressive_4x8_zz[i++];
2991
                block[idx + off] = value * scale;
2992
                if(!v->pquantizer)
2993
                    block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
2994
            }
2995
            if(!(subblkpat & (1 << (1 - j))))
2996
                s->dsp.vc1_inv_trans_4x8(block, j);
2997
        }
2998
        break;
2999
    }
3000
    return 0;
3001
}
3002

    
3003

    
3004
/** Decode one P-frame MB (in Simple/Main profile)
3005
 */
3006
static int vc1_decode_p_mb(VC1Context *v)
3007
{
3008
    MpegEncContext *s = &v->s;
3009
    GetBitContext *gb = &s->gb;
3010
    int i, j;
3011
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3012
    int cbp; /* cbp decoding stuff */
3013
    int mqdiff, mquant; /* MB quantization */
3014
    int ttmb = v->ttfrm; /* MB Transform type */
3015
    int status;
3016

    
3017
    static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
3018
      offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3019
    int mb_has_coeffs = 1; /* last_flag */
3020
    int dmv_x, dmv_y; /* Differential MV components */
3021
    int index, index1; /* LUT indices */
3022
    int val, sign; /* temp values */
3023
    int first_block = 1;
3024
    int dst_idx, off;
3025
    int skipped, fourmv;
3026

    
3027
    mquant = v->pq; /* Loosy initialization */
3028

    
3029
    if (v->mv_type_is_raw)
3030
        fourmv = get_bits1(gb);
3031
    else
3032
        fourmv = v->mv_type_mb_plane[mb_pos];
3033
    if (v->skip_is_raw)
3034
        skipped = get_bits1(gb);
3035
    else
3036
        skipped = v->s.mbskip_table[mb_pos];
3037

    
3038
    s->dsp.clear_blocks(s->block[0]);
3039

    
3040
    if (!fourmv) /* 1MV mode */
3041
    {
3042
        if (!skipped)
3043
        {
3044
            GET_MVDATA(dmv_x, dmv_y);
3045

    
3046
            if (s->mb_intra) {
3047
                s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
3048
                s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
3049
            }
3050
            s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
3051
            vc1_pred_mv(s, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
3052

    
3053
            /* FIXME Set DC val for inter block ? */
3054
            if (s->mb_intra && !mb_has_coeffs)
3055
            {
3056
                GET_MQUANT();
3057
                s->ac_pred = get_bits(gb, 1);
3058
                cbp = 0;
3059
            }
3060
            else if (mb_has_coeffs)
3061
            {
3062
                if (s->mb_intra) s->ac_pred = get_bits(gb, 1);
3063
                cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3064
                GET_MQUANT();
3065
            }
3066
            else
3067
            {
3068
                mquant = v->pq;
3069
                cbp = 0;
3070
            }
3071
            s->current_picture.qscale_table[mb_pos] = mquant;
3072

    
3073
            if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3074
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
3075
                                VC1_TTMB_VLC_BITS, 2);
3076
            if(!s->mb_intra) vc1_mc_1mv(v, 0);
3077
            dst_idx = 0;
3078
            for (i=0; i<6; i++)
3079
            {
3080
                s->dc_val[0][s->block_index[i]] = 0;
3081
                dst_idx += i >> 2;
3082
                val = ((cbp >> (5 - i)) & 1);
3083
                off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3084
                v->mb_type[0][s->block_index[i]] = s->mb_intra;
3085
                if(s->mb_intra) {
3086
                    /* check if prediction blocks A and C are available */
3087
                    v->a_avail = v->c_avail = 0;
3088
                    if(i == 2 || i == 3 || !s->first_slice_line)
3089
                        v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3090
                    if(i == 1 || i == 3 || s->mb_x)
3091
                        v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3092

    
3093
                    vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
3094
                    if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3095
                    s->dsp.vc1_inv_trans_8x8(s->block[i]);
3096
                    if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
3097
                    for(j = 0; j < 64; j++) s->block[i][j] += 128;
3098
                    if(!v->res_fasttx && v->res_x8) for(j = 0; j < 64; j++) s->block[i][j] += 16;
3099
                    s->dsp.put_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
3100
                    if(v->pq >= 9 && v->overlap) {
3101
                        if(v->c_avail)
3102
                            s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
3103
                        if(v->a_avail)
3104
                            s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
3105
                    }
3106
                } else if(val) {
3107
                    vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block);
3108
                    if(!v->ttmbf && ttmb < 8) ttmb = -1;
3109
                    first_block = 0;
3110
                    if((i<4) || !(s->flags & CODEC_FLAG_GRAY))
3111
                        s->dsp.add_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
3112
                }
3113
            }
3114
        }
3115
        else //Skipped
3116
        {
3117
            s->mb_intra = 0;
3118
            for(i = 0; i < 6; i++) {
3119
                v->mb_type[0][s->block_index[i]] = 0;
3120
                s->dc_val[0][s->block_index[i]] = 0;
3121
            }
3122
            s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
3123
            s->current_picture.qscale_table[mb_pos] = 0;
3124
            vc1_pred_mv(s, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);
3125
            vc1_mc_1mv(v, 0);
3126
            return 0;
3127
        }
3128
    } //1MV mode
3129
    else //4MV mode
3130
    {
3131
        if (!skipped /* unskipped MB */)
3132
        {
3133
            int intra_count = 0, coded_inter = 0;
3134
            int is_intra[6], is_coded[6];
3135
            /* Get CBPCY */
3136
            cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3137
            for (i=0; i<6; i++)
3138
            {
3139
                val = ((cbp >> (5 - i)) & 1);
3140
                s->dc_val[0][s->block_index[i]] = 0;
3141
                s->mb_intra = 0;
3142
                if(i < 4) {
3143
                    dmv_x = dmv_y = 0;
3144
                    s->mb_intra = 0;
3145
                    mb_has_coeffs = 0;
3146
                    if(val) {
3147
                        GET_MVDATA(dmv_x, dmv_y);
3148
                    }
3149
                    vc1_pred_mv(s, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);
3150
                    if(!s->mb_intra) vc1_mc_4mv_luma(v, i);
3151
                    intra_count += s->mb_intra;
3152
                    is_intra[i] = s->mb_intra;
3153
                    is_coded[i] = mb_has_coeffs;
3154
                }
3155
                if(i&4){
3156
                    is_intra[i] = (intra_count >= 3);
3157
                    is_coded[i] = val;
3158
                }
3159
                if(i == 4) vc1_mc_4mv_chroma(v);
3160
                v->mb_type[0][s->block_index[i]] = is_intra[i];
3161
                if(!coded_inter) coded_inter = !is_intra[i] & is_coded[i];
3162
            }
3163
            // if there are no coded blocks then don't do anything more
3164
            if(!intra_count && !coded_inter) return 0;
3165
            dst_idx = 0;
3166
            GET_MQUANT();
3167
            s->current_picture.qscale_table[mb_pos] = mquant;
3168
            /* test if block is intra and has pred */
3169
            {
3170
                int intrapred = 0;
3171
                for(i=0; i<6; i++)
3172
                    if(is_intra[i]) {
3173
                        if(((!s->first_slice_line || (i==2 || i==3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
3174
                            || ((s->mb_x || (i==1 || i==3)) && v->mb_type[0][s->block_index[i] - 1])) {
3175
                            intrapred = 1;
3176
                            break;
3177
                        }
3178
                    }
3179
                if(intrapred)s->ac_pred = get_bits(gb, 1);
3180
                else s->ac_pred = 0;
3181
            }
3182
            if (!v->ttmbf && coded_inter)
3183
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3184
            for (i=0; i<6; i++)
3185
            {
3186
                dst_idx += i >> 2;
3187
                off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3188
                s->mb_intra = is_intra[i];
3189
                if (is_intra[i]) {
3190
                    /* check if prediction blocks A and C are available */
3191
                    v->a_avail = v->c_avail = 0;
3192
                    if(i == 2 || i == 3 || !s->first_slice_line)
3193
                        v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3194
                    if(i == 1 || i == 3 || s->mb_x)
3195
                        v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3196

    
3197
                    vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant, (i&4)?v->codingset2:v->codingset);
3198
                    if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3199
                    s->dsp.vc1_inv_trans_8x8(s->block[i]);
3200
                    if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
3201
                    for(j = 0; j < 64; j++) s->block[i][j] += 128;
3202
                    if(!v->res_fasttx && v->res_x8) for(j = 0; j < 64; j++) s->block[i][j] += 16;
3203
                    s->dsp.put_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
3204
                    if(v->pq >= 9 && v->overlap) {
3205
                        if(v->c_avail)
3206
                            s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
3207
                        if(v->a_avail)
3208
                            s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
3209
                    }
3210
                } else if(is_coded[i]) {
3211
                    status = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block);
3212
                    if(!v->ttmbf && ttmb < 8) ttmb = -1;
3213
                    first_block = 0;
3214
                    if((i<4) || !(s->flags & CODEC_FLAG_GRAY))
3215
                        s->dsp.add_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
3216
                }
3217
            }
3218
            return status;
3219
        }
3220
        else //Skipped MB
3221
        {
3222
            s->mb_intra = 0;
3223
            s->current_picture.qscale_table[mb_pos] = 0;
3224
            for (i=0; i<6; i++) {
3225
                v->mb_type[0][s->block_index[i]] = 0;
3226
                s->dc_val[0][s->block_index[i]] = 0;
3227
            }
3228
            for (i=0; i<4; i++)
3229
            {
3230
                vc1_pred_mv(s, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0]);
3231
                vc1_mc_4mv_luma(v, i);
3232
            }
3233
            vc1_mc_4mv_chroma(v);
3234
            s->current_picture.qscale_table[mb_pos] = 0;
3235
            return 0;
3236
        }
3237
    }
3238

    
3239
    /* Should never happen */
3240
    return -1;
3241
}
3242

    
3243
/** Decode one B-frame MB (in Main profile)
3244
 */
3245
static void vc1_decode_b_mb(VC1Context *v)
3246
{
3247
    MpegEncContext *s = &v->s;
3248
    GetBitContext *gb = &s->gb;
3249
    int i, j;
3250
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3251
    int cbp = 0; /* cbp decoding stuff */
3252
    int mqdiff, mquant; /* MB quantization */
3253
    int ttmb = v->ttfrm; /* MB Transform type */
3254

    
3255
    static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
3256
      offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3257
    int mb_has_coeffs = 0; /* last_flag */
3258
    int index, index1; /* LUT indices */
3259
    int val, sign; /* temp values */
3260
    int first_block = 1;
3261
    int dst_idx, off;
3262
    int skipped, direct;
3263
    int dmv_x[2], dmv_y[2];
3264
    int bmvtype = BMV_TYPE_BACKWARD;
3265

    
3266
    mquant = v->pq; /* Loosy initialization */
3267
    s->mb_intra = 0;
3268

    
3269
    if (v->dmb_is_raw)
3270
        direct = get_bits1(gb);
3271
    else
3272
        direct = v->direct_mb_plane[mb_pos];
3273
    if (v->skip_is_raw)
3274
        skipped = get_bits1(gb);
3275
    else
3276
        skipped = v->s.mbskip_table[mb_pos];
3277

    
3278
    s->dsp.clear_blocks(s->block[0]);
3279
    dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
3280
    for(i = 0; i < 6; i++) {
3281
        v->mb_type[0][s->block_index[i]] = 0;
3282
        s->dc_val[0][s->block_index[i]] = 0;
3283
    }
3284
    s->current_picture.qscale_table[mb_pos] = 0;
3285

    
3286
    if (!direct) {
3287
        if (!skipped) {
3288
            GET_MVDATA(dmv_x[0], dmv_y[0]);
3289
            dmv_x[1] = dmv_x[0];
3290
            dmv_y[1] = dmv_y[0];
3291
        }
3292
        if(skipped || !s->mb_intra) {
3293
            bmvtype = decode012(gb);
3294
            switch(bmvtype) {
3295
            case 0:
3296
                bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
3297
                break;
3298
            case 1:
3299
                bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
3300
                break;
3301
            case 2:
3302
                bmvtype = BMV_TYPE_INTERPOLATED;
3303
                dmv_x[0] = dmv_y[0] = 0;
3304
            }
3305
        }
3306
    }
3307
    for(i = 0; i < 6; i++)
3308
        v->mb_type[0][s->block_index[i]] = s->mb_intra;
3309

    
3310
    if (skipped) {
3311
        if(direct) bmvtype = BMV_TYPE_INTERPOLATED;
3312
        vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
3313
        vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3314
        return;
3315
    }
3316
    if (direct) {
3317
        cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3318
        GET_MQUANT();
3319
        s->mb_intra = 0;
3320
        mb_has_coeffs = 0;
3321
        s->current_picture.qscale_table[mb_pos] = mquant;
3322
        if(!v->ttmbf)
3323
            ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3324
        dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
3325
        vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
3326
        vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3327
    } else {
3328
        if(!mb_has_coeffs && !s->mb_intra) {
3329
            /* no coded blocks - effectively skipped */
3330
            vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
3331
            vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3332
            return;
3333
        }
3334
        if(s->mb_intra && !mb_has_coeffs) {
3335
            GET_MQUANT();
3336
            s->current_picture.qscale_table[mb_pos] = mquant;
3337
            s->ac_pred = get_bits1(gb);
3338
            cbp = 0;
3339
            vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
3340
        } else {
3341
            if(bmvtype == BMV_TYPE_INTERPOLATED) {
3342
                GET_MVDATA(dmv_x[0], dmv_y[0]);
3343
                if(!mb_has_coeffs) {
3344
                    /* interpolated skipped block */
3345
                    vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
3346
                    vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3347
                    return;
3348
                }
3349
            }
3350
            vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
3351
            if(!s->mb_intra) {
3352
                vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3353
            }
3354
            if(s->mb_intra)
3355
                s->ac_pred = get_bits1(gb);
3356
            cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3357
            GET_MQUANT();
3358
            s->current_picture.qscale_table[mb_pos] = mquant;
3359
            if(!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3360
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3361
        }
3362
    }
3363
    dst_idx = 0;
3364
    for (i=0; i<6; i++)
3365
    {
3366
        s->dc_val[0][s->block_index[i]] = 0;
3367
        dst_idx += i >> 2;
3368
        val = ((cbp >> (5 - i)) & 1);
3369
        off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3370
        v->mb_type[0][s->block_index[i]] = s->mb_intra;
3371
        if(s->mb_intra) {
3372
            /* check if prediction blocks A and C are available */
3373
            v->a_avail = v->c_avail = 0;
3374
            if(i == 2 || i == 3 || !s->first_slice_line)
3375
                v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3376
            if(i == 1 || i == 3 || s->mb_x)
3377
                v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3378

    
3379
            vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
3380
            if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3381
            s->dsp.vc1_inv_trans_8x8(s->block[i]);
3382
            if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
3383
            for(j = 0; j < 64; j++) s->block[i][j] += 128;
3384
            s->dsp.put_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
3385
        } else if(val) {
3386
            vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block);
3387
            if(!v->ttmbf && ttmb < 8) ttmb = -1;
3388
            first_block = 0;
3389
            if((i<4) || !(s->flags & CODEC_FLAG_GRAY))
3390
                s->dsp.add_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
3391
        }
3392
    }
3393
}
3394

    
3395
/** Decode blocks of I-frame
3396
 */
3397
static void vc1_decode_i_blocks(VC1Context *v)
3398
{
3399
    int k, j;
3400
    MpegEncContext *s = &v->s;
3401
    int cbp, val;
3402
    uint8_t *coded_val;
3403
    int mb_pos;
3404

    
3405
    /* select codingmode used for VLC tables selection */
3406
    switch(v->y_ac_table_index){
3407
    case 0:
3408
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
3409
        break;
3410
    case 1:
3411
        v->codingset = CS_HIGH_MOT_INTRA;
3412
        break;
3413
    case 2:
3414
        v->codingset = CS_MID_RATE_INTRA;
3415
        break;
3416
    }
3417

    
3418
    switch(v->c_ac_table_index){
3419
    case 0:
3420
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
3421
        break;
3422
    case 1:
3423
        v->codingset2 = CS_HIGH_MOT_INTER;
3424
        break;
3425
    case 2:
3426
        v->codingset2 = CS_MID_RATE_INTER;
3427
        break;
3428
    }
3429

    
3430
    /* Set DC scale - y and c use the same */
3431
    s->y_dc_scale = s->y_dc_scale_table[v->pq];
3432
    s->c_dc_scale = s->c_dc_scale_table[v->pq];
3433

    
3434
    //do frame decode
3435
    s->mb_x = s->mb_y = 0;
3436
    s->mb_intra = 1;
3437
    s->first_slice_line = 1;
3438
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3439
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3440
        for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
3441
            ff_init_block_index(s);
3442
            ff_update_block_index(s);
3443
            s->dsp.clear_blocks(s->block[0]);
3444
            mb_pos = s->mb_x + s->mb_y * s->mb_width;
3445
            s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
3446
            s->current_picture.qscale_table[mb_pos] = v->pq;
3447
            s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
3448
            s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
3449

    
3450
            // do actual MB decoding and displaying
3451
            cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
3452
            v->s.ac_pred = get_bits(&v->s.gb, 1);
3453

    
3454
            for(k = 0; k < 6; k++) {
3455
                val = ((cbp >> (5 - k)) & 1);
3456

    
3457
                if (k < 4) {
3458
                    int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
3459
                    val = val ^ pred;
3460
                    *coded_val = val;
3461
                }
3462
                cbp |= val << (5 - k);
3463

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

    
3466
                s->dsp.vc1_inv_trans_8x8(s->block[k]);
3467
                if(!v->res_fasttx && !v->res_x8) for(j = 0; j < 64; j++) s->block[k][j] -= 16;
3468
                if(v->pq >= 9 && v->overlap) {
3469
                    for(j = 0; j < 64; j++) s->block[k][j] += 128;
3470
                }
3471
            }
3472

    
3473
            vc1_put_block(v, s->block);
3474
            if(v->pq >= 9 && v->overlap) {
3475
                if(s->mb_x) {
3476
                    s->dsp.vc1_h_overlap(s->dest[0], s->linesize);
3477
                    s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
3478
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
3479
                        s->dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
3480
                        s->dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
3481
                    }
3482
                }
3483
                s->dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
3484
                s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
3485
                if(!s->first_slice_line) {
3486
                    s->dsp.vc1_v_overlap(s->dest[0], s->linesize);
3487
                    s->dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
3488
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
3489
                        s->dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
3490
                        s->dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
3491
                    }
3492
                }
3493
                s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
3494
                s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
3495
            }
3496

    
3497
            if(get_bits_count(&s->gb) > v->bits) {
3498
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
3499
                return;
3500
            }
3501
        }
3502
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3503
        s->first_slice_line = 0;
3504
    }
3505
}
3506

    
3507
/** Decode blocks of I-frame for advanced profile
3508
 */
3509
static void vc1_decode_i_blocks_adv(VC1Context *v)
3510
{
3511
    int k, j;
3512
    MpegEncContext *s = &v->s;
3513
    int cbp, val;
3514
    uint8_t *coded_val;
3515
    int mb_pos;
3516
    int mquant = v->pq;
3517
    int mqdiff;
3518
    int overlap;
3519
    GetBitContext *gb = &s->gb;
3520

    
3521
    /* select codingmode used for VLC tables selection */
3522
    switch(v->y_ac_table_index){
3523
    case 0:
3524
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
3525
        break;
3526
    case 1:
3527
        v->codingset = CS_HIGH_MOT_INTRA;
3528
        break;
3529
    case 2:
3530
        v->codingset = CS_MID_RATE_INTRA;
3531
        break;
3532
    }
3533

    
3534
    switch(v->c_ac_table_index){
3535
    case 0:
3536
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
3537
        break;
3538
    case 1:
3539
        v->codingset2 = CS_HIGH_MOT_INTER;
3540
        break;
3541
    case 2:
3542
        v->codingset2 = CS_MID_RATE_INTER;
3543
        break;
3544
    }
3545

    
3546
    //do frame decode
3547
    s->mb_x = s->mb_y = 0;
3548
    s->mb_intra = 1;
3549
    s->first_slice_line = 1;
3550
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3551
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3552
        for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
3553
            ff_init_block_index(s);
3554
            ff_update_block_index(s);
3555
            s->dsp.clear_blocks(s->block[0]);
3556
            mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3557
            s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
3558
            s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
3559
            s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
3560

    
3561
            // do actual MB decoding and displaying
3562
            cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
3563
            if(v->acpred_is_raw)
3564
                v->s.ac_pred = get_bits(&v->s.gb, 1);
3565
            else
3566
                v->s.ac_pred = v->acpred_plane[mb_pos];
3567

    
3568
            if(v->condover == CONDOVER_SELECT) {
3569
                if(v->overflg_is_raw)
3570
                    overlap = get_bits(&v->s.gb, 1);
3571
                else
3572
                    overlap = v->over_flags_plane[mb_pos];
3573
            } else
3574
                overlap = (v->condover == CONDOVER_ALL);
3575

    
3576
            GET_MQUANT();
3577

    
3578
            s->current_picture.qscale_table[mb_pos] = mquant;
3579
            /* Set DC scale - y and c use the same */
3580
            s->y_dc_scale = s->y_dc_scale_table[mquant];
3581
            s->c_dc_scale = s->c_dc_scale_table[mquant];
3582

    
3583
            for(k = 0; k < 6; k++) {
3584
                val = ((cbp >> (5 - k)) & 1);
3585

    
3586
                if (k < 4) {
3587
                    int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
3588
                    val = val ^ pred;
3589
                    *coded_val = val;
3590
                }
3591
                cbp |= val << (5 - k);
3592

    
3593
                v->a_avail = !s->first_slice_line || (k==2 || k==3);
3594
                v->c_avail = !!s->mb_x || (k==1 || k==3);
3595

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

    
3598
                s->dsp.vc1_inv_trans_8x8(s->block[k]);
3599
                for(j = 0; j < 64; j++) s->block[k][j] += 128;
3600
            }
3601

    
3602
            vc1_put_block(v, s->block);
3603
            if(overlap) {
3604
                if(s->mb_x) {
3605
                    s->dsp.vc1_h_overlap(s->dest[0], s->linesize);
3606
                    s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
3607
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
3608
                        s->dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
3609
                        s->dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
3610
                    }
3611
                }
3612
                s->dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
3613
                s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
3614
                if(!s->first_slice_line) {
3615
                    s->dsp.vc1_v_overlap(s->dest[0], s->linesize);
3616
                    s->dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
3617
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
3618
                        s->dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
3619
                        s->dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
3620
                    }
3621
                }
3622
                s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
3623
                s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
3624
            }
3625

    
3626
            if(get_bits_count(&s->gb) > v->bits) {
3627
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
3628
                return;
3629
            }
3630
        }
3631
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3632
        s->first_slice_line = 0;
3633
    }
3634
}
3635

    
3636
static void vc1_decode_p_blocks(VC1Context *v)
3637
{
3638
    MpegEncContext *s = &v->s;
3639

    
3640
    /* select codingmode used for VLC tables selection */
3641
    switch(v->c_ac_table_index){
3642
    case 0:
3643
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
3644
        break;
3645
    case 1:
3646
        v->codingset = CS_HIGH_MOT_INTRA;
3647
        break;
3648
    case 2:
3649
        v->codingset = CS_MID_RATE_INTRA;
3650
        break;
3651
    }
3652

    
3653
    switch(v->c_ac_table_index){
3654
    case 0:
3655
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
3656
        break;
3657
    case 1:
3658
        v->codingset2 = CS_HIGH_MOT_INTER;
3659
        break;
3660
    case 2:
3661
        v->codingset2 = CS_MID_RATE_INTER;
3662
        break;
3663
    }
3664

    
3665
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3666
    s->first_slice_line = 1;
3667
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3668
        for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
3669
            ff_init_block_index(s);
3670
            ff_update_block_index(s);
3671
            s->dsp.clear_blocks(s->block[0]);
3672

    
3673
            vc1_decode_p_mb(v);
3674
            if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
3675
                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);
3676
                return;
3677
            }
3678
        }
3679
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3680
        s->first_slice_line = 0;
3681
    }
3682
}
3683

    
3684
static void vc1_decode_b_blocks(VC1Context *v)
3685
{
3686
    MpegEncContext *s = &v->s;
3687

    
3688
    /* select codingmode used for VLC tables selection */
3689
    switch(v->c_ac_table_index){
3690
    case 0:
3691
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
3692
        break;
3693
    case 1:
3694
        v->codingset = CS_HIGH_MOT_INTRA;
3695
        break;
3696
    case 2:
3697
        v->codingset = CS_MID_RATE_INTRA;
3698
        break;
3699
    }
3700

    
3701
    switch(v->c_ac_table_index){
3702
    case 0:
3703
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
3704
        break;
3705
    case 1:
3706
        v->codingset2 = CS_HIGH_MOT_INTER;
3707
        break;
3708
    case 2:
3709
        v->codingset2 = CS_MID_RATE_INTER;
3710
        break;
3711
    }
3712

    
3713
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3714
    s->first_slice_line = 1;
3715
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3716
        for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
3717
            ff_init_block_index(s);
3718
            ff_update_block_index(s);
3719
            s->dsp.clear_blocks(s->block[0]);
3720

    
3721
            vc1_decode_b_mb(v);
3722
            if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
3723
                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);
3724
                return;
3725
            }
3726
        }
3727
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3728
        s->first_slice_line = 0;
3729
    }
3730
}
3731

    
3732
static void vc1_decode_skip_blocks(VC1Context *v)
3733
{
3734
    MpegEncContext *s = &v->s;
3735

    
3736
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3737
    s->first_slice_line = 1;
3738
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3739
        s->mb_x = 0;
3740
        ff_init_block_index(s);
3741
        ff_update_block_index(s);
3742
        memcpy(s->dest[0], s->last_picture.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
3743
        memcpy(s->dest[1], s->last_picture.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
3744
        memcpy(s->dest[2], s->last_picture.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
3745
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3746
        s->first_slice_line = 0;
3747
    }
3748
    s->pict_type = P_TYPE;
3749
}
3750

    
3751
static void vc1_decode_blocks(VC1Context *v)
3752
{
3753

    
3754
    v->s.esc3_level_length = 0;
3755

    
3756
    switch(v->s.pict_type) {
3757
    case I_TYPE:
3758
        if(v->profile == PROFILE_ADVANCED)
3759
            vc1_decode_i_blocks_adv(v);
3760
        else
3761
            vc1_decode_i_blocks(v);
3762
        break;
3763
    case P_TYPE:
3764
        if(v->p_frame_skipped)
3765
            vc1_decode_skip_blocks(v);
3766
        else
3767
            vc1_decode_p_blocks(v);
3768
        break;
3769
    case B_TYPE:
3770
        if(v->bi_type){
3771
            if(v->profile == PROFILE_ADVANCED)
3772
                vc1_decode_i_blocks_adv(v);
3773
            else
3774
                vc1_decode_i_blocks(v);
3775
        }else
3776
            vc1_decode_b_blocks(v);
3777
        break;
3778
    }
3779
}
3780

    
3781
/** Find VC-1 marker in buffer
3782
 * @return position where next marker starts or end of buffer if no marker found
3783
 */
3784
static av_always_inline uint8_t* find_next_marker(uint8_t *src, uint8_t *end)
3785
{
3786
    uint32_t mrk = 0xFFFFFFFF;
3787

    
3788
    if(end-src < 4) return end;
3789
    while(src < end){
3790
        mrk = (mrk << 8) | *src++;
3791
        if(IS_MARKER(mrk))
3792
            return src-4;
3793
    }
3794
    return end;
3795
}
3796

    
3797
static av_always_inline int vc1_unescape_buffer(uint8_t *src, int size, uint8_t *dst)
3798
{
3799
    int dsize = 0, i;
3800

    
3801
    if(size < 4){
3802
        for(dsize = 0; dsize < size; dsize++) *dst++ = *src++;
3803
        return size;
3804
    }
3805
    for(i = 0; i < size; i++, src++) {
3806
        if(src[0] == 3 && i >= 2 && !src[-1] && !src[-2] && i < size-1 && src[1] < 4) {
3807
            dst[dsize++] = src[1];
3808
            src++;
3809
            i++;
3810
        } else
3811
            dst[dsize++] = *src;
3812
    }
3813
    return dsize;
3814
}
3815

    
3816
/** Initialize a VC1/WMV3 decoder
3817
 * @todo TODO: Handle VC-1 IDUs (Transport level?)
3818
 * @todo TODO: Decypher remaining bits in extra_data
3819
 */
3820
static int vc1_decode_init(AVCodecContext *avctx)
3821
{
3822
    VC1Context *v = avctx->priv_data;
3823
    MpegEncContext *s = &v->s;
3824
    GetBitContext gb;
3825

    
3826
    if (!avctx->extradata_size || !avctx->extradata) return -1;
3827
    if (!(avctx->flags & CODEC_FLAG_GRAY))
3828
        avctx->pix_fmt = PIX_FMT_YUV420P;
3829
    else
3830
        avctx->pix_fmt = PIX_FMT_GRAY8;
3831
    v->s.avctx = avctx;
3832
    avctx->flags |= CODEC_FLAG_EMU_EDGE;
3833
    v->s.flags |= CODEC_FLAG_EMU_EDGE;
3834

    
3835
    if(ff_h263_decode_init(avctx) < 0)
3836
        return -1;
3837
    if (vc1_init_common(v) < 0) return -1;
3838

    
3839
    avctx->coded_width = avctx->width;
3840
    avctx->coded_height = avctx->height;
3841
    if (avctx->codec_id == CODEC_ID_WMV3)
3842
    {
3843
        int count = 0;
3844

    
3845
        // looks like WMV3 has a sequence header stored in the extradata
3846
        // advanced sequence header may be before the first frame
3847
        // the last byte of the extradata is a version number, 1 for the
3848
        // samples we can decode
3849

    
3850
        init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
3851

    
3852
        if (decode_sequence_header(avctx, &gb) < 0)
3853
          return -1;
3854

    
3855
        count = avctx->extradata_size*8 - get_bits_count(&gb);
3856
        if (count>0)
3857
        {
3858
            av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
3859
                   count, get_bits(&gb, count));
3860
        }
3861
        else if (count < 0)
3862
        {
3863
            av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
3864
        }
3865
    } else { // VC1/WVC1
3866
        uint8_t *start = avctx->extradata, *end = avctx->extradata + avctx->extradata_size;
3867
        uint8_t *next; int size, buf2_size;
3868
        uint8_t *buf2 = NULL;
3869
        int seq_inited = 0, ep_inited = 0;
3870

    
3871
        if(avctx->extradata_size < 16) {
3872
            av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
3873
            return -1;
3874
        }
3875

    
3876
        buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
3877
        if(start[0]) start++; // in WVC1 extradata first byte is its size
3878
        next = start;
3879
        for(; next < end; start = next){
3880
            next = find_next_marker(start + 4, end);
3881
            size = next - start - 4;
3882
            if(size <= 0) continue;
3883
            buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
3884
            init_get_bits(&gb, buf2, buf2_size * 8);
3885
            switch(AV_RB32(start)){
3886
            case VC1_CODE_SEQHDR:
3887
                if(decode_sequence_header(avctx, &gb) < 0){
3888
                    av_free(buf2);
3889
                    return -1;
3890
                }
3891
                seq_inited = 1;
3892
                break;
3893
            case VC1_CODE_ENTRYPOINT:
3894
                if(decode_entry_point(avctx, &gb) < 0){
3895
                    av_free(buf2);
3896
                    return -1;
3897
                }
3898
                ep_inited = 1;
3899
                break;
3900
            }
3901
        }
3902
        av_free(buf2);
3903
        if(!seq_inited || !ep_inited){
3904
            av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
3905
            return -1;
3906
        }
3907
    }
3908
    avctx->has_b_frames= !!(avctx->max_b_frames);
3909
    s->low_delay = !avctx->has_b_frames;
3910

    
3911
    s->mb_width = (avctx->coded_width+15)>>4;
3912
    s->mb_height = (avctx->coded_height+15)>>4;
3913

    
3914
    /* Allocate mb bitplanes */
3915
    v->mv_type_mb_plane = av_malloc(s->mb_stride * s->mb_height);
3916
    v->direct_mb_plane = av_malloc(s->mb_stride * s->mb_height);
3917
    v->acpred_plane = av_malloc(s->mb_stride * s->mb_height);
3918
    v->over_flags_plane = av_malloc(s->mb_stride * s->mb_height);
3919

    
3920
    /* allocate block type info in that way so it could be used with s->block_index[] */
3921
    v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
3922
    v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
3923
    v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
3924
    v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
3925

    
3926
    /* Init coded blocks info */
3927
    if (v->profile == PROFILE_ADVANCED)
3928
    {
3929
//        if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
3930
//            return -1;
3931
//        if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
3932
//            return -1;
3933
    }
3934

    
3935
    return 0;
3936
}
3937

    
3938

    
3939
/** Decode a VC1/WMV3 frame
3940
 * @todo TODO: Handle VC-1 IDUs (Transport level?)
3941
 */
3942
static int vc1_decode_frame(AVCodecContext *avctx,
3943
                            void *data, int *data_size,
3944
                            uint8_t *buf, int buf_size)
3945
{
3946
    VC1Context *v = avctx->priv_data;
3947
    MpegEncContext *s = &v->s;
3948
    AVFrame *pict = data;
3949
    uint8_t *buf2 = NULL;
3950

    
3951
    /* no supplementary picture */
3952
    if (buf_size == 0) {
3953
        /* special case for last picture */
3954
        if (s->low_delay==0 && s->next_picture_ptr) {
3955
            *pict= *(AVFrame*)s->next_picture_ptr;
3956
            s->next_picture_ptr= NULL;
3957

    
3958
            *data_size = sizeof(AVFrame);
3959
        }
3960

    
3961
        return 0;
3962
    }
3963

    
3964
    /* We need to set current_picture_ptr before reading the header,
3965
     * otherwise we cannot store anything in there. */
3966
    if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
3967
        int i= ff_find_unused_picture(s, 0);
3968
        s->current_picture_ptr= &s->picture[i];
3969
    }
3970

    
3971
    //for advanced profile we may need to parse and unescape data
3972
    if (avctx->codec_id == CODEC_ID_VC1) {
3973
        int buf_size2 = 0;
3974
        buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
3975

    
3976
        if(IS_MARKER(AV_RB32(buf))){ /* frame starts with marker and needs to be parsed */
3977
            uint8_t *start, *end, *next;
3978
            int size;
3979

    
3980
            next = buf;
3981
            for(start = buf, end = buf + buf_size; next < end; start = next){
3982
                next = find_next_marker(start + 4, end);
3983
                size = next - start - 4;
3984
                if(size <= 0) continue;
3985
                switch(AV_RB32(start)){
3986
                case VC1_CODE_FRAME:
3987
                    buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
3988
                    break;
3989
                case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
3990
                    buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
3991
                    init_get_bits(&s->gb, buf2, buf_size2*8);
3992
                    decode_entry_point(avctx, &s->gb);
3993
                    break;
3994
                case VC1_CODE_SLICE:
3995
                    av_log(avctx, AV_LOG_ERROR, "Sliced decoding is not implemented (yet)\n");
3996
                    av_free(buf2);
3997
                    return -1;
3998
                }
3999
            }
4000
        }else if(v->interlace && ((buf[0] & 0xC0) == 0xC0)){ /* WVC1 interlaced stores both fields divided by marker */
4001
            uint8_t *divider;
4002

    
4003
            divider = find_next_marker(buf, buf + buf_size);
4004
            if((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD){
4005
                av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
4006
                return -1;
4007
            }
4008

    
4009
            buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
4010
            // TODO
4011
            av_free(buf2);return -1;
4012
        }else{
4013
            buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
4014
        }
4015
        init_get_bits(&s->gb, buf2, buf_size2*8);
4016
    } else
4017
        init_get_bits(&s->gb, buf, buf_size*8);
4018
    // do parse frame header
4019
    if(v->profile < PROFILE_ADVANCED) {
4020
        if(vc1_parse_frame_header(v, &s->gb) == -1) {
4021
            av_free(buf2);
4022
            return -1;
4023
        }
4024
    } else {
4025
        if(vc1_parse_frame_header_adv(v, &s->gb) == -1) {
4026
            av_free(buf2);
4027
            return -1;
4028
        }
4029
    }
4030

    
4031
    if(s->pict_type != I_TYPE && !v->res_rtm_flag){
4032
        av_free(buf2);
4033
        return -1;
4034
    }
4035

    
4036
    // for hurry_up==5
4037
    s->current_picture.pict_type= s->pict_type;
4038
    s->current_picture.key_frame= s->pict_type == I_TYPE;
4039

    
4040
    /* skip B-frames if we don't have reference frames */
4041
    if(s->last_picture_ptr==NULL && (s->pict_type==B_TYPE || s->dropable)){
4042
        av_free(buf2);
4043
        return -1;//buf_size;
4044
    }
4045
    /* skip b frames if we are in a hurry */
4046
    if(avctx->hurry_up && s->pict_type==B_TYPE) return -1;//buf_size;
4047
    if(   (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==B_TYPE)
4048
       || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=I_TYPE)
4049
       ||  avctx->skip_frame >= AVDISCARD_ALL) {
4050
        av_free(buf2);
4051
        return buf_size;
4052
    }
4053
    /* skip everything if we are in a hurry>=5 */
4054
    if(avctx->hurry_up>=5) {
4055
        av_free(buf2);
4056
        return -1;//buf_size;
4057
    }
4058

    
4059
    if(s->next_p_frame_damaged){
4060
        if(s->pict_type==B_TYPE)
4061
            return buf_size;
4062
        else
4063
            s->next_p_frame_damaged=0;
4064
    }
4065

    
4066
    if(MPV_frame_start(s, avctx) < 0) {
4067
        av_free(buf2);
4068
        return -1;
4069
    }
4070

    
4071
    ff_er_frame_start(s);
4072

    
4073
    v->bits = buf_size * 8;
4074
    vc1_decode_blocks(v);
4075
//av_log(s->avctx, AV_LOG_INFO, "Consumed %i/%i bits\n", get_bits_count(&s->gb), buf_size*8);
4076
//  if(get_bits_count(&s->gb) > buf_size * 8)
4077
//      return -1;
4078
    ff_er_frame_end(s);
4079

    
4080
    MPV_frame_end(s);
4081

    
4082
assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
4083
assert(s->current_picture.pict_type == s->pict_type);
4084
    if (s->pict_type == B_TYPE || s->low_delay) {
4085
        *pict= *(AVFrame*)s->current_picture_ptr;
4086
    } else if (s->last_picture_ptr != NULL) {
4087
        *pict= *(AVFrame*)s->last_picture_ptr;
4088
    }
4089

    
4090
    if(s->last_picture_ptr || s->low_delay){
4091
        *data_size = sizeof(AVFrame);
4092
        ff_print_debug_info(s, pict);
4093
    }
4094

    
4095
    /* Return the Picture timestamp as the frame number */
4096
    /* we substract 1 because it is added on utils.c    */
4097
    avctx->frame_number = s->picture_number - 1;
4098

    
4099
    av_free(buf2);
4100
    return buf_size;
4101
}
4102

    
4103

    
4104
/** Close a VC1/WMV3 decoder
4105
 * @warning Initial try at using MpegEncContext stuff
4106
 */
4107
static int vc1_decode_end(AVCodecContext *avctx)
4108
{
4109
    VC1Context *v = avctx->priv_data;
4110

    
4111
    av_freep(&v->hrd_rate);
4112
    av_freep(&v->hrd_buffer);
4113
    MPV_common_end(&v->s);
4114
    av_freep(&v->mv_type_mb_plane);
4115
    av_freep(&v->direct_mb_plane);
4116
    av_freep(&v->acpred_plane);
4117
    av_freep(&v->over_flags_plane);
4118
    av_freep(&v->mb_type_base);
4119
    return 0;
4120
}
4121

    
4122

    
4123
AVCodec vc1_decoder = {
4124
    "vc1",
4125
    CODEC_TYPE_VIDEO,
4126
    CODEC_ID_VC1,
4127
    sizeof(VC1Context),
4128
    vc1_decode_init,
4129
    NULL,
4130
    vc1_decode_end,
4131
    vc1_decode_frame,
4132
    CODEC_CAP_DELAY,
4133
    NULL
4134
};
4135

    
4136
AVCodec wmv3_decoder = {
4137
    "wmv3",
4138
    CODEC_TYPE_VIDEO,
4139
    CODEC_ID_WMV3,
4140
    sizeof(VC1Context),
4141
    vc1_decode_init,
4142
    NULL,
4143
    vc1_decode_end,
4144
    vc1_decode_frame,
4145
    CODEC_CAP_DELAY,
4146
    NULL
4147
};