Statistics
| Branch: | Revision:

ffmpeg / libavcodec / vc1.c @ 7d9a6a0f

History | View | Annotate | Download (144 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
/**
25
 * @file vc1.c
26
 * VC-1 and WMV3 decoder
27
 *
28
 */
29
#include "common.h"
30
#include "dsputil.h"
31
#include "avcodec.h"
32
#include "mpegvideo.h"
33
#include "vc1data.h"
34
#include "vc1acdata.h"
35
#include "vc1.h"
36

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

    
40
extern const uint32_t ff_table0_dc_lum[120][2], ff_table1_dc_lum[120][2];
41
extern const uint32_t ff_table0_dc_chroma[120][2], ff_table1_dc_chroma[120][2];
42
extern VLC ff_msmp4_dc_luma_vlc[2], ff_msmp4_dc_chroma_vlc[2];
43
#define MB_INTRA_VLC_BITS 9
44
extern VLC ff_msmp4_mb_i_vlc;
45
extern const uint16_t ff_msmp4_mb_i_table[64][2];
46
#define DC_VLC_BITS 9
47
#define AC_VLC_BITS 9
48
static const uint16_t table_mb_intra[64][2];
49

    
50

    
51
/**
52
 * Get unary code of limited length
53
 * @fixme FIXME Slow and ugly
54
 * @param gb GetBitContext
55
 * @param[in] stop The bitstop value (unary code of 1's or 0's)
56
 * @param[in] len Maximum length
57
 * @return Unary length/index
58
 */
59
static int get_prefix(GetBitContext *gb, int stop, int len)
60
{
61
#if 1
62
    int i;
63

    
64
    for(i = 0; i < len && get_bits1(gb) != stop; i++);
65
    return i;
66
/*  int i = 0, tmp = !stop;
67

68
  while (i != len && tmp != stop)
69
  {
70
    tmp = get_bits(gb, 1);
71
    i++;
72
  }
73
  if (i == len && tmp != stop) return len+1;
74
  return i;*/
75
#else
76
  unsigned int buf;
77
  int log;
78

    
79
  OPEN_READER(re, gb);
80
  UPDATE_CACHE(re, gb);
81
  buf=GET_CACHE(re, gb); //Still not sure
82
  if (stop) buf = ~buf;
83

    
84
  log= av_log2(-buf); //FIXME: -?
85
  if (log < limit){
86
    LAST_SKIP_BITS(re, gb, log+1);
87
    CLOSE_READER(re, gb);
88
    return log;
89
  }
90

    
91
  LAST_SKIP_BITS(re, gb, limit);
92
  CLOSE_READER(re, gb);
93
  return limit;
94
#endif
95
}
96

    
97
static inline int decode210(GetBitContext *gb){
98
    int n;
99
    n = get_bits1(gb);
100
    if (n == 1)
101
        return 0;
102
    else
103
        return 2 - get_bits1(gb);
104
}
105

    
106
/**
107
 * Init VC-1 specific tables and VC1Context members
108
 * @param v The VC1Context to initialize
109
 * @return Status
110
 */
111
static int vc1_init_common(VC1Context *v)
112
{
113
    static int done = 0;
114
    int i = 0;
115

    
116
    v->hrd_rate = v->hrd_buffer = NULL;
117

    
118
    /* VLC tables */
119
    if(!done)
120
    {
121
        done = 1;
122
        init_vlc(&vc1_bfraction_vlc, VC1_BFRACTION_VLC_BITS, 23,
123
                 vc1_bfraction_bits, 1, 1,
124
                 vc1_bfraction_codes, 1, 1, 1);
125
        init_vlc(&vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4,
126
                 vc1_norm2_bits, 1, 1,
127
                 vc1_norm2_codes, 1, 1, 1);
128
        init_vlc(&vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 64,
129
                 vc1_norm6_bits, 1, 1,
130
                 vc1_norm6_codes, 2, 2, 1);
131
        init_vlc(&vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7,
132
                 vc1_imode_bits, 1, 1,
133
                 vc1_imode_codes, 1, 1, 1);
134
        for (i=0; i<3; i++)
135
        {
136
            init_vlc(&vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 16,
137
                     vc1_ttmb_bits[i], 1, 1,
138
                     vc1_ttmb_codes[i], 2, 2, 1);
139
            init_vlc(&vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 8,
140
                     vc1_ttblk_bits[i], 1, 1,
141
                     vc1_ttblk_codes[i], 1, 1, 1);
142
            init_vlc(&vc1_subblkpat_vlc[i], VC1_SUBBLKPAT_VLC_BITS, 15,
143
                     vc1_subblkpat_bits[i], 1, 1,
144
                     vc1_subblkpat_codes[i], 1, 1, 1);
145
        }
146
        for(i=0; i<4; i++)
147
        {
148
            init_vlc(&vc1_4mv_block_pattern_vlc[i], VC1_4MV_BLOCK_PATTERN_VLC_BITS, 16,
149
                     vc1_4mv_block_pattern_bits[i], 1, 1,
150
                     vc1_4mv_block_pattern_codes[i], 1, 1, 1);
151
            init_vlc(&vc1_cbpcy_p_vlc[i], VC1_CBPCY_P_VLC_BITS, 64,
152
                     vc1_cbpcy_p_bits[i], 1, 1,
153
                     vc1_cbpcy_p_codes[i], 2, 2, 1);
154
            init_vlc(&vc1_mv_diff_vlc[i], VC1_MV_DIFF_VLC_BITS, 73,
155
                     vc1_mv_diff_bits[i], 1, 1,
156
                     vc1_mv_diff_codes[i], 2, 2, 1);
157
        }
158
        for(i=0; i<8; i++)
159
            init_vlc(&vc1_ac_coeff_table[i], AC_VLC_BITS, vc1_ac_sizes[i],
160
                     &vc1_ac_tables[i][0][1], 8, 4,
161
                     &vc1_ac_tables[i][0][0], 8, 4, 1);
162
        init_vlc(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64,
163
                 &ff_msmp4_mb_i_table[0][1], 4, 2,
164
                 &ff_msmp4_mb_i_table[0][0], 4, 2, 1);
165
    }
166

    
167
    /* Other defaults */
168
    v->pq = -1;
169
    v->mvrange = 0; /* 7.1.1.18, p80 */
170

    
171
    return 0;
172
}
173

    
174
/***********************************************************************/
175
/**
176
 * @defgroup bitplane VC9 Bitplane decoding
177
 * @see 8.7, p56
178
 * @{
179
 */
180

    
181
/** @addtogroup bitplane
182
 * Imode types
183
 * @{
184
 */
185
enum Imode {
186
    IMODE_RAW,
187
    IMODE_NORM2,
188
    IMODE_DIFF2,
189
    IMODE_NORM6,
190
    IMODE_DIFF6,
191
    IMODE_ROWSKIP,
192
    IMODE_COLSKIP
193
};
194
/** @} */ //imode defines
195

    
196
/** Decode rows by checking if they are skipped
197
 * @param plane Buffer to store decoded bits
198
 * @param[in] width Width of this buffer
199
 * @param[in] height Height of this buffer
200
 * @param[in] stride of this buffer
201
 */
202
static void decode_rowskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
203
    int x, y;
204

    
205
    for (y=0; y<height; y++){
206
        if (!get_bits(gb, 1)) //rowskip
207
            memset(plane, 0, width);
208
        else
209
            for (x=0; x<width; x++)
210
                plane[x] = get_bits(gb, 1);
211
        plane += stride;
212
    }
213
}
214

    
215
/** Decode columns by checking if they are skipped
216
 * @param plane Buffer to store decoded bits
217
 * @param[in] width Width of this buffer
218
 * @param[in] height Height of this buffer
219
 * @param[in] stride of this buffer
220
 * @fixme FIXME: Optimize
221
 */
222
static void decode_colskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
223
    int x, y;
224

    
225
    for (x=0; x<width; x++){
226
        if (!get_bits(gb, 1)) //colskip
227
            for (y=0; y<height; y++)
228
                plane[y*stride] = 0;
229
        else
230
            for (y=0; y<height; y++)
231
                plane[y*stride] = get_bits(gb, 1);
232
        plane ++;
233
    }
234
}
235

    
236
/** Decode a bitplane's bits
237
 * @param bp Bitplane where to store the decode bits
238
 * @param v VC-1 context for bit reading and logging
239
 * @return Status
240
 * @fixme FIXME: Optimize
241
 */
242
static int bitplane_decoding(uint8_t* data, int *raw_flag, VC1Context *v)
243
{
244
    GetBitContext *gb = &v->s.gb;
245

    
246
    int imode, x, y, code, offset;
247
    uint8_t invert, *planep = data;
248
    int width, height, stride;
249

    
250
    width = v->s.mb_width;
251
    height = v->s.mb_height;
252
    stride = v->s.mb_stride;
253
    invert = get_bits(gb, 1);
254
    imode = get_vlc2(gb, vc1_imode_vlc.table, VC1_IMODE_VLC_BITS, 1);
255

    
256
    *raw_flag = 0;
257
    switch (imode)
258
    {
259
    case IMODE_RAW:
260
        //Data is actually read in the MB layer (same for all tests == "raw")
261
        *raw_flag = 1; //invert ignored
262
        return invert;
263
    case IMODE_DIFF2:
264
    case IMODE_NORM2:
265
        if ((height * width) & 1)
266
        {
267
            *planep++ = get_bits(gb, 1);
268
            offset = 1;
269
        }
270
        else offset = 0;
271
        // decode bitplane as one long line
272
        for (y = offset; y < height * width; y += 2) {
273
            code = get_vlc2(gb, vc1_norm2_vlc.table, VC1_NORM2_VLC_BITS, 1);
274
            *planep++ = code & 1;
275
            offset++;
276
            if(offset == width) {
277
                offset = 0;
278
                planep += stride - width;
279
            }
280
            *planep++ = code >> 1;
281
            offset++;
282
            if(offset == width) {
283
                offset = 0;
284
                planep += stride - width;
285
            }
286
        }
287
        break;
288
    case IMODE_DIFF6:
289
    case IMODE_NORM6:
290
        if(!(height % 3) && (width % 3)) { // use 2x3 decoding
291
            for(y = 0; y < height; y+= 3) {
292
                for(x = width & 1; x < width; x += 2) {
293
                    code = get_vlc2(gb, vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
294
                    if(code < 0){
295
                        av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
296
                        return -1;
297
                    }
298
                    planep[x + 0] = (code >> 0) & 1;
299
                    planep[x + 1] = (code >> 1) & 1;
300
                    planep[x + 0 + stride] = (code >> 2) & 1;
301
                    planep[x + 1 + stride] = (code >> 3) & 1;
302
                    planep[x + 0 + stride * 2] = (code >> 4) & 1;
303
                    planep[x + 1 + stride * 2] = (code >> 5) & 1;
304
                }
305
                planep += stride * 3;
306
            }
307
            if(width & 1) decode_colskip(data, 1, height, stride, &v->s.gb);
308
        } else { // 3x2
309
            planep += (height & 1) * stride;
310
            for(y = height & 1; y < height; y += 2) {
311
                for(x = width % 3; x < width; x += 3) {
312
                    code = get_vlc2(gb, vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
313
                    if(code < 0){
314
                        av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
315
                        return -1;
316
                    }
317
                    planep[x + 0] = (code >> 0) & 1;
318
                    planep[x + 1] = (code >> 1) & 1;
319
                    planep[x + 2] = (code >> 2) & 1;
320
                    planep[x + 0 + stride] = (code >> 3) & 1;
321
                    planep[x + 1 + stride] = (code >> 4) & 1;
322
                    planep[x + 2 + stride] = (code >> 5) & 1;
323
                }
324
                planep += stride * 2;
325
            }
326
            x = width % 3;
327
            if(x) decode_colskip(data  ,             x, height    , stride, &v->s.gb);
328
            if(height & 1) decode_rowskip(data+x, width - x, 1, stride, &v->s.gb);
329
        }
330
        break;
331
    case IMODE_ROWSKIP:
332
        decode_rowskip(data, width, height, stride, &v->s.gb);
333
        break;
334
    case IMODE_COLSKIP:
335
        decode_colskip(data, width, height, stride, &v->s.gb);
336
        break;
337
    default: break;
338
    }
339

    
340
    /* Applying diff operator */
341
    if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6)
342
    {
343
        planep = data;
344
        planep[0] ^= invert;
345
        for (x=1; x<width; x++)
346
            planep[x] ^= planep[x-1];
347
        for (y=1; y<height; y++)
348
        {
349
            planep += stride;
350
            planep[0] ^= planep[-stride];
351
            for (x=1; x<width; x++)
352
            {
353
                if (planep[x-1] != planep[x-stride]) planep[x] ^= invert;
354
                else                                 planep[x] ^= planep[x-1];
355
            }
356
        }
357
    }
358
    else if (invert)
359
    {
360
        planep = data;
361
        for (x=0; x<stride*height; x++) planep[x] = !planep[x]; //FIXME stride
362
    }
363
    return (imode<<1) + invert;
364
}
365

    
366
/** @} */ //Bitplane group
367

    
368
/***********************************************************************/
369
/** VOP Dquant decoding
370
 * @param v VC-1 Context
371
 */
372
static int vop_dquant_decoding(VC1Context *v)
373
{
374
    GetBitContext *gb = &v->s.gb;
375
    int pqdiff;
376

    
377
    //variable size
378
    if (v->dquant == 2)
379
    {
380
        pqdiff = get_bits(gb, 3);
381
        if (pqdiff == 7) v->altpq = get_bits(gb, 5);
382
        else v->altpq = v->pq + pqdiff + 1;
383
    }
384
    else
385
    {
386
        v->dquantfrm = get_bits(gb, 1);
387
        if ( v->dquantfrm )
388
        {
389
            v->dqprofile = get_bits(gb, 2);
390
            switch (v->dqprofile)
391
            {
392
            case DQPROFILE_SINGLE_EDGE:
393
            case DQPROFILE_DOUBLE_EDGES:
394
                v->dqsbedge = get_bits(gb, 2);
395
                break;
396
            case DQPROFILE_ALL_MBS:
397
                v->dqbilevel = get_bits(gb, 1);
398
            default: break; //Forbidden ?
399
            }
400
            if (v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS)
401
            {
402
                pqdiff = get_bits(gb, 3);
403
                if (pqdiff == 7) v->altpq = get_bits(gb, 5);
404
                else v->altpq = v->pq + pqdiff + 1;
405
            }
406
        }
407
    }
408
    return 0;
409
}
410

    
411
/** Put block onto picture
412
 */
413
static void vc1_put_block(VC1Context *v, DCTELEM block[6][64])
414
{
415
    uint8_t *Y;
416
    int ys, us, vs;
417
    DSPContext *dsp = &v->s.dsp;
418

    
419
    if(v->rangeredfrm) {
420
        int i, j, k;
421
        for(k = 0; k < 6; k++)
422
            for(j = 0; j < 8; j++)
423
                for(i = 0; i < 8; i++)
424
                    block[k][i + j*8] = ((block[k][i + j*8] - 128) << 1) + 128;
425

    
426
    }
427
    ys = v->s.current_picture.linesize[0];
428
    us = v->s.current_picture.linesize[1];
429
    vs = v->s.current_picture.linesize[2];
430
    Y = v->s.dest[0];
431

    
432
    dsp->put_pixels_clamped(block[0], Y, ys);
433
    dsp->put_pixels_clamped(block[1], Y + 8, ys);
434
    Y += ys * 8;
435
    dsp->put_pixels_clamped(block[2], Y, ys);
436
    dsp->put_pixels_clamped(block[3], Y + 8, ys);
437

    
438
    if(!(v->s.flags & CODEC_FLAG_GRAY)) {
439
        dsp->put_pixels_clamped(block[4], v->s.dest[1], us);
440
        dsp->put_pixels_clamped(block[5], v->s.dest[2], vs);
441
    }
442
}
443

    
444
/** Do motion compensation over 1 macroblock
445
 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
446
 */
447
static void vc1_mc_1mv(VC1Context *v, int dir)
448
{
449
    MpegEncContext *s = &v->s;
450
    DSPContext *dsp = &v->s.dsp;
451
    uint8_t *srcY, *srcU, *srcV;
452
    int dxy, uvdxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
453

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

    
456
    mx = s->mv[dir][0][0];
457
    my = s->mv[dir][0][1];
458

    
459
    // store motion vectors for further use in B frames
460
    if(s->pict_type == P_TYPE) {
461
        s->current_picture.motion_val[1][s->block_index[0]][0] = mx;
462
        s->current_picture.motion_val[1][s->block_index[0]][1] = my;
463
    }
464
    uvmx = (mx + ((mx & 3) == 3)) >> 1;
465
    uvmy = (my + ((my & 3) == 3)) >> 1;
466
    if(v->fastuvmc) {
467
        uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
468
        uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
469
    }
470
    if(!dir) {
471
        srcY = s->last_picture.data[0];
472
        srcU = s->last_picture.data[1];
473
        srcV = s->last_picture.data[2];
474
    } else {
475
        srcY = s->next_picture.data[0];
476
        srcU = s->next_picture.data[1];
477
        srcV = s->next_picture.data[2];
478
    }
479

    
480
    src_x = s->mb_x * 16 + (mx >> 2);
481
    src_y = s->mb_y * 16 + (my >> 2);
482
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
483
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
484

    
485
    if(v->profile != PROFILE_ADVANCED){
486
        src_x   = av_clip(  src_x, -16, s->mb_width  * 16);
487
        src_y   = av_clip(  src_y, -16, s->mb_height * 16);
488
        uvsrc_x = av_clip(uvsrc_x,  -8, s->mb_width  *  8);
489
        uvsrc_y = av_clip(uvsrc_y,  -8, s->mb_height *  8);
490
    }else{
491
        src_x   = av_clip(  src_x, -17, s->avctx->coded_width);
492
        src_y   = av_clip(  src_y, -18, s->avctx->coded_height + 1);
493
        uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
494
        uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
495
    }
496

    
497
    srcY += src_y * s->linesize + src_x;
498
    srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
499
    srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
500

    
501
    /* for grayscale we should not try to read from unknown area */
502
    if(s->flags & CODEC_FLAG_GRAY) {
503
        srcU = s->edge_emu_buffer + 18 * s->linesize;
504
        srcV = s->edge_emu_buffer + 18 * s->linesize;
505
    }
506

    
507
    if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
508
       || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3
509
       || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){
510
        uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
511

    
512
        srcY -= s->mspel * (1 + s->linesize);
513
        ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
514
                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
515
        srcY = s->edge_emu_buffer;
516
        ff_emulated_edge_mc(uvbuf     , srcU, s->uvlinesize, 8+1, 8+1,
517
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
518
        ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
519
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
520
        srcU = uvbuf;
521
        srcV = uvbuf + 16;
522
        /* if we deal with range reduction we need to scale source blocks */
523
        if(v->rangeredfrm) {
524
            int i, j;
525
            uint8_t *src, *src2;
526

    
527
            src = srcY;
528
            for(j = 0; j < 17 + s->mspel*2; j++) {
529
                for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
530
                src += s->linesize;
531
            }
532
            src = srcU; src2 = srcV;
533
            for(j = 0; j < 9; j++) {
534
                for(i = 0; i < 9; i++) {
535
                    src[i] = ((src[i] - 128) >> 1) + 128;
536
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
537
                }
538
                src += s->uvlinesize;
539
                src2 += s->uvlinesize;
540
            }
541
        }
542
        /* if we deal with intensity compensation we need to scale source blocks */
543
        if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
544
            int i, j;
545
            uint8_t *src, *src2;
546

    
547
            src = srcY;
548
            for(j = 0; j < 17 + s->mspel*2; j++) {
549
                for(i = 0; i < 17 + s->mspel*2; i++) src[i] = v->luty[src[i]];
550
                src += s->linesize;
551
            }
552
            src = srcU; src2 = srcV;
553
            for(j = 0; j < 9; j++) {
554
                for(i = 0; i < 9; i++) {
555
                    src[i] = v->lutuv[src[i]];
556
                    src2[i] = v->lutuv[src2[i]];
557
                }
558
                src += s->uvlinesize;
559
                src2 += s->uvlinesize;
560
            }
561
        }
562
        srcY += s->mspel * (1 + s->linesize);
563
    }
564

    
565
    if(s->mspel) {
566
        dxy = ((my & 3) << 2) | (mx & 3);
567
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0]    , srcY    , s->linesize, v->rnd);
568
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
569
        srcY += s->linesize * 8;
