[svn] / branches / dev-api-3 / xvidcore / src / bitstream / mbcoding.c Repository:
ViewVC logotype

Diff of /branches/dev-api-3/xvidcore/src/bitstream/mbcoding.c

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

trunk/xvidcore/src/bitstream/mbcoding.c revision 78, Thu Mar 28 20:57:25 2002 UTC branches/dev-api-3/xvidcore/src/bitstream/mbcoding.c revision 619, Sat Nov 2 15:52:31 2002 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    
53    #include <stdlib.h>
54  #include "../portab.h"  #include "../portab.h"
55  #include "bitstream.h"  #include "bitstream.h"
56  #include "zigzag.h"  #include "zigzag.h"
57  #include "vlc_codes.h"  #include "vlc_codes.h"
58    #include "mbcoding.h"
59    
60  #include "../utils/mbfunctions.h"  #include "../utils/mbfunctions.h"
61    
 #include <stdlib.h> /* malloc, free */  
   
 #define ESCAPE 7167  
62  #define ABS(X) (((X)>0)?(X):-(X))  #define ABS(X) (((X)>0)?(X):-(X))
63  #define CLIP(X,A) (X > A) ? (A) : (X)  #define CLIP(X,A) (X > A) ? (A) : (X)
64    
65  static VLC *DCT3D[2];  VLC intra_table[524032];
66    VLC inter_table[524032];
67    
68    VLC DCT3Dintra[4096];
69    VLC DCT3Dinter[4096];
70    
71    /* not really MB related, but VLCs are only available here */
72    void inline bs_put_spritetrajectory(Bitstream * bs,
73                              const int val)
74    {
75            const int code = sprite_trajectory_code[val+16384].code;
76            const int len = sprite_trajectory_code[val+16384].len;
77            const int code2 = sprite_trajectory_len[len].code;
78            const int len2 = sprite_trajectory_len[len].len;
79    
80    //      printf("GMC=%d Code/Len  = %d / %d ",val, code,len);
81    //      printf("Code2 / Len2 = %d / %d \n",code2,len2);
82    
83            BitstreamPutBits(bs, code2, len2);
84            if (len) BitstreamPutBits(bs, code, len);
85    }
86    
 VLC *intra_table, *inter_table;  
 static short clip_table[4096];  
