Statistics
| Branch: | Revision:

ffmpeg / libavcodec / vc1.c @ 3c275f6d

History | View | Annotate | Download (137 KB)

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

    
22
/**
23
 * @file vc1.c
24
 * VC-1 and WMV3 decoder
25
 *
26
 */
27
#include "common.h"
28
#include "dsputil.h"
29
#include "avcodec.h"
30
#include "mpegvideo.h"
31
#include "vc1data.h"
32
#include "vc1acdata.h"
33

    
34
#undef NDEBUG
35
#include <assert.h>
36

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

    
47

    
48
/** Available Profiles */
49
//@{
50
enum Profile {
51
    PROFILE_SIMPLE,
52
    PROFILE_MAIN,
53
    PROFILE_COMPLEX, ///< TODO: WMV9 specific
54
    PROFILE_ADVANCED
55
};
56
//@}
57

    
58
/** Sequence quantizer mode */
59
//@{
60
enum QuantMode {
61
    QUANT_FRAME_IMPLICIT,    ///< Implicitly specified at frame level
62
    QUANT_FRAME_EXPLICIT,    ///< Explicitly specified at frame level
63
    QUANT_NON_UNIFORM,       ///< Non-uniform quant used for all frames
64
    QUANT_UNIFORM            ///< Uniform quant used for all frames
65
};
66
//@}
67

    
68
/** Where quant can be changed */
69
//@{
70
enum DQProfile {
71
    DQPROFILE_FOUR_EDGES,
72
    DQPROFILE_DOUBLE_EDGES,
73
    DQPROFILE_SINGLE_EDGE,
74
    DQPROFILE_ALL_MBS
75
};
76
//@}
77

    
78
/** @name Where quant can be changed
79
 */
80
//@{
81
enum DQSingleEdge {
82
    DQSINGLE_BEDGE_LEFT,
83
    DQSINGLE_BEDGE_TOP,
84
    DQSINGLE_BEDGE_RIGHT,
85
    DQSINGLE_BEDGE_BOTTOM
86
};
87
//@}
88

    
89
/** Which pair of edges is quantized with ALTPQUANT */
90
//@{
91
enum DQDoubleEdge {
92
    DQDOUBLE_BEDGE_TOPLEFT,
93
    DQDOUBLE_BEDGE_TOPRIGHT,
94
    DQDOUBLE_BEDGE_BOTTOMRIGHT,
95
    DQDOUBLE_BEDGE_BOTTOMLEFT
96
};
97
//@}
98

    
99
/** MV modes for P frames */
100
//@{
101
enum MVModes {
102
    MV_PMODE_1MV_HPEL_BILIN,
103
    MV_PMODE_1MV,
104
    MV_PMODE_1MV_HPEL,
105
    MV_PMODE_MIXED_MV,
106
    MV_PMODE_INTENSITY_COMP
107
};
108
//@}
109

    
110
/** @name MV types for B frames */
111
//@{
112
enum BMVTypes {
113
    BMV_TYPE_BACKWARD,
114
    BMV_TYPE_FORWARD,
115
    BMV_TYPE_INTERPOLATED
116
};
117
//@}
118

    
119
/** @name Block types for P/B frames */
120
//@{
121
enum TransformTypes {
122
    TT_8X8,
123
    TT_8X4_BOTTOM,
124
    TT_8X4_TOP,
125
    TT_8X4, //Both halves
126
    TT_4X8_RIGHT,
127
    TT_4X8_LEFT,
128
    TT_4X8, //Both halves
129
    TT_4X4
130
};
131
//@}
132

    
133
/** Table for conversion between TTBLK and TTMB */
134
static const int ttblk_to_tt[3][8] = {
135
  { TT_8X4, TT_4X8, TT_8X8, TT_4X4, TT_8X4_TOP, TT_8X4_BOTTOM, TT_4X8_RIGHT, TT_4X8_LEFT },
136
  { TT_8X8, TT_4X8_RIGHT, TT_4X8_LEFT, TT_4X4, TT_8X4, TT_4X8, TT_8X4_BOTTOM, TT_8X4_TOP },
137
  { TT_8X8, TT_4X8, TT_4X4, TT_8X4_BOTTOM, TT_4X8_RIGHT, TT_4X8_LEFT, TT_8X4, TT_8X4_TOP }
138
};
139

    
140
static const int ttfrm_to_tt[4] = { TT_8X8, TT_8X4, TT_4X8, TT_4X4 };
141

    
142
/** MV P mode - the 5th element is only used for mode 1 */
143
static const uint8_t mv_pmode_table[2][5] = {
144
  { MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_1MV, MV_PMODE_1MV_HPEL, MV_PMODE_INTENSITY_COMP, MV_PMODE_MIXED_MV },
145
  { MV_PMODE_1MV, MV_PMODE_MIXED_MV, MV_PMODE_1MV_HPEL, MV_PMODE_INTENSITY_COMP, MV_PMODE_1MV_HPEL_BILIN }
146
};
147
static const uint8_t mv_pmode_table2[2][4] = {
148
  { MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_1MV, MV_PMODE_1MV_HPEL, MV_PMODE_MIXED_MV },
149
  { MV_PMODE_1MV, MV_PMODE_MIXED_MV, MV_PMODE_1MV_HPEL, MV_PMODE_1MV_HPEL_BILIN }
150
};
151

    
152
/** One more frame type */
153
#define BI_TYPE 7
154

    
155
static const int fps_nr[5] = { 24, 25, 30, 50, 60 },
156
  fps_dr[2] = { 1000, 1001 };
157
static const uint8_t pquant_table[3][32] = {
158
  {  /* Implicit quantizer */
159
     0,  1,  2,  3,  4,  5,  6,  7,  8,  6,  7,  8,  9, 10, 11, 12,
160
    13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 29, 31
161
  },
162
  {  /* Explicit quantizer, pquantizer uniform */
163
     0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
164
    16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
165
  },
166
  {  /* Explicit quantizer, pquantizer non-uniform */
167
     0,  1,  1,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13,
168
    14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 31
169
  }
170
};
171

    
172
/** @name VC-1 VLC tables and defines
173
 *  @todo TODO move this into the context
174
 */
175
//@{
176
#define VC1_BFRACTION_VLC_BITS 7
177
static VLC vc1_bfraction_vlc;
178
#define VC1_IMODE_VLC_BITS 4
179
static VLC vc1_imode_vlc;
180
#define VC1_NORM2_VLC_BITS 3
181
static VLC vc1_norm2_vlc;
182
#define VC1_NORM6_VLC_BITS 9
183
static VLC vc1_norm6_vlc;
184
/* Could be optimized, one table only needs 8 bits */
185
#define VC1_TTMB_VLC_BITS 9 //12
186
static VLC vc1_ttmb_vlc[3];
187
#define VC1_MV_DIFF_VLC_BITS 9 //15
188
static VLC vc1_mv_diff_vlc[4];
189
#define VC1_CBPCY_P_VLC_BITS 9 //14
190
static VLC vc1_cbpcy_p_vlc[4];
191
#define VC1_4MV_BLOCK_PATTERN_VLC_BITS 6
192
static VLC vc1_4mv_block_pattern_vlc[4];
193
#define VC1_TTBLK_VLC_BITS 5
194
static VLC vc1_ttblk_vlc[3];
195
#define VC1_SUBBLKPAT_VLC_BITS 6
196
static VLC vc1_subblkpat_vlc[3];
197

    
198
static VLC vc1_ac_coeff_table[8];
199
//@}
200

    
201
enum CodingSet {
202
    CS_HIGH_MOT_INTRA = 0,
203
    CS_HIGH_MOT_INTER,
204
    CS_LOW_MOT_INTRA,
205
    CS_LOW_MOT_INTER,
206
    CS_MID_RATE_INTRA,
207
    CS_MID_RATE_INTER,
208
    CS_HIGH_RATE_INTRA,
209
    CS_HIGH_RATE_INTER
210
};
211

    
212
/** @name Overlap conditions for Advanced Profile */
213
//@{
214
enum COTypes {
215
    CONDOVER_NONE = 0,
216
    CONDOVER_ALL,
217
    CONDOVER_SELECT
218
};
219
//@}
220

    
221

    
222
/** The VC1 Context
223
 * @fixme Change size wherever another size is more efficient
224
 * Many members are only used for Advanced Profile
225
 */
226
typedef struct VC1Context{
227
    MpegEncContext s;
228

    
229
    int bits;
230

    
231
    /** Simple/Main Profile sequence header */
232
    //@{
233
    int res_sm;           ///< reserved, 2b
234
    int res_x8;           ///< reserved
235
    int multires;         ///< frame-level RESPIC syntax element present
236
    int res_fasttx;       ///< reserved, always 1
237
    int res_transtab;     ///< reserved, always 0
238
    int rangered;         ///< RANGEREDFRM (range reduction) syntax element present
239
                          ///< at frame level
240
    int res_rtm_flag;     ///< reserved, set to 1
241
    int reserved;         ///< reserved
242
    //@}
243

    
244
    /** Advanced Profile */
245
    //@{
246
    int level;            ///< 3bits, for Advanced/Simple Profile, provided by TS layer
247
    int chromaformat;     ///< 2bits, 2=4:2:0, only defined
248
    int postprocflag;     ///< Per-frame processing suggestion flag present
249
    int broadcast;        ///< TFF/RFF present
250
    int interlace;        ///< Progressive/interlaced (RPTFTM syntax element)
251
    int tfcntrflag;       ///< TFCNTR present
252
    int panscanflag;      ///< NUMPANSCANWIN, TOPLEFT{X,Y}, BOTRIGHT{X,Y} present
253
    int extended_dmv;     ///< Additional extended dmv range at P/B frame-level
254
    int color_prim;       ///< 8bits, chroma coordinates of the color primaries
255
    int transfer_char;    ///< 8bits, Opto-electronic transfer characteristics
256
    int matrix_coef;      ///< 8bits, Color primaries->YCbCr transform matrix
257
    int hrd_param_flag;   ///< Presence of Hypothetical Reference
258
                          ///< Decoder parameters
259
    int psf;              ///< Progressive Segmented Frame
260
    //@}
261

    
262
    /** Sequence header data for all Profiles
263
     * TODO: choose between ints, uint8_ts and monobit flags
264
     */
265
    //@{
266
    int profile;          ///< 2bits, Profile
267
    int frmrtq_postproc;  ///< 3bits,
268
    int bitrtq_postproc;  ///< 5bits, quantized framerate-based postprocessing strength
269
    int fastuvmc;         ///< Rounding of qpel vector to hpel ? (not in Simple)
270
    int extended_mv;      ///< Ext MV in P/B (not in Simple)
271
    int dquant;           ///< How qscale varies with MBs, 2bits (not in Simple)
272
    int vstransform;      ///< variable-size [48]x[48] transform type + info
273
    int overlap;          ///< overlapped transforms in use
274
    int quantizer_mode;   ///< 2bits, quantizer mode used for sequence, see QUANT_*
275
    int finterpflag;      ///< INTERPFRM present
276
    //@}
277

    
278
    /** Frame decoding info for all profiles */
279
    //@{
280
    uint8_t mv_mode;      ///< MV coding monde
281
    uint8_t mv_mode2;     ///< Secondary MV coding mode (B frames)
282
    int k_x;              ///< Number of bits for MVs (depends on MV range)
283
    int k_y;              ///< Number of bits for MVs (depends on MV range)
284
    int range_x, range_y; ///< MV range
285
    uint8_t pq, altpq;    ///< Current/alternate frame quantizer scale
286
    /** pquant parameters */
287
    //@{
288
    uint8_t dquantfrm;
289
    uint8_t dqprofile;
290
    uint8_t dqsbedge;
291
    uint8_t dqbilevel;
292
    //@}
293
    /** AC coding set indexes
294
     * @see 8.1.1.10, p(1)10
295
     */
296
    //@{
297
    int c_ac_table_index; ///< Chroma index from ACFRM element
298
    int y_ac_table_index; ///< Luma index from AC2FRM element
299
    //@}
300
    int ttfrm;            ///< Transform type info present at frame level
301
    uint8_t ttmbf;        ///< Transform type flag
302
    uint8_t ttblk4x4;     ///< Value of ttblk which indicates a 4x4 transform
303
    int codingset;        ///< index of current table set from 11.8 to use for luma block decoding
304
    int codingset2;       ///< index of current table set from 11.8 to use for chroma block decoding
305
    int pqindex;          ///< raw pqindex used in coding set selection
306
    int a_avail, c_avail;
307
    uint8_t *mb_type_base, *mb_type[3];
308

    
309

    
310
    /** Luma compensation parameters */
311
    //@{
312
    uint8_t lumscale;
313
    uint8_t lumshift;
314
    //@}
315
    int16_t bfraction;    ///< Relative position % anchors=> how to scale MVs
316
    uint8_t halfpq;       ///< Uniform quant over image and qp+.5
317
    uint8_t respic;       ///< Frame-level flag for resized images
318
    int buffer_fullness;  ///< HRD info
319
    /** Ranges:
320
     * -# 0 -> [-64n 63.f] x [-32, 31.f]
321
     * -# 1 -> [-128, 127.f] x [-64, 63.f]
322
     * -# 2 -> [-512, 511.f] x [-128, 127.f]
323
     * -# 3 -> [-1024, 1023.f] x [-256, 255.f]
324
     */
325
    uint8_t mvrange;
326
    uint8_t pquantizer;           ///< Uniform (over sequence) quantizer in use
327
    VLC *cbpcy_vlc;               ///< CBPCY VLC table
328
    int tt_index;                 ///< Index for Transform Type tables
329
    uint8_t* mv_type_mb_plane;    ///< bitplane for mv_type == (4MV)
330
    uint8_t* direct_mb_plane;     ///< bitplane for "direct" MBs
331
    int mv_type_is_raw;           ///< mv type mb plane is not coded
332
    int dmb_is_raw;               ///< direct mb plane is raw
333
    int skip_is_raw;              ///< skip mb plane is not coded
334
    uint8_t luty[256], lutuv[256]; // lookup tables used for intensity compensation
335
    int rnd;                      ///< rounding control
336

    
337
    /** Frame decoding info for S/M profiles only */
338
    //@{
339
    uint8_t rangeredfrm; ///< out_sample = CLIP((in_sample-128)*2+128)
340
    uint8_t interpfrm;
341
    //@}
342

    
343
    /** Frame decoding info for Advanced profile */
344
    //@{
345
    uint8_t fcm; ///< 0->Progressive, 2->Frame-Interlace, 3->Field-Interlace
346
    uint8_t numpanscanwin;
347
    uint8_t tfcntr;
348
    uint8_t rptfrm, tff, rff;
349
    uint16_t topleftx;
350
    uint16_t toplefty;
351
    uint16_t bottomrightx;
352
    uint16_t bottomrighty;
353
    uint8_t uvsamp;
354
    uint8_t postproc;
355
    int hrd_num_leaky_buckets;
356
    uint8_t bit_rate_exponent;
357
    uint8_t buffer_size_exponent;
358
    uint8_t* acpred_plane;       ///< AC prediction flags bitplane
359
    int acpred_is_raw;
360
    uint8_t* over_flags_plane;   ///< Overflags bitplane
361
    int overflg_is_raw;
362
    uint8_t condover;
363
    uint16_t *hrd_rate, *hrd_buffer;
364
    uint8_t *hrd_fullness;
365
    uint8_t range_mapy_flag;
366
    uint8_t range_mapuv_flag;
367
    uint8_t range_mapy;
368
    uint8_t range_mapuv;
369
    //@}
370

    
371
    int p_frame_skipped;
372
} VC1Context;
373

    
374
/**
375
 * Get unary code of limited length
376
 * @fixme FIXME Slow and ugly
377
 * @param gb GetBitContext
378
 * @param[in] stop The bitstop value (unary code of 1's or 0's)
379
 * @param[in] len Maximum length
380
 * @return Unary length/index
381
 */