570
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize    , srcY    , s->linesize, v->rnd);
571
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
572
    } else { // hpel mc - always used for luma
573
        dxy = (my & 2) | ((mx & 2) >> 1);
574

    
575
        if(!v->rnd)
576
            dsp->put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
577
        else
578
            dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
579
    }
580

    
581
    if(s->flags & CODEC_FLAG_GRAY) return;
582
    /* Chroma MC always uses qpel bilinear */
583
    uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
584
    uvmx = (uvmx&3)<<1;
585
    uvmy = (uvmy&3)<<1;
586
    if(!v->rnd){
587
        dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
588
        dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
589
    }else{
590
        dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
591
        dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
592
    }
593
}
594

    
595
/** Do motion compensation for 4-MV macroblock - luminance block
596
 */
597
static void vc1_mc_4mv_luma(VC1Context *v, int n)
598
{
599
    MpegEncContext *s = &v->s;
600
    DSPContext *dsp = &v->s.dsp;
601
    uint8_t *srcY;
602
    int dxy, mx, my, src_x, src_y;
603
    int off;
604

    
605
    if(!v->s.last_picture.data[0])return;
606
    mx = s->mv[0][n][0];
607
    my = s->mv[0][n][1];
608
    srcY = s->last_picture.data[0];
609

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

    
612
    src_x = s->mb_x * 16 + (n&1) * 8 + (mx >> 2);
613
    src_y = s->mb_y * 16 + (n&2) * 4 + (my >> 2);
614

    
615
    if(v->profile != PROFILE_ADVANCED){
616
        src_x   = av_clip(  src_x, -16, s->mb_width  * 16);
617
        src_y   = av_clip(  src_y, -16, s->mb_height * 16);
618
    }else{
619
        src_x   = av_clip(  src_x, -17, s->avctx->coded_width);
620
        src_y   = av_clip(  src_y, -18, s->avctx->coded_height + 1);
621
    }
622

    
623
    srcY += src_y * s->linesize + src_x;
624

    
625
    if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
626
       || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 8 - s->mspel*2
627
       || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 8 - s->mspel*2){
628
        srcY -= s->mspel * (1 + s->linesize);
629
        ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 9+s->mspel*2, 9+s->mspel*2,
630
                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
631
        srcY = s->edge_emu_buffer;
632
        /* if we deal with range reduction we need to scale source blocks */
633
        if(v->rangeredfrm) {
634
            int i, j;
635
            uint8_t *src;
636

    
637
            src = srcY;
638
            for(j = 0; j < 9 + s->mspel*2; j++) {
639
                for(i = 0; i < 9 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
640
                src += s->linesize;
641
            }
642
        }
643
        /* if we deal with intensity compensation we need to scale source blocks */
644
        if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
645
            int i, j;
646
            uint8_t *src;
647

    
648
            src = srcY;
649
            for(j = 0; j < 9 + s->mspel*2; j++) {
650
                for(i = 0; i < 9 + s->mspel*2; i++) src[i] = v->luty[src[i]];
651
                src += s->linesize;
652
            }
653
        }
654
        srcY += s->mspel * (1 + s->linesize);
655
    }
656

    
657
    if(s->mspel) {
658
        dxy = ((my & 3) << 2) | (mx & 3);
659
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, v->rnd);
660
    } else { // hpel mc - always used for luma
661
        dxy = (my & 2) | ((mx & 2) >> 1);
662
        if(!v->rnd)
663
            dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
664
        else
665
            dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
666
    }
667
}
668

    
669
static inline int median4(int a, int b, int c, int d)
670
{
671
    if(a < b) {
672
        if(c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
673
        else      return (FFMIN(b, c) + FFMAX(a, d)) / 2;
674
    } else {
675
        if(c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
676
        else      return (FFMIN(a, c) + FFMAX(b, d)) / 2;
677
    }
678
}
679

    
680

    
681
/** Do motion compensation for 4-MV macroblock - both chroma blocks
682
 */
683
static void vc1_mc_4mv_chroma(VC1Context *v)
684
{
685
    MpegEncContext *s = &v->s;
686
    DSPContext *dsp = &v->s.dsp;
687
    uint8_t *srcU, *srcV;
688
    int uvdxy, uvmx, uvmy, uvsrc_x, uvsrc_y;
689
    int i, idx, tx = 0, ty = 0;
690
    int mvx[4], mvy[4], intra[4];
691
    static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
692

    
693
    if(!v->s.last_picture.data[0])return;
694
    if(s->flags & CODEC_FLAG_GRAY) return;
695

    
696
    for(i = 0; i < 4; i++) {
697
        mvx[i] = s->mv[0][i][0];
698
        mvy[i] = s->mv[0][i][1];
699
        intra[i] = v->mb_type[0][s->block_index[i]];
700
    }
701

    
702
    /* calculate chroma MV vector from four luma MVs */
703
    idx = (intra[3] << 3) | (intra[2] << 2) | (intra[1] << 1) | intra[0];
704
    if(!idx) { // all blocks are inter
705
        tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
706
        ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
707
    } else if(count[idx] == 1) { // 3 inter blocks
708
        switch(idx) {
709
        case 0x1:
710
            tx = mid_pred(mvx[1], mvx[2], mvx[3]);
711
            ty = mid_pred(mvy[1], mvy[2], mvy[3]);
712
            break;
713
        case 0x2:
714
            tx = mid_pred(mvx[0], mvx[2], mvx[3]);
715
            ty = mid_pred(mvy[0], mvy[2], mvy[3]);
716
            break;
717
        case 0x4:
718
            tx = mid_pred(mvx[0], mvx[1], mvx[3]);
719
            ty = mid_pred(mvy[0], mvy[1], mvy[3]);
720
            break;
721
        case 0x8:
722
            tx = mid_pred(mvx[0], mvx[1], mvx[2]);
723
            ty = mid_pred(mvy[0], mvy[1], mvy[2]);
724
            break;
725
        }
726
    } else if(count[idx] == 2) {
727
        int t1 = 0, t2 = 0;
728
        for(i=0; i<3;i++) if(!intra[i]) {t1 = i; break;}
729
        for(i= t1+1; i<4; i++)if(!intra[i]) {t2 = i; break;}
730
        tx = (mvx[t1] + mvx[t2]) / 2;
731
        ty = (mvy[t1] + mvy[t2]) / 2;
732
    } else {
733
        s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
734
        s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
735
        return; //no need to do MC for inter blocks
736
    }
737

    
738
    s->current_picture.motion_val[1][s->block_index[0]][0] = tx;
739
    s->current_picture.motion_val[1][s->block_index[0]][1] = ty;
740
    uvmx = (tx + ((tx&3) == 3)) >> 1;
741
    uvmy = (ty + ((ty&3) == 3)) >> 1;
742
    if(v->fastuvmc) {
743
        uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
744
        uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
745
    }
746

    
747
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
748
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
749

    
750
    if(v->profile != PROFILE_ADVANCED){
751
        uvsrc_x = av_clip(uvsrc_x,  -8, s->mb_width  *  8);
752
        uvsrc_y = av_clip(uvsrc_y,  -8, s->mb_height *  8);
753
    }else{
754
        uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
755
        uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
756
    }
757

    
758
    srcU = s->last_picture.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
759
    srcV = s->last_picture.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
760
    if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
761
       || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
762
       || (unsigned)uvsrc_y > (s->v_edge_pos >> 1) - 9){
763
        ff_emulated_edge_mc(s->edge_emu_buffer     , srcU, s->uvlinesize, 8+1, 8+1,
764
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
765
        ff_emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize, 8+1, 8+1,
766
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
767
        srcU = s->edge_emu_buffer;
768
        srcV = s->edge_emu_buffer + 16;
769

    
770
        /* if we deal with range reduction we need to scale source blocks */
771
        if(v->rangeredfrm) {
772
            int i, j;
773
            uint8_t *src, *src2;
774

    
775
            src = srcU; src2 = srcV;
776
            for(j = 0; j < 9; j++) {
777
                for(i = 0; i < 9; i++) {
778
                    src[i] = ((src[i] - 128) >> 1) + 128;
779
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
780
                }
781
                src += s->uvlinesize;
782
                src2 += s->uvlinesize;
783
            }
784
        }
785
        /* if we deal with intensity compensation we need to scale source blocks */
786
        if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
787
            int i, j;
788
            uint8_t *src, *src2;
789

    
790
            src = srcU; src2 = srcV;
791
            for(j = 0; j < 9; j++) {
792
                for(i = 0; i < 9; i++) {
793
                    src[i] = v->lutuv[src[i]];
794
                    src2[i] = v->lutuv[src2[i]];
795
                }
796
                src += s->uvlinesize;
797
                src2 += s->uvlinesize;
798
            }
799
        }
800
    }
801

    
802
    /* Chroma MC always uses qpel bilinear */
803
    uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
804
    uvmx = (uvmx&3)<<1;
805
    uvmy = (uvmy&3)<<1;
806
    if(!v->rnd){
807
        dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
808
        dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
809
    }else{
810
        dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
811
        dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
812
    }
813
}
814

    
815
static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb);
816

    
817
/**
818
 * Decode Simple/Main Profiles sequence header
819
 * @see Figure 7-8, p16-17
820
 * @param avctx Codec context
821
 * @param gb GetBit context initialized from Codec context extra_data
822
 * @return Status
823
 */
824
static int decode_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
825
{
826
    VC1Context *v = avctx->priv_data;
827

    
828
    av_log(avctx, AV_LOG_DEBUG, "Header: %0X\n", show_bits(gb, 32));
829
    v->profile = get_bits(gb, 2);
830
    if (v->profile == PROFILE_COMPLEX)
831
    {
832
        av_log(avctx, AV_LOG_ERROR, "WMV3 Complex Profile is not fully supported\n");
833
    }
834

    
835
    if (v->profile == PROFILE_ADVANCED)
836
    {
837
        return decode_sequence_header_adv(v, gb);
838
    }
839
    else
840
    {
841
        v->res_sm = get_bits(gb, 2); //reserved
842
        if (v->res_sm)
843
        {
844
            av_log(avctx, AV_LOG_ERROR,
845
                   "Reserved RES_SM=%i is forbidden\n", v->res_sm);
846
            return -1;
847
        }
848
    }
849

    
850
    // (fps-2)/4 (->30)
851
    v->frmrtq_postproc = get_bits(gb, 3); //common
852
    // (bitrate-32kbps)/64kbps
853
    v->bitrtq_postproc = get_bits(gb, 5); //common
854
    v->s.loop_filter = get_bits(gb, 1); //common
855
    if(v->s.loop_filter == 1 && v->profile == PROFILE_SIMPLE)
856
    {
857
        av_log(avctx, AV_LOG_ERROR,
858
               "LOOPFILTER shell not be enabled in simple profile\n");
859
    }
860

    
861
    v->res_x8 = get_bits(gb, 1); //reserved
862
    if (v->res_x8)
863
    {
864
        av_log(avctx, AV_LOG_ERROR,
865
               "1 for reserved RES_X8 is forbidden\n");
866
        //return -1;
867
    }
868
    v->multires = get_bits(gb, 1);
869
    v->res_fasttx = get_bits(gb, 1);
870
    if (!v->res_fasttx)
871
    {
872
        av_log(avctx, AV_LOG_ERROR,
873
               "0 for reserved RES_FASTTX is forbidden\n");
874
        //return -1;
875
    }
876

    
877
    v->fastuvmc =  get_bits(gb, 1); //common
878
    if (!v->profile && !v->fastuvmc)
879
    {
880
        av_log(avctx, AV_LOG_ERROR,
881
               "FASTUVMC unavailable in Simple Profile\n");
882
        return -1;
883
    }
884
    v->extended_mv =  get_bits(gb, 1); //common
885
    if (!v->profile && v->extended_mv)
886
    {
887
        av_log(avctx, AV_LOG_ERROR,
888
               "Extended MVs unavailable in Simple Profile\n");
889
        return -1;
890
    }
891
    v->dquant =  get_bits(gb, 2); //common
892
    v->vstransform =  get_bits(gb, 1); //common
893

    
894
    v->res_transtab = get_bits(gb, 1);
895
    if (v->res_transtab)
896
    {
897
        av_log(avctx, AV_LOG_ERROR,
898
               "1 for reserved RES_TRANSTAB is forbidden\n");
899
        return -1;
900
    }
901

    
902
    v->overlap = get_bits(gb, 1); //common
903

    
904
    v->s.resync_marker = get_bits(gb, 1);
905
    v->rangered = get_bits(gb, 1);
906
    if (v->rangered && v->profile == PROFILE_SIMPLE)
907
    {
908
        av_log(avctx, AV_LOG_INFO,
909
               "RANGERED should be set to 0 in simple profile\n");
910
    }
911

    
912
    v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
913
    v->quantizer_mode = get_bits(gb, 2); //common
914

    
915
    v->finterpflag = get_bits(gb, 1); //common
916
    v->res_rtm_flag = get_bits(gb, 1); //reserved
917
    if (!v->res_rtm_flag)
918
    {
919
//            av_log(avctx, AV_LOG_ERROR,
920
//                   "0 for reserved RES_RTM_FLAG is forbidden\n");
921
        av_log(avctx, AV_LOG_ERROR,
922
               "Old WMV3 version detected, only I-frames will be decoded\n");
923
        //return -1;
924
    }
925
    //TODO: figure out what they mean (always 0x402F)
926
    if(!v->res_fasttx) skip_bits(gb, 16);
927
    av_log(avctx, AV_LOG_DEBUG,
928
               "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
929
               "LoopFilter=%i, MultiRes=%i, FastUVMC=%i, Extended MV=%i\n"
930
               "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
931
               "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
932
               v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
933
               v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
934
               v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
935
               v->dquant, v->quantizer_mode, avctx->max_b_frames
936
               );
937
    return 0;
938
}
939

    
940
static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb)
941
{
942
    v->res_rtm_flag = 1;
943
    v->level = get_bits(gb, 3);
944
    if(v->level >= 5)
945
    {
946
        av_log(v->s.avctx, AV_LOG_ERROR, "Reserved LEVEL %i\n",v->level);
947
    }
948
    v->chromaformat = get_bits(gb, 2);
949
    if (v->chromaformat != 1)
950
    {
951
        av_log(v->s.avctx, AV_LOG_ERROR,
952
               "Only 4:2:0 chroma format supported\n");
953
        return -1;
954
    }
955

    
956
    // (fps-2)/4 (->30)
957
    v->frmrtq_postproc = get_bits(gb, 3); //common
958
    // (bitrate-32kbps)/64kbps
959
    v->bitrtq_postproc = get_bits(gb, 5); //common
960
    v->postprocflag = get_bits(gb, 1); //common
961

    
962
    v->s.avctx->coded_width = (get_bits(gb, 12) + 1) << 1;
963
    v->s.avctx->coded_height = (get_bits(gb, 12) + 1) << 1;
964
    v->s.avctx->width = v->s.avctx->coded_width;
965
    v->s.avctx->height = v->s.avctx->coded_height;
966
    v->broadcast = get_bits1(gb);
967
    v->interlace = get_bits1(gb);
968
    v->tfcntrflag = get_bits1(gb);
969
    v->finterpflag = get_bits1(gb);
970
    get_bits1(gb); // reserved
971

    
972
    v->s.h_edge_pos = v->s.avctx->coded_width;
973
    v->s.v_edge_pos = v->s.avctx->coded_height;
974

    
975
    av_log(v->s.avctx, AV_LOG_DEBUG,
976
               "Advanced Profile level %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
977
               "LoopFilter=%i, ChromaFormat=%i, Pulldown=%i, Interlace: %i\n"
978
               "TFCTRflag=%i, FINTERPflag=%i\n",
979
               v->level, v->frmrtq_postproc, v->bitrtq_postproc,
980
               v->s.loop_filter, v->chromaformat, v->broadcast, v->interlace,
981
               v->tfcntrflag, v->finterpflag
982
               );
983

    
984
    v->psf = get_bits1(gb);
985
    if(v->psf) { //PsF, 6.1.13
986
        av_log(v->s.avctx, AV_LOG_ERROR, "Progressive Segmented Frame mode: not supported (yet)\n");
987
        return -1;
988
    }
989
    v->s.max_b_frames = v->s.avctx->max_b_frames = 7;
990
    if(get_bits1(gb)) { //Display Info - decoding is not affected by it
991
        int w, h, ar = 0;
992
        av_log(v->s.avctx, AV_LOG_DEBUG, "Display extended info:\n");
993
        v->s.avctx->width  = v->s.width  = w = get_bits(gb, 14) + 1;
994
        v->s.avctx->height = v->s.height = h = get_bits(gb, 14) + 1;
995
        av_log(v->s.avctx, AV_LOG_DEBUG, "Display dimensions: %ix%i\n", w, h);
996
        if(get_bits1(gb))
997
            ar = get_bits(gb, 4);
998
        if(ar && ar < 14){
999
            v->s.avctx->sample_aspect_ratio = vc1_pixel_aspect[ar];
1000
        }else if(ar == 15){
1001
            w = get_bits(gb, 8);
1002
            h = get_bits(gb, 8);
1003
            v->s.avctx->sample_aspect_ratio = (AVRational){w, h};
1004
        }
1005

    
1006
        if(get_bits1(gb)){ //framerate stuff
1007
            if(get_bits1(gb)) {
1008
                v->s.avctx->time_base.num = 32;
1009
                v->s.avctx->time_base.den = get_bits(gb, 16) + 1;
1010
            } else {
1011
                int nr, dr;
1012
                nr = get_bits(gb, 8);
1013
                dr = get_bits(gb, 4);
1014
                if(nr && nr < 8 && dr && dr < 3){
1015
                    v->s.avctx->time_base.num = fps_dr[dr - 1];
1016
                    v->s.avctx->time_base.den = fps_nr[nr - 1] * 1000;
1017
                }
1018
            }
1019
        }
1020

    
1021
        if(get_bits1(gb)){
1022
            v->color_prim = get_bits(gb, 8);
1023
            v->transfer_char = get_bits(gb, 8);
1024
            v->matrix_coef = get_bits(gb, 8);
1025
        }
1026
    }
1027

    
1028
    v->hrd_param_flag = get_bits1(gb);
1029
    if(v->hrd_param_flag) {
1030
        int i;
1031
        v->hrd_num_leaky_buckets = get_bits(gb, 5);
1032
        get_bits(gb, 4); //bitrate exponent
1033
        get_bits(gb, 4); //buffer size exponent
1034
        for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
1035
            get_bits(gb, 16); //hrd_rate[n]
1036
            get_bits(gb, 16); //hrd_buffer[n]
1037
        }
1038
    }
