[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.2.11, Thu Jan 16 21:16:04 2003 UTC revision 1.37, Sun Jan 5 16:54:36 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    *                                                                            *   *  - Macro Block coding functions -
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 file is part of XviD, a free MPEG-4 video encoder/decoder
9    *  would be at such party's own risk.  The original developer of this        *   *
10    *  software module and his/her company, and subsequent editors and their     *   *  XviD is free software; you can redistribute it and/or modify it
11    *  companies, will have no liability for use of this software or             *   *  under the terms of the GNU General Public License as published by
12    *  modifications or derivatives thereof.                                     *   *  the Free Software Foundation; either version 2 of the License, or
13    *                                                                            *   *  (at your option) any later version.
14    *  XviD is free software; you can redistribute it and/or modify it           *   *
15    *  under the terms of the GNU General Public License as published by         *   *  This program is distributed in the hope that it will be useful,
16    *  the Free Software Foundation; either version 2 of the License, or         *   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17    *  (at your option) any later version.                                       *   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    *                                                                            *   *  GNU General Public License for more details.
19    *  XviD is distributed in the hope that it will be useful, but               *   *
20    *  WITHOUT ANY WARRANTY; without even the implied warranty of                *   *  You should have received a copy of the GNU General Public License
21    *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *   *  along with this program; if not, write to the Free Software
22    *  GNU General Public License for more details.                              *   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23    *                                                                            *   *
24    *  You should have received a copy of the GNU General Public License         *   *  Under section 8 of the GNU General Public License, the copyright
25    *  along with this program; if not, write to the Free Software               *   *  holders of XVID explicitly forbid distribution in the following
26    *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA  *   *  countries:
27    *                                                                            *   *
28    ******************************************************************************/   *    - Japan
29     *    - United States of America
30   /******************************************************************************   *
31    *                                                                            *   *  Linking XviD statically or dynamically with other modules is making a
32    *  mbcoding.c                                                                *   *  combined work based on XviD.  Thus, the terms and conditions of the
33    *                                                                            *   *  GNU General Public License cover the whole combination.
34    *  Copyright (C) 2002 - Michael Militzer <isibaar@xvid.org>                  *   *
35    *                                                                            *   *  As a special exception, the copyright holders of XviD give you
36    *  For more information visit the XviD homepage: http://www.xvid.org         *   *  permission to link XviD with independent modules that communicate with
37    *                                                                            *   *  XviD solely through the VFW1.1 and DShow interfaces, regardless of the
38    ******************************************************************************/   *  license terms of these independent modules, and to copy and distribute
39     *  the resulting combined work under terms of your choice, provided that
40   /******************************************************************************   *  every copy of the combined work is accompanied by a complete copy of
41    *                                                                                                                                                        *   *  the source code of XviD (the version of XviD used to produce the
42    *  Revision history:                                                         *   *  combined work), being distributed under the terms of the GNU General
43    *                                                                            *   *  Public License plus this exception.  An independent module is a module
44    *  28.10.2002 GMC support - gruel                                                                                        *   *  which is not derived from or based on XviD.
45    *  28.06.2002 added check_resync_marker()                                    *   *
46    *  14.04.2002 bframe encoding                                                                                            *   *  Note that people who make modified versions of XviD are not obligated
47    *  08.03.2002 initial version; isibaar                                                           *   *  to grant this special exception for their modified versions; it is
48    *                                                                                                                                                        *   *  their choice whether to do so.  The GNU General Public License gives
49    ******************************************************************************/   *  permission to release a modified version without this exception; this
50     *  exception also makes it possible to release a modified version which
51     *  carries forward this exception.
52     *
53     * $Id$
54     *
55     ****************************************************************************/
56    
   
 #include <stdio.h>  
57  #include <stdlib.h>  #include <stdlib.h>
58  #include "../portab.h"  #include "../portab.h"
 #include "../global.h"  
59  #include "bitstream.h"  #include "bitstream.h"
60  #include "zigzag.h"  #include "zigzag.h"
61  #include "vlc_codes.h"  #include "vlc_codes.h"
# Line 60  Line 63 
63    
64  #include "../utils/mbfunctions.h"  #include "../utils/mbfunctions.h"
65    
66  /* #define BIGLUT */  #define ABS(X) (((X)>0)?(X):-(X))
67    #define CLIP(X,A) (X > A) ? (A) : (X)
68    
69    /*#define _BIGLUT_*/
70    
71  #ifdef BIGLUT  #ifdef _BIGLUT_
72  #define LEVELOFFSET 2048  #define LEVELOFFSET 2048
73  #else  #else
74  #define LEVELOFFSET 32  #define LEVELOFFSET 32
75  #endif  #endif
76    
77    /*****************************************************************************
78     * Local data
79     ****************************************************************************/
80    
81  static REVERSE_EVENT DCT3D[2][4096];  static REVERSE_EVENT DCT3D[2][4096];
82    
83  #ifdef BIGLUT  #ifdef _BIGLUT_
84  static VLC coeff_VLC[2][2][4096][64];  static VLC coeff_VLC[2][2][4096][64];
85  static VLC *intra_table, *inter_table;  VLC *intra_table, *inter_table;
86  #else  #else
87  static VLC coeff_VLC[2][2][64][64];  static VLC coeff_VLC[2][2][64][64];
88  #endif  #endif
89    
90  /* not really MB related, but VLCs are only available here */  /*****************************************************************************
91  void bs_put_spritetrajectory(Bitstream * bs, const int val)   * Vector Length Coding Initialization
92  {   ****************************************************************************/
         const int code = sprite_trajectory_code[val+16384].code;  
         const int len = sprite_trajectory_code[val+16384].len;  
         const int code2 = sprite_trajectory_len[len].code;  
         const int len2 = sprite_trajectory_len[len].len;  
   
 //      printf("GMC=%d Code/Len  = %d / %d ",val, code,len);  
 //      printf("Code2 / Len2 = %d / %d \n",code2,len2);  
   
         BitstreamPutBits(bs, code2, len2);  
         if (len) BitstreamPutBits(bs, code, len);  
 }  
   
 int bs_get_spritetrajectory(Bitstream * bs)  
 {  
         int i;  
         for (i = 0; i < 12; i++)  
         {  
                 if (BitstreamShowBits(bs, sprite_trajectory_len[i].len) == sprite_trajectory_len[i].code)  
                 {  
                         BitstreamSkip(bs, sprite_trajectory_len[i].len);  
                         return i;  
                 }  
         }  
         return -1;  
 }  
