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

📄 common.c

📁 MPEG 2的音频编码软件。喜欢多媒体的开发人员可以看看。
💻 C
📖 第 1 页 / 共 4 页
字号:
   unsigned long val = 0;
   unsigned char byte;
   unsigned char bytes[2048];	/* bytes of an MPEG-2 extention frame */
   long sync = EXT_SYNCWORD;		/* sync word maximum 32 bits */
   int N = SYNC_WORD_LNGTH;	/* sync word length */
   int i, sync_bytes = N / 8;
   int nbytes, lcrc, la_bytes, la_bits;

   if (fread (bytes, 1, 5, bs->pt) != 5)
   {
      printf ("next ext header not found\n");
      return (0);
   }

   for (i = 0; i < sync_bytes; i++)
   {
      val <<= 8;
      val |= bytes[i];
   }
   if ((N % 8) != 0)
   {
      val <<= N % 8;
      byte = bytes[sync_bytes] >> (8 - (N % 8));
      val |= byte;
   }
  
   if (val != sync)
   {
      printf ("no ext sync found: %4x\n", val);
      return (0);
   }

   nbytes = (((int) (bytes[3] & 0x0f) * 256) + bytes[4]) / 2;

   if (fread (&bytes[5], 1, nbytes-5, bs->pt) != nbytes-5)
      return (0);
   bytes_to_bits (bs->bits, bytes, nbytes);

   bs->totbits = nbytes * 8;
   bs->curpos = 12;
   bs->eobs = 0;

   info->ext_crc_check = getbits (bs, 16);
   info->ext_length = getbits (bs, 11);
   info->reserved_bit = get1bit (bs);

#ifdef EXTENSION_CRC_CHECK

   lcrc = info->ext_length * 8 - 28;
   if (lcrc > 128) lcrc = 128;
   la_bytes = (lcrc - 12) / 8;
   la_bits = (lcrc - 12) % 8;

   for (i = 0; i < la_bytes; i++)
      info->ext_crc_bits[i] = getbits (bs, 8);
   info->ext_crc_bits[la_bytes] = getbits (bs, la_bits);
   bs->curpos = 40;

   if (mc_ext_error_check (fr_ps, la_bytes, la_bits) == 0)
      return (0);
#endif

   return (1);
}

/*****************************************************************************
*
*  End of bit_stream.c package
*
*****************************************************************************/

/*****************************************************************************
*
*  CRC error protection package
*
*****************************************************************************/

void update_CRC(unsigned int data,
		unsigned int length,
		unsigned int *crc)
{
	unsigned int  masking, carry;

	masking = 1 << length;

	while((masking >>= 1)){
			carry = *crc & 0x8000;
			*crc <<= 1;
			if (!carry ^ !(data & masking))
					*crc ^= CRC16_POLYNOMIAL;
	}
	*crc &= 0xffff;

#ifdef	PrintCRCDebug
	printf ("crc_len: %2d code: %4x crc: %4x\n", length, data, *crc);
	fflush (stdout);
#endif
}

void I_CRC_calc (frame_params *fr_ps,
		 unsigned int bit_alloc[7][SBLIMIT], 
		 unsigned int *crc)
{
   int i, k;
   layer *info = fr_ps->header;
   int stereo  = fr_ps->stereo;
   int jsbound = fr_ps->jsbound;

   *crc = 0xffff; /* changed from '0' 92-08-11 shn */
   update_CRC (info->bitrate_index, 4, crc);
   update_CRC (info->sampling_frequency, 2, crc);
   update_CRC (info->padding, 1, crc);
   update_CRC (info->extension, 1, crc);
   update_CRC (info->mode, 2, crc);
   update_CRC (info->mode_ext, 2, crc);
   update_CRC (info->copyright, 1, crc);
   update_CRC (info->original, 1, crc);
   update_CRC (info->emphasis, 2, crc);

   for (i=0; i<SBLIMIT; i++)
      for (k=0; k<((i<jsbound)?stereo:1); k++)
	 update_CRC (bit_alloc[k][i], 4, crc);
}


