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

📄 common.c

📁 MPEG 2的音频编码软件。喜欢多媒体的开发人员可以看看。
💻 C
📖 第 1 页 / 共 4 页
字号:
	while(strncmp(ident.name,IFF_ID_SSND,4) != 0)
	{
		dummy = calloc( ident.ck_length, sizeof(char));
/* changed "fread( &dummy," to "fread ( dummy,", HP 26-may-93 */
		if(fread( dummy, ident.ck_length, 1, file_ptr) != 1)
		  return(-1);
		free(dummy);
		if(fread( &ident, sizeof(identifier),1, file_ptr) != 1)
		  return (-1);
/* the following lines are not necessary, HP 27-may-93 */
/*
		  {
		  fseek(file_ptr, 0, SEEK_SET);
		  if(fread( &ident, sizeof(identifier), 1, file_ptr) != 1)
		  return(-1);
		  }
*/

#ifdef MSDOS


	holder = ident.name[0];
	ident.name[0] = ident.name[3];
	ident.name[3] = holder;
	holder = ident.name[1];
	ident.name[1] = ident.name[2];
	ident.name[2] = holder;

	ident.ck_length = _lrotl(ident.ck_length, 8);

#endif
	}

		for(i = 0; i < 4; ++i)
		SndDChunk.ckID[i] = ident.name[i];

		SndDChunk.ckSize = ident.ck_length;

		if (fread(&SndDChunk.offset, sizeof(unsigned long), 1, file_ptr) != 1)
			return(-1);

		if (fread(&SndDChunk.blockSize, sizeof(unsigned long), 1,
             file_ptr) != 1)
			return(-1);

#ifdef MSDOS

  SndDChunk.offset = _lrotl(SndDChunk.offset, 8);
  SndDChunk.blockSize = _lrotl(SndDChunk.blockSize, 8);

#endif

/* why seek behinde the SSND Chunk ????, HP 27-may-93 */
/*
   seek_offset = SndDChunk.ckSize - sizeof(SoundDataChunk) +
      sizeof(ChunkHeader);

   if (fseek(file_ptr, seek_offset, SEEK_CUR) != 0)
	  return(-1);
*/

   aiff_ptr->numChannels       = CommChunk.numChannels;
   aiff_ptr->numSampleFrames   = CommChunk.numSampleFrames;
   aiff_ptr->sampleSize        = CommChunk.sampleSize;
   aiff_ptr->blkAlgn.offset    = SndDChunk.offset;
   aiff_ptr->blkAlgn.blockSize = SndDChunk.blockSize;
   strncpy(aiff_ptr->sampleType, SndDChunk.ckID, 4);

   return(0);

}


/*****************************************************************************
*
*  Seek past some Audio Interchange File Format (AIFF) headers to sound data.
*
*****************************************************************************/

int aiff_seek_to_sound_data(FILE *file_ptr)
{

/*   if (fseek(file_ptr, sizeof(Chunk) + sizeof(SoundDataChunk), SEEK_SET) != 0) */
   if (fseek(file_ptr, sizeof(Chunk) + sizeof(CommonChunk) + sizeof(SoundDataChunk), SEEK_SET) != 0)
      return(-1);
   else
      return(0);

}

/*******************************************************************************
*
*  Write Audio Interchange File Format (AIFF) headers.
*
*******************************************************************************/

