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

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

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

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

Legend:
Removed from v.313  
changed lines
  Added in v.1677

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