87    
88  void create_vlc_tables(void)  void
89    init_vlc_tables(void)
90  {  {
91    
92          int32_t k, l, i, intra, last;          int32_t k, l, i, intra, last;
# Line 24  Line 94 
94          VLC **coeff_ptr;          VLC **coeff_ptr;
95          VLC *vlc1, *vlc2;          VLC *vlc1, *vlc2;
96    
         VLC *DCT3Dintra;  
         VLC *DCT3Dinter;  
   
         DCT3Dintra = (VLC *) malloc(sizeof(VLC) * 4096);  
         DCT3Dinter = (VLC *) malloc(sizeof(VLC) * 4096);  
   
97          vlc1 = DCT3Dintra;          vlc1 = DCT3Dintra;
98          vlc2 = DCT3Dinter;          vlc2 = DCT3Dinter;
99    
100          vlc[0] = intra_table = (VLC *) malloc(128 * 511 * sizeof(VLC));          vlc[0] = intra_table;
101          vlc[1] = inter_table = (VLC *) malloc(128 * 511 * sizeof(VLC));          vlc[1] = inter_table;
102    
103          // initialize the clipping table          // generate encoding vlc lookup tables
104          for(i = -2048; i < 2048; i++) {          // the lookup table idea is taken from the excellent fame project by Vivien Chapellier
                 clip_table[i + 2048] = i;  
                 if(i < -255)  
                         clip_table[i + 2048] = -255;  
                 if(i > 255)  
                         clip_table[i + 2048] = 255;  
         }  
   
         // generate intra/inter vlc lookup table  
105          for(i = 0; i < 4; i++) {          for(i = 0; i < 4; i++) {
106                  intra = i % 2;                  intra = i % 2;
107                  last = i >> 1;                  last = i / 2;
108    
109                  coeff_ptr = coeff_vlc[last + (intra << 1)];                  coeff_ptr = coeff_vlc[last + 2 * intra];
110    
111                  for(k = -255; k < 256; k++) { // level                  for (k = -2047; k < 2048; k++) {        // level
112                          char *max_level_ptr = max_level[last + (intra << 1)];                          int8_t *max_level_ptr = max_level[last + 2 * intra];
113                          char *max_run_ptr = max_run[last + (intra << 1)];                          int8_t *max_run_ptr = max_run[last + 2 * intra];
114    
115                          for(l = 0; l < 64; l++) { // run                          for(l = 0; l < 64; l++) { // run
116                                  int32_t level = k;                                  int32_t level = k;
117                                  uint32_t run = l;                                  ptr_t run = l;
118    
119                                  if(abs(level) <= max_level_ptr[run] && run <= max_run_ptr[abs(level)]) {                                  if ((abs(level) <= max_level_ptr[run]) && (run <= (uint32_t) max_run_ptr[abs(level)])) {        // level < max_level and run < max_run
120    
                                         if(level > 0) {  
                                                 vlc[intra]->code = (coeff_ptr[run][level - 1].code) << 1;  
                                                 vlc[intra]->len = coeff_ptr[run][level - 1].len + 1;  
                                         }  
                                         else if(level < 0) {  
                                                 vlc[intra]->code = ((coeff_ptr[run][-level - 1].code) << 1) + 1;  
                                                 vlc[intra]->len = coeff_ptr[run][-level - 1].len + 1;  
                                         }  
                                         else {  
121                                                  vlc[intra]->code = 0;                                                  vlc[intra]->code = 0;
122                                                  vlc[intra]->len = 0;                                                  vlc[intra]->len = 0;
123                                          }                                          goto loop_end;
124                                  } else {                                  } else {
125                                          if(level > 0)                                          if (level > 0)  // correct level
126                                                  level -= max_level_ptr[run];                                                  level -= max_level_ptr[run];
127                                          else                                          else
128                                                  level += max_level_ptr[run];                                                  level += max_level_ptr[run];
129    
130                                          if(abs(level) <= max_level_ptr[run] &&                                          if ((abs(level) <= max_level_ptr[run]) &&
131                                                  run <= max_run_ptr[abs(level)]) {                                                  (run <= (uint32_t) max_run_ptr[abs(level)])) {
132    
                                                 if(level > 0) {  
                                                         vlc[intra]->code = (0x06 << (coeff_ptr[run][level - 1].len + 1)) |  
                                                                 (coeff_ptr[run][level - 1].code << 1);  
                                                         vlc[intra]->len = (coeff_ptr[run][level - 1].len + 1) + 8;  
                                                 }  
                                                 else if(level < 0) {  
                                                         vlc[intra]->code = (0x06 << (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) + 8;  
                                                 }  
                                                 else {  
133                                                          vlc[intra]->code = 0x06;                                                          vlc[intra]->code = 0x06;
134                                                          vlc[intra]->len = 8;                                                          vlc[intra]->len = 8;
135                                                    goto loop_end;
136                                                  }                                                  }
137                                          } else {  
138                                                  if(level > 0)                                          if (level > 0)  // still here?
139                                                          level += max_level_ptr[run];                                                  level += max_level_ptr[run];    // restore level
140                                                  else                                                  else
141                                                          level -= max_level_ptr[run];                                                          level -= max_level_ptr[run];
142    
143                                                  run -= max_run_ptr[abs(level)] + 1;                                          run -= max_run_ptr[abs(level)] + 1;     // and change run
144    
145                                                  if(abs(level) <= max_level_ptr[run] &&                                          if ((abs(level) <= max_level_ptr[run]) &&
146                                                          run <= max_run_ptr[abs(level)]) {                                                  (run <= (uint32_t) max_run_ptr[abs(level)])) {
147    
                                                         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 {  
148                                                                  vlc[intra]->code = 0x0e;                                                                  vlc[intra]->code = 0x0e;
149                                                                  vlc[intra]->len = 9;                                                                  vlc[intra]->len = 9;
150                                                    goto loop_end;
151                                                          }                                                          }
                                                 } else {  
                                                         if(level != 0)  
152                                                                  run += max_run_ptr[abs(level)] + 1;                                                                  run += max_run_ptr[abs(level)] + 1;
153                                                          else                                  }
                                                                 run++;  
154    
155                                                          vlc[intra]->code = (uint32_t) ((0x1e + last) << 20) |                                  vlc[intra]->code =
156                                                                                  (l << 14) | (1 << 13) | ((k & 0xfff) << 1) | 1;                                          (uint32_t) ((l << 14) | (0x1e + last) << 20) | (1 << 13) |
157                                            ((k & 0xfff) << 1) | 1;
158    
159                                                          vlc[intra]->len = 30;                                                          vlc[intra]->len = 30;
160                                    vlc[intra]++;
161                                    continue;
162    
163                              loop_end:
164                                    if (level != 0) {
165                                            vlc[intra]->code =
166                                                    (vlc[intra]->
167                                                     code << (coeff_ptr[run][abs(level) - 1].len +
168                                                                      1)) | (coeff_ptr[run][abs(level) -
169                                                                                                                    1].code << 1);
170                                            vlc[intra]->len =
171                                                    (coeff_ptr[run][abs(level) - 1].len + 1) +
172                                                    vlc[intra]->len;
173    
174                                            if (level < 0)
175                                                    vlc[intra]->code += 1;
176                                                  }                                                  }
177                                          }  
                                 }  
178                                  vlc[intra]++;                                  vlc[intra]++;
179                          }                          }
180                  }                  }
181          }          }
182    
         intra_table += 64*255; // center vlc tables  
         inter_table += 64*255; // center vlc tables  
   
183          for(i = 0; i < 4096; i++) {          for(i = 0; i < 4096; i++) {
184                  if(i >= 512) {                  if(i >= 512) {
185                          *vlc1 = DCT3Dtab3[(i >> 5) - 16];                          *vlc1 = DCT3Dtab3[(i >> 5) - 16];
186                          *vlc2 = DCT3Dtab0[(i >> 5) - 16];                          *vlc2 = DCT3Dtab0[(i >> 5) - 16];
187                  }                  } else if (i >= 128) {
                 else if(i >= 128) {  
188                          *vlc1 = DCT3Dtab4[(i >> 2) - 32];                          *vlc1 = DCT3Dtab4[(i >> 2) - 32];
189                          *vlc2 = DCT3Dtab1[(i >> 2) - 32];                          *vlc2 = DCT3Dtab1[(i >> 2) - 32];
190                  }                  } else if (i >= 8) {
                 else if(i >= 8) {  
191                          *vlc1 = DCT3Dtab5[i - 8];                          *vlc1 = DCT3Dtab5[i - 8];
192                          *vlc2 = DCT3Dtab2[i - 8];                          *vlc2 = DCT3Dtab2[i - 8];
193                  }                  } else {
                 else {  
194                          *vlc1 = ERRtab[i];                          *vlc1 = ERRtab[i];
195                          *vlc2 = ERRtab[i];                          *vlc2 = ERRtab[i];
196                  }                  }
# Line 167  Line 201 
201          DCT3D[0] = DCT3Dinter;          DCT3D[0] = DCT3Dinter;
202          DCT3D[1] = DCT3Dintra;          DCT3D[1] = DCT3Dintra;
203    
 }  
204    
205  void destroy_vlc_tables(void) {  /* init sprite_trajectory tables */
206    /* even if GMC is not specified (it might be used later...) */
207    
208          if(intra_table != NULL && inter_table != NULL) {          sprite_trajectory_code[0+16384].code = 0;
209                  intra_table -= 64*255; // uncenter vlc tables          sprite_trajectory_code[0+16384].len = 0;
210                  inter_table -= 64*255; // uncenter vlc tables          for (k=0;k<14;k++)
211            {
212                    int limit = (1<<k);
213    
214                  free(intra_table);                  for (i=-(2*limit-1); i<= -limit; i++)
215                  free(inter_table);                  {
216                            sprite_trajectory_code[i+16384].code = (2*limit-1)+i;
217                            sprite_trajectory_code[i+16384].len = k+1;
218          }          }
219    
220          if(DCT3D[0] != NULL && DCT3D[1] != NULL) {                  for (i=limit; i<= 2*limit-1; i++)
221                  free(DCT3D[0]);                  {
222                  free(DCT3D[1]);                          sprite_trajectory_code[i+16384].code = i;
223                            sprite_trajectory_code[i+16384].len = k+1;
224                    }
225          }          }
   
226  }  }
227    
228  static __inline void CodeVector(Bitstream *bs,  static __inline void
229                                  int16_t value,  CodeVector(Bitstream * bs,
230                                  int16_t f_code,                     int32_t value,
231                       int32_t f_code,
232                                  Statistics *pStat)                                  Statistics *pStat)
233  {  {
234    
# Line 205  Line 245 
245          pStat->iMvCount++;          pStat->iMvCount++;
246    
247          if (value == 0) {          if (value == 0) {
248                  BitstreamPutBits(bs, mb_motion_table[32].code, mb_motion_table[32].len);                  BitstreamPutBits(bs, mb_motion_table[32].code,
249                                                     mb_motion_table[32].len);
250          } else {          } else {
251                  uint16_t length, code, mv_res, sign;                  uint16_t length, code, mv_res, sign;
252    
# Line 230  Line 271 
271                          code = -code;                          code = -code;
272    
273                  code += 32;                  code += 32;
274                  BitstreamPutBits(bs, mb_motion_table[code].code, mb_motion_table[code].len);                  BitstreamPutBits(bs, mb_motion_table[code].code,
275                                                     mb_motion_table[code].len);
276    
277                  if(f_code)                  if(f_code)
278                          BitstreamPutBits(bs, mv_res, f_code);                          BitstreamPutBits(bs, mv_res, f_code);
# Line 239  Line 281 
281  }  }
282    
283    
284  static __inline void CodeCoeff(Bitstream *bs,  static __inline void
285                                 int16_t qcoeff[64],  CodeCoeff(Bitstream * bs,
286                      const int16_t qcoeff[64],
287                                 VLC *table,                                 VLC *table,
288                                 const uint16_t *zigzag,                                 const uint16_t *zigzag,
289                                 uint16_t intra)                                 uint16_t intra)
# Line 251  Line 294 
294          VLC *vlc;          VLC *vlc;
295    
296          j = intra;          j = intra;
297          last = 1 + intra;          last = intra;
298    
299          while((v = qcoeff[zigzag[j++]]) == 0);          while (j < 64 && (v = qcoeff[zigzag[j]]) == 0)
300                    j++;
301    
302          do {          do {
303                    vlc = table + 64 * 2047 + (v << 6) + j - last;
304                    last = ++j;
305    
306                  // count zeroes                  // count zeroes
307                  vlc = table + (clip_table[2048+v] << 6) + j - last;                  while (j < 64 && (v = qcoeff[zigzag[j]]) == 0)
308                  last = j + 1;                          j++;
                 while(j < 64 && (v = qcoeff[zigzag[j++]]) == 0);  
309    
310                  // write code                  // write code
311                  if(j != 64) {                  if(j != 64) {
312                          BitstreamPutBits(bs, vlc->code, vlc->len);                          BitstreamPutBits(bs, vlc->code, vlc->len);
313                  } else {                  } else {
314                          vlc += 64*511;                          vlc += 64 * 4095;
315                          BitstreamPutBits(bs, vlc->code, vlc->len);                          BitstreamPutBits(bs, vlc->code, vlc->len);
316                          break;                          break;
317                  }                  }
# Line 274  Line 320 
320  }  }
321    
322    
323  static void CodeBlockIntra(const MBParam * pParam,  static __inline void
324    CodeBlockIntra(const FRAMEINFO * const frame,
325                             const MACROBLOCK *pMB,                             const MACROBLOCK *pMB,
326                             int16_t qcoeff[6*64],                             int16_t qcoeff[6*64],
327                             Bitstream * bs,                             Bitstream * bs,
# Line 286  Line 333 
333          cbpy = pMB->cbp >> 2;          cbpy = pMB->cbp >> 2;
334    
335          // write mcbpc          // write mcbpc
336          if(pParam->coding_type == I_VOP) {          if (frame->coding_type == I_VOP) {
337                  mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2);                  mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2);
338                  BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, mcbpc_intra_tab[mcbpc].len);                  BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code,
339          }                                                   mcbpc_intra_tab[mcbpc].len);
340          else {          } else {
341                  mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);                  mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);
342                  BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len);                  BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,
343                                                     mcbpc_inter_tab[mcbpc].len);
344          }          }
345    
346          // ac prediction flag          // ac prediction flag
# Line 309  Line 357 
357                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, pMB->dquant, 2);
358    
359          // write interlacing          // write interlacing
360          if (pParam->global_flags & XVID_INTERLACING)          if (frame->global_flags & XVID_INTERLACING) {
         {  
361                  BitstreamPutBit(bs, pMB->field_dct);                  BitstreamPutBit(bs, pMB->field_dct);
362          }          }
   
