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

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

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