Statistics
| Branch: | Revision:

ffmpeg / libavcodec / vc1.c @ 0a45801f

History | View | Annotate | Download (97.7 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 = 3 //XXX: ??
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
/** The VC1 Context
213
 * @fixme Change size wherever another size is more efficient
214
 * Many members are only used for Advanced Profile
215
 */
216
typedef struct VC1Context{
217
    MpegEncContext s;
218

    
219
    int bits;
220

    
221
    /** Simple/Main Profile sequence header */
222
    //@{
223
    int res_sm;           ///< reserved, 2b
224
    int res_x8;           ///< reserved
225
    int multires;         ///< frame-level RESPIC syntax element present
226
    int res_fasttx;       ///< reserved, always 1
227
    int res_transtab;     ///< reserved, always 0
228
    int rangered;         ///< RANGEREDFRM (range reduction) syntax element present
229
                          ///< at frame level
230
    int res_rtm_flag;     ///< reserved, set to 1
231
    int reserved;         ///< reserved
232
    //@}
233

    
234
    /** Advanced Profile */
235
    //@{
236
    int level;            ///< 3bits, for Advanced/Simple Profile, provided by TS layer
237
    int chromaformat;     ///< 2bits, 2=4:2:0, only defined
238
    int postprocflag;     ///< Per-frame processing suggestion flag present
239
    int broadcast;        ///< TFF/RFF present
240
    int interlace;        ///< Progressive/interlaced (RPTFTM syntax element)
241
    int tfcntrflag;       ///< TFCNTR present
242
    int panscanflag;      ///< NUMPANSCANWIN, TOPLEFT{X,Y}, BOTRIGHT{X,Y} present
243
    int extended_dmv;     ///< Additional extended dmv range at P/B frame-level
244
    int color_prim;       ///< 8bits, chroma coordinates of the color primaries
245
    int transfer_char;    ///< 8bits, Opto-electronic transfer characteristics
246
    int matrix_coef;      ///< 8bits, Color primaries->YCbCr transform matrix
247
    int hrd_param_flag;   ///< Presence of Hypothetical Reference
248
                          ///< Decoder parameters
249
    //@}
250

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

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

    
298

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

    
325
    /** Frame decoding info for S/M profiles only */
326
    //@{
327
    uint8_t rangeredfrm; ///< out_sample = CLIP((in_sample-128)*2+128)
328
    uint8_t interpfrm;
329
    //@}
330

    
331
    /** Frame decoding info for Advanced profile */
332
    //@{
333
    uint8_t fcm; ///< 0->Progressive, 2->Frame-Interlace, 3->Field-Interlace
334
    uint8_t numpanscanwin;
335
    uint8_t tfcntr;
336
    uint8_t rptfrm, tff, rff;
337
    uint16_t topleftx;
338
    uint16_t toplefty;
339
    uint16_t bottomrightx;
340
    uint16_t bottomrighty;
341
    uint8_t uvsamp;
342
    uint8_t postproc;
343
    int hrd_num_leaky_buckets;
344
    uint8_t bit_rate_exponent;
345
    uint8_t buffer_size_exponent;
346
//    BitPlane ac_pred_plane;       ///< AC prediction flags bitplane
347
//    BitPlane over_flags_plane;    ///< Overflags bitplane
348
    uint8_t condover;
349
    uint16_t *hrd_rate, *hrd_buffer;
350
    uint8_t *hrd_fullness;
351
    uint8_t range_mapy_flag;
352
    uint8_t range_mapuv_flag;
353
    uint8_t range_mapy;
354
    uint8_t range_mapuv;
355
    //@}
356
} VC1Context;
357

    
358
/**
359
 * Get unary code of limited length
360
 * @fixme FIXME Slow and ugly
361
 * @param gb GetBitContext
362
 * @param[in] stop The bitstop value (unary code of 1's or 0's)
363
 * @param[in] len Maximum length
364
 * @return Unary length/index
365
 */
366
static int get_prefix(GetBitContext *gb, int stop, int len)
367
{
368
#if 1
369
    int i;
370

    
371
    for(i = 0; i < len && get_bits1(gb) != stop; i++);
372
    return i;
373
/*  int i = 0, tmp = !stop;
374

375
  while (i != len && tmp != stop)
376
  {
377
    tmp = get_bits(gb, 1);
378
    i++;
379
  }
380
  if (i == len && tmp != stop) return len+1;
381
  return i;*/
382
#else
383
  unsigned int buf;
384
  int log;
385

    
386
  OPEN_READER(re, gb);
387
  UPDATE_CACHE(re, gb);
388
  buf=GET_CACHE(re, gb); //Still not sure
389
  if (stop) buf = ~buf;
390

    
391
  log= av_log2(-buf); //FIXME: -?
392
  if (log < limit){
393
    LAST_SKIP_BITS(re, gb, log+1);
394
    CLOSE_READER(re, gb);
395
    return log;
396
  }
397

    
398
  LAST_SKIP_BITS(re, gb, limit);
399
  CLOSE_READER(re, gb);
400
  return limit;
401
#endif
402
}
403

    
404
static inline int decode210(GetBitContext *gb){
405
    int n;
406
    n = get_bits1(gb);
407
    if (n == 1)
408
        return 0;
409
    else
410
        return 2 - get_bits1(gb);
411
}
412

    
413
/**
414
 * Init VC-1 specific tables and VC1Context members
415
 * @param v The VC1Context to initialize
416
 * @return Status
417
 */
418
static int vc1_init_common(VC1Context *v)
419
{
420
    static int done = 0;
421
    int i = 0;
422

    
423
    v->hrd_rate = v->hrd_buffer = NULL;
424

    
425
    /* VLC tables */
426
    if(!done)
427
    {
428
        done = 1;
429
        init_vlc(&vc1_bfraction_vlc, VC1_BFRACTION_VLC_BITS, 23,
430
                 vc1_bfraction_bits, 1, 1,
431
                 vc1_bfraction_codes, 1, 1, 1);
432
        init_vlc(&vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4,
433
                 vc1_norm2_bits, 1, 1,
434
                 vc1_norm2_codes, 1, 1, 1);
435
        init_vlc(&vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 64,
436
                 vc1_norm6_bits, 1, 1,
437
                 vc1_norm6_codes, 2, 2, 1);
438
        init_vlc(&vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7,
439
                 vc1_imode_bits, 1, 1,
440
                 vc1_imode_codes, 1, 1, 1);
441
        for (i=0; i<3; i++)
442
        {
443
            init_vlc(&vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 16,
444
                     vc1_ttmb_bits[i], 1, 1,
445
                     vc1_ttmb_codes[i], 2, 2, 1);
446
            init_vlc(&vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 8,
447
                     vc1_ttblk_bits[i], 1, 1,
448
                     vc1_ttblk_codes[i], 1, 1, 1);
449
            init_vlc(&vc1_subblkpat_vlc[i], VC1_SUBBLKPAT_VLC_BITS, 15,
450
                     vc1_subblkpat_bits[i], 1, 1,
451
                     vc1_subblkpat_codes[i], 1, 1, 1);
452
        }
453
        for(i=0; i<4; i++)
454
        {
455
            init_vlc(&vc1_4mv_block_pattern_vlc[i], VC1_4MV_BLOCK_PATTERN_VLC_BITS, 16,
456
                     vc1_4mv_block_pattern_bits[i], 1, 1,
457
                     vc1_4mv_block_pattern_codes[i], 1, 1, 1);
458
            init_vlc(&vc1_cbpcy_p_vlc[i], VC1_CBPCY_P_VLC_BITS, 64,
459
                     vc1_cbpcy_p_bits[i], 1, 1,
460
                     vc1_cbpcy_p_codes[i], 2, 2, 1);
461
            init_vlc(&vc1_mv_diff_vlc[i], VC1_MV_DIFF_VLC_BITS, 73,
462
                     vc1_mv_diff_bits[i], 1, 1,
463
                     vc1_mv_diff_codes[i], 2, 2, 1);
464
        }
465
        for(i=0; i<8; i++)
466
            init_vlc(&vc1_ac_coeff_table[i], AC_VLC_BITS, vc1_ac_sizes[i],
467
                     &vc1_ac_tables[i][0][1], 8, 4,
468
                     &vc1_ac_tables[i][0][0], 8, 4, 1);
469
        init_vlc(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64,
470
                 &ff_msmp4_mb_i_table[0][1], 4, 2,
471
                 &ff_msmp4_mb_i_table[0][0], 4, 2, 1);
472
    }
473

    
474
    /* Other defaults */
475
    v->pq = -1;
476
    v->mvrange = 0; /* 7.1.1.18, p80 */
477

    
478
    return 0;
479
}
480

    
481
/***********************************************************************/
482
/**
483
 * @defgroup bitplane VC9 Bitplane decoding
484
 * @see 8.7, p56
485
 * @{
486
 */
487

    
488
/** @addtogroup bitplane
489
 * Imode types
490
 * @{
491
 */
492
enum Imode {
493
    IMODE_RAW,
494
    IMODE_NORM2,
495
    IMODE_DIFF2,
496
    IMODE_NORM6,
497
    IMODE_DIFF6,
498
    IMODE_ROWSKIP,
499
    IMODE_COLSKIP
500
};
501
/** @} */ //imode defines
502

    
503
/** Decode rows by checking if they are skipped
504
 * @param plane Buffer to store decoded bits
505
 * @param[in] width Width of this buffer
506
 * @param[in] height Height of this buffer
507
 * @param[in] stride of this buffer
508
 */
509
static void decode_rowskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
510
    int x, y;
511

    
512
    for (y=0; y<height; y++){
513
        if (!get_bits(gb, 1)) //rowskip
514
            memset(plane, 0, width);
515
        else
516
            for (x=0; x<width; x++)
517
                plane[x] = get_bits(gb, 1);
518
        plane += stride;
519
    }
520
}
521

    
522
/** Decode columns by checking if they are skipped
523
 * @param plane Buffer to store decoded bits
524
 * @param[in] width Width of this buffer
525
 * @param[in] height Height of this buffer
526
 * @param[in] stride of this buffer
527
 * @fixme FIXME: Optimize
528
 */
529
static void decode_colskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
530
    int x, y;
531

    
532
    for (x=0; x<width; x++){
533
        if (!get_bits(gb, 1)) //colskip
534
            for (y=0; y<height; y++)
535
                plane[y*stride] = 0;
536
        else
537
            for (y=0; y<height; y++)
538
                plane[y*stride] = get_bits(gb, 1);
539
        plane ++;
540
    }
541
}
542

    
543
/** Decode a bitplane's bits
544
 * @param bp Bitplane where to store the decode bits
545
 * @param v VC-1 context for bit reading and logging
546
 * @return Status
547
 * @fixme FIXME: Optimize
548
 * @todo TODO: Decide if a struct is needed
549
 */
550
static int bitplane_decoding(uint8_t* data, int *raw_flag, VC1Context *v)
551
{
552
    GetBitContext *gb = &v->s.gb;
553

    
554
    int imode, x, y, code, offset;
555
    uint8_t invert, *planep = data;
556
    int width, height, stride;
557

    
558
    width = v->s.mb_width;
559
    height = v->s.mb_height;
560
    stride = v->s.mb_stride;
561
    invert = get_bits(gb, 1);
562
    imode = get_vlc2(gb, vc1_imode_vlc.table, VC1_IMODE_VLC_BITS, 1);
563

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

    
648
    /* Applying diff operator */
649
    if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6)
650
    {
651
        planep = data;
652
        planep[0] ^= invert;
653
        for (x=1; x<width; x++)
654
            planep[x] ^= planep[x-1];
655
        for (y=1; y<height; y++)
656
        {
657
            planep += stride;
658
            planep[0] ^= planep[-stride];
659
            for (x=1; x<width; x++)
660
            {
661
                if (planep[x-1] != planep[x-stride]) planep[x] ^= invert;
662
                else                                 planep[x] ^= planep[x-1];
663
            }
664
        }
665
    }
666
    else if (invert)
667
    {
668
        planep = data;
669
        for (x=0; x<stride*height; x++) planep[x] = !planep[x]; //FIXME stride
670
    }
671
    return (imode<<1) + invert;
672
}
673

    
674
/** @} */ //Bitplane group
675

    
676
/***********************************************************************/
677
/** VOP Dquant decoding
678
 * @param v VC-1 Context
679
 */
