Statistics
| Branch: | Revision:

ffmpeg / libavcodec / vc1.c @ 63d2bad8

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
#include "unary.h"
36
#include "simple_idct.h"
37

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

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

    
46

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

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

    
64
    v->hrd_rate = v->hrd_buffer = NULL;
65

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

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

    
119
    return 0;
120
}
121

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

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

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

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

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

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

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

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

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

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

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

    
314
/** @} */ //Bitplane group
315

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

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

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

    
369
    if(v->rangeredfrm) {
370
        int i, j, k;
371
        for(k = 0; k < 6; k++)
372
            for(j = 0; j < 8; j++)
373
                for(i = 0; i < 8; i++)
374
                    block[k][i + j*8] = ((block[k][i + j*8] - 128) << 1) + 128;
375

    
376
    }
377
    ys = v->s.current_picture.linesize[0];
378
    us = v->s.current_picture.linesize[1];
379
    vs = v->s.current_picture.linesize[2];
380
    Y = v->s.dest[0];
381

    
382
    dsp->put_pixels_clamped(block[0], Y, ys);
383
    dsp->put_pixels_clamped(block[1], Y + 8, ys);
384
    Y += ys * 8;
385
    dsp->put_pixels_clamped(block[2], Y, ys);
386
    dsp->put_pixels_clamped(block[3], Y + 8, ys);
387

    
388
    if(!(v->s.flags & CODEC_FLAG_GRAY)) {
389
        dsp->put_pixels_clamped(block[4], v->s.dest[1], us);
390
        dsp->put_pixels_clamped(block[5], v->s.dest[2], vs);
391
    }
392
}
393

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

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

    
406
    mx = s->mv[dir][0][0];
407
    my = s->mv[dir][0][1];
408

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

    
430
    src_x = s->mb_x * 16 + (mx >> 2);
431
    src_y = s->mb_y * 16 + (my >> 2);
432
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
433
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
434

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

    
447
    srcY += src_y * s->linesize + src_x;
448
    srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
449
    srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
450

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

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

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

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

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

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

    
525
        if(!v->rnd)
526
            dsp->put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
527
        else
528
            dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
529
    }
530

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

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

    
555
    if(!v->s.last_picture.data[0])return;
556
    mx = s->mv[0][n][0];
557
    my = s->mv[0][n][1];
558
    srcY = s->last_picture.data[0];
559

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

    
562
    src_x = s->mb_x * 16 + (n&1) * 8 + (mx >> 2);
563
    src_y = s->mb_y * 16 + (n&2) * 4 + (my >> 2);
564

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

    
573
    srcY += src_y * s->linesize + src_x;
