[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.36, Sat Jan 4 04:28:48 2003 UTC revision 1.41, Thu Feb 13 17:31:33 2003 UTC
# Line 66  Line 66 
66  #define ABS(X) (((X)>0)?(X):-(X))  #define ABS(X) (((X)>0)?(X):-(X))
67  #define CLIP(X,A) (X > A) ? (A) : (X)  #define CLIP(X,A) (X > A) ? (A) : (X)
68    
69    /* #define BIGLUT */
70    
71    #ifdef BIGLUT
72    #define LEVELOFFSET 2048
73    #else
74    #define LEVELOFFSET 32
75    #endif
76    
77  /*****************************************************************************  /*****************************************************************************
78   * Local data   * Local data
79   ****************************************************************************/   ****************************************************************************/
80    
81  /* msvc sp5+pp gets confused if they globals are made static */  static REVERSE_EVENT DCT3D[2][4096];
 static VLC intra_table[524288];  
 static VLC inter_table[524288];  
82    
83  static VLC DCT3Dintra[4096];  #ifdef BIGLUT
84  static VLC DCT3Dinter[4096];  static VLC coeff_VLC[2][2][4096][64];
85    static VLC *intra_table, *inter_table;
86    #else
87    static VLC coeff_VLC[2][2][64][64];
88    #endif
89    
90  /*****************************************************************************  /*****************************************************************************
91   * Vector Length Coding Initialization   * Vector Length Coding Initialization
# Line 84  Line 94 
94  void  void
95  init_vlc_tables(void)  init_vlc_tables(void)
96  {  {
97            uint32_t i, j, intra, last, run,  run_esc, level, level_esc, escape, escape_len, offset;
98    
99          int32_t k, l, i, intra, last;  #ifdef BIGLUT
100          VLC *vlc[2];          intra_table = (VLC*)coeff_VLC[1];
101          VLC const **coeff_ptr;          inter_table = (VLC*)coeff_VLC[0];
102          VLC *vlc1, *vlc2;  #endif
103    
         vlc1 = DCT3Dintra;  
         vlc2 = DCT3Dinter;  
104    
105          vlc[0] = intra_table;          for (intra = 0; intra < 2; intra++)
106          vlc[1] = inter_table;                  for (i = 0; i < 4096; i++)
107                            DCT3D[intra][i].event.level = 0;
108    
109          /*          for (intra = 0; intra < 2; intra++)
110           * Generate encoding vlc lookup tables                  for (last = 0; last < 2; last++)
111           * the lookup table idea is taken from the excellent fame project                  {
112           * by Vivien Chapellier                          for (run = 0; run < 63 + last; run++)
113           */                                  for (level = 0; level < (uint32_t)(32 << intra); level++)
114          for (i = 0; i < 4; i++) {                                  {
115                  intra = i % 2;  #ifdef BIGLUT
116                  last = i / 2;                                          offset = LEVELOFFSET;
117    #else
118                  coeff_ptr = coeff_vlc[last + 2 * intra];                                          offset = !intra * LEVELOFFSET;
119    #endif
120                                            coeff_VLC[intra][last][level + offset][run].len = 128;
121                                    }
122                    }
123    
124                  for (k = -2047; k < 2048; k++) {        /* level */          for (intra = 0; intra < 2; intra++)
125                          int8_t const *max_level_ptr = max_level[last + 2 * intra];                  for (i = 0; i < 102; i++)
126                          int8_t const *max_run_ptr = max_run[last + 2 * intra];                  {
127    #ifdef BIGLUT
128                            offset = LEVELOFFSET;
129    #else
130                            offset = !intra * LEVELOFFSET;
131    #endif
132                            for (j = 0; j < (uint32_t)(1 << (12 - coeff_tab[intra][i].vlc.len)); j++)
133                            {
134                                    DCT3D[intra][(coeff_tab[intra][i].vlc.code << (12 - coeff_tab[intra][i].vlc.len)) | j].len       = coeff_tab[intra][i].vlc.len;
135                                    DCT3D[intra][(coeff_tab[intra][i].vlc.code << (12 - coeff_tab[intra][i].vlc.len)) | j].event = coeff_tab[intra][i].event;
136                            }
137    
138                          for (l = 0; l < 64; l++) {      /* run */                          coeff_VLC[intra][coeff_tab[intra][i].event.last][coeff_tab[intra][i].event.level + offset][coeff_tab[intra][i].event.run].code
139                                  int32_t level = k;                                  = coeff_tab[intra][i].vlc.code << 1;
140                                  ptr_t run = l;                          coeff_VLC[intra][coeff_tab[intra][i].event.last][coeff_tab[intra][i].event.level + offset][coeff_tab[intra][i].event.run].len
141                                    = coeff_tab[intra][i].vlc.len + 1;
142    #ifndef BIGLUT
143                            if (!intra)
144    #endif
145                            {
146                                    coeff_VLC[intra][coeff_tab[intra][i].event.last][offset - coeff_tab[intra][i].event.level][coeff_tab[intra][i].event.run].code
147                                            = (coeff_tab[intra][i].vlc.code << 1) | 1;
148                                    coeff_VLC[intra][coeff_tab[intra][i].event.last][offset - coeff_tab[intra][i].event.level][coeff_tab[intra][i].event.run].len
149                                            = coeff_tab[intra][i].vlc.len + 1;
150                            }
151                    }
152    
153                                  if ((abs(level) <= max_level_ptr[run]) && (run <= (uint32_t) max_run_ptr[abs(level)])) {        /* level < max_level and run < max_run */          for (intra = 0; intra < 2; intra++)
154                    for (last = 0; last < 2; last++)
155                            for (run = 0; run < 63 + last; run++)
156                            {
157                                    for (level = 1; level < (uint32_t)(32 << intra); level++)
158                                    {
159                                            if (level <= max_level[intra][last][run] && run <= max_run[intra][last][level])
160                                                continue;
161    
162                                          vlc[intra]->code = 0;  #ifdef BIGLUT
163                                          vlc[intra]->len = 0;                                          offset = LEVELOFFSET;
164                                          goto loop_end;  #else
165                                  } else {                                          offset = !intra * LEVELOFFSET;
166                                          if (level > 0)  /* correct level */  #endif
167                                                  level -= max_level_ptr[run];                      level_esc = level - max_level[intra][last][run];
168                                            run_esc = run - 1 - max_run[intra][last][level];
169    
170                                            if (level_esc <= max_level[intra][last][run] && run <= max_run[intra][last][level_esc])
171                                            {
172                                                    escape     = ESCAPE1;
173                                                    escape_len = 7 + 1;
174                                                    run_esc    = run;
175                                            }
176                                          else                                          else
177                                                  level += max_level_ptr[run];                                          {
178                                                    if (run_esc <= max_run[intra][last][level] && level <= max_level[intra][last][run_esc])
179                                          if ((abs(level) <= max_level_ptr[run]) &&                                                  {
180                                                  (run <= (uint32_t) max_run_ptr[abs(level)])) {                                                          escape     = ESCAPE2;
181                                                            escape_len = 7 + 2;
182                                                  vlc[intra]->code = 0x06;                                                          level_esc  = level;
                                                 vlc[intra]->len = 8;  
                                                 goto loop_end;  
183                                          }                                          }
   
                                         if (level > 0)  /* still here? */  
                                                 level += max_level_ptr[run];    /* restore level */  
184                                          else                                          else
185                                                  level -= max_level_ptr[run];                                                  {
186    #ifndef BIGLUT
187                                          run -= max_run_ptr[abs(level)] + 1;     /* and change run */                                                          if (!intra)
188    #endif
189                                          if ((abs(level) <= max_level_ptr[run]) &&                                                          {
190                                                  (run <= (uint32_t) max_run_ptr[abs(level)])) {                                                                  coeff_VLC[intra][last][level + offset][run].code
191                                                                            = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((level & 0xfff) << 1) | 1;
192                                                  vlc[intra]->code = 0x0e;                                                                  coeff_VLC[intra][last][level + offset][run].len = 30;
193                                                  vlc[intra]->len = 9;                                                                          coeff_VLC[intra][last][offset - level][run].code
194                                                  goto loop_end;                                                                          = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-level & 0xfff) << 1) | 1;
195                                                                    coeff_VLC[intra][last][offset - level][run].len = 30;
196                                                            }
197                                                            continue;
198                                          }                                          }
                                         run += max_run_ptr[abs(level)] + 1;  
199                                  }                                  }
200    
201                                  vlc[intra]->code =                                          coeff_VLC[intra][last][level + offset][run].code
202                                          (uint32_t) ((l << 14) | (0x1e + last) << 20) | (1 << 13) |                                                  = (escape << coeff_VLC[intra][last][level_esc + offset][run_esc].len)
203                                          ((k & 0xfff) << 1) | 1;                                                  |  coeff_VLC[intra][last][level_esc + offset][run_esc].code;
204                                            coeff_VLC[intra][last][level + offset][run].len
205                                                    = coeff_VLC[intra][last][level_esc + offset][run_esc].len + escape_len;
206    #ifndef BIGLUT
207                                            if (!intra)
208    #endif
209                                            {
210                                                    coeff_VLC[intra][last][offset - level][run].code
211                                                            = (escape << coeff_VLC[intra][last][level_esc + offset][run_esc].len)
212                                                            |  coeff_VLC[intra][last][level_esc + offset][run_esc].code | 1;
213                                                    coeff_VLC[intra][last][offset - level][run].len
214                                                            = coeff_VLC[intra][last][level_esc + offset][run_esc].len + escape_len;
215                                            }
216                                    }
217    
218                                  vlc[intra]->len = 30;  #ifdef BIGLUT
219                                  vlc[intra]++;                                  for (level = (uint32_t)(32 << intra); level < 2048; level++)
220                                  continue;                                  {
221                                            coeff_VLC[intra][last][level + offset][run].code
222                                                    = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((level & 0xfff) << 1) | 1;
223                                            coeff_VLC[intra][last][level + offset][run].len = 30;
224    
225                            loop_end:                                          coeff_VLC[intra][last][offset - level][run].code
226                                  if (level != 0) {                                                  = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-level & 0xfff) << 1) | 1;
227                                          vlc[intra]->code =                                          coeff_VLC[intra][last][offset - level][run].len = 30;
                                                 (vlc[intra]->  
                                                  code << (coeff_ptr[run][abs(level) - 1].len +  
                                                                   1)) | (coeff_ptr[run][abs(level) -  
                                                                                                                 1].code << 1);  
                                         vlc[intra]->len =  
                                                 (coeff_ptr[run][abs(level) - 1].len + 1) +  
                                                 vlc[intra]->len;  
   
                                         if (level < 0)  
                                                 vlc[intra]->code += 1;  
                                 }  
   
                                 vlc[intra]++;  
228                          }                          }
229    #else
230                                    if (!intra)
231                                    {
232                                            coeff_VLC[intra][last][0][run].code
233                                                    = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-32 & 0xfff) << 1) | 1;
234                                            coeff_VLC[intra][last][0][run].len = 30;
235                  }                  }
236    #endif
237          }          }
238    
239          for (i = 0; i < 4096; i++) {          /* Shut up the compiler -- gcc 3.3 pre release */
240                  if (i >= 512) {          i = dc_threshold[0];
                         *vlc1 = DCT3Dtab3[(i >> 5) - 16];  
                         *vlc2 = DCT3Dtab0[(i >> 5) - 16];  
                 } else if (i >= 128) {  
                         *vlc1 = DCT3Dtab4[(i >> 2) - 32];  
                         *vlc2 = DCT3Dtab1[(i >> 2) - 32];  
                 } else if (i >= 8) {  
                         *vlc1 = DCT3Dtab5[i - 8];  
                         *vlc2 = DCT3Dtab2[i - 8];  
                 } else {  
                         *vlc1 = ERRtab[i];  
                         *vlc2 = ERRtab[i];  
                 }  
   
                 vlc1++;  
                 vlc2++;  
         }  
         DCT3D[0] = DCT3Dinter;  
         DCT3D[1] = DCT3Dintra;  