680
static int vop_dquant_decoding(VC1Context *v)
681
{
682
    GetBitContext *gb = &v->s.gb;
683
    int pqdiff;
684

    
685
    //variable size
686
    if (v->dquant == 2)
687
    {
688
        pqdiff = get_bits(gb, 3);
689
        if (pqdiff == 7) v->altpq = get_bits(gb, 5);
690
        else v->altpq = v->pq + pqdiff + 1;
691
    }
692
    else
693
    {
694
        v->dquantfrm = get_bits(gb, 1);
695
        if ( v->dquantfrm )
696
        {
697
            v->dqprofile = get_bits(gb, 2);
698
            switch (v->dqprofile)
699
            {
700
            case DQPROFILE_SINGLE_EDGE:
701
            case DQPROFILE_DOUBLE_EDGES:
702
                v->dqsbedge = get_bits(gb, 2);
703
                break;
704
            case DQPROFILE_ALL_MBS:
705
                v->dqbilevel = get_bits(gb, 1);
706
            default: break; //Forbidden ?
707
            }
708
            if (v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS)
709
            {
710
                pqdiff = get_bits(gb, 3);
711
                if (pqdiff == 7) v->altpq = get_bits(gb, 5);
712
                else v->altpq = v->pq + pqdiff + 1;
713
            }
714
        }
715
    }
716
    return 0;
717
}
718

    
719

    
720
/** Do inverse transform
721
 */
722
static void vc1_inv_trans(DCTELEM block[64], int M, int N)
723
{
724
    int i;
725
    register int t1,t2,t3,t4,t5,t6,t7,t8;
726
    DCTELEM *src, *dst;
727

    
728
    src = block;
729
    dst = block;
730
    if(M==4){
731
        for(i = 0; i < N; i++){
732
            t1 = 17 * (src[0] + src[2]);
733
            t2 = 17 * (src[0] - src[2]);
734
            t3 = 22 * src[1];
735
            t4 = 22 * src[3];
736
            t5 = 10 * src[1];
737
            t6 = 10 * src[3];
738

    
739
            dst[0] = (t1 + t3 + t6 + 4) >> 3;
740
            dst[1] = (t2 - t4 + t5 + 4) >> 3;
741
            dst[2] = (t2 + t4 - t5 + 4) >> 3;
742
            dst[3] = (t1 - t3 - t6 + 4) >> 3;
743

    
744
            src += 8;
745
            dst += 8;
746
        }
747
    }else{
748
        for(i = 0; i < N; i++){
749
            t1 = 12 * (src[0] + src[4]);
750
            t2 = 12 * (src[0] - src[4]);
751
            t3 = 16 * src[2] +  6 * src[6];
752
            t4 =  6 * src[2] - 16 * src[6];
753

    
754
            t5 = t1 + t3;
755
            t6 = t2 + t4;
756
            t7 = t2 - t4;
757
            t8 = t1 - t3;
758

    
759
            t1 = 16 * src[1] + 15 * src[3] +  9 * src[5] +  4 * src[7];
760
            t2 = 15 * src[1] -  4 * src[3] - 16 * src[5] -  9 * src[7];
761
            t3 =  9 * src[1] - 16 * src[3] +  4 * src[5] + 15 * src[7];
762
            t4 =  4 * src[1] -  9 * src[3] + 15 * src[5] - 16 * src[7];
763

    
764
            dst[0] = (t5 + t1 + 4) >> 3;
765
            dst[1] = (t6 + t2 + 4) >> 3;
766
            dst[2] = (t7 + t3 + 4) >> 3;
767
            dst[3] = (t8 + t4 + 4) >> 3;
768
            dst[4] = (t8 - t4 + 4) >> 3;
769
            dst[5] = (t7 - t3 + 4) >> 3;
770
            dst[6] = (t6 - t2 + 4) >> 3;
771
            dst[7] = (t5 - t1 + 4) >> 3;
772

    
773
            src += 8;
774
            dst += 8;
775
        }
776
    }
777

    
778
    src = block;
779
    dst = block;
780
    if(N==4){
781
        for(i = 0; i < M; i++){
782
            t1 = 17 * (src[ 0] + src[16]);
783
            t2 = 17 * (src[ 0] - src[16]);
784
            t3 = 22 * src[ 8];
785
            t4 = 22 * src[24];
786
            t5 = 10 * src[ 8];
787
            t6 = 10 * src[24];
788

    
789
            dst[ 0] = (t1 + t3 + t6 + 64) >> 7;
790
            dst[ 8] = (t2 - t4 + t5 + 64) >> 7;
791
            dst[16] = (t2 + t4 - t5 + 64) >> 7;
792
            dst[24] = (t1 - t3 - t6 + 64) >> 7;
793

    
794
            src ++;
795
            dst ++;
796
        }
797
    }else{
798
        for(i = 0; i < M; i++){
799
            t1 = 12 * (src[ 0] + src[32]);
800
            t2 = 12 * (src[ 0] - src[32]);
801
            t3 = 16 * src[16] +  6 * src[48];
802
            t4 =  6 * src[16] - 16 * src[48];
803

    
804
            t5 = t1 + t3;
805
            t6 = t2 + t4;
806
            t7 = t2 - t4;
807
            t8 = t1 - t3;
808

    
809
            t1 = 16 * src[ 8] + 15 * src[24] +  9 * src[40] +  4 * src[56];
810
            t2 = 15 * src[ 8] -  4 * src[24] - 16 * src[40] -  9 * src[56];
811
            t3 =  9 * src[ 8] - 16 * src[24] +  4 * src[40] + 15 * src[56];
812
            t4 =  4 * src[ 8] -  9 * src[24] + 15 * src[40] - 16 * src[56];
813

    
814
            dst[ 0] = (t5 + t1 + 64) >> 7;
815
            dst[ 8] = (t6 + t2 + 64) >> 7;
816
            dst[16] = (t7 + t3 + 64) >> 7;
817
            dst[24] = (t8 + t4 + 64) >> 7;
818
            dst[32] = (t8 - t4 + 64 + 1) >> 7;
819
            dst[40] = (t7 - t3 + 64 + 1) >> 7;
820
            dst[48] = (t6 - t2 + 64 + 1) >> 7;
821
            dst[56] = (t5 - t1 + 64 + 1) >> 7;
822

    
823
            src++;
824
            dst++;
825
        }
826
    }
827
}
828

    
829
/** Apply overlap transform to vertical edge
830
 * @todo optimize
831
 * @todo move to DSPContext
832
 */
833
static void vc1_v_overlap(uint8_t* src, int stride)
834
{
835
    int i;
836
    int a, b, c, d;
837
    for(i = 0; i < 8; i++) {
838
        a = src[-2*stride];
839
        b = src[-stride];
840
        c = src[0];
841
        d = src[stride];
842

    
843
        src[-2*stride] = clip_uint8((7*a + d + 3) >> 3);
844
        src[-stride] = clip_uint8((-a + 7*b + c + d + 3) >> 3);
845
        src[0] = clip_uint8((a + b + 7*c - d + 3) >> 3);
846
        src[stride] = clip_uint8((a + 7*d + 3) >> 3);
847
        src++;
848
    }
849
}
850

    
851
/** Apply overlap transform to horizontal edge
852
 * @todo optimize
853
 * @todo move to DSPContext
854
 */
855
static void vc1_h_overlap(uint8_t* src, int stride)
856
{
857
    int i;
858
    int a, b, c, d;
859
    for(i = 0; i < 8; i++) {
860
        a = src[-2];
861
        b = src[-1];
862
        c = src[0];
863
        d = src[1];
864

    
865
        src[-2] = clip_uint8((7*a + d + 3) >> 3);
866
        src[-1] = clip_uint8((-a + 7*b + c + d + 3) >> 3);
867
        src[0] = clip_uint8((a + b + 7*c - d + 3) >> 3);
868
        src[1] = clip_uint8((a + 7*d + 3) >> 3);
869
        src += stride;
870
    }
871
}
872

    
873
/** Put block onto picture
874
 * @todo move to DSPContext
875
 */
876
static void vc1_put_block(VC1Context *v, DCTELEM block[6][64])
877
{
878
    uint8_t *Y;
879
    int ys, us, vs;
880
    DSPContext *dsp = &v->s.dsp;
881

    
882
    ys = v->s.current_picture.linesize[0];
883
    us = v->s.current_picture.linesize[1];
884
    vs = v->s.current_picture.linesize[2];
885
    Y = v->s.dest[0];
886

    
887
    dsp->put_pixels_clamped(block[0], Y, ys);
888
    dsp->put_pixels_clamped(block[1], Y + 8, ys);
889
    Y += ys * 8;
890
    dsp->put_pixels_clamped(block[2], Y, ys);
891
    dsp->put_pixels_clamped(block[3], Y + 8, ys);
892

    
893
    dsp->put_pixels_clamped(block[4], v->s.dest[1], us);
894
    dsp->put_pixels_clamped(block[5], v->s.dest[2], vs);
895
}
896

    
897
/* clip motion vector as specified in 8.3.6.5 */
898
#define CLIP_RANGE(mv, src, lim, bs)      \
899
    if(mv < -bs) mv = -bs - src * bs; \
900
    if(mv > lim) mv = lim - src * bs;
901

    
902
/** Do motion compensation over 1 macroblock
903
 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
904
 */
905
static void vc1_mc_1mv(VC1Context *v)
906
{
907
    MpegEncContext *s = &v->s;
908
    DSPContext *dsp = &v->s.dsp;
909
    uint8_t *srcY, *srcU, *srcV;
910
    int dxy, uvdxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
911

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

    
914
    mx = s->mv[0][0][0];
915
    my = s->mv[0][0][1];
916
    uvmx = (mx + ((mx & 3) == 3)) >> 1;
917
    uvmy = (my + ((my & 3) == 3)) >> 1;
918
    srcY = s->last_picture.data[0];
919
    srcU = s->last_picture.data[1];
920
    srcV = s->last_picture.data[2];
921

    
922
    src_x = s->mb_x * 16 + (mx >> 2);
923
    src_y = s->mb_y * 16 + (my >> 2);
924
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
925
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
926

    
927
    CLIP_RANGE(  src_x, s->mb_x, s->mb_width  * 16, 16);
928
    CLIP_RANGE(  src_y, s->mb_y, s->mb_height * 16, 16);
929
    CLIP_RANGE(uvsrc_x, s->mb_x, s->mb_width  *  8,  8);
930
    CLIP_RANGE(uvsrc_y, s->mb_y, s->mb_height *  8,  8);
931

    
932
    srcY += src_y * s->linesize + src_x;
933
    srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
934
    srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
935

    
936
    if((v->mv_mode == MV_PMODE_INTENSITY_COMP)
937
       || (unsigned)src_x > s->h_edge_pos - (mx&3) - 16
938
       || (unsigned)src_y > s->v_edge_pos - (my&3) - 16){
939
        uint8_t *uvbuf= s->edge_emu_buffer + 18 * s->linesize;
940

    
941
        ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 16+1, 16+1,
942
                            src_x, src_y, s->h_edge_pos, s->v_edge_pos);
943
        srcY = s->edge_emu_buffer;
944
        ff_emulated_edge_mc(uvbuf     , srcU, s->uvlinesize, 8+1, 8+1,
945
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
946
        ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
947
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
948
        srcU = uvbuf;
949
        srcV = uvbuf + 16;
950
        /* if we deal with intensity compensation we need to scale source blocks */
951
        if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
952
            int i, j;
953
            uint8_t *src, *src2;
954

    
955
            src = srcY;
956
            for(j = 0; j < 17; j++) {
957
                for(i = 0; i < 17; i++) src[i] = v->luty[src[i]];
958
                src += s->linesize;
959
            }
960
            src = srcU; src2 = srcV;
961
            for(j = 0; j < 9; j++) {
962
                for(i = 0; i < 9; i++) {
963
                    src[i] = v->lutuv[src[i]];
964
                    src2[i] = v->lutuv[src2[i]];
965
                }
966
                src += s->uvlinesize;
967
                src2 += s->uvlinesize;
968
            }
969
        }
970
    }
971

    
972
    if(v->fastuvmc) {
973
        uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
974
        uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
975
    }
976

    
977
    if(!s->quarter_sample) { // hpel mc
978
        mx >>= 1;
979
        my >>= 1;
980
        dxy = ((my & 1) << 1) | (mx & 1);
981

    
982
        if(!v->rnd)
983
            dsp->put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
984
        else
985
            dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
986
    } else {
987
        dxy = ((my & 3) << 2) | (mx & 3);
988

    
989
        if(!v->rnd)
990
            dsp->put_qpel_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize);
991
        else
992
            dsp->put_no_rnd_qpel_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize);
993
    }
994
    /* Chroma MC always uses qpel blilinear */
995
    uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
996
    if(!v->rnd){
997
        dsp->put_qpel_pixels_tab[1][uvdxy](s->dest[1], srcU, s->uvlinesize);
998
        dsp->put_qpel_pixels_tab[1][uvdxy](s->dest[2], srcV, s->uvlinesize);
999
    }else{
1000
        dsp->put_no_rnd_qpel_pixels_tab[1][uvdxy](s->dest[1], srcU, s->uvlinesize);
1001
        dsp->put_no_rnd_qpel_pixels_tab[1][uvdxy](s->dest[2], srcV, s->uvlinesize);
1002
    }
