[svn] / branches / dev-api-4 / xvidcore / src / bitstream / mbcoding.c Repository:
ViewVC logotype

Diff of /branches/dev-api-4/xvidcore/src/bitstream/mbcoding.c

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

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

Legend:
Removed from v.17  
changed lines
  Added in v.1161

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