574

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

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

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

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

    
619
static inline int median4(int a, int b, int c, int d)
620
{
621
    if(a < b) {
622
        if(c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
623
        else      return (FFMIN(b, c) + FFMAX(a, d)) / 2;
624
    } else {
625
        if(c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
626
        else      return (FFMIN(a, c) + FFMAX(b, d)) / 2;
627
    }
628
}
629

    
630

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

    
643
    if(!v->s.last_picture.data[0])return;
644
    if(s->flags & CODEC_FLAG_GRAY) return;
645

    
646
    for(i = 0; i < 4; i++) {
647
        mvx[i] = s->mv[0][i][0];
648
        mvy[i] = s->mv[0][i][1];
649
        intra[i] = v->mb_type[0][s->block_index[i]];
650
    }
651

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

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

    
697
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
698
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
699

    
700
    if(v->profile != PROFILE_ADVANCED){
701
        uvsrc_x = av_clip(uvsrc_x,  -8, s->mb_width  *  8);
702
        uvsrc_y = av_clip(uvsrc_y,  -8, s->mb_height *  8);
703
    }else{
704
        uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
705
        uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
706
    }
707

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

    
720
        /* if we deal with range reduction we need to scale source blocks */
721
        if(v->rangeredfrm) {
722
            int i, j;
723
            uint8_t *src, *src2;
724

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

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

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

    
765
static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb);
766

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

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

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

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

    
811
    v->res_x8 = get_bits1(gb); //reserved
812
    v->multires = get_bits1(gb);
813
    v->res_fasttx = get_bits1(gb);
814
    if (!v->res_fasttx)
815
    {
816
        v->s.dsp.vc1_inv_trans_8x8 = simple_idct;
817
    }
818

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

    
836
    v->res_transtab = get_bits1(gb);
837
    if (v->res_transtab)
838
    {
839
        av_log(avctx, AV_LOG_ERROR,
840
               "1 for reserved RES_TRANSTAB is forbidden\n");
841
        return -1;
842
    }
843

    
844
    v->overlap = get_bits1(gb); //common
845

    
846
    v->s.resync_marker = get_bits1(gb);
847
    v->rangered = get_bits1(gb);
848
    if (v->rangered && v->profile == PROFILE_SIMPLE)
849
    {
850
        av_log(avctx, AV_LOG_INFO,
851
               "RANGERED should be set to 0 in simple profile\n");
852
    }
853

    
854
    v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
855
    v->quantizer_mode = get_bits(gb, 2); //common
856

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

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

    
898
    // (fps-2)/4 (->30)
899
    v->frmrtq_postproc = get_bits(gb, 3); //common
900
    // (bitrate-32kbps)/64kbps
901
    v->bitrtq_postproc = get_bits(gb, 5); //common
902
    v->postprocflag = get_bits1(gb); //common
903

    
904
    v->s.avctx->coded_width = (get_bits(gb, 12) + 1) << 1;
905
    v->s.avctx->coded_height = (get_bits(gb, 12) + 1) << 1;
906
    v->s.avctx->width = v->s.avctx->coded_width;
907
    v->s.avctx->height = v->s.avctx->coded_height;
908
    v->broadcast = get_bits1(gb);
909
    v->interlace = get_bits1(gb);
910
    v->tfcntrflag = get_bits1(gb);
911
    v->finterpflag = get_bits1(gb);
912
    skip_bits1(gb); // reserved
913

    
914
    v->s.h_edge_pos = v->s.avctx->coded_width;
915
    v->s.v_edge_pos = v->s.avctx->coded_height;
916

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

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

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

    
963
        if(get_bits1(gb)){
964
            v->color_prim = get_bits(gb, 8);
965
            v->transfer_char = get_bits(gb, 8);
966
            v->matrix_coef = get_bits(gb, 8);
967
        }
968
    }
969

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

    
984
static int decode_entry_point(AVCodecContext *avctx, GetBitContext *gb)
985
{
986
    VC1Context *v = avctx->priv_data;
987
    int i, blink, clentry, refdist;
988

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

    
1002
    if(v->hrd_param_flag){
1003
        for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
1004
            skip_bits(gb, 8); //hrd_full[n]
1005
        }
1006
    }
1007

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

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

    
1030
    return 0;
1031
}
1032

    
1033
static int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
1034
{
1035
    int pqindex, lowquant, status;
1036

    
1037
    if(v->finterpflag) v->interpfrm = get_bits1(gb);
1038
    skip_bits(gb, 2); //framecnt unused
1039
    v->rangeredfrm = 0;
1040
    if (v->rangered) v->rangeredfrm = get_bits1(gb);
1041
    v->s.pict_type = get_bits1(gb);
1042
    if (v->s.avctx->max_b_frames) {
1043
        if (!v->s.pict_type) {
1044
            if (get_bits1(gb)) v->s.pict_type = I_TYPE;
1045
            else v->s.pict_type = B_TYPE;
1046
        } else v->s.pict_type = P_TYPE;
1047
    } else v->s.pict_type = v->s.pict_type ? P_TYPE : I_TYPE;
1048

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

    
1060
    /* calculate RND */
1061
    if(v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
1062
        v->rnd = 1;
1063
    if(v->s.pict_type == P_TYPE)
1064
        v->rnd ^= 1;
1065

    
1066
    /* Quantizer stuff */
1067
    pqindex = get_bits(gb, 5);
1068
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1069
        v->pq = ff_vc1_pquant_table[0][pqindex];
1070
    else
1071
        v->pq = ff_vc1_pquant_table[1][pqindex];
1072

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

    
1096
    if(v->res_x8 && (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)){
1097
        v->x8_type = get_bits1(gb);
1098
    }else v->x8_type = 0;
1099
//av_log(v->s.avctx, AV_LOG_INFO, "%c Frame: QP=[%i]%i (+%i/2) %i\n",
1100
//        (v->s.pict_type == P_TYPE) ? 'P' : ((v->s.pict_type == I_TYPE) ? 'I' : 'B'), pqindex, v->pq, v->halfpq, v->rangeredfrm);
1101

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

    
1104
    switch(v->s.pict_type) {
1105
    case P_TYPE:
1106
        if (v->pq < 5) v->tt_index = 0;
1107
        else if(v->pq < 13) v->tt_index = 1;
1108
        else v->tt_index = 2;
1109

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

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

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

    
1169
        if (v->dquant)
1170
        {
1171
            av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1172
            vop_dquant_decoding(v);
1173
        }
1174

    
1175
        v->ttfrm = 0; //FIXME Is that so ?
1176
        if (v->vstransform)
1177
        {
1178
            v->ttmbf = get_bits1(gb);
1179
            if (v->ttmbf)
1180
            {
1181
                v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
1182
            }
1183
        } else {
1184
            v->ttmbf = 1;
1185
            v->ttfrm = TT_8X8;
1186
        }
1187
        break;
1188
    case B_TYPE:
1189
        if (v->pq < 5) v->tt_index = 0;
1190
        else if(v->pq < 13) v->tt_index = 1;
1191
        else v->tt_index = 2;
1192

    
1193
        lowquant = (v->pq > 12) ? 0 : 1;
1194
        v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
1195
        v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
1196
        v->s.mspel = v->s.quarter_sample;
1197

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

    
1207
        v->s.mv_table_index = get_bits(gb, 2);
1208
        v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1209

    
1210
        if (v->dquant)
1211
        {
1212
            av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1213
            vop_dquant_decoding(v);
1214
        }
1215

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

    
1231
    if(!v->x8_type)
1232
    {
1233
        /* AC Syntax */
1234
        v->c_ac_table_index = decode012(gb);
1235
        if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
1236
        {
1237
            v->y_ac_table_index = decode012(gb);
1238
        }
1239
        /* DC Syntax */
1240
        v->s.dc_table_index = get_bits1(gb);
1241
    }
1242

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

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

    
1255
    v->p_frame_skipped = 0;
1256

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2149

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

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

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

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

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

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

    
2224

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

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

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

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

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

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

    
2254
    return pred;
2255
}
2256

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

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

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

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

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

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

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

    
2386
    //AC Decoding
2387
    i = 1;
2388

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

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

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

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

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

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

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

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

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

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

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

    
2485
    return 0;
2486
}
2487

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

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

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

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

    
2553
    //AC Decoding
2554
    i = 1;
2555

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2681
    return 0;
2682
}
2683

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

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

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

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

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

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

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

    
2758
    //AC Decoding
2759
    i = 1;
2760

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

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

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

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

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

    
2787
        zz_table = ff_vc1_simple_progressive_8x8_zz;
2788

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

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

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

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

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

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

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

    
2882
    return 0;
2883
}
2884

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

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

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

    
3002

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

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

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

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

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

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

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

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

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

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

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

    
3236
    /* Should never happen */
3237
    return -1;
3238
}
3239

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

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

    
3263
    mquant = v->pq; /* Loosy initialization */
3264
    s->mb_intra = 0;
3265

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

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

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

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

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

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

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

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

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

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

    
3446
            // do actual MB decoding and displaying
3447
            cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
3448
            v->s.ac_pred = get_bits1(&v->s.gb);
3449

    
3450
            for(k = 0; k < 6; k++) {
3451
                val = ((cbp >> (5 - k)) & 1);
3452

    
3453
                if (k < 4) {
3454
                    int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
3455
                    val = val ^ pred;
3456
                    *coded_val = val;
3457
                }
3458
                cbp |= val << (5 - k);
3459

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

    
3462
                s->dsp.vc1_inv_trans_8x8(s->block[k]);
3463
                if(v->pq >= 9 && v->overlap) {
3464
                    for(j = 0; j < 64; j++) s->block[k][j] += 128;
3465
                }
3466
            }
3467

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

    
3492
            if(get_bits_count(&s->gb) > v->bits) {
3493
                ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
3494
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
3495
                return;
3496
            }
3497
        }
3498
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3499
        s->first_slice_line = 0;
3500
    }
3501
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3502
}
3503

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

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

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

    
3543
    //do frame decode
3544
    s->mb_x = s->mb_y = 0;
3545
    s->mb_intra = 1;
3546
    s->first_slice_line = 1;
3547
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3548
        for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
3549
            ff_init_block_index(s);
3550
            ff_update_block_index(s);
3551
            s->dsp.clear_blocks(s->block[0]);
3552
            mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3553
            s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
3554
            s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
3555
            s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
3556

    
3557
            // do actual MB decoding and displaying
3558
            cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
3559
            if(v->acpred_is_raw)
3560
                v->s.ac_pred = get_bits1(&v->s.gb);
3561
            else
3562
                v->s.ac_pred = v->acpred_plane[mb_pos];
3563

    
3564
            if(v->condover == CONDOVER_SELECT) {
3565
                if(v->overflg_is_raw)
3566
                    overlap = get_bits1(&v->s.gb);
3567
                else
3568
                    overlap = v->over_flags_plane[mb_pos];
3569
            } else
3570
                overlap = (v->condover == CONDOVER_ALL);
3571

    
3572
            GET_MQUANT();
3573

    
3574
            s->current_picture.qscale_table[mb_pos] = mquant;
3575
            /* Set DC scale - y and c use the same */
3576
            s->y_dc_scale = s->y_dc_scale_table[mquant];
3577
            s->c_dc_scale = s->c_dc_scale_table[mquant];
3578

    
3579
            for(k = 0; k < 6; k++) {
3580
                val = ((cbp >> (5 - k)) & 1);
3581

    
3582
                if (k < 4) {
3583
                    int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
3584
                    val = val ^ pred;
3585
                    *coded_val = val;
3586
                }
3587
                cbp |= val << (5 - k);
3588

    
3589
                v->a_avail = !s->first_slice_line || (k==2 || k==3);
3590
                v->c_avail = !!s->mb_x || (k==1 || k==3);
3591

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

    
3594
                s->dsp.vc1_inv_trans_8x8(s->block[k]);
3595
                for(j = 0; j < 64; j++) s->block[k][j] += 128;
3596
            }
3597

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

    
3622
            if(get_bits_count(&s->gb) > v->bits) {
3623
                ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
3624
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
3625
                return;
3626
            }
3627
        }
3628
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3629
        s->first_slice_line = 0;
3630
    }
3631
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3632
}
3633

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

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

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

    
3663
    s->first_slice_line = 1;
3664
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3665
        for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
3666
            ff_init_block_index(s);
3667
            ff_update_block_index(s);
3668
            s->dsp.clear_blocks(s->block[0]);
3669

    
3670
            vc1_decode_p_mb(v);
3671
            if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
3672
                ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
3673
                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);
3674
                return;
3675
            }
3676
        }
3677
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3678
        s->first_slice_line = 0;
3679
    }
3680
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3681
}
3682

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

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

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

    
3712
    s->first_slice_line = 1;
3713
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3714
        for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
3715
            ff_init_block_index(s);
3716
            ff_update_block_index(s);
3717
            s->dsp.clear_blocks(s->block[0]);
3718

    
3719
            vc1_decode_b_mb(v);
3720
            if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
3721
                ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
3722
                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);
3723
                return;
3724
            }
3725
        }
3726
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3727
        s->first_slice_line = 0;
3728
    }