241    
242  }  }
243    
# Line 261  Line 300 
300    
301  }  }
302    
303    #ifdef BIGLUT
304    
305  static __inline void  static __inline void
306  CodeCoeff(Bitstream * bs,  CodeCoeff(Bitstream * bs,
307                    const int16_t qcoeff[64],                    const int16_t qcoeff[64],
# Line 280  Line 321 
321                  j++;                  j++;
322    
323          do {          do {
324                  vlc = table + 64 * 2047 + (v << 6) + j - last;                  vlc = table + 64 * 2048 + (v << 6) + j - last;
325                  last = ++j;                  last = ++j;
326    
327                  /* count zeroes */                  /* count zeroes */
# Line 291  Line 332 
332                  if (j != 64) {                  if (j != 64) {
333                          BitstreamPutBits(bs, vlc->code, vlc->len);                          BitstreamPutBits(bs, vlc->code, vlc->len);
334                  } else {                  } else {
335                          vlc += 64 * 4095;                          vlc += 64 * 4096;
336                          BitstreamPutBits(bs, vlc->code, vlc->len);                          BitstreamPutBits(bs, vlc->code, vlc->len);
337                          break;                          break;
338                  }                  }
# Line 299  Line 340 
340    
341  }  }
342    
343    #else
344    
345    static __inline void
346    CodeCoeffInter(Bitstream * bs,
347                      const int16_t qcoeff[64],
348                      const uint16_t * zigzag)
349    {
350            uint32_t i, run, prev_run, code, len;
351            int32_t level, prev_level, level_shifted;
352    
353            i       = 0;
354            run = 0;
355    
356            while (!(level = qcoeff[zigzag[i++]]))
357                    run++;
358    
359            prev_level = level;
360            prev_run   = run;
361            run = 0;
362    
363            while (i < 64)
364            {
365                    if ((level = qcoeff[zigzag[i++]]) != 0)
366                    {
367                            level_shifted = prev_level + 32;
368                            if (!(level_shifted & -64))
369                            {
370                                    code = coeff_VLC[0][0][level_shifted][prev_run].code;
371                                    len      = coeff_VLC[0][0][level_shifted][prev_run].len;
372                            }
373                            else
374                            {
375                                    code = (ESCAPE3 << 21) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1;
376                                    len  = 30;
377                            }
378                            BitstreamPutBits(bs, code, len);
379                            prev_level = level;
380                            prev_run   = run;
381                            run = 0;
382                    }
383                    else
384                            run++;
385            }
386    
387            level_shifted = prev_level + 32;
388            if (!(level_shifted & -64))
389            {
390                    code = coeff_VLC[0][1][level_shifted][prev_run].code;
391                    len      = coeff_VLC[0][1][level_shifted][prev_run].len;
392            }
393            else
394            {
395                    code = (ESCAPE3 << 21) | (1 << 20) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1;
396                    len  = 30;
397            }
398            BitstreamPutBits(bs, code, len);
399    }
400    
401    static __inline void
402    CodeCoeffIntra(Bitstream * bs,
403                      const int16_t qcoeff[64],
404                      const uint16_t * zigzag)
405    {
406            uint32_t i, abs_level, run, prev_run, code, len;
407            int32_t level, prev_level;
408    
409            i       = 1;
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                            abs_level = ABS(prev_level);
424                            abs_level = abs_level < 64 ? abs_level : 0;
425                            code      = coeff_VLC[1][0][abs_level][prev_run].code;
426                            len               = coeff_VLC[1][0][abs_level][prev_run].len;
427                            if (len != 128)
428                                    code |= (prev_level < 0);
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            abs_level = ABS(prev_level);
444            abs_level = abs_level < 64 ? abs_level : 0;
445            code      = coeff_VLC[1][1][abs_level][prev_run].code;
446            len               = coeff_VLC[1][1][abs_level][prev_run].len;
447            if (len != 128)
448                    code |= (prev_level < 0);
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    #endif
458    
459  /*****************************************************************************  /*****************************************************************************
460   * Local functions   * Local functions
461   ****************************************************************************/   ****************************************************************************/
# Line 355  Line 512 
512                  if (pMB->cbp & (1 << (5 - i))) {                  if (pMB->cbp & (1 << (5 - i))) {
513                          bits = BitstreamPos(bs);                          bits = BitstreamPos(bs);
514    
515    #ifdef BIGLUT
516                          CodeCoeff(bs, &qcoeff[i * 64], intra_table,                          CodeCoeff(bs, &qcoeff[i * 64], intra_table,
517                                            scan_tables[pMB->acpred_directions[i]], 1);                                            scan_tables[pMB->acpred_directions[i]], 1);
518    #else
519                            CodeCoeffIntra(bs, &qcoeff[i * 64], scan_tables[pMB->acpred_directions[i]]);
520    #endif
521                          bits = BitstreamPos(bs) - bits;                          bits = BitstreamPos(bs) - bits;
522                          pStat->iTextBits += bits;                          pStat->iTextBits += bits;
523                  }                  }
# Line 421  Line 581 
581          /* code block coeffs */          /* code block coeffs */
582          for (i = 0; i < 6; i++)          for (i = 0; i < 6; i++)
583                  if (pMB->cbp & (1 << (5 - i)))                  if (pMB->cbp & (1 << (5 - i)))
584    #ifdef BIGLUT
585                          CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0);                          CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0);
586    #else
587                            CodeCoeffInter(bs, &qcoeff[i * 64], scan_tables[0]);
588    #endif
589    
590          bits = BitstreamPos(bs) - bits;          bits = BitstreamPos(bs) - bits;
591          pStat->iTextBits += bits;          pStat->iTextBits += bits;
# Line 659  Line 823 
823  {  {
824    
825          uint32_t mode;          uint32_t mode;
         const VLC *tab;  
826          int32_t level;          int32_t level;
827            REVERSE_EVENT *reverse_event;
828    
829          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 */
830                  intra = 0;                  intra = 0;
831    
832          tab = &DCT3D[intra][BitstreamShowBits(bs, 12)];          if (BitstreamShowBits(bs, 7) != ESCAPE) {
833                    reverse_event = &DCT3D[intra][BitstreamShowBits(bs, 12)];
834    
835          if (tab->code == -1)                  if ((level = reverse_event->event.level) == 0)
836                  goto error;                  goto error;
837    
838          BitstreamSkip(bs, tab->len);                  *last = reverse_event->event.last;
839                    *run  = reverse_event->event.run;
840    
841          if (tab->code != ESCAPE) {                  BitstreamSkip(bs, reverse_event->len);
842                  if (!intra) {  
843                          *run = (tab->code >> 4) & 255;                  return BitstreamGetBits(bs, 1) ? -level : level;
                         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;  
844          }          }
845    
846            BitstreamSkip(bs, 7);
847    
848          if (short_video_header) {          if (short_video_header) {
849                  /* escape mode 4 - H.263 type, only used if short_video_header = 1  */                  /* escape mode 4 - H.263 type, only used if short_video_header = 1  */
850                  *last = BitstreamGetBit(bs);                  *last = BitstreamGetBit(bs);
# Line 694  Line 854 
854                  if (level == 0 || level == 128)                  if (level == 0 || level == 128)
855                          DPRINTF(DPRINTF_ERROR, "Illegal LEVEL for ESCAPE mode 4: %d", level);                          DPRINTF(DPRINTF_ERROR, "Illegal LEVEL for ESCAPE mode 4: %d", level);
856    
857                  return (level >= 128 ? -(256 - level) : level);                  return (level << 24) >> 24;
858          }          }
859    
860          mode = BitstreamShowBits(bs, 2);          mode = BitstreamShowBits(bs, 2);
# Line 702  Line 862 
862          if (mode < 3) {          if (mode < 3) {
863                  BitstreamSkip(bs, (mode == 2) ? 2 : 1);                  BitstreamSkip(bs, (mode == 2) ? 2 : 1);
864    
865                  tab = &DCT3D[intra][BitstreamShowBits(bs, 12)];                  reverse_event = &DCT3D[intra][BitstreamShowBits(bs, 12)];
866                  if (tab->code == -1)  
867                    if ((level = reverse_event->event.level) == 0)
868                          goto error;                          goto error;
869    
870                  BitstreamSkip(bs, tab->len);                  *last = reverse_event->event.last;
871                    *run  = reverse_event->event.run;
872    
873                  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;  
                 }  
874    
875                  if (mode < 2)                   /* first escape mode, level is offset */                  if (mode < 2)                   /* first escape mode, level is offset */
876                          level += max_level[*last + (!intra << 1)][*run];        /* need to add back the max level */                          level += max_level[intra][*last][*run];
877                  else if (mode == 2)             /* second escape mode, run is offset */                  else                                    /* second escape mode, run is offset */
878                          *run += max_run[*last + (!intra << 1)][level] + 1;                          *run += max_run[intra][*last][level] + 1;
879    
880                  return BitstreamGetBit(bs) ? -level : level;                  return BitstreamGetBits(bs, 1) ? -level : level;
881          }          }
882    
883          /* third escape mode - fixed length codes */          /* third escape mode - fixed length codes */
884          BitstreamSkip(bs, 2);          BitstreamSkip(bs, 2);
885          *last = BitstreamGetBits(bs, 1);          *last = BitstreamGetBits(bs, 1);
# Line 733  Line 888 
888          level = BitstreamGetBits(bs, 12);          level = BitstreamGetBits(bs, 12);
889          BitstreamSkip(bs, 1);           /* marker */          BitstreamSkip(bs, 1);           /* marker */
890    
891          return (level & 0x800) ? (level | (-1 ^ 0xfff)) : level;          return (level << 20) >> 20;
892    
893    error:    error:
894          *run = VLC_ERROR;          *run = VLC_ERROR;
895          return 0;          return 0;
   
896  }  }
897    
898  /*****************************************************************************  /*****************************************************************************

Legend:
Removed from v.1.36  
changed lines
  Added in v.1.41

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