1003
}
1004

    
1005
/** Do motion compensation for 4-MV macroblock - luminance block
1006
 */
1007
static void vc1_mc_4mv_luma(VC1Context *v, int n)
1008
{
1009
    MpegEncContext *s = &v->s;
1010
    DSPContext *dsp = &v->s.dsp;
1011
    uint8_t *srcY;
1012
    int dxy, mx, my, src_x, src_y;
1013
    int off;
1014

    
1015
    if(!v->s.last_picture.data[0])return;
1016
    mx = s->mv[0][n][0];
1017
    my = s->mv[0][n][1];
1018
    srcY = s->last_picture.data[0];
1019

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

    
1022
    src_x = s->mb_x * 16 + (n&1) * 8 + (mx >> 2);
1023
    src_y = s->mb_y * 16 + (n&2) * 4 + (my >> 2);
1024

    
1025
    CLIP_RANGE(src_x, s->mb_x, s->mb_width  * 16, 16);
1026
    CLIP_RANGE(src_y, s->mb_y, s->mb_height * 16, 16);
1027

    
1028
    srcY += src_y * s->linesize + src_x;
1029

    
1030
    if((unsigned)src_x > s->h_edge_pos - (mx&3) - 16
1031
       || (unsigned)src_y > s->v_edge_pos - (my&3) - 16){
1032
        ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 16+1, 16+1,
1033
                            src_x, src_y, s->h_edge_pos, s->v_edge_pos);
1034
        srcY = s->edge_emu_buffer;
1035
    }
1036

    
1037
    if(!s->quarter_sample) { // hpel mc
1038
        mx >>= 1;
1039
        my >>= 1;
1040
        dxy = ((my & 1) << 1) | (mx & 1);
1041

    
1042
        if(!v->rnd)
1043
            dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
1044
        else
1045
            dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
1046
    } else {
1047
        dxy = ((my & 3) << 2) | (mx & 3);
1048

    
1049
        if(!v->rnd)
1050
            dsp->put_qpel_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize);
1051
        else
1052
            dsp->put_no_rnd_qpel_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize);
1053
    }
1054
}
1055

    
1056
static inline int median4(int a, int b, int c, int d)
1057
{
1058
    if(a < b) {
1059
        if(c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
1060
        else      return (FFMIN(b, c) + FFMAX(a, d)) / 2;
1061
    } else {
1062
        if(c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
1063
        else      return (FFMIN(a, c) + FFMAX(b, d)) / 2;
1064
    }
1065
}
1066

    
1067

    
1068
/** Do motion compensation for 4-MV macroblock - both chroma blocks
1069
 */
1070
static void vc1_mc_4mv_chroma(VC1Context *v)
1071
{
1072
    MpegEncContext *s = &v->s;
1073
    DSPContext *dsp = &v->s.dsp;
1074
    uint8_t *srcU, *srcV;
1075
    int uvdxy, uvmx, uvmy, uvsrc_x, uvsrc_y;
1076
    int i, idx, tx = 0, ty = 0;
1077
    int mvx[4], mvy[4], intra[4];
1078
    static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
1079

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

    
1082
    for(i = 0; i < 4; i++) {
1083
        mvx[i] = s->mv[0][i][0];
1084
        mvy[i] = s->mv[0][i][1];
1085
        intra[i] = v->mb_type[0][s->block_index[i]];
1086
    }
1087

    
1088
    /* calculate chroma MV vector from four luma MVs */
1089
    idx = (intra[3] << 3) | (intra[2] << 2) | (intra[1] << 1) | intra[0];
1090
    if(!idx) { // all blocks are inter
1091
        tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
1092
        ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
1093
    } else if(count[idx] == 1) { // 3 inter blocks
1094
        switch(idx) {
1095
        case 0x1:
1096
            tx = mid_pred(mvx[1], mvx[2], mvx[3]);
1097
            ty = mid_pred(mvy[1], mvy[2], mvy[3]);
1098
            break;
1099
        case 0x2:
1100
            tx = mid_pred(mvx[0], mvx[2], mvx[3]);
1101
            ty = mid_pred(mvy[0], mvy[2], mvy[3]);
1102
            break;
1103
        case 0x4:
1104
            tx = mid_pred(mvx[0], mvx[1], mvx[3]);
1105
            ty = mid_pred(mvy[0], mvy[1], mvy[3]);
1106
            break;
1107
        case 0x8:
1108
            tx = mid_pred(mvx[0], mvx[1], mvx[2]);
1109
            ty = mid_pred(mvy[0], mvy[1], mvy[2]);
1110
            break;
1111
        }
1112
    } else if(count[idx] == 2) {
1113
        int t1 = 0, t2 = 0;
1114
        for(i=0; i<3;i++) if(!intra[i]) {t1 = i; break;}
1115
        for(i= t1+1; i<4; i++)if(!intra[i]) {t2 = i; break;}
1116
        tx = (mvx[t1] + mvx[t2]) / 2;
1117
        ty = (mvy[t1] + mvy[t2]) / 2;
1118
    } else
1119
        return; //no need to do MC for inter blocks
1120

    
1121
    uvmx = (tx + ((tx&3) == 3)) >> 1;
1122
    uvmy = (ty + ((ty&3) == 3)) >> 1;
1123

    
1124
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1125
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1126

    
1127
    CLIP_RANGE(uvsrc_x, s->mb_x, s->mb_width  *  8,  8);
1128
    CLIP_RANGE(uvsrc_y, s->mb_y, s->mb_height *  8,  8);
1129
    srcU = s->last_picture.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
1130
    srcV = s->last_picture.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
1131
    if((unsigned)uvsrc_x > (s->h_edge_pos >> 1) - ((uvmx >> 1)&1) - 8
1132
       || (unsigned)uvsrc_y > (s->v_edge_pos >> 1) - ((uvmy >> 1)&1) - 8){
1133
        ff_emulated_edge_mc(s->edge_emu_buffer     , srcU, s->uvlinesize, 8+1, 8+1,
1134
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
1135
        ff_emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize, 8+1, 8+1,
1136
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
1137
        srcU = s->edge_emu_buffer;
1138
        srcV = s->edge_emu_buffer + 16;
1139
    }
1140

    
1141
    if(v->fastuvmc) {
1142
        uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
1143
        uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
1144
    }
1145

    
1146
    /* Chroma MC always uses qpel blilinear */
1147
    uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
1148
    if(!v->rnd){
1149
        dsp->put_qpel_pixels_tab[1][uvdxy](s->dest[1], srcU, s->uvlinesize);
1150
        dsp->put_qpel_pixels_tab[1][uvdxy](s->dest[2], srcV, s->uvlinesize);
1151
    }else{
1152
        dsp->put_no_rnd_qpel_pixels_tab[1][uvdxy](s->dest[1], srcU, s->uvlinesize);
1153
        dsp->put_no_rnd_qpel_pixels_tab[1][uvdxy](s->dest[2], srcV, s->uvlinesize);
1154
    }
1155
}
1156

    
1157
/**
1158
 * Decode Simple/Main Profiles sequence header
1159
 * @see Figure 7-8, p16-17
1160
 * @param avctx Codec context
1161
 * @param gb GetBit context initialized from Codec context extra_data
1162
 * @return Status
1163
 */
1164
static int decode_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
1165
{
1166
    VC1Context *v = avctx->priv_data;
1167

    
1168
    av_log(avctx, AV_LOG_INFO, "Header: %0X\n", show_bits(gb, 32));
1169
    v->profile = get_bits(gb, 2);
1170
    if (v->profile == 2)
1171
    {
1172
        av_log(avctx, AV_LOG_ERROR, "Profile value 2 is forbidden (and WMV3 Complex Profile is unsupported)\n");
1173
        return -1;
1174
    }
1175

    
1176
    if (v->profile == PROFILE_ADVANCED)
1177
    {
1178
        v->level = get_bits(gb, 3);
1179
        if(v->level >= 5)
1180
        {
1181
            av_log(avctx, AV_LOG_ERROR, "Reserved LEVEL %i\n",v->level);
1182
        }
1183
        v->chromaformat = get_bits(gb, 2);
1184
        if (v->chromaformat != 1)
1185
        {
1186
            av_log(avctx, AV_LOG_ERROR,
1187
                   "Only 4:2:0 chroma format supported\n");
1188
            return -1;
1189
        }
1190
    }
1191
    else
1192
    {
1193
        v->res_sm = get_bits(gb, 2); //reserved
1194
        if (v->res_sm)
1195
        {
1196
            av_log(avctx, AV_LOG_ERROR,
1197
                   "Reserved RES_SM=%i is forbidden\n", v->res_sm);
1198
            return -1;
1199
        }
1200
    }
1201

    
1202
    // (fps-2)/4 (->30)
1203
    v->frmrtq_postproc = get_bits(gb, 3); //common
1204
    // (bitrate-32kbps)/64kbps
1205
    v->bitrtq_postproc = get_bits(gb, 5); //common
1206
    v->s.loop_filter = get_bits(gb, 1); //common
1207
    if(v->s.loop_filter == 1 && v->profile == PROFILE_SIMPLE)
1208
    {
1209
        av_log(avctx, AV_LOG_ERROR,
1210
               "LOOPFILTER shell not be enabled in simple profile\n");
1211
    }
1212

    
1213
    if (v->profile < PROFILE_ADVANCED)
1214
    {
1215
        v->res_x8 = get_bits(gb, 1); //reserved
1216
        if (v->res_x8)
1217
        {
1218
            av_log(avctx, AV_LOG_ERROR,
1219
                   "1 for reserved RES_X8 is forbidden\n");
1220
            //return -1;
1221
        }
1222
        v->multires = get_bits(gb, 1);
1223
        v->res_fasttx = get_bits(gb, 1);
1224
        if (!v->res_fasttx)
1225
        {
1226
            av_log(avctx, AV_LOG_ERROR,
1227
                   "0 for reserved RES_FASTTX is forbidden\n");
1228
            //return -1;
1229
        }
1230
    }
1231

    
1232
    v->fastuvmc =  get_bits(gb, 1); //common
1233
    if (!v->profile && !v->fastuvmc)
1234
    {
1235
        av_log(avctx, AV_LOG_ERROR,
1236
               "FASTUVMC unavailable in Simple Profile\n");
1237
        return -1;
1238
    }
1239
    v->extended_mv =  get_bits(gb, 1); //common
1240
    if (!v->profile && v->extended_mv)
1241
    {
1242
        av_log(avctx, AV_LOG_ERROR,
1243
               "Extended MVs unavailable in Simple Profile\n");
1244
        return -1;
1245
    }
1246
    v->dquant =  get_bits(gb, 2); //common
1247
    v->vstransform =  get_bits(gb, 1); //common
1248

    
1249
    if (v->profile < PROFILE_ADVANCED)
1250
    {
1251
        v->res_transtab = get_bits(gb, 1);
1252
        if (v->res_transtab)
1253
        {
1254
            av_log(avctx, AV_LOG_ERROR,
1255
                   "1 for reserved RES_TRANSTAB is forbidden\n");
1256
            return -1;
1257
        }
1258
    }
1259

    
1260
    v->overlap = get_bits(gb, 1); //common
1261

    
1262
    if (v->profile < PROFILE_ADVANCED)
1263
    {
1264
        v->s.resync_marker = get_bits(gb, 1);
1265
        v->rangered = get_bits(gb, 1);
1266
        if (v->rangered && v->profile == PROFILE_SIMPLE)
1267
        {
1268
            av_log(avctx, AV_LOG_INFO,
1269
                   "RANGERED should be set to 0 in simple profile\n");
1270
        }
1271
    }
1272

    
1273
    v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
1274
    v->quantizer_mode = get_bits(gb, 2); //common
1275

    
1276
    if (v->profile < PROFILE_ADVANCED)
1277
    {
1278
        v->finterpflag = get_bits(gb, 1); //common
1279
        v->res_rtm_flag = get_bits(gb, 1); //reserved
1280
        if (!v->res_rtm_flag)
1281
        {
1282
            av_log(avctx, AV_LOG_ERROR,
1283
                   "0 for reserved RES_RTM_FLAG is forbidden\n");
1284
            //return -1;
1285
        }
1286
        av_log(avctx, AV_LOG_DEBUG,
1287
               "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
1288
               "LoopFilter=%i, MultiRes=%i, FastUVMC=%i, Extended MV=%i\n"
1289
               "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
1290
               "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
1291
               v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
1292
               v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
1293
               v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
1294
               v->dquant, v->quantizer_mode, avctx->max_b_frames
1295
               );
1296
        return 0;
1297
    }
1298
    return -1;
1299
}
1300

    
1301

    
1302
static int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
1303
{
1304
    int pqindex, lowquant, status;
1305

    
1306
    if(v->finterpflag) v->interpfrm = get_bits(gb, 1);
1307
    skip_bits(gb, 2); //framecnt unused
1308
    v->rangeredfrm = 0;
1309
    if (v->rangered) v->rangeredfrm = get_bits(gb, 1);
1310
    v->s.pict_type = get_bits(gb, 1);
1311
    if (v->s.avctx->max_b_frames) {
1312
        if (!v->s.pict_type) {
1313
            if (get_bits(gb, 1)) v->s.pict_type = I_TYPE;
1314
            else v->s.pict_type = B_TYPE;
1315
        } else v->s.pict_type = P_TYPE;
1316
    } else v->s.pict_type = v->s.pict_type ? P_TYPE : I_TYPE;
1317

    
1318
    if(v->s.pict_type == I_TYPE)
1319
        get_bits(gb, 7); // skip buffer fullness
1320

    
1321
    /* calculate RND */
1322
    if(v->s.pict_type == I_TYPE)
1323
        v->rnd = 1;
1324
    if(v->s.pict_type == P_TYPE)
1325
        v->rnd ^= 1;
1326

    
1327
    /* Quantizer stuff */
1328
    pqindex = get_bits(gb, 5);
1329
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1330
        v->pq = pquant_table[0][pqindex];
1331
    else
1332
        v->pq = pquant_table[v->quantizer_mode-1][pqindex];
1333

    
1334
    v->pquantizer = 1;
1335
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1336
        v->pquantizer = pqindex < 9;
1337
    if (v->quantizer_mode == QUANT_NON_UNIFORM)
1338
        v->pquantizer = 0;
1339
    v->pqindex = pqindex;
1340
    if (pqindex < 9) v->halfpq = get_bits(gb, 1);
1341
    else v->halfpq = 0;
1342
    if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
1343
        v->pquantizer = get_bits(gb, 1);
1344
    v->dquantfrm = 0;
1345
    if (v->extended_mv == 1) v->mvrange = get_prefix(gb, 0, 3);
1346
    v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1347
    v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
1348
    v->range_x = 1 << (v->k_x - 1);
1349
    v->range_y = 1 << (v->k_y - 1);
1350
    if (v->profile == PROFILE_ADVANCED)
1351
    {
1352
        if (v->postprocflag) v->postproc = get_bits(gb, 1);
1353
    }
1354
    else
1355
        if (v->multires && v->s.pict_type != B_TYPE) v->respic = get_bits(gb, 2);
1356

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

    
1360
    //TODO: complete parsing for P/B/BI frames
1361
    switch(v->s.pict_type) {
1362
    case P_TYPE:
1363
        if (v->pq < 5) v->tt_index = 0;
1364
        else if(v->pq < 13) v->tt_index = 1;
1365
        else v->tt_index = 2;
1366

    
1367
        lowquant = (v->pq > 12) ? 0 : 1;
1368
        v->mv_mode = mv_pmode_table[lowquant][get_prefix(gb, 1, 4)];
1369
        if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
1370
        {
1371
            int scale, shift, i;
1372
            v->mv_mode2 = mv_pmode_table2[lowquant][get_prefix(gb, 1, 3)];
1373
            v->lumscale = get_bits(gb, 6);
1374
            v->lumshift = get_bits(gb, 6);
1375
            /* fill lookup tables for intensity compensation */
1376
            if(!v->lumscale) {
1377
                scale = -64;
1378
                shift = (255 - v->lumshift * 2) << 6;
1379
                if(v->lumshift > 31)
1380
                    shift += 128 << 6;
1381
            } else {
1382
                scale = v->lumscale + 32;
1383
                if(v->lumshift > 31)
1384
                    shift = (v->lumshift - 64) << 6;
1385
                else
1386
                    shift = v->lumshift << 6;
1387
            }
1388
            for(i = 0; i < 256; i++) {
1389
                v->luty[i] = clip_uint8((scale * i + shift + 32) >> 6);
1390
                v->lutuv[i] = clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
1391
            }
1392
        }
1393
        if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
1394
            v->s.quarter_sample = 0;
1395
        else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1396
            if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
1397
                v->s.quarter_sample = 0;
1398
            else
1399
                v->s.quarter_sample = 1;
1400
        } else
1401
            v->s.quarter_sample = 1;
1402

    
1403
        if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
1404
                 v->mv_mode2 == MV_PMODE_MIXED_MV)
1405
                || v->mv_mode == MV_PMODE_MIXED_MV)
1406
        {
1407
            status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
1408
            if (status < 0) return -1;
1409
            av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
1410
                   "Imode: %i, Invert: %i\n", status>>1, status&1);
1411
        } else {
1412
            v->mv_type_is_raw = 0;
1413
            memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
1414
        }
1415
        status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1416
        if (status < 0) return -1;
1417
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1418
               "Imode: %i, Invert: %i\n", status>>1, status&1);