382
static int get_prefix(GetBitContext *gb, int stop, int len)
383
{
384
#if 1
385
    int i;
386

    
387
    for(i = 0; i < len && get_bits1(gb) != stop; i++);
388
    return i;
389
/*  int i = 0, tmp = !stop;
390

391
  while (i != len && tmp != stop)
392
  {
393
    tmp = get_bits(gb, 1);
394
    i++;
395
  }
396
  if (i == len && tmp != stop) return len+1;
397
  return i;*/
398
#else
399
  unsigned int buf;
400
  int log;
401

    
402
  OPEN_READER(re, gb);
403
  UPDATE_CACHE(re, gb);
404
  buf=GET_CACHE(re, gb); //Still not sure
405
  if (stop) buf = ~buf;
406

    
407
  log= av_log2(-buf); //FIXME: -?
408
  if (log < limit){
409
    LAST_SKIP_BITS(re, gb, log+1);
410
    CLOSE_READER(re, gb);
411
    return log;
412
  }
413

    
414
  LAST_SKIP_BITS(re, gb, limit);
415
  CLOSE_READER(re, gb);
416
  return limit;
417
#endif
418
}
419

    
420
static inline int decode210(GetBitContext *gb){
421
    int n;
422
    n = get_bits1(gb);
423
    if (n == 1)
424
        return 0;
425
    else
426
        return 2 - get_bits1(gb);
427
}
428

    
429
/**
430
 * Init VC-1 specific tables and VC1Context members
431
 * @param v The VC1Context to initialize
432
 * @return Status
433
 */
434
static int vc1_init_common(VC1Context *v)
435
{
436
    static int done = 0;
437
    int i = 0;
438

    
439
    v->hrd_rate = v->hrd_buffer = NULL;
440

    
441
    /* VLC tables */
442
    if(!done)
443
    {
444
        done = 1;
445
        init_vlc(&vc1_bfraction_vlc, VC1_BFRACTION_VLC_BITS, 23,
446
                 vc1_bfraction_bits, 1, 1,
447
                 vc1_bfraction_codes, 1, 1, 1);
448
        init_vlc(&vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4,
449
                 vc1_norm2_bits, 1, 1,
450
                 vc1_norm2_codes, 1, 1, 1);
451
        init_vlc(&vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 64,
452
                 vc1_norm6_bits, 1, 1,
453
                 vc1_norm6_codes, 2, 2, 1);
454
        init_vlc(&vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7,
455
                 vc1_imode_bits, 1, 1,
456
                 vc1_imode_codes, 1, 1, 1);
457
        for (i=0; i<3; i++)
458
        {
459
            init_vlc(&vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 16,
460
                     vc1_ttmb_bits[i], 1, 1,
461
                     vc1_ttmb_codes[i], 2, 2, 1);
462
            init_vlc(&vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 8,
463
                     vc1_ttblk_bits[i], 1, 1,
464
                     vc1_ttblk_codes[i], 1, 1, 1);
465
            init_vlc(&vc1_subblkpat_vlc[i], VC1_SUBBLKPAT_VLC_BITS, 15,
466
                     vc1_subblkpat_bits[i], 1, 1,
467
                     vc1_subblkpat_codes[i], 1, 1, 1);
468
        }
469
        for(i=0; i<4; i++)
470
        {
471
            init_vlc(&vc1_4mv_block_pattern_vlc[i], VC1_4MV_BLOCK_PATTERN_VLC_BITS, 16,
472
                     vc1_4mv_block_pattern_bits[i], 1, 1,
473
                     vc1_4mv_block_pattern_codes[i], 1, 1, 1);
474
            init_vlc(&vc1_cbpcy_p_vlc[i], VC1_CBPCY_P_VLC_BITS, 64,
475
                     vc1_cbpcy_p_bits[i], 1, 1,
476
                     vc1_cbpcy_p_codes[i], 2, 2, 1);
477
            init_vlc(&vc1_mv_diff_vlc[i], VC1_MV_DIFF_VLC_BITS, 73,
478
                     vc1_mv_diff_bits[i], 1, 1,
479
                     vc1_mv_diff_codes[i], 2, 2, 1);
480
        }
481
        for(i=0; i<8; i++)
482
            init_vlc(&vc1_ac_coeff_table[i], AC_VLC_BITS, vc1_ac_sizes[i],
483
                     &vc1_ac_tables[i][0][1], 8, 4,
484
                     &vc1_ac_tables[i][0][0], 8, 4, 1);
485
        init_vlc(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64,
486
                 &ff_msmp4_mb_i_table[0][1], 4, 2,
487
                 &ff_msmp4_mb_i_table[0][0], 4, 2, 1);
488
    }
489

    
490
    /* Other defaults */
491
    v->pq = -1;
492
    v->mvrange = 0; /* 7.1.1.18, p80 */
493

    
494
    return 0;
495
}
496

    
497
/***********************************************************************/
498
/**
499
 * @defgroup bitplane VC9 Bitplane decoding
500
 * @see 8.7, p56
501
 * @{
502
 */
503

    
504
/** @addtogroup bitplane
505
 * Imode types
506
 * @{
507
 */
508
enum Imode {
509
    IMODE_RAW,
510
    IMODE_NORM2,
511
    IMODE_DIFF2,
512
    IMODE_NORM6,
513
    IMODE_DIFF6,
514
    IMODE_ROWSKIP,
515
    IMODE_COLSKIP
516
};
517
/** @} */ //imode defines
518

    
519
/** Decode rows by checking if they are skipped
520
 * @param plane Buffer to store decoded bits
521
 * @param[in] width Width of this buffer
522
 * @param[in] height Height of this buffer
523
 * @param[in] stride of this buffer
524
 */
525
static void decode_rowskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
526
    int x, y;
527

    
528
    for (y=0; y<height; y++){
529
        if (!get_bits(gb, 1)) //rowskip
530
            memset(plane, 0, width);
531
        else
532
            for (x=0; x<width; x++)
533
                plane[x] = get_bits(gb, 1);
534
        plane += stride;
535
    }
536
}
537

    
538
/** Decode columns by checking if they are skipped
539
 * @param plane Buffer to store decoded bits
540
 * @param[in] width Width of this buffer
541
 * @param[in] height Height of this buffer
542
 * @param[in] stride of this buffer
543
 * @fixme FIXME: Optimize
544
 */
545
static void decode_colskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
546
    int x, y;
547

    
548
    for (x=0; x<width; x++){
549
        if (!get_bits(gb, 1)) //colskip
550
            for (y=0; y<height; y++)
551
                plane[y*stride] = 0;
552
        else
553
            for (y=0; y<height; y++)
554
                plane[y*stride] = get_bits(gb, 1);
555
        plane ++;
556
    }
557
}
558

    
559
/** Decode a bitplane's bits
560
 * @param bp Bitplane where to store the decode bits
561
 * @param v VC-1 context for bit reading and logging
562
 * @return Status
563
 * @fixme FIXME: Optimize
564
 * @todo TODO: Decide if a struct is needed
565
 */
566
static int bitplane_decoding(uint8_t* data, int *raw_flag, VC1Context *v)
567
{
568
    GetBitContext *gb = &v->s.gb;
569

    
570
    int imode, x, y, code, offset;
571
    uint8_t invert, *planep = data;
572
    int width, height, stride;
573

    
574
    width = v->s.mb_width;
575
    height = v->s.mb_height;
576
    stride = v->s.mb_stride;
577
    invert = get_bits(gb, 1);
578
    imode = get_vlc2(gb, vc1_imode_vlc.table, VC1_IMODE_VLC_BITS, 1);
579

    
580
    *raw_flag = 0;
581
    switch (imode)
582
    {
583
    case IMODE_RAW:
584
        //Data is actually read in the MB layer (same for all tests == "raw")
585
        *raw_flag = 1; //invert ignored
586
        return invert;
587
    case IMODE_DIFF2:
588
    case IMODE_NORM2:
589
        if ((height * width) & 1)
590
        {
591
            *planep++ = get_bits(gb, 1);
592
            offset = 1;
593
        }
594
        else offset = 0;
595
        // decode bitplane as one long line
596
        for (y = offset; y < height * width; y += 2) {
597
            code = get_vlc2(gb, vc1_norm2_vlc.table, VC1_NORM2_VLC_BITS, 1);
598
            *planep++ = code & 1;
599
            offset++;
600
            if(offset == width) {
601
                offset = 0;
602
                planep += stride - width;
603
            }
604
            *planep++ = code >> 1;
605
            offset++;
606
            if(offset == width) {
607
                offset = 0;
608
                planep += stride - width;
609
            }
610
        }
611
        break;
612
    case IMODE_DIFF6:
613
    case IMODE_NORM6:
614
        if(!(height % 3) && (width % 3)) { // use 2x3 decoding
615
            for(y = 0; y < height; y+= 3) {
616
                for(x = width & 1; x < width; x += 2) {
617
                    code = get_vlc2(gb, vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
618
                    if(code < 0){
619
                        av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
620
                        return -1;
621
                    }
622
                    planep[x + 0] = (code >> 0) & 1;
623
                    planep[x + 1] = (code >> 1) & 1;
624
                    planep[x + 0 + stride] = (code >> 2) & 1;
625
                    planep[x + 1 + stride] = (code >> 3) & 1;
626
                    planep[x + 0 + stride * 2] = (code >> 4) & 1;
627
                    planep[x + 1 + stride * 2] = (code >> 5) & 1;
628
                }
629
                planep += stride * 3;
630
            }
631
            if(width & 1) decode_colskip(data, 1, height, stride, &v->s.gb);
632
        } else { // 3x2
633
            planep += (height & 1) * stride;
634
            for(y = height & 1; y < height; y += 2) {
635
                for(x = width % 3; x < width; x += 3) {
636
                    code = get_vlc2(gb, vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
637
                    if(code < 0){
638
                        av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
639
                        return -1;
640
                    }
641
                    planep[x + 0] = (code >> 0) & 1;
642
                    planep[x + 1] = (code >> 1) & 1;
643
                    planep[x + 2] = (code >> 2) & 1;
644
                    planep[x + 0 + stride] = (code >> 3) & 1;
645
                    planep[x + 1 + stride] = (code >> 4) & 1;
646
                    planep[x + 2 + stride] = (code >> 5) & 1;
647
                }
648
                planep += stride * 2;
649
            }
650
            x = width % 3;
651
            if(x) decode_colskip(data  ,             x, height    , stride, &v->s.gb);
652
            if(height & 1) decode_rowskip(data+x, width - x, 1, stride, &v->s.gb);
653
        }
654
        break;
655
    case IMODE_ROWSKIP:
656
        decode_rowskip(data, width, height, stride, &v->s.gb);
657
        break;
658
    case IMODE_COLSKIP:
659
        decode_colskip(data, width, height, stride, &v->s.gb);
660
        break;
661
    default: break;
662
    }
663

    
664
    /* Applying diff operator */
665
    if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6)
666
    {
667
        planep = data;
668
        planep[0] ^= invert;
669
        for (x=1; x<width; x++)
670
            planep[x] ^= planep[x-1];
671
        for (y=1; y<height; y++)
672
        {
673
            planep += stride;
674
            planep[0] ^= planep[-stride];
675
            for (x=1; x<width; x++)
676
            {
677
                if (planep[x-1] != planep[x-stride]) planep[x] ^= invert;
678
                else                                 planep[x] ^= planep[x-1];
679
            }
680
        }
681
    }
682
    else if (invert)
683
    {
684
        planep = data;
685
        for (x=0; x<stride*height; x++) planep[x] = !planep[x]; //FIXME stride
686
    }
687
    return (imode<<1) + invert;
688
}
689

    
690
/** @} */ //Bitplane group
691

    
692
/***********************************************************************/
693
/** VOP Dquant decoding
694
 * @param v VC-1 Context
695
 */
696
static int vop_dquant_decoding(VC1Context *v)
697
{
698
    GetBitContext *gb = &v->s.gb;
699
    int pqdiff;
700

    
701
    //variable size
702
    if (v->dquant == 2)
703
    {
704
        pqdiff = get_bits(gb, 3);
705
        if (pqdiff == 7) v->altpq = get_bits(gb, 5);
706
        else v->altpq = v->pq + pqdiff + 1;
707
    }
708
    else
709
    {
710
        v->dquantfrm = get_bits(gb, 1);
711
        if ( v->dquantfrm )
712
        {
713
            v->dqprofile = get_bits(gb, 2);
714
            switch (v->dqprofile)
715
            {
716
            case DQPROFILE_SINGLE_EDGE:
717
            case DQPROFILE_DOUBLE_EDGES:
718
                v->dqsbedge = get_bits(gb, 2);
719
                break;
720
            case DQPROFILE_ALL_MBS:
721
                v->dqbilevel = get_bits(gb, 1);
722
            default: break; //Forbidden ?
723
            }
724
            if (v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS)
725
            {
726
                pqdiff = get_bits(gb, 3);
727
                if (pqdiff == 7) v->altpq = get_bits(gb, 5);
728
                else v->altpq = v->pq + pqdiff + 1;
729
            }
730
        }
731
    }
732
    return 0;
733
}
734

    
735
/** Put block onto picture
736
 * @todo move to DSPContext
737
 */
738
static void vc1_put_block(VC1Context *v, DCTELEM block[6][64])
739
{
740
    uint8_t *Y;
741
    int ys, us, vs;
742
    DSPContext *dsp = &v->s.dsp;
743

    
744
    if(v->rangeredfrm) {
745
        int i, j, k;
746
        for(k = 0; k < 6; k++)
747
            for(j = 0; j < 8; j++)
748
                for(i = 0; i < 8; i++)
749
                    block[k][i + j*8] = ((block[k][i + j*8] - 128) << 1) + 128;
750

    
751
    }
752
    ys = v->s.current_picture.linesize[0];
753
    us = v->s.current_picture.linesize[1];
754
    vs = v->s.current_picture.linesize[2];
755
    Y = v->s.dest[0];
756

    
757
    dsp->put_pixels_clamped(block[0], Y, ys);
758
    dsp->put_pixels_clamped(block[1], Y + 8, ys);
759
    Y += ys * 8;
760
    dsp->put_pixels_clamped(block[2], Y, ys);
761
    dsp->put_pixels_clamped(block[3], Y + 8, ys);
762

    
763
    if(!(v->s.flags & CODEC_FLAG_GRAY)) {
764
        dsp->put_pixels_clamped(block[4], v->s.dest[1], us);
765
        dsp->put_pixels_clamped(block[5], v->s.dest[2], vs);
766
    }
767
}
768

    
769
/** Do motion compensation over 1 macroblock
770
 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
771
 */
772
static void vc1_mc_1mv(VC1Context *v, int dir)
773
{
774
    MpegEncContext *s = &v->s;
775
    DSPContext *dsp = &v->s.dsp;
776
    uint8_t *srcY, *srcU, *srcV;
777
    int dxy, uvdxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
778

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

    
781
    mx = s->mv[0][0][0];
782
    my = s->mv[0][0][1];
783
    uvmx = (mx + ((mx & 3) == 3)) >> 1;
784
    uvmy = (my + ((my & 3) == 3)) >> 1;
785
    if(!dir) {
786
        srcY = s->last_picture.data[0];
787
        srcU = s->last_picture.data[1];
788
        srcV = s->last_picture.data[2];
789
    } else {
790
        srcY = s->next_picture.data[0];
791
        srcU = s->next_picture.data[1];
792
        srcV = s->next_picture.data[2];
793
    }
794

    
795
    src_x = s->mb_x * 16 + (mx >> 2);
796
    src_y = s->mb_y * 16 + (my >> 2);
797
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
798
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
799

    
800
    src_x   = clip(  src_x, -16, s->mb_width  * 16);
801
    src_y   = clip(  src_y, -16, s->mb_height * 16);
802
    uvsrc_x = clip(uvsrc_x,  -8, s->mb_width  *  8);
803
    uvsrc_y = clip(uvsrc_y,  -8, s->mb_height *  8);
804

    
805
    srcY += src_y * s->linesize + src_x;
806
    srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
807
    srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
808

    
809
    /* for grayscale we should not try to read from unknown area */
810
    if(s->flags & CODEC_FLAG_GRAY) {
811
        srcU = s->edge_emu_buffer + 18 * s->linesize;
812
        srcV = s->edge_emu_buffer + 18 * s->linesize;
813
    }
814

    
815
    if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
816
       || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3
817
       || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){
818
        uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
819

    
820
        srcY -= s->mspel * (1 + s->linesize);
821
        ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
822
                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
823
        srcY = s->edge_emu_buffer;
824
        ff_emulated_edge_mc(uvbuf     , srcU, s->uvlinesize, 8+1, 8+1,
825
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
826
        ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
827
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
828
        srcU = uvbuf;
829
        srcV = uvbuf + 16;
830
        /* if we deal with range reduction we need to scale source blocks */
831
        if(v->rangeredfrm) {
832
            int i, j;
833
            uint8_t *src, *src2;
834

    
835
            src = srcY;
836
            for(j = 0; j < 17 + s->mspel*2; j++) {
837
                for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
838
                src += s->linesize;
839
            }
840
            src = srcU; src2 = srcV;
841
            for(j = 0; j < 9; j++) {
842
                for(i = 0; i < 9; i++) {
843
                    src[i] = ((src[i] - 128) >> 1) + 128;
844
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
845
                }
846
                src += s->uvlinesize;
847
                src2 += s->uvlinesize;
848
            }
849
        }
850
        /* if we deal with intensity compensation we need to scale source blocks */
851
        if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
852
            int i, j;
853
            uint8_t *src, *src2;
854

    
855
            src = srcY;
856
            for(j = 0; j < 17 + s->mspel*2; j++) {
857
                for(i = 0; i < 17 + s->mspel*2; i++) src[i] = v->luty[src[i]];
858
                src += s->linesize;
859
            }
860
            src = srcU; src2 = srcV;
861
            for(j = 0; j < 9; j++) {
862
                for(i = 0; i < 9; i++) {
863
                    src[i] = v->lutuv[src[i]];
864
                    src2[i] = v->lutuv[src2[i]];
865
                }
866
                src += s->uvlinesize;
867
                src2 += s->uvlinesize;
868
            }
869
        }
870
        srcY += s->mspel * (1 + s->linesize);
871
    }
872

    
873
    if(v->fastuvmc) {
874
        uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
875
        uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
876
    }
877

    
878
    if(s->mspel) {
879
        dxy = ((my & 3) << 2) | (mx & 3);
880
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0]    , srcY    , s->linesize, v->rnd);
881
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
882
        srcY += s->linesize * 8;