93    
94  void  void
95  init_vlc_tables(void)  init_vlc_tables(void)
96  {  {
97          uint32_t i, j, k, intra, last, run,  run_esc, level, level_esc, escape, escape_len, offset, limit;          uint32_t i, j, intra, last, run, offset;
98          int32_t l;          int32_t level;
99            VLC coeff_VLC_temp[2][2][64][64];
100    
101  #ifdef BIGLUT  #ifdef _BIGLUT_
102          intra_table = coeff_VLC[1];          intra_table = coeff_VLC[1];
103          inter_table = coeff_VLC[0];          inter_table = coeff_VLC[0];
104  #endif  #endif
# Line 128  Line 114 
114                          for (run = 0; run < 63 + last; run++)                          for (run = 0; run < 63 + last; run++)
115                                  for (level = 0; level < 32 << intra; level++)                                  for (level = 0; level < 32 << intra; level++)
116                                  {                                  {
117  #ifdef BIGLUT  #ifdef _BIGLUT_
118                                          offset = LEVELOFFSET;                                          offset = LEVELOFFSET;
119  #else  #else
120                                          offset = !intra * LEVELOFFSET;                                          offset = !intra * LEVELOFFSET;
121  #endif  #endif
122                                            coeff_VLC_temp[intra][last][level][run].len             = 128;
123                                          coeff_VLC[intra][last][level + offset][run].len = 128;                                          coeff_VLC[intra][last][level + offset][run].len = 128;
124                                  }                                  }
125                  }                  }
# Line 140  Line 127 
127          for (intra = 0; intra < 2; intra++)          for (intra = 0; intra < 2; intra++)
128                  for (i = 0; i < 102; i++)                  for (i = 0; i < 102; i++)
129                  {                  {
130  #ifdef BIGLUT  #ifdef _BIGLUT_
131                          offset = LEVELOFFSET;                          offset = LEVELOFFSET;
132  #else  #else
133                          offset = !intra * LEVELOFFSET;                          offset = !intra * LEVELOFFSET;
# Line 151  Line 138 
138                                  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;
139                          }                          }
140    
141                            coeff_VLC_temp[intra][coeff_tab[intra][i].event.last][coeff_tab[intra][i].event.level][coeff_tab[intra][i].event.run].code
142                                    = coeff_tab[intra][i].vlc.code << 1;
143                            coeff_VLC_temp[intra][coeff_tab[intra][i].event.last][coeff_tab[intra][i].event.level][coeff_tab[intra][i].event.run].len
144                                    = coeff_tab[intra][i].vlc.len + 1;
145    
146                          coeff_VLC[intra][coeff_tab[intra][i].event.last][coeff_tab[intra][i].event.level + offset][coeff_tab[intra][i].event.run].code                          coeff_VLC[intra][coeff_tab[intra][i].event.last][coeff_tab[intra][i].event.level + offset][coeff_tab[intra][i].event.run].code
147                                  = coeff_tab[intra][i].vlc.code << 1;                                  = coeff_tab[intra][i].vlc.code << 1;
148                          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
149                                  = coeff_tab[intra][i].vlc.len + 1;                                  = coeff_tab[intra][i].vlc.len + 1;
150  #ifndef BIGLUT  #ifndef _BIGLUT_
151                          if (!intra)                          if (!intra)
152  #endif  #endif
153                          {                          {
# Line 172  Line 164 
164                          {                          {
165                                  for (level = 1; level < 32 << intra; level++)                                  for (level = 1; level < 32 << intra; level++)
166                                  {                                  {
167                                          if (level <= max_level[intra][last][run] && run <= max_run[intra][last][level])  #ifdef _BIGLUT_
                                             continue;  
   
 #ifdef BIGLUT  
168                                          offset = LEVELOFFSET;                                          offset = LEVELOFFSET;
169  #else  #else
170                                          offset = !intra * LEVELOFFSET;                                          offset = !intra * LEVELOFFSET;
171  #endif  #endif
172                      level_esc = level - max_level[intra][last][run];                                          if ((max_level[intra][last][run]) && (level > max_level[intra][last][run]))
173                                          run_esc = run - 1 - max_run[intra][last][level];                                                  if (coeff_VLC_temp[intra][last][level - max_level[intra][last][run]][run].len != 128
174                                          /*use this test to use shorter esc2 codes when possible                                                          && coeff_VLC[intra][last][level + offset][run].len == 128)
                                         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))*/  
   
                                         if (level_esc <= max_level[intra][last][run] && run <= max_run[intra][last][level_esc])  
                                         {  
                                                 escape     = ESCAPE1;  
                                                 escape_len = 7 + 1;  
                                                 run_esc    = run;  
                                         }  
                                         else  
                                         {  
                                                 if (level <= max_level[intra][last][run_esc] && run_esc <= max_run[intra][last][level])  
175                                                  {                                                  {
176                                                          escape     = ESCAPE2;                                                          coeff_VLC[intra][last][level + offset][run].code
177                                                          escape_len = 7 + 2;                                                                  = (ESCAPE1 << coeff_VLC_temp[intra][last][level - max_level[intra][last][run]][run].len)
178                                                          level_esc  = level;                                                                  |  coeff_VLC_temp[intra][last][level - max_level[intra][last][run]][run].code;
179                                                  }                                                          coeff_VLC[intra][last][level + offset][run].len
180                                                  else                                                                  = coeff_VLC_temp[intra][last][level - max_level[intra][last][run]][run].len + 7 + 1;
181                                                  {  #ifndef _BIGLUT_
 #ifndef BIGLUT  
182                                                          if (!intra)                                                          if (!intra)
183  #endif  #endif
184                                                          {                                                          {
                                                                 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;  
185                                                                          coeff_VLC[intra][last][offset - level][run].code                                                                          coeff_VLC[intra][last][offset - level][run].code
186                                                                          = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-level & 0xfff) << 1) | 1;                                                                          = (ESCAPE1 << coeff_VLC_temp[intra][last][level - max_level[intra][last][run]][run].len)
187                                                                  coeff_VLC[intra][last][offset - level][run].len = 30;                                                                          |  coeff_VLC_temp[intra][last][level - max_level[intra][last][run]][run].code | 1;
188                                                          }                                                                  coeff_VLC[intra][last][offset - level][run].len
189                                                          continue;                                                                          = coeff_VLC_temp[intra][last][level - max_level[intra][last][run]][run].len + 7 + 1;
190                                                  }                                                  }
191                                          }                                          }
192                                            if (run > max_run[intra][last][level])
193                                                    if (coeff_VLC_temp[intra][last][level][run - 1 - max_run[intra][last][level]].len != 128
194                                                            && coeff_VLC[intra][last][level + offset][run].len == 128)
195                                                    /*use the lower test instead of the upper to use shorter escape codes when possible :
196                                                    if (coeff_VLC_temp[intra][last][level][run - 1 - max_run[intra][last][level]].len + 7 + 2
197                                                            < coeff_VLC[intra][last][level + offset][run].len)*/
198                                                    {
199                                          coeff_VLC[intra][last][level + offset][run].code                                          coeff_VLC[intra][last][level + offset][run].code
200                                                  = (escape << coeff_VLC[intra][last][level_esc + offset][run_esc].len)                                                                  = (ESCAPE2 << coeff_VLC_temp[intra][last][level][run - 1 - max_run[intra][last][level]].len)
201                                                  |  coeff_VLC[intra][last][level_esc + offset][run_esc].code;                                                                  |  coeff_VLC_temp[intra][last][level][run - 1 - max_run[intra][last][level]].code;
202                                          coeff_VLC[intra][last][level + offset][run].len                                          coeff_VLC[intra][last][level + offset][run].len
203                                                  = coeff_VLC[intra][last][level_esc + offset][run_esc].len + escape_len;                                                                  = coeff_VLC_temp[intra][last][level][run - 1 - max_run[intra][last][level]].len + 7 + 2;
204  #ifndef BIGLUT  #ifndef _BIGLUT_
205                                          if (!intra)                                          if (!intra)
206  #endif  #endif
207                                          {                                          {
208                                                  coeff_VLC[intra][last][offset - level][run].code                                                  coeff_VLC[intra][last][offset - level][run].code
209                                                          = (escape << coeff_VLC[intra][last][level_esc + offset][run_esc].len)                                                                          = (ESCAPE2 << coeff_VLC_temp[intra][last][level][run - 1 - max_run[intra][last][level]].len)
210                                                          |  coeff_VLC[intra][last][level_esc + offset][run_esc].code | 1;                                                                          |  coeff_VLC_temp[intra][last][level][run - 1 - max_run[intra][last][level]].code | 1;
211                                                  coeff_VLC[intra][last][offset - level][run].len                                                  coeff_VLC[intra][last][offset - level][run].len
212                                                          = coeff_VLC[intra][last][level_esc + offset][run_esc].len + escape_len;                                                                          = coeff_VLC_temp[intra][last][level][run - 1 - max_run[intra][last][level]].len + 7 + 2;
213                                          }                                          }
214                                  }                                  }
215    #ifndef _BIGLUT_
216                                            if (!intra)
217    #endif
218                                                    if (coeff_VLC[intra][last][level + offset][run].len == 128)
219                                                    {
220                                                            coeff_VLC[intra][last][level + offset][run].code
221                                                                    = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((level & 0xfff) << 1) | 1;
222                                                            coeff_VLC[intra][last][level + offset][run].len = 30;
223    
224  #ifdef BIGLUT                                                          coeff_VLC[intra][last][offset - level][run].code
225                                                                    = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-level & 0xfff) << 1) | 1;
226                                                            coeff_VLC[intra][last][offset - level][run].len = 30;
227                                                    }
228                                    }
229    #ifdef _BIGLUT_
230                                  for (level = 32 << intra; level < 2048; level++)                                  for (level = 32 << intra; level < 2048; level++)
231                                  {                                  {
232                                          coeff_VLC[intra][last][level + offset][run].code                                          coeff_VLC[intra][last][level + offset][run].code
# Line 255  Line 246 
246                                  }                                  }
247  #endif  #endif
248                          }                          }
 /* init sprite_trajectory tables */  
 /* even if GMC is not specified (it might be used later...) */  
   
         sprite_trajectory_code[0+16384].code = 0;  
         sprite_trajectory_code[0+16384].len = 0;  
         for (k=0;k<14;k++)  
         {  
                 int limit = (1<<k);  
   
                 for (l=-(2*limit-1); l <= -limit; l++)  
                 {  
                         sprite_trajectory_code[i+16384].code = (2*limit-1)+l;  
                         sprite_trajectory_code[i+16384].len = k+1;  
249                  }                  }
250    
251                  for (l=limit; l<= 2*limit-1; l++)  /*****************************************************************************
252                  {   * Local inlined functions for MB coding
253                          sprite_trajectory_code[i+16384].code = l;   ****************************************************************************/
                         sprite_trajectory_code[i+16384].len = k+1;  
                 }  
         }  
 }  
