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

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

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

revision 28, Fri Mar 15 09:20:03 2002 UTC revision 347, Sun Jul 28 13:06:46 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.06.2002 added check_resync_marker()                                    *
45      *  14.04.2002 bframe encoding                                                                                            *
46      *  08.03.2002 initial version; isibaar                                                           *
47      *                                                                                                                                                        *
48      ******************************************************************************/
49    
50    
51    
52    #include <stdlib.h>
53  #include "../portab.h"  #include "../portab.h"
54  #include "bitstream.h"  #include "bitstream.h"
55  #include "zigzag.h"  #include "zigzag.h"
56  #include "vlc_codes.h"  #include "vlc_codes.h"
57    #include "mbcoding.h"
58    
59  #include "../utils/mbfunctions.h"  #include "../utils/mbfunctions.h"
60    
 #include <stdlib.h> /* malloc, free */  
   
 #define ESCAPE 7167  
61  #define ABS(X) (((X)>0)?(X):-(X))  #define ABS(X) (((X)>0)?(X):-(X))
62  #define CLIP(X,A) (X > A) ? (A) : (X)  #define CLIP(X,A) (X > A) ? (A) : (X)
63    
64  static VLC *DCT3D[2];  VLC intra_table[524032];
65    VLC inter_table[524032];
66    
67  VLC *intra_table, *inter_table;  VLC DCT3Dintra[4096];
68  static short clip_table[4096];  VLC DCT3Dinter[4096];
69    
70  void create_vlc_tables(void)  void
71    init_vlc_tables(void)
72  {  {
73    
74          int32_t k, l, i, intra, last;          int32_t k, l, i, intra, last;
75          VLC *vlc[2];          VLC *vlc[2];
76          VLC **coeff_ptr;          VLC **coeff_ptr;
77          VLC *vlc1, *vlc2;          VLC *vlc1, *vlc2;
78    
         VLC *DCT3Dintra;  
         VLC *DCT3Dinter;  
   
         DCT3Dintra = (VLC *) malloc(sizeof(VLC) * 4096);  
         DCT3Dinter = (VLC *) malloc(sizeof(VLC) * 4096);  
   
79          vlc1 = DCT3Dintra;          vlc1 = DCT3Dintra;
80          vlc2 = DCT3Dinter;          vlc2 = DCT3Dinter;
81    
82          vlc[0] = intra_table = (VLC *) malloc(128 * 511 * sizeof(VLC));          vlc[0] = intra_table;
83          vlc[1] = inter_table = (VLC *) malloc(128 * 511 * sizeof(VLC));          vlc[1] = inter_table;
84    
85          // initialize the clipping table          // generate encoding vlc lookup tables
86          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  
87          for(i = 0; i < 4; i++) {          for(i = 0; i < 4; i++) {
88                  intra = i % 2;                  intra = i % 2;
89                  last = i >> 1;                  last = i / 2;
90    
91                  coeff_ptr = coeff_vlc[last + (intra << 1)];                  coeff_ptr = coeff_vlc[last + 2 * intra];
92    
93                  for(k = -255; k < 256; k++) { // level                  for (k = -2047; k < 2048; k++) {        // level
94                          char *max_level_ptr = max_level[last + (intra << 1)];                          int8_t *max_level_ptr = max_level[last + 2 * intra];
95                          char *max_run_ptr = max_run[last + (intra << 1)];                          int8_t *max_run_ptr = max_run[last + 2 * intra];
96    
97                          for(l = 0; l < 64; l++) { // run                          for(l = 0; l < 64; l++) { // run
98                                  int32_t level = k, run = l;                                  int32_t level = k;
99                                    ptr_t run = l;
100    
101                                  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
102    
                                         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 {  
103                                                  vlc[intra]->code = 0;                                                  vlc[intra]->code = 0;
104                                                  vlc[intra]->len = 0;                                                  vlc[intra]->len = 0;
105                                          }                                          goto loop_end;
106                                  } else {                                  } else {
107                                          if(level > 0)                                          if (level > 0)  // correct level
108                                                  level -= max_level_ptr[run];                                                  level -= max_level_ptr[run];
109                                          else                                          else
110                                                  level += max_level_ptr[run];                                                  level += max_level_ptr[run];
111    
112                                          if(abs(level) <= max_level_ptr[run] &&                                          if ((abs(level) <= max_level_ptr[run]) &&
113                                                  run <= max_run_ptr[abs(level)]) {                                                  (run <= (uint32_t) max_run_ptr[abs(level)])) {
114    
                                                 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 {  
115                                                          vlc[intra]->code = 0x06;                                                          vlc[intra]->code = 0x06;
116                                                          vlc[intra]->len = 8;                                                          vlc[intra]->len = 8;
117                                                    goto loop_end;
118                                                  }                                                  }
119                                          } else {  
120                                                  if(level > 0)                                          if (level > 0)  // still here?
121                                                          level += max_level_ptr[run];                                                  level += max_level_ptr[run];    // restore level
122                                                  else                                                  else
123                                                          level -= max_level_ptr[run];                                                          level -= max_level_ptr[run];
124                                                  DEBUG1("1) run:", run);  
125                                                  run -= max_run_ptr[abs(level)] + 1;                                          run -= max_run_ptr[abs(level)] + 1;     // and change run
126                                                  DEBUG1("2) run:", run);  
127                                            if ((abs(level) <= max_level_ptr[run]) &&
128                                                  if(abs(level) <= max_level_ptr[run] &&                                                  (run <= (uint32_t) max_run_ptr[abs(level)])) {
129                                                          run <= max_run_ptr[abs(level)]) {  
   
                                                         if(level > 0) {  
                                                                 vlc[intra]->code = (0x0e << (coeff_ptr[run][level - 1].len + 1)) |  
                                                                         (coeff_ptr[run][level - 1].code << 1);  
                                                                 vlc[intra]->len = (coeff_ptr[run][level - 1].len + 1) + 9;  
                                                         }  
                                                         else if(level < 0) {  
                                                                 vlc[intra]->code = (0x0e << (coeff_ptr[run][-level - 1].len + 1)) |  
                                                                         ((coeff_ptr[run][-level - 1].code << 1) + 1);  
                                                                 vlc[intra]->len = (coeff_ptr[run][-level - 1].len + 1) + 9;  
                                                         }  
                                                         else {  
130                                                                  vlc[intra]->code = 0x0e;                                                                  vlc[intra]->code = 0x0e;
131                                                                  vlc[intra]->len = 9;                                                                  vlc[intra]->len = 9;
132                                                    goto loop_end;
133                                                          }                                                          }
                                                 } else {  
                                                         if(level != 0)  
134                                                                  run += max_run_ptr[abs(level)] + 1;                                                                  run += max_run_ptr[abs(level)] + 1;
135                                                          else                                  }
                                                                 run++;  
   
                                                         DEBUG1("3) run:", run);  
136    
137                                                          vlc[intra]->code = (uint32_t) ((0x1e + last) << 20) |                                  vlc[intra]->code =
138                                                                                  (l << 14) | (1 << 13) | ((k & 0xfff) << 1) | 1;                                          (uint32_t) ((l << 14) | (0x1e + last) << 20) | (1 << 13) |
139                                            ((k & 0xfff) << 1) | 1;
140    
141                                                          vlc[intra]->len = 30;                                                          vlc[intra]->len = 30;
142                                    vlc[intra]++;
143                                    continue;
144    
145                              loop_end:
146                                    if (level != 0) {
147                                            vlc[intra]->code =
148                                                    (vlc[intra]->
149                                                     code << (coeff_ptr[run][abs(level) - 1].len +
150                                                                      1)) | (coeff_ptr[run][abs(level) -
151                                                                                                                    1].code << 1);
152                                            vlc[intra]->len =
153                                                    (coeff_ptr[run][abs(level) - 1].len + 1) +
154                                                    vlc[intra]->len;
155    
156                                            if (level < 0)
157                                                    vlc[intra]->code += 1;
158                                                  }                                                  }
159                                          }  
                                 }  
160                                  vlc[intra]++;                                  vlc[intra]++;
161                          }                          }
162                  }                  }
163          }          }
         intra_table += 64*255; // center vlc tables  
         inter_table += 64*255; // center vlc tables  
