[svn] / trunk / xvidcore / src / bitstream / mbcoding.c Repository:
ViewVC logotype

Diff of /trunk/xvidcore/src/bitstream/mbcoding.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 28, Fri Mar 15 09:20:03 2002 UTC revision 851, Sat Feb 15 15:22:19 2003 UTC
# Line 1  Line 1 
1     /******************************************************************************
2      *                                                                            *
3      *  This file is part of XviD, a free MPEG-4 video encoder/decoder            *
4      *                                                                            *
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    *
7      *  software module in hardware or software products are advised that its     *
8      *  use may infringe existing patents or copyrights, and any such use         *
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     *
11      *  companies, will have no liability for use of this software or             *
12      *  modifications or derivatives thereof.                                     *
13      *                                                                            *
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         *
16      *  the Free Software Foundation; either version 2 of the License, or         *
17      *  (at your option) any later version.                                       *
18      *                                                                            *
19      *  XviD is distributed in the hope that it will be useful, but               *
20      *  WITHOUT ANY WARRANTY; without even the implied warranty of                *
21      *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
22      *  GNU General Public License for more details.                              *
23      *                                                                            *
24      *  You should have received a copy of the GNU General Public License         *
25      *  along with this program; if not, write to the Free Software               *
26      *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA  *
27      *                                                                            *
28      ******************************************************************************/
29    
30     /******************************************************************************
31      *                                                                            *
32      *  mbcoding.c                                                                *
33      *                                                                            *
34      *  Copyright (C) 2002 - Michael Militzer <isibaar@xvid.org>                  *
35      *                                                                            *
36      *  For more information visit the XviD homepage: http://www.xvid.org         *
37      *                                                                            *
38      ******************************************************************************/
39    
40     /******************************************************************************
41      *                                                                                                                                                        *
42      *  Revision history:                                                         *
43      *                                                                            *
44      *  28.10.2002 GMC support - gruel                                                                                        *
45      *  28.06.2002 added check_resync_marker()                                    *
46      *  14.04.2002 bframe encoding                                                                                            *
47      *  08.03.2002 initial version; isibaar                                                           *
48      *                                                                                                                                                        *
49      ******************************************************************************/
50    
51    
52    #include <stdio.h>
53    #include <stdlib.h>
54  #include "../portab.h"  #include "../portab.h"
55    #include "../global.h"
56  #include "bitstream.h"  #include "bitstream.h"
57  #include "zigzag.h"  #include "zigzag.h"
58  #include "vlc_codes.h"  #include "vlc_codes.h"
59    #include "mbcoding.h"
60    
61  #include "../utils/mbfunctions.h"  #include "../utils/mbfunctions.h"
62    
63  #include <stdlib.h> /* malloc, free */  /* #define BIGLUT */
64    
65  #define ESCAPE 7167  #ifdef BIGLUT
66  #define ABS(X) (((X)>0)?(X):-(X))  #define LEVELOFFSET 2048
67  #define CLIP(X,A) (X > A) ? (A) : (X)  #else
68    #define LEVELOFFSET 32
69    #endif
70    
71  static VLC *DCT3D[2];  static REVERSE_EVENT DCT3D[2][4096];
72    
73  VLC *intra_table, *inter_table;  #ifdef BIGLUT
74  static short clip_table[4096];  static VLC coeff_VLC[2][2][4096][64];
75    VLC *intra_table;
76    static VLC *inter_table;
77    #else
78    static VLC coeff_VLC[2][2][64][64];
79    #endif
80    
81  void create_vlc_tables(void)  /* not really MB related, but VLCs are only available here */
82    void bs_put_spritetrajectory(Bitstream * bs, const int val)
83  {  {
84          int32_t k, l, i, intra, last;          const int code = sprite_trajectory_code[val+16384].code;
85          VLC *vlc[2];          const int len = sprite_trajectory_code[val+16384].len;
86          VLC **coeff_ptr;          const int code2 = sprite_trajectory_len[len].code;
87          VLC *vlc1, *vlc2;          const int len2 = sprite_trajectory_len[len].len;
   
         VLC *DCT3Dintra;  
         VLC *DCT3Dinter;  
   
         DCT3Dintra = (VLC *) malloc(sizeof(VLC) * 4096);  
         DCT3Dinter = (VLC *) malloc(sizeof(VLC) * 4096);  
   
         vlc1 = DCT3Dintra;  
         vlc2 = DCT3Dinter;  
88    
89          vlc[0] = intra_table = (VLC *) malloc(128 * 511 * sizeof(VLC));  //      printf("GMC=%d Code/Len  = %d / %d ",val, code,len);
90          vlc[1] = inter_table = (VLC *) malloc(128 * 511 * sizeof(VLC));  //      printf("Code2 / Len2 = %d / %d \n",code2,len2);
91    
92          // initialize the clipping table          BitstreamPutBits(bs, code2, len2);
93          for(i = -2048; i < 2048; i++) {          if (len) BitstreamPutBits(bs, code, len);
                 clip_table[i + 2048] = i;  
                 if(i < -255)  
                         clip_table[i + 2048] = -255;  
                 if(i > 255)  
                         clip_table[i + 2048] = 255;  
94          }          }
95    
96          // generate intra/inter vlc lookup table  int bs_get_spritetrajectory(Bitstream * bs)
97          for(i = 0; i < 4; i++) {  {
98                  intra = i % 2;          int i;
99                  last = i >> 1;          for (i = 0; i < 12; i++)
100            {
101                    if (BitstreamShowBits(bs, sprite_trajectory_len[i].len) == sprite_trajectory_len[i].code)
102                    {
103                            BitstreamSkip(bs, sprite_trajectory_len[i].len);
104                            return i;
105                    }
106            }
107            return -1;
108    }
109    
110                  coeff_ptr = coeff_vlc[last + (intra << 1)];  void
111    init_vlc_tables(void)
112    {
113            uint32_t i, j, k, intra, last, run,  run_esc, level, level_esc, escape, escape_len, offset;
114            int32_t l;
115    
116                  for(k = -255; k < 256; k++) { // level  #ifdef BIGLUT
117                          char *max_level_ptr = max_level[last + (intra << 1)];          intra_table = coeff_VLC[1];
118                          char *max_run_ptr = max_run[last + (intra << 1)];          inter_table = coeff_VLC[0];
119    #endif
120    
                         for(l = 0; l < 64; l++) { // run  
                                 int32_t level = k, run = l;  
121    
122                                  if(abs(level) <= max_level_ptr[run] && run <= max_run_ptr[abs(level)]) {          for (intra = 0; intra < 2; intra++)
123                    for (i = 0; i < 4096; i++)
124                            DCT3D[intra][i].event.level = 0;
125    
126                                          if(level > 0) {          for (intra = 0; intra < 2; intra++)
127                                                  vlc[intra]->code = (coeff_ptr[run][level - 1].code) << 1;                  for (last = 0; last < 2; last++)
128                                                  vlc[intra]->len = coeff_ptr[run][level - 1].len + 1;                  {
129                                          }                          for (run = 0; run < 63 + last; run++)
130                                          else if(level < 0) {                                  for (level = 0; level < (uint32_t)(32 << intra); level++)
131                                                  vlc[intra]->code = ((coeff_ptr[run][-level - 1].code) << 1) + 1;                                  {
132                                                  vlc[intra]->len = coeff_ptr[run][-level - 1].len + 1;  #ifdef BIGLUT
133                                            offset = LEVELOFFSET;
134    #else
135                                            offset = !intra * LEVELOFFSET;
136    #endif
137                                            coeff_VLC[intra][last][level + offset][run].len = 128;
138                                          }                                          }
                                         else {  
                                                 vlc[intra]->code = 0;  
                                                 vlc[intra]->len = 0;  
139                                          }                                          }
                                 } else {  
                                         if(level > 0)  
                                                 level -= max_level_ptr[run];  
                                         else  
                                                 level += max_level_ptr[run];  
140    
141                                          if(abs(level) <= max_level_ptr[run] &&          for (intra = 0; intra < 2; intra++)
142                                                  run <= max_run_ptr[abs(level)]) {                  for (i = 0; i < 102; i++)
143                    {
144    #ifdef BIGLUT
145                            offset = LEVELOFFSET;
146    #else
147                            offset = !intra * LEVELOFFSET;
148    #endif
149                            for (j = 0; j < (uint32_t)(1 << (12 - coeff_tab[intra][i].vlc.len)); j++)
150                            {
151                                    DCT3D[intra][(coeff_tab[intra][i].vlc.code << (12 - coeff_tab[intra][i].vlc.len)) | j].len       = coeff_tab[intra][i].vlc.len;
152                                    DCT3D[intra][(coeff_tab[intra][i].vlc.code << (12 - coeff_tab[intra][i].vlc.len)) | j].event = coeff_tab[intra][i].event;
153                            }
154    
155                                                  if(level > 0) {                          coeff_VLC[intra][coeff_tab[intra][i].event.last][coeff_tab[intra][i].event.level + offset][coeff_tab[intra][i].event.run].code
156                                                          vlc[intra]->code = (0x06 << (coeff_ptr[run][level - 1].len + 1)) |                                  = coeff_tab[intra][i].vlc.code << 1;
157                                                                  (coeff_ptr[run][level - 1].code << 1);                          coeff_VLC[intra][coeff_tab[intra][i].event.last][coeff_tab[intra][i].event.level + offset][coeff_tab[intra][i].event.run].len
158                                                          vlc[intra]->len = (coeff_ptr[run][level - 1].len + 1) + 8;                                  = coeff_tab[intra][i].vlc.len + 1;
159                                                  }  #ifndef BIGLUT
160                                                  else if(level < 0) {                          if (!intra)
161                                                          vlc[intra]->code = (0x06 << (coeff_ptr[run][-level - 1].len + 1)) |  #endif
162                                                                  ((coeff_ptr[run][-level - 1].code << 1) + 1);                          {
163                                                          vlc[intra]->len = (coeff_ptr[run][-level - 1].len + 1) + 8;                                  coeff_VLC[intra][coeff_tab[intra][i].event.last][offset - coeff_tab[intra][i].event.level][coeff_tab[intra][i].event.run].code
164                                                  }                                          = (coeff_tab[intra][i].vlc.code << 1) | 1;
165                                                  else {                                  coeff_VLC[intra][coeff_tab[intra][i].event.last][offset - coeff_tab[intra][i].event.level][coeff_tab[intra][i].event.run].len
166                                                          vlc[intra]->code = 0x06;                                          = coeff_tab[intra][i].vlc.len + 1;
                                                         vlc[intra]->len = 8;  
167                                                  }                                                  }
                                         } else {  
                                                 if(level > 0)  
                                                         level += max_level_ptr[run];  
                                                 else  
                                                         level -= max_level_ptr[run];  
                                                 DEBUG1("1) run:", run);  
                                                 run -= max_run_ptr[abs(level)] + 1;  
                                                 DEBUG1("2) run:", run);  
   
                                                 if(abs(level) <= max_level_ptr[run] &&  
                                                         run <= max_run_ptr[abs(level)]) {  
   
                                                         if(level > 0) {  
                                                                 vlc[intra]->code = (0x0e << (coeff_ptr[run][level - 1].len + 1)) |  
                                                                         (coeff_ptr[run][level - 1].code << 1);  
                                                                 vlc[intra]->len = (coeff_ptr[run][level - 1].len + 1) + 9;  
                                                         }  
                                                         else if(level < 0) {  
                                                                 vlc[intra]->code = (0x0e << (coeff_ptr[run][-level - 1].len + 1)) |  
                                                                         ((coeff_ptr[run][-level - 1].code << 1) + 1);  
                                                                 vlc[intra]->len = (coeff_ptr[run][-level - 1].len + 1) + 9;  
                                                         }  
                                                         else {  
                                                                 vlc[intra]->code = 0x0e;  
                                                                 vlc[intra]->len = 9;  
168                                                          }                                                          }
                                                 } else {  
                                                         if(level != 0)  
                                                                 run += max_run_ptr[abs(level)] + 1;  
                                                         else  
                                                                 run++;  
169    
170                                                          DEBUG1("3) run:", run);          for (intra = 0; intra < 2; intra++)
171                    for (last = 0; last < 2; last++)
172                            for (run = 0; run < 63 + last; run++)
173                            {
174                                    for (level = 1; level < (uint32_t)(32 << intra); level++)
175                                    {
176                                            if (level <= max_level[intra][last][run] && run <= max_run[intra][last][level])
177                                                continue;
178    
179                                                          vlc[intra]->code = (uint32_t) ((0x1e + last) << 20) |  #ifdef BIGLUT
180                                                                                  (l << 14) | (1 << 13) | ((k & 0xfff) << 1) | 1;                                          offset = LEVELOFFSET;
181    #else
182                                            offset = !intra * LEVELOFFSET;
183    #endif
184                        level_esc = level - max_level[intra][last][run];
185                                            run_esc = run - 1 - max_run[intra][last][level];
186                                            /*use this test to use shorter esc2 codes when possible
187                                            if (level_esc <= max_level[intra][last][run] && run <= max_run[intra][last][level_esc]
188                                                    && !(coeff_VLC[intra][last][level_esc + offset][run].len + 7 + 1
189                                                             > coeff_VLC[intra][last][level + offset][run_esc].code + 7 + 2))*/
190    
191                                                          vlc[intra]->len = 30;                                          if (level_esc <= max_level[intra][last][run] && run <= max_run[intra][last][level_esc])
192                                                  }                                          {
193                                                    escape     = ESCAPE1;
194                                                    escape_len = 7 + 1;
195                                                    run_esc    = run;
196                                          }                                          }
197                                            else
198                                            {
199                                                    if (run_esc <= max_run[intra][last][level] && level <= max_level[intra][last][run_esc])
200                                                    {
201                                                            escape     = ESCAPE2;
202                                                            escape_len = 7 + 2;
203                                                            level_esc  = level;
204                                  }                                  }
205                                  vlc[intra]++;                                                  else
206                                                    {
207    #ifndef BIGLUT
208                                                            if (!intra)
209    #endif
210                                                            {
211                                                                    coeff_VLC[intra][last][level + offset][run].code
212                                                                            = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((level & 0xfff) << 1) | 1;
213                                                                    coeff_VLC[intra][last][level + offset][run].len = 30;
214                                                                            coeff_VLC[intra][last][offset - level][run].code
215                                                                            = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-level & 0xfff) << 1) | 1;
216                                                                    coeff_VLC[intra][last][offset - level][run].len = 30;
217                          }                          }
218                                                            continue;
219                  }                  }
220          }          }
         intra_table += 64*255; // center vlc tables  
         inter_table += 64*255; // center vlc tables  
221    
222          for(i = 0; i < 4096; i++) {                                          coeff_VLC[intra][last][level + offset][run].code
223                  if(i >= 512) {                                                  = (escape << coeff_VLC[intra][last][level_esc + offset][run_esc].len)
224                          *vlc1 = DCT3Dtab3[(i >> 5) - 16];                                                  |  coeff_VLC[intra][last][level_esc + offset][run_esc].code;
225                          *vlc2 = DCT3Dtab0[(i >> 5) - 16];                                          coeff_VLC[intra][last][level + offset][run].len
226                  }                                                  = coeff_VLC[intra][last][level_esc + offset][run_esc].len + escape_len;
227                  else if(i >= 128) {  #ifndef BIGLUT
228                          *vlc1 = DCT3Dtab4[(i >> 2) - 32];                                          if (!intra)
229                          *vlc2 = DCT3Dtab1[(i >> 2) - 32];  #endif
230                  }                                          {
231                  else if(i >= 8) {                                                  coeff_VLC[intra][last][offset - level][run].code
232                          *vlc1 = DCT3Dtab5[i - 8];                                                          = (escape << coeff_VLC[intra][last][level_esc + offset][run_esc].len)
233                          *vlc2 = DCT3Dtab2[i - 8];                                                          |  coeff_VLC[intra][last][level_esc + offset][run_esc].code | 1;
234                                                    coeff_VLC[intra][last][offset - level][run].len
235                                                            = coeff_VLC[intra][last][level_esc + offset][run_esc].len + escape_len;
236                  }                  }
                 else {  
                         *vlc1 = ERRtab[i];  
                         *vlc2 = ERRtab[i];  
237                  }                  }
238    
239                  vlc1++;  #ifdef BIGLUT
240                  vlc2++;                                  for (level = 32 << intra; level < 2048; level++)
241          }                                  {
242          DCT3D[0] = DCT3Dinter;                                          coeff_VLC[intra][last][level + offset][run].code
243          DCT3D[1] = DCT3Dintra;                                                  = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((level & 0xfff) << 1) | 1;
244                                            coeff_VLC[intra][last][level + offset][run].len = 30;
245    
246                                            coeff_VLC[intra][last][offset - level][run].code
247                                                    = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-level & 0xfff) << 1) | 1;
248                                            coeff_VLC[intra][last][offset - level][run].len = 30;
249  }  }
250    #else
251                                    if (!intra)
252                                    {
253                                            coeff_VLC[intra][last][0][run].code
254                                                    = (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-32 & 0xfff) << 1) | 1;
255                                            coeff_VLC[intra][last][0][run].len = 30;
256                                    }
257    #endif
258                            }
259    /* init sprite_trajectory tables */
260    /* even if GMC is not specified (it might be used later...) */
261    
262  void destroy_vlc_tables(void) {          sprite_trajectory_code[0+16384].code = 0;
263            sprite_trajectory_code[0+16384].len = 0;
264          if(intra_table != NULL && inter_table != NULL) {          for (k=0;k<14;k++)
265                  intra_table -= 64*255; // uncenter vlc tables          {
266                  inter_table -= 64*255; // uncenter vlc tables                  int limit = (1<<k);
267    
268                  free(intra_table);                  for (l=-(2*limit-1); l <= -limit; l++)
269                  free(inter_table);                  {
270                            sprite_trajectory_code[l+16384].code = (2*limit-1)+l;
271                            sprite_trajectory_code[l+16384].len = k+1;
272          }          }
273    
274          if(DCT3D[0] != NULL && DCT3D[1] != NULL) {                  for (l=limit; l<= 2*limit-1; l++)
275                  free(DCT3D[0]);                  {
276                  free(DCT3D[1]);                          sprite_trajectory_code[l+16384].code = l;
277                            sprite_trajectory_code[l+16384].len = k+1;
278                    }
279          }          }
   
