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