[svn] / trunk / xvidcore / src / bitstream / mbcoding.c Repository:
ViewVC logotype

Diff of /trunk/xvidcore/src/bitstream/mbcoding.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 100, Thu Apr 4 13:58:18 2002 UTC revision 1486, Sat Jul 10 17:49:31 2004 UTC
# Line 1  Line 1 
1    /*****************************************************************************
2     *
3     *  XVID MPEG-4 VIDEO CODEC
4     *  - MB coding -
5     *
6     *  Copyright (C) 2002 Michael Militzer <isibaar@xvid.org>
7     *
8     *  This program is free software ; you can redistribute it and/or modify
9     *  it under the terms of the GNU General Public License as published by
10     *  the Free Software Foundation ; either version 2 of the License, or
11     *  (at your option) any later version.
12     *
13     *  This program is distributed in the hope that it will be useful,
14     *  but WITHOUT ANY WARRANTY ; without even the implied warranty of
15     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     *  GNU General Public License for more details.
17     *
18     *  You should have received a copy of the GNU General Public License
19     *  along with this program ; if not, write to the Free Software
20     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21     *
22     * $Id: mbcoding.c,v 1.47 2004-07-10 17:49:31 edgomez Exp $
23     *
24     ****************************************************************************/
25    
26    #include <stdio.h>
27  #include <stdlib.h>  #include <stdlib.h>
28    #include <string.h>
29    
30  #include "../portab.h"  #include "../portab.h"
31    #include "../global.h"
32  #include "bitstream.h"  #include "bitstream.h"
33  #include "zigzag.h"  #include "zigzag.h"
34  #include "vlc_codes.h"  #include "vlc_codes.h"
# Line 7  Line 36 
36    
37  #include "../utils/mbfunctions.h"  #include "../utils/mbfunctions.h"
38    
39  #define ESCAPE 7167  #define LEVELOFFSET 32
 #define ABS(X) (((X)>0)?(X):-(X))  
 #define CLIP(X,A) (X > A) ? (A) : (X)  
   
 VLC DCT3Dintra[4096];  
 VLC DCT3Dinter[4096];  
 static VLC *DCT3D[2];  
   
 VLC intra_table[65536];  
 VLC inter_table[65536];  
40    
41  static short clip_table[4096];  static REVERSE_EVENT DCT3D[2][4096];
42    static VLC coeff_VLC[2][2][64][64];
43    
44  void init_vlc_tables(void)  /* not really MB related, but VLCs are only available here */
45    void bs_put_spritetrajectory(Bitstream * bs, const int val)
46  {  {
47            const int code = sprite_trajectory_code[val+16384].code;
48            const int len = sprite_trajectory_code[val+16384].len;
49            const int code2 = sprite_trajectory_len[len].code;
50            const int len2 = sprite_trajectory_len[len].len;
51    
52          int32_t k, l, i, intra, last;  #if 0
53          VLC *vlc[2];          printf("GMC=%d Code/Len  = %d / %d ",val, code,len);
54          VLC **coeff_ptr;          printf("Code2 / Len2 = %d / %d \n",code2,len2);
55          VLC *vlc1, *vlc2;  #endif
56    
57          vlc1 = DCT3Dintra;          BitstreamPutBits(bs, code2, len2);
58          vlc2 = DCT3Dinter;          if (len) BitstreamPutBits(bs, code, len);
   
         vlc[0] = intra_table;  
         vlc[1] = inter_table;  
   
         // initialize the clipping table  
         for(i = -2048; i < 2048; i++) {  
                 clip_table[i + 2048] = i;  
                 if(i < -255)  
                         clip_table[i + 2048] = -255;  
                 if(i > 255)  
                         clip_table[i + 2048] = 255;  
59          }          }
60    
61          // generate encoding vlc lookup tables  int bs_get_spritetrajectory(Bitstream * bs)
62          for(i = 0; i < 4; i++) {  {
63                  intra = i % 2;          int i;
64                  last = i / 2;          for (i = 0; i < 12; i++)
65            {
66                  coeff_ptr = coeff_vlc[last + 2 * intra];                  if (BitstreamShowBits(bs, sprite_trajectory_len[i].len) == sprite_trajectory_len[i].code)
67                    {
68                  for(k = -255; k < 256; k++) { // level                          BitstreamSkip(bs, sprite_trajectory_len[i].len);
69                          int8_t *max_level_ptr = max_level[last + 2 * intra];                          return i;
70                          int8_t *max_run_ptr = max_run[last + 2 * intra];                  }
71            }
72            return -1;
73    }
74    
75                          for(l = 0; l < 64; l++) { // run  void
76                                  int32_t level = k;  init_vlc_tables(void)
77                                  uint32_t run = l;  {
78            uint32_t i, j, k, intra, last, run,  run_esc, level, level_esc, escape, escape_len, offset;
79            int32_t l;
80    
81                                  if((abs(level) <= max_level_ptr[run]) &&          for (intra = 0; intra < 2; intra++)
82                                     (run <= max_run_ptr[abs(level)])) { // level < max_level and run < max_run                  for (i = 0; i < 4096; i++)
83                            DCT3D[intra][i].event.level = 0;
84    
85                                                  vlc[intra]->code = 0;          for (intra = 0; intra < 2; intra++) {
86                                                  vlc[intra]->len = 0;                  for (last = 0; last < 2; last++) {
87                                                  goto loop_end;                          for (run = 0; run < 63 + last; run++) {
88                                    for (level = 0; level < (uint32_t)(32 << intra); level++) {
89                                            offset = !intra * LEVELOFFSET;
90                                            coeff_VLC[intra][last][level + offset][run].len = 128;
91                                    }
92                            }
93                                  }                                  }
                                 else {  
                                         if(level > 0)                                        // correct level  
                                                 level -= max_level_ptr[run];  
                                         else  
                                                 level += max_level_ptr[run];  
   
                                         if((abs(level) <= max_level_ptr[run]) &&  
                                            (run <= max_run_ptr[abs(level)])) {  
   
                                                 vlc[intra]->code = 0x06;  
                                                 vlc[intra]->len = 8;  
                                                 goto loop_end;  
94                                          }                                          }
95    
96                                          if(level > 0)                                       // still here?          for (intra = 0; intra < 2; intra++) {
97                                                  level += max_level_ptr[run];    // restore level                  for (i = 0; i < 102; i++) {
98                                          else                          offset = !intra * LEVELOFFSET;
                                                 level -= max_level_ptr[run];  
99    
100                                          run -= max_run_ptr[abs(level)] + 1; // and change run                          for (j = 0; j < (uint32_t)(1 << (12 - coeff_tab[intra][i].vlc.len)); j++) {
101                                    DCT3D[intra][(coeff_tab[intra][i].vlc.code << (12 - coeff_tab[intra][i].vlc.len)) | j].len       = coeff_tab[intra][i].vlc.len;
102                                    DCT3D[intra][(coeff_tab[intra][i].vlc.code << (12 - coeff_tab[intra][i].vlc.len)) | j].event = coeff_tab[intra][i].event;
103                            }
104    
105                                          if((abs(level) <= max_level_ptr[run]) &&                          coeff_VLC[intra][coeff_tab[intra][i].event.last][coeff_tab[intra][i].event.level + offset][coeff_tab[intra][i].event.run].code
106                                             (run <= max_run_ptr[abs(level)])) {                                  = coeff_tab[intra][i].vlc.code << 1;
107                            coeff_VLC[intra][coeff_tab[intra][i].event.last][coeff_tab[intra][i].event.level + offset][coeff_tab[intra][i].event.run].len
108                                    = coeff_tab[intra][i].vlc.len + 1;
109    
110                                                  vlc[intra]->code = 0x0e;                          if (!intra) {
111                                                  vlc[intra]->len = 9;                                  coeff_VLC[intra][coeff_tab[intra][i].event.last][offset - coeff_tab[intra][i].event.level][coeff_tab[intra][i].event.run].code
112                                                  goto loop_end;                                          = (coeff_tab[intra][i].vlc.code << 1) | 1;
113                                    coeff_VLC[intra][coeff_tab[intra][i].event.last][offset - coeff_tab[intra][i].event.level][coeff_tab[intra][i].event.run].len
114                                            = coeff_tab[intra][i].vlc.len + 1;
115                            }
116                                          }                                          }
                                         run += max_run_ptr[abs(level)] + 1;  
117                                  }                                  }
118    
119                                  vlc[intra]->code = (uint32_t) ((l << 14) | (0x1e + last) << 20) |          for (intra = 0; intra < 2; intra++) {
120                                                                                            (1 << 13) | ((k & 0xfff) << 1) | 1;                  for (last = 0; last < 2; last++) {
121                            for (run = 0; run < 63 + last; run++) {
122                                    for (level = 1; level < (uint32_t)(32 << intra); level++) {
123    
124                                  vlc[intra]->len = 30;                                          if (level <= max_level[intra][last][run] && run <= max_run[intra][last][level])
                                 vlc[intra]++;  
125                                  continue;                                  continue;
126    
127  loop_end:                                          offset = !intra * LEVELOFFSET;
128                                  if(level != 0) {                      level_esc = level - max_level[intra][last][run];
129                                          vlc[intra]->code = (vlc[intra]->code << (coeff_ptr[run][abs(level) - 1].len + 1)) |                                          run_esc = run - 1 - max_run[intra][last][level];
130                                                                             (coeff_ptr[run][abs(level) - 1].code << 1);  
131                                          vlc[intra]->len = (coeff_ptr[run][abs(level) - 1].len + 1) + vlc[intra]->len;                                          if (level_esc <= max_level[intra][last][run] && run <= max_run[intra][last][level_esc]) {
132                                                    escape     = ESCAPE1;
133                                          if(level < 0)                                                  escape_len = 7 + 1;
134                                                  vlc[intra]->code += 1;                                                  run_esc    = run;
135                                            } else {
136                                                    if (run_esc <= max_run[intra][last][level] && level <= max_level[intra][last][run_esc]) {
137                                                            escape     = ESCAPE2;
138                                                            escape_len = 7 + 2;
139                                                            level_esc  = level;
140                                                    } else {
141                                                            if (!intra) {
142                                                                    coeff_VLC[intra][last][level + offset][run].code
143                                                                            = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((level & 0xfff) << 1) | 1;
144                                                                    coeff_VLC[intra][last][level + offset][run].len = 30;
145                                                                            coeff_VLC[intra][last][offset - level][run].code
146                                                                            = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-(int32_t)level & 0xfff) << 1) | 1;
147                                                                    coeff_VLC[intra][last][offset - level][run].len = 30;
148                                  }                                  }
149                                                            continue;
                                 vlc[intra]++;  
150                          }                          }
151                  }                  }
152    
153                                            coeff_VLC[intra][last][level + offset][run].code
154                                                    = (escape << coeff_VLC[intra][last][level_esc + offset][run_esc].len)
155                                                    |  coeff_VLC[intra][last][level_esc + offset][run_esc].code;
156                                            coeff_VLC[intra][last][level + offset][run].len
157                                                    = coeff_VLC[intra][last][level_esc + offset][run_esc].len + escape_len;
158    
159                                            if (!intra) {
160                                                    coeff_VLC[intra][last][offset - level][run].code
161                                                            = (escape << coeff_VLC[intra][last][level_esc + offset][run_esc].len)
162                                                            |  coeff_VLC[intra][last][level_esc + offset][run_esc].code | 1;
163                                                    coeff_VLC[intra][last][offset - level][run].len
164                                                            = coeff_VLC[intra][last][level_esc + offset][run_esc].len + escape_len;
165                                            }
166          }          }
167    
168          for(i = 0; i < 4096; i++) {                                  if (!intra) {
169                  if(i >= 512) {                                          coeff_VLC[intra][last][0][run].code
170                          *vlc1 = DCT3Dtab3[(i >> 5) - 16];                                                  = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-32 & 0xfff) << 1) | 1;
171                          *vlc2 = DCT3Dtab0[(i >> 5) - 16];                                          coeff_VLC[intra][last][0][run].len = 30;
172                  }                  }
                 else if(i >= 128) {  
                         *vlc1 = DCT3Dtab4[(i >> 2) - 32];  
                         *vlc2 = DCT3Dtab1[(i >> 2) - 32];  
173                  }                  }
                 else if(i >= 8) {  
                         *vlc1 = DCT3Dtab5[i - 8];  
                         *vlc2 = DCT3Dtab2[i - 8];  
174                  }                  }
                 else {  
                         *vlc1 = ERRtab[i];  
                         *vlc2 = ERRtab[i];  
175                  }                  }
176    
177                  vlc1++;          /* init sprite_trajectory tables
178                  vlc2++;           * even if GMC is not specified (it might be used later...) */
179    
180            sprite_trajectory_code[0+16384].code = 0;
181            sprite_trajectory_code[0+16384].len = 0;
182            for (k=0;k<14;k++) {
183                    int limit = (1<<k);
184    
185                    for (l=-(2*limit-1); l <= -limit; l++) {
186                            sprite_trajectory_code[l+16384].code = (2*limit-1)+l;
187                            sprite_trajectory_code[l+16384].len = k+1;
188          }          }
         DCT3D[0] = DCT3Dinter;  
         DCT3D[1] = DCT3Dintra;  