883
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize    , srcY    , s->linesize, v->rnd);
884
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
885
    } else { // hpel mc - always used for luma
886
        dxy = (my & 2) | ((mx & 2) >> 1);
887

    
888
        if(!v->rnd)
889
            dsp->put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
890
        else
891
            dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
892
    }
893

    
894
    if(s->flags & CODEC_FLAG_GRAY) return;
895
    /* Chroma MC always uses qpel bilinear */
896
    uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
897
    uvmx = (uvmx&3)<<1;
898
    uvmy = (uvmy&3)<<1;
899
    if(!v->rnd){
900
        dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
901
        dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
902
    }else{
903
        dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
904
        dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
905
    }
906
}
907

    
908
/** Do motion compensation for 4-MV macroblock - luminance block
909
 */
910
static void vc1_mc_4mv_luma(VC1Context *v, int n)
911
{
912
    MpegEncContext *s = &v->s;
913
    DSPContext *dsp = &v->s.dsp;
914
    uint8_t *srcY;
915
    int dxy, mx, my, src_x, src_y;
916
    int off;
917

    
918
    if(!v->s.last_picture.data[0])return;
919
    mx = s->mv[0][n][0];
920
    my = s->mv[0][n][1];
921
    srcY = s->last_picture.data[0];
922

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

    
925
    src_x = s->mb_x * 16 + (n&1) * 8 + (mx >> 2);
926
    src_y = s->mb_y * 16 + (n&2) * 4 + (my >> 2);
927

    
928
    src_x   = clip(  src_x, -16, s->mb_width  * 16);
929
    src_y   = clip(  src_y, -16, s->mb_height * 16);
930

    
931
    srcY += src_y * s->linesize + src_x;
932

    
933
    if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
934
       || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 8 - s->mspel*2
935
       || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 8 - s->mspel*2){
936
        srcY -= s->mspel * (1 + s->linesize);
937
        ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 9+s->mspel*2, 9+s->mspel*2,
938
                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
939
        srcY = s->edge_emu_buffer;
940
        /* if we deal with range reduction we need to scale source blocks */
941
        if(v->rangeredfrm) {
942
            int i, j;
943
            uint8_t *src;
944

    
945
            src = srcY;
946
            for(j = 0; j < 9 + s->mspel*2; j++) {
947
                for(i = 0; i < 9 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
948
                src += s->linesize;
949
            }
950
        }
951
        /* if we deal with intensity compensation we need to scale source blocks */
952
        if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
953
            int i, j;
954
            uint8_t *src;
955

    
956
            src = srcY;
957
            for(j = 0; j < 9 + s->mspel*2; j++) {
958
                for(i = 0; i < 9 + s->mspel*2; i++) src[i] = v->luty[src[i]];
959
                src += s->linesize;
960
            }
961
        }
962
        srcY += s->mspel * (1 + s->linesize);
963
    }
964

    
965
    if(s->mspel) {
966
        dxy = ((my & 3) << 2) | (mx & 3);
967
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, v->rnd);
968
    } else { // hpel mc - always used for luma
969
        dxy = (my & 2) | ((mx & 2) >> 1);
970
        if(!v->rnd)
971
            dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
972
        else
973
            dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
974
    }
975
}
976

    
977
static inline int median4(int a, int b, int c, int d)
978
{
979
    if(a < b) {
980
        if(c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
981
        else      return (FFMIN(b, c) + FFMAX(a, d)) / 2;
982
    } else {
983
        if(c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
984
        else      return (FFMIN(a, c) + FFMAX(b, d)) / 2;
985
    }
986
}
987

    
988

    
989
/** Do motion compensation for 4-MV macroblock - both chroma blocks
990
 */
991
static void vc1_mc_4mv_chroma(VC1Context *v)
992
{
993
    MpegEncContext *s = &v->s;
994
    DSPContext *dsp = &v->s.dsp;
995
    uint8_t *srcU, *srcV;
996
    int uvdxy, uvmx, uvmy, uvsrc_x, uvsrc_y;
997
    int i, idx, tx = 0, ty = 0;
998
    int mvx[4], mvy[4], intra[4];
999
    static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
1000

    
1001
    if(!v->s.last_picture.data[0])return;
1002
    if(s->flags & CODEC_FLAG_GRAY) return;
1003

    
1004
    for(i = 0; i < 4; i++) {
1005
        mvx[i] = s->mv[0][i][0];
1006
        mvy[i] = s->mv[0][i][1];
1007
        intra[i] = v->mb_type[0][s->block_index[i]];
1008
    }
1009

    
1010
    /* calculate chroma MV vector from four luma MVs */
1011
    idx = (intra[3] << 3) | (intra[2] << 2) | (intra[1] << 1) | intra[0];
1012
    if(!idx) { // all blocks are inter
1013
        tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
1014
        ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
1015
    } else if(count[idx] == 1) { // 3 inter blocks
1016
        switch(idx) {
1017
        case 0x1:
1018
            tx = mid_pred(mvx[1], mvx[2], mvx[3]);
1019
            ty = mid_pred(mvy[1], mvy[2], mvy[3]);
1020
            break;
1021
        case 0x2:
1022
            tx = mid_pred(mvx[0], mvx[2], mvx[3]);
1023
            ty = mid_pred(mvy[0], mvy[2], mvy[3]);
1024
            break;
1025
        case 0x4:
1026
            tx = mid_pred(mvx[0], mvx[1], mvx[3]);
1027
            ty = mid_pred(mvy[0], mvy[1], mvy[3]);
1028
            break;
1029
        case 0x8:
1030
            tx = mid_pred(mvx[0], mvx[1], mvx[2]);
1031
            ty = mid_pred(mvy[0], mvy[1], mvy[2]);
1032
            break;
1033
        }
1034
    } else if(count[idx] == 2) {
1035
        int t1 = 0, t2 = 0;
1036
        for(i=0; i<3;i++) if(!intra[i]) {t1 = i; break;}
1037
        for(i= t1+1; i<4; i++)if(!intra[i]) {t2 = i; break;}
1038
        tx = (mvx[t1] + mvx[t2]) / 2;
1039
        ty = (mvy[t1] + mvy[t2]) / 2;
1040
    } else
1041
        return; //no need to do MC for inter blocks
1042

    
1043
    uvmx = (tx + ((tx&3) == 3)) >> 1;
1044
    uvmy = (ty + ((ty&3) == 3)) >> 1;
1045

    
1046
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1047
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1048

    
1049
    uvsrc_x = clip(uvsrc_x,  -8, s->mb_width  *  8);
1050
    uvsrc_y = clip(uvsrc_y,  -8, s->mb_height *  8);
1051
    srcU = s->last_picture.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
1052
    srcV = s->last_picture.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
1053
    if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
1054
       || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
1055
       || (unsigned)uvsrc_y > (s->v_edge_pos >> 1) - 9){
1056
        ff_emulated_edge_mc(s->edge_emu_buffer     , srcU, s->uvlinesize, 8+1, 8+1,
1057
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
1058
        ff_emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize, 8+1, 8+1,
1059
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
1060
        srcU = s->edge_emu_buffer;
1061
        srcV = s->edge_emu_buffer + 16;
1062

    
1063
        /* if we deal with range reduction we need to scale source blocks */
1064
        if(v->rangeredfrm) {
1065
            int i, j;
1066
            uint8_t *src, *src2;
1067

    
1068
            src = srcU; src2 = srcV;
1069
            for(j = 0; j < 9; j++) {
1070
                for(i = 0; i < 9; i++) {
1071
                    src[i] = ((src[i] - 128) >> 1) + 128;
1072
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
1073
                }
1074
                src += s->uvlinesize;
1075
                src2 += s->uvlinesize;
1076
            }
1077
        }
1078
        /* if we deal with intensity compensation we need to scale source blocks */
1079
        if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1080
            int i, j;
1081
            uint8_t *src, *src2;
1082

    
1083
            src = srcU; src2 = srcV;
1084
            for(j = 0; j < 9; j++) {
1085
                for(i = 0; i < 9; i++) {
1086
                    src[i] = v->lutuv[src[i]];
1087
                    src2[i] = v->lutuv[src2[i]];
1088
                }
1089
                src += s->uvlinesize;
1090
                src2 += s->uvlinesize;
1091
            }
1092
        }
1093
    }
1094

    
1095
    if(v->fastuvmc) {
1096
        uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
1097
        uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
1098
    }
1099

    
1100
    /* Chroma MC always uses qpel bilinear */
1101
    uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
1102
    uvmx = (uvmx&3)<<1;
1103
    uvmy = (uvmy&3)<<1;
1104
    if(!v->rnd){
1105
        dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
1106
        dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
1107
    }else{
1108
        dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
1109
        dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
1110
    }
1111
}
1112

    
1113
static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb);
1114

    
1115
/**
1116
 * Decode Simple/Main Profiles sequence header
1117
 * @see Figure 7-8, p16-17
1118
 * @param avctx Codec context
1119
 * @param gb GetBit context initialized from Codec context extra_data
1120
 * @return Status
1121
 */