1419

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

    
1424
        if (v->dquant)
1425
        {
1426
            av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1427
            vop_dquant_decoding(v);
1428
        }
1429

    
1430
        v->ttfrm = 0; //FIXME Is that so ?
1431
        if (v->vstransform)
1432
        {
1433
            v->ttmbf = get_bits(gb, 1);
1434
            if (v->ttmbf)
1435
            {
1436
                v->ttfrm = ttfrm_to_tt[get_bits(gb, 2)];
1437
            }
1438
        }
1439
        break;
1440
    case B_TYPE:
1441
        break;
1442
    }
1443

    
1444
    /* AC Syntax */
1445
    v->c_ac_table_index = decode012(gb);
1446
    if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
1447
    {
1448
        v->y_ac_table_index = decode012(gb);
1449
    }
1450
    /* DC Syntax */
1451
    v->s.dc_table_index = get_bits(gb, 1);
1452

    
1453
    return 0;
1454
}
1455

    
1456
/***********************************************************************/
1457
/**
1458
 * @defgroup block VC-1 Block-level functions
1459
 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1460
 * @todo TODO: Integrate to MpegEncContext facilities
1461
 * @{
1462
 */
1463

    
1464
/**
1465
 * @def GET_MQUANT
1466
 * @brief Get macroblock-level quantizer scale
1467
 * @warning XXX: qdiff to the frame quant, not previous quant ?
1468
 * @fixme XXX: Don't know how to initialize mquant otherwise in last case
1469
 */
1470
#define GET_MQUANT()                                           \
1471
  if (v->dquantfrm)                                            \
1472
  {                                                            \
1473
    int edges = 0;                                             \
1474
    if (v->dqprofile == DQPROFILE_ALL_MBS)                     \
1475
    {                                                          \
1476
      if (v->dqbilevel)                                        \
1477
      {                                                        \
1478
        mquant = (get_bits(gb, 1)) ? v->altpq : v->pq;         \
1479
      }                                                        \
1480
      else                                                     \
1481
      {                                                        \
1482
        mqdiff = get_bits(gb, 3);                              \
1483
        if (mqdiff != 7) mquant = v->pq + mqdiff;              \
1484
        else mquant = get_bits(gb, 5);                         \
1485
      }                                                        \
1486
    }                                                          \
1487
    if(v->dqprofile == DQPROFILE_SINGLE_EDGE)                  \
1488
        edges = 1 << v->dqsbedge;                              \
1489
    else if(v->dqprofile == DQPROFILE_DOUBLE_EDGES)            \
1490
        edges = (3 << v->dqsbedge) % 15;                       \
1491
    else if(v->dqprofile == DQPROFILE_FOUR_EDGES)              \
1492
        edges = 15;                                            \
1493
    if((edges&1) && !s->mb_x)                                  \
1494
        mquant = v->altpq;                                     \
1495
    if((edges&2) && s->first_slice_line)                       \
1496
        mquant = v->altpq;                                     \
1497
    if((edges&4) && s->mb_x == (s->mb_width - 1))              \
1498
        mquant = v->altpq;                                     \
1499
    if((edges&8) && s->mb_y == (s->mb_height - 1))             \
1500
        mquant = v->altpq;                                     \
1501
  }
1502

    
1503
/**
1504
 * @def GET_MVDATA(_dmv_x, _dmv_y)
1505
 * @brief Get MV differentials
1506
 * @see MVDATA decoding from 8.3.5.2, p(1)20
1507
 * @param _dmv_x Horizontal differential for decoded MV
1508
 * @param _dmv_y Vertical differential for decoded MV
1509
 * @todo TODO: Use MpegEncContext arrays to store them
1510
 */
1511
#define GET_MVDATA(_dmv_x, _dmv_y)                                  \
1512
  index = 1 + get_vlc2(gb, vc1_mv_diff_vlc[s->mv_table_index].table,\
1513
                       VC1_MV_DIFF_VLC_BITS, 2);                    \
1514
  if (index > 36)                                                   \
1515
  {                                                                 \
1516
    mb_has_coeffs = 1;                                              \
1517
    index -= 37;                                                    \
1518
  }                                                                 \
1519
  else mb_has_coeffs = 0;                                           \
1520
  s->mb_intra = 0;                                                  \
1521
  if (!index) { _dmv_x = _dmv_y = 0; }                              \
1522
  else if (index == 35)                                             \
1523
  {                                                                 \
1524
    _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample);          \
1525
    _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample);          \
1526
  }                                                                 \
1527
  else if (index == 36)                                             \
1528
  {                                                                 \
1529
    _dmv_x = 0;                                                     \
1530
    _dmv_y = 0;                                                     \
1531
    s->mb_intra = 1;                                                \
1532
  }                                                                 \
1533
  else                                                              \
1534
  {                                                                 \
1535
    index1 = index%6;                                               \
1536
    if (!s->quarter_sample && index1 == 5) val = 1;                 \
1537
    else                                   val = 0;                 \
1538
    if(size_table[index1] - val > 0)                                \
1539
        val = get_bits(gb, size_table[index1] - val);               \
1540
    else                                   val = 0;                 \
1541
    sign = 0 - (val&1);                                             \
1542
    _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign;     \
1543
                                                                    \
1544
    index1 = index/6;                                               \
1545
    if (!s->quarter_sample && index1 == 5) val = 1;                 \
1546
    else                                   val = 0;                 \
1547
    if(size_table[index1] - val > 0)                                \
1548
        val = get_bits(gb, size_table[index1] - val);               \
1549
    else                                   val = 0;                 \
1550
    sign = 0 - (val&1);                                             \
1551
    _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign;     \
1552
  }
1553

    
1554
/** Predict and set motion vector
1555
 */
