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

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

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

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

Legend:
Removed from v.153  
changed lines
  Added in v.1165

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