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

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

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

revision 1.1, Fri Mar 8 02:44:34 2002 UTC revision 1.10, Fri May 3 08:37:55 2002 UTC
# Line 41  Line 41 
41    *                                                                                                                                                        *    *                                                                                                                                                        *
42    *  Revision history:                                                         *    *  Revision history:                                                         *
43    *                                                                            *    *                                                                            *
44      *  01.05.2002 added BVOP support to BitstreamWriteVopHeader
45      *  15.04.2002 rewrite log2bin use asm386  By MinChen <chenm001@163.com>      *
46      *  26.03.2002 interlacing support                                                                                        *
47    *  03.03.2002 qmatrix writing                                                                                            *    *  03.03.2002 qmatrix writing                                                                                            *
48    *  03.03.2002 merged BITREADER and BITWRITER                                                             *    *  03.03.2002 merged BITREADER and BITWRITER                                                             *
49    *      30.02.2002     intra_dc_threshold support                                                                         *    *      30.02.2002     intra_dc_threshold support                                                                         *
# Line 52  Line 55 
55    
56  #include "bitstream.h"  #include "bitstream.h"
57  #include "zigzag.h"  #include "zigzag.h"
58    #include "../quant/quant_matrix.h"
59    
60    
61  static int __inline log2bin(int value)  static int __inline log2bin(int value)
62  {  {
63    /* Changed by Chenm001 */
64    #ifndef WIN32
65          int n = 0;          int n = 0;
66          while (value)          while (value)
67          {          {
# Line 63  Line 69 
69                  n++;                  n++;
70          }          }
71          return n;          return n;
72    #else
73            __asm{
74                    bsr eax,value
75                    inc eax
76            }
77    #endif
78  }  }
79    
80    
# Line 79  Line 91 
91  };  };
92    
93    
94    void bs_get_matrix(Bitstream * bs, uint8_t * matrix)
95    {
96            int i = 0;
97        int last, value = 0;
98    
99        do
100            {
101                    last = value;
102            value = BitstreamGetBits(bs, 8);
103            matrix[ scan_tables[0][i++] ]  = value;
104        }
105        while (value != 0 && i < 64);
106    
107            while (i < 64)
108            {
109                    matrix[ scan_tables[0][i++] ]  = last;
110            }
111    }
112    
113  /*  /*
114  decode headers  decode headers
115  returns coding_type, or -1 if error  returns coding_type, or -1 if error
116  */  */
117    
118  int BitstreamReadHeaders(Bitstream * bs, DECODER * dec, uint32_t * rounding, uint32_t * quant, uint32_t * fcode, uint32_t * intra_dc_threshold)  int BitstreamReadHeaders(Bitstream * bs, DECODER * dec, uint32_t * rounding, uint32_t * quant, uint32_t * fcode_forward, uint32_t * fcode_backward, uint32_t * intra_dc_threshold)
119  {  {
120          uint32_t vol_ver_id;          uint32_t vol_ver_id;
121          uint32_t time_inc_resolution;          static uint32_t time_increment_resolution;
122          uint32_t coding_type;          uint32_t coding_type;
123          uint32_t start_code;          uint32_t start_code;
124            uint32_t time_incr=0;
125            int32_t  time_increment;
126    
127          do          do
128          {          {
# Line 218  Line 251 
251    
252                          READ_MARKER();                          READ_MARKER();
253    
254                          time_inc_resolution = BitstreamGetBits(bs, 16); // vop_time_increment_resolution  // *************************** for decode B-frame time ***********************
255                          time_inc_resolution--;                          time_increment_resolution = BitstreamGetBits(bs, 16);   // vop_time_increment_resolution
256                          if (time_inc_resolution > 0)                          time_increment_resolution--;
257                            //DEBUG1("time_increment_resolution=",time_increment_resolution);
258                            if (time_increment_resolution > 0)
259                          {                          {
260                                  dec->time_inc_bits = log2bin(time_inc_resolution);                                  dec->time_inc_bits = log2bin(time_increment_resolution);
261                          }                          }
262                          else                          else
263                          {                          {
# Line 264  Line 299 
299    
300                                  }                                  }
301    
302                                  if (BitstreamGetBit(bs))                                // interlaced                                  if ((dec->interlacing = BitstreamGetBit(bs)))
303                                  {                                  {
304                                          DEBUG("TODO: interlaced");                                          DEBUG("vol: interlacing");
                                         // TODO  
                                         return -1;  
305                                  }                                  }
306    
307                                  if (!BitstreamGetBit(bs))                               // obmc_disable                                  if (!BitstreamGetBit(bs))                               // obmc_disable
# Line 314  Line 347 
347                                  {                                  {
348                                          if (BitstreamGetBit(bs))                // load_intra_quant_mat                                          if (BitstreamGetBit(bs))                // load_intra_quant_mat
349                                          {                                          {
350                                                  DEBUG("TODO: load_intra_quant_mat");                                                  uint8_t matrix[64];
351                                                  // TODO                                                  bs_get_matrix(bs, matrix);
352                                                  return -1;                                                  set_intra_matrix(matrix);
353                                          }                                          }
354                                            else
355                                                    set_intra_matrix(get_default_intra_matrix());
356    
357                                          if (BitstreamGetBit(bs))                // load_inter_quant_mat                                          if (BitstreamGetBit(bs))                // load_inter_quant_mat
358                                          {                                          {
359                                                  DEBUG("TODO: load_inter_quant_mat");                                                  uint8_t matrix[64];
360                                                  // TODO                                                  bs_get_matrix(bs, matrix);
361                                                  return -1;                                                  set_inter_matrix(matrix);
362                                          }                                          }
363                                            else
364                                                    set_inter_matrix(get_default_inter_matrix());
365    
366                                          if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE)                                          if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE)
367                                          {                                          {
# Line 331  Line 369 
369                                                  DEBUG("TODO: grayscale matrix stuff");                                                  DEBUG("TODO: grayscale matrix stuff");
370                                                  return -1;                                                  return -1;
371                                          }                                          }
372    
373                                  }                                  }
374    
375    
# Line 382  Line 421 
421                                          }                                          }
422                                  }                                  }
423    
424                                  if (BitstreamGetBit(bs))        // scalability                                  if ((dec->scalability=BitstreamGetBit(bs)))     // scalability
425                                  {                                  {
426                                          // TODO                                          // TODO
427                                          DEBUG("TODO: scalability");                                          DEBUG("TODO: scalability");
# Line 428  Line 467 
467                          coding_type = BitstreamGetBits(bs, 2);          // vop_coding_type                          coding_type = BitstreamGetBits(bs, 2);          // vop_coding_type
468                          //DEBUG1("coding_type", coding_type);                          //DEBUG1("coding_type", coding_type);
469    
470                          while (BitstreamGetBit(bs) != 0) ;                      // time_base  // *************************** for decode B-frame time ***********************
471                            while (BitstreamGetBit(bs) != 0)                        // time_base
472                                    time_incr++;
473    
474                          READ_MARKER();                          READ_MARKER();
475    
# Line 436  Line 477 
477                          //DEBUG1("vop_time_incr", BitstreamShowBits(bs, dec->time_inc_bits));                          //DEBUG1("vop_time_incr", BitstreamShowBits(bs, dec->time_inc_bits));
478                          if (dec->time_inc_bits)                          if (dec->time_inc_bits)
479                          {                          {
480                                  BitstreamSkip(bs, dec->time_inc_bits);  // vop_time_increment                                  //BitstreamSkip(bs, dec->time_inc_bits);        // vop_time_increment
481                                    time_increment = (BitstreamGetBits(bs, dec->time_inc_bits));    // vop_time_increment
482                            }
483                            if(coding_type != B_VOP){
484                                dec->last_time_base = dec->time_base;
485                                    dec->time_base += time_incr;
486                                    dec->time = dec->time_base*time_increment_resolution + time_increment;
487                                    dec->time_pp= (uint32_t)(dec->time - dec->last_non_b_time);
488                                    dec->last_non_b_time= dec->time;
489                            }else{
490                                    dec->time = (dec->last_time_base + time_incr)*time_increment_resolution + time_increment;
491                                    dec->time_bp= (uint32_t)(dec->last_non_b_time - dec->time);
492                          }                          }
493                            //DEBUG1("time_increment=",time_increment);
494    
495                          READ_MARKER();                          READ_MARKER();
496    
# Line 451  Line 504 
504                          }                          }
505                          */                          */
506    
507                          if (coding_type != I_VOP)                          // fix a little bug by MinChen <chenm002@163.com>
508                            if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) && (coding_type == P_VOP))
509                          {                          {
510                                  *rounding = BitstreamGetBit(bs);        // rounding_type                                  *rounding = BitstreamGetBit(bs);        // rounding_type
511                                  //DEBUG1("rounding", *rounding);                                  //DEBUG1("rounding", *rounding);
# Line 492  Line 546 
546                                  // intra_dc_vlc_threshold                                  // intra_dc_vlc_threshold
547                                  *intra_dc_threshold = intra_dc_threshold_table[ BitstreamGetBits(bs,3) ];                                  *intra_dc_threshold = intra_dc_threshold_table[ BitstreamGetBits(bs,3) ];
548    
549                                  /* if (interlaced)                                  if (dec->interlacing)
550                                          {                                          {
551                                                  BitstreamSkip(bs, 1);           // top_field_first                                          if ((dec->top_field_first = BitstreamGetBit(bs)))
552                                                  BitstreamSkip(bs, 1);           // alternative_vertical_scan_flag                                          {
553                                  */                                                  DEBUG("vop: top_field_first");
554                                            }
555                                            if ((dec->alternate_vertical_scan = BitstreamGetBit(bs)))
556                                            {
557                                                    DEBUG("vop: alternate_vertical_scan");
558                                            }
559                          }                          }
560                            }
561    
562                            if((*quant = BitstreamGetBits(bs, dec->quant_bits)) < 1)                // vop_quant
563                                    *quant = 1;
564    
                         *quant = BitstreamGetBits(bs, dec->quant_bits);         // vop_quant  