1556
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)
1557
{
1558
    int xy, wrap, off = 0;
1559
    int16_t *A, *B, *C;
1560
    int px, py;
1561
    int sum;
1562

    
1563
    /* scale MV difference to be quad-pel */
1564
    dmv_x <<= 1 - s->quarter_sample;
1565
    dmv_y <<= 1 - s->quarter_sample;
1566

    
1567
    wrap = s->b8_stride;
1568
    xy = s->block_index[n];
1569

    
1570
    if(s->mb_intra){
1571
        s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
1572
        s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
1573
        if(mv1) { /* duplicate motion data for 1-MV block */
1574
            s->current_picture.motion_val[0][xy + 1][0] = 0;
1575
            s->current_picture.motion_val[0][xy + 1][1] = 0;
1576
            s->current_picture.motion_val[0][xy + wrap][0] = 0;
1577
            s->current_picture.motion_val[0][xy + wrap][1] = 0;
1578
            s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
1579
            s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
1580
        }
1581
        return;
1582
    }
1583

    
1584
    C = s->current_picture.motion_val[0][xy - 1];
1585
    A = s->current_picture.motion_val[0][xy - wrap];
1586
    if(mv1)
1587
        off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1588
    else {
1589
        //in 4-MV mode different blocks have different B predictor position
1590
        switch(n){
1591
        case 0:
1592
            off = (s->mb_x > 0) ? -1 : 1;
1593
            break;
1594
        case 1:
1595
            off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1596
            break;
1597
        case 2:
1598
            off = 1;
1599
            break;
1600
        case 3:
1601
            off = -1;
1602
        }
1603
    }
1604
    B = s->current_picture.motion_val[0][xy - wrap + off];
1605

    
1606
    if(!s->first_slice_line || (n==2 || n==3)) { // predictor A is not out of bounds
1607
        if(s->mb_width == 1) {
1608
            px = A[0];
1609
            py = A[1];
1610
        } else {
1611
            px = mid_pred(A[0], B[0], C[0]);
1612
            py = mid_pred(A[1], B[1], C[1]);
1613
        }
1614
    } else if(s->mb_x || (n==1 || n==3)) { // predictor C is not out of bounds
1615
        px = C[0];
1616
        py = C[1];
1617
    } else {
1618
        px = py = 0;
1619
    }
1620
    /* Pullback MV as specified in 8.3.5.3.4 */
1621
    {
1622
        int qx, qy, X, Y;
1623
        qx = (s->mb_x << 6) + ((n==1 || n==3) ? 32 : 0);
1624
        qy = (s->mb_y << 6) + ((n==2 || n==3) ? 32 : 0);
1625
        X = (s->mb_width << 6) - 4;
1626
        Y = (s->mb_height << 6) - 4;
1627
        if(mv1) {
1628
            if(qx + px < -60) px = -60 - qx;
1629
            if(qy + py < -60) py = -60 - qy;
1630
        } else {
1631
            if(qx + px < -28) px = -28 - qx;
1632
            if(qy + py < -28) py = -28 - qy;
1633
        }
1634
        if(qx + px > X) px = X - qx;
1635
        if(qy + py > Y) py = Y - qy;
1636
    }
1637
    /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
1638
    if((!s->first_slice_line || (n==2 || n==3)) && (s->mb_x || (n==1 || n==3))) {
1639
        if(is_intra[xy - wrap])
1640
            sum = ABS(px) + ABS(py);
1641
        else
1642
            sum = ABS(px - A[0]) + ABS(py - A[1]);
1643
        if(sum > 32) {
1644
            if(get_bits1(&s->gb)) {
1645
                px = A[0];
1646
                py = A[1];
1647
            } else {
1648
                px = C[0];
1649
                py = C[1];
1650
            }
1651
        } else {
1652
            if(is_intra[xy - 1])
1653
                sum = ABS(px) + ABS(py);
1654
            else
1655
                sum = ABS(px - C[0]) + ABS(py - C[1]);
1656
            if(sum > 32) {
1657
                if(get_bits1(&s->gb)) {
1658
                    px = A[0];
1659
                    py = A[1];
1660
                } else {
1661
                    px = C[0];
1662
                    py = C[1];
1663
                }
1664
            }
1665
        }
1666
    }
1667
    /* store MV using signed modulus of MV range defined in 4.11 */
1668
    s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1669
    s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
1670
    if(mv1) { /* duplicate motion data for 1-MV block */
1671
        s->current_picture.motion_val[0][xy + 1][0] = s->current_picture.motion_val[0][xy][0];
1672
        s->current_picture.motion_val[0][xy + 1][1] = s->current_picture.motion_val[0][xy][1];
1673
        s->current_picture.motion_val[0][xy + wrap][0] = s->current_picture.motion_val[0][xy][0];
1674
        s->current_picture.motion_val[0][xy + wrap][1] = s->current_picture.motion_val[0][xy][1];
1675
        s->current_picture.motion_val[0][xy + wrap + 1][0] = s->current_picture.motion_val[0][xy][0];
1676
        s->current_picture.motion_val[0][xy + wrap + 1][1] = s->current_picture.motion_val[0][xy][1];
1677
    }
1678
}
1679

    
1680
/** Get predicted DC value for I-frames only
1681
 * prediction dir: left=0, top=1
1682
 * @param s MpegEncContext
1683
 * @param[in] n block index in the current MB
1684
 * @param dc_val_ptr Pointer to DC predictor
1685
 * @param dir_ptr Prediction direction for use in AC prediction
1686
 */
1687
static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
1688
                              int16_t **dc_val_ptr, int *dir_ptr)
1689
{
1690
    int a, b, c, wrap, pred, scale;
1691
    int16_t *dc_val;
1692
    static const uint16_t dcpred[32] = {
1693
    -1, 1024,  512,  341,  256,  205,  171,  146,  128,
1694
         114,  102,   93,   85,   79,   73,   68,   64,
1695
          60,   57,   54,   51,   49,   47,   45,   43,
1696
          41,   39,   38,   37,   35,   34,   33
1697
    };
1698

    
1699
    /* find prediction - wmv3_dc_scale always used here in fact */
1700
    if (n < 4)     scale = s->y_dc_scale;
1701
    else           scale = s->c_dc_scale;
1702

    
1703
    wrap = s->block_wrap[n];
1704
    dc_val= s->dc_val[0] + s->block_index[n];
1705

    
1706
    /* B A
1707
     * C X
1708
     */
1709
    c = dc_val[ - 1];
1710
    b = dc_val[ - 1 - wrap];
1711
    a = dc_val[ - wrap];
1712

    
1713
    if (pq < 9 || !overlap)
1714
    {
1715
        /* Set outer values */
1716
        if (s->first_slice_line && (n!=2 && n!=3)) b=a=dcpred[scale];
1717
        if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=dcpred[scale];
1718
    }
1719
    else
1720
    {
1721
        /* Set outer values */
1722
        if (s->first_slice_line && (n!=2 && n!=3)) b=a=0;
1723
        if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=0;
1724
    }
1725

    
1726
    if (abs(a - b) <= abs(b - c)) {
1727
        pred = c;
1728
        *dir_ptr = 1;//left
1729
    } else {
1730
        pred = a;
1731
        *dir_ptr = 0;//top
1732
    }
1733

    
1734
    /* update predictor */
1735
    *dc_val_ptr = &dc_val[0];
1736
    return pred;
1737
}
1738

    
1739

    
1740
/** Get predicted DC value
1741
 * prediction dir: left=0, top=1
1742
 * @param s MpegEncContext
1743
 * @param[in] n block index in the current MB
1744
 * @param dc_val_ptr Pointer to DC predictor
1745
 * @param dir_ptr Prediction direction for use in AC prediction
1746
 */
1747
static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
1748
                              int a_avail, int c_avail,
1749
                              int16_t **dc_val_ptr, int *dir_ptr)