void II_CRC_calc(frame_params *fr_ps,
		 unsigned int bit_alloc[7][SBLIMIT], 
		 unsigned int scfsi[7][SBLIMIT],
		 unsigned int *crc)
{
   int i, k;
   layer *info = fr_ps->header;
   int stereo  = fr_ps->stereo;
   int sblimit = fr_ps->sblimit;
   int jsbound = fr_ps->jsbound;
   al_table *alloc = fr_ps->alloc;

   *crc = 0xffff; /* changed from '0' 92-08-11 shn */
   update_CRC (info->bitrate_index, 4, crc);
   update_CRC (info->sampling_frequency, 2, crc);
   update_CRC (info->padding, 1, crc);
   update_CRC (info->extension, 1, crc);
   update_CRC (info->mode, 2, crc);
   update_CRC (info->mode_ext, 2, crc);
   update_CRC (info->copyright, 1, crc);
   update_CRC (info->original, 1, crc);
   update_CRC (info->emphasis, 2, crc);

   for (i=0; i<sblimit; i++)
      for (k=0; k<((i<jsbound)?stereo:1); k++)
	 update_CRC (bit_alloc[k][i], (*alloc)[i][0].bits, crc);

   for (i=0; i<sblimit; i++)
      for (k=0; k<stereo; k++)
	 if (bit_alloc[k][i])
	    update_CRC (scfsi[k][i], 2, crc);
}


void mc_error_check(frame_params *fr_ps,
		    unsigned int bit_alloc[7][SBLIMIT], 
		    unsigned int scfsi[7][SBLIMIT],
		    unsigned int *crc,
			     int ch_start,
			     int channels)
{
	 int i, m, l, pci,c,sbgr;
	 layer *info = fr_ps->header;
	 int sblimit = fr_ps->sblimit_mc;
	 al_table *alloc = fr_ps->alloc_mc;

	 *crc = 0xffff; /* changed from '0' 92-08-11 shn */
	 update_CRC(info->ext_bit_stream_present, 1, crc);
	 if ( info->ext_bit_stream_present == 1)
		update_CRC(info->n_ad_bytes, 8, crc);

	 update_CRC(info->center, 2, crc);
	 update_CRC(info->surround, 2, crc);
	 update_CRC(info->lfe, 1, crc);
	 update_CRC(info->audio_mix, 1,crc);
	 update_CRC(info->dematrix_procedure, 2, crc);
	 update_CRC(info->no_of_multi_lingual_ch, 3, crc);
	 update_CRC(info->multi_lingual_fs, 1, crc);
	 update_CRC(info->multi_lingual_layer, 1, crc);
	 update_CRC(info->copyright_ident_bit, 1, crc);
	 update_CRC(info->copyright_ident_start, 1, crc);
	 
	 update_CRC(info->tc_sbgr_select, 1, crc);
	 update_CRC(info->dyn_cross_on, 1, crc);
	 update_CRC(info->mc_prediction_on, 1, crc);

	 if (info->tc_sbgr_select == 1)
	    update_CRC (info->tc_allocation, fr_ps->alloc_bits, crc);
	 else
	    for (i = 0; i < 12; i++)
	       update_CRC (info->tc_alloc[i], fr_ps->alloc_bits, crc);
	   
	 if (info->dyn_cross_on == 1)
	 {
		update_CRC(info->dyn_cross_LR, 1, crc);
		for(i = 0; i < 12; i++)
		{
			update_CRC(info->dyn_cross_mode[i], fr_ps->dyn_cross_bits, crc);
			if (info->surround == 3)
				update_CRC(info->dyn_second_stereo[i], 1, crc);
		}
	 }

	 if (info->mc_prediction_on == 1)
	 {
		for(i = 0; i < 8; i++)
		{ 	/* new sbgr < 8 */
			update_CRC(info->mc_prediction[i], 1, crc);
			if( info->mc_prediction[i] == 1 )
			{
			   for(pci=0; pci< pred_coef_table[fr_ps->pred_mode][info->dyn_cross_mode[i]]; pci++)
				update_CRC(info->mc_predsi[i][pci], 2, crc);
			}
		}
	 }

	 if( info->lfe )
		update_CRC(info->lfe_alloc, (*alloc)[0][0].bits, crc);

	 for (i=0;i<sblimit;i++)
		   if( info->dyn_cross_on == 0)
			for(m = ch_start; m < channels; ++m)
			{
				if((i < 12) || (m != 2) || (fr_ps->header->center != 3))
					update_CRC(bit_alloc[m][i], (*alloc)[i][0].bits, crc);
					/* to limit the CRC calc., if the phant. center valid */
			}
		   else
		   {
			if(i == 0) sbgr = 0;
			else
	   		   for(c = 1; c < 12; c++)
	   		   {
	      			if((sb_groups[c-1] < i) && (i <= sb_groups[c]))
	      			{
			  	   sbgr = c;  /* search the valid subband group */
			  	   break;
	         		}
			   }

			/* 960816 FdB new setup for dyn. crosstalk modes */
			if (info->dyn_cross_mode[sbgr] == 0)
			{
			   for (m = ch_start; m < channels; m++)
			      if ((info->center != 3) || (i < 12) || (m !=2))
				 if (info->surround == 3 && info->dyn_second_stereo[sbgr] == 1)
				 {
				    if ((info->center == 0 || m != 4) && (info->center != 0 || m != (ch_start+1)))
				       update_CRC (bit_alloc[m][i], (*alloc)[i][0].bits, crc);
				 }
				 else
				    update_CRC (bit_alloc[m][i], (*alloc)[i][0].bits, crc);
			}
			else if (fr_ps->dyn_cross_bits == 1)   /* for channel mode 3/0 and 2/1 */
			{
			   if (info->surround == 3)		/* 3/0 + 2/0 */
			   {
			      update_CRC (bit_alloc[3][i], (*alloc)[i][0].bits, crc);
			      if (info->dyn_second_stereo[sbgr] == 0)
			         update_CRC (bit_alloc[4][i], (*alloc)[i][0].bits, crc);
			   }
			}
			else if (fr_ps->dyn_cross_bits == 3)   /* for channel mode 3/1 and 2/2 */
			{
			   if ((info->center != 3) || (i < 12))    /* 3/2 + no phantom center */
			      if ((info->dyn_cross_mode[sbgr] == 1) || (info->dyn_cross_mode[sbgr] == 4))
			         update_CRC (bit_alloc[2][i], (*alloc)[i][0].bits, crc);

			   if (info->dyn_cross_mode[sbgr] == 2)
			      update_CRC (bit_alloc[3][i], (*alloc)[i][0].bits, crc);
			} 
			else if (fr_ps->dyn_cross_bits == 4)   /* for channel mode 3/2 */
			{
			   /* T2 */
			   if ((info->center != 3) || (i < 12))    /* 3/2 + no phantom center */
			      switch (info->dyn_cross_mode[sbgr])
			   {
			   case 1 :   /* T2 contains bitalloc info */
			   case 2 :
			   case 4 :
			   case 8 :
			   case 9 :
			   case 10: 
			   case 11: 
			   case 12: 
			   case 14: 
				      update_CRC (bit_alloc[2][i], (*alloc)[i][0].bits, crc);
				      break;
			   }

			   /* T3 */
			   switch (info->dyn_cross_mode[sbgr])
			   {
			   case 1 :   /* T3 contains bitalloc info */
			   case 3 :
			   case 5 :
			   case 8 :
			   case 10: 
			   case 13: 
				      update_CRC (bit_alloc[3][i], (*alloc)[i][0].bits, crc);
				      break;
			   }

			   /* T4 */
			   switch (info->dyn_cross_mode[sbgr])
			   {
			   case 2 :   /* T4 contains bitalloc info */
			   case 3 :
			   case 6 :
			   case 9 : 
				      update_CRC (bit_alloc[4][i], (*alloc)[i][0].bits, crc);
				      break;
			   }
			}
		   }

	     for (i = 0; i < sblimit; i++)
		for (m = ch_start; m < channels; m++)
			if (bit_alloc[m][i])
				update_CRC (scfsi[m][i], 2, crc); 
}


#ifdef Augmentation_7ch
void mc_aug_error_check(frame_params *fr_ps,
		    unsigned int bit_alloc[7][SBLIMIT], 
		    unsigned int scfsi[7][SBLIMIT],
		    unsigned int *crc)
{
   int i, m, l, pci,c,sbgr;
   layer *info = fr_ps->header;
   int sblimit = fr_ps->sblimit_mc;
   al_table *alloc = fr_ps->alloc_mc;

   *crc = 0xffff; /* changed from '0' 92-08-11 shn */

   update_CRC(info->aug_mtx_proc, 2, crc);
   update_CRC(info->aug_dyn_cross_on, 1, crc);
   update_CRC(info->aug_future_ext, 1, crc);

   if(info->aug_mtx_proc == 0)
      for(i = 0; i < 12; i++)
	 update_CRC(info->tc_aug_alloc[i], 3, crc);
   else if(info->aug_mtx_proc == 1)
      for(i = 0; i < 12; i++)
	 update_CRC(info->tc_aug_alloc[i], 2, crc);
     
   if(info->aug_dyn_cross_on == 1)
      for(i = 0; i < 12; i++)
	 update_CRC(info->dyn_cross_aug_mode[i], 5, crc);

   for (i=0;i<sblimit;i++)
   {
      if(i == 0) sbgr = 0;
      else
	 for(c = 1; c < 12; c++)
	 {
	    if((sb_groups[c-1] < i) && (i <= sb_groups[c]))
	    {
	      sbgr = c;  /* search the valid subband group */
	      break;
	    }
	 }
 
      /* check bitalloc info */
      switch (info->dyn_cross_aug_mode[sbgr])
      {
      case  0: /* T5 and T6 contains bitalloc info */
	       update_CRC(bit_alloc[5][i], (*alloc)[i][0].bits, crc);
	       update_CRC(bit_alloc[6][i], (*alloc)[i][0].bits, crc);
	       break;

      case  1: /* T5 contains bitalloc info */
      case  2:
      case  3:
      case  4:
	       update_CRC(bit_alloc[5][i], (*alloc)[i][0].bits, crc);
	       break;

      case  5:   /* T6 contains bitalloc info */
      case 10:
      case 14:
	       update_CRC(bit_alloc[6][i], (*alloc)[i][0].bits, crc);
	       break;
      }
   }

   for (i = 0; i < sblimit; i++)
      for (m = 5; m < 7; m++)
	 if (bit_alloc[m][i])
		 update_CRC (scfsi[m][i], 2, crc); 
}
#endif


int mc_ext_error_check (frame_params *fr_ps, int bytes, int bits)
{
	layer *info = fr_ps->header;
	int i;
	unsigned int crc;

	crc = 0xffff;
	update_CRC(info->ext_length, 11, &crc);
	update_CRC(info->reserved_bit, 1, &crc);
	for( i = 0; i < bytes; i++)
		update_CRC(info->ext_crc_bits[i], 8, &crc);
	update_CRC(info->ext_crc_bits[bytes], bits, &crc);

	if (crc != info->ext_crc_check)
	{
		printf ("\nERROR in EXT.-CRC \n");
		return (0);
	}
	else
		return(1);
}

/*****************************************************************************
*
*  End of CRC error protection package
*
*****************************************************************************/

#ifdef  MACINTOSH
/*****************************************************************************
*
*  Set Macintosh file attributes.
*
*****************************************************************************/

void    set_mac_file_attr(char fileName[MAX_NAME_SIZE],
			  short vRefNum,
			  OsType creator,
			  OsType fileType)
{

short   theFile;
char    pascal_fileName[MAX_NAME_SIZE];
FInfo   fndrInfo;

		CtoPstr(strcpy(pascal_fileName, fileName));

        FSOpen(pascal_fileName, vRefNum, &theFile);
        GetFInfo(pascal_fileName, vRefNum, &fndrInfo);
		fndrInfo.fdCreator = creator;
        fndrInfo.fdType = fileType;
        SetFInfo(pascal_fileName, vRefNum, &fndrInfo);
        FSClose(theFile);

}
#endif

⌨️ 快捷键说明

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