254    
255  static __inline void  static __inline void
256  CodeVector(Bitstream * bs,  CodeVector(Bitstream * bs,
# Line 333  Line 307 
307    
308  }  }
309    
310  #ifdef BIGLUT  #ifdef __BIGLUT_
311    
312  static __inline void  static __inline void
313  CodeCoeff(Bitstream * bs,  CodeCoeff(Bitstream * bs,
# Line 489  Line 463 
463    
464  #endif  #endif
465    
466  static __inline void  /*****************************************************************************
467  CodeBlockIntra(const FRAMEINFO * const frame,   * Local functions
468     ****************************************************************************/
469    
470    static void
471    CodeBlockIntra(const FRAMEINFO * frame,
472                             const MACROBLOCK * pMB,                             const MACROBLOCK * pMB,
473                             int16_t qcoeff[6 * 64],                             int16_t qcoeff[6 * 64],
474                             Bitstream * bs,                             Bitstream * bs,
# Line 501  Line 479 
479    
480          cbpy = pMB->cbp >> 2;          cbpy = pMB->cbp >> 2;
481    
482          // write mcbpc          /* write mcbpc */
483          if (frame->coding_type == I_VOP) {          if (frame->coding_type == I_VOP) {
484                  mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2);                  mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2);
485                  BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code,                  BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code,
# Line 512  Line 490 
490                                                   mcbpc_inter_tab[mcbpc].len);                                                   mcbpc_inter_tab[mcbpc].len);
491          }          }
492    
493          // ac prediction flag          /* ac prediction flag */
494          if (pMB->acpred_directions[0])          if (pMB->acpred_directions[0])
495                  BitstreamPutBits(bs, 1, 1);                  BitstreamPutBits(bs, 1, 1);
496          else          else
497                  BitstreamPutBits(bs, 0, 1);                  BitstreamPutBits(bs, 0, 1);
498    
499          // write cbpy          /* write cbpy */
500          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);
501    
502          // write dquant          /* write dquant */
503          if (pMB->mode == MODE_INTRA_Q)          if (pMB->mode == MODE_INTRA_Q)
504                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, pMB->dquant, 2);
505    
506          // write interlacing          /* write interlacing */
507          if (frame->global_flags & XVID_INTERLACING) {          if (frame->global_flags & XVID_INTERLACING) {
508                  BitstreamPutBit(bs, pMB->field_dct);                  BitstreamPutBit(bs, pMB->field_dct);
509          }          }
510          // code block coeffs          /* code block coeffs */
511          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
512                  if (i < 4)                  if (i < 4)
513                          BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code,                          BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code,
# Line 539  Line 517 
517                                                           dcc_tab[qcoeff[i * 64 + 0] + 255].len);                                                           dcc_tab[qcoeff[i * 64 + 0] + 255].len);
518    
519                  if (pMB->cbp & (1 << (5 - i))) {                  if (pMB->cbp & (1 << (5 - i))) {
                         const uint16_t *scan_table =  
                                 frame->global_flags & XVID_ALTERNATESCAN ?  
                                 scan_tables[2] : scan_tables[pMB->acpred_directions[i]];  
   
520                          bits = BitstreamPos(bs);                          bits = BitstreamPos(bs);
521    
522  #ifdef BIGLUT  #ifdef _BIGLUT_
523                          CodeCoeff(bs, &qcoeff[i * 64], intra_table, scan_table, 1);                          CodeCoeff(bs, &qcoeff[i * 64], intra_table,
524                                              scan_tables[pMB->acpred_directions[i]], 1);
525  #else  #else
526                          CodeCoeffIntra(bs, &qcoeff[i * 64], scan_table);                          CodeCoeffIntra(bs, &qcoeff[i * 64], scan_tables[pMB->acpred_directions[i]]);
527  #endif  #endif
   
528                          bits = BitstreamPos(bs) - bits;                          bits = BitstreamPos(bs) - bits;
529                          pStat->iTextBits += bits;                          pStat->iTextBits += bits;
530                  }                  }
# Line 560  Line 534 
534    
535    
536  static void  static void
537  CodeBlockInter(const FRAMEINFO * const frame,  CodeBlockInter(const FRAMEINFO * frame,
538                             const MACROBLOCK * pMB,                             const MACROBLOCK * pMB,
539                             int16_t qcoeff[6 * 64],                             int16_t qcoeff[6 * 64],
540                             Bitstream * bs,                             Bitstream * bs,
# Line 573  Line 547 
547          mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);          mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);
548          cbpy = 15 - (pMB->cbp >> 2);          cbpy = 15 - (pMB->cbp >> 2);
549    
550          // write mcbpc          /* write mcbpc */
551          BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,          BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,
552                                           mcbpc_inter_tab[mcbpc].len);                                           mcbpc_inter_tab[mcbpc].len);
553    
554          if ( (frame->coding_type == S_VOP) && (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) )          /* write cbpy */
                 BitstreamPutBit(bs, pMB->mcsel);                // mcsel: '0'=local motion, '1'=GMC  
   
         // write cbpy  
