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