3729
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
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
    if(v->x8_type){
3756
        ff_intrax8_decode_picture(&v->x8, 2*v->pq+v->halfpq, v->pq*(!v->pquantizer) );
3757
    }else{
3758

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

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

    
3792
    if(end-src < 4) return end;
3793
    while(src < end){
3794
        mrk = (mrk << 8) | *src++;
3795
        if(IS_MARKER(mrk))
3796
            return src-4;
3797
    }
3798
    return end;
3799
}
3800

    
3801
static av_always_inline int vc1_unescape_buffer(uint8_t *src, int size, uint8_t *dst)
3802
{
3803
    int dsize = 0, i;
3804

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

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

    
3830
    if (!avctx->extradata_size || !avctx->extradata) return -1;
3831
    if (!(avctx->flags & CODEC_FLAG_GRAY))
3832
        avctx->pix_fmt = PIX_FMT_YUV420P;
3833
    else
3834
        avctx->pix_fmt = PIX_FMT_GRAY8;
3835
    v->s.avctx = avctx;
3836
    avctx->flags |= CODEC_FLAG_EMU_EDGE;
3837
    v->s.flags |= CODEC_FLAG_EMU_EDGE;
3838

    
3839
    if(avctx->idct_algo==FF_IDCT_AUTO){
3840
        avctx->idct_algo=FF_IDCT_WMV2;
3841
    }
3842

    
3843
    if(ff_h263_decode_init(avctx) < 0)
3844
        return -1;
3845
    if (vc1_init_common(v) < 0) return -1;
3846

    
3847
    avctx->coded_width = avctx->width;
3848
    avctx->coded_height = avctx->height;
3849
    if (avctx->codec_id == CODEC_ID_WMV3)
3850
    {
3851
        int count = 0;
3852

    
3853
        // looks like WMV3 has a sequence header stored in the extradata
3854
        // advanced sequence header may be before the first frame
3855
        // the last byte of the extradata is a version number, 1 for the
3856
        // samples we can decode
3857

    
3858
        init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
3859

    
3860
        if (decode_sequence_header(avctx, &gb) < 0)
3861
          return -1;
3862

    
3863
        count = avctx->extradata_size*8 - get_bits_count(&gb);
3864
        if (count>0)
3865
        {
3866
            av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
3867
                   count, get_bits(&gb, count));
3868
        }
3869
        else if (count < 0)
3870
        {
3871
            av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
3872
        }
3873
    } else { // VC1/WVC1
3874
        uint8_t *start = avctx->extradata, *end = avctx->extradata + avctx->extradata_size;
3875
        uint8_t *next; int size, buf2_size;
3876
        uint8_t *buf2 = NULL;
3877
        int seq_inited = 0, ep_inited = 0;
3878

    
3879
        if(avctx->extradata_size < 16) {
3880
            av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
3881
            return -1;
3882
        }
3883

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

    
3919
    s->mb_width = (avctx->coded_width+15)>>4;
3920
    s->mb_height = (avctx->coded_height+15)>>4;
3921

    
3922
    /* Allocate mb bitplanes */
3923
    v->mv_type_mb_plane = av_malloc(s->mb_stride * s->mb_height);
3924
    v->direct_mb_plane = av_malloc(s->mb_stride * s->mb_height);
3925
    v->acpred_plane = av_malloc(s->mb_stride * s->mb_height);
3926
    v->over_flags_plane = av_malloc(s->mb_stride * s->mb_height);
3927

    
3928
    /* allocate block type info in that way so it could be used with s->block_index[] */
3929
    v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
3930
    v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
3931
    v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
3932
    v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
3933

    
3934
    /* Init coded blocks info */
3935
    if (v->profile == PROFILE_ADVANCED)
3936
    {
3937
//        if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
3938
//            return -1;
3939
//        if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
3940
//            return -1;
3941
    }
3942

    
3943
    ff_intrax8_common_init(&v->x8,s);
3944
    return 0;
3945
}
3946

    
3947

    
3948
/** Decode a VC1/WMV3 frame
3949
 * @todo TODO: Handle VC-1 IDUs (Transport level?)
3950
 */
3951
static int vc1_decode_frame(AVCodecContext *avctx,
3952
                            void *data, int *data_size,
3953
                            uint8_t *buf, int buf_size)
3954
{
3955
    VC1Context *v = avctx->priv_data;
3956
    MpegEncContext *s = &v->s;
3957
    AVFrame *pict = data;
3958
    uint8_t *buf2 = NULL;
3959

    
3960
    /* no supplementary picture */
3961
    if (buf_size == 0) {
3962
        /* special case for last picture */
3963
        if (s->low_delay==0 && s->next_picture_ptr) {
3964
            *pict= *(AVFrame*)s->next_picture_ptr;
3965
            s->next_picture_ptr= NULL;
3966

    
3967
            *data_size = sizeof(AVFrame);
3968
        }
3969

    
3970
        return 0;
3971
    }
3972

    
3973
    /* We need to set current_picture_ptr before reading the header,
3974
     * otherwise we cannot store anything in there. */
3975
    if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
3976
        int i= ff_find_unused_picture(s, 0);
3977
        s->current_picture_ptr= &s->picture[i];
3978
    }
3979

    
3980
    //for advanced profile we may need to parse and unescape data