555          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);
556    
557          // write dquant          /* write dquant */
558          if (pMB->mode == MODE_INTER_Q)          if (pMB->mode == MODE_INTER_Q)
559                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, pMB->dquant, 2);
560    
561          // interlacing          /* interlacing */
562          if (frame->global_flags & XVID_INTERLACING) {          if (frame->global_flags & XVID_INTERLACING) {
563                  if (pMB->cbp) {                  if (pMB->cbp) {
564                          BitstreamPutBit(bs, pMB->field_dct);                          BitstreamPutBit(bs, pMB->field_dct);
565                          DPRINTF(DPRINTF_MB,"codep: field_dct: %i", pMB->field_dct);                          DPRINTF(DPRINTF_DEBUG, "codep: field_dct: %d", pMB->field_dct);
566                  }                  }
567    
568                  // if inter block, write field ME flag                  /* if inter block, write field ME flag */
569                  if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {                  if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
570                          BitstreamPutBit(bs, pMB->field_pred);                          BitstreamPutBit(bs, pMB->field_pred);
571                          DPRINTF(DPRINTF_MB,"codep: field_pred: %i", pMB->field_pred);                          DPRINTF(DPRINTF_DEBUG, "codep: field_pred: %d", pMB->field_pred);
572    
573                          // write field prediction references                          /* write field prediction references */
574                          if (pMB->field_pred) {                          if (pMB->field_pred) {
575                                  BitstreamPutBit(bs, pMB->field_for_top);                                  BitstreamPutBit(bs, pMB->field_for_top);
576                                  BitstreamPutBit(bs, pMB->field_for_bot);                                  BitstreamPutBit(bs, pMB->field_for_bot);
577                          }                          }
578                  }                  }
579          }          }
580          // code motion vector(s) if motion is local          /* code motion vector(s) */
         if (!pMB->mcsel)  
