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