3981
    if (avctx->codec_id == CODEC_ID_VC1) {
3982
        int buf_size2 = 0;
3983
        buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
3984

    
3985
        if(IS_MARKER(AV_RB32(buf))){ /* frame starts with marker and needs to be parsed */
3986
            uint8_t *start, *end, *next;
3987
            int size;
3988

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

    
4012
            divider = find_next_marker(buf, buf + buf_size);
4013
            if((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD){
4014
                av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
4015
                return -1;
4016
            }
4017

    
4018
            buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
4019
            // TODO
4020
            av_free(buf2);return -1;
4021
        }else{
4022
            buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
4023
        }
4024
        init_get_bits(&s->gb, buf2, buf_size2*8);
4025
    } else
4026
        init_get_bits(&s->gb, buf, buf_size*8);
4027
    // do parse frame header
4028
    if(v->profile < PROFILE_ADVANCED) {
4029
        if(vc1_parse_frame_header(v, &s->gb) == -1) {
4030
            av_free(buf2);
4031
            return -1;
4032
        }
4033
    } else {
4034
        if(vc1_parse_frame_header_adv(v, &s->gb) == -1) {
4035
            av_free(buf2);
4036
            return -1;
4037
        }
4038
    }
4039

    
4040
    if(s->pict_type != I_TYPE && !v->res_rtm_flag){
4041
        av_free(buf2);
4042
        return -1;
4043
    }
4044

    
4045
    // for hurry_up==5
4046
    s->current_picture.pict_type= s->pict_type;
4047
    s->current_picture.key_frame= s->pict_type == I_TYPE;
4048

    
4049
    /* skip B-frames if we don't have reference frames */
4050
    if(s->last_picture_ptr==NULL && (s->pict_type==B_TYPE || s->dropable)){
4051
        av_free(buf2);
4052
        return -1;//buf_size;
4053
    }
4054
    /* skip b frames if we are in a hurry */
4055
    if(avctx->hurry_up && s->pict_type==B_TYPE) return -1;//buf_size;
4056
    if(   (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==B_TYPE)
4057
       || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=I_TYPE)
4058
       ||  avctx->skip_frame >= AVDISCARD_ALL) {
4059
        av_free(buf2);
4060
        return buf_size;
4061
    }
4062
    /* skip everything if we are in a hurry>=5 */
4063
    if(avctx->hurry_up>=5) {
4064
        av_free(buf2);
4065
        return -1;//buf_size;
4066
    }
4067

    
4068
    if(s->next_p_frame_damaged){
4069
        if(s->pict_type==B_TYPE)
4070
            return buf_size;
4071
        else
4072
            s->next_p_frame_damaged=0;
4073
    }
4074

    
4075
    if(MPV_frame_start(s, avctx) < 0) {
4076
        av_free(buf2);
4077
        return -1;
4078
    }
4079

    
4080
    s->me.qpel_put= s->dsp.put_qpel_pixels_tab;
4081
    s->me.qpel_avg= s->dsp.avg_qpel_pixels_tab;
4082

    
4083
    ff_er_frame_start(s);
4084

    
4085
    v->bits = buf_size * 8;
4086
    vc1_decode_blocks(v);
4087
//av_log(s->avctx, AV_LOG_INFO, "Consumed %i/%i bits\n", get_bits_count(&s->gb), buf_size*8);
4088
//  if(get_bits_count(&s->gb) > buf_size * 8)
4089
//      return -1;
4090
    ff_er_frame_end(s);
4091

    
4092
    MPV_frame_end(s);
4093

    
4094
assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
4095
assert(s->current_picture.pict_type == s->pict_type);
4096
    if (s->pict_type == B_TYPE || s->low_delay) {
4097
        *pict= *(AVFrame*)s->current_picture_ptr;
4098
    } else if (s->last_picture_ptr != NULL) {
4099
        *pict= *(AVFrame*)s->last_picture_ptr;
4100
    }
4101

    
4102
    if(s->last_picture_ptr || s->low_delay){
4103
        *data_size = sizeof(AVFrame);
4104
        ff_print_debug_info(s, pict);
4105
    }
4106

    
4107
    /* Return the Picture timestamp as the frame number */
4108
    /* we substract 1 because it is added on utils.c    */
4109
    avctx->frame_number = s->picture_number - 1;
4110

    
4111
    av_free(buf2);
4112
    return buf_size;
4113
}
4114

    
4115

    
4116
/** Close a VC1/WMV3 decoder
4117
 * @warning Initial try at using MpegEncContext stuff
4118
 */
4119
static int vc1_decode_end(AVCodecContext *avctx)
4120
{
4121
    VC1Context *v = avctx->priv_data;
4122

    
4123
    av_freep(&v->hrd_rate);
4124
    av_freep(&v->hrd_buffer);
4125
    MPV_common_end(&v->s);
4126
    av_freep(&v->mv_type_mb_plane);
4127
    av_freep(&v->direct_mb_plane);
4128
    av_freep(&v->acpred_plane);
4129
    av_freep(&v->over_flags_plane);
4130
    av_freep(&v->mb_type_base);
4131
    return 0;
4132
}
4133

    
4134

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

    
4148
AVCodec wmv3_decoder = {
4149
    "wmv3",
4150
    CODEC_TYPE_VIDEO,
4151
    CODEC_ID_WMV3,
4152
    sizeof(VC1Context),
4153
    vc1_decode_init,
4154
    NULL,
4155
    vc1_decode_end,
4156
    vc1_decode_frame,
4157
    CODEC_CAP_DELAY,
4158
    NULL
4159
};