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

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

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

revision 1.25, Wed Sep 4 03:23:28 2002 UTC revision 1.25.2.6, Sat Dec 14 09:39:42 2002 UTC
# Line 41  Line 41 
41    *                                                                                                                                                        *    *                                                                                                                                                        *
42    *  Revision history:                                                         *    *  Revision history:                                                         *
43    *                                                                            *    *                                                                            *
44      *  28.10.2002 GMC support - gruel                                                                                        *
45    *  28.06.2002 added check_resync_marker()                                    *    *  28.06.2002 added check_resync_marker()                                    *
46    *  14.04.2002 bframe encoding                                                                                            *    *  14.04.2002 bframe encoding                                                                                            *
47    *  08.03.2002 initial version; isibaar                                                           *    *  08.03.2002 initial version; isibaar                                                           *
# Line 67  Line 68 
68  VLC DCT3Dintra[4096];  VLC DCT3Dintra[4096];
69  VLC DCT3Dinter[4096];  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    int bs_get_spritetrajectory(Bitstream * bs)
87    {
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  void
101  init_vlc_tables(void)  init_vlc_tables(void)
102  {  {
# Line 183  Line 213 
213          DCT3D[0] = DCT3Dinter;          DCT3D[0] = DCT3Dinter;
214          DCT3D[1] = DCT3Dintra;          DCT3D[1] = DCT3Dintra;
215    
216    
217    /* init sprite_trajectory tables */
218    /* even if GMC is not specified (it might be used later...) */
219    
220            sprite_trajectory_code[0+16384].code = 0;
221            sprite_trajectory_code[0+16384].len = 0;
222            for (k=0;k<14;k++)
223            {
224                    int limit = (1<<k);
225    
226                    for (i=-(2*limit-1); i<= -limit; i++)
227                    {
228                            sprite_trajectory_code[i+16384].code = (2*limit-1)+i;
229                            sprite_trajectory_code[i+16384].len = k+1;
230                    }
231    
232                    for (i=limit; i<= 2*limit-1; i++)
233                    {
234                            sprite_trajectory_code[i+16384].code = i;
235                            sprite_trajectory_code[i+16384].len = k+1;
236                    }
237            }
238  }  }
239    
240  static __inline void  static __inline void
# Line 280  Line 332 
332  }  }
333    
334    
335  static void  static __inline void
336  CodeBlockIntra(const FRAMEINFO * frame,  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 330  Line 382 
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, &qcoeff[i * 64], intra_table,                          CodeCoeff(bs, &qcoeff[i * 64], intra_table, scan_table, 1);
                                           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 399 
399    
400    
401  static void  static void
402  CodeBlockInter(const FRAMEINFO * frame,  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);
# Line 361  Line 417 
417          BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,          BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,
418                                           mcbpc_inter_tab[mcbpc].len);                                           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);
434    
# Line 372  Line 440 
440          if (frame->global_flags & XVID_INTERLACING) {          if (frame->global_flags & XVID_INTERLACING) {
441                  if (pMB->cbp) {                  if (pMB->cbp) {
442                          BitstreamPutBit(bs, pMB->field_dct);                          BitstreamPutBit(bs, pMB->field_dct);
443                          DEBUG1("codep: field_dct: ", pMB->field_dct);                          DPRINTF(DPRINTF_MB,"codep: field_dct: %i", 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);                          DPRINTF(DPRINTF_MB,"codep: field_pred: %i", pMB->field_pred);
450    
451                          // write field prediction references                          // write field prediction references
452                          if (pMB->field_pred) {                          if (pMB->field_pred) {
# Line 387  Line 455 
455                          }                          }
456                  }                  }
457          }          }
458          // code motion vector(s)          // code motion vector(s) if motion is local
459            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);                  CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat);
462                  CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat);                  CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat);
# Line 398  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  void
484  MBCoding(const FRAMEINFO * frame,  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          if (frame->coding_type == P_VOP) {                          BitstreamPutBit(bs, 0); // not_coded
                         BitstreamPutBit(bs, 0); // coded  
         }  
492    
493          if (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);                  CodeBlockIntra(frame, pMB, qcoeff, bs, pStat);
# Line 425  Line 497 
497    
498  }  }
499    
500    /*
501    // moved to mbcoding.h so that in can be 'static __inline'
502  void  void
503  MBSkip(Bitstream * bs)  MBSkip(Bitstream * bs)
504  {  {
505          BitstreamPutBit(bs, 1); // not coded          BitstreamPutBit(bs, 1); // not coded
         return;  
506  }  }
507    */
508    
509  /***************************************************************  /***************************************************************
510   * bframe encoding start   * bframe encoding start
# Line 446  Line 518 
518          3       0001b   forward mc+q            dbquant, mvdf          3       0001b   forward mc+q            dbquant, mvdf
519  */  */
520    
521  void  static __inline void
522  put_bvop_mbtype(Bitstream * bs,  put_bvop_mbtype(Bitstream * bs,
523                                  int value)                                  int value)
524  {  {
525          switch (value) {          switch (value) {
526          case 0:                  case MODE_FORWARD:
                 BitstreamPutBit(bs, 1);  
                 return;  
   
         case 1:  
                 BitstreamPutBit(bs, 0);  
                 BitstreamPutBit(bs, 1);  
                 return;  
   
         case 2:  
                 BitstreamPutBit(bs, 0);  
                 BitstreamPutBit(bs, 0);  
                 BitstreamPutBit(bs, 1);  
                 return;  
   
         case 3:  
527                  BitstreamPutBit(bs, 0);                  BitstreamPutBit(bs, 0);
528                    case MODE_BACKWARD:
529                  BitstreamPutBit(bs, 0);                  BitstreamPutBit(bs, 0);
530                    case MODE_INTERPOLATE:
531                  BitstreamPutBit(bs, 0);                  BitstreamPutBit(bs, 0);
532                    case MODE_DIRECT:
533                  BitstreamPutBit(bs, 1);                  BitstreamPutBit(bs, 1);
534                  return;                  default:
535                            break;
         default:;                                       // invalid!  
   
536          }          }
   
537  }  }
538    
539  /*  /*
# Line 486  Line 543 
543          +2      11b          +2      11b
544  */  */
545    
546  void  static __inline void
547  put_bvop_dbquant(Bitstream * bs,  put_bvop_dbquant(Bitstream * bs,
548                                   int value)                                   int value)
549  {  {
# Line 517  Line 574 
574                           const int32_t fcode,                           const int32_t fcode,
575                           const int32_t bcode,                           const int32_t bcode,
576                           Bitstream * bs,                           Bitstream * bs,
577                           Statistics * pStat)                           Statistics * pStat,
578                             int direction)
579  {  {
580          int i;          int vcode = fcode;
581            unsigned int i;
582    
583  /*      ------------------------------------------------------------------  /*      ------------------------------------------------------------------
584                  when a block is skipped it is decoded DIRECT(0,0)                  when a block is skipped it is decoded DIRECT(0,0)
# Line 549  Line 608 
608                  put_bvop_dbquant(bs, 0);        // todo: mb->dquant = 0                  put_bvop_dbquant(bs, 0);        // todo: mb->dquant = 0
609          }          }
610    
611          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) {          switch (mb->mode) {
612                  CodeVector(bs, mb->pmvs[0].x, fcode, pStat);                  case MODE_INTERPOLATE:
613                  CodeVector(bs, mb->pmvs[0].y, fcode, pStat);                          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          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) {                          vcode = bcode;
617                  CodeVector(bs, mb->b_pmvs[0].x, bcode, pStat);                  case MODE_FORWARD:
618                  CodeVector(bs, mb->b_pmvs[0].y, bcode, pStat);                          CodeVector(bs, mb->pmvs[0].x, vcode, pStat);
619          }                          CodeVector(bs, mb->pmvs[0].y, vcode, pStat);
620                            break;
621          if (mb->mode == MODE_DIRECT) {                  case MODE_DIRECT:
622                  CodeVector(bs, mb->deltamv.x, 1, pStat);                /* fcode is always 1 for delta vector */                          CodeVector(bs, mb->pmvs[3].x, 1, pStat);        // fcode is always 1 for delta vector
623                  CodeVector(bs, mb->deltamv.y, 1, pStat);                /* prediction is always (0,0) */                          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++) {          for (i = 0; i < 6; i++) {
628                  if (mb->cbp & (1 << (5 - i))) {                  if (mb->cbp & (1 << (5 - i))) {
629                          CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0);                          CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[direction], 0);
630                  }                  }
631          }          }
632  }  }
# Line 649  Line 709 
709    
710  }  }
711    
712  int  static __inline int
713  get_mv_data(Bitstream * bs)  get_mv_data(Bitstream * bs)
714  {  {
715    
# Line 766  Line 826 
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");                          DPRINTF(DPRINTF_ERROR,"fatal: invalid run");
835                          break;                          break;
836                  }                  }
837                  coeff += run;                  coeff += run;
# Line 783  Line 841 
841                  //DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[coeff], level, BitstreamShowBits(bs, 32));                  //DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[coeff], level, BitstreamShowBits(bs, 32));
842    
843                  if (level < -127 || level > 127) {                  if (level < -127 || level > 127) {
844                          DEBUG1("warning: intra_overflow", level);                          DPRINTF(DPRINTF_ERROR,"warning: intra_overflow %i", level);
845                  }                  }
846                  coeff++;                  coeff++;
847          } while (!last);          } while (!last);
# Line 792  Line 850 
850    
851  void  void
852  get_inter_block(Bitstream * bs,  get_inter_block(Bitstream * bs,
853                                  int16_t * block)                                  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;
# Line 805  Line 864 
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");                          DPRINTF(DPRINTF_ERROR,"fatal: invalid run");
868                          break;                          break;
869                  }                  }
870                  p += run;                  p += run;
# Line 816  Line 875 
875                  // DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[p], level, BitstreamShowBits(bs, 32));                  // DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[p], level, BitstreamShowBits(bs, 32));
876    
877                  if (level < -127 || level > 127) {                  if (level < -127 || level > 127) {
878                          DEBUG1("warning: inter_overflow", level);                          DPRINTF(DPRINTF_ERROR,"warning: inter overflow %i", level);
879                  }                  }
880                  p++;                  p++;
881          } while (!last);          } while (!last);

Legend:
Removed from v.1.25  
changed lines
  Added in v.1.25.2.6

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