581                  for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) {                  for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) {
582                          CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat);                          CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat);
583                          CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat);                          CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat);
# Line 615  Line 585 
585    
586          bits = BitstreamPos(bs);          bits = BitstreamPos(bs);
587    
588          // code block coeffs          /* code block coeffs */
589          for (i = 0; i < 6; i++)          for (i = 0; i < 6; i++)
590                  if (pMB->cbp & (1 << (5 - i)))                  if (pMB->cbp & (1 << (5 - i)))
591                  {  #ifdef _BIGLUT_
592                          const uint16_t *scan_table =                          CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0);
                                 frame->global_flags & XVID_ALTERNATESCAN ?  
                                 scan_tables[2] : scan_tables[0];  
   
 #ifdef BIGLUT  
                         CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_table, 0);  
593  #else  #else
594                          CodeCoeffInter(bs, &qcoeff[i * 64], scan_table);                          CodeCoeffInter(bs, &qcoeff[i * 64], scan_tables[0]);
595  #endif  #endif
                 }  
596    
597          bits = BitstreamPos(bs) - bits;          bits = BitstreamPos(bs) - bits;
598          pStat->iTextBits += bits;          pStat->iTextBits += bits;
599    
600  }  }
601    
602    /*****************************************************************************
603     * Macro Block bitstream encoding functions
604     ****************************************************************************/
605    
606  void  void
607  MBCoding(const FRAMEINFO * const frame,  MBCoding(const FRAMEINFO * frame,
608                   MACROBLOCK * pMB,                   MACROBLOCK * pMB,
609                   int16_t qcoeff[6 * 64],                   int16_t qcoeff[6 * 64],
610                   Bitstream * bs,                   Bitstream * bs,
611                   Statistics * pStat)                   Statistics * pStat)
612  {  {
613          if (frame->coding_type != I_VOP)  
614                          BitstreamPutBit(bs, 0); // not_coded          if (frame->coding_type == P_VOP) {
615                            BitstreamPutBit(bs, 0); /* coded */
616            }
617    
618          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)
619                  CodeBlockIntra(frame, pMB, qcoeff, bs, pStat);                  CodeBlockIntra(frame, pMB, qcoeff, bs, pStat);
# Line 652  Line 622 
622    
623  }  }
624    
625  /*  
 // moved to mbcoding.h so that in can be 'static __inline'  
626  void  void
627  MBSkip(Bitstream * bs)  MBSkip(Bitstream * bs)
628  {  {
629          BitstreamPutBit(bs, 1); // not coded          BitstreamPutBit(bs, 1); /* not coded */
630            return;
631  }  }
 */  
   
 /***************************************************************  
  * bframe encoding start  
  ***************************************************************/  
632    
633  /*  /*****************************************************************************
634          mbtype   * decoding stuff starts here
635          0       1b              direct(h263)            mvdb   ****************************************************************************/
         1       01b             interpolate mc+q        dbquant, mvdf, mvdb  
         2       001b    backward mc+q           dbquant, mvdb  
         3       0001b   forward mc+q            dbquant, mvdf  
 */  
   
 static __inline void  
 put_bvop_mbtype(Bitstream * bs,  
                                 int value)  
 {  
         switch (value) {  
                 case MODE_FORWARD:  
                         BitstreamPutBit(bs, 0);  
                 case MODE_BACKWARD:  
                         BitstreamPutBit(bs, 0);  
                 case MODE_INTERPOLATE:  
                         BitstreamPutBit(bs, 0);  
                 case MODE_DIRECT:  
                         BitstreamPutBit(bs, 1);  
                 default:  
                         break;  
         }  
 }  
636    
637  /*  /*
638          dbquant   * For IVOP addbits == 0
639          -2      10b   * For PVOP addbits == fcode - 1
640          0       0b   * For BVOP addbits == max(fcode,bcode) - 1
641          +2      11b   * returns true or false
642  */  */
643    
 static __inline void  
 put_bvop_dbquant(Bitstream * bs,  
                                  int value)  
 {  
         switch (value) {  
         case 0:  
                 BitstreamPutBit(bs, 0);  
                 return;  
   
         case -2:  
                 BitstreamPutBit(bs, 1);  
                 BitstreamPutBit(bs, 0);  
                 return;  
   
         case 2:  
                 BitstreamPutBit(bs, 1);  
                 BitstreamPutBit(bs, 1);  
                 return;  
   
         default:;                                       // invalid  
         }  
 }  
   
   
   
 void  
 MBCodingBVOP(const MACROBLOCK * mb,  
                          const int16_t qcoeff[6 * 64],  
                          const int32_t fcode,  
                          const int32_t bcode,  
                          Bitstream * bs,  
                          Statistics * pStat,  
                          int direction)  
 {  
         int vcode = fcode;  
         unsigned int i;  
   
 /*      ------------------------------------------------------------------  
                 when a block is skipped it is decoded DIRECT(0,0)  
                 hence is interpolated from forward & backward frames  
         ------------------------------------------------------------------ */  
   
         if (mb->mode == MODE_DIRECT_NONE_MV) {  
                 BitstreamPutBit(bs, 1); // skipped  
                 return;  
         }  
   
         BitstreamPutBit(bs, 0);         // not skipped  
   
         if (mb->cbp == 0) {  
                 BitstreamPutBit(bs, 1); // cbp == 0  
         } else {  
                 BitstreamPutBit(bs, 0); // cbp == xxx  
         }  
   
         put_bvop_mbtype(bs, mb->mode);  
   
         if (mb->cbp) {  
                 BitstreamPutBits(bs, mb->cbp, 6);  
         }  
   
         if (mb->mode != MODE_DIRECT && mb->cbp != 0) {  
                 put_bvop_dbquant(bs, 0);        // todo: mb->dquant = 0  
         }  
   
         switch (mb->mode) {  
                 case MODE_INTERPOLATE:  
                         CodeVector(bs, mb->pmvs[1].x, vcode, pStat); //forward vector of interpolate mode  
                         CodeVector(bs, mb->pmvs[1].y, vcode, pStat);  
                 case MODE_BACKWARD:  
                         vcode = bcode;  
                 case MODE_FORWARD:  
                         CodeVector(bs, mb->pmvs[0].x, vcode, pStat);  
                         CodeVector(bs, mb->pmvs[0].y, vcode, pStat);  
                         break;  
                 case MODE_DIRECT:  
                         CodeVector(bs, mb->pmvs[3].x, 1, pStat);        // fcode is always 1 for delta vector  
                         CodeVector(bs, mb->pmvs[3].y, 1, pStat);        // prediction is always (0,0)  
                 default: break;  
         }  
   
         for (i = 0; i < 6; i++) {  
                 if (mb->cbp & (1 << (5 - i))) {  
 #ifdef BIGLUT  
                         CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0);  
 #else  
                         CodeCoeffInter(bs, &qcoeff[i * 64], scan_tables[0]);  
 #endif  
                 }  
         }  
 }  
   
   
   
 /***************************************************************  
  * decoding stuff starts here                                  *  
  ***************************************************************/  
   
   
 // for IVOP addbits == 0  
 // for PVOP addbits == fcode - 1  
 // for BVOP addbits == max(fcode,bcode) - 1  
 // returns true or false  