int aiff_write_headers(FILE *file_ptr,
		       IFF_AIFF *aiff_ptr)
{

register char   i;
register long   seek_offset;

char            temp_sampleRate[10];

Chunk           FormChunk;
CommonChunk     CommChunk;
SoundDataChunk  SndDChunk;


	strcpy( FormChunk.ckID, IFF_ID_FORM);
	strcpy( FormChunk.formType, IFF_ID_AIFF);
	strcpy( CommChunk.ckID, IFF_ID_COMM);  /*7/7/93,SR,changed FormChunk to CommChunk*/


   double_to_extended(&aiff_ptr->sampleRate, temp_sampleRate);

   for (i = 0; i < sizeof(char[10]); i++)
      CommChunk.sampleRate[i] = temp_sampleRate[i];

   CommChunk.numChannels             = aiff_ptr->numChannels;
   CommChunk.numSampleFrames         = aiff_ptr->numSampleFrames;
   CommChunk.sampleSize              = aiff_ptr->sampleSize;
   SndDChunk.offset                  = aiff_ptr->blkAlgn.offset + 2;
   SndDChunk.blockSize               = aiff_ptr->blkAlgn.blockSize;
   strncpy(/*(unsigned long *)*/ SndDChunk.ckID, aiff_ptr->sampleType,  4);

   CommChunk.ckSize = sizeof(CommChunk.numChannels) +
      sizeof(CommChunk.numSampleFrames) + sizeof(CommChunk.sampleSize) +
      sizeof(CommChunk.sampleRate);

   SndDChunk.ckSize = sizeof(SoundDataChunk) - sizeof(ChunkHeader) +
      (CommChunk.sampleSize + BITS_IN_A_BYTE - 1) / BITS_IN_A_BYTE *
      CommChunk.numChannels * CommChunk.numSampleFrames;

   FormChunk.ckSize = sizeof(Chunk) + SndDChunk.ckSize + sizeof(ChunkHeader) +
	  CommChunk.ckSize;

   if (fseek(file_ptr, 0, SEEK_SET) != 0)
      return(-1);

   if (fwrite(&FormChunk, sizeof(Chunk), 1, file_ptr) != 1)
      return(-1);
/*
   if (fwrite(&SndDChunk, sizeof(SoundDataChunk), 1, file_ptr) != 1)
      return(-1);

   seek_offset = SndDChunk.ckSize - sizeof(SoundDataChunk) +
      sizeof(ChunkHeader);

   if (fseek(file_ptr, seek_offset, SEEK_CUR) != 0)
      return(-1);
*/

   if (fwrite(CommChunk.ckID, sizeof(ID), 1, file_ptr) != 1)
      return(-1);

   if (fwrite(&CommChunk.ckSize, sizeof(long), 1, file_ptr) != 1)
      return(-1);

   if (fwrite(&CommChunk.numChannels, sizeof(short), 1, file_ptr) != 1)
      return(-1);

   if (fwrite(&CommChunk.numSampleFrames, sizeof(unsigned long), 1,
	      file_ptr) != 1)
	  return(-1);

   if (fwrite(&CommChunk.sampleSize, sizeof(short), 1, file_ptr) != 1)
	  return(-1);

   if (fwrite(CommChunk.sampleRate, sizeof(char[10]), 1, file_ptr) != 1)
      return(-1);

   /* 960815 FdB put the sound data chunk after the common chunk */
   if (fwrite(&SndDChunk, sizeof(SoundDataChunk), 1, file_ptr) != 1)
      return(-1);

   return(0);

}

/*****************************************************************************
*
*  bit_stream.c package
*  Author:  Jean-Georges Fritsch, C-Cube Microsystems
*
*****************************************************************************/

/********************************************************************
  This package provides functions to write (exclusive or read)
  information from (exclusive or to) the bit stream.

  If the bit stream is opened in read mode only the get functions are
  available. If the bit stream is opened in write mode only the put
  functions are available.
********************************************************************/

static char *he = "0123456789ABCDEF";

/* open the device to read the bit stream from it */
int open_bit_stream_r (Bit_stream *bs,   /* bit stream structure */
		       char *bs_filenam,       /* name of the bit stream file */
		       int size                /* size of the buffer */)
{
   register unsigned long n;
   register int i=0,j=0;
   register unsigned char flag = 1;
   unsigned char val;

   if ((bs->pt = fopen (bs_filenam, "rb")) == NULL)
   {
	  printf("Could not find \"%s\".\n", bs_filenam);
	  return (0);
   }
   fseek (bs->pt, bs->header_size, 0);

   do
   {
	 n = fread (&val, sizeof (unsigned char), 1, bs->pt);
	 switch (val)
	 {
	  case 0x30:
	  case 0x31:
	  case 0x32:
	  case 0x33:
	  case 0x34:
	  case 0x35:
	  case 0x36:
	  case 0x37:
	  case 0x38:
	  case 0x39:
	  case 0x41:
	  case 0x42:
	  case 0x43:
	  case 0x44:
	  case 0x45:
	  case 0x46:  j++;
	  case 0xa:  /* \n */
		  break;

	  default: /* detection of an binary character */
		  flag--;
		  i = 300;
		  break;
	 }

   } while (flag & n);

   if (flag)
   {
	  printf ("the bit stream file %s is an ASCII file\n", bs_filenam);
	  printf ("ASCII files are not supported in this version\n");
	  exit (1);
	  bs->format = ASCII;
   }
   else
   {
	  bs->format = BINARY;
	  printf ("the bit stream file %s is a BINARY file\n", bs_filenam);
   }

   fclose (bs->pt);

   if ((bs->pt = fopen (bs_filenam, "rb")) == NULL)
   {
      printf("Could not find \"%s\".\n", bs_filenam);
      return(0);
   }
   fseek (bs->pt, bs->header_size, 0);

   bs->curpos=0;
   bs->totbits=0;
   bs->mode = READ_MODE;
   bs->eobs = FALSE;
   return(1);
}

/*close the device containing the bit stream after a read process*/
void close_bit_stream_r(Bit_stream *bs)
{
   fclose(bs->pt);
}

