[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 152, Wed May 1 13:00:02 2002 UTC branches/dev-api-3/xvidcore/src/bitstream/mbcoding.c revision 543, Thu Sep 26 01:54:54 2002 UTC
# Line 29  Line 29 
29    
30   /******************************************************************************   /******************************************************************************
31    *                                                                            *    *                                                                            *
32    *  bitstream.c                                                               *    *  mbcoding.c                                                                *
33    *                                                                            *    *                                                                            *
34    *  Copyright (C) 2001 - Peter Ross <pross@cs.rmit.edu.au>                    *    *  Copyright (C) 2002 - Michael Militzer <isibaar@xvid.org>                  *
35    *                                                                            *    *                                                                            *
36    *  For more information visit the XviD homepage: http://www.xvid.org         *    *  For more information visit the XviD homepage: http://www.xvid.org         *
37    *                                                                            *    *                                                                            *
# Line 41  Line 41 
41    *                                                                                                                                                        *    *                                                                                                                                                        *
42    *  Revision history:                                                         *    *  Revision history:                                                         *
43    *                                                                            *    *                                                                            *
44    *  14.04.2002 bframe encoding    *  28.06.2002 added check_resync_marker()                                    *
45      *  14.04.2002 bframe encoding                                                                                            *
46    *  08.03.2002 initial version; isibaar                                                           *    *  08.03.2002 initial version; isibaar                                                           *
47    *                                                                                                                                                        *    *                                                                                                                                                        *
48    ******************************************************************************/    ******************************************************************************/
# Line 60  Line 61 
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  VLC intra_table[65536];  VLC intra_table[524032];
65  VLC inter_table[65536];  VLC inter_table[524032];
66    
67  VLC DCT3Dintra[4096];  VLC DCT3Dintra[4096];
68  VLC DCT3Dinter[4096];  VLC DCT3Dinter[4096];
69    
70  static int16_t clip_table[4096];  void
71    init_vlc_tables(void)
 void init_vlc_tables(void)  
72  {  {
73    
74          int32_t k, l, i, intra, last;          int32_t k, l, i, intra, last;
# Line 82  Line 82 
82          vlc[0] = intra_table;          vlc[0] = intra_table;
83          vlc[1] = inter_table;          vlc[1] = inter_table;
84    
         // 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;  
         }  
   
85          // generate encoding vlc lookup tables          // generate encoding vlc lookup tables
86            // the lookup table idea is taken from the excellent fame project by Vivien Chapellier
87          for(i = 0; i < 4; i++) {          for(i = 0; i < 4; i++) {
88                  intra = i % 2;                  intra = i % 2;
89                  last = i / 2;                  last = i / 2;
90    
91                  coeff_ptr = coeff_vlc[last + 2 * intra];                  coeff_ptr = coeff_vlc[last + 2 * intra];
92    
93                  for(k = -255; k < 256; k++) { // level                  for (k = -2047; k < 2048; k++) {        // level
94                          int8_t *max_level_ptr = max_level[last + 2 * intra];                          int8_t *max_level_ptr = max_level[last + 2 * intra];
95                          int8_t *max_run_ptr = max_run[last + 2 * intra];                          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;                                  int32_t level = k;
99                                  uint32_t run = l;                                  ptr_t run = l;
100    
101                                  if((abs(level) <= max_level_ptr[run]) &&                                  if ((abs(level) <= max_level_ptr[run]) && (run <= (uint32_t) max_run_ptr[abs(level)])) {        // level < max_level and run < max_run
                                    (run <= (uint32_t)max_run_ptr[abs(level)])) { // level < max_level and run < max_run  
102    
103                                                  vlc[intra]->code = 0;                                                  vlc[intra]->code = 0;
104                                                  vlc[intra]->len = 0;                                                  vlc[intra]->len = 0;
105                                                  goto loop_end;                                                  goto loop_end;
106                                  }                                  } else {
                                 else {  
107                                          if(level > 0)                                        // correct level                                          if(level > 0)                                        // correct level
108                                                  level -= max_level_ptr[run];                                                  level -= max_level_ptr[run];
109                                          else                                          else
# Line 144  Line 134 
134                                          run += max_run_ptr[abs(level)] + 1;                                          run += max_run_ptr[abs(level)] + 1;
135                                  }                                  }
136    
137                                  vlc[intra]->code = (uint32_t) ((l << 14) | (0x1e + last) << 20) |                                  vlc[intra]->code =
138                                                                                            (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]++;                                  vlc[intra]++;
# Line 153  Line 144 
144    
145  loop_end:  loop_end:
146                                  if(level != 0) {                                  if(level != 0) {
147                                          vlc[intra]->code = (vlc[intra]->code << (coeff_ptr[run][abs(level) - 1].len + 1)) |                                          vlc[intra]->code =
148                                                                             (coeff_ptr[run][abs(level) - 1].code << 1);                                                  (vlc[intra]->
149                                          vlc[intra]->len = (coeff_ptr[run][abs(level) - 1].len + 1) + vlc[intra]->len;                                                   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)                                          if(level < 0)
157                                                  vlc[intra]->code += 1;                                                  vlc[intra]->code += 1;
# Line 170  Line 166 
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 192  Line 185 
185    
186  }  }
187    
188  static __inline void CodeVector(Bitstream *bs,  static __inline void
189    CodeVector(Bitstream * bs,
190                                  int32_t value,                                  int32_t value,
191                                  int32_t f_code,                                  int32_t f_code,
192                                  Statistics *pStat)                                  Statistics *pStat)
# Line 211  Line 205 
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                                                     mb_motion_table[32].len);
210          } else {          } else {
211                  uint16_t length, code, mv_res, sign;                  uint16_t length, code, mv_res, sign;
212    
# Line 236  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);
# Line 245  Line 241 
241  }  }
242    
243    
244  static __inline void CodeCoeff(Bitstream *bs,  static __inline void
245    CodeCoeff(Bitstream * bs,
246                                 const int16_t qcoeff[64],                                 const int16_t qcoeff[64],
247                                 VLC *table,                                 VLC *table,
248                                 const uint16_t *zigzag,                                 const uint16_t *zigzag,
# Line 259  Line 256 
256          j = intra;          j = intra;
257          last = intra;          last = intra;
258    
259          while((v = qcoeff[zigzag[j]]) == 0) j++;          while (j < 64 && (v = qcoeff[zigzag[j]]) == 0)
260                    j++;
261    
262          do {          do {
263                  // count zeroes                  vlc = table + 64 * 2047 + (v << 6) + j - last;
                 vlc = table + 64*255 + (clip_table[2048+v] << 6) + j - last;  
264                  last = ++j;                  last = ++j;
265                  while(j < 64 && (v = qcoeff[zigzag[j]]) == 0) j++;  
266                    // count zeroes
267                    while (j < 64 && (v = qcoeff[zigzag[j]]) == 0)
268                            j++;
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                  }                  }
# Line 280  Line 280 
280  }  }
281    
282    
283  static void CodeBlockIntra(const FRAMEINFO * frame,  static __inline void
284    CodeBlockIntra(const FRAMEINFO * frame,
285                             const MACROBLOCK *pMB,                             const MACROBLOCK *pMB,
286                             int16_t qcoeff[6*64],                             int16_t qcoeff[6*64],
287                             Bitstream * bs,                             Bitstream * bs,
# Line 294  Line 295 
295          // write mcbpc          // write mcbpc
296          if(frame->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 315  Line 317 
317                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, pMB->dquant, 2);
318    
319          // write interlacing          // write interlacing
320          if (frame->global_flags & XVID_INTERLACING)          if (frame->global_flags & XVID_INTERLACING) {
         {  
321                  BitstreamPutBit(bs, pMB->field_dct);                  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,                          BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code,
                                          dcy_tab[qcoeff[i*64 + 0] + 255].code,  
327                                           dcy_tab[qcoeff[i*64 + 0] + 255].len);                                           dcy_tab[qcoeff[i*64 + 0] + 255].len);
328                  else                  else
329                          BitstreamPutBits(bs,                          BitstreamPutBits(bs, dcc_tab[qcoeff[i * 64 + 0] + 255].code,
                                          dcc_tab[qcoeff[i*64 + 0] + 255].code,  
330                                           dcc_tab[qcoeff[i*64 + 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                  {                          const uint16_t *scan_table =
334                                    frame->global_flags & XVID_ALTERNATESCAN ?
335                                    scan_tables[2] : scan_tables[pMB->acpred_directions[i]];
336    
337                          bits = BitstreamPos(bs);                          bits = BitstreamPos(bs);
338    
339                          CodeCoeff(bs,                          CodeCoeff(bs, &qcoeff[i * 64], intra_table, scan_table, 1);
                                   &qcoeff[i*64],  
                                   intra_table,  
                                   scan_tables[pMB->acpred_directions[i]],  
                                   1);  
340    
341                          bits = BitstreamPos(bs) - bits;                          bits = BitstreamPos(bs) - bits;
342                          pStat->iTextBits += bits;                          pStat->iTextBits += bits;
# Line 350  Line 346 
346  }  }
347    
348    
349  static void CodeBlockInter(const FRAMEINFO * frame,  static void
350    CodeBlockInter(const FRAMEINFO * frame,
351                             const MACROBLOCK *pMB,                             const MACROBLOCK *pMB,
352                             int16_t qcoeff[6*64],                             int16_t qcoeff[6*64],
353                             Bitstream * bs,                             Bitstream * bs,
# Line 364  Line 361 
361          cbpy = 15 - (pMB->cbp >> 2);          cbpy = 15 - (pMB->cbp >> 2);
362    
363          // write mcbpc          // write mcbpc
364          BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len);          BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,
365                                             mcbpc_inter_tab[mcbpc].len);
366    
367          // write cbpy          // write cbpy
368          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);
# Line 374  Line 372 
372                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, pMB->dquant, 2);
373    
374          // interlacing          // interlacing
375          if (frame->global_flags & XVID_INTERLACING)          if (frame->global_flags & XVID_INTERLACING) {
376          {                  if (pMB->cbp) {
377                  BitstreamPutBit(bs, pMB->field_dct);                  BitstreamPutBit(bs, pMB->field_dct);
378                  DEBUG1("codep: field_dct: ", pMB->field_dct);                  DEBUG1("codep: field_dct: ", pMB->field_dct);
379                    }
380    
381                  // if inter block, write field ME flag                  // if inter block, write field ME flag
382                  if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)                  if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
                 {  
383                          BitstreamPutBit(bs, pMB->field_pred);                          BitstreamPutBit(bs, pMB->field_pred);
384                          DEBUG1("codep: field_pred: ", pMB->field_pred);                          DEBUG1("codep: field_pred: ", pMB->field_pred);
385    
386                          // write field prediction references                          // write field prediction references
387                          if (pMB->field_pred)                          if (pMB->field_pred) {
                         {  
388                                  BitstreamPutBit(bs, pMB->field_for_top);                                  BitstreamPutBit(bs, pMB->field_for_top);
389                                  BitstreamPutBit(bs, pMB->field_for_bot);                                  BitstreamPutBit(bs, pMB->field_for_bot);
390                          }                          }
391                  }                  }
392          }          }
   
393          // code motion vector(s)          // code motion vector(s)
394          for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++)          for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) {
         {  
395                  CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat);                  CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat);
396                  CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat);                  CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat);
397          }          }
# Line 406  Line 401 
401          // code block coeffs          // code block coeffs
402          for(i = 0; i < 6; i++)          for(i = 0; i < 6; i++)
403                  if(pMB->cbp & (1 << (5 - i)))                  if(pMB->cbp & (1 << (5 - i)))
404                          CodeCoeff(bs, &qcoeff[i*64], inter_table, scan_tables[0], 0);                  {
405                            const uint16_t *scan_table =
406                                    frame->global_flags & XVID_ALTERNATESCAN ?
407                                    scan_tables[2] : scan_tables[0];
408    
409                            CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_table, 0);
410                    }
411    
412          bits = BitstreamPos(bs) - bits;          bits = BitstreamPos(bs) - bits;
413          pStat->iTextBits += bits;          pStat->iTextBits += bits;
# Line 414  Line 415 
415  }  }
416    
417    
418  void MBCoding(const FRAMEINFO * frame,  void
419    MBCoding(const FRAMEINFO * frame,
420                MACROBLOCK *pMB,                MACROBLOCK *pMB,
421                int16_t qcoeff[6*64],                int16_t qcoeff[6*64],
422                Bitstream * bs,                Bitstream * bs,
423                Statistics * pStat)                Statistics * pStat)
424  {  {
425    
         int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q);  
   
426          if(frame->coding_type == P_VOP) {          if(frame->coding_type == P_VOP) {
                 if(pMB->cbp == 0 && pMB->mode == MODE_INTER &&  
                    pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0)  
                 {  
                         BitstreamPutBit(bs, 1);         // not_coded  
                         return;  
                 }  
                 else  
427                          BitstreamPutBit(bs, 0);         // coded                          BitstreamPutBit(bs, 0);         // coded
428          }          }
429    
430          if(intra)          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)
431                  CodeBlockIntra(frame, pMB, qcoeff, bs, pStat);                  CodeBlockIntra(frame, pMB, qcoeff, bs, pStat);
432          else          else
433                  CodeBlockInter(frame, pMB, qcoeff, bs, pStat);                  CodeBlockInter(frame, pMB, qcoeff, bs, pStat);
434    
435  }  }
436    
437    /*
438    // moved to mbcoding.h so that in can be 'static __inline'
439    void
440    MBSkip(Bitstream * bs)
441    {
442            BitstreamPutBit(bs, 1); // not coded
443    }
444    */
445    
446  /***************************************************************  /***************************************************************
447   * bframe encoding start   * bframe encoding start
448   ***************************************************************/   ***************************************************************/
# Line 453  Line 455 
455          3       0001b   forward mc+q            dbquant, mvdf          3       0001b   forward mc+q            dbquant, mvdf
456  */  */
457    
458  void put_bvop_mbtype(Bitstream * bs, int value)  static __inline void
459  {  put_bvop_mbtype(Bitstream * bs,
460          switch(value)                                  int value)
461          {          {
462          case 0 :        BitstreamPutBit(bs, 1);          switch (value) {
463                                  return;                  case MODE_FORWARD:
   
         case 1 :        BitstreamPutBit(bs, 0);  
                                 BitstreamPutBit(bs, 1);  
                                 return;  
   
         case 2 :        BitstreamPutBit(bs, 0);  
464                                  BitstreamPutBit(bs, 0);                                  BitstreamPutBit(bs, 0);
465                                  BitstreamPutBit(bs, 1);                  case MODE_BACKWARD:
                                 return;  
   
         case 3 :        BitstreamPutBit(bs, 0);  
466                                  BitstreamPutBit(bs, 0);                                  BitstreamPutBit(bs, 0);
467                    case MODE_INTERPOLATE:
468                                  BitstreamPutBit(bs, 0);                                  BitstreamPutBit(bs, 0);
469                    case MODE_DIRECT:
470                                  BitstreamPutBit(bs, 1);                                  BitstreamPutBit(bs, 1);
471                                  return;                  default:
472                            break;
         default :       ; // invalid!  
   
473          }          }
   
474  }  }
475    
476  /*  /*
# Line 488  Line 480 
480          +2      11b          +2      11b
481  */  */
482    
483  void put_bvop_dbquant(Bitstream *bs, int value)  static __inline void
484  {  put_bvop_dbquant(Bitstream * bs,
485          switch (value)                                   int value)
486          {          {
487          case 0 :        BitstreamPutBit(bs, 0);          switch (value) {
488            case 0:
489                    BitstreamPutBit(bs, 0);
490                                  return;                                  return;
491    
492          case -2 :       BitstreamPutBit(bs, 1);          case -2:
493                    BitstreamPutBit(bs, 1);
494                                  BitstreamPutBit(bs, 0);                                  BitstreamPutBit(bs, 0);
495                                  return;                                  return;
496    
497          case 2 :        BitstreamPutBit(bs, 1);          case 2:
498                    BitstreamPutBit(bs, 1);
499                                  BitstreamPutBit(bs, 1);                                  BitstreamPutBit(bs, 1);
500                                  return;                                  return;
501    
# Line 509  Line 505 
505    
506    
507    
508  void MBCodingBVOP(const MACROBLOCK * mb,  void
509    MBCodingBVOP(const MACROBLOCK * mb,
510                                    const int16_t qcoeff[6*64],                                    const int16_t qcoeff[6*64],
511                                    const int32_t fcode,                                    const int32_t fcode,
512                                    const int32_t bcode,                                    const int32_t bcode,
513                                    Bitstream * bs,                                    Bitstream * bs,
514                                    Statistics * pStat)                           Statistics * pStat,
515                             int direction)
516  {  {
517          int i;          int vcode = fcode;
518            unsigned int i;
519    
520  /*      ------------------------------------------------------------------  /*      ------------------------------------------------------------------
521                  when a block is skipped it is decoded DIRECT(0,)                  when a block is skipped it is decoded DIRECT(0,0)
522                  hence are interpolated from forward & backward frames                  hence is interpolated from forward & backward frames
523          ------------------------------------------------------------------ */          ------------------------------------------------------------------ */
524    
525          if (mb->mode == 5)          if (mb->mode == MODE_DIRECT_NONE_MV) {
         {  
526                  BitstreamPutBit(bs, 1);         // skipped                  BitstreamPutBit(bs, 1);         // skipped
527                  return;                  return;
528          }          }
529    
530          BitstreamPutBit(bs, 0);         // not skipped          BitstreamPutBit(bs, 0);         // not skipped
531    
532          if (mb->cbp == 0)          if (mb->cbp == 0) {
         {  
533                  BitstreamPutBit(bs, 1);         // cbp == 0                  BitstreamPutBit(bs, 1);         // cbp == 0
534          }          } else {
         else  
         {  
535                  BitstreamPutBit(bs, 0);         // cbp == xxx                  BitstreamPutBit(bs, 0);         // cbp == xxx
536          }          }
537    
538          put_bvop_mbtype(bs, mb->mode);          put_bvop_mbtype(bs, mb->mode);
539    
540          if (mb->cbp)          if (mb->cbp) {
         {  
541                  BitstreamPutBits(bs, mb->cbp, 6);                  BitstreamPutBits(bs, mb->cbp, 6);
542          }          }
543    
544          if (mb->mode != MODE_DIRECT && mb->cbp != 0)          if (mb->mode != MODE_DIRECT && mb->cbp != 0) {
         {  
545                  put_bvop_dbquant(bs, 0); // todo: mb->dquant = 0                  put_bvop_dbquant(bs, 0); // todo: mb->dquant = 0
546          }          }
547    
548          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD)          switch (mb->mode) {
549          {                  case MODE_INTERPOLATE:
550              CodeVector(bs, mb->pmvs[0].x, fcode, pStat);                          CodeVector(bs, mb->pmvs[1].x, vcode, pStat); //forward vector of interpolate mode
551              CodeVector(bs, mb->pmvs[0].y, fcode, pStat);                          CodeVector(bs, mb->pmvs[1].y, vcode, pStat);
552          }                  case MODE_BACKWARD:
553                            vcode = bcode;
554          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD)                  case MODE_FORWARD:
555          {                          CodeVector(bs, mb->pmvs[0].x, vcode, pStat);
556              CodeVector(bs, mb->b_pmvs[0].x, bcode, pStat);                          CodeVector(bs, mb->pmvs[0].y, vcode, pStat);
557              CodeVector(bs, mb->b_pmvs[0].y, bcode, pStat);                          break;
558          }                  case MODE_DIRECT:
559                            CodeVector(bs, mb->pmvs[3].x, 1, pStat);        // fcode is always 1 for delta vector
560          if (mb->mode == MODE_DIRECT)                          CodeVector(bs, mb->pmvs[3].y, 1, pStat);        // prediction is always (0,0)
561          {                  default: break;
                 // TODO: direct  
562          }          }
563    
564      for (i = 0; i < 6; i++)          for (i = 0; i < 6; i++) {
565      {                  if (mb->cbp & (1 << (5 - i))) {
566                  if (mb->cbp & (1 << (5 - i)))                          CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[direction], 0);
                 {  
                         CodeCoeff(bs, &qcoeff[i*64], inter_table, scan_tables[0], 0);  
567                  }                  }
568      }      }
569  }  }
# Line 584  Line 574 
574   * decoding stuff starts here                                  *   * decoding stuff starts here                                  *
575   ***************************************************************/   ***************************************************************/
576    
577  int get_mcbpc_intra(Bitstream * bs)  
578    // for IVOP addbits == 0
579    // for PVOP addbits == fcode - 1
580    // for BVOP addbits == max(fcode,bcode) - 1
581    // returns true or false
582    int
583    check_resync_marker(Bitstream * bs, int addbits)
584    {
585            uint32_t nbits;
586            uint32_t code;
587            uint32_t nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER + addbits;
588    
589            nbits = BitstreamNumBitsToByteAlign(bs);
590            code = BitstreamShowBits(bs, nbits);
591    
592            if (code == (((uint32_t)1 << (nbits - 1)) - 1))
593  {  {
594                    return BitstreamShowBitsFromByteAlign(bs, nbitsresyncmarker) == RESYNC_MARKER;
595            }
596    
597            return 0;
598    }
599    
600    
         uint32_t index;  
601    
602          while((index = BitstreamShowBits(bs, 9)) == 1)  int
603                  BitstreamSkip(bs, 9);  get_mcbpc_intra(Bitstream * bs)
604    {
605    
606            uint32_t index;
607    
608            index = BitstreamShowBits(bs, 9);
609          index >>= 3;          index >>= 3;
610    
611          BitstreamSkip(bs, mcbpc_intra_table[index].len);          BitstreamSkip(bs, mcbpc_intra_table[index].len);
# Line 600  Line 614 
614    
615  }  }
616    
617  int get_mcbpc_inter(Bitstream * bs)  int
618    get_mcbpc_inter(Bitstream * bs)
619  {  {
620    
621          uint32_t index;          uint32_t index;
622    
623          while((index = CLIP(BitstreamShowBits(bs, 9), 256)) == 1)          index = CLIP(BitstreamShowBits(bs, 9), 256);
                 BitstreamSkip(bs, 9);  
624    
625          BitstreamSkip(bs,  mcbpc_inter_table[index].len);          BitstreamSkip(bs,  mcbpc_inter_table[index].len);
626    
# Line 614  Line 628 
628    
629  }  }
630    
631  int get_cbpy(Bitstream * bs, int intra)  int
632    get_cbpy(Bitstream * bs,
633                     int intra)
634  {  {
635    
636          int cbpy;          int cbpy;
# Line 630  Line 646 
646    
647  }  }
648    
649  int get_mv_data(Bitstream * bs)  static __inline int
650    get_mv_data(Bitstream * bs)
651  {  {
652    
653          uint32_t index;          uint32_t index;
# Line 640  Line 657 
657    
658          index = BitstreamShowBits(bs, 12);          index = BitstreamShowBits(bs, 12);
659    
660          if(index >= 512)          if (index >= 512) {
         {  
661                  index = (index >> 8) - 2;                  index = (index >> 8) - 2;
662                  BitstreamSkip(bs, TMNMVtab0[index].len);                  BitstreamSkip(bs, TMNMVtab0[index].len);
663                  return TMNMVtab0[index].code;                  return TMNMVtab0[index].code;
664          }          }
665    
666          if(index >= 128)          if (index >= 128) {
         {  
667                  index = (index >> 2) - 32;                  index = (index >> 2) - 32;
668                  BitstreamSkip(bs, TMNMVtab1[index].len);                  BitstreamSkip(bs, TMNMVtab1[index].len);
669                  return TMNMVtab1[index].code;                  return TMNMVtab1[index].code;
# Line 661  Line 676 
676    
677  }  }
678    
679  int get_mv(Bitstream * bs, int fcode)  int
680    get_mv(Bitstream * bs,
681               int fcode)
682  {  {
683    
684          int data;          int data;
# Line 681  Line 698 
698    
699  }  }
700    
701  int get_dc_dif(Bitstream * bs, uint32_t dc_size)  int
702    get_dc_dif(Bitstream * bs,
703                       uint32_t dc_size)
704  {  {
705    
706          int code = BitstreamGetBits(bs, dc_size);          int code = BitstreamGetBits(bs, dc_size);
# Line 694  Line 713 
713    
714  }  }
715    
716  int get_dc_size_lum(Bitstream * bs)  int
717    get_dc_size_lum(Bitstream * bs)
718  {  {
719    
720          int code, i;          int code, i;
721    
722          code = BitstreamShowBits(bs, 11);          code = BitstreamShowBits(bs, 11);
723    
724          for(i = 11; i > 3; i--) {          for(i = 11; i > 3; i--) {
# Line 714  Line 735 
735  }  }
736    
737    
738  int get_dc_size_chrom(Bitstream * bs)  int
739    get_dc_size_chrom(Bitstream * bs)
740  {  {
741    
742          uint32_t code, i;          uint32_t code, i;
743    
744          code = BitstreamShowBits(bs, 12);          code = BitstreamShowBits(bs, 12);
745    
746          for(i = 12; i > 2; i--) {          for(i = 12; i > 2; i--) {
# Line 732  Line 755 
755    
756  }  }
757    
758  void get_intra_block(Bitstream * bs, int16_t * block, int direction, int coeff)  void
759    get_intra_block(Bitstream * bs,
760                                    int16_t * block,
761                                    int direction,
762                                    int coeff)
763  {  {
764    
765          const uint16_t * scan = scan_tables[ direction ];          const uint16_t * scan = scan_tables[ direction ];
766          int level;          int level, run, last;
         int run;  
         int last;  
767    
768          do          do {
         {  
769                  level = get_coeff(bs, &run, &last, 1, 0);                  level = get_coeff(bs, &run, &last, 1, 0);
770                  if (run == -1)                  if (run == -1) {
                 {  
771                          DEBUG("fatal: invalid run");                          DEBUG("fatal: invalid run");
772                          break;                          break;
773                  }                  }
774                  coeff += run;                  coeff += run;
775                  block[ scan[coeff] ] = level;                  block[ scan[coeff] ] = level;
776                  if (level < -127 || level > 127)  
777                  {                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level);
778                    //DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[coeff], level, BitstreamShowBits(bs, 32));
779    
780                    if (level < -127 || level > 127) {
781                          DEBUG1("warning: intra_overflow", level);                          DEBUG1("warning: intra_overflow", level);
782                  }                  }
783                  coeff++;                  coeff++;
# Line 759  Line 785 
785    
786  }  }
787    
788  void get_inter_block(Bitstream * bs, int16_t * block)  void
789    get_inter_block(Bitstream * bs,
790                                    int16_t * block,
791                                    int direction)
792  {  {
793    
794          const uint16_t * scan = scan_tables[0];          const uint16_t *scan = scan_tables[direction];
795          int p;          int p;
796          int level;          int level;
797          int run;          int run;
798          int last;          int last;
799    
800          p = 0;          p = 0;
801          do          do {
         {  
802                  level = get_coeff(bs, &run, &last, 0, 0);                  level = get_coeff(bs, &run, &last, 0, 0);
803                  if (run == -1)                  if (run == -1) {
                 {  
804                          DEBUG("fatal: invalid run");                          DEBUG("fatal: invalid run");
805                          break;                          break;
806                  }                  }
807                  p += run;                  p += run;
808    
809                  block[ scan[p] ] = level;                  block[ scan[p] ] = level;
810                  if (level < -127 || level > 127)  
811                  {                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level);
812                    // DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[p], level, BitstreamShowBits(bs, 32));
813    
814                    if (level < -127 || level > 127) {
815                          DEBUG1("warning: inter_overflow", level);                          DEBUG1("warning: inter_overflow", level);
816                  }                  }
817                  p++;                  p++;

Legend:
Removed from v.152  
changed lines
  Added in v.543

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