AVPROBE_SCORE_MAX doc
[libav.git] / libavcodec / vc1.c
CommitLineData
21aa398f 1/*
10b9c374 2 * VC-1 and WMV3 decoder
be3492ec
KS
3 * Copyright (c) 2006 Konstantin Shishkov
4 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
21aa398f
AB
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
5509bffa 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21aa398f
AB
19 *
20 */
21
22/**
10b9c374
KS
23 * @file vc1.c
24 * VC-1 and WMV3 decoder
21aa398f 25 *
21aa398f
AB
26 */
27#include "common.h"
28#include "dsputil.h"
29#include "avcodec.h"
30#include "mpegvideo.h"
10b9c374 31#include "vc1data.h"
be3492ec 32#include "vc1acdata.h"
0d33db8a 33
9da235c8
MN
34#undef NDEBUG
35#include <assert.h>
36
0c040aac 37extern const uint32_t ff_table0_dc_lum[120][2], ff_table1_dc_lum[120][2];
38extern const uint32_t ff_table0_dc_chroma[120][2], ff_table1_dc_chroma[120][2];
0d33db8a 39extern VLC ff_msmp4_dc_luma_vlc[2], ff_msmp4_dc_chroma_vlc[2];
40#define MB_INTRA_VLC_BITS 9
41extern VLC ff_msmp4_mb_i_vlc;
be3492ec 42extern const uint16_t ff_msmp4_mb_i_table[64][2];
0d33db8a 43#define DC_VLC_BITS 9
be3492ec 44#define AC_VLC_BITS 9
0d33db8a 45static const uint16_t table_mb_intra[64][2];
21aa398f 46
21aa398f 47
2ce151f8 48/** Available Profiles */
49//@{
be3492ec
KS
50enum Profile {
51 PROFILE_SIMPLE,
52 PROFILE_MAIN,
53 PROFILE_COMPLEX, ///< TODO: WMV9 specific
54 PROFILE_ADVANCED
55};
2ce151f8 56//@}
21aa398f 57
2ce151f8 58/** Sequence quantizer mode */
59//@{
be3492ec
KS
60enum 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};
2ce151f8 66//@}
21aa398f 67
2ce151f8 68/** Where quant can be changed */
69//@{
be3492ec
KS
70enum DQProfile {
71 DQPROFILE_FOUR_EDGES,
72 DQPROFILE_DOUBLE_EDGES,
73 DQPROFILE_SINGLE_EDGE,
74 DQPROFILE_ALL_MBS
75};
2ce151f8 76//@}
21aa398f 77
2ce151f8 78/** @name Where quant can be changed
79 */
80//@{
be3492ec
KS
81enum DQSingleEdge {
82 DQSINGLE_BEDGE_LEFT,
83 DQSINGLE_BEDGE_TOP,
84 DQSINGLE_BEDGE_RIGHT,
85 DQSINGLE_BEDGE_BOTTOM
86};
2ce151f8 87//@}
21aa398f 88
2ce151f8 89/** Which pair of edges is quantized with ALTPQUANT */
90//@{
be3492ec
KS
91enum DQDoubleEdge {
92 DQDOUBLE_BEDGE_TOPLEFT,
93 DQDOUBLE_BEDGE_TOPRIGHT,
94 DQDOUBLE_BEDGE_BOTTOMRIGHT,
95 DQDOUBLE_BEDGE_BOTTOMLEFT
96};
2ce151f8 97//@}
21aa398f 98
2ce151f8 99/** MV modes for P frames */
100//@{
be3492ec
KS
101enum 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};
2ce151f8 108//@}
21aa398f 109
2ce151f8 110/** @name MV types for B frames */
111//@{
be3492ec
KS
112enum BMVTypes {
113 BMV_TYPE_BACKWARD,
114 BMV_TYPE_FORWARD,
fb2d9140 115 BMV_TYPE_INTERPOLATED
be3492ec
KS
116};
117//@}
118
119/** @name Block types for P/B frames */
120//@{
121enum 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};
2ce151f8 131//@}
e5540b3f 132
be3492ec
KS
133/** Table for conversion between TTBLK and TTMB */
134static 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
8a66a390
KS
140static const int ttfrm_to_tt[4] = { TT_8X8, TT_8X4, TT_4X8, TT_4X4 };
141
2ce151f8 142/** MV P mode - the 5th element is only used for mode 1 */
21aa398f 143static const uint8_t mv_pmode_table[2][5] = {
be3492ec
KS
144 { MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_1MV, MV_PMODE_1MV_HPEL, MV_PMODE_INTENSITY_COMP, MV_PMODE_MIXED_MV },
145 { MV_PMODE_1MV, MV_PMODE_MIXED_MV, MV_PMODE_1MV_HPEL, MV_PMODE_INTENSITY_COMP, MV_PMODE_1MV_HPEL_BILIN }
21aa398f 146};
8a66a390
KS
147static 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};
21aa398f 151
2ce151f8 152/** One more frame type */
21aa398f
AB
153#define BI_TYPE 7
154
21aa398f
AB
155static const int fps_nr[5] = { 24, 25, 30, 50, 60 },
156 fps_dr[2] = { 1000, 1001 };
157static 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
10b9c374 172/** @name VC-1 VLC tables and defines
2ce151f8 173 * @todo TODO move this into the context
174 */
175//@{
10b9c374
KS
176#define VC1_BFRACTION_VLC_BITS 7
177static VLC vc1_bfraction_vlc;
178#define VC1_IMODE_VLC_BITS 4
179static VLC vc1_imode_vlc;
180#define VC1_NORM2_VLC_BITS 3
181static VLC vc1_norm2_vlc;
182#define VC1_NORM6_VLC_BITS 9
183static VLC vc1_norm6_vlc;
21aa398f 184/* Could be optimized, one table only needs 8 bits */
10b9c374
KS
185#define VC1_TTMB_VLC_BITS 9 //12
186static VLC vc1_ttmb_vlc[3];
187#define VC1_MV_DIFF_VLC_BITS 9 //15
188static VLC vc1_mv_diff_vlc[4];
189#define VC1_CBPCY_P_VLC_BITS 9 //14
190static VLC vc1_cbpcy_p_vlc[4];
191#define VC1_4MV_BLOCK_PATTERN_VLC_BITS 6
192static VLC vc1_4mv_block_pattern_vlc[4];
193#define VC1_TTBLK_VLC_BITS 5
194static VLC vc1_ttblk_vlc[3];
195#define VC1_SUBBLKPAT_VLC_BITS 6
196static VLC vc1_subblkpat_vlc[3];
be3492ec
KS
197
198static VLC vc1_ac_coeff_table[8];
2ce151f8 199//@}
e5540b3f 200
be3492ec
KS
201enum 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
3c275f6d
KS
212/** @name Overlap conditions for Advanced Profile */
213//@{
214enum COTypes {
215 CONDOVER_NONE = 0,
216 CONDOVER_ALL,
217 CONDOVER_SELECT
218};
219//@}
220
221
10b9c374 222/** The VC1 Context
8da75fb2 223 * @fixme Change size wherever another size is more efficient
224 * Many members are only used for Advanced Profile
225 */
10b9c374 226typedef struct VC1Context{
2ce151f8 227 MpegEncContext s;
228
be3492ec
KS
229 int bits;
230
2ce151f8 231 /** Simple/Main Profile sequence header */
232 //@{
233 int res_sm; ///< reserved, 2b
234 int res_x8; ///< reserved
235 int multires; ///< frame-level RESPIC syntax element present
236 int res_fasttx; ///< reserved, always 1
237 int res_transtab; ///< reserved, always 0
238 int rangered; ///< RANGEREDFRM (range reduction) syntax element present
239 ///< at frame level
240 int res_rtm_flag; ///< reserved, set to 1
241 int reserved; ///< reserved
242 //@}
21aa398f 243
2ce151f8 244 /** Advanced Profile */
245 //@{
246 int level; ///< 3bits, for Advanced/Simple Profile, provided by TS layer
247 int chromaformat; ///< 2bits, 2=4:2:0, only defined
248 int postprocflag; ///< Per-frame processing suggestion flag present
249 int broadcast; ///< TFF/RFF present
250 int interlace; ///< Progressive/interlaced (RPTFTM syntax element)
251 int tfcntrflag; ///< TFCNTR present
252 int panscanflag; ///< NUMPANSCANWIN, TOPLEFT{X,Y}, BOTRIGHT{X,Y} present
253 int extended_dmv; ///< Additional extended dmv range at P/B frame-level
254 int color_prim; ///< 8bits, chroma coordinates of the color primaries
255 int transfer_char; ///< 8bits, Opto-electronic transfer characteristics
256 int matrix_coef; ///< 8bits, Color primaries->YCbCr transform matrix
257 int hrd_param_flag; ///< Presence of Hypothetical Reference
258 ///< Decoder parameters
3c275f6d 259 int psf; ///< Progressive Segmented Frame
2ce151f8 260 //@}
21aa398f 261
2ce151f8 262 /** Sequence header data for all Profiles
263 * TODO: choose between ints, uint8_ts and monobit flags
264 */
265 //@{
266 int profile; ///< 2bits, Profile
115329f1 267 int frmrtq_postproc; ///< 3bits,
2ce151f8 268 int bitrtq_postproc; ///< 5bits, quantized framerate-based postprocessing strength
269 int fastuvmc; ///< Rounding of qpel vector to hpel ? (not in Simple)
270 int extended_mv; ///< Ext MV in P/B (not in Simple)
271 int dquant; ///< How qscale varies with MBs, 2bits (not in Simple)
272 int vstransform; ///< variable-size [48]x[48] transform type + info
273 int overlap; ///< overlapped transforms in use
274 int quantizer_mode; ///< 2bits, quantizer mode used for sequence, see QUANT_*
275 int finterpflag; ///< INTERPFRM present
276 //@}
277
278 /** Frame decoding info for all profiles */
279 //@{
280 uint8_t mv_mode; ///< MV coding monde
281 uint8_t mv_mode2; ///< Secondary MV coding mode (B frames)
7cc84d24 282 int k_x; ///< Number of bits for MVs (depends on MV range)
283 int k_y; ///< Number of bits for MVs (depends on MV range)
be3492ec 284 int range_x, range_y; ///< MV range
2ce151f8 285 uint8_t pq, altpq; ///< Current/alternate frame quantizer scale
286 /** pquant parameters */
287 //@{
288 uint8_t dquantfrm;
289 uint8_t dqprofile;
290 uint8_t dqsbedge;
291 uint8_t dqbilevel;
292 //@}
8da75fb2 293 /** AC coding set indexes
294 * @see 8.1.1.10, p(1)10
295 */
296 //@{
297 int c_ac_table_index; ///< Chroma index from ACFRM element
298 int y_ac_table_index; ///< Luma index from AC2FRM element
299 //@}
2ce151f8 300 int ttfrm; ///< Transform type info present at frame level
7cc84d24 301 uint8_t ttmbf; ///< Transform type flag
7cc84d24 302 uint8_t ttblk4x4; ///< Value of ttblk which indicates a 4x4 transform
be3492ec
KS
303 int codingset; ///< index of current table set from 11.8 to use for luma block decoding
304 int codingset2; ///< index of current table set from 11.8 to use for chroma block decoding
305 int pqindex; ///< raw pqindex used in coding set selection
f26c2ef5 306 int a_avail, c_avail;
e4bf0302 307 uint8_t *mb_type_base, *mb_type[3];
be3492ec
KS
308
309
2ce151f8 310 /** Luma compensation parameters */
311 //@{
312 uint8_t lumscale;
313 uint8_t lumshift;
314 //@}
315 int16_t bfraction; ///< Relative position % anchors=> how to scale MVs
316 uint8_t halfpq; ///< Uniform quant over image and qp+.5
317 uint8_t respic; ///< Frame-level flag for resized images
318 int buffer_fullness; ///< HRD info
319 /** Ranges:
320 * -# 0 -> [-64n 63.f] x [-32, 31.f]
321 * -# 1 -> [-128, 127.f] x [-64, 63.f]
322 * -# 2 -> [-512, 511.f] x [-128, 127.f]
323 * -# 3 -> [-1024, 1023.f] x [-256, 255.f]
324 */
325 uint8_t mvrange;
bf2bc926 326 uint8_t pquantizer; ///< Uniform (over sequence) quantizer in use
7cc84d24 327 VLC *cbpcy_vlc; ///< CBPCY VLC table
328 int tt_index; ///< Index for Transform Type tables
87dfe848 329 uint8_t* mv_type_mb_plane; ///< bitplane for mv_type == (4MV)
5df68893 330 uint8_t* direct_mb_plane; ///< bitplane for "direct" MBs
87dfe848 331 int mv_type_is_raw; ///< mv type mb plane is not coded
5df68893 332 int dmb_is_raw; ///< direct mb plane is raw
87dfe848 333 int skip_is_raw; ///< skip mb plane is not coded
66d0ad26 334 uint8_t luty[256], lutuv[256]; // lookup tables used for intensity compensation
c5b32ec1 335 int rnd; ///< rounding control
2ce151f8 336
337 /** Frame decoding info for S/M profiles only */
338 //@{
339 uint8_t rangeredfrm; ///< out_sample = CLIP((in_sample-128)*2+128)
340 uint8_t interpfrm;
341 //@}
21aa398f 342
2ce151f8 343 /** Frame decoding info for Advanced profile */
344 //@{
345 uint8_t fcm; ///< 0->Progressive, 2->Frame-Interlace, 3->Field-Interlace
346 uint8_t numpanscanwin;
347 uint8_t tfcntr;
348 uint8_t rptfrm, tff, rff;
bf2bc926 349 uint16_t topleftx;
350 uint16_t toplefty;
351 uint16_t bottomrightx;
352 uint16_t bottomrighty;
2ce151f8 353 uint8_t uvsamp;
354 uint8_t postproc;
355 int hrd_num_leaky_buckets;
356 uint8_t bit_rate_exponent;
357 uint8_t buffer_size_exponent;
3c275f6d
KS
358 uint8_t* acpred_plane; ///< AC prediction flags bitplane
359 int acpred_is_raw;
360 uint8_t* over_flags_plane; ///< Overflags bitplane
361 int overflg_is_raw;
2ce151f8 362 uint8_t condover;
363 uint16_t *hrd_rate, *hrd_buffer;
1cf9f514
IK
364 uint8_t *hrd_fullness;
365 uint8_t range_mapy_flag;
366 uint8_t range_mapuv_flag;
367 uint8_t range_mapy;
368 uint8_t range_mapuv;
2ce151f8 369 //@}
3c275f6d
KS
370
371 int p_frame_skipped;
10b9c374 372} VC1Context;
21aa398f 373
2ce151f8 374/**
375 * Get unary code of limited length
376 * @fixme FIXME Slow and ugly
377 * @param gb GetBitContext
378 * @param[in] stop The bitstop value (unary code of 1's or 0's)
379 * @param[in] len Maximum length
380 * @return Unary length/index
381 */
21aa398f
AB
382static int get_prefix(GetBitContext *gb, int stop, int len)
383{
e5540b3f 384#if 1
be3492ec
KS
385 int i;
386
387 for(i = 0; i < len && get_bits1(gb) != stop; i++);
388 return i;
389/* int i = 0, tmp = !stop;
21aa398f
AB
390
391 while (i != len && tmp != stop)
392 {
393 tmp = get_bits(gb, 1);
394 i++;
395 }
bf2bc926 396 if (i == len && tmp != stop) return len+1;
be3492ec 397 return i;*/
e5540b3f 398#else
399 unsigned int buf;
400 int log;
401
402 OPEN_READER(re, gb);
403 UPDATE_CACHE(re, gb);
404 buf=GET_CACHE(re, gb); //Still not sure
405 if (stop) buf = ~buf;
42cc17f9 406
e5540b3f 407 log= av_log2(-buf); //FIXME: -?
408 if (log < limit){
409 LAST_SKIP_BITS(re, gb, log+1);
410 CLOSE_READER(re, gb);
411 return log;
412 }
42cc17f9 413
e5540b3f 414 LAST_SKIP_BITS(re, gb, limit);
415 CLOSE_READER(re, gb);
416 return limit;
417#endif
418}
419
be3492ec
KS
420static inline int decode210(GetBitContext *gb){
421 int n;
422 n = get_bits1(gb);
423 if (n == 1)
424 return 0;
425 else
426 return 2 - get_bits1(gb);
427}
428
2ce151f8 429/**
10b9c374
KS
430 * Init VC-1 specific tables and VC1Context members
431 * @param v The VC1Context to initialize
2ce151f8 432 * @return Status
433 */
10b9c374 434static int vc1_init_common(VC1Context *v)
21aa398f
AB
435{
436 static int done = 0;
bf2bc926 437 int i = 0;
21aa398f 438
21aa398f 439 v->hrd_rate = v->hrd_buffer = NULL;
e5540b3f 440
441 /* VLC tables */
21aa398f
AB
442 if(!done)
443 {
444 done = 1;
be3492ec 445 init_vlc(&vc1_bfraction_vlc, VC1_BFRACTION_VLC_BITS, 23,
10b9c374
KS
446 vc1_bfraction_bits, 1, 1,
447 vc1_bfraction_codes, 1, 1, 1);
be3492ec 448 init_vlc(&vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4,
10b9c374
KS
449 vc1_norm2_bits, 1, 1,
450 vc1_norm2_codes, 1, 1, 1);
be3492ec 451 init_vlc(&vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 64,
10b9c374
KS
452 vc1_norm6_bits, 1, 1,
453 vc1_norm6_codes, 2, 2, 1);
be3492ec 454 init_vlc(&vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7,
10b9c374
KS
455 vc1_imode_bits, 1, 1,
456 vc1_imode_codes, 1, 1, 1);
e5540b3f 457 for (i=0; i<3; i++)
458 {
be3492ec 459 init_vlc(&vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 16,
10b9c374
KS
460 vc1_ttmb_bits[i], 1, 1,
461 vc1_ttmb_codes[i], 2, 2, 1);
be3492ec 462 init_vlc(&vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 8,
10b9c374
KS
463 vc1_ttblk_bits[i], 1, 1,
464 vc1_ttblk_codes[i], 1, 1, 1);
be3492ec 465 init_vlc(&vc1_subblkpat_vlc[i], VC1_SUBBLKPAT_VLC_BITS, 15,
10b9c374
KS
466 vc1_subblkpat_bits[i], 1, 1,
467 vc1_subblkpat_codes[i], 1, 1, 1);
e5540b3f 468 }
469 for(i=0; i<4; i++)
21aa398f 470 {
be3492ec 471 init_vlc(&vc1_4mv_block_pattern_vlc[i], VC1_4MV_BLOCK_PATTERN_VLC_BITS, 16,
10b9c374
KS
472 vc1_4mv_block_pattern_bits[i], 1, 1,
473 vc1_4mv_block_pattern_codes[i], 1, 1, 1);
be3492ec 474 init_vlc(&vc1_cbpcy_p_vlc[i], VC1_CBPCY_P_VLC_BITS, 64,
10b9c374
KS
475 vc1_cbpcy_p_bits[i], 1, 1,
476 vc1_cbpcy_p_codes[i], 2, 2, 1);
be3492ec 477 init_vlc(&vc1_mv_diff_vlc[i], VC1_MV_DIFF_VLC_BITS, 73,
10b9c374
KS
478 vc1_mv_diff_bits[i], 1, 1,
479 vc1_mv_diff_codes[i], 2, 2, 1);
21aa398f 480 }
be3492ec
KS
481 for(i=0; i<8; i++)
482 init_vlc(&vc1_ac_coeff_table[i], AC_VLC_BITS, vc1_ac_sizes[i],
483 &vc1_ac_tables[i][0][1], 8, 4,
484 &vc1_ac_tables[i][0][0], 8, 4, 1);
485 init_vlc(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64,
486 &ff_msmp4_mb_i_table[0][1], 4, 2,
487 &ff_msmp4_mb_i_table[0][0], 4, 2, 1);
21aa398f
AB
488 }
489
e5540b3f 490 /* Other defaults */
491 v->pq = -1;
492 v->mvrange = 0; /* 7.1.1.18, p80 */
493
21aa398f
AB
494 return 0;
495}
496
be3492ec 497/***********************************************************************/
2ce151f8 498/**
be3492ec
KS
499 * @defgroup bitplane VC9 Bitplane decoding
500 * @see 8.7, p56
501 * @{
502 */
503
504/** @addtogroup bitplane
505 * Imode types
506 * @{
507 */
508enum Imode {
509 IMODE_RAW,
510 IMODE_NORM2,
511 IMODE_DIFF2,
512 IMODE_NORM6,
513 IMODE_DIFF6,
514 IMODE_ROWSKIP,
515 IMODE_COLSKIP
516};
517/** @} */ //imode defines
518
be3492ec
KS
519/** Decode rows by checking if they are skipped
520 * @param plane Buffer to store decoded bits
521 * @param[in] width Width of this buffer
522 * @param[in] height Height of this buffer
523 * @param[in] stride of this buffer
524 */
525static void decode_rowskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
526 int x, y;
21aa398f 527
be3492ec
KS
528 for (y=0; y<height; y++){
529 if (!get_bits(gb, 1)) //rowskip
530 memset(plane, 0, width);
531 else
532 for (x=0; x<width; x++)
533 plane[x] = get_bits(gb, 1);
534 plane += stride;
1cf9f514 535 }
be3492ec 536}
21aa398f 537
be3492ec
KS
538/** Decode columns by checking if they are skipped
539 * @param plane Buffer to store decoded bits
540 * @param[in] width Width of this buffer
541 * @param[in] height Height of this buffer
542 * @param[in] stride of this buffer
543 * @fixme FIXME: Optimize
544 */
545static void decode_colskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
546 int x, y;
21aa398f 547
be3492ec
KS
548 for (x=0; x<width; x++){
549 if (!get_bits(gb, 1)) //colskip
550 for (y=0; y<height; y++)
551 plane[y*stride] = 0;
552 else
553 for (y=0; y<height; y++)
554 plane[y*stride] = get_bits(gb, 1);
555 plane ++;
21aa398f 556 }
21aa398f
AB
557}
558
be3492ec
KS
559/** Decode a bitplane's bits
560 * @param bp Bitplane where to store the decode bits
561 * @param v VC-1 context for bit reading and logging
2ce151f8 562 * @return Status
be3492ec
KS
563 * @fixme FIXME: Optimize
564 * @todo TODO: Decide if a struct is needed
2ce151f8 565 */
87dfe848 566static int bitplane_decoding(uint8_t* data, int *raw_flag, VC1Context *v)
21aa398f 567{
be3492ec 568 GetBitContext *gb = &v->s.gb;
21aa398f 569
be3492ec 570 int imode, x, y, code, offset;
87dfe848
KS
571 uint8_t invert, *planep = data;
572 int width, height, stride;
21aa398f 573
87dfe848
KS
574 width = v->s.mb_width;
575 height = v->s.mb_height;
576 stride = v->s.mb_stride;
be3492ec
KS
577 invert = get_bits(gb, 1);
578 imode = get_vlc2(gb, vc1_imode_vlc.table, VC1_IMODE_VLC_BITS, 1);
21aa398f 579
87dfe848 580 *raw_flag = 0;
be3492ec 581 switch (imode)
21aa398f 582 {
be3492ec
KS
583 case IMODE_RAW:
584 //Data is actually read in the MB layer (same for all tests == "raw")
87dfe848 585 *raw_flag = 1; //invert ignored
be3492ec
KS
586 return invert;
587 case IMODE_DIFF2:
588 case IMODE_NORM2:
87dfe848 589 if ((height * width) & 1)
21aa398f 590 {
be3492ec
KS
591 *planep++ = get_bits(gb, 1);
592 offset = 1;
21aa398f 593 }
be3492ec
KS
594 else offset = 0;
595 // decode bitplane as one long line
87dfe848 596 for (y = offset; y < height * width; y += 2) {
be3492ec
KS
597 code = get_vlc2(gb, vc1_norm2_vlc.table, VC1_NORM2_VLC_BITS, 1);
598 *planep++ = code & 1;
599 offset++;
87dfe848 600 if(offset == width) {
be3492ec 601 offset = 0;
87dfe848 602 planep += stride - width;
21aa398f 603 }
be3492ec
KS
604 *planep++ = code >> 1;
605 offset++;
87dfe848 606 if(offset == width) {
be3492ec 607 offset = 0;
87dfe848 608 planep += stride - width;
21aa398f 609 }
be3492ec
KS
610 }
611 break;
612 case IMODE_DIFF6:
613 case IMODE_NORM6:
87dfe848
KS
614 if(!(height % 3) && (width % 3)) { // use 2x3 decoding
615 for(y = 0; y < height; y+= 3) {
616 for(x = width & 1; x < width; x += 2) {
be3492ec
KS
617 code = get_vlc2(gb, vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
618 if(code < 0){
619 av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
620 return -1;
621 }
622 planep[x + 0] = (code >> 0) & 1;
623 planep[x + 1] = (code >> 1) & 1;
87dfe848
KS
624 planep[x + 0 + stride] = (code >> 2) & 1;
625 planep[x + 1 + stride] = (code >> 3) & 1;
626 planep[x + 0 + stride * 2] = (code >> 4) & 1;
627 planep[x + 1 + stride * 2] = (code >> 5) & 1;
be3492ec 628 }
87dfe848 629 planep += stride * 3;
be3492ec 630 }
87dfe848 631 if(width & 1) decode_colskip(data, 1, height, stride, &v->s.gb);
be3492ec 632 } else { // 3x2
8a66a390 633 planep += (height & 1) * stride;
87dfe848
KS
634 for(y = height & 1; y < height; y += 2) {
635 for(x = width % 3; x < width; x += 3) {
be3492ec
KS
636 code = get_vlc2(gb, vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
637 if(code < 0){
638 av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
639 return -1;
640 }
641 planep[x + 0] = (code >> 0) & 1;
642 planep[x + 1] = (code >> 1) & 1;
643 planep[x + 2] = (code >> 2) & 1;
87dfe848
KS
644 planep[x + 0 + stride] = (code >> 3) & 1;
645 planep[x + 1 + stride] = (code >> 4) & 1;
646 planep[x + 2 + stride] = (code >> 5) & 1;
be3492ec 647 }
87dfe848 648 planep += stride * 2;
be3492ec 649 }
87dfe848
KS
650 x = width % 3;
651 if(x) decode_colskip(data , x, height , stride, &v->s.gb);
652 if(height & 1) decode_rowskip(data+x, width - x, 1, stride, &v->s.gb);
be3492ec
KS
653 }
654 break;
655 case IMODE_ROWSKIP:
87dfe848 656 decode_rowskip(data, width, height, stride, &v->s.gb);
be3492ec
KS
657 break;
658 case IMODE_COLSKIP:
87dfe848 659 decode_colskip(data, width, height, stride, &v->s.gb);
be3492ec
KS
660 break;
661 default: break;
662 }
663
664 /* Applying diff operator */
665 if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6)
666 {
87dfe848 667 planep = data;
be3492ec 668 planep[0] ^= invert;
87dfe848 669 for (x=1; x<width; x++)
be3492ec 670 planep[x] ^= planep[x-1];
87dfe848 671 for (y=1; y<height; y++)
be3492ec 672 {
87dfe848
KS
673 planep += stride;
674 planep[0] ^= planep[-stride];
675 for (x=1; x<width; x++)
21aa398f 676 {
87dfe848
KS
677 if (planep[x-1] != planep[x-stride]) planep[x] ^= invert;
678 else planep[x] ^= planep[x-1];
21aa398f
AB
679 }
680 }
681 }
be3492ec 682 else if (invert)
21aa398f 683 {
87dfe848
KS
684 planep = data;
685 for (x=0; x<stride*height; x++) planep[x] = !planep[x]; //FIXME stride
21aa398f 686 }
be3492ec
KS
687 return (imode<<1) + invert;
688}
87dfe848 689
be3492ec 690/** @} */ //Bitplane group
21aa398f 691
be3492ec
KS
692/***********************************************************************/
693/** VOP Dquant decoding
694 * @param v VC-1 Context
695 */
696static int vop_dquant_decoding(VC1Context *v)
697{
698 GetBitContext *gb = &v->s.gb;
699 int pqdiff;
700
701 //variable size
702 if (v->dquant == 2)
703 {
704 pqdiff = get_bits(gb, 3);
705 if (pqdiff == 7) v->altpq = get_bits(gb, 5);
706 else v->altpq = v->pq + pqdiff + 1;
707 }
708 else
21aa398f 709 {
be3492ec
KS
710 v->dquantfrm = get_bits(gb, 1);
711 if ( v->dquantfrm )
21aa398f 712 {
be3492ec
KS
713 v->dqprofile = get_bits(gb, 2);
714 switch (v->dqprofile)
21aa398f 715 {
be3492ec
KS
716 case DQPROFILE_SINGLE_EDGE:
717 case DQPROFILE_DOUBLE_EDGES:
718 v->dqsbedge = get_bits(gb, 2);
719 break;
720 case DQPROFILE_ALL_MBS:
721 v->dqbilevel = get_bits(gb, 1);
722 default: break; //Forbidden ?
42cc17f9 723 }
3a3f1cf3 724 if (v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS)
21aa398f 725 {
be3492ec
KS
726 pqdiff = get_bits(gb, 3);
727 if (pqdiff == 7) v->altpq = get_bits(gb, 5);
728 else v->altpq = v->pq + pqdiff + 1;
21aa398f 729 }
21aa398f
AB
730 }
731 }
be3492ec
KS
732 return 0;
733}
21aa398f 734
be3492ec
KS
735/** Put block onto picture
736 * @todo move to DSPContext
737 */
738static void vc1_put_block(VC1Context *v, DCTELEM block[6][64])
739{
740 uint8_t *Y;
741 int ys, us, vs;
742 DSPContext *dsp = &v->s.dsp;
743
ffb9a8b1
KS
744 if(v->rangeredfrm) {
745 int i, j, k;
746 for(k = 0; k < 6; k++)
747 for(j = 0; j < 8; j++)
748 for(i = 0; i < 8; i++)
749 block[k][i + j*8] = ((block[k][i + j*8] - 128) << 1) + 128;
750
751 }
be3492ec
KS
752 ys = v->s.current_picture.linesize[0];
753 us = v->s.current_picture.linesize[1];
754 vs = v->s.current_picture.linesize[2];
755 Y = v->s.dest[0];
756
757 dsp->put_pixels_clamped(block[0], Y, ys);
758 dsp->put_pixels_clamped(block[1], Y + 8, ys);
759 Y += ys * 8;
760 dsp->put_pixels_clamped(block[2], Y, ys);
761 dsp->put_pixels_clamped(block[3], Y + 8, ys);
762
138712fe
KS
763 if(!(v->s.flags & CODEC_FLAG_GRAY)) {
764 dsp->put_pixels_clamped(block[4], v->s.dest[1], us);
765 dsp->put_pixels_clamped(block[5], v->s.dest[2], vs);
766 }
be3492ec
KS
767}
768
769/** Do motion compensation over 1 macroblock
770 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
771 */
5df68893 772static void vc1_mc_1mv(VC1Context *v, int dir)
be3492ec
KS
773{
774 MpegEncContext *s = &v->s;
775 DSPContext *dsp = &v->s.dsp;
776 uint8_t *srcY, *srcU, *srcV;
87dfe848 777 int dxy, uvdxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
be3492ec
KS
778
779 if(!v->s.last_picture.data[0])return;
780
87dfe848
KS
781 mx = s->mv[0][0][0];
782 my = s->mv[0][0][1];
783 uvmx = (mx + ((mx & 3) == 3)) >> 1;
784 uvmy = (my + ((my & 3) == 3)) >> 1;
5df68893
KS
785 if(!dir) {
786 srcY = s->last_picture.data[0];
787 srcU = s->last_picture.data[1];
788 srcV = s->last_picture.data[2];
789 } else {
790 srcY = s->next_picture.data[0];
791 srcU = s->next_picture.data[1];
792 srcV = s->next_picture.data[2];
793 }
be3492ec 794
87dfe848
KS
795 src_x = s->mb_x * 16 + (mx >> 2);
796 src_y = s->mb_y * 16 + (my >> 2);
797 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
798 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
799
230e9d4c
KS
800 src_x = clip( src_x, -16, s->mb_width * 16);
801 src_y = clip( src_y, -16, s->mb_height * 16);
802 uvsrc_x = clip(uvsrc_x, -8, s->mb_width * 8);
803 uvsrc_y = clip(uvsrc_y, -8, s->mb_height * 8);
87dfe848
KS
804
805 srcY += src_y * s->linesize + src_x;
806 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
807 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
808
138712fe
KS
809 /* for grayscale we should not try to read from unknown area */
810 if(s->flags & CODEC_FLAG_GRAY) {
811 srcU = s->edge_emu_buffer + 18 * s->linesize;
812 srcV = s->edge_emu_buffer + 18 * s->linesize;
813 }
814
ffb9a8b1 815 if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
8295eb30
KS
816 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3
817 || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){
818 uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
87dfe848 819
8295eb30
KS
820 srcY -= s->mspel * (1 + s->linesize);
821 ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
822 src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
87dfe848
KS
823 srcY = s->edge_emu_buffer;
824 ff_emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8+1, 8+1,
825 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
826 ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
827 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
828 srcU = uvbuf;
829 srcV = uvbuf + 16;
ffb9a8b1
KS
830 /* if we deal with range reduction we need to scale source blocks */
831 if(v->rangeredfrm) {
832 int i, j;
833 uint8_t *src, *src2;
834
835 src = srcY;
8295eb30
KS
836 for(j = 0; j < 17 + s->mspel*2; j++) {
837 for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
ffb9a8b1
KS
838 src += s->linesize;
839 }
840 src = srcU; src2 = srcV;
841 for(j = 0; j < 9; j++) {
842 for(i = 0; i < 9; i++) {
843 src[i] = ((src[i] - 128) >> 1) + 128;
844 src2[i] = ((src2[i] - 128) >> 1) + 128;
845 }
846 src += s->uvlinesize;
847 src2 += s->uvlinesize;
848 }
849 }
66d0ad26
KS
850 /* if we deal with intensity compensation we need to scale source blocks */
851 if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
852 int i, j;
853 uint8_t *src, *src2;
854
855 src = srcY;
8295eb30
KS
856 for(j = 0; j < 17 + s->mspel*2; j++) {
857 for(i = 0; i < 17 + s->mspel*2; i++) src[i] = v->luty[src[i]];
66d0ad26
KS
858 src += s->linesize;
859 }
860 src = srcU; src2 = srcV;
861 for(j = 0; j < 9; j++) {
862 for(i = 0; i < 9; i++) {
863 src[i] = v->lutuv[src[i]];
864 src2[i] = v->lutuv[src2[i]];
865 }
866 src += s->uvlinesize;
867 src2 += s->uvlinesize;
868 }
869 }
8295eb30 870 srcY += s->mspel * (1 + s->linesize);
87dfe848
KS
871 }
872
913e93d5
KS
873 if(v->fastuvmc) {
874 uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
875 uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
876 }
877
8295eb30
KS
878 if(s->mspel) {
879 dxy = ((my & 3) << 2) | (mx & 3);
880 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] , srcY , s->linesize, v->rnd);
881 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
882 srcY += s->linesize * 8;
883 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize , srcY , s->linesize, v->rnd);
884 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
66ff2c1f
KS
885 } else { // hpel mc - always used for luma
886 dxy = (my & 2) | ((mx & 2) >> 1);
87dfe848 887
c5b32ec1
KS
888 if(!v->rnd)
889 dsp->put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
890 else
891 dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
c5b32ec1 892 }
138712fe
KS
893
894 if(s->flags & CODEC_FLAG_GRAY) return;
2d5eadcc 895 /* Chroma MC always uses qpel bilinear */
c5b32ec1 896 uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
b0c8e1b8
KS
897 uvmx = (uvmx&3)<<1;
898 uvmy = (uvmy&3)<<1;
899 if(!v->rnd){
900 dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
901 dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
902 }else{
903 dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
904 dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
905 }
21aa398f 906}
21aa398f 907
e4bf0302
KS
908/** Do motion compensation for 4-MV macroblock - luminance block
909 */
910static void vc1_mc_4mv_luma(VC1Context *v, int n)
911{
912 MpegEncContext *s = &v->s;
913 DSPContext *dsp = &v->s.dsp;
914 uint8_t *srcY;
915 int dxy, mx, my, src_x, src_y;
916 int off;
917
918 if(!v->s.last_picture.data[0])return;
919 mx = s->mv[0][n][0];
920 my = s->mv[0][n][1];
921 srcY = s->last_picture.data[0];
922
923 off = s->linesize * 4 * (n&2) + (n&1) * 8;
924
925 src_x = s->mb_x * 16 + (n&1) * 8 + (mx >> 2);
926 src_y = s->mb_y * 16 + (n&2) * 4 + (my >> 2);
927
230e9d4c
KS
928 src_x = clip( src_x, -16, s->mb_width * 16);
929 src_y = clip( src_y, -16, s->mb_height * 16);
e4bf0302
KS
930
931 srcY += src_y * s->linesize + src_x;
932
99f649a2 933 if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
78cbfc0c
KS
934 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 8 - s->mspel*2
935 || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 8 - s->mspel*2){
8295eb30
KS
936 srcY -= s->mspel * (1 + s->linesize);
937 ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 9+s->mspel*2, 9+s->mspel*2,
938 src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
e4bf0302 939 srcY = s->edge_emu_buffer;
ffb9a8b1
KS
940 /* if we deal with range reduction we need to scale source blocks */
941 if(v->rangeredfrm) {
942 int i, j;
943 uint8_t *src;
944
945 src = srcY;
8295eb30
KS
946 for(j = 0; j < 9 + s->mspel*2; j++) {
947 for(i = 0; i < 9 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
ffb9a8b1
KS
948 src += s->linesize;
949 }
950 }
99f649a2
KS
951 /* if we deal with intensity compensation we need to scale source blocks */
952 if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
953 int i, j;
954 uint8_t *src;
955
956 src = srcY;
957 for(j = 0; j < 9 + s->mspel*2; j++) {
958 for(i = 0; i < 9 + s->mspel*2; i++) src[i] = v->luty[src[i]];
959 src += s->linesize;
960 }
961 }
8295eb30 962 srcY += s->mspel * (1 + s->linesize);
e4bf0302
KS
963 }
964
8295eb30
KS
965 if(s->mspel) {
966 dxy = ((my & 3) << 2) | (mx & 3);
967 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, v->rnd);
66ff2c1f
KS
968 } else { // hpel mc - always used for luma
969 dxy = (my & 2) | ((mx & 2) >> 1);
c5b32ec1
KS
970 if(!v->rnd)
971 dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
972 else
973 dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
e4bf0302
KS
974 }
975}
976
e4bf0302
KS
977static inline int median4(int a, int b, int c, int d)
978{
35a9cac8 979 if(a < b) {
913e93d5
KS
980 if(c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
981 else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
35a9cac8 982 } else {
913e93d5
KS
983 if(c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
984 else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
35a9cac8 985 }
e4bf0302
KS
986}
987
988
989/** Do motion compensation for 4-MV macroblock - both chroma blocks
990 */
991static void vc1_mc_4mv_chroma(VC1Context *v)
992{
993 MpegEncContext *s = &v->s;
994 DSPContext *dsp = &v->s.dsp;
995 uint8_t *srcU, *srcV;
996 int uvdxy, uvmx, uvmy, uvsrc_x, uvsrc_y;
997 int i, idx, tx = 0, ty = 0;
998 int mvx[4], mvy[4], intra[4];
999 static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
1000
1001 if(!v->s.last_picture.data[0])return;
138712fe 1002 if(s->flags & CODEC_FLAG_GRAY) return;
e4bf0302
KS
1003
1004 for(i = 0; i < 4; i++) {
1005 mvx[i] = s->mv[0][i][0];
1006 mvy[i] = s->mv[0][i][1];
1007 intra[i] = v->mb_type[0][s->block_index[i]];
1008 }
1009
1010 /* calculate chroma MV vector from four luma MVs */
1ae4a8e6 1011 idx = (intra[3] << 3) | (intra[2] << 2) | (intra[1] << 1) | intra[0];
e4bf0302
KS
1012 if(!idx) { // all blocks are inter
1013 tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
1014 ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
1015 } else if(count[idx] == 1) { // 3 inter blocks
1016 switch(idx) {
1017 case 0x1:
1018 tx = mid_pred(mvx[1], mvx[2], mvx[3]);
1ae4a8e6 1019 ty = mid_pred(mvy[1], mvy[2], mvy[3]);
e4bf0302
KS
1020 break;
1021 case 0x2:
1022 tx = mid_pred(mvx[0], mvx[2], mvx[3]);
1ae4a8e6 1023 ty = mid_pred(mvy[0], mvy[2], mvy[3]);
e4bf0302
KS
1024 break;
1025 case 0x4:
1026 tx = mid_pred(mvx[0], mvx[1], mvx[3]);
1ae4a8e6 1027 ty = mid_pred(mvy[0], mvy[1], mvy[3]);
e4bf0302
KS
1028 break;
1029 case 0x8:
1030 tx = mid_pred(mvx[0], mvx[1], mvx[2]);
1ae4a8e6 1031 ty = mid_pred(mvy[0], mvy[1], mvy[2]);
e4bf0302
KS
1032 break;
1033 }
1034 } else if(count[idx] == 2) {
1035 int t1 = 0, t2 = 0;
1036 for(i=0; i<3;i++) if(!intra[i]) {t1 = i; break;}
1037 for(i= t1+1; i<4; i++)if(!intra[i]) {t2 = i; break;}
913e93d5
KS
1038 tx = (mvx[t1] + mvx[t2]) / 2;
1039 ty = (mvy[t1] + mvy[t2]) / 2;
e4bf0302
KS
1040 } else
1041 return; //no need to do MC for inter blocks
1042
1043 uvmx = (tx + ((tx&3) == 3)) >> 1;
1044 uvmy = (ty + ((ty&3) == 3)) >> 1;
1045
1046 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1047 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1048
230e9d4c
KS
1049 uvsrc_x = clip(uvsrc_x, -8, s->mb_width * 8);
1050 uvsrc_y = clip(uvsrc_y, -8, s->mb_height * 8);
e4bf0302
KS
1051 srcU = s->last_picture.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
1052 srcV = s->last_picture.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
99f649a2
KS
1053 if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
1054 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
83b3df83 1055 || (unsigned)uvsrc_y > (s->v_edge_pos >> 1) - 9){
e4bf0302
KS
1056 ff_emulated_edge_mc(s->edge_emu_buffer , srcU, s->uvlinesize, 8+1, 8+1,
1057 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
1058 ff_emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize, 8+1, 8+1,
1059 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
1060 srcU = s->edge_emu_buffer;
1061 srcV = s->edge_emu_buffer + 16;
ffb9a8b1
KS
1062
1063 /* if we deal with range reduction we need to scale source blocks */
1064 if(v->rangeredfrm) {
1065 int i, j;
1066 uint8_t *src, *src2;
1067
1068 src = srcU; src2 = srcV;
1069 for(j = 0; j < 9; j++) {
1070 for(i = 0; i < 9; i++) {
1071 src[i] = ((src[i] - 128) >> 1) + 128;
1072 src2[i] = ((src2[i] - 128) >> 1) + 128;
1073 }
1074 src += s->uvlinesize;
1075 src2 += s->uvlinesize;
1076 }
1077 }
99f649a2
KS
1078 /* if we deal with intensity compensation we need to scale source blocks */
1079 if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1080 int i, j;
1081 uint8_t *src, *src2;
1082
1083 src = srcU; src2 = srcV;
1084 for(j = 0; j < 9; j++) {
1085 for(i = 0; i < 9; i++) {
1086 src[i] = v->lutuv[src[i]];
1087 src2[i] = v->lutuv[src2[i]];
1088 }
1089 src += s->uvlinesize;
1090 src2 += s->uvlinesize;
1091 }
1092 }
e4bf0302
KS
1093 }
1094
913e93d5
KS
1095 if(v->fastuvmc) {
1096 uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
1097 uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
1098 }
1099
2d5eadcc 1100 /* Chroma MC always uses qpel bilinear */
c5b32ec1 1101 uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
b0c8e1b8
KS
1102 uvmx = (uvmx&3)<<1;
1103 uvmy = (uvmy&3)<<1;
1104 if(!v->rnd){
1105 dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
1106 dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
1107 }else{
1108 dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
1109 dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
1110 }
e4bf0302
KS
1111}
1112
3c275f6d
KS
1113static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb);
1114
115329f1 1115/**
2ce151f8 1116 * Decode Simple/Main Profiles sequence header
1117 * @see Figure 7-8, p16-17
1118 * @param avctx Codec context
1119 * @param gb GetBit context initialized from Codec context extra_data
1120 * @return Status
1121 */
21aa398f
AB
1122static int decode_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
1123{
10b9c374 1124 VC1Context *v = avctx->priv_data;
21aa398f 1125
be3492ec 1126 av_log(avctx, AV_LOG_INFO, "Header: %0X\n", show_bits(gb, 32));
21aa398f 1127 v->profile = get_bits(gb, 2);
7cc84d24 1128 if (v->profile == 2)
25a0a0a5 1129 {
be3492ec 1130 av_log(avctx, AV_LOG_ERROR, "Profile value 2 is forbidden (and WMV3 Complex Profile is unsupported)\n");
25a0a0a5
IK
1131 return -1;
1132 }
21aa398f 1133
7cc84d24 1134 if (v->profile == PROFILE_ADVANCED)
21aa398f 1135 {
3c275f6d 1136 return decode_sequence_header_adv(v, gb);
21aa398f
AB
1137 }
1138 else
21aa398f
AB
1139 {
1140 v->res_sm = get_bits(gb, 2); //reserved
1141 if (v->res_sm)
1142 {
1143 av_log(avctx, AV_LOG_ERROR,
1144 "Reserved RES_SM=%i is forbidden\n", v->res_sm);
7cc84d24 1145 return -1;
21aa398f
AB
1146 }
1147 }
1148
1149 // (fps-2)/4 (->30)
1150 v->frmrtq_postproc = get_bits(gb, 3); //common
1151 // (bitrate-32kbps)/64kbps
1152 v->bitrtq_postproc = get_bits(gb, 5); //common
5e424311 1153 v->s.loop_filter = get_bits(gb, 1); //common
25a0a0a5
IK
1154 if(v->s.loop_filter == 1 && v->profile == PROFILE_SIMPLE)
1155 {
1156 av_log(avctx, AV_LOG_ERROR,
1157 "LOOPFILTER shell not be enabled in simple profile\n");
1158 }
21aa398f 1159
7cc84d24 1160 if (v->profile < PROFILE_ADVANCED)
21aa398f
AB
1161 {
1162 v->res_x8 = get_bits(gb, 1); //reserved
1163 if (v->res_x8)
1164 {
1165 av_log(avctx, AV_LOG_ERROR,
1166 "1 for reserved RES_X8 is forbidden\n");
5e424311 1167 //return -1;
21aa398f
AB
1168 }
1169 v->multires = get_bits(gb, 1);
1170 v->res_fasttx = get_bits(gb, 1);
1171 if (!v->res_fasttx)
1172 {
1173 av_log(avctx, AV_LOG_ERROR,
1174 "0 for reserved RES_FASTTX is forbidden\n");
1175 //return -1;
1176 }
1177 }
1178
1179 v->fastuvmc = get_bits(gb, 1); //common
1180 if (!v->profile && !v->fastuvmc)
1181 {
1182 av_log(avctx, AV_LOG_ERROR,
1183 "FASTUVMC unavailable in Simple Profile\n");
1184 return -1;
1185 }
1186 v->extended_mv = get_bits(gb, 1); //common
1187 if (!v->profile && v->extended_mv)
1188 {
1189 av_log(avctx, AV_LOG_ERROR,
1190 "Extended MVs unavailable in Simple Profile\n");
1191 return -1;
1192 }
1193 v->dquant = get_bits(gb, 2); //common
1194 v->vstransform = get_bits(gb, 1); //common
42cc17f9 1195
7cc84d24 1196 if (v->profile < PROFILE_ADVANCED)
21aa398f
AB
1197 {
1198 v->res_transtab = get_bits(gb, 1);
1199 if (v->res_transtab)
1200 {
1201 av_log(avctx, AV_LOG_ERROR,
1202 "1 for reserved RES_TRANSTAB is forbidden\n");
1203 return -1;
1204 }
1205 }
1206
1207 v->overlap = get_bits(gb, 1); //common
1208
7cc84d24 1209 if (v->profile < PROFILE_ADVANCED)
21aa398f 1210 {
0d33db8a 1211 v->s.resync_marker = get_bits(gb, 1);
21aa398f 1212 v->rangered = get_bits(gb, 1);
25a0a0a5
IK
1213 if (v->rangered && v->profile == PROFILE_SIMPLE)
1214 {
be3492ec 1215 av_log(avctx, AV_LOG_INFO,
42cc17f9 1216 "RANGERED should be set to 0 in simple profile\n");
25a0a0a5 1217 }
21aa398f
AB
1218 }
1219
0d33db8a 1220 v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
21aa398f
AB
1221 v->quantizer_mode = get_bits(gb, 2); //common
1222
7cc84d24 1223 if (v->profile < PROFILE_ADVANCED)
21aa398f
AB
1224 {
1225 v->finterpflag = get_bits(gb, 1); //common
1226 v->res_rtm_flag = get_bits(gb, 1); //reserved
1227 if (!v->res_rtm_flag)
1228 {
48d3fca7
KS
1229// av_log(avctx, AV_LOG_ERROR,
1230// "0 for reserved RES_RTM_FLAG is forbidden\n");
21aa398f 1231 av_log(avctx, AV_LOG_ERROR,
48d3fca7 1232 "Old WMV3 version detected, only I-frames will be decoded\n");
21aa398f
AB
1233 //return -1;
1234 }
be3492ec 1235 av_log(avctx, AV_LOG_DEBUG,
21aa398f 1236 "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
e148c6e1 1237 "LoopFilter=%i, MultiRes=%i, FastUVMC=%i, Extended MV=%i\n"
21aa398f
AB
1238 "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
1239 "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
1240 v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
2ce151f8 1241 v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
0d33db8a 1242 v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
21aa398f
AB
1243 v->dquant, v->quantizer_mode, avctx->max_b_frames
1244 );
e5540b3f 1245 return 0;
21aa398f 1246 }
be3492ec 1247 return -1;
21aa398f
AB
1248}
1249
3c275f6d
KS
1250static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb)
1251{
1252 v->res_rtm_flag = 1;
1253 v->level = get_bits(gb, 3);
1254 if(v->level >= 5)
1255 {
1256 av_log(v->s.avctx, AV_LOG_ERROR, "Reserved LEVEL %i\n",v->level);
1257 }
1258 v->chromaformat = get_bits(gb, 2);
1259 if (v->chromaformat != 1)
1260 {
1261 av_log(v->s.avctx, AV_LOG_ERROR,
1262 "Only 4:2:0 chroma format supported\n");
1263 return -1;
1264 }
1265
1266 // (fps-2)/4 (->30)
1267 v->frmrtq_postproc = get_bits(gb, 3); //common
1268 // (bitrate-32kbps)/64kbps
1269 v->bitrtq_postproc = get_bits(gb, 5); //common
1270 v->postprocflag = get_bits(gb, 1); //common
1271
1272 v->s.avctx->coded_width = (get_bits(gb, 12) + 1) << 1;
1273 v->s.avctx->coded_height = (get_bits(gb, 12) + 1) << 1;
1274 v->broadcast = get_bits1(gb);
1275 v->interlace = get_bits1(gb);
1276 v->tfcntrflag = get_bits1(gb);
1277 v->finterpflag = get_bits1(gb);
1278 get_bits1(gb); // reserved
1279 v->psf = get_bits1(gb);
1280 if(v->psf) { //PsF, 6.1.13
1281 av_log(v->s.avctx, AV_LOG_ERROR, "Progressive Segmented Frame mode: not supported (yet)\n");
1282 return -1;
1283 }
1284 if(get_bits1(gb)) { //Display Info - decoding is not affected by it
1285 int w, h, ar = 0;
1286 av_log(v->s.avctx, AV_LOG_INFO, "Display extended info:\n");
1287 w = get_bits(gb, 14);
1288 h = get_bits(gb, 14);
1289 av_log(v->s.avctx, AV_LOG_INFO, "Display dimensions: %ix%i\n", w, h);
1290 //TODO: store aspect ratio in AVCodecContext
1291 if(get_bits1(gb))
1292 ar = get_bits(gb, 4);
1293 if(ar == 15) {
1294 w = get_bits(gb, 8);
1295 h = get_bits(gb, 8);
1296 }
1297
1298 if(get_bits1(gb)){ //framerate stuff
1299 if(get_bits1(gb)) {
1300 get_bits(gb, 16);
1301 } else {
1302 get_bits(gb, 8);
1303 get_bits(gb, 4);
1304 }
1305 }
1306
1307 if(get_bits1(gb)){
1308 v->color_prim = get_bits(gb, 8);
1309 v->transfer_char = get_bits(gb, 8);
1310 v->matrix_coef = get_bits(gb, 8);
1311 }
1312 }
1313
1314 v->hrd_param_flag = get_bits1(gb);
1315 if(v->hrd_param_flag) {
1316 int i;
1317 v->hrd_num_leaky_buckets = get_bits(gb, 5);
1318 get_bits(gb, 4); //bitrate exponent
1319 get_bits(gb, 4); //buffer size exponent
1320 for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
1321 get_bits(gb, 16); //hrd_rate[n]
1322 get_bits(gb, 16); //hrd_buffer[n]
1323 }
1324 }
1325 return 0;
1326}
1327
1328static int decode_entry_point(AVCodecContext *avctx, GetBitContext *gb)
1329{
1330 VC1Context *v = avctx->priv_data;
1331 int i;
1332
1333 av_log(avctx, AV_LOG_DEBUG, "Entry point: %08X\n", show_bits_long(gb, 32));
1334 get_bits1(gb); // broken link
1335 get_bits1(gb); // closed entry
1336 v->panscanflag = get_bits1(gb);
1337 get_bits1(gb); // refdist flag
1338 v->s.loop_filter = get_bits1(gb);
1339 v->fastuvmc = get_bits1(gb);
1340 v->extended_mv = get_bits1(gb);
1341 v->dquant = get_bits(gb, 2);
1342 v->vstransform = get_bits1(gb);
1343 v->overlap = get_bits1(gb);
1344 v->quantizer_mode = get_bits(gb, 2);
1345
1346 if(v->hrd_param_flag){
1347 for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
1348 get_bits(gb, 8); //hrd_full[n]
1349 }
1350 }
1351
1352 if(get_bits1(gb)){
1353 avctx->coded_width = (get_bits(gb, 12)+1)<<1;
1354 avctx->coded_height = (get_bits(gb, 12)+1)<<1;
1355 }
1356 if(v->extended_mv)
1357 v->extended_dmv = get_bits1(gb);
1358 if(get_bits1(gb)) {
1359 av_log(avctx, AV_LOG_ERROR, "Luma scaling is not supported, expect wrong picture\n");
1360 skip_bits(gb, 3); // Y range, ignored for now
1361 }
1362 if(get_bits1(gb)) {
1363 av_log(avctx, AV_LOG_ERROR, "Chroma scaling is not supported, expect wrong picture\n");
1364 skip_bits(gb, 3); // UV range, ignored for now
1365 }
1366
1367 return 0;
1368}
21aa398f 1369
be3492ec 1370static int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
21aa398f 1371{
be3492ec
KS
1372 int pqindex, lowquant, status;
1373
1374 if(v->finterpflag) v->interpfrm = get_bits(gb, 1);
1375 skip_bits(gb, 2); //framecnt unused
1376 v->rangeredfrm = 0;
1377 if (v->rangered) v->rangeredfrm = get_bits(gb, 1);
1378 v->s.pict_type = get_bits(gb, 1);
1379 if (v->s.avctx->max_b_frames) {
1380 if (!v->s.pict_type) {
1381 if (get_bits(gb, 1)) v->s.pict_type = I_TYPE;
1382 else v->s.pict_type = B_TYPE;
1383 } else v->s.pict_type = P_TYPE;
1384 } else v->s.pict_type = v->s.pict_type ? P_TYPE : I_TYPE;
1385
1386 if(v->s.pict_type == I_TYPE)
1387 get_bits(gb, 7); // skip buffer fullness
5df68893
KS
1388 if(v->s.pict_type == B_TYPE) {
1389 v->bfraction = get_vlc2(gb, vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
1390 v->bfraction = vc1_bfraction_lut[v->bfraction];
1391 if(v->bfraction == -1) {
1392 v->s.pict_type = BI_TYPE;
1393 }
1394 }
be3492ec 1395
c5b32ec1
KS
1396 /* calculate RND */
1397 if(v->s.pict_type == I_TYPE)
1398 v->rnd = 1;
1399 if(v->s.pict_type == P_TYPE)
1400 v->rnd ^= 1;
1401
be3492ec
KS
1402 /* Quantizer stuff */
1403 pqindex = get_bits(gb, 5);
1404 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1405 v->pq = pquant_table[0][pqindex];
1406 else
b53921aa 1407 v->pq = pquant_table[1][pqindex];
be3492ec 1408
0a45801f 1409 v->pquantizer = 1;
be3492ec
KS
1410 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1411 v->pquantizer = pqindex < 9;
0a45801f
KS
1412 if (v->quantizer_mode == QUANT_NON_UNIFORM)
1413 v->pquantizer = 0;
be3492ec
KS
1414 v->pqindex = pqindex;
1415 if (pqindex < 9) v->halfpq = get_bits(gb, 1);
1416 else v->halfpq = 0;
1417 if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
1418 v->pquantizer = get_bits(gb, 1);
1419 v->dquantfrm = 0;
5c4b8efd
KS
1420 if (v->extended_mv == 1) v->mvrange = get_prefix(gb, 0, 3);
1421 v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1422 v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
1423 v->range_x = 1 << (v->k_x - 1);
1424 v->range_y = 1 << (v->k_y - 1);
1425 if (v->profile == PROFILE_ADVANCED)
1426 {
1427 if (v->postprocflag) v->postproc = get_bits(gb, 1);
1428 }
1429 else
1430 if (v->multires && v->s.pict_type != B_TYPE) v->respic = get_bits(gb, 2);
be3492ec
KS
1431
1432//av_log(v->s.avctx, AV_LOG_INFO, "%c Frame: QP=[%i]%i (+%i/2) %i\n",
1433// (v->s.pict_type == P_TYPE) ? 'P' : ((v->s.pict_type == I_TYPE) ? 'I' : 'B'), pqindex, v->pq, v->halfpq, v->rangeredfrm);
1434
1435 //TODO: complete parsing for P/B/BI frames
1436 switch(v->s.pict_type) {
1437 case P_TYPE:
1438 if (v->pq < 5) v->tt_index = 0;
1439 else if(v->pq < 13) v->tt_index = 1;
1440 else v->tt_index = 2;
1441
be3492ec
KS
1442 lowquant = (v->pq > 12) ? 0 : 1;
1443 v->mv_mode = mv_pmode_table[lowquant][get_prefix(gb, 1, 4)];
1444 if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
1445 {
66d0ad26
KS
1446 int scale, shift, i;
1447 v->mv_mode2 = mv_pmode_table2[lowquant][get_prefix(gb, 1, 3)];
be3492ec
KS
1448 v->lumscale = get_bits(gb, 6);
1449 v->lumshift = get_bits(gb, 6);
66d0ad26
KS
1450 /* fill lookup tables for intensity compensation */
1451 if(!v->lumscale) {
1452 scale = -64;
1453 shift = (255 - v->lumshift * 2) << 6;
1454 if(v->lumshift > 31)
1455 shift += 128 << 6;
1456 } else {
1457 scale = v->lumscale + 32;
1458 if(v->lumshift > 31)
1459 shift = (v->lumshift - 64) << 6;
1460 else
1461 shift = v->lumshift << 6;
1462 }
1463 for(i = 0; i < 256; i++) {
1464 v->luty[i] = clip_uint8((scale * i + shift + 32) >> 6);
1465 v->lutuv[i] = clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
1466 }
be3492ec
KS
1467 }
1468 if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
87dfe848 1469 v->s.quarter_sample = 0;
05103ed3
KS
1470 else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1471 if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
1472 v->s.quarter_sample = 0;
1473 else
1474 v->s.quarter_sample = 1;
1475 } else
87dfe848 1476 v->s.quarter_sample = 1;
09be55df 1477 v->s.mspel = !(v->mv_mode == MV_PMODE_1MV_HPEL_BILIN || (v->mv_mode == MV_PMODE_INTENSITY_COMP && v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN));
be3492ec 1478
be3492ec
KS
1479 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
1480 v->mv_mode2 == MV_PMODE_MIXED_MV)
1481 || v->mv_mode == MV_PMODE_MIXED_MV)
1482 {
87dfe848 1483 status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
be3492ec
KS
1484 if (status < 0) return -1;
1485 av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
1486 "Imode: %i, Invert: %i\n", status>>1, status&1);
87dfe848
KS
1487 } else {
1488 v->mv_type_is_raw = 0;
1489 memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
be3492ec 1490 }
0f7344aa 1491 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
be3492ec
KS
1492 if (status < 0) return -1;
1493 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1494 "Imode: %i, Invert: %i\n", status>>1, status&1);
1495
1496 /* Hopefully this is correct for P frames */
1497 v->s.mv_table_index = get_bits(gb, 2); //but using vc1_ tables
1498 v->cbpcy_vlc = &vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1499
1500 if (v->dquant)
1501 {
1502 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1503 vop_dquant_decoding(v);
1504 }
1505
1506 v->ttfrm = 0; //FIXME Is that so ?
1507 if (v->vstransform)
1508 {
1509 v->ttmbf = get_bits(gb, 1);
1510 if (v->ttmbf)
1511 {
8a66a390 1512 v->ttfrm = ttfrm_to_tt[get_bits(gb, 2)];
be3492ec 1513 }
b53921aa
KS
1514 } else {
1515 v->ttmbf = 1;
1516 v->ttfrm = TT_8X8;
be3492ec
KS
1517 }
1518 break;
1519 case B_TYPE:
5df68893
KS
1520 if (v->pq < 5) v->tt_index = 0;
1521 else if(v->pq < 13) v->tt_index = 1;
1522 else v->tt_index = 2;
1523
1524 lowquant = (v->pq > 12) ? 0 : 1;
1525 v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
1526 v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
09be55df 1527 v->s.mspel = v->s.quarter_sample;
5df68893
KS
1528
1529 status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
1530 if (status < 0) return -1;
1531 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
1532 "Imode: %i, Invert: %i\n", status>>1, status&1);
1533 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1534 if (status < 0) return -1;
1535 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1536 "Imode: %i, Invert: %i\n", status>>1, status&1);
1537
1538 v->s.mv_table_index = get_bits(gb, 2);
1539 v->cbpcy_vlc = &vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1540
1541 if (v->dquant)
1542 {
1543 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1544 vop_dquant_decoding(v);
1545 }
1546
1547 v->ttfrm = 0;
1548 if (v->vstransform)
1549 {
1550 v->ttmbf = get_bits(gb, 1);
1551 if (v->ttmbf)
1552 {
1553 v->ttfrm = ttfrm_to_tt[get_bits(gb, 2)];
1554 }
1555 } else {
1556 v->ttmbf = 1;
1557 v->ttfrm = TT_8X8;
1558 }
be3492ec 1559 break;
21aa398f 1560 }
be3492ec
KS
1561
1562 /* AC Syntax */
1563 v->c_ac_table_index = decode012(gb);
1564 if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
21aa398f 1565 {
be3492ec 1566 v->y_ac_table_index = decode012(gb);
21aa398f 1567 }
be3492ec
KS
1568 /* DC Syntax */
1569 v->s.dc_table_index = get_bits(gb, 1);
1570
21aa398f
AB
1571 return 0;
1572}
21aa398f 1573
3c275f6d
KS
1574static int vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb)
1575{
1576 int fcm;
1577 int pqindex, lowquant;
1578 int status;
1579
1580 v->p_frame_skipped = 0;
1581
1582 if(v->interlace)
1583 fcm = decode012(gb);
1584 switch(get_prefix(gb, 0, 4)) {
1585 case 0:
1586 v->s.pict_type = P_TYPE;
1587 break;
1588 case 1:
1589 v->s.pict_type = B_TYPE;
1590 return -1;
1591// break;
1592 case 2:
1593 v->s.pict_type = I_TYPE;
1594 break;
1595 case 3:
1596 v->s.pict_type = BI_TYPE;
1597 return -1;
1598// break;
1599 case 4:
1600 v->s.pict_type = P_TYPE; // skipped pic
1601 v->p_frame_skipped = 1;
1602 return 0;
1603 }
1604 if(v->tfcntrflag)
1605 get_bits(gb, 8);
1606 if(v->broadcast) {
1607 if(!v->interlace || v->panscanflag) {
1608 get_bits(gb, 2);
1609 } else {
1610 get_bits1(gb);
1611 get_bits1(gb);
1612 }
1613 }
1614 if(v->panscanflag) {
1615 //...
1616 }
1617 v->rnd = get_bits1(gb);
1618 if(v->interlace)
1619 v->uvsamp = get_bits1(gb);
1620 if(v->finterpflag) v->interpfrm = get_bits(gb, 1);
1621 pqindex = get_bits(gb, 5);
1622 v->pqindex = pqindex;
1623 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1624 v->pq = pquant_table[0][pqindex];
1625 else
1626 v->pq = pquant_table[1][pqindex];
1627
1628 v->pquantizer = 1;
1629 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1630 v->pquantizer = pqindex < 9;
1631 if (v->quantizer_mode == QUANT_NON_UNIFORM)
1632 v->pquantizer = 0;
1633 v->pqindex = pqindex;
1634 if (pqindex < 9) v->halfpq = get_bits(gb, 1);
1635 else v->halfpq = 0;
1636 if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
1637 v->pquantizer = get_bits(gb, 1);
1638
1639 switch(v->s.pict_type) {
1640 case I_TYPE:
1641 status = bitplane_decoding(v->acpred_plane, &v->acpred_is_raw, v);
1642 if (status < 0) return -1;
1643 av_log(v->s.avctx, AV_LOG_DEBUG, "ACPRED plane encoding: "
1644 "Imode: %i, Invert: %i\n", status>>1, status&1);
1645 v->condover = CONDOVER_NONE;
1646 if(v->overlap && v->pq <= 8) {
1647 v->condover = decode012(gb);
1648 if(v->condover == CONDOVER_SELECT) {
1649 status = bitplane_decoding(v->over_flags_plane, &v->overflg_is_raw, v);
1650 if (status < 0) return -1;
1651 av_log(v->s.avctx, AV_LOG_DEBUG, "CONDOVER plane encoding: "
1652 "Imode: %i, Invert: %i\n", status>>1, status&1);
1653 }
1654 }
1655 break;
1656 case P_TYPE:
1657 if(v->postprocflag)
1658 v->postproc = get_bits1(gb);
1659 if (v->extended_mv) v->mvrange = get_prefix(gb, 0, 3);
1660 else v->mvrange = 0;
1661 v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1662 v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
1663 v->range_x = 1 << (v->k_x - 1);
1664 v->range_y = 1 << (v->k_y - 1);
1665
1666 if (v->pq < 5) v->tt_index = 0;
1667 else if(v->pq < 13) v->tt_index = 1;
1668 else v->tt_index = 2;
1669
1670 lowquant = (v->pq > 12) ? 0 : 1;
1671 v->mv_mode = mv_pmode_table[lowquant][get_prefix(gb, 1, 4)];
1672 if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
1673 {
1674 int scale, shift, i;
1675 v->mv_mode2 = mv_pmode_table2[lowquant][get_prefix(gb, 1, 3)];
1676 v->lumscale = get_bits(gb, 6);
1677 v->lumshift = get_bits(gb, 6);
1678 /* fill lookup tables for intensity compensation */
1679 if(!v->lumscale) {
1680 scale = -64;
1681 shift = (255 - v->lumshift * 2) << 6;
1682 if(v->lumshift > 31)
1683 shift += 128 << 6;
1684 } else {
1685 scale = v->lumscale + 32;
1686 if(v->lumshift > 31)
1687 shift = (v->lumshift - 64) << 6;
1688 else
1689 shift = v->lumshift << 6;
1690 }
1691 for(i = 0; i < 256; i++) {
1692 v->luty[i] = clip_uint8((scale * i + shift + 32) >> 6);
1693 v->lutuv[i] = clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
1694 }
1695 }
1696 if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
1697 v->s.quarter_sample = 0;
1698 else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1699 if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
1700 v->s.quarter_sample = 0;
1701 else
1702 v->s.quarter_sample = 1;
1703 } else
1704 v->s.quarter_sample = 1;
1705 v->s.mspel = !(v->mv_mode == MV_PMODE_1MV_HPEL_BILIN || (v->mv_mode == MV_PMODE_INTENSITY_COMP && v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN));
1706
1707 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
1708 v->mv_mode2 == MV_PMODE_MIXED_MV)
1709 || v->mv_mode == MV_PMODE_MIXED_MV)
1710 {
1711 status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
1712 if (status < 0) return -1;
1713 av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
1714 "Imode: %i, Invert: %i\n", status>>1, status&1);
1715 } else {
1716 v->mv_type_is_raw = 0;
1717 memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
1718 }
1719 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1720 if (status < 0) return -1;
1721 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1722 "Imode: %i, Invert: %i\n", status>>1, status&1);
1723
1724 /* Hopefully this is correct for P frames */
1725 v->s.mv_table_index = get_bits(gb, 2); //but using vc1_ tables
1726 v->cbpcy_vlc = &vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1727 if (v->dquant)
1728 {
1729 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1730 vop_dquant_decoding(v);
1731 }
1732
1733 v->ttfrm = 0; //FIXME Is that so ?
1734 if (v->vstransform)
1735 {
1736 v->ttmbf = get_bits(gb, 1);
1737 if (v->ttmbf)
1738 {
1739 v->ttfrm = ttfrm_to_tt[get_bits(gb, 2)];
1740 }
1741 } else {
1742 v->ttmbf = 1;
1743 v->ttfrm = TT_8X8;
1744 }
1745 break;
1746 }
1747
1748 /* AC Syntax */
1749 v->c_ac_table_index = decode012(gb);
1750 if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
1751 {
1752 v->y_ac_table_index = decode012(gb);
1753 }
1754 /* DC Syntax */
1755 v->s.dc_table_index = get_bits(gb, 1);
1756 if (v->s.pict_type == I_TYPE && v->dquant) {
1757 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1758 vop_dquant_decoding(v);
1759 }
1760
1761 return 0;
1762}
1763
2ce151f8 1764/***********************************************************************/
1765/**
be3492ec
KS
1766 * @defgroup block VC-1 Block-level functions
1767 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1768 * @todo TODO: Integrate to MpegEncContext facilities
2ce151f8 1769 * @{
1770 */
1771
be3492ec
KS
1772/**
1773 * @def GET_MQUANT
1774 * @brief Get macroblock-level quantizer scale
1775 * @warning XXX: qdiff to the frame quant, not previous quant ?
1776 * @fixme XXX: Don't know how to initialize mquant otherwise in last case
2ce151f8 1777 */
be3492ec
KS
1778#define GET_MQUANT() \
1779 if (v->dquantfrm) \
1780 { \
35a9cac8 1781 int edges = 0; \
be3492ec
KS
1782 if (v->dqprofile == DQPROFILE_ALL_MBS) \
1783 { \
1784 if (v->dqbilevel) \
1785 { \
3a3f1cf3 1786 mquant = (get_bits(gb, 1)) ? v->altpq : v->pq; \
be3492ec
KS
1787 } \
1788 else \
1789 { \
1790 mqdiff = get_bits(gb, 3); \
1791 if (mqdiff != 7) mquant = v->pq + mqdiff; \
1792 else mquant = get_bits(gb, 5); \
1793 } \
1794 } \
35a9cac8
KS
1795 if(v->dqprofile == DQPROFILE_SINGLE_EDGE) \
1796 edges = 1 << v->dqsbedge; \
e4bf0302 1797 else if(v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
35a9cac8 1798 edges = (3 << v->dqsbedge) % 15; \
e4bf0302 1799 else if(v->dqprofile == DQPROFILE_FOUR_EDGES) \
35a9cac8 1800 edges = 15; \
35a9cac8
KS
1801 if((edges&1) && !s->mb_x) \
1802 mquant = v->altpq; \
3a3f1cf3 1803 if((edges&2) && s->first_slice_line) \
35a9cac8
KS
1804 mquant = v->altpq; \
1805 if((edges&4) && s->mb_x == (s->mb_width - 1)) \
1806 mquant = v->altpq; \
1807 if((edges&8) && s->mb_y == (s->mb_height - 1)) \
1808 mquant = v->altpq; \
be3492ec 1809 }
2ce151f8 1810
be3492ec
KS
1811/**
1812 * @def GET_MVDATA(_dmv_x, _dmv_y)
1813 * @brief Get MV differentials
1814 * @see MVDATA decoding from 8.3.5.2, p(1)20
1815 * @param _dmv_x Horizontal differential for decoded MV
1816 * @param _dmv_y Vertical differential for decoded MV
1817 * @todo TODO: Use MpegEncContext arrays to store them
2ce151f8 1818 */
be3492ec
KS
1819#define GET_MVDATA(_dmv_x, _dmv_y) \
1820 index = 1 + get_vlc2(gb, vc1_mv_diff_vlc[s->mv_table_index].table,\
1821 VC1_MV_DIFF_VLC_BITS, 2); \
1822 if (index > 36) \
1823 { \
1824 mb_has_coeffs = 1; \
1825 index -= 37; \
1826 } \
1827 else mb_has_coeffs = 0; \
1828 s->mb_intra = 0; \
1829 if (!index) { _dmv_x = _dmv_y = 0; } \
1830 else if (index == 35) \
1831 { \
87dfe848
KS
1832 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
1833 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
be3492ec
KS
1834 } \
1835 else if (index == 36) \
1836 { \
1837 _dmv_x = 0; \
1838 _dmv_y = 0; \
1839 s->mb_intra = 1; \
1840 } \
1841 else \
1842 { \
1843 index1 = index%6; \
87dfe848
KS
1844 if (!s->quarter_sample && index1 == 5) val = 1; \
1845 else val = 0; \
e8ba1cea
KS
1846 if(size_table[index1] - val > 0) \
1847 val = get_bits(gb, size_table[index1] - val); \
1848 else val = 0; \
be3492ec
KS
1849 sign = 0 - (val&1); \
1850 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1851 \
1852 index1 = index/6; \
87dfe848
KS
1853 if (!s->quarter_sample && index1 == 5) val = 1; \
1854 else val = 0; \
e8ba1cea
KS
1855 if(size_table[index1] - val > 0) \
1856 val = get_bits(gb, size_table[index1] - val); \
1857 else val = 0; \
be3492ec
KS
1858 sign = 0 - (val&1); \
1859 _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1860 }
21aa398f 1861
be3492ec 1862/** Predict and set motion vector
2ce151f8 1863 */
e4bf0302 1864static 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)
0d33db8a 1865{
e4bf0302 1866 int xy, wrap, off = 0;
be3492ec
KS
1867 int16_t *A, *B, *C;
1868 int px, py;
1869 int sum;
0d33db8a 1870
be3492ec 1871 /* scale MV difference to be quad-pel */
87dfe848
KS
1872 dmv_x <<= 1 - s->quarter_sample;
1873 dmv_y <<= 1 - s->quarter_sample;
d29f0cd9 1874
be3492ec 1875 wrap = s->b8_stride;
e4bf0302 1876 xy = s->block_index[n];
d29f0cd9 1877
e4bf0302
KS
1878 if(s->mb_intra){
1879 s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
1880 s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
1881 if(mv1) { /* duplicate motion data for 1-MV block */
1882 s->current_picture.motion_val[0][xy + 1][0] = 0;
1883 s->current_picture.motion_val[0][xy + 1][1] = 0;
1884 s->current_picture.motion_val[0][xy + wrap][0] = 0;
1885 s->current_picture.motion_val[0][xy + wrap][1] = 0;
1886 s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
1887 s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
1888 }
1889 return;
1890 }
1891
1892 C = s->current_picture.motion_val[0][xy - 1];
1893 A = s->current_picture.motion_val[0][xy - wrap];
1894 if(mv1)
1895 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1896 else {
1897 //in 4-MV mode different blocks have different B predictor position
1898 switch(n){
1899 case 0:
1900 off = (s->mb_x > 0) ? -1 : 1;
1901 break;
1902 case 1:
1903 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1904 break;
1905 case 2:
1906 off = 1;
1907 break;
1908 case 3:
1909 off = -1;
1910 }
1911 }
1912 B = s->current_picture.motion_val[0][xy - wrap + off];
d29f0cd9 1913
e4bf0302 1914 if(!s->first_slice_line || (n==2 || n==3)) { // predictor A is not out of bounds
be3492ec
KS
1915 if(s->mb_width == 1) {
1916 px = A[0];
1917 py = A[1];
1918 } else {
1919 px = mid_pred(A[0], B[0], C[0]);
1920 py = mid_pred(A[1], B[1], C[1]);
1921 }
e4bf0302 1922 } else if(s->mb_x || (n==1 || n==3)) { // predictor C is not out of bounds
be3492ec
KS
1923 px = C[0];
1924 py = C[1];
1925 } else {
1926 px = py = 0;
1927 }
be3492ec
KS
1928 /* Pullback MV as specified in 8.3.5.3.4 */
1929 {
1930 int qx, qy, X, Y;
ef6cc8ce
KS
1931 qx = (s->mb_x << 6) + ((n==1 || n==3) ? 32 : 0);
1932 qy = (s->mb_y << 6) + ((n==2 || n==3) ? 32 : 0);
be3492ec
KS
1933 X = (s->mb_width << 6) - 4;
1934 Y = (s->mb_height << 6) - 4;
1935 if(mv1) {
1936 if(qx + px < -60) px = -60 - qx;
1937 if(qy + py < -60) py = -60 - qy;
1938 } else {
1939 if(qx + px < -28) px = -28 - qx;
1940 if(qy + py < -28) py = -28 - qy;
1941 }
1942 if(qx + px > X) px = X - qx;
1943 if(qy + py > Y) py = Y - qy;
1944 }
1945 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
e4bf0302
KS
1946 if((!s->first_slice_line || (n==2 || n==3)) && (s->mb_x || (n==1 || n==3))) {
1947 if(is_intra[xy - wrap])
be3492ec 1948 sum = ABS(px) + ABS(py);
d29f0cd9 1949 else
be3492ec
KS
1950 sum = ABS(px - A[0]) + ABS(py - A[1]);
1951 if(sum > 32) {
1952 if(get_bits1(&s->gb)) {
1953 px = A[0];
1954 py = A[1];
1955 } else {
1956 px = C[0];
1957 py = C[1];
1958 }
1959 } else {
e4bf0302 1960 if(is_intra[xy - 1])
be3492ec
KS
1961 sum = ABS(px) + ABS(py);
1962 else
1963 sum = ABS(px - C[0]) + ABS(py - C[1]);
1964 if(sum > 32) {
1965 if(get_bits1(&s->gb)) {
1966 px = A[0];
1967 py = A[1];
1968 } else {
1969 px = C[0];
1970 py = C[1];
1971 }
1972 }
1973 }
d29f0cd9 1974 }
be3492ec 1975 /* store MV using signed modulus of MV range defined in 4.11 */
e4bf0302
KS
1976 s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1977 s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
1978 if(mv1) { /* duplicate motion data for 1-MV block */
1979 s->current_picture.motion_val[0][xy + 1][0] = s->current_picture.motion_val[0][xy][0];
1980 s->current_picture.motion_val[0][xy + 1][1] = s->current_picture.motion_val[0][xy][1];
1981 s->current_picture.motion_val[0][xy + wrap][0] = s->current_picture.motion_val[0][xy][0];
1982 s->current_picture.motion_val[0][xy + wrap][1] = s->current_picture.motion_val[0][xy][1];
1983 s->current_picture.motion_val[0][xy + wrap + 1][0] = s->current_picture.motion_val[0][xy][0];
1984 s->current_picture.motion_val[0][xy + wrap + 1][1] = s->current_picture.motion_val[0][xy][1];
1985 }
d29f0cd9
MN
1986}
1987
fb2d9140
KS
1988/** Motion compensation for direct or interpolated blocks in B-frames
1989 */
1990static void vc1_interp_mc(VC1Context *v)
1991{
1992 MpegEncContext *s = &v->s;
1993 DSPContext *dsp = &v->s.dsp;
1994 uint8_t *srcY, *srcU, *srcV;
1995 int dxy, uvdxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1996
1997 if(!v->s.next_picture.data[0])return;
1998
1999 mx = s->mv[1][0][0];
2000 my = s->mv[1][0][1];
2001 uvmx = (mx + ((mx & 3) == 3)) >> 1;
2002 uvmy = (my + ((my & 3) == 3)) >> 1;
2003 srcY = s->next_picture.data[0];
2004 srcU = s->next_picture.data[1];
2005 srcV = s->next_picture.data[2];
2006
2007 src_x = s->mb_x * 16 + (mx >> 2);
2008 src_y = s->mb_y * 16 + (my >> 2);
2009 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
2010 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
2011
2012 src_x = clip( src_x, -16, s->mb_width * 16);
2013 src_y = clip( src_y, -16, s->mb_height * 16);
2014 uvsrc_x = clip(uvsrc_x, -8, s->mb_width * 8);
2015 uvsrc_y = clip(uvsrc_y, -8, s->mb_height * 8);
2016
2017 srcY += src_y * s->linesize + src_x;
2018 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
2019 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
2020
2021 /* for grayscale we should not try to read from unknown area */
2022 if(s->flags & CODEC_FLAG_GRAY) {
2023 srcU = s->edge_emu_buffer + 18 * s->linesize;
2024 srcV = s->edge_emu_buffer + 18 * s->linesize;
2025 }
2026
2027 if(v->rangeredfrm
2028 || (unsigned)src_x > s->h_edge_pos - (mx&3) - 16
2029 || (unsigned)src_y > s->v_edge_pos - (my&3) - 16){
2030 uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
2031
2032 ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17, 17,
2033 src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
2034 srcY = s->edge_emu_buffer;
2035 ff_emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8+1, 8+1,
2036 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
2037 ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
2038 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
2039 srcU = uvbuf;
2040 srcV = uvbuf + 16;
2041 /* if we deal with range reduction we need to scale source blocks */
2042 if(v->rangeredfrm) {
2043 int i, j;
2044 uint8_t *src, *src2;
2045
2046 src = srcY;
2047 for(j = 0; j < 17; j++) {
2048 for(i = 0; i < 17; i++) src[i] = ((src[i] - 128) >> 1) + 128;
2049 src += s->linesize;
2050 }
2051 src = srcU; src2 = srcV;
2052 for(j = 0; j < 9; j++) {
2053 for(i = 0; i < 9; i++) {
2054 src[i] = ((src[i] - 128) >> 1) + 128;
2055 src2[i] = ((src2[i] - 128) >> 1) + 128;
2056 }
2057 src += s->uvlinesize;
2058 src2 += s->uvlinesize;
2059 }
2060 }
2061 }
2062
2063 if(v->fastuvmc) {
2064 uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
2065 uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
2066 }
2067
66ff2c1f
KS
2068 mx >>= 1;
2069 my >>= 1;
2070 dxy = ((my & 1) << 1) | (mx & 1);
fb2d9140 2071
66ff2c1f 2072 dsp->avg_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
fb2d9140
KS
2073
2074 if(s->flags & CODEC_FLAG_GRAY) return;
2075 /* Chroma MC always uses qpel blilinear */
2076 uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
2077 dsp->avg_qpel_pixels_tab[1][uvdxy](s->dest[1], srcU, s->uvlinesize);
2078 dsp->avg_qpel_pixels_tab[1][uvdxy](s->dest[2], srcV, s->uvlinesize);
2079}
2080
5df68893
KS
2081/** Reconstruct motion vector for B-frame and do motion compensation
2082 */
2083static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mode)
2084{
2085 MpegEncContext *s = &v->s;
2086 int mx[4], my[4], mv_x, mv_y;
2087 int i;
2088
2089 /* scale MV difference to be quad-pel */
2090 dmv_x[0] <<= 1 - s->quarter_sample;
2091 dmv_y[0] <<= 1 - s->quarter_sample;
2092 dmv_x[1] <<= 1 - s->quarter_sample;
2093 dmv_y[1] <<= 1 - s->quarter_sample;
2094
fb2d9140
KS
2095 if(direct) {
2096 for(i = 0; i < 4; i++) {
2097 mx[i] = s->last_picture.motion_val[0][s->block_index[i]][0];
2098 my[i] = s->last_picture.motion_val[0][s->block_index[i]][1];
2099 }
2100 mv_x = median4(mx[0], mx[1], mx[2], mx[3]);
2101 mv_y = median4(my[0], my[1], my[2], my[3]);
2102 s->mv[0][0][0] = (mv_x * v->bfraction + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2103 s->mv[0][0][1] = (mv_y * v->bfraction + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2104 vc1_mc_1mv(v, 0);
2105
2106 for(i = 0; i < 4; i++) {
2107 mx[i] = s->next_picture.motion_val[0][s->block_index[i]][0];
2108 my[i] = s->next_picture.motion_val[0][s->block_index[i]][1];
2109 }
2110 mv_x = median4(mx[0], mx[1], mx[2], mx[3]);
2111 mv_y = median4(my[0], my[1], my[2], my[3]);
2112 s->mv[1][0][0] = (mv_x * (B_FRACTION_DEN - v->bfraction) + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2113 s->mv[1][0][1] = (mv_y * (B_FRACTION_DEN - v->bfraction) + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2114 vc1_interp_mc(v);
2115 return;
2116 }
2117 if(mode == BMV_TYPE_INTERPOLATED) {
2118 s->mv[0][0][0] = dmv_x[0];
2119 s->mv[0][0][1] = dmv_y[0];
2120 vc1_mc_1mv(v, 0);
2121 s->mv[1][0][0] = dmv_x[1];
2122 s->mv[1][0][1] = dmv_y[1];
2123 vc1_interp_mc(v);
5df68893
KS
2124 return;
2125 }
2126
2127 if(mode == BMV_TYPE_BACKWARD) {
2128 for(i = 0; i < 4; i++) {
2129 mx[i] = s->last_picture.motion_val[0][s->block_index[i]][0];
2130 my[i] = s->last_picture.motion_val[0][s->block_index[i]][1];
2131 }
2132 } else {
2133 for(i = 0; i < 4; i++) {
2134 mx[i] = s->next_picture.motion_val[0][s->block_index[i]][0];
2135 my[i] = s->next_picture.motion_val[0][s->block_index[i]][1];
2136 }
2137 }
2138
2139 /* XXX: not right but how to determine 4-MV intra/inter in another frame? */
2140 mv_x = median4(mx[0], mx[1], mx[2], mx[3]);
2141 mv_y = median4(my[0], my[1], my[2], my[3]);
2142 s->mv[0][0][0] = mv_x;
2143 s->mv[0][0][1] = mv_y;
2144
2145 vc1_mc_1mv(v, (mode == BMV_TYPE_FORWARD));
2146}
2147
be3492ec
KS
2148/** Get predicted DC value for I-frames only
2149 * prediction dir: left=0, top=1
2150 * @param s MpegEncContext
2151 * @param[in] n block index in the current MB
2152 * @param dc_val_ptr Pointer to DC predictor
2153 * @param dir_ptr Prediction direction for use in AC prediction
2ce151f8 2154 */
be3492ec
KS
2155static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2156 int16_t **dc_val_ptr, int *dir_ptr)
21aa398f 2157{
be3492ec
KS
2158 int a, b, c, wrap, pred, scale;
2159 int16_t *dc_val;
2160 static const uint16_t dcpred[32] = {
2161 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2162 114, 102, 93, 85, 79, 73, 68, 64,
2163 60, 57, 54, 51, 49, 47, 45, 43,
2164 41, 39, 38, 37, 35, 34, 33
2165 };
0d33db8a 2166
be3492ec
KS
2167 /* find prediction - wmv3_dc_scale always used here in fact */
2168 if (n < 4) scale = s->y_dc_scale;
2169 else scale = s->c_dc_scale;
21aa398f 2170
be3492ec
KS
2171 wrap = s->block_wrap[n];
2172 dc_val= s->dc_val[0] + s->block_index[n];
21aa398f 2173
be3492ec
KS
2174 /* B A
2175 * C X
2176 */
2177 c = dc_val[ - 1];
2178 b = dc_val[ - 1 - wrap];
2179 a = dc_val[ - wrap];
21aa398f 2180
be3492ec 2181 if (pq < 9 || !overlap)
21aa398f 2182 {
be3492ec 2183 /* Set outer values */
d2779ecd 2184 if (s->first_slice_line && (n!=2 && n!=3)) b=a=dcpred[scale];
be3492ec 2185 if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=dcpred[scale];
bf2bc926 2186 }
2187 else
2188 {
be3492ec 2189 /* Set outer values */
d2779ecd 2190 if (s->first_slice_line && (n!=2 && n!=3)) b=a=0;
be3492ec 2191 if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=0;
21aa398f 2192 }
21aa398f 2193
be3492ec
KS
2194 if (abs(a - b) <= abs(b - c)) {
2195 pred = c;
2196 *dir_ptr = 1;//left
2197 } else {
2198 pred = a;
2199 *dir_ptr = 0;//top
21aa398f
AB
2200 }
2201
be3492ec
KS
2202 /* update predictor */
2203 *dc_val_ptr = &dc_val[0];
2204 return pred;
bf2bc926 2205}
2206
21aa398f 2207
be3492ec
KS
2208/** Get predicted DC value
2209 * prediction dir: left=0, top=1
2210 * @param s MpegEncContext
2211 * @param[in] n block index in the current MB
2212 * @param dc_val_ptr Pointer to DC predictor
2213 * @param dir_ptr Prediction direction for use in AC prediction
2ce151f8 2214 */
be3492ec
KS
2215static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2216 int a_avail, int c_avail,
2217 int16_t **dc_val_ptr, int *dir_ptr)
21aa398f 2218{
be3492ec
KS
2219 int a, b, c, wrap, pred, scale;
2220 int16_t *dc_val;
2221 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
50af2fd3 2222 int q1, q2 = 0;
21aa398f 2223
be3492ec
KS
2224 /* find prediction - wmv3_dc_scale always used here in fact */
2225 if (n < 4) scale = s->y_dc_scale;
2226 else scale = s->c_dc_scale;
21aa398f 2227
be3492ec
KS
2228 wrap = s->block_wrap[n];
2229 dc_val= s->dc_val[0] + s->block_index[n];
21aa398f 2230
be3492ec
KS
2231 /* B A
2232 * C X
2233 */
2234 c = dc_val[ - 1];
2235 b = dc_val[ - 1 - wrap];
2236 a = dc_val[ - wrap];
27ed1a0d
KS
2237 /* scale predictors if needed */
2238 q1 = s->current_picture.qscale_table[mb_pos];
2239 if(c_avail && (n!= 1 && n!=3)) {
2240 q2 = s->current_picture.qscale_table[mb_pos - 1];
2241 if(q2 && q2 != q1)
2242 c = (c * s->y_dc_scale_table[q2] * vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
2243 }
2244 if(a_avail && (n!= 2 && n!=3)) {
2245 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2246 if(q2 && q2 != q1)
2247 a = (a * s->y_dc_scale_table[q2] * vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
2248 }
2249 if(a_avail && c_avail && (n!=3)) {
2250 int off = mb_pos;
2251 if(n != 1) off--;
2252 if(n != 2) off -= s->mb_stride;
2253 q2 = s->current_picture.qscale_table[off];
2254 if(q2 && q2 != q1)
2255 b = (b * s->y_dc_scale_table[q2] * vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
2256 }
21aa398f 2257
be3492ec
KS
2258 if(a_avail && c_avail) {
2259 if(abs(a - b) <= abs(b - c)) {
2260 pred = c;
2261 *dir_ptr = 1;//left
2262 } else {
2263 pred = a;
2264 *dir_ptr = 0;//top
2265 }
2266 } else if(a_avail) {
2267 pred = a;
2268 *dir_ptr = 0;//top
2269 } else if(c_avail) {
2270 pred = c;
2271 *dir_ptr = 1;//left
2272 } else {
2273 pred = 0;
2274 *dir_ptr = 1;//left
21aa398f
AB
2275 }
2276
be3492ec
KS
2277 /* update predictor */
2278 *dc_val_ptr = &dc_val[0];
2279 return pred;
21aa398f 2280}
2ce151f8 2281
21aa398f 2282
115329f1 2283/**
be3492ec
KS
2284 * @defgroup std_mb VC1 Macroblock-level functions in Simple/Main Profiles
2285 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2286 * @todo TODO: Integrate to MpegEncContext facilities
2ce151f8 2287 * @{
2288 */
21aa398f 2289
be3492ec 2290static inline int vc1_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
21aa398f 2291{
be3492ec 2292 int xy, wrap, pred, a, b, c;
21aa398f 2293
be3492ec
KS
2294 xy = s->block_index[n];
2295 wrap = s->b8_stride;
21aa398f 2296
be3492ec
KS
2297 /* B C
2298 * A X
2299 */
2300 a = s->coded_block[xy - 1 ];
2301 b = s->coded_block[xy - 1 - wrap];
2302 c = s->coded_block[xy - wrap];
21aa398f 2303
be3492ec
KS
2304 if (b == c) {
2305 pred = a;
2306 } else {
2307 pred = c;
21aa398f 2308 }
be3492ec
KS
2309
2310 /* store value */
2311 *coded_block_ptr = &s->coded_block[xy];
2312
2313 return pred;
0d33db8a 2314}
2315
be3492ec
KS
2316/**
2317 * Decode one AC coefficient
2318 * @param v The VC1 context
2319 * @param last Last coefficient
2320 * @param skip How much zero coefficients to skip
2321 * @param value Decoded AC coefficient value
2322 * @see 8.1.3.4
2ce151f8 2323 */
be3492ec 2324static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip, int *value, int codingset)
0d33db8a 2325{
2326 GetBitContext *gb = &v->s.gb;
be3492ec
KS
2327 int index, escape, run = 0, level = 0, lst = 0;
2328
2329 index = get_vlc2(gb, vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2330 if (index != vc1_ac_sizes[codingset] - 1) {
2331 run = vc1_index_decode_table[codingset][index][0];
2332 level = vc1_index_decode_table[codingset][index][1];
2333 lst = index >= vc1_last_decode_table[codingset];
2334 if(get_bits(gb, 1))
2335 level = -level;
2336 } else {
2337 escape = decode210(gb);
87dfe848 2338 if (escape != 2) {
be3492ec
KS
2339 index = get_vlc2(gb, vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2340 run = vc1_index_decode_table[codingset][index][0];
2341 level = vc1_index_decode_table[codingset][index][1];
2342 lst = index >= vc1_last_decode_table[codingset];
87dfe848
KS
2343 if(escape == 0) {
2344 if(lst)
2345 level += vc1_last_delta_level_table[codingset][run];
2346 else
2347 level += vc1_delta_level_table[codingset][run];
2348 } else {
2349 if(lst)
2350 run += vc1_last_delta_run_table[codingset][level] + 1;
2351 else
2352 run += vc1_delta_run_table[codingset][level] + 1;
2353 }
be3492ec
KS
2354 if(get_bits(gb, 1))
2355 level = -level;
2356 } else {
2357 int sign;
2358 lst = get_bits(gb, 1);
2359 if(v->s.esc3_level_length == 0) {
2360 if(v->pq < 8 || v->dquantfrm) { // table 59
2361 v->s.esc3_level_length = get_bits(gb, 3);
2362 if(!v->s.esc3_level_length)
2363 v->s.esc3_level_length = get_bits(gb, 2) + 8;
2364 } else { //table 60
2365 v->s.esc3_level_length = get_prefix(gb, 1, 6) + 2;
2366 }
2367 v->s.esc3_run_length = 3 + get_bits(gb, 2);
2368 }
2369 run = get_bits(gb, v->s.esc3_run_length);
2370 sign = get_bits(gb, 1);
2371 level = get_bits(gb, v->s.esc3_level_length);
2372 if(sign)
2373 level = -level;
2374 }
21aa398f 2375 }
7cc84d24 2376
be3492ec
KS
2377 *last = lst;
2378 *skip = run;
2379 *value = level;
21aa398f 2380}
21aa398f 2381
be3492ec
KS
2382/** Decode intra block in intra frames - should be faster than decode_intra_block
2383 * @param v VC1Context
2384 * @param block block to decode
2385 * @param coded are AC coeffs present or not
2386 * @param codingset set of VLC to decode data
2ce151f8 2387 */
be3492ec 2388static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset)
21aa398f 2389{
0d33db8a 2390 GetBitContext *gb = &v->s.gb;
be3492ec
KS
2391 MpegEncContext *s = &v->s;
2392 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2393 int run_diff, i;
2394 int16_t *dc_val;
2395 int16_t *ac_val, *ac_val2;
2396 int dcdiff;
21aa398f 2397
be3492ec
KS
2398 /* Get DC differential */
2399 if (n < 4) {
2400 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2401 } else {
2402 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
21aa398f 2403 }
be3492ec
KS
2404 if (dcdiff < 0){
2405 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2406 return -1;
2407 }
2408 if (dcdiff)
21aa398f 2409 {
be3492ec 2410 if (dcdiff == 119 /* ESC index value */)
21aa398f 2411 {
be3492ec
KS
2412 /* TODO: Optimize */
2413 if (v->pq == 1) dcdiff = get_bits(gb, 10);
2414 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2415 else dcdiff = get_bits(gb, 8);
21aa398f 2416 }
be3492ec 2417 else
21aa398f 2418 {
be3492ec
KS
2419 if (v->pq == 1)
2420 dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
2421 else if (v->pq == 2)
2422 dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
21aa398f 2423 }
be3492ec
KS
2424 if (get_bits(gb, 1))
2425 dcdiff = -dcdiff;
21aa398f 2426 }
21aa398f 2427
be3492ec
KS
2428 /* Prediction */
2429 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2430 *dc_val = dcdiff;
0d33db8a 2431
be3492ec 2432 /* Store the quantized DC coeff, used for prediction */
be3492ec
KS
2433 if (n < 4) {
2434 block[0] = dcdiff * s->y_dc_scale;
2435 } else {
2436 block[0] = dcdiff * s->c_dc_scale;
21aa398f 2437 }
be3492ec
KS
2438 /* Skip ? */
2439 run_diff = 0;
2440 i = 0;
2441 if (!coded) {
2442 goto not_coded;
21aa398f 2443 }
7cc84d24 2444
be3492ec
KS
2445 //AC Decoding
2446 i = 1;
7cc84d24 2447
be3492ec
KS
2448 {
2449 int last = 0, skip, value;
2450 const int8_t *zz_table;
2451 int scale;
2452 int k;
8da75fb2 2453
be3492ec 2454 scale = v->pq * 2 + v->halfpq;
42cc17f9 2455
be3492ec
KS
2456 if(v->s.ac_pred) {
2457 if(!dc_pred_dir)
2458 zz_table = vc1_horizontal_zz;
2459 else
2460 zz_table = vc1_vertical_zz;
2461 } else
2462 zz_table = vc1_normal_zz;
2463
2464 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2465 ac_val2 = ac_val;
2466 if(dc_pred_dir) //left
2467 ac_val -= 16;
2468 else //top
2469 ac_val -= 16 * s->block_wrap[n];
2470
2471 while (!last) {
2472 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2473 i += skip;
2474 if(i > 63)
2475 break;
2476 block[zz_table[i++]] = value;
2477 }
7cc84d24 2478
be3492ec
KS
2479 /* apply AC prediction if needed */
2480 if(s->ac_pred) {
2481 if(dc_pred_dir) { //left
2482 for(k = 1; k < 8; k++)
2483 block[k << 3] += ac_val[k];
2484 } else { //top
2485 for(k = 1; k < 8; k++)
2486 block[k] += ac_val[k + 8];
2487 }
2488 }
2489 /* save AC coeffs for further prediction */
2490 for(k = 1; k < 8; k++) {
2491 ac_val2[k] = block[k << 3];
2492 ac_val2[k + 8] = block[k];
2493 }
42cc17f9 2494
be3492ec
KS
2495 /* scale AC coeffs */
2496 for(k = 1; k < 64; k++)
2497 if(block[k]) {
2498 block[k] *= scale;
2499 if(!v->pquantizer)
2500 block[k] += (block[k] < 0) ? -v->pq : v->pq;
2501 }
8da75fb2 2502
be3492ec
KS
2503 if(s->ac_pred) i = 63;
2504 }
2505
2506not_coded:
2507 if(!coded) {
2508 int k, scale;
2509 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2510 ac_val2 = ac_val;
2511
2512 scale = v->pq * 2 + v->halfpq;
2513 memset(ac_val2, 0, 16 * 2);
2514 if(dc_pred_dir) {//left
2515 ac_val -= 16;
2516 if(s->ac_pred)
2517 memcpy(ac_val2, ac_val, 8 * 2);
2518 } else {//top
2519 ac_val -= 16 * s->block_wrap[n];
2520 if(s->ac_pred)
2521 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
8da75fb2 2522 }
7cc84d24 2523
be3492ec
KS
2524 /* apply AC prediction if needed */
2525 if(s->ac_pred) {
2526 if(dc_pred_dir) { //left
2527 for(k = 1; k < 8; k++) {
2528 block[k << 3] = ac_val[k] * scale;
4b944659 2529 if(!v->pquantizer && block[k << 3])
be3492ec
KS
2530 block[k << 3] += (block[k << 3] < 0) ? -v->pq : v->pq;
2531 }
2532 } else { //top
2533 for(k = 1; k < 8; k++) {
2534 block[k] = ac_val[k + 8] * scale;
4b944659 2535 if(!v->pquantizer && block[k])
be3492ec
KS
2536 block[k] += (block[k] < 0) ? -v->pq : v->pq;
2537 }
2538 }
2539 i = 63;
2540 }
7cc84d24 2541 }
be3492ec 2542 s->block_last_index[n] = i;
7cc84d24 2543
be3492ec 2544 return 0;
7cc84d24 2545}
2546
3c275f6d 2547/** Decode intra block in intra frames - should be faster than decode_intra_block
be3492ec
KS
2548 * @param v VC1Context
2549 * @param block block to decode
2550 * @param coded are AC coeffs present or not
be3492ec 2551 * @param codingset set of VLC to decode data
7cc84d24 2552 */
3c275f6d 2553static int vc1_decode_i_block_adv(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset, int mquant)
e5540b3f 2554{
0d33db8a 2555 GetBitContext *gb = &v->s.gb;
7cc84d24 2556 MpegEncContext *s = &v->s;
be3492ec 2557 int dc_pred_dir = 0; /* Direction of the DC prediction used */
7cc84d24 2558 int run_diff, i;
be3492ec
KS
2559 int16_t *dc_val;
2560 int16_t *ac_val, *ac_val2;
2561 int dcdiff;
f26c2ef5 2562 int a_avail = v->a_avail, c_avail = v->c_avail;
4a5343fa
KS
2563 int use_pred = s->ac_pred;
2564 int scale;
50af2fd3 2565 int q1, q2 = 0;
3c275f6d 2566 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
be3492ec 2567
be3492ec
KS
2568 /* Get DC differential */
2569 if (n < 4) {
2570 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2571 } else {
2572 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2573 }
2574 if (dcdiff < 0){
2575 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2576 return -1;
2577 }
2578 if (dcdiff)
2579 {
2580 if (dcdiff == 119 /* ESC index value */)
2581 {
2582 /* TODO: Optimize */
2583 if (mquant == 1) dcdiff = get_bits(gb, 10);
2584 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2585 else dcdiff = get_bits(gb, 8);
2586 }
2587 else
2588 {
2589 if (mquant == 1)
2590 dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
2591 else if (mquant == 2)
2592 dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
2593 }
2594 if (get_bits(gb, 1))
2595 dcdiff = -dcdiff;
2596 }
2597
2598 /* Prediction */
3c275f6d 2599 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
be3492ec
KS
2600 *dc_val = dcdiff;
2601
2602 /* Store the quantized DC coeff, used for prediction */
be3492ec
KS
2603 if (n < 4) {
2604 block[0] = dcdiff * s->y_dc_scale;
2605 } else {
2606 block[0] = dcdiff * s->c_dc_scale;
2607 }
2608 /* Skip ? */
2609 run_diff = 0;
2610 i = 0;
be3492ec
KS
2611
2612 //AC Decoding
2613 i = 1;
8da75fb2 2614
4a5343fa
KS
2615 /* check if AC is needed at all and adjust direction if needed */
2616 if(!a_avail) dc_pred_dir = 1;
2617 if(!c_avail) dc_pred_dir = 0;
2618 if(!a_avail && !c_avail) use_pred = 0;
2619 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2620 ac_val2 = ac_val;
2621
0a45801f 2622 scale = mquant * 2 + v->halfpq;
4a5343fa
KS
2623
2624 if(dc_pred_dir) //left
2625 ac_val -= 16;
2626 else //top
2627 ac_val -= 16 * s->block_wrap[n];
2628
50af2fd3
KS
2629 q1 = s->current_picture.qscale_table[mb_pos];
2630 if(dc_pred_dir && c_avail) q2 = s->current_picture.qscale_table[mb_pos - 1];
2631 if(!dc_pred_dir && a_avail) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2632 if(n && n<4) q2 = q1;
2633
4a5343fa 2634 if(coded) {
be3492ec
KS
2635 int last = 0, skip, value;
2636 const int8_t *zz_table;
be3492ec 2637 int k;
be3492ec 2638
3c275f6d
KS
2639 if(v->s.ac_pred) {
2640 if(!dc_pred_dir)
2641 zz_table = vc1_horizontal_zz;
2642 else
2643 zz_table = vc1_vertical_zz;
2644 } else
2645 zz_table = vc1_normal_zz;
be3492ec 2646
be3492ec
KS
2647 while (!last) {
2648 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2649 i += skip;
2650 if(i > 63)
2651 break;
2652 block[zz_table[i++]] = value;
e5540b3f 2653 }
7cc84d24 2654
be3492ec 2655 /* apply AC prediction if needed */
8f8d0e48 2656 if(use_pred) {
be3492ec 2657 /* scale predictors if needed*/
50af2fd3 2658 if(q2 && q1!=q2) {
be3492ec
KS
2659 q1 = q1 * 2 - 1;
2660 q2 = q2 * 2 - 1;
2661
2662 if(dc_pred_dir) { //left
2663 for(k = 1; k < 8; k++)
2664 block[k << 3] += (ac_val[k] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2665 } else { //top
2666 for(k = 1; k < 8; k++)
2667 block[k] += (ac_val[k + 8] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2668 }
2669 } else {
2670 if(dc_pred_dir) { //left
2671 for(k = 1; k < 8; k++)
2672 block[k << 3] += ac_val[k];
2673 } else { //top
2674 for(k = 1; k < 8; k++)
2675 block[k] += ac_val[k + 8];
2676 }
2677 }
7cc84d24 2678 }
be3492ec
KS
2679 /* save AC coeffs for further prediction */
2680 for(k = 1; k < 8; k++) {
2681 ac_val2[k] = block[k << 3];
2682 ac_val2[k + 8] = block[k];
e5540b3f 2683 }
7cc84d24 2684
be3492ec
KS
2685 /* scale AC coeffs */
2686 for(k = 1; k < 64; k++)
2687 if(block[k]) {
2688 block[k] *= scale;
2689 if(!v->pquantizer)
2690 block[k] += (block[k] < 0) ? -mquant : mquant;
2691 }
7cc84d24 2692
8f8d0e48 2693 if(use_pred) i = 63;
4a5343fa
KS
2694 } else { // no AC coeffs
2695 int k;
8f8d0e48 2696
be3492ec
KS
2697 memset(ac_val2, 0, 16 * 2);
2698 if(dc_pred_dir) {//left
e4bf0302 2699 if(use_pred) {
be3492ec 2700 memcpy(ac_val2, ac_val, 8 * 2);
50af2fd3 2701 if(q2 && q1!=q2) {
e4bf0302
KS
2702 q1 = q1 * 2 - 1;
2703 q2 = q2 * 2 - 1;
2704 for(k = 1; k < 8; k++)
2705 ac_val2[k] = (ac_val2[k] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2706 }
2707 }
be3492ec 2708 } else {//top
e4bf0302 2709 if(use_pred) {
be3492ec 2710 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
50af2fd3 2711 if(q2 && q1!=q2) {
e4bf0302
KS
2712 q1 = q1 * 2 - 1;
2713 q2 = q2 * 2 - 1;
2714 for(k = 1; k < 8; k++)
2715 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2716 }
2717 }
be3492ec 2718 }
7cc84d24 2719
be3492ec 2720 /* apply AC prediction if needed */
8f8d0e48 2721 if(use_pred) {
be3492ec
KS
2722 if(dc_pred_dir) { //left
2723 for(k = 1; k < 8; k++) {
e4bf0302 2724 block[k << 3] = ac_val2[k] * scale;
4b944659 2725 if(!v->pquantizer && block[k << 3])
be3492ec
KS
2726 block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
2727 }
2728 } else { //top
2729 for(k = 1; k < 8; k++) {
e4bf0302 2730 block[k] = ac_val2[k + 8] * scale;
4b944659 2731 if(!v->pquantizer && block[k])
be3492ec
KS
2732 block[k] += (block[k] < 0) ? -mquant : mquant;
2733 }
2734 }
2735 i = 63;
7cc84d24 2736 }
2737 }
7cc84d24 2738 s->block_last_index[n] = i;
be3492ec 2739
e5540b3f 2740 return 0;
2741}
7cc84d24 2742
3c275f6d
KS
2743/** Decode intra block in inter frames - more generic version than vc1_decode_i_block
2744 * @param v VC1Context
2745 * @param block block to decode
2746 * @param coded are AC coeffs present or not
2747 * @param mquant block quantizer
2748 * @param codingset set of VLC to decode data
2ce151f8 2749 */
3c275f6d 2750static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n, int coded, int mquant, int codingset)
7cc84d24 2751{
3c275f6d 2752 GetBitContext *gb = &v->s.gb;
be3492ec 2753 MpegEncContext *s = &v->s;
3c275f6d
KS
2754 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2755 int run_diff, i;
2756 int16_t *dc_val;
2757 int16_t *ac_val, *ac_val2;
2758 int dcdiff;
2759 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2760 int a_avail = v->a_avail, c_avail = v->c_avail;
2761 int use_pred = s->ac_pred;
2762 int scale;
2763 int q1, q2 = 0;
42cc17f9 2764
3c275f6d
KS
2765 /* XXX: Guard against dumb values of mquant */
2766 mquant = (mquant < 1) ? 0 : ( (mquant>31) ? 31 : mquant );
42cc17f9 2767
3c275f6d
KS
2768 /* Set DC scale - y and c use the same */
2769 s->y_dc_scale = s->y_dc_scale_table[mquant];
2770 s->c_dc_scale = s->c_dc_scale_table[mquant];
2771
2772 /* Get DC differential */
2773 if (n < 4) {
2774 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2775 } else {
2776 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2777 }
2778 if (dcdiff < 0){
2779 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2780 return -1;
2781 }
2782 if (dcdiff)
2783 {
2784 if (dcdiff == 119 /* ESC index value */)
2785 {
2786 /* TODO: Optimize */
2787 if (mquant == 1) dcdiff = get_bits(gb, 10);
2788 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2789 else dcdiff = get_bits(gb, 8);
2790 }
2791 else
2792 {
2793 if (mquant == 1)
2794 dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
2795 else if (mquant == 2)
2796 dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
2797 }
2798 if (get_bits(gb, 1))
2799 dcdiff = -dcdiff;
2800 }
2801
2802 /* Prediction */
2803 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
2804 *dc_val = dcdiff;
2805
2806 /* Store the quantized DC coeff, used for prediction */
2807
2808 if (n < 4) {
2809 block[0] = dcdiff * s->y_dc_scale;
2810 } else {
2811 block[0] = dcdiff * s->c_dc_scale;
2812 }
2813 /* Skip ? */
2814 run_diff = 0;
2815 i = 0;
2816
2817 //AC Decoding
2818 i = 1;
2819
2820 /* check if AC is needed at all and adjust direction if needed */
2821 if(!a_avail) dc_pred_dir = 1;
2822 if(!c_avail) dc_pred_dir = 0;
2823 if(!a_avail && !c_avail) use_pred = 0;
2824 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2825 ac_val2 = ac_val;
2826
2827 scale = mquant * 2 + v->halfpq;
2828
2829 if(dc_pred_dir) //left
2830 ac_val -= 16;
2831 else //top
2832 ac_val -= 16 * s->block_wrap[n];
2833
2834 q1 = s->current_picture.qscale_table[mb_pos];
2835 if(dc_pred_dir && c_avail) q2 = s->current_picture.qscale_table[mb_pos - 1];
2836 if(!dc_pred_dir && a_avail) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2837 if(n && n<4) q2 = q1;
2838
2839 if(coded) {
2840 int last = 0, skip, value;
2841 const int8_t *zz_table;
2842 int k;
2843
2844 zz_table = vc1_simple_progressive_8x8_zz;
2845
2846 while (!last) {
2847 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2848 i += skip;
2849 if(i > 63)
2850 break;
2851 block[zz_table[i++]] = value;
2852 }
2853
2854 /* apply AC prediction if needed */
2855 if(use_pred) {
2856 /* scale predictors if needed*/
2857 if(q2 && q1!=q2) {
2858 q1 = q1 * 2 - 1;
2859 q2 = q2 * 2 - 1;
2860
2861 if(dc_pred_dir) { //left
2862 for(k = 1; k < 8; k++)
2863 block[k << 3] += (ac_val[k] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2864 } else { //top
2865 for(k = 1; k < 8; k++)
2866 block[k] += (ac_val[k + 8] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2867 }
2868 } else {
2869 if(dc_pred_dir) { //left
2870 for(k = 1; k < 8; k++)
2871 block[k << 3] += ac_val[k];
2872 } else { //top
2873 for(k = 1; k < 8; k++)
2874 block[k] += ac_val[k + 8];
2875 }
2876 }
2877 }
2878 /* save AC coeffs for further prediction */
2879 for(k = 1; k < 8; k++) {
2880 ac_val2[k] = block[k << 3];
2881 ac_val2[k + 8] = block[k];
2882 }
2883
2884 /* scale AC coeffs */
2885 for(k = 1; k < 64; k++)
2886 if(block[k]) {
2887 block[k] *= scale;
2888 if(!v->pquantizer)
2889 block[k] += (block[k] < 0) ? -mquant : mquant;
2890 }
2891
2892 if(use_pred) i = 63;
2893 } else { // no AC coeffs
2894 int k;
2895
2896 memset(ac_val2, 0, 16 * 2);
2897 if(dc_pred_dir) {//left
2898 if(use_pred) {
2899 memcpy(ac_val2, ac_val, 8 * 2);
2900 if(q2 && q1!=q2) {
2901 q1 = q1 * 2 - 1;
2902 q2 = q2 * 2 - 1;
2903 for(k = 1; k < 8; k++)
2904 ac_val2[k] = (ac_val2[k] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2905 }
2906 }
2907 } else {//top
2908 if(use_pred) {
2909 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2910 if(q2 && q1!=q2) {
2911 q1 = q1 * 2 - 1;
2912 q2 = q2 * 2 - 1;
2913 for(k = 1; k < 8; k++)
2914 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2915 }
2916 }
2917 }
2918
2919 /* apply AC prediction if needed */
2920 if(use_pred) {
2921 if(dc_pred_dir) { //left
2922 for(k = 1; k < 8; k++) {
2923 block[k << 3] = ac_val2[k] * scale;
2924 if(!v->pquantizer && block[k << 3])
2925 block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
2926 }
2927 } else { //top
2928 for(k = 1; k < 8; k++) {
2929 block[k] = ac_val2[k + 8] * scale;
2930 if(!v->pquantizer && block[k])
2931 block[k] += (block[k] < 0) ? -mquant : mquant;
2932 }
2933 }
2934 i = 63;
2935 }
2936 }
2937 s->block_last_index[n] = i;
2938
2939 return 0;
2940}
2941
2942/** Decode P block
2943 */
2944static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n, int mquant, int ttmb, int first_block)
2945{
2946 MpegEncContext *s = &v->s;
2947 GetBitContext *gb = &s->gb;
2948 int i, j;
2949 int subblkpat = 0;
2950 int scale, off, idx, last, skip, value;
2951 int ttblk = ttmb & 7;
2952
2953 if(ttmb == -1) {
2954 ttblk = ttblk_to_tt[v->tt_index][get_vlc2(gb, vc1_ttblk_vlc[v->tt_index].table, VC1_TTBLK_VLC_BITS, 1)];
2955 }
2956 if(ttblk == TT_4X4) {
2957 subblkpat = ~(get_vlc2(gb, vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
2958 }
2959 if((ttblk != TT_8X8 && ttblk != TT_4X4) && (v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))) {
2960 subblkpat = decode012(gb);
2961 if(subblkpat) subblkpat ^= 3; //swap decoded pattern bits
2962 if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) ttblk = TT_8X4;
2963 if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) ttblk = TT_4X8;
2964 }
2965 scale = 2 * mquant + v->halfpq;
2966
2967 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
be3492ec 2968 if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
be3492ec 2969 subblkpat = 2 - (ttblk == TT_8X4_TOP);
e9f2396e 2970 ttblk = TT_8X4;
be3492ec
KS
2971 }
2972 if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
be3492ec 2973 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
e9f2396e 2974 ttblk = TT_4X8;
be3492ec 2975 }
be3492ec
KS
2976 switch(ttblk) {
2977 case TT_8X8:
2978 i = 0;
2979 last = 0;
2980 while (!last) {
2981 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2982 i += skip;
2983 if(i > 63)
2984 break;
2985 idx = vc1_simple_progressive_8x8_zz[i++];
2986 block[idx] = value * scale;
0a45801f
KS
2987 if(!v->pquantizer)
2988 block[idx] += (block[idx] < 0) ? -mquant : mquant;
7cc84d24 2989 }
5ce425f7 2990 s->dsp.vc1_inv_trans_8x8(block);
be3492ec
KS
2991 break;
2992 case TT_4X4:
2993 for(j = 0; j < 4; j++) {
2994 last = subblkpat & (1 << (3 - j));
2995 i = 0;
87dfe848 2996 off = (j & 1) * 4 + (j & 2) * 16;
be3492ec
KS
2997 while (!last) {
2998 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2999 i += skip;
3000 if(i > 15)
3001 break;
3002 idx = vc1_simple_progressive_4x4_zz[i++];
3003 block[idx + off] = value * scale;
0a45801f
KS
3004 if(!v->pquantizer)
3005 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
be3492ec 3006 }
87dfe848 3007 if(!(subblkpat & (1 << (3 - j))))
5ce425f7 3008 s->dsp.vc1_inv_trans_4x4(block, j);
be3492ec
KS
3009 }
3010 break;
3011 case TT_8X4:
3012 for(j = 0; j < 2; j++) {
3013 last = subblkpat & (1 << (1 - j));
3014 i = 0;
3015 off = j * 32;
3016 while (!last) {
3017 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3018 i += skip;
3019 if(i > 31)
3020 break;
3c275f6d
KS
3021 if(v->profile < PROFILE_ADVANCED)
3022 idx = vc1_simple_progressive_8x4_zz[i++];
3023 else
3024 idx = vc1_adv_progressive_8x4_zz[i++];
be3492ec 3025 block[idx + off] = value * scale;
0a45801f
KS
3026 if(!v->pquantizer)
3027 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
be3492ec 3028 }
87dfe848 3029 if(!(subblkpat & (1 << (1 - j))))
5ce425f7 3030 s->dsp.vc1_inv_trans_8x4(block, j);
be3492ec
KS
3031 }
3032 break;
3033 case TT_4X8:
3034 for(j = 0; j < 2; j++) {
3035 last = subblkpat & (1 << (1 - j));
3036 i = 0;
3037 off = j * 4;
3038 while (!last) {
3039 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3040 i += skip;
3041 if(i > 31)
3042 break;
3c275f6d
KS
3043 if(v->profile < PROFILE_ADVANCED)
3044 idx = vc1_simple_progressive_4x8_zz[i++];
3045 else
3046 idx = vc1_adv_progressive_4x8_zz[i++];
be3492ec 3047 block[idx + off] = value * scale;
0a45801f
KS
3048 if(!v->pquantizer)
3049 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
be3492ec 3050 }
87dfe848 3051 if(!(subblkpat & (1 << (1 - j))))
5ce425f7 3052 s->dsp.vc1_inv_trans_4x8(block, j);
8da75fb2 3053 }
be3492ec 3054 break;
7cc84d24 3055 }
3056 return 0;
3057}
3058
be3492ec 3059
8da75fb2 3060/** Decode one P-frame MB (in Simple/Main profile)
3061 * @todo TODO: Extend to AP
3062 * @fixme FIXME: DC value for inter blocks not set
3063 */
7e84f276 3064static int vc1_decode_p_mb(VC1Context *v)
7cc84d24 3065{
3066 MpegEncContext *s = &v->s;
3067 GetBitContext *gb = &s->gb;
87dfe848 3068 int i, j;
be3492ec 3069 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
7cc84d24 3070 int cbp; /* cbp decoding stuff */
7cc84d24 3071 int mqdiff, mquant; /* MB quantization */
8a66a390 3072 int ttmb = v->ttfrm; /* MB Transform type */
7cc84d24 3073 int status;
7cc84d24 3074
3075 static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
3076 offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3077 int mb_has_coeffs = 1; /* last_flag */
3078 int dmv_x, dmv_y; /* Differential MV components */
3079 int index, index1; /* LUT indices */
3080 int val, sign; /* temp values */
be3492ec
KS
3081 int first_block = 1;
3082 int dst_idx, off;
87dfe848 3083 int skipped, fourmv;
7cc84d24 3084
8da75fb2 3085 mquant = v->pq; /* Loosy initialization */
3086
87dfe848
KS
3087 if (v->mv_type_is_raw)
3088 fourmv = get_bits1(gb);
3089 else
3090 fourmv = v->mv_type_mb_plane[mb_pos];
3091 if (v->skip_is_raw)
3092 skipped = get_bits1(gb);
3093 else
0f7344aa 3094 skipped = v->s.mbskip_table[mb_pos];
87dfe848 3095
e4bf0302
KS
3096 s->dsp.clear_blocks(s->block[0]);
3097
87dfe848 3098 if (!fourmv) /* 1MV mode */
7cc84d24 3099 {
87dfe848 3100 if (!skipped)
7cc84d24 3101 {
3102 GET_MVDATA(dmv_x, dmv_y);
42cc17f9 3103
be3492ec 3104 s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
e4bf0302 3105 vc1_pred_mv(s, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
be3492ec 3106
8da75fb2 3107 /* FIXME Set DC val for inter block ? */
7cc84d24 3108 if (s->mb_intra && !mb_has_coeffs)
3109 {
3110 GET_MQUANT();
3111 s->ac_pred = get_bits(gb, 1);
8da75fb2 3112 cbp = 0;
7cc84d24 3113 }
3114 else if (mb_has_coeffs)
3115 {
3116 if (s->mb_intra) s->ac_pred = get_bits(gb, 1);
10b9c374 3117 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
7cc84d24 3118 GET_MQUANT();
3119 }
3120 else
3121 {
3122 mquant = v->pq;
8da75fb2 3123 cbp = 0;
7cc84d24 3124 }
be3492ec 3125 s->current_picture.qscale_table[mb_pos] = mquant;
7cc84d24 3126
be3492ec 3127 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
10b9c374 3128 ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table,
be3492ec 3129 VC1_TTMB_VLC_BITS, 2);
5df68893 3130 if(!s->mb_intra) vc1_mc_1mv(v, 0);
be3492ec 3131 dst_idx = 0;
7cc84d24 3132 for (i=0; i<6; i++)
3133 {
be3492ec
KS
3134 s->dc_val[0][s->block_index[i]] = 0;
3135 dst_idx += i >> 2;
7cc84d24 3136 val = ((cbp >> (5 - i)) & 1);
be3492ec 3137 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
e4bf0302 3138 v->mb_type[0][s->block_index[i]] = s->mb_intra;
be3492ec 3139 if(s->mb_intra) {
f26c2ef5
KS
3140 /* check if prediction blocks A and C are available */
3141 v->a_avail = v->c_avail = 0;
d2779ecd 3142 if(i == 2 || i == 3 || !s->first_slice_line)
e4bf0302
KS
3143 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3144 if(i == 1 || i == 3 || s->mb_x)
3145 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
f26c2ef5 3146
7e84f276 3147 vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
138712fe 3148 if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
5ce425f7 3149 s->dsp.vc1_inv_trans_8x8(s->block[i]);
ffb9a8b1 3150 if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
7e84f276
KS
3151 for(j = 0; j < 64; j++) s->block[i][j] += 128;
3152 s->dsp.put_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
f26c2ef5 3153 /* TODO: proper loop filtering */
87dfe848
KS
3154 if(v->pq >= 9 && v->overlap) {
3155 if(v->a_avail)
5ce425f7 3156 s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2), (i<4) ? ((i&1)>>1) : (s->mb_y&1));
87dfe848 3157 if(v->c_avail)
5ce425f7 3158 s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2), (i<4) ? (i&1) : (s->mb_x&1));
87dfe848 3159 }
be3492ec 3160 } else if(val) {
7e84f276 3161 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block);
be3492ec
KS
3162 if(!v->ttmbf && ttmb < 8) ttmb = -1;
3163 first_block = 0;
138712fe
KS
3164 if((i<4) || !(s->flags & CODEC_FLAG_GRAY))
3165 s->dsp.add_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
7cc84d24 3166 }
7cc84d24 3167 }
3168 }
3169 else //Skipped
3170 {
be3492ec 3171 s->mb_intra = 0;
92ad0d9d
KS
3172 for(i = 0; i < 6; i++) {
3173 v->mb_type[0][s->block_index[i]] = 0;
3174 s->dc_val[0][s->block_index[i]] = 0;
3175 }
be3492ec 3176 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
e4bf0302
KS
3177 s->current_picture.qscale_table[mb_pos] = 0;
3178 vc1_pred_mv(s, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);
5df68893 3179 vc1_mc_1mv(v, 0);
7cc84d24 3180 return 0;
3181 }
3182 } //1MV mode
3183 else //4MV mode
e4bf0302 3184 {
87dfe848 3185 if (!skipped /* unskipped MB */)
7cc84d24 3186 {
e4bf0302
KS
3187 int intra_count = 0, coded_inter = 0;
3188 int is_intra[6], is_coded[6];
7cc84d24 3189 /* Get CBPCY */
10b9c374 3190 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
e4bf0302 3191 for (i=0; i<6; i++)
7cc84d24 3192 {
3193 val = ((cbp >> (5 - i)) & 1);
e4bf0302
KS
3194 s->dc_val[0][s->block_index[i]] = 0;
3195 s->mb_intra = 0;
3196 if(i < 4) {
3197 dmv_x = dmv_y = 0;
3198 s->mb_intra = 0;
3199 mb_has_coeffs = 0;
3200 if(val) {
3201 GET_MVDATA(dmv_x, dmv_y);
3202 }
3203 vc1_pred_mv(s, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);
3204 if(!s->mb_intra) vc1_mc_4mv_luma(v, i);
3205 intra_count += s->mb_intra;
3206 is_intra[i] = s->mb_intra;
3207 is_coded[i] = mb_has_coeffs;
3208 }
3209 if(i&4){
3210 is_intra[i] = (intra_count >= 3);
3211 is_coded[i] = val;
7cc84d24 3212 }
e4bf0302
KS
3213 if(i == 4) vc1_mc_4mv_chroma(v);
3214 v->mb_type[0][s->block_index[i]] = is_intra[i];
3215 if(!coded_inter) coded_inter = !is_intra[i] & is_coded[i];
be3492ec 3216 }
fdb59832 3217 // if there are no coded blocks then don't do anything more
c39e3c6f 3218 if(!intra_count && !coded_inter) return 0;
e4bf0302
KS
3219 dst_idx = 0;
3220 GET_MQUANT();
3221 s->current_picture.qscale_table[mb_pos] = mquant;
3222 /* test if block is intra and has pred */
3223 {
3224 int intrapred = 0;
3225 for(i=0; i<6; i++)
3226 if(is_intra[i]) {
d2779ecd
KS
3227 if(((!s->first_slice_line || (i==2 || i==3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
3228 || ((s->mb_x || (i==1 || i==3)) && v->mb_type[0][s->block_index[i] - 1])) {
e4bf0302
KS
3229 intrapred = 1;
3230 break;
3231 }
3232 }
3233 if(intrapred)s->ac_pred = get_bits(gb, 1);
3234 else s->ac_pred = 0;
3235 }
3236 if (!v->ttmbf && coded_inter)
c39e3c6f 3237 ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
e4bf0302
KS
3238 for (i=0; i<6; i++)
3239 {
3240 dst_idx += i >> 2;
3241 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3242 s->mb_intra = is_intra[i];
3243 if (is_intra[i]) {
3244 /* check if prediction blocks A and C are available */
3245 v->a_avail = v->c_avail = 0;
d2779ecd 3246 if(i == 2 || i == 3 || !s->first_slice_line)
e4bf0302
KS
3247 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3248 if(i == 1 || i == 3 || s->mb_x)
3249 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
8da75fb2 3250
e4bf0302 3251 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant, (i&4)?v->codingset2:v->codingset);
138712fe 3252 if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
5ce425f7 3253 s->dsp.vc1_inv_trans_8x8(s->block[i]);
ffb9a8b1 3254 if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
7e84f276 3255 for(j = 0; j < 64; j++) s->block[i][j] += 128;
ef6cc8ce 3256 s->dsp.put_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
e4bf0302
KS
3257 /* TODO: proper loop filtering */
3258 if(v->pq >= 9 && v->overlap) {
3259 if(v->a_avail)
5ce425f7 3260 s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2), (i<4) ? ((i&1)>>1) : (s->mb_y&1));
e4bf0302 3261 if(v->c_avail)
5ce425f7 3262 s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2), (i<4) ? (i&1) : (s->mb_x&1));
be3492ec 3263 }
e4bf0302
KS
3264 } else if(is_coded[i]) {
3265 status = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block);
3266 if(!v->ttmbf && ttmb < 8) ttmb = -1;
3267 first_block = 0;
138712fe
KS
3268 if((i<4) || !(s->flags & CODEC_FLAG_GRAY))
3269 s->dsp.add_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
be3492ec 3270 }
7cc84d24 3271 }
3272 return status;
3273 }
3274 else //Skipped MB
3275 {
ef6cc8ce 3276 s->mb_intra = 0;
c39e3c6f 3277 s->current_picture.qscale_table[mb_pos] = 0;
92ad0d9d
KS
3278 for (i=0; i<6; i++) {
3279 v->mb_type[0][s->block_index[i]] = 0;
3280 s->dc_val[0][s->block_index[i]] = 0;
3281 }
7cc84d24 3282 for (i=0; i<4; i++)
3283 {
e4bf0302
KS
3284 vc1_pred_mv(s, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0]);
3285 vc1_mc_4mv_luma(v, i);
7cc84d24 3286 }
e4bf0302 3287 vc1_mc_4mv_chroma(v);
e4bf0302 3288 s->current_picture.qscale_table[mb_pos] = 0;
7cc84d24 3289 return 0;
3290 }
3291 }
42cc17f9 3292
7cc84d24 3293 /* Should never happen */
3294 return -1;
3295}
3296
5df68893
KS
3297/** Decode one B-frame MB (in Main profile)
3298 */
3299static void vc1_decode_b_mb(VC1Context *v)
3300{
3301 MpegEncContext *s = &v->s;
3302 GetBitContext *gb = &s->gb;
3303 int i, j;
3304 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
fb2d9140 3305 int cbp = 0; /* cbp decoding stuff */
5df68893
KS
3306 int mqdiff, mquant; /* MB quantization */
3307 int ttmb = v->ttfrm; /* MB Transform type */
3308
3309 static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
3310 offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3311 int mb_has_coeffs = 0; /* last_flag */
3312 int index, index1; /* LUT indices */
3313 int val, sign; /* temp values */
3314 int first_block = 1;
3315 int dst_idx, off;
3316 int skipped, direct;
3317 int dmv_x[2], dmv_y[2];
3318 int bmvtype = BMV_TYPE_BACKWARD; /* XXX: is it so? */
3319
3320 mquant = v->pq; /* Loosy initialization */
3321 s->mb_intra = 0;
3322
3323 if (v->dmb_is_raw)
3324 direct = get_bits1(gb);
3325 else
3326 direct = v->direct_mb_plane[mb_pos];
3327 if (v->skip_is_raw)
3328 skipped = get_bits1(gb);
3329 else
3330 skipped = v->s.mbskip_table[mb_pos];
3331
3332 s->dsp.clear_blocks(s->block[0]);
3333 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
3334 for(i = 0; i < 6; i++) {
3335 v->mb_type[0][s->block_index[i]] = 0;
3336 s->dc_val[0][s->block_index[i]] = 0;
3337 }
3338 s->current_picture.qscale_table[mb_pos] = 0;
3339
3340 if (!direct) {
3341 if (!skipped) {
3342 GET_MVDATA(dmv_x[0], dmv_y[0]);
fb2d9140
KS
3343 dmv_x[1] = dmv_x[0];
3344 dmv_y[1] = dmv_y[0];
5df68893
KS
3345 }
3346 if(skipped || !s->mb_intra) {
3347 bmvtype = decode012(gb);
3348 switch(bmvtype) {
3349 case 0:
3350 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
3351 break;
3352 case 1:
3353 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
3354 break;
3355 case 2:
3356 bmvtype = BMV_TYPE_INTERPOLATED;
3357 }
3358 }
3359 }
5df68893
KS
3360 if (skipped) {
3361 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3362 return;
3363 }
3364 if (direct) {
3365 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3366 GET_MQUANT();
fb2d9140
KS
3367 s->mb_intra = 0;
3368 mb_has_coeffs = 0;
5df68893 3369 s->current_picture.qscale_table[mb_pos] = mquant;
fb2d9140 3370 if(!v->ttmbf)
5df68893
KS
3371 ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3372 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3373 } else {
3374 if(!mb_has_coeffs && !s->mb_intra) {
3375 /* no coded blocks - effectively skipped */
3376 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3377 return;
3378 }
3379 if(s->mb_intra && !mb_has_coeffs) {
3380 GET_MQUANT();
3381 s->current_picture.qscale_table[mb_pos] = mquant;
3382 s->ac_pred = get_bits1(gb);
3383 cbp = 0;
3384 } else {
3385 if(bmvtype == BMV_TYPE_INTERPOLATED) {
3386 GET_MVDATA(dmv_x[1], dmv_y[1]);
3387 if(!mb_has_coeffs) {
3388 /* interpolated skipped block */
3389 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3390 return;
3391 }
3392 }
3393 if(!s->mb_intra)
3394 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3395 if(s->mb_intra)
3396 s->ac_pred = get_bits1(gb);
3397 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3398 GET_MQUANT();
3399 s->current_picture.qscale_table[mb_pos] = mquant;
3400 if(!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3401 ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3402 }
3403 }
3404 dst_idx = 0;
3405 for (i=0; i<6; i++)
3406 {
3407 s->dc_val[0][s->block_index[i]] = 0;
3408 dst_idx += i >> 2;
3409 val = ((cbp >> (5 - i)) & 1);
3410 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3411 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3412 if(s->mb_intra) {
3413 /* check if prediction blocks A and C are available */
3414 v->a_avail = v->c_avail = 0;
3415 if(i == 2 || i == 3 || !s->first_slice_line)
3416 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3417 if(i == 1 || i == 3 || s->mb_x)
3418 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3419
3420 vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
138712fe 3421 if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
5ce425f7 3422 s->dsp.vc1_inv_trans_8x8(s->block[i]);
ffb9a8b1 3423 if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
5df68893
KS
3424 for(j = 0; j < 64; j++) s->block[i][j] += 128;
3425 s->dsp.put_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
5df68893
KS
3426 } else if(val) {
3427 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block);
3428 if(!v->ttmbf && ttmb < 8) ttmb = -1;
3429 first_block = 0;
138712fe
KS
3430 if((i<4) || !(s->flags & CODEC_FLAG_GRAY))
3431 s->dsp.add_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
5df68893
KS
3432 }
3433 }
3434}
3435
be3492ec 3436/** Decode blocks of I-frame
8da75fb2 3437 */
be3492ec 3438static void vc1_decode_i_blocks(VC1Context *v)
7cc84d24 3439{
65c7bb9e 3440 int k, j;
8da75fb2 3441 MpegEncContext *s = &v->s;
be3492ec
KS
3442 int cbp, val;
3443 uint8_t *coded_val;
3444 int mb_pos;
42cc17f9 3445
be3492ec
KS
3446 /* select codingmode used for VLC tables selection */
3447 switch(v->y_ac_table_index){
3448 case 0:
3449 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
3450 break;
3451 case 1:
3452 v->codingset = CS_HIGH_MOT_INTRA;
3453 break;
3454 case 2:
3455 v->codingset = CS_MID_RATE_INTRA;
3456 break;
3457 }
42cc17f9 3458
be3492ec
KS
3459 switch(v->c_ac_table_index){
3460 case 0:
3461 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
3462 break;
3463 case 1:
3464 v->codingset2 = CS_HIGH_MOT_INTER;
3465 break;
3466 case 2:
3467 v->codingset2 = CS_MID_RATE_INTER;
3468 break;
3469 }
8da75fb2 3470
be3492ec
KS
3471 /* Set DC scale - y and c use the same */
3472 s->y_dc_scale = s->y_dc_scale_table[v->pq];
3473 s->c_dc_scale = s->c_dc_scale_table[v->pq];
3474
3475 //do frame decode
3476 s->mb_x = s->mb_y = 0;
3477 s->mb_intra = 1;
d2779ecd 3478 s->first_slice_line = 1;
be3492ec
KS
3479 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3480 for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3481 for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
3482 ff_init_block_index(s);
3483 ff_update_block_index(s);
3484 s->dsp.clear_blocks(s->block[0]);
3485 mb_pos = s->mb_x + s->mb_y * s->mb_width;
3486 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
3487 s->current_picture.qscale_table[mb_pos] = v->pq;
3488
3489 // do actual MB decoding and displaying
3490 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
3491 v->s.ac_pred = get_bits(&v->s.gb, 1);
3492
3493 for(k = 0; k < 6; k++) {
3494 val = ((cbp >> (5 - k)) & 1);
f26c2ef5 3495
be3492ec
KS
3496 if (k < 4) {
3497 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
3498 val = val ^ pred;
3499 *coded_val = val;
3500 }
3501 cbp |= val << (5 - k);
42cc17f9 3502
be3492ec
KS
3503 vc1_decode_i_block(v, s->block[k], k, val, (k<4)? v->codingset : v->codingset2);
3504
5ce425f7 3505 s->dsp.vc1_inv_trans_8x8(s->block[k]);
be3492ec 3506 if(v->pq >= 9 && v->overlap) {
65c7bb9e 3507 for(j = 0; j < 64; j++) s->block[k][j] += 128;
be3492ec 3508 }
8da75fb2 3509 }
8da75fb2 3510
be3492ec 3511 vc1_put_block(v, s->block);
f26c2ef5 3512 if(v->pq >= 9 && v->overlap) { /* XXX: do proper overlapping insted of loop filter */
65c7bb9e 3513 if(!s->first_slice_line) {
5ce425f7
KS
3514 s->dsp.vc1_v_overlap(s->dest[0], s->linesize, 0);
3515 s->dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize, 0);
138712fe 3516 if(!(s->flags & CODEC_FLAG_GRAY)) {
5ce425f7
KS
3517 s->dsp.vc1_v_overlap(s->dest[1], s->uvlinesize, s->mb_y&1);
3518 s->dsp.vc1_v_overlap(s->dest[2], s->uvlinesize, s->mb_y&1);
138712fe 3519 }
f26c2ef5 3520 }
5ce425f7
KS
3521 s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize, 1);
3522 s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize, 1);
f26c2ef5 3523 if(s->mb_x) {
5ce425f7
KS
3524 s->dsp.vc1_h_overlap(s->dest[0], s->linesize, 0);
3525 s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize, 0);
138712fe 3526 if(!(s->flags & CODEC_FLAG_GRAY)) {
5ce425f7
KS
3527 s->dsp.vc1_h_overlap(s->dest[1], s->uvlinesize, s->mb_x&1);
3528 s->dsp.vc1_h_overlap(s->dest[2], s->uvlinesize, s->mb_x&1);
138712fe 3529 }
f26c2ef5 3530 }
5ce425f7
KS
3531 s->dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize, 1);
3532 s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize, 1);
f26c2ef5 3533 }
8da75fb2 3534
be3492ec
KS
3535 if(get_bits_count(&s->gb) > v->bits) {
3536 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
3537 return;
3538 }
3539 }
3540 ff_draw_horiz_band(s, s->mb_y * 16, 16);
d2779ecd 3541 s->first_slice_line = 0;
7cc84d24 3542 }
7cc84d24 3543}
3544
3c275f6d
KS
3545/** Decode blocks of I-frame for advanced profile
3546 */
3547static void vc1_decode_i_blocks_adv(VC1Context *v)
3548{
3549 int k, j;
3550 MpegEncContext *s = &v->s;
3551 int cbp, val;
3552 uint8_t *coded_val;
3553 int mb_pos;
3554 int mquant = v->pq;