[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.10, Sat Jan 11 14:59:24 2003 UTC
# Line 41  Line 41 
41    *                                                                                                                                                        *    *                                                                                                                                                        *
42    *  Revision history:                                                         *    *  Revision history:                                                         *
43    *                                                                            *    *                                                                            *
44      *  04.01.2003 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 48  Line 49 
49    ******************************************************************************/    ******************************************************************************/
50    
51    
52    #include <stdio.h>
53  #include <stdlib.h>  #include <stdlib.h>
54  #include "../portab.h"  #include "../portab.h"
55    #include "../global.h"
56  #include "bitstream.h"  #include "bitstream.h"
57  #include "zigzag.h"  #include "zigzag.h"
58  #include "vlc_codes.h"  #include "vlc_codes.h"
# Line 58  Line 60 
60    
61  #include "../utils/mbfunctions.h"  #include "../utils/mbfunctions.h"
62    
63  #define ABS(X) (((X)>0)?(X):-(X))  VLC intra_table[4*2048*64];
64  #define CLIP(X,A) (X > A) ? (A) : (X)  VLC inter_table[4*2048*64];
   
 VLC intra_table[524032];  
 VLC inter_table[524032];  
65    
66  VLC DCT3Dintra[4096];  VLC DCT3Dintra[4096];
67  VLC DCT3Dinter[4096];  VLC DCT3Dinter[4096];
68    
69    /* not really MB related, but VLCs are only available here */
70    void bs_put_spritetrajectory(Bitstream * bs, const int val)
71    {
72            const int code = sprite_trajectory_code[val+16384].code;
73            const int len = sprite_trajectory_code[val+16384].len;
74            const int code2 = sprite_trajectory_len[len].code;
75            const int len2 = sprite_trajectory_len[len].len;
76    
77    //      printf("GMC=%d Code/Len  = %d / %d ",val, code,len);
78    //      printf("Code2 / Len2 = %d / %d \n",code2,len2);
79    
80            BitstreamPutBits(bs, code2, len2);
81            if (len) BitstreamPutBits(bs, code, len);
82    }
83    
84    int bs_get_spritetrajectory(Bitstream * bs)
85    {
86            int i;
87            for (i = 0; i < 12; i++)
88            {
89                    if (BitstreamShowBits(bs, sprite_trajectory_len[i].len) == sprite_trajectory_len[i].code)
90                    {
91                            BitstreamSkip(bs, sprite_trajectory_len[i].len);
92                            return i;
93                    }
94            }
95            return -1;
96    }
97    
98  void  void
99  init_vlc_tables(void)  init_vlc_tables(void)
100  {  {
# Line 183  Line 211 
211          DCT3D[0] = DCT3Dinter;          DCT3D[0] = DCT3Dinter;
212          DCT3D[1] = DCT3Dintra;          DCT3D[1] = DCT3Dintra;
213    
214    
215    /* init sprite_trajectory tables */
216    /* even if GMC is not specified (it might be used later...) */
217    
218            sprite_trajectory_code[0+16384].code = 0;
219            sprite_trajectory_code[0+16384].len = 0;
220            for (k=0;k<14;k++)
221            {
222                    int limit = (1<<k);
223    
224                    for (i=-(2*limit-1); i<= -limit; i++)
225                    {
226                            sprite_trajectory_code[i+16384].code = (2*limit-1)+i;
227                            sprite_trajectory_code[i+16384].len = k+1;
228                    }
229    
230                    for (i=limit; i<= 2*limit-1; i++)
231                    {
232                            sprite_trajectory_code[i+16384].code = i;
233                            sprite_trajectory_code[i+16384].len = k+1;
234                    }
235            }
236  }  }
237    
238  static __inline void  static __inline void
# Line 280  Line 330 
330  }  }
331    
332    
333  static void  static __inline void
334  CodeBlockIntra(const FRAMEINFO * frame,  CodeBlockIntra(const FRAMEINFO * const frame,
335                             const MACROBLOCK * pMB,                             const MACROBLOCK * pMB,
336                             int16_t qcoeff[6 * 64],                             int16_t qcoeff[6 * 64],
337                             Bitstream * bs,                             Bitstream * bs,
# Line 330  Line 380 
380                                                           dcc_tab[qcoeff[i * 64 + 0] + 255].len);                                                           dcc_tab[qcoeff[i * 64 + 0] + 255].len);
381    
382                  if (pMB->cbp & (1 << (5 - i))) {                  if (pMB->cbp & (1 << (5 - i))) {
383                            const uint16_t *scan_table =
384                                    frame->global_flags & XVID_ALTERNATESCAN ?
385                                    scan_tables[2] : scan_tables[pMB->acpred_directions[i]];
386    
387                          bits = BitstreamPos(bs);                          bits = BitstreamPos(bs);
388    
389                          CodeCoeff(bs, &qcoeff[i * 64], intra_table,                          CodeCoeff(bs, &qcoeff[i * 64], intra_table, scan_table, 1);
                                           scan_tables[pMB->acpred_directions[i]], 1);  
390    
391                          bits = BitstreamPos(bs) - bits;                          bits = BitstreamPos(bs) - bits;
392                          pStat->iTextBits += bits;                          pStat->iTextBits += bits;
# Line 344  Line 397 
397    
398    
399  static void  static void
400  CodeBlockInter(const FRAMEINFO * frame,  CodeBlockInter(const FRAMEINFO * const frame,
401                             const MACROBLOCK * pMB,                             const MACROBLOCK * pMB,
402                             int16_t qcoeff[6 * 64],                             int16_t qcoeff[6 * 64],
403                             Bitstream * bs,                             Bitstream * bs,
# Line 361  Line 414 
414          BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,          BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,
415                                           mcbpc_inter_tab[mcbpc].len);                                           mcbpc_inter_tab[mcbpc].len);
416    
417            if ( (frame->coding_type == S_VOP) && (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) )
418            {
419                            /* decision on GMC is done in encoder.c now */
420                    BitstreamPutBit(bs, pMB->mcsel);                // mcsel: '0'=local motion, '1'=GMC
421            }
422    
423          // write cbpy          // write cbpy
424          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);
425    
# Line 372  Line 431 
431          if (frame->global_flags & XVID_INTERLACING) {          if (frame->global_flags & XVID_INTERLACING) {
432                  if (pMB->cbp) {                  if (pMB->cbp) {
433                          BitstreamPutBit(bs, pMB->field_dct);                          BitstreamPutBit(bs, pMB->field_dct);
434                          DEBUG1("codep: field_dct: ", pMB->field_dct);                          DPRINTF(DPRINTF_MB,"codep: field_dct: %i", pMB->field_dct);
435                  }                  }
436    
437                  // if inter block, write field ME flag                  // if inter block, write field ME flag
438                  if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {                  if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
439                          BitstreamPutBit(bs, pMB->field_pred);                          BitstreamPutBit(bs, pMB->field_pred);
440                          DEBUG1("codep: field_pred: ", pMB->field_pred);                          DPRINTF(DPRINTF_MB,"codep: field_pred: %i", pMB->field_pred);
441    
442                          // write field prediction references                          // write field prediction references
443                          if (pMB->field_pred) {                          if (pMB->field_pred) {
# Line 387  Line 446 
446                          }                          }
447                  }                  }
448          }          }
449          // code motion vector(s)          // code motion vector(s) if motion is local
450            if (!pMB->mcsel)
451          for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) {          for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) {
452                  CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat);                  CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat);
453                  CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat);                  CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat);
# Line 398  Line 458 
458          // code block coeffs          // code block coeffs
459          for (i = 0; i < 6; i++)          for (i = 0; i < 6; i++)
460                  if (pMB->cbp & (1 << (5 - i)))                  if (pMB->cbp & (1 << (5 - i)))
461                          CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0);                  {
462                            const uint16_t *scan_table =
463                                    frame->global_flags & XVID_ALTERNATESCAN ?
464                                    scan_tables[2] : scan_tables[0];
465    
466                            CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_table, 0);
467                    }
468    
469          bits = BitstreamPos(bs) - bits;          bits = BitstreamPos(bs) - bits;
470          pStat->iTextBits += bits;          pStat->iTextBits += bits;
   
