[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 631, Thu Nov 7 10:31:03 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 bs_put_spritetrajectory(Bitstream * bs, const int val)
73    {
74            const int code = sprite_trajectory_code[val+16384].code;
75            const int len = sprite_trajectory_code[val+16384].len;
76            const int code2 = sprite_trajectory_len[len].code;
77            const int len2 = sprite_trajectory_len[len].len;
78    
79    //      printf("GMC=%d Code/Len  = %d / %d ",val, code,len);
80    //      printf("Code2 / Len2 = %d / %d \n",code2,len2);
81    
82            BitstreamPutBits(bs, code2, len2);
83            if (len) BitstreamPutBits(bs, code, len);
84    }
85    
86  VLC *intra_table, *inter_table;  int bs_get_spritetrajectory(Bitstream * bs)
87  static short clip_table[4096];  {
88            int i;
89            for (i = 0; i < 12; i++)
90            {
91                    if (BitstreamShowBits(bs, sprite_trajectory_len[i].len) == sprite_trajectory_len[i].code)
92                    {
93                            BitstreamSkip(bs, sprite_trajectory_len[i].len);
94                            return i;
95                    }
96            }
97            return -1;
98    }
99    
100  void create_vlc_tables(void)  void
101    init_vlc_tables(void)
102  {  {
103    
104          int32_t k, l, i, intra, last;          int32_t k, l, i, intra, last;
# Line 24  Line 106 
106          VLC **coeff_ptr;          VLC **coeff_ptr;
107          VLC *vlc1, *vlc2;          VLC *vlc1, *vlc2;
108    
         VLC *DCT3Dintra;  
         VLC *DCT3Dinter;  
   
         DCT3Dintra = (VLC *) malloc(sizeof(VLC) * 4096);  
         DCT3Dinter = (VLC *) malloc(sizeof(VLC) * 4096);  
   
109          vlc1 = DCT3Dintra;          vlc1 = DCT3Dintra;
110          vlc2 = DCT3Dinter;          vlc2 = DCT3Dinter;
111    
112          vlc[0] = intra_table = (VLC *) malloc(128 * 511 * sizeof(VLC));          vlc[0] = intra_table;
113          vlc[1] = inter_table = (VLC *) malloc(128 * 511 * sizeof(VLC));          vlc[1] = inter_table;
   
         // initialize the clipping table  
         for(i = -2048; i < 2048; i++) {  
                 clip_table[i + 2048] = i;  
                 if(i < -255)  
                         clip_table[i + 2048] = -255;  
                 if(i > 255)  
                         clip_table[i + 2048] = 255;  
         }  
114    
115          // generate intra/inter vlc lookup table          // generate encoding vlc lookup tables
116            // the lookup table idea is taken from the excellent fame project by Vivien Chapellier
117          for(i = 0; i < 4; i++) {          for(i = 0; i < 4; i++) {
118                  intra = i % 2;                  intra = i % 2;
119                  last = i >> 1;                  last = i / 2;
120    
121                  coeff_ptr = coeff_vlc[last + (intra << 1)];                  coeff_ptr = coeff_vlc[last + 2 * intra];
122    
123                  for(k = -255; k < 256; k++) { // level                  for (k = -2047; k < 2048; k++) {        // level
124                          char *max_level_ptr = max_level[last + (intra << 1)];                          int8_t *max_level_ptr = max_level[last + 2 * intra];
125                          char *max_run_ptr = max_run[last + (intra << 1)];                          int8_t *max_run_ptr = max_run[last + 2 * intra];
126    
127                          for(l = 0; l < 64; l++) { // run                          for(l = 0; l < 64; l++) { // run
128                                  int32_t level = k;                                  int32_t level = k;
129                                  uint32_t run = l;                                  ptr_t run = l;
130    
131                                  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
132    
                                         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 {  
133                                                  vlc[intra]->code = 0;                                                  vlc[intra]->code = 0;
134                                                  vlc[intra]->len = 0;                                                  vlc[intra]->len = 0;
135                                          }                                          goto loop_end;
136                                  } else {                                  } else {
137                                          if(level > 0)                                          if (level > 0)  // correct level
138                                                  level -= max_level_ptr[run];                                                  level -= max_level_ptr[run];
139                                          else                                          else
140                                                  level += max_level_ptr[run];                                                  level += max_level_ptr[run];
141    
142                                          if(abs(level) <= max_level_ptr[run] &&                                          if ((abs(level) <= max_level_ptr[run]) &&
143                                                  run <= max_run_ptr[abs(level)]) {                                                  (run <= (uint32_t) max_run_ptr[abs(level)])) {
144    
                                                 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 {  
145                                                          vlc[intra]->code = 0x06;                                                          vlc[intra]->code = 0x06;
146                                                          vlc[intra]->len = 8;                                                          vlc[intra]->len = 8;
147                                                    goto loop_end;
148                                                  }                                                  }
149                                          } else {  
150                                                  if(level > 0)                                          if (level > 0)  // still here?
151                                                          level += max_level_ptr[run];                                                  level += max_level_ptr[run];    // restore level
152                                                  else                                                  else
153                                                          level -= max_level_ptr[run];                                                          level -= max_level_ptr[run];
154    
155                                                  run -= max_run_ptr[abs(level)] + 1;                                          run -= max_run_ptr[abs(level)] + 1;     // and change run
156    
157                                                  if(abs(level) <= max_level_ptr[run] &&                                          if ((abs(level) <= max_level_ptr[run]) &&
158                                                          run <= max_run_ptr[abs(level)]) {                                                  (run <= (uint32_t) max_run_ptr[abs(level)])) {
159    
                                                         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 {  
160                                                                  vlc[intra]->code = 0x0e;                                                                  vlc[intra]->code = 0x0e;
161                                                                  vlc[intra]->len = 9;                                                                  vlc[intra]->len = 9;
162                                                    goto loop_end;
163                                                          }                                                          }
                                                 } else {  
                                                         if(level != 0)  
164                                                                  run += max_run_ptr[abs(level)] + 1;                                                                  run += max_run_ptr[abs(level)] + 1;
165                                                          else                                  }
                                                                 run++;  
166    
167                                                          vlc[intra]->code = (uint32_t) ((0x1e + last) << 20) |                                  vlc[intra]->code =
168                                                                                  (l << 14) | (1 << 13) | ((k & 0xfff) << 1) | 1;                                          (uint32_t) ((l << 14) | (0x1e + last) << 20) | (1 << 13) |
169                                            ((k & 0xfff) << 1) | 1;
170    
171                                                          vlc[intra]->len = 30;                                                          vlc[intra]->len = 30;
172                                    vlc[intra]++;
173                                    continue;
174    
175                              loop_end:
176                                    if (level != 0) {
177                                            vlc[intra]->code =
178                                                    (vlc[intra]->
179                                                     code << (coeff_ptr[run][abs(level) - 1].len +
180                                                                      1)) | (coeff_ptr[run][abs(level) -
181                                                                                                                    1].code << 1);
182                                            vlc[intra]->len =
183                                                    (coeff_ptr[run][abs(level) - 1].len + 1) +
184                                                    vlc[intra]->len;
185    
186                                            if (level < 0)
187                                                    vlc[intra]->code += 1;
188                                                  }                                                  }
189                                          }  
                                 }  
190                                  vlc[intra]++;                                  vlc[intra]++;
191                          }                          }
192                  }                  }
193          }          }
194    
         intra_table += 64*255; // center vlc tables  
         inter_table += 64*255; // center vlc tables  
   
