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

📄 musicout.c

📁 MPEG 2的音频编码软件。喜欢多媒体的开发人员可以看看。
💻 C
📖 第 1 页 / 共 4 页
字号:
	   if ((open_bit_stream_r (&bs_ext, ext_bitstream_name, BUFFER_SIZE)) == 0)
	   { 	
		printf("No extension bitstream <%s> present \n",ext_bitstream_name);
		printf("Do you want to decode an MPEG 2 bitstream ? (<y>/n) : ");
		gets(t);
	   	if (*t == 'N' || *t == 'n') 
		{
			mpeg = 1;
			printf("An MPEG-1 Layer II bitstream will be decoded \n");
		}
	   	else
		{
			mpeg = 2;
			printf("An MPEG-2 Layer II bitstream will be decoded \n");
		}
	   } 
	   printf("Do you wish to write an AIFF compatible sound file ? (<y>/n) : ");
	   gets(t);
	   if (*t == 'N' || *t == 'n') need_aiff = FALSE;
	   else                        need_aiff = TRUE;
	   if (need_aiff)
		printf(">>> An AIFF compatible sound file will be written\n");
	   else printf(">>> A non-headered PCM sound file will be written\n");
	   
	   printf("Do you want to print out all decoding information ? (y/<n>) :");
	   gets(t);
	   if( *t == 'y' || *t == 'Y' ) print_out = 1;
	   else		      		print_out = 0;

	   printf(
		  "Do you wish to exit (last chance before decoding) ? (y/<n>) : ");
	   gets(t);
	   if (*t == 'y' || *t == 'Y') exit(0);
	}

	else	/************** interpret CL Args *****************/
		/* default: -write an AIFF file
	 		    -print encoded bitstream information  */
	{        
	   int i=0, err=0;

#ifdef UNIX /* default values */
	   need_aiff = TRUE;
	   print_out = 0;   /* 10/31/95 Ralf Schwalbe: set print_out off in CL Args */
	   mpeg = 2;
#else
	   need_aiff = FALSE;
#endif
	   encoded_file_name[0] = NULL_CHAR;
	   decoded_file_name[0] = NULL_CHAR;
	   ext_bitstream_name[0] = NULL_CHAR;

	   while(++i<argc && err == 0)
	   {
		  char c, *token, *arg, *nextArg;
		  int  argUsed;

		  token = argv[i];
		  if(*token++ == '-')
		  {
			 if(i+1 < argc) nextArg = argv[i+1];
			 else           nextArg = "";
			 argUsed = 0;
			 while((c = *token++) != 0)
			 {
				if(*token /* NumericQ(token) */) arg = token;
				else                             arg = nextArg;
				switch(c)
				{
				   case 'a':  need_aiff = FALSE;
					      break;

				   case 'b':  bits_log = 1;
					      break;
					    			
				   case 'd':  dynx_log = 1;
					      break;
					    			
				   case 'h':  bs_mpg.header_size = 2304;
					      bs_ext.header_size = 2304;
					      break;

				   case 'i':  print_out = 1;
					      break;
					    			
				   case 'm':  mpeg = 1;
					      break;
#ifdef Augmentation_7ch
				   case 'n':  mpeg = 3;
					      break;
#endif
				   case 'r':  rate_log = 1;
					      break;
					    			
				   case 's':  scfsi_log = 1;
					      break;
					    			
				   case 't':  tca_log = 1;
					      break;
					    			
				   default:   fprintf(stderr,"%s: unrecognized option %c\n",
							  programName, c);
					      err = 1;
					      break;
				}
				if(argUsed)
				{
					if(arg == token) token = ""; /* no more from token */
					else  ++i; /* skip arg we used */
					arg = ""; argUsed = 0;
				}
			 }
		  }
		  else
		  {
		     if (encoded_file_name[0] == NULL_CHAR)
		     {
			strcpy (encoded_file_name, argv[i]);
			f = strlen (encoded_file_name);
			strcpy (&encoded_file_name[f], DFLT_IPEXT_MPG);
			strcpy (ext_bitstream_name, encoded_file_name);
			strcpy (&ext_bitstream_name[f], DFLT_IPEXT_EXT);
		     }
		     else if (decoded_file_name[0] == NULL_CHAR)
		     {
			strcpy (decoded_file_name, argv[i]);
			f = strlen (decoded_file_name);
			strcpy (&decoded_file_name[f], DFLT_OPEXT_DEC);
		     }
		     else
		     {
			fprintf (stderr, "%s: excess arg %s\n", programName, argv[i]);
			err = 1;
		     }
		  }
       } /* end while argc */

       if (bits_log || dynx_log || rate_log || scfsi_log || tca_log)
	  any_log = 1;


       if (err || encoded_file_name[0] == NULL_CHAR)
          usage ();  /* never returns */

       if ((open_bit_stream_r (&bs_ext, ext_bitstream_name, BUFFER_SIZE)) == 0)
       {
		printf("No extension bitstream <%s> present \n", ext_bitstream_name);
		ext_bitstream_name[0] = NULL_CHAR;
       }
       
       if (decoded_file_name[0] == NULL_CHAR)
       {
	  f = strlen (encoded_file_name) - 4;
	  strcpy (decoded_file_name, encoded_file_name);
	  strcpy (&decoded_file_name[f], DFLT_OPEXT_DEC);
       }

    }  /* else arg-line end */

    /* JMZ 10/03/1995 Multilingual */
    strcpy(decoded_file_name_ml, decoded_file_name);
    strcat(decoded_file_name_ml, DFLT_OPEXT_ML);

	/* report results of dialog / command line */
    printf("Input file = '%s' \nOutput file = '%s' ",
		   encoded_file_name, decoded_file_name);
    if (mpeg >= 2 && ext_bitstream_name[0] != NULL_CHAR)
    	printf("Ext_bitstream = '%s'\n", ext_bitstream_name);
    if(mpeg == 1)
	   	printf(">>>\nA MPEG 1 Layer-1/2 bitstream will be decoded\n");
	   else
		printf(">>>\nA MPEG 2 Layer-1/2 bitstream will be decoded\n");  
	  
    if(need_aiff) printf("Output file written in AIFF format\n");

    if ((musicout = fopen(decoded_file_name, "w+b")) == NULL)
    {
	   fprintf (stderr, "Could not create \"%s\".\n", decoded_file_name);
	   exit (1);
    }
    if (open_bit_stream_r (&bs_mpg, encoded_file_name, BUFFER_SIZE) == 0)
    {
	fprintf (stderr, "Could not open \"%s\".\n", encoded_file_name);
	exit (1);
    }

    if (need_aiff)
	  if (aiff_seek_to_sound_data(musicout) == -1)
	  {
		  fprintf (stderr, "Could not seek to PCM sound data in \"%s\".\n",
				 decoded_file_name);
		  exit (1);
	   }

    sample_frames = 0;
    sample_frames_ml = 0;
    total_error_count = total_error_count_mc = 0;
