[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.44, Fri Feb 21 08:32:34 2003 UTC revision 1.44.2.18, Sun Dec 14 03:32:01 2003 UTC
# Line 1  Line 1 
1   /******************************************************************************  /*****************************************************************************
2    *                                                                            *   *
3    *  This file is part of XviD, a free MPEG-4 video encoder/decoder            *   *  XVID MPEG-4 VIDEO CODEC
4    *                                                                            *   *  - MB coding -
5    *  XviD is an implementation of a part of one or more MPEG-4 Video tools     *   *
6    *  as specified in ISO/IEC 14496-2 standard.  Those intending to use this    *   *  Copyright (C) 2002 Michael Militzer <isibaar@xvid.org>
7    *  software module in hardware or software products are advised that its     *   *
8    *  use may infringe existing patents or copyrights, and any such use         *   *  This program is free software ; you can redistribute it and/or modify
9    *  would be at such party's own risk.  The original developer of this        *   *  it under the terms of the GNU General Public License as published by
10    *  software module and his/her company, and subsequent editors and their     *   *  the Free Software Foundation ; either version 2 of the License, or
11    *  companies, will have no liability for use of this software or             *   *  (at your option) any later version.
12    *  modifications or derivatives thereof.                                     *   *
13    *                                                                            *   *  This program is distributed in the hope that it will be useful,
14    *  XviD is free software; you can redistribute it and/or modify it           *   *  but WITHOUT ANY WARRANTY ; without even the implied warranty of
15    *  under the terms of the GNU General Public License as published by         *   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    *  the Free Software Foundation; either version 2 of the License, or         *   *  GNU General Public License for more details.
17    *  (at your option) any later version.                                       *   *
18    *                                                                            *   *  You should have received a copy of the GNU General Public License
19    *  XviD is distributed in the hope that it will be useful, but               *   *  along with this program ; if not, write to the Free Software
20    *  WITHOUT ANY WARRANTY; without even the implied warranty of                *   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21    *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *   *
22    *  GNU General Public License for more details.                              *   * $Id$
23    *                                                                            *   *
24    *  You should have received a copy of the GNU General Public License         *   ****************************************************************************/
   *  along with this program; if not, write to the Free Software               *  
   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA  *  
   *                                                                            *  
   ******************************************************************************/  
   
  /******************************************************************************  
   *                                                                            *  
   *  mbcoding.c                                                                *  
   *                                                                            *  
   *  Copyright (C) 2002 - Michael Militzer <isibaar@xvid.org>                  *  
   *                                                                            *  
   *  For more information visit the XviD homepage: http://www.xvid.org         *  
   *                                                                            *  
   ******************************************************************************/  
   
  /******************************************************************************  
   *                                                                                                                                                        *  
   *  Revision history:                                                         *  
   *                                                                            *  
   *  28.10.2002 GMC support - gruel                                                                                        *  
   *  28.06.2002 added check_resync_marker()                                    *  
   *  14.04.2002 bframe encoding                                                                                            *  
   *  08.03.2002 initial version; isibaar                                                           *  
   *                                                                                                                                                        *  
   ******************************************************************************/  
   
25    
26  #include <stdio.h>  #include <stdio.h>
27  #include <stdlib.h>  #include <stdlib.h>
28    #include <string.h>
29    
30  #include "../portab.h"  #include "../portab.h"
31  #include "../global.h"  #include "../global.h"
32  #include "bitstream.h"  #include "bitstream.h"
# Line 60  Line 36 
36    
37  #include "../utils/mbfunctions.h"  #include "../utils/mbfunctions.h"
38    
 /* #define BIGLUT */  
   
 #ifdef BIGLUT  
 #define LEVELOFFSET 2048  
 #else  
39  #define LEVELOFFSET 32  #define LEVELOFFSET 32
 #endif  
40    
41  static REVERSE_EVENT DCT3D[2][4096];  static REVERSE_EVENT DCT3D[2][4096];
   
 #ifdef BIGLUT  
 static VLC coeff_VLC[2][2][4096][64];  
 VLC *intra_table;  
 static VLC *inter_table;  
 #else  
42  static VLC coeff_VLC[2][2][64][64];  static VLC coeff_VLC[2][2][64][64];
 #endif  
43    
44  /* not really MB related, but VLCs are only available here */  /* not really MB related, but VLCs are only available here */
45  void bs_put_spritetrajectory(Bitstream * bs, const int val)  void bs_put_spritetrajectory(Bitstream * bs, const int val)
# Line 86  Line 49 
49          const int code2 = sprite_trajectory_len[len].code;          const int code2 = sprite_trajectory_len[len].code;
50          const int len2 = sprite_trajectory_len[len].len;          const int len2 = sprite_trajectory_len[len].len;
51    
52  //      printf("GMC=%d Code/Len  = %d / %d ",val, code,len);  #if 0
53  //      printf("Code2 / Len2 = %d / %d \n",code2,len2);          printf("GMC=%d Code/Len  = %d / %d ",val, code,len);
54            printf("Code2 / Len2 = %d / %d \n",code2,len2);
55    #endif
56    
57          BitstreamPutBits(bs, code2, len2);          BitstreamPutBits(bs, code2, len2);
58          if (len) BitstreamPutBits(bs, code, len);          if (len) BitstreamPutBits(bs, code, len);
# Line 113  Line 78 
78          uint32_t i, j, k, intra, last, run,  run_esc, level, level_esc, escape, escape_len, offset;          uint32_t i, j, k, intra, last, run,  run_esc, level, level_esc, escape, escape_len, offset;
79          int32_t l;          int32_t l;
80    
 #ifdef BIGLUT  
         intra_table = coeff_VLC[1];  
         inter_table = coeff_VLC[0];  
 #endif  
   
   
81          for (intra = 0; intra < 2; intra++)          for (intra = 0; intra < 2; intra++)
82                  for (i = 0; i < 4096; i++)                  for (i = 0; i < 4096; i++)
83                          DCT3D[intra][i].event.level = 0;                          DCT3D[intra][i].event.level = 0;
84    
85          for (intra = 0; intra < 2; intra++)          for (intra = 0; intra < 2; intra++) {
86                  for (last = 0; last < 2; last++)                  for (last = 0; last < 2; last++) {
87                  {                          for (run = 0; run < 63 + last; run++) {
88                          for (run = 0; run < 63 + last; run++)                                  for (level = 0; level < (uint32_t)(32 << intra); level++) {
                                 for (level = 0; level < (uint32_t)(32 << intra); level++)  
                                 {  
 #ifdef BIGLUT  
                                         offset = LEVELOFFSET;  
 #else  
89                                          offset = !intra * LEVELOFFSET;                                          offset = !intra * LEVELOFFSET;
 #endif  
90                                          coeff_VLC[intra][last][level + offset][run].len = 128;                                          coeff_VLC[intra][last][level + offset][run].len = 128;
91                                  }                                  }
92                  }                  }
93                    }
94            }
95    
96          for (intra = 0; intra < 2; intra++)          for (intra = 0; intra < 2; intra++) {
97                  for (i = 0; i < 102; i++)                  for (i = 0; i < 102; i++) {
                 {  
 #ifdef BIGLUT  
                         offset = LEVELOFFSET;  
 #else  
98                          offset = !intra * LEVELOFFSET;                          offset = !intra * LEVELOFFSET;
99  #endif  
100                          for (j = 0; j < (uint32_t)(1 << (12 - coeff_tab[intra][i].vlc.len)); j++)                          for (j = 0; j < (uint32_t)(1 << (12 - coeff_tab[intra][i].vlc.len)); j++) {
                         {  
101                                  DCT3D[intra][(coeff_tab[intra][i].vlc.code << (12 - coeff_tab[intra][i].vlc.len)) | j].len       = coeff_tab[intra][i].vlc.len;                                  DCT3D[intra][(coeff_tab[intra][i].vlc.code << (12 - coeff_tab[intra][i].vlc.len)) | j].len       = coeff_tab[intra][i].vlc.len;
102                                  DCT3D[intra][(coeff_tab[intra][i].vlc.code << (12 - coeff_tab[intra][i].vlc.len)) | j].event = coeff_tab[intra][i].event;                                  DCT3D[intra][(coeff_tab[intra][i].vlc.code << (12 - coeff_tab[intra][i].vlc.len)) | j].event = coeff_tab[intra][i].event;
103                          }                          }
# Line 156  Line 106 
106                                  = coeff_tab[intra][i].vlc.code << 1;                                  = coeff_tab[intra][i].vlc.code << 1;
107                          coeff_VLC[intra][coeff_tab[intra][i].event.last][coeff_tab[intra][i].event.level + offset][coeff_tab[intra][i].event.run].len                          coeff_VLC[intra][coeff_tab[intra][i].event.last][coeff_tab[intra][i].event.level + offset][coeff_tab[intra][i].event.run].len
108                                  = coeff_tab[intra][i].vlc.len + 1;                                  = coeff_tab[intra][i].vlc.len + 1;
109  #ifndef BIGLUT  
110                          if (!intra)                          if (!intra) {
 #endif  
                         {  
111                                  coeff_VLC[intra][coeff_tab[intra][i].event.last][offset - coeff_tab[intra][i].event.level][coeff_tab[intra][i].event.run].code                                  coeff_VLC[intra][coeff_tab[intra][i].event.last][offset - coeff_tab[intra][i].event.level][coeff_tab[intra][i].event.run].code
112                                          = (coeff_tab[intra][i].vlc.code << 1) | 1;                                          = (coeff_tab[intra][i].vlc.code << 1) | 1;
113                                  coeff_VLC[intra][coeff_tab[intra][i].event.last][offset - coeff_tab[intra][i].event.level][coeff_tab[intra][i].event.run].len                                  coeff_VLC[intra][coeff_tab[intra][i].event.last][offset - coeff_tab[intra][i].event.level][coeff_tab[intra][i].event.run].len
114                                          = coeff_tab[intra][i].vlc.len + 1;                                          = coeff_tab[intra][i].vlc.len + 1;
115                          }                          }
116                  }                  }
117            }
118    
119            for (intra = 0; intra < 2; intra++) {
120                    for (last = 0; last < 2; last++) {
121                            for (run = 0; run < 63 + last; run++) {
122                                    for (level = 1; level < (uint32_t)(32 << intra); level++) {
123    
         for (intra = 0; intra < 2; intra++)  
                 for (last = 0; last < 2; last++)  
                         for (run = 0; run < 63 + last; run++)  
                         {  
                                 for (level = 1; level < (uint32_t)(32 << intra); level++)  
                                 {  
124                                          if (level <= max_level[intra][last][run] && run <= max_run[intra][last][level])                                          if (level <= max_level[intra][last][run] && run <= max_run[intra][last][level])
125                                              continue;                                              continue;
126    
 #ifdef BIGLUT  
                                         offset = LEVELOFFSET;  
 #else  
127                                          offset = !intra * LEVELOFFSET;                                          offset = !intra * LEVELOFFSET;
 #endif  
128                      level_esc = level - max_level[intra][last][run];                      level_esc = level - max_level[intra][last][run];
129                                          run_esc = run - 1 - max_run[intra][last][level];                                          run_esc = run - 1 - max_run[intra][last][level];
                                         /*use this test to use shorter esc2 codes when possible  
                                         if (level_esc <= max_level[intra][last][run] && run <= max_run[intra][last][level_esc]  
                                                 && !(coeff_VLC[intra][last][level_esc + offset][run].len + 7 + 1  
                                                          > coeff_VLC[intra][last][level + offset][run_esc].code + 7 + 2))*/  
130    
131                                          if (level_esc <= max_level[intra][last][run] && run <= max_run[intra][last][level_esc])                                          if (level_esc <= max_level[intra][last][run] && run <= max_run[intra][last][level_esc]) {
                                         {  
132                                                  escape     = ESCAPE1;                                                  escape     = ESCAPE1;
133                                                  escape_len = 7 + 1;                                                  escape_len = 7 + 1;
134                                                  run_esc    = run;                                                  run_esc    = run;
135                                          }                                          } else {
136                                          else                                                  if (run_esc <= max_run[intra][last][level] && level <= max_level[intra][last][run_esc]) {
                                         {  
                                                 if (run_esc <= max_run[intra][last][level] && level <= max_level[intra][last][run_esc])  
                                                 {  
137                                                          escape     = ESCAPE2;                                                          escape     = ESCAPE2;
138                                                          escape_len = 7 + 2;                                                          escape_len = 7 + 2;
139                                                          level_esc  = level;                                                          level_esc  = level;
140                                                  }                                                  } else {
141                                                  else                                                          if (!intra) {
                                                 {  
 #ifndef BIGLUT  
                                                         if (!intra)  
 #endif  
                                                         {  
142                                                                  coeff_VLC[intra][last][level + offset][run].code                                                                  coeff_VLC[intra][last][level + offset][run].code
143                                                                          = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((level & 0xfff) << 1) | 1;                                                                          = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((level & 0xfff) << 1) | 1;
144                                                                  coeff_VLC[intra][last][level + offset][run].len = 30;                                                                  coeff_VLC[intra][last][level + offset][run].len = 30;
# Line 224  Line 155 
155                                                  |  coeff_VLC[intra][last][level_esc + offset][run_esc].code;                                                  |  coeff_VLC[intra][last][level_esc + offset][run_esc].code;
156                                          coeff_VLC[intra][last][level + offset][run].len                                          coeff_VLC[intra][last][level + offset][run].len
157                                                  = coeff_VLC[intra][last][level_esc + offset][run_esc].len + escape_len;                                                  = coeff_VLC[intra][last][level_esc + offset][run_esc].len + escape_len;
158  #ifndef BIGLUT  
159                                          if (!intra)                                          if (!intra) {
 #endif  
                                         {  
160                                                  coeff_VLC[intra][last][offset - level][run].code                                                  coeff_VLC[intra][last][offset - level][run].code
161                                                          = (escape << coeff_VLC[intra][last][level_esc + offset][run_esc].len)                                                          = (escape << coeff_VLC[intra][last][level_esc + offset][run_esc].len)
162                                                          |  coeff_VLC[intra][last][level_esc + offset][run_esc].code | 1;                                                          |  coeff_VLC[intra][last][level_esc + offset][run_esc].code | 1;
# Line 236  Line 165 
165                                          }                                          }
166                                  }                                  }
167    
168  #ifdef BIGLUT                                  if (!intra) {
                                 for (level = 32 << intra; level < 2048; level++)  
                                 {  
                                         coeff_VLC[intra][last][level + offset][run].code  
                                                 = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((level & 0xfff) << 1) | 1;  
                                         coeff_VLC[intra][last][level + offset][run].len = 30;  
   
                                         coeff_VLC[intra][last][offset - level][run].code  
                                                 = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-level & 0xfff) << 1) | 1;  
                                         coeff_VLC[intra][last][offset - level][run].len = 30;  
                                 }  
 #else  
                                 if (!intra)  
                                 {  
169                                          coeff_VLC[intra][last][0][run].code                                          coeff_VLC[intra][last][0][run].code
170                                                  = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-32 & 0xfff) << 1) | 1;                                                  = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-32 & 0xfff) << 1) | 1;
171                                          coeff_VLC[intra][last][0][run].len = 30;                                          coeff_VLC[intra][last][0][run].len = 30;
172                                  }                                  }
 #endif  
173                          }                          }
174  /* init sprite_trajectory tables */                  }
175  /* even if GMC is not specified (it might be used later...) */          }
176    
177            /* init sprite_trajectory tables
178             * even if GMC is not specified (it might be used later...) */
179    
180          sprite_trajectory_code[0+16384].code = 0;          sprite_trajectory_code[0+16384].code = 0;
181          sprite_trajectory_code[0+16384].len = 0;          sprite_trajectory_code[0+16384].len = 0;
182          for (k=0;k<14;k++)          for (k=0;k<14;k++) {
         {  
183                  int limit = (1<<k);                  int limit = (1<<k);
184    
185                  for (l=-(2*limit-1); l <= -limit; l++)                  for (l=-(2*limit-1); l <= -limit; l++) {
                 {  
186                          sprite_trajectory_code[l+16384].code = (2*limit-1)+l;                          sprite_trajectory_code[l+16384].code = (2*limit-1)+l;
187                          sprite_trajectory_code[l+16384].len = k+1;                          sprite_trajectory_code[l+16384].len = k+1;
188                  }                  }
189    
190                  for (l=limit; l<= 2*limit-1; l++)                  for (l=limit; l<= 2*limit-1; l++) {
                 {  
191                          sprite_trajectory_code[l+16384].code = l;                          sprite_trajectory_code[l+16384].code = l;
192                          sprite_trajectory_code[l+16384].len = k+1;                          sprite_trajectory_code[l+16384].len = k+1;
193                  }                  }
# Line 334  Line 249 
249    
250  }  }
251    
 #ifdef BIGLUT  
   
 static __inline void  
 CodeCoeff(Bitstream * bs,  
                   const int16_t qcoeff[64],  
                   VLC * table,  
                   const uint16_t * zigzag,  
                   uint16_t intra)  
 {  
   
         uint32_t j, last;  
         short v;  
         VLC *vlc;  
   
         j = intra;  
         last = intra;  
   
         while (j < 64 && (v = qcoeff[zigzag[j]]) == 0)  
                 j++;  
   
         do {  
                 vlc = table + 64 * 2048 + (v << 6) + j - last;  
                 last = ++j;  
   
                 /* count zeroes */  
                 while (j < 64 && (v = qcoeff[zigzag[j]]) == 0)  
                         j++;  
   
                 /* write code */  
                 if (j != 64) {  
                         BitstreamPutBits(bs, vlc->code, vlc->len);  
                 } else {  
                         vlc += 64 * 4096;  
                         BitstreamPutBits(bs, vlc->code, vlc->len);  
                         break;  
                 }  
         } while (1);  
   
 }  
   
   
   
 /* returns the number of bits required to encode qcoeff */  
 int  
 CodeCoeff_CalcBits(const int16_t qcoeff[64],  
                   VLC * table,  
                   const uint16_t * zigzag,  
                   uint16_t intra)  
 {  
         int bits = 0;  
         uint32_t j, last;  
         short v;  
         VLC *vlc;  
   
         j = intra;  
         last = intra;  
   
         while (j < 64 && (v = qcoeff[zigzag[j]]) == 0)  
                 j++;  
   
         if (j >= 64) return 0;  /* empty block */  
   
         do {  
                 vlc = table + 64 * 2048 + (v << 6) + j - last;  
                 last = ++j;  
   
                 /* count zeroes */  
                 while (j < 64 && (v = qcoeff[zigzag[j]]) == 0)  
                         j++;  
   
                 /* write code */  
                 if (j != 64) {  
                         bits += vlc->len;  
                 } else {  
                         vlc += 64 * 4096;  
                         bits += vlc->len;  
                         break;  
                 }  
         } while (1);  
   
         return bits;  
 }  
   
   
 #else  
   