195          for(i = 0; i < 4096; i++) {          for(i = 0; i < 4096; i++) {
196                  if(i >= 512) {                  if(i >= 512) {
197                          *vlc1 = DCT3Dtab3[(i >> 5) - 16];                          *vlc1 = DCT3Dtab3[(i >> 5) - 16];
198                          *vlc2 = DCT3Dtab0[(i >> 5) - 16];                          *vlc2 = DCT3Dtab0[(i >> 5) - 16];
199                  }                  } else if (i >= 128) {
                 else if(i >= 128) {  
200                          *vlc1 = DCT3Dtab4[(i >> 2) - 32];                          *vlc1 = DCT3Dtab4[(i >> 2) - 32];
201                          *vlc2 = DCT3Dtab1[(i >> 2) - 32];                          *vlc2 = DCT3Dtab1[(i >> 2) - 32];
202                  }                  } else if (i >= 8) {
                 else if(i >= 8) {  
203                          *vlc1 = DCT3Dtab5[i - 8];                          *vlc1 = DCT3Dtab5[i - 8];
204                          *vlc2 = DCT3Dtab2[i - 8];                          *vlc2 = DCT3Dtab2[i - 8];
205                  }                  } else {
                 else {  
206                          *vlc1 = ERRtab[i];                          *vlc1 = ERRtab[i];
207                          *vlc2 = ERRtab[i];                          *vlc2 = ERRtab[i];
208                  }                  }
# Line 167  Line 213 
213          DCT3D[0] = DCT3Dinter;          DCT3D[0] = DCT3Dinter;
214          DCT3D[1] = DCT3Dintra;          DCT3D[1] = DCT3Dintra;
215    
 }  
216    
217  void destroy_vlc_tables(void) {  /* init sprite_trajectory tables */
218    /* even if GMC is not specified (it might be used later...) */
219    
220          if(intra_table != NULL && inter_table != NULL) {          sprite_trajectory_code[0+16384].code = 0;
221                  intra_table -= 64*255; // uncenter vlc tables          sprite_trajectory_code[0+16384].len = 0;
222                  inter_table -= 64*255; // uncenter vlc tables          for (k=0;k<14;k++)
223            {
224                    int limit = (1<<k);
225    
226                  free(intra_table);                  for (i=-(2*limit-1); i<= -limit; i++)
227                  free(inter_table);                  {
228                            sprite_trajectory_code[i+16384].code = (2*limit-1)+i;
229                            sprite_trajectory_code[i+16384].len = k+1;
230          }          }
231    
232          if(DCT3D[0] != NULL && DCT3D[1] != NULL) {                  for (i=limit; i<= 2*limit-1; i++)
233                  free(DCT3D[0]);                  {
234                  free(DCT3D[1]);                          sprite_trajectory_code[i+16384].code = i;
235                            sprite_trajectory_code[i+16384].len = k+1;
236                    }
237          }          }
   
238  }  }
239    
240  static __inline void CodeVector(Bitstream *bs,  static __inline void
241                                  int16_t value,  CodeVector(Bitstream * bs,
242                                  int16_t f_code,                     int32_t value,
243                       int32_t f_code,
244                                  Statistics *pStat)                                  Statistics *pStat)
245  {  {
246    
# Line 205  Line 257 
257          pStat->iMvCount++;          pStat->iMvCount++;
258    
259          if (value == 0) {          if (value == 0) {
260                  BitstreamPutBits(bs, mb_motion_table[32].code, mb_motion_table[32].len);                  BitstreamPutBits(bs, mb_motion_table[32].code,
261                                                     mb_motion_table[32].len);
262          } else {          } else {
263                  uint16_t length, code, mv_res, sign;                  uint16_t length, code, mv_res, sign;
264    
# Line 230  Line 283 
283                          code = -code;                          code = -code;
284    
285                  code += 32;                  code += 32;
286                  BitstreamPutBits(bs, mb_motion_table[code].code, mb_motion_table[code].len);                  BitstreamPutBits(bs, mb_motion_table[code].code,
287                                                     mb_motion_table[code].len);
288    
289                  if(f_code)                  if(f_code)
290                          BitstreamPutBits(bs, mv_res, f_code);                          BitstreamPutBits(bs, mv_res, f_code);
# Line 239  Line 293 
293  }  }
294    
295    
296  static __inline void CodeCoeff(Bitstream *bs,  static __inline void
297                                 int16_t qcoeff[64],  CodeCoeff(Bitstream * bs,
298                      const int16_t qcoeff[64],
299                                 VLC *table,                                 VLC *table,
300                                 const uint16_t *zigzag,                                 const uint16_t *zigzag,
301                                 uint16_t intra)                                 uint16_t intra)
# Line 251  Line 306 
306          VLC *vlc;          VLC *vlc;
307    
308          j = intra;          j = intra;
309          last = 1 + intra;          last = intra;
310    
311          while((v = qcoeff[zigzag[j++]]) == 0);          while (j < 64 && (v = qcoeff[zigzag[j]]) == 0)
312                    j++;
313    
314          do {          do {
315                    vlc = table + 64 * 2047 + (v << 6) + j - last;
316                    last = ++j;
317    
318                  // count zeroes                  // count zeroes
319                  vlc = table + (clip_table[2048+v] << 6) + j - last;                  while (j < 64 && (v = qcoeff[zigzag[j]]) == 0)
320                  last = j + 1;                          j++;
                 while(j < 64 && (v = qcoeff[zigzag[j++]]) == 0);  
321    
322                  // write code                  // write code
323                  if(j != 64) {                  if(j != 64) {
324                          BitstreamPutBits(bs, vlc->code, vlc->len);                          BitstreamPutBits(bs, vlc->code, vlc->len);
325                  } else {                  } else {
326                          vlc += 64*511;                          vlc += 64 * 4095;
327                          BitstreamPutBits(bs, vlc->code, vlc->len);                          BitstreamPutBits(bs, vlc->code, vlc->len);
328                          break;                          break;
329                  }                  }
# Line 274  Line 332 
332  }  }
333    
334    
335  static void CodeBlockIntra(const MBParam * pParam,  static __inline void
336    CodeBlockIntra(const FRAMEINFO * const frame,
337                             const MACROBLOCK *pMB,                             const MACROBLOCK *pMB,
338                             int16_t qcoeff[6*64],                             int16_t qcoeff[6*64],
339                             Bitstream * bs,                             Bitstream * bs,
# Line 286  Line 345 
345          cbpy = pMB->cbp >> 2;          cbpy = pMB->cbp >> 2;
346    
347          // write mcbpc          // write mcbpc
348          if(pParam->coding_type == I_VOP) {          if (frame->coding_type == I_VOP) {
349                  mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2);                  mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2);
350                  BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, mcbpc_intra_tab[mcbpc].len);                  BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code,
351          }                                                   mcbpc_intra_tab[mcbpc].len);
352          else {          } else {
353                  mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);                  mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);
354                  BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len);                  BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,
355                                                     mcbpc_inter_tab[mcbpc].len);
356          }          }
357    
358          // ac prediction flag          // ac prediction flag
# Line 309  Line 369 
369                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, pMB->dquant, 2);
370    
371          // write interlacing          // write interlacing
372          if (pParam->global_flags & XVID_INTERLACING)          if (frame->global_flags & XVID_INTERLACING) {
         {  
373                  BitstreamPutBit(bs, pMB->field_dct);                  BitstreamPutBit(bs, pMB->field_dct);
374          }          }
   