363          // code block coeffs          // code block coeffs
364          for(i = 0; i < 6; i++)          for (i = 0; i < 6; i++) {
         {  
365                  if(i < 4)                  if(i < 4)
366                          BitstreamPutBits(bs,                          BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code,
                                          dcy_tab[qcoeff[i*64 + 0] + 255].code,  
367                                           dcy_tab[qcoeff[i*64 + 0] + 255].len);                                           dcy_tab[qcoeff[i*64 + 0] + 255].len);
368                  else                  else
369                          BitstreamPutBits(bs,                          BitstreamPutBits(bs, dcc_tab[qcoeff[i * 64 + 0] + 255].code,
                                          dcc_tab[qcoeff[i*64 + 0] + 255].code,  
370                                           dcc_tab[qcoeff[i*64 + 0] + 255].len);                                           dcc_tab[qcoeff[i*64 + 0] + 255].len);
371    
372                  if(pMB->cbp & (1 << (5 - i)))                  if (pMB->cbp & (1 << (5 - i))) {
373                  {                          const uint16_t *scan_table =
374                                    frame->global_flags & XVID_ALTERNATESCAN ?
375                                    scan_tables[2] : scan_tables[pMB->acpred_directions[i]];
376    
377                          bits = BitstreamPos(bs);                          bits = BitstreamPos(bs);
378    
379                          CodeCoeff(bs,                          CodeCoeff(bs, &qcoeff[i * 64], intra_table, scan_table, 1);
                                   &qcoeff[i*64],  
                                   intra_table,  
                                   scan_tables[pMB->acpred_directions[i]],  
                                   1);  
380    
381                          bits = BitstreamPos(bs) - bits;                          bits = BitstreamPos(bs) - bits;
382                          pStat->iTextBits += bits;                          pStat->iTextBits += bits;
# Line 344  Line 386 
386  }  }
387    
388    
389  static void CodeBlockInter(const MBParam * pParam,  static void
390    CodeBlockInter(const FRAMEINFO * const frame,
391                             const MACROBLOCK *pMB,                             const MACROBLOCK *pMB,
392                             int16_t qcoeff[6*64],                             int16_t qcoeff[6*64],
393                             Bitstream * bs,                             Bitstream * bs,
# Line 353  Line 396 
396    
397          int32_t i;          int32_t i;
398          uint32_t bits, mcbpc, cbpy;          uint32_t bits, mcbpc, cbpy;
399            int mcsel=0;
400    
401          mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);          mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);
402          cbpy = 15 - (pMB->cbp >> 2);          cbpy = 15 - (pMB->cbp >> 2);
403    
404          // write mcbpc          // write mcbpc
405          BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len);          BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,
406                                             mcbpc_inter_tab[mcbpc].len);
407    
408            if ( (frame->coding_type == S_VOP) && (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) )
409            {
410                    if (frame->quarterpel) {
411                            if ( (pMB->qmvs[0].x == frame->GMC_MV.x) && (pMB->qmvs[0].y == frame->GMC_MV.y) )
412                                    mcsel=1;
413                    } else {
414                            if ( (pMB->mvs[0].x == frame->GMC_MV.x) && (pMB->mvs[0].y == frame->GMC_MV.y) )
415                                    mcsel=1;
416                    }
417                    BitstreamPutBit(bs, mcsel);             // mcsel: '0'=local motion, '1'=GMC
418            }
419    
420          // write cbpy          // write cbpy
421          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);
# Line 368  Line 425 
425                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, pMB->dquant, 2);
426    
427          // interlacing          // interlacing
428          if (pParam->global_flags & XVID_INTERLACING)          if (frame->global_flags & XVID_INTERLACING) {
429          {                  if (pMB->cbp) {
430                  BitstreamPutBit(bs, pMB->field_dct);                  BitstreamPutBit(bs, pMB->field_dct);
431                  DEBUG1("codep: field_dct: ", pMB->field_dct);                  DEBUG1("codep: field_dct: ", pMB->field_dct);
432                    }
433    
434                  // if inter block, write field ME flag                  // if inter block, write field ME flag
435                  if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)                  if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
                 {  
436                          BitstreamPutBit(bs, pMB->field_pred);                          BitstreamPutBit(bs, pMB->field_pred);
437                          DEBUG1("codep: field_pred: ", pMB->field_pred);                          DEBUG1("codep: field_pred: ", pMB->field_pred);
438    
439                          // write field prediction references                          // write field prediction references
440                          if (pMB->field_pred)                          if (pMB->field_pred) {
                         {  
441                                  BitstreamPutBit(bs, pMB->field_for_top);                                  BitstreamPutBit(bs, pMB->field_for_top);
442                                  BitstreamPutBit(bs, pMB->field_for_bot);                                  BitstreamPutBit(bs, pMB->field_for_bot);
443                          }                          }
444                  }                  }
445          }          }
446            // code motion vector(s) if motion is local
447          // code motion vector(s)          if (mcsel==0)
448          for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++)                  for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) {
449          {                          CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat);
450                  CodeVector(bs, pMB->pmvs[i].x, pParam->fixed_code, pStat);                          CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat);
                 CodeVector(bs, pMB->pmvs[i].y, pParam->fixed_code, pStat);  