1039
    return 0;
1040
}
1041

    
1042
static int decode_entry_point(AVCodecContext *avctx, GetBitContext *gb)
1043
{
1044
    VC1Context *v = avctx->priv_data;
1045
    int i, blink, clentry, refdist;
1046

    
1047
    av_log(avctx, AV_LOG_DEBUG, "Entry point: %08X\n", show_bits_long(gb, 32));
1048
    blink = get_bits1(gb); // broken link
1049
    clentry = get_bits1(gb); // closed entry
1050
    v->panscanflag = get_bits1(gb);
1051
    refdist = get_bits1(gb); // refdist flag
1052
    v->s.loop_filter = get_bits1(gb);
1053
    v->fastuvmc = get_bits1(gb);
1054
    v->extended_mv = get_bits1(gb);
1055
    v->dquant = get_bits(gb, 2);
1056
    v->vstransform = get_bits1(gb);
1057
    v->overlap = get_bits1(gb);
1058
    v->quantizer_mode = get_bits(gb, 2);
1059

    
1060
    if(v->hrd_param_flag){
1061
        for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
1062
            get_bits(gb, 8); //hrd_full[n]
1063
        }
1064
    }
1065

    
1066
    if(get_bits1(gb)){
1067
        avctx->coded_width = (get_bits(gb, 12)+1)<<1;
1068
        avctx->coded_height = (get_bits(gb, 12)+1)<<1;
1069
    }
1070
    if(v->extended_mv)
1071
        v->extended_dmv = get_bits1(gb);
1072
    if(get_bits1(gb)) {
1073
        av_log(avctx, AV_LOG_ERROR, "Luma scaling is not supported, expect wrong picture\n");
1074
        skip_bits(gb, 3); // Y range, ignored for now
1075
    }
1076
    if(get_bits1(gb)) {
1077
        av_log(avctx, AV_LOG_ERROR, "Chroma scaling is not supported, expect wrong picture\n");
1078
        skip_bits(gb, 3); // UV range, ignored for now
1079
    }
1080

    
1081
    av_log(avctx, AV_LOG_DEBUG, "Entry point info:\n"
1082
        "BrokenLink=%i, ClosedEntry=%i, PanscanFlag=%i\n"
1083
        "RefDist=%i, Postproc=%i, FastUVMC=%i, ExtMV=%i\n"
1084
        "DQuant=%i, VSTransform=%i, Overlap=%i, Qmode=%i\n",
1085
        blink, clentry, v->panscanflag, refdist, v->s.loop_filter,
1086
        v->fastuvmc, v->extended_mv, v->dquant, v->vstransform, v->overlap, v->quantizer_mode);
1087

    
1088
    return 0;
1089
}
1090

    
1091
static int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
1092
{
1093
    int pqindex, lowquant, status;
1094

    
1095
    if(v->finterpflag) v->interpfrm = get_bits(gb, 1);
1096
    skip_bits(gb, 2); //framecnt unused
1097
    v->rangeredfrm = 0;
1098
    if (v->rangered) v->rangeredfrm = get_bits(gb, 1);
1099
    v->s.pict_type = get_bits(gb, 1);
1100
    if (v->s.avctx->max_b_frames) {
1101
        if (!v->s.pict_type) {
1102
            if (get_bits(gb, 1)) v->s.pict_type = I_TYPE;
1103
            else v->s.pict_type = B_TYPE;
1104
        } else v->s.pict_type = P_TYPE;
1105
    } else v->s.pict_type = v->s.pict_type ? P_TYPE : I_TYPE;
1106

    
1107
    v->bi_type = 0;
1108
    if(v->s.pict_type == B_TYPE) {
1109
        v->bfraction = get_vlc2(gb, vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
1110
        v->bfraction = vc1_bfraction_lut[v->bfraction];
1111
        if(v->bfraction == 0) {
1112
            v->s.pict_type = BI_TYPE;
1113
        }
1114
    }
1115
    if(v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
1116
        get_bits(gb, 7); // skip buffer fullness
1117

    
1118
    /* calculate RND */
1119
    if(v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
1120
        v->rnd = 1;
1121
    if(v->s.pict_type == P_TYPE)
1122
        v->rnd ^= 1;
1123

    
1124
    /* Quantizer stuff */
1125
    pqindex = get_bits(gb, 5);
1126
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1127
        v->pq = pquant_table[0][pqindex];
1128
    else
1129
        v->pq = pquant_table[1][pqindex];
1130

    
1131
    v->pquantizer = 1;
1132
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1133
        v->pquantizer = pqindex < 9;
1134
    if (v->quantizer_mode == QUANT_NON_UNIFORM)
1135
        v->pquantizer = 0;
1136
    v->pqindex = pqindex;
1137
    if (pqindex < 9) v->halfpq = get_bits(gb, 1);
1138
    else v->halfpq = 0;
1139
    if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
1140
        v->pquantizer = get_bits(gb, 1);
1141
    v->dquantfrm = 0;
1142
    if (v->extended_mv == 1) v->mvrange = get_prefix(gb, 0, 3);
1143
    v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1144
    v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
1145
    v->range_x = 1 << (v->k_x - 1);
1146
    v->range_y = 1 << (v->k_y - 1);
1147
    if (v->profile == PROFILE_ADVANCED)
1148
    {
1149
        if (v->postprocflag) v->postproc = get_bits(gb, 1);
1150
    }
1151
    else
1152
        if (v->multires && v->s.pict_type != B_TYPE) v->respic = get_bits(gb, 2);
1153

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

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

    
1162
    switch(v->s.pict_type) {
1163
    case P_TYPE:
1164
        if (v->pq < 5) v->tt_index = 0;
1165
        else if(v->pq < 13) v->tt_index = 1;
1166
        else v->tt_index = 2;
1167

    
1168
        lowquant = (v->pq > 12) ? 0 : 1;
1169
        v->mv_mode = mv_pmode_table[lowquant][get_prefix(gb, 1, 4)];
1170
        if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
1171
        {
1172
            int scale, shift, i;
1173
            v->mv_mode2 = mv_pmode_table2[lowquant][get_prefix(gb, 1, 3)];
1174
            v->lumscale = get_bits(gb, 6);
1175
            v->lumshift = get_bits(gb, 6);
1176
            v->use_ic = 1;
1177
            /* fill lookup tables for intensity compensation */
1178
            if(!v->lumscale) {
1179
                scale = -64;
1180
                shift = (255 - v->lumshift * 2) << 6;
1181
                if(v->lumshift > 31)
1182
                    shift += 128 << 6;
1183
            } else {
1184
                scale = v->lumscale + 32;
1185
                if(v->lumshift > 31)
1186
                    shift = (v->lumshift - 64) << 6;
1187
                else
1188
                    shift = v->lumshift << 6;
1189
            }
1190
            for(i = 0; i < 256; i++) {
1191
                v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
1192
                v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
1193
            }
1194
        }
1195
        if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
1196
            v->s.quarter_sample = 0;
1197
        else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1198
            if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
1199
                v->s.quarter_sample = 0;
1200
            else
1201
                v->s.quarter_sample = 1;
1202
        } else
1203
            v->s.quarter_sample = 1;
1204
        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));
1205

    
1206
        if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
1207
                 v->mv_mode2 == MV_PMODE_MIXED_MV)
1208
                || v->mv_mode == MV_PMODE_MIXED_MV)
1209
        {
1210
            status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
1211
            if (status < 0) return -1;
1212
            av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
1213
                   "Imode: %i, Invert: %i\n", status>>1, status&1);
1214
        } else {
1215
            v->mv_type_is_raw = 0;
1216
            memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
1217
        }
1218
        status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1219
        if (status < 0) return -1;
1220
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1221
               "Imode: %i, Invert: %i\n", status>>1, status&1);
1222

    
1223
        /* Hopefully this is correct for P frames */
1224
        v->s.mv_table_index = get_bits(gb, 2); //but using vc1_ tables
1225
        v->cbpcy_vlc = &vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1226

    
1227
        if (v->dquant)
1228
        {
1229
            av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1230
            vop_dquant_decoding(v);
1231
        }
1232

    
1233
        v->ttfrm = 0; //FIXME Is that so ?
1234
        if (v->vstransform)
1235
        {
1236
            v->ttmbf = get_bits(gb, 1);
1237
            if (v->ttmbf)
1238
            {
1239
                v->ttfrm = ttfrm_to_tt[get_bits(gb, 2)];
1240
            }
1241
        } else {
1242
            v->ttmbf = 1;
1243
            v->ttfrm = TT_8X8;
1244
        }
1245
        break;
1246
    case B_TYPE:
1247
        if (v->pq < 5) v->tt_index = 0;
1248
        else if(v->pq < 13) v->tt_index = 1;
1249
        else v->tt_index = 2;
1250

    
1251
        lowquant = (v->pq > 12) ? 0 : 1;
1252
        v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
1253
        v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
1254
        v->s.mspel = v->s.quarter_sample;
1255

    
1256
        status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
1257
        if (status < 0) return -1;
1258
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
1259
               "Imode: %i, Invert: %i\n", status>>1, status&1);
1260
        status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1261
        if (status < 0) return -1;
1262
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1263
               "Imode: %i, Invert: %i\n", status>>1, status&1);
1264

    
1265
        v->s.mv_table_index = get_bits(gb, 2);
1266
        v->cbpcy_vlc = &vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1267

    
1268
        if (v->dquant)
1269
        {
1270
            av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1271
            vop_dquant_decoding(v);
1272
        }
1273

    
1274
        v->ttfrm = 0;
1275
        if (v->vstransform)
1276
        {
1277
            v->ttmbf = get_bits(gb, 1);
1278
            if (v->ttmbf)
1279
            {
1280
                v->ttfrm = ttfrm_to_tt[get_bits(gb, 2)];
1281
            }
1282
        } else {
1283
            v->ttmbf = 1;
1284
            v->ttfrm = TT_8X8;
1285
        }
1286
        break;
1287
    }
1288

    
1289
    /* AC Syntax */
1290
    v->c_ac_table_index = decode012(gb);
1291
    if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
1292
    {
1293
        v->y_ac_table_index = decode012(gb);
1294
    }
1295
    /* DC Syntax */
1296
    v->s.dc_table_index = get_bits(gb, 1);
1297

    
1298
    if(v->s.pict_type == BI_TYPE) {
1299
        v->s.pict_type = B_TYPE;
1300
        v->bi_type = 1;
1301
    }
1302
    return 0;
1303
}
1304

    
1305
static int vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb)
1306
{
1307
    int pqindex, lowquant;
1308
    int status;
1309

    
1310
    v->p_frame_skipped = 0;
1311

    
1312
    if(v->interlace){
1313
        v->fcm = decode012(gb);
1314
        if(v->fcm) return -1; // interlaced frames/fields are not implemented
1315
    }
1316
    switch(get_prefix(gb, 0, 4)) {
1317
    case 0:
1318
        v->s.pict_type = P_TYPE;
1319
        break;
1320
    case 1:
1321
        v->s.pict_type = B_TYPE;
1322
        break;
1323
    case 2:
1324
        v->s.pict_type = I_TYPE;
1325
        break;
1326
    case 3:
1327
        v->s.pict_type = BI_TYPE;
1328
        break;
1329
    case 4:
1330
        v->s.pict_type = P_TYPE; // skipped pic
1331
        v->p_frame_skipped = 1;
1332
        return 0;
1333
    }
1334
    if(v->tfcntrflag)
1335
        get_bits(gb, 8);
1336
    if(v->broadcast) {
1337
        if(!v->interlace || v->psf) {
1338
            v->rptfrm = get_bits(gb, 2);
1339
        } else {
1340
            v->tff = get_bits1(gb);
1341
            v->rptfrm = get_bits1(gb);
1342
        }
1343
    }
1344
    if(v->panscanflag) {
1345
        //...
1346
    }
1347
    v->rnd = get_bits1(gb);
1348
    if(v->interlace)
1349
        v->uvsamp = get_bits1(gb);
1350
    if(v->finterpflag) v->interpfrm = get_bits(gb, 1);
1351
    if(v->s.pict_type == B_TYPE) {
1352
        v->bfraction = get_vlc2(gb, vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
1353
        v->bfraction = vc1_bfraction_lut[v->bfraction];
1354
        if(v->bfraction == 0) {
1355
            v->s.pict_type = BI_TYPE; /* XXX: should not happen here */
1356
        }
1357
    }
1358
    pqindex = get_bits(gb, 5);
1359
    v->pqindex = pqindex;
1360
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1361
        v->pq = pquant_table[0][pqindex];
1362
    else
1363
        v->pq = pquant_table[1][pqindex];
1364

    
1365
    v->pquantizer = 1;
1366
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1367
        v->pquantizer = pqindex < 9;
1368
    if (v->quantizer_mode == QUANT_NON_UNIFORM)
1369
        v->pquantizer = 0;
1370
    v->pqindex = pqindex;
1371
    if (pqindex < 9) v->halfpq = get_bits(gb, 1);
1372
    else v->halfpq = 0;
1373
    if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
1374
        v->pquantizer = get_bits(gb, 1);
1375

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

    
1378
    switch(v->s.pict_type) {
1379
    case I_TYPE:
1380
    case BI_TYPE:
1381
        status = bitplane_decoding(v->acpred_plane, &v->acpred_is_raw, v);
1382
        if (status < 0) return -1;
1383
        av_log(v->s.avctx, AV_LOG_DEBUG, "ACPRED plane encoding: "
1384
                "Imode: %i, Invert: %i\n", status>>1, status&1);
1385
        v->condover = CONDOVER_NONE;
1386
        if(v->overlap && v->pq <= 8) {
1387
            v->condover = decode012(gb);
1388
            if(v->condover == CONDOVER_SELECT) {
1389
                status = bitplane_decoding(v->over_flags_plane, &v->overflg_is_raw, v);
1390
                if (status < 0) return -1;
1391
                av_log(v->s.avctx, AV_LOG_DEBUG, "CONDOVER plane encoding: "
1392
                        "Imode: %i, Invert: %i\n", status>>1, status&1);
1393
            }
1394
        }
1395
        break;
1396
    case P_TYPE:
1397
        if(v->postprocflag)
1398
            v->postproc = get_bits1(gb);
1399
        if (v->extended_mv) v->mvrange = get_prefix(gb, 0, 3);
1400
        else v->mvrange = 0;
1401
        v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1402
        v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
1403
        v->range_x = 1 << (v->k_x - 1);
1404
        v->range_y = 1 << (v->k_y - 1);
1405

    
1406
        if (v->pq < 5) v->tt_index = 0;
1407
        else if(v->pq < 13) v->tt_index = 1;
1408
        else v->tt_index = 2;
1409

    
1410
        lowquant = (v->pq > 12) ? 0 : 1;
1411
        v->mv_mode = mv_pmode_table[lowquant][get_prefix(gb, 1, 4)];
1412
        if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
1413
        {
1414
            int scale, shift, i;
1415
            v->mv_mode2 = mv_pmode_table2[lowquant][get_prefix(gb, 1, 3)];
1416
            v->lumscale = get_bits(gb, 6);
1417
            v->lumshift = get_bits(gb, 6);
1418
            /* fill lookup tables for intensity compensation */
1419
            if(!v->lumscale) {
1420
                scale = -64;
1421
                shift = (255 - v->lumshift * 2) << 6;
1422
                if(v->lumshift > 31)
1423
                    shift += 128 << 6;
1424
            } else {
1425
                scale = v->lumscale + 32;
1426
                if(v->lumshift > 31)
1427
                    shift = (v->lumshift - 64) << 6;
1428
                else
1429
                    shift = v->lumshift << 6;
1430
            }
1431
            for(i = 0; i < 256; i++) {
1432
                v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
1433
                v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
1434
            }
1435
            v->use_ic = 1;
1436
        }
1437
        if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
1438
            v->s.quarter_sample = 0;
1439
        else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1440
            if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
1441
                v->s.quarter_sample = 0;
1442
            else
1443
                v->s.quarter_sample = 1;
1444
        } else
1445
            v->s.quarter_sample = 1;
1446
        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));
1447

    
1448
        if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
1449
                 v->mv_mode2 == MV_PMODE_MIXED_MV)
1450
                || v->mv_mode == MV_PMODE_MIXED_MV)
1451
        {
1452
            status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
1453
            if (status < 0) return -1;
1454
            av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
1455
                   "Imode: %i, Invert: %i\n", status>>1, status&1);
1456
        } else {
1457
            v->mv_type_is_raw = 0;
1458
            memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
1459
        }
1460
        status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1461
        if (status < 0) return -1;
1462
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1463
               "Imode: %i, Invert: %i\n", status>>1, status&1);
1464

    
1465
        /* Hopefully this is correct for P frames */
1466
        v->s.mv_table_index = get_bits(gb, 2); //but using vc1_ tables
1467
        v->cbpcy_vlc = &vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1468
        if (v->dquant)
1469
        {
1470
            av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1471
            vop_dquant_decoding(v);
1472
        }
1473

    
1474
        v->ttfrm = 0; //FIXME Is that so ?
1475
        if (v->vstransform)
1476
        {
1477
            v->ttmbf = get_bits(gb, 1);
1478
            if (v->ttmbf)
1479
            {
1480
                v->ttfrm = ttfrm_to_tt[get_bits(gb, 2)];
1481
            }
1482
        } else {
1483
            v->ttmbf = 1;
1484
            v->ttfrm = TT_8X8;
1485
        }
1486
        break;
1487
    case B_TYPE:
1488
        if(v->postprocflag)
1489
            v->postproc = get_bits1(gb);
1490
        if (v->extended_mv) v->mvrange = get_prefix(gb, 0, 3);
1491
        else v->mvrange = 0;
1492
        v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1493
        v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
1494
        v->range_x = 1 << (v->k_x - 1);
1495
        v->range_y = 1 << (v->k_y - 1);
1496

    
1497
        if (v->pq < 5) v->tt_index = 0;
1498
        else if(v->pq < 13) v->tt_index = 1;
1499
        else v->tt_index = 2;
1500

    
1501
        lowquant = (v->pq > 12) ? 0 : 1;
1502
        v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
1503
        v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
1504
        v->s.mspel = v->s.quarter_sample;
1505

    
1506
        status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
1507
        if (status < 0) return -1;
1508
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
1509
               "Imode: %i, Invert: %i\n", status>>1, status&1);
1510
        status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1511
        if (status < 0) return -1;
1512
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1513
               "Imode: %i, Invert: %i\n", status>>1, status&1);