252  static __inline void  static __inline void
253  CodeCoeffInter(Bitstream * bs,  CodeCoeffInter(Bitstream * bs,
254                    const int16_t qcoeff[64],                    const int16_t qcoeff[64],
# Line 498  Line 327 
327          {          {
328                  if ((level = qcoeff[zigzag[i++]]) != 0)                  if ((level = qcoeff[zigzag[i++]]) != 0)
329                  {                  {
330                          abs_level = ABS(prev_level);                          abs_level = abs(prev_level);
331                          abs_level = abs_level < 64 ? abs_level : 0;                          abs_level = abs_level < 64 ? abs_level : 0;
332                          code      = coeff_VLC[1][0][abs_level][prev_run].code;                          code      = coeff_VLC[1][0][abs_level][prev_run].code;
333                          len               = coeff_VLC[1][0][abs_level][prev_run].len;                          len               = coeff_VLC[1][0][abs_level][prev_run].len;
# Line 518  Line 347 
347                          run++;                          run++;
348          }          }
349    
350          abs_level = ABS(prev_level);          abs_level = abs(prev_level);
351          abs_level = abs_level < 64 ? abs_level : 0;          abs_level = abs_level < 64 ? abs_level : 0;
352          code      = coeff_VLC[1][1][abs_level][prev_run].code;          code      = coeff_VLC[1][1][abs_level][prev_run].code;
353          len               = coeff_VLC[1][1][abs_level][prev_run].len;          len               = coeff_VLC[1][1][abs_level][prev_run].len;
# Line 559  Line 388 
388          {          {
389                  if ((level = qcoeff[zigzag[i++]]) != 0)                  if ((level = qcoeff[zigzag[i++]]) != 0)
390                  {                  {
391                          abs_level = ABS(prev_level);                          abs_level = abs(prev_level);
392                          abs_level = abs_level < 64 ? abs_level : 0;                          abs_level = abs_level < 64 ? abs_level : 0;
393                          len               = coeff_VLC[1][0][abs_level][prev_run].len;                          len               = coeff_VLC[1][0][abs_level][prev_run].len;
394                          bits      += len!=128 ? len : 30;                          bits      += len!=128 ? len : 30;
# Line 572  Line 401 
401                          run++;                          run++;
402          }          }
403    
404          abs_level = ABS(prev_level);          abs_level = abs(prev_level);
405          abs_level = abs_level < 64 ? abs_level : 0;          abs_level = abs_level < 64 ? abs_level : 0;
406          len               = coeff_VLC[1][1][abs_level][prev_run].len;          len               = coeff_VLC[1][1][abs_level][prev_run].len;
407          bits      += len!=128 ? len : 30;          bits      += len!=128 ? len : 30;
# Line 624  Line 453 
453          return bits;          return bits;
454  }  }
455    
456    static int iDQtab[5] = {
457            1, 0, -1 /* no change */, 2, 3
458    };
459    #define DQ_VALUE2INDEX(value)  iDQtab[(value)+2]
460    
 #endif  
461    
462  static __inline void  static __inline void
463  CodeBlockIntra(const FRAMEINFO * const frame,  CodeBlockIntra(const FRAMEINFO * const frame,
# Line 639  Line 471 
471    
472          cbpy = pMB->cbp >> 2;          cbpy = pMB->cbp >> 2;
473    
474          // write mcbpc          /* write mcbpc */
475          if (frame->coding_type == I_VOP) {          if (frame->coding_type == I_VOP) {
476                  mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2);                  mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2);
477                  BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code,                  BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code,
# Line 650  Line 482 
482                                                   mcbpc_inter_tab[mcbpc].len);                                                   mcbpc_inter_tab[mcbpc].len);
483          }          }
484    
485          // ac prediction flag          /* ac prediction flag */
486          if (pMB->acpred_directions[0])          if (pMB->acpred_directions[0])
487                  BitstreamPutBits(bs, 1, 1);                  BitstreamPutBits(bs, 1, 1);
488          else          else
489                  BitstreamPutBits(bs, 0, 1);                  BitstreamPutBits(bs, 0, 1);
490    
491          // write cbpy          /* write cbpy */
492          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);          BitstreamPutBits(bs, xvid_cbpy_tab[cbpy].code, xvid_cbpy_tab[cbpy].len);
493    
494          // write dquant          /* write dquant */
495          if (pMB->mode == MODE_INTRA_Q)          if (pMB->mode == MODE_INTRA_Q)
496                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, DQ_VALUE2INDEX(pMB->dquant), 2);
497    
498          // write interlacing          /* write interlacing */
499          if (frame->global_flags & XVID_INTERLACING) {          if (frame->vol_flags & XVID_VOL_INTERLACING) {
500                  BitstreamPutBit(bs, pMB->field_dct);                  BitstreamPutBit(bs, pMB->field_dct);
501          }          }
502          // code block coeffs          /* code block coeffs */
503          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
504                  if (i < 4)                  if (i < 4)
505                          BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code,                          BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code,
# Line 678  Line 510 
510    
511                  if (pMB->cbp & (1 << (5 - i))) {                  if (pMB->cbp & (1 << (5 - i))) {
512                          const uint16_t *scan_table =                          const uint16_t *scan_table =
513                                  frame->global_flags & XVID_ALTERNATESCAN ?                                  frame->vop_flags & XVID_VOP_ALTERNATESCAN ?
514                                  scan_tables[2] : scan_tables[pMB->acpred_directions[i]];                                  scan_tables[2] : scan_tables[pMB->acpred_directions[i]];
515    
516                          bits = BitstreamPos(bs);                          bits = BitstreamPos(bs);
517    
 #ifdef BIGLUT  
                         CodeCoeff(bs, &qcoeff[i * 64], intra_table, scan_table, 1);  
 #else  
518                          CodeCoeffIntra(bs, &qcoeff[i * 64], scan_table);                          CodeCoeffIntra(bs, &qcoeff[i * 64], scan_table);
 #endif  
519    
520                          bits = BitstreamPos(bs) - bits;                          bits = BitstreamPos(bs) - bits;
521                          pStat->iTextBits += bits;                          pStat->iTextBits += bits;
# Line 711  Line 539 
539          mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);          mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);
540          cbpy = 15 - (pMB->cbp >> 2);          cbpy = 15 - (pMB->cbp >> 2);
541    
542          // write mcbpc          /* write mcbpc */
543          BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,          BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,
544                                           mcbpc_inter_tab[mcbpc].len);                                           mcbpc_inter_tab[mcbpc].len);
545    
546          if ( (frame->coding_type == S_VOP) && (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) )          if ( (frame->coding_type == S_VOP) && (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) )
547                  BitstreamPutBit(bs, pMB->mcsel);                // mcsel: '0'=local motion, '1'=GMC                  BitstreamPutBit(bs, pMB->mcsel);                /* mcsel: '0'=local motion, '1'=GMC */
548    
549          // write cbpy          /* write cbpy */
550          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);          BitstreamPutBits(bs, xvid_cbpy_tab[cbpy].code, xvid_cbpy_tab[cbpy].len);
551    
552          // write dquant          /* write dquant */
553          if (pMB->mode == MODE_INTER_Q)          if (pMB->mode == MODE_INTER_Q)
554                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, DQ_VALUE2INDEX(pMB->dquant), 2);
555    
556          // interlacing          /* interlacing */
557          if (frame->global_flags & XVID_INTERLACING) {          if (frame->vol_flags & XVID_VOL_INTERLACING) {
558                  if (pMB->cbp) {                  if (pMB->cbp) {
559                          BitstreamPutBit(bs, pMB->field_dct);                          BitstreamPutBit(bs, pMB->field_dct);
560                          DPRINTF(DPRINTF_MB,"codep: field_dct: %i", pMB->field_dct);                          DPRINTF(XVID_DEBUG_MB,"codep: field_dct: %i\n", pMB->field_dct);
561                  }                  }
562    
563                  // if inter block, write field ME flag                  /* if inter block, write field ME flag */
564                  if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {                  if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) && (pMB->mcsel == 0)) {
565                          BitstreamPutBit(bs, pMB->field_pred);                          BitstreamPutBit(bs, 0 /*pMB->field_pred*/); /* not implemented yet */
566                          DPRINTF(DPRINTF_MB,"codep: field_pred: %i", pMB->field_pred);                          DPRINTF(XVID_DEBUG_MB,"codep: field_pred: %i\n", pMB->field_pred);
567    
568                          // write field prediction references                          /* write field prediction references */
569    #if 0 /* Remove the #if once field_pred is supported */
570                          if (pMB->field_pred) {                          if (pMB->field_pred) {
571                                  BitstreamPutBit(bs, pMB->field_for_top);                                  BitstreamPutBit(bs, pMB->field_for_top);
572                                  BitstreamPutBit(bs, pMB->field_for_bot);                                  BitstreamPutBit(bs, pMB->field_for_bot);
573                          }                          }
574    #endif
575                  }                  }
576          }          }
577          // code motion vector(s) if motion is local          /* code motion vector(s) if motion is local  */
578          if (!pMB->mcsel)          if (!pMB->mcsel)
579                  for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) {                  for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) {
580                          CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat);                          CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat);
# Line 753  Line 583 
583    
584          bits = BitstreamPos(bs);          bits = BitstreamPos(bs);
585    
586          // code block coeffs          /* code block coeffs */
587          for (i = 0; i < 6; i++)          for (i = 0; i < 6; i++)
588                  if (pMB->cbp & (1 << (5 - i)))                  if (pMB->cbp & (1 << (5 - i))) {
                 {  
589                          const uint16_t *scan_table =                          const uint16_t *scan_table =
590                                  frame->global_flags & XVID_ALTERNATESCAN ?                                  frame->vop_flags & XVID_VOP_ALTERNATESCAN ?
591                                  scan_tables[2] : scan_tables[0];                                  scan_tables[2] : scan_tables[0];
592    
 #ifdef BIGLUT  
                         CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_table, 0);  
 #else  
593                          CodeCoeffInter(bs, &qcoeff[i * 64], scan_table);                          CodeCoeffInter(bs, &qcoeff[i * 64], scan_table);
 #endif  
594                  }                  }
595    
596          bits = BitstreamPos(bs) - bits;          bits = BitstreamPos(bs) - bits;
# Line 781  Line 606 
606                   Statistics * pStat)                   Statistics * pStat)
607  {  {
608          if (frame->coding_type != I_VOP)          if (frame->coding_type != I_VOP)
609                          BitstreamPutBit(bs, 0); // not_coded                          BitstreamPutBit(bs, 0); /* not_coded */
610    
611          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)
612                  CodeBlockIntra(frame, pMB, qcoeff, bs, pStat);                  CodeBlockIntra(frame, pMB, qcoeff, bs, pStat);
# Line 790  Line 615 
615    
616  }  }
617    
 /*  
 // moved to mbcoding.h so that in can be 'static __inline'  
 void  
 MBSkip(Bitstream * bs)  
 {  
         BitstreamPutBit(bs, 1); // not coded  
 }  
 */  
   
