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