1514

    
1515
        v->s.mv_table_index = get_bits(gb, 2);
1516
        v->cbpcy_vlc = &vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1517

    
1518
        if (v->dquant)
1519
        {
1520
            av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1521
            vop_dquant_decoding(v);
1522
        }
1523

    
1524
        v->ttfrm = 0;
1525
        if (v->vstransform)
1526
        {
1527
            v->ttmbf = get_bits(gb, 1);
1528
            if (v->ttmbf)
1529
            {
1530
                v->ttfrm = ttfrm_to_tt[get_bits(gb, 2)];
1531
            }
1532
        } else {
1533
            v->ttmbf = 1;
1534
            v->ttfrm = TT_8X8;
1535
        }
1536
        break;
1537
    }
1538

    
1539
    /* AC Syntax */
1540
    v->c_ac_table_index = decode012(gb);
1541
    if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
1542
    {
1543
        v->y_ac_table_index = decode012(gb);
1544
    }
1545
    /* DC Syntax */
1546
    v->s.dc_table_index = get_bits(gb, 1);
1547
    if ((v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE) && v->dquant) {
1548
        av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1549
        vop_dquant_decoding(v);
1550
    }
1551

    
1552
    v->bi_type = 0;
1553
    if(v->s.pict_type == BI_TYPE) {
1554
        v->s.pict_type = B_TYPE;
1555
        v->bi_type = 1;
1556
    }
1557
    return 0;
1558
}
1559

    
1560
/***********************************************************************/
1561
/**
1562
 * @defgroup block VC-1 Block-level functions
1563
 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1564
 * @{
1565
 */
1566

    
1567
/**
1568
 * @def GET_MQUANT
1569
 * @brief Get macroblock-level quantizer scale
1570
 */
1571
#define GET_MQUANT()                                           \
1572
  if (v->dquantfrm)                                            \
1573
  {                                                            \
1574
    int edges = 0;                                             \
1575
    if (v->dqprofile == DQPROFILE_ALL_MBS)                     \
1576
    {                                                          \
1577
      if (v->dqbilevel)                                        \
1578
      {                                                        \
1579
        mquant = (get_bits(gb, 1)) ? v->altpq : v->pq;         \
1580
      }                                                        \
1581
      else                                                     \
1582
      {                                                        \
1583
        mqdiff = get_bits(gb, 3);                              \
1584
        if (mqdiff != 7) mquant = v->pq + mqdiff;              \
1585
        else mquant = get_bits(gb, 5);                         \
1586
      }                                                        \
1587
    }                                                          \
1588
    if(v->dqprofile == DQPROFILE_SINGLE_EDGE)                  \
1589
        edges = 1 << v->dqsbedge;                              \
1590
    else if(v->dqprofile == DQPROFILE_DOUBLE_EDGES)            \
1591
        edges = (3 << v->dqsbedge) % 15;                       \
1592
    else if(v->dqprofile == DQPROFILE_FOUR_EDGES)              \
1593
        edges = 15;                                            \
1594
    if((edges&1) && !s->mb_x)                                  \
1595
        mquant = v->altpq;                                     \
1596
    if((edges&2) && s->first_slice_line)                       \
1597
        mquant = v->altpq;                                     \
1598
    if((edges&4) && s->mb_x == (s->mb_width - 1))              \
1599
        mquant = v->altpq;                                     \
1600
    if((edges&8) && s->mb_y == (s->mb_height - 1))             \
1601
        mquant = v->altpq;                                     \
1602
  }
1603

    
1604
/**
1605
 * @def GET_MVDATA(_dmv_x, _dmv_y)
1606
 * @brief Get MV differentials
1607
 * @see MVDATA decoding from 8.3.5.2, p(1)20
1608
 * @param _dmv_x Horizontal differential for decoded MV
1609
 * @param _dmv_y Vertical differential for decoded MV
1610
 */
1611
#define GET_MVDATA(_dmv_x, _dmv_y)                                  \
1612
  index = 1 + get_vlc2(gb, vc1_mv_diff_vlc[s->mv_table_index].table,\
1613
                       VC1_MV_DIFF_VLC_BITS, 2);                    \
1614
  if (index > 36)                                                   \
1615
  {                                                                 \
1616
    mb_has_coeffs = 1;                                              \
1617
    index -= 37;                                                    \
1618
  }                                                                 \
1619
  else mb_has_coeffs = 0;                                           \
1620
  s->mb_intra = 0;                                                  \
1621
  if (!index) { _dmv_x = _dmv_y = 0; }                              \
1622
  else if (index == 35)                                             \
1623
  {                                                                 \
1624
    _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample);          \
1625
    _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample);          \
1626
  }                                                                 \
1627
  else if (index == 36)                                             \
1628
  {                                                                 \
1629
    _dmv_x = 0;                                                     \
1630
    _dmv_y = 0;                                                     \
1631
    s->mb_intra = 1;                                                \
1632
  }                                                                 \
1633
  else                                                              \
1634
  {                                                                 \
1635
    index1 = index%6;                                               \
1636
    if (!s->quarter_sample && index1 == 5) val = 1;                 \
1637
    else                                   val = 0;                 \
1638
    if(size_table[index1] - val > 0)                                \
1639
        val = get_bits(gb, size_table[index1] - val);               \
1640
    else                                   val = 0;                 \
1641
    sign = 0 - (val&1);                                             \
1642
    _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign;     \
1643
                                                                    \
1644
    index1 = index/6;                                               \
1645
    if (!s->quarter_sample && index1 == 5) val = 1;                 \
1646
    else                                   val = 0;                 \
1647
    if(size_table[index1] - val > 0)                                \
1648
        val = get_bits(gb, size_table[index1] - val);               \
1649
    else                                   val = 0;                 \
1650
    sign = 0 - (val&1);                                             \
1651
    _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign;     \
1652
  }
1653

    
1654
/** Predict and set motion vector
1655
 */
1656
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)
1657
{
1658
    int xy, wrap, off = 0;
1659
    int16_t *A, *B, *C;
1660
    int px, py;
1661
    int sum;
1662

    
1663
    /* scale MV difference to be quad-pel */
1664
    dmv_x <<= 1 - s->quarter_sample;
1665
    dmv_y <<= 1 - s->quarter_sample;
1666

    
1667
    wrap = s->b8_stride;
1668
    xy = s->block_index[n];
1669

    
1670
    if(s->mb_intra){
1671
        s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
1672
        s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
1673
        s->current_picture.motion_val[1][xy][0] = 0;
1674
        s->current_picture.motion_val[1][xy][1] = 0;
1675
        if(mv1) { /* duplicate motion data for 1-MV block */
1676
            s->current_picture.motion_val[0][xy + 1][0] = 0;
1677
            s->current_picture.motion_val[0][xy + 1][1] = 0;
1678
            s->current_picture.motion_val[0][xy + wrap][0] = 0;
1679
            s->current_picture.motion_val[0][xy + wrap][1] = 0;
1680
            s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
1681
            s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
1682
            s->current_picture.motion_val[1][xy + 1][0] = 0;
1683
            s->current_picture.motion_val[1][xy + 1][1] = 0;
1684
            s->current_picture.motion_val[1][xy + wrap][0] = 0;
1685
            s->current_picture.motion_val[1][xy + wrap][1] = 0;
1686
            s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
1687
            s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
1688
        }
1689
        return;
1690
    }
1691

    
1692
    C = s->current_picture.motion_val[0][xy - 1];
1693
    A = s->current_picture.motion_val[0][xy - wrap];
1694
    if(mv1)
1695
        off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1696
    else {
1697
        //in 4-MV mode different blocks have different B predictor position
1698
        switch(n){
1699
        case 0:
1700
            off = (s->mb_x > 0) ? -1 : 1;
1701
            break;
1702
        case 1:
1703
            off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1704
            break;
1705
        case 2:
1706
            off = 1;
1707
            break;
1708
        case 3:
1709
            off = -1;
1710
        }
1711
    }
1712
    B = s->current_picture.motion_val[0][xy - wrap + off];
1713

    
1714
    if(!s->first_slice_line || (n==2 || n==3)) { // predictor A is not out of bounds
1715
        if(s->mb_width == 1) {
1716
            px = A[0];
1717
            py = A[1];
1718
        } else {
1719
            px = mid_pred(A[0], B[0], C[0]);
1720
            py = mid_pred(A[1], B[1], C[1]);
1721
        }
1722
    } else if(s->mb_x || (n==1 || n==3)) { // predictor C is not out of bounds
1723
        px = C[0];
1724
        py = C[1];
1725
    } else {
1726
        px = py = 0;
1727
    }
1728
    /* Pullback MV as specified in 8.3.5.3.4 */
1729
    {
1730
        int qx, qy, X, Y;
1731
        qx = (s->mb_x << 6) + ((n==1 || n==3) ? 32 : 0);
1732
        qy = (s->mb_y << 6) + ((n==2 || n==3) ? 32 : 0);
1733
        X = (s->mb_width << 6) - 4;
1734
        Y = (s->mb_height << 6) - 4;
1735
        if(mv1) {
1736
            if(qx + px < -60) px = -60 - qx;
1737
            if(qy + py < -60) py = -60 - qy;
1738
        } else {
1739
            if(qx + px < -28) px = -28 - qx;
1740
            if(qy + py < -28) py = -28 - qy;
1741
        }
1742
        if(qx + px > X) px = X - qx;
1743
        if(qy + py > Y) py = Y - qy;
1744
    }
1745
    /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
1746
    if((!s->first_slice_line || (n==2 || n==3)) && (s->mb_x || (n==1 || n==3))) {
1747
        if(is_intra[xy - wrap])
1748
            sum = FFABS(px) + FFABS(py);
1749
        else
1750
            sum = FFABS(px - A[0]) + FFABS(py - A[1]);
1751
        if(sum > 32) {
1752
            if(get_bits1(&s->gb)) {
1753
                px = A[0];
1754
                py = A[1];
1755
            } else {
1756
                px = C[0];
1757
                py = C[1];
1758
            }
1759
        } else {
1760
            if(is_intra[xy - 1])
1761
                sum = FFABS(px) + FFABS(py);
1762
            else
1763
                sum = FFABS(px - C[0]) + FFABS(py - C[1]);
1764
            if(sum > 32) {
1765
                if(get_bits1(&s->gb)) {
1766
                    px = A[0];
1767
                    py = A[1];
1768
                } else {
1769
                    px = C[0];
1770
                    py = C[1];
1771
                }
1772
            }
1773
        }
1774
    }
1775
    /* store MV using signed modulus of MV range defined in 4.11 */
1776
    s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1777
    s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
1778
    if(mv1) { /* duplicate motion data for 1-MV block */
1779
        s->current_picture.motion_val[0][xy + 1][0] = s->current_picture.motion_val[0][xy][0];
1780
        s->current_picture.motion_val[0][xy + 1][1] = s->current_picture.motion_val[0][xy][1];
1781
        s->current_picture.motion_val[0][xy + wrap][0] = s->current_picture.motion_val[0][xy][0];
1782
        s->current_picture.motion_val[0][xy + wrap][1] = s->current_picture.motion_val[0][xy][1];
1783
        s->current_picture.motion_val[0][xy + wrap + 1][0] = s->current_picture.motion_val[0][xy][0];
1784
        s->current_picture.motion_val[0][xy + wrap + 1][1] = s->current_picture.motion_val[0][xy][1];
1785
    }
1786
}
1787

    
1788
/** Motion compensation for direct or interpolated blocks in B-frames
1789
 */
1790
static void vc1_interp_mc(VC1Context *v)
1791
{
1792
    MpegEncContext *s = &v->s;
1793
    DSPContext *dsp = &v->s.dsp;
1794
    uint8_t *srcY, *srcU, *srcV;
1795
    int dxy, uvdxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1796

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

    
1799
    mx = s->mv[1][0][0];
1800
    my = s->mv[1][0][1];
1801
    uvmx = (mx + ((mx & 3) == 3)) >> 1;
1802
    uvmy = (my + ((my & 3) == 3)) >> 1;
1803
    if(v->fastuvmc) {
1804
        uvmx = uvmx + ((uvmx<0)?-(uvmx&1):(uvmx&1));
1805
        uvmy = uvmy + ((uvmy<0)?-(uvmy&1):(uvmy&1));
1806
    }
1807
    srcY = s->next_picture.data[0];
1808
    srcU = s->next_picture.data[1];
1809
    srcV = s->next_picture.data[2];
1810

    
1811
    src_x = s->mb_x * 16 + (mx >> 2);
1812
    src_y = s->mb_y * 16 + (my >> 2);
1813
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1814
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1815

    
1816
    if(v->profile != PROFILE_ADVANCED){
1817
        src_x   = av_clip(  src_x, -16, s->mb_width  * 16);
1818
        src_y   = av_clip(  src_y, -16, s->mb_height * 16);
1819
        uvsrc_x = av_clip(uvsrc_x,  -8, s->mb_width  *  8);
1820
        uvsrc_y = av_clip(uvsrc_y,  -8, s->mb_height *  8);
1821
    }else{
1822
        src_x   = av_clip(  src_x, -17, s->avctx->coded_width);
1823
        src_y   = av_clip(  src_y, -18, s->avctx->coded_height + 1);
1824
        uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
1825
        uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
1826
    }
1827

    
1828
    srcY += src_y * s->linesize + src_x;
1829
    srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1830
    srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1831

    
1832
    /* for grayscale we should not try to read from unknown area */
1833
    if(s->flags & CODEC_FLAG_GRAY) {
1834
        srcU = s->edge_emu_buffer + 18 * s->linesize;
1835
        srcV = s->edge_emu_buffer + 18 * s->linesize;
1836
    }
1837

    
1838
    if(v->rangeredfrm
1839
       || (unsigned)src_x > s->h_edge_pos - (mx&3) - 16
1840
       || (unsigned)src_y > s->v_edge_pos - (my&3) - 16){
1841
        uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
1842

    
1843
        srcY -= s->mspel * (1 + s->linesize);
1844
        ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
1845
                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
1846
        srcY = s->edge_emu_buffer;
1847
        ff_emulated_edge_mc(uvbuf     , srcU, s->uvlinesize, 8+1, 8+1,
1848
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
1849
        ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
1850
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
1851
        srcU = uvbuf;
1852
        srcV = uvbuf + 16;
1853
        /* if we deal with range reduction we need to scale source blocks */
1854
        if(v->rangeredfrm) {
1855
            int i, j;
1856
            uint8_t *src, *src2;
1857

    
1858
            src = srcY;
1859
            for(j = 0; j < 17 + s->mspel*2; j++) {
1860
                for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
1861
                src += s->linesize;
1862
            }
1863
            src = srcU; src2 = srcV;
1864
            for(j = 0; j < 9; j++) {
1865
                for(i = 0; i < 9; i++) {
1866
                    src[i] = ((src[i] - 128) >> 1) + 128;
1867
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
1868
                }
1869
                src += s->uvlinesize;
1870
                src2 += s->uvlinesize;
1871
            }
1872
        }
1873
        srcY += s->mspel * (1 + s->linesize);
1874
    }
1875

    
1876
    mx >>= 1;
1877
    my >>= 1;
1878
    dxy = ((my & 1) << 1) | (mx & 1);
1879

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

    
1882
    if(s->flags & CODEC_FLAG_GRAY) return;
1883
    /* Chroma MC always uses qpel blilinear */
1884
    uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
1885
    uvmx = (uvmx&3)<<1;
1886
    uvmy = (uvmy&3)<<1;
1887
    dsp->avg_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
1888
    dsp->avg_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
1889
}
1890

    
1891
static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
1892
{
1893
    int n = bfrac;
1894

    
1895
#if B_FRACTION_DEN==256
1896
    if(inv)
1897
        n -= 256;
1898
    if(!qs)
1899
        return 2 * ((value * n + 255) >> 9);
1900
    return (value * n + 128) >> 8;
1901
#else
1902
    if(inv)
1903
        n -= B_FRACTION_DEN;
1904
    if(!qs)
1905
        return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
1906
    return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
1907
#endif
1908
}
1909

    
1910
/** Reconstruct motion vector for B-frame and do motion compensation
1911
 */
1912
static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mode)
1913
{
1914
    if(v->use_ic) {
1915
        v->mv_mode2 = v->mv_mode;
1916
        v->mv_mode = MV_PMODE_INTENSITY_COMP;
1917
    }
1918
    if(direct) {
1919
        vc1_mc_1mv(v, 0);
1920
        vc1_interp_mc(v);
1921
        if(v->use_ic) v->mv_mode = v->mv_mode2;
1922
        return;
1923
    }
1924
    if(mode == BMV_TYPE_INTERPOLATED) {
1925
        vc1_mc_1mv(v, 0);
1926
        vc1_interp_mc(v);
1927
        if(v->use_ic) v->mv_mode = v->mv_mode2;
1928
        return;
1929
    }
1930

    
1931
    if(v->use_ic && (mode == BMV_TYPE_BACKWARD)) v->mv_mode = v->mv_mode2;
1932
    vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
1933
    if(v->use_ic) v->mv_mode = v->mv_mode2;
1934
}
1935

    
1936
static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mvtype)
1937
{
1938
    MpegEncContext *s = &v->s;
1939
    int xy, wrap, off = 0;
1940
    int16_t *A, *B, *C;
1941
    int px, py;
1942
    int sum;
1943
    int r_x, r_y;
1944
    const uint8_t *is_intra = v->mb_type[0];
1945

    
1946
    r_x = v->range_x;
1947
    r_y = v->range_y;
1948
    /* scale MV difference to be quad-pel */
1949
    dmv_x[0] <<= 1 - s->quarter_sample;
1950
    dmv_y[0] <<= 1 - s->quarter_sample;
1951
    dmv_x[1] <<= 1 - s->quarter_sample;
1952
    dmv_y[1] <<= 1 - s->quarter_sample;
1953

    
1954
    wrap = s->b8_stride;
1955
    xy = s->block_index[0];
1956

    
1957
    if(s->mb_intra) {
1958
        s->current_picture.motion_val[0][xy][0] =
1959
        s->current_picture.motion_val[0][xy][1] =
1960
        s->current_picture.motion_val[1][xy][0] =
1961
        s->current_picture.motion_val[1][xy][1] = 0;
1962
        return;
1963
    }
1964
    s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
1965
    s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
1966
    s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
1967
    s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
1968

    
1969
    /* Pullback predicted motion vectors as specified in 8.4.5.4 */
1970
    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));
1971
    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));
1972
    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));
1973
    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));
1974
    if(direct) {
1975
        s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
1976
        s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
1977
        s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
1978
        s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
1979
        return;
1980
    }