1122
static int decode_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
1123
{
1124
    VC1Context *v = avctx->priv_data;
1125

    
1126
    av_log(avctx, AV_LOG_INFO, "Header: %0X\n", show_bits(gb, 32));
1127
    v->profile = get_bits(gb, 2);
1128
    if (v->profile == 2)
1129
    {
1130
        av_log(avctx, AV_LOG_ERROR, "Profile value 2 is forbidden (and WMV3 Complex Profile is unsupported)\n");
1131
        return -1;
1132
    }
1133

    
1134
    if (v->profile == PROFILE_ADVANCED)
1135
    {
1136
        return decode_sequence_header_adv(v, gb);
1137
    }
1138
    else
1139
    {
1140
        v->res_sm = get_bits(gb, 2); //reserved
1141
        if (v->res_sm)
1142
        {
1143
            av_log(avctx, AV_LOG_ERROR,
1144
                   "Reserved RES_SM=%i is forbidden\n", v->res_sm);
1145
            return -1;
1146
        }
1147
    }
1148

    
1149
    // (fps-2)/4 (->30)
1150
    v->frmrtq_postproc = get_bits(gb, 3); //common
1151
    // (bitrate-32kbps)/64kbps
1152
    v->bitrtq_postproc = get_bits(gb, 5); //common
1153
    v->s.loop_filter = get_bits(gb, 1); //common
1154
    if(v->s.loop_filter == 1 && v->profile == PROFILE_SIMPLE)
1155
    {
1156
        av_log(avctx, AV_LOG_ERROR,
1157
               "LOOPFILTER shell not be enabled in simple profile\n");
1158
    }
1159

    
1160
    if (v->profile < PROFILE_ADVANCED)
1161
    {
1162
        v->res_x8 = get_bits(gb, 1); //reserved
1163
        if (v->res_x8)
1164
        {
1165
            av_log(avctx, AV_LOG_ERROR,
1166
                   "1 for reserved RES_X8 is forbidden\n");
1167
            //return -1;
1168
        }
1169
        v->multires = get_bits(gb, 1);
1170
        v->res_fasttx = get_bits(gb, 1);
1171
        if (!v->res_fasttx)
1172
        {
1173
            av_log(avctx, AV_LOG_ERROR,
1174
                   "0 for reserved RES_FASTTX is forbidden\n");
1175
            //return -1;
1176
        }
1177
    }
1178

    
1179
    v->fastuvmc =  get_bits(gb, 1); //common
1180
    if (!v->profile && !v->fastuvmc)
1181
    {
1182
        av_log(avctx, AV_LOG_ERROR,
1183
               "FASTUVMC unavailable in Simple Profile\n");
1184
        return -1;
1185
    }
1186
    v->extended_mv =  get_bits(gb, 1); //common
1187
    if (!v->profile && v->extended_mv)
1188
    {
1189
        av_log(avctx, AV_LOG_ERROR,
1190
               "Extended MVs unavailable in Simple Profile\n");
1191
        return -1;
1192
    }
1193
    v->dquant =  get_bits(gb, 2); //common
1194
    v->vstransform =  get_bits(gb, 1); //common
1195

    
1196
    if (v->profile < PROFILE_ADVANCED)
1197
    {
1198
        v->res_transtab = get_bits(gb, 1);
1199
        if (v->res_transtab)
1200
        {
1201
            av_log(avctx, AV_LOG_ERROR,
1202
                   "1 for reserved RES_TRANSTAB is forbidden\n");
1203
            return -1;
1204
        }
1205
    }
1206

    
1207
    v->overlap = get_bits(gb, 1); //common
1208

    
1209
    if (v->profile < PROFILE_ADVANCED)
1210
    {
1211
        v->s.resync_marker = get_bits(gb, 1);
1212
        v->rangered = get_bits(gb, 1);
1213
        if (v->rangered && v->profile == PROFILE_SIMPLE)
1214
        {
1215
            av_log(avctx, AV_LOG_INFO,
1216
                   "RANGERED should be set to 0 in simple profile\n");
1217
        }
1218
    }
1219

    
1220
    v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
1221
    v->quantizer_mode = get_bits(gb, 2); //common
1222

    
1223
    if (v->profile < PROFILE_ADVANCED)
1224
    {
1225
        v->finterpflag = get_bits(gb, 1); //common
1226
        v->res_rtm_flag = get_bits(gb, 1); //reserved
1227
        if (!v->res_rtm_flag)
1228
        {
1229
//            av_log(avctx, AV_LOG_ERROR,
1230
//                   "0 for reserved RES_RTM_FLAG is forbidden\n");
1231
            av_log(avctx, AV_LOG_ERROR,
1232
                   "Old WMV3 version detected, only I-frames will be decoded\n");
1233
            //return -1;
1234
        }
1235
        av_log(avctx, AV_LOG_DEBUG,
1236
               "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
1237
               "LoopFilter=%i, MultiRes=%i, FastUVMC=%i, Extended MV=%i\n"
1238
               "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
1239
               "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
1240
               v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
1241
               v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
1242
               v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
1243
               v->dquant, v->quantizer_mode, avctx->max_b_frames
1244
               );
1245
        return 0;
1246
    }
1247
    return -1;
1248
}
1249

    
1250
static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb)
1251
{
1252
    v->res_rtm_flag = 1;
1253
    v->level = get_bits(gb, 3);
1254
    if(v->level >= 5)
1255
    {
1256
        av_log(v->s.avctx, AV_LOG_ERROR, "Reserved LEVEL %i\n",v->level);
1257
    }
1258
    v->chromaformat = get_bits(gb, 2);
1259
    if (v->chromaformat != 1)
1260
    {
1261
        av_log(v->s.avctx, AV_LOG_ERROR,
1262
               "Only 4:2:0 chroma format supported\n");
1263
        return -1;
1264
    }
1265

    
1266
    // (fps-2)/4 (->30)
1267
    v->frmrtq_postproc = get_bits(gb, 3); //common
1268
    // (bitrate-32kbps)/64kbps
1269
    v->bitrtq_postproc = get_bits(gb, 5); //common
1270
    v->postprocflag = get_bits(gb, 1); //common
1271

    
1272
    v->s.avctx->coded_width = (get_bits(gb, 12) + 1) << 1;
1273
    v->s.avctx->coded_height = (get_bits(gb, 12) + 1) << 1;
1274
    v->broadcast = get_bits1(gb);
1275
    v->interlace = get_bits1(gb);
1276
    v->tfcntrflag = get_bits1(gb);
1277
    v->finterpflag = get_bits1(gb);
1278
    get_bits1(gb); // reserved
1279
    v->psf = get_bits1(gb);
1280
    if(v->psf) { //PsF, 6.1.13
1281
        av_log(v->s.avctx, AV_LOG_ERROR, "Progressive Segmented Frame mode: not supported (yet)\n");
1282
        return -1;
1283
    }
1284
    if(get_bits1(gb)) { //Display Info - decoding is not affected by it
1285
        int w, h, ar = 0;
1286
        av_log(v->s.avctx, AV_LOG_INFO, "Display extended info:\n");
1287
        w = get_bits(gb, 14);
1288
        h = get_bits(gb, 14);
1289
        av_log(v->s.avctx, AV_LOG_INFO, "Display dimensions: %ix%i\n", w, h);
1290
        //TODO: store aspect ratio in AVCodecContext
1291
        if(get_bits1(gb))
1292
            ar = get_bits(gb, 4);
1293
        if(ar == 15) {
1294
            w = get_bits(gb, 8);
1295
            h = get_bits(gb, 8);
1296
        }
1297

    
1298
        if(get_bits1(gb)){ //framerate stuff
1299
            if(get_bits1(gb)) {
1300
                get_bits(gb, 16);
1301
            } else {
1302
                get_bits(gb, 8);
1303
                get_bits(gb, 4);
1304
            }
1305
        }
1306

    
1307
        if(get_bits1(gb)){
1308
            v->color_prim = get_bits(gb, 8);
1309
            v->transfer_char = get_bits(gb, 8);
1310
            v->matrix_coef = get_bits(gb, 8);
1311
        }
1312
    }
1313

    
1314
    v->hrd_param_flag = get_bits1(gb);
1315
    if(v->hrd_param_flag) {
1316
        int i;
1317
        v->hrd_num_leaky_buckets = get_bits(gb, 5);
1318
        get_bits(gb, 4); //bitrate exponent
1319
        get_bits(gb, 4); //buffer size exponent
1320
        for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
1321
            get_bits(gb, 16); //hrd_rate[n]
1322
            get_bits(gb, 16); //hrd_buffer[n]
1323
        }
1324
    }
1325
    return 0;
1326
}
1327

    
1328
static int decode_entry_point(AVCodecContext *avctx, GetBitContext *gb)
1329
{
1330
    VC1Context *v = avctx->priv_data;
1331
    int i;
1332

    
1333
    av_log(avctx, AV_LOG_DEBUG, "Entry point: %08X\n", show_bits_long(gb, 32));
1334
    get_bits1(gb); // broken link
1335
    get_bits1(gb); // closed entry
1336
    v->panscanflag = get_bits1(gb);
1337
    get_bits1(gb); // refdist flag
1338
    v->s.loop_filter = get_bits1(gb);
1339
    v->fastuvmc = get_bits1(gb);
1340
    v->extended_mv = get_bits1(gb);
1341
    v->dquant = get_bits(gb, 2);
1342
    v->vstransform = get_bits1(gb);
1343
    v->overlap = get_bits1(gb);
1344
    v->quantizer_mode = get_bits(gb, 2);
1345

    
1346
    if(v->hrd_param_flag){
1347
        for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
1348
            get_bits(gb, 8); //hrd_full[n]
1349
        }
1350
    }
1351

    
1352
    if(get_bits1(gb)){
1353
        avctx->coded_width = (get_bits(gb, 12)+1)<<1;
1354
        avctx->coded_height = (get_bits(gb, 12)+1)<<1;
1355
    }
1356
    if(v->extended_mv)
1357
        v->extended_dmv = get_bits1(gb);
1358
    if(get_bits1(gb)) {
1359
        av_log(avctx, AV_LOG_ERROR, "Luma scaling is not supported, expect wrong picture\n");
1360
        skip_bits(gb, 3); // Y range, ignored for now
1361
    }
1362
    if(get_bits1(gb)) {
1363
        av_log(avctx, AV_LOG_ERROR, "Chroma scaling is not supported, expect wrong picture\n");
1364
        skip_bits(gb, 3); // UV range, ignored for now
1365
    }
1366

    
1367
    return 0;
1368
}
1369

    
1370
static int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
1371
{
1372
    int pqindex, lowquant, status;
1373

    
1374
    if(v->finterpflag) v->interpfrm = get_bits(gb, 1);
1375
    skip_bits(gb, 2); //framecnt unused
1376
    v->rangeredfrm = 0;
1377
    if (v->rangered) v->rangeredfrm = get_bits(gb, 1);
1378
    v->s.pict_type = get_bits(gb, 1);
1379
    if (v->s.avctx->max_b_frames) {
1380
        if (!v->s.pict_type) {
1381
            if (get_bits(gb, 1)) v->s.pict_type = I_TYPE;
1382
            else v->s.pict_type = B_TYPE;
1383
        } else v->s.pict_type = P_TYPE;
1384
    } else v->s.pict_type = v->s.pict_type ? P_TYPE : I_TYPE;
1385

    
1386
    if(v->s.pict_type == I_TYPE)
1387
        get_bits(gb, 7); // skip buffer fullness
1388
    if(v->s.pict_type == B_TYPE) {
1389
        v->bfraction = get_vlc2(gb, vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
1390
        v->bfraction = vc1_bfraction_lut[v->bfraction];
1391
        if(v->bfraction == -1) {
1392
            v->s.pict_type = BI_TYPE;
1393
        }
1394
    }
1395

    
1396
    /* calculate RND */
1397
    if(v->s.pict_type == I_TYPE)
1398
        v->rnd = 1;
1399
    if(v->s.pict_type == P_TYPE)
1400
        v->rnd ^= 1;
1401

    
1402
    /* Quantizer stuff */
1403
    pqindex = get_bits(gb, 5);
1404
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1405
        v->pq = pquant_table[0][pqindex];
1406
    else
1407
        v->pq = pquant_table[1][pqindex];
1408

    
1409
    v->pquantizer = 1;
1410
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1411
        v->pquantizer = pqindex < 9;
1412
    if (v->quantizer_mode == QUANT_NON_UNIFORM)
1413
        v->pquantizer = 0;
1414
    v->pqindex = pqindex;
1415
    if (pqindex < 9) v->halfpq = get_bits(gb, 1);
1416
    else v->halfpq = 0;
1417
    if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
1418
        v->pquantizer = get_bits(gb, 1);
1419
    v->dquantfrm = 0;
1420
    if (v->extended_mv == 1) v->mvrange = get_prefix(gb, 0, 3);
1421
    v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1422
    v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
1423
    v->range_x = 1 << (v->k_x - 1);
1424
    v->range_y = 1 << (v->k_y - 1);
1425
    if (v->profile == PROFILE_ADVANCED)
1426
    {
1427
        if (v->postprocflag) v->postproc = get_bits(gb, 1);
1428
    }
1429
    else
1430
        if (v->multires && v->s.pict_type != B_TYPE) v->respic = get_bits(gb, 2);
1431

    
1432
//av_log(v->s.avctx, AV_LOG_INFO, "%c Frame: QP=[%i]%i (+%i/2) %i\n",
1433
//        (v->s.pict_type == P_TYPE) ? 'P' : ((v->s.pict_type == I_TYPE) ? 'I' : 'B'), pqindex, v->pq, v->halfpq, v->rangeredfrm);
1434

    
1435
    //TODO: complete parsing for P/B/BI frames
1436
    switch(v->s.pict_type) {
1437
    case P_TYPE:
1438
        if (v->pq < 5) v->tt_index = 0;
1439
        else if(v->pq < 13) v->tt_index = 1;
1440
        else v->tt_index = 2;
1441

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

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

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

    
1500
        if (v->dquant)
1501
        {
1502
            av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1503
            vop_dquant_decoding(v);
1504
        }
1505

    
1506
        v->ttfrm = 0; //FIXME Is that so ?
1507
        if (v->vstransform)
1508
        {
1509
            v->ttmbf = get_bits(gb, 1);
1510
            if (v->ttmbf)
1511
            {
1512
                v->ttfrm = ttfrm_to_tt[get_bits(gb, 2)];
1513
            }
1514
        } else {
1515
            v->ttmbf = 1;
1516
            v->ttfrm = TT_8X8;
1517
        }
1518
        break;
1519
    case B_TYPE:
1520
        if (v->pq < 5) v->tt_index = 0;
1521
        else if(v->pq < 13) v->tt_index = 1;
1522
        else v->tt_index = 2;
1523

    
1524
        lowquant = (v->pq > 12) ? 0 : 1;
1525
        v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
1526
        v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
1527
        v->s.mspel = v->s.quarter_sample;
1528

    
1529
        status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
1530
        if (status < 0) return -1;
1531
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
1532
               "Imode: %i, Invert: %i\n", status>>1, status&1);
1533
        status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1534
        if (status < 0) return -1;
1535
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1536
               "Imode: %i, Invert: %i\n", status>>1, status&1);
1537

    
1538
        v->s.mv_table_index = get_bits(gb, 2);
1539
        v->cbpcy_vlc = &vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1540

    
1541
        if (v->dquant)
1542
        {
1543
            av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1544
            vop_dquant_decoding(v);
1545
        }
1546

    
1547
        v->ttfrm = 0;
1548
        if (v->vstransform)
1549
        {
1550
            v->ttmbf = get_bits(gb, 1);
1551
            if (v->ttmbf)
1552
            {
1553
                v->ttfrm = ttfrm_to_tt[get_bits(gb, 2)];
1554
            }
1555
        } else {
1556
            v->ttmbf = 1;
1557
            v->ttfrm = TT_8X8;
1558
        }
1559
        break;
1560
    }