451          }          }
452    
453          bits = BitstreamPos(bs);          bits = BitstreamPos(bs);
# Line 400  Line 455 
455          // code block coeffs          // code block coeffs
456          for(i = 0; i < 6; i++)          for(i = 0; i < 6; i++)
457                  if(pMB->cbp & (1 << (5 - i)))                  if(pMB->cbp & (1 << (5 - i)))
458                          CodeCoeff(bs, &qcoeff[i*64], inter_table, scan_tables[0], 0);                  {
459                            const uint16_t *scan_table =
460                                    frame->global_flags & XVID_ALTERNATESCAN ?
461                                    scan_tables[2] : scan_tables[0];
462    
463                            CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_table, 0);
464                    }
465    
466          bits = BitstreamPos(bs) - bits;          bits = BitstreamPos(bs) - bits;
467          pStat->iTextBits += bits;          pStat->iTextBits += bits;
   
468  }  }
469    
470    
471  void MBCoding(const MBParam * pParam,  void
472    MBCoding(const FRAMEINFO * const frame,
473                MACROBLOCK *pMB,                MACROBLOCK *pMB,
474                int16_t qcoeff[6*64],                int16_t qcoeff[6*64],
475                Bitstream * bs,                Bitstream * bs,
476                Statistics * pStat)                Statistics * pStat)
477  {  {
478            if (frame->coding_type != I_VOP)
479                            BitstreamPutBit(bs, 0); // not_coded
480    
481          int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q);          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)
482                    CodeBlockIntra(frame, pMB, qcoeff, bs, pStat);
483            else
484                    CodeBlockInter(frame, pMB, qcoeff, bs, pStat);
485    
486          if(pParam->coding_type == P_VOP) {  }
487                  if(pMB->cbp == 0 && pMB->mode == MODE_INTER &&  
488                     pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0)  /*
489    // moved to mbcoding.h so that in can be 'static __inline'
490    void
491    MBSkip(Bitstream * bs)
492                  {                  {
493                          BitstreamPutBit(bs, 1);         // not_coded          BitstreamPutBit(bs, 1); // not coded
494    }
495    */
496    
497    /***************************************************************
498     * bframe encoding start
499     ***************************************************************/
500    
501    /*
502            mbtype
503            0       1b              direct(h263)            mvdb
504            1       01b             interpolate mc+q        dbquant, mvdf, mvdb
505            2       001b    backward mc+q           dbquant, mvdb
506            3       0001b   forward mc+q            dbquant, mvdf
507    */
508    
509    static __inline void
510    put_bvop_mbtype(Bitstream * bs,
511                                    int value)
512    {
513            switch (value) {
514                    case MODE_FORWARD:
515                            BitstreamPutBit(bs, 0);
516                    case MODE_BACKWARD:
517                            BitstreamPutBit(bs, 0);
518                    case MODE_INTERPOLATE:
519                            BitstreamPutBit(bs, 0);
520                    case MODE_DIRECT:
521                            BitstreamPutBit(bs, 1);
522                    default:
523                            break;
524            }
525    }
526    
527    /*
528            dbquant
529            -2      10b
530            0       0b
531            +2      11b
532    */
533    
534    static __inline void
535    put_bvop_dbquant(Bitstream * bs,
536                                     int value)
537    {
538            switch (value) {
539            case 0:
540                    BitstreamPutBit(bs, 0);
541                    return;
542    
543            case -2:
544                    BitstreamPutBit(bs, 1);
545                    BitstreamPutBit(bs, 0);
546                    return;
547    
548            case 2:
549                    BitstreamPutBit(bs, 1);
550                    BitstreamPutBit(bs, 1);
551                          return;                          return;
552    
553            default:;                                       // invalid
554                  }                  }
                 else  
                         BitstreamPutBit(bs, 0);         // coded  