189    
190                    for (l=limit; l<= 2*limit-1; l++) {
191                            sprite_trajectory_code[l+16384].code = l;
192                            sprite_trajectory_code[l+16384].len = k+1;
193                    }
194            }
195  }  }
196    
197  static __inline void CodeVector(Bitstream *bs,  static __inline void
198                                  int16_t value,  CodeVector(Bitstream * bs,
199                                  int16_t f_code,                     int32_t value,
200                       int32_t f_code,
201                                  Statistics *pStat)                                  Statistics *pStat)
202  {  {
203    
# Line 163  Line 214 
214          pStat->iMvCount++;          pStat->iMvCount++;
215    
216          if (value == 0) {          if (value == 0) {
217                  BitstreamPutBits(bs, mb_motion_table[32].code, mb_motion_table[32].len);                  BitstreamPutBits(bs, mb_motion_table[32].code,
218                                                     mb_motion_table[32].len);
219          } else {          } else {
220                  uint16_t length, code, mv_res, sign;                  uint16_t length, code, mv_res, sign;
221    
# Line 188  Line 240 
240                          code = -code;                          code = -code;
241    
242                  code += 32;                  code += 32;
243                  BitstreamPutBits(bs, mb_motion_table[code].code, mb_motion_table[code].len);                  BitstreamPutBits(bs, mb_motion_table[code].code,
244                                                     mb_motion_table[code].len);
245    
246                  if(f_code)                  if(f_code)
247                          BitstreamPutBits(bs, mv_res, f_code);                          BitstreamPutBits(bs, mv_res, f_code);
# Line 196  Line 249 
249    
250  }  }
251    
252    static __inline void
253    CodeCoeffInter(Bitstream * bs,
254                      const int16_t qcoeff[64],
255                      const uint16_t * zigzag)
256    {
257            uint32_t i, run, prev_run, code, len;
258            int32_t level, prev_level, level_shifted;
259    
260            i       = 0;
261            run = 0;
262    
263            while (!(level = qcoeff[zigzag[i++]]))
264                    run++;
265    
266            prev_level = level;
267            prev_run   = run;
268            run = 0;
269    
270  static __inline void CodeCoeff(Bitstream *bs,          while (i < 64)
                                int16_t qcoeff[64],  
                                VLC *table,  
                                const uint16_t *zigzag,  
                                uint16_t intra)  
271  {  {
272                    if ((level = qcoeff[zigzag[i++]]) != 0)
273                    {
274                            level_shifted = prev_level + 32;
275                            if (!(level_shifted & -64))
276                            {
277                                    code = coeff_VLC[0][0][level_shifted][prev_run].code;
278                                    len      = coeff_VLC[0][0][level_shifted][prev_run].len;
279                            }
280                            else
281                            {
282                                    code = (ESCAPE3 << 21) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1;
283                                    len  = 30;
284                            }
285                            BitstreamPutBits(bs, code, len);
286                            prev_level = level;
287                            prev_run   = run;
288                            run = 0;
289                    }
290                    else
291                            run++;
292            }
293    
294          uint32_t j, last;          level_shifted = prev_level + 32;
295          short v;          if (!(level_shifted & -64))
296          VLC *vlc;          {
297                    code = coeff_VLC[0][1][level_shifted][prev_run].code;
298                    len      = coeff_VLC[0][1][level_shifted][prev_run].len;
299            }
300            else
301            {
302                    code = (ESCAPE3 << 21) | (1 << 20) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1;
303                    len  = 30;
304            }
305            BitstreamPutBits(bs, code, len);
306    }
307    
308          j = intra;  static __inline void
309          last = 1 + intra;  CodeCoeffIntra(Bitstream * bs,
310                      const int16_t qcoeff[64],
311                      const uint16_t * zigzag)
312    {
313            uint32_t i, abs_level, run, prev_run, code, len;
314            int32_t level, prev_level;
315    
316          while((v = qcoeff[zigzag[j++]]) == 0);          i       = 1;
317            run = 0;
318    
319          do {          while (i<64 && !(level = qcoeff[zigzag[i++]]))
320                  // count zeroes                  run++;
321                  vlc = table + 64*255 + (clip_table[2048+v] << 6) + j - last;  
322                  last = j + 1;          prev_level = level;
323                  while(j < 64 && (v = qcoeff[zigzag[j++]]) == 0);          prev_run   = run;
324            run = 0;
325                  // write code  
326                  if(j != 64) {          while (i < 64)
327                          BitstreamPutBits(bs, vlc->code, vlc->len);          {
328                  } else {                  if ((level = qcoeff[zigzag[i++]]) != 0)
329                          vlc += 64*511;                  {
330                          BitstreamPutBits(bs, vlc->code, vlc->len);                          abs_level = abs(prev_level);
331                          break;                          abs_level = abs_level < 64 ? abs_level : 0;
332                            code      = coeff_VLC[1][0][abs_level][prev_run].code;
333                            len               = coeff_VLC[1][0][abs_level][prev_run].len;
334                            if (len != 128)
335                                    code |= (prev_level < 0);
336                            else
337                            {
338                            code = (ESCAPE3 << 21) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1;
339                                    len  = 30;
340                            }
341                            BitstreamPutBits(bs, code, len);
342                            prev_level = level;
343                            prev_run   = run;
344                            run = 0;
345                    }
346                    else
347                            run++;
348            }
349    
350            abs_level = abs(prev_level);
351            abs_level = abs_level < 64 ? abs_level : 0;
352            code      = coeff_VLC[1][1][abs_level][prev_run].code;
353            len               = coeff_VLC[1][1][abs_level][prev_run].len;
354            if (len != 128)
355                    code |= (prev_level < 0);
356            else
357            {
358                    code = (ESCAPE3 << 21) | (1 << 20) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1;
359                    len  = 30;
360            }
361            BitstreamPutBits(bs, code, len);
362    }
363    
364    
365    
366    /* returns the number of bits required to encode qcoeff */
367    
368    int
369    CodeCoeffIntra_CalcBits(const int16_t qcoeff[64], const uint16_t * zigzag)
370    {
371            int bits = 0;
372            uint32_t i, abs_level, run, prev_run, len;
373            int32_t level, prev_level;
374    
375            i       = 1;
376            run = 0;
377    
378            while (i<64 && !(level = qcoeff[zigzag[i++]]))
379                    run++;
380    
381            if (i >= 64) return 0;  /* empty block */
382    
383            prev_level = level;
384            prev_run   = run;
385            run = 0;
386    
387            while (i < 64)
388            {
389                    if ((level = qcoeff[zigzag[i++]]) != 0)
390                    {
391                            abs_level = abs(prev_level);
392                            abs_level = abs_level < 64 ? abs_level : 0;
393                            len               = coeff_VLC[1][0][abs_level][prev_run].len;
394                            bits      += len!=128 ? len : 30;
395    
396                            prev_level = level;
397                            prev_run   = run;
398                            run = 0;
399                    }
400                    else
401                            run++;
402                  }                  }
         } while(1);  
403    
404            abs_level = abs(prev_level);
405            abs_level = abs_level < 64 ? abs_level : 0;
406            len               = coeff_VLC[1][1][abs_level][prev_run].len;
407            bits      += len!=128 ? len : 30;
408    
409            return bits;
410  }  }
411    
412    int
413    CodeCoeffInter_CalcBits(const int16_t qcoeff[64], const uint16_t * zigzag)
414    {
415            uint32_t i, run, prev_run, len;
416            int32_t level, prev_level, level_shifted;
417            int bits = 0;
418    
419  static void CodeBlockIntra(const MBParam * pParam,          i       = 0;
420            run = 0;
421    
422            while (!(level = qcoeff[zigzag[i++]]))
423                    run++;
424    
425            prev_level = level;
426            prev_run   = run;
427            run = 0;
428    
429            while (i < 64) {
430                    if ((level = qcoeff[zigzag[i++]]) != 0) {
431                            level_shifted = prev_level + 32;
432                            if (!(level_shifted & -64))
433                                    len      = coeff_VLC[0][0][level_shifted][prev_run].len;
434                            else
435                                    len  = 30;
436    
437                            bits += len;
438                            prev_level = level;
439                            prev_run   = run;
440                            run = 0;
441                    }
442                    else
443                            run++;
444            }
445    
446            level_shifted = prev_level + 32;
447            if (!(level_shifted & -64))
448                    len      = coeff_VLC[0][1][level_shifted][prev_run].len;
449            else
450                    len  = 30;
451            bits += len;
452    
453            return bits;
454    }
455    
456    static int iDQtab[5] = {
457            1, 0, -1 /* no change */, 2, 3
458    };
459    #define DQ_VALUE2INDEX(value)  iDQtab[(value)+2]
460    
461    
462    static __inline void
463    CodeBlockIntra(const FRAMEINFO * const frame,
464                             const MACROBLOCK *pMB,                             const MACROBLOCK *pMB,
465                             int16_t qcoeff[6*64],                             int16_t qcoeff[6*64],
466                             Bitstream * bs,                             Bitstream * bs,
# Line 243  Line 471 
471    
472          cbpy = pMB->cbp >> 2;          cbpy = pMB->cbp >> 2;
473    
474          // write mcbpc          /* write mcbpc */
475          if(pParam->coding_type == I_VOP) {          if (frame->coding_type == I_VOP) {
476                  mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2);                  mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2);
477                  BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, mcbpc_intra_tab[mcbpc].len);                  BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code,
478          }                                                   mcbpc_intra_tab[mcbpc].len);
479          else {          } else {
480                  mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);                  mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);
481                  BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len);                  BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,
482                                                     mcbpc_inter_tab[mcbpc].len);
483          }          }
484    
485          // ac prediction flag          /* ac prediction flag */
486          if(pMB->acpred_directions[0])          if(pMB->acpred_directions[0])
487                  BitstreamPutBits(bs, 1, 1);                  BitstreamPutBits(bs, 1, 1);
488          else          else
489                  BitstreamPutBits(bs, 0, 1);                  BitstreamPutBits(bs, 0, 1);
490    
491          // write cbpy          /* write cbpy */
492          BitstreamPutBits (bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);          BitstreamPutBits(bs, xvid_cbpy_tab[cbpy].code, xvid_cbpy_tab[cbpy].len);
493    
494          // write dquant          /* write dquant */
495          if(pMB->mode == MODE_INTRA_Q)          if(pMB->mode == MODE_INTRA_Q)
496                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, DQ_VALUE2INDEX(pMB->dquant), 2);
497    
498          // write interlacing          /* write interlacing */
499          if (pParam->global_flags & XVID_INTERLACING)          if (frame->vol_flags & XVID_VOL_INTERLACING) {
         {  
500                  BitstreamPutBit(bs, pMB->field_dct);                  BitstreamPutBit(bs, pMB->field_dct);
501          }          }
502            /* code block coeffs */
503          // code block coeffs          for (i = 0; i < 6; i++) {
         for(i = 0; i < 6; i++)  
         {  
504                  if(i < 4)                  if(i < 4)
505                          BitstreamPutBits(bs,                          BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code,
                                          dcy_tab[qcoeff[i*64 + 0] + 255].code,  
506                                           dcy_tab[qcoeff[i*64 + 0] + 255].len);                                           dcy_tab[qcoeff[i*64 + 0] + 255].len);
507                  else                  else
508                          BitstreamPutBits(bs,                          BitstreamPutBits(bs, dcc_tab[qcoeff[i * 64 + 0] + 255].code,
                                          dcc_tab[qcoeff[i*64 + 0] + 255].code,  
509                                           dcc_tab[qcoeff[i*64 + 0] + 255].len);                                           dcc_tab[qcoeff[i*64 + 0] + 255].len);
510    
511                  if(pMB->cbp & (1 << (5 - i)))                  if (pMB->cbp & (1 << (5 - i))) {
512                  {                          const uint16_t *scan_table =
513                                    frame->vop_flags & XVID_VOP_ALTERNATESCAN ?
514                                    scan_tables[2] : scan_tables[pMB->acpred_directions[i]];
515    
516                          bits = BitstreamPos(bs);                          bits = BitstreamPos(bs);
517    
518                          CodeCoeff(bs,                          CodeCoeffIntra(bs, &qcoeff[i * 64], scan_table);
                                   &qcoeff[i*64],  
                                   intra_table,  
                                   scan_tables[pMB->acpred_directions[i]],  
                                   1);  
519    
520                          bits = BitstreamPos(bs) - bits;                          bits = BitstreamPos(bs) - bits;
521                          pStat->iTextBits += bits;                          pStat->iTextBits += bits;
# Line 302  Line 525 
525  }  }
526    
527    
528  static void CodeBlockInter(const MBParam * pParam,  static void
529    CodeBlockInter(const FRAMEINFO * const frame,
530                             const MACROBLOCK *pMB,                             const MACROBLOCK *pMB,
531                             int16_t qcoeff[6*64],                             int16_t qcoeff[6*64],
532                             Bitstream * bs,                             Bitstream * bs,
# Line 315  Line 539 
539          mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);          mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);
540          cbpy = 15 - (pMB->cbp >> 2);          cbpy = 15 - (pMB->cbp >> 2);
541    
542          // write mcbpc          /* write mcbpc */
543          BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len);          BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,
544                                             mcbpc_inter_tab[mcbpc].len);
545    
546            if ( (frame->coding_type == S_VOP) && (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) )
547                    BitstreamPutBit(bs, pMB->mcsel);                /* mcsel: '0'=local motion, '1'=GMC */
548    
549          // write cbpy          /* write cbpy */
550          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);          BitstreamPutBits(bs, xvid_cbpy_tab[cbpy].code, xvid_cbpy_tab[cbpy].len);
551    
552          // write dquant          /* write dquant */
553          if(pMB->mode == MODE_INTER_Q)          if(pMB->mode == MODE_INTER_Q)
554                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, DQ_VALUE2INDEX(pMB->dquant), 2);
555    
556          // interlacing          /* interlacing */
557          if (pParam->global_flags & XVID_INTERLACING)          if (frame->vol_flags & XVID_VOL_INTERLACING) {
558          {                  if (pMB->cbp) {
559                  BitstreamPutBit(bs, pMB->field_dct);                  BitstreamPutBit(bs, pMB->field_dct);
560                  DEBUG1("codep: field_dct: ", pMB->field_dct);                          DPRINTF(XVID_DEBUG_MB,"codep: field_dct: %i\n", pMB->field_dct);
561                    }
562    
563                  // if inter block, write field ME flag                  /* if inter block, write field ME flag */
564                  if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)                  if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) && (pMB->mcsel == 0)) {
565                  {                          BitstreamPutBit(bs, 0 /*pMB->field_pred*/); /* not implemented yet */
566                          BitstreamPutBit(bs, pMB->field_pred);                          DPRINTF(XVID_DEBUG_MB,"codep: field_pred: %i\n", pMB->field_pred);
                         DEBUG1("codep: field_pred: ", pMB->field_pred);  
567    
568                          // write field prediction references                          /* write field prediction references */
569                          if (pMB->field_pred)  #if 0 /* Remove the #if once field_pred is supported */
570                          {                          if (pMB->field_pred) {
571                                  BitstreamPutBit(bs, pMB->field_for_top);                                  BitstreamPutBit(bs, pMB->field_for_top);
572                                  BitstreamPutBit(bs, pMB->field_for_bot);                                  BitstreamPutBit(bs, pMB->field_for_bot);
573                          }                          }
574    #endif
575                  }                  }
576          }          }
577            /* code motion vector(s) if motion is local  */
578          // code motion vector(s)          if (!pMB->mcsel)
579          for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++)                  for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) {
580          {                          CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat);
581                  CodeVector(bs, pMB->pmvs[i].x, pParam->fixed_code, pStat);                          CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat);
                 CodeVector(bs, pMB->pmvs[i].y, pParam->fixed_code, pStat);  