565                          //DEBUG1("quant", *quant);                          //DEBUG1("quant", *quant);
566    
567                          if (coding_type != I_VOP)                          if (coding_type != I_VOP)
568                          {                          {
569                                  *fcode = BitstreamGetBits(bs, 3);                       // fcode_forward                                  *fcode_forward = BitstreamGetBits(bs, 3);               // fcode_forward
570                          }                          }
571    
572                          if (coding_type == B_VOP)                          if (coding_type == B_VOP)
573                          {                          {
574                                  // *fcode_backward = BitstreamGetBits(bs, 3);           // fcode_backward                                  *fcode_backward = BitstreamGetBits(bs, 3);              // fcode_backward
575                            }
576                            if (!dec->scalability){
577                                    if ((dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) && (coding_type != I_VOP)){
578                                            BitstreamSkip(bs, 1);           // vop_shape_coding_type
579                                    }
580                          }                          }
581                          return coding_type;                          return coding_type;
582                  }                  }
# Line 559  Line 626 
626          write vol header          write vol header
627  */  */
628  void BitstreamWriteVolHeader(Bitstream * const bs,  void BitstreamWriteVolHeader(Bitstream * const bs,
629                                                  const int width,                                                  const MBParam * pParam,  const FRAMEINFO * frame)
                                                 const int height,  
                                                 const int quant_type)  
