⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bitpack.cpp

📁 此源码是在VC平台下,实现MPEG4编解码的源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
   return v;
}



/* Read nbits bits from a file */
Int CVTCCommon::get_X_bits(Int nbits)
{
   Int v=0;

   while (nbits--)
      v = (v << 1) + nextinputbit();
   return v;
}

/*-------------------------------------------------------------------*/
/*********************************************************/
/* get a parameter Into the file, refer to VTC syntax    */
/* for algorithm                                         */
/*********************************************************/
Int CVTCDecoder::get_param(Int nbits)
{
   Int countg=0;
   Int word=0;
   Int value=0;
   Int module=1<<(nbits);

   do {
     word=get_X_bits(nbits+1);
     value += (word & (module-1))<<(countg*nbits);
     countg++;
   } while (word>>nbits);
   return (value);
}




/******************************************************************/
/* function to adjust the extra bytes read in by arithmetic coder.*/
/* called by ac_decoder_done                                      */
/******************************************************************/
Void CVTCDecoder::restore_arithmetic_offset(Int bits_to_go)
{
  /* for nonalignment, 14 has something to do with Max_frequency */
  bit_num +=14;
  count -= 14;

  if(((Int)(bit_buf>>(bit_num+1)) &1)==0){
    bit_num--;
    count++;
  }
}


/**********************************************************/
/* This function returns the next n bits in the bitstream */
/* where n<=32                                            */
/**********************************************************/
UInt CVTCEncoder::LookBitsFromStream (Int n)
{
  Int tmp_bit_num=bit_num+1, tmp_byte_ptr=byte_ptr;
  unsigned long tmp_buf=bit_buf,v;

  /* test if enough bits */
  /* hjlee 0901 */
  if(n>MAXLOOKBITS)
    errorHandler("LookBitsFromStream() can only return a maximum of "\
                 "%d bits.\n", MAXLOOKBITS);  
  if(buffer_length<BUFFER_SIZE-EXTRABYTES)
    if(((buffer_length-tmp_byte_ptr)<<3)+tmp_bit_num<n)
      /*errorHandler("LookBitsFromStream(): "\
                   "There are not enough bits in the bitstream.\n");*/
      return(0);

  /* get bits */
  while(tmp_bit_num<n/* && tmp_bit_num<(MAXLOOKBITS-8)*/){
    tmp_bit_num +=8;
    tmp_buf = (tmp_buf << 8) + output_buffer[tmp_byte_ptr++];
  }
  v =   (tmp_buf >> (tmp_bit_num-n));
  return(v&1 ); 

}


/* modified by Jie Liang (Texas Instruments)
   to return the last bits left in the last byte */

Int CVTCDecoder::align_byte1()
{
  Int i;
  
  if((i=(bit_num+1)%8)==0)
    return 0;
  
  return(get_X_bits(i)<<(8-i));
}

Int CVTCDecoder::align_byte ()
{
  Int i;
  
  if((i=(bit_num+1)%8)==0)
    i=8;    /* for stuffing as defined in CD 6.2.1 */

  junkCount += i;
  count -= i;

  return(get_X_bits(i)<<(8-i));
}


/* added by Jie Liang (Texas Instruments) */
/* the data needs to by byte aligned first */

Int CVTCDecoder::get_allbits (Char *buffer)
{ 
  Int n,len;
  Int loc=0;

  /* read until end of file */
  do {
    buffer[loc]=get_X_bits(8);
    loc++;
  }while (!feof(bitfile));

  /* read until the data in buffer is finished */
  len = buffer_length-byte_ptr+2;
  for (n=0;n<len;n++)
    {
      buffer[loc]=get_X_bits(8);
      loc++;
    }
    
  return loc;
}


/* added by Jie Liang (Texas Instruments) */
/* check the next four bytes for start code */

Int CVTCDecoder::Is_startcode (long startcode)
{
  long next_4bytes=0;
  Int i;
  Int offset;
  
  if (bit_num>=7)
    offset=1;
  else
    offset=0;

  next_4bytes = output_buffer[byte_ptr-offset];
  for(i=0;i<3;i++)
    next_4bytes = (next_4bytes<<8)+output_buffer[byte_ptr-offset+1+i];

  if(next_4bytes == startcode)
    return 1;
  
  return 0;
}

#define MAXRUNZERO 22  // hjlee
Void CVTCEncoder::emit_bits_checksc(UInt code, Int size)
{
  Int m;
  Int bit;

  for(m=size-1;m>=0;m--){
    bit = (code>>m) & (0x01);
    emit_bits (bit, 1);
    
    /* update zero bit count */
    if (bit)
      zerocount=0;
    else
      zerocount++;

    if (zerocount>=MAXRUNZERO)
      {  /* exceeding maximum runs, add 1 */
        emit_bits (1, 1);
        zerocount=0;

    }
  } /* end of m - bits */

  return;
    
}


Void CVTCEncoder::emit_bits_checksc_init()
{
  zerocount=0;
}


Int CVTCDecoder::get_X_bits_checksc(Int nbits)
{
   Int v=0;
   Int bit;

   while (nbits--){
      if(zerocount==MAXRUNZERO) /*skip next bit */
        {
//          if(!nextinputbit())  ; deleted by swinder
		  nextinputbit(); // added by swinder
//            noteProgress("Possible start code emulation error: should be 1 after MAXZERORUN 0's");
		  zerocount = 0;
        }

      bit = nextinputbit();
      if(!bit)
        zerocount++;
      else
        zerocount =0;

      v = (v << 1) + bit;
   }

   return v;
}

Void CVTCDecoder::get_X_bits_checksc_init()
{
  zerocount=0;
}


Int CVTCDecoder::get_allbits_checksc (unsigned char *buffer)
{ 
  Int n,len;
  Int loc=0;

  /* read until end of file */
  do {
    buffer[loc]=get_X_bits_checksc(8);
    loc++;
  }while (!feof(bitfile));

  /* read until the data in buffer is finished */
  len = buffer_length-byte_ptr+2;
  for (n=0;n<len;n++)
    {
      buffer[loc]=get_X_bits_checksc(8);
      loc++;
    }
    
  return loc;
}

Int CVTCDecoder::align_byte_checksc ()
{
  Int bit;
  Int i;
  Int m;
  Int out=0;
  Int n=0;

  if((i=(bit_num+1)%8)==0)
    return 0;
  
  for(m=0;m<i;m++){
    if(zerocount==MAXRUNZERO){
      get_X_bits(1);
      zerocount=0;
    }
    else{
      bit = get_X_bits(1);
      out = (out<<1) | bit;
      if (bit)
        zerocount=0;
      else
        zerocount++;

      n++;
    }
  }

  return(out<<(8-n));
}


/* This program combine the stuff in bitbuffer Into the actual bitstream */
Void CVTCEncoder::write_to_bitstream(UChar *bitbuffer,Int total_bits)
{
  Int i,bit_stream_length=total_bits>>3, resid=total_bits%8;

  /* write to file assume file has been opened by control program*/
  for(i=0;i<bit_stream_length;i++)
    emit_bits(bitbuffer[i],8);

  if(resid != 0)
    emit_bits(bitbuffer[bit_stream_length]>>(8-resid),resid);
}



/* get the number of bytes already decoded */
int CVTCDecoder::decoded_bytes_from_bitstream ()
{
    long n;

    n = ftell(bitfile);
    return (n+byte_ptr);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -