Statistics
| Branch: | Revision:

ffmpeg / libavcodec / vc1.c @ 727c236a

History | View | Annotate | Download (114 KB)

1 21aa398f Alex Beregszaszi
/*
2 10b9c374 Kostya Shishkov
 * VC-1 and WMV3 decoder
3 be3492ec Kostya Shishkov
 * Copyright (c) 2006 Konstantin Shishkov
4
 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
5 21aa398f Alex Beregszaszi
 *
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 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 21aa398f Alex Beregszaszi
 *
20
 */
21
22
/**
23 10b9c374 Kostya Shishkov
 * @file vc1.c
24
 * VC-1 and WMV3 decoder
25 21aa398f Alex Beregszaszi
 *
26
 */
27
#include "common.h"
28
#include "dsputil.h"
29
#include "avcodec.h"
30
#include "mpegvideo.h"
31 10b9c374 Kostya Shishkov
#include "vc1data.h"
32 be3492ec Kostya Shishkov
#include "vc1acdata.h"
33 0d33db8a anonymous
34 9da235c8 Michael Niedermayer
#undef NDEBUG
35
#include <assert.h>
36
37 0c040aac anonymous
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 0d33db8a anonymous
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 be3492ec Kostya Shishkov
extern const uint16_t ff_msmp4_mb_i_table[64][2];
43 0d33db8a anonymous
#define DC_VLC_BITS 9
44 be3492ec Kostya Shishkov
#define AC_VLC_BITS 9
45 0d33db8a anonymous
static const uint16_t table_mb_intra[64][2];
46 21aa398f Alex Beregszaszi
47
48 2ce151f8 anonymous
/** Available Profiles */
49
//@{
50 be3492ec Kostya Shishkov
enum Profile {
51
    PROFILE_SIMPLE,
52
    PROFILE_MAIN,
53
    PROFILE_COMPLEX, ///< TODO: WMV9 specific
54
    PROFILE_ADVANCED
55
};
56 2ce151f8 anonymous
//@}
57 21aa398f Alex Beregszaszi
58 2ce151f8 anonymous
/** Sequence quantizer mode */
59
//@{
60 be3492ec Kostya Shishkov
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 2ce151f8 anonymous
//@}
67 21aa398f Alex Beregszaszi
68 2ce151f8 anonymous
/** Where quant can be changed */
69
//@{
70 be3492ec Kostya Shishkov
enum DQProfile {
71
    DQPROFILE_FOUR_EDGES,
72
    DQPROFILE_DOUBLE_EDGES,
73
    DQPROFILE_SINGLE_EDGE,
74
    DQPROFILE_ALL_MBS
75
};
76 2ce151f8 anonymous
//@}
77 21aa398f Alex Beregszaszi
78 2ce151f8 anonymous
/** @name Where quant can be changed
79
 */
80
//@{
81 be3492ec Kostya Shishkov
enum DQSingleEdge {
82
    DQSINGLE_BEDGE_LEFT,
83
    DQSINGLE_BEDGE_TOP,
84
    DQSINGLE_BEDGE_RIGHT,
85
    DQSINGLE_BEDGE_BOTTOM
86
};
87 2ce151f8 anonymous
//@}
88 21aa398f Alex Beregszaszi
89 2ce151f8 anonymous
/** Which pair of edges is quantized with ALTPQUANT */
90
//@{
91 be3492ec Kostya Shishkov
enum DQDoubleEdge {
92
    DQDOUBLE_BEDGE_TOPLEFT,
93
    DQDOUBLE_BEDGE_TOPRIGHT,
94
    DQDOUBLE_BEDGE_BOTTOMRIGHT,
95
    DQDOUBLE_BEDGE_BOTTOMLEFT
96
};
97 2ce151f8 anonymous
//@}
98 21aa398f Alex Beregszaszi
99 2ce151f8 anonymous
/** MV modes for P frames */
100
//@{
101 be3492ec Kostya Shishkov
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 2ce151f8 anonymous
//@}
109 21aa398f Alex Beregszaszi
110 2ce151f8 anonymous
/** @name MV types for B frames */
111
//@{
112 be3492ec Kostya Shishkov
enum BMVTypes {
113
    BMV_TYPE_BACKWARD,
114
    BMV_TYPE_FORWARD,
115 fb2d9140 Kostya Shishkov
    BMV_TYPE_INTERPOLATED
116 be3492ec Kostya Shishkov
};
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 2ce151f8 anonymous
//@}
132 e5540b3f anonymous
133 be3492ec Kostya Shishkov
/** 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 8a66a390 Kostya Shishkov
static const int ttfrm_to_tt[4] = { TT_8X8, TT_8X4, TT_4X8, TT_4X4 };
141
142 2ce151f8 anonymous
/** MV P mode - the 5th element is only used for mode 1 */
143 21aa398f Alex Beregszaszi
static const uint8_t mv_pmode_table[2][5] = {
144 be3492ec Kostya Shishkov
  { 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 21aa398f Alex Beregszaszi
};
147 8a66a390 Kostya Shishkov
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 21aa398f Alex Beregszaszi
152 2ce151f8 anonymous
/** One more frame type */
153 21aa398f Alex Beregszaszi
#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 10b9c374 Kostya Shishkov
/** @name VC-1 VLC tables and defines
173 2ce151f8 anonymous
 *  @todo TODO move this into the context
174
 */
175
//@{
176 10b9c374 Kostya Shishkov
#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 21aa398f Alex Beregszaszi
/* Could be optimized, one table only needs 8 bits */
185 10b9c374 Kostya Shishkov
#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 be3492ec Kostya Shishkov
198
static VLC vc1_ac_coeff_table[8];
199 2ce151f8 anonymous
//@}
200 e5540b3f anonymous
201 be3492ec Kostya Shishkov
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 10b9c374 Kostya Shishkov
/** The VC1 Context
213 8da75fb2 anonymous
 * @fixme Change size wherever another size is more efficient
214
 * Many members are only used for Advanced Profile
215
 */
216 10b9c374 Kostya Shishkov
typedef struct VC1Context{
217 2ce151f8 anonymous
    MpegEncContext s;
218
219 be3492ec Kostya Shishkov
    int bits;
220
221 2ce151f8 anonymous
    /** 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 21aa398f Alex Beregszaszi
234 2ce151f8 anonymous
    /** 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 21aa398f Alex Beregszaszi
251 2ce151f8 anonymous
    /** Sequence header data for all Profiles
252
     * TODO: choose between ints, uint8_ts and monobit flags
253
     */
254
    //@{
255
    int profile;          ///< 2bits, Profile
256 115329f1 Diego Biurrun
    int frmrtq_postproc;  ///< 3bits,
257 2ce151f8 anonymous
    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 7cc84d24 anonymous
    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 be3492ec Kostya Shishkov
    int range_x, range_y; ///< MV range
274 2ce151f8 anonymous
    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 8da75fb2 anonymous
    /** 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 2ce151f8 anonymous
    int ttfrm;            ///< Transform type info present at frame level
290 7cc84d24 anonymous
    uint8_t ttmbf;        ///< Transform type flag
291
    uint8_t ttblk4x4;     ///< Value of ttblk which indicates a 4x4 transform
292 be3492ec Kostya Shishkov
    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 f26c2ef5 Kostya Shishkov
    int a_avail, c_avail;
296 e4bf0302 Kostya Shishkov
    uint8_t *mb_type_base, *mb_type[3];
297 be3492ec Kostya Shishkov
298
299 2ce151f8 anonymous
    /** 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 bf2bc926 anonymous
    uint8_t pquantizer;           ///< Uniform (over sequence) quantizer in use
316 7cc84d24 anonymous
    VLC *cbpcy_vlc;               ///< CBPCY VLC table
317
    int tt_index;                 ///< Index for Transform Type tables
318 87dfe848 Kostya Shishkov
    uint8_t* mv_type_mb_plane;    ///< bitplane for mv_type == (4MV)
319 5df68893 Kostya Shishkov
    uint8_t* direct_mb_plane;     ///< bitplane for "direct" MBs
320 87dfe848 Kostya Shishkov
    int mv_type_is_raw;           ///< mv type mb plane is not coded
321 5df68893 Kostya Shishkov
    int dmb_is_raw;               ///< direct mb plane is raw
322 87dfe848 Kostya Shishkov
    int skip_is_raw;              ///< skip mb plane is not coded
323 66d0ad26 Kostya Shishkov
    uint8_t luty[256], lutuv[256]; // lookup tables used for intensity compensation
324 c5b32ec1 Kostya Shishkov
    int rnd;                      ///< rounding control
325 2ce151f8 anonymous
326
    /** Frame decoding info for S/M profiles only */
327
    //@{
328
    uint8_t rangeredfrm; ///< out_sample = CLIP((in_sample-128)*2+128)
329
    uint8_t interpfrm;
330
    //@}
331 21aa398f Alex Beregszaszi
332 2ce151f8 anonymous
    /** Frame decoding info for Advanced profile */
333
    //@{
334
    uint8_t fcm; ///< 0->Progressive, 2->Frame-Interlace, 3->Field-Interlace
335
    uint8_t numpanscanwin;
336
    uint8_t tfcntr;
337
    uint8_t rptfrm, tff, rff;
338 bf2bc926 anonymous
    uint16_t topleftx;
339
    uint16_t toplefty;
340
    uint16_t bottomrightx;
341
    uint16_t bottomrighty;
342 2ce151f8 anonymous
    uint8_t uvsamp;
343
    uint8_t postproc;
344
    int hrd_num_leaky_buckets;
345
    uint8_t bit_rate_exponent;
346
    uint8_t buffer_size_exponent;
347 87dfe848 Kostya Shishkov
//    BitPlane ac_pred_plane;       ///< AC prediction flags bitplane
348
//    BitPlane over_flags_plane;    ///< Overflags bitplane
349 2ce151f8 anonymous
    uint8_t condover;
350
    uint16_t *hrd_rate, *hrd_buffer;
351 1cf9f514 Ivan Kalvachev
    uint8_t *hrd_fullness;
352
    uint8_t range_mapy_flag;
353
    uint8_t range_mapuv_flag;
354
    uint8_t range_mapy;
355
    uint8_t range_mapuv;
356 2ce151f8 anonymous
    //@}
357 10b9c374 Kostya Shishkov
} VC1Context;
358 21aa398f Alex Beregszaszi
359 2ce151f8 anonymous
/**
360
 * Get unary code of limited length
361
 * @fixme FIXME Slow and ugly
362
 * @param gb GetBitContext
363
 * @param[in] stop The bitstop value (unary code of 1's or 0's)
364
 * @param[in] len Maximum length
365
 * @return Unary length/index
366
 */
367 21aa398f Alex Beregszaszi
static int get_prefix(GetBitContext *gb, int stop, int len)
368
{
369 e5540b3f anonymous
#if 1
370 be3492ec Kostya Shishkov
    int i;
371
372
    for(i = 0; i < len && get_bits1(gb) != stop; i++);
373
    return i;
374
/*  int i = 0, tmp = !stop;
375 21aa398f Alex Beregszaszi

376
  while (i != len && tmp != stop)
377
  {
378
    tmp = get_bits(gb, 1);
379
    i++;
380
  }
381 bf2bc926 anonymous
  if (i == len && tmp != stop) return len+1;
382 be3492ec Kostya Shishkov
  return i;*/
383 e5540b3f anonymous
#else
384
  unsigned int buf;
385
  int log;
386
387
  OPEN_READER(re, gb);
388
  UPDATE_CACHE(re, gb);
389
  buf=GET_CACHE(re, gb); //Still not sure
390
  if (stop) buf = ~buf;
391 42cc17f9 Ivan Kalvachev
392 e5540b3f anonymous
  log= av_log2(-buf); //FIXME: -?
393
  if (log < limit){
394
    LAST_SKIP_BITS(re, gb, log+1);
395
    CLOSE_READER(re, gb);
396
    return log;
397
  }
398 42cc17f9 Ivan Kalvachev
399 e5540b3f anonymous
  LAST_SKIP_BITS(re, gb, limit);
400
  CLOSE_READER(re, gb);
401
  return limit;
402
#endif
403
}
404
405 be3492ec Kostya Shishkov
static inline int decode210(GetBitContext *gb){
406
    int n;
407
    n = get_bits1(gb);
408
    if (n == 1)
409
        return 0;
410
    else
411
        return 2 - get_bits1(gb);
412
}
413
414 2ce151f8 anonymous
/**
415 10b9c374 Kostya Shishkov
 * Init VC-1 specific tables and VC1Context members
416
 * @param v The VC1Context to initialize
417 2ce151f8 anonymous
 * @return Status
418
 */
419 10b9c374 Kostya Shishkov
static int vc1_init_common(VC1Context *v)
420 21aa398f Alex Beregszaszi
{
421
    static int done = 0;
422 bf2bc926 anonymous
    int i = 0;
423 21aa398f Alex Beregszaszi
424
    v->hrd_rate = v->hrd_buffer = NULL;
425 e5540b3f anonymous
426
    /* VLC tables */
427 21aa398f Alex Beregszaszi
    if(!done)
428
    {
429
        done = 1;
430 be3492ec Kostya Shishkov
        init_vlc(&vc1_bfraction_vlc, VC1_BFRACTION_VLC_BITS, 23,
431 10b9c374 Kostya Shishkov
                 vc1_bfraction_bits, 1, 1,
432
                 vc1_bfraction_codes, 1, 1, 1);
433 be3492ec Kostya Shishkov
        init_vlc(&vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4,
434 10b9c374 Kostya Shishkov
                 vc1_norm2_bits, 1, 1,
435
                 vc1_norm2_codes, 1, 1, 1);
436 be3492ec Kostya Shishkov
        init_vlc(&vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 64,
437 10b9c374 Kostya Shishkov
                 vc1_norm6_bits, 1, 1,
438
                 vc1_norm6_codes, 2, 2, 1);
439 be3492ec Kostya Shishkov
        init_vlc(&vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7,
440 10b9c374 Kostya Shishkov
                 vc1_imode_bits, 1, 1,
441
                 vc1_imode_codes, 1, 1, 1);
442 e5540b3f anonymous
        for (i=0; i<3; i++)
443
        {
444 be3492ec Kostya Shishkov
            init_vlc(&vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 16,
445 10b9c374 Kostya Shishkov
                     vc1_ttmb_bits[i], 1, 1,
446
                     vc1_ttmb_codes[i], 2, 2, 1);
447 be3492ec Kostya Shishkov
            init_vlc(&vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 8,
448 10b9c374 Kostya Shishkov
                     vc1_ttblk_bits[i], 1, 1,
449
                     vc1_ttblk_codes[i], 1, 1, 1);
450 be3492ec Kostya Shishkov
            init_vlc(&vc1_subblkpat_vlc[i], VC1_SUBBLKPAT_VLC_BITS, 15,
451 10b9c374 Kostya Shishkov
                     vc1_subblkpat_bits[i], 1, 1,
452
                     vc1_subblkpat_codes[i], 1, 1, 1);
453 e5540b3f anonymous
        }
454
        for(i=0; i<4; i++)
455 21aa398f Alex Beregszaszi
        {
456 be3492ec Kostya Shishkov
            init_vlc(&vc1_4mv_block_pattern_vlc[i], VC1_4MV_BLOCK_PATTERN_VLC_BITS, 16,
457 10b9c374 Kostya Shishkov
                     vc1_4mv_block_pattern_bits[i], 1, 1,
458
                     vc1_4mv_block_pattern_codes[i], 1, 1, 1);
459 be3492ec Kostya Shishkov
            init_vlc(&vc1_cbpcy_p_vlc[i], VC1_CBPCY_P_VLC_BITS, 64,
460 10b9c374 Kostya Shishkov
                     vc1_cbpcy_p_bits[i], 1, 1,
461
                     vc1_cbpcy_p_codes[i], 2, 2, 1);
462 be3492ec Kostya Shishkov
            init_vlc(&vc1_mv_diff_vlc[i], VC1_MV_DIFF_VLC_BITS, 73,
463 10b9c374 Kostya Shishkov
                     vc1_mv_diff_bits[i], 1, 1,
464
                     vc1_mv_diff_codes[i], 2, 2, 1);
465 21aa398f Alex Beregszaszi
        }
466 be3492ec Kostya Shishkov
        for(i=0; i<8; i++)
467
            init_vlc(&vc1_ac_coeff_table[i], AC_VLC_BITS, vc1_ac_sizes[i],
468
                     &vc1_ac_tables[i][0][1], 8, 4,
469
                     &vc1_ac_tables[i][0][0], 8, 4, 1);
470
        init_vlc(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64,
471
                 &ff_msmp4_mb_i_table[0][1], 4, 2,
472
                 &ff_msmp4_mb_i_table[0][0], 4, 2, 1);
473 21aa398f Alex Beregszaszi
    }
474
475 e5540b3f anonymous
    /* Other defaults */
476
    v->pq = -1;
477
    v->mvrange = 0; /* 7.1.1.18, p80 */
478
479 21aa398f Alex Beregszaszi
    return 0;
480
}
481
482 be3492ec Kostya Shishkov
/***********************************************************************/
483 2ce151f8 anonymous
/**
484 be3492ec Kostya Shishkov
 * @defgroup bitplane VC9 Bitplane decoding
485
 * @see 8.7, p56
486
 * @{
487
 */
488
489
/** @addtogroup bitplane
490
 * Imode types
491
 * @{
492
 */
493
enum Imode {
494
    IMODE_RAW,
495
    IMODE_NORM2,
496
    IMODE_DIFF2,
497
    IMODE_NORM6,
498
    IMODE_DIFF6,
499
    IMODE_ROWSKIP,
500
    IMODE_COLSKIP
501
};
502
/** @} */ //imode defines
503
504
/** Decode rows by checking if they are skipped
505
 * @param plane Buffer to store decoded bits
506
 * @param[in] width Width of this buffer
507
 * @param[in] height Height of this buffer
508
 * @param[in] stride of this buffer
509
 */
510
static void decode_rowskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
511
    int x, y;
512 21aa398f Alex Beregszaszi
513 be3492ec Kostya Shishkov
    for (y=0; y<height; y++){
514
        if (!get_bits(gb, 1)) //rowskip
515
            memset(plane, 0, width);
516
        else
517
            for (x=0; x<width; x++)
518
                plane[x] = get_bits(gb, 1);
519
        plane += stride;
520 1cf9f514 Ivan Kalvachev
    }
521 be3492ec Kostya Shishkov
}
522 21aa398f Alex Beregszaszi
523 be3492ec Kostya Shishkov
/** Decode columns by checking if they are skipped
524
 * @param plane Buffer to store decoded bits
525
 * @param[in] width Width of this buffer
526
 * @param[in] height Height of this buffer
527
 * @param[in] stride of this buffer
528
 * @fixme FIXME: Optimize
529
 */
530
static void decode_colskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
531
    int x, y;
532 21aa398f Alex Beregszaszi
533 be3492ec Kostya Shishkov
    for (x=0; x<width; x++){
534
        if (!get_bits(gb, 1)) //colskip
535
            for (y=0; y<height; y++)
536
                plane[y*stride] = 0;
537
        else
538
            for (y=0; y<height; y++)
539
                plane[y*stride] = get_bits(gb, 1);
540
        plane ++;
541 21aa398f Alex Beregszaszi
    }
542
}
543
544 be3492ec Kostya Shishkov
/** Decode a bitplane's bits
545
 * @param bp Bitplane where to store the decode bits
546
 * @param v VC-1 context for bit reading and logging
547 2ce151f8 anonymous
 * @return Status
548 be3492ec Kostya Shishkov
 * @fixme FIXME: Optimize
549
 * @todo TODO: Decide if a struct is needed
550 2ce151f8 anonymous
 */
551 87dfe848 Kostya Shishkov
static int bitplane_decoding(uint8_t* data, int *raw_flag, VC1Context *v)
552 21aa398f Alex Beregszaszi
{
553 be3492ec Kostya Shishkov
    GetBitContext *gb = &v->s.gb;
554 21aa398f Alex Beregszaszi
555 be3492ec Kostya Shishkov
    int imode, x, y, code, offset;
556 87dfe848 Kostya Shishkov
    uint8_t invert, *planep = data;
557
    int width, height, stride;
558 21aa398f Alex Beregszaszi
559 87dfe848 Kostya Shishkov
    width = v->s.mb_width;
560
    height = v->s.mb_height;
561
    stride = v->s.mb_stride;
562 be3492ec Kostya Shishkov
    invert = get_bits(gb, 1);
563
    imode = get_vlc2(gb, vc1_imode_vlc.table, VC1_IMODE_VLC_BITS, 1);
564 21aa398f Alex Beregszaszi
565 87dfe848 Kostya Shishkov
    *raw_flag = 0;
566 be3492ec Kostya Shishkov
    switch (imode)
567 21aa398f Alex Beregszaszi
    {
568 be3492ec Kostya Shishkov
    case IMODE_RAW:
569
        //Data is actually read in the MB layer (same for all tests == "raw")
570 87dfe848 Kostya Shishkov
        *raw_flag = 1; //invert ignored
571 be3492ec Kostya Shishkov
        return invert;
572
    case IMODE_DIFF2:
573
    case IMODE_NORM2:
574 87dfe848 Kostya Shishkov
        if ((height * width) & 1)
575 21aa398f Alex Beregszaszi
        {
576 be3492ec Kostya Shishkov
            *planep++ = get_bits(gb, 1);
577
            offset = 1;
578 21aa398f Alex Beregszaszi
        }
579 be3492ec Kostya Shishkov
        else offset = 0;
580
        // decode bitplane as one long line
581 87dfe848 Kostya Shishkov
        for (y = offset; y < height * width; y += 2) {
582 be3492ec Kostya Shishkov
            code = get_vlc2(gb, vc1_norm2_vlc.table, VC1_NORM2_VLC_BITS, 1);
583
            *planep++ = code & 1;
584
            offset++;
585 87dfe848 Kostya Shishkov
            if(offset == width) {
586 be3492ec Kostya Shishkov
                offset = 0;
587 87dfe848 Kostya Shishkov
                planep += stride - width;
588 21aa398f Alex Beregszaszi
            }
589 be3492ec Kostya Shishkov
            *planep++ = code >> 1;
590
            offset++;
591 87dfe848 Kostya Shishkov
            if(offset == width) {
592 be3492ec Kostya Shishkov
                offset = 0;
593 87dfe848 Kostya Shishkov
                planep += stride - width;
594 21aa398f Alex Beregszaszi
            }
595 be3492ec Kostya Shishkov
        }
596
        break;
597
    case IMODE_DIFF6:
598
    case IMODE_NORM6:
599 87dfe848 Kostya Shishkov
        if(!(height % 3) && (width % 3)) { // use 2x3 decoding
600
            for(y = 0; y < height; y+= 3) {
601
                for(x = width & 1; x < width; x += 2) {
602 be3492ec Kostya Shishkov
                    code = get_vlc2(gb, vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
603
                    if(code < 0){
604
                        av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
605
                        return -1;
606
                    }
607
                    planep[x + 0] = (code >> 0) & 1;
608
                    planep[x + 1] = (code >> 1) & 1;
609 87dfe848 Kostya Shishkov
                    planep[x + 0 + stride] = (code >> 2) & 1;
610
                    planep[x + 1 + stride] = (code >> 3) & 1;
611
                    planep[x + 0 + stride * 2] = (code >> 4) & 1;
612
                    planep[x + 1 + stride * 2] = (code >> 5) & 1;
613 be3492ec Kostya Shishkov
                }
614 87dfe848 Kostya Shishkov
                planep += stride * 3;
615 be3492ec Kostya Shishkov
            }
616 87dfe848 Kostya Shishkov
            if(width & 1) decode_colskip(data, 1, height, stride, &v->s.gb);
617 be3492ec Kostya Shishkov
        } else { // 3x2
618 8a66a390 Kostya Shishkov
            planep += (height & 1) * stride;
619 87dfe848 Kostya Shishkov
            for(y = height & 1; y < height; y += 2) {
620
                for(x = width % 3; x < width; x += 3) {
621 be3492ec Kostya Shishkov
                    code = get_vlc2(gb, vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
622
                    if(code < 0){
623
                        av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
624
                        return -1;
625
                    }
626
                    planep[x + 0] = (code >> 0) & 1;
627
                    planep[x + 1] = (code >> 1) & 1;
628
                    planep[x + 2] = (code >> 2) & 1;
629 87dfe848 Kostya Shishkov
                    planep[x + 0 + stride] = (code >> 3) & 1;
630
                    planep[x + 1 + stride] = (code >> 4) & 1;
631
                    planep[x + 2 + stride] = (code >> 5) & 1;
632 be3492ec Kostya Shishkov
                }
633 87dfe848 Kostya Shishkov
                planep += stride * 2;
634 be3492ec Kostya Shishkov
            }
635 87dfe848 Kostya Shishkov
            x = width % 3;
636
            if(x) decode_colskip(data  ,             x, height    , stride, &v->s.gb);
637
            if(height & 1) decode_rowskip(data+x, width - x, 1, stride, &v->s.gb);
638 be3492ec Kostya Shishkov
        }
639
        break;
640
    case IMODE_ROWSKIP:
641 87dfe848 Kostya Shishkov
        decode_rowskip(data, width, height, stride, &v->s.gb);
642 be3492ec Kostya Shishkov
        break;
643
    case IMODE_COLSKIP:
644 87dfe848 Kostya Shishkov
        decode_colskip(data, width, height, stride, &v->s.gb);
645 be3492ec Kostya Shishkov
        break;
646
    default: break;
647
    }
648
649
    /* Applying diff operator */
650
    if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6)
651
    {
652 87dfe848 Kostya Shishkov
        planep = data;
653 be3492ec Kostya Shishkov
        planep[0] ^= invert;
654 87dfe848 Kostya Shishkov
        for (x=1; x<width; x++)
655 be3492ec Kostya Shishkov
            planep[x] ^= planep[x-1];
656 87dfe848 Kostya Shishkov
        for (y=1; y<height; y++)
657 be3492ec Kostya Shishkov
        {
658 87dfe848 Kostya Shishkov
            planep += stride;
659
            planep[0] ^= planep[-stride];
660
            for (x=1; x<width; x++)
661 21aa398f Alex Beregszaszi
            {
662 87dfe848 Kostya Shishkov
                if (planep[x-1] != planep[x-stride]) planep[x] ^= invert;
663
                else                                 planep[x] ^= planep[x-1];
664 21aa398f Alex Beregszaszi
            }
665
        }
666
    }
667 be3492ec Kostya Shishkov
    else if (invert)
668 21aa398f Alex Beregszaszi
    {
669 87dfe848 Kostya Shishkov
        planep = data;
670
        for (x=0; x<stride*height; x++) planep[x] = !planep[x]; //FIXME stride
671 21aa398f Alex Beregszaszi
    }
672 be3492ec Kostya Shishkov
    return (imode<<1) + invert;
673
}
674 87dfe848 Kostya Shishkov
675 be3492ec Kostya Shishkov
/** @} */ //Bitplane group
676 21aa398f Alex Beregszaszi
677 be3492ec Kostya Shishkov
/***********************************************************************/
678
/** VOP Dquant decoding
679
 * @param v VC-1 Context
680
 */
681
static int vop_dquant_decoding(VC1Context *v)
682
{
683
    GetBitContext *gb = &v->s.gb;
684
    int pqdiff;
685
686
    //variable size
687
    if (v->dquant == 2)
688
    {
689
        pqdiff = get_bits(gb, 3);
690
        if (pqdiff == 7) v->altpq = get_bits(gb, 5);
691
        else v->altpq = v->pq + pqdiff + 1;
692
    }
693
    else
694 21aa398f Alex Beregszaszi
    {
695 be3492ec Kostya Shishkov
        v->dquantfrm = get_bits(gb, 1);
696
        if ( v->dquantfrm )
697 21aa398f Alex Beregszaszi
        {
698 be3492ec Kostya Shishkov
            v->dqprofile = get_bits(gb, 2);
699
            switch (v->dqprofile)
700 21aa398f Alex Beregszaszi
            {
701 be3492ec Kostya Shishkov
            case DQPROFILE_SINGLE_EDGE:
702
            case DQPROFILE_DOUBLE_EDGES:
703
                v->dqsbedge = get_bits(gb, 2);
704
                break;
705
            case DQPROFILE_ALL_MBS:
706
                v->dqbilevel = get_bits(gb, 1);
707
            default: break; //Forbidden ?
708 42cc17f9 Ivan Kalvachev
            }
709 3a3f1cf3 Kostya Shishkov
            if (v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS)
710 21aa398f Alex Beregszaszi
            {
711 be3492ec Kostya Shishkov
                pqdiff = get_bits(gb, 3);
712
                if (pqdiff == 7) v->altpq = get_bits(gb, 5);
713
                else v->altpq = v->pq + pqdiff + 1;
714 21aa398f Alex Beregszaszi
            }
715
        }
716
    }
717 be3492ec Kostya Shishkov
    return 0;
718
}
719 21aa398f Alex Beregszaszi
720 be3492ec Kostya Shishkov
/** Put block onto picture
721
 * @todo move to DSPContext
722
 */
723
static void vc1_put_block(VC1Context *v, DCTELEM block[6][64])
724
{
725
    uint8_t *Y;
726
    int ys, us, vs;
727
    DSPContext *dsp = &v->s.dsp;
728
729 ffb9a8b1 Kostya Shishkov
    if(v->rangeredfrm) {
730
        int i, j, k;
731
        for(k = 0; k < 6; k++)
732
            for(j = 0; j < 8; j++)
733
                for(i = 0; i < 8; i++)
734
                    block[k][i + j*8] = ((block[k][i + j*8] - 128) << 1) + 128;
735
736
    }
737 be3492ec Kostya Shishkov
    ys = v->s.current_picture.linesize[0];
738
    us = v->s.current_picture.linesize[1];
739
    vs = v->s.current_picture.linesize[2];
740
    Y = v->s.dest[0];
741
742
    dsp->put_pixels_clamped(block[0], Y, ys);
743
    dsp->put_pixels_clamped(block[1], Y + 8, ys);
744
    Y += ys * 8;
745
    dsp->put_pixels_clamped(block[2], Y, ys);
746
    dsp->put_pixels_clamped(block[3], Y + 8, ys);
747
748 138712fe Kostya Shishkov
    if(!(v->s.flags & CODEC_FLAG_GRAY)) {
749
        dsp->put_pixels_clamped(block[4], v->s.dest[1], us);
750
        dsp->put_pixels_clamped(block[5], v->s.dest[2], vs);
751
    }
752 be3492ec Kostya Shishkov
}
753
754
/** Do motion compensation over 1 macroblock
755
 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
756
 */
757 5df68893 Kostya Shishkov
static void vc1_mc_1mv(VC1Context *v, int dir)
758 be3492ec Kostya Shishkov
{
759
    MpegEncContext *s = &v->s;
760
    DSPContext *dsp = &v->s.dsp;
761
    uint8_t *srcY, *srcU, *srcV;
762 87dfe848 Kostya Shishkov
    int dxy, uvdxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
763 be3492ec Kostya Shishkov
764
    if(!v->s.last_picture.data[0])return;
765
766 87dfe848 Kostya Shishkov
    mx = s->mv[0][0][0];
767
    my = s->mv[0][0][1];
768
    uvmx = (mx + ((mx & 3) == 3)) >> 1;
769
    uvmy = (my + ((my & 3) == 3)) >> 1;
770 5df68893 Kostya Shishkov
    if(!dir) {
771
        srcY = s->last_picture.data[0];
772
        srcU = s->last_picture.data[1];
773
        srcV = s->last_picture.data[2];
774
    } else {
775
        srcY = s->next_picture.data[0];
776
        srcU = s->next_picture.data[1];
777
        srcV = s->next_picture.data[2];
778
    }
779 be3492ec Kostya Shishkov
780 87dfe848 Kostya Shishkov
    src_x = s->mb_x * 16 + (mx >> 2);
781
    src_y = s->mb_y * 16 + (my >> 2);
782
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
783
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
784
785 230e9d4c Kostya Shishkov
    src_x   = clip(  src_x, -16, s->mb_width  * 16);
786
    src_y   = clip(  src_y, -16, s->mb_height * 16);
787
    uvsrc_x = clip(uvsrc_x,  -8, s->mb_width  *  8);
788
    uvsrc_y = clip(uvsrc_y,  -8, s->mb_height *  8);
789 87dfe848 Kostya Shishkov
790
    srcY += src_y * s->linesize + src_x;
791
    srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
792
    srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
793
794 138712fe Kostya Shishkov
    /* for grayscale we should not try to read from unknown area */
795
    if(s->flags & CODEC_FLAG_GRAY) {
796
        srcU = s->edge_emu_buffer + 18 * s->linesize;
797
        srcV = s->edge_emu_buffer + 18 * s->linesize;
798
    }
799
800 ffb9a8b1 Kostya Shishkov
    if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
801 8295eb30 Kostya Shishkov
       || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3
802
       || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){
803
        uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
804 87dfe848 Kostya Shishkov
805 8295eb30 Kostya Shishkov
        srcY -= s->mspel * (1 + s->linesize);
806
        ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
807
                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
808 87dfe848 Kostya Shishkov
        srcY = s->edge_emu_buffer;
809
        ff_emulated_edge_mc(uvbuf     , srcU, s->uvlinesize, 8+1, 8+1,
810
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
811
        ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
812
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
813
        srcU = uvbuf;
814
        srcV = uvbuf + 16;
815 ffb9a8b1 Kostya Shishkov
        /* if we deal with range reduction we need to scale source blocks */
816
        if(v->rangeredfrm) {
817
            int i, j;
818
            uint8_t *src, *src2;
819
820
            src = srcY;
821 8295eb30 Kostya Shishkov
            for(j = 0; j < 17 + s->mspel*2; j++) {
822
                for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
823 ffb9a8b1 Kostya Shishkov
                src += s->linesize;
824
            }
825
            src = srcU; src2 = srcV;
826
            for(j = 0; j < 9; j++) {
827
                for(i = 0; i < 9; i++) {
828
                    src[i] = ((src[i] - 128) >> 1) + 128;
829
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
830
                }
831
                src += s->uvlinesize;
832
                src2 += s->uvlinesize;
833
            }
834
        }
835 66d0ad26 Kostya Shishkov
        /* if we deal with intensity compensation we need to scale source blocks */
836
        if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
837
            int i, j;
838
            uint8_t *src, *src2;
839
840
            src = srcY;
841 8295eb30 Kostya Shishkov
            for(j = 0; j < 17 + s->mspel*2; j++) {
842
                for(i = 0; i < 17 + s->mspel*2; i++) src[i] = v->luty[src[i]];
843 66d0ad26 Kostya Shishkov
                src += s->linesize;
844
            }
845
            src = srcU; src2 = srcV;
846
            for(j = 0; j < 9; j++) {
847
                for(i = 0; i < 9; i++) {
848
                    src[i] = v->lutuv[src[i]];
849
                    src2[i] = v->lutuv[src2[i]];
850
                }
851
                src += s->uvlinesize;
852
                src2 += s->uvlinesize;
853
            }
854
        }
855 8295eb30 Kostya Shishkov
        srcY += s->mspel * (1 + s->linesize);
856 87dfe848 Kostya Shishkov
    }
857
858 913e93d5 Kostya Shishkov
    if(v->fastuvmc) {
859
        uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
860
        uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
861
    }
862
863 8295eb30 Kostya Shishkov
    if(s->mspel) {
864
        dxy = ((my & 3) << 2) | (mx & 3);
865
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0]    , srcY    , s->linesize, v->rnd);
866
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
867
        srcY += s->linesize * 8;
868
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize    , srcY    , s->linesize, v->rnd);
869
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
870
    } else if(!s->quarter_sample) { // hpel mc
871 87dfe848 Kostya Shishkov
        mx >>= 1;
872
        my >>= 1;
873 be3492ec Kostya Shishkov
        dxy = ((my & 1) << 1) | (mx & 1);
874 87dfe848 Kostya Shishkov
875 c5b32ec1 Kostya Shishkov
        if(!v->rnd)
876
            dsp->put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
877
        else
878
            dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
879 be3492ec Kostya Shishkov
    } else {
880 87dfe848 Kostya Shishkov
        dxy = ((my & 3) << 2) | (mx & 3);
881 be3492ec Kostya Shishkov
882 c5b32ec1 Kostya Shishkov
        if(!v->rnd)
883
            dsp->put_qpel_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize);
884
        else
885
            dsp->put_no_rnd_qpel_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize);
886
    }
887 138712fe Kostya Shishkov
888
    if(s->flags & CODEC_FLAG_GRAY) return;
889 c5b32ec1 Kostya Shishkov
    /* Chroma MC always uses qpel blilinear */
890
    uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
891
    if(!v->rnd){
892
        dsp->put_qpel_pixels_tab[1][uvdxy](s->dest[1], srcU, s->uvlinesize);
893
        dsp->put_qpel_pixels_tab[1][uvdxy](s->dest[2], srcV, s->uvlinesize);
894
    }else{
895
        dsp->put_no_rnd_qpel_pixels_tab[1][uvdxy](s->dest[1], srcU, s->uvlinesize);
896
        dsp->put_no_rnd_qpel_pixels_tab[1][uvdxy](s->dest[2], srcV, s->uvlinesize);
897 be3492ec Kostya Shishkov
    }
898 21aa398f Alex Beregszaszi
}
899
900 e4bf0302 Kostya Shishkov
/** Do motion compensation for 4-MV macroblock - luminance block
901
 */
902
static void vc1_mc_4mv_luma(VC1Context *v, int n)
903
{
904
    MpegEncContext *s = &v->s;
905
    DSPContext *dsp = &v->s.dsp;
906
    uint8_t *srcY;
907
    int dxy, mx, my, src_x, src_y;
908
    int off;
909
910
    if(!v->s.last_picture.data[0])return;
911
    mx = s->mv[0][n][0];
912
    my = s->mv[0][n][1];
913
    srcY = s->last_picture.data[0];
914
915
    off = s->linesize * 4 * (n&2) + (n&1) * 8;
916
917
    src_x = s->mb_x * 16 + (n&1) * 8 + (mx >> 2);
918
    src_y = s->mb_y * 16 + (n&2) * 4 + (my >> 2);
919
920 230e9d4c Kostya Shishkov
    src_x   = clip(  src_x, -16, s->mb_width  * 16);
921
    src_y   = clip(  src_y, -16, s->mb_height * 16);
922 e4bf0302 Kostya Shishkov
923
    srcY += src_y * s->linesize + src_x;
924
925 99f649a2 Kostya Shishkov
    if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
926 78cbfc0c Kostya Shishkov
       || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 8 - s->mspel*2
927
       || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 8 - s->mspel*2){
928 8295eb30 Kostya Shishkov
        srcY -= s->mspel * (1 + s->linesize);
929
        ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 9+s->mspel*2, 9+s->mspel*2,
930
                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
931 e4bf0302 Kostya Shishkov
        srcY = s->edge_emu_buffer;
932 ffb9a8b1 Kostya Shishkov
        /* if we deal with range reduction we need to scale source blocks */
933
        if(v->rangeredfrm) {
934
            int i, j;
935
            uint8_t *src;
936
937
            src = srcY;
938 8295eb30 Kostya Shishkov
            for(j = 0; j < 9 + s->mspel*2; j++) {
939
                for(i = 0; i < 9 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
940 ffb9a8b1 Kostya Shishkov
                src += s->linesize;
941
            }
942
        }
943 99f649a2 Kostya Shishkov
        /* if we deal with intensity compensation we need to scale source blocks */
944
        if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
945
            int i, j;
946
            uint8_t *src;
947
948
            src = srcY;
949
            for(j = 0; j < 9 + s->mspel*2; j++) {
950
                for(i = 0; i < 9 + s->mspel*2; i++) src[i] = v->luty[src[i]];
951
                src += s->linesize;
952
            }
953
        }
954 8295eb30 Kostya Shishkov
        srcY += s->mspel * (1 + s->linesize);
955 e4bf0302 Kostya Shishkov
    }
956
957 8295eb30 Kostya Shishkov
    if(s->mspel) {
958
        dxy = ((my & 3) << 2) | (mx & 3);
959
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, v->rnd);
960
    } else if(!s->quarter_sample) { // hpel mc
961 e4bf0302 Kostya Shishkov
        mx >>= 1;
962
        my >>= 1;
963
        dxy = ((my & 1) << 1) | (mx & 1);
964
965 c5b32ec1 Kostya Shishkov
        if(!v->rnd)
966
            dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
967
        else
968
            dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
969 e4bf0302 Kostya Shishkov
    } else {
970
        dxy = ((my & 3) << 2) | (mx & 3);
971
972 c5b32ec1 Kostya Shishkov
        if(!v->rnd)
973
            dsp->put_qpel_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize);
974
        else
975
            dsp->put_no_rnd_qpel_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize);
976 e4bf0302 Kostya Shishkov
    }
977
}
978
979
static inline int median4(int a, int b, int c, int d)
980
{
981 35a9cac8 Kostya Shishkov
    if(a < b) {
982 913e93d5 Kostya Shishkov
        if(c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
983
        else      return (FFMIN(b, c) + FFMAX(a, d)) / 2;
984 35a9cac8 Kostya Shishkov
    } else {
985 913e93d5 Kostya Shishkov
        if(c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
986
        else      return (FFMIN(a, c) + FFMAX(b, d)) / 2;
987 35a9cac8 Kostya Shishkov
    }
988 e4bf0302 Kostya Shishkov
}
989
990
991
/** Do motion compensation for 4-MV macroblock - both chroma blocks
992
 */
993
static void vc1_mc_4mv_chroma(VC1Context *v)
994
{
995
    MpegEncContext *s = &v->s;
996
    DSPContext *dsp = &v->s.dsp;
997
    uint8_t *srcU, *srcV;
998
    int uvdxy, uvmx, uvmy, uvsrc_x, uvsrc_y;
999
    int i, idx, tx = 0, ty = 0;
1000
    int mvx[4], mvy[4], intra[4];
1001
    static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
1002
1003
    if(!v->s.last_picture.data[0])return;
1004 138712fe Kostya Shishkov
    if(s->flags & CODEC_FLAG_GRAY) return;
1005 e4bf0302 Kostya Shishkov
1006
    for(i = 0; i < 4; i++) {
1007
        mvx[i] = s->mv[0][i][0];
1008
        mvy[i] = s->mv[0][i][1];
1009
        intra[i] = v->mb_type[0][s->block_index[i]];
1010
    }
1011
1012
    /* calculate chroma MV vector from four luma MVs */
1013 1ae4a8e6 Kostya Shishkov
    idx = (intra[3] << 3) | (intra[2] << 2) | (intra[1] << 1) | intra[0];
1014 e4bf0302 Kostya Shishkov
    if(!idx) { // all blocks are inter
1015
        tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
1016
        ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
1017
    } else if(count[idx] == 1) { // 3 inter blocks
1018
        switch(idx) {
1019
        case 0x1:
1020
            tx = mid_pred(mvx[1], mvx[2], mvx[3]);
1021 1ae4a8e6 Kostya Shishkov
            ty = mid_pred(mvy[1], mvy[2], mvy[3]);
1022 e4bf0302 Kostya Shishkov
            break;
1023
        case 0x2:
1024
            tx = mid_pred(mvx[0], mvx[2], mvx[3]);
1025 1ae4a8e6 Kostya Shishkov
            ty = mid_pred(mvy[0], mvy[2], mvy[3]);
1026 e4bf0302 Kostya Shishkov
            break;
1027
        case 0x4:
1028
            tx = mid_pred(mvx[0], mvx[1], mvx[3]);
1029 1ae4a8e6 Kostya Shishkov
            ty = mid_pred(mvy[0], mvy[1], mvy[3]);
1030 e4bf0302 Kostya Shishkov
            break;
1031
        case 0x8:
1032
            tx = mid_pred(mvx[0], mvx[1], mvx[2]);
1033 1ae4a8e6 Kostya Shishkov
            ty = mid_pred(mvy[0], mvy[1], mvy[2]);
1034 e4bf0302 Kostya Shishkov
            break;
1035
        }
1036
    } else if(count[idx] == 2) {
1037
        int t1 = 0, t2 = 0;
1038
        for(i=0; i<3;i++) if(!intra[i]) {t1 = i; break;}
1039
        for(i= t1+1; i<4; i++)if(!intra[i]) {t2 = i; break;}
1040 913e93d5 Kostya Shishkov
        tx = (mvx[t1] + mvx[t2]) / 2;
1041
        ty = (mvy[t1] + mvy[t2]) / 2;
1042 e4bf0302 Kostya Shishkov
    } else
1043
        return; //no need to do MC for inter blocks
1044
1045
    uvmx = (tx + ((tx&3) == 3)) >> 1;
1046
    uvmy = (ty + ((ty&3) == 3)) >> 1;
1047
1048
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1049
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1050
1051 230e9d4c Kostya Shishkov
    uvsrc_x = clip(uvsrc_x,  -8, s->mb_width  *  8);
1052
    uvsrc_y = clip(uvsrc_y,  -8, s->mb_height *  8);
1053 e4bf0302 Kostya Shishkov
    srcU = s->last_picture.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
1054
    srcV = s->last_picture.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
1055 99f649a2 Kostya Shishkov
    if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
1056
       || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
1057 83b3df83 Kostya Shishkov
       || (unsigned)uvsrc_y > (s->v_edge_pos >> 1) - 9){
1058 e4bf0302 Kostya Shishkov
        ff_emulated_edge_mc(s->edge_emu_buffer     , srcU, s->uvlinesize, 8+1, 8+1,
1059
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
1060
        ff_emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize, 8+1, 8+1,
1061
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
1062
        srcU = s->edge_emu_buffer;
1063
        srcV = s->edge_emu_buffer + 16;
1064 ffb9a8b1 Kostya Shishkov
1065
        /* if we deal with range reduction we need to scale source blocks */
1066
        if(v->rangeredfrm) {
1067
            int i, j;
1068
            uint8_t *src, *src2;
1069
1070
            src = srcU; src2 = srcV;
1071
            for(j = 0; j < 9; j++) {
1072
                for(i = 0; i < 9; i++) {
1073
                    src[i] = ((src[i] - 128) >> 1) + 128;
1074
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
1075
                }
1076
                src += s->uvlinesize;
1077
                src2 += s->uvlinesize;
1078
            }
1079
        }
1080 99f649a2 Kostya Shishkov
        /* if we deal with intensity compensation we need to scale source blocks */
1081
        if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1082
            int i, j;
1083
            uint8_t *src, *src2;
1084
1085
            src = srcU; src2 = srcV;
1086
            for(j = 0; j < 9; j++) {
1087
                for(i = 0; i < 9; i++) {
1088
                    src[i] = v->lutuv[src[i]];
1089
                    src2[i] = v->lutuv[src2[i]];
1090
                }
1091
                src += s->uvlinesize;
1092
                src2 += s->uvlinesize;
1093
            }
1094
        }
1095 e4bf0302 Kostya Shishkov
    }
1096
1097 913e93d5 Kostya Shishkov
    if(v->fastuvmc) {
1098
        uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
1099
        uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
1100
    }
1101
1102 c5b32ec1 Kostya Shishkov
    /* Chroma MC always uses qpel blilinear */
1103
    uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
1104
    if(!v->rnd){
1105
        dsp->put_qpel_pixels_tab[1][uvdxy](s->dest[1], srcU, s->uvlinesize);
1106
        dsp->put_qpel_pixels_tab[1][uvdxy](s->dest[2], srcV, s->uvlinesize);
1107
    }else{
1108
        dsp->put_no_rnd_qpel_pixels_tab[1][uvdxy](s->dest[1], srcU, s->uvlinesize);
1109
        dsp->put_no_rnd_qpel_pixels_tab[1][uvdxy](s->dest[2], srcV, s->uvlinesize);
1110
    }
1111 e4bf0302 Kostya Shishkov
}
1112
1113 115329f1 Diego Biurrun
/**
1114 2ce151f8 anonymous
 * Decode Simple/Main Profiles sequence header
1115
 * @see Figure 7-8, p16-17
1116
 * @param avctx Codec context
1117
 * @param gb GetBit context initialized from Codec context extra_data
1118
 * @return Status
1119
 */
1120 21aa398f Alex Beregszaszi
static int decode_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
1121
{
1122 10b9c374 Kostya Shishkov
    VC1Context *v = avctx->priv_data;
1123 21aa398f Alex Beregszaszi
1124 be3492ec Kostya Shishkov
    av_log(avctx, AV_LOG_INFO, "Header: %0X\n", show_bits(gb, 32));
1125 21aa398f Alex Beregszaszi
    v->profile = get_bits(gb, 2);
1126 7cc84d24 anonymous
    if (v->profile == 2)
1127 25a0a0a5 Ivan Kalvachev
    {
1128 be3492ec Kostya Shishkov
        av_log(avctx, AV_LOG_ERROR, "Profile value 2 is forbidden (and WMV3 Complex Profile is unsupported)\n");
1129 25a0a0a5 Ivan Kalvachev
        return -1;
1130
    }
1131 21aa398f Alex Beregszaszi
1132 7cc84d24 anonymous
    if (v->profile == PROFILE_ADVANCED)
1133 21aa398f Alex Beregszaszi
    {
1134
        v->level = get_bits(gb, 3);
1135 25a0a0a5 Ivan Kalvachev
        if(v->level >= 5)
1136
        {
1137
            av_log(avctx, AV_LOG_ERROR, "Reserved LEVEL %i\n",v->level);
1138
        }
1139 21aa398f Alex Beregszaszi
        v->chromaformat = get_bits(gb, 2);
1140
        if (v->chromaformat != 1)
1141
        {
1142
            av_log(avctx, AV_LOG_ERROR,
1143
                   "Only 4:2:0 chroma format supported\n");
1144
            return -1;
1145
        }
1146
    }
1147
    else
1148
    {
1149
        v->res_sm = get_bits(gb, 2); //reserved
1150
        if (v->res_sm)
1151
        {
1152
            av_log(avctx, AV_LOG_ERROR,
1153
                   "Reserved RES_SM=%i is forbidden\n", v->res_sm);
1154 7cc84d24 anonymous
            return -1;
1155 21aa398f Alex Beregszaszi
        }
1156
    }
1157
1158
    // (fps-2)/4 (->30)
1159
    v->frmrtq_postproc = get_bits(gb, 3); //common
1160
    // (bitrate-32kbps)/64kbps
1161
    v->bitrtq_postproc = get_bits(gb, 5); //common
1162 5e424311 anonymous
    v->s.loop_filter = get_bits(gb, 1); //common
1163 25a0a0a5 Ivan Kalvachev
    if(v->s.loop_filter == 1 && v->profile == PROFILE_SIMPLE)
1164
    {
1165
        av_log(avctx, AV_LOG_ERROR,
1166
               "LOOPFILTER shell not be enabled in simple profile\n");
1167
    }
1168 21aa398f Alex Beregszaszi
1169 7cc84d24 anonymous
    if (v->profile < PROFILE_ADVANCED)
1170 21aa398f Alex Beregszaszi
    {
1171
        v->res_x8 = get_bits(gb, 1); //reserved
1172
        if (v->res_x8)
1173
        {
1174
            av_log(avctx, AV_LOG_ERROR,
1175
                   "1 for reserved RES_X8 is forbidden\n");
1176 5e424311 anonymous
            //return -1;
1177 21aa398f Alex Beregszaszi
        }
1178
        v->multires = get_bits(gb, 1);
1179
        v->res_fasttx = get_bits(gb, 1);
1180
        if (!v->res_fasttx)
1181
        {
1182
            av_log(avctx, AV_LOG_ERROR,
1183
                   "0 for reserved RES_FASTTX is forbidden\n");
1184
            //return -1;
1185
        }
1186
    }
1187
1188
    v->fastuvmc =  get_bits(gb, 1); //common
1189
    if (!v->profile && !v->fastuvmc)
1190
    {
1191
        av_log(avctx, AV_LOG_ERROR,
1192
               "FASTUVMC unavailable in Simple Profile\n");
1193
        return -1;
1194
    }
1195
    v->extended_mv =  get_bits(gb, 1); //common
1196
    if (!v->profile && v->extended_mv)
1197
    {
1198
        av_log(avctx, AV_LOG_ERROR,
1199
               "Extended MVs unavailable in Simple Profile\n");
1200
        return -1;
1201
    }
1202
    v->dquant =  get_bits(gb, 2); //common
1203
    v->vstransform =  get_bits(gb, 1); //common
1204 42cc17f9 Ivan Kalvachev
1205 7cc84d24 anonymous
    if (v->profile < PROFILE_ADVANCED)
1206 21aa398f Alex Beregszaszi
    {
1207
        v->res_transtab = get_bits(gb, 1);
1208
        if (v->res_transtab)
1209
        {
1210
            av_log(avctx, AV_LOG_ERROR,
1211
                   "1 for reserved RES_TRANSTAB is forbidden\n");
1212
            return -1;
1213
        }
1214
    }
1215
1216
    v->overlap = get_bits(gb, 1); //common
1217
1218 7cc84d24 anonymous
    if (v->profile < PROFILE_ADVANCED)
1219 21aa398f Alex Beregszaszi
    {
1220 0d33db8a anonymous
        v->s.resync_marker = get_bits(gb, 1);
1221 21aa398f Alex Beregszaszi
        v->rangered = get_bits(gb, 1);
1222 25a0a0a5 Ivan Kalvachev
        if (v->rangered && v->profile == PROFILE_SIMPLE)
1223
        {
1224 be3492ec Kostya Shishkov
            av_log(avctx, AV_LOG_INFO,
1225 42cc17f9 Ivan Kalvachev
                   "RANGERED should be set to 0 in simple profile\n");
1226 25a0a0a5 Ivan Kalvachev
        }
1227 21aa398f Alex Beregszaszi
    }
1228
1229 0d33db8a anonymous
    v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
1230 21aa398f Alex Beregszaszi
    v->quantizer_mode = get_bits(gb, 2); //common
1231
1232 7cc84d24 anonymous
    if (v->profile < PROFILE_ADVANCED)
1233 21aa398f Alex Beregszaszi
    {
1234
        v->finterpflag = get_bits(gb, 1); //common
1235
        v->res_rtm_flag = get_bits(gb, 1); //reserved
1236
        if (!v->res_rtm_flag)
1237
        {
1238 48d3fca7 Kostya Shishkov
//            av_log(avctx, AV_LOG_ERROR,
1239
//                   "0 for reserved RES_RTM_FLAG is forbidden\n");
1240 21aa398f Alex Beregszaszi
            av_log(avctx, AV_LOG_ERROR,
1241 48d3fca7 Kostya Shishkov
                   "Old WMV3 version detected, only I-frames will be decoded\n");
1242 21aa398f Alex Beregszaszi
            //return -1;
1243
        }
1244 be3492ec Kostya Shishkov
        av_log(avctx, AV_LOG_DEBUG,
1245 21aa398f Alex Beregszaszi
               "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
1246 e148c6e1 Diego Biurrun
               "LoopFilter=%i, MultiRes=%i, FastUVMC=%i, Extended MV=%i\n"
1247 21aa398f Alex Beregszaszi
               "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
1248
               "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
1249
               v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
1250 2ce151f8 anonymous
               v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
1251 0d33db8a anonymous
               v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
1252 21aa398f Alex Beregszaszi
               v->dquant, v->quantizer_mode, avctx->max_b_frames
1253
               );
1254 e5540b3f anonymous
        return 0;
1255 21aa398f Alex Beregszaszi
    }
1256 be3492ec Kostya Shishkov
    return -1;
1257 21aa398f Alex Beregszaszi
}
1258
1259
1260 be3492ec Kostya Shishkov
static int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
1261 21aa398f Alex Beregszaszi
{
1262 be3492ec Kostya Shishkov
    int pqindex, lowquant, status;
1263
1264
    if(v->finterpflag) v->interpfrm = get_bits(gb, 1);
1265
    skip_bits(gb, 2); //framecnt unused
1266
    v->rangeredfrm = 0;
1267
    if (v->rangered) v->rangeredfrm = get_bits(gb, 1);
1268
    v->s.pict_type = get_bits(gb, 1);
1269
    if (v->s.avctx->max_b_frames) {
1270
        if (!v->s.pict_type) {
1271
            if (get_bits(gb, 1)) v->s.pict_type = I_TYPE;
1272
            else v->s.pict_type = B_TYPE;
1273
        } else v->s.pict_type = P_TYPE;
1274
    } else v->s.pict_type = v->s.pict_type ? P_TYPE : I_TYPE;
1275
1276
    if(v->s.pict_type == I_TYPE)
1277
        get_bits(gb, 7); // skip buffer fullness
1278 5df68893 Kostya Shishkov
    if(v->s.pict_type == B_TYPE) {
1279
        v->bfraction = get_vlc2(gb, vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
1280
        v->bfraction = vc1_bfraction_lut[v->bfraction];
1281
        if(v->bfraction == -1) {
1282
            v->s.pict_type = BI_TYPE;
1283
        }
1284
    }
1285 be3492ec Kostya Shishkov
1286 c5b32ec1 Kostya Shishkov
    /* calculate RND */
1287
    if(v->s.pict_type == I_TYPE)
1288
        v->rnd = 1;
1289
    if(v->s.pict_type == P_TYPE)
1290
        v->rnd ^= 1;
1291
1292 be3492ec Kostya Shishkov
    /* Quantizer stuff */
1293
    pqindex = get_bits(gb, 5);
1294
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1295
        v->pq = pquant_table[0][pqindex];
1296
    else
1297 b53921aa Kostya Shishkov
        v->pq = pquant_table[1][pqindex];
1298 be3492ec Kostya Shishkov
1299 0a45801f Kostya Shishkov
    v->pquantizer = 1;
1300 be3492ec Kostya Shishkov
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1301
        v->pquantizer = pqindex < 9;
1302 0a45801f Kostya Shishkov
    if (v->quantizer_mode == QUANT_NON_UNIFORM)
1303
        v->pquantizer = 0;
1304 be3492ec Kostya Shishkov
    v->pqindex = pqindex;
1305
    if (pqindex < 9) v->halfpq = get_bits(gb, 1);
1306
    else v->halfpq = 0;
1307
    if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
1308
        v->pquantizer = get_bits(gb, 1);
1309
    v->dquantfrm = 0;
1310 5c4b8efd Kostya Shishkov
    if (v->extended_mv == 1) v->mvrange = get_prefix(gb, 0, 3);
1311
    v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1312
    v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
1313
    v->range_x = 1 << (v->k_x - 1);
1314
    v->range_y = 1 << (v->k_y - 1);
1315
    if (v->profile == PROFILE_ADVANCED)
1316
    {
1317
        if (v->postprocflag) v->postproc = get_bits(gb, 1);
1318
    }
1319
    else
1320
        if (v->multires && v->s.pict_type != B_TYPE) v->respic = get_bits(gb, 2);
1321 be3492ec Kostya Shishkov
1322
//av_log(v->s.avctx, AV_LOG_INFO, "%c Frame: QP=[%i]%i (+%i/2) %i\n",
1323
//        (v->s.pict_type == P_TYPE) ? 'P' : ((v->s.pict_type == I_TYPE) ? 'I' : 'B'), pqindex, v->pq, v->halfpq, v->rangeredfrm);
1324
1325
    //TODO: complete parsing for P/B/BI frames
1326
    switch(v->s.pict_type) {
1327
    case P_TYPE:
1328
        if (v->pq < 5) v->tt_index = 0;
1329
        else if(v->pq < 13) v->tt_index = 1;
1330
        else v->tt_index = 2;
1331
1332
        lowquant = (v->pq > 12) ? 0 : 1;
1333
        v->mv_mode = mv_pmode_table[lowquant][get_prefix(gb, 1, 4)];
1334
        if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
1335
        {
1336 66d0ad26 Kostya Shishkov
            int scale, shift, i;
1337
            v->mv_mode2 = mv_pmode_table2[lowquant][get_prefix(gb, 1, 3)];
1338 be3492ec Kostya Shishkov
            v->lumscale = get_bits(gb, 6);
1339
            v->lumshift = get_bits(gb, 6);
1340 66d0ad26 Kostya Shishkov
            /* fill lookup tables for intensity compensation */
1341
            if(!v->lumscale) {
1342
                scale = -64;
1343
                shift = (255 - v->lumshift * 2) << 6;
1344
                if(v->lumshift > 31)
1345
                    shift += 128 << 6;
1346
            } else {
1347
                scale = v->lumscale + 32;
1348
                if(v->lumshift > 31)
1349
                    shift = (v->lumshift - 64) << 6;
1350
                else
1351
                    shift = v->lumshift << 6;
1352
            }
1353
            for(i = 0; i < 256; i++) {
1354
                v->luty[i] = clip_uint8((scale * i + shift + 32) >> 6);
1355
                v->lutuv[i] = clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
1356
            }
1357 be3492ec Kostya Shishkov
        }
1358
        if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
1359 87dfe848 Kostya Shishkov
            v->s.quarter_sample = 0;
1360 05103ed3 Kostya Shishkov
        else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1361
            if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
1362
                v->s.quarter_sample = 0;
1363
            else
1364
                v->s.quarter_sample = 1;
1365
        } else
1366 87dfe848 Kostya Shishkov
            v->s.quarter_sample = 1;
1367 09be55df Kostya Shishkov
        v->s.mspel = !(v->mv_mode == MV_PMODE_1MV_HPEL_BILIN || (v->mv_mode == MV_PMODE_INTENSITY_COMP && v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN));
1368 be3492ec Kostya Shishkov
1369
        if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
1370
                 v->mv_mode2 == MV_PMODE_MIXED_MV)
1371
                || v->mv_mode == MV_PMODE_MIXED_MV)
1372
        {
1373 87dfe848 Kostya Shishkov
            status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
1374 be3492ec Kostya Shishkov
            if (status < 0) return -1;
1375
            av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
1376
                   "Imode: %i, Invert: %i\n", status>>1, status&1);
1377 87dfe848 Kostya Shishkov
        } else {
1378
            v->mv_type_is_raw = 0;
1379
            memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
1380 be3492ec Kostya Shishkov
        }
1381 0f7344aa Kostya Shishkov
        status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1382 be3492ec Kostya Shishkov
        if (status < 0) return -1;
1383
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1384
               "Imode: %i, Invert: %i\n", status>>1, status&1);
1385
1386
        /* Hopefully this is correct for P frames */
1387
        v->s.mv_table_index = get_bits(gb, 2); //but using vc1_ tables
1388
        v->cbpcy_vlc = &vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1389
1390
        if (v->dquant)
1391
        {
1392
            av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1393
            vop_dquant_decoding(v);
1394
        }
1395
1396
        v->ttfrm = 0; //FIXME Is that so ?
1397
        if (v->vstransform)
1398
        {
1399
            v->ttmbf = get_bits(gb, 1);
1400
            if (v->ttmbf)
1401
            {
1402 8a66a390 Kostya Shishkov
                v->ttfrm = ttfrm_to_tt[get_bits(gb, 2)];
1403 be3492ec Kostya Shishkov
            }
1404 b53921aa Kostya Shishkov
        } else {
1405
            v->ttmbf = 1;
1406
            v->ttfrm = TT_8X8;
1407 be3492ec Kostya Shishkov
        }
1408
        break;
1409
    case B_TYPE:
1410 5df68893 Kostya Shishkov
        if (v->pq < 5) v->tt_index = 0;
1411
        else if(v->pq < 13) v->tt_index = 1;
1412
        else v->tt_index = 2;
1413
1414
        lowquant = (v->pq > 12) ? 0 : 1;
1415
        v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
1416
        v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
1417 09be55df Kostya Shishkov
        v->s.mspel = v->s.quarter_sample;
1418 5df68893 Kostya Shishkov
1419
        status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
1420
        if (status < 0) return -1;
1421
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
1422
               "Imode: %i, Invert: %i\n", status>>1, status&1);
1423
        status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1424
        if (status < 0) return -1;
1425
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1426
               "Imode: %i, Invert: %i\n", status>>1, status&1);
1427
1428
        v->s.mv_table_index = get_bits(gb, 2);
1429
        v->cbpcy_vlc = &vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1430
1431
        if (v->dquant)
1432
        {
1433
            av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1434
            vop_dquant_decoding(v);
1435
        }
1436
1437
        v->ttfrm = 0;
1438
        if (v->vstransform)
1439
        {
1440
            v->ttmbf = get_bits(gb, 1);
1441
            if (v->ttmbf)
1442
            {
1443
                v->ttfrm = ttfrm_to_tt[get_bits(gb, 2)];
1444
            }
1445
        } else {
1446
            v->ttmbf = 1;
1447
            v->ttfrm = TT_8X8;
1448
        }
1449 be3492ec Kostya Shishkov
        break;
1450 21aa398f Alex Beregszaszi
    }
1451 be3492ec Kostya Shishkov
1452
    /* AC Syntax */
1453
    v->c_ac_table_index = decode012(gb);
1454
    if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
1455 21aa398f Alex Beregszaszi
    {
1456 be3492ec Kostya Shishkov
        v->y_ac_table_index = decode012(gb);
1457 21aa398f Alex Beregszaszi
    }
1458 be3492ec Kostya Shishkov
    /* DC Syntax */
1459
    v->s.dc_table_index = get_bits(gb, 1);
1460
1461 21aa398f Alex Beregszaszi
    return 0;
1462
}
1463
1464 2ce151f8 anonymous
/***********************************************************************/
1465
/**
1466 be3492ec Kostya Shishkov
 * @defgroup block VC-1 Block-level functions
1467
 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1468
 * @todo TODO: Integrate to MpegEncContext facilities
1469 2ce151f8 anonymous
 * @{
1470
 */
1471
1472 be3492ec Kostya Shishkov
/**
1473
 * @def GET_MQUANT
1474
 * @brief Get macroblock-level quantizer scale
1475
 * @warning XXX: qdiff to the frame quant, not previous quant ?
1476
 * @fixme XXX: Don't know how to initialize mquant otherwise in last case
1477 2ce151f8 anonymous
 */
1478 be3492ec Kostya Shishkov
#define GET_MQUANT()                                           \
1479
  if (v->dquantfrm)                                            \
1480
  {                                                            \
1481 35a9cac8 Kostya Shishkov
    int edges = 0;                                             \
1482 be3492ec Kostya Shishkov
    if (v->dqprofile == DQPROFILE_ALL_MBS)                     \
1483
    {                                                          \
1484
      if (v->dqbilevel)                                        \
1485
      {                                                        \
1486 3a3f1cf3 Kostya Shishkov
        mquant = (get_bits(gb, 1)) ? v->altpq : v->pq;         \
1487 be3492ec Kostya Shishkov
      }                                                        \
1488
      else                                                     \
1489
      {                                                        \
1490
        mqdiff = get_bits(gb, 3);                              \
1491
        if (mqdiff != 7) mquant = v->pq + mqdiff;              \
1492
        else mquant = get_bits(gb, 5);                         \
1493
      }                                                        \
1494
    }                                                          \
1495 35a9cac8 Kostya Shishkov
    if(v->dqprofile == DQPROFILE_SINGLE_EDGE)                  \
1496
        edges = 1 << v->dqsbedge;                              \
1497 e4bf0302 Kostya Shishkov
    else if(v->dqprofile == DQPROFILE_DOUBLE_EDGES)            \
1498 35a9cac8 Kostya Shishkov
        edges = (3 << v->dqsbedge) % 15;                       \
1499 e4bf0302 Kostya Shishkov
    else if(v->dqprofile == DQPROFILE_FOUR_EDGES)              \
1500 35a9cac8 Kostya Shishkov
        edges = 15;                                            \
1501
    if((edges&1) && !s->mb_x)                                  \
1502
        mquant = v->altpq;                                     \
1503 3a3f1cf3 Kostya Shishkov
    if((edges&2) && s->first_slice_line)                       \
1504 35a9cac8 Kostya Shishkov
        mquant = v->altpq;                                     \
1505
    if((edges&4) && s->mb_x == (s->mb_width - 1))              \
1506
        mquant = v->altpq;                                     \
1507
    if((edges&8) && s->mb_y == (s->mb_height - 1))             \
1508
        mquant = v->altpq;                                     \
1509 be3492ec Kostya Shishkov
  }
1510 2ce151f8 anonymous
1511 be3492ec Kostya Shishkov
/**
1512
 * @def GET_MVDATA(_dmv_x, _dmv_y)
1513
 * @brief Get MV differentials
1514
 * @see MVDATA decoding from 8.3.5.2, p(1)20
1515
 * @param _dmv_x Horizontal differential for decoded MV
1516
 * @param _dmv_y Vertical differential for decoded MV
1517
 * @todo TODO: Use MpegEncContext arrays to store them
1518 2ce151f8 anonymous
 */
1519 be3492ec Kostya Shishkov
#define GET_MVDATA(_dmv_x, _dmv_y)                                  \
1520
  index = 1 + get_vlc2(gb, vc1_mv_diff_vlc[s->mv_table_index].table,\
1521
                       VC1_MV_DIFF_VLC_BITS, 2);                    \
1522
  if (index > 36)                                                   \
1523
  {                                                                 \
1524
    mb_has_coeffs = 1;                                              \
1525
    index -= 37;                                                    \
1526
  }                                                                 \
1527
  else mb_has_coeffs = 0;                                           \
1528
  s->mb_intra = 0;                                                  \
1529
  if (!index) { _dmv_x = _dmv_y = 0; }                              \
1530
  else if (index == 35)                                             \
1531
  {                                                                 \
1532 87dfe848 Kostya Shishkov
    _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample);          \
1533
    _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample);          \
1534 be3492ec Kostya Shishkov
  }                                                                 \
1535
  else if (index == 36)                                             \
1536
  {                                                                 \
1537
    _dmv_x = 0;                                                     \
1538
    _dmv_y = 0;                                                     \
1539
    s->mb_intra = 1;                                                \
1540
  }                                                                 \
1541
  else                                                              \
1542
  {                                                                 \
1543
    index1 = index%6;                                               \
1544 87dfe848 Kostya Shishkov
    if (!s->quarter_sample && index1 == 5) val = 1;                 \
1545
    else                                   val = 0;                 \
1546 e8ba1cea Kostya Shishkov
    if(size_table[index1] - val > 0)                                \
1547
        val = get_bits(gb, size_table[index1] - val);               \
1548
    else                                   val = 0;                 \
1549 be3492ec Kostya Shishkov
    sign = 0 - (val&1);                                             \
1550
    _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign;     \
1551
                                                                    \
1552
    index1 = index/6;                                               \
1553 87dfe848 Kostya Shishkov
    if (!s->quarter_sample && index1 == 5) val = 1;                 \
1554
    else                                   val = 0;                 \
1555 e8ba1cea Kostya Shishkov
    if(size_table[index1] - val > 0)                                \
1556
        val = get_bits(gb, size_table[index1] - val);               \
1557
    else                                   val = 0;                 \
1558 be3492ec Kostya Shishkov
    sign = 0 - (val&1);                                             \
1559
    _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign;     \
1560
  }
1561 21aa398f Alex Beregszaszi
1562 be3492ec Kostya Shishkov
/** Predict and set motion vector
1563 2ce151f8 anonymous
 */
1564 e4bf0302 Kostya Shishkov
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)
1565 0d33db8a anonymous
{
1566 e4bf0302 Kostya Shishkov
    int xy, wrap, off = 0;
1567 be3492ec Kostya Shishkov
    int16_t *A, *B, *C;
1568
    int px, py;
1569
    int sum;
1570 0d33db8a anonymous
1571 be3492ec Kostya Shishkov
    /* scale MV difference to be quad-pel */
1572 87dfe848 Kostya Shishkov
    dmv_x <<= 1 - s->quarter_sample;
1573
    dmv_y <<= 1 - s->quarter_sample;
1574 d29f0cd9 Michael Niedermayer
1575 be3492ec Kostya Shishkov
    wrap = s->b8_stride;
1576 e4bf0302 Kostya Shishkov
    xy = s->block_index[n];
1577 d29f0cd9 Michael Niedermayer
1578 e4bf0302 Kostya Shishkov
    if(s->mb_intra){
1579
        s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
1580
        s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
1581
        if(mv1) { /* duplicate motion data for 1-MV block */
1582
            s->current_picture.motion_val[0][xy + 1][0] = 0;
1583
            s->current_picture.motion_val[0][xy + 1][1] = 0;
1584
            s->current_picture.motion_val[0][xy + wrap][0] = 0;
1585
            s->current_picture.motion_val[0][xy + wrap][1] = 0;
1586
            s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
1587
            s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
1588
        }
1589
        return;
1590
    }
1591
1592
    C = s->current_picture.motion_val[0][xy - 1];
1593
    A = s->current_picture.motion_val[0][xy - wrap];
1594
    if(mv1)
1595
        off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1596
    else {
1597
        //in 4-MV mode different blocks have different B predictor position
1598
        switch(n){
1599
        case 0:
1600
            off = (s->mb_x > 0) ? -1 : 1;
1601
            break;
1602
        case 1:
1603
            off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1604
            break;
1605
        case 2:
1606
            off = 1;
1607
            break;
1608
        case 3:
1609
            off = -1;
1610
        }
1611
    }
1612
    B = s->current_picture.motion_val[0][xy - wrap + off];
1613 d29f0cd9 Michael Niedermayer
1614 e4bf0302 Kostya Shishkov
    if(!s->first_slice_line || (n==2 || n==3)) { // predictor A is not out of bounds
1615 be3492ec Kostya Shishkov
        if(s->mb_width == 1) {
1616
            px = A[0];
1617
            py = A[1];
1618
        } else {
1619
            px = mid_pred(A[0], B[0], C[0]);
1620
            py = mid_pred(A[1], B[1], C[1]);
1621
        }
1622 e4bf0302 Kostya Shishkov
    } else if(s->mb_x || (n==1 || n==3)) { // predictor C is not out of bounds
1623 be3492ec Kostya Shishkov
        px = C[0];
1624
        py = C[1];
1625
    } else {
1626
        px = py = 0;
1627
    }
1628
    /* Pullback MV as specified in 8.3.5.3.4 */
1629
    {
1630
        int qx, qy, X, Y;
1631 ef6cc8ce Kostya Shishkov
        qx = (s->mb_x << 6) + ((n==1 || n==3) ? 32 : 0);
1632
        qy = (s->mb_y << 6) + ((n==2 || n==3) ? 32 : 0);
1633 be3492ec Kostya Shishkov
        X = (s->mb_width << 6) - 4;
1634
        Y = (s->mb_height << 6) - 4;
1635
        if(mv1) {
1636
            if(qx + px < -60) px = -60 - qx;
1637
            if(qy + py < -60) py = -60 - qy;
1638
        } else {
1639
            if(qx + px < -28) px = -28 - qx;
1640
            if(qy + py < -28) py = -28 - qy;
1641
        }
1642
        if(qx + px > X) px = X - qx;
1643
        if(qy + py > Y) py = Y - qy;
1644
    }
1645
    /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
1646 e4bf0302 Kostya Shishkov
    if((!s->first_slice_line || (n==2 || n==3)) && (s->mb_x || (n==1 || n==3))) {
1647
        if(is_intra[xy - wrap])
1648 be3492ec Kostya Shishkov
            sum = ABS(px) + ABS(py);
1649 d29f0cd9 Michael Niedermayer
        else
1650 be3492ec Kostya Shishkov
            sum = ABS(px - A[0]) + ABS(py - A[1]);
1651
        if(sum > 32) {
1652
            if(get_bits1(&s->gb)) {
1653
                px = A[0];
1654
                py = A[1];
1655
            } else {
1656
                px = C[0];
1657
                py = C[1];
1658
            }
1659
        } else {
1660 e4bf0302 Kostya Shishkov
            if(is_intra[xy - 1])
1661 be3492ec Kostya Shishkov
                sum = ABS(px) + ABS(py);
1662
            else
1663
                sum = ABS(px - C[0]) + ABS(py - C[1]);
1664
            if(sum > 32) {
1665
                if(get_bits1(&s->gb)) {
1666
                    px = A[0];
1667
                    py = A[1];
1668
                } else {
1669
                    px = C[0];
1670
                    py = C[1];
1671
                }
1672
            }
1673
        }
1674 d29f0cd9 Michael Niedermayer
    }
1675 be3492ec Kostya Shishkov
    /* store MV using signed modulus of MV range defined in 4.11 */
1676 e4bf0302 Kostya Shishkov
    s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1677
    s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
1678
    if(mv1) { /* duplicate motion data for 1-MV block */
1679
        s->current_picture.motion_val[0][xy + 1][0] = s->current_picture.motion_val[0][xy][0];
1680
        s->current_picture.motion_val[0][xy + 1][1] = s->current_picture.motion_val[0][xy][1];
1681
        s->current_picture.motion_val[0][xy + wrap][0] = s->current_picture.motion_val[0][xy][0];
1682
        s->current_picture.motion_val[0][xy + wrap][1] = s->current_picture.motion_val[0][xy][1];
1683
        s->current_picture.motion_val[0][xy + wrap + 1][0] = s->current_picture.motion_val[0][xy][0];
1684
        s->current_picture.motion_val[0][xy + wrap + 1][1] = s->current_picture.motion_val[0][xy][1];
1685
    }
1686 d29f0cd9 Michael Niedermayer
}
1687
1688 fb2d9140 Kostya Shishkov
/** Motion compensation for direct or interpolated blocks in B-frames
1689
 */
1690
static void vc1_interp_mc(VC1Context *v)
1691
{
1692
    MpegEncContext *s = &v->s;
1693
    DSPContext *dsp = &v->s.dsp;
1694
    uint8_t *srcY, *srcU, *srcV;
1695
    int dxy, uvdxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1696
1697
    if(!v->s.next_picture.data[0])return;
1698
1699
    mx = s->mv[1][0][0];
1700
    my = s->mv[1][0][1];
1701
    uvmx = (mx + ((mx & 3) == 3)) >> 1;
1702
    uvmy = (my + ((my & 3) == 3)) >> 1;
1703
    srcY = s->next_picture.data[0];
1704
    srcU = s->next_picture.data[1];
1705
    srcV = s->next_picture.data[2];
1706
1707
    src_x = s->mb_x * 16 + (mx >> 2);
1708
    src_y = s->mb_y * 16 + (my >> 2);
1709
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1710
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1711
1712
    src_x   = clip(  src_x, -16, s->mb_width  * 16);
1713
    src_y   = clip(  src_y, -16, s->mb_height * 16);
1714
    uvsrc_x = clip(uvsrc_x,  -8, s->mb_width  *  8);
1715
    uvsrc_y = clip(uvsrc_y,  -8, s->mb_height *  8);
1716
1717
    srcY += src_y * s->linesize + src_x;
1718
    srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1719
    srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1720
1721
    /* for grayscale we should not try to read from unknown area */
1722
    if(s->flags & CODEC_FLAG_GRAY) {
1723
        srcU = s->edge_emu_buffer + 18 * s->linesize;
1724
        srcV = s->edge_emu_buffer + 18 * s->linesize;
1725
    }
1726
1727
    if(v->rangeredfrm
1728
       || (unsigned)src_x > s->h_edge_pos - (mx&3) - 16
1729
       || (unsigned)src_y > s->v_edge_pos - (my&3) - 16){
1730
        uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
1731
1732
        ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17, 17,
1733
                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
1734
        srcY = s->edge_emu_buffer;
1735
        ff_emulated_edge_mc(uvbuf     , srcU, s->uvlinesize, 8+1, 8+1,
1736
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
1737
        ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
1738
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
1739
        srcU = uvbuf;
1740
        srcV = uvbuf + 16;
1741
        /* if we deal with range reduction we need to scale source blocks */
1742
        if(v->rangeredfrm) {
1743
            int i, j;
1744
            uint8_t *src, *src2;
1745
1746
            src = srcY;
1747
            for(j = 0; j < 17; j++) {
1748
                for(i = 0; i < 17; i++) src[i] = ((src[i] - 128) >> 1) + 128;
1749
                src += s->linesize;
1750
            }
1751
            src = srcU; src2 = srcV;
1752
            for(j = 0; j < 9; j++) {
1753
                for(i = 0; i < 9; i++) {
1754
                    src[i] = ((src[i] - 128) >> 1) + 128;
1755
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
1756
                }
1757
                src += s->uvlinesize;
1758
                src2 += s->uvlinesize;
1759
            }
1760
        }
1761
    }
1762
1763
    if(v->fastuvmc) {
1764
        uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
1765
        uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
1766
    }
1767
1768
    if(!s->quarter_sample) { // hpel mc
1769
        mx >>= 1;
1770
        my >>= 1;
1771
        dxy = ((my & 1) << 1) | (mx & 1);
1772
1773
        dsp->avg_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
1774
    } else {
1775
        dxy = ((my & 3) << 2) | (mx & 3);
1776
1777
        dsp->avg_qpel_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize);
1778
    }
1779
1780
    if(s->flags & CODEC_FLAG_GRAY) return;
1781
    /* Chroma MC always uses qpel blilinear */
1782
    uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
1783
    dsp->avg_qpel_pixels_tab[1][uvdxy](s->dest[1], srcU, s->uvlinesize);
1784
    dsp->avg_qpel_pixels_tab[1][uvdxy](s->dest[2], srcV, s->uvlinesize);
1785
}
1786
1787 5df68893 Kostya Shishkov
/** Reconstruct motion vector for B-frame and do motion compensation
1788
 */
1789
static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mode)
1790
{
1791
    MpegEncContext *s = &v->s;
1792
    int mx[4], my[4], mv_x, mv_y;
1793
    int i;
1794
1795
    /* scale MV difference to be quad-pel */
1796
    dmv_x[0] <<= 1 - s->quarter_sample;
1797
    dmv_y[0] <<= 1 - s->quarter_sample;
1798
    dmv_x[1] <<= 1 - s->quarter_sample;
1799
    dmv_y[1] <<= 1 - s->quarter_sample;
1800
1801 fb2d9140 Kostya Shishkov
    if(direct) {
1802
        for(i = 0; i < 4; i++) {
1803
            mx[i] = s->last_picture.motion_val[0][s->block_index[i]][0];
1804
            my[i] = s->last_picture.motion_val[0][s->block_index[i]][1];
1805
        }
1806
        mv_x = median4(mx[0], mx[1], mx[2], mx[3]);
1807
        mv_y = median4(my[0], my[1], my[2], my[3]);
1808
        s->mv[0][0][0] = (mv_x * v->bfraction + B_FRACTION_DEN/2) / B_FRACTION_DEN;
1809
        s->mv[0][0][1] = (mv_y * v->bfraction + B_FRACTION_DEN/2) / B_FRACTION_DEN;
1810
        vc1_mc_1mv(v, 0);
1811
1812
        for(i = 0; i < 4; i++) {
1813
            mx[i] = s->next_picture.motion_val[0][s->block_index[i]][0];
1814
            my[i] = s->next_picture.motion_val[0][s->block_index[i]][1];
1815
        }
1816
        mv_x = median4(mx[0], mx[1], mx[2], mx[3]);
1817
        mv_y = median4(my[0], my[1], my[2], my[3]);
1818
        s->mv[1][0][0] = (mv_x * (B_FRACTION_DEN - v->bfraction) + B_FRACTION_DEN/2) / B_FRACTION_DEN;
1819
        s->mv[1][0][1] = (mv_y * (B_FRACTION_DEN - v->bfraction) + B_FRACTION_DEN/2) / B_FRACTION_DEN;
1820
        vc1_interp_mc(v);
1821
        return;
1822
    }
1823
    if(mode == BMV_TYPE_INTERPOLATED) {
1824
        s->mv[0][0][0] = dmv_x[0];
1825
        s->mv[0][0][1] = dmv_y[0];
1826
        vc1_mc_1mv(v, 0);
1827
        s->mv[1][0][0] = dmv_x[1];
1828
        s->mv[1][0][1] = dmv_y[1];
1829
        vc1_interp_mc(v);
1830 5df68893 Kostya Shishkov
        return;
1831
    }
1832
1833
    if(mode == BMV_TYPE_BACKWARD) {
1834
        for(i = 0; i < 4; i++) {
1835
            mx[i] = s->last_picture.motion_val[0][s->block_index[i]][0];
1836
            my[i] = s->last_picture.motion_val[0][s->block_index[i]][1];
1837
        }
1838
    } else {
1839
        for(i = 0; i < 4; i++) {
1840
            mx[i] = s->next_picture.motion_val[0][s->block_index[i]][0];
1841
            my[i] = s->next_picture.motion_val[0][s->block_index[i]][1];
1842
        }
1843
    }
1844
1845
    /* XXX: not right but how to determine 4-MV intra/inter in another frame? */
1846
    mv_x = median4(mx[0], mx[1], mx[2], mx[3]);
1847
    mv_y = median4(my[0], my[1], my[2], my[3]);
1848
    s->mv[0][0][0] = mv_x;
1849
    s->mv[0][0][1] = mv_y;
1850
1851
    vc1_mc_1mv(v, (mode == BMV_TYPE_FORWARD));
1852
}
1853
1854 be3492ec Kostya Shishkov
/** Get predicted DC value for I-frames only
1855
 * prediction dir: left=0, top=1
1856
 * @param s MpegEncContext
1857
 * @param[in] n block index in the current MB
1858
 * @param dc_val_ptr Pointer to DC predictor
1859
 * @param dir_ptr Prediction direction for use in AC prediction
1860 2ce151f8 anonymous
 */
1861 be3492ec Kostya Shishkov
static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
1862
                              int16_t **dc_val_ptr, int *dir_ptr)
1863 21aa398f Alex Beregszaszi
{
1864 be3492ec Kostya Shishkov
    int a, b, c, wrap, pred, scale;
1865
    int16_t *dc_val;
1866
    static const uint16_t dcpred[32] = {
1867
    -1, 1024,  512,  341,  256,  205,  171,  146,  128,
1868
         114,  102,   93,   85,   79,   73,   68,   64,
1869
          60,   57,   54,   51,   49,   47,   45,   43,
1870
          41,   39,   38,   37,   35,   34,   33
1871
    };
1872 0d33db8a anonymous
1873 be3492ec Kostya Shishkov
    /* find prediction - wmv3_dc_scale always used here in fact */
1874
    if (n < 4)     scale = s->y_dc_scale;
1875
    else           scale = s->c_dc_scale;
1876 21aa398f Alex Beregszaszi
1877 be3492ec Kostya Shishkov
    wrap = s->block_wrap[n];
1878
    dc_val= s->dc_val[0] + s->block_index[n];
1879 21aa398f Alex Beregszaszi
1880 be3492ec Kostya Shishkov
    /* B A
1881
     * C X
1882
     */
1883
    c = dc_val[ - 1];
1884
    b = dc_val[ - 1 - wrap];
1885
    a = dc_val[ - wrap];
1886 21aa398f Alex Beregszaszi
1887 be3492ec Kostya Shishkov
    if (pq < 9 || !overlap)
1888 21aa398f Alex Beregszaszi
    {
1889 be3492ec Kostya Shishkov
        /* Set outer values */
1890 d2779ecd Kostya Shishkov
        if (s->first_slice_line && (n!=2 && n!=3)) b=a=dcpred[scale];
1891 be3492ec Kostya Shishkov
        if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=dcpred[scale];
1892 bf2bc926 anonymous
    }
1893
    else
1894
    {
1895 be3492ec Kostya Shishkov
        /* Set outer values */
1896 d2779ecd Kostya Shishkov
        if (s->first_slice_line && (n!=2 && n!=3)) b=a=0;
1897 be3492ec Kostya Shishkov
        if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=0;
1898 21aa398f Alex Beregszaszi
    }
1899
1900 be3492ec Kostya Shishkov
    if (abs(a - b) <= abs(b - c)) {
1901
        pred = c;
1902
        *dir_ptr = 1;//left
1903
    } else {
1904
        pred = a;
1905
        *dir_ptr = 0;//top
1906 21aa398f Alex Beregszaszi
    }
1907
1908 be3492ec Kostya Shishkov
    /* update predictor */
1909
    *dc_val_ptr = &dc_val[0];
1910
    return pred;
1911 bf2bc926 anonymous
}
1912
1913 21aa398f Alex Beregszaszi
1914 be3492ec Kostya Shishkov
/** Get predicted DC value
1915
 * prediction dir: left=0, top=1
1916
 * @param s MpegEncContext
1917
 * @param[in] n block index in the current MB
1918
 * @param dc_val_ptr Pointer to DC predictor
1919
 * @param dir_ptr Prediction direction for use in AC prediction
1920 2ce151f8 anonymous
 */
1921 be3492ec Kostya Shishkov
static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
1922
                              int a_avail, int c_avail,
1923
                              int16_t **dc_val_ptr, int *dir_ptr)
1924 21aa398f Alex Beregszaszi
{
1925 be3492ec Kostya Shishkov
    int a, b, c, wrap, pred, scale;
1926
    int16_t *dc_val;
1927
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1928 50af2fd3 Kostya Shishkov
    int q1, q2 = 0;
1929 21aa398f Alex Beregszaszi
1930 be3492ec Kostya Shishkov
    /* find prediction - wmv3_dc_scale always used here in fact */
1931
    if (n < 4)     scale = s->y_dc_scale;
1932
    else           scale = s->c_dc_scale;
1933 21aa398f Alex Beregszaszi
1934 be3492ec Kostya Shishkov
    wrap = s->block_wrap[n];
1935
    dc_val= s->dc_val[0] + s->block_index[n];
1936 21aa398f Alex Beregszaszi
1937 be3492ec Kostya Shishkov
    /* B A
1938
     * C X
1939
     */
1940
    c = dc_val[ - 1];
1941
    b = dc_val[ - 1 - wrap];
1942
    a = dc_val[ - wrap];
1943 27ed1a0d Kostya Shishkov
    /* scale predictors if needed */
1944
    q1 = s->current_picture.qscale_table[mb_pos];
1945
    if(c_avail && (n!= 1 && n!=3)) {
1946
        q2 = s->current_picture.qscale_table[mb_pos - 1];
1947
        if(q2 && q2 != q1)
1948
            c = (c * s->y_dc_scale_table[q2] * vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
1949
    }
1950
    if(a_avail && (n!= 2 && n!=3)) {
1951
        q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
1952
        if(q2 && q2 != q1)
1953
            a = (a * s->y_dc_scale_table[q2] * vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
1954
    }
1955
    if(a_avail && c_avail && (n!=3)) {
1956
        int off = mb_pos;
1957
        if(n != 1) off--;
1958
        if(n != 2) off -= s->mb_stride;
1959
        q2 = s->current_picture.qscale_table[off];
1960
        if(q2 && q2 != q1)
1961
            b = (b * s->y_dc_scale_table[q2] * vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
1962
    }
1963 21aa398f Alex Beregszaszi
1964 be3492ec Kostya Shishkov
    if(a_avail && c_avail) {
1965
        if(abs(a - b) <= abs(b - c)) {
1966
            pred = c;
1967
            *dir_ptr = 1;//left
1968
        } else {
1969
            pred = a;
1970
            *dir_ptr = 0;//top
1971
        }
1972
    } else if(a_avail) {
1973
        pred = a;
1974
        *dir_ptr = 0;//top
1975
    } else if(c_avail) {
1976
        pred = c;
1977
        *dir_ptr = 1;//left
1978
    } else {
1979
        pred = 0;
1980
        *dir_ptr = 1;//left
1981 21aa398f Alex Beregszaszi
    }
1982
1983 be3492ec Kostya Shishkov
    /* update predictor */
1984
    *dc_val_ptr = &dc_val[0];
1985
    return pred;
1986 21aa398f Alex Beregszaszi
}
1987 2ce151f8 anonymous
1988 21aa398f Alex Beregszaszi
1989 115329f1 Diego Biurrun
/**
1990 be3492ec Kostya Shishkov
 * @defgroup std_mb VC1 Macroblock-level functions in Simple/Main Profiles
1991
 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1992
 * @todo TODO: Integrate to MpegEncContext facilities
1993 2ce151f8 anonymous
 * @{
1994
 */
1995 21aa398f Alex Beregszaszi
1996 be3492ec Kostya Shishkov
static inline int vc1_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
1997 21aa398f Alex Beregszaszi
{
1998 be3492ec Kostya Shishkov
    int xy, wrap, pred, a, b, c;
1999 21aa398f Alex Beregszaszi
2000 be3492ec Kostya Shishkov
    xy = s->block_index[n];
2001
    wrap = s->b8_stride;
2002 21aa398f Alex Beregszaszi
2003 be3492ec Kostya Shishkov
    /* B C
2004
     * A X
2005
     */
2006
    a = s->coded_block[xy - 1       ];
2007
    b = s->coded_block[xy - 1 - wrap];
2008
    c = s->coded_block[xy     - wrap];
2009 21aa398f Alex Beregszaszi
2010 be3492ec Kostya Shishkov
    if (b == c) {
2011
        pred = a;
2012
    } else {
2013
        pred = c;
2014 21aa398f Alex Beregszaszi
    }
2015 be3492ec Kostya Shishkov
2016
    /* store value */
2017
    *coded_block_ptr = &s->coded_block[xy];
2018
2019
    return pred;
2020 0d33db8a anonymous
}
2021
2022 be3492ec Kostya Shishkov
/**
2023
 * Decode one AC coefficient
2024
 * @param v The VC1 context
2025
 * @param last Last coefficient
2026
 * @param skip How much zero coefficients to skip
2027
 * @param value Decoded AC coefficient value
2028
 * @see 8.1.3.4
2029 2ce151f8 anonymous
 */
2030 be3492ec Kostya Shishkov
static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip, int *value, int codingset)
2031 0d33db8a anonymous
{
2032
    GetBitContext *gb = &v->s.gb;
2033 be3492ec Kostya Shishkov
    int index, escape, run = 0, level = 0, lst = 0;
2034
2035
    index = get_vlc2(gb, vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2036
    if (index != vc1_ac_sizes[codingset] - 1) {
2037
        run = vc1_index_decode_table[codingset][index][0];
2038
        level = vc1_index_decode_table[codingset][index][1];
2039
        lst = index >= vc1_last_decode_table[codingset];
2040
        if(get_bits(gb, 1))
2041
            level = -level;
2042
    } else {
2043
        escape = decode210(gb);
2044 87dfe848 Kostya Shishkov
        if (escape != 2) {
2045 be3492ec Kostya Shishkov
            index = get_vlc2(gb, vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2046
            run = vc1_index_decode_table[codingset][index][0];
2047
            level = vc1_index_decode_table[codingset][index][1];
2048
            lst = index >= vc1_last_decode_table[codingset];
2049 87dfe848 Kostya Shishkov
            if(escape == 0) {
2050
                if(lst)
2051
                    level += vc1_last_delta_level_table[codingset][run];
2052
                else
2053
                    level += vc1_delta_level_table[codingset][run];
2054
            } else {
2055
                if(lst)
2056
                    run += vc1_last_delta_run_table[codingset][level] + 1;
2057
                else
2058
                    run += vc1_delta_run_table[codingset][level] + 1;
2059
            }
2060 be3492ec Kostya Shishkov
            if(get_bits(gb, 1))
2061
                level = -level;
2062
        } else {
2063
            int sign;
2064
            lst = get_bits(gb, 1);
2065
            if(v->s.esc3_level_length == 0) {
2066
                if(v->pq < 8 || v->dquantfrm) { // table 59
2067
                    v->s.esc3_level_length = get_bits(gb, 3);
2068
                    if(!v->s.esc3_level_length)
2069
                        v->s.esc3_level_length = get_bits(gb, 2) + 8;
2070
                } else { //table 60
2071
                    v->s.esc3_level_length = get_prefix(gb, 1, 6) + 2;
2072
                }
2073
                v->s.esc3_run_length = 3 + get_bits(gb, 2);
2074
            }
2075
            run = get_bits(gb, v->s.esc3_run_length);
2076
            sign = get_bits(gb, 1);
2077
            level = get_bits(gb, v->s.esc3_level_length);
2078
            if(sign)
2079
                level = -level;
2080
        }
2081 21aa398f Alex Beregszaszi
    }
2082 7cc84d24 anonymous
2083 be3492ec Kostya Shishkov
    *last = lst;
2084
    *skip = run;
2085
    *value = level;
2086 21aa398f Alex Beregszaszi
}
2087
2088 be3492ec Kostya Shishkov
/** Decode intra block in intra frames - should be faster than decode_intra_block
2089
 * @param v VC1Context
2090
 * @param block block to decode
2091
 * @param coded are AC coeffs present or not
2092
 * @param codingset set of VLC to decode data
2093 2ce151f8 anonymous
 */
2094 be3492ec Kostya Shishkov
static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset)
2095 21aa398f Alex Beregszaszi
{
2096 0d33db8a anonymous
    GetBitContext *gb = &v->s.gb;
2097 be3492ec Kostya Shishkov
    MpegEncContext *s = &v->s;
2098
    int dc_pred_dir = 0; /* Direction of the DC prediction used */
2099
    int run_diff, i;
2100
    int16_t *dc_val;
2101
    int16_t *ac_val, *ac_val2;
2102
    int dcdiff;
2103 21aa398f Alex Beregszaszi
2104 be3492ec Kostya Shishkov
    /* Get DC differential */
2105
    if (n < 4) {
2106
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2107
    } else {
2108
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2109 21aa398f Alex Beregszaszi
    }
2110 be3492ec Kostya Shishkov
    if (dcdiff < 0){
2111
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2112
        return -1;
2113
    }
2114
    if (dcdiff)
2115 21aa398f Alex Beregszaszi
    {
2116 be3492ec Kostya Shishkov
        if (dcdiff == 119 /* ESC index value */)
2117 21aa398f Alex Beregszaszi
        {
2118 be3492ec Kostya Shishkov
            /* TODO: Optimize */
2119
            if (v->pq == 1) dcdiff = get_bits(gb, 10);
2120
            else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2121
            else dcdiff = get_bits(gb, 8);
2122 21aa398f Alex Beregszaszi
        }
2123 be3492ec Kostya Shishkov
        else
2124 21aa398f Alex Beregszaszi
        {
2125 be3492ec Kostya Shishkov
            if (v->pq == 1)
2126
                dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
2127
            else if (v->pq == 2)
2128
                dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
2129 21aa398f Alex Beregszaszi
        }
2130 be3492ec Kostya Shishkov
        if (get_bits(gb, 1))
2131
            dcdiff = -dcdiff;
2132 21aa398f Alex Beregszaszi
    }
2133
2134 be3492ec Kostya Shishkov
    /* Prediction */
2135
    dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2136
    *dc_val = dcdiff;
2137 0d33db8a anonymous
2138 be3492ec Kostya Shishkov
    /* Store the quantized DC coeff, used for prediction */
2139
    if (n < 4) {
2140
        block[0] = dcdiff * s->y_dc_scale;
2141
    } else {
2142
        block[0] = dcdiff * s->c_dc_scale;
2143 21aa398f Alex Beregszaszi
    }
2144 be3492ec Kostya Shishkov
    /* Skip ? */
2145
    run_diff = 0;
2146
    i = 0;
2147
    if (!coded) {
2148
        goto not_coded;
2149 21aa398f Alex Beregszaszi
    }
2150 7cc84d24 anonymous
2151 be3492ec Kostya Shishkov
    //AC Decoding
2152
    i = 1;
2153 7cc84d24 anonymous
2154 be3492ec Kostya Shishkov
    {
2155
        int last = 0, skip, value;
2156
        const int8_t *zz_table;
2157
        int scale;
2158
        int k;
2159 8da75fb2 anonymous
2160 be3492ec Kostya Shishkov
        scale = v->pq * 2 + v->halfpq;
2161 42cc17f9 Ivan Kalvachev
2162 be3492ec Kostya Shishkov
        if(v->s.ac_pred) {
2163
            if(!dc_pred_dir)
2164
                zz_table = vc1_horizontal_zz;
2165
            else
2166
                zz_table = vc1_vertical_zz;
2167
        } else
2168
            zz_table = vc1_normal_zz;
2169
2170
        ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2171
        ac_val2 = ac_val;
2172
        if(dc_pred_dir) //left
2173
            ac_val -= 16;
2174
        else //top
2175
            ac_val -= 16 * s->block_wrap[n];
2176
2177
        while (!last) {
2178
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2179
            i += skip;
2180
            if(i > 63)
2181
                break;
2182
            block[zz_table[i++]] = value;
2183
        }
2184 7cc84d24 anonymous
2185 be3492ec Kostya Shishkov
        /* apply AC prediction if needed */
2186
        if(s->ac_pred) {
2187
            if(dc_pred_dir) { //left
2188
                for(k = 1; k < 8; k++)
2189
                    block[k << 3] += ac_val[k];
2190
            } else { //top
2191
                for(k = 1; k < 8; k++)
2192
                    block[k] += ac_val[k + 8];
2193
            }
2194
        }
2195
        /* save AC coeffs for further prediction */
2196
        for(k = 1; k < 8; k++) {
2197
            ac_val2[k] = block[k << 3];
2198
            ac_val2[k + 8] = block[k];
2199
        }
2200 42cc17f9 Ivan Kalvachev
2201 be3492ec Kostya Shishkov
        /* scale AC coeffs */
2202
        for(k = 1; k < 64; k++)
2203
            if(block[k]) {
2204
                block[k] *= scale;
2205
                if(!v->pquantizer)
2206
                    block[k] += (block[k] < 0) ? -v->pq : v->pq;
2207
            }
2208 8da75fb2 anonymous
2209 be3492ec Kostya Shishkov
        if(s->ac_pred) i = 63;
2210
    }
2211
2212
not_coded:
2213
    if(!coded) {
2214
        int k, scale;
2215
        ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2216
        ac_val2 = ac_val;
2217
2218
        scale = v->pq * 2 + v->halfpq;
2219
        memset(ac_val2, 0, 16 * 2);
2220
        if(dc_pred_dir) {//left
2221
            ac_val -= 16;
2222
            if(s->ac_pred)
2223
                memcpy(ac_val2, ac_val, 8 * 2);
2224
        } else {//top
2225
            ac_val -= 16 * s->block_wrap[n];
2226
            if(s->ac_pred)
2227
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2228 8da75fb2 anonymous
        }
2229 7cc84d24 anonymous
2230 be3492ec Kostya Shishkov
        /* apply AC prediction if needed */
2231
        if(s->ac_pred) {
2232
            if(dc_pred_dir) { //left
2233
                for(k = 1; k < 8; k++) {
2234
                    block[k << 3] = ac_val[k] * scale;
2235 4b944659 Kostya Shishkov
                    if(!v->pquantizer && block[k << 3])
2236 be3492ec Kostya Shishkov
                        block[k << 3] += (block[k << 3] < 0) ? -v->pq : v->pq;
2237
                }
2238
            } else { //top
2239
                for(k = 1; k < 8; k++) {
2240
                    block[k] = ac_val[k + 8] * scale;
2241 4b944659 Kostya Shishkov
                    if(!v->pquantizer && block[k])
2242 be3492ec Kostya Shishkov
                        block[k] += (block[k] < 0) ? -v->pq : v->pq;
2243
                }
2244
            }
2245
            i = 63;
2246
        }
2247 7cc84d24 anonymous
    }
2248 be3492ec Kostya Shishkov
    s->block_last_index[n] = i;
2249 7cc84d24 anonymous
2250 be3492ec Kostya Shishkov
    return 0;
2251 7cc84d24 anonymous
}
2252
2253 be3492ec Kostya Shishkov
/** Decode intra block in inter frames - more generic version than vc1_decode_i_block
2254
 * @param v VC1Context
2255
 * @param block block to decode
2256
 * @param coded are AC coeffs present or not
2257
 * @param mquant block quantizer
2258
 * @param codingset set of VLC to decode data
2259 7cc84d24 anonymous
 */
2260 be3492ec Kostya Shishkov
static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n, int coded, int mquant, int codingset)
2261 e5540b3f anonymous
{
2262 0d33db8a anonymous
    GetBitContext *gb = &v->s.gb;
2263 7cc84d24 anonymous
    MpegEncContext *s = &v->s;
2264 be3492ec Kostya Shishkov
    int dc_pred_dir = 0; /* Direction of the DC prediction used */
2265 7cc84d24 anonymous
    int run_diff, i;
2266 be3492ec Kostya Shishkov
    int16_t *dc_val;
2267
    int16_t *ac_val, *ac_val2;
2268
    int dcdiff;
2269
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2270 f26c2ef5 Kostya Shishkov
    int a_avail = v->a_avail, c_avail = v->c_avail;
2271 4a5343fa Kostya Shishkov
    int use_pred = s->ac_pred;
2272
    int scale;
2273 50af2fd3 Kostya Shishkov
    int q1, q2 = 0;
2274 e5540b3f anonymous
2275 8da75fb2 anonymous
    /* XXX: Guard against dumb values of mquant */
2276
    mquant = (mquant < 1) ? 0 : ( (mquant>31) ? 31 : mquant );
2277
2278 be3492ec Kostya Shishkov
    /* Set DC scale - y and c use the same */
2279
    s->y_dc_scale = s->y_dc_scale_table[mquant];
2280
    s->c_dc_scale = s->c_dc_scale_table[mquant];
2281
2282
    /* Get DC differential */
2283
    if (n < 4) {
2284
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2285
    } else {
2286
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2287
    }
2288
    if (dcdiff < 0){
2289
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2290
        return -1;
2291
    }
2292
    if (dcdiff)
2293
    {
2294
        if (dcdiff == 119 /* ESC index value */)
2295
        {
2296
            /* TODO: Optimize */
2297
            if (mquant == 1) dcdiff = get_bits(gb, 10);
2298
            else if (mquant == 2) dcdiff = get_bits(gb, 9);
2299
            else dcdiff = get_bits(gb, 8);
2300
        }
2301
        else
2302
        {
2303
            if (mquant == 1)
2304
                dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
2305
            else if (mquant == 2)
2306
                dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
2307
        }
2308
        if (get_bits(gb, 1))
2309
            dcdiff = -dcdiff;
2310
    }
2311
2312
    /* Prediction */
2313
    dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
2314
    *dc_val = dcdiff;
2315
2316
    /* Store the quantized DC coeff, used for prediction */
2317
2318
    if (n < 4) {
2319
        block[0] = dcdiff * s->y_dc_scale;
2320
    } else {
2321
        block[0] = dcdiff * s->c_dc_scale;
2322
    }
2323
    /* Skip ? */
2324
    run_diff = 0;
2325
    i = 0;
2326
2327
    //AC Decoding
2328
    i = 1;
2329 8da75fb2 anonymous
2330 4a5343fa Kostya Shishkov
    /* check if AC is needed at all and adjust direction if needed */
2331
    if(!a_avail) dc_pred_dir = 1;
2332
    if(!c_avail) dc_pred_dir = 0;
2333
    if(!a_avail && !c_avail) use_pred = 0;
2334
    ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2335
    ac_val2 = ac_val;
2336
2337 0a45801f Kostya Shishkov
    scale = mquant * 2 + v->halfpq;
2338 4a5343fa Kostya Shishkov
2339
    if(dc_pred_dir) //left
2340
        ac_val -= 16;
2341
    else //top
2342
        ac_val -= 16 * s->block_wrap[n];
2343
2344 50af2fd3 Kostya Shishkov
    q1 = s->current_picture.qscale_table[mb_pos];
2345
    if(dc_pred_dir && c_avail) q2 = s->current_picture.qscale_table[mb_pos - 1];
2346
    if(!dc_pred_dir && a_avail) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2347
    if(n && n<4) q2 = q1;
2348
2349 4a5343fa Kostya Shishkov
    if(coded) {
2350 be3492ec Kostya Shishkov
        int last = 0, skip, value;
2351
        const int8_t *zz_table;
2352
        int k;
2353
2354 8985c915 Kostya Shishkov
        zz_table = vc1_simple_progressive_8x8_zz;
2355 be3492ec Kostya Shishkov
2356
        while (!last) {
2357
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2358
            i += skip;
2359
            if(i > 63)
2360
                break;
2361
            block[zz_table[i++]] = value;
2362 e5540b3f anonymous
        }
2363 7cc84d24 anonymous
2364 be3492ec Kostya Shishkov
        /* apply AC prediction if needed */
2365 8f8d0e48 Kostya Shishkov
        if(use_pred) {
2366 be3492ec Kostya Shishkov
            /* scale predictors if needed*/
2367 50af2fd3 Kostya Shishkov
            if(q2 && q1!=q2) {
2368 be3492ec Kostya Shishkov
                q1 = q1 * 2 - 1;
2369
                q2 = q2 * 2 - 1;
2370
2371
                if(dc_pred_dir) { //left
2372
                    for(k = 1; k < 8; k++)
2373
                        block[k << 3] += (ac_val[k] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2374
                } else { //top
2375
                    for(k = 1; k < 8; k++)
2376
                        block[k] += (ac_val[k + 8] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2377
                }
2378
            } else {
2379
                if(dc_pred_dir) { //left
2380
                    for(k = 1; k < 8; k++)
2381
                        block[k << 3] += ac_val[k];
2382
                } else { //top
2383
                    for(k = 1; k < 8; k++)
2384
                        block[k] += ac_val[k + 8];
2385
                }
2386
            }
2387 7cc84d24 anonymous
        }
2388 be3492ec Kostya Shishkov
        /* save AC coeffs for further prediction */
2389
        for(k = 1; k < 8; k++) {
2390
            ac_val2[k] = block[k << 3];
2391
            ac_val2[k + 8] = block[k];
2392 e5540b3f anonymous
        }
2393 7cc84d24 anonymous
2394 be3492ec Kostya Shishkov
        /* scale AC coeffs */
2395
        for(k = 1; k < 64; k++)
2396
            if(block[k]) {
2397
                block[k] *= scale;
2398
                if(!v->pquantizer)
2399
                    block[k] += (block[k] < 0) ? -mquant : mquant;
2400
            }
2401 7cc84d24 anonymous
2402 8f8d0e48 Kostya Shishkov
        if(use_pred) i = 63;
2403 4a5343fa Kostya Shishkov
    } else { // no AC coeffs
2404
        int k;
2405 8f8d0e48 Kostya Shishkov
2406 be3492ec Kostya Shishkov
        memset(ac_val2, 0, 16 * 2);
2407
        if(dc_pred_dir) {//left
2408 e4bf0302 Kostya Shishkov
            if(use_pred) {
2409 be3492ec Kostya Shishkov
                memcpy(ac_val2, ac_val, 8 * 2);
2410 50af2fd3 Kostya Shishkov
                if(q2 && q1!=q2) {
2411 e4bf0302 Kostya Shishkov
                    q1 = q1 * 2 - 1;
2412
                    q2 = q2 * 2 - 1;
2413
                    for(k = 1; k < 8; k++)
2414
                        ac_val2[k] = (ac_val2[k] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2415
                }
2416
            }
2417 be3492ec Kostya Shishkov
        } else {//top
2418 e4bf0302 Kostya Shishkov
            if(use_pred) {
2419 be3492ec Kostya Shishkov
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2420 50af2fd3 Kostya Shishkov
                if(q2 && q1!=q2) {
2421 e4bf0302 Kostya Shishkov
                    q1 = q1 * 2 - 1;
2422
                    q2 = q2 * 2 - 1;
2423
                    for(k = 1; k < 8; k++)
2424
                        ac_val2[k + 8] = (ac_val2[k + 8] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2425
                }
2426
            }
2427 be3492ec Kostya Shishkov
        }
2428 7cc84d24 anonymous
2429 be3492ec Kostya Shishkov
        /* apply AC prediction if needed */
2430 8f8d0e48 Kostya Shishkov
        if(use_pred) {
2431 be3492ec Kostya Shishkov
            if(dc_pred_dir) { //left
2432
                for(k = 1; k < 8; k++) {
2433 e4bf0302 Kostya Shishkov
                    block[k << 3] = ac_val2[k] * scale;
2434 4b944659 Kostya Shishkov
                    if(!v->pquantizer && block[k << 3])
2435 be3492ec Kostya Shishkov
                        block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
2436
                }
2437
            } else { //top
2438
                for(k = 1; k < 8; k++) {
2439 e4bf0302 Kostya Shishkov
                    block[k] = ac_val2[k + 8] * scale;
2440 4b944659 Kostya Shishkov
                    if(!v->pquantizer && block[k])
2441 be3492ec Kostya Shishkov
                        block[k] += (block[k] < 0) ? -mquant : mquant;
2442
                }
2443
            }
2444
            i = 63;
2445 7cc84d24 anonymous
        }
2446
    }
2447
    s->block_last_index[n] = i;
2448 be3492ec Kostya Shishkov
2449 e5540b3f anonymous
    return 0;
2450
}
2451 7cc84d24 anonymous
2452 be3492ec Kostya Shishkov
/** Decode P block
2453 2ce151f8 anonymous
 */
2454 be3492ec Kostya Shishkov
static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n, int mquant, int ttmb, int first_block)
2455 7cc84d24 anonymous
{
2456 be3492ec Kostya Shishkov
    MpegEncContext *s = &v->s;
2457
    GetBitContext *gb = &s->gb;
2458
    int i, j;
2459
    int subblkpat = 0;
2460
    int scale, off, idx, last, skip, value;
2461
    int ttblk = ttmb & 7;
2462 42cc17f9 Ivan Kalvachev
2463 be3492ec Kostya Shishkov
    if(ttmb == -1) {
2464
        ttblk = ttblk_to_tt[v->tt_index][get_vlc2(gb, vc1_ttblk_vlc[v->tt_index].table, VC1_TTBLK_VLC_BITS, 1)];
2465 7cc84d24 anonymous
    }
2466 be3492ec Kostya Shishkov
    if(ttblk == TT_4X4) {
2467
        subblkpat = ~(get_vlc2(gb, vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
2468
    }
2469
    if((ttblk != TT_8X8 && ttblk != TT_4X4) && (v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))) {
2470
        subblkpat = decode012(gb);
2471 f26c2ef5 Kostya Shishkov
        if(subblkpat) subblkpat ^= 3; //swap decoded pattern bits
2472 be3492ec Kostya Shishkov
        if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) ttblk = TT_8X4;
2473
        if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) ttblk = TT_4X8;
2474
    }
2475 0a45801f Kostya Shishkov
    scale = 2 * mquant + v->halfpq;
2476 42cc17f9 Ivan Kalvachev
2477 be3492ec Kostya Shishkov
    // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
2478
    if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
2479
        subblkpat = 2 - (ttblk == TT_8X4_TOP);
2480 e9f2396e Kostya Shishkov
        ttblk = TT_8X4;
2481 be3492ec Kostya Shishkov
    }
2482
    if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
2483
        subblkpat = 2 - (ttblk == TT_4X8_LEFT);
2484 e9f2396e Kostya Shishkov
        ttblk = TT_4X8;
2485 be3492ec Kostya Shishkov
    }
2486
    switch(ttblk) {
2487
    case TT_8X8:
2488
        i = 0;
2489
        last = 0;
2490
        while (!last) {
2491
            vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2492
            i += skip;
2493
            if(i > 63)
2494
                break;
2495
            idx = vc1_simple_progressive_8x8_zz[i++];
2496
            block[idx] = value * scale;
2497 0a45801f Kostya Shishkov
            if(!v->pquantizer)
2498
                block[idx] += (block[idx] < 0) ? -mquant : mquant;
2499 7cc84d24 anonymous
        }
2500 5ce425f7 Kostya Shishkov
        s->dsp.vc1_inv_trans_8x8(block);
2501 be3492ec Kostya Shishkov
        break;
2502
    case TT_4X4:
2503
        for(j = 0; j < 4; j++) {
2504
            last = subblkpat & (1 << (3 - j));
2505
            i = 0;
2506 87dfe848 Kostya Shishkov
            off = (j & 1) * 4 + (j & 2) * 16;
2507 be3492ec Kostya Shishkov
            while (!last) {
2508
                vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2509
                i += skip;
2510
                if(i > 15)
2511
                    break;
2512
                idx = vc1_simple_progressive_4x4_zz[i++];
2513
                block[idx + off] = value * scale;
2514 0a45801f Kostya Shishkov
                if(!v->pquantizer)
2515
                    block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
2516 be3492ec Kostya Shishkov
            }
2517 87dfe848 Kostya Shishkov
            if(!(subblkpat & (1 << (3 - j))))
2518 5ce425f7 Kostya Shishkov
                s->dsp.vc1_inv_trans_4x4(block, j);
2519 be3492ec Kostya Shishkov
        }
2520
        break;
2521
    case TT_8X4:
2522
        for(j = 0; j < 2; j++) {
2523
            last = subblkpat & (1 << (1 - j));
2524
            i = 0;
2525
            off = j * 32;
2526
            while (!last) {
2527
                vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2528
                i += skip;
2529
                if(i > 31)
2530
                    break;
2531
                idx = vc1_simple_progressive_8x4_zz[i++];
2532
                block[idx + off] = value * scale;
2533 0a45801f Kostya Shishkov
                if(!v->pquantizer)
2534
                    block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
2535 be3492ec Kostya Shishkov
            }
2536 87dfe848 Kostya Shishkov
            if(!(subblkpat & (1 << (1 - j))))
2537 5ce425f7 Kostya Shishkov
                s->dsp.vc1_inv_trans_8x4(block, j);
2538 be3492ec Kostya Shishkov
        }
2539
        break;
2540
    case TT_4X8:
2541
        for(j = 0; j < 2; j++) {
2542
            last = subblkpat & (1 << (1 - j));
2543
            i = 0;
2544
            off = j * 4;
2545
            while (!last) {
2546
                vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2547
                i += skip;
2548
                if(i > 31)
2549
                    break;
2550 c436ff41 Kostya Shishkov
                idx = vc1_simple_progressive_4x8_zz[i++];
2551 be3492ec Kostya Shishkov
                block[idx + off] = value * scale;
2552 0a45801f Kostya Shishkov
                if(!v->pquantizer)
2553
                    block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
2554 be3492ec Kostya Shishkov
            }
2555 87dfe848 Kostya Shishkov
            if(!(subblkpat & (1 << (1 - j))))
2556 5ce425f7 Kostya Shishkov
                s->dsp.vc1_inv_trans_4x8(block, j);
2557 8da75fb2 anonymous
        }
2558 be3492ec Kostya Shishkov
        break;
2559 7cc84d24 anonymous
    }
2560
    return 0;
2561
}
2562
2563 be3492ec Kostya Shishkov
2564 8da75fb2 anonymous
/** Decode one P-frame MB (in Simple/Main profile)
2565
 * @todo TODO: Extend to AP
2566
 * @fixme FIXME: DC value for inter blocks not set
2567
 */
2568 7e84f276 Kostya Shishkov
static int vc1_decode_p_mb(VC1Context *v)
2569 7cc84d24 anonymous
{
2570
    MpegEncContext *s = &v->s;
2571
    GetBitContext *gb = &s->gb;
2572 87dfe848 Kostya Shishkov
    int i, j;
2573 be3492ec Kostya Shishkov
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2574 7cc84d24 anonymous
    int cbp; /* cbp decoding stuff */
2575
    int mqdiff, mquant; /* MB quantization */
2576 8a66a390 Kostya Shishkov
    int ttmb = v->ttfrm; /* MB Transform type */
2577 7cc84d24 anonymous
    int status;
2578
2579
    static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
2580
      offset_table[6] = { 0, 1, 3, 7, 15, 31 };
2581
    int mb_has_coeffs = 1; /* last_flag */
2582
    int dmv_x, dmv_y; /* Differential MV components */
2583
    int index, index1; /* LUT indices */
2584
    int val, sign; /* temp values */
2585 be3492ec Kostya Shishkov
    int first_block = 1;
2586
    int dst_idx, off;
2587 87dfe848 Kostya Shishkov
    int skipped, fourmv;
2588 7cc84d24 anonymous
2589 8da75fb2 anonymous
    mquant = v->pq; /* Loosy initialization */
2590
2591 87dfe848 Kostya Shishkov
    if (v->mv_type_is_raw)
2592
        fourmv = get_bits1(gb);
2593
    else
2594
        fourmv = v->mv_type_mb_plane[mb_pos];
2595
    if (v->skip_is_raw)
2596
        skipped = get_bits1(gb);
2597
    else
2598 0f7344aa Kostya Shishkov
        skipped = v->s.mbskip_table[mb_pos];
2599 87dfe848 Kostya Shishkov
2600 e4bf0302 Kostya Shishkov
    s->dsp.clear_blocks(s->block[0]);
2601
2602 87dfe848 Kostya Shishkov
    if (!fourmv) /* 1MV mode */
2603 7cc84d24 anonymous
    {
2604 87dfe848 Kostya Shishkov
        if (!skipped)
2605 7cc84d24 anonymous
        {
2606
            GET_MVDATA(dmv_x, dmv_y);
2607 42cc17f9 Ivan Kalvachev
2608 be3492ec Kostya Shishkov
            s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
2609 e4bf0302 Kostya Shishkov
            vc1_pred_mv(s, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
2610 be3492ec Kostya Shishkov
2611 8da75fb2 anonymous
            /* FIXME Set DC val for inter block ? */
2612 7cc84d24 anonymous
            if (s->mb_intra && !mb_has_coeffs)
2613
            {
2614
                GET_MQUANT();
2615
                s->ac_pred = get_bits(gb, 1);
2616 8da75fb2 anonymous
                cbp = 0;
2617 7cc84d24 anonymous
            }
2618
            else if (mb_has_coeffs)
2619
            {
2620
                if (s->mb_intra) s->ac_pred = get_bits(gb, 1);
2621 10b9c374 Kostya Shishkov
                cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2622 7cc84d24 anonymous
                GET_MQUANT();
2623
            }
2624
            else
2625
            {
2626
                mquant = v->pq;
2627 8da75fb2 anonymous
                cbp = 0;
2628 7cc84d24 anonymous
            }
2629 be3492ec Kostya Shishkov
            s->current_picture.qscale_table[mb_pos] = mquant;
2630 7cc84d24 anonymous
2631 be3492ec Kostya Shishkov
            if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
2632 10b9c374 Kostya Shishkov
                ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table,
2633 be3492ec Kostya Shishkov
                                VC1_TTMB_VLC_BITS, 2);
2634 5df68893 Kostya Shishkov
            if(!s->mb_intra) vc1_mc_1mv(v, 0);
2635 be3492ec Kostya Shishkov
            dst_idx = 0;
2636 7cc84d24 anonymous
            for (i=0; i<6; i++)
2637
            {
2638 be3492ec Kostya Shishkov
                s->dc_val[0][s->block_index[i]] = 0;
2639
                dst_idx += i >> 2;
2640 7cc84d24 anonymous
                val = ((cbp >> (5 - i)) & 1);
2641 be3492ec Kostya Shishkov
                off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2642 e4bf0302 Kostya Shishkov
                v->mb_type[0][s->block_index[i]] = s->mb_intra;
2643 be3492ec Kostya Shishkov
                if(s->mb_intra) {
2644 f26c2ef5 Kostya Shishkov
                    /* check if prediction blocks A and C are available */
2645
                    v->a_avail = v->c_avail = 0;
2646 d2779ecd Kostya Shishkov
                    if(i == 2 || i == 3 || !s->first_slice_line)
2647 e4bf0302 Kostya Shishkov
                        v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2648
                    if(i == 1 || i == 3 || s->mb_x)
2649
                        v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2650 f26c2ef5 Kostya Shishkov
2651 7e84f276 Kostya Shishkov
                    vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
2652 138712fe Kostya Shishkov
                    if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
2653 5ce425f7 Kostya Shishkov
                    s->dsp.vc1_inv_trans_8x8(s->block[i]);
2654 ffb9a8b1 Kostya Shishkov
                    if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
2655 7e84f276 Kostya Shishkov
                    for(j = 0; j < 64; j++) s->block[i][j] += 128;
2656
                    s->dsp.put_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
2657 f26c2ef5 Kostya Shishkov
                    /* TODO: proper loop filtering */
2658 87dfe848 Kostya Shishkov
                    if(v->pq >= 9 && v->overlap) {
2659
                        if(v->a_avail)
2660 5ce425f7 Kostya Shishkov
                            s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2), (i<4) ? ((i&1)>>1) : (s->mb_y&1));
2661 87dfe848 Kostya Shishkov
                        if(v->c_avail)
2662 5ce425f7 Kostya Shishkov
                            s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2), (i<4) ? (i&1) : (s->mb_x&1));
2663 87dfe848 Kostya Shishkov
                    }
2664 be3492ec Kostya Shishkov
                } else if(val) {
2665 7e84f276 Kostya Shishkov
                    vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block);
2666 be3492ec Kostya Shishkov
                    if(!v->ttmbf && ttmb < 8) ttmb = -1;
2667
                    first_block = 0;
2668 138712fe Kostya Shishkov
                    if((i<4) || !(s->flags & CODEC_FLAG_GRAY))
2669
                        s->dsp.add_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
2670 7cc84d24 anonymous
                }
2671
            }
2672
        }
2673
        else //Skipped
2674
        {
2675 be3492ec Kostya Shishkov
            s->mb_intra = 0;
2676 92ad0d9d Kostya Shishkov
            for(i = 0; i < 6; i++) {
2677
                v->mb_type[0][s->block_index[i]] = 0;
2678
                s->dc_val[0][s->block_index[i]] = 0;
2679
            }
2680 be3492ec Kostya Shishkov
            s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
2681 e4bf0302 Kostya Shishkov
            s->current_picture.qscale_table[mb_pos] = 0;
2682
            vc1_pred_mv(s, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);
2683 5df68893 Kostya Shishkov
            vc1_mc_1mv(v, 0);
2684 7cc84d24 anonymous
            return 0;
2685
        }
2686
    } //1MV mode
2687
    else //4MV mode
2688 e4bf0302 Kostya Shishkov
    {
2689 87dfe848 Kostya Shishkov
        if (!skipped /* unskipped MB */)
2690 7cc84d24 anonymous
        {
2691 e4bf0302 Kostya Shishkov
            int intra_count = 0, coded_inter = 0;
2692
            int is_intra[6], is_coded[6];
2693 7cc84d24 anonymous
            /* Get CBPCY */
2694 10b9c374 Kostya Shishkov
            cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2695 e4bf0302 Kostya Shishkov
            for (i=0; i<6; i++)
2696 7cc84d24 anonymous
            {
2697
                val = ((cbp >> (5 - i)) & 1);
2698 e4bf0302 Kostya Shishkov
                s->dc_val[0][s->block_index[i]] = 0;
2699
                s->mb_intra = 0;
2700
                if(i < 4) {
2701
                    dmv_x = dmv_y = 0;
2702
                    s->mb_intra = 0;
2703
                    mb_has_coeffs = 0;
2704
                    if(val) {
2705
                        GET_MVDATA(dmv_x, dmv_y);
2706
                    }
2707
                    vc1_pred_mv(s, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);
2708
                    if(!s->mb_intra) vc1_mc_4mv_luma(v, i);
2709
                    intra_count += s->mb_intra;
2710
                    is_intra[i] = s->mb_intra;
2711
                    is_coded[i] = mb_has_coeffs;
2712
                }
2713
                if(i&4){
2714
                    is_intra[i] = (intra_count >= 3);
2715
                    is_coded[i] = val;
2716 7cc84d24 anonymous
                }
2717 e4bf0302 Kostya Shishkov
                if(i == 4) vc1_mc_4mv_chroma(v);
2718
                v->mb_type[0][s->block_index[i]] = is_intra[i];
2719
                if(!coded_inter) coded_inter = !is_intra[i] & is_coded[i];
2720 be3492ec Kostya Shishkov
            }
2721 fdb59832 Kostya Shishkov
            // if there are no coded blocks then don't do anything more
2722 c39e3c6f Kostya Shishkov
            if(!intra_count && !coded_inter) return 0;
2723 e4bf0302 Kostya Shishkov
            dst_idx = 0;
2724
            GET_MQUANT();
2725
            s->current_picture.qscale_table[mb_pos] = mquant;
2726
            /* test if block is intra and has pred */
2727
            {
2728
                int intrapred = 0;
2729
                for(i=0; i<6; i++)
2730
                    if(is_intra[i]) {
2731 d2779ecd Kostya Shishkov
                        if(((!s->first_slice_line || (i==2 || i==3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
2732
                            || ((s->mb_x || (i==1 || i==3)) && v->mb_type[0][s->block_index[i] - 1])) {
2733 e4bf0302 Kostya Shishkov
                            intrapred = 1;
2734
                            break;
2735
                        }
2736
                    }
2737
                if(intrapred)s->ac_pred = get_bits(gb, 1);
2738
                else s->ac_pred = 0;
2739
            }
2740
            if (!v->ttmbf && coded_inter)
2741 c39e3c6f Kostya Shishkov
                ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2742 e4bf0302 Kostya Shishkov
            for (i=0; i<6; i++)
2743
            {
2744
                dst_idx += i >> 2;
2745
                off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2746
                s->mb_intra = is_intra[i];
2747
                if (is_intra[i]) {
2748
                    /* check if prediction blocks A and C are available */
2749
                    v->a_avail = v->c_avail = 0;
2750 d2779ecd Kostya Shishkov
                    if(i == 2 || i == 3 || !s->first_slice_line)
2751 e4bf0302 Kostya Shishkov
                        v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2752
                    if(i == 1 || i == 3 || s->mb_x)
2753
                        v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2754 8da75fb2 anonymous
2755 e4bf0302 Kostya Shishkov
                    vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant, (i&4)?v->codingset2:v->codingset);
2756 138712fe Kostya Shishkov
                    if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
2757 5ce425f7 Kostya Shishkov
                    s->dsp.vc1_inv_trans_8x8(s->block[i]);
2758 ffb9a8b1 Kostya Shishkov
                    if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
2759 7e84f276 Kostya Shishkov
                    for(j = 0; j < 64; j++) s->block[i][j] += 128;
2760 ef6cc8ce Kostya Shishkov
                    s->dsp.put_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
2761 e4bf0302 Kostya Shishkov
                    /* TODO: proper loop filtering */
2762
                    if(v->pq >= 9 && v->overlap) {
2763
                        if(v->a_avail)
2764 5ce425f7 Kostya Shishkov
                            s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2), (i<4) ? ((i&1)>>1) : (s->mb_y&1));
2765 e4bf0302 Kostya Shishkov
                        if(v->c_avail)
2766 5ce425f7 Kostya Shishkov
                            s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2), (i<4) ? (i&1) : (s->mb_x&1));
2767 be3492ec Kostya Shishkov
                    }
2768 e4bf0302 Kostya Shishkov
                } else if(is_coded[i]) {
2769
                    status = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block);
2770
                    if(!v->ttmbf && ttmb < 8) ttmb = -1;
2771
                    first_block = 0;
2772 138712fe Kostya Shishkov
                    if((i<4) || !(s->flags & CODEC_FLAG_GRAY))
2773
                        s->dsp.add_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
2774 be3492ec Kostya Shishkov
                }
2775 7cc84d24 anonymous
            }
2776
            return status;
2777
        }
2778
        else //Skipped MB
2779
        {
2780 ef6cc8ce Kostya Shishkov
            s->mb_intra = 0;
2781 c39e3c6f Kostya Shishkov
            s->current_picture.qscale_table[mb_pos] = 0;
2782 92ad0d9d Kostya Shishkov
            for (i=0; i<6; i++) {
2783
                v->mb_type[0][s->block_index[i]] = 0;
2784
                s->dc_val[0][s->block_index[i]] = 0;
2785
            }
2786 7cc84d24 anonymous
            for (i=0; i<4; i++)
2787
            {
2788 e4bf0302 Kostya Shishkov
                vc1_pred_mv(s, i, 0, 0, 0</