1561

    
1562
    /* AC Syntax */
1563
    v->c_ac_table_index = decode012(gb);
1564
    if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
1565
    {
1566
        v->y_ac_table_index = decode012(gb);
1567
    }
1568
    /* DC Syntax */
1569
    v->s.dc_table_index = get_bits(gb, 1);
1570

    
1571
    return 0;
1572
}
1573

    
1574
static int vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb)
1575
{
1576
    int fcm;
1577
    int pqindex, lowquant;
1578
    int status;
1579

    
1580
    v->p_frame_skipped = 0;
1581

    
1582
    if(v->interlace)
1583
        fcm = decode012(gb);
1584
    switch(get_prefix(gb, 0, 4)) {
1585
    case 0:
1586
        v->s.pict_type = P_TYPE;
1587
        break;
1588
    case 1:
1589
        v->s.pict_type = B_TYPE;
1590
        return -1;
1591
//      break;
1592
    case 2:
1593
        v->s.pict_type = I_TYPE;
1594
        break;
1595
    case 3:
1596
        v->s.pict_type = BI_TYPE;
1597
        return -1;
1598
//      break;
1599
    case 4:
1600
        v->s.pict_type = P_TYPE; // skipped pic
1601
        v->p_frame_skipped = 1;
1602
        return 0;
1603
    }
1604
    if(v->tfcntrflag)
1605
        get_bits(gb, 8);
1606
    if(v->broadcast) {
1607
        if(!v->interlace || v->panscanflag) {
1608
            get_bits(gb, 2);
1609
        } else {
1610
            get_bits1(gb);
1611
            get_bits1(gb);
1612
        }
1613
    }
1614
    if(v->panscanflag) {
1615
        //...
1616
    }
1617
    v->rnd = get_bits1(gb);
1618
    if(v->interlace)
1619
        v->uvsamp = get_bits1(gb);
1620
    if(v->finterpflag) v->interpfrm = get_bits(gb, 1);
1621
    pqindex = get_bits(gb, 5);
1622
    v->pqindex = pqindex;
1623
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1624
        v->pq = pquant_table[0][pqindex];
1625
    else
1626
        v->pq = pquant_table[1][pqindex];
1627

    
1628
    v->pquantizer = 1;
1629
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1630
        v->pquantizer = pqindex < 9;
1631
    if (v->quantizer_mode == QUANT_NON_UNIFORM)
1632
        v->pquantizer = 0;
1633
    v->pqindex = pqindex;
1634
    if (pqindex < 9) v->halfpq = get_bits(gb, 1);
1635
    else v->halfpq = 0;
1636
    if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
1637
        v->pquantizer = get_bits(gb, 1);
1638

    
1639
    switch(v->s.pict_type) {
1640
    case I_TYPE:
1641
        status = bitplane_decoding(v->acpred_plane, &v->acpred_is_raw, v);
1642
        if (status < 0) return -1;
1643
        av_log(v->s.avctx, AV_LOG_DEBUG, "ACPRED plane encoding: "
1644
                "Imode: %i, Invert: %i\n", status>>1, status&1);
1645
        v->condover = CONDOVER_NONE;
1646
        if(v->overlap && v->pq <= 8) {
1647
            v->condover = decode012(gb);
1648
            if(v->condover == CONDOVER_SELECT) {
1649
                status = bitplane_decoding(v->over_flags_plane, &v->overflg_is_raw, v);
1650
                if (status < 0) return -1;
1651
                av_log(v->s.avctx, AV_LOG_DEBUG, "CONDOVER plane encoding: "
1652
                        "Imode: %i, Invert: %i\n", status>>1, status&1);
1653
            }
1654
        }
1655
        break;
1656
    case P_TYPE:
1657
        if(v->postprocflag)
1658
            v->postproc = get_bits1(gb);
1659
        if (v->extended_mv) v->mvrange = get_prefix(gb, 0, 3);
1660
        else v->mvrange = 0;
1661
        v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1662
        v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
1663
        v->range_x = 1 << (v->k_x - 1);
1664
        v->range_y = 1 << (v->k_y - 1);
1665

    
1666
        if (v->pq < 5) v->tt_index = 0;
1667
        else if(v->pq < 13) v->tt_index = 1;
1668
        else v->tt_index = 2;
1669

    
1670
        lowquant = (v->pq > 12) ? 0 : 1;
1671
        v->mv_mode = mv_pmode_table[lowquant][get_prefix(gb, 1, 4)];
1672
        if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
1673
        {
1674
            int scale, shift, i;
1675
            v->mv_mode2 = mv_pmode_table2[lowquant][get_prefix(gb, 1, 3)];
1676
            v->lumscale = get_bits(gb, 6);
1677
            v->lumshift = get_bits(gb, 6);
1678
            /* fill lookup tables for intensity compensation */
1679
            if(!v->lumscale) {
1680
                scale = -64;
1681
                shift = (255 - v->lumshift * 2) << 6;
1682
                if(v->lumshift > 31)
1683
                    shift += 128 << 6;
1684
            } else {
1685
                scale = v->lumscale + 32;
1686
                if(v->lumshift > 31)
1687
                    shift = (v->lumshift - 64) << 6;
1688
                else
1689
                    shift = v->lumshift << 6;
1690
            }
1691
            for(i = 0; i < 256; i++) {
1692
                v->luty[i] = clip_uint8((scale * i + shift + 32) >> 6);
1693
                v->lutuv[i] = clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
1694
            }
1695
        }
1696
        if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
1697
            v->s.quarter_sample = 0;
1698
        else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1699
            if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
1700
                v->s.quarter_sample = 0;
1701
            else
1702
                v->s.quarter_sample = 1;
1703
        } else
1704
            v->s.quarter_sample = 1;
1705
        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));
1706

    
1707
        if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
1708
                 v->mv_mode2 == MV_PMODE_MIXED_MV)
1709
                || v->mv_mode == MV_PMODE_MIXED_MV)
1710
        {
1711
            status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
1712
            if (status < 0) return -1;
1713
            av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
1714
                   "Imode: %i, Invert: %i\n", status>>1, status&1);
1715
        } else {
1716
            v->mv_type_is_raw = 0;
1717
            memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
1718
        }
1719
        status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1720
        if (status < 0) return -1;
1721
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1722
               "Imode: %i, Invert: %i\n", status>>1, status&1);
1723

    
1724
        /* Hopefully this is correct for P frames */
1725
        v->s.mv_table_index = get_bits(gb, 2); //but using vc1_ tables
1726
        v->cbpcy_vlc = &vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1727
        if (v->dquant)
1728
        {
1729
            av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1730
            vop_dquant_decoding(v);
1731
        }
1732

    
1733
        v->ttfrm = 0; //FIXME Is that so ?
1734
        if (v->vstransform)
1735
        {
1736
            v->ttmbf = get_bits(gb, 1);
1737
            if (v->ttmbf)
1738
            {
1739
                v->ttfrm = ttfrm_to_tt[get_bits(gb, 2)];
1740
            }
1741
        } else {
1742
            v->ttmbf = 1;
1743
            v->ttfrm = TT_8X8;
1744
        }
1745
        break;
1746
    }
1747

    
1748
    /* AC Syntax */
1749
    v->c_ac_table_index = decode012(gb);
1750
    if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
1751
    {
1752
        v->y_ac_table_index = decode012(gb);
1753
    }
1754
    /* DC Syntax */
1755
    v->s.dc_table_index = get_bits(gb, 1);
1756
    if (v->s.pict_type == I_TYPE && v->dquant) {
1757
        av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1758
        vop_dquant_decoding(v);
1759
    }
1760

    
1761
    return 0;
1762
}
1763

    
1764
/***********************************************************************/
1765
/**
1766
 * @defgroup block VC-1 Block-level functions
1767
 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1768
 * @todo TODO: Integrate to MpegEncContext facilities
1769
 * @{
1770
 */
1771

    
1772
/**
1773
 * @def GET_MQUANT
1774
 * @brief Get macroblock-level quantizer scale
1775
 * @warning XXX: qdiff to the frame quant, not previous quant ?
1776
 * @fixme XXX: Don't know how to initialize mquant otherwise in last case
1777
 */
1778
#define GET_MQUANT()                                           \
1779
  if (v->dquantfrm)                                            \
1780
  {                                                            \
1781
    int edges = 0;                                             \
1782
    if (v->dqprofile == DQPROFILE_ALL_MBS)                     \
1783
    {                                                          \
1784
      if (v->dqbilevel)                                        \
1785
      {                                                        \
1786
        mquant = (get_bits(gb, 1)) ? v->altpq : v->pq;         \
1787
      }                                                        \
1788
      else                                                     \
1789
      {                                                        \
1790
        mqdiff = get_bits(gb, 3);                              \
1791
        if (mqdiff != 7) mquant = v->pq + mqdiff;              \
1792
        else mquant = get_bits(gb, 5);                         \
1793
      }                                                        \
1794
    }                                                          \
1795
    if(v->dqprofile == DQPROFILE_SINGLE_EDGE)                  \
1796
        edges = 1 << v->dqsbedge;                              \
1797
    else if(v->dqprofile == DQPROFILE_DOUBLE_EDGES)            \
1798
        edges = (3 << v->dqsbedge) % 15;                       \
1799
    else if(v->dqprofile == DQPROFILE_FOUR_EDGES)              \
1800
        edges = 15;                                            \
1801
    if((edges&1) && !s->mb_x)                                  \
1802
        mquant = v->altpq;                                     \
1803
    if((edges&2) && s->first_slice_line)                       \
1804
        mquant = v->altpq;                                     \
1805
    if((edges&4) && s->mb_x == (s->mb_width - 1))              \
1806
        mquant = v->altpq;                                     \
1807
    if((edges&8) && s->mb_y == (s->mb_height - 1))             \
1808
        mquant = v->altpq;                                     \
1809
  }
1810

    
1811
/**
1812
 * @def GET_MVDATA(_dmv_x, _dmv_y)
1813
 * @brief Get MV differentials
1814
 * @see MVDATA decoding from 8.3.5.2, p(1)20
1815
 * @param _dmv_x Horizontal differential for decoded MV
1816
 * @param _dmv_y Vertical differential for decoded MV
1817
 * @todo TODO: Use MpegEncContext arrays to store them
1818
 */
1819
#define GET_MVDATA(_dmv_x, _dmv_y)                                  \
1820
  index = 1 + get_vlc2(gb, vc1_mv_diff_vlc[s->mv_table_index].table,\
1821
                       VC1_MV_DIFF_VLC_BITS, 2);                    \
1822
  if (index > 36)                                                   \
1823
  {                                                                 \
1824
    mb_has_coeffs = 1;                                              \
1825
    index -= 37;                                                    \
1826
  }                                                                 \
1827
  else mb_has_coeffs = 0;                                           \
1828
  s->mb_intra = 0;                                                  \
1829
  if (!index) { _dmv_x = _dmv_y = 0; }                              \
1830
  else if (index == 35)                                             \
1831
  {                                                                 \
1832
    _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample);          \
1833
    _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample);          \
1834
  }                                                                 \
1835
  else if (index == 36)                                             \
1836
  {                                                                 \
1837
    _dmv_x = 0;                                                     \
1838
    _dmv_y = 0;                                                     \
1839
    s->mb_intra = 1;                                                \
1840
  }                                                                 \
1841
  else                                                              \
1842
  {                                                                 \
1843
    index1 = index%6;                                               \
1844
    if (!s->quarter_sample && index1 == 5) val = 1;                 \
1845
    else                                   val = 0;                 \
1846
    if(size_table[index1] - val > 0)                                \
1847
        val = get_bits(gb, size_table[index1] - val);               \
1848
    else                                   val = 0;                 \
1849
    sign = 0 - (val&1);                                             \
1850
    _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign;     \
1851
                                                                    \
1852
    index1 = index/6;                                               \
1853
    if (!s->quarter_sample && index1 == 5) val = 1;                 \
1854
    else                                   val = 0;                 \
1855
    if(size_table[index1] - val > 0)                                \
1856
        val = get_bits(gb, size_table[index1] - val);               \
1857
    else                                   val = 0;                 \
1858
    sign = 0 - (val&1);                                             \
1859
    _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign;     \
1860
  }
1861

    
1862
/** Predict and set motion vector
1863
 */