1981

    
1982
    if((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
1983
        C = s->current_picture.motion_val[0][xy - 2];
1984
        A = s->current_picture.motion_val[0][xy - wrap*2];
1985
        off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1986
        B = s->current_picture.motion_val[0][xy - wrap*2 + off];
1987

    
1988
        if(!s->mb_x) C[0] = C[1] = 0;
1989
        if(!s->first_slice_line) { // predictor A is not out of bounds
1990
            if(s->mb_width == 1) {
1991
                px = A[0];
1992
                py = A[1];
1993
            } else {
1994
                px = mid_pred(A[0], B[0], C[0]);
1995
                py = mid_pred(A[1], B[1], C[1]);
1996
            }
1997
        } else if(s->mb_x) { // predictor C is not out of bounds
1998
            px = C[0];
1999
            py = C[1];
2000
        } else {
2001
            px = py = 0;
2002
        }
2003
        /* Pullback MV as specified in 8.3.5.3.4 */
2004
        {
2005
            int qx, qy, X, Y;
2006
            if(v->profile < PROFILE_ADVANCED) {
2007
                qx = (s->mb_x << 5);
2008
                qy = (s->mb_y << 5);
2009
                X = (s->mb_width << 5) - 4;
2010
                Y = (s->mb_height << 5) - 4;
2011
                if(qx + px < -28) px = -28 - qx;
2012
                if(qy + py < -28) py = -28 - qy;
2013
                if(qx + px > X) px = X - qx;
2014
                if(qy + py > Y) py = Y - qy;
2015
            } else {
2016
                qx = (s->mb_x << 6);
2017
                qy = (s->mb_y << 6);
2018
                X = (s->mb_width << 6) - 4;
2019
                Y = (s->mb_height << 6) - 4;
2020
                if(qx + px < -60) px = -60 - qx;
2021
                if(qy + py < -60) py = -60 - qy;
2022
                if(qx + px > X) px = X - qx;
2023
                if(qy + py > Y) py = Y - qy;
2024
            }
2025
        }
2026
        /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2027
        if(0 && !s->first_slice_line && s->mb_x) {
2028
            if(is_intra[xy - wrap])
2029
                sum = FFABS(px) + FFABS(py);
2030
            else
2031
                sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2032
            if(sum > 32) {
2033
                if(get_bits1(&s->gb)) {
2034
                    px = A[0];
2035
                    py = A[1];
2036
                } else {
2037
                    px = C[0];
2038
                    py = C[1];
2039
                }
2040
            } else {
2041
                if(is_intra[xy - 2])
2042
                    sum = FFABS(px) + FFABS(py);
2043
                else
2044
                    sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2045
                if(sum > 32) {
2046
                    if(get_bits1(&s->gb)) {
2047
                        px = A[0];
2048
                        py = A[1];
2049
                    } else {
2050
                        px = C[0];
2051
                        py = C[1];
2052
                    }
2053
                }
2054
            }
2055
        }
2056
        /* store MV using signed modulus of MV range defined in 4.11 */
2057
        s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2058
        s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2059
    }
2060
    if((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2061
        C = s->current_picture.motion_val[1][xy - 2];
2062
        A = s->current_picture.motion_val[1][xy - wrap*2];
2063
        off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2064
        B = s->current_picture.motion_val[1][xy - wrap*2 + off];
2065

    
2066
        if(!s->mb_x) C[0] = C[1] = 0;
2067
        if(!s->first_slice_line) { // predictor A is not out of bounds
2068
            if(s->mb_width == 1) {
2069
                px = A[0];
2070
                py = A[1];
2071
            } else {
2072
                px = mid_pred(A[0], B[0], C[0]);
2073
                py = mid_pred(A[1], B[1], C[1]);
2074
            }
2075
        } else if(s->mb_x) { // predictor C is not out of bounds
2076
            px = C[0];
2077
            py = C[1];
2078
        } else {
2079
            px = py = 0;
2080
        }
2081
        /* Pullback MV as specified in 8.3.5.3.4 */
2082
        {
2083
            int qx, qy, X, Y;
2084
            if(v->profile < PROFILE_ADVANCED) {
2085
                qx = (s->mb_x << 5);
2086
                qy = (s->mb_y << 5);
2087
                X = (s->mb_width << 5) - 4;
2088
                Y = (s->mb_height << 5) - 4;
2089
                if(qx + px < -28) px = -28 - qx;
2090
                if(qy + py < -28) py = -28 - qy;
2091
                if(qx + px > X) px = X - qx;
2092
                if(qy + py > Y) py = Y - qy;
2093
            } else {
2094
                qx = (s->mb_x << 6);
2095
                qy = (s->mb_y << 6);
2096
                X = (s->mb_width << 6) - 4;
2097
                Y = (s->mb_height << 6) - 4;
2098
                if(qx + px < -60) px = -60 - qx;
2099
                if(qy + py < -60) py = -60 - qy;
2100
                if(qx + px > X) px = X - qx;
2101
                if(qy + py > Y) py = Y - qy;
2102
            }
2103
        }
2104
        /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2105
        if(0 && !s->first_slice_line && s->mb_x) {
2106
            if(is_intra[xy - wrap])
2107
                sum = FFABS(px) + FFABS(py);
2108
            else
2109
                sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2110
            if(sum > 32) {
2111
                if(get_bits1(&s->gb)) {
2112
                    px = A[0];
2113
                    py = A[1];
2114
                } else {
2115
                    px = C[0];
2116
                    py = C[1];
2117
                }
2118
            } else {
2119
                if(is_intra[xy - 2])
2120
                    sum = FFABS(px) + FFABS(py);
2121
                else
2122
                    sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2123
                if(sum > 32) {
2124
                    if(get_bits1(&s->gb)) {
2125
                        px = A[0];
2126
                        py = A[1];
2127
                    } else {
2128
                        px = C[0];
2129
                        py = C[1];
2130
                    }
2131
                }
2132
            }
2133
        }
2134
        /* store MV using signed modulus of MV range defined in 4.11 */
2135

    
2136
        s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2137
        s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2138
    }
2139
    s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
2140
    s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
2141
    s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
2142
    s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
2143
}
2144

    
2145
/** Get predicted DC value for I-frames only
2146
 * prediction dir: left=0, top=1
2147
 * @param s MpegEncContext
2148
 * @param[in] n block index in the current MB
2149
 * @param dc_val_ptr Pointer to DC predictor
2150
 * @param dir_ptr Prediction direction for use in AC prediction
2151
 */
2152
static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2153
                              int16_t **dc_val_ptr, int *dir_ptr)
2154
{
2155
    int a, b, c, wrap, pred, scale;
2156
    int16_t *dc_val;
2157
    static const uint16_t dcpred[32] = {
2158
    -1, 1024,  512,  341,  256,  205,  171,  146,  128,
2159
         114,  102,   93,   85,   79,   73,   68,   64,
2160
          60,   57,   54,   51,   49,   47,   45,   43,
2161
          41,   39,   38,   37,   35,   34,   33
2162
    };
2163

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

    
2168
    wrap = s->block_wrap[n];
2169
    dc_val= s->dc_val[0] + s->block_index[n];
2170

    
2171
    /* B A
2172
     * C X
2173
     */
2174
    c = dc_val[ - 1];
2175
    b = dc_val[ - 1 - wrap];
2176
    a = dc_val[ - wrap];
2177

    
2178
    if (pq < 9 || !overlap)
2179
    {
2180
        /* Set outer values */
2181
        if (s->first_slice_line && (n!=2 && n!=3)) b=a=dcpred[scale];
2182
        if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=dcpred[scale];
2183
    }
2184
    else
2185
    {
2186
        /* Set outer values */
2187
        if (s->first_slice_line && (n!=2 && n!=3)) b=a=0;
2188
        if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=0;
2189
    }
2190

    
2191
    if (abs(a - b) <= abs(b - c)) {
2192
        pred = c;
2193
        *dir_ptr = 1;//left
2194
    } else {
2195
        pred = a;
2196
        *dir_ptr = 0;//top
2197
    }
2198

    
2199
    /* update predictor */
2200
    *dc_val_ptr = &dc_val[0];
2201
    return pred;
2202
}
2203

    
2204

    
2205
/** Get predicted DC value
2206
 * prediction dir: left=0, top=1
2207
 * @param s MpegEncContext
2208
 * @param[in] n block index in the current MB
2209
 * @param dc_val_ptr Pointer to DC predictor
2210
 * @param dir_ptr Prediction direction for use in AC prediction
2211
 */
2212
static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2213
                              int a_avail, int c_avail,
2214
                              int16_t **dc_val_ptr, int *dir_ptr)