644  int  int
645  check_resync_marker(Bitstream * bs, int addbits)  check_resync_marker(Bitstream * bs, int addbits)
646  {  {
# Line 842  Line 682 
682    
683          uint32_t index;          uint32_t index;
684    
685          index = MIN(BitstreamShowBits(bs, 9), 256);          index = CLIP(BitstreamShowBits(bs, 9), 256);
686    
687          BitstreamSkip(bs, mcbpc_inter_table[index].len);          BitstreamSkip(bs, mcbpc_inter_table[index].len);
688    
# Line 868  Line 708 
708    
709  }  }
710    
711  static __inline int  int
712  get_mv_data(Bitstream * bs)  get_mv_data(Bitstream * bs)
713  {  {
714    
# Line 977  Line 817 
817    
818  }  }
819    
820    /*****************************************************************************
821     * Local inlined function to "decode" written vlc codes
822     ****************************************************************************/
823    
824  static __inline int  static __inline int
825  get_coeff(Bitstream * bs,  get_coeff(Bitstream * bs,
826                    int *run,                    int *run,
# Line 1058  Line 902 
902          return 0;          return 0;
903  }  }
904    
905    /*****************************************************************************
906     * MB reading functions
907     ****************************************************************************/
908    
909  void  void
910  get_intra_block(Bitstream * bs,  get_intra_block(Bitstream * bs,
911                                  int16_t * block,                                  int16_t * block,
# Line 1066  Line 914 
914  {  {
915    
916          const uint16_t *scan = scan_tables[direction];          const uint16_t *scan = scan_tables[direction];
917          int level, run, last;          int level;
918            int run;
919            int last;
920    
921          do {          do {
922                  level = get_coeff(bs, &run, &last, 1, 0);                  level = get_coeff(bs, &run, &last, 1, 0);
923                  if (run == -1) {                  if (run == -1) {
924                          DPRINTF(DPRINTF_ERROR,"fatal: invalid run");                          DPRINTF(DPRINTF_DEBUG, "fatal: invalid run");
925                          break;                          break;
926                  }                  }
927                  coeff += run;                  coeff += run;
928                  block[scan[coeff]] = level;                  block[scan[coeff]] = level;
929    
930                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level);                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level);
931                  //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)); */
932    
933                  if (level < -2047 || level > 2047) {                  if (level < -2047 || level > 2047) {
934                          DPRINTF(DPRINTF_ERROR,"warning: intra_overflow %i", level);                          DPRINTF(DPRINTF_DEBUG, "warning: intra_overflow: %d", level);
935                  }                  }
936                  coeff++;                  coeff++;
937          } while (!last);          } while (!last);
# Line 1090  Line 940 
940    
941  void  void
942  get_inter_block(Bitstream * bs,  get_inter_block(Bitstream * bs,
943                                  int16_t * block,                                  int16_t * block)
                                 int direction)  
944  {  {
945    
946          const uint16_t *scan = scan_tables[direction];          const uint16_t *scan = scan_tables[0];
947          int p;          int p;
948          int level;          int level;
949          int run;          int run;
# Line 1112  Line 961 
961                  block[scan[p]] = level;                  block[scan[p]] = level;
962    
963                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level);                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level);
                 // DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[p], level, BitstreamShowBits(bs, 32));  
964    
965                  if (level < -2047 || level > 2047) {                  if (level < -2047 || level > 2047) {
966                          DPRINTF(DPRINTF_ERROR,"warning: inter overflow %i", level);                          DPRINTF(DPRINTF_DEBUG, "warning: inter_overflow: %d", level);
967                  }                  }
968                  p++;                  p++;
969          } while (!last);          } while (!last);

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

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