1864
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)
1865
{
1866
    int xy, wrap, off = 0;
1867
    int16_t *A, *B, *C;
1868
    int px, py;
1869
    int sum;
1870

    
1871
    /* scale MV difference to be quad-pel */
1872
    dmv_x <<= 1 - s->quarter_sample;
1873
    dmv_y <<= 1 - s->quarter_sample;
1874

    
1875
    wrap = s->b8_stride;
1876
    xy = s->block_index[n];
1877

    
1878
    if(s->mb_intra){
1879
        s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
1880
        s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
1881
        if(mv1) { /* duplicate motion data for 1-MV block */
1882
            s->current_picture.motion_val[0][xy + 1][0] = 0;
1883
            s->current_picture.motion_val[0][xy + 1][1] = 0;
1884
            s->current_picture.motion_val[0][xy + wrap][0] = 0;
1885
            s->current_picture.motion_val[0][xy + wrap][1] = 0;
1886
            s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
1887
            s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
1888
        }
1889
        return;
1890
    }
1891

    
1892
    C = s->current_picture.motion_val[0][xy - 1];
1893
    A = s->current_picture.motion_val[0][xy - wrap];
1894
    if(mv1)
1895
        off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1896
    else {
1897
        //in 4-MV mode different blocks have different B predictor position
1898
        switch(n){
1899
        case 0:
1900
            off = (s->mb_x > 0) ? -1 : 1;
1901
            break;
1902
        case 1:
1903
            off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1904
            break;
1905
        case 2:
1906
            off = 1;
1907
            break;
1908
        case 3:
1909
            off = -1;
1910
        }
1911
    }
1912
    B = s->current_picture.motion_val[0][xy - wrap + off];
1913

    
1914
    if(!s->first_slice_line || (n==2 || n==3)) { // predictor A is not out of bounds
1915
        if(s->mb_width == 1) {
1916
            px = A[0];
1917
            py = A[1];
1918
        } else {
1919
            px = mid_pred(A[0], B[0], C[0]);
1920
            py = mid_pred(A[1], B[1], C[1]);
1921
        }
1922
    } else if(s->mb_x || (n==1 || n==3)) { // predictor C is not out of bounds
1923
        px = C[0];
1924
        py = C[1];
1925
    } else {
1926
        px = py = 0;
1927
    }
1928
    /* Pullback MV as specified in 8.3.5.3.4 */
1929
    {
1930
        int qx, qy, X, Y;
1931
        qx = (s->mb_x << 6) + ((n==1 || n==3) ? 32 : 0);
1932
        qy = (s->mb_y << 6) + ((n==2 || n==3) ? 32 : 0);
1933
        X = (s->mb_width << 6) - 4;
1934
        Y = (s->mb_height << 6) - 4;
1935
        if(mv1) {
1936
            if(qx + px < -60) px = -60 - qx;
1937
            if(qy + py < -60) py = -60 - qy;
1938
        } else {
1939
            if(qx + px < -28) px = -28 - qx;
1940
            if(qy + py < -28) py = -28 - qy;
1941
        }
1942
        if(qx + px > X) px = X - qx;
1943
        if(qy + py > Y) py = Y - qy;
1944
    }
1945
    /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
1946
    if((!s->first_slice_line || (n==2 || n==3)) && (s->mb_x || (n==1 || n==3))) {
1947
        if(is_intra[xy - wrap])
1948
            sum = ABS(px) + ABS(py);
1949
        else
1950
            sum = ABS(px - A[0]) + ABS(py - A[1]);
1951
        if(sum > 32) {
1952
            if(get_bits1(&s->gb)) {
1953
                px = A[0];
1954
                py = A[1];
1955
            } else {
1956
                px = C[0];
1957
                py = C[1];
1958
            }
1959
        } else {
1960
            if(is_intra[xy - 1])
1961
                sum = ABS(px) + ABS(py);
1962
            else
1963
                sum = ABS(px - C[0]) + ABS(py - C[1]);
1964
            if(sum > 32) {
1965
                if(get_bits1(&s->gb)) {
1966
                    px = A[0];
1967
                    py = A[1];
1968
                } else {
1969
                    px = C[0];
1970
                    py = C[1];
1971
                }
1972
            }
1973
        }
1974
    }
1975
    /* store MV using signed modulus of MV range defined in 4.11 */
1976
    s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1977
    s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
1978
    if(mv1) { /* duplicate motion data for 1-MV block */
1979
        s->current_picture.motion_val[0][xy + 1][0] = s->current_picture.motion_val[0][xy][0];
1980
        s->current_picture.motion_val[0][xy + 1][1] = s->current_picture.motion_val[0][xy][1];
1981
        s->current_picture.motion_val[0][xy + wrap][0] = s->current_picture.motion_val[0][xy][0];
1982
        s->current_picture.motion_val[0][xy + wrap][1] = s->current_picture.motion_val[0][xy][1];
1983
        s->current_picture.motion_val[0][xy + wrap + 1][0] = s->current_picture.motion_val[0][xy][0];
1984
        s->current_picture.motion_val[0][xy + wrap + 1][1] = s->current_picture.motion_val[0][xy][1];
1985
    }
1986
}
1987

    
1988
/** Motion compensation for direct or interpolated blocks in B-frames
1989
 */
1990
static void vc1_interp_mc(VC1Context *v)
1991
{
1992
    MpegEncContext *s = &v->s;
1993
    DSPContext *dsp = &v->s.dsp;
1994
    uint8_t *srcY, *srcU, *srcV;
1995
    int dxy, uvdxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1996

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

    
1999
    mx = s->mv[1][0][0];
2000
    my = s->mv[1][0][1];
2001
    uvmx = (mx + ((mx & 3) == 3)) >> 1;
2002
    uvmy = (my + ((my & 3) == 3)) >> 1;
2003
    srcY = s->next_picture.data[0];
2004
    srcU = s->next_picture.data[1];
2005
    srcV = s->next_picture.data[2];
2006

    
2007
    src_x = s->mb_x * 16 + (mx >> 2);
2008
    src_y = s->mb_y * 16 + (my >> 2);
2009
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
2010
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
2011

    
2012
    src_x   = clip(  src_x, -16, s->mb_width  * 16);
2013
    src_y   = clip(  src_y, -16, s->mb_height * 16);
2014
    uvsrc_x = clip(uvsrc_x,  -8, s->mb_width  *  8);
2015
    uvsrc_y = clip(uvsrc_y,  -8, s->mb_height *  8);
2016

    
2017
    srcY += src_y * s->linesize + src_x;
2018
    srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
2019
    srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
2020

    
2021
    /* for grayscale we should not try to read from unknown area */
2022
    if(s->flags & CODEC_FLAG_GRAY) {
2023
        srcU = s->edge_emu_buffer + 18 * s->linesize;
2024
        srcV = s->edge_emu_buffer + 18 * s->linesize;
2025
    }
2026

    
2027
    if(v->rangeredfrm
2028
       || (unsigned)src_x > s->h_edge_pos - (mx&3) - 16
2029
       || (unsigned)src_y > s->v_edge_pos - (my&3) - 16){
2030
        uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
2031

    
2032
        ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17, 17,
2033
                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
2034
        srcY = s->edge_emu_buffer;
2035
        ff_emulated_edge_mc(uvbuf     , srcU, s->uvlinesize, 8+1, 8+1,
2036
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
2037
        ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
2038
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
2039
        srcU = uvbuf;
2040
        srcV = uvbuf + 16;
2041
        /* if we deal with range reduction we need to scale source blocks */
2042
        if(v->rangeredfrm) {
2043
            int i, j;
2044
            uint8_t *src, *src2;
2045

    
2046
            src = srcY;
2047
            for(j = 0; j < 17; j++) {
2048
                for(i = 0; i < 17; i++) src[i] = ((src[i] - 128) >> 1) + 128;
2049
                src += s->linesize;
2050
            }
2051
            src = srcU; src2 = srcV;
2052
            for(j = 0; j < 9; j++) {
2053
                for(i = 0; i < 9; i++) {
2054
                    src[i] = ((src[i] - 128) >> 1) + 128;
2055
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
2056
                }
2057
                src += s->uvlinesize;
2058
                src2 += s->uvlinesize;
2059
            }
2060
        }
2061
    }
2062

    
2063
    if(v->fastuvmc) {
2064
        uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
2065
        uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
2066
    }
2067

    
2068
    mx >>= 1;
2069
    my >>= 1;
2070
    dxy = ((my & 1) << 1) | (mx & 1);
2071

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

    
2074
    if(s->flags & CODEC_FLAG_GRAY) return;
2075
    /* Chroma MC always uses qpel blilinear */
2076
    uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
2077
    dsp->avg_qpel_pixels_tab[1][uvdxy](s->dest[1], srcU, s->uvlinesize);
2078
    dsp->avg_qpel_pixels_tab[1][uvdxy](s->dest[2], srcV, s->uvlinesize);
2079
}
2080

    
2081
/** Reconstruct motion vector for B-frame and do motion compensation
2082
 */
2083
static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mode)
2084
{
2085
    MpegEncContext *s = &v->s;
2086
    int mx[4], my[4], mv_x, mv_y;
2087
    int i;
2088

    
2089
    /* scale MV difference to be quad-pel */
2090
    dmv_x[0] <<= 1 - s->quarter_sample;
2091
    dmv_y[0] <<= 1 - s->quarter_sample;
2092
    dmv_x[1] <<= 1 - s->quarter_sample;
2093
    dmv_y[1] <<= 1 - s->quarter_sample;
2094

    
2095
    if(direct) {
2096
        for(i = 0; i < 4; i++) {
2097
            mx[i] = s->last_picture.motion_val[0][s->block_index[i]][0];
2098
            my[i] = s->last_picture.motion_val[0][s->block_index[i]][1];
2099
        }
2100
        mv_x = median4(mx[0], mx[1], mx[2], mx[3]);
2101
        mv_y = median4(my[0], my[1], my[2], my[3]);
2102
        s->mv[0][0][0] = (mv_x * v->bfraction + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2103
        s->mv[0][0][1] = (mv_y * v->bfraction + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2104
        vc1_mc_1mv(v, 0);
2105

    
2106
        for(i = 0; i < 4; i++) {
2107
            mx[i] = s->next_picture.motion_val[0][s->block_index[i]][0];
2108
            my[i] = s->next_picture.motion_val[0][s->block_index[i]][1];
2109
        }
2110
        mv_x = median4(mx[0], mx[1], mx[2], mx[3]);
2111
        mv_y = median4(my[0], my[1], my[2], my[3]);
2112
        s->mv[1][0][0] = (mv_x * (B_FRACTION_DEN - v->bfraction) + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2113
        s->mv[1][0][1] = (mv_y * (B_FRACTION_DEN - v->bfraction) + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2114
        vc1_interp_mc(v);
2115
        return;
2116
    }
2117
    if(mode == BMV_TYPE_INTERPOLATED) {
2118
        s->mv[0][0][0] = dmv_x[0];
2119
        s->mv[0][0][1] = dmv_y[0];
2120
        vc1_mc_1mv(v, 0);
2121
        s->mv[1][0][0] = dmv_x[1];
2122
        s->mv[1][0][1] = dmv_y[1];
2123
        vc1_interp_mc(v);
2124
        return;
2125
    }
2126

    
2127
    if(mode == BMV_TYPE_BACKWARD) {
2128
        for(i = 0; i < 4; i++) {
2129
            mx[i] = s->last_picture.motion_val[0][s->block_index[i]][0];
2130
            my[i] = s->last_picture.motion_val[0][s->block_index[i]][1];
2131
        }
2132
    } else {
2133
        for(i = 0; i < 4; i++) {
2134
            mx[i] = s->next_picture.motion_val[0][s->block_index[i]][0];
2135
            my[i] = s->next_picture.motion_val[0][s->block_index[i]][1];
2136
        }
2137
    }
2138

    
2139
    /* XXX: not right but how to determine 4-MV intra/inter in another frame? */
2140
    mv_x = median4(mx[0], mx[1], mx[2], mx[3]);
2141
    mv_y = median4(my[0], my[1], my[2], my[3]);
2142
    s->mv[0][0][0] = mv_x;
2143
    s->mv[0][0][1] = mv_y;
2144

    
2145
    vc1_mc_1mv(v, (mode == BMV_TYPE_FORWARD));
2146
}
2147

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

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

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

    
2174
    /* B A
2175
     * C X
2176
     */
2177
    c = dc_val[ - 1];
2178
    b = dc_val[ - 1 - wrap];
2179
    a = dc_val[ - wrap];
2180

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

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

    
2202
    /* update predictor */
2203
    *dc_val_ptr = &dc_val[0];
2204
    return pred;
2205
}
2206

    
2207

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

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

    
2228
    wrap = s->block_wrap[n];
2229
    dc_val= s->dc_val[0] + s->block_index[n];
2230

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

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

    
2277
    /* update predictor */
2278
    *dc_val_ptr = &dc_val[0];
2279
    return pred;
2280
}
2281

    
2282

    
2283
/**
2284
 * @defgroup std_mb VC1 Macroblock-level functions in Simple/Main Profiles
2285
 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2286
 * @todo TODO: Integrate to MpegEncContext facilities
2287
 * @{
2288
 */
2289

    
2290
static inline int vc1_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
2291
{
2292
    int xy, wrap, pred, a, b, c;
2293

    
2294
    xy = s->block_index[n];
2295
    wrap = s->b8_stride;
2296

    
2297
    /* B C
2298
     * A X
2299
     */
2300
    a = s->coded_block[xy - 1       ];
2301
    b = s->coded_block[xy - 1 - wrap];
2302
    c = s->coded_block[xy     - wrap];
2303

    
2304
    if (b == c) {
2305
        pred = a;
2306
    } else {
2307
        pred = c;
2308
    }
2309

    
2310
    /* store value */
2311
    *coded_block_ptr = &s->coded_block[xy];
2312

    
2313
    return pred;
2314
}
2315

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

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

    
2377
    *last = lst;
2378
    *skip = run;
2379
    *value = level;
2380
}
2381

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

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

    
2428
    /* Prediction */
2429
    dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2430
    *dc_val = dcdiff;
2431

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

    
2445
    //AC Decoding
2446
    i = 1;
2447

    
2448
    {
2449
        int last = 0, skip, value;
2450
        const int8_t *zz_table;
2451
        int scale;
2452
        int k;
2453

    
2454
        scale = v->pq * 2 + v->halfpq;
2455

    
2456
        if(v->s.ac_pred) {
2457
            if(!dc_pred_dir)
2458
                zz_table = vc1_horizontal_zz;
2459
            else
2460
                zz_table = vc1_vertical_zz;
2461
        } else
2462
            zz_table = vc1_normal_zz;
2463

    
2464
        ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2465
        ac_val2 = ac_val;
2466
        if(dc_pred_dir) //left
2467
            ac_val -= 16;
2468
        else //top
2469
            ac_val -= 16 * s->block_wrap[n];
2470

    
2471
        while (!last) {
2472
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2473
            i += skip;
2474
            if(i > 63)
2475
                break;
2476
            block[zz_table[i++]] = value;
2477
        }
2478

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

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

    
2503
        if(s->ac_pred) i = 63;
2504
    }
2505

    
2506
not_coded:
2507
    if(!coded) {
2508
        int k, scale;
2509
        ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2510
        ac_val2 = ac_val;
2511

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

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

    
2544
    return 0;
2545
}
2546

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

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

    
2598
    /* Prediction */
2599
    dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
2600
    *dc_val = dcdiff;
2601

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

    
2612
    //AC Decoding
2613
    i = 1;
2614

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

    
2622
    scale = mquant * 2 + v->halfpq;
2623

    
2624
    if(dc_pred_dir) //left
2625
        ac_val -= 16;
2626
    else //top
2627
        ac_val -= 16 * s->block_wrap[n];
2628

    
2629
    q1 = s->current_picture.qscale_table[mb_pos];
2630
    if(dc_pred_dir && c_avail) q2 = s->current_picture.qscale_table[mb_pos - 1];
2631
    if(!dc_pred_dir && a_avail) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2632
    if(n && n<4) q2 = q1;
2633

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

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

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

    
2655
        /* apply AC prediction if needed */
2656
        if(use_pred) {
2657
            /* scale predictors if needed*/
2658
            if(q2 && q1!=q2) {
2659
                q1 = q1 * 2 - 1;
2660
                q2 = q2 * 2 - 1;
2661

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

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

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

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

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

    
2740
    return 0;
2741
}
2742

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

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

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

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

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

    
2806
    /* Store the quantized DC coeff, used for prediction */
2807

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

    
2817
    //AC Decoding
2818
    i = 1;
2819

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

    
2827
    scale = mquant * 2 + v->halfpq;
2828

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

    
2834
    q1 = s->current_picture.qscale_table[mb_pos];
2835
    if(dc_pred_dir && c_avail) q2 = s->current_picture.qscale_table[mb_pos - 1];
2836
    if(!dc_pred_dir && a_avail) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2837
    if(n && n<4) 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 - 1;
2859
                q2 = q2 * 2 - 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 - 1;
2902
                    q2 = q2 * 2 - 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 - 1;
2912
                    q2 = q2 * 2 - 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
 * @todo TODO: Extend to AP
3062
 * @fixme FIXME: DC value for inter blocks not set
3063
 */
3064
static int vc1_decode_p_mb(VC1Context *v)
3065
{
3066
    MpegEncContext *s = &v->s;
3067
    GetBitContext *gb = &s->gb;
3068
    int i, j;
3069
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3070
    int cbp; /* cbp decoding stuff */
3071
    int mqdiff, mquant; /* MB quantization */
3072
    int ttmb = v->ttfrm; /* MB Transform type */
3073
    int status;
3074

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

    
3085
    mquant = v->pq; /* Loosy initialization */
3086

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

    
3096
    s->dsp.clear_blocks(s->block[0]);
3097

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

    
3104
            s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
3105
            vc1_pred_mv(s, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
3106

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

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

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

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

    
3293
    /* Should never happen */
3294
    return -1;
3295
}
3296

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

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

    
3320
    mquant = v->pq; /* Loosy initialization */
3321
    s->mb_intra = 0;
3322

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

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

    
3340
    if (!direct) {
3341
        if (!skipped) {
3342
            GET_MVDATA(dmv_x[0], dmv_y[0]);
3343
            dmv_x[1] = dmv_x[0];
3344
            dmv_y[1] = dmv_y[0];
3345
        }
3346
        if(skipped || !s->mb_intra) {
3347
            bmvtype = decode012(gb);
3348
            switch(bmvtype) {
3349
            case 0:
3350
                bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
3351
                break;
3352
            case 1:
3353
                bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
3354
                break;
3355
            case 2:
3356
                bmvtype = BMV_TYPE_INTERPOLATED;
3357
            }
3358
        }
3359
    }
3360
    if (skipped) {
3361
        vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3362
        return;
3363
    }
3364
    if (direct) {
3365
        cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3366
        GET_MQUANT();
3367
        s->mb_intra = 0;
3368
        mb_has_coeffs = 0;
3369
        s->current_picture.qscale_table[mb_pos] = mquant;
3370
        if(!v->ttmbf)
3371
            ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3372
        vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3373
    } else {
3374
        if(!mb_has_coeffs && !s->mb_intra) {
3375
            /* no coded blocks - effectively skipped */
3376
            vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3377
            return;
3378
        }
3379
        if(s->mb_intra && !mb_has_coeffs) {
3380
            GET_MQUANT();
3381
            s->current_picture.qscale_table[mb_pos] = mquant;
3382
            s->ac_pred = get_bits1(gb);
3383
            cbp = 0;
3384
        } else {
3385
            if(bmvtype == BMV_TYPE_INTERPOLATED) {
3386
                GET_MVDATA(dmv_x[1], dmv_y[1]);
3387
                if(!mb_has_coeffs) {
3388
                    /* interpolated skipped block */
3389
                    vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3390
                    return;
3391
                }
3392
            }
3393
            if(!s->mb_intra)
3394
                vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3395
            if(s->mb_intra)
3396
                s->ac_pred = get_bits1(gb);
3397
            cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3398
            GET_MQUANT();
3399
            s->current_picture.qscale_table[mb_pos] = mquant;
3400
            if(!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3401
                ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3402
        }
3403
    }
3404
    dst_idx = 0;
3405
    for (i=0; i<6; i++)
3406
    {
3407
        s->dc_val[0][s->block_index[i]] = 0;
3408
        dst_idx += i >> 2;
3409
        val = ((cbp >> (5 - i)) & 1);
3410
        off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3411
        v->mb_type[0][s->block_index[i]] = s->mb_intra;
3412
        if(s->mb_intra) {
3413
            /* check if prediction blocks A and C are available */
3414
            v->a_avail = v->c_avail = 0;
3415
            if(i == 2 || i == 3 || !s->first_slice_line)
3416
                v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3417
            if(i == 1 || i == 3 || s->mb_x)
3418
                v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3419

    
3420
            vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
3421
            if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3422
            s->dsp.vc1_inv_trans_8x8(s->block[i]);
3423
            if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
3424
            for(j = 0; j < 64; j++) s->block[i][j] += 128;
3425
            s->dsp.put_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
3426
        } else if(val) {
3427
            vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block);
3428
            if(!v->ttmbf && ttmb < 8) ttmb = -1;
3429
            first_block = 0;
3430
            if((i<4) || !(s->flags & CODEC_FLAG_GRAY))
3431
                s->dsp.add_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
3432
        }
3433
    }
3434
}
3435

    
3436
/** Decode blocks of I-frame
3437
 */
3438
static void vc1_decode_i_blocks(VC1Context *v)
3439
{
3440
    int k, j;
3441
    MpegEncContext *s = &v->s;
3442
    int cbp, val;
3443
    uint8_t *coded_val;
3444
    int mb_pos;
3445

    
3446
    /* select codingmode used for VLC tables selection */
3447
    switch(v->y_ac_table_index){
3448
    case 0:
3449
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
3450
        break;
3451
    case 1:
3452
        v->codingset = CS_HIGH_MOT_INTRA;
3453
        break;
3454
    case 2:
3455
        v->codingset = CS_MID_RATE_INTRA;
3456
        break;
3457
    }
3458

    
3459
    switch(v->c_ac_table_index){
3460
    case 0:
3461
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
3462
        break;
3463
    case 1:
3464
        v->codingset2 = CS_HIGH_MOT_INTER;
3465
        break;
3466
    case 2:
3467
        v->codingset2 = CS_MID_RATE_INTER;
3468
        break;
3469
    }
3470

    
3471
    /* Set DC scale - y and c use the same */
3472
    s->y_dc_scale = s->y_dc_scale_table[v->pq];
3473
    s->c_dc_scale = s->c_dc_scale_table[v->pq];
3474

    
3475
    //do frame decode
3476
    s->mb_x = s->mb_y = 0;
3477
    s->mb_intra = 1;
3478
    s->first_slice_line = 1;
3479
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3480
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3481
        for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
3482
            ff_init_block_index(s);
3483
            ff_update_block_index(s);
3484
            s->dsp.clear_blocks(s->block[0]);
3485
            mb_pos = s->mb_x + s->mb_y * s->mb_width;
3486
            s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
3487
            s->current_picture.qscale_table[mb_pos] = v->pq;
3488

    
3489
            // do actual MB decoding and displaying
3490
            cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
3491
            v->s.ac_pred = get_bits(&v->s.gb, 1);
3492

    
3493
            for(k = 0; k < 6; k++) {
3494
                val = ((cbp >> (5 - k)) & 1);
3495

    
3496
                if (k < 4) {
3497
                    int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
3498
                    val = val ^ pred;
3499
                    *coded_val = val;
3500
                }
3501
                cbp |= val << (5 - k);
3502

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

    
3505
                s->dsp.vc1_inv_trans_8x8(s->block[k]);
3506
                if(v->pq >= 9 && v->overlap) {
3507
                    for(j = 0; j < 64; j++) s->block[k][j] += 128;
3508
                }
3509
            }
3510

    
3511
            vc1_put_block(v, s->block);
3512
            if(v->pq >= 9 && v->overlap) { /* XXX: do proper overlapping insted of loop filter */
3513
                if(!s->first_slice_line) {
3514
                    s->dsp.vc1_v_overlap(s->dest[0], s->linesize, 0);
3515
                    s->dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize, 0);
3516
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
3517
                        s->dsp.vc1_v_overlap(s->dest[1], s->uvlinesize, s->mb_y&1);
3518
                        s->dsp.vc1_v_overlap(s->dest[2], s->uvlinesize, s->mb_y&1);
3519
                    }
3520
                }
3521
                s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize, 1);
3522
                s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize, 1);