1750
{
1751
    int a, b, c, wrap, pred, scale;
1752
    int16_t *dc_val;
1753
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1754
    int q1, q2 = 0;
1755

    
1756
    /* find prediction - wmv3_dc_scale always used here in fact */
1757
    if (n < 4)     scale = s->y_dc_scale;
1758
    else           scale = s->c_dc_scale;
1759

    
1760
    wrap = s->block_wrap[n];
1761
    dc_val= s->dc_val[0] + s->block_index[n];
1762

    
1763
    /* B A
1764
     * C X
1765
     */
1766
    c = dc_val[ - 1];
1767
    b = dc_val[ - 1 - wrap];
1768
    a = dc_val[ - wrap];
1769

    
1770
    if(a_avail && c_avail) {
1771
        if(abs(a - b) <= abs(b - c)) {
1772
            pred = c;
1773
            *dir_ptr = 1;//left
1774
            q2 = s->current_picture.qscale_table[mb_pos - 1];
1775
        } else {
1776
            pred = a;
1777
            *dir_ptr = 0;//top
1778
            q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
1779
        }
1780
    } else if(a_avail) {
1781
        pred = a;
1782
        *dir_ptr = 0;//top
1783
        q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
1784
    } else if(c_avail) {
1785
        pred = c;
1786
        *dir_ptr = 1;//left
1787
        q2 = s->current_picture.qscale_table[mb_pos - 1];
1788
    } else {
1789
        pred = 0;
1790
        *dir_ptr = 1;//left
1791
    }
1792

    
1793
    /* scale coeffs if needed */
1794
    q1 = s->current_picture.qscale_table[mb_pos];
1795
    if(n && n<4) q2=q1;
1796

    
1797
    if(q2 && q1!=q2) {
1798
        pred = (pred * s->y_dc_scale_table[q2] * vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
1799
    }
1800

    
1801
    /* update predictor */
1802
    *dc_val_ptr = &dc_val[0];
1803
    return pred;
1804
}
1805

    
1806

    
1807
/**
1808
 * @defgroup std_mb VC1 Macroblock-level functions in Simple/Main Profiles
1809
 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1810
 * @todo TODO: Integrate to MpegEncContext facilities
1811
 * @{
1812
 */
1813

    
1814
static inline int vc1_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
1815
{
1816
    int xy, wrap, pred, a, b, c;
1817

    
1818
    xy = s->block_index[n];
1819
    wrap = s->b8_stride;
1820

    
1821
    /* B C
1822
     * A X
1823
     */
1824
    a = s->coded_block[xy - 1       ];
1825
    b = s->coded_block[xy - 1 - wrap];
1826
    c = s->coded_block[xy     - wrap];
1827

    
1828
    if (b == c) {
1829
        pred = a;
1830
    } else {
1831
        pred = c;
1832
    }
1833

    
1834
    /* store value */
1835
    *coded_block_ptr = &s->coded_block[xy];
1836

    
1837
    return pred;
1838
}
1839

    
1840
/**
1841
 * Decode one AC coefficient
1842
 * @param v The VC1 context
1843
 * @param last Last coefficient
1844
 * @param skip How much zero coefficients to skip
1845
 * @param value Decoded AC coefficient value
1846
 * @see 8.1.3.4
1847
 */
1848
static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip, int *value, int codingset)
1849
{
1850
    GetBitContext *gb = &v->s.gb;
1851
    int index, escape, run = 0, level = 0, lst = 0;
1852

    
1853
    index = get_vlc2(gb, vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
1854
    if (index != vc1_ac_sizes[codingset] - 1) {
1855
        run = vc1_index_decode_table[codingset][index][0];
1856
        level = vc1_index_decode_table[codingset][index][1];
1857
        lst = index >= vc1_last_decode_table[codingset];
1858
        if(get_bits(gb, 1))
1859
            level = -level;
1860
    } else {
1861
        escape = decode210(gb);
1862
        if (escape != 2) {
1863
            index = get_vlc2(gb, vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
1864
            run = vc1_index_decode_table[codingset][index][0];
1865
            level = vc1_index_decode_table[codingset][index][1];
1866
            lst = index >= vc1_last_decode_table[codingset];
1867
            if(escape == 0) {
1868
                if(lst)
1869
                    level += vc1_last_delta_level_table[codingset][run];
1870
                else
1871
                    level += vc1_delta_level_table[codingset][run];
1872
            } else {
1873
                if(lst)
1874
                    run += vc1_last_delta_run_table[codingset][level] + 1;
1875
                else
1876
                    run += vc1_delta_run_table[codingset][level] + 1;
1877
            }
1878
            if(get_bits(gb, 1))
1879
                level = -level;
1880
        } else {
1881
            int sign;
1882
            lst = get_bits(gb, 1);
1883
            if(v->s.esc3_level_length == 0) {
1884
                if(v->pq < 8 || v->dquantfrm) { // table 59
1885
                    v->s.esc3_level_length = get_bits(gb, 3);
1886
                    if(!v->s.esc3_level_length)
1887
                        v->s.esc3_level_length = get_bits(gb, 2) + 8;
1888
                } else { //table 60
1889
                    v->s.esc3_level_length = get_prefix(gb, 1, 6) + 2;
1890
                }
1891
                v->s.esc3_run_length = 3 + get_bits(gb, 2);
1892
            }
1893
            run = get_bits(gb, v->s.esc3_run_length);
1894
            sign = get_bits(gb, 1);
1895
            level = get_bits(gb, v->s.esc3_level_length);
1896
            if(sign)
1897
                level = -level;
1898
        }
1899
    }
1900

    
1901
    *last = lst;
1902
    *skip = run;
1903
    *value = level;
1904
}
1905

    
1906
/** Decode intra block in intra frames - should be faster than decode_intra_block
1907
 * @param v VC1Context
1908
 * @param block block to decode
1909
 * @param coded are AC coeffs present or not
1910
 * @param codingset set of VLC to decode data
1911
 */
1912
static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset)
1913
{
1914
    GetBitContext *gb = &v->s.gb;
1915
    MpegEncContext *s = &v->s;
1916
    int dc_pred_dir = 0; /* Direction of the DC prediction used */
1917
    int run_diff, i;
1918
    int16_t *dc_val;
1919
    int16_t *ac_val, *ac_val2;
1920
    int dcdiff;
1921

    
1922
    /* Get DC differential */
1923
    if (n < 4) {
1924
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1925
    } else {
1926
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1927
    }
1928
    if (dcdiff < 0){
1929
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
1930
        return -1;
1931
    }
1932
    if (dcdiff)
1933
    {
1934
        if (dcdiff == 119 /* ESC index value */)
1935
        {
1936
            /* TODO: Optimize */
1937
            if (v->pq == 1) dcdiff = get_bits(gb, 10);
1938
            else if (v->pq == 2) dcdiff = get_bits(gb, 9);
1939
            else dcdiff = get_bits(gb, 8);
1940
        }
1941
        else
1942
        {
1943
            if (v->pq == 1)
1944
                dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
1945
            else if (v->pq == 2)
1946
                dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
1947
        }
1948
        if (get_bits(gb, 1))
1949
            dcdiff = -dcdiff;
1950
    }
1951

    
1952
    /* Prediction */
1953
    dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
1954
    *dc_val = dcdiff;
1955

    
1956
    /* Store the quantized DC coeff, used for prediction */
1957
    if (n < 4) {
1958
        block[0] = dcdiff * s->y_dc_scale;
1959
    } else {
1960
        block[0] = dcdiff * s->c_dc_scale;
1961
    }
1962
    /* Skip ? */
1963
    run_diff = 0;
1964
    i = 0;
1965
    if (!coded) {
1966
        goto not_coded;
1967
    }
1968

    
1969
    //AC Decoding
1970
    i = 1;
1971

    
1972
    {
1973
        int last = 0, skip, value;
1974
        const int8_t *zz_table;
1975
        int scale;
1976
        int k;
1977

    
1978
        scale = v->pq * 2 + v->halfpq;
1979

    
1980
        if(v->s.ac_pred) {
1981
            if(!dc_pred_dir)
1982
                zz_table = vc1_horizontal_zz;
1983
            else
1984
                zz_table = vc1_vertical_zz;
1985
        } else
1986
            zz_table = vc1_normal_zz;
1987

    
1988
        ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1989
        ac_val2 = ac_val;
1990
        if(dc_pred_dir) //left
1991
            ac_val -= 16;
1992
        else //top
1993
            ac_val -= 16 * s->block_wrap[n];
1994

    
1995
        while (!last) {
1996
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1997
            i += skip;
1998
            if(i > 63)
1999
                break;
2000
            block[zz_table[i++]] = value;
2001
        }
2002

    
2003
        /* apply AC prediction if needed */
2004
        if(s->ac_pred) {
2005
            if(dc_pred_dir) { //left
2006
                for(k = 1; k < 8; k++)
2007
                    block[k << 3] += ac_val[k];
2008
            } else { //top
2009
                for(k = 1; k < 8; k++)
2010
                    block[k] += ac_val[k + 8];
2011
            }
2012
        }
2013
        /* save AC coeffs for further prediction */
2014
        for(k = 1; k < 8; k++) {
2015
            ac_val2[k] = block[k << 3];
2016
            ac_val2[k + 8] = block[k];
2017
        }
2018

    
2019
        /* scale AC coeffs */
2020
        for(k = 1; k < 64; k++)
2021
            if(block[k]) {
2022
                block[k] *= scale;
2023
                if(!v->pquantizer)
2024
                    block[k] += (block[k] < 0) ? -v->pq : v->pq;
2025
            }
2026

    
2027
        if(s->ac_pred) i = 63;
2028
    }
2029

    
2030
not_coded:
2031
    if(!coded) {
2032
        int k, scale;
2033
        ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2034
        ac_val2 = ac_val;
2035

    
2036
        scale = v->pq * 2 + v->halfpq;
2037
        memset(ac_val2, 0, 16 * 2);
2038
        if(dc_pred_dir) {//left
2039
            ac_val -= 16;
2040
            if(s->ac_pred)
2041
                memcpy(ac_val2, ac_val, 8 * 2);
2042
        } else {//top
2043
            ac_val -= 16 * s->block_wrap[n];
2044
            if(s->ac_pred)
2045
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2046
        }
2047

    
2048
        /* apply AC prediction if needed */
2049
        if(s->ac_pred) {
2050
            if(dc_pred_dir) { //left
2051
                for(k = 1; k < 8; k++) {
2052
                    block[k << 3] = ac_val[k] * scale;
2053
                    if(!v->pquantizer)
2054
                        block[k << 3] += (block[k << 3] < 0) ? -v->pq : v->pq;
2055
                }
2056
            } else { //top
2057
                for(k = 1; k < 8; k++) {
2058
                    block[k] = ac_val[k + 8] * scale;
2059
                    if(!v->pquantizer)
2060
                        block[k] += (block[k] < 0) ? -v->pq : v->pq;
2061
                }
2062
            }
2063
            i = 63;
2064
        }
2065
    }
2066
    s->block_last_index[n] = i;
2067

    
2068
    return 0;
2069
}
2070

    
2071
/** Decode intra block in inter frames - more generic version than vc1_decode_i_block
2072
 * @param v VC1Context
2073
 * @param block block to decode
2074
 * @param coded are AC coeffs present or not
2075
 * @param mquant block quantizer
2076
 * @param codingset set of VLC to decode data
2077
 */
2078
static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n, int coded, int mquant, int codingset)
2079
{
2080
    GetBitContext *gb = &v->s.gb;
2081
    MpegEncContext *s = &v->s;
2082
    int dc_pred_dir = 0; /* Direction of the DC prediction used */
2083
    int run_diff, i;
2084
    int16_t *dc_val;
2085
    int16_t *ac_val, *ac_val2;
2086
    int dcdiff;
2087
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2088
    int a_avail = v->a_avail, c_avail = v->c_avail;
2089
    int use_pred = s->ac_pred;
2090
    int scale;
2091
    int q1, q2 = 0;
2092

    
2093
    /* XXX: Guard against dumb values of mquant */
2094
    mquant = (mquant < 1) ? 0 : ( (mquant>31) ? 31 : mquant );
2095

    
2096
    /* Set DC scale - y and c use the same */
2097
    s->y_dc_scale = s->y_dc_scale_table[mquant];
2098
    s->c_dc_scale = s->c_dc_scale_table[mquant];
2099

    
2100
    /* Get DC differential */
2101
    if (n < 4) {
2102
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2103
    } else {
2104
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2105
    }
2106
    if (dcdiff < 0){
2107
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2108
        return -1;
2109
    }
2110
    if (dcdiff)
2111
    {
2112
        if (dcdiff == 119 /* ESC index value */)
2113
        {
2114
            /* TODO: Optimize */
2115
            if (mquant == 1) dcdiff = get_bits(gb, 10);
2116
            else if (mquant == 2) dcdiff = get_bits(gb, 9);
2117
            else dcdiff = get_bits(gb, 8);
2118
        }
2119
        else
2120
        {
2121
            if (mquant == 1)
2122
                dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
2123
            else if (mquant == 2)
2124
                dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
2125
        }
2126
        if (get_bits(gb, 1))
2127
            dcdiff = -dcdiff;
2128
    }
2129

    
2130
    /* Prediction */
2131
    dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
2132
    *dc_val = dcdiff;
2133

    
2134
    /* Store the quantized DC coeff, used for prediction */
2135

    
2136
    if (n < 4) {
2137
        block[0] = dcdiff * s->y_dc_scale;
2138
    } else {
2139
        block[0] = dcdiff * s->c_dc_scale;
2140
    }
2141
    /* Skip ? */
2142
    run_diff = 0;
2143
    i = 0;
2144

    
2145
    //AC Decoding
2146
    i = 1;
2147

    
2148
    /* check if AC is needed at all and adjust direction if needed */
2149
    if(!a_avail) dc_pred_dir = 1;
2150
    if(!c_avail) dc_pred_dir = 0;
2151
    if(!a_avail && !c_avail) use_pred = 0;
2152
    ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2153
    ac_val2 = ac_val;
2154

    
2155
    scale = mquant * 2 + v->halfpq;
2156

    
2157
    if(dc_pred_dir) //left
2158
        ac_val -= 16;
2159
    else //top
2160
        ac_val -= 16 * s->block_wrap[n];
2161

    
2162
    q1 = s->current_picture.qscale_table[mb_pos];
2163
    if(dc_pred_dir && c_avail) q2 = s->current_picture.qscale_table[mb_pos - 1];
2164
    if(!dc_pred_dir && a_avail) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2165
    if(n && n<4) q2 = q1;
2166

    
2167
    if(coded) {
2168
        int last = 0, skip, value;
2169
        const int8_t *zz_table;
2170
        int k;
2171

    
2172
        zz_table = vc1_simple_progressive_8x8_zz;
2173

    
2174
        while (!last) {
2175
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2176
            i += skip;
2177
            if(i > 63)
2178
                break;
2179
            block[zz_table[i++]] = value;
2180
        }
2181

    
2182
        /* apply AC prediction if needed */
2183
        if(use_pred) {
2184
            /* scale predictors if needed*/
2185
            if(q2 && q1!=q2) {
2186
                q1 = q1 * 2 - 1;
2187
                q2 = q2 * 2 - 1;
2188

    
2189
                if(dc_pred_dir) { //left
2190
                    for(k = 1; k < 8; k++)
2191
                        block[k << 3] += (ac_val[k] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2192
                } else { //top
2193
                    for(k = 1; k < 8; k++)
2194
                        block[k] += (ac_val[k + 8] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2195
                }
2196
            } else {
2197
                if(dc_pred_dir) { //left
2198
                    for(k = 1; k < 8; k++)
2199
                        block[k << 3] += ac_val[k];
2200
                } else { //top
2201
                    for(k = 1; k < 8; k++)
2202
                        block[k] += ac_val[k + 8];
2203
                }
2204
            }
2205
        }
2206
        /* save AC coeffs for further prediction */
2207
        for(k = 1; k < 8; k++) {
2208
            ac_val2[k] = block[k << 3];
2209
            ac_val2[k + 8] = block[k];
2210
        }
2211

    
2212
        /* scale AC coeffs */
2213
        for(k = 1; k < 64; k++)
2214
            if(block[k]) {
2215
                block[k] *= scale;
2216
                if(!v->pquantizer)
2217
                    block[k] += (block[k] < 0) ? -mquant : mquant;
2218
            }
2219

    
2220
        if(use_pred) i = 63;
2221
    } else { // no AC coeffs
2222
        int k;
2223

    
2224
        memset(ac_val2, 0, 16 * 2);
2225
        if(dc_pred_dir) {//left
2226
            if(use_pred) {
2227
                memcpy(ac_val2, ac_val, 8 * 2);
2228
                if(q2 && q1!=q2) {
2229
                    q1 = q1 * 2 - 1;
2230
                    q2 = q2 * 2 - 1;
2231
                    for(k = 1; k < 8; k++)
2232
                        ac_val2[k] = (ac_val2[k] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2233
                }
2234
            }
2235
        } else {//top
2236
            if(use_pred) {
2237
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2238
                if(q2 && q1!=q2) {
2239
                    q1 = q1 * 2 - 1;
2240
                    q2 = q2 * 2 - 1;
2241
                    for(k = 1; k < 8; k++)
2242
                        ac_val2[k + 8] = (ac_val2[k + 8] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2243
                }
2244
            }
2245
        }
2246

    
2247
        /* apply AC prediction if needed */
2248
        if(use_pred) {
2249
            if(dc_pred_dir) { //left
2250
                for(k = 1; k < 8; k++) {
2251
                    block[k << 3] = ac_val2[k] * scale;
2252
                    if(!v->pquantizer)
2253
                        block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
2254
                }
2255
            } else { //top
2256
                for(k = 1; k < 8; k++) {
2257
                    block[k] = ac_val2[k + 8] * scale;
2258
                    if(!v->pquantizer)
2259
                        block[k] += (block[k] < 0) ? -mquant : mquant;
2260
                }
2261
            }
2262
            i = 63;
2263
        }
2264
    }
2265
    s->block_last_index[n] = i;
2266

    
2267
    return 0;
2268
}
2269

    
2270
/** Decode P block
2271
 */
2272
static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n, int mquant, int ttmb, int first_block)
2273
{
2274
    MpegEncContext *s = &v->s;
2275
    GetBitContext *gb = &s->gb;
2276
    int i, j;
2277
    int subblkpat = 0;
2278
    int scale, off, idx, last, skip, value;
2279
    int ttblk = ttmb & 7;
2280

    
2281
    if(ttmb == -1) {
2282
        ttblk = ttblk_to_tt[v->tt_index][get_vlc2(gb, vc1_ttblk_vlc[v->tt_index].table, VC1_TTBLK_VLC_BITS, 1)];
2283
    }
2284
    if(ttblk == TT_4X4) {
2285
        subblkpat = ~(get_vlc2(gb, vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
2286
    }
2287
    if((ttblk != TT_8X8 && ttblk != TT_4X4) && (v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))) {
2288
        subblkpat = decode012(gb);
2289
        if(subblkpat) subblkpat ^= 3; //swap decoded pattern bits
2290
        if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) ttblk = TT_8X4;
2291
        if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) ttblk = TT_4X8;
2292
    }
2293
    scale = 2 * mquant + v->halfpq;
2294

    
2295
    // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
2296
    if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
2297
        subblkpat = 2 - (ttblk == TT_8X4_TOP);
2298
        ttblk = TT_8X4;
2299
    }
2300
    if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
2301
        subblkpat = 2 - (ttblk == TT_4X8_LEFT);
2302
        ttblk = TT_4X8;
2303
    }
2304
    switch(ttblk) {
2305
    case TT_8X8:
2306
        i = 0;
2307
        last = 0;
2308
        while (!last) {
2309
            vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2310
            i += skip;
2311
            if(i > 63)
2312
                break;
2313
            idx = vc1_simple_progressive_8x8_zz[i++];
2314
            block[idx] = value * scale;
2315
            if(!v->pquantizer)
2316
                block[idx] += (block[idx] < 0) ? -mquant : mquant;
2317
        }
2318
        vc1_inv_trans(block, 8, 8);
2319
        break;
2320
    case TT_4X4:
2321
        for(j = 0; j < 4; j++) {
2322
            last = subblkpat & (1 << (3 - j));
2323
            i = 0;
2324
            off = (j & 1) * 4 + (j & 2) * 16;
2325
            while (!last) {
2326
                vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2327
                i += skip;
2328
                if(i > 15)
2329
                    break;
2330
                idx = vc1_simple_progressive_4x4_zz[i++];
2331
                block[idx + off] = value * scale;
2332
                if(!v->pquantizer)
2333
                    block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
2334
            }
2335
            if(!(subblkpat & (1 << (3 - j))))
2336
                vc1_inv_trans(block + off, 4, 4);
2337
        }
2338
        break;
2339
    case TT_8X4:
2340
        for(j = 0; j < 2; j++) {
2341
            last = subblkpat & (1 << (1 - j));
2342
            i = 0;
2343
            off = j * 32;
2344
            while (!last) {
2345
                vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2346
                i += skip;
2347
                if(i > 31)
2348
                    break;
2349
                idx = vc1_simple_progressive_8x4_zz[i++];
2350
                block[idx + off] = value * scale;
2351
                if(!v->pquantizer)
2352
                    block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
2353
            }
2354
            if(!(subblkpat & (1 << (1 - j))))
2355
                vc1_inv_trans(block + off, 8, 4);
2356
        }
2357
        break;
2358
    case TT_4X8:
2359
        for(j = 0; j < 2; j++) {
2360
            last = subblkpat & (1 << (1 - j));
2361
            i = 0;
2362
            off = j * 4;
2363
            while (!last) {
2364
                vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2365
                i += skip;
2366
                if(i > 31)
2367
                    break;
2368
                idx = vc1_simple_progressive_4x8_zz[i++];
2369
                block[idx + off] = value * scale;
2370
                if(!v->pquantizer)
2371
                    block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
2372
            }
2373
            if(!(subblkpat & (1 << (1 - j))))
2374
                vc1_inv_trans(block + off, 4, 8);
2375
        }
2376
        break;
2377
    }
2378
    return 0;
2379
}
2380

    
2381

    
2382
/** Decode one P-frame MB (in Simple/Main profile)
2383
 * @todo TODO: Extend to AP
2384
 * @fixme FIXME: DC value for inter blocks not set
2385
 */