2215
{
2216
    int a, b, c, wrap, pred, scale;
2217
    int16_t *dc_val;
2218
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2219
    int q1, q2 = 0;
2220

    
2221
    /* find prediction - wmv3_dc_scale always used here in fact */
2222
    if (n < 4)     scale = s->y_dc_scale;
2223
    else           scale = s->c_dc_scale;
2224

    
2225
    wrap = s->block_wrap[n];
2226
    dc_val= s->dc_val[0] + s->block_index[n];
2227

    
2228
    /* B A
2229
     * C X
2230
     */
2231
    c = dc_val[ - 1];
2232
    b = dc_val[ - 1 - wrap];
2233
    a = dc_val[ - wrap];
2234
    /* scale predictors if needed */
2235
    q1 = s->current_picture.qscale_table[mb_pos];
2236
    if(c_avail && (n!= 1 && n!=3)) {
2237
        q2 = s->current_picture.qscale_table[mb_pos - 1];
2238
        if(q2 && q2 != q1)
2239
            c = (c * s->y_dc_scale_table[q2] * vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
2240
    }
2241
    if(a_avail && (n!= 2 && n!=3)) {
2242
        q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2243
        if(q2 && q2 != q1)
2244
            a = (a * s->y_dc_scale_table[q2] * vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
2245
    }
2246
    if(a_avail && c_avail && (n!=3)) {
2247
        int off = mb_pos;
2248
        if(n != 1) off--;
2249
        if(n != 2) off -= s->mb_stride;
2250
        q2 = s->current_picture.qscale_table[off];
2251
        if(q2 && q2 != q1)
2252
            b = (b * s->y_dc_scale_table[q2] * vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
2253
    }
2254

    
2255
    if(a_avail && c_avail) {
2256
        if(abs(a - b) <= abs(b - c)) {
2257
            pred = c;
2258
            *dir_ptr = 1;//left
2259
        } else {
2260
            pred = a;
2261
            *dir_ptr = 0;//top
2262
        }
2263
    } else if(a_avail) {
2264
        pred = a;
2265
        *dir_ptr = 0;//top
2266
    } else if(c_avail) {
2267
        pred = c;
2268
        *dir_ptr = 1;//left
2269
    } else {
2270
        pred = 0;
2271
        *dir_ptr = 1;//left
2272
    }
2273

    
2274
    /* update predictor */
2275
    *dc_val_ptr = &dc_val[0];
2276
    return pred;
2277
}
2278

    
2279

    
2280
/**
2281
 * @defgroup std_mb VC1 Macroblock-level functions in Simple/Main Profiles
2282
 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2283
 * @{
2284
 */
2285

    
2286
static inline int vc1_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
2287
{
2288
    int xy, wrap, pred, a, b, c;
2289

    
2290
    xy = s->block_index[n];
2291
    wrap = s->b8_stride;
2292

    
2293
    /* B C
2294
     * A X
2295
     */
2296
    a = s->coded_block[xy - 1       ];
2297
    b = s->coded_block[xy - 1 - wrap];
2298
    c = s->coded_block[xy     - wrap];
2299

    
2300
    if (b == c) {
2301
        pred = a;
2302
    } else {
2303
        pred = c;
2304
    }
2305

    
2306
    /* store value */
2307
    *coded_block_ptr = &s->coded_block[xy];
2308

    
2309
    return pred;
2310
}
2311

    
2312
/**
2313
 * Decode one AC coefficient
2314
 * @param v The VC1 context
2315
 * @param last Last coefficient
2316
 * @param skip How much zero coefficients to skip
2317
 * @param value Decoded AC coefficient value
2318
 * @see 8.1.3.4
2319
 */
2320
static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip, int *value, int codingset)
2321
{
2322
    GetBitContext *gb = &v->s.gb;
2323
    int index, escape, run = 0, level = 0, lst = 0;
2324

    
2325
    index = get_vlc2(gb, vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2326
    if (index != vc1_ac_sizes[codingset] - 1) {
2327
        run = vc1_index_decode_table[codingset][index][0];
2328
        level = vc1_index_decode_table[codingset][index][1];
2329
        lst = index >= vc1_last_decode_table[codingset];
2330
        if(get_bits(gb, 1))
2331
            level = -level;
2332
    } else {
2333
        escape = decode210(gb);
2334
        if (escape != 2) {
2335
            index = get_vlc2(gb, vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2336
            run = vc1_index_decode_table[codingset][index][0];
2337
            level = vc1_index_decode_table[codingset][index][1];
2338
            lst = index >= vc1_last_decode_table[codingset];
2339
            if(escape == 0) {
2340
                if(lst)
2341
                    level += vc1_last_delta_level_table[codingset][run];
2342
                else
2343
                    level += vc1_delta_level_table[codingset][run];
2344
            } else {
2345
                if(lst)
2346
                    run += vc1_last_delta_run_table[codingset][level] + 1;
2347
                else
2348
                    run += vc1_delta_run_table[codingset][level] + 1;
2349
            }
2350
            if(get_bits(gb, 1))
2351
                level = -level;
2352
        } else {
2353
            int sign;
2354
            lst = get_bits(gb, 1);
2355
            if(v->s.esc3_level_length == 0) {
2356
                if(v->pq < 8 || v->dquantfrm) { // table 59
2357
                    v->s.esc3_level_length = get_bits(gb, 3);
2358
                    if(!v->s.esc3_level_length)
2359
                        v->s.esc3_level_length = get_bits(gb, 2) + 8;
2360
                } else { //table 60
2361
                    v->s.esc3_level_length = get_prefix(gb, 1, 6) + 2;
2362
                }
2363
                v->s.esc3_run_length = 3 + get_bits(gb, 2);
2364
            }
2365
            run = get_bits(gb, v->s.esc3_run_length);
2366
            sign = get_bits(gb, 1);
2367
            level = get_bits(gb, v->s.esc3_level_length);
2368
            if(sign)
2369
                level = -level;
2370
        }
2371
    }
2372

    
2373
    *last = lst;
2374
    *skip = run;
2375
    *value = level;
2376
}
2377

    
2378
/** Decode intra block in intra frames - should be faster than decode_intra_block
2379
 * @param v VC1Context
2380
 * @param block block to decode
2381
 * @param coded are AC coeffs present or not
2382
 * @param codingset set of VLC to decode data
2383
 */
2384
static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset)
2385
{
2386
    GetBitContext *gb = &v->s.gb;
2387
    MpegEncContext *s = &v->s;
2388
    int dc_pred_dir = 0; /* Direction of the DC prediction used */
2389
    int run_diff, i;
2390
    int16_t *dc_val;
2391
    int16_t *ac_val, *ac_val2;
2392
    int dcdiff;
2393

    
2394
    /* Get DC differential */
2395
    if (n < 4) {
2396
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2397
    } else {
2398
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2399
    }
2400
    if (dcdiff < 0){
2401
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2402
        return -1;
2403
    }
2404
    if (dcdiff)
2405
    {
2406
        if (dcdiff == 119 /* ESC index value */)
2407
        {
2408
            /* TODO: Optimize */
2409
            if (v->pq == 1) dcdiff = get_bits(gb, 10);
2410
            else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2411
            else dcdiff = get_bits(gb, 8);
2412
        }
2413
        else
2414
        {
2415
            if (v->pq == 1)
2416
                dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
2417
            else if (v->pq == 2)
2418
                dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
2419
        }
2420
        if (get_bits(gb, 1))
2421
            dcdiff = -dcdiff;
2422
    }
2423

    
2424
    /* Prediction */
2425
    dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2426
    *dc_val = dcdiff;
2427

    
2428
    /* Store the quantized DC coeff, used for prediction */
2429
    if (n < 4) {
2430
        block[0] = dcdiff * s->y_dc_scale;
2431
    } else {
2432
        block[0] = dcdiff * s->c_dc_scale;
2433
    }
2434
    /* Skip ? */
2435
    run_diff = 0;
2436
    i = 0;
2437
    if (!coded) {
2438
        goto not_coded;
2439
    }
2440

    
2441
    //AC Decoding
2442
    i = 1;
2443

    
2444
    {
2445
        int last = 0, skip, value;
2446
        const int8_t *zz_table;
2447
        int scale;
2448
        int k;
2449

    
2450
        scale = v->pq * 2 + v->halfpq;
2451

    
2452
        if(v->s.ac_pred) {
2453
            if(!dc_pred_dir)
2454
                zz_table = vc1_horizontal_zz;
2455
            else
2456
                zz_table = vc1_vertical_zz;
2457
        } else
2458
            zz_table = vc1_normal_zz;
2459

    
2460
        ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2461
        ac_val2 = ac_val;
2462
        if(dc_pred_dir) //left
2463
            ac_val -= 16;
2464
        else //top
2465
            ac_val -= 16 * s->block_wrap[n];
2466

    
2467
        while (!last) {
2468
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2469
            i += skip;
2470
            if(i > 63)
2471
                break;
2472
            block[zz_table[i++]] = value;
2473
        }
2474

    
2475
        /* apply AC prediction if needed */
2476
        if(s->ac_pred) {
2477
            if(dc_pred_dir) { //left
2478
                for(k = 1; k < 8; k++)
2479
                    block[k << 3] += ac_val[k];
2480
            } else { //top
2481
                for(k = 1; k < 8; k++)
2482
                    block[k] += ac_val[k + 8];
2483
            }
2484
        }
2485
        /* save AC coeffs for further prediction */
2486
        for(k = 1; k < 8; k++) {
2487
            ac_val2[k] = block[k << 3];
2488
            ac_val2[k + 8] = block[k];
2489
        }
2490

    
2491
        /* scale AC coeffs */
2492
        for(k = 1; k < 64; k++)
2493
            if(block[k]) {
2494
                block[k] *= scale;
2495
                if(!v->pquantizer)
2496
                    block[k] += (block[k] < 0) ? -v->pq : v->pq;
2497
            }
2498

    
2499
        if(s->ac_pred) i = 63;
2500
    }
2501

    
2502
not_coded:
2503
    if(!coded) {
2504
        int k, scale;
2505
        ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2506
        ac_val2 = ac_val;
2507

    
2508
        scale = v->pq * 2 + v->halfpq;
2509
        memset(ac_val2, 0, 16 * 2);
2510
        if(dc_pred_dir) {//left
2511
            ac_val -= 16;
2512
            if(s->ac_pred)
2513
                memcpy(ac_val2, ac_val, 8 * 2);
2514
        } else {//top
2515
            ac_val -= 16 * s->block_wrap[n];
2516
            if(s->ac_pred)
2517
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2518
        }
2519

    
2520
        /* apply AC prediction if needed */
2521
        if(s->ac_pred) {
2522
            if(dc_pred_dir) { //left
2523
                for(k = 1; k < 8; k++) {
2524
                    block[k << 3] = ac_val[k] * scale;
2525
                    if(!v->pquantizer && block[k << 3])
2526
                        block[k << 3] += (block[k << 3] < 0) ? -v->pq : v->pq;
2527
                }
2528
            } else { //top
2529
                for(k = 1; k < 8; k++) {
2530
                    block[k] = ac_val[k + 8] * scale;
2531
                    if(!v->pquantizer && block[k])
2532
                        block[k] += (block[k] < 0) ? -v->pq : v->pq;
2533
                }
2534
            }
2535
            i = 63;
2536
        }
2537
    }
2538
    s->block_last_index[n] = i;
2539

    
2540
    return 0;
2541
}
2542

    
2543
/** Decode intra block in intra frames - should be faster than decode_intra_block
2544
 * @param v VC1Context
2545
 * @param block block to decode
2546
 * @param coded are AC coeffs present or not
2547
 * @param codingset set of VLC to decode data
2548
 */
2549
static int vc1_decode_i_block_adv(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset, int mquant)
2550
{
2551
    GetBitContext *gb = &v->s.gb;
2552
    MpegEncContext *s = &v->s;
2553
    int dc_pred_dir = 0; /* Direction of the DC prediction used */
2554
    int run_diff, i;
2555
    int16_t *dc_val;
2556
    int16_t *ac_val, *ac_val2;
2557
    int dcdiff;
2558
    int a_avail = v->a_avail, c_avail = v->c_avail;
2559
    int use_pred = s->ac_pred;
2560
    int scale;
2561
    int q1, q2 = 0;
2562
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2563

    
2564
    /* Get DC differential */
2565
    if (n < 4) {
2566
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2567
    } else {
2568
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2569
    }
2570
    if (dcdiff < 0){
2571
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2572
        return -1;
2573
    }
2574
    if (dcdiff)
2575
    {
2576
        if (dcdiff == 119 /* ESC index value */)
2577
        {
2578
            /* TODO: Optimize */
2579
            if (mquant == 1) dcdiff = get_bits(gb, 10);
2580
            else if (mquant == 2) dcdiff = get_bits(gb, 9);
2581
            else dcdiff = get_bits(gb, 8);
2582
        }
2583
        else
2584
        {
2585
            if (mquant == 1)
2586
                dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
2587
            else if (mquant == 2)
2588
                dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
2589
        }
2590
        if (get_bits(gb, 1))
2591
            dcdiff = -dcdiff;
2592
    }
2593

    
2594
    /* Prediction */
2595
    dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
2596
    *dc_val = dcdiff;
2597

    
2598
    /* Store the quantized DC coeff, used for prediction */
2599
    if (n < 4) {
2600
        block[0] = dcdiff * s->y_dc_scale;
2601
    } else {
2602
        block[0] = dcdiff * s->c_dc_scale;
2603
    }
2604
    /* Skip ? */
2605
    run_diff = 0;
2606
    i = 0;
2607

    
2608
    //AC Decoding
2609
    i = 1;
2610

    
2611
    /* check if AC is needed at all and adjust direction if needed */
2612
    if(!a_avail) dc_pred_dir = 1;
2613
    if(!c_avail) dc_pred_dir = 0;
2614
    if(!a_avail && !c_avail) use_pred = 0;
2615
    ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2616
    ac_val2 = ac_val;
2617

    
2618
    scale = mquant * 2 + v->halfpq;
2619

    
2620
    if(dc_pred_dir) //left
2621
        ac_val -= 16;
2622
    else //top
2623
        ac_val -= 16 * s->block_wrap[n];
2624

    
2625
    q1 = s->current_picture.qscale_table[mb_pos];
2626
    if(dc_pred_dir && c_avail && mb_pos) q2 = s->current_picture.qscale_table[mb_pos - 1];
2627
    if(!dc_pred_dir && a_avail && mb_pos >= s->mb_stride) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2628
    if(dc_pred_dir && n==1) q2 = q1;
2629
    if(!dc_pred_dir && n==2) q2 = q1;
2630
    if(n==3) q2 = q1;
2631

    
2632
    if(coded) {
2633
        int last = 0, skip, value;
2634
        const int8_t *zz_table;
2635
        int k;
2636

    
2637
        if(v->s.ac_pred) {
2638
            if(!dc_pred_dir)
2639
                zz_table = vc1_horizontal_zz;
2640
            else
2641
                zz_table = vc1_vertical_zz;
2642
        } else
2643
            zz_table = vc1_normal_zz;
2644

    
2645
        while (!last) {
2646
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2647
            i += skip;
2648
            if(i > 63)
2649
                break;
2650
            block[zz_table[i++]] = value;
2651
        }
2652

    
2653
        /* apply AC prediction if needed */
2654
        if(use_pred) {
2655
            /* scale predictors if needed*/
2656
            if(q2 && q1!=q2) {
2657
                q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2658
                q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2659

    
2660
                if(dc_pred_dir) { //left
2661
                    for(k = 1; k < 8; k++)
2662
                        block[k << 3] += (ac_val[k] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2663
                } else { //top
2664
                    for(k = 1; k < 8; k++)
2665
                        block[k] += (ac_val[k + 8] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2666
                }
2667
            } else {
2668
                if(dc_pred_dir) { //left
2669
                    for(k = 1; k < 8; k++)
2670
                        block[k << 3] += ac_val[k];
2671
                } else { //top
2672
                    for(k = 1; k < 8; k++)
2673
                        block[k] += ac_val[k + 8];
2674
                }
2675
            }
2676
        }
2677
        /* save AC coeffs for further prediction */
2678
        for(k = 1; k < 8; k++) {
2679
            ac_val2[k] = block[k << 3];
2680
            ac_val2[k + 8] = block[k];
2681
        }
2682

    
2683
        /* scale AC coeffs */
2684
        for(k = 1; k < 64; k++)
2685
            if(block[k]) {
2686
                block[k] *= scale;
2687
                if(!v->pquantizer)
2688
                    block[k] += (block[k] < 0) ? -mquant : mquant;
2689
            }
2690

    
2691
        if(use_pred) i = 63;
2692
    } else { // no AC coeffs
2693
        int k;
2694

    
2695
        memset(ac_val2, 0, 16 * 2);
2696
        if(dc_pred_dir) {//left
2697
            if(use_pred) {
2698
                memcpy(ac_val2, ac_val, 8 * 2);
2699
                if(q2 && q1!=q2) {
2700
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2701
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2702
                    for(k = 1; k < 8; k++)
2703
                        ac_val2[k] = (ac_val2[k] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2704
                }
2705
            }
2706
        } else {//top
2707
            if(use_pred) {
2708
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2709
                if(q2 && q1!=q2) {
2710
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2711
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2712
                    for(k = 1; k < 8; k++)
2713
                        ac_val2[k + 8] = (ac_val2[k + 8] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2714
                }
2715
            }
2716
        }
2717

    
2718
        /* apply AC prediction if needed */
2719
        if(use_pred) {
2720
            if(dc_pred_dir) { //left
2721
                for(k = 1; k < 8; k++) {
2722
                    block[k << 3] = ac_val2[k] * scale;
2723
                    if(!v->pquantizer && block[k << 3])
2724
                        block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
2725
                }
2726
            } else { //top
2727
                for(k = 1; k < 8; k++) {
2728
                    block[k] = ac_val2[k + 8] * scale;
2729
                    if(!v->pquantizer && block[k])
2730
                        block[k] += (block[k] < 0) ? -mquant : mquant;
2731
                }
2732
            }
2733
            i = 63;
2734
        }
2735
    }
2736
    s->block_last_index[n] = i;
2737

    
2738
    return 0;
2739
}
2740

    
2741
/** Decode intra block in inter frames - more generic version than vc1_decode_i_block
2742
 * @param v VC1Context
2743
 * @param block block to decode
2744
 * @param coded are AC coeffs present or not
2745
 * @param mquant block quantizer
2746
 * @param codingset set of VLC to decode data
2747
 */
2748
static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n, int coded, int mquant, int codingset)
2749
{
2750
    GetBitContext *gb = &v->s.gb;
2751
    MpegEncContext *s = &v->s;
2752
    int dc_pred_dir = 0; /* Direction of the DC prediction used */
2753
    int run_diff, i;
2754
    int16_t *dc_val;
2755
    int16_t *ac_val, *ac_val2;
2756
    int dcdiff;
2757
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2758
    int a_avail = v->a_avail, c_avail = v->c_avail;
2759
    int use_pred = s->ac_pred;
2760
    int scale;
2761
    int q1, q2 = 0;
2762

    
2763
    /* XXX: Guard against dumb values of mquant */
2764
    mquant = (mquant < 1) ? 0 : ( (mquant>31) ? 31 : mquant );
2765

    
2766
    /* Set DC scale - y and c use the same */
2767
    s->y_dc_scale = s->y_dc_scale_table[mquant];
2768
    s->c_dc_scale = s->c_dc_scale_table[mquant];
2769

    
2770
    /* Get DC differential */
2771
    if (n < 4) {
2772
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2773
    } else {
2774
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2775
    }
2776
    if (dcdiff < 0){
2777
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2778
        return -1;
2779
    }
2780
    if (dcdiff)
2781
    {
2782
        if (dcdiff == 119 /* ESC index value */)
2783
        {
2784
            /* TODO: Optimize */
2785
            if (mquant == 1) dcdiff = get_bits(gb, 10);
2786
            else if (mquant == 2) dcdiff = get_bits(gb, 9);
2787
            else dcdiff = get_bits(gb, 8);
2788
        }
2789
        else
2790
        {
2791
            if (mquant == 1)
2792
                dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
2793
            else if (mquant == 2)
2794
                dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
2795
        }
2796
        if (get_bits(gb, 1))
2797
            dcdiff = -dcdiff;
2798
    }
2799

    
2800
    /* Prediction */
2801
    dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
2802
    *dc_val = dcdiff;
2803

    
2804
    /* Store the quantized DC coeff, used for prediction */
2805

    
2806
    if (n < 4) {
2807
        block[0] = dcdiff * s->y_dc_scale;
2808
    } else {
2809
        block[0] = dcdiff * s->c_dc_scale;
2810
    }
2811
    /* Skip ? */
2812
    run_diff = 0;
2813
    i = 0;
2814

    
2815
    //AC Decoding
2816
    i = 1;
2817

    
2818
    /* check if AC is needed at all and adjust direction if needed */
2819
    if(!a_avail) dc_pred_dir = 1;
2820
    if(!c_avail) dc_pred_dir = 0;
2821
    if(!a_avail && !c_avail) use_pred = 0;
2822
    ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2823
    ac_val2 = ac_val;
2824

    
2825
    scale = mquant * 2 + v->halfpq;
2826

    
2827
    if(dc_pred_dir) //left
2828
        ac_val -= 16;
2829
    else //top
2830
        ac_val -= 16 * s->block_wrap[n];
2831

    
2832
    q1 = s->current_picture.qscale_table[mb_pos];
2833
    if(dc_pred_dir && c_avail && mb_pos) q2 = s->current_picture.qscale_table[mb_pos - 1];
2834
    if(!dc_pred_dir && a_avail && mb_pos >= s->mb_stride) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2835
    if(dc_pred_dir && n==1) q2 = q1;
2836
    if(!dc_pred_dir && n==2) q2 = q1;
2837
    if(n==3) q2 = q1;
2838

    
2839
    if(coded) {
2840
        int last = 0, skip, value;
2841
        const int8_t *zz_table;
2842
        int k;
2843

    
2844
        zz_table = vc1_simple_progressive_8x8_zz;
2845

    
2846
        while (!last) {
2847
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2848
            i += skip;
2849
            if(i > 63)
2850
                break;
2851
            block[zz_table[i++]] = value;
2852
        }
2853

    
2854
        /* apply AC prediction if needed */
2855
        if(use_pred) {
2856
            /* scale predictors if needed*/
2857
            if(q2 && q1!=q2) {
2858
                q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2859
                q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2860

    
2861
                if(dc_pred_dir) { //left
2862
                    for(k = 1; k < 8; k++)
2863
                        block[k << 3] += (ac_val[k] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2864
                } else { //top
2865
                    for(k = 1; k < 8; k++)
2866
                        block[k] += (ac_val[k + 8] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2867
                }
2868
            } else {
2869
                if(dc_pred_dir) { //left
2870
                    for(k = 1; k < 8; k++)
2871
                        block[k << 3] += ac_val[k];
2872
                } else { //top
2873
                    for(k = 1; k < 8; k++)
2874
                        block[k] += ac_val[k + 8];
2875
                }
2876
            }
2877
        }
2878
        /* save AC coeffs for further prediction */
2879
        for(k = 1; k < 8; k++) {
2880
            ac_val2[k] = block[k << 3];
2881
            ac_val2[k + 8] = block[k];
2882
        }
2883

    
2884
        /* scale AC coeffs */
2885
        for(k = 1; k < 64; k++)
2886
            if(block[k]) {
2887
                block[k] *= scale;
2888
                if(!v->pquantizer)
2889
                    block[k] += (block[k] < 0) ? -mquant : mquant;
2890
            }
2891

    
2892
        if(use_pred) i = 63;
2893
    } else { // no AC coeffs
2894
        int k;
2895

    
2896
        memset(ac_val2, 0, 16 * 2);
2897
        if(dc_pred_dir) {//left
2898
            if(use_pred) {
2899
                memcpy(ac_val2, ac_val, 8 * 2);
2900
                if(q2 && q1!=q2) {
2901
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2902
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2903
                    for(k = 1; k < 8; k++)
2904
                        ac_val2[k] = (ac_val2[k] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2905
                }
2906
            }
2907
        } else {//top
2908
            if(use_pred) {
2909
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2910
                if(q2 && q1!=q2) {
2911
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2912
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2913
                    for(k = 1; k < 8; k++)
2914
                        ac_val2[k + 8] = (ac_val2[k + 8] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2915
                }
2916
            }
2917
        }
2918

    
2919
        /* apply AC prediction if needed */
2920
        if(use_pred) {
2921
            if(dc_pred_dir) { //left
2922
                for(k = 1; k < 8; k++) {
2923
                    block[k << 3] = ac_val2[k] * scale;
2924
                    if(!v->pquantizer && block[k << 3])
2925
                        block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
2926
                }
2927
            } else { //top
2928
                for(k = 1; k < 8; k++) {
2929
                    block[k] = ac_val2[k + 8] * scale;
2930
                    if(!v->pquantizer && block[k])
2931
                        block[k] += (block[k] < 0) ? -mquant : mquant;
2932
                }
2933
            }
2934
            i = 63;
2935
        }
2936
    }
2937
    s->block_last_index[n] = i;
2938

    
2939
    return 0;
2940
}
2941

    
2942
/** Decode P block
2943
 */
2944
static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n, int mquant, int ttmb, int first_block)
2945
{
2946
    MpegEncContext *s = &v->s;
2947
    GetBitContext *gb = &s->gb;
2948
    int i, j;
2949
    int subblkpat = 0;
2950
    int scale, off, idx, last, skip, value;
2951
    int ttblk = ttmb & 7;
2952

    
2953
    if(ttmb == -1) {
2954
        ttblk = ttblk_to_tt[v->tt_index][get_vlc2(gb, vc1_ttblk_vlc[v->tt_index].table, VC1_TTBLK_VLC_BITS, 1)];
2955
    }
2956
    if(ttblk == TT_4X4) {
2957
        subblkpat = ~(get_vlc2(gb, vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
2958
    }
2959
    if((ttblk != TT_8X8 && ttblk != TT_4X4) && (v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))) {
2960
        subblkpat = decode012(gb);
2961
        if(subblkpat) subblkpat ^= 3; //swap decoded pattern bits
2962
        if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) ttblk = TT_8X4;
2963
        if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) ttblk = TT_4X8;
2964
    }
2965
    scale = 2 * mquant + v->halfpq;
2966

    
2967
    // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
2968
    if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
2969
        subblkpat = 2 - (ttblk == TT_8X4_TOP);
2970
        ttblk = TT_8X4;
2971
    }
2972
    if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
2973
        subblkpat = 2 - (ttblk == TT_4X8_LEFT);
2974
        ttblk = TT_4X8;
2975
    }
2976
    switch(ttblk) {
2977
    case TT_8X8:
2978
        i = 0;
2979
        last = 0;
2980
        while (!last) {
2981
            vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2982
            i += skip;
2983
            if(i > 63)
2984
                break;
2985
            idx = vc1_simple_progressive_8x8_zz[i++];
2986
            block[idx] = value * scale;
2987
            if(!v->pquantizer)
2988
                block[idx] += (block[idx] < 0) ? -mquant : mquant;
2989
        }
2990
        s->dsp.vc1_inv_trans_8x8(block);
2991
        break;
2992
    case TT_4X4:
2993
        for(j = 0; j < 4; j++) {
2994
            last = subblkpat & (1 << (3 - j));
2995
            i = 0;
2996
            off = (j & 1) * 4 + (j & 2) * 16;
2997
            while (!last) {
2998
                vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2999
                i += skip;
3000
                if(i > 15)
3001
                    break;
3002
                idx = vc1_simple_progressive_4x4_zz[i++];
3003
                block[idx + off] = value * scale;
3004
                if(!v->pquantizer)
3005
                    block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
3006
            }
3007
            if(!(subblkpat & (1 << (3 - j))))
3008
                s->dsp.vc1_inv_trans_4x4(block, j);
3009
        }
3010
        break;
3011
    case TT_8X4:
3012
        for(j = 0; j < 2; j++) {
3013
            last = subblkpat & (1 << (1 - j));
3014
            i = 0;
3015
            off = j * 32;
3016
            while (!last) {
3017
                vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3018
                i += skip;
3019
                if(i > 31)
3020
                    break;
3021
                if(v->profile < PROFILE_ADVANCED)
3022
                    idx = vc1_simple_progressive_8x4_zz[i++];
3023
                else
3024
                    idx = vc1_adv_progressive_8x4_zz[i++];
3025
                block[idx + off] = value * scale;
3026
                if(!v->pquantizer)
3027
                    block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
3028
            }
3029
            if(!(subblkpat & (1 << (1 - j))))
3030
                s->dsp.vc1_inv_trans_8x4(block, j);
3031
        }
3032
        break;
3033
    case TT_4X8:
3034
        for(j = 0; j < 2; j++) {
3035
            last = subblkpat & (1 << (1 - j));
3036
            i = 0;
3037
            off = j * 4;
3038
            while (!last) {
3039
                vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3040
                i += skip;
3041
                if(i > 31)
3042
                    break;
3043
                if(v->profile < PROFILE_ADVANCED)
3044
                    idx = vc1_simple_progressive_4x8_zz[i++];
3045
                else
3046
                    idx = vc1_adv_progressive_4x8_zz[i++];
3047
                block[idx + off] = value * scale;
3048
                if(!v->pquantizer)
3049
                    block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
3050
            }
3051
            if(!(subblkpat & (1 << (1 - j))))
3052
                s->dsp.vc1_inv_trans_4x8(block, j);
3053
        }
3054
        break;
3055
    }
3056
    return 0;
3057
}
3058

    
3059

    
3060
/** Decode one P-frame MB (in Simple/Main profile)
3061
 */
3062
static int vc1_decode_p_mb(VC1Context *v)
3063
{
3064
    MpegEncContext *s = &v->s;
3065
    GetBitContext *gb = &s->gb;
3066
    int i, j;
3067
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3068
    int cbp; /* cbp decoding stuff */
3069
    int mqdiff, mquant; /* MB quantization */
3070
    int ttmb = v->ttfrm; /* MB Transform type */
3071
    int status;
3072

    
3073
    static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
3074
      offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3075
    int mb_has_coeffs = 1; /* last_flag */
3076
    int dmv_x, dmv_y; /* Differential MV components */
3077
    int index, index1; /* LUT indices */
3078
    int val, sign; /* temp values */
3079
    int first_block = 1;
3080
    int dst_idx, off;
3081
    int skipped, fourmv;
3082

    
3083
    mquant = v->pq; /* Loosy initialization */
3084

    
3085
    if (v->mv_type_is_raw)
3086
        fourmv = get_bits1(gb);
3087
    else
3088
        fourmv = v->mv_type_mb_plane[mb_pos];
3089
    if (v->skip_is_raw)
3090
        skipped = get_bits1(gb);
3091
    else
3092
        skipped = v->s.mbskip_table[mb_pos];
3093

    
3094
    s->dsp.clear_blocks(s->block[0]);
3095

    
3096
    if (!fourmv) /* 1MV mode */
3097
    {
3098
        if (!skipped)
3099
        {
3100
            GET_MVDATA(dmv_x, dmv_y);
3101

    
3102
            if (s->mb_intra) {
3103
                s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
3104
                s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
3105
            }
3106
            s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
3107
            vc1_pred_mv(s, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
3108

    
3109
            /* FIXME Set DC val for inter block ? */
3110
            if (s->mb_intra && !mb_has_coeffs)
3111
            {
3112
                GET_MQUANT();
3113
                s->ac_pred = get_bits(gb, 1);
3114
                cbp = 0;
3115
            }
3116
            else if (mb_has_coeffs)
3117
            {
3118
                if (s->mb_intra) s->ac_pred = get_bits(gb, 1);
3119
                cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3120
                GET_MQUANT();
3121
            }
3122
            else
3123
            {
3124
                mquant = v->pq;
3125
                cbp = 0;
3126
            }
3127
            s->current_picture.qscale_table[mb_pos] = mquant;
3128

    
3129
            if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3130
                ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table,
3131
                                VC1_TTMB_VLC_BITS, 2);
3132
            if(!s->mb_intra) vc1_mc_1mv(v, 0);
3133
            dst_idx = 0;
3134
            for (i=0; i<6; i++)
3135
            {
3136
                s->dc_val[0][s->block_index[i]] = 0;
3137
                dst_idx += i >> 2;
3138
                val = ((cbp >> (5 - i)) & 1);
3139
                off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3140
                v->mb_type[0][s->block_index[i]] = s->mb_intra;
3141
                if(s->mb_intra) {
3142
                    /* check if prediction blocks A and C are available */
3143
                    v->a_avail = v->c_avail = 0;
3144
                    if(i == 2 || i == 3 || !s->first_slice_line)
3145
                        v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3146
                    if(i == 1 || i == 3 || s->mb_x)
3147
                        v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3148

    
3149
                    vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
3150
                    if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3151
                    s->dsp.vc1_inv_trans_8x8(s->block[i]);
3152
                    if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
3153
                    for(j = 0; j < 64; j++) s->block[i][j] += 128;
3154
                    if(!v->res_fasttx && v->res_x8) for(j = 0; j < 64; j++) s->block[i][j] += 16;
3155
                    s->dsp.put_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
3156
                    if(v->pq >= 9 && v->overlap) {
3157
                        if(v->c_avail)
3158
                            s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
3159
                        if(v->a_avail)
3160
                            s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
3161
                    }
3162
                } else if(val) {
3163
                    vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block);
3164
                    if(!v->ttmbf && ttmb < 8) ttmb = -1;
3165
                    first_block = 0;
3166
                    if((i<4) || !(s->flags & CODEC_FLAG_GRAY))
3167
                        s->dsp.add_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
3168
                }
3169
            }
3170
        }
3171
        else //Skipped
3172
        {
3173
            s->mb_intra = 0;
3174
            for(i = 0; i < 6; i++) {
3175
                v->mb_type[0][s->block_index[i]] = 0;
3176
                s->dc_val[0][s->block_index[i]] = 0;
3177
            }
3178
            s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
3179
            s->current_picture.qscale_table[mb_pos] = 0;
3180
            vc1_pred_mv(s, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);
3181
            vc1_mc_1mv(v, 0);
3182
            return 0;
3183
        }
3184
    } //1MV mode
3185
    else //4MV mode
3186
    {
3187
        if (!skipped /* unskipped MB */)
3188
        {
3189
            int intra_count = 0, coded_inter = 0;
3190
            int is_intra[6], is_coded[6];
3191
            /* Get CBPCY */
3192
            cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3193
            for (i=0; i<6; i++)
3194
            {
3195
                val = ((cbp >> (5 - i)) & 1);
3196
                s->dc_val[0][s->block_index[i]] = 0;
3197
                s->mb_intra = 0;
3198
                if(i < 4) {
3199
                    dmv_x = dmv_y = 0;
3200
                    s->mb_intra = 0;
3201
                    mb_has_coeffs = 0;
3202
                    if(val) {
3203
                        GET_MVDATA(dmv_x, dmv_y);
3204
                    }
3205
                    vc1_pred_mv(s, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);
3206
                    if(!s->mb_intra) vc1_mc_4mv_luma(v, i);
3207
                    intra_count += s->mb_intra;
3208
                    is_intra[i] = s->mb_intra;
3209
                    is_coded[i] = mb_has_coeffs;
3210
                }
3211
                if(i&4){
3212
                    is_intra[i] = (intra_count >= 3);
3213
                    is_coded[i] = val;
3214
                }
3215
                if(i == 4) vc1_mc_4mv_chroma(v);
3216
                v->mb_type[0][s->block_index[i]] = is_intra[i];
3217
                if(!coded_inter) coded_inter = !is_intra[i] & is_coded[i];
3218
            }
3219
            // if there are no coded blocks then don't do anything more
3220
            if(!intra_count && !coded_inter) return 0;
3221
            dst_idx = 0;
3222
            GET_MQUANT();
3223
            s->current_picture.qscale_table[mb_pos] = mquant;
3224
            /* test if block is intra and has pred */
3225
            {
3226
                int intrapred = 0;
3227
                for(i=0; i<6; i++)
3228
                    if(is_intra[i]) {
3229
                        if(((!s->first_slice_line || (i==2 || i==3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
3230
                            || ((s->mb_x || (i==1 || i==3)) && v->mb_type[0][s->block_index[i] - 1])) {
3231
                            intrapred = 1;
3232
                            break;
3233
                        }
3234
                    }
3235
                if(intrapred)s->ac_pred = get_bits(gb, 1);
3236
                else s->ac_pred = 0;
3237
            }
3238
            if (!v->ttmbf && coded_inter)
3239
                ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3240
            for (i=0; i<6; i++)
3241
            {
3242
                dst_idx += i >> 2;
3243
                off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3244
                s->mb_intra = is_intra[i];
3245
                if (is_intra[i]) {
3246
                    /* check if prediction blocks A and C are available */
3247
                    v->a_avail = v->c_avail = 0;
3248
                    if(i == 2 || i == 3 || !s->first_slice_line)
3249
                        v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3250
                    if(i == 1 || i == 3 || s->mb_x)
3251
                        v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3252

    
3253
                    vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant, (i&4)?v->codingset2:v->codingset);
3254
                    if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3255
                    s->dsp.vc1_inv_trans_8x8(s->block[i]);
3256
                    if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
3257
                    for(j = 0; j < 64; j++) s->block[i][j] += 128;
3258
                    if(!v->res_fasttx && v->res_x8) for(j = 0; j < 64; j++) s->block[i][j] += 16;
3259
                    s->dsp.put_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
3260
                    if(v->pq >= 9 && v->overlap) {
3261
                        if(v->c_avail)
3262
                            s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
3263
                        if(v->a_avail)
3264
                            s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
3265
                    }
3266
                } else if(is_coded[i]) {
3267
                    status = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block);
3268
                    if(!v->ttmbf && ttmb < 8) ttmb = -1;
3269
                    first_block = 0;
3270
                    if((i<4) || !(s->flags & CODEC_FLAG_GRAY))
3271
                        s->dsp.add_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
3272
                }
3273
            }
3274
            return status;
3275
        }
3276
        else //Skipped MB
3277
        {
3278
            s->mb_intra = 0;
3279
            s->current_picture.qscale_table[mb_pos] = 0;
3280
            for (i=0; i<6; i++) {
3281
                v->mb_type[0][s->block_index[i]] = 0;
3282
                s->dc_val[0][s->block_index[i]] = 0;
3283
            }
3284
            for (i=0; i<4; i++)
3285
            {
3286
                vc1_pred_mv(s, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0]);
3287
                vc1_mc_4mv_luma(v, i);
3288
            }
3289
            vc1_mc_4mv_chroma(v);
3290
            s->current_picture.qscale_table[mb_pos] = 0;
3291
            return 0;
3292
        }
3293
    }
3294

    
3295
    /* Should never happen */
3296
    return -1;
3297
}
3298

    
3299
/** Decode one B-frame MB (in Main profile)
3300
 */
3301
static void vc1_decode_b_mb(VC1Context *v)
3302
{
3303
    MpegEncContext *s = &v->s;
3304
    GetBitContext *gb = &s->gb;
3305
    int i, j;
3306
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3307
    int cbp = 0; /* cbp decoding stuff */
3308
    int mqdiff, mquant; /* MB quantization */
3309
    int ttmb = v->ttfrm; /* MB Transform type */
3310

    
3311
    static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
3312
      offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3313
    int mb_has_coeffs = 0; /* last_flag */
3314
    int index, index1; /* LUT indices */
3315
    int val, sign; /* temp values */
3316
    int first_block = 1;
3317
    int dst_idx, off;
3318
    int skipped, direct;
3319
    int dmv_x[2], dmv_y[2];
3320
    int bmvtype = BMV_TYPE_BACKWARD;
3321

    
3322
    mquant = v->pq; /* Loosy initialization */
3323
    s->mb_intra = 0;
3324

    
3325
    if (v->dmb_is_raw)
3326
        direct = get_bits1(gb);
3327
    else
3328
        direct = v->direct_mb_plane[mb_pos];
3329
    if (v->skip_is_raw)
3330
        skipped = get_bits1(gb);
3331
    else
3332
        skipped = v->s.mbskip_table[mb_pos];
3333

    
3334
    s->dsp.clear_blocks(s->block[0]);
3335
    dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
3336
    for(i = 0; i < 6; i++) {
3337
        v->mb_type[0][s->block_index[i]] = 0;
3338
        s->dc_val[0][s->block_index[i]] = 0;
3339
    }
3340
    s->current_picture.qscale_table[mb_pos] = 0;
3341

    
3342
    if (!direct) {
3343
        if (!skipped) {
3344
            GET_MVDATA(dmv_x[0], dmv_y[0]);
3345
            dmv_x[1] = dmv_x[0];
3346
            dmv_y[1] = dmv_y[0];
3347
        }
3348
        if(skipped || !s->mb_intra) {
3349
            bmvtype = decode012(gb);
3350
            switch(bmvtype) {
3351
            case 0:
3352
                bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
3353
                break;
3354
            case 1:
3355
                bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
3356
                break;
3357
            case 2:
3358
                bmvtype = BMV_TYPE_INTERPOLATED;
3359
                dmv_x[0] = dmv_y[0] = 0;
3360
            }
3361
        }
3362
    }
3363
    for(i = 0; i < 6; i++)
3364
        v->mb_type[0][s->block_index[i]] = s->mb_intra;
3365

    
3366
    if (skipped) {
3367
        if(direct) bmvtype = BMV_TYPE_INTERPOLATED;
3368
        vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
3369
        vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3370
        return;
3371
    }
3372
    if (direct) {
3373
        cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3374
        GET_MQUANT();
3375
        s->mb_intra = 0;
3376
        mb_has_coeffs = 0;
3377
        s->current_picture.qscale_table[mb_pos] = mquant;
3378
        if(!v->ttmbf)
3379
            ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3380
        dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
3381
        vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
3382
        vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3383
    } else {
3384
        if(!mb_has_coeffs && !s->mb_intra) {
3385
            /* no coded blocks - effectively skipped */
3386
            vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
3387
            vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3388
            return;
3389
        }
3390
        if(s->mb_intra && !mb_has_coeffs) {
3391
            GET_MQUANT();
3392
            s->current_picture.qscale_table[mb_pos] = mquant;
3393
            s->ac_pred = get_bits1(gb);
3394
            cbp = 0;
3395
            vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
3396
        } else {
3397
            if(bmvtype == BMV_TYPE_INTERPOLATED) {
3398
                GET_MVDATA(dmv_x[0], dmv_y[0]);
3399
                if(!mb_has_coeffs) {
3400
                    /* interpolated skipped block */
3401
                    vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
3402
                    vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3403
                    return;
3404
                }
3405
            }
3406
            vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
3407
            if(!s->mb_intra) {
3408
                vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3409
            }
3410
            if(s->mb_intra)
3411
                s->ac_pred = get_bits1(gb);
3412
            cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3413
            GET_MQUANT();
3414
            s->current_picture.qscale_table[mb_pos] = mquant;
3415
            if(!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3416
                ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3417
        }
3418
    }
3419
    dst_idx = 0;
3420
    for (i=0; i<6; i++)
3421
    {
3422
        s->dc_val[0][s->block_index[i]] = 0;
3423
        dst_idx += i >> 2;
3424
        val = ((cbp >> (5 - i)) & 1);
3425
        off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3426
        v->mb_type[0][s->block_index[i]] = s->mb_intra;
3427
        if(s->mb_intra) {
3428
            /* check if prediction blocks A and C are available */
3429
            v->a_avail = v->c_avail = 0;
3430
            if(i == 2 || i == 3 || !s->first_slice_line)
3431
                v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3432
            if(i == 1 || i == 3 || s->mb_x)
3433
                v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3434

    
3435
            vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
3436
            if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3437
            s->dsp.vc1_inv_trans_8x8(s->block[i]);
3438
            if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
3439
            for(j = 0; j < 64; j++) s->block[i][j] += 128;
3440
            s->dsp.put_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
3441
        } else if(val) {
3442
            vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block);
3443
            if(!v->ttmbf && ttmb < 8) ttmb = -1;
3444
            first_block = 0;
3445
            if((i<4) || !(s->flags & CODEC_FLAG_GRAY))
3446
                s->dsp.add_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
3447
        }
3448
    }
3449
}
3450

    
3451
/** Decode blocks of I-frame
3452
 */
3453
static void vc1_decode_i_blocks(VC1Context *v)
3454
{
3455
    int k, j;
3456
    MpegEncContext *s = &v->s;
3457
    int cbp, val;
3458
    uint8_t *coded_val;
3459
    int mb_pos;
3460

    
3461
    /* select codingmode used for VLC tables selection */
3462
    switch(v->y_ac_table_index){
3463
    case 0:
3464
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
3465
        break;
3466
    case 1:
3467
        v->codingset = CS_HIGH_MOT_INTRA;
3468
        break;
3469
    case 2:
3470
        v->codingset = CS_MID_RATE_INTRA;
3471
        break;
3472
    }
3473

    
3474
    switch(v->c_ac_table_index){
3475
    case 0:
3476
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
3477
        break;
3478
    case 1:
3479
        v->codingset2 = CS_HIGH_MOT_INTER;
3480
        break;
3481
    case 2:
3482
        v->codingset2 = CS_MID_RATE_INTER;
3483
        break;
3484
    }
3485

    
3486
    /* Set DC scale - y and c use the same */
3487
    s->y_dc_scale = s->y_dc_scale_table[v->pq];
3488
    s->c_dc_scale = s->c_dc_scale_table[v->pq];
3489

    
3490
    //do frame decode
3491
    s->mb_x = s->mb_y = 0;
3492
    s->mb_intra = 1;
3493
    s->first_slice_line = 1;
3494
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3495
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3496
        for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
3497
            ff_init_block_index(s);
3498
            ff_update_block_index(s);
3499
            s->dsp.clear_blocks(s->block[0]);
3500
            mb_pos = s->mb_x + s->mb_y * s->mb_width;
3501
            s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
3502
            s->current_picture.qscale_table[mb_pos] = v->pq;
3503
            s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
3504
            s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
3505

    
3506
            // do actual MB decoding and displaying
3507
            cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
3508
            v->s.ac_pred = get_bits(&v->s.gb, 1);
3509

    
3510
            for(k = 0; k < 6; k++) {
3511
                val = ((cbp >> (5 - k)) & 1);
3512

    
3513
                if (k < 4) {
3514
                    int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
3515
                    val = val ^ pred;
3516
                    *coded_val = val;
3517
                }
3518
                cbp |= val << (5 - k);
3519

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

    
3522
                s->dsp.vc1_inv_trans_8x8(s->block[k]);
3523
                if(!v->res_fasttx && !v->res_x8) for(j = 0; j < 64; j++) s->block[k][j] -= 16;
3524
                if(v->pq >= 9 && v->overlap) {
3525
                    for(j = 0; j < 64; j++) s->block[k][j] += 128;
3526
                }
3527
            }
3528

    
3529
            vc1_put_block(v, s->block);
3530
            if(v->pq >= 9 && v->overlap) {
3531
                if(s->mb_x) {
3532
                    s->dsp.vc1_h_overlap(s->dest[0], s->linesize);
3533
                    s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
3534
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
3535
                        s->dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
3536
                        s->dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
3537
                    }
3538
                }
3539
                s->dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
3540
                s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
3541
                if(!s->first_slice_line) {
3542
                    s->dsp.vc1_v_overlap(s->dest[0], s->linesize);
3543
                    s->dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
3544
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
3545
                        s->dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
3546
                        s->dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
3547
                    }
3548
                }
3549
                s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
3550
                s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
3551
            }
3552

    
3553
            if(get_bits_count(&s->gb) > v->bits) {
3554
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
3555
                return;
3556
            }
3557
        }
3558
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3559
        s->first_slice_line = 0;
3560
    }
3561
}
3562

    
3563
/** Decode blocks of I-frame for advanced profile
3564
 */
3565
static void vc1_decode_i_blocks_adv(VC1Context *v)
3566
{
3567
    int k, j;
3568
    MpegEncContext *s = &v->s;
3569
    int cbp, val;
3570
    uint8_t *coded_val;
3571
    int mb_pos;
3572
    int mquant = v->pq;
3573
    int mqdiff;
3574
    int overlap;
3575
    GetBitContext *gb = &s->gb;
3576

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

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

    
3602
    //do frame decode
3603
    s->mb_x = s->mb_y = 0;
3604
    s->mb_intra = 1;
3605
    s->first_slice_line = 1;
3606
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3607
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3608
        for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
3609
            ff_init_block_index(s);
3610
            ff_update_block_index(s);
3611
            s->dsp.clear_blocks(s->block[0]);
3612
            mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3613
            s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
3614
            s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
3615
            s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
3616

    
3617
            // do actual MB decoding and displaying
3618
            cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
3619
            if(v->acpred_is_raw)
3620
                v->s.ac_pred = get_bits(&v->s.gb, 1);
3621
            else
3622
                v->s.ac_pred = v->acpred_plane[mb_pos];
3623

    
3624
            if(v->condover == CONDOVER_SELECT) {
3625
                if(v->overflg_is_raw)
3626
                    overlap = get_bits(&v->s.gb, 1);
3627
                else
3628
                    overlap = v->over_flags_plane[mb_pos];
3629
            } else
3630
                overlap = (v->condover == CONDOVER_ALL);
3631

    
3632
            GET_MQUANT();
3633

    
3634
            s->current_picture.qscale_table[mb_pos] = mquant;
3635
            /* Set DC scale - y and c use the same */
3636
            s->y_dc_scale = s->y_dc_scale_table[mquant];
3637
            s->c_dc_scale = s->c_dc_scale_table[mquant];
3638

    
3639
            for(k = 0; k < 6; k++) {
3640
                val = ((cbp >> (5 - k)) & 1);
3641

    
3642
                if (k < 4) {
3643
                    int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
3644
                    val = val ^ pred;
3645
                    *coded_val = val;
3646
                }
3647
                cbp |= val << (5 - k);
3648

    
3649
                v->a_avail = !s->first_slice_line || (k==2 || k==3);
3650
                v->c_avail = !!s->mb_x || (k==1 || k==3);
3651

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

    
3654
                s->dsp.vc1_inv_trans_8x8(s->block[k]);
3655
                for(j = 0; j < 64; j++) s->block[k][j] += 128;
3656
            }
3657

    
3658
            vc1_put_block(v, s->block);
3659
            if(overlap) {
3660
                if(s->mb_x) {
3661
                    s->dsp.vc1_h_overlap(s->dest[0], s->linesize);
3662
                    s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
3663
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
3664
                        s->dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
3665
                        s->dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
3666
                    }
3667
                }
3668
                s->dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
3669
                s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
3670
                if(!s->first_slice_line) {
3671
                    s->dsp.vc1_v_overlap(s->dest[0], s->linesize);
3672
                    s->dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
3673
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
3674
                        s->dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
3675
                        s->dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
3676
                    }
3677
                }
3678
                s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
3679
                s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
3680
            }