3523
                if(s->mb_x) {
3524
                    s->dsp.vc1_h_overlap(s->dest[0], s->linesize, 0);
3525
                    s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize, 0);
3526
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
3527
                        s->dsp.vc1_h_overlap(s->dest[1], s->uvlinesize, s->mb_x&1);
3528
                        s->dsp.vc1_h_overlap(s->dest[2], s->uvlinesize, s->mb_x&1);
3529
                    }
3530
                }
3531
                s->dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize, 1);
3532
                s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize, 1);
3533
            }
3534

    
3535
            if(get_bits_count(&s->gb) > v->bits) {
3536
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
3537
                return;
3538
            }
3539
        }
3540
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3541
        s->first_slice_line = 0;
3542
    }
3543
}
3544

    
3545
/** Decode blocks of I-frame for advanced profile
3546
 */
3547
static void vc1_decode_i_blocks_adv(VC1Context *v)
3548
{
3549
    int k, j;
3550
    MpegEncContext *s = &v->s;
3551
    int cbp, val;
3552
    uint8_t *coded_val;
3553
    int mb_pos;
3554
    int mquant = v->pq;
3555
    int mqdiff;
3556
    int overlap;
3557
    GetBitContext *gb = &s->gb;
3558

    
3559
    /* select codingmode used for VLC tables selection */
3560
    switch(v->y_ac_table_index){
3561
    case 0:
3562
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
3563
        break;
3564
    case 1:
3565
        v->codingset = CS_HIGH_MOT_INTRA;
3566
        break;
3567
    case 2:
3568
        v->codingset = CS_MID_RATE_INTRA;
3569
        break;
3570
    }
3571

    
3572
    switch(v->c_ac_table_index){
3573
    case 0:
3574
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
3575
        break;
3576
    case 1:
3577
        v->codingset2 = CS_HIGH_MOT_INTER;
3578
        break;
3579
    case 2:
3580
        v->codingset2 = CS_MID_RATE_INTER;
3581
        break;
3582
    }
3583

    
3584
    /* Set DC scale - y and c use the same */
3585
    s->y_dc_scale = s->y_dc_scale_table[v->pq];
3586
    s->c_dc_scale = s->c_dc_scale_table[v->pq];
3587

    
3588
    //do frame decode
3589
    s->mb_x = s->mb_y = 0;
3590
    s->mb_intra = 1;
3591
    s->first_slice_line = 1;
3592
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3593
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3594
        for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
3595
            ff_init_block_index(s);
3596
            ff_update_block_index(s);
3597
            s->dsp.clear_blocks(s->block[0]);
3598
            mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3599
            s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
3600

    
3601
            // do actual MB decoding and displaying
3602
            cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
3603
            if(v->acpred_is_raw)
3604
                v->s.ac_pred = get_bits(&v->s.gb, 1);
3605
            else
3606
                v->s.ac_pred = v->acpred_plane[mb_pos];
3607

    
3608
            if(v->condover == CONDOVER_SELECT) {
3609
                if(v->overflg_is_raw)
3610
                    overlap = get_bits(&v->s.gb, 1);
3611
                else
3612
                    overlap = v->over_flags_plane[mb_pos];
3613
            } else
3614
                overlap = (v->condover == CONDOVER_ALL);
3615

    
3616
            GET_MQUANT();
3617

    
3618
            s->current_picture.qscale_table[mb_pos] = mquant;
3619

    
3620
            for(k = 0; k < 6; k++) {
3621
                val = ((cbp >> (5 - k)) & 1);
3622

    
3623
                if (k < 4) {
3624
                    int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
3625
                    val = val ^ pred;
3626
                    *coded_val = val;
3627
                }
3628
                cbp |= val << (5 - k);
3629

    
3630
                v->a_avail = !s->first_slice_line || (k==2 || k==3);
3631
                v->c_avail = !!s->mb_x || (k==1 || k==3);
3632

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

    
3635
                s->dsp.vc1_inv_trans_8x8(s->block[k]);
3636
                for(j = 0; j < 64; j++) s->block[k][j] += 128;
3637
            }
3638

    
3639
            vc1_put_block(v, s->block);
3640
            if(overlap) {
3641
                if(!s->first_slice_line) {
3642
                    s->dsp.vc1_v_overlap(s->dest[0], s->linesize, 0);
3643
                    s->dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize, 0);
3644
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
3645
                        s->dsp.vc1_v_overlap(s->dest[1], s->uvlinesize, s->mb_y&1);
3646
                        s->dsp.vc1_v_overlap(s->dest[2], s->uvlinesize, s->mb_y&1);
3647
                    }
3648
                }
3649
                s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize, 1);
3650
                s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize, 1);
3651
                if(s->mb_x) {
3652
                    s->dsp.vc1_h_overlap(s->dest[0], s->linesize, 0);
3653
                    s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize, 0);
3654
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
3655
                        s->dsp.vc1_h_overlap(s->dest[1], s->uvlinesize, s->mb_x&1);
3656
                        s->dsp.vc1_h_overlap(s->dest[2], s->uvlinesize, s->mb_x&1);
3657
                    }
3658
                }
3659
                s->dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize, 1);
3660
                s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize, 1);
