Statistics
| Branch: | Revision:

ffmpeg / libavcodec / vc1.c @ a28d5224

History | View | Annotate | Download (154 KB)

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

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

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

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

    
48

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

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

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

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

    
114
    return 0;
115
}
116

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

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

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

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

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

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

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

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

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

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

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

    
309
/** @} */ //Bitplane group
310

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
707

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1117
    return 0;
1118
}
1119

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1343
    v->p_frame_skipped = 0;
1344

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1686
/** Predict and set motion vector
1687
 */
1688
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)
1689
{
1690
    int xy, wrap, off = 0;
1691
    int16_t *A, *B, *C;
1692
    int px, py;
1693
    int sum;
1694

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2001
    /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2002
    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));
2003
    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));
2004
    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));
2005
    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));
2006
    if(direct) {
2007
        s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
2008
        s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
2009
        s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
2010
        s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
2011
        return;
2012
    }
2013

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

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

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

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

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

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

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

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

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

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

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

    
2236

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

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

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

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

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

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

    
2311

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

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

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

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

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

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

    
2341
    return pred;
2342
}
2343

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

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

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

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

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

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

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

    
2473
    //AC Decoding
2474
    i = 1;
2475

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

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

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

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

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

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

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

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

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

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

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

    
2572
    return 0;
2573
}
2574

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

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

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

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

    
2640
    //AC Decoding
2641
    i = 1;
2642

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2768
    return 0;
2769
}
2770

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

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

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

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

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

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

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

    
2845
    //AC Decoding
2846
    i = 1;
2847

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

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

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

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

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

    
2874
        zz_table = wmv1_scantable[0];
2875

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

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

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

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

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

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

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

    
2969
    return 0;
2970
}
2971

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

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

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

    
3111

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
3709
    //do frame decode
3710
    s->mb_x = s->mb_y = 0;
3711
    s->mb_intra = 1;
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
            mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3719
            s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
3720
            s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
3721
            s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
3722

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

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

    
3738
            GET_MQUANT();
3739

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

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

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

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

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

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

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

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

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

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

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

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

    
3838
            vc1_decode_p_mb(v);
3839
            if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
3840
                ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
3841
                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);
3842
                return;
3843
            }
3844
        }
3845
        memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0])*s->mb_stride);
3846
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3847
        s->first_slice_line = 0;
3848
    }
3849
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3850
}
3851

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

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

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

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

    
3888
            vc1_decode_b_mb(v);
3889
            if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
3890
                ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
3891
                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);
3892
                return;
3893
            }
3894
            if(v->s.loop_filter) vc1_loop_filter_iblk(s, s->current_picture.qscale_table[s->mb_x + s->mb_y *s->mb_stride]);
3895
        }
3896
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3897
        s->first_slice_line = 0;
3898
    }
3899
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3900
}
3901

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
4122

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

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

    
4143
            *data_size = sizeof(AVFrame);
4144
        }
4145

    
4146
        return 0;
4147
    }
4148

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

    
4156
    if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
4157
        if (v->profile < PROFILE_ADVANCED)
4158
            avctx->pix_fmt = PIX_FMT_VDPAU_WMV3;
4159
        else
4160
            avctx->pix_fmt = PIX_FMT_VDPAU_VC1;
4161
    }
4162

    
4163
    //for advanced profile we may need to parse and unescape data
4164
    if (avctx->codec_id == CODEC_ID_VC1) {
4165
        int buf_size2 = 0;
4166
        buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
4167

    
4168
        if(IS_MARKER(AV_RB32(buf))){ /* frame starts with marker and needs to be parsed */
4169
            const uint8_t *start, *end, *next;
4170
            int size;
4171

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

    
4197
            divider = find_next_marker(buf, buf + buf_size);
4198
            if((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD){
4199
                av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
4200
                av_free(buf2);
4201
                return -1;
4202
            }
4203

    
4204
            buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
4205
            // TODO
4206
            av_free(buf2);return -1;
4207
        }else{
4208
            buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
4209
        }
4210
        init_get_bits(&s->gb, buf2, buf_size2*8);
4211
    } else
4212
        init_get_bits(&s->gb, buf, buf_size*8);
4213
    // do parse frame header
4214
    if(v->profile < PROFILE_ADVANCED) {
4215
        if(vc1_parse_frame_header(v, &s->gb) == -1) {
4216
            av_free(buf2);
4217
            return -1;
4218
        }
4219
    } else {
4220
        if(vc1_parse_frame_header_adv(v, &s->gb) == -1) {
4221
            av_free(buf2);
4222
            return -1;
4223
        }
4224
    }
4225

    
4226
    if(s->pict_type != FF_I_TYPE && !v->res_rtm_flag){
4227
        av_free(buf2);
4228
        return -1;
4229
    }
4230

    
4231
    // for hurry_up==5
4232
    s->current_picture.pict_type= s->pict_type;
4233
    s->current_picture.key_frame= s->pict_type == FF_I_TYPE;
4234

    
4235
    /* skip B-frames if we don't have reference frames */
4236
    if(s->last_picture_ptr==NULL && (s->pict_type==FF_B_TYPE || s->dropable)){
4237
        av_free(buf2);
4238
        return -1;//buf_size;
4239
    }
4240
    /* skip b frames if we are in a hurry */
4241
    if(avctx->hurry_up && s->pict_type==FF_B_TYPE) return -1;//buf_size;
4242
    if(   (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==FF_B_TYPE)
4243
       || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=FF_I_TYPE)
4244
       ||  avctx->skip_frame >= AVDISCARD_ALL) {
4245
        av_free(buf2);
4246
        return buf_size;
4247
    }
