[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.25, Wed Sep 4 03:23:28 2002 UTC revision 1.25.2.12, Fri Jan 17 16:05:27 2003 UTC
# Line 41  Line 41 
41    *                                                                                                                                                        *    *                                                                                                                                                        *
42    *  Revision history:                                                         *    *  Revision history:                                                         *
43    *                                                                            *    *                                                                            *
44      *  28.10.2002 GMC support - gruel                                                                                        *
45    *  28.06.2002 added check_resync_marker()                                    *    *  28.06.2002 added check_resync_marker()                                    *
46    *  14.04.2002 bframe encoding                                                                                            *    *  14.04.2002 bframe encoding                                                                                            *
47    *  08.03.2002 initial version; isibaar                                                           *    *  08.03.2002 initial version; isibaar                                                           *
# Line 48  Line 49 
49    ******************************************************************************/    ******************************************************************************/
50    
51    
52    #include <stdio.h>
53  #include <stdlib.h>  #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"
# Line 58  Line 60 
60    
61  #include "../utils/mbfunctions.h"  #include "../utils/mbfunctions.h"
62    
63  #define ABS(X) (((X)>0)?(X):-(X))  /* #define BIGLUT */
 #define CLIP(X,A) (X > A) ? (A) : (X)  
64    
65  VLC intra_table[524032];  #ifdef BIGLUT
66  VLC inter_table[524032];  #define LEVELOFFSET 2048
67    #else
68    #define LEVELOFFSET 32
69    #endif
70    
71  VLC DCT3Dintra[4096];  static REVERSE_EVENT DCT3D[2][4096];
 VLC DCT3Dinter[4096];  
72    
73  void  #ifdef BIGLUT
74  init_vlc_tables(void)  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    /* not really MB related, but VLCs are only available here */
81    void bs_put_spritetrajectory(Bitstream * bs, const int val)
82  {  {
83            const int code = sprite_trajectory_code[val+16384].code;
84            const int len = sprite_trajectory_code[val+16384].len;
85            const int code2 = sprite_trajectory_len[len].code;
86            const int len2 = sprite_trajectory_len[len].len;
87    
88          int32_t k, l, i, intra, last;  //      printf("GMC=%d Code/Len  = %d / %d ",val, code,len);
89          VLC *vlc[2];  //      printf("Code2 / Len2 = %d / %d \n",code2,len2);
         VLC **coeff_ptr;  
         VLC *vlc1, *vlc2;  
   
         vlc1 = DCT3Dintra;  
         vlc2 = DCT3Dinter;  
   
         vlc[0] = intra_table;  
         vlc[1] = inter_table;  
   
         // generate encoding vlc lookup tables  
         // the lookup table idea is taken from the excellent fame project by Vivien Chapellier  
         for (i = 0; i < 4; i++) {  
                 intra = i % 2;  
                 last = i / 2;  
   
                 coeff_ptr = coeff_vlc[last + 2 * intra];  
   
                 for (k = -2047; k < 2048; k++) {        // level  
                         int8_t *max_level_ptr = max_level[last + 2 * intra];  
                         int8_t *max_run_ptr = max_run[last + 2 * intra];  
   
                         for (l = 0; l < 64; l++) {      // run  
                                 int32_t level = k;  
                                 ptr_t run = l;  
   
                                 if ((abs(level) <= max_level_ptr[run]) && (run <= (uint32_t) max_run_ptr[abs(level)])) {        // level < max_level and run < max_run  
   
                                         vlc[intra]->code = 0;  
                                         vlc[intra]->len = 0;  
                                         goto loop_end;  
                                 } else {  
                                         if (level > 0)  // correct level  
                                                 level -= max_level_ptr[run];  
                                         else  
                                                 level += max_level_ptr[run];  
90    
91                                          if ((abs(level) <= max_level_ptr[run]) &&          BitstreamPutBits(bs, code2, len2);
92                                                  (run <= (uint32_t) max_run_ptr[abs(level)])) {          if (len) BitstreamPutBits(bs, code, len);
93    }
94    
95                                                  vlc[intra]->code = 0x06;  int bs_get_spritetrajectory(Bitstream * bs)
96                                                  vlc[intra]->len = 8;  {
97                                                  goto loop_end;          int i;
98            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                                          if (level > 0)  // still here?  void
110                                                  level += max_level_ptr[run];    // restore level  init_vlc_tables(void)
111                                          else  {
112                                                  level -= max_level_ptr[run];          uint32_t i, j, k, intra, last, run,  run_esc, level, level_esc, escape, escape_len, offset, limit;
113            int32_t l;
114    
115                                          run -= max_run_ptr[abs(level)] + 1;     // and change run  #ifdef BIGLUT
116            intra_table = coeff_VLC[1];
117            inter_table = coeff_VLC[0];
118    #endif
119    
                                         if ((abs(level) <= max_level_ptr[run]) &&  
                                                 (run <= (uint32_t) max_run_ptr[abs(level)])) {  
120    
121                                                  vlc[intra]->code = 0x0e;          for (intra = 0; intra < 2; intra++)
122                                                  vlc[intra]->len = 9;                  for (i = 0; i < 4096; i++)
123                                                  goto loop_end;                          DCT3D[intra][i].event.level = 0;
124    
125            for (intra = 0; intra < 2; intra++)
126                    for (last = 0; last < 2; last++)
127                    {
128                            for (run = 0; run < 63 + last; run++)
129                                    for (level = 0; level < 32 << intra; level++)
130                                    {
131    #ifdef BIGLUT
132                                            offset = LEVELOFFSET;
133    #else
134                                            offset = !intra * LEVELOFFSET;
135    #endif
136                                            coeff_VLC[intra][last][level + offset][run].len = 128;
137                                          }                                          }
                                         run += max_run_ptr[abs(level)] + 1;  
138                                  }                                  }
139    
140                                  vlc[intra]->code =          for (intra = 0; intra < 2; intra++)
141                                          (uint32_t) ((l << 14) | (0x1e + last) << 20) | (1 << 13) |                  for (i = 0; i < 102; i++)
142                                          ((k & 0xfff) << 1) | 1;                  {
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                                  vlc[intra]->len = 30;                          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]++;                                  = coeff_tab[intra][i].vlc.code << 1;
156                            coeff_VLC[intra][coeff_tab[intra][i].event.last][coeff_tab[intra][i].event.level + offset][coeff_tab[intra][i].event.run].len
157                                    = coeff_tab[intra][i].vlc.len + 1;
158    #ifndef BIGLUT
159                            if (!intra)
160    #endif
161                            {
162                                    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                                    coeff_VLC[intra][coeff_tab[intra][i].event.last][offset - coeff_tab[intra][i].event.level][coeff_tab[intra][i].event.run].len
165                                            = coeff_tab[intra][i].vlc.len + 1;
166                            }
167                    }
168    
169            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;                                  continue;
177    
178                            loop_end:  #ifdef BIGLUT
179                                  if (level != 0) {                                          offset = LEVELOFFSET;
180                                          vlc[intra]->code =  #else
181                                                  (vlc[intra]->                                          offset = !intra * LEVELOFFSET;
182                                                   code << (coeff_ptr[run][abs(level) - 1].len +  #endif
183                                                                    1)) | (coeff_ptr[run][abs(level) -                      level_esc = level - max_level[intra][last][run];
184                                                                                                                  1].code << 1);                                          run_esc = run - 1 - max_run[intra][last][level];
185                                          vlc[intra]->len =                                          /*use this test to use shorter esc2 codes when possible
186                                                  (coeff_ptr[run][abs(level) - 1].len + 1) +                                          if (level_esc <= max_level[intra][last][run] && run <= max_run[intra][last][level_esc]
187                                                  vlc[intra]->len;                                                  && !(coeff_VLC[intra][last][level_esc + offset][run].len + 7 + 1
188                                                             > coeff_VLC[intra][last][level + offset][run_esc].code + 7 + 2))*/
189                                          if (level < 0)  
190                                                  vlc[intra]->code += 1;                                          if (level_esc <= max_level[intra][last][run] && run <= max_run[intra][last][level_esc])
191                                  }                                          {
192                                                    escape     = ESCAPE1;
193                                  vlc[intra]++;                                                  escape_len = 7 + 1;
194                                                    run_esc    = run;
195                          }                          }
196                                            else
197                                            {
198                                                    if (level <= max_level[intra][last][run_esc] && run_esc <= max_run[intra][last][level])
199                                                    {
200                                                            escape     = ESCAPE2;
201                                                            escape_len = 7 + 2;
202                                                            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                  }                  }
219          }          }
220    
221          for (i = 0; i < 4096; i++) {                                          coeff_VLC[intra][last][level + offset][run].code
222                  if (i >= 512) {                                                  = (escape << coeff_VLC[intra][last][level_esc + offset][run_esc].len)
223                          *vlc1 = DCT3Dtab3[(i >> 5) - 16];                                                  |  coeff_VLC[intra][last][level_esc + offset][run_esc].code;
224                          *vlc2 = DCT3Dtab0[(i >> 5) - 16];                                          coeff_VLC[intra][last][level + offset][run].len
225                  } else if (i >= 128) {                                                  = coeff_VLC[intra][last][level_esc + offset][run_esc].len + escape_len;
226                          *vlc1 = DCT3Dtab4[(i >> 2) - 32];  #ifndef BIGLUT
227                          *vlc2 = DCT3Dtab1[(i >> 2) - 32];                                          if (!intra)
228                  } else if (i >= 8) {  #endif
229                          *vlc1 = DCT3Dtab5[i - 8];                                          {
230                          *vlc2 = DCT3Dtab2[i - 8];                                                  coeff_VLC[intra][last][offset - level][run].code
231                  } else {                                                          = (escape << coeff_VLC[intra][last][level_esc + offset][run_esc].len)
232                          *vlc1 = ERRtab[i];                                                          |  coeff_VLC[intra][last][level_esc + offset][run_esc].code | 1;
233                          *vlc2 = ERRtab[i];                                                  coeff_VLC[intra][last][offset - level][run].len
234                                                            = coeff_VLC[intra][last][level_esc + offset][run_esc].len + escape_len;
235                                            }
236                  }                  }
237    
238                  vlc1++;  #ifdef BIGLUT
239                  vlc2++;                                  for (level = 32 << intra; level < 2048; level++)
240                                    {
241                                            coeff_VLC[intra][last][level + offset][run].code
242                                                    = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((level & 0xfff) << 1) | 1;
243                                            coeff_VLC[intra][last][level + offset][run].len = 30;
244    
245                                            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          DCT3D[0] = DCT3Dinter;  #else
250          DCT3D[1] = DCT3Dintra;                                  if (!intra)
251                                    {
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    #endif
257                            }
258    /* init sprite_trajectory tables */
259    /* even if GMC is not specified (it might be used later...) */
260    
261            sprite_trajectory_code[0+16384].code = 0;
262            sprite_trajectory_code[0+16384].len = 0;
263            for (k=0;k<14;k++)
264            {
265                    int limit = (1<<k);
266    
267                    for (l=-(2*limit-1); l <= -limit; l++)
268                    {
269                            sprite_trajectory_code[l+16384].code = (2*limit-1)+l;
270                            sprite_trajectory_code[l+16384].len = k+1;
271                    }
272    
273                    for (l=limit; l<= 2*limit-1; l++)
274                    {
275                            sprite_trajectory_code[l+16384].code = l;
276                            sprite_trajectory_code[l+16384].len = k+1;
277                    }
278            }
279  }  }
280    
281  static __inline void  static __inline void
# Line 240  Line 333 
333    
334  }  }
335    
336    #ifdef BIGLUT
337    
338  static __inline void  static __inline void
339  CodeCoeff(Bitstream * bs,  CodeCoeff(Bitstream * bs,
# Line 260  Line 354 
354                  j++;                  j++;
355    
356          do {          do {
357                  vlc = table + 64 * 2047 + (v << 6) + j - last;                  vlc = table + 64 * 2048 + (v << 6) + j - last;
358                  last = ++j;                  last = ++j;
359    
360                  // count zeroes                  /* count zeroes */
361                  while (j < 64 && (v = qcoeff[zigzag[j]]) == 0)                  while (j < 64 && (v = qcoeff[zigzag[j]]) == 0)
362                          j++;                          j++;
363    
364                  // write code                  /* 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 * 4095;                          vlc += 64 * 4096;
369                          BitstreamPutBits(bs, vlc->code, vlc->len);                          BitstreamPutBits(bs, vlc->code, vlc->len);
370                          break;                          break;
371                  }                  }
# Line 279  Line 373 
373    
374  }  }
375    
376    #else
377    
378  static void  static __inline void
379  CodeBlockIntra(const FRAMEINFO * frame,  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    #endif
491    
492    static __inline void
493    CodeBlockIntra(const FRAMEINFO * const frame,
494                             const MACROBLOCK * pMB,                             const MACROBLOCK * pMB,
495                             int16_t qcoeff[6 * 64],                             int16_t qcoeff[6 * 64],
496                             Bitstream * bs,                             Bitstream * bs,
# Line 330  Line 539 
539                                                           dcc_tab[qcoeff[i * 64 + 0] + 255].len);                                                           dcc_tab[qcoeff[i * 64 + 0] + 255].len);
540    
541                  if (pMB->cbp & (1 << (5 - i))) {                  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    
546                          bits = BitstreamPos(bs);                          bits = BitstreamPos(bs);
547    
548                          CodeCoeff(bs, &qcoeff[i * 64], intra_table,  #ifdef BIGLUT
549                                            scan_tables[pMB->acpred_directions[i]], 1);                          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;
# Line 344  Line 560 
560    
561    
562  static void  static void
563  CodeBlockInter(const FRAMEINFO * frame,  CodeBlockInter(const FRAMEINFO * const frame,
564                             const MACROBLOCK * pMB,                             const MACROBLOCK * pMB,
565                             int16_t qcoeff[6 * 64],                             int16_t qcoeff[6 * 64],
566                             Bitstream * bs,                             Bitstream * bs,
# Line 361  Line 577 
577          BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,          BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,
578                                           mcbpc_inter_tab[mcbpc].len);                                           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);
585    
# Line 372  Line 591 
591          if (frame->global_flags & XVID_INTERLACING) {          if (frame->global_flags & XVID_INTERLACING) {
592                  if (pMB->cbp) {                  if (pMB->cbp) {
593                          BitstreamPutBit(bs, pMB->field_dct);                          BitstreamPutBit(bs, pMB->field_dct);
594                          DEBUG1("codep: field_dct: ", pMB->field_dct);                          DPRINTF(DPRINTF_MB,"codep: field_dct: %i", pMB->field_dct);
595                  }                  }
596    
597                  // if inter block, write field ME flag                  // if inter block, write field ME flag
598                  if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {                  if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
599                          BitstreamPutBit(bs, pMB->field_pred);                          BitstreamPutBit(bs, pMB->field_pred);
600                          DEBUG1("codep: field_pred: ", pMB->field_pred);                          DPRINTF(DPRINTF_MB,"codep: field_pred: %i", pMB->field_pred);
601    
602                          // write field prediction references                          // write field prediction references
603                          if (pMB->field_pred) {                          if (pMB->field_pred) {
# Line 387  Line 606 
606                          }                          }
607                  }                  }
608          }          }
609          // code motion vector(s)          // code motion vector(s) if motion is local
610            if (!pMB->mcsel)
611          for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) {          for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) {
612                  CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat);                  CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat);
613                  CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat);                  CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat);
# Line 398  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 * 64], 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  void
639  MBCoding(const FRAMEINFO * frame,  MBCoding(const FRAMEINFO * const frame,
640                   MACROBLOCK * pMB,                   MACROBLOCK * pMB,
641                   int16_t qcoeff[6 * 64],                   int16_t qcoeff[6 * 64],
642                   Bitstream * bs,                   Bitstream * bs,
643                   Statistics * pStat)                   Statistics * pStat)
644  {  {
645            if (frame->coding_type != I_VOP)
646          if (frame->coding_type == P_VOP) {                          BitstreamPutBit(bs, 0); // not_coded
                         BitstreamPutBit(bs, 0); // coded  
         }  
647    
648          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)
649                  CodeBlockIntra(frame, pMB, qcoeff, bs, pStat);                  CodeBlockIntra(frame, pMB, qcoeff, bs, pStat);
# Line 425  Line 652 
652    
653  }  }
654    
655    /*
656    // moved to mbcoding.h so that in can be 'static __inline'
657  void  void
658  MBSkip(Bitstream * bs)  MBSkip(Bitstream * bs)
659  {  {
660          BitstreamPutBit(bs, 1); // not coded          BitstreamPutBit(bs, 1); // not coded
         return;  
661  }  }
662    */
663    
664  /***************************************************************  /***************************************************************
665   * bframe encoding start   * bframe encoding start
# Line 446  Line 673 
673          3       0001b   forward mc+q            dbquant, mvdf          3       0001b   forward mc+q            dbquant, mvdf
674  */  */
675    
676  void  static __inline void
677  put_bvop_mbtype(Bitstream * bs,  put_bvop_mbtype(Bitstream * bs,
678                                  int value)                                  int value)
679  {  {
680          switch (value) {          switch (value) {
681          case 0:                  case MODE_FORWARD:
                 BitstreamPutBit(bs, 1);  
                 return;  
   
         case 1:  
682                  BitstreamPutBit(bs, 0);                  BitstreamPutBit(bs, 0);
683                  BitstreamPutBit(bs, 1);                  case MODE_BACKWARD:
                 return;  
   
         case 2:  
684                  BitstreamPutBit(bs, 0);                  BitstreamPutBit(bs, 0);
685                    case MODE_INTERPOLATE:
686                  BitstreamPutBit(bs, 0);                  BitstreamPutBit(bs, 0);
687                    case MODE_DIRECT:
688                  BitstreamPutBit(bs, 1);                  BitstreamPutBit(bs, 1);
689                  return;                  default:
690                            break;
         case 3:  
                 BitstreamPutBit(bs, 0);  
                 BitstreamPutBit(bs, 0);  
                 BitstreamPutBit(bs, 0);  
                 BitstreamPutBit(bs, 1);  
                 return;  
   
         default:;                                       // invalid!  
   
691          }          }
   
692  }  }
693    
694  /*  /*
# Line 486  Line 698 
698          +2      11b          +2      11b
699  */  */
700    
701  void  static __inline void
702  put_bvop_dbquant(Bitstream * bs,  put_bvop_dbquant(Bitstream * bs,
703                                   int value)                                   int value)
704  {  {
# Line 517  Line 729 
729                           const int32_t fcode,                           const int32_t fcode,
730                           const int32_t bcode,                           const int32_t bcode,
731                           Bitstream * bs,                           Bitstream * bs,
732                           Statistics * pStat)                           Statistics * pStat,
733                             int direction)
734  {  {
735          int i;          int vcode = fcode;
736            unsigned int i;
737    
738  /*      ------------------------------------------------------------------  /*      ------------------------------------------------------------------
739                  when a block is skipped it is decoded DIRECT(0,0)                  when a block is skipped it is decoded DIRECT(0,0)
# Line 549  Line 763 
763                  put_bvop_dbquant(bs, 0);        // todo: mb->dquant = 0                  put_bvop_dbquant(bs, 0);        // todo: mb->dquant = 0
764          }          }
765    
766          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) {          switch (mb->mode) {
767                  CodeVector(bs, mb->pmvs[0].x, fcode, pStat);                  case MODE_INTERPOLATE:
768                  CodeVector(bs, mb->pmvs[0].y, fcode, pStat);                          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          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) {                          vcode = bcode;
772                  CodeVector(bs, mb->b_pmvs[0].x, bcode, pStat);                  case MODE_FORWARD:
773                  CodeVector(bs, mb->b_pmvs[0].y, bcode, pStat);                          CodeVector(bs, mb->pmvs[0].x, vcode, pStat);
774          }                          CodeVector(bs, mb->pmvs[0].y, vcode, pStat);
775                            break;
776          if (mb->mode == MODE_DIRECT) {                  case MODE_DIRECT:
777                  CodeVector(bs, mb->deltamv.x, 1, pStat);                /* fcode is always 1 for delta vector */                          CodeVector(bs, mb->pmvs[3].x, 1, pStat);        // fcode is always 1 for delta vector
778                  CodeVector(bs, mb->deltamv.y, 1, pStat);                /* prediction is always (0,0) */                          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++) {          for (i = 0; i < 6; i++) {
783                  if (mb->cbp & (1 << (5 - i))) {                  if (mb->cbp & (1 << (5 - i))) {
784    #ifdef BIGLUT
785                          CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0);                          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  }  }
# Line 623  Line 842 
842    
843          uint32_t index;          uint32_t index;
844    
845          index = CLIP(BitstreamShowBits(bs, 9), 256);          index = MIN(BitstreamShowBits(bs, 9), 256);
846    
847          BitstreamSkip(bs, mcbpc_inter_table[index].len);          BitstreamSkip(bs, mcbpc_inter_table[index].len);
848    
# Line 649  Line 868 
868    
869  }  }
870    
871  int  static __inline int
872  get_mv_data(Bitstream * bs)  get_mv_data(Bitstream * bs)
873  {  {
874    
# Line 758  Line 977 
977    
978  }  }
979    
980    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;
989            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 */
993                    intra = 0;
994    
995            if (BitstreamShowBits(bs, 7) != ESCAPE) {
996                    reverse_event = &DCT3D[intra][BitstreamShowBits(bs, 12)];
997    
998                    if ((level = reverse_event->event.level) == 0)
999                            goto error;
1000    
1001                    *last = reverse_event->event.last;
1002                    *run  = reverse_event->event.run;
1003    
1004                    BitstreamSkip(bs, reverse_event->len);
1005    
1006                    return BitstreamGetBits(bs, 1) ? -level : level;
1007            }
1008    
1009            BitstreamSkip(bs, 7);
1010    
1011            if (short_video_header) {
1012                    /* escape mode 4 - H.263 type, only used if short_video_header = 1  */
1013                    *last = BitstreamGetBit(bs);
1014                    *run = BitstreamGetBits(bs, 6);
1015                    level = BitstreamGetBits(bs, 8);
1016    
1017                    if (level == 0 || level == 128)
1018                            DPRINTF(DPRINTF_ERROR, "Illegal LEVEL for ESCAPE mode 4: %d", level);
1019    
1020                    return (level << 24) >> 24;
1021            }
1022    
1023            mode = BitstreamShowBits(bs, 2);
1024    
1025            if (mode < 3) {
1026                    BitstreamSkip(bs, (mode == 2) ? 2 : 1);
1027    
1028                    reverse_event = &DCT3D[intra][BitstreamShowBits(bs, 12)];
1029    
1030                    if ((level = reverse_event->event.level) == 0)
1031                            goto error;
1032    
1033                    *last = reverse_event->event.last;
1034                    *run  = reverse_event->event.run;
1035    
1036                    BitstreamSkip(bs, reverse_event->len);
1037    
1038                    if (mode < 2)                   /* first escape mode, level is offset */
1039                            level += max_level[intra][*last][*run];
1040                    else                                    /* second escape mode, run is offset */
1041                            *run += max_run[intra][*last][level] + 1;
1042    
1043                    return BitstreamGetBits(bs, 1) ? -level : level;
1044            }
1045    
1046            /* third escape mode - fixed length codes */
1047            BitstreamSkip(bs, 2);
1048            *last = BitstreamGetBits(bs, 1);
1049            *run = BitstreamGetBits(bs, 6);
1050            BitstreamSkip(bs, 1);           /* marker */
1051            level = BitstreamGetBits(bs, 12);
1052            BitstreamSkip(bs, 1);           /* marker */
1053    
1054            return (level << 20) >> 20;
1055    
1056      error:
1057            *run = VLC_ERROR;
1058            return 0;
1059    }
1060    
1061  void  void
1062  get_intra_block(Bitstream * bs,  get_intra_block(Bitstream * bs,
1063                                  int16_t * block,                                  int16_t * block,
# Line 766  Line 1066 
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                          DEBUG("fatal: invalid run");                          DPRINTF(DPRINTF_ERROR,"fatal: invalid run");
1075                          break;                          break;
1076                  }                  }
1077                  coeff += run;                  coeff += run;
# Line 782  Line 1080 
1080                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level);                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level);
1081                  //DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[coeff], level, BitstreamShowBits(bs, 32));                  //DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[coeff], level, BitstreamShowBits(bs, 32));
1082    
1083                  if (level < -127 || level > 127) {                  if (level < -2047 || level > 2047) {
1084                          DEBUG1("warning: intra_overflow", level);                          DPRINTF(DPRINTF_ERROR,"warning: intra_overflow %i", level);
1085                  }                  }
1086                  coeff++;                  coeff++;
1087          } while (!last);          } while (!last);
# Line 792  Line 1090 
1090    
1091  void  void
1092  get_inter_block(Bitstream * bs,  get_inter_block(Bitstream * bs,
1093                                  int16_t * block)                                  int16_t * block,
1094                                    int direction)
1095  {  {
1096    
1097          const uint16_t *scan = scan_tables[0];          const uint16_t *scan = scan_tables[direction];
1098          int p;          int p;
1099          int level;          int level;
1100          int run;          int run;
# Line 805  Line 1104 
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                          DEBUG("fatal: invalid run");                          DPRINTF(DPRINTF_ERROR,"fatal: invalid run");
1108                          break;                          break;
1109                  }                  }
1110                  p += run;                  p += run;
# Line 815  Line 1114 
1114                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level);                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level);
1115                  // DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[p], level, BitstreamShowBits(bs, 32));                  // DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[p], level, BitstreamShowBits(bs, 32));
1116    
1117                  if (level < -127 || level > 127) {                  if (level < -2047 || level > 2047) {
1118                          DEBUG1("warning: inter_overflow", level);                          DPRINTF(DPRINTF_ERROR,"warning: inter overflow %i", level);
1119                  }                  }
1120                  p++;                  p++;
1121          } while (!last);          } while (!last);

Legend:
Removed from v.1.25  
changed lines
  Added in v.1.25.2.12

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