3661
            }
3662

    
3663
            if(get_bits_count(&s->gb) > v->bits) {
3664
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
3665
                return;
3666
            }
3667
        }
3668
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3669
        s->first_slice_line = 0;
3670
    }
3671
}
3672

    
3673
static void vc1_decode_p_blocks(VC1Context *v)
3674
{
3675
    MpegEncContext *s = &v->s;
3676

    
3677
    /* select codingmode used for VLC tables selection */
3678
    switch(v->c_ac_table_index){
3679
    case 0:
3680
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
3681
        break;
3682
    case 1:
3683
        v->codingset = CS_HIGH_MOT_INTRA;
3684
        break;
3685
    case 2:
3686
        v->codingset = CS_MID_RATE_INTRA;
3687
        break;
3688
    }
3689

    
3690
    switch(v->c_ac_table_index){
3691
    case 0:
3692
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
3693
        break;
3694
    case 1:
3695
        v->codingset2 = CS_HIGH_MOT_INTER;
3696
        break;
3697
    case 2:
3698
        v->codingset2 = CS_MID_RATE_INTER;
3699
        break;
3700
    }
3701

    
3702
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3703
    s->first_slice_line = 1;
3704
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3705
        for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
3706
            ff_init_block_index(s);
3707
            ff_update_block_index(s);
3708
            s->dsp.clear_blocks(s->block[0]);
3709

    
3710
            vc1_decode_p_mb(v);
3711
            if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
3712
                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);
3713
                return;
3714
            }
3715
        }
3716
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3717
        s->first_slice_line = 0;
3718
    }
3719
}
3720

    
3721
static void vc1_decode_b_blocks(VC1Context *v)
3722
{
3723
    MpegEncContext *s = &v->s;
3724

    
3725
    /* select codingmode used for VLC tables selection */
3726
    switch(v->c_ac_table_index){
3727
    case 0:
3728
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
3729
        break;
3730
    case 1:
3731
        v->codingset = CS_HIGH_MOT_INTRA;
3732
        break;
3733
    case 2:
3734
        v->codingset = CS_MID_RATE_INTRA;
3735
        break;
3736
    }
3737

    
3738
    switch(v->c_ac_table_index){
3739
    case 0:
3740
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
3741
        break;
3742
    case 1:
3743
        v->codingset2 = CS_HIGH_MOT_INTER;
3744
        break;
3745
    case 2:
3746
        v->codingset2 = CS_MID_RATE_INTER;
3747
        break;
3748
    }
3749

    
3750
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3751
    s->first_slice_line = 1;
3752
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3753
        for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
3754
            ff_init_block_index(s);
3755
            ff_update_block_index(s);
3756
            s->dsp.clear_blocks(s->block[0]);
3757

    
3758
            vc1_decode_b_mb(v);
3759
            if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
3760
                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);
3761
                return;
3762
            }
3763
        }
3764
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3765
        s->first_slice_line = 0;
3766
    }
3767
}
3768

    
3769
static void vc1_decode_skip_blocks(VC1Context *v)
3770
{
3771
    MpegEncContext *s = &v->s;
3772

    
3773
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3774
    s->first_slice_line = 1;
3775
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3776
        s->mb_x = 0;
3777
        ff_init_block_index(s);
3778
        ff_update_block_index(s);
3779
        memcpy(s->dest[0], s->last_picture.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
3780
        memcpy(s->dest[1], s->last_picture.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
3781
        memcpy(s->dest[2], s->last_picture.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
3782
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3783
        s->first_slice_line = 0;
3784
    }
3785
    s->pict_type = P_TYPE;
3786
}
3787

    
3788
static void vc1_decode_blocks(VC1Context *v)
3789
{
3790

    
3791
    v->s.esc3_level_length = 0;
3792

    
3793
    switch(v->s.pict_type) {
3794
    case I_TYPE:
3795
        if(v->profile == PROFILE_ADVANCED)
3796
            vc1_decode_i_blocks_adv(v);
3797
        else
3798
            vc1_decode_i_blocks(v);
3799
        break;
3800
    case P_TYPE:
3801
        if(v->p_frame_skipped)
3802
            vc1_decode_skip_blocks(v);
3803
        else
3804
            vc1_decode_p_blocks(v);
3805
        break;
3806
    case B_TYPE:
3807
        vc1_decode_b_blocks(v);
3808
        break;
3809
    }
3810
}
3811

    
3812

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

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

    
3832
    if(ff_h263_decode_init(avctx) < 0)
3833
        return -1;
3834
    if (vc1_init_common(v) < 0) return -1;
3835

    
3836
    avctx->coded_width = avctx->width;
3837
    avctx->coded_height = avctx->height;
3838
    if (avctx->codec_id == CODEC_ID_WMV3)
3839
    {
3840
        int count = 0;
3841

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

    
3847
        init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
3848

    
3849
        if (decode_sequence_header(avctx, &gb) < 0)
3850
          return -1;
3851

    
3852
        count = avctx->extradata_size*8 - get_bits_count(&gb);
3853
        if (count>0)
3854
        {
3855
            av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
3856
                   count, get_bits(&gb, count));
3857
        }
3858
        else if (count < 0)
3859
        {
3860
            av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
3861
        }
3862
    } else { // VC1/WVC1
3863
        int edata_size = avctx->extradata_size;
3864
        uint8_t *edata = avctx->extradata;
3865

    
3866
        if(avctx->extradata_size < 16) {
3867
            av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", edata_size);
3868
            return -1;
3869
        }
3870
        while(edata_size > 8) {
3871
            // test if we've found header
3872
            if(BE_32(edata) == 0x0000010F) {
3873
                edata += 4;
3874
                edata_size -= 4;
3875
                break;
3876
            }
3877
            edata_size--;
3878
            edata++;
3879
        }
3880

    
3881
        init_get_bits(&gb, edata, edata_size*8);
3882

    
3883
        if (decode_sequence_header(avctx, &gb) < 0)
3884
          return -1;
3885

    
3886
        while(edata_size > 8) {
3887
            // test if we've found entry point
3888
            if(BE_32(edata) == 0x0000010E) {
3889
                edata += 4;
3890
                edata_size -= 4;
3891
                break;
3892
            }
3893
            edata_size--;
3894
            edata++;
3895
        }
3896

    
3897
        init_get_bits(&gb, edata, edata_size*8);
3898

    
3899
        if (decode_entry_point(avctx, &gb) < 0)
3900
          return -1;
3901
    }
3902
    avctx->has_b_frames= !!(avctx->max_b_frames);
3903

    
3904
    s->mb_width = (avctx->coded_width+15)>>4;
3905
    s->mb_height = (avctx->coded_height+15)>>4;
3906

    
3907
    /* Allocate mb bitplanes */
3908
    v->mv_type_mb_plane = av_malloc(s->mb_stride * s->mb_height);
3909
    v->direct_mb_plane = av_malloc(s->mb_stride * s->mb_height);
3910
    v->acpred_plane = av_malloc(s->mb_stride * s->mb_height);
3911
    v->over_flags_plane = av_malloc(s->mb_stride * s->mb_height);
3912

    
3913
    /* allocate block type info in that way so it could be used with s->block_index[] */
3914
    v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
3915
    v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
3916
    v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
3917
    v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
3918

    
3919
    /* Init coded blocks info */
3920
    if (v->profile == PROFILE_ADVANCED)
3921
    {
3922
//        if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
3923
//            return -1;
3924
//        if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
3925
//            return -1;
3926
    }
3927

    
3928
    return 0;
3929
}
3930

    
3931

    
3932
/** Decode a VC1/WMV3 frame
3933
 * @todo TODO: Handle VC-1 IDUs (Transport level?)
3934
 * @warning Initial try at using MpegEncContext stuff
3935
 */
3936
static int vc1_decode_frame(AVCodecContext *avctx,
3937
                            void *data, int *data_size,
3938
                            uint8_t *buf, int buf_size)
3939
{
3940
    VC1Context *v = avctx->priv_data;
3941
    MpegEncContext *s = &v->s;
3942
    AVFrame *pict = data;
3943
    uint8_t *buf2 = NULL;
3944

    
3945
    /* no supplementary picture */
3946
    if (buf_size == 0) {
3947
        /* special case for last picture */
3948
        if (s->low_delay==0 && s->next_picture_ptr) {
3949
            *pict= *(AVFrame*)s->next_picture_ptr;
3950
            s->next_picture_ptr= NULL;
3951

    
3952
            *data_size = sizeof(AVFrame);
3953
        }
3954

    
3955
        return 0;
3956
    }
3957

    
3958
    //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
3959
    if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
3960
        int i= ff_find_unused_picture(s, 0);
3961
        s->current_picture_ptr= &s->picture[i];
3962
    }
3963

    
3964
    avctx->has_b_frames= !s->low_delay;
3965

    
3966
    //for advanced profile we need to unescape buffer
3967
    if (avctx->codec_id == CODEC_ID_VC1) {
3968
        int i, buf_size2;
3969
        buf2 = av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
3970
        buf_size2 = 0;
3971
        for(i = 0; i < buf_size; i++) {
3972
            if(buf[i] == 3 && i >= 2 && !buf[i-1] && !buf[i-2] && i < buf_size-1 && buf[i+1] < 4) {
3973
                buf2[buf_size2++] = buf[i+1];
3974
                i++;
3975
            } else
3976
                buf2[buf_size2++] = buf[i];
3977
        }
3978
        init_get_bits(&s->gb, buf2, buf_size2*8);
3979
    } else
3980
        init_get_bits(&s->gb, buf, buf_size*8);
3981
    // do parse frame header
3982
    if(v->profile < PROFILE_ADVANCED) {
3983
        if(vc1_parse_frame_header(v, &s->gb) == -1) {
3984
            if(buf2)av_free(buf2);
3985
            return -1;
3986
        }
3987
    } else {
3988
        if(vc1_parse_frame_header_adv(v, &s->gb) == -1) {
3989
            if(buf2)av_free(buf2);
3990
            return -1;
3991
        }
3992
    }
3993

    
3994
    if(s->pict_type != I_TYPE && !v->res_rtm_flag){
3995
        if(buf2)av_free(buf2);
3996
        return -1;
3997
    }
3998

    
3999
    // for hurry_up==5
4000
    s->current_picture.pict_type= s->pict_type;
4001
    s->current_picture.key_frame= s->pict_type == I_TYPE;
4002

    
4003
    /* skip B-frames if we don't have reference frames */
4004
    if(s->last_picture_ptr==NULL && (s->pict_type==B_TYPE || s->dropable)){
4005
        if(buf2)av_free(buf2);
4006
        return -1;//buf_size;
4007
    }
4008
    /* skip b frames if we are in a hurry */
4009
    if(avctx->hurry_up && s->pict_type==B_TYPE) return -1;//buf_size;
4010
    if(   (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==B_TYPE)
4011
       || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=I_TYPE)
4012
       ||  avctx->skip_frame >= AVDISCARD_ALL) {
4013
        if(buf2)av_free(buf2);
4014
        return buf_size;
4015
    }
4016
    /* skip everything if we are in a hurry>=5 */
4017
    if(avctx->hurry_up>=5) {
4018
        if(buf2)av_free(buf2);
4019
        return -1;//buf_size;
4020
    }
4021

    
4022
    if(s->next_p_frame_damaged){
4023
        if(s->pict_type==B_TYPE)
4024
            return buf_size;
4025
        else
4026
            s->next_p_frame_damaged=0;
4027
    }
4028

    
4029
    if(MPV_frame_start(s, avctx) < 0) {
4030
        if(buf2)av_free(buf2);
4031
        return -1;
4032
    }
4033

    
4034
    ff_er_frame_start(s);
4035

    
4036
    v->bits = buf_size * 8;
4037
    vc1_decode_blocks(v);
4038
//av_log(s->avctx, AV_LOG_INFO, "Consumed %i/%i bits\n", get_bits_count(&s->gb), buf_size*8);
4039
//  if(get_bits_count(&s->gb) > buf_size * 8)
4040
//      return -1;
4041
    ff_er_frame_end(s);
4042

    
4043
    MPV_frame_end(s);
4044

    
4045
assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
4046
assert(s->current_picture.pict_type == s->pict_type);
4047
    if (s->pict_type == B_TYPE || s->low_delay) {
4048
        *pict= *(AVFrame*)s->current_picture_ptr;
4049
    } else if (s->last_picture_ptr != NULL) {
4050
        *pict= *(AVFrame*)s->last_picture_ptr;
4051
    }
4052

    
4053
    if(s->last_picture_ptr || s->low_delay){
4054
        *data_size = sizeof(AVFrame);
4055
        ff_print_debug_info(s, pict);
4056
    }
4057

    
4058
    /* Return the Picture timestamp as the frame number */
4059
    /* we substract 1 because it is added on utils.c    */
4060
    avctx->frame_number = s->picture_number - 1;
4061

    
4062
    if(buf2)av_free(buf2);
4063
    return buf_size;
4064
}
4065

    
4066

    
4067
/** Close a VC1/WMV3 decoder
4068
 * @warning Initial try at using MpegEncContext stuff
4069
 */
4070
static int vc1_decode_end(AVCodecContext *avctx)
4071
{
4072
    VC1Context *v = avctx->priv_data;
4073

    
4074
    av_freep(&v->hrd_rate);
4075
    av_freep(&v->hrd_buffer);
4076
    MPV_common_end(&v->s);
4077
    av_freep(&v->mv_type_mb_plane);
4078
    av_freep(&v->direct_mb_plane);
4079
    av_freep(&v->acpred_plane);
4080
    av_freep(&v->over_flags_plane);
4081
    av_freep(&v->mb_type_base);
4082
    return 0;
4083
}
4084

    
4085

    
4086
AVCodec vc1_decoder = {
4087
    "vc1",
4088
    CODEC_TYPE_VIDEO,
4089
    CODEC_ID_VC1,
4090
    sizeof(VC1Context),
4091
    vc1_decode_init,
4092
    NULL,
4093
    vc1_decode_end,
4094
    vc1_decode_frame,
4095
    CODEC_CAP_DELAY,
4096
    NULL
4097
};
4098

    
4099
AVCodec wmv3_decoder = {
4100
    "wmv3",
4101
    CODEC_TYPE_VIDEO,
4102
    CODEC_ID_WMV3,
4103
    sizeof(VC1Context),
4104
    vc1_decode_init,
4105
    NULL,
4106
    vc1_decode_end,
4107
    vc1_decode_frame,
4108
    CODEC_CAP_DELAY,
4109
    NULL
4110
};