]> git.sesse.net Git - ffmpeg/blob - libavcodec/vc1.c
Added loop filtering as ersatz for overlap filter (improves picture quality for coars...
[ffmpeg] / 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 library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  *
20  */
21
22 /**
23  * @file vc1.c
24  * VC-1 and WMV3 decoder
25  *
26  */
27 #include "common.h"
28 #include "dsputil.h"
29 #include "avcodec.h"
30 #include "mpegvideo.h"
31 #include "vc1data.h"
32 #include "vc1acdata.h"
33
34 #undef NDEBUG
35 #include <assert.h>
36
37 extern const uint32_t ff_table0_dc_lum[120][2], ff_table1_dc_lum[120][2];
38 extern const uint32_t ff_table0_dc_chroma[120][2], ff_table1_dc_chroma[120][2];
39 extern VLC ff_msmp4_dc_luma_vlc[2], ff_msmp4_dc_chroma_vlc[2];
40 #define MB_INTRA_VLC_BITS 9
41 extern VLC ff_msmp4_mb_i_vlc;
42 extern const uint16_t ff_msmp4_mb_i_table[64][2];
43 #define DC_VLC_BITS 9
44 #define AC_VLC_BITS 9
45 static const uint16_t table_mb_intra[64][2];
46
47
48 /** Available Profiles */
49 //@{
50 enum Profile {
51     PROFILE_SIMPLE,
52     PROFILE_MAIN,
53     PROFILE_COMPLEX, ///< TODO: WMV9 specific
54     PROFILE_ADVANCED
55 };
56 //@}
57
58 /** Sequence quantizer mode */
59 //@{
60 enum QuantMode {
61     QUANT_FRAME_IMPLICIT,    ///< Implicitly specified at frame level
62     QUANT_FRAME_EXPLICIT,    ///< Explicitly specified at frame level
63     QUANT_NON_UNIFORM,       ///< Non-uniform quant used for all frames
64     QUANT_UNIFORM            ///< Uniform quant used for all frames
65 };
66 //@}
67
68 /** Where quant can be changed */
69 //@{
70 enum DQProfile {
71     DQPROFILE_FOUR_EDGES,
72     DQPROFILE_DOUBLE_EDGES,
73     DQPROFILE_SINGLE_EDGE,
74     DQPROFILE_ALL_MBS
75 };
76 //@}
77
78 /** @name Where quant can be changed
79  */
80 //@{
81 enum DQSingleEdge {
82     DQSINGLE_BEDGE_LEFT,
83     DQSINGLE_BEDGE_TOP,
84     DQSINGLE_BEDGE_RIGHT,
85     DQSINGLE_BEDGE_BOTTOM
86 };
87 //@}
88
89 /** Which pair of edges is quantized with ALTPQUANT */
90 //@{
91 enum DQDoubleEdge {
92     DQDOUBLE_BEDGE_TOPLEFT,
93     DQDOUBLE_BEDGE_TOPRIGHT,
94     DQDOUBLE_BEDGE_BOTTOMRIGHT,
95     DQDOUBLE_BEDGE_BOTTOMLEFT
96 };
97 //@}
98
99 /** MV modes for P frames */
100 //@{
101 enum MVModes {
102     MV_PMODE_1MV_HPEL_BILIN,
103     MV_PMODE_1MV,
104     MV_PMODE_1MV_HPEL,
105     MV_PMODE_MIXED_MV,
106     MV_PMODE_INTENSITY_COMP
107 };
108 //@}
109
110 /** @name MV types for B frames */
111 //@{
112 enum BMVTypes {
113     BMV_TYPE_BACKWARD,
114     BMV_TYPE_FORWARD,
115     BMV_TYPE_INTERPOLATED = 3 //XXX: ??
116 };
117 //@}
118
119 /** @name Block types for P/B frames */
120 //@{
121 enum TransformTypes {
122     TT_8X8,
123     TT_8X4_BOTTOM,
124     TT_8X4_TOP,
125     TT_8X4, //Both halves
126     TT_4X8_RIGHT,
127     TT_4X8_LEFT,
128     TT_4X8, //Both halves
129     TT_4X4
130 };
131 //@}
132
133 /** Table for conversion between TTBLK and TTMB */
134 static const int ttblk_to_tt[3][8] = {
135   { TT_8X4, TT_4X8, TT_8X8, TT_4X4, TT_8X4_TOP, TT_8X4_BOTTOM, TT_4X8_RIGHT, TT_4X8_LEFT },
136   { TT_8X8, TT_4X8_RIGHT, TT_4X8_LEFT, TT_4X4, TT_8X4, TT_4X8, TT_8X4_BOTTOM, TT_8X4_TOP },
137   { TT_8X8, TT_4X8, TT_4X4, TT_8X4_BOTTOM, TT_4X8_RIGHT, TT_4X8_LEFT, TT_8X4, TT_8X4_TOP }
138 };
139
140 /** MV P mode - the 5th element is only used for mode 1 */
141 static const uint8_t mv_pmode_table[2][5] = {
142   { MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_1MV, MV_PMODE_1MV_HPEL, MV_PMODE_INTENSITY_COMP, MV_PMODE_MIXED_MV },
143   { MV_PMODE_1MV, MV_PMODE_MIXED_MV, MV_PMODE_1MV_HPEL, MV_PMODE_INTENSITY_COMP, MV_PMODE_1MV_HPEL_BILIN }
144 };
145
146 /** One more frame type */
147 #define BI_TYPE 7
148
149 static const int fps_nr[5] = { 24, 25, 30, 50, 60 },
150   fps_dr[2] = { 1000, 1001 };
151 static const uint8_t pquant_table[3][32] = {
152   {  /* Implicit quantizer */
153      0,  1,  2,  3,  4,  5,  6,  7,  8,  6,  7,  8,  9, 10, 11, 12,
154     13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 29, 31
155   },
156   {  /* Explicit quantizer, pquantizer uniform */
157      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
158     16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
159   },
160   {  /* Explicit quantizer, pquantizer non-uniform */
161      0,  1,  1,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13,
162     14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 31
163   }
164 };
165
166 /** @name VC-1 VLC tables and defines
167  *  @todo TODO move this into the context
168  */
169 //@{
170 #define VC1_BFRACTION_VLC_BITS 7
171 static VLC vc1_bfraction_vlc;
172 #define VC1_IMODE_VLC_BITS 4
173 static VLC vc1_imode_vlc;
174 #define VC1_NORM2_VLC_BITS 3
175 static VLC vc1_norm2_vlc;
176 #define VC1_NORM6_VLC_BITS 9
177 static VLC vc1_norm6_vlc;
178 /* Could be optimized, one table only needs 8 bits */
179 #define VC1_TTMB_VLC_BITS 9 //12
180 static VLC vc1_ttmb_vlc[3];
181 #define VC1_MV_DIFF_VLC_BITS 9 //15
182 static VLC vc1_mv_diff_vlc[4];
183 #define VC1_CBPCY_P_VLC_BITS 9 //14
184 static VLC vc1_cbpcy_p_vlc[4];
185 #define VC1_4MV_BLOCK_PATTERN_VLC_BITS 6
186 static VLC vc1_4mv_block_pattern_vlc[4];
187 #define VC1_TTBLK_VLC_BITS 5
188 static VLC vc1_ttblk_vlc[3];
189 #define VC1_SUBBLKPAT_VLC_BITS 6
190 static VLC vc1_subblkpat_vlc[3];
191
192 static VLC vc1_ac_coeff_table[8];
193 //@}
194
195 enum CodingSet {
196     CS_HIGH_MOT_INTRA = 0,
197     CS_HIGH_MOT_INTER,
198     CS_LOW_MOT_INTRA,
199     CS_LOW_MOT_INTER,
200     CS_MID_RATE_INTRA,
201     CS_MID_RATE_INTER,
202     CS_HIGH_RATE_INTRA,
203     CS_HIGH_RATE_INTER
204 };
205
206 /** Bitplane struct
207  * We mainly need data and is_raw, so this struct could be avoided
208  * to save a level of indirection; feel free to modify
209  * @fixme For now, stride=width
210  * @warning Data are bits, either 1 or 0
211  */
212 typedef struct BitPlane {
213     uint8_t *data;      ///< Data buffer
214     int width;          ///< Width of the buffer
215     int stride;         ///< Stride of the buffer
216     int height;         ///< Plane height
217     uint8_t is_raw;     ///< Bit values must be read at MB level
218 } BitPlane;
219
220
221 /** Block data for DC/AC prediction
222 */
223 typedef struct Block {
224     uint16_t dc;
225     int16_t hor_ac[7];
226     int16_t vert_ac[7];
227     int16_t dcstep, step;
228 } Block;
229
230 /** The VC1 Context
231  * @fixme Change size wherever another size is more efficient
232  * Many members are only used for Advanced Profile
233  */
234 typedef struct VC1Context{
235     MpegEncContext s;
236
237     int bits;
238
239     /** Simple/Main Profile sequence header */
240     //@{
241     int res_sm;           ///< reserved, 2b
242     int res_x8;           ///< reserved
243     int multires;         ///< frame-level RESPIC syntax element present
244     int res_fasttx;       ///< reserved, always 1
245     int res_transtab;     ///< reserved, always 0
246     int rangered;         ///< RANGEREDFRM (range reduction) syntax element present
247                           ///< at frame level
248     int res_rtm_flag;     ///< reserved, set to 1
249     int reserved;         ///< reserved
250     //@}
251
252     /** Advanced Profile */
253     //@{
254     int level;            ///< 3bits, for Advanced/Simple Profile, provided by TS layer
255     int chromaformat;     ///< 2bits, 2=4:2:0, only defined
256     int postprocflag;     ///< Per-frame processing suggestion flag present
257     int broadcast;        ///< TFF/RFF present
258     int interlace;        ///< Progressive/interlaced (RPTFTM syntax element)
259     int tfcntrflag;       ///< TFCNTR present
260     int panscanflag;      ///< NUMPANSCANWIN, TOPLEFT{X,Y}, BOTRIGHT{X,Y} present
261     int extended_dmv;     ///< Additional extended dmv range at P/B frame-level
262     int color_prim;       ///< 8bits, chroma coordinates of the color primaries
263     int transfer_char;    ///< 8bits, Opto-electronic transfer characteristics
264     int matrix_coef;      ///< 8bits, Color primaries->YCbCr transform matrix
265     int hrd_param_flag;   ///< Presence of Hypothetical Reference
266                           ///< Decoder parameters
267     //@}
268
269     /** Sequence header data for all Profiles
270      * TODO: choose between ints, uint8_ts and monobit flags
271      */
272     //@{
273     int profile;          ///< 2bits, Profile
274     int frmrtq_postproc;  ///< 3bits,
275     int bitrtq_postproc;  ///< 5bits, quantized framerate-based postprocessing strength
276     int fastuvmc;         ///< Rounding of qpel vector to hpel ? (not in Simple)
277     int extended_mv;      ///< Ext MV in P/B (not in Simple)
278     int dquant;           ///< How qscale varies with MBs, 2bits (not in Simple)
279     int vstransform;      ///< variable-size [48]x[48] transform type + info
280     int overlap;          ///< overlapped transforms in use
281     int quantizer_mode;   ///< 2bits, quantizer mode used for sequence, see QUANT_*
282     int finterpflag;      ///< INTERPFRM present
283     //@}
284
285     /** Frame decoding info for all profiles */
286     //@{
287     uint8_t mv_mode;      ///< MV coding monde
288     uint8_t mv_mode2;     ///< Secondary MV coding mode (B frames)
289     int k_x;              ///< Number of bits for MVs (depends on MV range)
290     int k_y;              ///< Number of bits for MVs (depends on MV range)
291     int range_x, range_y; ///< MV range
292     uint8_t pq, altpq;    ///< Current/alternate frame quantizer scale
293     /** pquant parameters */
294     //@{
295     uint8_t dquantfrm;
296     uint8_t dqprofile;
297     uint8_t dqsbedge;
298     uint8_t dqbilevel;
299     //@}
300     /** AC coding set indexes
301      * @see 8.1.1.10, p(1)10
302      */
303     //@{
304     int c_ac_table_index; ///< Chroma index from ACFRM element
305     int y_ac_table_index; ///< Luma index from AC2FRM element
306     //@}
307     int ttfrm;            ///< Transform type info present at frame level
308     uint8_t ttmbf;        ///< Transform type flag
309     int ttmb;             ///< Transform type
310     uint8_t ttblk4x4;     ///< Value of ttblk which indicates a 4x4 transform
311     int codingset;        ///< index of current table set from 11.8 to use for luma block decoding
312     int codingset2;       ///< index of current table set from 11.8 to use for chroma block decoding
313     int pqindex;          ///< raw pqindex used in coding set selection
314     int a_avail, c_avail;
315
316
317     /** Luma compensation parameters */
318     //@{
319     uint8_t lumscale;
320     uint8_t lumshift;
321     //@}
322     int16_t bfraction;    ///< Relative position % anchors=> how to scale MVs
323     uint8_t halfpq;       ///< Uniform quant over image and qp+.5
324     uint8_t respic;       ///< Frame-level flag for resized images
325     int buffer_fullness;  ///< HRD info
326     /** Ranges:
327      * -# 0 -> [-64n 63.f] x [-32, 31.f]
328      * -# 1 -> [-128, 127.f] x [-64, 63.f]
329      * -# 2 -> [-512, 511.f] x [-128, 127.f]
330      * -# 3 -> [-1024, 1023.f] x [-256, 255.f]
331      */
332     uint8_t mvrange;
333     uint8_t pquantizer;           ///< Uniform (over sequence) quantizer in use
334     uint8_t *previous_line_cbpcy; ///< To use for predicted CBPCY
335     VLC *cbpcy_vlc;               ///< CBPCY VLC table
336     int tt_index;                 ///< Index for Transform Type tables
337     BitPlane mv_type_mb_plane;    ///< bitplane for mv_type == (4MV)
338     BitPlane skip_mb_plane;       ///< bitplane for skipped MBs
339     BitPlane direct_mb_plane;     ///< bitplane for "direct" MBs
340
341     /** Frame decoding info for S/M profiles only */
342     //@{
343     uint8_t rangeredfrm; ///< out_sample = CLIP((in_sample-128)*2+128)
344     uint8_t interpfrm;
345     //@}
346
347     /** Frame decoding info for Advanced profile */
348     //@{
349     uint8_t fcm; ///< 0->Progressive, 2->Frame-Interlace, 3->Field-Interlace
350     uint8_t numpanscanwin;
351     uint8_t tfcntr;
352     uint8_t rptfrm, tff, rff;
353     uint16_t topleftx;
354     uint16_t toplefty;
355     uint16_t bottomrightx;
356     uint16_t bottomrighty;
357     uint8_t uvsamp;
358     uint8_t postproc;
359     int hrd_num_leaky_buckets;
360     uint8_t bit_rate_exponent;
361     uint8_t buffer_size_exponent;
362     BitPlane ac_pred_plane;       ///< AC prediction flags bitplane
363     BitPlane over_flags_plane;    ///< Overflags bitplane
364     uint8_t condover;
365     uint16_t *hrd_rate, *hrd_buffer;
366     uint8_t *hrd_fullness;
367     uint8_t range_mapy_flag;
368     uint8_t range_mapuv_flag;
369     uint8_t range_mapy;
370     uint8_t range_mapuv;
371     //@}
372 } VC1Context;
373
374 /**
375  * Get unary code of limited length
376  * @fixme FIXME Slow and ugly
377  * @param gb GetBitContext
378  * @param[in] stop The bitstop value (unary code of 1's or 0's)
379  * @param[in] len Maximum length
380  * @return Unary length/index
381  */
382 static int get_prefix(GetBitContext *gb, int stop, int len)
383 {
384 #if 1
385     int i;
386
387     for(i = 0; i < len && get_bits1(gb) != stop; i++);
388     return i;
389 /*  int i = 0, tmp = !stop;
390
391   while (i != len && tmp != stop)
392   {
393     tmp = get_bits(gb, 1);
394     i++;
395   }
396   if (i == len && tmp != stop) return len+1;
397   return i;*/
398 #else
399   unsigned int buf;
400   int log;
401
402   OPEN_READER(re, gb);
403   UPDATE_CACHE(re, gb);
404   buf=GET_CACHE(re, gb); //Still not sure
405   if (stop) buf = ~buf;
406
407   log= av_log2(-buf); //FIXME: -?
408   if (log < limit){
409     LAST_SKIP_BITS(re, gb, log+1);
410     CLOSE_READER(re, gb);
411     return log;
412   }
413
414   LAST_SKIP_BITS(re, gb, limit);
415   CLOSE_READER(re, gb);
416   return limit;
417 #endif
418 }
419
420 static inline int decode210(GetBitContext *gb){
421     int n;
422     n = get_bits1(gb);
423     if (n == 1)
424         return 0;
425     else
426         return 2 - get_bits1(gb);
427 }
428
429 /**
430  * Init VC-1 specific tables and VC1Context members
431  * @param v The VC1Context to initialize
432  * @return Status
433  */
434 static int vc1_init_common(VC1Context *v)
435 {
436     static int done = 0;
437     int i = 0;
438
439     /* Set the bit planes */
440     v->mv_type_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
441     v->direct_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
442     v->skip_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
443     v->ac_pred_plane = v->over_flags_plane = (struct BitPlane) { NULL, 0, 0, 0 };
444     v->hrd_rate = v->hrd_buffer = NULL;
445
446     /* VLC tables */
447     if(!done)
448     {
449         done = 1;
450         init_vlc(&vc1_bfraction_vlc, VC1_BFRACTION_VLC_BITS, 23,
451                  vc1_bfraction_bits, 1, 1,
452                  vc1_bfraction_codes, 1, 1, 1);
453         init_vlc(&vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4,
454                  vc1_norm2_bits, 1, 1,
455                  vc1_norm2_codes, 1, 1, 1);
456         init_vlc(&vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 64,
457                  vc1_norm6_bits, 1, 1,
458                  vc1_norm6_codes, 2, 2, 1);
459         init_vlc(&vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7,
460                  vc1_imode_bits, 1, 1,
461                  vc1_imode_codes, 1, 1, 1);
462         for (i=0; i<3; i++)
463         {
464             init_vlc(&vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 16,
465                      vc1_ttmb_bits[i], 1, 1,
466                      vc1_ttmb_codes[i], 2, 2, 1);
467             init_vlc(&vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 8,
468                      vc1_ttblk_bits[i], 1, 1,
469                      vc1_ttblk_codes[i], 1, 1, 1);
470             init_vlc(&vc1_subblkpat_vlc[i], VC1_SUBBLKPAT_VLC_BITS, 15,
471                      vc1_subblkpat_bits[i], 1, 1,
472                      vc1_subblkpat_codes[i], 1, 1, 1);
473         }
474         for(i=0; i<4; i++)
475         {
476             init_vlc(&vc1_4mv_block_pattern_vlc[i], VC1_4MV_BLOCK_PATTERN_VLC_BITS, 16,
477                      vc1_4mv_block_pattern_bits[i], 1, 1,
478                      vc1_4mv_block_pattern_codes[i], 1, 1, 1);
479             init_vlc(&vc1_cbpcy_p_vlc[i], VC1_CBPCY_P_VLC_BITS, 64,
480                      vc1_cbpcy_p_bits[i], 1, 1,
481                      vc1_cbpcy_p_codes[i], 2, 2, 1);
482             init_vlc(&vc1_mv_diff_vlc[i], VC1_MV_DIFF_VLC_BITS, 73,
483                      vc1_mv_diff_bits[i], 1, 1,
484                      vc1_mv_diff_codes[i], 2, 2, 1);
485         }
486         for(i=0; i<8; i++)
487             init_vlc(&vc1_ac_coeff_table[i], AC_VLC_BITS, vc1_ac_sizes[i],
488                      &vc1_ac_tables[i][0][1], 8, 4,
489                      &vc1_ac_tables[i][0][0], 8, 4, 1);
490         init_vlc(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64,
491                  &ff_msmp4_mb_i_table[0][1], 4, 2,
492                  &ff_msmp4_mb_i_table[0][0], 4, 2, 1);
493     }
494
495     /* Other defaults */
496     v->pq = -1;
497     v->mvrange = 0; /* 7.1.1.18, p80 */
498
499     return 0;
500 }
501
502 /***********************************************************************/
503 /**
504  * @defgroup bitplane VC9 Bitplane decoding
505  * @see 8.7, p56
506  * @{
507  */
508
509 /** @addtogroup bitplane
510  * Imode types
511  * @{
512  */
513 enum Imode {
514     IMODE_RAW,
515     IMODE_NORM2,
516     IMODE_DIFF2,
517     IMODE_NORM6,
518     IMODE_DIFF6,
519     IMODE_ROWSKIP,
520     IMODE_COLSKIP
521 };
522 /** @} */ //imode defines
523
524 /** Allocate the buffer from a bitplane, given its dimensions
525  * @param bp Bitplane which buffer is to allocate
526  * @param[in] width Width of the buffer
527  * @param[in] height Height of the buffer
528  * @return Status
529  * @todo TODO: Take into account stride
530  * @todo TODO: Allow use of external buffers ?
531  */
532 static int alloc_bitplane(BitPlane *bp, int width, int height)
533 {
534     if (!bp || bp->width<0 || bp->height<0) return -1;
535     bp->data = (uint8_t*)av_malloc(width*height);
536     if (!bp->data) return -1;
537     bp->width = bp->stride = width;
538     bp->height = height;
539     return 0;
540 }
541
542 /** Free the bitplane's buffer
543  * @param bp Bitplane which buffer is to free
544  */
545 static void free_bitplane(BitPlane *bp)
546 {
547     bp->width = bp->stride = bp->height = 0;
548     if (bp->data) av_freep(&bp->data);
549 }
550
551 /** Decode rows by checking if they are skipped
552  * @param plane Buffer to store decoded bits
553  * @param[in] width Width of this buffer
554  * @param[in] height Height of this buffer
555  * @param[in] stride of this buffer
556  */
557 static void decode_rowskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
558     int x, y;
559
560     for (y=0; y<height; y++){
561         if (!get_bits(gb, 1)) //rowskip
562             memset(plane, 0, width);
563         else
564             for (x=0; x<width; x++)
565                 plane[x] = get_bits(gb, 1);
566         plane += stride;
567     }
568 }
569
570 /** Decode columns by checking if they are skipped
571  * @param plane Buffer to store decoded bits
572  * @param[in] width Width of this buffer
573  * @param[in] height Height of this buffer
574  * @param[in] stride of this buffer
575  * @fixme FIXME: Optimize
576  */
577 static void decode_colskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
578     int x, y;
579
580     for (x=0; x<width; x++){
581         if (!get_bits(gb, 1)) //colskip
582             for (y=0; y<height; y++)
583                 plane[y*stride] = 0;
584         else
585             for (y=0; y<height; y++)
586                 plane[y*stride] = get_bits(gb, 1);
587         plane ++;
588     }
589 }
590
591 /** Decode a bitplane's bits
592  * @param bp Bitplane where to store the decode bits
593  * @param v VC-1 context for bit reading and logging
594  * @return Status
595  * @fixme FIXME: Optimize
596  * @todo TODO: Decide if a struct is needed
597  */
598 static int bitplane_decoding(BitPlane *bp, VC1Context *v)
599 {
600     GetBitContext *gb = &v->s.gb;
601
602     int imode, x, y, code, offset;
603     uint8_t invert, *planep = bp->data;
604
605     invert = get_bits(gb, 1);
606     imode = get_vlc2(gb, vc1_imode_vlc.table, VC1_IMODE_VLC_BITS, 1);
607
608     bp->is_raw = 0;
609     switch (imode)
610     {
611     case IMODE_RAW:
612         //Data is actually read in the MB layer (same for all tests == "raw")
613         bp->is_raw = 1; //invert ignored
614         return invert;
615     case IMODE_DIFF2:
616     case IMODE_NORM2:
617         if ((bp->height * bp->width) & 1)
618         {
619             *planep++ = get_bits(gb, 1);
620             offset = 1;
621         }
622         else offset = 0;
623         // decode bitplane as one long line
624         for (y = offset; y < bp->height * bp->width; y += 2) {
625             code = get_vlc2(gb, vc1_norm2_vlc.table, VC1_NORM2_VLC_BITS, 1);
626             *planep++ = code & 1;
627             offset++;
628             if(offset == bp->width) {
629                 offset = 0;
630                 planep += bp->stride - bp->width;
631             }
632             *planep++ = code >> 1;
633             offset++;
634             if(offset == bp->width) {
635                 offset = 0;
636                 planep += bp->stride - bp->width;
637             }
638         }
639         break;
640     case IMODE_DIFF6:
641     case IMODE_NORM6:
642         if(!(bp->height % 3) && (bp->width % 3)) { // use 2x3 decoding
643             for(y = 0; y < bp->height; y+= 3) {
644                 for(x = bp->width & 1; x < bp->width; x += 2) {
645                     code = get_vlc2(gb, vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
646                     if(code < 0){
647                         av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
648                         return -1;
649                     }
650                     planep[x + 0] = (code >> 0) & 1;
651                     planep[x + 1] = (code >> 1) & 1;
652                     planep[x + 0 + bp->stride] = (code >> 2) & 1;
653                     planep[x + 1 + bp->stride] = (code >> 3) & 1;
654                     planep[x + 0 + bp->stride * 2] = (code >> 4) & 1;
655                     planep[x + 1 + bp->stride * 2] = (code >> 5) & 1;
656                 }
657                 planep += bp->stride * 3;
658             }
659             if(bp->width & 1) decode_colskip(bp->data, 1, bp->height, bp->stride, &v->s.gb);
660         } else { // 3x2
661             for(y = bp->height & 1; y < bp->height; y += 2) {
662                 for(x = bp->width % 3; x < bp->width; x += 3) {
663                     code = get_vlc2(gb, vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
664                     if(code < 0){
665                         av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
666                         return -1;
667                     }
668                     planep[x + 0] = (code >> 0) & 1;
669                     planep[x + 1] = (code >> 1) & 1;
670                     planep[x + 2] = (code >> 2) & 1;
671                     planep[x + 0 + bp->stride] = (code >> 3) & 1;
672                     planep[x + 1 + bp->stride] = (code >> 4) & 1;
673                     planep[x + 2 + bp->stride] = (code >> 5) & 1;
674                 }
675                 planep += bp->stride * 2;
676             }
677             x = bp->width % 3;
678             if(x) decode_colskip(bp->data  ,             x, bp->height    , bp->stride, &v->s.gb);
679             if(bp->height & 1) decode_rowskip(bp->data+x, bp->width - x, bp->height & 1, bp->stride, &v->s.gb);
680         }
681         break;
682     case IMODE_ROWSKIP:
683         decode_rowskip(bp->data, bp->width, bp->height, bp->stride, &v->s.gb);
684         break;
685     case IMODE_COLSKIP:
686         decode_colskip(bp->data, bp->width, bp->height, bp->stride, &v->s.gb);
687         break;
688     default: break;
689     }
690
691     /* Applying diff operator */
692     if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6)
693     {
694         planep = bp->data;
695         planep[0] ^= invert;
696         for (x=1; x<bp->width; x++)
697             planep[x] ^= planep[x-1];
698         for (y=1; y<bp->height; y++)
699         {
700             planep += bp->stride;
701             planep[0] ^= planep[-bp->stride];
702             for (x=1; x<bp->width; x++)
703             {
704                 if (planep[x-1] != planep[x-bp->stride]) planep[x] ^= invert;
705                 else                                     planep[x] ^= planep[x-1];
706             }
707         }
708     }
709     else if (invert)
710     {
711         planep = bp->data;
712         for (x=0; x<bp->width*bp->height; x++) planep[x] = !planep[x]; //FIXME stride
713     }
714     return (imode<<1) + invert;
715 }
716 /** @} */ //Bitplane group
717
718 /***********************************************************************/
719 /** VOP Dquant decoding
720  * @param v VC-1 Context
721  */
722 static int vop_dquant_decoding(VC1Context *v)
723 {
724     GetBitContext *gb = &v->s.gb;
725     int pqdiff;
726
727     //variable size
728     if (v->dquant == 2)
729     {
730         pqdiff = get_bits(gb, 3);
731         if (pqdiff == 7) v->altpq = get_bits(gb, 5);
732         else v->altpq = v->pq + pqdiff + 1;
733     }
734     else
735     {
736         v->dquantfrm = get_bits(gb, 1);
737         if ( v->dquantfrm )
738         {
739             v->dqprofile = get_bits(gb, 2);
740             switch (v->dqprofile)
741             {
742             case DQPROFILE_SINGLE_EDGE:
743             case DQPROFILE_DOUBLE_EDGES:
744                 v->dqsbedge = get_bits(gb, 2);
745                 break;
746             case DQPROFILE_ALL_MBS:
747                 v->dqbilevel = get_bits(gb, 1);
748             default: break; //Forbidden ?
749             }
750             if (!v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS)
751             {
752                 pqdiff = get_bits(gb, 3);
753                 if (pqdiff == 7) v->altpq = get_bits(gb, 5);
754                 else v->altpq = v->pq + pqdiff + 1;
755             }
756         }
757     }
758     return 0;
759 }
760
761
762 /** Do inverse transform
763  */
764 static void vc1_inv_trans(DCTELEM block[64], int M, int N)
765 {
766     int i;
767     register int t1,t2,t3,t4,t5,t6,t7,t8;
768     DCTELEM *src, *dst;
769
770     src = block;
771     dst = block;
772     if(M==4){
773         for(i = 0; i < N; i++){
774             t1 = 17 * (src[0] + src[2]);
775             t2 = 17 * (src[0] - src[2]);
776             t3 = 22 * src[1];
777             t4 = 22 * src[3];
778             t5 = 10 * src[1];
779             t6 = 10 * src[3];
780
781             dst[0] = (t1 + t3 + t6 + 4) >> 3;
782             dst[1] = (t2 - t4 + t5 + 4) >> 3;
783             dst[2] = (t2 + t4 - t5 + 4) >> 3;
784             dst[3] = (t1 - t3 - t6 + 4) >> 3;
785
786             src += 8;
787             dst += 8;
788         }
789     }else{
790         for(i = 0; i < N; i++){
791             t1 = 12 * (src[0] + src[4]);
792             t2 = 12 * (src[0] - src[4]);
793             t3 = 16 * src[2] +  6 * src[6];
794             t4 =  6 * src[2] - 16 * src[6];
795
796             t5 = t1 + t3;
797             t6 = t2 + t4;
798             t7 = t2 - t4;
799             t8 = t1 - t3;
800
801             t1 = 16 * src[1] + 15 * src[3] +  9 * src[5] +  4 * src[7];
802             t2 = 15 * src[1] -  4 * src[3] - 16 * src[5] -  9 * src[7];
803             t3 =  9 * src[1] - 16 * src[3] +  4 * src[5] + 15 * src[7];
804             t4 =  4 * src[1] -  9 * src[3] + 15 * src[5] - 16 * src[7];
805
806             dst[0] = (t5 + t1 + 4) >> 3;
807             dst[1] = (t6 + t2 + 4) >> 3;
808             dst[2] = (t7 + t3 + 4) >> 3;
809             dst[3] = (t8 + t4 + 4) >> 3;
810             dst[4] = (t8 - t4 + 4) >> 3;
811             dst[5] = (t7 - t3 + 4) >> 3;
812             dst[6] = (t6 - t2 + 4) >> 3;
813             dst[7] = (t5 - t1 + 4) >> 3;
814
815             src += 8;
816             dst += 8;
817         }
818     }
819
820     src = block;
821     dst = block;
822     if(N==4){
823         for(i = 0; i < M; i++){
824             t1 = 17 * (src[ 0] + src[16]);
825             t2 = 17 * (src[ 0] - src[16]);
826             t3 = 22 * src[ 8];
827             t4 = 22 * src[24];
828             t5 = 10 * src[ 8];
829             t6 = 10 * src[24];
830
831             dst[ 0] = (t1 + t3 + t6 + 64) >> 7;
832             dst[ 8] = (t2 - t4 + t5 + 64) >> 7;
833             dst[16] = (t2 + t4 - t5 + 64) >> 7;
834             dst[24] = (t1 - t3 - t6 + 64) >> 7;
835
836             src ++;
837             dst ++;
838         }
839     }else{
840         for(i = 0; i < M; i++){
841             t1 = 12 * (src[ 0] + src[32]);
842             t2 = 12 * (src[ 0] - src[32]);
843             t3 = 16 * src[16] +  6 * src[48];
844             t4 =  6 * src[16] - 16 * src[48];
845
846             t5 = t1 + t3;
847             t6 = t2 + t4;
848             t7 = t2 - t4;
849             t8 = t1 - t3;
850
851             t1 = 16 * src[ 8] + 15 * src[24] +  9 * src[40] +  4 * src[56];
852             t2 = 15 * src[ 8] -  4 * src[24] - 16 * src[40] -  9 * src[56];
853             t3 =  9 * src[ 8] - 16 * src[24] +  4 * src[40] + 15 * src[56];
854             t4 =  4 * src[ 8] -  9 * src[24] + 15 * src[40] - 16 * src[56];
855
856             dst[ 0] = (t5 + t1 + 64) >> 7;
857             dst[ 8] = (t6 + t2 + 64) >> 7;
858             dst[16] = (t7 + t3 + 64) >> 7;
859             dst[24] = (t8 + t4 + 64) >> 7;
860             dst[32] = (t8 - t4 + 64 + 1) >> 7;
861             dst[40] = (t7 - t3 + 64 + 1) >> 7;
862             dst[48] = (t6 - t2 + 64 + 1) >> 7;
863             dst[56] = (t5 - t1 + 64 + 1) >> 7;
864
865             src++;
866             dst++;
867         }
868     }
869 }
870
871 /** Apply overlap transform
872  * @todo optimize
873  * @todo move to DSPContext
874  */
875 static void vc1_overlap_block(MpegEncContext *s, DCTELEM block[64], int n, int do_hor, int do_vert)
876 {
877     int i;
878
879     if(do_hor) { //TODO
880     }
881     if(do_vert) { //TODO
882     }
883
884     for(i = 0; i < 64; i++)
885         block[i] += 128;
886 }
887
888
889 /** Put block onto picture
890  * @todo move to DSPContext
891  */
892 static void vc1_put_block(VC1Context *v, DCTELEM block[6][64])
893 {
894     uint8_t *Y;
895     int ys, us, vs;
896     DSPContext *dsp = &v->s.dsp;
897
898     ys = v->s.current_picture.linesize[0];
899     us = v->s.current_picture.linesize[1];
900     vs = v->s.current_picture.linesize[2];
901     Y = v->s.dest[0];
902
903     dsp->put_pixels_clamped(block[0], Y, ys);
904     dsp->put_pixels_clamped(block[1], Y + 8, ys);
905     Y += ys * 8;
906     dsp->put_pixels_clamped(block[2], Y, ys);
907     dsp->put_pixels_clamped(block[3], Y + 8, ys);
908
909     dsp->put_pixels_clamped(block[4], v->s.dest[1], us);
910     dsp->put_pixels_clamped(block[5], v->s.dest[2], vs);
911 }
912
913 /** Do motion compensation over 1 macroblock
914  * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
915  */
916 static void vc1_mc_1mv(VC1Context *v)
917 {
918     MpegEncContext *s = &v->s;
919     DSPContext *dsp = &v->s.dsp;
920     uint8_t *srcY, *srcU, *srcV;
921     int dxy, mx, my, src_x, src_y;
922     int width = s->mb_width * 16, height = s->mb_height * 16;
923
924     if(!v->s.last_picture.data[0])return;
925
926     mx = s->mv[0][0][0] >> s->mspel;
927     my = s->mv[0][0][1] >> s->mspel;
928     srcY = s->last_picture.data[0];
929     srcU = s->last_picture.data[1];
930     srcV = s->last_picture.data[2];
931
932     if(s->mspel) { // hpel mc
933         dxy = ((my & 1) << 1) | (mx & 1);
934         src_x = s->mb_x * 16 + (mx >> 1);
935         src_y = s->mb_y * 16 + (my >> 1);
936 /*        src_x = clip(src_x, -16, width); //FIXME unneeded for emu?
937         if (src_x == width)
938             dxy &= ~1;
939         src_y = clip(src_y, -16, height);
940         if (src_y == height)
941             dxy &= ~2;*/
942         srcY += src_y * s->linesize + src_x;
943         srcU += (src_y >> 1) * s->uvlinesize + (src_x >> 1);
944         srcV += (src_y >> 1) * s->uvlinesize + (src_x >> 1);
945
946         if((unsigned)src_x > s->h_edge_pos - (mx&1) - 16
947            || (unsigned)src_y > s->v_edge_pos - (my&1) - 16){
948             uint8_t *uvbuf= s->edge_emu_buffer + 18 * s->linesize;
949
950             ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 16+1, 16+1,
951                              src_x, src_y, s->h_edge_pos, s->v_edge_pos);
952             srcY = s->edge_emu_buffer;
953             ff_emulated_edge_mc(uvbuf, srcU, s->uvlinesize, 8+1, 8+1,
954                              src_x >> 1, src_y >> 1, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
955             ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
956                              src_x >> 1, src_y >> 1, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
957             srcU = uvbuf;
958             srcV = uvbuf + 16;
959         }
960         dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
961         dsp->put_no_rnd_pixels_tab[1][0](s->dest[1], srcU, s->uvlinesize, 8);
962         dsp->put_no_rnd_pixels_tab[1][0](s->dest[2], srcV, s->uvlinesize, 8);
963     } else {
964         int motion_x = mx, motion_y = my, uvdxy, uvsrc_x, uvsrc_y;
965         dxy = ((motion_y & 3) << 2) | (motion_x & 3);
966         src_x = s->mb_x * 16 + (mx >> 2);
967         src_y = s->mb_y * 16 + (my >> 2);
968
969         mx= motion_x/2;
970         my= motion_y/2;
971
972         mx= (mx>>1)|(mx&1);
973         my= (my>>1)|(my&1);
974
975         uvdxy= (mx&1) | ((my&1)<<1);
976         mx>>=1;
977         my>>=1;
978
979         uvsrc_x = s->mb_x * 8 + mx;
980         uvsrc_y = s->mb_y * 8 + my;
981
982         srcY = s->last_picture.data[0] +   src_y *   s->linesize +   src_x;
983         srcU = s->last_picture.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
984         srcV = s->last_picture.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
985
986         if(   (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 16
987               || (unsigned)src_y > s->v_edge_pos - (motion_y&3) - 16  ){
988             uint8_t *uvbuf= s->edge_emu_buffer + 18*s->linesize;
989             ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17, 17,
990                                 src_x, src_y, s->h_edge_pos, s->v_edge_pos);
991             srcY = s->edge_emu_buffer;
992             ff_emulated_edge_mc(uvbuf, srcU, s->uvlinesize, 9, 9,
993                                     uvsrc_x, uvsrc_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
994             ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 9, 9,
995                                     uvsrc_x, uvsrc_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
996             srcU = uvbuf;
997             srcV = uvbuf + 16;
998         }
999
1000         dsp->put_no_rnd_qpel_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize);
1001         dsp->put_no_rnd_pixels_tab[1][uvdxy](s->dest[1], srcU, s->uvlinesize, 8);
1002         dsp->put_no_rnd_pixels_tab[1][uvdxy](s->dest[2], srcV, s->uvlinesize, 8);
1003     }
1004 }
1005
1006 /**
1007  * Decode Simple/Main Profiles sequence header
1008  * @see Figure 7-8, p16-17
1009  * @param avctx Codec context
1010  * @param gb GetBit context initialized from Codec context extra_data
1011  * @return Status
1012  */
1013 static int decode_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
1014 {
1015     VC1Context *v = avctx->priv_data;
1016
1017     av_log(avctx, AV_LOG_INFO, "Header: %0X\n", show_bits(gb, 32));
1018     v->profile = get_bits(gb, 2);
1019     if (v->profile == 2)
1020     {
1021         av_log(avctx, AV_LOG_ERROR, "Profile value 2 is forbidden (and WMV3 Complex Profile is unsupported)\n");
1022         return -1;
1023     }
1024
1025     if (v->profile == PROFILE_ADVANCED)
1026     {
1027         v->level = get_bits(gb, 3);
1028         if(v->level >= 5)
1029         {
1030             av_log(avctx, AV_LOG_ERROR, "Reserved LEVEL %i\n",v->level);
1031         }
1032         v->chromaformat = get_bits(gb, 2);
1033         if (v->chromaformat != 1)
1034         {
1035             av_log(avctx, AV_LOG_ERROR,
1036                    "Only 4:2:0 chroma format supported\n");
1037             return -1;
1038         }
1039     }
1040     else
1041     {
1042         v->res_sm = get_bits(gb, 2); //reserved
1043         if (v->res_sm)
1044         {
1045             av_log(avctx, AV_LOG_ERROR,
1046                    "Reserved RES_SM=%i is forbidden\n", v->res_sm);
1047             return -1;
1048         }
1049     }
1050
1051     // (fps-2)/4 (->30)
1052     v->frmrtq_postproc = get_bits(gb, 3); //common
1053     // (bitrate-32kbps)/64kbps
1054     v->bitrtq_postproc = get_bits(gb, 5); //common
1055     v->s.loop_filter = get_bits(gb, 1); //common
1056     if(v->s.loop_filter == 1 && v->profile == PROFILE_SIMPLE)
1057     {
1058         av_log(avctx, AV_LOG_ERROR,
1059                "LOOPFILTER shell not be enabled in simple profile\n");
1060     }
1061
1062     if (v->profile < PROFILE_ADVANCED)
1063     {
1064         v->res_x8 = get_bits(gb, 1); //reserved
1065         if (v->res_x8)
1066         {
1067             av_log(avctx, AV_LOG_ERROR,
1068                    "1 for reserved RES_X8 is forbidden\n");
1069             //return -1;
1070         }
1071         v->multires = get_bits(gb, 1);
1072         v->res_fasttx = get_bits(gb, 1);
1073         if (!v->res_fasttx)
1074         {
1075             av_log(avctx, AV_LOG_ERROR,
1076                    "0 for reserved RES_FASTTX is forbidden\n");
1077             //return -1;
1078         }
1079     }
1080
1081     v->fastuvmc =  get_bits(gb, 1); //common
1082     if (!v->profile && !v->fastuvmc)
1083     {
1084         av_log(avctx, AV_LOG_ERROR,
1085                "FASTUVMC unavailable in Simple Profile\n");
1086         return -1;
1087     }
1088     v->extended_mv =  get_bits(gb, 1); //common
1089     if (!v->profile && v->extended_mv)
1090     {
1091         av_log(avctx, AV_LOG_ERROR,
1092                "Extended MVs unavailable in Simple Profile\n");
1093         return -1;
1094     }
1095     v->dquant =  get_bits(gb, 2); //common
1096     v->vstransform =  get_bits(gb, 1); //common
1097
1098     if (v->profile < PROFILE_ADVANCED)
1099     {
1100         v->res_transtab = get_bits(gb, 1);
1101         if (v->res_transtab)
1102         {
1103             av_log(avctx, AV_LOG_ERROR,
1104                    "1 for reserved RES_TRANSTAB is forbidden\n");
1105             return -1;
1106         }
1107     }
1108
1109     v->overlap = get_bits(gb, 1); //common
1110
1111     if (v->profile < PROFILE_ADVANCED)
1112     {
1113         v->s.resync_marker = get_bits(gb, 1);
1114         v->rangered = get_bits(gb, 1);
1115         if (v->rangered && v->profile == PROFILE_SIMPLE)
1116         {
1117             av_log(avctx, AV_LOG_INFO,
1118                    "RANGERED should be set to 0 in simple profile\n");
1119         }
1120     }
1121
1122     v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
1123     v->quantizer_mode = get_bits(gb, 2); //common
1124
1125     if (v->profile < PROFILE_ADVANCED)
1126     {
1127         v->finterpflag = get_bits(gb, 1); //common
1128         v->res_rtm_flag = get_bits(gb, 1); //reserved
1129         if (!v->res_rtm_flag)
1130         {
1131             av_log(avctx, AV_LOG_ERROR,
1132                    "0 for reserved RES_RTM_FLAG is forbidden\n");
1133             //return -1;
1134         }
1135         av_log(avctx, AV_LOG_DEBUG,
1136                "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
1137                "LoopFilter=%i, MultiRes=%i, FastUVMV=%i, Extended MV=%i\n"
1138                "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
1139                "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
1140                v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
1141                v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
1142                v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
1143                v->dquant, v->quantizer_mode, avctx->max_b_frames
1144                );
1145         return 0;
1146     }
1147     return -1;
1148 }
1149
1150
1151 static int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
1152 {
1153     int pqindex, lowquant, status;
1154
1155     if(v->finterpflag) v->interpfrm = get_bits(gb, 1);
1156     skip_bits(gb, 2); //framecnt unused
1157     v->rangeredfrm = 0;
1158     if (v->rangered) v->rangeredfrm = get_bits(gb, 1);
1159     v->s.pict_type = get_bits(gb, 1);
1160     if (v->s.avctx->max_b_frames) {
1161         if (!v->s.pict_type) {
1162             if (get_bits(gb, 1)) v->s.pict_type = I_TYPE;
1163             else v->s.pict_type = B_TYPE;
1164         } else v->s.pict_type = P_TYPE;
1165     } else v->s.pict_type = v->s.pict_type ? P_TYPE : I_TYPE;
1166
1167     if(v->s.pict_type == I_TYPE)
1168         get_bits(gb, 7); // skip buffer fullness
1169
1170     /* Quantizer stuff */
1171     pqindex = get_bits(gb, 5);
1172     if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1173         v->pq = pquant_table[0][pqindex];
1174     else
1175         v->pq = pquant_table[v->quantizer_mode-1][pqindex];
1176
1177     if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1178         v->pquantizer = pqindex < 9;
1179     if (v->quantizer_mode == QUANT_UNIFORM || v->quantizer_mode == QUANT_NON_UNIFORM)
1180         v->pquantizer = v->quantizer_mode == QUANT_UNIFORM;
1181     v->pqindex = pqindex;
1182     if (pqindex < 9) v->halfpq = get_bits(gb, 1);
1183     else v->halfpq = 0;
1184     if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
1185         v->pquantizer = get_bits(gb, 1);
1186     v->dquantfrm = 0;
1187
1188 //av_log(v->s.avctx, AV_LOG_INFO, "%c Frame: QP=[%i]%i (+%i/2) %i\n",
1189 //        (v->s.pict_type == P_TYPE) ? 'P' : ((v->s.pict_type == I_TYPE) ? 'I' : 'B'), pqindex, v->pq, v->halfpq, v->rangeredfrm);
1190
1191     //TODO: complete parsing for P/B/BI frames
1192     switch(v->s.pict_type) {
1193     case P_TYPE:
1194         if (v->pq < 5) v->tt_index = 0;
1195         else if(v->pq < 13) v->tt_index = 1;
1196         else v->tt_index = 2;
1197
1198         if (v->extended_mv == 1) v->mvrange = get_prefix(gb, 0, 3);
1199         v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1200         v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
1201         v->range_x = 1 << (v->k_x - 1);
1202         v->range_y = 1 << (v->k_y - 1);
1203         if (v->profile == PROFILE_ADVANCED)
1204         {
1205             if (v->postprocflag) v->postproc = get_bits(gb, 1);
1206         }
1207         else
1208             if (v->multires) v->respic = get_bits(gb, 2);
1209         lowquant = (v->pq > 12) ? 0 : 1;
1210         v->mv_mode = mv_pmode_table[lowquant][get_prefix(gb, 1, 4)];
1211         if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
1212         {
1213             v->mv_mode2 = mv_pmode_table[lowquant][get_prefix(gb, 1, 3)];
1214             v->lumscale = get_bits(gb, 6);
1215             v->lumshift = get_bits(gb, 6);
1216         }
1217         if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
1218             v->s.mspel = 1;
1219         else
1220             v->s.mspel = 0;
1221
1222 if(v->mv_mode != MV_PMODE_1MV && v->mv_mode != MV_PMODE_1MV_HPEL && v->mv_mode != MV_PMODE_1MV_HPEL_BILIN) {
1223     av_log(v->s.avctx, AV_LOG_ERROR, "Only 1MV P-frames are supported by now\n");
1224     return -1;
1225 }
1226         if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
1227                  v->mv_mode2 == MV_PMODE_MIXED_MV)
1228                 || v->mv_mode == MV_PMODE_MIXED_MV)
1229         {
1230             status = bitplane_decoding(&v->mv_type_mb_plane, v);
1231             if (status < 0) return -1;
1232             av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
1233                    "Imode: %i, Invert: %i\n", status>>1, status&1);
1234         }
1235         status = bitplane_decoding(&v->skip_mb_plane, v);
1236         if (status < 0) return -1;
1237         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1238                "Imode: %i, Invert: %i\n", status>>1, status&1);
1239
1240         /* Hopefully this is correct for P frames */
1241         v->s.mv_table_index = get_bits(gb, 2); //but using vc1_ tables
1242         v->cbpcy_vlc = &vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1243
1244         if (v->dquant)
1245         {
1246             av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1247             vop_dquant_decoding(v);
1248         }
1249
1250         v->ttfrm = 0; //FIXME Is that so ?
1251         if (v->vstransform)
1252         {
1253             v->ttmbf = get_bits(gb, 1);
1254             if (v->ttmbf)
1255             {
1256                 v->ttfrm = get_bits(gb, 2);
1257             }
1258         }
1259         break;
1260     case B_TYPE:
1261         break;
1262     }
1263
1264     /* AC Syntax */
1265     v->c_ac_table_index = decode012(gb);
1266     if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
1267     {
1268         v->y_ac_table_index = decode012(gb);
1269     }
1270     /* DC Syntax */
1271     v->s.dc_table_index = get_bits(gb, 1);
1272
1273     return 0;
1274 }
1275
1276 /***********************************************************************/
1277 /**
1278  * @defgroup block VC-1 Block-level functions
1279  * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1280  * @todo TODO: Integrate to MpegEncContext facilities
1281  * @{
1282  */
1283
1284 /**
1285  * @def GET_MQUANT
1286  * @brief Get macroblock-level quantizer scale
1287  * @warning XXX: qdiff to the frame quant, not previous quant ?
1288  * @fixme XXX: Don't know how to initialize mquant otherwise in last case
1289  */
1290 #define GET_MQUANT()                                           \
1291   if (v->dquantfrm)                                            \
1292   {                                                            \
1293     if (v->dqprofile == DQPROFILE_ALL_MBS)                     \
1294     {                                                          \
1295       if (v->dqbilevel)                                        \
1296       {                                                        \
1297         mquant = (get_bits(gb, 1)) ? v->pq : v->altpq;         \
1298       }                                                        \
1299       else                                                     \
1300       {                                                        \
1301         mqdiff = get_bits(gb, 3);                              \
1302         if (mqdiff != 7) mquant = v->pq + mqdiff;              \
1303         else mquant = get_bits(gb, 5);                         \
1304       }                                                        \
1305     }                                                          \
1306     else mquant = v->pq;                                       \
1307   }
1308
1309 /**
1310  * @def GET_MVDATA(_dmv_x, _dmv_y)
1311  * @brief Get MV differentials
1312  * @see MVDATA decoding from 8.3.5.2, p(1)20
1313  * @param _dmv_x Horizontal differential for decoded MV
1314  * @param _dmv_y Vertical differential for decoded MV
1315  * @todo TODO: Use MpegEncContext arrays to store them
1316  */
1317 #define GET_MVDATA(_dmv_x, _dmv_y)                                  \
1318   index = 1 + get_vlc2(gb, vc1_mv_diff_vlc[s->mv_table_index].table,\
1319                        VC1_MV_DIFF_VLC_BITS, 2);                    \
1320   if (index > 36)                                                   \
1321   {                                                                 \
1322     mb_has_coeffs = 1;                                              \
1323     index -= 37;                                                    \
1324   }                                                                 \
1325   else mb_has_coeffs = 0;                                           \
1326   s->mb_intra = 0;                                                  \
1327   if (!index) { _dmv_x = _dmv_y = 0; }                              \
1328   else if (index == 35)                                             \
1329   {                                                                 \
1330     _dmv_x = get_bits(gb, v->k_x - s->mspel);                       \
1331     _dmv_y = get_bits(gb, v->k_y - s->mspel);                       \
1332   }                                                                 \
1333   else if (index == 36)                                             \
1334   {                                                                 \
1335     _dmv_x = 0;                                                     \
1336     _dmv_y = 0;                                                     \
1337     s->mb_intra = 1;                                                \
1338   }                                                                 \
1339   else                                                              \
1340   {                                                                 \
1341     index1 = index%6;                                               \
1342     if (s->mspel && index1 == 5) val = 1;                           \
1343     else                         val = 0;                           \
1344     val = get_bits(gb, size_table[index1] - val);                   \
1345     sign = 0 - (val&1);                                             \
1346     _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign;     \
1347                                                                     \
1348     index1 = index/6;                                               \
1349     if (s->mspel && index1 == 5) val = 1;                           \
1350     else                          val = 0;                          \
1351     val = get_bits(gb, size_table[index1] - val);                   \
1352     sign = 0 - (val&1);                                             \
1353     _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign;     \
1354   }
1355
1356 /** Predict and set motion vector
1357  */
1358 static inline void vc1_pred_mv(MpegEncContext *s, int dmv_x, int dmv_y, int mv1, int r_x, int r_y)
1359 {
1360     int xy, wrap, off;
1361     int16_t *A, *B, *C;
1362     int px, py;
1363     int sum;
1364     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1365
1366     /* scale MV difference to be quad-pel */
1367     dmv_x <<= s->mspel;
1368     dmv_y <<= s->mspel;
1369
1370     wrap = s->b8_stride;
1371     xy = s->block_index[0];
1372
1373     C = s->current_picture.motion_val[0][xy - (1 << mv1)];
1374     A = s->current_picture.motion_val[0][xy - (wrap << mv1)];
1375     off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1376     B = s->current_picture.motion_val[0][xy + ((off - wrap) << mv1)];
1377
1378     if(!s->first_slice_line) { // predictor A is not out of bounds
1379         if(s->mb_width == 1) {
1380             px = A[0];
1381             py = A[1];
1382         } else {
1383             px = mid_pred(A[0], B[0], C[0]);
1384             py = mid_pred(A[1], B[1], C[1]);
1385         }
1386     } else if(s->mb_x) { // predictor C is not out of bounds
1387         px = C[0];
1388         py = C[1];
1389     } else {
1390         px = py = 0;
1391     }
1392     if(s->mb_intra) px = py = 0;
1393
1394     /* Pullback MV as specified in 8.3.5.3.4 */
1395     {
1396         int qx, qy, X, Y;
1397         qx = s->mb_x << 6; //FIXME: add real block coords for 4MV mode
1398         qy = s->mb_y << 6;
1399         X = (s->mb_width << 6) - 4;
1400         Y = (s->mb_height << 6) - 4;
1401         if(mv1) {
1402             if(qx + px < -60) px = -60 - qx;
1403             if(qy + py < -60) py = -60 - qy;
1404         } else {
1405             if(qx + px < -28) px = -28 - qx;
1406             if(qy + py < -28) py = -28 - qy;
1407         }
1408         if(qx + px > X) px = X - qx;
1409         if(qy + py > Y) py = Y - qy;
1410     }
1411     /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
1412     if(!s->mb_intra && !s->first_slice_line && s->mb_x) {
1413         if(IS_INTRA(s->current_picture.mb_type[mb_pos - s->mb_stride]))
1414             sum = ABS(px) + ABS(py);
1415         else
1416             sum = ABS(px - A[0]) + ABS(py - A[1]);
1417         if(sum > 32) {
1418             if(get_bits1(&s->gb)) {
1419                 px = A[0];
1420                 py = A[1];
1421             } else {
1422                 px = C[0];
1423                 py = C[1];
1424             }
1425         } else {
1426             if(IS_INTRA(s->current_picture.mb_type[mb_pos - 1]))
1427                 sum = ABS(px) + ABS(py);
1428             else
1429                 sum = ABS(px - C[0]) + ABS(py - C[1]);
1430             if(sum > 32) {
1431                 if(get_bits1(&s->gb)) {
1432                     px = A[0];
1433                     py = A[1];
1434                 } else {
1435                     px = C[0];
1436                     py = C[1];
1437                 }
1438             }
1439         }
1440     }
1441     /* store MV using signed modulus of MV range defined in 4.11 */
1442     s->mv[0][0][0] = s->current_picture.motion_val[0][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1443     s->mv[0][0][1] = s->current_picture.motion_val[0][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
1444 }
1445
1446 /** Get predicted DC value for I-frames only
1447  * prediction dir: left=0, top=1
1448  * @param s MpegEncContext
1449  * @param[in] n block index in the current MB
1450  * @param dc_val_ptr Pointer to DC predictor
1451  * @param dir_ptr Prediction direction for use in AC prediction
1452  */
1453 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
1454                               int16_t **dc_val_ptr, int *dir_ptr)
1455 {
1456     int a, b, c, wrap, pred, scale;
1457     int16_t *dc_val;
1458     static const uint16_t dcpred[32] = {
1459     -1, 1024,  512,  341,  256,  205,  171,  146,  128,
1460          114,  102,   93,   85,   79,   73,   68,   64,
1461           60,   57,   54,   51,   49,   47,   45,   43,
1462           41,   39,   38,   37,   35,   34,   33
1463     };
1464
1465     /* find prediction - wmv3_dc_scale always used here in fact */
1466     if (n < 4)     scale = s->y_dc_scale;
1467     else           scale = s->c_dc_scale;
1468
1469     wrap = s->block_wrap[n];
1470     dc_val= s->dc_val[0] + s->block_index[n];
1471
1472     /* B A
1473      * C X
1474      */
1475     c = dc_val[ - 1];
1476     b = dc_val[ - 1 - wrap];
1477     a = dc_val[ - wrap];
1478
1479     if (pq < 9 || !overlap)
1480     {
1481         /* Set outer values */
1482         if (!s->mb_y && (n!=2 && n!=3)) b=a=dcpred[scale];
1483         if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=dcpred[scale];
1484     }
1485     else
1486     {
1487         /* Set outer values */
1488         if (!s->mb_y && (n!=2 && n!=3)) b=a=0;
1489         if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=0;
1490     }
1491
1492     if (abs(a - b) <= abs(b - c)) {
1493         pred = c;
1494         *dir_ptr = 1;//left
1495     } else {
1496         pred = a;
1497         *dir_ptr = 0;//top
1498     }
1499
1500     /* update predictor */
1501     *dc_val_ptr = &dc_val[0];
1502     return pred;
1503 }
1504
1505
1506 /** Get predicted DC value
1507  * prediction dir: left=0, top=1
1508  * @param s MpegEncContext
1509  * @param[in] n block index in the current MB
1510  * @param dc_val_ptr Pointer to DC predictor
1511  * @param dir_ptr Prediction direction for use in AC prediction
1512  */
1513 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
1514                               int a_avail, int c_avail,
1515                               int16_t **dc_val_ptr, int *dir_ptr)
1516 {
1517     int a, b, c, wrap, pred, scale;
1518     int16_t *dc_val;
1519     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1520     int mb_pos2, q1, q2;
1521
1522     /* find prediction - wmv3_dc_scale always used here in fact */
1523     if (n < 4)     scale = s->y_dc_scale;
1524     else           scale = s->c_dc_scale;
1525
1526     wrap = s->block_wrap[n];
1527     dc_val= s->dc_val[0] + s->block_index[n];
1528
1529     /* B A
1530      * C X
1531      */
1532     c = dc_val[ - 1];
1533     b = dc_val[ - 1 - wrap];
1534     a = dc_val[ - wrap];
1535
1536     if(a_avail && c_avail) {
1537         if(abs(a - b) <= abs(b - c)) {
1538             pred = c;
1539             *dir_ptr = 1;//left
1540         } else {
1541             pred = a;
1542             *dir_ptr = 0;//top
1543         }
1544     } else if(a_avail) {
1545         pred = a;
1546         *dir_ptr = 0;//top
1547     } else if(c_avail) {
1548         pred = c;
1549         *dir_ptr = 1;//left
1550     } else {
1551         pred = 0;
1552         *dir_ptr = 1;//left
1553     }
1554
1555     /* scale coeffs if needed */
1556     mb_pos2 = mb_pos - *dir_ptr - (1 - *dir_ptr) * s->mb_stride;
1557     q1 = s->current_picture.qscale_table[mb_pos];
1558     q2 = s->current_picture.qscale_table[mb_pos2];
1559     if(0 && q1 && q2 && q1 != q2) {
1560         q1 = s->y_dc_scale_table[q1];
1561         q2 = s->y_dc_scale_table[q2];
1562         pred = (pred * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1563     }
1564
1565     /* update predictor */
1566     *dc_val_ptr = &dc_val[0];
1567     return pred;
1568 }
1569
1570
1571 /**
1572  * @defgroup std_mb VC1 Macroblock-level functions in Simple/Main Profiles
1573  * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1574  * @todo TODO: Integrate to MpegEncContext facilities
1575  * @{
1576  */
1577
1578 static inline int vc1_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
1579 {
1580     int xy, wrap, pred, a, b, c;
1581
1582     xy = s->block_index[n];
1583     wrap = s->b8_stride;
1584
1585     /* B C
1586      * A X
1587      */
1588     a = s->coded_block[xy - 1       ];
1589     b = s->coded_block[xy - 1 - wrap];
1590     c = s->coded_block[xy     - wrap];
1591
1592     if (b == c) {
1593         pred = a;
1594     } else {
1595         pred = c;
1596     }
1597
1598     /* store value */
1599     *coded_block_ptr = &s->coded_block[xy];
1600
1601     return pred;
1602 }
1603
1604 /**
1605  * Decode one AC coefficient
1606  * @param v The VC1 context
1607  * @param last Last coefficient
1608  * @param skip How much zero coefficients to skip
1609  * @param value Decoded AC coefficient value
1610  * @see 8.1.3.4
1611  */
1612 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip, int *value, int codingset)
1613 {
1614     GetBitContext *gb = &v->s.gb;
1615     int index, escape, run = 0, level = 0, lst = 0;
1616
1617     index = get_vlc2(gb, vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
1618     if (index != vc1_ac_sizes[codingset] - 1) {
1619         run = vc1_index_decode_table[codingset][index][0];
1620         level = vc1_index_decode_table[codingset][index][1];
1621         lst = index >= vc1_last_decode_table[codingset];
1622         if(get_bits(gb, 1))
1623             level = -level;
1624     } else {
1625         escape = decode210(gb);
1626         if (escape == 0) {
1627             index = get_vlc2(gb, vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
1628             run = vc1_index_decode_table[codingset][index][0];
1629             level = vc1_index_decode_table[codingset][index][1];
1630             lst = index >= vc1_last_decode_table[codingset];
1631             if(lst)
1632                 level += vc1_last_delta_level_table[codingset][run];
1633             else
1634                 level += vc1_delta_level_table[codingset][run];
1635             if(get_bits(gb, 1))
1636                 level = -level;
1637         } else if (escape == 1) {
1638             index = get_vlc2(gb, vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
1639             run = vc1_index_decode_table[codingset][index][0];
1640             level = vc1_index_decode_table[codingset][index][1];
1641             lst = index >= vc1_last_decode_table[codingset];
1642             if(lst)
1643                 run += vc1_last_delta_run_table[codingset][level] + 1;
1644             else
1645                 run += vc1_delta_run_table[codingset][level] + 1;
1646             if(get_bits(gb, 1))
1647                 level = -level;
1648         } else {
1649             int sign;
1650             lst = get_bits(gb, 1);
1651             if(v->s.esc3_level_length == 0) {
1652                 if(v->pq < 8 || v->dquantfrm) { // table 59
1653                     v->s.esc3_level_length = get_bits(gb, 3);
1654                     if(!v->s.esc3_level_length)
1655                         v->s.esc3_level_length = get_bits(gb, 2) + 8;
1656                 } else { //table 60
1657                     v->s.esc3_level_length = get_prefix(gb, 1, 6) + 2;
1658                 }
1659                 v->s.esc3_run_length = 3 + get_bits(gb, 2);
1660             }
1661             run = get_bits(gb, v->s.esc3_run_length);
1662             sign = get_bits(gb, 1);
1663             level = get_bits(gb, v->s.esc3_level_length);
1664             if(sign)
1665                 level = -level;
1666         }
1667     }
1668
1669     *last = lst;
1670     *skip = run;
1671     *value = level;
1672 }
1673
1674 /** Decode intra block in intra frames - should be faster than decode_intra_block
1675  * @param v VC1Context
1676  * @param block block to decode
1677  * @param coded are AC coeffs present or not
1678  * @param codingset set of VLC to decode data
1679  */
1680 static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset)
1681 {
1682     GetBitContext *gb = &v->s.gb;
1683     MpegEncContext *s = &v->s;
1684     int dc_pred_dir = 0; /* Direction of the DC prediction used */
1685     int run_diff, i;
1686     int16_t *dc_val;
1687     int16_t *ac_val, *ac_val2;
1688     int dcdiff;
1689
1690     /* Get DC differential */
1691     if (n < 4) {
1692         dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1693     } else {
1694         dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1695     }
1696     if (dcdiff < 0){
1697         av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
1698         return -1;
1699     }
1700     if (dcdiff)
1701     {
1702         if (dcdiff == 119 /* ESC index value */)
1703         {
1704             /* TODO: Optimize */
1705             if (v->pq == 1) dcdiff = get_bits(gb, 10);
1706             else if (v->pq == 2) dcdiff = get_bits(gb, 9);
1707             else dcdiff = get_bits(gb, 8);
1708         }
1709         else
1710         {
1711             if (v->pq == 1)
1712                 dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
1713             else if (v->pq == 2)
1714                 dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
1715         }
1716         if (get_bits(gb, 1))
1717             dcdiff = -dcdiff;
1718     }
1719
1720     /* Prediction */
1721     dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
1722     *dc_val = dcdiff;
1723
1724     /* Store the quantized DC coeff, used for prediction */
1725
1726     if (n < 4) {
1727         block[0] = dcdiff * s->y_dc_scale;
1728     } else {
1729         block[0] = dcdiff * s->c_dc_scale;
1730     }
1731     /* Skip ? */
1732     run_diff = 0;
1733     i = 0;
1734     if (!coded) {
1735         goto not_coded;
1736     }
1737
1738     //AC Decoding
1739     i = 1;
1740
1741     {
1742         int last = 0, skip, value;
1743         const int8_t *zz_table;
1744         int scale;
1745         int k;
1746
1747         scale = v->pq * 2 + v->halfpq;
1748
1749         if(v->s.ac_pred) {
1750             if(!dc_pred_dir)
1751                 zz_table = vc1_horizontal_zz;
1752             else
1753                 zz_table = vc1_vertical_zz;
1754         } else
1755             zz_table = vc1_normal_zz;
1756
1757         ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1758         ac_val2 = ac_val;
1759         if(dc_pred_dir) //left
1760             ac_val -= 16;
1761         else //top
1762             ac_val -= 16 * s->block_wrap[n];
1763
1764         while (!last) {
1765             vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1766             i += skip;
1767             if(i > 63)
1768                 break;
1769             block[zz_table[i++]] = value;
1770         }
1771
1772         /* apply AC prediction if needed */
1773         if(s->ac_pred) {
1774             if(dc_pred_dir) { //left
1775                 for(k = 1; k < 8; k++)
1776                     block[k << 3] += ac_val[k];
1777             } else { //top
1778                 for(k = 1; k < 8; k++)
1779                     block[k] += ac_val[k + 8];
1780             }
1781         }
1782         /* save AC coeffs for further prediction */
1783         for(k = 1; k < 8; k++) {
1784             ac_val2[k] = block[k << 3];
1785             ac_val2[k + 8] = block[k];
1786         }
1787
1788         /* scale AC coeffs */
1789         for(k = 1; k < 64; k++)
1790             if(block[k]) {
1791                 block[k] *= scale;
1792                 if(!v->pquantizer)
1793                     block[k] += (block[k] < 0) ? -v->pq : v->pq;
1794             }
1795
1796         if(s->ac_pred) i = 63;
1797     }
1798
1799 not_coded:
1800     if(!coded) {
1801         int k, scale;
1802         ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1803         ac_val2 = ac_val;
1804
1805         scale = v->pq * 2 + v->halfpq;
1806         memset(ac_val2, 0, 16 * 2);
1807         if(dc_pred_dir) {//left
1808             ac_val -= 16;
1809             if(s->ac_pred)
1810                 memcpy(ac_val2, ac_val, 8 * 2);
1811         } else {//top
1812             ac_val -= 16 * s->block_wrap[n];
1813             if(s->ac_pred)
1814                 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1815         }
1816
1817         /* apply AC prediction if needed */
1818         if(s->ac_pred) {
1819             if(dc_pred_dir) { //left
1820                 for(k = 1; k < 8; k++) {
1821                     block[k << 3] = ac_val[k] * scale;
1822                     if(!v->pquantizer)
1823                         block[k << 3] += (block[k << 3] < 0) ? -v->pq : v->pq;
1824                 }
1825             } else { //top
1826                 for(k = 1; k < 8; k++) {
1827                     block[k] = ac_val[k + 8] * scale;
1828                     if(!v->pquantizer)
1829                         block[k] += (block[k] < 0) ? -v->pq : v->pq;
1830                 }
1831             }
1832             i = 63;
1833         }
1834     }
1835     s->block_last_index[n] = i;
1836
1837     return 0;
1838 }
1839
1840 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
1841  * @param v VC1Context
1842  * @param block block to decode
1843  * @param coded are AC coeffs present or not
1844  * @param mquant block quantizer
1845  * @param codingset set of VLC to decode data
1846  */
1847 static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n, int coded, int mquant, int codingset)
1848 {
1849     GetBitContext *gb = &v->s.gb;
1850     MpegEncContext *s = &v->s;
1851     int dc_pred_dir = 0; /* Direction of the DC prediction used */
1852     int run_diff, i;
1853     int16_t *dc_val;
1854     int16_t *ac_val, *ac_val2;
1855     int dcdiff;
1856     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1857     int a_avail = v->a_avail, c_avail = v->c_avail;
1858
1859     /* XXX: Guard against dumb values of mquant */
1860     mquant = (mquant < 1) ? 0 : ( (mquant>31) ? 31 : mquant );
1861
1862     /* Set DC scale - y and c use the same */
1863     s->y_dc_scale = s->y_dc_scale_table[mquant];
1864     s->c_dc_scale = s->c_dc_scale_table[mquant];
1865
1866     /* Get DC differential */
1867     if (n < 4) {
1868         dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1869     } else {
1870         dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1871     }
1872     if (dcdiff < 0){
1873         av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
1874         return -1;
1875     }
1876     if (dcdiff)
1877     {
1878         if (dcdiff == 119 /* ESC index value */)
1879         {
1880             /* TODO: Optimize */
1881             if (mquant == 1) dcdiff = get_bits(gb, 10);
1882             else if (mquant == 2) dcdiff = get_bits(gb, 9);
1883             else dcdiff = get_bits(gb, 8);
1884         }
1885         else
1886         {
1887             if (mquant == 1)
1888                 dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
1889             else if (mquant == 2)
1890                 dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
1891         }
1892         if (get_bits(gb, 1))
1893             dcdiff = -dcdiff;
1894     }
1895
1896     /* Prediction */
1897     dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
1898     *dc_val = dcdiff;
1899
1900     /* Store the quantized DC coeff, used for prediction */
1901
1902     if (n < 4) {
1903         block[0] = dcdiff * s->y_dc_scale;
1904     } else {
1905         block[0] = dcdiff * s->c_dc_scale;
1906     }
1907     /* Skip ? */
1908     run_diff = 0;
1909     i = 0;
1910     if (!coded) {
1911         goto not_coded;
1912     }
1913
1914     //AC Decoding
1915     i = 1;
1916
1917     {
1918         int last = 0, skip, value;
1919         const int8_t *zz_table;
1920         int scale;
1921         int k;
1922
1923         scale = mquant * 2;
1924
1925         zz_table = vc1_simple_progressive_8x8_zz;
1926
1927         ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1928         ac_val2 = ac_val;
1929         if(dc_pred_dir) //left
1930             ac_val -= 16;
1931         else //top
1932             ac_val -= 16 * s->block_wrap[n];
1933
1934         while (!last) {
1935             vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1936             i += skip;
1937             if(i > 63)
1938                 break;
1939             block[zz_table[i++]] = value;
1940         }
1941
1942         /* apply AC prediction if needed */
1943         if(s->ac_pred) {
1944             /* scale predictors if needed*/
1945             int mb_pos2, q1, q2;
1946
1947             mb_pos2 = mb_pos - dc_pred_dir - (1 - dc_pred_dir) * s->mb_stride;
1948             q1 = s->current_picture.qscale_table[mb_pos];
1949             q2 = s->current_picture.qscale_table[mb_pos2];
1950
1951             if(!c_avail) {
1952                 memset(ac_val, 0, 8 * sizeof(ac_val[0]));
1953                 dc_pred_dir = 0;
1954             }
1955             if(!a_avail) {
1956                 memset(ac_val + 8, 0, 8 * sizeof(ac_val[0]));
1957                 dc_pred_dir = 1;
1958             }
1959             if(!q1 && q1 && q2 && q1 != q2) {
1960                 q1 = q1 * 2 - 1;
1961                 q2 = q2 * 2 - 1;
1962
1963                 if(dc_pred_dir) { //left
1964                     for(k = 1; k < 8; k++)
1965                         block[k << 3] += (ac_val[k] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1966                 } else { //top
1967                     for(k = 1; k < 8; k++)
1968                         block[k] += (ac_val[k + 8] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1969                 }
1970             } else {
1971                 if(dc_pred_dir) { //left
1972                     for(k = 1; k < 8; k++)
1973                         block[k << 3] += ac_val[k];
1974                 } else { //top
1975                     for(k = 1; k < 8; k++)
1976                         block[k] += ac_val[k + 8];
1977                 }
1978             }
1979         }
1980         /* save AC coeffs for further prediction */
1981         for(k = 1; k < 8; k++) {
1982             ac_val2[k] = block[k << 3];
1983             ac_val2[k + 8] = block[k];
1984         }
1985
1986         /* scale AC coeffs */
1987         for(k = 1; k < 64; k++)
1988             if(block[k]) {
1989                 block[k] *= scale;
1990                 if(!v->pquantizer)
1991                     block[k] += (block[k] < 0) ? -mquant : mquant;
1992             }
1993
1994         if(s->ac_pred) i = 63;
1995     }
1996
1997 not_coded:
1998     if(!coded) {
1999         int k, scale;
2000         ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2001         ac_val2 = ac_val;
2002
2003         if(!c_avail) {
2004             memset(ac_val, 0, 8 * sizeof(ac_val[0]));
2005             dc_pred_dir = 0;
2006         }
2007         if(!a_avail) {
2008             memset(ac_val + 8, 0, 8 * sizeof(ac_val[0]));
2009             dc_pred_dir = 1;
2010         }
2011
2012         scale = mquant * 2;
2013         memset(ac_val2, 0, 16 * 2);
2014         if(dc_pred_dir) {//left
2015             ac_val -= 16;
2016             if(s->ac_pred)
2017                 memcpy(ac_val2, ac_val, 8 * 2);
2018         } else {//top
2019             ac_val -= 16 * s->block_wrap[n];
2020             if(s->ac_pred)
2021                 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2022         }
2023
2024         /* apply AC prediction if needed */
2025         if(s->ac_pred) {
2026             if(dc_pred_dir) { //left
2027                 for(k = 1; k < 8; k++) {
2028                     block[k << 3] = ac_val[k] * scale;
2029                     if(!v->pquantizer)
2030                         block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
2031                 }
2032             } else { //top
2033                 for(k = 1; k < 8; k++) {
2034                     block[k] = ac_val[k + 8] * scale;
2035                     if(!v->pquantizer)
2036                         block[k] += (block[k] < 0) ? -mquant : mquant;
2037                 }
2038             }
2039             i = 63;
2040         }
2041     }
2042     s->block_last_index[n] = i;
2043
2044     return 0;
2045 }
2046
2047 /** Decode P block
2048  */
2049 static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n, int mquant, int ttmb, int first_block)
2050 {
2051     MpegEncContext *s = &v->s;
2052     GetBitContext *gb = &s->gb;
2053     int i, j;
2054     int subblkpat = 0;
2055     int scale, off, idx, last, skip, value;
2056     int ttblk = ttmb & 7;
2057
2058     if(ttmb == -1) {
2059         ttblk = ttblk_to_tt[v->tt_index][get_vlc2(gb, vc1_ttblk_vlc[v->tt_index].table, VC1_TTBLK_VLC_BITS, 1)];
2060     }
2061     if(ttblk == TT_4X4) {
2062         subblkpat = ~(get_vlc2(gb, vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
2063     }
2064     if((ttblk != TT_8X8 && ttblk != TT_4X4) && (v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))) {
2065         subblkpat = decode012(gb);
2066         if(subblkpat) subblkpat ^= 3; //swap decoded pattern bits
2067         if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) ttblk = TT_8X4;
2068         if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) ttblk = TT_4X8;
2069     }
2070     scale = 2 * mquant;
2071
2072     // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
2073     if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
2074         ttblk = TT_8X4;
2075         subblkpat = 2 - (ttblk == TT_8X4_TOP);
2076     }
2077     if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
2078         ttblk = TT_4X8;
2079         subblkpat = 2 - (ttblk == TT_4X8_LEFT);
2080     }
2081
2082     switch(ttblk) {
2083     case TT_8X8:
2084         i = 0;
2085         last = 0;
2086         while (!last) {
2087             vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2088             i += skip;
2089             if(i > 63)
2090                 break;
2091             idx = vc1_simple_progressive_8x8_zz[i++];
2092             block[idx] = value * scale;
2093         }
2094         vc1_inv_trans(block, 8, 8);
2095         break;
2096     case TT_4X4:
2097         for(j = 0; j < 4; j++) {
2098             last = subblkpat & (1 << (3 - j));
2099             i = 0;
2100             off = (j & 1) * 4 + (j & 2) * 32;
2101             while (!last) {
2102                 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2103                 i += skip;
2104                 if(i > 15)
2105                     break;
2106                 idx = vc1_simple_progressive_4x4_zz[i++];
2107                 block[idx + off] = value * scale;
2108             }
2109             vc1_inv_trans(block + off, 4, 4);
2110         }
2111         break;
2112     case TT_8X4:
2113         for(j = 0; j < 2; j++) {
2114             last = subblkpat & (1 << (1 - j));
2115             i = 0;
2116             off = j * 32;
2117             while (!last) {
2118                 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2119                 i += skip;
2120                 if(i > 31)
2121                     break;
2122                 idx = vc1_simple_progressive_8x4_zz[i++];
2123                 block[idx + off] = value * scale;
2124             }
2125             if(!(subblkpat & (1 << (1 - j)))) vc1_inv_trans(block + off, 8, 4);
2126         }
2127         break;
2128     case TT_4X8:
2129         for(j = 0; j < 2; j++) {
2130             last = subblkpat & (1 << (1 - j));
2131             i = 0;
2132             off = j * 4;
2133             while (!last) {
2134                 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2135                 i += skip;
2136                 if(i > 31)
2137                     break;
2138                 idx = vc1_simple_progressive_8x4_zz[i++];
2139                 block[idx + off] = value * scale;
2140             }
2141             vc1_inv_trans(block + off, 4, 8);
2142         }
2143         break;
2144     }
2145     return 0;
2146 }
2147
2148
2149 /** Decode one P-frame MB (in Simple/Main profile)
2150  * @todo TODO: Extend to AP
2151  * @fixme FIXME: DC value for inter blocks not set
2152  */
2153 static int vc1_decode_p_mb(VC1Context *v, DCTELEM block[6][64])
2154 {
2155     MpegEncContext *s = &v->s;
2156     GetBitContext *gb = &s->gb;
2157     int i, j, mb_offset = s->mb_x + s->mb_y*s->mb_width; /* XXX: mb_stride */
2158     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2159     int cbp; /* cbp decoding stuff */
2160     int hybrid_pred; /* Prediction types */
2161     int mqdiff, mquant; /* MB quantization */
2162     int ttmb = v->ttmb; /* MB Transform type */
2163     int status;
2164
2165     static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
2166       offset_table[6] = { 0, 1, 3, 7, 15, 31 };
2167     int mb_has_coeffs = 1; /* last_flag */
2168     int dmv_x, dmv_y; /* Differential MV components */
2169     int index, index1; /* LUT indices */
2170     int val, sign; /* temp values */
2171     int first_block = 1;
2172     int dst_idx, off;
2173
2174     mquant = v->pq; /* Loosy initialization */
2175
2176     if (v->mv_type_mb_plane.is_raw)
2177         v->mv_type_mb_plane.data[mb_offset] = get_bits(gb, 1);
2178     if (v->skip_mb_plane.is_raw)
2179         v->skip_mb_plane.data[mb_offset] = get_bits(gb, 1);
2180     s->current_picture.mbskip_table[mb_pos] = v->skip_mb_plane.data[mb_offset];
2181     if (!v->mv_type_mb_plane.data[mb_offset]) /* 1MV mode */
2182     {
2183         if (!v->skip_mb_plane.data[mb_offset])
2184         {
2185             GET_MVDATA(dmv_x, dmv_y);
2186
2187             s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
2188             vc1_pred_mv(s, dmv_x, dmv_y, 1, v->range_x, v->range_y);
2189
2190             /* FIXME Set DC val for inter block ? */
2191             if (s->mb_intra && !mb_has_coeffs)
2192             {
2193                 GET_MQUANT();
2194                 s->ac_pred = get_bits(gb, 1);
2195                 cbp = 0;
2196             }
2197             else if (mb_has_coeffs)
2198             {
2199                 if (s->mb_intra) s->ac_pred = get_bits(gb, 1);
2200                 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2201                 GET_MQUANT();
2202             }
2203             else
2204             {
2205                 mquant = v->pq;
2206                 cbp = 0;
2207             }
2208             s->current_picture.qscale_table[mb_pos] = mquant;
2209
2210             if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
2211                 ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table,
2212                                 VC1_TTMB_VLC_BITS, 2);
2213             s->dsp.clear_blocks(block[0]);
2214             vc1_mc_1mv(v);
2215             dst_idx = 0;
2216             for (i=0; i<6; i++)
2217             {
2218                 s->dc_val[0][s->block_index[i]] = 0;
2219                 dst_idx += i >> 2;
2220                 val = ((cbp >> (5 - i)) & 1);
2221                 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2222                 if(s->mb_intra) {
2223                     /* check if prediction blocks A and C are available */
2224                     v->a_avail = v->c_avail = 0;
2225                     if((i == 2 || i == 3) || (s->mb_y && IS_INTRA(s->current_picture.mb_type[mb_pos - s->mb_stride])))
2226                         v->a_avail = 1;
2227                     if((i == 1 || i == 3) || (s->mb_x && IS_INTRA(s->current_picture.mb_type[mb_pos - 1])))
2228                         v->c_avail = 1;
2229
2230                     vc1_decode_intra_block(v, block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
2231                     vc1_inv_trans(s->block[i], 8, 8);
2232                     for(j = 0; j < 64; j++) s->block[i][j] += 128;
2233                     s->dsp.put_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
2234                     /* TODO: proper loop filtering */
2235                     if(v->a_avail)
2236                         s->dsp.h263_v_loop_filter(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2), s->y_dc_scale);
2237                     if(v->c_avail)
2238                         s->dsp.h263_h_loop_filter(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2), s->y_dc_scale);
2239                 } else if(val) {
2240                     vc1_decode_p_block(v, block[i], i, mquant, ttmb, first_block);
2241                     if(!v->ttmbf && ttmb < 8) ttmb = -1;
2242                     first_block = 0;
2243                     s->dsp.add_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
2244                 }
2245             }
2246         }
2247         else //Skipped
2248         {
2249             s->mb_intra = 0;
2250             s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
2251             vc1_pred_mv(s, 0, 0, 1, v->range_x, v->range_y);
2252             vc1_mc_1mv(v);
2253             return 0;
2254         }
2255     } //1MV mode
2256     else //4MV mode
2257     {//FIXME: looks not conforming to standard and is not even theoretically complete
2258         if (!v->skip_mb_plane.data[mb_offset] /* unskipped MB */)
2259         {
2260             int blk_intra[4], blk_coded[4];
2261             /* Get CBPCY */
2262             cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2263             for (i=0; i<4; i++)
2264             {
2265                 val = ((cbp >> (5 - i)) & 1);
2266                 blk_intra[i] = 0;
2267                 blk_coded[i] = val;
2268                 if(val) {
2269                     GET_MVDATA(dmv_x, dmv_y);
2270                     blk_intra[i] = s->mb_intra;
2271                 }
2272                 if (v->mv_mode == MV_PMODE_MIXED_MV /* Hybrid pred */)
2273                     hybrid_pred = get_bits(gb, 1);
2274             }
2275             if((blk_intra[0] | blk_intra[1] | blk_intra[2] | blk_intra[3]) ||
2276                 (blk_coded[0] | blk_coded[1] | blk_coded[2] | blk_coded[3])) {
2277                 GET_MQUANT();
2278
2279                 if (s->mb_intra /* One of the 4 blocks is intra */
2280                     /* non-zero pred for that block */)
2281                     s->ac_pred = get_bits(gb, 1);
2282                 if (!v->ttmbf)
2283                     ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table,
2284                                     VC1_TTMB_VLC_BITS, 12);
2285                 for(i = 0; i < 6; i++) {
2286                     val = ((cbp >> (5 - i)) & 1);
2287                     if(i & 4 || blk_intra[i] || val) {
2288                         if(i < 4 && blk_intra[i])
2289                             status = vc1_decode_intra_block(v, block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
2290                         else
2291                             status = vc1_decode_p_block(v, block[i], i, mquant, ttmb, 0);
2292                     }
2293                 }
2294             }
2295             return status;
2296         }
2297         else //Skipped MB
2298         {
2299             /* XXX: Skipped => cbp=0 and mquant doesn't matter ? */
2300             for (i=0; i<4; i++)
2301             {
2302                 if (v->mv_mode == MV_PMODE_MIXED_MV /* Hybrid pred */)
2303                     hybrid_pred = get_bits(gb, 1);
2304             }
2305             /* TODO: blah */
2306             return 0;
2307         }
2308     }
2309
2310     /* Should never happen */
2311     return -1;
2312 }
2313
2314 /** Decode blocks of I-frame
2315  */
2316 static void vc1_decode_i_blocks(VC1Context *v)
2317 {
2318     int k;
2319     MpegEncContext *s = &v->s;
2320     int cbp, val;
2321     uint8_t *coded_val;
2322     int mb_pos;
2323
2324     /* select codingmode used for VLC tables selection */
2325     switch(v->y_ac_table_index){
2326     case 0:
2327         v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2328         break;
2329     case 1:
2330         v->codingset = CS_HIGH_MOT_INTRA;
2331         break;
2332     case 2:
2333         v->codingset = CS_MID_RATE_INTRA;
2334         break;
2335     }
2336
2337     switch(v->c_ac_table_index){
2338     case 0:
2339         v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2340         break;
2341     case 1:
2342         v->codingset2 = CS_HIGH_MOT_INTER;
2343         break;
2344     case 2:
2345         v->codingset2 = CS_MID_RATE_INTER;
2346         break;
2347     }
2348
2349     /* Set DC scale - y and c use the same */
2350     s->y_dc_scale = s->y_dc_scale_table[v->pq];
2351     s->c_dc_scale = s->c_dc_scale_table[v->pq];
2352
2353     //do frame decode
2354     s->mb_x = s->mb_y = 0;
2355     s->mb_intra = 1;
2356     ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2357     for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2358         for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
2359             ff_init_block_index(s);
2360             ff_update_block_index(s);
2361             s->dsp.clear_blocks(s->block[0]);
2362             mb_pos = s->mb_x + s->mb_y * s->mb_width;
2363             s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2364             s->current_picture.qscale_table[mb_pos] = v->pq;
2365
2366             // do actual MB decoding and displaying
2367             cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2368             v->s.ac_pred = get_bits(&v->s.gb, 1);
2369
2370             for(k = 0; k < 6; k++) {
2371                 val = ((cbp >> (5 - k)) & 1);
2372
2373                 if (k < 4) {
2374                     int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2375                     val = val ^ pred;
2376                     *coded_val = val;
2377                 }
2378                 cbp |= val << (5 - k);
2379
2380                 vc1_decode_i_block(v, s->block[k], k, val, (k<4)? v->codingset : v->codingset2);
2381
2382                 vc1_inv_trans(s->block[k], 8, 8);
2383                 if(v->pq >= 9 && v->overlap) {
2384                     vc1_overlap_block(s, s->block[k], k, (s->mb_y || k>1), (s->mb_x || (k != 0 && k != 2)));
2385                 }
2386             }
2387
2388             vc1_put_block(v, s->block);
2389             if(v->pq >= 9 && v->overlap) { /* XXX: do proper overlapping insted of loop filter */
2390                 if(s->mb_y) {
2391                     s->dsp.h263_v_loop_filter(s->dest[0], s->linesize, s->y_dc_scale);
2392                     s->dsp.h263_v_loop_filter(s->dest[0] + 8, s->linesize, s->y_dc_scale);
2393                     s->dsp.h263_v_loop_filter(s->dest[1], s->uvlinesize, s->y_dc_scale);
2394                     s->dsp.h263_v_loop_filter(s->dest[2], s->uvlinesize, s->y_dc_scale);
2395                 }
2396                 s->dsp.h263_v_loop_filter(s->dest[0] + 8 * s->linesize, s->linesize, s->y_dc_scale);
2397                 s->dsp.h263_v_loop_filter(s->dest[0] + 8 * s->linesize + 8, s->linesize, s->y_dc_scale);
2398                 if(s->mb_x) {
2399                     s->dsp.h263_h_loop_filter(s->dest[0], s->linesize, s->y_dc_scale);
2400                     s->dsp.h263_h_loop_filter(s->dest[0] + 8 * s->linesize, s->linesize, s->y_dc_scale);
2401                     s->dsp.h263_h_loop_filter(s->dest[1], s->uvlinesize, s->y_dc_scale);
2402                     s->dsp.h263_h_loop_filter(s->dest[2], s->uvlinesize, s->y_dc_scale);
2403                 }
2404                 s->dsp.h263_h_loop_filter(s->dest[0] + 8, s->linesize, s->y_dc_scale);
2405                 s->dsp.h263_h_loop_filter(s->dest[0] + 8 * s->linesize + 8, s->linesize, s->y_dc_scale);
2406             }
2407
2408             if(get_bits_count(&s->gb) > v->bits) {
2409                 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
2410                 return;
2411             }
2412         }
2413         ff_draw_horiz_band(s, s->mb_y * 16, 16);
2414     }
2415 }
2416
2417 static void vc1_decode_p_blocks(VC1Context *v)
2418 {
2419     MpegEncContext *s = &v->s;
2420
2421     /* select codingmode used for VLC tables selection */
2422     switch(v->c_ac_table_index){
2423     case 0:
2424         v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2425         break;
2426     case 1:
2427         v->codingset = CS_HIGH_MOT_INTRA;
2428         break;
2429     case 2:
2430         v->codingset = CS_MID_RATE_INTRA;
2431         break;
2432     }
2433
2434     switch(v->c_ac_table_index){
2435     case 0:
2436         v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2437         break;
2438     case 1:
2439         v->codingset2 = CS_HIGH_MOT_INTER;
2440         break;
2441     case 2:
2442         v->codingset2 = CS_MID_RATE_INTER;
2443         break;
2444     }
2445
2446     ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2447     s->first_slice_line = 1;
2448     for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2449         for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
2450             ff_init_block_index(s);
2451             ff_update_block_index(s);
2452             s->dsp.clear_blocks(s->block[0]);
2453
2454             vc1_decode_p_mb(v, s->block);
2455             if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2456                 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n", get_bits_count(&s->gb), v->bits,s->mb_x,s->mb_y);
2457                 return;
2458             }
2459         }
2460         ff_draw_horiz_band(s, s->mb_y * 16, 16);
2461         s->first_slice_line = 0;
2462     }
2463 }
2464
2465 static void vc1_decode_blocks(VC1Context *v)
2466 {
2467
2468     v->s.esc3_level_length = 0;
2469
2470     switch(v->s.pict_type) {
2471     case I_TYPE:
2472         vc1_decode_i_blocks(v);
2473         break;
2474     case P_TYPE:
2475         vc1_decode_p_blocks(v);
2476         break;
2477     }
2478 }
2479
2480
2481 /** Initialize a VC1/WMV3 decoder
2482  * @todo TODO: Handle VC-1 IDUs (Transport level?)
2483  * @todo TODO: Decypher remaining bits in extra_data
2484  */
2485 static int vc1_decode_init(AVCodecContext *avctx)
2486 {
2487     VC1Context *v = avctx->priv_data;
2488     MpegEncContext *s = &v->s;
2489     GetBitContext gb;
2490
2491     if (!avctx->extradata_size || !avctx->extradata) return -1;
2492     avctx->pix_fmt = PIX_FMT_YUV420P;
2493     v->s.avctx = avctx;
2494
2495     if(ff_h263_decode_init(avctx) < 0)
2496         return -1;
2497     if (vc1_init_common(v) < 0) return -1;
2498
2499     av_log(avctx, AV_LOG_INFO, "This decoder is not supposed to produce picture. Dont report this as a bug!\n");
2500     av_log(avctx, AV_LOG_INFO, "If you see a picture, don't believe your eyes.\n");
2501
2502     avctx->flags |= CODEC_FLAG_EMU_EDGE;
2503     avctx->coded_width = avctx->width;
2504     avctx->coded_height = avctx->height;
2505     if (avctx->codec_id == CODEC_ID_WMV3)
2506     {
2507         int count = 0;
2508
2509         // looks like WMV3 has a sequence header stored in the extradata
2510         // advanced sequence header may be before the first frame
2511         // the last byte of the extradata is a version number, 1 for the
2512         // samples we can decode
2513
2514         init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
2515
2516         if (decode_sequence_header(avctx, &gb) < 0)
2517           return -1;
2518
2519         count = avctx->extradata_size*8 - get_bits_count(&gb);
2520         if (count>0)
2521         {
2522             av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
2523                    count, get_bits(&gb, count));
2524         }
2525         else if (count < 0)
2526         {
2527             av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
2528         }
2529     }
2530     avctx->has_b_frames= !!(avctx->max_b_frames);
2531
2532     s->mb_width = (avctx->coded_width+15)>>4;
2533     s->mb_height = (avctx->coded_height+15)>>4;
2534
2535     /* Allocate mb bitplanes */
2536     if (alloc_bitplane(&v->mv_type_mb_plane, s->mb_width, s->mb_height) < 0)
2537         return -1;
2538     if (alloc_bitplane(&v->mv_type_mb_plane, s->mb_width, s->mb_height) < 0)
2539         return -1;
2540     if (alloc_bitplane(&v->skip_mb_plane, s->mb_width, s->mb_height) < 0)
2541         return -1;
2542     if (alloc_bitplane(&v->direct_mb_plane, s->mb_width, s->mb_height) < 0)
2543         return -1;
2544
2545     /* For predictors */
2546     v->previous_line_cbpcy = (uint8_t *)av_malloc(s->mb_stride*4);
2547     if (!v->previous_line_cbpcy) return -1;
2548
2549     /* Init coded blocks info */
2550     if (v->profile == PROFILE_ADVANCED)
2551     {
2552         if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
2553             return -1;
2554         if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
2555             return -1;
2556     }
2557
2558     return 0;
2559 }
2560
2561
2562 /** Decode a VC1/WMV3 frame
2563  * @todo TODO: Handle VC-1 IDUs (Transport level?)
2564  * @warning Initial try at using MpegEncContext stuff
2565  */
2566 static int vc1_decode_frame(AVCodecContext *avctx,
2567                             void *data, int *data_size,
2568                             uint8_t *buf, int buf_size)
2569 {
2570     VC1Context *v = avctx->priv_data;
2571     MpegEncContext *s = &v->s;
2572     AVFrame *pict = data;
2573
2574     /* no supplementary picture */
2575     if (buf_size == 0) {
2576         /* special case for last picture */
2577         if (s->low_delay==0 && s->next_picture_ptr) {
2578             *pict= *(AVFrame*)s->next_picture_ptr;
2579             s->next_picture_ptr= NULL;
2580
2581             *data_size = sizeof(AVFrame);
2582         }
2583
2584         return 0;
2585     }
2586
2587     //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
2588     if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
2589         int i= ff_find_unused_picture(s, 0);
2590         s->current_picture_ptr= &s->picture[i];
2591     }
2592
2593     avctx->has_b_frames= !s->low_delay;
2594
2595     init_get_bits(&s->gb, buf, buf_size*8);
2596     // do parse frame header
2597     if(vc1_parse_frame_header(v, &s->gb) == -1)
2598         return -1;
2599
2600     if(s->pict_type != I_TYPE && s->pict_type != P_TYPE)return -1;
2601
2602     // for hurry_up==5
2603     s->current_picture.pict_type= s->pict_type;
2604     s->current_picture.key_frame= s->pict_type == I_TYPE;
2605
2606     /* skip B-frames if we don't have reference frames */
2607     if(s->last_picture_ptr==NULL && (s->pict_type==B_TYPE || s->dropable)) return -1;//buf_size;
2608     /* skip b frames if we are in a hurry */
2609     if(avctx->hurry_up && s->pict_type==B_TYPE) return -1;//buf_size;
2610     if(   (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==B_TYPE)
2611        || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=I_TYPE)
2612        ||  avctx->skip_frame >= AVDISCARD_ALL)
2613         return buf_size;
2614     /* skip everything if we are in a hurry>=5 */
2615     if(avctx->hurry_up>=5) return -1;//buf_size;
2616
2617     if(s->next_p_frame_damaged){
2618         if(s->pict_type==B_TYPE)
2619             return buf_size;
2620         else
2621             s->next_p_frame_damaged=0;
2622     }
2623
2624     if(MPV_frame_start(s, avctx) < 0)
2625         return -1;
2626
2627     ff_er_frame_start(s);
2628
2629     v->bits = buf_size * 8;
2630     vc1_decode_blocks(v);
2631 //av_log(s->avctx, AV_LOG_INFO, "Consumed %i/%i bits\n", get_bits_count(&s->gb), buf_size*8);
2632 //  if(get_bits_count(&s->gb) > buf_size * 8)
2633 //      return -1;
2634     ff_er_frame_end(s);
2635
2636     MPV_frame_end(s);
2637
2638 assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
2639 assert(s->current_picture.pict_type == s->pict_type);
2640     if (s->pict_type == B_TYPE || s->low_delay) {
2641         *pict= *(AVFrame*)s->current_picture_ptr;
2642     } else if (s->last_picture_ptr != NULL) {
2643         *pict= *(AVFrame*)s->last_picture_ptr;
2644     }
2645
2646     if(s->last_picture_ptr || s->low_delay){
2647         *data_size = sizeof(AVFrame);
2648         ff_print_debug_info(s, pict);
2649     }
2650
2651     /* Return the Picture timestamp as the frame number */
2652     /* we substract 1 because it is added on utils.c    */
2653     avctx->frame_number = s->picture_number - 1;
2654
2655     return buf_size;
2656 }
2657
2658
2659 /** Close a VC1/WMV3 decoder
2660  * @warning Initial try at using MpegEncContext stuff
2661  */
2662 static int vc1_decode_end(AVCodecContext *avctx)
2663 {
2664     VC1Context *v = avctx->priv_data;
2665
2666     av_freep(&v->hrd_rate);
2667     av_freep(&v->hrd_buffer);
2668     MPV_common_end(&v->s);
2669     free_bitplane(&v->mv_type_mb_plane);
2670     free_bitplane(&v->skip_mb_plane);
2671     free_bitplane(&v->direct_mb_plane);
2672     return 0;
2673 }
2674
2675
2676 AVCodec vc1_decoder = {
2677     "vc1",
2678     CODEC_TYPE_VIDEO,
2679     CODEC_ID_VC1,
2680     sizeof(VC1Context),
2681     vc1_decode_init,
2682     NULL,
2683     vc1_decode_end,
2684     vc1_decode_frame,
2685     CODEC_CAP_DELAY,
2686     NULL
2687 };
2688
2689 AVCodec wmv3_decoder = {
2690     "wmv3",
2691     CODEC_TYPE_VIDEO,
2692     CODEC_ID_WMV3,
2693     sizeof(VC1Context),
2694     vc1_decode_init,
2695     NULL,
2696     vc1_decode_end,
2697     vc1_decode_frame,
2698     CODEC_CAP_DELAY,
2699     NULL
2700 };