280  }  }
281    
282  static __inline void CodeVector(Bitstream *bs, int16_t value, int16_t f_code, Statistics *pStat)  static __inline void
283    CodeVector(Bitstream * bs,
284                       int32_t value,
285                       int32_t f_code,
286                       Statistics * pStat)
287  {  {
288    
289          const int scale_factor = 1 << (f_code - 1);          const int scale_factor = 1 << (f_code - 1);
290          const int cmp = scale_factor << 5;          const int cmp = scale_factor << 5;
291    
# Line 200  Line 298 
298      pStat->iMvSum += value * value;      pStat->iMvSum += value * value;
299      pStat->iMvCount++;      pStat->iMvCount++;
300    
301          if (value == 0)          if (value == 0) {
302                  BitstreamPutBits(bs, mb_motion_table[32].code, mb_motion_table[32].len);                  BitstreamPutBits(bs, mb_motion_table[32].code,
303      else {                                                   mb_motion_table[32].len);
304            } else {
305                  uint16_t length, code, mv_res, sign;                  uint16_t length, code, mv_res, sign;
306    
307                  length = 16 << f_code;                  length = 16 << f_code;
# Line 226  Line 325 
325                          code = -code;                          code = -code;
326    
327                  code += 32;                  code += 32;
328                  BitstreamPutBits(bs, mb_motion_table[code].code, mb_motion_table[code].len);                  BitstreamPutBits(bs, mb_motion_table[code].code,
329                                                     mb_motion_table[code].len);
330    
331                  if(f_code)                  if(f_code)
332                          BitstreamPutBits(bs, mv_res, f_code);                          BitstreamPutBits(bs, mv_res, f_code);
333    }    }
334    
335  }  }
336    
337    #ifdef BIGLUT
338    
339    static __inline void
340    CodeCoeff(Bitstream * bs,
341                      const int16_t qcoeff[64],
342                      VLC * table,
343                      const uint16_t * zigzag,
344                      uint16_t intra)
345    {
346    
 static __inline void CodeCoeff(Bitstream *bs, int16_t qcoeff[64], VLC *table,  
                                                            const uint16_t *zigzag, uint16_t intra) {  
347          uint32_t j, last;          uint32_t j, last;
348          short v;          short v;
349          VLC *vlc;          VLC *vlc;
350    
351          j = intra;          j = intra;
352          last = 1 + intra;          last = intra;
353    
354          while((v = qcoeff[zigzag[j++]]) == 0);          while (j < 64 && (v = qcoeff[zigzag[j]]) == 0)
355                    j++;
356    
357          do {          do {
358                  // count zeroes                  vlc = table + 64 * 2048 + (v << 6) + j - last;
359                  vlc = table + (clip_table[2048+v] << 6) + j - last;                  last = ++j;
                 last = j + 1;  
                 while(j < 64 && (v = qcoeff[zigzag[j++]]) == 0);  
360    
361                  // write code                  /* count zeroes */
362                    while (j < 64 && (v = qcoeff[zigzag[j]]) == 0)
363                            j++;
364    
365                    /* write code */
366                  if(j != 64) {                  if(j != 64) {
367                          BitstreamPutBits(bs, vlc->code, vlc->len);                          BitstreamPutBits(bs, vlc->code, vlc->len);
368                  } else {                  } else {
369                          vlc += 64*511;                          vlc += 64 * 4096;
370                          BitstreamPutBits(bs, vlc->code, vlc->len);                          BitstreamPutBits(bs, vlc->code, vlc->len);
371                          break;                          break;
372                  }                  }
373          } while(1);          } while(1);
374    
375    }
376    
377    
378    
379    /* returns the number of bits required to encode qcoeff */
380    int
381    CodeCoeff_CalcBits(const int16_t qcoeff[64],
382                      VLC * table,
383                      const uint16_t * zigzag,
384                      uint16_t intra)
385    {
386            int bits = 0;
387            uint32_t j, last;
388            short v;
389            VLC *vlc;
390    
391            j = intra;
392            last = intra;
393    
394            while (j < 64 && (v = qcoeff[zigzag[j]]) == 0)
395                    j++;
396    
397            if (j >= 64) return 0;  /* empty block */
398    
399            do {
400                    vlc = table + 64 * 2048 + (v << 6) + j - last;
401                    last = ++j;
402    
403                    /* count zeroes */
404                    while (j < 64 && (v = qcoeff[zigzag[j]]) == 0)
405                            j++;
406    
407                    /* write code */
408                    if (j != 64) {
409                            bits += vlc->len;
410                    } else {
411                            vlc += 64 * 4096;
412                            bits += vlc->len;
413                            break;
414                    }
415            } while (1);
416    
417            return bits;
418    }
419    
420    
421    #else
422    
423    static __inline void
424    CodeCoeffInter(Bitstream * bs,
425                      const int16_t qcoeff[64],
426                      const uint16_t * zigzag)
427    {
428            uint32_t i, run, prev_run, code, len;
429            int32_t level, prev_level, level_shifted;
430    
431            i       = 0;
432            run = 0;
433    
434            while (!(level = qcoeff[zigzag[i++]]))
435                    run++;
436    
437            prev_level = level;
438            prev_run   = run;
439            run = 0;
440    
441            while (i < 64)
442            {
443                    if ((level = qcoeff[zigzag[i++]]) != 0)
444                    {
445                            level_shifted = prev_level + 32;
446                            if (!(level_shifted & -64))
447                            {
448                                    code = coeff_VLC[0][0][level_shifted][prev_run].code;
449                                    len      = coeff_VLC[0][0][level_shifted][prev_run].len;
450                            }
451                            else
452                            {
453                                    code = (ESCAPE3 << 21) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1;
454                                    len  = 30;
455                            }
456                            BitstreamPutBits(bs, code, len);
457                            prev_level = level;
458                            prev_run   = run;
459                            run = 0;
460                    }
461                    else
462                            run++;
463  }  }
464    
465            level_shifted = prev_level + 32;
466            if (!(level_shifted & -64))
467            {
468                    code = coeff_VLC[0][1][level_shifted][prev_run].code;
469                    len      = coeff_VLC[0][1][level_shifted][prev_run].len;
470            }
471            else
472            {
473                    code = (ESCAPE3 << 21) | (1 << 20) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1;
474                    len  = 30;
475            }
476            BitstreamPutBits(bs, code, len);
477    }
478    
479  static void CodeBlockIntra(const MBParam * pParam, const MACROBLOCK *pMB,  static __inline void
480                                                                    int16_t qcoeff[][64], Bitstream * bs, Statistics * pStat)  CodeCoeffIntra(Bitstream * bs,
481                      const int16_t qcoeff[64],
482                      const uint16_t * zigzag)
483  {  {
484            uint32_t i, abs_level, run, prev_run, code, len;
485            int32_t level, prev_level;
486    
487            i       = 1;
488            run = 0;
489    
490            while (i<64 && !(level = qcoeff[zigzag[i++]]))
491                    run++;
492    
493            prev_level = level;
494            prev_run   = run;
495            run = 0;
496    
497            while (i < 64)
498            {
499                    if ((level = qcoeff[zigzag[i++]]) != 0)
500                    {
501                            abs_level = ABS(prev_level);
502                            abs_level = abs_level < 64 ? abs_level : 0;
503                            code      = coeff_VLC[1][0][abs_level][prev_run].code;
504                            len               = coeff_VLC[1][0][abs_level][prev_run].len;
505                            if (len != 128)
506                                    code |= (prev_level < 0);
507                            else
508                            {
509                            code = (ESCAPE3 << 21) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1;
510                                    len  = 30;
511                            }
512                            BitstreamPutBits(bs, code, len);
513                            prev_level = level;
514                            prev_run   = run;
515                            run = 0;
516                    }
517                    else
518                            run++;
519            }
520    
521            abs_level = ABS(prev_level);
522            abs_level = abs_level < 64 ? abs_level : 0;
523            code      = coeff_VLC[1][1][abs_level][prev_run].code;
524            len               = coeff_VLC[1][1][abs_level][prev_run].len;
525            if (len != 128)
526                    code |= (prev_level < 0);
527            else
528            {
529                    code = (ESCAPE3 << 21) | (1 << 20) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1;
530                    len  = 30;
531            }
532            BitstreamPutBits(bs, code, len);
533    }
534    
535    
536    
537    /* returns the number of bits required to encode qcoeff */
538    
539    int
540    CodeCoeffIntra_CalcBits(const int16_t qcoeff[64], const uint16_t * zigzag)
541    {
542            int bits = 0;
543            uint32_t i, abs_level, run, prev_run, len;
544            int32_t level, prev_level;
545    
546            i       = 1;
547            run = 0;
548    
549            while (i<64 && !(level = qcoeff[zigzag[i++]]))
550                    run++;
551    
552            if (i >= 64) return 0;  /* empty block */
553    
554            prev_level = level;
555            prev_run   = run;
556            run = 0;
557    
558            while (i < 64)
559            {
560                    if ((level = qcoeff[zigzag[i++]]) != 0)
561                    {
562                            abs_level = ABS(prev_level);
563                            abs_level = abs_level < 64 ? abs_level : 0;
564                            len               = coeff_VLC[1][0][abs_level][prev_run].len;
565                            bits      += len!=128 ? len : 30;
566    
567                            prev_level = level;
568                            prev_run   = run;
569                            run = 0;
570                    }
571                    else
572                            run++;
573            }
574    
575            abs_level = ABS(prev_level);
576            abs_level = abs_level < 64 ? abs_level : 0;
577            len               = coeff_VLC[1][1][abs_level][prev_run].len;
578            bits      += len!=128 ? len : 30;
579    
580            return bits;
581    }
582    
583    int
584    CodeCoeffInter_CalcBits(const int16_t qcoeff[64], const uint16_t * zigzag)
585    {
586            uint32_t i, run, prev_run, len;
587            int32_t level, prev_level, level_shifted;
588            int bits = 0;
589    
590            i       = 0;
591            run = 0;
592    
593            while (!(level = qcoeff[zigzag[i++]]))
594                    run++;
595    
596            prev_level = level;
597            prev_run   = run;
598            run = 0;
599    
600            while (i < 64) {
601                    if ((level = qcoeff[zigzag[i++]]) != 0) {
602                            level_shifted = prev_level + 32;
603                            if (!(level_shifted & -64))
604                                    len      = coeff_VLC[0][0][level_shifted][prev_run].len;
605                            else
606                                    len  = 30;
607    
608                            bits += len;
609                            prev_level = level;
610                            prev_run   = run;
611                            run = 0;
612                    }
613                    else
614                            run++;
615            }
616    
617            level_shifted = prev_level + 32;
618            if (!(level_shifted & -64))
619                    len      = coeff_VLC[0][1][level_shifted][prev_run].len;
620            else
621                    len  = 30;
622            bits += len;
623    
624            return bits;
625    }
626    
627    
628    #endif
629    
630    static __inline void
631    CodeBlockIntra(const FRAMEINFO * const frame,
632                               const MACROBLOCK * pMB,
633                               int16_t qcoeff[6 * 64],
634                               Bitstream * bs,
635                               Statistics * pStat)
636    {
637    
638          uint32_t i, mcbpc, cbpy, bits;          uint32_t i, mcbpc, cbpy, bits;
639    
640          cbpy = pMB->cbp >> 2;          cbpy = pMB->cbp >> 2;
641    
642      // write mcbpc      // write mcbpc
643          if(pParam->coding_type == I_VOP) {          if (frame->coding_type == I_VOP) {
644              mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2);              mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2);
645                  BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, mcbpc_intra_tab[mcbpc].len);                  BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code,
646          }                                                   mcbpc_intra_tab[mcbpc].len);
647          else {          } else {
648              mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);              mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);
649                  BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len);                  BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,
650                                                     mcbpc_inter_tab[mcbpc].len);
651          }          }
652    
653          // ac prediction flag          // ac prediction flag
# Line 293  Line 663 
663      if(pMB->mode == MODE_INTRA_Q)      if(pMB->mode == MODE_INTRA_Q)
664                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, pMB->dquant, 2);
665    
666            // write interlacing
667            if (frame->global_flags & XVID_INTERLACING) {
668                    BitstreamPutBit(bs, pMB->field_dct);
669            }
670          // code block coeffs          // code block coeffs
671          for(i = 0; i < 6; i++)          for (i = 0; i < 6; i++) {
         {  
672                  if(i < 4)                  if(i < 4)
673                          BitstreamPutBits(bs, dcy_tab[qcoeff[i][0] + 255].code,                          BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code,
674                                                           dcy_tab[qcoeff[i][0] + 255].len);                                                           dcy_tab[qcoeff[i * 64 + 0] + 255].len);
675                  else                  else
676                          BitstreamPutBits(bs, dcc_tab[qcoeff[i][0] + 255].code,                          BitstreamPutBits(bs, dcc_tab[qcoeff[i * 64 + 0] + 255].code,
677                                           dcc_tab[qcoeff[i][0] + 255].len);                                                           dcc_tab[qcoeff[i * 64 + 0] + 255].len);
678    
679                    if (pMB->cbp & (1 << (5 - i))) {
680                            const uint16_t *scan_table =
681                                    frame->global_flags & XVID_ALTERNATESCAN ?
682                                    scan_tables[2] : scan_tables[pMB->acpred_directions[i]];
683    
                 if(pMB->cbp & (1 << (5 - i)))  
                 {  
684                          bits = BitstreamPos(bs);                          bits = BitstreamPos(bs);
685    
686                          CodeCoeff(bs, qcoeff[i], intra_table, scan_tables[pMB->acpred_directions[i]], 1);  #ifdef BIGLUT
687                            CodeCoeff(bs, &qcoeff[i * 64], intra_table, scan_table, 1);
688    #else
689                            CodeCoeffIntra(bs, &qcoeff[i * 64], scan_table);
690    #endif
691    
692                          bits = BitstreamPos(bs) - bits;                          bits = BitstreamPos(bs) - bits;
693                          pStat->iTextBits += bits;                          pStat->iTextBits += bits;
694                  }                  }
695          }          }
696    
697  }  }
698    
699    
700  static void CodeBlockInter(const MBParam * pParam, const MACROBLOCK *pMB,  static void
701                                                                    int16_t qcoeff[][64], Bitstream * bs, Statistics * pStat)  CodeBlockInter(const FRAMEINFO * const frame,
702                               const MACROBLOCK * pMB,
703                               int16_t qcoeff[6 * 64],
704                               Bitstream * bs,
705                               Statistics * pStat)
706  {  {
707    
708          int32_t i;          int32_t i;
709          uint32_t bits, mcbpc, cbpy;          uint32_t bits, mcbpc, cbpy;
710    
# Line 326  Line 712 
712          cbpy = 15 - (pMB->cbp >> 2);          cbpy = 15 - (pMB->cbp >> 2);
713    
714          // write mcbpc          // write mcbpc
715      BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len);          BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,
716                                             mcbpc_inter_tab[mcbpc].len);
717    
718            if ( (frame->coding_type == S_VOP) && (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) )
719                    BitstreamPutBit(bs, pMB->mcsel);                // mcsel: '0'=local motion, '1'=GMC
720    
721          // write cbpy          // write cbpy
722          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);
# Line 335  Line 725 
725      if(pMB->mode == MODE_INTER_Q)      if(pMB->mode == MODE_INTER_Q)
726                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, pMB->dquant, 2);
727    
728          // code motion vector(s)          // interlacing
729          for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++)          if (frame->global_flags & XVID_INTERLACING) {
730          {                  if (pMB->cbp) {
731                  CodeVector(bs, pMB->pmvs[i].x, pParam->fixed_code, pStat);                          BitstreamPutBit(bs, pMB->field_dct);
732                  CodeVector(bs, pMB->pmvs[i].y, pParam->fixed_code, pStat);                          DPRINTF(DPRINTF_MB,"codep: field_dct: %i", pMB->field_dct);
733                    }
734    
735                    // if inter block, write field ME flag
736                    if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
737                            BitstreamPutBit(bs, pMB->field_pred);
738                            DPRINTF(DPRINTF_MB,"codep: field_pred: %i", pMB->field_pred);
739    
740                            // write field prediction references
741                            if (pMB->field_pred) {
742                                    BitstreamPutBit(bs, pMB->field_for_top);
743                                    BitstreamPutBit(bs, pMB->field_for_bot);
744                            }
745                    }
746            }
747            // code motion vector(s) if motion is local
748            if (!pMB->mcsel)
749                    for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) {
750                            CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat);
751                            CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat);
752          }          }
753    
754          bits = BitstreamPos(bs);          bits = BitstreamPos(bs);
# Line 347  Line 756 
756          // code block coeffs          // code block coeffs
757          for(i = 0; i < 6; i++)          for(i = 0; i < 6; i++)
758                  if(pMB->cbp & (1 << (5 - i)))                  if(pMB->cbp & (1 << (5 - i)))
759                          CodeCoeff(bs, qcoeff[i], inter_table, scan_tables[0], 0);                  {
760                            const uint16_t *scan_table =
761                                    frame->global_flags & XVID_ALTERNATESCAN ?
762                                    scan_tables[2] : scan_tables[0];
763    
764    #ifdef BIGLUT
765                            CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_table, 0);
766    #else
767                            CodeCoeffInter(bs, &qcoeff[i * 64], scan_table);
768    #endif
769                    }
770    
771          bits = BitstreamPos(bs) - bits;          bits = BitstreamPos(bs) - bits;
772          pStat->iTextBits += bits;          pStat->iTextBits += bits;
773  }  }
774    
775    
776  void MBCoding(const MBParam * pParam, MACROBLOCK *pMB,  void
777                int16_t qcoeff[][64],  MBCoding(const FRAMEINFO * const frame,
778                    Bitstream * bs, Statistics * pStat)                   MACROBLOCK * pMB,
779                     int16_t qcoeff[6 * 64],
780                     Bitstream * bs,
781                     Statistics * pStat)
782  {  {
783          int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q);          if (frame->coding_type != I_VOP)
784                            BitstreamPutBit(bs, 0); // not_coded
785    
786            if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)
787                    CodeBlockIntra(frame, pMB, qcoeff, bs, pStat);
788            else
789                    CodeBlockInter(frame, pMB, qcoeff, bs, pStat);
790    
791      if(pParam->coding_type == P_VOP) {  }
792                  if(pMB->cbp == 0 && pMB->mode == MODE_INTER &&  
793                          pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0)  /*
794    // moved to mbcoding.h so that in can be 'static __inline'
795    void
796    MBSkip(Bitstream * bs)
797                  {                  {
798                          BitstreamPutBit(bs, 1);         // not_coded          BitstreamPutBit(bs, 1); // not coded
799    }
800    */
801    
802    /***************************************************************
803     * bframe encoding start
804     ***************************************************************/
805    
806    /*
807            mbtype
808            0       1b              direct(h263)            mvdb
809            1       01b             interpolate mc+q        dbquant, mvdf, mvdb
810            2       001b    backward mc+q           dbquant, mvdb
811            3       0001b   forward mc+q            dbquant, mvdf
812    */
813    
814    static __inline void
815    put_bvop_mbtype(Bitstream * bs,
816                                    int value)
817    {
818            switch (value) {
819                    case MODE_FORWARD:
820                            BitstreamPutBit(bs, 0);
821                    case MODE_BACKWARD:
822                            BitstreamPutBit(bs, 0);
823                    case MODE_INTERPOLATE:
824                            BitstreamPutBit(bs, 0);
825                    case MODE_DIRECT:
826                            BitstreamPutBit(bs, 1);
827                    default:
828                            break;
829            }
830    }
831    
832    /*
833            dbquant
834            -2      10b
835            0       0b
836            +2      11b
837    */
838    
839    static __inline void
840    put_bvop_dbquant(Bitstream * bs,
841                                     int value)
842    {
843            switch (value) {
844            case 0:
845                    BitstreamPutBit(bs, 0);
846                          return;                          return;
847    
848            case -2:
849                    BitstreamPutBit(bs, 1);
850                    BitstreamPutBit(bs, 0);
851                    return;
852    
853            case 2:
854                    BitstreamPutBit(bs, 1);
855                    BitstreamPutBit(bs, 1);
856                    return;
857    
858            default:;                                       // invalid
859                  }                  }
                 else  
                         BitstreamPutBit(bs, 0);         // coded  
