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

Legend:
Removed from v.78  
changed lines
  Added in v.1777

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