582          }          }
583    
584          bits = BitstreamPos(bs);          bits = BitstreamPos(bs);
585    
586          // code block coeffs          /* code block coeffs */
587          for(i = 0; i < 6; i++)          for(i = 0; i < 6; i++)
588                  if(pMB->cbp & (1 << (5 - i)))                  if (pMB->cbp & (1 << (5 - i))) {
589                          CodeCoeff(bs, &qcoeff[i*64], inter_table, scan_tables[0], 0);                          const uint16_t *scan_table =
590                                    frame->vop_flags & XVID_VOP_ALTERNATESCAN ?
591                                    scan_tables[2] : scan_tables[0];
592    
593                            CodeCoeffInter(bs, &qcoeff[i * 64], scan_table);
594                    }
595    
596          bits = BitstreamPos(bs) - bits;          bits = BitstreamPos(bs) - bits;
597          pStat->iTextBits += bits;          pStat->iTextBits += bits;
   
598  }  }
599    
600    
601  void MBCoding(const MBParam * pParam,  void
602    MBCoding(const FRAMEINFO * const frame,
603                MACROBLOCK *pMB,                MACROBLOCK *pMB,
604                int16_t qcoeff[6*64],                int16_t qcoeff[6*64],
605                Bitstream * bs,                Bitstream * bs,
606                Statistics * pStat)                Statistics * pStat)
607  {  {
608            if (frame->coding_type != I_VOP)
609                            BitstreamPutBit(bs, 0); /* not_coded */
610    
611            if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)
612                    CodeBlockIntra(frame, pMB, qcoeff, bs, pStat);
613            else
614                    CodeBlockInter(frame, pMB, qcoeff, bs, pStat);
615    
616    }
617    
618    /***************************************************************
619     * bframe encoding start
620     ***************************************************************/
621    
622    /*
623            mbtype
624            0       1b              direct(h263)            mvdb
625            1       01b             interpolate mc+q        dbquant, mvdf, mvdb
626            2       001b    backward mc+q           dbquant, mvdb
627            3       0001b   forward mc+q            dbquant, mvdf
628    */
629    
630    static __inline void
631    put_bvop_mbtype(Bitstream * bs,
632                                    int value)
633    {
634            switch (value) {
635                    case MODE_FORWARD:
636                            BitstreamPutBit(bs, 0);
637                    case MODE_BACKWARD:
638                            BitstreamPutBit(bs, 0);
639                    case MODE_INTERPOLATE:
640                            BitstreamPutBit(bs, 0);
641                    case MODE_DIRECT:
642                            BitstreamPutBit(bs, 1);
643                    default:
644                            break;
645            }
646    }
647    
648    /*
649            dbquant
650            -2      10b
651            0       0b
652            +2      11b
653    */
654    
655    static __inline void
656    put_bvop_dbquant(Bitstream * bs,
657                                     int value)
658    {
659            switch (value) {
660            case 0:
661                    BitstreamPutBit(bs, 0);
662                    return;
663    
664            case -2:
665                    BitstreamPutBit(bs, 1);
666                    BitstreamPutBit(bs, 0);
667                    return;
668    
669            case 2:
670                    BitstreamPutBit(bs, 1);
671                    BitstreamPutBit(bs, 1);
672                    return;
673    
674            default:;                                       /* invalid */
675            }
676    }
677    
         int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q);  