164    
165          for(i = 0; i < 4096; i++) {          for(i = 0; i < 4096; i++) {
166                  if(i >= 512) {                  if(i >= 512) {
167                          *vlc1 = DCT3Dtab3[(i >> 5) - 16];                          *vlc1 = DCT3Dtab3[(i >> 5) - 16];
168                          *vlc2 = DCT3Dtab0[(i >> 5) - 16];                          *vlc2 = DCT3Dtab0[(i >> 5) - 16];
169                  }                  } else if (i >= 128) {
                 else if(i >= 128) {  
170                          *vlc1 = DCT3Dtab4[(i >> 2) - 32];                          *vlc1 = DCT3Dtab4[(i >> 2) - 32];
171                          *vlc2 = DCT3Dtab1[(i >> 2) - 32];                          *vlc2 = DCT3Dtab1[(i >> 2) - 32];
172                  }                  } else if (i >= 8) {
                 else if(i >= 8) {  
173                          *vlc1 = DCT3Dtab5[i - 8];                          *vlc1 = DCT3Dtab5[i - 8];
174                          *vlc2 = DCT3Dtab2[i - 8];                          *vlc2 = DCT3Dtab2[i - 8];
175                  }                  } else {
                 else {  
176                          *vlc1 = ERRtab[i];                          *vlc1 = ERRtab[i];
177                          *vlc2 = ERRtab[i];                          *vlc2 = ERRtab[i];
178                  }                  }
# Line 169  Line 185 
185    
186  }  }
187    
188  void destroy_vlc_tables(void) {  static __inline void
189    CodeVector(Bitstream * bs,
190          if(intra_table != NULL && inter_table != NULL) {                     int32_t value,
191                  intra_table -= 64*255; // uncenter vlc tables                     int32_t f_code,
192                  inter_table -= 64*255; // uncenter vlc tables                     Statistics * pStat)
   
                 free(intra_table);  
                 free(inter_table);  
         }  
   
         if(DCT3D[0] != NULL && DCT3D[1] != NULL) {  
                 free(DCT3D[0]);  
                 free(DCT3D[1]);  
         }  
   
 }  
   
 static __inline void CodeVector(Bitstream *bs, int16_t value, int16_t f_code, Statistics *pStat)  
193  {  {
194    
195          const int scale_factor = 1 << (f_code - 1);          const int scale_factor = 1 << (f_code - 1);
196          const int cmp = scale_factor << 5;          const int cmp = scale_factor << 5;
197    
# Line 200  Line 204 
204      pStat->iMvSum += value * value;      pStat->iMvSum += value * value;
205      pStat->iMvCount++;      pStat->iMvCount++;
206    
207          if (value == 0)          if (value == 0) {
208                  BitstreamPutBits(bs, mb_motion_table[32].code, mb_motion_table[32].len);                  BitstreamPutBits(bs, mb_motion_table[32].code,
209      else {                                                   mb_motion_table[32].len);
210            } else {
211                  uint16_t length, code, mv_res, sign;                  uint16_t length, code, mv_res, sign;
212    
213                  length = 16 << f_code;                  length = 16 << f_code;
# Line 226  Line 231 
231                          code = -code;                          code = -code;
232    
233                  code += 32;                  code += 32;
234                  BitstreamPutBits(bs, mb_motion_table[code].code, mb_motion_table[code].len);                  BitstreamPutBits(bs, mb_motion_table[code].code,
235                                                     mb_motion_table[code].len);
236    
237                  if(f_code)                  if(f_code)
238                          BitstreamPutBits(bs, mv_res, f_code);                          BitstreamPutBits(bs, mv_res, f_code);
239    }    }
240    
241  }  }
242    
243    
244  static __inline void CodeCoeff(Bitstream *bs, int16_t qcoeff[64], VLC *table,  static __inline void
245                                                             const uint16_t *zigzag, uint16_t intra) {  CodeCoeff(Bitstream * bs,
246                      const int16_t qcoeff[64],
247                      VLC * table,
248                      const uint16_t * zigzag,
249                      uint16_t intra)
250    {
251    
252          uint32_t j, last;          uint32_t j, last;
253          short v;          short v;
254          VLC *vlc;          VLC *vlc;
255    
256          j = intra;          j = intra;
257          last = 1 + intra;          last = intra;
258    
259          while((v = qcoeff[zigzag[j++]]) == 0);          while (j < 64 && (v = qcoeff[zigzag[j]]) == 0)
260                    j++;
261    
262          do {          do {
263                    vlc = table + 64 * 2047 + (v << 6) + j - last;
264                    last = ++j;
265    
266                  // count zeroes                  // count zeroes
267                  vlc = table + (clip_table[2048+v] << 6) + j - last;                  while (j < 64 && (v = qcoeff[zigzag[j]]) == 0)
268                  last = j + 1;                          j++;
                 while(j < 64 && (v = qcoeff[zigzag[j++]]) == 0);  
269    
270                  // write code                  // write code
271                  if(j != 64) {                  if(j != 64) {
272                          BitstreamPutBits(bs, vlc->code, vlc->len);                          BitstreamPutBits(bs, vlc->code, vlc->len);
273                  } else {                  } else {
274                          vlc += 64*511;                          vlc += 64 * 4095;
275                          BitstreamPutBits(bs, vlc->code, vlc->len);                          BitstreamPutBits(bs, vlc->code, vlc->len);
276                          break;                          break;
277                  }                  }
278          } while(1);          } while(1);
279    
280  }  }
281    
282    
283  static void CodeBlockIntra(const MBParam * pParam, const MACROBLOCK *pMB,  static void
284                                                                    int16_t qcoeff[][64], Bitstream * bs, Statistics * pStat)  CodeBlockIntra(const FRAMEINFO * frame,
285                               const MACROBLOCK * pMB,
286                               int16_t qcoeff[6 * 64],
287                               Bitstream * bs,
288                               Statistics * pStat)
289  {  {
290    
291          uint32_t i, mcbpc, cbpy, bits;          uint32_t i, mcbpc, cbpy, bits;
292    
293          cbpy = pMB->cbp >> 2;          cbpy = pMB->cbp >> 2;
294    
295      // write mcbpc      // write mcbpc
296          if(pParam->coding_type == I_VOP) {          if (frame->coding_type == I_VOP) {
297              mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2);              mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2);
298                  BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, mcbpc_intra_tab[mcbpc].len);                  BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code,
299          }                                                   mcbpc_intra_tab[mcbpc].len);
300          else {          } else {
301              mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);              mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);
302                  BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len);                  BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,
303                                                     mcbpc_inter_tab[mcbpc].len);
304          }          }
305    
306          // ac prediction flag          // ac prediction flag
# Line 293  Line 316 
316      if(pMB->mode == MODE_INTRA_Q)      if(pMB->mode == MODE_INTRA_Q)
317                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, pMB->dquant, 2);
318    
319            // write interlacing
320            if (frame->global_flags & XVID_INTERLACING) {
321                    BitstreamPutBit(bs, pMB->field_dct);
322            }
323          // code block coeffs          // code block coeffs
324          for(i = 0; i < 6; i++)          for (i = 0; i < 6; i++) {
         {  
325                  if(i < 4)                  if(i < 4)
326                          BitstreamPutBits(bs, dcy_tab[qcoeff[i][0] + 255].code,                          BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code,
327                                                           dcy_tab[qcoeff[i][0] + 255].len);                                                           dcy_tab[qcoeff[i * 64 + 0] + 255].len);
328                  else                  else
329                          BitstreamPutBits(bs, dcc_tab[qcoeff[i][0] + 255].code,                          BitstreamPutBits(bs, dcc_tab[qcoeff[i * 64 + 0] + 255].code,
330                                           dcc_tab[qcoeff[i][0] + 255].len);                                                           dcc_tab[qcoeff[i * 64 + 0] + 255].len);
331    
332                  if(pMB->cbp & (1 << (5 - i)))                  if (pMB->cbp & (1 << (5 - i))) {
                 {  
333                          bits = BitstreamPos(bs);                          bits = BitstreamPos(bs);
334    
335                          CodeCoeff(bs, qcoeff[i], intra_table, scan_tables[pMB->acpred_directions[i]], 1);                          CodeCoeff(bs, &qcoeff[i * 64], intra_table,
336                                              scan_tables[pMB->acpred_directions[i]], 1);
337    
338                          bits = BitstreamPos(bs) - bits;                          bits = BitstreamPos(bs) - bits;
339                          pStat->iTextBits += bits;                          pStat->iTextBits += bits;
340                  }                  }
341          }          }
342    
343  }  }
344    
345    
346  static void CodeBlockInter(const MBParam * pParam, const MACROBLOCK *pMB,  static void
347                                                                    int16_t qcoeff[][64], Bitstream * bs, Statistics * pStat)  CodeBlockInter(const FRAMEINFO * frame,
348                               const MACROBLOCK * pMB,
349                               int16_t qcoeff[6 * 64],
350                               Bitstream * bs,
351                               Statistics * pStat)
352  {  {
353    
354          int32_t i;          int32_t i;
355          uint32_t bits, mcbpc, cbpy;          uint32_t bits, mcbpc, cbpy;
356    
# Line 326  Line 358 
358          cbpy = 15 - (pMB->cbp >> 2);          cbpy = 15 - (pMB->cbp >> 2);
359    
360          // write mcbpc          // write mcbpc
361      BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len);          BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,
362                                             mcbpc_inter_tab[mcbpc].len);
363    
364          // write cbpy          // write cbpy
365          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);
# Line 335  Line 368 
368      if(pMB->mode == MODE_INTER_Q)      if(pMB->mode == MODE_INTER_Q)
369                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, pMB->dquant, 2);
370    
371            // interlacing
372            if (frame->global_flags & XVID_INTERLACING) {
373                    BitstreamPutBit(bs, pMB->field_dct);
374                    DEBUG1("codep: field_dct: ", pMB->field_dct);
375    
376                    // if inter block, write field ME flag
377                    if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
378                            BitstreamPutBit(bs, pMB->field_pred);
379                            DEBUG1("codep: field_pred: ", pMB->field_pred);
380    
381                            // write field prediction references
382                            if (pMB->field_pred) {
383                                    BitstreamPutBit(bs, pMB->field_for_top);
384                                    BitstreamPutBit(bs, pMB->field_for_bot);
385                            }
386                    }
387            }
388          // code motion vector(s)          // code motion vector(s)
389          for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++)          for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) {
390          {                  CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat);
391                  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);  
392          }          }
393    
394          bits = BitstreamPos(bs);          bits = BitstreamPos(bs);
# Line 347  Line 396 
396          // code block coeffs          // code block coeffs
397          for(i = 0; i < 6; i++)          for(i = 0; i < 6; i++)
398                  if(pMB->cbp & (1 << (5 - i)))                  if(pMB->cbp & (1 << (5 - i)))
399                          CodeCoeff(bs, qcoeff[i], inter_table, scan_tables[0], 0);                          CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0);
400    
401          bits = BitstreamPos(bs) - bits;          bits = BitstreamPos(bs) - bits;
402          pStat->iTextBits += bits;          pStat->iTextBits += bits;
403    
404  }  }
405    
406    
407  void MBCoding(const MBParam * pParam, MACROBLOCK *pMB,  void
408                int16_t qcoeff[][64],  MBCoding(const FRAMEINFO * frame,
409                    Bitstream * bs, Statistics * pStat)                   MACROBLOCK * pMB,
410                     int16_t qcoeff[6 * 64],
411                     Bitstream * bs,
412                     Statistics * pStat)
413  {  {
         int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q);  
414    
415      if(pParam->coding_type == P_VOP) {          if (frame->coding_type == P_VOP) {
416                  if(pMB->cbp == 0 && pMB->mode == MODE_INTER &&                          BitstreamPutBit(bs, 0); // coded
417                          pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0)          }
418    
419            if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)
420                    CodeBlockIntra(frame, pMB, qcoeff, bs, pStat);
421            else
422                    CodeBlockInter(frame, pMB, qcoeff, bs, pStat);
423    
424    }
425    
426    
427    void
428    MBSkip(Bitstream * bs)
429                  {                  {
430                          BitstreamPutBit(bs, 1);         // not_coded          BitstreamPutBit(bs, 1); // not coded
431                          return;                          return;
432                  }                  }
433                  else  
434                          BitstreamPutBit(bs, 0);         // coded  
435    /***************************************************************
436     * bframe encoding start
437     ***************************************************************/
438    
439    /*
440            mbtype
441            0       1b              direct(h263)            mvdb
442            1       01b             interpolate mc+q        dbquant, mvdf, mvdb
443            2       001b    backward mc+q           dbquant, mvdb
444            3       0001b   forward mc+q            dbquant, mvdf
445    */
446    
447    void
448    put_bvop_mbtype(Bitstream * bs,
449                                    int value)
450    {
451            switch (value) {
452            case 0:
453                    BitstreamPutBit(bs, 1);
454                    return;
455    
456            case 1:
457                    BitstreamPutBit(bs, 0);
458                    BitstreamPutBit(bs, 1);
459                    return;
460    
461            case 2:
462                    BitstreamPutBit(bs, 0);
463                    BitstreamPutBit(bs, 0);
464                    BitstreamPutBit(bs, 1);
465                    return;
466    
467            case 3:
468                    BitstreamPutBit(bs, 0);
469                    BitstreamPutBit(bs, 0);
470                    BitstreamPutBit(bs, 0);
471                    BitstreamPutBit(bs, 1);
472                    return;
473    
474            default:;                                       // invalid!
475    
476          }          }
477    
         if(intra)  
                 CodeBlockIntra(pParam, pMB, qcoeff, bs, pStat);  
         else  
                 CodeBlockInter(pParam, pMB, qcoeff, bs, pStat);  