618  /***************************************************************  /***************************************************************
619   * bframe encoding start   * bframe encoding start
620   ***************************************************************/   ***************************************************************/
# Line 855  Line 671 
671                  BitstreamPutBit(bs, 1);                  BitstreamPutBit(bs, 1);
672                  return;                  return;
673    
674          default:;                                       // invalid          default:;                                       /* invalid */
675          }          }
676  }  }
677    
678    
679    
680  void  void
681  MBCodingBVOP(const MACROBLOCK * mb,  MBCodingBVOP(const FRAMEINFO * const frame,
682                             const MACROBLOCK * mb,
683                           const int16_t qcoeff[6 * 64],                           const int16_t qcoeff[6 * 64],
684                           const int32_t fcode,                           const int32_t fcode,
685                           const int32_t bcode,                           const int32_t bcode,
686                           Bitstream * bs,                           Bitstream * bs,
687                           Statistics * pStat,                           Statistics * pStat)
                          int direction)  
688  {  {
689          int vcode = fcode;          int vcode = fcode;
690          unsigned int i;          unsigned int i;
691    
692            const uint16_t *scan_table =
693                    frame->vop_flags & XVID_VOP_ALTERNATESCAN ?
694                    scan_tables[2] : scan_tables[0];
695            int bits;
696    
697    
698  /*      ------------------------------------------------------------------  /*      ------------------------------------------------------------------
699                  when a block is skipped it is decoded DIRECT(0,0)                  when a block is skipped it is decoded DIRECT(0,0)
700                  hence is interpolated from forward & backward frames                  hence is interpolated from forward & backward frames
701          ------------------------------------------------------------------ */          ------------------------------------------------------------------ */
702    
703          if (mb->mode == MODE_DIRECT_NONE_MV) {          if (mb->mode == MODE_DIRECT_NONE_MV) {
704                  BitstreamPutBit(bs, 1); // skipped                  BitstreamPutBit(bs, 1); /* skipped */
705                  return;                  return;
706          }          }
707    
708          BitstreamPutBit(bs, 0);         // not skipped          BitstreamPutBit(bs, 0);         /* not skipped */
709    
710          if (mb->cbp == 0) {          if (mb->cbp == 0) {
711                  BitstreamPutBit(bs, 1); // cbp == 0                  BitstreamPutBit(bs, 1); /* cbp == 0 */
712          } else {          } else {
713                  BitstreamPutBit(bs, 0); // cbp == xxx                  BitstreamPutBit(bs, 0); /* cbp == xxx */
714          }          }
715    
716          put_bvop_mbtype(bs, mb->mode);          put_bvop_mbtype(bs, mb->mode);
# Line 898  Line 720 
720          }          }
721    
722          if (mb->mode != MODE_DIRECT && mb->cbp != 0) {          if (mb->mode != MODE_DIRECT && mb->cbp != 0) {
723                  put_bvop_dbquant(bs, 0);        // todo: mb->dquant = 0                  put_bvop_dbquant(bs, 0);        /* todo: mb->dquant = 0 */
724            }
725    
726            if (frame->vol_flags & XVID_VOL_INTERLACING) {
727                    if (mb->cbp) {
728                            BitstreamPutBit(bs, mb->field_dct);
729                            DPRINTF(XVID_DEBUG_MB,"codep: field_dct: %i\n", mb->field_dct);
730                    }
731    
732                    /* if not direct block, write field ME flag */
733                    if (mb->mode != MODE_DIRECT) {
734                            BitstreamPutBit(bs, 0 /*mb->field_pred*/); /* field ME not implemented */
735    
736                            /* write field prediction references */
737    #if 0 /* Remove the #if once field_pred is supported */
738                            if (mb->field_pred) {
739                                    BitstreamPutBit(bs, mb->field_for_top);
740                                    BitstreamPutBit(bs, mb->field_for_bot);
741                            }
742    #endif
743                    }
744          }          }
745    
746    
747          switch (mb->mode) {          switch (mb->mode) {
748                  case MODE_INTERPOLATE:                  case MODE_INTERPOLATE:
749                          CodeVector(bs, mb->pmvs[1].x, vcode, pStat); //forward vector of interpolate mode                          CodeVector(bs, mb->pmvs[1].x, vcode, pStat); /* forward vector of interpolate mode */
750                          CodeVector(bs, mb->pmvs[1].y, vcode, pStat);                          CodeVector(bs, mb->pmvs[1].y, vcode, pStat);
751                  case MODE_BACKWARD:                  case MODE_BACKWARD:
752                          vcode = bcode;                          vcode = bcode;
# Line 912  Line 755 
755                          CodeVector(bs, mb->pmvs[0].y, vcode, pStat);                          CodeVector(bs, mb->pmvs[0].y, vcode, pStat);
756                          break;                          break;
757                  case MODE_DIRECT:                  case MODE_DIRECT:
758                          CodeVector(bs, mb->pmvs[3].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 */
759                          CodeVector(bs, mb->pmvs[3].y, 1, pStat);        // prediction is always (0,0)                          CodeVector(bs, mb->pmvs[3].y, 1, pStat);        /* prediction is always (0,0) */
760                  default: break;                  default: break;
761          }          }
762    
763            bits = BitstreamPos(bs);
764          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
765                  if (mb->cbp & (1 << (5 - i))) {                  if (mb->cbp & (1 << (5 - i))) {
766  #ifdef BIGLUT                          CodeCoeffInter(bs, &qcoeff[i * 64], scan_table);
                         CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0);  
 #else  
                         CodeCoeffInter(bs, &qcoeff[i * 64], scan_tables[0]);  
 #endif  
767                  }                  }
768          }          }
769            pStat->iTextBits += BitstreamPos(bs) - bits;
770  }  }
771    
772    
# Line 935  Line 776 
776   ***************************************************************/   ***************************************************************/
777    
778    
779  // for IVOP addbits == 0  /*
780  // for PVOP addbits == fcode - 1   * for IVOP addbits == 0
781  // for BVOP addbits == max(fcode,bcode) - 1   * for PVOP addbits == fcode - 1
782  // returns true or false   * for BVOP addbits == max(fcode,bcode) - 1
783     * returns true or false
784     */
785  int  int
786  check_resync_marker(Bitstream * bs, int addbits)  check_resync_marker(Bitstream * bs, int addbits)
787  {  {
# Line 1052  Line 895 
895                  return data;                  return data;
896    
897          res = BitstreamGetBits(bs, fcode - 1);          res = BitstreamGetBits(bs, fcode - 1);
898          mv = ((ABS(data) - 1) * scale_fac) + res + 1;          mv = ((abs(data) - 1) * scale_fac) + res + 1;
899    
900          return data < 0 ? -mv : mv;          return data < 0 ? -mv : mv;
901    
# Line 1153  Line 996 
996                  level = BitstreamGetBits(bs, 8);                  level = BitstreamGetBits(bs, 8);
997    
998                  if (level == 0 || level == 128)                  if (level == 0 || level == 128)
999                          DPRINTF(DPRINTF_ERROR, "Illegal LEVEL for ESCAPE mode 4: %d", level);                          DPRINTF(XVID_DEBUG_ERROR, "Illegal LEVEL for ESCAPE mode 4: %d\n", level);
1000    
1001                  return (level << 24) >> 24;                  return (level << 24) >> 24;
1002          }          }
# Line 1209  Line 1052 
1052          do {          do {
1053                  level = get_coeff(bs, &run, &last, 1, 0);                  level = get_coeff(bs, &run, &last, 1, 0);
1054                  if (run == -1) {                  if (run == -1) {
1055                          DPRINTF(DPRINTF_ERROR,"fatal: invalid run");                          DPRINTF(XVID_DEBUG_ERROR,"fatal: invalid run");
1056                          break;                          break;
1057                  }                  }
1058                  coeff += run;                  coeff += run;
1059                  block[scan[coeff]] = level;                  block[scan[coeff]] = level;
1060    
1061                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level);                  DPRINTF(XVID_DEBUG_COEFF,"block[%i] %i\n", scan[coeff], level);
1062                  //DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[coeff], level, BitstreamShowBits(bs, 32));  #if 0
1063                    DPRINTF(XVID_DEBUG_COEFF,"block[%i] %i %08x\n", scan[coeff], level, BitstreamShowBits(bs, 32));
1064    #endif
1065    
1066                  if (level < -2047 || level > 2047) {                  if (level < -2047 || level > 2047) {
1067                          DPRINTF(DPRINTF_ERROR,"warning: intra_overflow %i", level);                          DPRINTF(XVID_DEBUG_ERROR,"warning: intra_overflow %i\n", level);
1068                  }                  }
1069                  coeff++;                  coeff++;
1070          } while (!last);          } while (!last);
# Line 1242  Line 1087 
1087          do {          do {
1088                  level = get_coeff(bs, &run, &last, 0, 0);                  level = get_coeff(bs, &run, &last, 0, 0);
1089                  if (run == -1) {                  if (run == -1) {
1090                          DPRINTF(DPRINTF_ERROR,"fatal: invalid run");                          DPRINTF(XVID_DEBUG_ERROR,"fatal: invalid run");
1091                          break;                          break;
1092                  }                  }
1093                  p += run;                  p += run;
1094    
1095                  block[scan[p]] = level;                  block[scan[p]] = level;
1096    
1097                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level);                  DPRINTF(XVID_DEBUG_COEFF,"block[%i] %i\n", scan[p], level);
1098                  // DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[p], level, BitstreamShowBits(bs, 32));                  /* DPRINTF(XVID_DEBUG_COEFF,"block[%i] %i %08x\n", scan[p], level, BitstreamShowBits(bs, 32)); */
1099    
1100                  if (level < -2047 || level > 2047) {                  if (level < -2047 || level > 2047) {
1101                          DPRINTF(DPRINTF_ERROR,"warning: inter overflow %i", level);                          DPRINTF(XVID_DEBUG_ERROR,"warning: inter overflow %i\n", level);
1102                  }                  }
1103                  p++;                  p++;
1104          } while (!last);          } while (!last);
1105    
1106  }  }
1107    
1108    
1109  /*****************************************************************************  /*****************************************************************************
1110   * VLC tables and other constant arrays   * VLC tables and other constant arrays
1111   ****************************************************************************/   ****************************************************************************/
# Line 1590  Line 1436 
1436  VLC sprite_trajectory_len[15] = {  VLC sprite_trajectory_len[15] = {
1437          { 0x00 , 2},          { 0x00 , 2},
1438          { 0x02 , 3}, { 0x03, 3}, { 0x04, 3}, { 0x05, 3}, { 0x06, 3},          { 0x02 , 3}, { 0x03, 3}, { 0x04, 3}, { 0x05, 3}, { 0x06, 3},
1439          { 0x0E , 4}, { 0x1E, 5}, { 0x3E, 6}, { 0x7F, 7}, { 0xFE, 8},          { 0x0E , 4}, { 0x1E, 5}, { 0x3E, 6}, { 0x7E, 7}, { 0xFE, 8},
1440          { 0x1FE, 9}, {0x3FE,10}, {0x7FE,11}, {0xFFE,12} };          { 0x1FE, 9}, {0x3FE,10}, {0x7FE,11}, {0xFFE,12} };
1441    
1442    
# Line 1618  Line 1464 
1464          {5, 6}, {5, 9}, {5, 8}, {3, 7}, {2, 9}          {5, 6}, {5, 9}, {5, 8}, {3, 7}, {2, 9}
1465  };  };
1466    
1467  const VLC cbpy_tab[16] = {  const VLC xvid_cbpy_tab[16] = {
1468          {3, 4}, {5, 5}, {4, 5}, {9, 4}, {3, 5}, {7, 4}, {2, 6}, {11, 4},          {3, 4}, {5, 5}, {4, 5}, {9, 4}, {3, 5}, {7, 4}, {2, 6}, {11, 4},
1469          {2, 5}, {3, 6}, {5, 4}, {10, 4}, {4, 4}, {8, 4}, {6, 4}, {3, 2}          {2, 5}, {3, 6}, {5, 4}, {10, 4}, {4, 4}, {8, 4}, {6, 4}, {3, 2}
1470  };  };

Legend:
Removed from v.1.44  
changed lines
  Added in v.1.44.2.18

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