678    
679          if(pParam->coding_type == P_VOP) {  
680                  if(pMB->cbp == 0 && pMB->mode == MODE_INTER &&  void
681                     pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0)  MBCodingBVOP(const FRAMEINFO * const frame,
682                             const MACROBLOCK * mb,
683                             const int16_t qcoeff[6 * 64],
684                             const int32_t fcode,
685                             const int32_t bcode,
686                             Bitstream * bs,
687                             Statistics * pStat)
688                  {                  {
689                          BitstreamPutBit(bs, 1);         // not_coded          int vcode = fcode;
690            unsigned int i;
691    
692            const uint16_t *scan_table =
693                    frame->vop_flags & XVID_VOP_ALTERNATESCAN ?
694                    scan_tables[2] : scan_tables[0];
695            int bits;
696    
697    
698    /*      ------------------------------------------------------------------
699                    when a block is skipped it is decoded DIRECT(0,0)
700                    hence is interpolated from forward & backward frames
701            ------------------------------------------------------------------ */
702    
703            if (mb->mode == MODE_DIRECT_NONE_MV) {
704                    BitstreamPutBit(bs, 1); /* skipped */
705                          return;                          return;
706                  }                  }
707                  else  
708                          BitstreamPutBit(bs, 0);         // coded          BitstreamPutBit(bs, 0);         /* not skipped */
709    
710            if (mb->cbp == 0) {
711                    BitstreamPutBit(bs, 1); /* cbp == 0 */
712            } else {
713                    BitstreamPutBit(bs, 0); /* cbp == xxx */
714          }          }
715    
716          if(intra)          put_bvop_mbtype(bs, mb->mode);
                 CodeBlockIntra(pParam, pMB, qcoeff, bs, pStat);  
         else  
                 CodeBlockInter(pParam, pMB, qcoeff, bs, pStat);  
717    
718            if (mb->cbp) {
719                    BitstreamPutBits(bs, mb->cbp, 6);
720  }  }
721    
722            if (mb->mode != MODE_DIRECT && mb->cbp != 0) {
723                    put_bvop_dbquant(bs, 0);        /* todo: mb->dquant = 0 */
724            }
725    
726            if (frame->vol_flags & XVID_VOL_INTERLACING) {
727                    if (mb->cbp) {
728                            BitstreamPutBit(bs, mb->field_dct);
729                            DPRINTF(XVID_DEBUG_MB,"codep: field_dct: %i\n", mb->field_dct);
730                    }
731    
732                    /* if not direct block, write field ME flag */
733                    if (mb->mode != MODE_DIRECT) {
734                            BitstreamPutBit(bs, 0 /*mb->field_pred*/); /* field ME not implemented */
735    
736                            /* write field prediction references */
737    #if 0 /* Remove the #if once field_pred is supported */
738                            if (mb->field_pred) {
739                                    BitstreamPutBit(bs, mb->field_for_top);
740                                    BitstreamPutBit(bs, mb->field_for_bot);
741                            }
742    #endif
743                    }
744            }
745    
746    
747            switch (mb->mode) {
748                    case MODE_INTERPOLATE:
749                            CodeVector(bs, mb->pmvs[1].x, vcode, pStat); /* forward vector of interpolate mode */
750                            CodeVector(bs, mb->pmvs[1].y, vcode, pStat);
751                    case MODE_BACKWARD:
752                            vcode = bcode;
753                    case MODE_FORWARD:
754                            CodeVector(bs, mb->pmvs[0].x, vcode, pStat);
755                            CodeVector(bs, mb->pmvs[0].y, vcode, pStat);
756                            break;
757                    case MODE_DIRECT:
758                            CodeVector(bs, mb->pmvs[3].x, 1, pStat);        /* fcode is always 1 for delta vector */
759                            CodeVector(bs, mb->pmvs[3].y, 1, pStat);        /* prediction is always (0,0) */
760                    default: break;
761            }
762    
763            bits = BitstreamPos(bs);
764            for (i = 0; i < 6; i++) {
765                    if (mb->cbp & (1 << (5 - i))) {
766                            CodeCoeffInter(bs, &qcoeff[i * 64], scan_table);
767                    }
768            }
769            pStat->iTextBits += BitstreamPos(bs) - bits;
770    }
771    
772    
773    
774  /***************************************************************  /***************************************************************
775   * decoding stuff starts here                                  *   * decoding stuff starts here                                  *
776   ***************************************************************/   ***************************************************************/
777    
778  int get_mcbpc_intra(Bitstream * bs)  
779    /*
780     * for IVOP addbits == 0
781     * for PVOP addbits == fcode - 1
782     * for BVOP addbits == max(fcode,bcode) - 1
783     * returns true or false
784     */
785    int
786    check_resync_marker(Bitstream * bs, int addbits)
787    {
788            uint32_t nbits;
789            uint32_t code;
790            uint32_t nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER + addbits;
791    
792            nbits = BitstreamNumBitsToByteAlign(bs);
793            code = BitstreamShowBits(bs, nbits);
794    
795            if (code == (((uint32_t)1 << (nbits - 1)) - 1))
796  {  {
797                    return BitstreamShowBitsFromByteAlign(bs, nbitsresyncmarker) == RESYNC_MARKER;
798            }
799    
800            return 0;
801    }
802    
803    
         uint32_t index;  
804    
805          while((index = BitstreamShowBits(bs, 9)) == 1)  int
806                  BitstreamSkip(bs, 9);  get_mcbpc_intra(Bitstream * bs)
807    {
808    
809            uint32_t index;
810    
811            index = BitstreamShowBits(bs, 9);
812          index >>= 3;          index >>= 3;
813    
814          BitstreamSkip(bs, mcbpc_intra_table[index].len);          BitstreamSkip(bs, mcbpc_intra_table[index].len);
# Line 414  Line 817 
817    
818  }  }
819    
820  int get_mcbpc_inter(Bitstream * bs)  int
821    get_mcbpc_inter(Bitstream * bs)
822  {  {
823    
824          uint32_t index;          uint32_t index;
825    
826          while((index = CLIP(BitstreamShowBits(bs, 9), 256)) == 1)          index = MIN(BitstreamShowBits(bs, 9), 256);
                 BitstreamSkip(bs, 9);  
827    
828          BitstreamSkip(bs,  mcbpc_inter_table[index].len);          BitstreamSkip(bs,  mcbpc_inter_table[index].len);
829    
# Line 428  Line 831 
831    
832  }  }
833    
834  int get_cbpy(Bitstream * bs, int intra)  int
835    get_cbpy(Bitstream * bs,
836                     int intra)
837  {  {
838    
839          int cbpy;          int cbpy;
# Line 444  Line 849 
849    
850  }  }
851    
852  int get_mv_data(Bitstream * bs)  static __inline int
853    get_mv_data(Bitstream * bs)
854  {  {
855    
856          uint32_t index;          uint32_t index;
# Line 454  Line 860 
860    
861          index = BitstreamShowBits(bs, 12);          index = BitstreamShowBits(bs, 12);
862    
863          if(index >= 512)          if (index >= 512) {
         {  
864                  index = (index >> 8) - 2;                  index = (index >> 8) - 2;
865                  BitstreamSkip(bs, TMNMVtab0[index].len);                  BitstreamSkip(bs, TMNMVtab0[index].len);
866                  return TMNMVtab0[index].code;                  return TMNMVtab0[index].code;
867          }          }
868    
869          if(index >= 128)          if (index >= 128) {
         {  
870                  index = (index >> 2) - 32;                  index = (index >> 2) - 32;
871                  BitstreamSkip(bs, TMNMVtab1[index].len);                  BitstreamSkip(bs, TMNMVtab1[index].len);
872                  return TMNMVtab1[index].code;                  return TMNMVtab1[index].code;
# Line 475  Line 879 
879    
880  }  }
881    
882  int get_mv(Bitstream * bs, int fcode)  int
883    get_mv(Bitstream * bs,
884               int fcode)
885  {  {
886    
887          int data;          int data;
# Line 489  Line 895 
895                  return data;                  return data;
896    
897          res = BitstreamGetBits(bs, fcode - 1);          res = BitstreamGetBits(bs, fcode - 1);
898          mv = ((ABS(data) - 1) * scale_fac) + res + 1;          mv = ((abs(data) - 1) * scale_fac) + res + 1;
899    
900          return data < 0 ? -mv : mv;          return data < 0 ? -mv : mv;
901    
902  }  }
903    
904  int get_dc_dif(Bitstream * bs, uint32_t dc_size)  int
905    get_dc_dif(Bitstream * bs,
906                       uint32_t dc_size)
907  {  {
908    
909          int code = BitstreamGetBits(bs, dc_size);          int code = BitstreamGetBits(bs, dc_size);
# Line 508  Line 916 
916    
917  }  }
918    
919  int get_dc_size_lum(Bitstream * bs)  int
920    get_dc_size_lum(Bitstream * bs)
921  {  {
922    
923          int code, i;          int code, i;
924    
925          code = BitstreamShowBits(bs, 11);          code = BitstreamShowBits(bs, 11);
926    
927          for(i = 11; i > 3; i--) {          for(i = 11; i > 3; i--) {
# Line 528  Line 938 
938  }  }
939    
940    
941  int get_dc_size_chrom(Bitstream * bs)  int
942    get_dc_size_chrom(Bitstream * bs)
943  {  {
944    
945          uint32_t code, i;          uint32_t code, i;
946    
947          code = BitstreamShowBits(bs, 12);          code = BitstreamShowBits(bs, 12);
948    
949          for(i = 12; i > 2; i--) {          for(i = 12; i > 2; i--) {
# Line 546  Line 958 
958    
959  }  }
960    
961  int get_coeff(Bitstream * bs, int *run, int *last, int intra, int short_video_header)  #define GET_BITS(cache, n) ((cache)>>(32-(n)))
962    
963    static __inline int
964    get_coeff(Bitstream * bs,
965                      int *run,
966                      int *last,
967                      int intra,
968                      int short_video_header)
969  {  {
970    
971          uint32_t mode;          uint32_t mode;
         const VLC *tab;  
972          int32_t level;          int32_t level;
973            REVERSE_EVENT *reverse_event;
974    
975            uint32_t cache = BitstreamShowBits(bs, 32);
976    
977          if(short_video_header) // inter-VLCs will be used for both intra and inter blocks          if (short_video_header)         /* inter-VLCs will be used for both intra and inter blocks */
978                  intra = 0;                  intra = 0;
979    
980          tab = &DCT3D[intra][BitstreamShowBits(bs, 12)];          if (GET_BITS(cache, 7) != ESCAPE) {
981                    reverse_event = &DCT3D[intra][GET_BITS(cache, 12)];
982    
983          if(tab->code == -1)                  if ((level = reverse_event->event.level) == 0)
984                  goto error;                  goto error;
985    
986          BitstreamSkip(bs, tab->len);                  *last = reverse_event->event.last;
987                    *run  = reverse_event->event.run;
988    
989          if(tab->code != ESCAPE) {                  /* Don't forget to update the bitstream position */
990                  if(!intra)                  BitstreamSkip(bs, reverse_event->len+1);
991                  {  
992                          *run = (tab->code >> 4) & 255;                  return (GET_BITS(cache, reverse_event->len+1)&0x01) ? -level : level;
                         level = tab->code & 15;  
                         *last = (tab->code >> 12) & 1;  
                 }  
                 else  
                 {  
                         *run = (tab->code >> 8) & 255;  
                         level = tab->code & 255;  
                         *last = (tab->code >> 16) & 1;  
                 }  
                 return BitstreamGetBit(bs) ? -level : level;  
993          }          }
994    
995          if(short_video_header)          /* flush 7bits of cache */
996          {          cache <<= 7;
997                  // escape mode 4 - H.263 type, only used if short_video_header = 1  
998                  *last = BitstreamGetBit(bs);          if (short_video_header) {
999                  *run = BitstreamGetBits(bs, 6);                  /* escape mode 4 - H.263 type, only used if short_video_header = 1  */
1000                  level = BitstreamGetBits(bs, 8);                  *last =  GET_BITS(cache, 1);
1001                    *run  = (GET_BITS(cache, 7) &0x3f);
1002                    level = (GET_BITS(cache, 15)&0xff);
1003    
1004                  if (level == 0 || level == 128)                  if (level == 0 || level == 128)
1005                          DEBUG1("Illegal LEVEL for ESCAPE mode 4:", level);                          DPRINTF(XVID_DEBUG_ERROR, "Illegal LEVEL for ESCAPE mode 4: %d\n", level);
1006    
1007                    /* We've "eaten" 22 bits */
1008                    BitstreamSkip(bs, 22);
1009    
1010                  return (level >= 128 ? -(256 - level) : level);                  return (level << 24) >> 24;
1011          }          }
1012    
1013          mode = BitstreamShowBits(bs, 2);          if ((mode = GET_BITS(cache, 2)) < 3) {
1014                    const int skip[3] = {1, 1, 2};
1015                    cache <<= skip[mode];
1016    
1017          if(mode < 3) {                  reverse_event = &DCT3D[intra][GET_BITS(cache, 12)];
                 BitstreamSkip(bs, (mode == 2) ? 2 : 1);  
1018    
1019                  tab = &DCT3D[intra][BitstreamShowBits(bs, 12)];                  if ((level = reverse_event->event.level) == 0)
                 if (tab->code == -1)  
1020                          goto error;                          goto error;
1021    
1022                  BitstreamSkip(bs, tab->len);                  *last = reverse_event->event.last;
1023                    *run  = reverse_event->event.run;
1024    
1025                  if (!intra) {                  if (mode < 2) {
1026                          *run = (tab->code >> 4) & 255;                          /* first escape mode, level is offset */
1027                          level = tab->code & 15;                          level += max_level[intra][*last][*run];
1028                          *last = (tab->code >> 12) & 1;                  } else {
1029                  }                          /* second escape mode, run is offset */
1030                  else                          *run += max_run[intra][*last][level] + 1;
                 {  
                         *run = (tab->code >> 8) & 255;  
                         level = tab->code & 255;  
                         *last = (tab->code >> 16) & 1;  
1031                  }                  }
1032    
1033                  if(mode < 2) // first escape mode, level is offset                  /* Update bitstream position */
1034                          level += max_level[*last + (!intra<<1)][*run]; // need to add back the max level                  BitstreamSkip(bs, 7 + skip[mode] + reverse_event->len + 1);
                 else if(mode == 2)  // second escape mode, run is offset  
                         *run += max_run[*last + (!intra<<1)][level] + 1;  
1035    
1036                  return BitstreamGetBit(bs) ? -level : level;                  return (GET_BITS(cache, reverse_event->len+1)&0x01) ? -level : level;
1037          }          }
1038    
1039          // third escape mode - fixed length codes          /* third escape mode - fixed length codes */
1040          BitstreamSkip(bs, 2);          cache <<= 2;
1041          *last = BitstreamGetBits(bs, 1);          *last =  GET_BITS(cache, 1);
1042          *run = BitstreamGetBits(bs, 6);          *run  = (GET_BITS(cache, 7)&0x3f);
1043          BitstreamSkip(bs, 1);                           // marker          level = (GET_BITS(cache, 20)&0xfff);
1044          level = BitstreamGetBits(bs, 12);  
1045          BitstreamSkip(bs, 1);                           // marker          /* Update bitstream position */
1046            BitstreamSkip(bs, 30);
1047    
1048          return (level & 0x800) ? (level | (-1 ^ 0xfff)) : level;          return (level << 20) >> 20;
1049    
1050   error:   error:
1051          *run = VLC_ERROR;          *run = VLC_ERROR;
1052          return 0;          return 0;
   
1053  }  }
1054    
1055    void
1056  void get_intra_block(Bitstream * bs, int16_t * block, int direction, int coeff)  get_intra_block(Bitstream * bs,
1057                                    int16_t * block,
1058                                    int direction,
1059                                    int coeff)
1060  {  {
1061    
1062          const uint16_t * scan = scan_tables[ direction ];          const uint16_t * scan = scan_tables[ direction ];
1063          int level;          int level, run, last;
         int run;  
         int last;  
1064    
1065          do          do {
         {  
1066                  level = get_coeff(bs, &run, &last, 1, 0);                  level = get_coeff(bs, &run, &last, 1, 0);
1067                  if (run == -1)                  if (run == -1) {
1068                  {                          DPRINTF(XVID_DEBUG_ERROR,"fatal: invalid run");
                         DEBUG("fatal: invalid run");  
1069                          break;                          break;
1070                  }                  }
1071                  coeff += run;                  coeff += run;
1072                  block[ scan[coeff] ] = level;                  block[ scan[coeff] ] = level;
1073                  if (level < -127 || level > 127)  
1074                  {                  DPRINTF(XVID_DEBUG_COEFF,"block[%i] %i\n", scan[coeff], level);
1075                          DEBUG1("warning: intra_overflow", level);  #if 0
1076                    DPRINTF(XVID_DEBUG_COEFF,"block[%i] %i %08x\n", scan[coeff], level, BitstreamShowBits(bs, 32));
1077    #endif
1078    
1079                    if (level < -2047 || level > 2047) {
1080                            DPRINTF(XVID_DEBUG_ERROR,"warning: intra_overflow %i\n", level);
1081                  }                  }
1082                  coeff++;                  coeff++;
1083          } while (!last);          } while (!last);
1084    
1085  }  }
1086    
1087  void get_inter_block(Bitstream * bs, int16_t * block)  void
1088    get_inter_block_h263(
1089                    Bitstream * bs,
1090                    int16_t * block,
1091                    int direction,
1092                    const int quant,
1093                    const uint16_t *matrix)
1094  {  {
1095    
1096          const uint16_t * scan = scan_tables[0];          const uint16_t *scan = scan_tables[direction];
1097            const uint16_t quant_m_2 = quant << 1;
1098            const uint16_t quant_add = (quant & 1 ? quant : quant - 1);
1099          int p;          int p;
1100          int level;          int level;
1101          int run;          int run;
1102          int last;          int last;
1103    
1104          p = 0;          p = 0;
1105          do          do {
         {  
1106                  level = get_coeff(bs, &run, &last, 0, 0);                  level = get_coeff(bs, &run, &last, 0, 0);
1107                  if (run == -1)                  if (run == -1) {
1108                  {                          DPRINTF(XVID_DEBUG_ERROR,"fatal: invalid run");
                         DEBUG("fatal: invalid run");  
1109                          break;                          break;
1110                  }                  }
1111                  p += run;                  p += run;
1112                  block[ scan[p] ] = level;  
1113                  if (level < -127 || level > 127)                  if (level < 0) {
1114                  {                          level = level*quant_m_2 - quant_add;
1115                          DEBUG1("warning: inter_overflow", level);                          block[scan[p]] = (level >= -2048 ? level : -2048);
1116                    } else {
1117                            level = level * quant_m_2 + quant_add;
1118                            block[scan[p]] = (level <= 2047 ? level : 2047);
1119                  }                  }
1120                  p++;                  p++;
1121          } while (!last);          } while (!last);
1122    }
1123    
1124    void
1125    get_inter_block_mpeg(
1126                    Bitstream * bs,
1127                    int16_t * block,
1128                    int direction,
1129                    const int quant,
1130                    const uint16_t *matrix)
1131    {
1132            const uint16_t *scan = scan_tables[direction];
1133            uint32_t sum = 0;
1134            int p;
1135            int level;
1136            int run;
1137            int last;
1138    
1139            p = 0;
1140            do {
1141                    level = get_coeff(bs, &run, &last, 0, 0);
1142                    if (run == -1) {
1143                            DPRINTF(XVID_DEBUG_ERROR,"fatal: invalid run");
1144                            break;
1145  }  }
1146                    p += run;
1147    
1148                    if (level < 0) {
1149                            level = ((2 * -level + 1) * matrix[scan[p]] * quant) >> 4;
1150                            block[scan[p]] = (level <= 2048 ? -level : -2048);
1151                    } else {
1152                            level = ((2 *  level + 1) * matrix[scan[p]] * quant) >> 4;
1153                            block[scan[p]] = (level <= 2047 ? level : 2047);
1154                    }
1155    
1156                    sum ^= block[scan[p]];
1157    
1158                    p++;
1159            } while (!last);
1160    
1161            /*      mismatch control */
1162            if ((sum & 1) == 0) {
1163                    block[63] ^= 1;
1164            }
1165    }
1166    
1167    
1168    /*****************************************************************************
1169     * VLC tables and other constant arrays
1170     ****************************************************************************/
1171    
1172    VLC_TABLE const coeff_tab[2][102] =
1173    {
1174            /* intra = 0 */
1175            {
1176                    {{ 2,  2}, {0, 0, 1}},
1177                    {{15,  4}, {0, 0, 2}},
1178                    {{21,  6}, {0, 0, 3}},
1179                    {{23,  7}, {0, 0, 4}},
1180                    {{31,  8}, {0, 0, 5}},
1181                    {{37,  9}, {0, 0, 6}},
1182                    {{36,  9}, {0, 0, 7}},
1183                    {{33, 10}, {0, 0, 8}},
1184                    {{32, 10}, {0, 0, 9}},
1185                    {{ 7, 11}, {0, 0, 10}},
1186                    {{ 6, 11}, {0, 0, 11}},
1187                    {{32, 11}, {0, 0, 12}},
1188                    {{ 6,  3}, {0, 1, 1}},
1189                    {{20,  6}, {0, 1, 2}},
1190                    {{30,  8}, {0, 1, 3}},
1191                    {{15, 10}, {0, 1, 4}},
1192                    {{33, 11}, {0, 1, 5}},
1193                    {{80, 12}, {0, 1, 6}},
1194                    {{14,  4}, {0, 2, 1}},
1195                    {{29,  8}, {0, 2, 2}},
1196                    {{14, 10}, {0, 2, 3}},
1197                    {{81, 12}, {0, 2, 4}},
1198                    {{13,  5}, {0, 3, 1}},
1199                    {{35,  9}, {0, 3, 2}},
1200                    {{13, 10}, {0, 3, 3}},
1201                    {{12,  5}, {0, 4, 1}},
1202                    {{34,  9}, {0, 4, 2}},
1203                    {{82, 12}, {0, 4, 3}},
1204                    {{11,  5}, {0, 5, 1}},
1205                    {{12, 10}, {0, 5, 2}},
1206                    {{83, 12}, {0, 5, 3}},
1207                    {{19,  6}, {0, 6, 1}},
1208                    {{11, 10}, {0, 6, 2}},
1209                    {{84, 12}, {0, 6, 3}},
1210                    {{18,  6}, {0, 7, 1}},
1211                    {{10, 10}, {0, 7, 2}},
1212                    {{17,  6}, {0, 8, 1}},
1213                    {{ 9, 10}, {0, 8, 2}},
1214                    {{16,  6}, {0, 9, 1}},
1215                    {{ 8, 10}, {0, 9, 2}},
1216                    {{22,  7}, {0, 10, 1}},
1217                    {{85, 12}, {0, 10, 2}},
1218                    {{21,  7}, {0, 11, 1}},
1219                    {{20,  7}, {0, 12, 1}},
1220                    {{28,  8}, {0, 13, 1}},
1221                    {{27,  8}, {0, 14, 1}},
1222                    {{33,  9}, {0, 15, 1}},
1223                    {{32,  9}, {0, 16, 1}},
1224                    {{31,  9}, {0, 17, 1}},
1225                    {{30,  9}, {0, 18, 1}},
1226                    {{29,  9}, {0, 19, 1}},
1227                    {{28,  9}, {0, 20, 1}},
1228                    {{27,  9}, {0, 21, 1}},
1229                    {{26,  9}, {0, 22, 1}},
1230                    {{34, 11}, {0, 23, 1}},
1231                    {{35, 11}, {0, 24, 1}},
1232                    {{86, 12}, {0, 25, 1}},
1233                    {{87, 12}, {0, 26, 1}},
1234                    {{ 7,  4}, {1, 0, 1}},
1235                    {{25,  9}, {1, 0, 2}},
1236                    {{ 5, 11}, {1, 0, 3}},
1237                    {{15,  6}, {1, 1, 1}},
1238                    {{ 4, 11}, {1, 1, 2}},
1239                    {{14,  6}, {1, 2, 1}},
1240                    {{13,  6}, {1, 3, 1}},
1241                    {{12,  6}, {1, 4, 1}},
1242                    {{19,  7}, {1, 5, 1}},
1243                    {{18,  7}, {1, 6, 1}},
1244                    {{17,  7}, {1, 7, 1}},
1245                    {{16,  7}, {1, 8, 1}},
1246                    {{26,  8}, {1, 9, 1}},
1247                    {{25,  8}, {1, 10, 1}},
1248                    {{24,  8}, {1, 11, 1}},
1249                    {{23,  8}, {1, 12, 1}},
1250                    {{22,  8}, {1, 13, 1}},
1251                    {{21,  8}, {1, 14, 1}},
1252                    {{20,  8}, {1, 15, 1}},
1253                    {{19,  8}, {1, 16, 1}},
1254                    {{24,  9}, {1, 17, 1}},
1255                    {{23,  9}, {1, 18, 1}},
1256                    {{22,  9}, {1, 19, 1}},
1257                    {{21,  9}, {1, 20, 1}},
1258                    {{20,  9}, {1, 21, 1}},
1259                    {{19,  9}, {1, 22, 1}},
1260                    {{18,  9}, {1, 23, 1}},
1261                    {{17,  9}, {1, 24, 1}},
1262                    {{ 7, 10}, {1, 25, 1}},
1263                    {{ 6, 10}, {1, 26, 1}},
1264                    {{ 5, 10}, {1, 27, 1}},
1265                    {{ 4, 10}, {1, 28, 1}},
1266                    {{36, 11}, {1, 29, 1}},
1267                    {{37, 11}, {1, 30, 1}},
1268                    {{38, 11}, {1, 31, 1}},
1269                    {{39, 11}, {1, 32, 1}},
1270                    {{88, 12}, {1, 33, 1}},
1271                    {{89, 12}, {1, 34, 1}},
1272                    {{90, 12}, {1, 35, 1}},
1273                    {{91, 12}, {1, 36, 1}},
1274                    {{92, 12}, {1, 37, 1}},
1275                    {{93, 12}, {1, 38, 1}},
1276                    {{94, 12}, {1, 39, 1}},
1277                    {{95, 12}, {1, 40, 1}}
1278            },
1279            /* intra = 1 */
1280            {
1281                    {{ 2,  2}, {0, 0, 1}},
1282                    {{15,  4}, {0, 0, 3}},
1283                    {{21,  6}, {0, 0, 6}},
1284                    {{23,  7}, {0, 0, 9}},
1285                    {{31,  8}, {0, 0, 10}},
1286                    {{37,  9}, {0, 0, 13}},
1287                    {{36,  9}, {0, 0, 14}},
1288                    {{33, 10}, {0, 0, 17}},
1289                    {{32, 10}, {0, 0, 18}},
1290                    {{ 7, 11}, {0, 0, 21}},
1291                    {{ 6, 11}, {0, 0, 22}},
1292                    {{32, 11}, {0, 0, 23}},
1293                    {{ 6,  3}, {0, 0, 2}},
1294                    {{20,  6}, {0, 1, 2}},
1295                    {{30,  8}, {0, 0, 11}},
1296                    {{15, 10}, {0, 0, 19}},
1297                    {{33, 11}, {0, 0, 24}},
1298                    {{80, 12}, {0, 0, 25}},
1299                    {{14,  4}, {0, 1, 1}},
1300                    {{29,  8}, {0, 0, 12}},
1301                    {{14, 10}, {0, 0, 20}},
1302                    {{81, 12}, {0, 0, 26}},
1303                    {{13,  5}, {0, 0, 4}},
1304                    {{35,  9}, {0, 0, 15}},
1305                    {{13, 10}, {0, 1, 7}},
1306                    {{12,  5}, {0, 0, 5}},
1307                    {{34,  9}, {0, 4, 2}},
1308                    {{82, 12}, {0, 0, 27}},
1309                    {{11,  5}, {0, 2, 1}},
1310                    {{12, 10}, {0, 2, 4}},
1311                    {{83, 12}, {0, 1, 9}},
1312                    {{19,  6}, {0, 0, 7}},
1313                    {{11, 10}, {0, 3, 4}},
1314                    {{84, 12}, {0, 6, 3}},
1315                    {{18,  6}, {0, 0, 8}},
1316                    {{10, 10}, {0, 4, 3}},
1317                    {{17,  6}, {0, 3, 1}},
1318                    {{ 9, 10}, {0, 8, 2}},
1319                    {{16,  6}, {0, 4, 1}},
1320                    {{ 8, 10}, {0, 5, 3}},
1321                    {{22,  7}, {0, 1, 3}},
1322                    {{85, 12}, {0, 1, 10}},
1323                    {{21,  7}, {0, 2, 2}},
1324                    {{20,  7}, {0, 7, 1}},
1325                    {{28,  8}, {0, 1, 4}},
1326                    {{27,  8}, {0, 3, 2}},
1327                    {{33,  9}, {0, 0, 16}},
1328                    {{32,  9}, {0, 1, 5}},
1329                    {{31,  9}, {0, 1, 6}},
1330                    {{30,  9}, {0, 2, 3}},
1331                    {{29,  9}, {0, 3, 3}},
1332                    {{28,  9}, {0, 5, 2}},
1333                    {{27,  9}, {0, 6, 2}},
1334                    {{26,  9}, {0, 7, 2}},
1335                    {{34, 11}, {0, 1, 8}},
1336                    {{35, 11}, {0, 9, 2}},
1337                    {{86, 12}, {0, 2, 5}},
1338                    {{87, 12}, {0, 7, 3}},
1339                    {{ 7,  4}, {1, 0, 1}},
1340                    {{25,  9}, {0, 11, 1}},
1341                    {{ 5, 11}, {1, 0, 6}},
1342                    {{15,  6}, {1, 1, 1}},
1343                    {{ 4, 11}, {1, 0, 7}},
1344                    {{14,  6}, {1, 2, 1}},
1345                    {{13,  6}, {0, 5, 1}},
1346                    {{12,  6}, {1, 0, 2}},
1347                    {{19,  7}, {1, 5, 1}},
1348                    {{18,  7}, {0, 6, 1}},
1349                    {{17,  7}, {1, 3, 1}},
1350                    {{16,  7}, {1, 4, 1}},
1351                    {{26,  8}, {1, 9, 1}},
1352                    {{25,  8}, {0, 8, 1}},
1353                    {{24,  8}, {0, 9, 1}},
1354                    {{23,  8}, {0, 10, 1}},
1355                    {{22,  8}, {1, 0, 3}},
1356                    {{21,  8}, {1, 6, 1}},
1357                    {{20,  8}, {1, 7, 1}},
1358                    {{19,  8}, {1, 8, 1}},
1359                    {{24,  9}, {0, 12, 1}},
1360                    {{23,  9}, {1, 0, 4}},
1361                    {{22,  9}, {1, 1, 2}},
1362                    {{21,  9}, {1, 10, 1}},
1363                    {{20,  9}, {1, 11, 1}},
1364                    {{19,  9}, {1, 12, 1}},
1365                    {{18,  9}, {1, 13, 1}},
1366                    {{17,  9}, {1, 14, 1}},
1367                    {{ 7, 10}, {0, 13, 1}},
1368                    {{ 6, 10}, {1, 0, 5}},
1369                    {{ 5, 10}, {1, 1, 3}},
1370                    {{ 4, 10}, {1, 2, 2}},
1371                    {{36, 11}, {1, 3, 2}},
1372                    {{37, 11}, {1, 4, 2}},
1373                    {{38, 11}, {1, 15, 1}},
1374                    {{39, 11}, {1, 16, 1}},
1375                    {{88, 12}, {0, 14, 1}},
1376                    {{89, 12}, {1, 0, 8}},
1377                    {{90, 12}, {1, 5, 2}},
1378                    {{91, 12}, {1, 6, 2}},
1379                    {{92, 12}, {1, 17, 1}},
1380                    {{93, 12}, {1, 18, 1}},
1381                    {{94, 12}, {1, 19, 1}},
1382                    {{95, 12}, {1, 20, 1}}
1383            }
1384    };
1385    
1386    /* constants taken from momusys/vm_common/inlcude/max_level.h */
1387    uint8_t const max_level[2][2][64] = {
1388            {
1389                    /* intra = 0, last = 0 */
1390                    {
1391                            12, 6, 4, 3, 3, 3, 3, 2,
1392                            2, 2, 2, 1, 1, 1, 1, 1,
1393                            1, 1, 1, 1, 1, 1, 1, 1,
1394                            1, 1, 1, 0, 0, 0, 0, 0,
1395                            0, 0, 0, 0, 0, 0, 0, 0,
1396                            0, 0, 0, 0, 0, 0, 0, 0,
1397                            0, 0, 0, 0, 0, 0, 0, 0,
1398                            0, 0, 0, 0, 0, 0, 0, 0
1399                    },
1400                    /* intra = 0, last = 1 */
1401                    {
1402                            3, 2, 1, 1, 1, 1, 1, 1,
1403                            1, 1, 1, 1, 1, 1, 1, 1,
1404                            1, 1, 1, 1, 1, 1, 1, 1,
1405                            1, 1, 1, 1, 1, 1, 1, 1,
1406                            1, 1, 1, 1, 1, 1, 1, 1,
1407                            1, 0, 0, 0, 0, 0, 0, 0,
1408                            0, 0, 0, 0, 0, 0, 0, 0,
1409                            0, 0, 0, 0, 0, 0, 0, 0
1410                    }
1411            },
1412            {
1413                    /* intra = 1, last = 0 */
1414                    {
1415                            27, 10, 5, 4, 3, 3, 3, 3,
1416                            2, 2, 1, 1, 1, 1, 1, 0,
1417                            0, 0, 0, 0, 0, 0, 0, 0,
1418                            0, 0, 0, 0, 0, 0, 0, 0,
1419                            0, 0, 0, 0, 0, 0, 0, 0,
1420                            0, 0, 0, 0, 0, 0, 0, 0,
1421                            0, 0, 0, 0, 0, 0, 0, 0,
1422                            0, 0, 0, 0, 0, 0, 0, 0
1423                    },
1424                    /* intra = 1, last = 1 */
1425                    {
1426                            8, 3, 2, 2, 2, 2, 2, 1,
1427                            1, 1, 1, 1, 1, 1, 1, 1,
1428                            1, 1, 1, 1, 1, 0, 0, 0,
1429                            0, 0, 0, 0, 0, 0, 0, 0,
1430                            0, 0, 0, 0, 0, 0, 0, 0,
1431                            0, 0, 0, 0, 0, 0, 0, 0,
1432                            0, 0, 0, 0, 0, 0, 0, 0,
1433                            0, 0, 0, 0, 0, 0, 0, 0
1434                    }
1435            }
1436    };
1437    
1438    uint8_t const max_run[2][2][64] = {
1439            {
1440                    /* intra = 0, last = 0 */
1441                    {
1442                            0, 26, 10, 6, 2, 1, 1, 0,
1443                            0, 0, 0, 0, 0, 0, 0, 0,
1444                            0, 0, 0, 0, 0, 0, 0, 0,
1445                            0, 0, 0, 0, 0, 0, 0, 0,
1446                            0, 0, 0, 0, 0, 0, 0, 0,
1447                            0, 0, 0, 0, 0, 0, 0, 0,
1448                            0, 0, 0, 0, 0, 0, 0, 0,
1449                            0, 0, 0, 0, 0, 0, 0, 0,
1450                    },
1451                    /* intra = 0, last = 1 */
1452                    {
1453                            0, 40, 1, 0, 0, 0, 0, 0,
1454                            0, 0, 0, 0, 0, 0, 0, 0,
1455                            0, 0, 0, 0, 0, 0, 0, 0,
1456                            0, 0, 0, 0, 0, 0, 0, 0,
1457                            0, 0, 0, 0, 0, 0, 0, 0,
1458                            0, 0, 0, 0, 0, 0, 0, 0,
1459                            0, 0, 0, 0, 0, 0, 0, 0,
1460                            0, 0, 0, 0, 0, 0, 0, 0,
1461                    }
1462            },
1463            {
1464                    /* intra = 1, last = 0 */
1465                    {
1466                            0, 14, 9, 7, 3, 2, 1, 1,
1467                            1, 1, 1, 0, 0, 0, 0, 0,
1468                            0, 0, 0, 0, 0, 0, 0, 0,
1469                            0, 0, 0, 0, 0, 0, 0, 0,
1470                            0, 0, 0, 0, 0, 0, 0, 0,
1471                            0, 0, 0, 0, 0, 0, 0, 0,
1472                            0, 0, 0, 0, 0, 0, 0, 0,
1473                            0, 0, 0, 0, 0, 0, 0, 0,
1474                    },
1475                    /* intra = 1, last = 1 */
1476                    {
1477                            0, 20, 6, 1, 0, 0, 0, 0,
1478                            0, 0, 0, 0, 0, 0, 0, 0,
1479                            0, 0, 0, 0, 0, 0, 0, 0,
1480                            0, 0, 0, 0, 0, 0, 0, 0,
1481                            0, 0, 0, 0, 0, 0, 0, 0,
1482                            0, 0, 0, 0, 0, 0, 0, 0,
1483                            0, 0, 0, 0, 0, 0, 0, 0,
1484                            0, 0, 0, 0, 0, 0, 0, 0,
1485                    }
1486            }
1487    };
1488    
1489    /******************************************************************
1490     * encoder tables                                                 *
1491     ******************************************************************/
1492    
1493    VLC sprite_trajectory_code[32768];
1494    
1495    VLC sprite_trajectory_len[15] = {
1496            { 0x00 , 2},
1497            { 0x02 , 3}, { 0x03, 3}, { 0x04, 3}, { 0x05, 3}, { 0x06, 3},
1498            { 0x0E , 4}, { 0x1E, 5}, { 0x3E, 6}, { 0x7E, 7}, { 0xFE, 8},
1499            { 0x1FE, 9}, {0x3FE,10}, {0x7FE,11}, {0xFFE,12} };
1500    
1501    
1502    /* DCT coefficients. Four tables, two for last = 0, two for last = 1.
1503       the sign bit must be added afterwards. */
1504    
1505    /* MCBPC Indexing by cbpc in first two bits, mode in last two.
1506     CBPC as in table 4/H.263, MB type (mode): 3 = 01, 4 = 10.
1507     Example: cbpc = 01 and mode = 4 gives index = 0110 = 6. */
1508    
1509    VLC mcbpc_intra_tab[15] = {
1510            {0x01, 9}, {0x01, 1}, {0x01, 4}, {0x00, 0},
1511            {0x00, 0}, {0x01, 3}, {0x01, 6}, {0x00, 0},
1512            {0x00, 0}, {0x02, 3}, {0x02, 6}, {0x00, 0},
1513            {0x00, 0}, {0x03, 3}, {0x03, 6}
1514    };
1515    
1516    /* MCBPC inter.
1517       Addressing: 5 bit ccmmm (cc = CBPC, mmm = mode (1-4 binary)) */
1518    
1519    VLC mcbpc_inter_tab[29] = {
1520            {1, 1}, {3, 3}, {2, 3}, {3, 5}, {4, 6}, {1, 9}, {0, 0}, {0, 0},
1521            {3, 4}, {7, 7}, {5, 7}, {4, 8}, {4, 9}, {0, 0}, {0, 0}, {0, 0},
1522            {2, 4}, {6, 7}, {4, 7}, {3, 8}, {3, 9}, {0, 0}, {0, 0}, {0, 0},
1523            {5, 6}, {5, 9}, {5, 8}, {3, 7}, {2, 9}
1524    };
1525    
1526    const VLC xvid_cbpy_tab[16] = {
1527            {3, 4}, {5, 5}, {4, 5}, {9, 4}, {3, 5}, {7, 4}, {2, 6}, {11, 4},
1528            {2, 5}, {3, 6}, {5, 4}, {10, 4}, {4, 4}, {8, 4}, {6, 4}, {3, 2}
1529    };
1530    
1531    const VLC dcy_tab[511] = {
1532            {0x100, 15}, {0x101, 15}, {0x102, 15}, {0x103, 15},
1533            {0x104, 15}, {0x105, 15}, {0x106, 15}, {0x107, 15},
1534            {0x108, 15}, {0x109, 15}, {0x10a, 15}, {0x10b, 15},
1535            {0x10c, 15}, {0x10d, 15}, {0x10e, 15}, {0x10f, 15},
1536            {0x110, 15}, {0x111, 15}, {0x112, 15}, {0x113, 15},
1537            {0x114, 15}, {0x115, 15}, {0x116, 15}, {0x117, 15},
1538            {0x118, 15}, {0x119, 15}, {0x11a, 15}, {0x11b, 15},
1539            {0x11c, 15}, {0x11d, 15}, {0x11e, 15}, {0x11f, 15},
1540            {0x120, 15}, {0x121, 15}, {0x122, 15}, {0x123, 15},
1541            {0x124, 15}, {0x125, 15}, {0x126, 15}, {0x127, 15},
1542            {0x128, 15}, {0x129, 15}, {0x12a, 15}, {0x12b, 15},
1543            {0x12c, 15}, {0x12d, 15}, {0x12e, 15}, {0x12f, 15},
1544            {0x130, 15}, {0x131, 15}, {0x132, 15}, {0x133, 15},
1545            {0x134, 15}, {0x135, 15}, {0x136, 15}, {0x137, 15},
1546            {0x138, 15}, {0x139, 15}, {0x13a, 15}, {0x13b, 15},
1547            {0x13c, 15}, {0x13d, 15}, {0x13e, 15}, {0x13f, 15},
1548            {0x140, 15}, {0x141, 15}, {0x142, 15}, {0x143, 15},
1549            {0x144, 15}, {0x145, 15}, {0x146, 15}, {0x147, 15},
1550            {0x148, 15}, {0x149, 15}, {0x14a, 15}, {0x14b, 15},
1551            {0x14c, 15}, {0x14d, 15}, {0x14e, 15}, {0x14f, 15},
1552            {0x150, 15}, {0x151, 15}, {0x152, 15}, {0x153, 15},
1553            {0x154, 15}, {0x155, 15}, {0x156, 15}, {0x157, 15},
1554            {0x158, 15}, {0x159, 15}, {0x15a, 15}, {0x15b, 15},
1555            {0x15c, 15}, {0x15d, 15}, {0x15e, 15}, {0x15f, 15},
1556            {0x160, 15}, {0x161, 15}, {0x162, 15}, {0x163, 15},
1557            {0x164, 15}, {0x165, 15}, {0x166, 15}, {0x167, 15},
1558            {0x168, 15}, {0x169, 15}, {0x16a, 15}, {0x16b, 15},
1559            {0x16c, 15}, {0x16d, 15}, {0x16e, 15}, {0x16f, 15},
1560            {0x170, 15}, {0x171, 15}, {0x172, 15}, {0x173, 15},
1561            {0x174, 15}, {0x175, 15}, {0x176, 15}, {0x177, 15},
1562            {0x178, 15}, {0x179, 15}, {0x17a, 15}, {0x17b, 15},
1563            {0x17c, 15}, {0x17d, 15}, {0x17e, 15}, {0x17f, 15},
1564            {0x80, 13}, {0x81, 13}, {0x82, 13}, {0x83, 13},
1565            {0x84, 13}, {0x85, 13}, {0x86, 13}, {0x87, 13},
1566            {0x88, 13}, {0x89, 13}, {0x8a, 13}, {0x8b, 13},
1567            {0x8c, 13}, {0x8d, 13}, {0x8e, 13}, {0x8f, 13},
1568            {0x90, 13}, {0x91, 13}, {0x92, 13}, {0x93, 13},
1569            {0x94, 13}, {0x95, 13}, {0x96, 13}, {0x97, 13},
1570            {0x98, 13}, {0x99, 13}, {0x9a, 13}, {0x9b, 13},
1571            {0x9c, 13}, {0x9d, 13}, {0x9e, 13}, {0x9f, 13},
1572            {0xa0, 13}, {0xa1, 13}, {0xa2, 13}, {0xa3, 13},
1573            {0xa4, 13}, {0xa5, 13}, {0xa6, 13}, {0xa7, 13},
1574            {0xa8, 13}, {0xa9, 13}, {0xaa, 13}, {0xab, 13},
1575            {0xac, 13}, {0xad, 13}, {0xae, 13}, {0xaf, 13},
1576            {0xb0, 13}, {0xb1, 13}, {0xb2, 13}, {0xb3, 13},
1577            {0xb4, 13}, {0xb5, 13}, {0xb6, 13}, {0xb7, 13},
1578            {0xb8, 13}, {0xb9, 13}, {0xba, 13}, {0xbb, 13},
1579            {0xbc, 13}, {0xbd, 13}, {0xbe, 13}, {0xbf, 13},
1580            {0x40, 11}, {0x41, 11}, {0x42, 11}, {0x43, 11},
1581            {0x44, 11}, {0x45, 11}, {0x46, 11}, {0x47, 11},
1582            {0x48, 11}, {0x49, 11}, {0x4a, 11}, {0x4b, 11},
1583            {0x4c, 11}, {0x4d, 11}, {0x4e, 11}, {0x4f, 11},
1584            {0x50, 11}, {0x51, 11}, {0x52, 11}, {0x53, 11},
1585            {0x54, 11}, {0x55, 11}, {0x56, 11}, {0x57, 11},
1586            {0x58, 11}, {0x59, 11}, {0x5a, 11}, {0x5b, 11},
1587            {0x5c, 11}, {0x5d, 11}, {0x5e, 11}, {0x5f, 11},
1588            {0x20, 9}, {0x21, 9}, {0x22, 9}, {0x23, 9},
1589            {0x24, 9}, {0x25, 9}, {0x26, 9}, {0x27, 9},
1590            {0x28, 9}, {0x29, 9}, {0x2a, 9}, {0x2b, 9},
1591            {0x2c, 9}, {0x2d, 9}, {0x2e, 9}, {0x2f, 9},
1592            {0x10, 7}, {0x11, 7}, {0x12, 7}, {0x13, 7},
1593            {0x14, 7}, {0x15, 7}, {0x16, 7}, {0x17, 7},
1594            {0x10, 6}, {0x11, 6}, {0x12, 6}, {0x13, 6},
1595            {0x08, 4}, {0x09, 4}, {0x06, 3}, {0x03, 3},
1596            {0x07, 3}, {0x0a, 4}, {0x0b, 4}, {0x14, 6},
1597            {0x15, 6}, {0x16, 6}, {0x17, 6}, {0x18, 7},
1598            {0x19, 7}, {0x1a, 7}, {0x1b, 7}, {0x1c, 7},
1599            {0x1d, 7}, {0x1e, 7}, {0x1f, 7}, {0x30, 9},
1600            {0x31, 9}, {0x32, 9}, {0x33, 9}, {0x34, 9},
1601            {0x35, 9}, {0x36, 9}, {0x37, 9}, {0x38, 9},
1602            {0x39, 9}, {0x3a, 9}, {0x3b, 9}, {0x3c, 9},
1603            {0x3d, 9}, {0x3e, 9}, {0x3f, 9}, {0x60, 11},
1604            {0x61, 11}, {0x62, 11}, {0x63, 11}, {0x64, 11},
1605            {0x65, 11}, {0x66, 11}, {0x67, 11}, {0x68, 11},
1606            {0x69, 11}, {0x6a, 11}, {0x6b, 11}, {0x6c, 11},
1607            {0x6d, 11}, {0x6e, 11}, {0x6f, 11}, {0x70, 11},
1608            {0x71, 11}, {0x72, 11}, {0x73, 11}, {0x74, 11},
1609            {0x75, 11}, {0x76, 11}, {0x77, 11}, {0x78, 11},
1610            {0x79, 11}, {0x7a, 11}, {0x7b, 11}, {0x7c, 11},
1611            {0x7d, 11}, {0x7e, 11}, {0x7f, 11}, {0xc0, 13},
1612            {0xc1, 13}, {0xc2, 13}, {0xc3, 13}, {0xc4, 13},
1613            {0xc5, 13}, {0xc6, 13}, {0xc7, 13}, {0xc8, 13},
1614            {0xc9, 13}, {0xca, 13}, {0xcb, 13}, {0xcc, 13},
1615            {0xcd, 13}, {0xce, 13}, {0xcf, 13}, {0xd0, 13},
1616            {0xd1, 13}, {0xd2, 13}, {0xd3, 13}, {0xd4, 13},
1617            {0xd5, 13}, {0xd6, 13}, {0xd7, 13}, {0xd8, 13},
1618            {0xd9, 13}, {0xda, 13}, {0xdb, 13}, {0xdc, 13},
1619            {0xdd, 13}, {0xde, 13}, {0xdf, 13}, {0xe0, 13},
1620            {0xe1, 13}, {0xe2, 13}, {0xe3, 13}, {0xe4, 13},
1621            {0xe5, 13}, {0xe6, 13}, {0xe7, 13}, {0xe8, 13},
1622            {0xe9, 13}, {0xea, 13}, {0xeb, 13}, {0xec, 13},
1623            {0xed, 13}, {0xee, 13}, {0xef, 13}, {0xf0, 13},
1624            {0xf1, 13}, {0xf2, 13}, {0xf3, 13}, {0xf4, 13},
1625            {0xf5, 13}, {0xf6, 13}, {0xf7, 13}, {0xf8, 13},
1626            {0xf9, 13}, {0xfa, 13}, {0xfb, 13}, {0xfc, 13},
1627            {0xfd, 13}, {0xfe, 13}, {0xff, 13}, {0x180, 15},
1628            {0x181, 15}, {0x182, 15}, {0x183, 15}, {0x184, 15},
1629            {0x185, 15}, {0x186, 15}, {0x187, 15}, {0x188, 15},
1630            {0x189, 15}, {0x18a, 15}, {0x18b, 15}, {0x18c, 15},
1631            {0x18d, 15}, {0x18e, 15}, {0x18f, 15}, {0x190, 15},
1632            {0x191, 15}, {0x192, 15}, {0x193, 15}, {0x194, 15},
1633            {0x195, 15}, {0x196, 15}, {0x197, 15}, {0x198, 15},
1634            {0x199, 15}, {0x19a, 15}, {0x19b, 15}, {0x19c, 15},
1635            {0x19d, 15}, {0x19e, 15}, {0x19f, 15}, {0x1a0, 15},
1636            {0x1a1, 15}, {0x1a2, 15}, {0x1a3, 15}, {0x1a4, 15},
1637            {0x1a5, 15}, {0x1a6, 15}, {0x1a7, 15}, {0x1a8, 15},
1638            {0x1a9, 15}, {0x1aa, 15}, {0x1ab, 15}, {0x1ac, 15},
1639            {0x1ad, 15}, {0x1ae, 15}, {0x1af, 15}, {0x1b0, 15},
1640            {0x1b1, 15}, {0x1b2, 15}, {0x1b3, 15}, {0x1b4, 15},
1641            {0x1b5, 15}, {0x1b6, 15}, {0x1b7, 15}, {0x1b8, 15},
1642            {0x1b9, 15}, {0x1ba, 15}, {0x1bb, 15}, {0x1bc, 15},
1643            {0x1bd, 15}, {0x1be, 15}, {0x1bf, 15}, {0x1c0, 15},
1644            {0x1c1, 15}, {0x1c2, 15}, {0x1c3, 15}, {0x1c4, 15},
1645            {0x1c5, 15}, {0x1c6, 15}, {0x1c7, 15}, {0x1c8, 15},
1646            {0x1c9, 15}, {0x1ca, 15}, {0x1cb, 15}, {0x1cc, 15},
1647            {0x1cd, 15}, {0x1ce, 15}, {0x1cf, 15}, {0x1d0, 15},
1648            {0x1d1, 15}, {0x1d2, 15}, {0x1d3, 15}, {0x1d4, 15},
1649            {0x1d5, 15}, {0x1d6, 15}, {0x1d7, 15}, {0x1d8, 15},
1650            {0x1d9, 15}, {0x1da, 15}, {0x1db, 15}, {0x1dc, 15},
1651            {0x1dd, 15}, {0x1de, 15}, {0x1df, 15}, {0x1e0, 15},
1652            {0x1e1, 15}, {0x1e2, 15}, {0x1e3, 15}, {0x1e4, 15},
1653            {0x1e5, 15}, {0x1e6, 15}, {0x1e7, 15}, {0x1e8, 15},
1654            {0x1e9, 15}, {0x1ea, 15}, {0x1eb, 15}, {0x1ec, 15},
1655            {0x1ed, 15}, {0x1ee, 15}, {0x1ef, 15}, {0x1f0, 15},
1656            {0x1f1, 15}, {0x1f2, 15}, {0x1f3, 15}, {0x1f4, 15},
1657            {0x1f5, 15}, {0x1f6, 15}, {0x1f7, 15}, {0x1f8, 15},
1658            {0x1f9, 15}, {0x1fa, 15}, {0x1fb, 15}, {0x1fc, 15},
1659            {0x1fd, 15}, {0x1fe, 15}, {0x1ff, 15},
1660    };
1661    
1662    const VLC dcc_tab[511] = {
1663            {0x100, 16}, {0x101, 16}, {0x102, 16}, {0x103, 16},
1664            {0x104, 16}, {0x105, 16}, {0x106, 16}, {0x107, 16},
1665            {0x108, 16}, {0x109, 16}, {0x10a, 16}, {0x10b, 16},
1666            {0x10c, 16}, {0x10d, 16}, {0x10e, 16}, {0x10f, 16},
1667            {0x110, 16}, {0x111, 16}, {0x112, 16}, {0x113, 16},
1668            {0x114, 16}, {0x115, 16}, {0x116, 16}, {0x117, 16},
1669            {0x118, 16}, {0x119, 16}, {0x11a, 16}, {0x11b, 16},
1670            {0x11c, 16}, {0x11d, 16}, {0x11e, 16}, {0x11f, 16},
1671            {0x120, 16}, {0x121, 16}, {0x122, 16}, {0x123, 16},
1672            {0x124, 16}, {0x125, 16}, {0x126, 16}, {0x127, 16},
1673            {0x128, 16}, {0x129, 16}, {0x12a, 16}, {0x12b, 16},
1674            {0x12c, 16}, {0x12d, 16}, {0x12e, 16}, {0x12f, 16},
1675            {0x130, 16}, {0x131, 16}, {0x132, 16}, {0x133, 16},
1676            {0x134, 16}, {0x135, 16}, {0x136, 16}, {0x137, 16},
1677            {0x138, 16}, {0x139, 16}, {0x13a, 16}, {0x13b, 16},
1678            {0x13c, 16}, {0x13d, 16}, {0x13e, 16}, {0x13f, 16},
1679            {0x140, 16}, {0x141, 16}, {0x142, 16}, {0x143, 16},
1680            {0x144, 16}, {0x145, 16}, {0x146, 16}, {0x147, 16},
1681            {0x148, 16}, {0x149, 16}, {0x14a, 16}, {0x14b, 16},
1682            {0x14c, 16}, {0x14d, 16}, {0x14e, 16}, {0x14f, 16},
1683            {0x150, 16}, {0x151, 16}, {0x152, 16}, {0x153, 16},
1684            {0x154, 16}, {0x155, 16}, {0x156, 16}, {0x157, 16},
1685            {0x158, 16}, {0x159, 16}, {0x15a, 16}, {0x15b, 16},
1686            {0x15c, 16}, {0x15d, 16}, {0x15e, 16}, {0x15f, 16},
1687            {0x160, 16}, {0x161, 16}, {0x162, 16}, {0x163, 16},
1688            {0x164, 16}, {0x165, 16}, {0x166, 16}, {0x167, 16},
1689            {0x168, 16}, {0x169, 16}, {0x16a, 16}, {0x16b, 16},
1690            {0x16c, 16}, {0x16d, 16}, {0x16e, 16}, {0x16f, 16},
1691            {0x170, 16}, {0x171, 16}, {0x172, 16}, {0x173, 16},
1692            {0x174, 16}, {0x175, 16}, {0x176, 16}, {0x177, 16},
1693            {0x178, 16}, {0x179, 16}, {0x17a, 16}, {0x17b, 16},
1694            {0x17c, 16}, {0x17d, 16}, {0x17e, 16}, {0x17f, 16},
1695            {0x80, 14}, {0x81, 14}, {0x82, 14}, {0x83, 14},
1696            {0x84, 14}, {0x85, 14}, {0x86, 14}, {0x87, 14},
1697            {0x88, 14}, {0x89, 14}, {0x8a, 14}, {0x8b, 14},
1698            {0x8c, 14}, {0x8d, 14}, {0x8e, 14}, {0x8f, 14},
1699            {0x90, 14}, {0x91, 14}, {0x92, 14}, {0x93, 14},
1700            {0x94, 14}, {0x95, 14}, {0x96, 14}, {0x97, 14},
1701            {0x98, 14}, {0x99, 14}, {0x9a, 14}, {0x9b, 14},
1702            {0x9c, 14}, {0x9d, 14}, {0x9e, 14}, {0x9f, 14},
1703            {0xa0, 14}, {0xa1, 14}, {0xa2, 14}, {0xa3, 14},
1704            {0xa4, 14}, {0xa5, 14}, {0xa6, 14}, {0xa7, 14},
1705            {0xa8, 14}, {0xa9, 14}, {0xaa, 14}, {0xab, 14},
1706            {0xac, 14}, {0xad, 14}, {0xae, 14}, {0xaf, 14},
1707            {0xb0, 14}, {0xb1, 14}, {0xb2, 14}, {0xb3, 14},
1708            {0xb4, 14}, {0xb5, 14}, {0xb6, 14}, {0xb7, 14},
1709            {0xb8, 14}, {0xb9, 14}, {0xba, 14}, {0xbb, 14},
1710            {0xbc, 14}, {0xbd, 14}, {0xbe, 14}, {0xbf, 14},
1711            {0x40, 12}, {0x41, 12}, {0x42, 12}, {0x43, 12},
1712            {0x44, 12}, {0x45, 12}, {0x46, 12}, {0x47, 12},
1713            {0x48, 12}, {0x49, 12}, {0x4a, 12}, {0x4b, 12},
1714            {0x4c, 12}, {0x4d, 12}, {0x4e, 12}, {0x4f, 12},
1715            {0x50, 12}, {0x51, 12}, {0x52, 12}, {0x53, 12},
1716            {0x54, 12}, {0x55, 12}, {0x56, 12}, {0x57, 12},
1717            {0x58, 12}, {0x59, 12}, {0x5a, 12}, {0x5b, 12},
1718            {0x5c, 12}, {0x5d, 12}, {0x5e, 12}, {0x5f, 12},
1719            {0x20, 10}, {0x21, 10}, {0x22, 10}, {0x23, 10},
1720            {0x24, 10}, {0x25, 10}, {0x26, 10}, {0x27, 10},
1721            {0x28, 10}, {0x29, 10}, {0x2a, 10}, {0x2b, 10},
1722            {0x2c, 10}, {0x2d, 10}, {0x2e, 10}, {0x2f, 10},
1723            {0x10, 8}, {0x11, 8}, {0x12, 8}, {0x13, 8},
1724            {0x14, 8}, {0x15, 8}, {0x16, 8}, {0x17, 8},
1725            {0x08, 6}, {0x09, 6}, {0x0a, 6}, {0x0b, 6},
1726            {0x04, 4}, {0x05, 4}, {0x04, 3}, {0x03, 2},
1727            {0x05, 3}, {0x06, 4}, {0x07, 4}, {0x0c, 6},
1728            {0x0d, 6}, {0x0e, 6}, {0x0f, 6}, {0x18, 8},
1729            {0x19, 8}, {0x1a, 8}, {0x1b, 8}, {0x1c, 8},
1730            {0x1d, 8}, {0x1e, 8}, {0x1f, 8}, {0x30, 10},
1731            {0x31, 10}, {0x32, 10}, {0x33, 10}, {0x34, 10},
1732            {0x35, 10}, {0x36, 10}, {0x37, 10}, {0x38, 10},
1733            {0x39, 10}, {0x3a, 10}, {0x3b, 10}, {0x3c, 10},
1734            {0x3d, 10}, {0x3e, 10}, {0x3f, 10}, {0x60, 12},
1735            {0x61, 12}, {0x62, 12}, {0x63, 12}, {0x64, 12},
1736            {0x65, 12}, {0x66, 12}, {0x67, 12}, {0x68, 12},
1737            {0x69, 12}, {0x6a, 12}, {0x6b, 12}, {0x6c, 12},
1738            {0x6d, 12}, {0x6e, 12}, {0x6f, 12}, {0x70, 12},
1739            {0x71, 12}, {0x72, 12}, {0x73, 12}, {0x74, 12},
1740            {0x75, 12}, {0x76, 12}, {0x77, 12}, {0x78, 12},
1741            {0x79, 12}, {0x7a, 12}, {0x7b, 12}, {0x7c, 12},
1742            {0x7d, 12}, {0x7e, 12}, {0x7f, 12}, {0xc0, 14},
1743            {0xc1, 14}, {0xc2, 14}, {0xc3, 14}, {0xc4, 14},
1744            {0xc5, 14}, {0xc6, 14}, {0xc7, 14}, {0xc8, 14},
1745            {0xc9, 14}, {0xca, 14}, {0xcb, 14}, {0xcc, 14},
1746            {0xcd, 14}, {0xce, 14}, {0xcf, 14}, {0xd0, 14},
1747            {0xd1, 14}, {0xd2, 14}, {0xd3, 14}, {0xd4, 14},
1748            {0xd5, 14}, {0xd6, 14}, {0xd7, 14}, {0xd8, 14},
1749            {0xd9, 14}, {0xda, 14}, {0xdb, 14}, {0xdc, 14},
1750            {0xdd, 14}, {0xde, 14}, {0xdf, 14}, {0xe0, 14},
1751            {0xe1, 14}, {0xe2, 14}, {0xe3, 14}, {0xe4, 14},
1752            {0xe5, 14}, {0xe6, 14}, {0xe7, 14}, {0xe8, 14},
1753            {0xe9, 14}, {0xea, 14}, {0xeb, 14}, {0xec, 14},
1754            {0xed, 14}, {0xee, 14}, {0xef, 14}, {0xf0, 14},
1755            {0xf1, 14}, {0xf2, 14}, {0xf3, 14}, {0xf4, 14},
1756            {0xf5, 14}, {0xf6, 14}, {0xf7, 14}, {0xf8, 14},
1757            {0xf9, 14}, {0xfa, 14}, {0xfb, 14}, {0xfc, 14},
1758            {0xfd, 14}, {0xfe, 14}, {0xff, 14}, {0x180, 16},
1759            {0x181, 16}, {0x182, 16}, {0x183, 16}, {0x184, 16},
1760            {0x185, 16}, {0x186, 16}, {0x187, 16}, {0x188, 16},
1761            {0x189, 16}, {0x18a, 16}, {0x18b, 16}, {0x18c, 16},
1762            {0x18d, 16}, {0x18e, 16}, {0x18f, 16}, {0x190, 16},
1763            {0x191, 16}, {0x192, 16}, {0x193, 16}, {0x194, 16},
1764            {0x195, 16}, {0x196, 16}, {0x197, 16}, {0x198, 16},
1765            {0x199, 16}, {0x19a, 16}, {0x19b, 16}, {0x19c, 16},
1766            {0x19d, 16}, {0x19e, 16}, {0x19f, 16}, {0x1a0, 16},
1767            {0x1a1, 16}, {0x1a2, 16}, {0x1a3, 16}, {0x1a4, 16},
1768            {0x1a5, 16}, {0x1a6, 16}, {0x1a7, 16}, {0x1a8, 16},
1769            {0x1a9, 16}, {0x1aa, 16}, {0x1ab, 16}, {0x1ac, 16},
1770            {0x1ad, 16}, {0x1ae, 16}, {0x1af, 16}, {0x1b0, 16},
1771            {0x1b1, 16}, {0x1b2, 16}, {0x1b3, 16}, {0x1b4, 16},
1772            {0x1b5, 16}, {0x1b6, 16}, {0x1b7, 16}, {0x1b8, 16},
1773            {0x1b9, 16}, {0x1ba, 16}, {0x1bb, 16}, {0x1bc, 16},
1774            {0x1bd, 16}, {0x1be, 16}, {0x1bf, 16}, {0x1c0, 16},
1775            {0x1c1, 16}, {0x1c2, 16}, {0x1c3, 16}, {0x1c4, 16},
1776            {0x1c5, 16}, {0x1c6, 16}, {0x1c7, 16}, {0x1c8, 16},
1777            {0x1c9, 16}, {0x1ca, 16}, {0x1cb, 16}, {0x1cc, 16},
1778            {0x1cd, 16}, {0x1ce, 16}, {0x1cf, 16}, {0x1d0, 16},
1779            {0x1d1, 16}, {0x1d2, 16}, {0x1d3, 16}, {0x1d4, 16},
1780            {0x1d5, 16}, {0x1d6, 16}, {0x1d7, 16}, {0x1d8, 16},
1781            {0x1d9, 16}, {0x1da, 16}, {0x1db, 16}, {0x1dc, 16},
1782            {0x1dd, 16}, {0x1de, 16}, {0x1df, 16}, {0x1e0, 16},
1783            {0x1e1, 16}, {0x1e2, 16}, {0x1e3, 16}, {0x1e4, 16},
1784            {0x1e5, 16}, {0x1e6, 16}, {0x1e7, 16}, {0x1e8, 16},
1785            {0x1e9, 16}, {0x1ea, 16}, {0x1eb, 16}, {0x1ec, 16},
1786            {0x1ed, 16}, {0x1ee, 16}, {0x1ef, 16}, {0x1f0, 16},
1787            {0x1f1, 16}, {0x1f2, 16}, {0x1f3, 16}, {0x1f4, 16},
1788            {0x1f5, 16}, {0x1f6, 16}, {0x1f7, 16}, {0x1f8, 16},
1789            {0x1f9, 16}, {0x1fa, 16}, {0x1fb, 16}, {0x1fc, 16},
1790            {0x1fd, 16}, {0x1fe, 16}, {0x1ff, 16},
1791    };
1792    
1793    
1794    const VLC mb_motion_table[65] = {
1795            {0x05, 13}, {0x07, 13}, {0x05, 12}, {0x07, 12},
1796            {0x09, 12}, {0x0b, 12}, {0x0d, 12}, {0x0f, 12},
1797            {0x09, 11}, {0x0b, 11}, {0x0d, 11}, {0x0f, 11},
1798            {0x11, 11}, {0x13, 11}, {0x15, 11}, {0x17, 11},
1799            {0x19, 11}, {0x1b, 11}, {0x1d, 11}, {0x1f, 11},
1800            {0x21, 11}, {0x23, 11}, {0x13, 10}, {0x15, 10},
1801            {0x17, 10}, {0x07, 8}, {0x09, 8}, {0x0b, 8},
1802            {0x07, 7}, {0x03, 5}, {0x03, 4}, {0x03, 3},
1803            {0x01, 1}, {0x02, 3}, {0x02, 4}, {0x02, 5},
1804            {0x06, 7}, {0x0a, 8}, {0x08, 8}, {0x06, 8},
1805            {0x16, 10}, {0x14, 10}, {0x12, 10}, {0x22, 11},
1806            {0x20, 11}, {0x1e, 11}, {0x1c, 11}, {0x1a, 11},
1807            {0x18, 11}, {0x16, 11}, {0x14, 11}, {0x12, 11},
1808            {0x10, 11}, {0x0e, 11}, {0x0c, 11}, {0x0a, 11},
1809            {0x08, 11}, {0x0e, 12}, {0x0c, 12}, {0x0a, 12},
1810            {0x08, 12}, {0x06, 12}, {0x04, 12}, {0x06, 13},
1811            {0x04, 13}
1812    };
1813    
1814    
1815    /******************************************************************
1816     * decoder tables                                                 *
1817     ******************************************************************/
1818    
1819    VLC const mcbpc_intra_table[64] = {
1820            {-1, 0}, {20, 6}, {36, 6}, {52, 6}, {4, 4},  {4, 4},  {4, 4},  {4, 4},
1821            {19, 3}, {19, 3}, {19, 3}, {19, 3}, {19, 3}, {19, 3}, {19, 3}, {19, 3},
1822            {35, 3}, {35, 3}, {35, 3}, {35, 3}, {35, 3}, {35, 3}, {35, 3}, {35, 3},
1823            {51, 3}, {51, 3}, {51, 3}, {51, 3}, {51, 3}, {51, 3}, {51, 3}, {51, 3},
1824            {3, 1},  {3, 1},  {3, 1},  {3, 1},  {3, 1},  {3, 1},  {3, 1},  {3, 1},
1825            {3, 1},  {3, 1},  {3, 1},  {3, 1},  {3, 1},  {3, 1},  {3, 1},  {3, 1},
1826            {3, 1},  {3, 1},  {3, 1},  {3, 1},  {3, 1},  {3, 1},  {3, 1},  {3, 1},
1827            {3, 1},  {3, 1},  {3, 1},  {3, 1},  {3, 1},  {3, 1},  {3, 1},  {3, 1}
1828    };
1829    
1830    VLC const mcbpc_inter_table[257] = {
1831            {VLC_ERROR, 0}, {255, 9}, {52, 9}, {36, 9}, {20, 9}, {49, 9}, {35, 8}, {35, 8},
1832            {19, 8}, {19, 8}, {50, 8}, {50, 8}, {51, 7}, {51, 7}, {51, 7}, {51, 7},
1833            {34, 7}, {34, 7}, {34, 7}, {34, 7}, {18, 7}, {18, 7}, {18, 7}, {18, 7},
1834            {33, 7}, {33, 7}, {33, 7}, {33, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7},
1835            {4, 6}, {4, 6}, {4, 6}, {4, 6}, {4, 6}, {4, 6}, {4, 6}, {4, 6},
1836            {48, 6}, {48, 6}, {48, 6}, {48, 6}, {48, 6}, {48, 6}, {48, 6}, {48, 6},
1837            {3, 5}, {3, 5}, {3, 5}, {3, 5}, {3, 5}, {3, 5}, {3, 5}, {3, 5},
1838            {3, 5}, {3, 5}, {3, 5}, {3, 5}, {3, 5}, {3, 5}, {3, 5}, {3, 5},
1839            {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
1840            {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
1841            {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
1842            {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
1843            {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
1844            {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
1845            {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
1846            {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
1847            {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3},
1848            {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3},
1849            {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3},
1850            {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3},
1851            {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3},
1852            {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3},
1853            {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3},
1854            {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3},
1855            {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3},
1856            {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3},
1857            {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3},
1858            {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3},
1859            {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3},
1860            {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3},
1861            {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3},
1862            {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3},
1863            {0, 1}
1864    };
1865    
1866    VLC const cbpy_table[64] = {
1867            {-1, 0}, {-1, 0}, {6, 6},  {9, 6},  {8, 5},  {8, 5},  {4, 5},  {4, 5},
1868            {2, 5},  {2, 5},  {1, 5},  {1, 5},  {0, 4},  {0, 4},  {0, 4},  {0, 4},
1869            {12, 4}, {12, 4}, {12, 4}, {12, 4}, {10, 4}, {10, 4}, {10, 4}, {10, 4},
1870            {14, 4}, {14, 4}, {14, 4}, {14, 4}, {5, 4},  {5, 4},  {5, 4},  {5, 4},
1871            {13, 4}, {13, 4}, {13, 4}, {13, 4}, {3, 4},  {3, 4},  {3, 4},  {3, 4},
1872            {11, 4}, {11, 4}, {11, 4}, {11, 4}, {7, 4},  {7, 4},  {7, 4},  {7, 4},
1873            {15, 2}, {15, 2}, {15, 2}, {15, 2}, {15, 2}, {15, 2}, {15, 2}, {15, 2},
1874            {15, 2}, {15, 2}, {15, 2}, {15, 2}, {15, 2}, {15, 2}, {15, 2}, {15, 2}
1875    };
1876    
1877    VLC const TMNMVtab0[] = {
1878            {3, 4}, {-3, 4}, {2, 3}, {2, 3}, {-2, 3}, {-2, 3}, {1, 2},
1879            {1, 2}, {1, 2}, {1, 2}, {-1, 2}, {-1, 2}, {-1, 2}, {-1, 2}
1880    };
1881    
1882    VLC const TMNMVtab1[] = {
1883            {12, 10}, {-12, 10}, {11, 10}, {-11, 10},
1884            {10, 9}, {10, 9}, {-10, 9}, {-10, 9},
1885            {9, 9}, {9, 9}, {-9, 9}, {-9, 9},
1886            {8, 9}, {8, 9}, {-8, 9}, {-8, 9},
1887            {7, 7}, {7, 7}, {7, 7}, {7, 7},
1888            {7, 7}, {7, 7}, {7, 7}, {7, 7},
1889            {-7, 7}, {-7, 7}, {-7, 7}, {-7, 7},
1890            {-7, 7}, {-7, 7}, {-7, 7}, {-7, 7},
1891            {6, 7}, {6, 7}, {6, 7}, {6, 7},
1892            {6, 7}, {6, 7}, {6, 7}, {6, 7},
1893            {-6, 7}, {-6, 7}, {-6, 7}, {-6, 7},
1894            {-6, 7}, {-6, 7}, {-6, 7}, {-6, 7},
1895            {5, 7}, {5, 7}, {5, 7}, {5, 7},
1896            {5, 7}, {5, 7}, {5, 7}, {5, 7},
1897            {-5, 7}, {-5, 7}, {-5, 7}, {-5, 7},
1898            {-5, 7}, {-5, 7}, {-5, 7}, {-5, 7},
1899            {4, 6}, {4, 6}, {4, 6}, {4, 6},
1900            {4, 6}, {4, 6}, {4, 6}, {4, 6},
1901            {4, 6}, {4, 6}, {4, 6}, {4, 6},
1902            {4, 6}, {4, 6}, {4, 6}, {4, 6},
1903            {-4, 6}, {-4, 6}, {-4, 6}, {-4, 6},
1904            {-4, 6}, {-4, 6}, {-4, 6}, {-4, 6},
1905            {-4, 6}, {-4, 6}, {-4, 6}, {-4, 6},
1906            {-4, 6}, {-4, 6}, {-4, 6}, {-4, 6}
1907    };
1908    
1909    VLC const TMNMVtab2[] = {
1910            {32, 12}, {-32, 12}, {31, 12}, {-31, 12},
1911            {30, 11}, {30, 11}, {-30, 11}, {-30, 11},
1912            {29, 11}, {29, 11}, {-29, 11}, {-29, 11},
1913            {28, 11}, {28, 11}, {-28, 11}, {-28, 11},
1914            {27, 11}, {27, 11}, {-27, 11}, {-27, 11},
1915            {26, 11}, {26, 11}, {-26, 11}, {-26, 11},
1916            {25, 11}, {25, 11}, {-25, 11}, {-25, 11},
1917            {24, 10}, {24, 10}, {24, 10}, {24, 10},
1918            {-24, 10}, {-24, 10}, {-24, 10}, {-24, 10},
1919            {23, 10}, {23, 10}, {23, 10}, {23, 10},
1920            {-23, 10}, {-23, 10}, {-23, 10}, {-23, 10},
1921            {22, 10}, {22, 10}, {22, 10}, {22, 10},
1922            {-22, 10}, {-22, 10}, {-22, 10}, {-22, 10},
1923            {21, 10}, {21, 10}, {21, 10}, {21, 10},
1924            {-21, 10}, {-21, 10}, {-21, 10}, {-21, 10},
1925            {20, 10}, {20, 10}, {20, 10}, {20, 10},
1926            {-20, 10}, {-20, 10}, {-20, 10}, {-20, 10},
1927            {19, 10}, {19, 10}, {19, 10}, {19, 10},
1928            {-19, 10}, {-19, 10}, {-19, 10}, {-19, 10},
1929            {18, 10}, {18, 10}, {18, 10}, {18, 10},
1930            {-18, 10}, {-18, 10}, {-18, 10}, {-18, 10},
1931            {17, 10}, {17, 10}, {17, 10}, {17, 10},
1932            {-17, 10}, {-17, 10}, {-17, 10}, {-17, 10},
1933            {16, 10}, {16, 10}, {16, 10}, {16, 10},
1934            {-16, 10}, {-16, 10}, {-16, 10}, {-16, 10},
1935            {15, 10}, {15, 10}, {15, 10}, {15, 10},
1936            {-15, 10}, {-15, 10}, {-15, 10}, {-15, 10},
1937            {14, 10}, {14, 10}, {14, 10}, {14, 10},
1938            {-14, 10}, {-14, 10}, {-14, 10}, {-14, 10},
1939            {13, 10}, {13, 10}, {13, 10}, {13, 10},
1940            {-13, 10}, {-13, 10}, {-13, 10}, {-13, 10}
1941    };
1942    
1943    short const dc_threshold[] = {
1944            21514, 26984,  8307, 28531, 29798, 24951, 25970, 26912,
1945             8307, 25956, 26994, 25974,  8292, 29286, 28015, 29728,
1946            25960, 18208, 21838, 18208, 19536, 22560, 26998,  8260,
1947            28515, 25956,  8291, 25640, 30309, 27749, 11817, 22794,
1948            30063,  8306, 28531, 29798, 24951, 25970, 25632, 29545,
1949            29300, 25193, 29813, 29295, 26656, 29537, 29728,  8303,
1950            26983, 25974, 24864, 25443, 29541,  8307, 28532, 26912,
1951            29556, 29472, 30063, 25458,  8293, 28515, 25956,  2606
1952    };
1953    
1954    VLC const dc_lum_tab[] = {
1955            {0, 0}, {4, 3}, {3, 3}, {0, 3},
1956            {2, 2}, {2, 2}, {1, 2}, {1, 2},
1957    };

Legend:
Removed from v.100  
changed lines
  Added in v.1486

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4