[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.8, Sat Jan 4 04:23:53 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    
57  #include <stdlib.h>  #include <stdlib.h>
58  #include "../portab.h"  #include "../portab.h"
# Line 62  Line 66 
66  #define ABS(X) (((X)>0)?(X):-(X))  #define ABS(X) (((X)>0)?(X):-(X))
67  #define CLIP(X,A) (X > A) ? (A) : (X)  #define CLIP(X,A) (X > A) ? (A) : (X)
68    
69  VLC intra_table[4*2048*64];  /*#define _BIGLUT_*/
 VLC inter_table[4*2048*64];  
70    
71  VLC DCT3Dintra[4096];  #ifdef _BIGLUT_
72  VLC DCT3Dinter[4096];  #define LEVELOFFSET 2048
73    #else
74    #define LEVELOFFSET 32
75    #endif
76    
77    /*****************************************************************************
78     * Local data
79     ****************************************************************************/
80    
81    static REVERSE_EVENT DCT3D[2][4096];
82    
83    #ifdef _BIGLUT_
84    static VLC coeff_VLC[2][2][4096][64];
85    VLC *intra_table, *inter_table;
86    #else
87    static VLC coeff_VLC[2][2][64][64];
88    #endif
89    
90    /*****************************************************************************
91     * Vector Length Coding Initialization
92     ****************************************************************************/
93    
94  /* not really MB related, but VLCs are only available here */  void
95  void bs_put_spritetrajectory(Bitstream * bs, const int val)  init_vlc_tables(void)
96  {  {
97          const int code = sprite_trajectory_code[val+16384].code;          uint32_t i, j, intra, last, run, offset;
98          const int len = sprite_trajectory_code[val+16384].len;          int32_t level;
99          const int code2 = sprite_trajectory_len[len].code;          VLC coeff_VLC_temp[2][2][64][64];
         const int len2 = sprite_trajectory_len[len].len;  
100    
101  //      printf("GMC=%d Code/Len  = %d / %d ",val, code,len);  #ifdef _BIGLUT_
102  //      printf("Code2 / Len2 = %d / %d \n",code2,len2);          intra_table = coeff_VLC[1];
103            inter_table = coeff_VLC[0];
104    #endif
105    
         BitstreamPutBits(bs, code2, len2);  
         if (len) BitstreamPutBits(bs, code, len);  
 }  
106    
107  int bs_get_spritetrajectory(Bitstream * bs)          for (intra = 0; intra < 2; intra++)
108  {                  for (i = 0; i < 4096; i++)
109          int i;                          DCT3D[intra][i].event.level = 0;
110          for (i = 0; i < 12; i++)  
111            for (intra = 0; intra < 2; intra++)
112                    for (last = 0; last < 2; last++)
113          {          {
114                  if (BitstreamShowBits(bs, sprite_trajectory_len[i].len) == sprite_trajectory_len[i].code)                          for (run = 0; run < 63 + last; run++)
115                                    for (level = 0; level < 32 << intra; level++)
116                  {                  {
117                          BitstreamSkip(bs, sprite_trajectory_len[i].len);  #ifdef _BIGLUT_
118                          return i;                                          offset = LEVELOFFSET;
119                  }  #else
120                                            offset = !intra * LEVELOFFSET;
121    #endif
122                                            coeff_VLC_temp[intra][last][level][run].len             = 128;
123                                            coeff_VLC[intra][last][level + offset][run].len = 128;
124          }          }
         return -1;  
125  }  }
126    
127  void          for (intra = 0; intra < 2; intra++)
128  init_vlc_tables(void)                  for (i = 0; i < 102; i++)
129  {  {
130    #ifdef _BIGLUT_
131          int32_t k, l, i, intra, last;                          offset = LEVELOFFSET;
132          VLC *vlc[2];  #else
133          VLC **coeff_ptr;                          offset = !intra * LEVELOFFSET;
134          VLC *vlc1, *vlc2;  #endif
135                            for (j = 0; j < 1 << (12 - coeff_tab[intra][i].vlc.len); j++)
136          vlc1 = DCT3Dintra;                          {
137          vlc2 = DCT3Dinter;                                  DCT3D[intra][(coeff_tab[intra][i].vlc.code << (12 - coeff_tab[intra][i].vlc.len)) | j].len       = coeff_tab[intra][i].vlc.len;
138                                    DCT3D[intra][(coeff_tab[intra][i].vlc.code << (12 - coeff_tab[intra][i].vlc.len)) | j].event = coeff_tab[intra][i].event;
         vlc[0] = intra_table;  
         vlc[1] = inter_table;  
   
         // generate encoding vlc lookup tables  
         // the lookup table idea is taken from the excellent fame project by Vivien Chapellier  
         for (i = 0; i < 4; i++) {  
                 intra = i % 2;  
                 last = i / 2;  
   
                 coeff_ptr = coeff_vlc[last + 2 * intra];  
   
                 for (k = -2047; k < 2048; k++) {        // level  
                         int8_t *max_level_ptr = max_level[last + 2 * intra];  
                         int8_t *max_run_ptr = max_run[last + 2 * intra];  
   
                         for (l = 0; l < 64; l++) {      // run  
                                 int32_t level = k;  
                                 ptr_t run = l;  
   
                                 if ((abs(level) <= max_level_ptr[run]) && (run <= (uint32_t) max_run_ptr[abs(level)])) {        // level < max_level and run < max_run  
   
                                         vlc[intra]->code = 0;  
                                         vlc[intra]->len = 0;  
                                         goto loop_end;  
                                 } else {  
                                         if (level > 0)  // correct level  
                                                 level -= max_level_ptr[run];  
                                         else  
                                                 level += max_level_ptr[run];  
   
                                         if ((abs(level) <= max_level_ptr[run]) &&  
                                                 (run <= (uint32_t) max_run_ptr[abs(level)])) {  
   
                                                 vlc[intra]->code = 0x06;  
                                                 vlc[intra]->len = 8;  
                                                 goto loop_end;  
139                                          }                                          }
140    
141                                          if (level > 0)  // still here?                          coeff_VLC_temp[intra][coeff_tab[intra][i].event.last][coeff_tab[intra][i].event.level][coeff_tab[intra][i].event.run].code
142                                                  level += max_level_ptr[run];    // restore level                                  = coeff_tab[intra][i].vlc.code << 1;
143                                          else                          coeff_VLC_temp[intra][coeff_tab[intra][i].event.last][coeff_tab[intra][i].event.level][coeff_tab[intra][i].event.run].len
144                                                  level -= max_level_ptr[run];                                  = coeff_tab[intra][i].vlc.len + 1;
   
                                         run -= max_run_ptr[abs(level)] + 1;     // and change run  
145    
146                                          if ((abs(level) <= max_level_ptr[run]) &&                          coeff_VLC[intra][coeff_tab[intra][i].event.last][coeff_tab[intra][i].event.level + offset][coeff_tab[intra][i].event.run].code
147                                                  (run <= (uint32_t) max_run_ptr[abs(level)])) {                                  = 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
149                                                  vlc[intra]->code = 0x0e;                                  = coeff_tab[intra][i].vlc.len + 1;
150                                                  vlc[intra]->len = 9;  #ifndef _BIGLUT_
151                                                  goto loop_end;                          if (!intra)
152                                          }  #endif
153                                          run += max_run_ptr[abs(level)] + 1;                          {
154                                    coeff_VLC[intra][coeff_tab[intra][i].event.last][offset - coeff_tab[intra][i].event.level][coeff_tab[intra][i].event.run].code
155                                            = (coeff_tab[intra][i].vlc.code << 1) | 1;
156                                    coeff_VLC[intra][coeff_tab[intra][i].event.last][offset - coeff_tab[intra][i].event.level][coeff_tab[intra][i].event.run].len
157                                            = coeff_tab[intra][i].vlc.len + 1;
158                                  }                                  }
   
                                 vlc[intra]->code =  
                                         (uint32_t) ((l << 14) | (0x1e + last) << 20) | (1 << 13) |  
                                         ((k & 0xfff) << 1) | 1;  
   
                                 vlc[intra]->len = 30;  
                                 vlc[intra]++;  
                                 continue;  
   
                           loop_end:  
                                 if (level != 0) {  
                                         vlc[intra]->code =  
                                                 (vlc[intra]->  
                                                  code << (coeff_ptr[run][abs(level) - 1].len +  
                                                                   1)) | (coeff_ptr[run][abs(level) -  
                                                                                                                 1].code << 1);  
                                         vlc[intra]->len =  
                                                 (coeff_ptr[run][abs(level) - 1].len + 1) +  
                                                 vlc[intra]->len;  
   
                                         if (level < 0)  
                                                 vlc[intra]->code += 1;  
159                                  }                                  }
160    
161                                  vlc[intra]++;          for (intra = 0; intra < 2; intra++)
162                          }                  for (last = 0; last < 2; last++)
163                            for (run = 0; run < 63 + last; run++)
164                            {
165                                    for (level = 1; level < 32 << intra; level++)
166                                    {
167    #ifdef _BIGLUT_
168                                            offset = LEVELOFFSET;
169    #else
170                                            offset = !intra * LEVELOFFSET;
171    #endif
172                                            if ((max_level[intra][last][run]) && (level > max_level[intra][last][run]))
173                                                    if (coeff_VLC_temp[intra][last][level - max_level[intra][last][run]][run].len != 128
174                                                            && coeff_VLC[intra][last][level + offset][run].len == 128)
175                                                    {
176                                                            coeff_VLC[intra][last][level + offset][run].code
177                                                                    = (ESCAPE1 << coeff_VLC_temp[intra][last][level - max_level[intra][last][run]][run].len)
178                                                                    |  coeff_VLC_temp[intra][last][level - max_level[intra][last][run]][run].code;
179                                                            coeff_VLC[intra][last][level + offset][run].len
180                                                                    = coeff_VLC_temp[intra][last][level - max_level[intra][last][run]][run].len + 7 + 1;
181    #ifndef _BIGLUT_
182                                                            if (!intra)
183    #endif
184                                                            {
185                                                                    coeff_VLC[intra][last][offset - level][run].code
186                                                                            = (ESCAPE1 << coeff_VLC_temp[intra][last][level - max_level[intra][last][run]][run].len)
187                                                                            |  coeff_VLC_temp[intra][last][level - max_level[intra][last][run]][run].code | 1;
188                                                                    coeff_VLC[intra][last][offset - level][run].len
189                                                                            = 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
200                                                                    = (ESCAPE2 << coeff_VLC_temp[intra][last][level][run - 1 - max_run[intra][last][level]].len)
201                                                                    |  coeff_VLC_temp[intra][last][level][run - 1 - max_run[intra][last][level]].code;
202                                                            coeff_VLC[intra][last][level + offset][run].len
203                                                                    = coeff_VLC_temp[intra][last][level][run - 1 - max_run[intra][last][level]].len + 7 + 2;
204    #ifndef _BIGLUT_
205                                                            if (!intra)
206    #endif
207                                                            {
208                                                                    coeff_VLC[intra][last][offset - level][run].code
209                                                                            = (ESCAPE2 << coeff_VLC_temp[intra][last][level][run - 1 - max_run[intra][last][level]].len)
210                                                                            |  coeff_VLC_temp[intra][last][level][run - 1 - max_run[intra][last][level]].code | 1;
211                                                                    coeff_VLC[intra][last][offset - level][run].len
212                                                                            = 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          for (i = 0; i < 4096; i++) {                                                          coeff_VLC[intra][last][offset - level][run].code
225                  if (i >= 512) {                                                                  = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-level & 0xfff) << 1) | 1;
226                          *vlc1 = DCT3Dtab3[(i >> 5) - 16];                                                          coeff_VLC[intra][last][offset - level][run].len = 30;
                         *vlc2 = DCT3Dtab0[(i >> 5) - 16];  
                 } else if (i >= 128) {  
                         *vlc1 = DCT3Dtab4[(i >> 2) - 32];  
                         *vlc2 = DCT3Dtab1[(i >> 2) - 32];  
                 } else if (i >= 8) {  
                         *vlc1 = DCT3Dtab5[i - 8];  
                         *vlc2 = DCT3Dtab2[i - 8];  
                 } else {  
                         *vlc1 = ERRtab[i];  
                         *vlc2 = ERRtab[i];  
227                  }                  }
   
                 vlc1++;  
                 vlc2++;  
228          }          }
229          DCT3D[0] = DCT3Dinter;  #ifdef _BIGLUT_
230          DCT3D[1] = DCT3Dintra;                                  for (level = 32 << intra; level < 2048; level++)
   
   
 /* 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++)  
231          {          {
232                  int limit = (1<<k);                                          coeff_VLC[intra][last][level + offset][run].code
233                                                    = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((level & 0xfff) << 1) | 1;
234                                            coeff_VLC[intra][last][level + offset][run].len = 30;
235    
236                  for (i=-(2*limit-1); i<= -limit; i++)                                          coeff_VLC[intra][last][offset - level][run].code
237                  {                                                  = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-level & 0xfff) << 1) | 1;
238                          sprite_trajectory_code[i+16384].code = (2*limit-1)+i;                                          coeff_VLC[intra][last][offset - level][run].len = 30;
                         sprite_trajectory_code[i+16384].len = k+1;  
239                  }                  }
240    #else
241                  for (i=limit; i<= 2*limit-1; i++)                                  if (!intra)
242                  {                  {
243                          sprite_trajectory_code[i+16384].code = i;                                          coeff_VLC[intra][last][0][run].code
244                          sprite_trajectory_code[i+16384].len = k+1;                                                  = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-32 & 0xfff) << 1) | 1;
245                                            coeff_VLC[intra][last][0][run].len = 30;
246                  }                  }
247    #endif
248          }          }
249  }  }
250    
251    /*****************************************************************************
252     * Local inlined functions for MB coding
253     ****************************************************************************/
254    
255  static __inline void  static __inline void
256  CodeVector(Bitstream * bs,  CodeVector(Bitstream * bs,
257                     int32_t value,                     int32_t value,
# Line 292  Line 307 
307    
308  }  }
309    
310    #ifdef __BIGLUT_
311    
312  static __inline void  static __inline void
313  CodeCoeff(Bitstream * bs,  CodeCoeff(Bitstream * bs,
# Line 312  Line 328 
328                  j++;                  j++;
329    
330          do {          do {
331                  vlc = table + 64 * 2047 + (v << 6) + j - last;                  vlc = table + 64 * 2048 + (v << 6) + j - last;
332                  last = ++j;                  last = ++j;
333    
334                  // count zeroes                  /* count zeroes */
335                  while (j < 64 && (v = qcoeff[zigzag[j]]) == 0)                  while (j < 64 && (v = qcoeff[zigzag[j]]) == 0)
336                          j++;                          j++;
337    
338                  // write code                  /* write code */
339                  if (j != 64) {                  if (j != 64) {
340                          BitstreamPutBits(bs, vlc->code, vlc->len);                          BitstreamPutBits(bs, vlc->code, vlc->len);
341                  } else {                  } else {
342                          vlc += 64 * 4095;                          vlc += 64 * 4096;
343                          BitstreamPutBits(bs, vlc->code, vlc->len);                          BitstreamPutBits(bs, vlc->code, vlc->len);
344                          break;                          break;
345                  }                  }
# Line 331  Line 347 
347    
348  }  }
349    
350    #else
351    
352    static __inline void
353    CodeCoeffInter(Bitstream * bs,
354                      const int16_t qcoeff[64],
355                      const uint16_t * zigzag)
356    {
357            uint32_t i, run, prev_run, code, len;
358            int32_t level, prev_level, level_shifted;
359    
360            i       = 0;
361            run = 0;
362    
363            while (!(level = qcoeff[zigzag[i++]]))
364                    run++;
365    
366            prev_level = level;
367            prev_run   = run;
368            run = 0;
369    
370            while (i < 64)
371            {
372                    if ((level = qcoeff[zigzag[i++]]) != 0)
373                    {
374                            level_shifted = prev_level + 32;
375                            if (!(level_shifted & -64))
376                            {
377                                    code = coeff_VLC[0][0][level_shifted][prev_run].code;
378                                    len      = coeff_VLC[0][0][level_shifted][prev_run].len;
379                            }
380                            else
381                            {
382                                    code = (ESCAPE3 << 21) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1;
383                                    len  = 30;
384                            }
385                            BitstreamPutBits(bs, code, len);
386                            prev_level = level;
387                            prev_run   = run;
388                            run = 0;
389                    }
390                    else
391                            run++;
392            }
393    
394            level_shifted = prev_level + 32;
395            if (!(level_shifted & -64))
396            {
397                    code = coeff_VLC[0][1][level_shifted][prev_run].code;
398                    len      = coeff_VLC[0][1][level_shifted][prev_run].len;
399            }
400            else
401            {
402                    code = (ESCAPE3 << 21) | (1 << 20) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1;
403                    len  = 30;
404            }
405            BitstreamPutBits(bs, code, len);
406    }
407    
408  static __inline void  static __inline void
409  CodeBlockIntra(const FRAMEINFO * const frame,  CodeCoeffIntra(Bitstream * bs,
410                      const int16_t qcoeff[64],
411                      const uint16_t * zigzag)
412    {
413            uint32_t i, abs_level, run, prev_run, code, len;
414            int32_t level, prev_level;
415    
416            i       = 1;
417            run = 0;
418    
419            while (!(level = qcoeff[zigzag[i++]]))
420                    run++;
421    
422            prev_level = level;
423            prev_run   = run;
424            run = 0;
425    
426            while (i < 64)
427            {
428                    if ((level = qcoeff[zigzag[i++]]) != 0)
429                    {
430                            abs_level = ABS(prev_level);
431                            abs_level = abs_level < 64 ? abs_level : 0;
432                            code      = coeff_VLC[1][0][abs_level][prev_run].code;
433                            len               = coeff_VLC[1][0][abs_level][prev_run].len;
434                            if (len != 128)
435                                    code |= (prev_level < 0);
436                            else
437                            {
438                            code = (ESCAPE3 << 21) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1;
439                                    len  = 30;
440                            }
441                            BitstreamPutBits(bs, code, len);
442                            prev_level = level;
443                            prev_run   = run;
444                            run = 0;
445                    }
446                    else
447                            run++;
448            }
449    
450            abs_level = ABS(prev_level);
451            abs_level = abs_level < 64 ? abs_level : 0;
452            code      = coeff_VLC[1][1][abs_level][prev_run].code;
453            len               = coeff_VLC[1][1][abs_level][prev_run].len;
454            if (len != 128)
455                    code |= (prev_level < 0);
456            else
457            {
458                    code = (ESCAPE3 << 21) | (1 << 20) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1;
459                    len  = 30;
460            }
461            BitstreamPutBits(bs, code, len);
462    }
463    
464    #endif
465    
466    /*****************************************************************************
467     * 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 344  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 355  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 382  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                          CodeCoeff(bs, &qcoeff[i * 64], intra_table, scan_table, 1);  #ifdef _BIGLUT_
523                            CodeCoeff(bs, &qcoeff[i * 64], intra_table,
524                                              scan_tables[pMB->acpred_directions[i]], 1);
525    #else
526                            CodeCoeffIntra(bs, &qcoeff[i * 64], scan_tables[pMB->acpred_directions[i]]);
527    #endif
528                          bits = BitstreamPos(bs) - bits;                          bits = BitstreamPos(bs) - bits;
529                          pStat->iTextBits += bits;                          pStat->iTextBits += bits;
530                  }                  }
# Line 399  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 408  Line 543 
543    
544          int32_t i;          int32_t i;
545          uint32_t bits, mcbpc, cbpy;          uint32_t bits, mcbpc, cbpy;
         int mcsel=0;  
546    
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 */
         {  
                 if (frame->quarterpel) {  
                         if ( (pMB->qmvs[0].x == frame->GMC_MV.x) && (pMB->qmvs[0].y == frame->GMC_MV.y) )  
                                 mcsel=1;  
                 } else {  
                         if ( (pMB->mvs[0].x == frame->GMC_MV.x) && (pMB->mvs[0].y == frame->GMC_MV.y) )  
                                 mcsel=1;  
                 }  
                 BitstreamPutBit(bs, 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 (mcsel==0)  
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 464  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);
593                                  frame->global_flags & XVID_ALTERNATESCAN ?  #else
594                                  scan_tables[2] : scan_tables[0];                          CodeCoeffInter(bs, &qcoeff[i * 64], scan_tables[0]);
595    #endif
                         CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_table, 0);  
                 }  
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 497  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  }  }
 */  
632    
633  /***************************************************************  /*****************************************************************************
634   * bframe encoding start   * decoding stuff starts here
635   ***************************************************************/   ****************************************************************************/
636    
637  /*  /*
638          mbtype   * For IVOP addbits == 0
639          0       1b              direct(h263)            mvdb   * For PVOP addbits == fcode - 1
640          1       01b             interpolate mc+q        dbquant, mvdf, mvdb   * For BVOP addbits == max(fcode,bcode) - 1
641          2       001b    backward mc+q           dbquant, mvdb   * returns true or false
         3       0001b   forward mc+q            dbquant, mvdf  
642  */  */
643    
 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;  
         }  
 }  
   
 /*  
         dbquant  
         -2      10b  
         0       0b  
         +2      11b  
 */  
   
 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))) {  
                         CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[direction], 0);  
                 }  
         }  
 }  
   
   
   
 /***************************************************************  
  * 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 709  Line 708 
708    
709  }  }
710    
711  static __inline int  int
712  get_mv_data(Bitstream * bs)  get_mv_data(Bitstream * bs)
713  {  {
714    
# Line 818  Line 817 
817    
818  }  }
819    
820    /*****************************************************************************
821     * Local inlined function to "decode" written vlc codes
822     ****************************************************************************/
823    
824    static __inline int
825    get_coeff(Bitstream * bs,
826                      int *run,
827                      int *last,
828                      int intra,
829                      int short_video_header)
830    {
831    
832            uint32_t mode;
833            int32_t level;
834            REVERSE_EVENT *reverse_event;
835    
836            if (short_video_header)         /* inter-VLCs will be used for both intra and inter blocks */
837                    intra = 0;
838    
839            if (BitstreamShowBits(bs, 7) != ESCAPE) {
840                    reverse_event = &DCT3D[intra][BitstreamShowBits(bs, 12)];
841    
842                    if ((level = reverse_event->event.level) == 0)
843                            goto error;
844    
845                    *last = reverse_event->event.last;
846                    *run  = reverse_event->event.run;
847    
848                    BitstreamSkip(bs, reverse_event->len);
849    
850                    return BitstreamGetBits(bs, 1) ? -level : level;
851            }
852    
853            BitstreamSkip(bs, 7);
854    
855            if (short_video_header) {
856                    /* escape mode 4 - H.263 type, only used if short_video_header = 1  */
857                    *last = BitstreamGetBit(bs);
858                    *run = BitstreamGetBits(bs, 6);
859                    level = BitstreamGetBits(bs, 8);
860    
861                    if (level == 0 || level == 128)
862                            DPRINTF(DPRINTF_ERROR, "Illegal LEVEL for ESCAPE mode 4: %d", level);
863    
864                    return (level << 24) >> 24;
865            }
866    
867            mode = BitstreamShowBits(bs, 2);
868    
869            if (mode < 3) {
870                    BitstreamSkip(bs, (mode == 2) ? 2 : 1);
871    
872                    reverse_event = &DCT3D[intra][BitstreamShowBits(bs, 12)];
873    
874                    if ((level = reverse_event->event.level) == 0)
875                            goto error;
876    
877                    *last = reverse_event->event.last;
878                    *run  = reverse_event->event.run;
879    
880                    BitstreamSkip(bs, reverse_event->len);
881    
882                    if (mode < 2)                   /* first escape mode, level is offset */
883                            level += max_level[intra][*last][*run];
884                    else                                    /* second escape mode, run is offset */
885                            *run += max_run[intra][*last][level] + 1;
886    
887                    return BitstreamGetBits(bs, 1) ? -level : level;
888            }
889    
890            /* third escape mode - fixed length codes */
891            BitstreamSkip(bs, 2);
892            *last = BitstreamGetBits(bs, 1);
893            *run = BitstreamGetBits(bs, 6);
894            BitstreamSkip(bs, 1);           /* marker */
895            level = BitstreamGetBits(bs, 12);
896            BitstreamSkip(bs, 1);           /* marker */
897    
898            return (level << 20) >> 20;
899    
900      error:
901            *run = VLC_ERROR;
902            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 826  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 850  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 872  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.8  
changed lines
  Added in v.1.37

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