471  }  }
472    
473    
474  void  void
475  MBCoding(const FRAMEINFO * frame,  MBCoding(const FRAMEINFO * const frame,
476                   MACROBLOCK * pMB,                   MACROBLOCK * pMB,
477                   int16_t qcoeff[6 * 64],                   int16_t qcoeff[6 * 64],
478                   Bitstream * bs,                   Bitstream * bs,
479                   Statistics * pStat)                   Statistics * pStat)
480  {  {
481            if (frame->coding_type != I_VOP)
482          if (frame->coding_type == P_VOP) {                          BitstreamPutBit(bs, 0); // not_coded
                         BitstreamPutBit(bs, 0); // coded  
         }  
483    
484          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)
485                  CodeBlockIntra(frame, pMB, qcoeff, bs, pStat);                  CodeBlockIntra(frame, pMB, qcoeff, bs, pStat);
# Line 425  Line 488 
488    
489  }  }
490    
491    /*
492    // moved to mbcoding.h so that in can be 'static __inline'
493  void  void
494  MBSkip(Bitstream * bs)  MBSkip(Bitstream * bs)
495  {  {
496          BitstreamPutBit(bs, 1); // not coded          BitstreamPutBit(bs, 1); // not coded
         return;  
497  }  }
498    */
499    
500  /***************************************************************  /***************************************************************
501   * bframe encoding start   * bframe encoding start
# Line 446  Line 509 
509          3       0001b   forward mc+q            dbquant, mvdf          3       0001b   forward mc+q            dbquant, mvdf
510  */  */
511    
512  void  static __inline void
513  put_bvop_mbtype(Bitstream * bs,  put_bvop_mbtype(Bitstream * bs,
514                                  int value)                                  int value)
515  {  {
516          switch (value) {          switch (value) {
517          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:  
518                  BitstreamPutBit(bs, 0);                  BitstreamPutBit(bs, 0);
519                    case MODE_BACKWARD:
520                  BitstreamPutBit(bs, 0);                  BitstreamPutBit(bs, 0);
521                    case MODE_INTERPOLATE:
522                  BitstreamPutBit(bs, 0);                  BitstreamPutBit(bs, 0);
523                    case MODE_DIRECT:
524                  BitstreamPutBit(bs, 1);                  BitstreamPutBit(bs, 1);
525                  return;                  default:
526                            break;
         default:;                                       // invalid!  
   
527          }          }
   
528  }  }
529    
530  /*  /*
# Line 486  Line 534 
534          +2      11b          +2      11b
535  */  */
536    
537  void  static __inline void
538  put_bvop_dbquant(Bitstream * bs,  put_bvop_dbquant(Bitstream * bs,
539                                   int value)                                   int value)
540  {  {
# Line 517  Line 565 
565                           const int32_t fcode,                           const int32_t fcode,
566                           const int32_t bcode,                           const int32_t bcode,
567                           Bitstream * bs,                           Bitstream * bs,
568                           Statistics * pStat)                           Statistics * pStat,
569                             int direction)
570  {  {
571          int i;          int vcode = fcode;
572            unsigned int i;
573    
574  /*      ------------------------------------------------------------------  /*      ------------------------------------------------------------------
575                  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 599 
599                  put_bvop_dbquant(bs, 0);        // todo: mb->dquant = 0                  put_bvop_dbquant(bs, 0);        // todo: mb->dquant = 0
600          }          }
601    
602          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) {          switch (mb->mode) {
603                  CodeVector(bs, mb->pmvs[0].x, fcode, pStat);                  case MODE_INTERPOLATE:
604                  CodeVector(bs, mb->pmvs[0].y, fcode, pStat);                          CodeVector(bs, mb->pmvs[1].x, vcode, pStat); //forward vector of interpolate mode
605          }                          CodeVector(bs, mb->pmvs[1].y, vcode, pStat);
606                    case MODE_BACKWARD:
607          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) {                          vcode = bcode;
608                  CodeVector(bs, mb->b_pmvs[0].x, bcode, pStat);                  case MODE_FORWARD:
609                  CodeVector(bs, mb->b_pmvs[0].y, bcode, pStat);                          CodeVector(bs, mb->pmvs[0].x, vcode, pStat);
610          }                          CodeVector(bs, mb->pmvs[0].y, vcode, pStat);
611                            break;
612          if (mb->mode == MODE_DIRECT) {                  case MODE_DIRECT:
613                  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
614                  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)
615                    default: break;
616          }          }
617    
618          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
619                  if (mb->cbp & (1 << (5 - i))) {                  if (mb->cbp & (1 << (5 - i))) {
620                          CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0);                          CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[direction], 0);
621                  }                  }
622          }          }
623  }  }
# Line 623  Line 674 
674    
675          uint32_t index;          uint32_t index;
676    
677          index = CLIP(BitstreamShowBits(bs, 9), 256);          index = MIN(BitstreamShowBits(bs, 9), 256);
678    
679          BitstreamSkip(bs, mcbpc_inter_table[index].len);          BitstreamSkip(bs, mcbpc_inter_table[index].len);
680    
# Line 649  Line 700 
700    
701  }  }
702    
703  int  static __inline int
704  get_mv_data(Bitstream * bs)  get_mv_data(Bitstream * bs)
705  {  {
706    
# Line 766  Line 817 
817  {  {
818    
819          const uint16_t *scan = scan_tables[direction];          const uint16_t *scan = scan_tables[direction];
820          int level;          int level, run, last;
         int run;  
         int last;  
821    
822          do {          do {
823                  level = get_coeff(bs, &run, &last, 1, 0);                  level = get_coeff(bs, &run, &last, 1, 0);
824                  if (run == -1) {                  if (run == -1) {
825                          DEBUG("fatal: invalid run");                          DPRINTF(DPRINTF_ERROR,"fatal: invalid run");
826                          break;                          break;
827                  }                  }
828                  coeff += run;                  coeff += run;
# Line 782  Line 831 
831                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level);                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level);
832                  //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));
833    
834                  if (level < -127 || level > 127) {                  if (level < -2047 || level > 2047) {
835                          DEBUG1("warning: intra_overflow", level);                          DPRINTF(DPRINTF_ERROR,"warning: intra_overflow %i", level);
836                  }                  }
837                  coeff++;                  coeff++;
838          } while (!last);          } while (!last);
# Line 792  Line 841 
841    
842  void  void
843  get_inter_block(Bitstream * bs,  get_inter_block(Bitstream * bs,
844                                  int16_t * block)                                  int16_t * block,
845                                    int direction)
846  {  {
847    
848          const uint16_t *scan = scan_tables[0];          const uint16_t *scan = scan_tables[direction];
849          int p;          int p;
850          int level;          int level;
851          int run;          int run;
# Line 805  Line 855 
855          do {          do {
856                  level = get_coeff(bs, &run, &last, 0, 0);                  level = get_coeff(bs, &run, &last, 0, 0);
857                  if (run == -1) {                  if (run == -1) {
858                          DEBUG("fatal: invalid run");                          DPRINTF(DPRINTF_ERROR,"fatal: invalid run");
859                          break;                          break;
860                  }                  }
861                  p += run;                  p += run;
# Line 815  Line 865 
865                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level);                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level);
866                  // 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));
867    
868                  if (level < -127 || level > 127) {                  if (level < -2047 || level > 2047) {
869                          DEBUG1("warning: inter_overflow", level);                          DPRINTF(DPRINTF_ERROR,"warning: inter overflow %i", level);
870                  }                  }
871                  p++;                  p++;
872          } while (!last);          } while (!last);

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

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