630  {  {
631          // video object_start_code & vo_id          // video object_start_code & vo_id
632      BitstreamPad(bs);      BitstreamPad(bs);
# Line 597  Line 662 
662          // BitstreamPutBits(bs, 0, 15);          // BitstreamPutBits(bs, 0, 15);
663    
664          WRITE_MARKER();          WRITE_MARKER();
665          BitstreamPutBits(bs, width, 13);                // width          BitstreamPutBits(bs, pParam->width, 13);                // width
666          WRITE_MARKER();          WRITE_MARKER();
667          BitstreamPutBits(bs, height, 13);               // height          BitstreamPutBits(bs, pParam->height, 13);               // height
668          WRITE_MARKER();          WRITE_MARKER();
669    
670          BitstreamPutBit(bs, 0);         // interlace          BitstreamPutBit(bs, frame->global_flags & XVID_INTERLACING);            // interlace
671          BitstreamPutBit(bs, 1);         // obmc_disable (overlapped block motion compensation)          BitstreamPutBit(bs, 1);         // obmc_disable (overlapped block motion compensation)
672          BitstreamPutBit(bs, 0);         // sprite_enable          BitstreamPutBit(bs, 0);         // sprite_enable
673          BitstreamPutBit(bs, 0);         // not_in_bit          BitstreamPutBit(bs, 0);         // not_in_bit
674    
675          // quant_type   0=h.263  1=mpeg4(quantizer tables)          // quant_type   0=h.263  1=mpeg4(quantizer tables)
676          BitstreamPutBit(bs, quant_type);          BitstreamPutBit(bs, pParam->m_quant_type);
677  /*  
678          if (quant_type)          if (pParam->m_quant_type)
679          {          {
680                  BitstreamPutBit(bs, qmatrix->custom_intra);             // load_intra_quant_mat                  BitstreamPutBit(bs, get_intra_matrix_status()); // load_intra_quant_mat
681                  if (qmatrix->custom_intra)                  if (get_intra_matrix_status())
682                  {                  {
683                          bs_put_matrix(bs, qmatrix->intra);                          bs_put_matrix(bs, get_intra_matrix());
684                  }                  }
685    
686                  BitstreamPutBit(bs, qmatrix->custom_inter);             // load_inter_quant_mat                  BitstreamPutBit(bs, get_inter_matrix_status());         // load_inter_quant_mat
687                  if (qmatrix->custom_inter)                  if (get_inter_matrix_status())
688                  {                  {
689                          bs_put_matrix(bs, qmatrix->inter);                          bs_put_matrix(bs, get_inter_matrix());
690                  }                  }
691    
692          }          }
 */  
         if (quant_type)  
         {  
                 BitstreamPutBit(bs, 0);         // load_intra_quant_mat  
                 BitstreamPutBit(bs, 0);         // load_inter_quant_mat  
         }  
693    
694          BitstreamPutBit(bs, 1);         // complexity_estimation_disable          BitstreamPutBit(bs, 1);         // complexity_estimation_disable
695          BitstreamPutBit(bs, 1);         // resync_marker_disable          BitstreamPutBit(bs, 1);         // resync_marker_disable
# Line 648  Line 707 
707    (decoder uses these values to determine precise time since last resync)    (decoder uses these values to determine precise time since last resync)
708  */  */
709  void BitstreamWriteVopHeader(Bitstream * const bs,  void BitstreamWriteVopHeader(Bitstream * const bs,
710                            VOP_TYPE prediction_type,                                                  const MBParam * pParam,
711                            const int rounding_type,                                                  const FRAMEINFO * frame)
                           const uint32_t quant,  
                           const uint32_t fcode)  
712  {  {
713    #ifdef BFRAMES
714            uint32_t i;
715    #endif
716      BitstreamPad(bs);      BitstreamPad(bs);
717      BitstreamPutBits(bs, VOP_START_CODE, 32);      BitstreamPutBits(bs, VOP_START_CODE, 32);
718    
719      BitstreamPutBits(bs, prediction_type, 2);      BitstreamPutBits(bs, frame->coding_type, 2);
720    
721          // time_base = 0  write n x PutBit(1), PutBit(0)          // time_base = 0  write n x PutBit(1), PutBit(0)
722    #ifdef BFRAMES
723            for (i = 0; i < frame->seconds; i++)
724            {
725                    BitstreamPutBit(bs, 1);
726            }
727            BitstreamPutBit(bs, 0);
728    #else
729          BitstreamPutBits(bs, 0, 1);          BitstreamPutBits(bs, 0, 1);
730    #endif
731    
732          WRITE_MARKER();          WRITE_MARKER();
733    
734          // time_increment: value=nth_of_sec, nbits = log2(resolution)          // time_increment: value=nth_of_sec, nbits = log2(resolution)
735    #ifdef BFRAMES
736            BitstreamPutBits(bs, frame->ticks, 5);
737            dprintf("[%i:%i] %c\n", frame->seconds, frame->ticks, frame->coding_type == I_VOP ? 'I' : frame->coding_type == P_VOP ? 'P' : 'B');
738    #else
739          BitstreamPutBits(bs, 1, 1);          BitstreamPutBits(bs, 1, 1);
740    #endif
741    
742          WRITE_MARKER();          WRITE_MARKER();
743    
744          BitstreamPutBits(bs, 1, 1);                             // vop_coded          BitstreamPutBits(bs, 1, 1);                             // vop_coded
745    
746          if (prediction_type != I_VOP)          if (frame->coding_type != I_VOP)
747                  BitstreamPutBits(bs, rounding_type, 1);                  BitstreamPutBits(bs, frame->rounding_type, 1);
748    
749          BitstreamPutBits(bs, 0, 3);                             // intra_dc_vlc_threshold          BitstreamPutBits(bs, 0, 3);                             // intra_dc_vlc_threshold
750    
751          BitstreamPutBits(bs, quant, 5);                 // quantizer          if (frame->global_flags & XVID_INTERLACING)
752            {
753                    BitstreamPutBit(bs, 1);         // top field first
754                    BitstreamPutBit(bs, 0);         // alternate vertical scan
755            }
756    
757            BitstreamPutBits(bs, frame->quant, 5);                  // quantizer
758    
759            if (frame->coding_type != I_VOP)
760                    BitstreamPutBits(bs, frame->fcode, 3);          // forward_fixed_code
761    
762            if (frame->coding_type == B_VOP)
763                    BitstreamPutBits(bs, frame->bcode, 3);          // backward_fixed_code
764    
         if (prediction_type != I_VOP)  
                 BitstreamPutBits(bs, fcode, 3);         // fixed_code = [1,4]  
765  }  }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.10

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