555          }          }
556    
         if(intra)  
                 CodeBlockIntra(pParam, pMB, qcoeff, bs, pStat);  
         else  
                 CodeBlockInter(pParam, pMB, qcoeff, bs, pStat);  
557    
558    
559    void
560    MBCodingBVOP(const MACROBLOCK * mb,
561                             const int16_t qcoeff[6 * 64],
562                             const int32_t fcode,
563                             const int32_t bcode,
564                             Bitstream * bs,
565                             Statistics * pStat,
566                             int direction)
567    {
568            int vcode = fcode;
569            unsigned int i;
570    
571    /*      ------------------------------------------------------------------
572                    when a block is skipped it is decoded DIRECT(0,0)
573                    hence is interpolated from forward & backward frames
574            ------------------------------------------------------------------ */
575    
576            if (mb->mode == MODE_DIRECT_NONE_MV) {
577                    BitstreamPutBit(bs, 1); // skipped
578                    return;
579            }
580    
581            BitstreamPutBit(bs, 0);         // not skipped
582    
583            if (mb->cbp == 0) {
584                    BitstreamPutBit(bs, 1); // cbp == 0
585            } else {
586                    BitstreamPutBit(bs, 0); // cbp == xxx
587            }
588    
589            put_bvop_mbtype(bs, mb->mode);
590    
591            if (mb->cbp) {
592                    BitstreamPutBits(bs, mb->cbp, 6);
593            }
594    
595            if (mb->mode != MODE_DIRECT && mb->cbp != 0) {
596                    put_bvop_dbquant(bs, 0);        // todo: mb->dquant = 0
597  }  }
598    
599            switch (mb->mode) {
600                    case MODE_INTERPOLATE:
601                            CodeVector(bs, mb->pmvs[1].x, vcode, pStat); //forward vector of interpolate mode
602                            CodeVector(bs, mb->pmvs[1].y, vcode, pStat);
603                    case MODE_BACKWARD:
604                            vcode = bcode;
605                    case MODE_FORWARD:
606                            CodeVector(bs, mb->pmvs[0].x, vcode, pStat);
607                            CodeVector(bs, mb->pmvs[0].y, vcode, pStat);
608                            break;
609                    case MODE_DIRECT:
610                            CodeVector(bs, mb->pmvs[3].x, 1, pStat);        // fcode is always 1 for delta vector
611                            CodeVector(bs, mb->pmvs[3].y, 1, pStat);        // prediction is always (0,0)
612                    default: break;
613            }
614    
615            for (i = 0; i < 6; i++) {
616                    if (mb->cbp & (1 << (5 - i))) {
617                            CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[direction], 0);
618                    }
619            }
620    }
621    
622    
623    
624  /***************************************************************  /***************************************************************
625   * decoding stuff starts here                                  *   * decoding stuff starts here                                  *
626   ***************************************************************/   ***************************************************************/
627    
628  int get_mcbpc_intra(Bitstream * bs)  
629    // for IVOP addbits == 0
630    // for PVOP addbits == fcode - 1
631    // for BVOP addbits == max(fcode,bcode) - 1
632    // returns true or false
633    int
634    check_resync_marker(Bitstream * bs, int addbits)
635    {
636            uint32_t nbits;
637            uint32_t code;
638            uint32_t nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER + addbits;
639    
640            nbits = BitstreamNumBitsToByteAlign(bs);
641            code = BitstreamShowBits(bs, nbits);
642    
643            if (code == (((uint32_t)1 << (nbits - 1)) - 1))
644  {  {
645                    return BitstreamShowBitsFromByteAlign(bs, nbitsresyncmarker) == RESYNC_MARKER;
646            }
647    
648            return 0;
649    }
650    
         uint32_t index;  
651    
         while((index = BitstreamShowBits(bs, 9)) == 1)  
                 BitstreamSkip(bs, 9);  
652    
653    int
654    get_mcbpc_intra(Bitstream * bs)
655    {
656    
657            uint32_t index;
658    
659            index = BitstreamShowBits(bs, 9);
660          index >>= 3;          index >>= 3;
661    
662          BitstreamSkip(bs, mcbpc_intra_table[index].len);          BitstreamSkip(bs, mcbpc_intra_table[index].len);
# Line 456  Line 665 
665    
666  }  }
667    
668  int get_mcbpc_inter(Bitstream * bs)  int
669    get_mcbpc_inter(Bitstream * bs)
670  {  {
671    
672          uint32_t index;          uint32_t index;
673    
674          while((index = CLIP(BitstreamShowBits(bs, 9), 256)) == 1)          index = CLIP(BitstreamShowBits(bs, 9), 256);
                 BitstreamSkip(bs, 9);  
675    
676          BitstreamSkip(bs,  mcbpc_inter_table[index].len);          BitstreamSkip(bs,  mcbpc_inter_table[index].len);
677    
# Line 470  Line 679 
679    
680  }  }
681    
682  int get_cbpy(Bitstream * bs, int intra)  int
683    get_cbpy(Bitstream * bs,
684                     int intra)
685  {  {
686    
687          int cbpy;          int cbpy;
# Line 486  Line 697 
697    
698  }  }
699    
700  int get_mv_data(Bitstream * bs)  static __inline int
701    get_mv_data(Bitstream * bs)
702  {  {
703    
704          uint32_t index;          uint32_t index;
# Line 496  Line 708 
708    
709          index = BitstreamShowBits(bs, 12);          index = BitstreamShowBits(bs, 12);
710    
711          if(index >= 512)          if (index >= 512) {
         {  
712                  index = (index >> 8) - 2;                  index = (index >> 8) - 2;
713                  BitstreamSkip(bs, TMNMVtab0[index].len);                  BitstreamSkip(bs, TMNMVtab0[index].len);
714                  return TMNMVtab0[index].code;                  return TMNMVtab0[index].code;
715          }          }
716    
717          if(index >= 128)          if (index >= 128) {
         {  
718                  index = (index >> 2) - 32;                  index = (index >> 2) - 32;
719                  BitstreamSkip(bs, TMNMVtab1[index].len);                  BitstreamSkip(bs, TMNMVtab1[index].len);
720                  return TMNMVtab1[index].code;                  return TMNMVtab1[index].code;
# Line 517  Line 727 
727    
728  }  }
729    
730  int get_mv(Bitstream * bs, int fcode)  int
731    get_mv(Bitstream * bs,
732               int fcode)
733  {  {
734    
735          int data;          int data;
# Line 537  Line 749 
749    
750  }  }
751    
752  int get_dc_dif(Bitstream * bs, uint32_t dc_size)  int
753    get_dc_dif(Bitstream * bs,
754                       uint32_t dc_size)
755  {  {
756    
757          int code = BitstreamGetBits(bs, dc_size);          int code = BitstreamGetBits(bs, dc_size);
# Line 550  Line 764 
764    
765  }  }
766    
767  int get_dc_size_lum(Bitstream * bs)  int
768    get_dc_size_lum(Bitstream * bs)
769  {  {
770    
771          int code, i;          int code, i;
772    
773          code = BitstreamShowBits(bs, 11);          code = BitstreamShowBits(bs, 11);
774    
775          for(i = 11; i > 3; i--) {          for(i = 11; i > 3; i--) {
# Line 570  Line 786 
786  }  }
787    
788    
789  int get_dc_size_chrom(Bitstream * bs)  int
790    get_dc_size_chrom(Bitstream * bs)
791  {  {
792    
793          uint32_t code, i;          uint32_t code, i;
794    
795          code = BitstreamShowBits(bs, 12);          code = BitstreamShowBits(bs, 12);
796    
797          for(i = 12; i > 2; i--) {          for(i = 12; i > 2; i--) {
# Line 588  Line 806 
806    
807  }  }
808    
809  int get_coeff(Bitstream * bs, int *run, int *last, int intra, int short_video_header)  void
810  {  get_intra_block(Bitstream * bs,
811                                    int16_t * block,
812          uint32_t mode;                                  int direction,
813          const VLC *tab;                                  int coeff)
         int32_t level;  
   
         if(short_video_header) // inter-VLCs will be used for both intra and inter blocks  
                 intra = 0;  
   
         tab = &DCT3D[intra][BitstreamShowBits(bs, 12)];  
   
         if(tab->code == -1)  
                 goto error;  
   
         BitstreamSkip(bs, tab->len);  
   
         if(tab->code != ESCAPE) {  
                 if(!intra)  
                 {  
                         *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;  
         }  
   
         if(short_video_header)  
         {  
                 // escape mode 4 - H.263 type, only used if short_video_header = 1  
                 *last = BitstreamGetBit(bs);  
                 *run = BitstreamGetBits(bs, 6);  
                 level = BitstreamGetBits(bs, 8);  
   
                 if (level == 0 || level == 128)  
                         DEBUG1("Illegal LEVEL for ESCAPE mode 4:", level);  
   
                 return (level >= 128 ? -(256 - level) : level);  
         }  
   
         mode = BitstreamShowBits(bs, 2);  
   
         if(mode < 3) {  
                 BitstreamSkip(bs, (mode == 2) ? 2 : 1);  
   
                 tab = &DCT3D[intra][BitstreamShowBits(bs, 12)];  
                 if (tab->code == -1)  
                         goto error;  
   
                 BitstreamSkip(bs, tab->len);  
   
                 if (!intra) {  
                         *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;  
                 }  
   
                 if(mode < 2) // first escape mode, level is offset  
                         level += max_level[*last + (!intra<<1)][*run]; // need to add back the max level  
                 else if(mode == 2)  // second escape mode, run is offset  
                         *run += max_run[*last + (!intra<<1)][level] + 1;  
   
                 return BitstreamGetBit(bs) ? -level : level;  
         }  
   
         // third escape mode - fixed length codes  
         BitstreamSkip(bs, 2);  
         *last = BitstreamGetBits(bs, 1);  
         *run = BitstreamGetBits(bs, 6);  
         BitstreamSkip(bs, 1);                           // marker  
         level = BitstreamGetBits(bs, 12);  
         BitstreamSkip(bs, 1);                           // marker  
   
         return (level & 0x800) ? (level | (-1 ^ 0xfff)) : level;  
   
  error:  
         *run = VLC_ERROR;  
         return 0;  
   
 }  
   
   
 void get_intra_block(Bitstream * bs, int16_t * block, int direction, int coeff)  
814  {  {
815    
816          const uint16_t * scan = scan_tables[ direction ];          const uint16_t * scan = scan_tables[ direction ];
817          int level;          int level, run, last;
         int run;  
         int last;  
818    
819          do          do {
         {  
820                  level = get_coeff(bs, &run, &last, 1, 0);                  level = get_coeff(bs, &run, &last, 1, 0);
821                  if (run == -1)                  if (run == -1) {
                 {  
822                          DEBUG("fatal: invalid run");                          DEBUG("fatal: invalid run");
823                          break;                          break;
824                  }                  }
825                  coeff += run;                  coeff += run;
826                  block[ scan[coeff] ] = level;                  block[ scan[coeff] ] = level;
827                  if (level < -127 || level > 127)  
828                  {                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level);
829                    //DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[coeff], level, BitstreamShowBits(bs, 32));
830    
831                    if (level < -127 || level > 127) {
832                          DEBUG1("warning: intra_overflow", level);                          DEBUG1("warning: intra_overflow", level);
833                  }                  }
834                  coeff++;                  coeff++;
# Line 709  Line 836 
836    
837  }  }
838    
839  void get_inter_block(Bitstream * bs, int16_t * block)  void
840    get_inter_block(Bitstream * bs,
841                                    int16_t * block,
842                                    int direction)
843  {  {
844    
845          const uint16_t * scan = scan_tables[0];          const uint16_t *scan = scan_tables[direction];
846          int p;          int p;
847          int level;          int level;
848          int run;          int run;
849          int last;          int last;
850    
851          p = 0;          p = 0;
852          do          do {
         {  
853                  level = get_coeff(bs, &run, &last, 0, 0);                  level = get_coeff(bs, &run, &last, 0, 0);
854                  if (run == -1)                  if (run == -1) {
                 {  
855                          DEBUG("fatal: invalid run");                          DEBUG("fatal: invalid run");
856                          break;                          break;
857                  }                  }
858                  p += run;                  p += run;
859    
860                  block[ scan[p] ] = level;                  block[ scan[p] ] = level;
861                  if (level < -127 || level > 127)  
862                  {                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level);
863                    // DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[p], level, BitstreamShowBits(bs, 32));
864    
865                    if (level < -127 || level > 127) {
866                          DEBUG1("warning: inter_overflow", level);                          DEBUG1("warning: inter_overflow", level);
867                  }                  }
868                  p++;                  p++;

Legend:
Removed from v.78  
changed lines
  Added in v.619

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