375          // code block coeffs          // code block coeffs
376          for(i = 0; i < 6; i++)          for (i = 0; i < 6; i++) {
         {  
377                  if(i < 4)                  if(i < 4)
378                          BitstreamPutBits(bs,                          BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code,
                                          dcy_tab[qcoeff[i*64 + 0] + 255].code,  
379                                           dcy_tab[qcoeff[i*64 + 0] + 255].len);                                           dcy_tab[qcoeff[i*64 + 0] + 255].len);
380                  else                  else
381                          BitstreamPutBits(bs,                          BitstreamPutBits(bs, dcc_tab[qcoeff[i * 64 + 0] + 255].code,
                                          dcc_tab[qcoeff[i*64 + 0] + 255].code,  
382                                           dcc_tab[qcoeff[i*64 + 0] + 255].len);                                           dcc_tab[qcoeff[i*64 + 0] + 255].len);
383    
384                  if(pMB->cbp & (1 << (5 - i)))                  if (pMB->cbp & (1 << (5 - i))) {
385                  {                          const uint16_t *scan_table =
386                                    frame->global_flags & XVID_ALTERNATESCAN ?
387                                    scan_tables[2] : scan_tables[pMB->acpred_directions[i]];
388    
389                          bits = BitstreamPos(bs);                          bits = BitstreamPos(bs);
390    
391                          CodeCoeff(bs,                          CodeCoeff(bs, &qcoeff[i * 64], intra_table, scan_table, 1);
                                   &qcoeff[i*64],  
                                   intra_table,  
                                   scan_tables[pMB->acpred_directions[i]],  
                                   1);  
392    
393                          bits = BitstreamPos(bs) - bits;                          bits = BitstreamPos(bs) - bits;
394                          pStat->iTextBits += bits;                          pStat->iTextBits += bits;
# Line 344  Line 398 
398  }  }
399    
400    
401  static void CodeBlockInter(const MBParam * pParam,  static void
402    CodeBlockInter(const FRAMEINFO * const frame,
403                             const MACROBLOCK *pMB,                             const MACROBLOCK *pMB,
404                             int16_t qcoeff[6*64],                             int16_t qcoeff[6*64],
405                             Bitstream * bs,                             Bitstream * bs,
# Line 353  Line 408 
408    
409          int32_t i;          int32_t i;
410          uint32_t bits, mcbpc, cbpy;          uint32_t bits, mcbpc, cbpy;
411            int mcsel=0;
412    
413          mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);          mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);
414          cbpy = 15 - (pMB->cbp >> 2);          cbpy = 15 - (pMB->cbp >> 2);
415    
416          // write mcbpc          // write mcbpc
417          BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len);          BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,
418                                             mcbpc_inter_tab[mcbpc].len);
419    
420            if ( (frame->coding_type == S_VOP) && (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) )
421            {
422                    if (frame->quarterpel) {
423                            if ( (pMB->qmvs[0].x == frame->GMC_MV.x) && (pMB->qmvs[0].y == frame->GMC_MV.y) )
424                                    mcsel=1;
425                    } else {
426                            if ( (pMB->mvs[0].x == frame->GMC_MV.x) && (pMB->mvs[0].y == frame->GMC_MV.y) )
427                                    mcsel=1;
428                    }
429                    BitstreamPutBit(bs, mcsel);             // mcsel: '0'=local motion, '1'=GMC
430            }
431    
432          // write cbpy          // write cbpy
433          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);
# Line 368  Line 437 
437                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, pMB->dquant, 2);
438    
439          // interlacing          // interlacing
440          if (pParam->global_flags & XVID_INTERLACING)          if (frame->global_flags & XVID_INTERLACING) {
441          {                  if (pMB->cbp) {
442                  BitstreamPutBit(bs, pMB->field_dct);                  BitstreamPutBit(bs, pMB->field_dct);
443                  DEBUG1("codep: field_dct: ", pMB->field_dct);                  DEBUG1("codep: field_dct: ", pMB->field_dct);
444                    }
445    
446                  // if inter block, write field ME flag                  // if inter block, write field ME flag
447                  if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)                  if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
                 {  
448                          BitstreamPutBit(bs, pMB->field_pred);                          BitstreamPutBit(bs, pMB->field_pred);
449                          DEBUG1("codep: field_pred: ", pMB->field_pred);                          DEBUG1("codep: field_pred: ", pMB->field_pred);
450    
451                          // write field prediction references                          // write field prediction references
452                          if (pMB->field_pred)                          if (pMB->field_pred) {
                         {  
453                                  BitstreamPutBit(bs, pMB->field_for_top);                                  BitstreamPutBit(bs, pMB->field_for_top);
454                                  BitstreamPutBit(bs, pMB->field_for_bot);                                  BitstreamPutBit(bs, pMB->field_for_bot);
455                          }                          }
456                  }                  }
457          }          }
458            // code motion vector(s) if motion is local
459          // code motion vector(s)          if (mcsel==0)
460          for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++)                  for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) {
461          {                          CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat);
462                  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);  
463          }          }
464    
465          bits = BitstreamPos(bs);          bits = BitstreamPos(bs);
# Line 400  Line 467 
467          // code block coeffs          // code block coeffs
468          for(i = 0; i < 6; i++)          for(i = 0; i < 6; i++)
469                  if(pMB->cbp & (1 << (5 - i)))                  if(pMB->cbp & (1 << (5 - i)))
470                          CodeCoeff(bs, &qcoeff[i*64], inter_table, scan_tables[0], 0);                  {
471                            const uint16_t *scan_table =
472                                    frame->global_flags & XVID_ALTERNATESCAN ?
473                                    scan_tables[2] : scan_tables[0];
474    
475                            CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_table, 0);
476                    }
477    
478          bits = BitstreamPos(bs) - bits;          bits = BitstreamPos(bs) - bits;
479          pStat->iTextBits += bits;          pStat->iTextBits += bits;
   
