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

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

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

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

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

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