478  }  }
479    
480    /*
481            dbquant
482            -2      10b
483            0       0b
484            +2      11b
485    */
486    
487    void
488    put_bvop_dbquant(Bitstream * bs,
489                                     int value)
490    {
491            switch (value) {
492            case 0:
493                    BitstreamPutBit(bs, 0);
494                    return;
495    
496            case -2:
497                    BitstreamPutBit(bs, 1);
498                    BitstreamPutBit(bs, 0);
499                    return;
500    
501            case 2:
502                    BitstreamPutBit(bs, 1);
503                    BitstreamPutBit(bs, 1);
504                    return;
505    
506            default:;                                       // invalid
507            }
508    }
509    
510    
511    
512    void
513    MBCodingBVOP(const MACROBLOCK * mb,
514                             const int16_t qcoeff[6 * 64],
515                             const int32_t fcode,
516                             const int32_t bcode,
517                             Bitstream * bs,
518                             Statistics * pStat)
519    {
520            int i;
521    
522    /*      ------------------------------------------------------------------
523                    when a block is skipped it is decoded DIRECT(0,0)
524                    hence is interpolated from forward & backward frames
525            ------------------------------------------------------------------ */
526    
527            if (mb->mode == MODE_DIRECT_NONE_MV) {
528                    BitstreamPutBit(bs, 1); // skipped
529                    return;
530            }
531    
532            BitstreamPutBit(bs, 0);         // not skipped
533    
534            if (mb->cbp == 0) {
535                    BitstreamPutBit(bs, 1); // cbp == 0
536            } else {
537                    BitstreamPutBit(bs, 0); // cbp == xxx
538            }
539    
540            put_bvop_mbtype(bs, mb->mode);
541    
542            if (mb->cbp) {
543                    BitstreamPutBits(bs, mb->cbp, 6);
544            }
545    
546            if (mb->mode != MODE_DIRECT && mb->cbp != 0) {
547                    put_bvop_dbquant(bs, 0);        // todo: mb->dquant = 0
548            }
549    
550            if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) {
551                    CodeVector(bs, mb->pmvs[0].x, fcode, pStat);
552                    CodeVector(bs, mb->pmvs[0].y, fcode, pStat);
553            }
554    
555            if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) {
556                    CodeVector(bs, mb->b_pmvs[0].x, bcode, pStat);
557                    CodeVector(bs, mb->b_pmvs[0].y, bcode, pStat);
558            }
559    
560            if (mb->mode == MODE_DIRECT) {
561                    CodeVector(bs, mb->deltamv.x, 1, pStat);                /* fcode is always 1 for delta vector */
562                    CodeVector(bs, mb->deltamv.y, 1, pStat);                /* prediction is always (0,0) */
563            }
564    
565            for (i = 0; i < 6; i++) {
566                    if (mb->cbp & (1 << (5 - i))) {
567                            CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0);
568                    }
569            }
570    }
571    
572    
573    
574  /***************************************************************  /***************************************************************
575   * decoding stuff starts here                                  *   * decoding stuff starts here                                  *
576   ***************************************************************/   ***************************************************************/
577    
578  int get_mcbpc_intra(Bitstream * bs)  
579    // for IVOP addbits == 0
580    // for PVOP addbits == fcode - 1
581    // for BVOP addbits == max(fcode,bcode) - 1
582    // returns true or false
583    int
584    check_resync_marker(Bitstream * bs, int addbits)
585    {
586            uint32_t nbits;
587            uint32_t code;
588            uint32_t nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER + addbits;
589    
590            nbits = BitstreamNumBitsToByteAlign(bs);
591            code = BitstreamShowBits(bs, nbits);
592    
593            if (code == (((uint32_t)1 << (nbits - 1)) - 1))
594  {  {
595          uint32_t index;                  return BitstreamShowBitsFromByteAlign(bs, nbitsresyncmarker) == RESYNC_MARKER;
596            }
597    
598            return 0;
599    }
600    
601    
         while((index = BitstreamShowBits(bs, 9)) == 1)  
                 BitstreamSkip(bs, 9);  
602    
603    int
604    get_mcbpc_intra(Bitstream * bs)
605    {
606    
607            uint32_t index;
608    
609            index = BitstreamShowBits(bs, 9);
610          index >>= 3;          index >>= 3;
611    
612          BitstreamSkip(bs, mcbpc_intra_table[index].len);          BitstreamSkip(bs, mcbpc_intra_table[index].len);
613    
614          return mcbpc_intra_table[index].code;          return mcbpc_intra_table[index].code;
615    
616  }  }
617    
618  int get_mcbpc_inter(Bitstream * bs)  int
619    get_mcbpc_inter(Bitstream * bs)
620  {  {
621    
622          uint32_t index;          uint32_t index;
623    
624          while((index = CLIP(BitstreamShowBits(bs, 9), 256)) == 1)          index = CLIP(BitstreamShowBits(bs, 9), 256);
                 BitstreamSkip(bs, 9);  
625    
626      BitstreamSkip(bs,  mcbpc_inter_table[index].len);      BitstreamSkip(bs,  mcbpc_inter_table[index].len);
627    
628          return mcbpc_inter_table[index].code;          return mcbpc_inter_table[index].code;
629    
630  }  }
631    
632  int get_cbpy(Bitstream * bs, int intra)  int
633    get_cbpy(Bitstream * bs,
634                     int intra)
635  {  {
636    
637          int cbpy;          int cbpy;
638          uint32_t index = BitstreamShowBits(bs, 6);          uint32_t index = BitstreamShowBits(bs, 6);
639    
# Line 418  Line 644 
644                  cbpy = 15 - cbpy;                  cbpy = 15 - cbpy;
645    
646          return cbpy;          return cbpy;
647    
648  }  }
649    
650  int get_mv_data(Bitstream * bs)  int
651    get_mv_data(Bitstream * bs)
652  {  {
653    
654          uint32_t index;          uint32_t index;
655    
656          if(BitstreamGetBit(bs))          if(BitstreamGetBit(bs))
# Line 429  Line 658 
658    
659          index = BitstreamShowBits(bs, 12);          index = BitstreamShowBits(bs, 12);
660    
661          if(index >= 512)          if (index >= 512) {
         {  
662                  index = (index >> 8) - 2;                  index = (index >> 8) - 2;
663                  BitstreamSkip(bs, TMNMVtab0[index].len);                  BitstreamSkip(bs, TMNMVtab0[index].len);
664                  return TMNMVtab0[index].code;                  return TMNMVtab0[index].code;
665          }          }
666    
667          if(index >= 128)          if (index >= 128) {
         {  
668                  index = (index >> 2) - 32;                  index = (index >> 2) - 32;
669                  BitstreamSkip(bs, TMNMVtab1[index].len);                  BitstreamSkip(bs, TMNMVtab1[index].len);
670                  return TMNMVtab1[index].code;                  return TMNMVtab1[index].code;
# Line 447  Line 674 
674    
675          BitstreamSkip(bs, TMNMVtab2[index].len);          BitstreamSkip(bs, TMNMVtab2[index].len);
676          return TMNMVtab2[index].code;          return TMNMVtab2[index].code;
677    
678  }  }
679    
680  int get_mv(Bitstream * bs, int fcode)  int
681    get_mv(Bitstream * bs,
682               int fcode)
683  {  {
684    
685          int data;          int data;
686          int res;          int res;
687          int mv;          int mv;
# Line 465  Line 696 
696          mv = ((ABS(data) - 1) * scale_fac) + res + 1;          mv = ((ABS(data) - 1) * scale_fac) + res + 1;
697    
698          return data < 0 ? -mv : mv;          return data < 0 ? -mv : mv;
699    
700  }  }
701    
702  int get_dc_dif(Bitstream * bs, uint32_t dc_size)  int
703    get_dc_dif(Bitstream * bs,
704                       uint32_t dc_size)
705  {  {
706    
707          int code = BitstreamGetBits(bs, dc_size);          int code = BitstreamGetBits(bs, dc_size);
708          int msb = code >> (dc_size - 1);          int msb = code >> (dc_size - 1);
709    
# Line 476  Line 711 
711                  return (-1 * (code^((1 << dc_size) - 1)));                  return (-1 * (code^((1 << dc_size) - 1)));
712    
713          return code;          return code;
714    
715  }  }
716    
717  int get_dc_size_lum(Bitstream * bs)  int
718    get_dc_size_lum(Bitstream * bs)
719  {  {
720    
721          int code, i;          int code, i;
722    
723          code = BitstreamShowBits(bs, 11);          code = BitstreamShowBits(bs, 11);
724    
725          for(i = 11; i > 3; i--) {          for(i = 11; i > 3; i--) {
# Line 493  Line 732 
732    
733          BitstreamSkip(bs, dc_lum_tab[code].len);          BitstreamSkip(bs, dc_lum_tab[code].len);
734          return dc_lum_tab[code].code;          return dc_lum_tab[code].code;
735    
736  }  }
737    
738    
739  int get_dc_size_chrom(Bitstream * bs)  int
740    get_dc_size_chrom(Bitstream * bs)
741  {  {
742    
743          uint32_t code, i;          uint32_t code, i;
744    
745          code = BitstreamShowBits(bs, 12);          code = BitstreamShowBits(bs, 12);
746    
747          for(i = 12; i > 2; i--) {          for(i = 12; i > 2; i--) {
# Line 510  Line 753 
753          }          }
754    
755          return 3 - BitstreamGetBits(bs, 2);          return 3 - BitstreamGetBits(bs, 2);
 }  
   
 int get_coeff(Bitstream * bs, int *run, int *last, int intra, int short_video_header)  
 {  
     uint32_t mode;  
     const VLC *tab;  
         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)];  
756    
         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;  
757          }          }
758    
759          if(short_video_header)  void
760    get_intra_block(Bitstream * bs,
761                                    int16_t * block,
762                                    int direction,
763                                    int coeff)
764          {          {
                 // 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;  
                 }  
765    
                 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)  
 {  
766          const uint16_t * scan = scan_tables[ direction ];          const uint16_t * scan = scan_tables[ direction ];
767          int level;          int level;
768          int run;          int run;
769          int last;          int last;
770    
771          do          do {
         {  
772                  level = get_coeff(bs, &run, &last, 1, 0);                  level = get_coeff(bs, &run, &last, 1, 0);
773                  if (run == -1)                  if (run == -1) {
                 {  
774                          DEBUG("fatal: invalid run");                          DEBUG("fatal: invalid run");
775                          break;                          break;
776                  }                  }
777                  coeff += run;                  coeff += run;
778                  block[ scan[coeff] ] = level;                  block[ scan[coeff] ] = level;
779                  if (level < -127 || level > 127)  
780                  {                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level);
781                    //DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[coeff], level, BitstreamShowBits(bs, 32));
782    
783                    if (level < -127 || level > 127) {
784                          DEBUG1("warning: intra_overflow", level);                          DEBUG1("warning: intra_overflow", level);
785                  }                  }
786                  coeff++;                  coeff++;
787          } while (!last);          } while (!last);
788    
789  }  }
790    
791  void get_inter_block(Bitstream * bs, int16_t * block)  void
792    get_inter_block(Bitstream * bs,
793                                    int16_t * block)
794  {  {
795    
796          const uint16_t * scan = scan_tables[0];          const uint16_t * scan = scan_tables[0];
797          int p;          int p;
798          int level;          int level;
# Line 638  Line 800 
800          int last;          int last;
801    
802          p = 0;          p = 0;
803          do          do {
         {  
804                  level = get_coeff(bs, &run, &last, 0, 0);                  level = get_coeff(bs, &run, &last, 0, 0);
805                  if (run == -1)                  if (run == -1) {
                 {  
806                          DEBUG("fatal: invalid run");                          DEBUG("fatal: invalid run");
807                          break;                          break;
808                  }                  }
809                  p += run;                  p += run;
810    
811                  block[ scan[p] ] = level;                  block[ scan[p] ] = level;
812                  if (level < -127 || level > 127)  
813                  {                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level);
814                    // DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[p], level, BitstreamShowBits(bs, 32));
815    
816                    if (level < -127 || level > 127) {
817                          DEBUG1("warning: inter_overflow", level);                          DEBUG1("warning: inter_overflow", level);
818                  }                  }
819                  p++;                  p++;
820          } while (!last);          } while (!last);
821    
822  }  }

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

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