480  }  }
481    
482    
483  void MBCoding(const MBParam * pParam,  void
484    MBCoding(const FRAMEINFO * const frame,
485                MACROBLOCK *pMB,                MACROBLOCK *pMB,
486                int16_t qcoeff[6*64],                int16_t qcoeff[6*64],
487                Bitstream * bs,                Bitstream * bs,
488                Statistics * pStat)                Statistics * pStat)
489  {  {
490            if (frame->coding_type != I_VOP)
491                            BitstreamPutBit(bs, 0); // not_coded
492    
493          int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q);          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)
494                    CodeBlockIntra(frame, pMB, qcoeff, bs, pStat);
495            else
496                    CodeBlockInter(frame, pMB, qcoeff, bs, pStat);
497    
498    }
499    
500          if(pParam->coding_type == P_VOP) {  /*
501                  if(pMB->cbp == 0 && pMB->mode == MODE_INTER &&  // moved to mbcoding.h so that in can be 'static __inline'
502                     pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0)  void
503    MBSkip(Bitstream * bs)
504                  {                  {
505                          BitstreamPutBit(bs, 1);         // not_coded          BitstreamPutBit(bs, 1); // not coded
506    }
507    */
508    
509    /***************************************************************
510     * bframe encoding start
511     ***************************************************************/
512    
513    /*
514            mbtype
515            0       1b              direct(h263)            mvdb
516            1       01b             interpolate mc+q        dbquant, mvdf, mvdb
517            2       001b    backward mc+q           dbquant, mvdb
518            3       0001b   forward mc+q            dbquant, mvdf
519    */
520    
521    static __inline void
522    put_bvop_mbtype(Bitstream * bs,
523                                    int value)
524    {
525            switch (value) {
526                    case MODE_FORWARD:
527                            BitstreamPutBit(bs, 0);
528                    case MODE_BACKWARD:
529                            BitstreamPutBit(bs, 0);
530                    case MODE_INTERPOLATE:
531                            BitstreamPutBit(bs, 0);
532                    case MODE_DIRECT:
533                            BitstreamPutBit(bs, 1);
534                    default:
535                            break;
536            }
537    }
538    
539    /*
540            dbquant
541            -2      10b
542            0       0b
543            +2      11b
544    */
545    
546    static __inline void
547    put_bvop_dbquant(Bitstream * bs,
548                                     int value)
549    {
550            switch (value) {
551            case 0:
552                    BitstreamPutBit(bs, 0);
553                          return;                          return;
554    
555            case -2:
556                    BitstreamPutBit(bs, 1);
557                    BitstreamPutBit(bs, 0);
558                    return;
559    
560            case 2:
561                    BitstreamPutBit(bs, 1);
562                    BitstreamPutBit(bs, 1);
563                    return;
564    
565            default:;                                       // invalid
566                  }                  }
                 else  
                         BitstreamPutBit(bs, 0);         // coded  
567          }          }
568    
         if(intra)  
                 CodeBlockIntra(pParam, pMB, qcoeff, bs, pStat);  
         else  
                 CodeBlockInter(pParam, pMB, qcoeff, bs, pStat);  