#ifdef Augmentation_7ch
    total_error_count_aug = 0;
#endif

    while (!end_bs (&bs_mpg))
    {
    /****************************************************************/
    /*                                                              */
    /*                  START OF FRAME LOOP                         */
    /*                                                              */
    /****************************************************************/

	   sync = seek_sync_mpg (&bs_mpg);
	   frameBits = bs_mpg.totbits;
	   if (!sync)
	   {
		  printf("\nFrame cannot be located, end of input stream \n");
		  done = TRUE;
		  /* finally write out the buffer */
		  out_fifo (*pcm_sample, 3, &fr_ps, done, musicout, &sample_frames, channels);
		  out_fifo_ml (*pcm_sample_ml, 3, &fr_ps, done, musicout_ml, &sample_frames_ml);
		  break;
	   }

	   info.lfe = 0;  		/* R.S. ! */
	   decode_info (&bs_mpg, &fr_ps);
	   hdr_to_frps (&fr_ps);

	   stereo = fr_ps.stereo;
	   error_protection = info.error_protection;
	   crc_error_count = 0;
	   if (frameNum == 0) WriteHdr (&fr_ps, stdout);  /* printout layer/mode */

	   if (frameNum % frameMod == 0)
	   {
		fprintf(stderr, "{%4lu}\r", frameNum++); fflush(stderr);
		if (frameNum / frameMod >= 100) frameMod *= 10;
	   }
	   else
		frameNum++;

	   Frame_Bits = 144*Bitrate/S_freq; 
	   if(info.padding)
		Frame_Bits++;    /* R.Schwalbe add a padding byte */
	   Frame_Bits*=8;	 /* R.Schwalbe to get bits per frame *8 */
	   if (error_protection) buffer_CRC(&bs_mpg, &old_crc);
          

           /* 7/8/95 added message WtK 7/8/95 */
	   if (info.lay == 3)
	   {
	     fprintf (stderr, "\nLayer = %d, not supported.\nexit.\n\n",info.lay); break;
	     exit (1);
	   }
	   
	   bs_mc.totbits = bs_mc.curpos = 0;

	   if (info.lay == 1)
	   {
	      if (mpeg > 1)
		 layer_I_frames = 3;
	      else
	      	 layer_I_frames = 1;

	      for (part = 0; part < layer_I_frames; part++)
	      {
		 bitsPerSlot = 32;
		 samplesPerFrame = 384;
		 ch_start = 0;
		 channels = stereo;

		 if (part > 0)
		 {
		    sync = seek_sync_mpg (&bs_mpg);
		    frameBits = bs_mpg.totbits;
		    if (!sync)
		    {
			   fprintf (stderr, "2nd or 3th Frame cannot be located, end of input stream\n");
			   exit (1);
		    }

		    decode_info (&bs_mpg, &fr_ps);
		    hdr_to_frps (&fr_ps);
	 
		    stereo = fr_ps.stereo;
		    error_protection = info.error_protection;
		    crc_error_count = 0;

		    Frame_Bits = 12*Bitrate/S_freq; 
		    if (info.padding)
			 Frame_Bits++;    /* R.Schwalbe add a padding byte */
		    Frame_Bits *=32;	 /* R.Schwalbe to get bits per frame *32 */
		    if (error_protection)
		       buffer_CRC (&bs_mpg, &old_crc);
          
		    /* 7/8/95 added message WtK 7/8/95 */
		    if (info.lay != 1)
		    {
		      fprintf (stderr, "Change of Layer = %d, not supported.\nexit.\n\n", info.lay);
		      exit (1);
		    }
		 }

		 I_decode_bitalloc (&bs_mpg, &fr_ps, I_bit_alloc[part], bits_log);
		 I_decode_scale (&bs_mpg, &fr_ps, I_bit_alloc[part], scale_index, part, scfsi_log);

		 if (error_protection)
		 {
			I_CRC_calc (&fr_ps, I_bit_alloc[part], &new_crc);
			if (new_crc != old_crc)
			{
			   printf(" \n ERROR in LAYER 1 - CRC! \n");
			   crc_error_count++;
			   total_error_count++;
			   recover_CRC_error (*pcm_sample, crc_error_count,
					 &fr_ps, musicout, &sample_frames, channels);
			}
			else
			   crc_error_count = 0;
		 }

		 clip = 0;

		 for (i=0;i<12;i++)
		 {
			I_buffer_sample (&bs_mpg, &fr_ps, (*sample), I_bit_alloc[part]);
if (!any_log)
{
			I_dequantize_sample ((*sample), part, I_bit_alloc[part], *fraction, &fr_ps, &i);
			I_denormalize_sample (*fraction, scale_index, part, &fr_ps, &i);
}
		 }   /* end of for loop */

		 if (mpeg > 1)
		 {
		    while (bs_mpg.curpos < bs_mpg.totbits-1)
		       bs_mc.bits[bs_mc.totbits++] = bs_mpg.bits[bs_mpg.curpos++];

		    if (part == 0)
		    {
		       mc_header (&bs_mc, &fr_ps);
		       mc_hdr_to_frps (&fr_ps);
		    }

		    bs_mc.totbits -= fr_ps.header->n_ad_bytes * 8;
		 }
	      }
	      if (mpeg > 1)
	      {
		 if (fr_ps.header->ext_bit_stream_present)
		 {
		    sync = seek_sync_ext (&bs_ext, &fr_ps);

⌨️ 快捷键说明

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