unsigned int get1bit(Bit_stream *bs)
{
   if (bs->curpos < bs->totbits)
   {
#ifdef	PrintBitDebug
      printf ("pos: %5d bits: %2d code: %4x val: %5d\n",
		bs->curpos,  1, bs->bits[bs->curpos], bs->bits[bs->curpos]);
      fflush (stdout);
#endif
      return (bs->bits[bs->curpos++]);
   }
   else
   {
      fprintf (stderr, "get1bits: bs->curpos = %4d bs->totbits = %4d bits\n",
	       bs->curpos, bs->totbits);
      exit (1);
   }
}

unsigned long getbits(Bit_stream *bs, int n)
{
   unsigned long val;
   int i;

   if (bs->curpos + n <= bs->totbits)
   {
      for (i = val = 0; i < n; i++)
         val = 2*val + bs->bits[bs->curpos++];

#ifdef	PrintBitDebug
      printf ("pos: %5d bits: %2d code: %4x val: %5d\n", 
		 bs->curpos - n,  n, val, val);
      fflush (stdout);
#endif

      return (val);
   }
   else
   {
      fprintf (stderr, "getbits: n: %4d bs->curpos: %4d bs->totbits = %4d bits\n",
	       n, bs->curpos, bs->totbits);
      exit (1);
   }
}

void program_information(void)
{
 printf("ISO MPEG Audio Subgroup Software Simulation Group (1996)\n");
 printf("ISO 13818-3 MPEG-2 Audio Multichannel Decoder\n");
 printf("%s\n", VERSION);
}

/*******************************************************************/

/*return the status of the bit stream*/
/* returns 1 if end of bit stream was reached */
/* returns 0 if end of bit stream was not reached */
int end_bs(Bit_stream *bs)
{
  return(bs->eobs);
}

static void bytes_to_bits (char *w_code, unsigned char *in, int nbytes)
{
  int i, j, bpos, d;

  bpos = 0;
  for (i = 0; i < nbytes; i++) {
    d = in[i];
    for (j = 7; j >= 0; j--) {
      w_code[bpos+j] = d & 1;
      d = d / 2;
    }
    bpos += 8;
  }
}

/*this function seeks for a byte aligned sync word in the bit stream and
  places the bit stream pointer right after the sync.
  This function returns 1 if the sync was found otherwise it returns 0 */

int seek_sync_mpg (Bit_stream *bs)   /* bit stream structure */
{
   unsigned long val = 0;
   unsigned char byte;
   unsigned char bytes[2000];	/* bytes of an MPEG-1 frame */
   long sync = SYNC_WORD;		/* sync word maximum 32 bits */
   int N = SYNC_WORD_LNGTH;	/* sync word length */
   int i, sync_bytes = N / 8;
   int version, layer, br_index, bit_rate, c, f_sampl, padding, slots, nbytes;

   if (fread (bytes, 1, 4, bs->pt) != 4)
   {
      printf ("next mpg 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 mpg sync found: %4x\n", val);
      return (0);
   }

   bytes_to_bits (bs->bits, bytes, 4);
  
   version = bs->bits[12];
   if (version == 0)
   {
      fprintf (stderr, "Error: Illegal version bit.\n");
      exit (1);
   }
   layer = 4 - 2*bs->bits[13] - bs->bits[14];
   for (i = 16, br_index = 0; i < 20; i++)
      br_index = 2*br_index + bs->bits[i];
   if (br_index == 0 || br_index == 15)
   {
      fprintf (stderr, "Error: Illegal bit_rate index (0 or 15).\n");
      exit (1);
   }
   bit_rate = bitrate[layer-1][br_index] * 1000;
   c = 2*bs->bits[20] + bs->bits[21];
   switch (c) {
   case 0: f_sampl = 44100;
	   break;
   case 1: f_sampl = 48000;
	   break;
   case 2: f_sampl = 32000;
	   break;
   case 3: fprintf (stderr, "Error: reserved sampling rate.\n");
	   exit (1);
   }
   padding = bs->bits[22];

   if (layer == 1)
      slots = bit_rate * 12 / f_sampl;
   else
      slots = bit_rate * 144 / f_sampl;

   slots += padding;

   if (layer == 1)
      nbytes = slots * 4;
   else
      nbytes = slots;

   if (fread (&bytes[4], 1, nbytes-4, bs->pt) != nbytes-4)
   {
      printf ("no n-4 bytes\n");
      return (0);
   }

   bytes_to_bits (&bs->bits[32], &bytes[4], nbytes-4);

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

   return (1);
}

int seek_sync_ext (Bit_stream *bs, frame_params *fr_ps)   /* bit stream structure */
{
   layer *info = fr_ps->header;

⌨️ 快捷键说明

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