569    
570    
571    void
572    MBCodingBVOP(const MACROBLOCK * mb,
573                             const int16_t qcoeff[6 * 64],
574                             const int32_t fcode,
575                             const int32_t bcode,
576                             Bitstream * bs,
577                             Statistics * pStat,
578                             int direction)
579    {
580            int vcode = fcode;
581            unsigned int i;
582    
583    /*      ------------------------------------------------------------------
584                    when a block is skipped it is decoded DIRECT(0,0)
585                    hence is interpolated from forward & backward frames
586            ------------------------------------------------------------------ */
587    
588            if (mb->mode == MODE_DIRECT_NONE_MV) {
589                    BitstreamPutBit(bs, 1); // skipped
590                    return;
591            }
592    
593            BitstreamPutBit(bs, 0);         // not skipped
594    
595            if (mb->cbp == 0) {
596                    BitstreamPutBit(bs, 1); // cbp == 0
597            } else {
598                    BitstreamPutBit(bs, 0); // cbp == xxx
599            }
600    
601            put_bvop_mbtype(bs, mb->mode);
602    
603            if (mb->cbp) {
604                    BitstreamPutBits(bs, mb->cbp, 6);
605            }
606    
607            if (mb->mode != MODE_DIRECT && mb->cbp != 0) {
608                    put_bvop_dbquant(bs, 0);        // todo: mb->dquant = 0
609            }
610    
611            switch (mb->mode) {
612                    case MODE_INTERPOLATE:
613                            CodeVector(bs, mb->pmvs[1].x, vcode, pStat); //forward vector of interpolate mode
614                            CodeVector(bs, mb->pmvs[1].y, vcode, pStat);
615                    case MODE_BACKWARD:
616                            vcode = bcode;
617                    case MODE_FORWARD:
618                            CodeVector(bs, mb->pmvs[0].x, vcode, pStat);
619                            CodeVector(bs, mb->pmvs[0].y, vcode, pStat);
620                            break;
621                    case MODE_DIRECT:
622                            CodeVector(bs, mb->pmvs[3].x, 1, pStat);        // fcode is always 1 for delta vector
623                            CodeVector(bs, mb->pmvs[3].y, 1, pStat);        // prediction is always (0,0)
624                    default: break;
625            }
626    
627            for (i = 0; i < 6; i++) {
628                    if (mb->cbp & (1 << (5 - i))) {
629                            CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[direction], 0);
630                    }
631            }
632  }  }
633    
634    
635    
636  /***************************************************************  /***************************************************************
637   * decoding stuff starts here                                  *   * decoding stuff starts here                                  *
638   ***************************************************************/   ***************************************************************/
639    
640  int get_mcbpc_intra(Bitstream * bs)  
641    // for IVOP addbits == 0
642    // for PVOP addbits == fcode - 1
643    // for BVOP addbits == max(fcode,bcode) - 1
644    // returns true or false
645    int
646    check_resync_marker(Bitstream * bs, int addbits)
647    {
648            uint32_t nbits;
649            uint32_t code;
650            uint32_t nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER + addbits;
651    
652            nbits = BitstreamNumBitsToByteAlign(bs);
653            code = BitstreamShowBits(bs, nbits);
654    
655            if (code == (((uint32_t)1 << (nbits - 1)) - 1))
656  {  {
657                    return BitstreamShowBitsFromByteAlign(bs, nbitsresyncmarker) == RESYNC_MARKER;
658            }
659    
660            return 0;
661    }
662    
         uint32_t index;  
663    
         while((index = BitstreamShowBits(bs, 9)) == 1)  
                 BitstreamSkip(bs, 9);  
664    
665    int
666    get_mcbpc_intra(Bitstream * bs)
667    {
668    
669            uint32_t index;
670    
671            index = BitstreamShowBits(bs, 9);
672          index >>= 3;          index >>= 3;
673    
674          BitstreamSkip(bs, mcbpc_intra_table[index].len);          BitstreamSkip(bs, mcbpc_intra_table[index].len);
# Line 456  Line 677 
677    
678  }  }
679    
680  int get_mcbpc_inter(Bitstream * bs)  int
681    get_mcbpc_inter(Bitstream * bs)
682  {  {
683    
684          uint32_t index;          uint32_t index;
685    
686          while((index = CLIP(BitstreamShowBits(bs, 9), 256)) == 1)          index = CLIP(BitstreamShowBits(bs, 9), 256);
                 BitstreamSkip(bs, 9);  
687    
688          BitstreamSkip(bs,  mcbpc_inter_table[index].len);          BitstreamSkip(bs,  mcbpc_inter_table[index].len);
689    
# Line 470  Line 691 
691    
692  }  }
693    
694  int get_cbpy(Bitstream * bs, int intra)  int
695    get_cbpy(Bitstream * bs,
696                     int intra)
697  {  {
698    
699          int cbpy;          int cbpy;
# Line 486  Line 709 
709    
710  }  }
711    
712  int get_mv_data(Bitstream * bs)  static __inline int
713    get_mv_data(Bitstream * bs)
714  {  {
715    
716          uint32_t index;          uint32_t index;
# Line 496  Line 720 
720    
721          index = BitstreamShowBits(bs, 12);          index = BitstreamShowBits(bs, 12);
722    
723          if(index >= 512)          if (index >= 512) {
         {  
724                  index = (index >> 8) - 2;                  index = (index >> 8) - 2;
725                  BitstreamSkip(bs, TMNMVtab0[index].len);                  BitstreamSkip(bs, TMNMVtab0[index].len);
726                  return TMNMVtab0[index].code;                  return TMNMVtab0[index].code;
727          }          }
728    
729          if(index >= 128)          if (index >= 128) {
         {  
730                  index = (index >> 2) - 32;                  index = (index >> 2) - 32;
731                  BitstreamSkip(bs, TMNMVtab1[index].len);                  BitstreamSkip(bs, TMNMVtab1[index].len);
732                  return TMNMVtab1[index].code;                  return TMNMVtab1[index].code;
# Line 517  Line 739 
739    
740  }  }
741    
742  int get_mv(Bitstream * bs, int fcode)  int
743    get_mv(Bitstream * bs,
744               int fcode)
745  {  {
746    
747          int data;          int data;
# Line 537  Line 761 
761    
762  }  }
763    
764  int get_dc_dif(Bitstream * bs, uint32_t dc_size)  int
765    get_dc_dif(Bitstream * bs,
766                       uint32_t dc_size)
767  {  {
768    
769          int code = BitstreamGetBits(bs, dc_size);          int code = BitstreamGetBits(bs, dc_size);
# Line 550  Line 776 
776    
777  }  }
778    
779  int get_dc_size_lum(Bitstream * bs)  int
780    get_dc_size_lum(Bitstream * bs)
781  {  {
782    
783          int code, i;          int code, i;
784    
785          code = BitstreamShowBits(bs, 11);          code = BitstreamShowBits(bs, 11);
786    
787          for(i = 11; i > 3; i--) {          for(i = 11; i > 3; i--) {
# Line 570  Line 798 
798  }  }
799    
800    
801  int get_dc_size_chrom(Bitstream * bs)  int
802    get_dc_size_chrom(Bitstream * bs)
803  {  {
804    
805          uint32_t code, i;          uint32_t code, i;
806    
807          code = BitstreamShowBits(bs, 12);          code = BitstreamShowBits(bs, 12);
808    
809          for(i = 12; i > 2; i--) {          for(i = 12; i > 2; i--) {
# Line 588  Line 818 
818    
819  }  }
820    
821  int get_coeff(Bitstream * bs, int *run, int *last, int intra, int short_video_header)  void
822  {  get_intra_block(Bitstream * bs,
823                                    int16_t * block,
824          uint32_t mode;                                  int direction,
825          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)  
826  {  {
827    
828          const uint16_t * scan = scan_tables[ direction ];          const uint16_t * scan = scan_tables[ direction ];
829          int level;          int level, run, last;
         int run;  
         int last;  
830    
831          do          do {
         {  
832                  level = get_coeff(bs, &run, &last, 1, 0);                  level = get_coeff(bs, &run, &last, 1, 0);
833                  if (run == -1)                  if (run == -1) {
                 {  
834                          DEBUG("fatal: invalid run");                          DEBUG("fatal: invalid run");
835                          break;                          break;
836                  }                  }
837                  coeff += run;                  coeff += run;
838                  block[ scan[coeff] ] = level;                  block[ scan[coeff] ] = level;
839                  if (level < -127 || level > 127)  
840                  {                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level);
841                    //DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[coeff], level, BitstreamShowBits(bs, 32));
842    
843                    if (level < -127 || level > 127) {
844                          DEBUG1("warning: intra_overflow", level);                          DEBUG1("warning: intra_overflow", level);
845                  }                  }
846                  coeff++;                  coeff++;
# Line 709  Line 848 
848    
849  }  }
850    
851  void get_inter_block(Bitstream * bs, int16_t * block)  void
852    get_inter_block(Bitstream * bs,
853                                    int16_t * block,
854                                    int direction)
855  {  {
856    
857          const uint16_t * scan = scan_tables[0];          const uint16_t *scan = scan_tables[direction];
858          int p;          int p;
859          int level;          int level;
860          int run;          int run;
861          int last;          int last;
862    
863          p = 0;          p = 0;
864          do          do {
         {  
865                  level = get_coeff(bs, &run, &last, 0, 0);                  level = get_coeff(bs, &run, &last, 0, 0);
866                  if (run == -1)                  if (run == -1) {
                 {  
867                          DEBUG("fatal: invalid run");                          DEBUG("fatal: invalid run");
868                          break;                          break;
869                  }                  }
870                  p += run;                  p += run;
871    
872                  block[ scan[p] ] = level;                  block[ scan[p] ] = level;
873                  if (level < -127 || level > 127)  
874                  {                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level);
875                    // DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[p], level, BitstreamShowBits(bs, 32));
876    
877                    if (level < -127 || level > 127) {
878                          DEBUG1("warning: inter_overflow", level);                          DEBUG1("warning: inter_overflow", level);
879                  }                  }
880                  p++;                  p++;

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

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