2386
static int vc1_decode_p_mb(VC1Context *v, DCTELEM block[6][64])
2387
{
2388
    MpegEncContext *s = &v->s;
2389
    GetBitContext *gb = &s->gb;
2390
    int i, j;
2391
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2392
    int cbp; /* cbp decoding stuff */
2393
    int mqdiff, mquant; /* MB quantization */
2394
    int ttmb = v->ttfrm; /* MB Transform type */
2395
    int status;
2396

    
2397
    static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
2398
      offset_table[6] = { 0, 1, 3, 7, 15, 31 };
2399
    int mb_has_coeffs = 1; /* last_flag */
2400
    int dmv_x, dmv_y; /* Differential MV components */
2401
    int index, index1; /* LUT indices */
2402
    int val, sign; /* temp values */
2403
    int first_block = 1;
2404
    int dst_idx, off;
2405
    int skipped, fourmv;
2406

    
2407
    mquant = v->pq; /* Loosy initialization */
2408

    
2409
    if (v->mv_type_is_raw)
2410
        fourmv = get_bits1(gb);
2411
    else
2412
        fourmv = v->mv_type_mb_plane[mb_pos];
2413
    if (v->skip_is_raw)
2414
        skipped = get_bits1(gb);
2415
    else
2416
        skipped = v->s.mbskip_table[mb_pos];
2417

    
2418
    s->dsp.clear_blocks(s->block[0]);
2419

    
2420
    if (!fourmv) /* 1MV mode */
2421
    {
2422
        if (!skipped)
2423
        {
2424
            GET_MVDATA(dmv_x, dmv_y);
2425

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

    
2429
            /* FIXME Set DC val for inter block ? */
2430
            if (s->mb_intra && !mb_has_coeffs)
2431
            {
2432
                GET_MQUANT();
2433
                s->ac_pred = get_bits(gb, 1);
2434
                cbp = 0;
2435
            }
2436
            else if (mb_has_coeffs)
2437
            {
2438
                if (s->mb_intra) s->ac_pred = get_bits(gb, 1);
2439
                cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2440
                GET_MQUANT();
2441
            }
2442
            else
2443
            {
2444
                mquant = v->pq;
2445
                cbp = 0;
2446
            }
2447
            s->current_picture.qscale_table[mb_pos] = mquant;
2448

    
2449
            if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
2450
                ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table,
2451
                                VC1_TTMB_VLC_BITS, 2);
2452
            if(!s->mb_intra) vc1_mc_1mv(v);
2453
            dst_idx = 0;
2454
            for (i=0; i<6; i++)
2455
            {
2456
                s->dc_val[0][s->block_index[i]] = 0;
2457
                dst_idx += i >> 2;
2458
                val = ((cbp >> (5 - i)) & 1);
2459
                off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2460
                v->mb_type[0][s->block_index[i]] = s->mb_intra;
2461
                if(s->mb_intra) {
2462
                    /* check if prediction blocks A and C are available */
2463
                    v->a_avail = v->c_avail = 0;
2464
                    if(i == 2 || i == 3 || !s->first_slice_line)
2465
                        v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2466
                    if(i == 1 || i == 3 || s->mb_x)
2467
                        v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2468

    
2469
                    vc1_decode_intra_block(v, block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
2470
                    vc1_inv_trans(block[i], 8, 8);
2471
                    for(j = 0; j < 64; j++) block[i][j] += 128;
2472
                    s->dsp.put_pixels_clamped(block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
2473
                    /* TODO: proper loop filtering */
2474
                    if(v->pq >= 9 && v->overlap) {
2475
                        if(v->a_avail)
2476
                            vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
2477
                        if(v->c_avail)
2478
                            vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
2479
                    }
2480
                } else if(val) {
2481
                    vc1_decode_p_block(v, block[i], i, mquant, ttmb, first_block);
2482
                    if(!v->ttmbf && ttmb < 8) ttmb = -1;
2483
                    first_block = 0;
2484
                    s->dsp.add_pixels_clamped(block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
2485
                }
2486
            }
2487
        }
2488
        else //Skipped
2489
        {
2490
            s->mb_intra = 0;
2491
            for(i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
2492
            s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
2493
            s->current_picture.qscale_table[mb_pos] = 0;
2494
            vc1_pred_mv(s, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);
2495
            vc1_mc_1mv(v);
2496
            return 0;
2497
        }
2498
    } //1MV mode
2499
    else //4MV mode
2500
    {
2501
        if (!skipped /* unskipped MB */)
2502
        {
2503
            int intra_count = 0, coded_inter = 0;
2504
            int is_intra[6], is_coded[6];
2505
            /* Get CBPCY */
2506
            cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2507
            for (i=0; i<6; i++)
2508
            {
2509
                val = ((cbp >> (5 - i)) & 1);
2510
                s->dc_val[0][s->block_index[i]] = 0;
2511
                s->mb_intra = 0;
2512
                if(i < 4) {
2513
                    dmv_x = dmv_y = 0;
2514
                    s->mb_intra = 0;
2515
                    mb_has_coeffs = 0;
2516
                    if(val) {
2517
                        GET_MVDATA(dmv_x, dmv_y);
2518
                    }
2519
                    vc1_pred_mv(s, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);
2520
                    if(!s->mb_intra) vc1_mc_4mv_luma(v, i);
2521
                    intra_count += s->mb_intra;
2522
                    is_intra[i] = s->mb_intra;
2523
                    is_coded[i] = mb_has_coeffs;
2524
                }
2525
                if(i&4){
2526
                    is_intra[i] = (intra_count >= 3);
2527
                    is_coded[i] = val;
2528
                }
2529
                if(i == 4) vc1_mc_4mv_chroma(v);
2530
                v->mb_type[0][s->block_index[i]] = is_intra[i];
2531
                if(!coded_inter) coded_inter = !is_intra[i] & is_coded[i];
2532
            }
2533
            // if there are no coded blocks then don't do anything more
2534
            if(!intra_count && !coded_inter) return 0;
2535
            dst_idx = 0;
2536
            GET_MQUANT();
2537
            s->current_picture.qscale_table[mb_pos] = mquant;
2538
            /* test if block is intra and has pred */
2539
            {
2540
                int intrapred = 0;
2541
                for(i=0; i<6; i++)
2542
                    if(is_intra[i]) {
2543
                        if(((!s->first_slice_line || (i==2 || i==3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
2544
                            || ((s->mb_x || (i==1 || i==3)) && v->mb_type[0][s->block_index[i] - 1])) {
2545
                            intrapred = 1;
2546
                            break;
2547
                        }
2548
                    }
2549
                if(intrapred)s->ac_pred = get_bits(gb, 1);
2550
                else s->ac_pred = 0;
2551
            }
2552
            if (!v->ttmbf && coded_inter)
2553
                ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 12);
2554
            for (i=0; i<6; i++)
2555
            {
2556
                dst_idx += i >> 2;
2557
                off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2558
                s->mb_intra = is_intra[i];
2559
                if (is_intra[i]) {
2560
                    /* check if prediction blocks A and C are available */
2561
                    v->a_avail = v->c_avail = 0;
2562
                    if(i == 2 || i == 3 || !s->first_slice_line)
2563
                        v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2564
                    if(i == 1 || i == 3 || s->mb_x)
2565
                        v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2566

    
2567
                    vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant, (i&4)?v->codingset2:v->codingset);
2568
                    vc1_inv_trans(block[i], 8, 8);
2569
                    for(j = 0; j < 64; j++) block[i][j] += 128;
2570
                    s->dsp.put_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
2571
                    /* TODO: proper loop filtering */
2572
                    if(v->pq >= 9 && v->overlap) {
2573
                        if(v->a_avail)
2574
                            vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
2575
                        if(v->c_avail)
2576
                            vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
2577
                    }
2578
                } else if(is_coded[i]) {
2579
                    status = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block);
2580
                    if(!v->ttmbf && ttmb < 8) ttmb = -1;
2581
                    first_block = 0;
2582
                    s->dsp.add_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
2583
                }
2584
            }
2585
            return status;
2586
        }
2587
        else //Skipped MB
2588
        {
2589
            s->mb_intra = 0;
2590
            for (i=0; i<6; i++) v->mb_type[0][s->block_index[i]] = 0;
2591
            for (i=0; i<4; i++)
2592
            {
2593
                vc1_pred_mv(s, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0]);
2594
                vc1_mc_4mv_luma(v, i);
2595
            }
2596
            vc1_mc_4mv_chroma(v);
2597
            s->current_picture.qscale_table[mb_pos] = 0;
2598
            return 0;
2599
        }
2600
    }
2601

    
2602
    /* Should never happen */
2603
    return -1;
2604
}
2605

    
2606
/** Decode blocks of I-frame
2607
 */
2608
static void vc1_decode_i_blocks(VC1Context *v)
2609
{
2610
    int k, j;
2611
    MpegEncContext *s = &v->s;
2612
    int cbp, val;
2613
    uint8_t *coded_val;
2614
    int mb_pos;
2615

    
2616
    /* select codingmode used for VLC tables selection */
2617
    switch(v->y_ac_table_index){
2618
    case 0:
2619
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2620
        break;
2621
    case 1:
2622
        v->codingset = CS_HIGH_MOT_INTRA;
2623
        break;
2624
    case 2:
2625
        v->codingset = CS_MID_RATE_INTRA;
2626
        break;
2627
    }
2628

    
2629
    switch(v->c_ac_table_index){
2630
    case 0:
2631
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2632
        break;
2633
    case 1:
2634
        v->codingset2 = CS_HIGH_MOT_INTER;
2635
        break;
2636
    case 2:
2637
        v->codingset2 = CS_MID_RATE_INTER;
2638
        break;
2639
    }
2640

    
2641
    /* Set DC scale - y and c use the same */
2642
    s->y_dc_scale = s->y_dc_scale_table[v->pq];
2643
    s->c_dc_scale = s->c_dc_scale_table[v->pq];
2644

    
2645
    //do frame decode
2646
    s->mb_x = s->mb_y = 0;
2647
    s->mb_intra = 1;
2648
    s->first_slice_line = 1;
2649
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2650
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2651
        for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
2652
            ff_init_block_index(s);
2653
            ff_update_block_index(s);
2654
            s->dsp.clear_blocks(s->block[0]);
2655
            mb_pos = s->mb_x + s->mb_y * s->mb_width;
2656
            s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2657
            s->current_picture.qscale_table[mb_pos] = v->pq;
2658

    
2659
            // do actual MB decoding and displaying
2660
            cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2661
            v->s.ac_pred = get_bits(&v->s.gb, 1);
2662

    
2663
            for(k = 0; k < 6; k++) {
2664
                val = ((cbp >> (5 - k)) & 1);
2665

    
2666
                if (k < 4) {
2667
                    int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2668
                    val = val ^ pred;
2669
                    *coded_val = val;
2670
                }
2671
                cbp |= val << (5 - k);
2672

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

    
2675
                vc1_inv_trans(s->block[k], 8, 8);
2676
                if(v->pq >= 9 && v->overlap) {
2677
                    for(j = 0; j < 64; j++) s->block[k][j] += 128;
2678
                }
2679
            }
2680

    
2681
            vc1_put_block(v, s->block);
2682
            if(v->pq >= 9 && v->overlap) { /* XXX: do proper overlapping insted of loop filter */
2683
                if(!s->first_slice_line) {
2684
                    vc1_v_overlap(s->dest[0], s->linesize);
2685
                    vc1_v_overlap(s->dest[0] + 8, s->linesize);
2686
                    vc1_v_overlap(s->dest[1], s->uvlinesize);
2687
                    vc1_v_overlap(s->dest[2], s->uvlinesize);
2688
                }
2689
                vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2690
                vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2691
                if(s->mb_x) {
2692
                    vc1_h_overlap(s->dest[0], s->linesize);
2693
                    vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2694
                    vc1_h_overlap(s->dest[1], s->uvlinesize);
2695
                    vc1_h_overlap(s->dest[2], s->uvlinesize);
2696
                }
2697
                vc1_h_overlap(s->dest[0] + 8, s->linesize);
2698
                vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2699
            }
2700

    
2701
            if(get_bits_count(&s->gb) > v->bits) {
2702
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
2703
                return;
2704
            }
2705
        }
2706
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
2707
        s->first_slice_line = 0;
2708
    }
2709
}
2710

    
2711
static void vc1_decode_p_blocks(VC1Context *v)
2712
{
2713
    MpegEncContext *s = &v->s;
2714

    
2715
    /* select codingmode used for VLC tables selection */
2716
    switch(v->c_ac_table_index){
2717
    case 0:
2718
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2719
        break;
2720
    case 1:
2721
        v->codingset = CS_HIGH_MOT_INTRA;
2722
        break;
2723
    case 2:
2724
        v->codingset = CS_MID_RATE_INTRA;
2725
        break;
2726
    }
2727

    
2728
    switch(v->c_ac_table_index){
2729
    case 0:
2730
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2731
        break;
2732
    case 1:
2733
        v->codingset2 = CS_HIGH_MOT_INTER;
2734
        break;
2735
    case 2:
2736
        v->codingset2 = CS_MID_RATE_INTER;
2737
        break;
2738
    }
2739

    
2740
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2741
    s->first_slice_line = 1;
2742
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2743
        for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
2744
            ff_init_block_index(s);
2745
            ff_update_block_index(s);
2746
            s->dsp.clear_blocks(s->block[0]);
2747

    
2748
            vc1_decode_p_mb(v, s->block);
2749
            if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2750
                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);