3681

    
3682
            if(get_bits_count(&s->gb) > v->bits) {
3683
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
3684
                return;
3685
            }
3686
        }
3687
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3688
        s->first_slice_line = 0;
3689
    }
3690
}
3691

    
3692
static void vc1_decode_p_blocks(VC1Context *v)
3693
{
3694
    MpegEncContext *s = &v->s;
3695

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

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

    
3721
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3722
    s->first_slice_line = 1;
3723
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3724
        for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
3725
            ff_init_block_index(s);
3726
            ff_update_block_index(s);
3727
            s->dsp.clear_blocks(s->block[0]);
3728

    
3729
            vc1_decode_p_mb(v);
3730
            if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
3731
                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);
3732
                return;
3733
            }
3734
        }
3735
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3736
        s->first_slice_line = 0;
3737
    }
3738
}
3739

    
3740
static void vc1_decode_b_blocks(VC1Context *v)
3741
{
3742
    MpegEncContext *s = &v->s;
3743

    
3744
    /* select codingmode used for VLC tables selection */
3745
    switch(v->c_ac_table_index){
3746
    case 0:
3747
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
3748
        break;
3749
    case 1:
3750
        v->codingset = CS_HIGH_MOT_INTRA;
3751
        break;
3752
    case 2:
3753
        v->codingset = CS_MID_RATE_INTRA;
3754
        break;
3755
    }
3756

    
3757
    switch(v->c_ac_table_index){
3758
    case 0:
3759
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
3760
        break;
3761
    case 1:
3762
        v->codingset2 = CS_HIGH_MOT_INTER;
3763
        break;
3764
    case 2:
3765
        v->codingset2 = CS_MID_RATE_INTER;
3766
        break;
3767
    }
3768

    
3769
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3770
    s->first_slice_line = 1;
3771
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3772
        for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
3773
            ff_init_block_index(s);
3774
            ff_update_block_index(s);
3775
            s->dsp.clear_blocks(s->block[0]);
3776

    
3777
            vc1_decode_b_mb(v);
3778
            if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
3779
                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);