860          }          }
861    
862          if(intra)  
863                  CodeBlockIntra(pParam, pMB, qcoeff, bs, pStat);  
864          else  void
865                  CodeBlockInter(pParam, pMB, qcoeff, bs, pStat);  MBCodingBVOP(const MACROBLOCK * mb,
866                             const int16_t qcoeff[6 * 64],
867                             const int32_t fcode,
868                             const int32_t bcode,
869                             Bitstream * bs,
870                             Statistics * pStat,
871                             int direction)
872    {
873            int vcode = fcode;
874            unsigned int i;
875    
876    /*      ------------------------------------------------------------------
877                    when a block is skipped it is decoded DIRECT(0,0)
878                    hence is interpolated from forward & backward frames
879            ------------------------------------------------------------------ */
880    
881            if (mb->mode == MODE_DIRECT_NONE_MV) {
882                    BitstreamPutBit(bs, 1); // skipped
883                    return;
884            }
885    
886            BitstreamPutBit(bs, 0);         // not skipped
887    
888            if (mb->cbp == 0) {
889                    BitstreamPutBit(bs, 1); // cbp == 0
890            } else {
891                    BitstreamPutBit(bs, 0); // cbp == xxx
892            }
893    
894            put_bvop_mbtype(bs, mb->mode);
895    
896            if (mb->cbp) {
897                    BitstreamPutBits(bs, mb->cbp, 6);
898  }  }
899    
900            if (mb->mode != MODE_DIRECT && mb->cbp != 0) {
901                    put_bvop_dbquant(bs, 0);        // todo: mb->dquant = 0
902            }
903    
904            switch (mb->mode) {
905                    case MODE_INTERPOLATE:
906                            CodeVector(bs, mb->pmvs[1].x, vcode, pStat); //forward vector of interpolate mode
907                            CodeVector(bs, mb->pmvs[1].y, vcode, pStat);
908                    case MODE_BACKWARD:
909                            vcode = bcode;
910                    case MODE_FORWARD:
911                            CodeVector(bs, mb->pmvs[0].x, vcode, pStat);
912                            CodeVector(bs, mb->pmvs[0].y, vcode, pStat);
913                            break;
914                    case MODE_DIRECT:
915                            CodeVector(bs, mb->pmvs[3].x, 1, pStat);        // fcode is always 1 for delta vector
916                            CodeVector(bs, mb->pmvs[3].y, 1, pStat);        // prediction is always (0,0)
917                    default: break;
918            }
919    
920            for (i = 0; i < 6; i++) {
921                    if (mb->cbp & (1 << (5 - i))) {
922    #ifdef BIGLUT
923                            CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0);
924    #else
925                            CodeCoeffInter(bs, &qcoeff[i * 64], scan_tables[0]);
926    #endif
927                    }
928            }
929    }
930    
931    
932    
933  /***************************************************************  /***************************************************************
934   * decoding stuff starts here                                  *   * decoding stuff starts here                                  *
935   ***************************************************************/   ***************************************************************/
936    
937  int get_mcbpc_intra(Bitstream * bs)  
938    // for IVOP addbits == 0
939    // for PVOP addbits == fcode - 1
940    // for BVOP addbits == max(fcode,bcode) - 1
941    // returns true or false
942    int
943    check_resync_marker(Bitstream * bs, int addbits)
944    {
945            uint32_t nbits;
946            uint32_t code;
947            uint32_t nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER + addbits;
948    
949            nbits = BitstreamNumBitsToByteAlign(bs);
950            code = BitstreamShowBits(bs, nbits);
951    
952            if (code == (((uint32_t)1 << (nbits - 1)) - 1))
953  {  {
954          uint32_t index;                  return BitstreamShowBitsFromByteAlign(bs, nbitsresyncmarker) == RESYNC_MARKER;
955            }
956    
957          while((index = BitstreamShowBits(bs, 9)) == 1)          return 0;
958                  BitstreamSkip(bs, 9);  }
959    
960    
961    
962    int
963    get_mcbpc_intra(Bitstream * bs)
964    {
965    
966            uint32_t index;
967    
968            index = BitstreamShowBits(bs, 9);
969          index >>= 3;          index >>= 3;
970    
971          BitstreamSkip(bs, mcbpc_intra_table[index].len);          BitstreamSkip(bs, mcbpc_intra_table[index].len);
972    
973          return mcbpc_intra_table[index].code;          return mcbpc_intra_table[index].code;
974    
975  }  }
976    
977  int get_mcbpc_inter(Bitstream * bs)  int
978    get_mcbpc_inter(Bitstream * bs)
979  {  {
980    
981          uint32_t index;          uint32_t index;
982    
983          while((index = CLIP(BitstreamShowBits(bs, 9), 256)) == 1)          index = MIN(BitstreamShowBits(bs, 9), 256);
                 BitstreamSkip(bs, 9);  
984    
985      BitstreamSkip(bs,  mcbpc_inter_table[index].len);      BitstreamSkip(bs,  mcbpc_inter_table[index].len);
986    
987          return mcbpc_inter_table[index].code;          return mcbpc_inter_table[index].code;
988    
989  }  }
990    
991  int get_cbpy(Bitstream * bs, int intra)  int
992    get_cbpy(Bitstream * bs,
993                     int intra)
994  {  {
995    
996          int cbpy;          int cbpy;
997          uint32_t index = BitstreamShowBits(bs, 6);          uint32_t index = BitstreamShowBits(bs, 6);
998    
# Line 418  Line 1003 
1003                  cbpy = 15 - cbpy;                  cbpy = 15 - cbpy;
1004    
1005          return cbpy;          return cbpy;
1006    
1007  }  }
1008    
1009  int get_mv_data(Bitstream * bs)  static __inline int
1010    get_mv_data(Bitstream * bs)
1011  {  {
1012    
1013          uint32_t index;          uint32_t index;
1014    
1015          if(BitstreamGetBit(bs))          if(BitstreamGetBit(bs))
# Line 429  Line 1017 
1017    
1018          index = BitstreamShowBits(bs, 12);          index = BitstreamShowBits(bs, 12);
1019    
1020          if(index >= 512)          if (index >= 512) {
         {  
1021                  index = (index >> 8) - 2;                  index = (index >> 8) - 2;
1022                  BitstreamSkip(bs, TMNMVtab0[index].len);                  BitstreamSkip(bs, TMNMVtab0[index].len);
1023                  return TMNMVtab0[index].code;                  return TMNMVtab0[index].code;
1024          }          }
1025    
1026          if(index >= 128)          if (index >= 128) {
         {  
1027                  index = (index >> 2) - 32;                  index = (index >> 2) - 32;
1028                  BitstreamSkip(bs, TMNMVtab1[index].len);                  BitstreamSkip(bs, TMNMVtab1[index].len);
1029                  return TMNMVtab1[index].code;                  return TMNMVtab1[index].code;
# Line 447  Line 1033 
1033    
1034          BitstreamSkip(bs, TMNMVtab2[index].len);          BitstreamSkip(bs, TMNMVtab2[index].len);
1035          return TMNMVtab2[index].code;          return TMNMVtab2[index].code;
1036    
1037  }  }
1038    
1039  int get_mv(Bitstream * bs, int fcode)  int
1040    get_mv(Bitstream * bs,
1041               int fcode)
1042  {  {
1043    
1044          int data;          int data;
1045          int res;          int res;
1046          int mv;          int mv;
# Line 465  Line 1055 
1055          mv = ((ABS(data) - 1) * scale_fac) + res + 1;          mv = ((ABS(data) - 1) * scale_fac) + res + 1;
1056    
1057          return data < 0 ? -mv : mv;          return data < 0 ? -mv : mv;
1058    
1059  }  }
1060    
1061  int get_dc_dif(Bitstream * bs, uint32_t dc_size)  int
1062    get_dc_dif(Bitstream * bs,
1063                       uint32_t dc_size)
1064  {  {
1065    
1066          int code = BitstreamGetBits(bs, dc_size);          int code = BitstreamGetBits(bs, dc_size);
1067          int msb = code >> (dc_size - 1);          int msb = code >> (dc_size - 1);
1068    
# Line 476  Line 1070 
1070                  return (-1 * (code^((1 << dc_size) - 1)));                  return (-1 * (code^((1 << dc_size) - 1)));
1071    
1072          return code;          return code;
1073    
1074  }  }
1075    
1076  int get_dc_size_lum(Bitstream * bs)  int
1077    get_dc_size_lum(Bitstream * bs)
1078  {  {
1079    
1080          int code, i;          int code, i;
1081    
1082          code = BitstreamShowBits(bs, 11);          code = BitstreamShowBits(bs, 11);
1083    
1084          for(i = 11; i > 3; i--) {          for(i = 11; i > 3; i--) {
# Line 493  Line 1091 
1091    
1092          BitstreamSkip(bs, dc_lum_tab[code].len);          BitstreamSkip(bs, dc_lum_tab[code].len);
1093          return dc_lum_tab[code].code;          return dc_lum_tab[code].code;
1094    
1095  }  }
1096    
1097    
1098  int get_dc_size_chrom(Bitstream * bs)  int
1099    get_dc_size_chrom(Bitstream * bs)
1100  {  {
1101    
1102          uint32_t code, i;          uint32_t code, i;
1103    
1104          code = BitstreamShowBits(bs, 12);          code = BitstreamShowBits(bs, 12);
1105    
1106          for(i = 12; i > 2; i--) {          for(i = 12; i > 2; i--) {
# Line 510  Line 1112 
1112          }          }
1113    
1114          return 3 - BitstreamGetBits(bs, 2);          return 3 - BitstreamGetBits(bs, 2);
1115    
1116  }  }
1117    
1118  int get_coeff(Bitstream * bs, int *run, int *last, int intra, int short_video_header)  static __inline int
1119    get_coeff(Bitstream * bs,
1120                      int *run,
1121                      int *last,
1122                      int intra,
1123                      int short_video_header)
1124  {  {
1125    
1126      uint32_t mode;      uint32_t mode;
     const VLC *tab;  
1127          int32_t level;          int32_t level;
1128            REVERSE_EVENT *reverse_event;
1129    
1130          if(short_video_header) // inter-VLCs will be used for both intra and inter blocks          if (short_video_header)         /* inter-VLCs will be used for both intra and inter blocks */
1131                  intra = 0;                  intra = 0;
1132    
1133          tab = &DCT3D[intra][BitstreamShowBits(bs, 12)];          if (BitstreamShowBits(bs, 7) != ESCAPE) {
1134                    reverse_event = &DCT3D[intra][BitstreamShowBits(bs, 12)];
1135    
1136          if(tab->code == -1)                  if ((level = reverse_event->event.level) == 0)
1137                  goto error;                  goto error;
1138    
1139          BitstreamSkip(bs, tab->len);                  *last = reverse_event->event.last;
1140                    *run  = reverse_event->event.run;
1141    
1142          if(tab->code != ESCAPE) {                  BitstreamSkip(bs, reverse_event->len);
1143                  if(!intra)  
1144                  {                  return BitstreamGetBits(bs, 1) ? -level : level;
                         *run = (tab->code >> 4) & 255;  
                         level = tab->code & 15;  
                         *last = (tab->code >> 12) & 1;  
                 }  
             else  
                 {  
                         *run = (tab->code >> 8) & 255;  
                         level = tab->code & 255;  
                         *last = (tab->code >> 16) & 1;  
                 }  
                 return BitstreamGetBit(bs) ? -level : level;  
1145          }          }
1146    
1147          if(short_video_header)          BitstreamSkip(bs, 7);
1148          {  
1149                  // escape mode 4 - H.263 type, only used if short_video_header = 1          if (short_video_header) {
1150                    /* escape mode 4 - H.263 type, only used if short_video_header = 1  */
1151                  *last = BitstreamGetBit(bs);                  *last = BitstreamGetBit(bs);
1152                  *run = BitstreamGetBits(bs, 6);                  *run = BitstreamGetBits(bs, 6);
1153                  level = BitstreamGetBits(bs, 8);                  level = BitstreamGetBits(bs, 8);
1154    
1155                  if (level == 0 || level == 128)                  if (level == 0 || level == 128)
1156                          DEBUG1("Illegal LEVEL for ESCAPE mode 4:", level);                          DPRINTF(DPRINTF_ERROR, "Illegal LEVEL for ESCAPE mode 4: %d", level);
1157    
1158                  return (level >= 128 ? -(256 - level) : level);                  return (level << 24) >> 24;
1159          }          }
1160    
1161          mode = BitstreamShowBits(bs, 2);          mode = BitstreamShowBits(bs, 2);
# Line 562  Line 1163 
1163          if(mode < 3) {          if(mode < 3) {
1164                  BitstreamSkip(bs, (mode == 2) ? 2 : 1);                  BitstreamSkip(bs, (mode == 2) ? 2 : 1);
1165    
1166                  tab = &DCT3D[intra][BitstreamShowBits(bs, 12)];                  reverse_event = &DCT3D[intra][BitstreamShowBits(bs, 12)];
1167                  if (tab->code == -1)  
1168                    if ((level = reverse_event->event.level) == 0)
1169                          goto error;                          goto error;
1170    
1171                  BitstreamSkip(bs, tab->len);                  *last = reverse_event->event.last;
1172                    *run  = reverse_event->event.run;
1173    
1174                  if (!intra) {                  BitstreamSkip(bs, reverse_event->len);
                         *run = (tab->code >> 4) & 255;  
                         level = tab->code & 15;  
                         *last = (tab->code >> 12) & 1;  
                 }  
                 else  
                 {  
                         *run = (tab->code >> 8) & 255;  
                         level = tab->code & 255;  
                         *last = (tab->code >> 16) & 1;  
                 }  
1175    
1176                  if(mode < 2) // first escape mode, level is offset                  if (mode < 2)                   /* first escape mode, level is offset */
1177                          level += max_level[*last + (!intra<<1)][*run]; // need to add back the max level                          level += max_level[intra][*last][*run];
1178                  else if(mode == 2)  // second escape mode, run is offset                  else                                    /* second escape mode, run is offset */
1179                          *run += max_run[*last + (!intra<<1)][level] + 1;                          *run += max_run[intra][*last][level] + 1;
1180    
1181                  return BitstreamGetBit(bs) ? -level : level;                  return BitstreamGetBits(bs, 1) ? -level : level;
1182          }          }
1183    
1184          // third escape mode - fixed length codes          /* third escape mode - fixed length codes */
1185          BitstreamSkip(bs, 2);          BitstreamSkip(bs, 2);
1186          *last = BitstreamGetBits(bs, 1);          *last = BitstreamGetBits(bs, 1);
1187          *run = BitstreamGetBits(bs, 6);          *run = BitstreamGetBits(bs, 6);
1188          BitstreamSkip(bs, 1);                           // marker          BitstreamSkip(bs, 1);           /* marker */
1189          level = BitstreamGetBits(bs, 12);          level = BitstreamGetBits(bs, 12);
1190          BitstreamSkip(bs, 1);                           // marker          BitstreamSkip(bs, 1);           /* marker */
1191    
1192          return (level & 0x800) ? (level | (-1 ^ 0xfff)) : level;          return (level << 20) >> 20;
1193    
1194  error:  error:
1195          *run = VLC_ERROR;          *run = VLC_ERROR;
1196          return 0;          return 0;
1197  }  }
1198    
1199    void
1200  void get_intra_block(Bitstream * bs, int16_t * block, int direction, int coeff)  get_intra_block(Bitstream * bs,
1201                                    int16_t * block,
1202                                    int direction,
1203                                    int coeff)
1204  {  {
1205    
1206          const uint16_t * scan = scan_tables[ direction ];          const uint16_t * scan = scan_tables[ direction ];
1207          int level;          int level, run, last;
         int run;  
         int last;  
1208    
1209          do          do {
         {  
1210                  level = get_coeff(bs, &run, &last, 1, 0);                  level = get_coeff(bs, &run, &last, 1, 0);
1211                  if (run == -1)                  if (run == -1) {
1212                  {                          DPRINTF(DPRINTF_ERROR,"fatal: invalid run");
                         DEBUG("fatal: invalid run");  
1213                          break;                          break;
1214                  }                  }
1215                  coeff += run;                  coeff += run;
1216                  block[ scan[coeff] ] = level;                  block[ scan[coeff] ] = level;
1217                  if (level < -127 || level > 127)  
1218                  {                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level);
1219                          DEBUG1("warning: intra_overflow", level);                  //DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[coeff], level, BitstreamShowBits(bs, 32));
1220    
1221                    if (level < -2047 || level > 2047) {
1222                            DPRINTF(DPRINTF_ERROR,"warning: intra_overflow %i", level);
1223                  }                  }
1224                  coeff++;                  coeff++;
1225          } while (!last);          } while (!last);
1226    
1227  }  }
1228    
1229  void get_inter_block(Bitstream * bs, int16_t * block)  void
1230    get_inter_block(Bitstream * bs,
1231                                    int16_t * block,
1232                                    int direction)
1233  {  {
1234          const uint16_t * scan = scan_tables[0];  
1235            const uint16_t *scan = scan_tables[direction];
1236          int p;          int p;
1237          int level;          int level;
1238          int run;          int run;
1239          int last;          int last;
1240    
1241          p = 0;          p = 0;
1242          do          do {
         {  
1243                  level = get_coeff(bs, &run, &last, 0, 0);                  level = get_coeff(bs, &run, &last, 0, 0);
1244                  if (run == -1)                  if (run == -1) {
1245                  {                          DPRINTF(DPRINTF_ERROR,"fatal: invalid run");
                         DEBUG("fatal: invalid run");  
1246                          break;                          break;
1247                  }                  }
1248                  p += run;                  p += run;
1249    
1250                  block[ scan[p] ] = level;                  block[ scan[p] ] = level;
1251                  if (level < -127 || level > 127)  
1252                  {                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level);
1253                          DEBUG1("warning: inter_overflow", level);                  // DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[p], level, BitstreamShowBits(bs, 32));
1254    
1255                    if (level < -2047 || level > 2047) {
1256                            DPRINTF(DPRINTF_ERROR,"warning: inter overflow %i", level);
1257                  }                  }
1258                  p++;                  p++;
1259          } while (!last);          } while (!last);
1260    
1261  }  }

Legend:
Removed from v.28  
changed lines
  Added in v.851

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