4248
    /* skip everything if we are in a hurry>=5 */
4249
    if(avctx->hurry_up>=5) {
4250
        av_free(buf2);
4251
        return -1;//buf_size;
4252
    }
4253

    
4254
    if(s->next_p_frame_damaged){
4255
        if(s->pict_type==FF_B_TYPE)
4256
            return buf_size;
4257
        else
4258
            s->next_p_frame_damaged=0;
4259
    }
4260

    
4261
    if(MPV_frame_start(s, avctx) < 0) {
4262
        av_free(buf2);
4263
        return -1;
4264
    }
4265

    
4266
    s->me.qpel_put= s->dsp.put_qpel_pixels_tab;
4267
    s->me.qpel_avg= s->dsp.avg_qpel_pixels_tab;
4268

    
4269
    if ((CONFIG_VC1_VDPAU_DECODER || CONFIG_WMV3_VDPAU_DECODER)
4270
        &&s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
4271
        ff_vdpau_vc1_decode_picture(s, buf_vdpau, (buf + buf_size) - buf_vdpau);
4272
    else {
4273
        ff_er_frame_start(s);
4274

    
4275
        v->bits = buf_size * 8;
4276
        vc1_decode_blocks(v);
4277
//av_log(s->avctx, AV_LOG_INFO, "Consumed %i/%i bits\n", get_bits_count(&s->gb), buf_size*8);
4278
//  if(get_bits_count(&s->gb) > buf_size * 8)
4279
//      return -1;
4280
        ff_er_frame_end(s);
4281
    }
4282

    
4283
    MPV_frame_end(s);
4284

    
4285
assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
4286
assert(s->current_picture.pict_type == s->pict_type);
4287
    if (s->pict_type == FF_B_TYPE || s->low_delay) {
4288
        *pict= *(AVFrame*)s->current_picture_ptr;
4289
    } else if (s->last_picture_ptr != NULL) {
4290
        *pict= *(AVFrame*)s->last_picture_ptr;
4291
    }
4292

    
4293
    if(s->last_picture_ptr || s->low_delay){
4294
        *data_size = sizeof(AVFrame);
4295
        ff_print_debug_info(s, pict);
4296
    }
4297

    
4298
    /* Return the Picture timestamp as the frame number */
4299
    /* we subtract 1 because it is added on utils.c     */
4300
    avctx->frame_number = s->picture_number - 1;
4301

    
4302
    av_free(buf2);
4303
    return buf_size;
4304
}
4305

    
4306

    
4307
/** Close a VC1/WMV3 decoder
4308
 * @warning Initial try at using MpegEncContext stuff
4309
 */
4310
static av_cold int vc1_decode_end(AVCodecContext *avctx)
4311
{
4312
    VC1Context *v = avctx->priv_data;
4313

    
4314
    av_freep(&v->hrd_rate);
4315
    av_freep(&v->hrd_buffer);
4316
    MPV_common_end(&v->s);
4317
    av_freep(&v->mv_type_mb_plane);
4318
    av_freep(&v->direct_mb_plane);
4319
    av_freep(&v->acpred_plane);
4320
    av_freep(&v->over_flags_plane);
4321
    av_freep(&v->mb_type_base);
4322
    av_freep(&v->cbp_base);
4323
    ff_intrax8_common_end(&v->x8);
4324
    return 0;
4325
}
4326

    
4327

    
4328
AVCodec vc1_decoder = {
4329
    "vc1",
4330
    CODEC_TYPE_VIDEO,
4331
    CODEC_ID_VC1,
4332
    sizeof(VC1Context),
4333
    vc1_decode_init,
4334
    NULL,
4335
    vc1_decode_end,
4336
    vc1_decode_frame,
4337
    CODEC_CAP_DELAY,
4338
    NULL,
4339
    .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
4340
};
4341

    
4342
AVCodec wmv3_decoder = {
4343
    "wmv3",
4344
    CODEC_TYPE_VIDEO,
4345
    CODEC_ID_WMV3,
4346
    sizeof(VC1Context),
4347
    vc1_decode_init,
4348
    NULL,
4349
    vc1_decode_end,
4350
    vc1_decode_frame,
4351
    CODEC_CAP_DELAY,
4352
    NULL,
4353
    .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
4354
};
4355

    
4356
#if CONFIG_WMV3_VDPAU_DECODER
4357
AVCodec wmv3_vdpau_decoder = {
4358
    "wmv3_vdpau",
4359
    CODEC_TYPE_VIDEO,
4360
    CODEC_ID_WMV3,
4361
    sizeof(VC1Context),
4362
    vc1_decode_init,
4363
    NULL,
4364
    vc1_decode_end,
4365
    vc1_decode_frame,
4366
    CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
4367
    NULL,
4368
    .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
4369
};
4370
#endif
4371

    
4372
#if CONFIG_VC1_VDPAU_DECODER
4373
AVCodec vc1_vdpau_decoder = {
4374
    "vc1_vdpau",
4375
    CODEC_TYPE_VIDEO,
4376
    CODEC_ID_VC1,
4377
    sizeof(VC1Context),
4378
    vc1_decode_init,
4379
    NULL,
4380
    vc1_decode_end,
4381
    vc1_decode_frame,
4382
    CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
4383
    NULL,
4384
    .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
4385
};
4386
#endif