3780
                return;
3781
            }
3782
        }
3783
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3784
        s->first_slice_line = 0;
3785
    }
3786
}
3787

    
3788
static void vc1_decode_skip_blocks(VC1Context *v)
3789
{
3790
    MpegEncContext *s = &v->s;
3791

    
3792
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3793
    s->first_slice_line = 1;
3794
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3795
        s->mb_x = 0;
3796
        ff_init_block_index(s);
3797
        ff_update_block_index(s);
3798
        memcpy(s->dest[0], s->last_picture.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
3799
        memcpy(s->dest[1], s->last_picture.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
3800
        memcpy(s->dest[2], s->last_picture.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
3801
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3802
        s->first_slice_line = 0;
3803
    }
3804
    s->pict_type = P_TYPE;
3805
}
3806

    
3807
static void vc1_decode_blocks(VC1Context *v)
3808
{
3809

    
3810
    v->s.esc3_level_length = 0;
3811

    
3812
    switch(v->s.pict_type) {
3813
    case I_TYPE:
3814
        if(v->profile == PROFILE_ADVANCED)
3815
            vc1_decode_i_blocks_adv(v);
3816
        else
3817
            vc1_decode_i_blocks(v);
3818
        break;
3819
    case P_TYPE:
3820
        if(v->p_frame_skipped)
3821
            vc1_decode_skip_blocks(v);
3822
        else
3823
            vc1_decode_p_blocks(v);
3824
        break;
3825
    case B_TYPE:
3826
        if(v->bi_type){
3827
            if(v->profile == PROFILE_ADVANCED)
3828
                vc1_decode_i_blocks_adv(v);
3829
            else
3830
                vc1_decode_i_blocks(v);
3831
        }else
3832
            vc1_decode_b_blocks(v);
3833
        break;
3834
    }
3835
}
3836

    
3837
/** Find VC-1 marker in buffer
3838
 * @return position where next marker starts or end of buffer if no marker found
3839
 */
3840
static av_always_inline uint8_t* find_next_marker(uint8_t *src, uint8_t *end)
3841
{
3842
    uint32_t mrk = 0xFFFFFFFF;
3843

    
3844
    if(end-src < 4) return end;
3845
    while(src < end){
3846
        mrk = (mrk << 8) | *src++;
3847
        if(IS_MARKER(mrk))
3848
            return src-4;
3849
    }
3850
    return end;
3851
}
3852

    
3853
static av_always_inline int vc1_unescape_buffer(uint8_t *src, int size, uint8_t *dst)
3854
{
3855
    int dsize = 0, i;
3856

    
3857
    if(size < 4){
3858
        for(dsize = 0; dsize < size; dsize++) *dst++ = *src++;
3859
        return size;
3860
    }
3861
    for(i = 0; i < size; i++, src++) {
3862
        if(src[0] == 3 && i >= 2 && !src[-1] && !src[-2] && i < size-1 && src[1] < 4) {
3863
            dst[dsize++] = src[1];
3864
            src++;
3865
            i++;
3866
        } else
3867
            dst[dsize++] = *src;
3868
    }
3869
    return dsize;
3870
}
3871

    
3872
/** Initialize a VC1/WMV3 decoder
3873
 * @todo TODO: Handle VC-1 IDUs (Transport level?)
3874
 * @todo TODO: Decypher remaining bits in extra_data
3875
 */
3876
static int vc1_decode_init(AVCodecContext *avctx)
3877
{
3878
    VC1Context *v = avctx->priv_data;
3879
    MpegEncContext *s = &v->s;
3880
    GetBitContext gb;
3881

    
3882
    if (!avctx->extradata_size || !avctx->extradata) return -1;
3883
    if (!(avctx->flags & CODEC_FLAG_GRAY))
3884
        avctx->pix_fmt = PIX_FMT_YUV420P;
3885
    else
3886
        avctx->pix_fmt = PIX_FMT_GRAY8;
3887
    v->s.avctx = avctx;
3888
    avctx->flags |= CODEC_FLAG_EMU_EDGE;
3889
    v->s.flags |= CODEC_FLAG_EMU_EDGE;
3890

    
3891
    if(ff_h263_decode_init(avctx) < 0)
3892
        return -1;
3893
    if (vc1_init_common(v) < 0) return -1;
3894

    
3895
    avctx->coded_width = avctx->width;
3896
    avctx->coded_height = avctx->height;
3897
    if (avctx->codec_id == CODEC_ID_WMV3)
3898
    {
3899
        int count = 0;
3900

    
3901
        // looks like WMV3 has a sequence header stored in the extradata
3902
        // advanced sequence header may be before the first frame
3903
        // the last byte of the extradata is a version number, 1 for the
3904
        // samples we can decode
3905

    
3906
        init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
3907

    
3908
        if (decode_sequence_header(avctx, &gb) < 0)
3909
          return -1;
3910

    
3911
        count = avctx->extradata_size*8 - get_bits_count(&gb);
3912
        if (count>0)
3913
        {
3914
            av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
3915
                   count, get_bits(&gb, count));
3916
        }
3917
        else if (count < 0)
3918
        {
3919
            av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
3920
        }
3921
    } else { // VC1/WVC1
3922
        uint8_t *start = avctx->extradata, *end = avctx->extradata + avctx->extradata_size;
3923
        uint8_t *next; int size, buf2_size;
3924
        uint8_t *buf2 = NULL;
3925
        int seq_inited = 0, ep_inited = 0;
3926

    
3927
        if(avctx->extradata_size < 16) {
3928
            av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
3929
            return -1;
3930
        }
3931

    
3932
        buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
3933
        if(start[0]) start++; // in WVC1 extradata first byte is its size
3934
        next = start;
3935
        for(; next < end; start = next){
3936
            next = find_next_marker(start + 4, end);
3937
            size = next - start - 4;
3938
            if(size <= 0) continue;
3939
            buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
3940
            init_get_bits(&gb, buf2, buf2_size * 8);
3941
            switch(AV_RB32(start)){
3942
            case VC1_CODE_SEQHDR:
3943
                if(decode_sequence_header(avctx, &gb) < 0){
3944
                    av_free(buf2);
3945
                    return -1;
3946
                }
3947
                seq_inited = 1;
3948
                break;
3949
            case VC1_CODE_ENTRYPOINT:
3950
                if(decode_entry_point(avctx, &gb) < 0){
3951
                    av_free(buf2);
3952
                    return -1;
3953
                }
3954
                ep_inited = 1;
3955
                break;
3956
            }
3957
        }
3958
        av_free(buf2);
3959
        if(!seq_inited || !ep_inited){
3960
            av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
3961
            return -1;
3962
        }
3963
    }
3964
    avctx->has_b_frames= !!(avctx->max_b_frames);
3965
    s->low_delay = !avctx->has_b_frames;
3966

    
3967
    s->mb_width = (avctx->coded_width+15)>>4;
3968
    s->mb_height = (avctx->coded_height+15)>>4;
3969

    
3970
    /* Allocate mb bitplanes */
3971
    v->mv_type_mb_plane = av_malloc(s->mb_stride * s->mb_height);
3972
    v->direct_mb_plane = av_malloc(s->mb_stride * s->mb_height);
3973
    v->acpred_plane = av_malloc(s->mb_stride * s->mb_height);
3974
    v->over_flags_plane = av_malloc(s->mb_stride * s->mb_height);
3975

    
3976
    /* allocate block type info in that way so it could be used with s->block_index[] */
3977
    v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
3978
    v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
3979
    v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
3980
    v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
3981

    
3982
    /* Init coded blocks info */
3983
    if (v->profile == PROFILE_ADVANCED)
3984
    {
3985
//        if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
3986
//            return -1;
3987
//        if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
3988
//            return -1;
3989
    }
3990

    
3991
    return 0;
3992
}
3993

    
3994

    
3995
/** Decode a VC1/WMV3 frame
3996
 * @todo TODO: Handle VC-1 IDUs (Transport level?)
3997
 */
3998
static int vc1_decode_frame(AVCodecContext *avctx,
3999
                            void *data, int *data_size,
4000
                            uint8_t *buf, int buf_size)
4001
{
4002
    VC1Context *v = avctx->priv_data;
4003
    MpegEncContext *s = &v->s;
4004
    AVFrame *pict = data;
4005
    uint8_t *buf2 = NULL;
4006

    
4007
    /* no supplementary picture */
4008
    if (buf_size == 0) {
4009
        /* special case for last picture */
4010
        if (s->low_delay==0 && s->next_picture_ptr) {
4011
            *pict= *(AVFrame*)s->next_picture_ptr;
4012
            s->next_picture_ptr= NULL;
4013

    
4014
            *data_size = sizeof(AVFrame);
4015
        }
4016

    
4017
        return 0;
4018
    }
4019

    
4020
    //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
4021
    if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
4022
        int i= ff_find_unused_picture(s, 0);
4023
        s->current_picture_ptr= &s->picture[i];
4024
    }
4025

    
4026
    //for advanced profile we may need to parse and unescape data
4027
    if (avctx->codec_id == CODEC_ID_VC1) {
4028
        int buf_size2 = 0;
4029
        buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
4030

    
4031
        if(IS_MARKER(AV_RB32(buf))){ /* frame starts with marker and needs to be parsed */
4032
            uint8_t *start, *end, *next;
4033
            int size;
4034

    
4035
            next = buf;
4036
            for(start = buf, end = buf + buf_size; next < end; start = next){
4037
                next = find_next_marker(start + 4, end);
4038
                size = next - start - 4;
4039
                if(size <= 0) continue;
4040
                switch(AV_RB32(start)){
4041
                case VC1_CODE_FRAME:
4042
                    buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
4043
                    break;
4044
                case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
4045
                    buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
4046
                    init_get_bits(&s->gb, buf2, buf_size2*8);
4047
                    decode_entry_point(avctx, &s->gb);
4048
                    break;
4049
                case VC1_CODE_SLICE:
4050
                    av_log(avctx, AV_LOG_ERROR, "Sliced decoding is not implemented (yet)\n");
4051
                    av_free(buf2);
4052
                    return -1;
4053
                }
4054
            }
4055
        }else if(v->interlace && ((buf[0] & 0xC0) == 0xC0)){ /* WVC1 interlaced stores both fields divided by marker */
4056
            uint8_t *divider;
4057

    
4058
            divider = find_next_marker(buf, buf + buf_size);
4059
            if((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD){
4060
                av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
4061
                return -1;
4062
            }
4063

    
4064
            buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
4065
            // TODO
4066
            av_free(buf2);return -1;
4067
        }else{
4068
            buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
4069
        }
4070
        init_get_bits(&s->gb, buf2, buf_size2*8);
4071
    } else
4072
        init_get_bits(&s->gb, buf, buf_size*8);
4073
    // do parse frame header
4074
    if(v->profile < PROFILE_ADVANCED) {
4075
        if(vc1_parse_frame_header(v, &s->gb) == -1) {
4076
            av_free(buf2);
4077
            return -1;
4078
        }
4079
    } else {
4080
        if(vc1_parse_frame_header_adv(v, &s->gb) == -1) {
4081
            av_free(buf2);
4082
            return -1;
4083
        }
4084
    }
4085

    
4086
    if(s->pict_type != I_TYPE && !v->res_rtm_flag){
4087
        av_free(buf2);
4088
        return -1;
4089
    }
4090

    
4091
    // for hurry_up==5
4092
    s->current_picture.pict_type= s->pict_type;
4093
    s->current_picture.key_frame= s->pict_type == I_TYPE;
4094

    
4095
    /* skip B-frames if we don't have reference frames */
4096
    if(s->last_picture_ptr==NULL && (s->pict_type==B_TYPE || s->dropable)){
4097
        av_free(buf2);
4098
        return -1;//buf_size;
4099
    }
4100
    /* skip b frames if we are in a hurry */
4101
    if(avctx->hurry_up && s->pict_type==B_TYPE) return -1;//buf_size;
4102
    if(   (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==B_TYPE)
4103
       || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=I_TYPE)
4104
       ||  avctx->skip_frame >= AVDISCARD_ALL) {
4105
        av_free(buf2);
4106
        return buf_size;
4107
    }
4108
    /* skip everything if we are in a hurry>=5 */
4109
    if(avctx->hurry_up>=5) {
4110
        av_free(buf2);
4111
        return -1;//buf_size;
4112
    }
4113

    
4114
    if(s->next_p_frame_damaged){
4115
        if(s->pict_type==B_TYPE)
4116
            return buf_size;
4117
        else
4118
            s->next_p_frame_damaged=0;
4119
    }
4120

    
4121
    if(MPV_frame_start(s, avctx) < 0) {
4122
        av_free(buf2);
4123
        return -1;
4124
    }
4125

    
4126
    ff_er_frame_start(s);
4127

    
4128
    v->bits = buf_size * 8;
4129
    vc1_decode_blocks(v);
4130
//av_log(s->avctx, AV_LOG_INFO, "Consumed %i/%i bits\n", get_bits_count(&s->gb), buf_size*8);
4131
//  if(get_bits_count(&s->gb) > buf_size * 8)
4132
//      return -1;
4133
    ff_er_frame_end(s);
4134

    
4135
    MPV_frame_end(s);
4136

    
4137
assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
4138
assert(s->current_picture.pict_type == s->pict_type);
4139
    if (s->pict_type == B_TYPE || s->low_delay) {
4140
        *pict= *(AVFrame*)s->current_picture_ptr;
4141
    } else if (s->last_picture_ptr != NULL) {
4142
        *pict= *(AVFrame*)s->last_picture_ptr;
4143
    }
4144

    
4145
    if(s->last_picture_ptr || s->low_delay){
4146
        *data_size = sizeof(AVFrame);
4147
        ff_print_debug_info(s, pict);
4148
    }
4149

    
4150
    /* Return the Picture timestamp as the frame number */
4151
    /* we substract 1 because it is added on utils.c    */
4152
    avctx->frame_number = s->picture_number - 1;
4153

    
4154
    av_free(buf2);
4155
    return buf_size;
4156
}
4157

    
4158

    
4159
/** Close a VC1/WMV3 decoder
4160
 * @warning Initial try at using MpegEncContext stuff
4161
 */
4162
static int vc1_decode_end(AVCodecContext *avctx)
4163
{
4164
    VC1Context *v = avctx->priv_data;
4165

    
4166
    av_freep(&v->hrd_rate);
4167
    av_freep(&v->hrd_buffer);
4168
    MPV_common_end(&v->s);
4169
    av_freep(&v->mv_type_mb_plane);
4170
    av_freep(&v->direct_mb_plane);
4171
    av_freep(&v->acpred_plane);
4172
    av_freep(&v->over_flags_plane);
4173
    av_freep(&v->mb_type_base);
4174
    return 0;
4175
}
4176

    
4177

    
4178
AVCodec vc1_decoder = {
4179
    "vc1",
4180
    CODEC_TYPE_VIDEO,
4181
    CODEC_ID_VC1,
4182
    sizeof(VC1Context),
4183
    vc1_decode_init,
4184
    NULL,
4185
    vc1_decode_end,
4186
    vc1_decode_frame,
4187
    CODEC_CAP_DELAY,
4188
    NULL
4189
};
4190

    
4191
AVCodec wmv3_decoder = {
4192
    "wmv3",
4193
    CODEC_TYPE_VIDEO,
4194
    CODEC_ID_WMV3,
4195
    sizeof(VC1Context),
4196
    vc1_decode_init,
4197
    NULL,
4198
    vc1_decode_end,
4199
    vc1_decode_frame,
4200
    CODEC_CAP_DELAY,
4201
    NULL
4202
};