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

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

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

revision 1.5, Sat Mar 16 15:52:34 2002 UTC revision 1.25.2.11, Thu Jan 16 21:16:04 2003 UTC
# Line 1  Line 1 
1     /******************************************************************************
2      *                                                                            *
3      *  This file is part of XviD, a free MPEG-4 video encoder/decoder            *
4      *                                                                            *
5      *  XviD is an implementation of a part of one or more MPEG-4 Video tools     *
6      *  as specified in ISO/IEC 14496-2 standard.  Those intending to use this    *
7      *  software module in hardware or software products are advised that its     *
8      *  use may infringe existing patents or copyrights, and any such use         *
9      *  would be at such party's own risk.  The original developer of this        *
10      *  software module and his/her company, and subsequent editors and their     *
11      *  companies, will have no liability for use of this software or             *
12      *  modifications or derivatives thereof.                                     *
13      *                                                                            *
14      *  XviD is free software; you can redistribute it and/or modify it           *
15      *  under the terms of the GNU General Public License as published by         *
16      *  the Free Software Foundation; either version 2 of the License, or         *
17      *  (at your option) any later version.                                       *
18      *                                                                            *
19      *  XviD is distributed in the hope that it will be useful, but               *
20      *  WITHOUT ANY WARRANTY; without even the implied warranty of                *
21      *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
22      *  GNU General Public License for more details.                              *
23      *                                                                            *
24      *  You should have received a copy of the GNU General Public License         *
25      *  along with this program; if not, write to the Free Software               *
26      *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA  *
27      *                                                                            *
28      ******************************************************************************/
29    
30     /******************************************************************************
31      *                                                                            *
32      *  mbcoding.c                                                                *
33      *                                                                            *
34      *  Copyright (C) 2002 - Michael Militzer <isibaar@xvid.org>                  *
35      *                                                                            *
36      *  For more information visit the XviD homepage: http://www.xvid.org         *
37      *                                                                            *
38      ******************************************************************************/
39    
40     /******************************************************************************
41      *                                                                                                                                                        *
42      *  Revision history:                                                         *
43      *                                                                            *
44      *  28.10.2002 GMC support - gruel                                                                                        *
45      *  28.06.2002 added check_resync_marker()                                    *
46      *  14.04.2002 bframe encoding                                                                                            *
47      *  08.03.2002 initial version; isibaar                                                           *
48      *                                                                                                                                                        *
49      ******************************************************************************/
50    
51    
52    #include <stdio.h>
53    #include <stdlib.h>
54  #include "../portab.h"  #include "../portab.h"
55    #include "../global.h"
56  #include "bitstream.h"  #include "bitstream.h"
57  #include "zigzag.h"  #include "zigzag.h"
58  #include "vlc_codes.h"  #include "vlc_codes.h"
59    #include "mbcoding.h"
60    
61  #include "../utils/mbfunctions.h"  #include "../utils/mbfunctions.h"
62    
63  #include <stdlib.h> /* malloc, free */  /* #define BIGLUT */
64    
65  #define ESCAPE 7167  #ifdef BIGLUT
66  #define ABS(X) (((X)>0)?(X):-(X))  #define LEVELOFFSET 2048
67  #define CLIP(X,A) (X > A) ? (A) : (X)  #else
68    #define LEVELOFFSET 32
69    #endif
70    
71  static VLC *DCT3D[2];  static REVERSE_EVENT DCT3D[2][4096];
72    
73  VLC *intra_table, *inter_table;  #ifdef BIGLUT
74  static short clip_table[4096];  static VLC coeff_VLC[2][2][4096][64];
75    static VLC *intra_table, *inter_table;
76    #else
77    static VLC coeff_VLC[2][2][64][64];
78    #endif
79    
80  void create_vlc_tables(void)  /* not really MB related, but VLCs are only available here */
81    void bs_put_spritetrajectory(Bitstream * bs, const int val)
82  {  {
83          int32_t k, l, i, intra, last;          const int code = sprite_trajectory_code[val+16384].code;
84          VLC *vlc[2];          const int len = sprite_trajectory_code[val+16384].len;
85          VLC **coeff_ptr;          const int code2 = sprite_trajectory_len[len].code;
86          VLC *vlc1, *vlc2;          const int len2 = sprite_trajectory_len[len].len;
   
         VLC *DCT3Dintra;  
         VLC *DCT3Dinter;  
   
         DCT3Dintra = (VLC *) malloc(sizeof(VLC) * 4096);  
         DCT3Dinter = (VLC *) malloc(sizeof(VLC) * 4096);  
87    
88          vlc1 = DCT3Dintra;  //      printf("GMC=%d Code/Len  = %d / %d ",val, code,len);
89          vlc2 = DCT3Dinter;  //      printf("Code2 / Len2 = %d / %d \n",code2,len2);
90    
91          vlc[0] = intra_table = (VLC *) malloc(128 * 511 * sizeof(VLC));          BitstreamPutBits(bs, code2, len2);
92          vlc[1] = inter_table = (VLC *) malloc(128 * 511 * sizeof(VLC));          if (len) BitstreamPutBits(bs, code, len);
   
         // initialize the clipping table  
         for(i = -2048; i < 2048; i++) {  
                 clip_table[i + 2048] = i;  
                 if(i < -255)  
                         clip_table[i + 2048] = -255;  
                 if(i > 255)  
                         clip_table[i + 2048] = 255;  
93          }          }
94    
95          // generate intra/inter vlc lookup table  int bs_get_spritetrajectory(Bitstream * bs)
96          for(i = 0; i < 4; i++) {  {
97                  intra = i % 2;          int i;
98                  last = i >> 1;          for (i = 0; i < 12; i++)
99            {
100                    if (BitstreamShowBits(bs, sprite_trajectory_len[i].len) == sprite_trajectory_len[i].code)
101                    {
102                            BitstreamSkip(bs, sprite_trajectory_len[i].len);
103                            return i;
104                    }
105            }
106            return -1;
107    }
108    
109                  coeff_ptr = coeff_vlc[last + (intra << 1)];  void
110    init_vlc_tables(void)
111    {
112            uint32_t i, j, k, intra, last, run,  run_esc, level, level_esc, escape, escape_len, offset, limit;
113            int32_t l;
114    
115                  for(k = -255; k < 256; k++) { // level  #ifdef BIGLUT
116                          char *max_level_ptr = max_level[last + (intra << 1)];          intra_table = coeff_VLC[1];
117                          char *max_run_ptr = max_run[last + (intra << 1)];          inter_table = coeff_VLC[0];
118    #endif
119    
                         for(l = 0; l < 64; l++) { // run  
                                 int32_t level = k;  
                                 uint32_t run = l;  
120    
121                                  if(abs(level) <= max_level_ptr[run] && run <= max_run_ptr[abs(level)]) {          for (intra = 0; intra < 2; intra++)
122                    for (i = 0; i < 4096; i++)
123                            DCT3D[intra][i].event.level = 0;
124    
125                                          if(level > 0) {          for (intra = 0; intra < 2; intra++)
126                                                  vlc[intra]->code = (coeff_ptr[run][level - 1].code) << 1;                  for (last = 0; last < 2; last++)
127                                                  vlc[intra]->len = coeff_ptr[run][level - 1].len + 1;                  {
128                                          }                          for (run = 0; run < 63 + last; run++)
129                                          else if(level < 0) {                                  for (level = 0; level < 32 << intra; level++)
130                                                  vlc[intra]->code = ((coeff_ptr[run][-level - 1].code) << 1) + 1;                                  {
131                                                  vlc[intra]->len = coeff_ptr[run][-level - 1].len + 1;  #ifdef BIGLUT
132                                            offset = LEVELOFFSET;
133    #else
134                                            offset = !intra * LEVELOFFSET;
135    #endif
136                                            coeff_VLC[intra][last][level + offset][run].len = 128;
137                                          }                                          }
                                         else {  
                                                 vlc[intra]->code = 0;  
                                                 vlc[intra]->len = 0;  
138                                          }                                          }
                                 } else {  
                                         if(level > 0)  
                                                 level -= max_level_ptr[run];  
                                         else  
                                                 level += max_level_ptr[run];  
139    
140                                          if(abs(level) <= max_level_ptr[run] &&          for (intra = 0; intra < 2; intra++)
141                                                  run <= max_run_ptr[abs(level)]) {                  for (i = 0; i < 102; i++)
142                    {
143    #ifdef BIGLUT
144                            offset = LEVELOFFSET;
145    #else
146                            offset = !intra * LEVELOFFSET;
147    #endif
148                            for (j = 0; j < 1 << (12 - coeff_tab[intra][i].vlc.len); j++)
149                            {
150                                    DCT3D[intra][(coeff_tab[intra][i].vlc.code << (12 - coeff_tab[intra][i].vlc.len)) | j].len       = coeff_tab[intra][i].vlc.len;
151                                    DCT3D[intra][(coeff_tab[intra][i].vlc.code << (12 - coeff_tab[intra][i].vlc.len)) | j].event = coeff_tab[intra][i].event;
152                            }
153    
154                                                  if(level > 0) {                          coeff_VLC[intra][coeff_tab[intra][i].event.last][coeff_tab[intra][i].event.level + offset][coeff_tab[intra][i].event.run].code
155                                                          vlc[intra]->code = (0x06 << (coeff_ptr[run][level - 1].len + 1)) |                                  = coeff_tab[intra][i].vlc.code << 1;
156                                                                  (coeff_ptr[run][level - 1].code << 1);                          coeff_VLC[intra][coeff_tab[intra][i].event.last][coeff_tab[intra][i].event.level + offset][coeff_tab[intra][i].event.run].len
157                                                          vlc[intra]->len = (coeff_ptr[run][level - 1].len + 1) + 8;                                  = coeff_tab[intra][i].vlc.len + 1;
158                                                  }  #ifndef BIGLUT
159                                                  else if(level < 0) {                          if (!intra)
160                                                          vlc[intra]->code = (0x06 << (coeff_ptr[run][-level - 1].len + 1)) |  #endif
161                                                                  ((coeff_ptr[run][-level - 1].code << 1) + 1);                          {
162                                                          vlc[intra]->len = (coeff_ptr[run][-level - 1].len + 1) + 8;                                  coeff_VLC[intra][coeff_tab[intra][i].event.last][offset - coeff_tab[intra][i].event.level][coeff_tab[intra][i].event.run].code
163                                                  }                                          = (coeff_tab[intra][i].vlc.code << 1) | 1;
164                                                  else {                                  coeff_VLC[intra][coeff_tab[intra][i].event.last][offset - coeff_tab[intra][i].event.level][coeff_tab[intra][i].event.run].len
165                                                          vlc[intra]->code = 0x06;                                          = coeff_tab[intra][i].vlc.len + 1;
166                                                          vlc[intra]->len = 8;                          }
167                                                  }                                                  }
                                         } else {  
                                                 if(level > 0)  
                                                         level += max_level_ptr[run];  
                                                 else  
                                                         level -= max_level_ptr[run];  
168    
169                                                  run -= max_run_ptr[abs(level)] + 1;          for (intra = 0; intra < 2; intra++)
170                    for (last = 0; last < 2; last++)
171                            for (run = 0; run < 63 + last; run++)
172                            {
173                                    for (level = 1; level < 32 << intra; level++)
174                                    {
175                                            if (level <= max_level[intra][last][run] && run <= max_run[intra][last][level])
176                                                continue;
177    
178                                                  if(abs(level) <= max_level_ptr[run] &&  #ifdef BIGLUT
179                                                          run <= max_run_ptr[abs(level)]) {                                          offset = LEVELOFFSET;
180    #else
181                                            offset = !intra * LEVELOFFSET;
182    #endif
183                        level_esc = level - max_level[intra][last][run];
184                                            run_esc = run - 1 - max_run[intra][last][level];
185                                            /*use this test to use shorter esc2 codes when possible
186                                            if (level_esc <= max_level[intra][last][run] && run <= max_run[intra][last][level_esc]
187                                                    && !(coeff_VLC[intra][last][level_esc + offset][run].len + 7 + 1
188                                                             > coeff_VLC[intra][last][level + offset][run_esc].code + 7 + 2))*/
189    
190                                                          if(level > 0) {                                          if (level_esc <= max_level[intra][last][run] && run <= max_run[intra][last][level_esc])
191                                                                  vlc[intra]->code = (0x0e << (coeff_ptr[run][level - 1].len + 1)) |                                          {
192                                                                          (coeff_ptr[run][level - 1].code << 1);                                                  escape     = ESCAPE1;
193                                                                  vlc[intra]->len = (coeff_ptr[run][level - 1].len + 1) + 9;                                                  escape_len = 7 + 1;
194                                                          }                                                  run_esc    = run;
                                                         else if(level < 0) {  
                                                                 vlc[intra]->code = (0x0e << (coeff_ptr[run][-level - 1].len + 1)) |  
                                                                         ((coeff_ptr[run][-level - 1].code << 1) + 1);  
                                                                 vlc[intra]->len = (coeff_ptr[run][-level - 1].len + 1) + 9;  
                                                         }  
                                                         else {  
                                                                 vlc[intra]->code = 0x0e;  
                                                                 vlc[intra]->len = 9;  
195                                                          }                                                          }
                                                 } else {  
                                                         if(level != 0)  
                                                                 run += max_run_ptr[abs(level)] + 1;  
196                                                          else                                                          else
197                                                                  run++;                                          {
198                                                    if (level <= max_level[intra][last][run_esc] && run_esc <= max_run[intra][last][level])
199                                                          vlc[intra]->code = (uint32_t) ((0x1e + last) << 20) |                                                  {
200                                                                                  (l << 14) | (1 << 13) | ((k & 0xfff) << 1) | 1;                                                          escape     = ESCAPE2;
201                                                            escape_len = 7 + 2;
202                                                          vlc[intra]->len = 30;                                                          level_esc  = level;
203                                                  }                                                  }
204                                                    else
205                                                    {
206    #ifndef BIGLUT
207                                                            if (!intra)
208    #endif
209                                                            {
210                                                                    coeff_VLC[intra][last][level + offset][run].code
211                                                                            = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((level & 0xfff) << 1) | 1;
212                                                                    coeff_VLC[intra][last][level + offset][run].len = 30;
213                                                                            coeff_VLC[intra][last][offset - level][run].code
214                                                                            = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-level & 0xfff) << 1) | 1;
215                                                                    coeff_VLC[intra][last][offset - level][run].len = 30;
216                                          }                                          }
217                                                            continue;
218                                  }                                  }
                                 vlc[intra]++;  
219                          }                          }
220    
221                                            coeff_VLC[intra][last][level + offset][run].code
222                                                    = (escape << coeff_VLC[intra][last][level_esc + offset][run_esc].len)
223                                                    |  coeff_VLC[intra][last][level_esc + offset][run_esc].code;
224                                            coeff_VLC[intra][last][level + offset][run].len
225                                                    = coeff_VLC[intra][last][level_esc + offset][run_esc].len + escape_len;
226    #ifndef BIGLUT
227                                            if (!intra)
228    #endif
229                                            {
230                                                    coeff_VLC[intra][last][offset - level][run].code
231                                                            = (escape << coeff_VLC[intra][last][level_esc + offset][run_esc].len)
232                                                            |  coeff_VLC[intra][last][level_esc + offset][run_esc].code | 1;
233                                                    coeff_VLC[intra][last][offset - level][run].len
234                                                            = coeff_VLC[intra][last][level_esc + offset][run_esc].len + escape_len;
235                  }                  }
236          }          }
         intra_table += 64*255; // center vlc tables  
         inter_table += 64*255; // center vlc tables  
237    
238          for(i = 0; i < 4096; i++) {  #ifdef BIGLUT
239                  if(i >= 512) {                                  for (level = 32 << intra; level < 2048; level++)
240                          *vlc1 = DCT3Dtab3[(i >> 5) - 16];                                  {
241                          *vlc2 = DCT3Dtab0[(i >> 5) - 16];                                          coeff_VLC[intra][last][level + offset][run].code
242                  }                                                  = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((level & 0xfff) << 1) | 1;
243                  else if(i >= 128) {                                          coeff_VLC[intra][last][level + offset][run].len = 30;
244                          *vlc1 = DCT3Dtab4[(i >> 2) - 32];  
245                          *vlc2 = DCT3Dtab1[(i >> 2) - 32];                                          coeff_VLC[intra][last][offset - level][run].code
246                                                    = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-level & 0xfff) << 1) | 1;
247                                            coeff_VLC[intra][last][offset - level][run].len = 30;
248                  }                  }
249                  else if(i >= 8) {  #else
250                          *vlc1 = DCT3Dtab5[i - 8];                                  if (!intra)
251                          *vlc2 = DCT3Dtab2[i - 8];                                  {
252                                            coeff_VLC[intra][last][0][run].code
253                                                    = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-32 & 0xfff) << 1) | 1;
254                                            coeff_VLC[intra][last][0][run].len = 30;
255                  }                  }
256                  else {  #endif
                         *vlc1 = ERRtab[i];  
                         *vlc2 = ERRtab[i];  
257                  }                  }
258    /* init sprite_trajectory tables */
259    /* even if GMC is not specified (it might be used later...) */
260    
261                  vlc1++;          sprite_trajectory_code[0+16384].code = 0;
262                  vlc2++;          sprite_trajectory_code[0+16384].len = 0;
263          }          for (k=0;k<14;k++)
264          DCT3D[0] = DCT3Dinter;          {
265          DCT3D[1] = DCT3Dintra;                  int limit = (1<<k);
266    
267                    for (l=-(2*limit-1); l <= -limit; l++)
268                    {
269                            sprite_trajectory_code[i+16384].code = (2*limit-1)+l;
270                            sprite_trajectory_code[i+16384].len = k+1;
271  }  }
272    
273  void destroy_vlc_tables(void) {                  for (l=limit; l<= 2*limit-1; l++)
274                    {
275          if(intra_table != NULL && inter_table != NULL) {                          sprite_trajectory_code[i+16384].code = l;
276                  intra_table -= 64*255; // uncenter vlc tables                          sprite_trajectory_code[i+16384].len = k+1;
                 inter_table -= 64*255; // uncenter vlc tables  
   
                 free(intra_table);  
                 free(inter_table);  
277          }          }
   
         if(DCT3D[0] != NULL && DCT3D[1] != NULL) {  
                 free(DCT3D[0]);  
                 free(DCT3D[1]);  
278          }          }
   