2751
                return;
2752
            }
2753
        }
2754
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
2755
        s->first_slice_line = 0;
2756
    }
2757
}
2758

    
2759
static void vc1_decode_blocks(VC1Context *v)
2760
{
2761

    
2762
    v->s.esc3_level_length = 0;
2763

    
2764
    switch(v->s.pict_type) {
2765
    case I_TYPE:
2766
        vc1_decode_i_blocks(v);
2767
        break;
2768
    case P_TYPE:
2769
        vc1_decode_p_blocks(v);
2770
        break;
2771
    }
2772
}
2773

    
2774

    
2775
/** Initialize a VC1/WMV3 decoder
2776
 * @todo TODO: Handle VC-1 IDUs (Transport level?)
2777
 * @todo TODO: Decypher remaining bits in extra_data
2778
 */
2779
static int vc1_decode_init(AVCodecContext *avctx)
2780
{
2781
    VC1Context *v = avctx->priv_data;
2782
    MpegEncContext *s = &v->s;
2783
    GetBitContext gb;
2784

    
2785
    if (!avctx->extradata_size || !avctx->extradata) return -1;
2786
    avctx->pix_fmt = PIX_FMT_YUV420P;
2787
    v->s.avctx = avctx;
2788
    avctx->flags |= CODEC_FLAG_EMU_EDGE;
2789
    v->s.flags |= CODEC_FLAG_EMU_EDGE;
2790

    
2791
    if(ff_h263_decode_init(avctx) < 0)
2792
        return -1;
2793
    if (vc1_init_common(v) < 0) return -1;
2794

    
2795
    av_log(avctx, AV_LOG_INFO, "This decoder is not supposed to produce picture. Dont report this as a bug!\n");
2796
    av_log(avctx, AV_LOG_INFO, "If you see a picture, don't believe your eyes.\n");
2797

    
2798
    avctx->coded_width = avctx->width;
2799
    avctx->coded_height = avctx->height;
2800
    if (avctx->codec_id == CODEC_ID_WMV3)
2801
    {
2802
        int count = 0;
2803

    
2804
        // looks like WMV3 has a sequence header stored in the extradata
2805
        // advanced sequence header may be before the first frame
2806
        // the last byte of the extradata is a version number, 1 for the
2807
        // samples we can decode
2808

    
2809
        init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
2810

    
2811
        if (decode_sequence_header(avctx, &gb) < 0)
2812
          return -1;
2813

    
2814
        count = avctx->extradata_size*8 - get_bits_count(&gb);
2815
        if (count>0)
2816
        {
2817
            av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
2818
                   count, get_bits(&gb, count));
2819
        }
2820
        else if (count < 0)
2821
        {
2822
            av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
2823
        }
2824
    }
2825
    avctx->has_b_frames= !!(avctx->max_b_frames);
2826

    
2827
    s->mb_width = (avctx->coded_width+15)>>4;
2828
    s->mb_height = (avctx->coded_height+15)>>4;
2829

    
2830
    /* Allocate mb bitplanes */
2831
    v->mv_type_mb_plane = av_malloc(s->mb_stride * s->mb_height);
2832

    
2833
    /* allocate block type info in that way so it could be used with s->block_index[] */
2834
    v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
2835
    v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
2836
    v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
2837
    v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
2838

    
2839
    /* Init coded blocks info */
2840
    if (v->profile == PROFILE_ADVANCED)
2841
    {
2842
//        if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
2843
//            return -1;
2844
//        if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
2845
//            return -1;
2846
    }
2847

    
2848
    return 0;
2849
}
2850

    
2851

    
2852
/** Decode a VC1/WMV3 frame
2853
 * @todo TODO: Handle VC-1 IDUs (Transport level?)
2854
 * @warning Initial try at using MpegEncContext stuff
2855
 */
2856
static int vc1_decode_frame(AVCodecContext *avctx,
2857
                            void *data, int *data_size,
2858
                            uint8_t *buf, int buf_size)
2859
{
2860
    VC1Context *v = avctx->priv_data;
2861
    MpegEncContext *s = &v->s;
2862
    AVFrame *pict = data;
2863

    
2864
    /* no supplementary picture */
2865
    if (buf_size == 0) {
2866
        /* special case for last picture */
2867
        if (s->low_delay==0 && s->next_picture_ptr) {
2868
            *pict= *(AVFrame*)s->next_picture_ptr;
2869
            s->next_picture_ptr= NULL;
2870

    
2871
            *data_size = sizeof(AVFrame);
2872
        }
2873

    
2874
        return 0;
2875
    }
2876

    
2877
    //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
2878
    if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
2879
        int i= ff_find_unused_picture(s, 0);
2880
        s->current_picture_ptr= &s->picture[i];
2881
    }
2882

    
2883
    avctx->has_b_frames= !s->low_delay;
2884

    
2885
    init_get_bits(&s->gb, buf, buf_size*8);
2886
    // do parse frame header
2887
    if(vc1_parse_frame_header(v, &s->gb) == -1)
2888
        return -1;
2889

    
2890
    if(s->pict_type != I_TYPE && s->pict_type != P_TYPE)return -1;
2891

    
2892
    // for hurry_up==5
2893
    s->current_picture.pict_type= s->pict_type;
2894
    s->current_picture.key_frame= s->pict_type == I_TYPE;
2895

    
2896
    /* skip B-frames if we don't have reference frames */
2897
    if(s->last_picture_ptr==NULL && (s->pict_type==B_TYPE || s->dropable)) return -1;//buf_size;
2898
    /* skip b frames if we are in a hurry */
2899
    if(avctx->hurry_up && s->pict_type==B_TYPE) return -1;//buf_size;
2900
    if(   (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==B_TYPE)
2901
       || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=I_TYPE)
2902
       ||  avctx->skip_frame >= AVDISCARD_ALL)
2903
        return buf_size;
2904
    /* skip everything if we are in a hurry>=5 */
2905
    if(avctx->hurry_up>=5) return -1;//buf_size;
2906

    
2907
    if(s->next_p_frame_damaged){
2908
        if(s->pict_type==B_TYPE)
2909
            return buf_size;
2910
        else
2911
            s->next_p_frame_damaged=0;
2912
    }
2913

    
2914
    if(MPV_frame_start(s, avctx) < 0)
2915
        return -1;
2916

    
2917
    ff_er_frame_start(s);
2918

    
2919
    v->bits = buf_size * 8;
2920
    vc1_decode_blocks(v);
2921
//av_log(s->avctx, AV_LOG_INFO, "Consumed %i/%i bits\n", get_bits_count(&s->gb), buf_size*8);
2922
//  if(get_bits_count(&s->gb) > buf_size * 8)
2923
//      return -1;
2924
    ff_er_frame_end(s);
2925

    
2926
    MPV_frame_end(s);
2927

    
2928
assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
2929
assert(s->current_picture.pict_type == s->pict_type);
2930
    if (s->pict_type == B_TYPE || s->low_delay) {
2931
        *pict= *(AVFrame*)s->current_picture_ptr;
2932
    } else if (s->last_picture_ptr != NULL) {
2933
        *pict= *(AVFrame*)s->last_picture_ptr;
2934
    }
2935

    
2936
    if(s->last_picture_ptr || s->low_delay){
2937
        *data_size = sizeof(AVFrame);
2938
        ff_print_debug_info(s, pict);
2939
    }
2940

    
2941
    /* Return the Picture timestamp as the frame number */
2942
    /* we substract 1 because it is added on utils.c    */
2943
    avctx->frame_number = s->picture_number - 1;
2944

    
2945
    return buf_size;
2946
}
2947

    
2948

    
2949
/** Close a VC1/WMV3 decoder
2950
 * @warning Initial try at using MpegEncContext stuff
2951
 */
2952
static int vc1_decode_end(AVCodecContext *avctx)
2953
{
2954
    VC1Context *v = avctx->priv_data;
2955

    
2956
    av_freep(&v->hrd_rate);
2957
    av_freep(&v->hrd_buffer);
2958
    MPV_common_end(&v->s);
2959
    av_freep(&v->mv_type_mb_plane);
2960
    av_freep(&v->mb_type_base);
2961
    return 0;
2962
}
2963

    
2964

    
2965
AVCodec vc1_decoder = {
2966
    "vc1",
2967
    CODEC_TYPE_VIDEO,
2968
    CODEC_ID_VC1,
2969
    sizeof(VC1Context),
2970
    vc1_decode_init,
2971
    NULL,
2972
    vc1_decode_end,
2973
    vc1_decode_frame,
2974
    CODEC_CAP_DELAY,
2975
    NULL
2976
};
2977

    
2978
AVCodec wmv3_decoder = {
2979
    "wmv3",
2980
    CODEC_TYPE_VIDEO,
2981
    CODEC_ID_WMV3,
2982
    sizeof(VC1Context),
2983
    vc1_decode_init,
2984
    NULL,
2985
    vc1_decode_end,
2986
    vc1_decode_frame,
2987
    CODEC_CAP_DELAY,
2988
    NULL
2989
};