279  }  }
280    
281  static __inline void CodeVector(Bitstream *bs, int16_t value, int16_t f_code, Statistics *pStat)  static __inline void
282    CodeVector(Bitstream * bs,
283                       int32_t value,
284                       int32_t f_code,
285                       Statistics * pStat)
286  {  {
287    
288          const int scale_factor = 1 << (f_code - 1);          const int scale_factor = 1 << (f_code - 1);
289          const int cmp = scale_factor << 5;          const int cmp = scale_factor << 5;
290    
# Line 198  Line 297 
297      pStat->iMvSum += value * value;      pStat->iMvSum += value * value;
298      pStat->iMvCount++;      pStat->iMvCount++;
299    
300          if (value == 0)          if (value == 0) {
301                  BitstreamPutBits(bs, mb_motion_table[32].code, mb_motion_table[32].len);                  BitstreamPutBits(bs, mb_motion_table[32].code,
302      else {                                                   mb_motion_table[32].len);
303            } else {
304                  uint16_t length, code, mv_res, sign;                  uint16_t length, code, mv_res, sign;
305    
306                  length = 16 << f_code;                  length = 16 << f_code;
# Line 224  Line 324 
324                          code = -code;                          code = -code;
325    
326                  code += 32;                  code += 32;
327                  BitstreamPutBits(bs, mb_motion_table[code].code, mb_motion_table[code].len);                  BitstreamPutBits(bs, mb_motion_table[code].code,
328                                                     mb_motion_table[code].len);
329    
330                  if(f_code)                  if(f_code)
331                          BitstreamPutBits(bs, mv_res, f_code);                          BitstreamPutBits(bs, mv_res, f_code);
332    }    }
333    
334  }  }
335    
336    #ifdef BIGLUT
337    
338    static __inline void
339    CodeCoeff(Bitstream * bs,
340                      const int16_t qcoeff[64],
341                      VLC * table,
342                      const uint16_t * zigzag,
343                      uint16_t intra)
344    {
345    
 static __inline void CodeCoeff(Bitstream *bs, int16_t qcoeff[64], VLC *table,  
                                                            const uint16_t *zigzag, uint16_t intra) {  
346          uint32_t j, last;          uint32_t j, last;
347          short v;          short v;
348          VLC *vlc;          VLC *vlc;
349    
350          j = intra;          j = intra;
351          last = 1 + intra;          last = intra;
352    
353          while((v = qcoeff[zigzag[j++]]) == 0);          while (j < 64 && (v = qcoeff[zigzag[j]]) == 0)
354                    j++;
355    
356          do {          do {
357                  // count zeroes                  vlc = table + 64 * 2048 + (v << 6) + j - last;
358                  vlc = table + (clip_table[2048+v] << 6) + j - last;                  last = ++j;
                 last = j + 1;  
                 while(j < 64 && (v = qcoeff[zigzag[j++]]) == 0);  
359    
360                  // write code                  /* count zeroes */
361                    while (j < 64 && (v = qcoeff[zigzag[j]]) == 0)
362                            j++;
363    
364                    /* write code */
365                  if(j != 64) {                  if(j != 64) {
366                          BitstreamPutBits(bs, vlc->code, vlc->len);                          BitstreamPutBits(bs, vlc->code, vlc->len);
367                  } else {                  } else {
368                          vlc += 64*511;                          vlc += 64 * 4096;
369                          BitstreamPutBits(bs, vlc->code, vlc->len);                          BitstreamPutBits(bs, vlc->code, vlc->len);
370                          break;                          break;
371                  }                  }
372          } while(1);          } while(1);
373    
374    }
375    
376    #else
377    
378    static __inline void
379    CodeCoeffInter(Bitstream * bs,
380                      const int16_t qcoeff[64],
381                      const uint16_t * zigzag)
382    {
383            uint32_t i, run, prev_run, code, len;
384            int32_t level, prev_level, level_shifted;
385    
386            i       = 0;
387            run = 0;
388    
389            while (!(level = qcoeff[zigzag[i++]]))
390                    run++;
391    
392            prev_level = level;
393            prev_run   = run;
394            run = 0;
395    
396            while (i < 64)
397            {
398                    if ((level = qcoeff[zigzag[i++]]) != 0)
399                    {
400                            level_shifted = prev_level + 32;
401                            if (!(level_shifted & -64))
402                            {
403                                    code = coeff_VLC[0][0][level_shifted][prev_run].code;
404                                    len      = coeff_VLC[0][0][level_shifted][prev_run].len;
405                            }
406                            else
407                            {
408                                    code = (ESCAPE3 << 21) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1;
409                                    len  = 30;
410                            }
411                            BitstreamPutBits(bs, code, len);
412                            prev_level = level;
413                            prev_run   = run;
414                            run = 0;
415                    }
416                    else
417                            run++;
418            }
419    
420            level_shifted = prev_level + 32;
421            if (!(level_shifted & -64))
422            {
423                    code = coeff_VLC[0][1][level_shifted][prev_run].code;
424                    len      = coeff_VLC[0][1][level_shifted][prev_run].len;
425            }
426            else
427            {
428                    code = (ESCAPE3 << 21) | (1 << 20) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1;
429                    len  = 30;
430            }
431            BitstreamPutBits(bs, code, len);
432  }  }
433    
434    static __inline void
435    CodeCoeffIntra(Bitstream * bs,
436                      const int16_t qcoeff[64],
437                      const uint16_t * zigzag)
438    {
439            uint32_t i, abs_level, run, prev_run, code, len;
440            int32_t level, prev_level;
441    
442            i       = 1;
443            run = 0;
444    
445            while (!(level = qcoeff[zigzag[i++]]))
446                    run++;
447    
448            prev_level = level;
449            prev_run   = run;
450            run = 0;
451    
452            while (i < 64)
453            {
454                    if ((level = qcoeff[zigzag[i++]]) != 0)
455                    {
456                            abs_level = ABS(prev_level);
457                            abs_level = abs_level < 64 ? abs_level : 0;
458                            code      = coeff_VLC[1][0][abs_level][prev_run].code;
459                            len               = coeff_VLC[1][0][abs_level][prev_run].len;
460                            if (len != 128)
461                                    code |= (prev_level < 0);
462                            else
463                            {
464                            code = (ESCAPE3 << 21) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1;
465                                    len  = 30;
466                            }
467                            BitstreamPutBits(bs, code, len);
468                            prev_level = level;
469                            prev_run   = run;
470                            run = 0;
471                    }
472                    else
473                            run++;
474            }
475    
476            abs_level = ABS(prev_level);
477            abs_level = abs_level < 64 ? abs_level : 0;
478            code      = coeff_VLC[1][1][abs_level][prev_run].code;
479            len               = coeff_VLC[1][1][abs_level][prev_run].len;
480            if (len != 128)
481                    code |= (prev_level < 0);
482            else
483            {
484                    code = (ESCAPE3 << 21) | (1 << 20) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1;
485                    len  = 30;
486            }
487            BitstreamPutBits(bs, code, len);
488    }
489    
490  static void CodeBlockIntra(const MBParam * pParam, const MACROBLOCK *pMB,  #endif
491                                                                    int16_t qcoeff[][64], Bitstream * bs, Statistics * pStat)  
492    static __inline void
493    CodeBlockIntra(const FRAMEINFO * const frame,
494                               const MACROBLOCK * pMB,
495                               int16_t qcoeff[6 * 64],
496                               Bitstream * bs,
497                               Statistics * pStat)
498  {  {
499    
500          uint32_t i, mcbpc, cbpy, bits;          uint32_t i, mcbpc, cbpy, bits;
501    
502          cbpy = pMB->cbp >> 2;          cbpy = pMB->cbp >> 2;
503    
504      // write mcbpc      // write mcbpc
505          if(pParam->coding_type == I_VOP) {          if (frame->coding_type == I_VOP) {
506              mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2);              mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2);
507                  BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, mcbpc_intra_tab[mcbpc].len);                  BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code,
508          }                                                   mcbpc_intra_tab[mcbpc].len);
509          else {          } else {
510              mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);              mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);
511                  BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len);                  BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,
512                                                     mcbpc_inter_tab[mcbpc].len);
513          }          }
514    
515          // ac prediction flag          // ac prediction flag
# Line 291  Line 525 
525      if(pMB->mode == MODE_INTRA_Q)      if(pMB->mode == MODE_INTRA_Q)
526                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, pMB->dquant, 2);
527    
528            // write interlacing
529            if (frame->global_flags & XVID_INTERLACING) {
530                    BitstreamPutBit(bs, pMB->field_dct);
531            }
532          // code block coeffs          // code block coeffs
533          for(i = 0; i < 6; i++)          for (i = 0; i < 6; i++) {
         {  
534                  if(i < 4)                  if(i < 4)
535                          BitstreamPutBits(bs, dcy_tab[qcoeff[i][0] + 255].code,                          BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code,
536                                                           dcy_tab[qcoeff[i][0] + 255].len);                                                           dcy_tab[qcoeff[i * 64 + 0] + 255].len);
537                  else                  else
538                          BitstreamPutBits(bs, dcc_tab[qcoeff[i][0] + 255].code,                          BitstreamPutBits(bs, dcc_tab[qcoeff[i * 64 + 0] + 255].code,
539                                           dcc_tab[qcoeff[i][0] + 255].len);                                                           dcc_tab[qcoeff[i * 64 + 0] + 255].len);
540    
541                    if (pMB->cbp & (1 << (5 - i))) {
542                            const uint16_t *scan_table =
543                                    frame->global_flags & XVID_ALTERNATESCAN ?
544                                    scan_tables[2] : scan_tables[pMB->acpred_directions[i]];
545    
                 if(pMB->cbp & (1 << (5 - i)))  
                 {  
546                          bits = BitstreamPos(bs);                          bits = BitstreamPos(bs);
547    
548                          CodeCoeff(bs, qcoeff[i], intra_table, scan_tables[pMB->acpred_directions[i]], 1);  #ifdef BIGLUT
549                            CodeCoeff(bs, &qcoeff[i * 64], intra_table, scan_table, 1);
550    #else
551                            CodeCoeffIntra(bs, &qcoeff[i * 64], scan_table);
552    #endif
553    
554                          bits = BitstreamPos(bs) - bits;                          bits = BitstreamPos(bs) - bits;
555                          pStat->iTextBits += bits;                          pStat->iTextBits += bits;
556                  }                  }
557          }          }
558    
559  }  }
560    
561    
562  static void CodeBlockInter(const MBParam * pParam, const MACROBLOCK *pMB,  static void
563                                                                    int16_t qcoeff[][64], Bitstream * bs, Statistics * pStat)  CodeBlockInter(const FRAMEINFO * const frame,
564                               const MACROBLOCK * pMB,
565                               int16_t qcoeff[6 * 64],
566                               Bitstream * bs,
567                               Statistics * pStat)
568  {  {
569    
570          int32_t i;          int32_t i;
571          uint32_t bits, mcbpc, cbpy;          uint32_t bits, mcbpc, cbpy;
572    
# Line 324  Line 574 
574          cbpy = 15 - (pMB->cbp >> 2);          cbpy = 15 - (pMB->cbp >> 2);
575    
576          // write mcbpc          // write mcbpc
577      BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len);          BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,
578                                             mcbpc_inter_tab[mcbpc].len);
579    
580            if ( (frame->coding_type == S_VOP) && (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) )
581                    BitstreamPutBit(bs, pMB->mcsel);                // mcsel: '0'=local motion, '1'=GMC
582    
583          // write cbpy          // write cbpy
584          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);
# Line 333  Line 587 
587      if(pMB->mode == MODE_INTER_Q)      if(pMB->mode == MODE_INTER_Q)
588                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, pMB->dquant, 2);
589    
590          // code motion vector(s)          // interlacing
591          for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++)          if (frame->global_flags & XVID_INTERLACING) {
592          {                  if (pMB->cbp) {
593                  CodeVector(bs, pMB->pmvs[i].x, pParam->fixed_code, pStat);                          BitstreamPutBit(bs, pMB->field_dct);
594                  CodeVector(bs, pMB->pmvs[i].y, pParam->fixed_code, pStat);                          DPRINTF(DPRINTF_MB,"codep: field_dct: %i", pMB->field_dct);
595                    }
596    
597                    // if inter block, write field ME flag
598                    if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
599                            BitstreamPutBit(bs, pMB->field_pred);
600                            DPRINTF(DPRINTF_MB,"codep: field_pred: %i", pMB->field_pred);
601    
602                            // write field prediction references
603                            if (pMB->field_pred) {
604                                    BitstreamPutBit(bs, pMB->field_for_top);
605                                    BitstreamPutBit(bs, pMB->field_for_bot);
606                            }
607                    }
608            }
609            // code motion vector(s) if motion is local
610            if (!pMB->mcsel)
611                    for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) {
612                            CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat);
613                            CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat);
614          }          }
615    
616          bits = BitstreamPos(bs);          bits = BitstreamPos(bs);
# Line 345  Line 618 
618          // code block coeffs          // code block coeffs
619          for(i = 0; i < 6; i++)          for(i = 0; i < 6; i++)
620                  if(pMB->cbp & (1 << (5 - i)))                  if(pMB->cbp & (1 << (5 - i)))
621                          CodeCoeff(bs, qcoeff[i], inter_table, scan_tables[0], 0);                  {
622                            const uint16_t *scan_table =
623                                    frame->global_flags & XVID_ALTERNATESCAN ?
624                                    scan_tables[2] : scan_tables[0];
625    
626    #ifdef BIGLUT
627                            CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_table, 0);
628    #else
629                            CodeCoeffInter(bs, &qcoeff[i * 64], scan_table);
630    #endif
631                    }
632    
633          bits = BitstreamPos(bs) - bits;          bits = BitstreamPos(bs) - bits;
634          pStat->iTextBits += bits;          pStat->iTextBits += bits;
635  }  }
636    
637    
638  void MBCoding(const MBParam * pParam, MACROBLOCK *pMB,  void
639                int16_t qcoeff[][64],  MBCoding(const FRAMEINFO * const frame,
640                    Bitstream * bs, Statistics * pStat)                   MACROBLOCK * pMB,
641                     int16_t qcoeff[6 * 64],
642                     Bitstream * bs,
643                     Statistics * pStat)
644  {  {
645          int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q);          if (frame->coding_type != I_VOP)
646                            BitstreamPutBit(bs, 0); // not_coded
647    
648            if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)
649                    CodeBlockIntra(frame, pMB, qcoeff, bs, pStat);
650            else
651                    CodeBlockInter(frame, pMB, qcoeff, bs, pStat);
652    
653      if(pParam->coding_type == P_VOP) {  }
654                  if(pMB->cbp == 0 && pMB->mode == MODE_INTER &&  
655                          pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0)  /*
656    // moved to mbcoding.h so that in can be 'static __inline'
657    void
658    MBSkip(Bitstream * bs)
659                  {                  {
660                          BitstreamPutBit(bs, 1);         // not_coded          BitstreamPutBit(bs, 1); // not coded
661    }
662    */
663    
664    /***************************************************************
665     * bframe encoding start
666     ***************************************************************/
667    
668    /*
669            mbtype
670            0       1b              direct(h263)            mvdb
671            1       01b             interpolate mc+q        dbquant, mvdf, mvdb
672            2       001b    backward mc+q           dbquant, mvdb
673            3       0001b   forward mc+q            dbquant, mvdf
674    */
675    
676    static __inline void
677    put_bvop_mbtype(Bitstream * bs,
678                                    int value)
679    {
680            switch (value) {
681                    case MODE_FORWARD:
682                            BitstreamPutBit(bs, 0);
683                    case MODE_BACKWARD:
684                            BitstreamPutBit(bs, 0);
685                    case MODE_INTERPOLATE:
686                            BitstreamPutBit(bs, 0);
687                    case MODE_DIRECT:
688                            BitstreamPutBit(bs, 1);
689                    default:
690                            break;
691            }
692    }
693    
694    /*
695            dbquant
696            -2      10b
697            0       0b
698            +2      11b
699    */
700    
701    static __inline void
702    put_bvop_dbquant(Bitstream * bs,
703                                     int value)
704    {
705            switch (value) {
706            case 0:
707                    BitstreamPutBit(bs, 0);
708                    return;
709    
710            case -2:
711                    BitstreamPutBit(bs, 1);
712                    BitstreamPutBit(bs, 0);
713                    return;
714    
715            case 2:
716                    BitstreamPutBit(bs, 1);
717                    BitstreamPutBit(bs, 1);
718                          return;                          return;
719    
720            default:;                                       // invalid
721                  }                  }
                 else  
                         BitstreamPutBit(bs, 0);         // coded  
722          }          }
723    
724          if(intra)  
725                  CodeBlockIntra(pParam, pMB, qcoeff, bs, pStat);  
726          else  void
727                  CodeBlockInter(pParam, pMB, qcoeff, bs, pStat);  MBCodingBVOP(const MACROBLOCK * mb,
728                             const int16_t qcoeff[6 * 64],
729                             const int32_t fcode,
730                             const int32_t bcode,
731                             Bitstream * bs,
732                             Statistics * pStat,
733                             int direction)
734    {
735            int vcode = fcode;
736            unsigned int i;
737    
738    /*      ------------------------------------------------------------------
739                    when a block is skipped it is decoded DIRECT(0,0)
740                    hence is interpolated from forward & backward frames
741            ------------------------------------------------------------------ */
742    
743            if (mb->mode == MODE_DIRECT_NONE_MV) {
744                    BitstreamPutBit(bs, 1); // skipped
745                    return;
746            }
747    
748            BitstreamPutBit(bs, 0);         // not skipped
749    
750            if (mb->cbp == 0) {
751                    BitstreamPutBit(bs, 1); // cbp == 0
752            } else {
753                    BitstreamPutBit(bs, 0); // cbp == xxx
754            }
755    
756            put_bvop_mbtype(bs, mb->mode);
757    
758            if (mb->cbp) {
759                    BitstreamPutBits(bs, mb->cbp, 6);
760            }
761    
762            if (mb->mode != MODE_DIRECT && mb->cbp != 0) {
763                    put_bvop_dbquant(bs, 0);        // todo: mb->dquant = 0
764            }
765    
766            switch (mb->mode) {
767                    case MODE_INTERPOLATE:
768                            CodeVector(bs, mb->pmvs[1].x, vcode, pStat); //forward vector of interpolate mode
769                            CodeVector(bs, mb->pmvs[1].y, vcode, pStat);
770                    case MODE_BACKWARD:
771                            vcode = bcode;
772                    case MODE_FORWARD:
773                            CodeVector(bs, mb->pmvs[0].x, vcode, pStat);
774                            CodeVector(bs, mb->pmvs[0].y, vcode, pStat);
775                            break;
776                    case MODE_DIRECT:
777                            CodeVector(bs, mb->pmvs[3].x, 1, pStat);        // fcode is always 1 for delta vector
778                            CodeVector(bs, mb->pmvs[3].y, 1, pStat);        // prediction is always (0,0)
779                    default: break;
780            }
781    
782            for (i = 0; i < 6; i++) {
783                    if (mb->cbp & (1 << (5 - i))) {
784    #ifdef BIGLUT
785                            CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0);
786    #else
787                            CodeCoeffInter(bs, &qcoeff[i * 64], scan_tables[0]);
788    #endif
789                    }
790  }  }
791    }
792    
793    
794    
795  /***************************************************************  /***************************************************************
796   * decoding stuff starts here                                  *   * decoding stuff starts here                                  *
797   ***************************************************************/   ***************************************************************/
798    
799  int get_mcbpc_intra(Bitstream * bs)  
800    // for IVOP addbits == 0
801    // for PVOP addbits == fcode - 1
802    // for BVOP addbits == max(fcode,bcode) - 1
803    // returns true or false
804    int
805    check_resync_marker(Bitstream * bs, int addbits)
806    {
807            uint32_t nbits;
808            uint32_t code;
809            uint32_t nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER + addbits;
810    
811            nbits = BitstreamNumBitsToByteAlign(bs);
812            code = BitstreamShowBits(bs, nbits);
813    
814            if (code == (((uint32_t)1 << (nbits - 1)) - 1))
815            {
816                    return BitstreamShowBitsFromByteAlign(bs, nbitsresyncmarker) == RESYNC_MARKER;
817            }
818    
819            return 0;
820    }
821    
822    
823    
824    int
825    get_mcbpc_intra(Bitstream * bs)
826  {  {
         uint32_t index;  
827    
828          while((index = BitstreamShowBits(bs, 9)) == 1)          uint32_t index;
                 BitstreamSkip(bs, 9);  
829    
830            index = BitstreamShowBits(bs, 9);
831          index >>= 3;          index >>= 3;
832    
833          BitstreamSkip(bs, mcbpc_intra_table[index].len);          BitstreamSkip(bs, mcbpc_intra_table[index].len);
834    
835          return mcbpc_intra_table[index].code;          return mcbpc_intra_table[index].code;
836    
837  }  }
838    
839  int get_mcbpc_inter(Bitstream * bs)  int
840    get_mcbpc_inter(Bitstream * bs)
841  {  {
842    
843          uint32_t index;          uint32_t index;
844    
845          while((index = CLIP(BitstreamShowBits(bs, 9), 256)) == 1)          index = MIN(BitstreamShowBits(bs, 9), 256);
                 BitstreamSkip(bs, 9);  
846    
847      BitstreamSkip(bs,  mcbpc_inter_table[index].len);      BitstreamSkip(bs,  mcbpc_inter_table[index].len);
848    
849          return mcbpc_inter_table[index].code;          return mcbpc_inter_table[index].code;
850    
851  }  }
852    
853  int get_cbpy(Bitstream * bs, int intra)  int
854    get_cbpy(Bitstream * bs,
855                     int intra)
856  {  {
857    
858          int cbpy;          int cbpy;
859          uint32_t index = BitstreamShowBits(bs, 6);          uint32_t index = BitstreamShowBits(bs, 6);
860    
# Line 416  Line 865 
865                  cbpy = 15 - cbpy;                  cbpy = 15 - cbpy;
866    
867          return cbpy;          return cbpy;
868    
869  }  }
870    
871  int get_mv_data(Bitstream * bs)  static __inline int
872    get_mv_data(Bitstream * bs)
873  {  {
874    
875          uint32_t index;          uint32_t index;
876    
877          if(BitstreamGetBit(bs))          if(BitstreamGetBit(bs))
# Line 427  Line 879 
879    
880          index = BitstreamShowBits(bs, 12);          index = BitstreamShowBits(bs, 12);
881    
882          if(index >= 512)          if (index >= 512) {
         {  
883                  index = (index >> 8) - 2;                  index = (index >> 8) - 2;
884                  BitstreamSkip(bs, TMNMVtab0[index].len);                  BitstreamSkip(bs, TMNMVtab0[index].len);
885                  return TMNMVtab0[index].code;                  return TMNMVtab0[index].code;
886          }          }
887    
888          if(index >= 128)          if (index >= 128) {
         {  
889                  index = (index >> 2) - 32;                  index = (index >> 2) - 32;
890                  BitstreamSkip(bs, TMNMVtab1[index].len);                  BitstreamSkip(bs, TMNMVtab1[index].len);
891                  return TMNMVtab1[index].code;                  return TMNMVtab1[index].code;
# Line 445  Line 895 
895    
896          BitstreamSkip(bs, TMNMVtab2[index].len);          BitstreamSkip(bs, TMNMVtab2[index].len);
897          return TMNMVtab2[index].code;          return TMNMVtab2[index].code;
898    
899  }  }
900    
901  int get_mv(Bitstream * bs, int fcode)  int
902    get_mv(Bitstream * bs,
903               int fcode)
904  {  {
905    
906          int data;          int data;
907          int res;          int res;
908          int mv;          int mv;
# Line 463  Line 917 
917          mv = ((ABS(data) - 1) * scale_fac) + res + 1;          mv = ((ABS(data) - 1) * scale_fac) + res + 1;
918    
919          return data < 0 ? -mv : mv;          return data < 0 ? -mv : mv;
920    
921  }  }
922    
923  int get_dc_dif(Bitstream * bs, uint32_t dc_size)  int
924    get_dc_dif(Bitstream * bs,
925                       uint32_t dc_size)
926  {  {
927    
928          int code = BitstreamGetBits(bs, dc_size);          int code = BitstreamGetBits(bs, dc_size);
929          int msb = code >> (dc_size - 1);          int msb = code >> (dc_size - 1);
930    
# Line 474  Line 932 
932                  return (-1 * (code^((1 << dc_size) - 1)));                  return (-1 * (code^((1 << dc_size) - 1)));
933    
934          return code;          return code;
935    
936  }  }
937    
938  int get_dc_size_lum(Bitstream * bs)  int
939    get_dc_size_lum(Bitstream * bs)
940  {  {
941    
942          int code, i;          int code, i;
943    
944          code = BitstreamShowBits(bs, 11);          code = BitstreamShowBits(bs, 11);
945    
946          for(i = 11; i > 3; i--) {          for(i = 11; i > 3; i--) {
# Line 491  Line 953 
953    
954          BitstreamSkip(bs, dc_lum_tab[code].len);          BitstreamSkip(bs, dc_lum_tab[code].len);
955          return dc_lum_tab[code].code;          return dc_lum_tab[code].code;
956    
957  }  }
958    
959    
960  int get_dc_size_chrom(Bitstream * bs)  int
961    get_dc_size_chrom(Bitstream * bs)
962  {  {
963    
964          uint32_t code, i;          uint32_t code, i;
965    
966          code = BitstreamShowBits(bs, 12);          code = BitstreamShowBits(bs, 12);
967    
968          for(i = 12; i > 2; i--) {          for(i = 12; i > 2; i--) {
# Line 508  Line 974 
974          }          }
975    
976          return 3 - BitstreamGetBits(bs, 2);          return 3 - BitstreamGetBits(bs, 2);
977    
978  }  }
979    
980  int get_coeff(Bitstream * bs, int *run, int *last, int intra, int short_video_header)  static __inline int
981    get_coeff(Bitstream * bs,
982                      int *run,
983                      int *last,
984                      int intra,
985                      int short_video_header)
986  {  {
987    
988      uint32_t mode;      uint32_t mode;
     const VLC *tab;  
989          int32_t level;          int32_t level;
990            REVERSE_EVENT *reverse_event;
991    
992          if(short_video_header) // inter-VLCs will be used for both intra and inter blocks          if (short_video_header)         /* inter-VLCs will be used for both intra and inter blocks */
993                  intra = 0;                  intra = 0;
994    
995          tab = &DCT3D[intra][BitstreamShowBits(bs, 12)];          if (BitstreamShowBits(bs, 7) != ESCAPE) {
996                    reverse_event = &DCT3D[intra][BitstreamShowBits(bs, 12)];
997    
998          if(tab->code == -1)                  if ((level = reverse_event->event.level) == 0)
999                  goto error;                  goto error;
1000    
1001          BitstreamSkip(bs, tab->len);                  *last = reverse_event->event.last;
1002                    *run  = reverse_event->event.run;
1003    
1004          if(tab->code != ESCAPE) {                  BitstreamSkip(bs, reverse_event->len);
1005                  if(!intra)  
1006                  {                  return BitstreamGetBits(bs, 1) ? -level : level;
                         *run = (tab->code >> 4) & 255;  
                         level = tab->code & 15;  
                         *last = (tab->code >> 12) & 1;  
                 }  
             else  
                 {  
                         *run = (tab->code >> 8) & 255;  
                         level = tab->code & 255;  
                         *last = (tab->code >> 16) & 1;  
                 }  
                 return BitstreamGetBit(bs) ? -level : level;  
1007          }          }
1008    
1009          if(short_video_header)          BitstreamSkip(bs, 7);
1010          {  
1011                  // escape mode 4 - H.263 type, only used if short_video_header = 1          if (short_video_header) {
1012                    /* escape mode 4 - H.263 type, only used if short_video_header = 1  */
1013                  *last = BitstreamGetBit(bs);                  *last = BitstreamGetBit(bs);
1014                  *run = BitstreamGetBits(bs, 6);                  *run = BitstreamGetBits(bs, 6);
1015                  level = BitstreamGetBits(bs, 8);                  level = BitstreamGetBits(bs, 8);
1016    
1017                  if (level == 0 || level == 128)                  if (level == 0 || level == 128)
1018                          DEBUG1("Illegal LEVEL for ESCAPE mode 4:", level);                          DPRINTF(DPRINTF_ERROR, "Illegal LEVEL for ESCAPE mode 4: %d", level);
1019    
1020                  return (level >= 128 ? -(256 - level) : level);                  return (level << 24) >> 24;
1021          }          }
1022    
1023          mode = BitstreamShowBits(bs, 2);          mode = BitstreamShowBits(bs, 2);
# Line 560  Line 1025 
1025          if(mode < 3) {          if(mode < 3) {
1026                  BitstreamSkip(bs, (mode == 2) ? 2 : 1);                  BitstreamSkip(bs, (mode == 2) ? 2 : 1);
1027    
1028                  tab = &DCT3D[intra][BitstreamShowBits(bs, 12)];                  reverse_event = &DCT3D[intra][BitstreamShowBits(bs, 12)];
1029                  if (tab->code == -1)  
1030                    if ((level = reverse_event->event.level) == 0)
1031                          goto error;                          goto error;
1032    
1033                  BitstreamSkip(bs, tab->len);                  *last = reverse_event->event.last;
1034                    *run  = reverse_event->event.run;
1035    
1036                  if (!intra) {                  BitstreamSkip(bs, reverse_event->len);
                         *run = (tab->code >> 4) & 255;  
                         level = tab->code & 15;  
                         *last = (tab->code >> 12) & 1;  
                 }  
                 else  
                 {  
                         *run = (tab->code >> 8) & 255;  
                         level = tab->code & 255;  
                         *last = (tab->code >> 16) & 1;  
                 }  
1037    
1038                  if(mode < 2) // first escape mode, level is offset                  if (mode < 2)                   /* first escape mode, level is offset */
1039                          level += max_level[*last + (!intra<<1)][*run]; // need to add back the max level                          level += max_level[intra][*last][*run];
1040                  else if(mode == 2)  // second escape mode, run is offset                  else                                    /* second escape mode, run is offset */
1041                          *run += max_run[*last + (!intra<<1)][level] + 1;                          *run += max_run[intra][*last][level] + 1;
1042    
1043                  return BitstreamGetBit(bs) ? -level : level;                  return BitstreamGetBits(bs, 1) ? -level : level;
1044          }          }
1045    
1046          // third escape mode - fixed length codes          /* third escape mode - fixed length codes */
1047          BitstreamSkip(bs, 2);          BitstreamSkip(bs, 2);
1048          *last = BitstreamGetBits(bs, 1);          *last = BitstreamGetBits(bs, 1);
1049          *run = BitstreamGetBits(bs, 6);          *run = BitstreamGetBits(bs, 6);
1050          BitstreamSkip(bs, 1);                           // marker          BitstreamSkip(bs, 1);           /* marker */
1051          level = BitstreamGetBits(bs, 12);          level = BitstreamGetBits(bs, 12);
1052          BitstreamSkip(bs, 1);                           // marker          BitstreamSkip(bs, 1);           /* marker */
1053    
1054          return (level & 0x800) ? (level | (-1 ^ 0xfff)) : level;          return (level << 20) >> 20;
1055    
1056  error:  error:
1057          *run = VLC_ERROR;          *run = VLC_ERROR;
1058          return 0;          return 0;
1059  }  }
1060    
1061    void
1062  void get_intra_block(Bitstream * bs, int16_t * block, int direction, int coeff)  get_intra_block(Bitstream * bs,
1063                                    int16_t * block,
1064                                    int direction,
1065                                    int coeff)
1066  {  {
1067    
1068          const uint16_t * scan = scan_tables[ direction ];          const uint16_t * scan = scan_tables[ direction ];
1069          int level;          int level, run, last;
         int run;  
         int last;  
1070    
1071          do          do {
         {  
1072                  level = get_coeff(bs, &run, &last, 1, 0);                  level = get_coeff(bs, &run, &last, 1, 0);
1073                  if (run == -1)                  if (run == -1) {
1074                  {                          DPRINTF(DPRINTF_ERROR,"fatal: invalid run");
                         DEBUG("fatal: invalid run");  
1075                          break;                          break;
1076                  }                  }
1077                  coeff += run;                  coeff += run;
1078                  block[ scan[coeff] ] = level;                  block[ scan[coeff] ] = level;
1079                  if (level < -127 || level > 127)  
1080                  {                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level);
1081                          DEBUG1("warning: intra_overflow", level);                  //DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[coeff], level, BitstreamShowBits(bs, 32));
1082    
1083                    if (level < -2047 || level > 2047) {
1084                            DPRINTF(DPRINTF_ERROR,"warning: intra_overflow %i", level);
1085                  }                  }
1086                  coeff++;                  coeff++;
1087          } while (!last);          } while (!last);
1088    
1089  }  }
1090    
1091  void get_inter_block(Bitstream * bs, int16_t * block)  void
1092    get_inter_block(Bitstream * bs,
1093                                    int16_t * block,
1094                                    int direction)
1095  {  {
1096          const uint16_t * scan = scan_tables[0];  
1097            const uint16_t *scan = scan_tables[direction];
1098          int p;          int p;
1099          int level;          int level;
1100          int run;          int run;
1101          int last;          int last;
1102    
1103          p = 0;          p = 0;
1104          do          do {
         {  
1105                  level = get_coeff(bs, &run, &last, 0, 0);                  level = get_coeff(bs, &run, &last, 0, 0);
1106                  if (run == -1)                  if (run == -1) {
1107                  {                          DPRINTF(DPRINTF_ERROR,"fatal: invalid run");
                         DEBUG("fatal: invalid run");  
1108                          break;                          break;
1109                  }                  }
1110                  p += run;                  p += run;
1111    
1112                  block[ scan[p] ] = level;                  block[ scan[p] ] = level;
1113                  if (level < -127 || level > 127)  
1114                  {                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level);
1115                          DEBUG1("warning: inter_overflow", level);                  // DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[p], level, BitstreamShowBits(bs, 32));
1116    
1117                    if (level < -2047 || level > 2047) {
1118                            DPRINTF(DPRINTF_ERROR,"warning: inter overflow %i", level);
1119                  }                  }
1120                  p++;                  p++;
1121          } while (!last);          } while (!last);
1122    
1123  }  }

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.25.2.11

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