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

📄 musicout.c

📁 mp3 源代码void III_hufman_decode
💻 C
📖 第 1 页 / 共 2 页
字号:
           encoded_file_name, decoded_file_name);
    if(need_aiff) printf("Output file written in AIFF format\n");
    if(need_esps) printf("Output file written in ESPS format\n"); /* MI */

    if ((musicout = fopen(decoded_file_name, "w+b")) == NULL) {
       printf ("Could not create \"%s\".\n", decoded_file_name);
       exit(1);
    }

    open_bit_stream_r(&bs, encoded_file_name, BUFFER_SIZE);

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

    sample_frames = 0;

    while (!end_bs(&bs)) {

       sync = seek_sync(&bs, SYNC_WORD, SYNC_WORD_LNGTH);
       frameBits = sstell(&bs) - gotBits;
       if(frameNum > 0)        /* don't want to print on 1st loop; no lay */
          if(frameBits%bitsPerSlot)
             fprintf(stderr,"Got %ld bits = %ld slots plus %ld\n",
                     frameBits, frameBits/bitsPerSlot, frameBits%bitsPerSlot);
       gotBits += frameBits;

       if (!sync) {
          printf("Frame cannot be located\n");
          printf("Input stream may be empty\n");
          done = TRUE;
          /* finally write out the buffer */
          if (info.lay != 1) out_fifo(*pcm_sample, 3, &fr_ps, done,
                                      musicout, &sample_frames);
          else               out_fifo(*pcm_sample, 1, &fr_ps, done,
                                      musicout, &sample_frames);
          break;
       }

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

#ifdef ESPS
if (frameNum == 0 && need_esps) {
esps_write_header(musicout,(long) sample_frames, (double)
s_freq[info.sampling_frequency] * 1000,
(int) stereo, decoded_file_name );
} /* MI */
#endif

       fprintf(stderr, "{%4lu}", frameNum++); fflush(stderr);
       if (error_protection) buffer_CRC(&bs, &old_crc);

       switch (info.lay) {

          case 1: {
             bitsPerSlot = 32;        samplesPerFrame = 384;
             I_decode_bitalloc(&bs,bit_alloc,&fr_ps);
             I_decode_scale(&bs, bit_alloc, scale_index, &fr_ps);

             if (error_protection) {
                I_CRC_calc(&fr_ps, bit_alloc, &new_crc);
                if (new_crc != old_crc) {
                   crc_error_count++;
                   total_error_count++;
                   recover_CRC_error(*pcm_sample, crc_error_count,
                                     &fr_ps, musicout, &sample_frames);
                   break;
                }
                else crc_error_count = 0;
             }

             clip = 0;
             for (i=0;i<SCALE_BLOCK;i++) {
                I_buffer_sample(&bs,(*sample),bit_alloc,&fr_ps);
                I_dequantize_sample(*sample,*fraction,bit_alloc,&fr_ps);
                I_denormalize_sample((*fraction),scale_index,&fr_ps);
                if(topSb>0)        /* clear channels to 0 */
                   for(j=topSb; j<fr_ps.sblimit; ++j)
                      for(k=0; k<stereo; ++k)
                         (*fraction)[k][0][j] = 0;

                for (j=0;j<stereo;j++) {
                   clip += SubBandSynthesis (&((*fraction)[j][0][0]), j,
                                             &((*pcm_sample)[j][0][0]));
                }
                out_fifo(*pcm_sample, 1, &fr_ps, done,
                         musicout, &sample_frames);
             }
             if(clip > 0) printf("%d output samples clipped\n", clip);
             break;
          }

          case 2: {
             bitsPerSlot = 8;        samplesPerFrame = 1152;
             II_decode_bitalloc(&bs, bit_alloc, &fr_ps);
             II_decode_scale(&bs, scfsi, bit_alloc, scale_index, &fr_ps);

             if (error_protection) { 
                II_CRC_calc(&fr_ps, bit_alloc, scfsi, &new_crc);
                if (new_crc != old_crc) {
                   crc_error_count++;
                   total_error_count++;
                   recover_CRC_error(*pcm_sample, crc_error_count,
                                     &fr_ps, musicout, &sample_frames);
                   break;
                }
                else crc_error_count = 0;
             }

             clip = 0;
             for (i=0;i<SCALE_BLOCK;i++) {
                II_buffer_sample(&bs,(*sample),bit_alloc,&fr_ps);
                II_dequantize_sample((*sample),bit_alloc,(*fraction),&fr_ps);
                II_denormalize_sample((*fraction),scale_index,&fr_ps,i>>2);

                if(topSb>0)        /* debug : clear channels to 0 */
                   for(j=topSb; j<fr_ps.sblimit; ++j)
                      for(k=0; k<stereo; ++k)
                         (*fraction)[k][0][j] =
                         (*fraction)[k][1][j] =
                         (*fraction)[k][2][j] = 0;

                for (j=0;j<3;j++) for (k=0;k<stereo;k++) {
                   clip += SubBandSynthesis (&((*fraction)[k][j][0]), k,
                                             &((*pcm_sample)[k][j][0]));
                }
                out_fifo(*pcm_sample, 3, &fr_ps, done, musicout,
                         &sample_frames);
             }
             if(clip > 0) printf("%d samples clipped\n", clip);
             break;
          }

          case 3: {
             int nSlots;
             int gr, ch, ss, sb, main_data_end, flush_main ;
	     int  bytes_to_discard ;
	     static int frame_start = 0;
             bitsPerSlot = 8;        samplesPerFrame = 1152;

             III_get_side_info(&bs, &III_side_info, &fr_ps);
             nSlots = main_data_slots(fr_ps);
             for (; nSlots > 0; nSlots--)  /* read main data. */
                hputbuf((unsigned int) getbits(&bs,8), 8);
	     main_data_end = hsstell() / 8; /*of privious frame*/
             if ( flush_main=(hsstell() % bitsPerSlot) ) { 
                hgetbits((int)(bitsPerSlot - flush_main));
		main_data_end ++;
	     }
             bytes_to_discard = frame_start - main_data_end
 			            - III_side_info.main_data_begin ;
             if( main_data_end > 4096 )
             {   frame_start -= 4096;
                 rewindNbytes( 4096 );
             }

             frame_start += main_data_slots(fr_ps);
             if (bytes_to_discard < 0) {
         printf("Not enough main data to decode frame %d.  Frame discarded.\n", 
                        frameNum - 1); break;
             }
             for (; bytes_to_discard > 0; bytes_to_discard--) hgetbits(8);

             clip = 0;
             for (gr=0;gr<2;gr++) {
               double lr[2][SBLIMIT][SSLIMIT],ro[2][SBLIMIT][SSLIMIT];

               for (ch=0; ch<stereo; ch++) {
                 long int is[SBLIMIT][SSLIMIT];   /* Quantized samples. */
                 int part2_start;
                 part2_start = hsstell();
                 III_get_scale_factors(III_scalefac,&III_side_info,gr,ch,
			&fr_ps);
                 III_hufman_decode(is, &III_side_info, ch, gr, part2_start,
                                   &fr_ps);
                 III_dequantize_sample(is, ro[ch], &III_scalefac,
                                   &(III_side_info.ch[ch].gr[gr]), ch, &fr_ps);
               }
               III_stereo(ro,lr,III_scalefac,
                            &(III_side_info.ch[0].gr[gr]), &fr_ps);
               for (ch=0; ch<stereo; ch++) {
                    double re[SBLIMIT][SSLIMIT];
                    double hybridIn[SBLIMIT][SSLIMIT];/* Hybrid filter input */
                    double hybridOut[SBLIMIT][SSLIMIT];/* Hybrid filter out */
                    double polyPhaseIn[SBLIMIT];     /* PolyPhase Input. */

                    III_reorder (lr[ch],re,&(III_side_info.ch[ch].gr[gr]),
                                  &fr_ps);
                    III_antialias(re, hybridIn, /* Antialias butterflies. */
                                  &(III_side_info.ch[ch].gr[gr]), &fr_ps);
                    for (sb=0; sb<SBLIMIT; sb++) { /* Hybrid synthesis. */
                        III_hybrid(hybridIn[sb], hybridOut[sb], sb, ch,
                                   &(III_side_info.ch[ch].gr[gr]), &fr_ps);
                    }
                    for (ss=0;ss<18;ss++) /*Frequency inversion for polyphase.*/
                       for (sb=0; sb<SBLIMIT; sb++)
                          if ((ss%2) && (sb%2))
                             hybridOut[sb][ss] = -hybridOut[sb][ss];
                    for (ss=0;ss<18;ss++) { /* Polyphase synthesis */
                        for (sb=0; sb<SBLIMIT; sb++)
                            polyPhaseIn[sb] = hybridOut[sb][ss];
                        clip += SubBandSynthesis (polyPhaseIn, ch,
                                                  &((*pcm_sample)[ch][ss][0]));
                        }
                    }
                /* Output PCM sample points for one granule. */
                out_fifo(*pcm_sample, 18, &fr_ps, done, musicout,
                         &sample_frames);
             }
             if(clip > 0) printf("%d samples clipped.\n", clip);
             break;
          }
       }
    }

    if (need_aiff) {
       pcm_aiff_data.numChannels       = stereo;
       pcm_aiff_data.numSampleFrames   = sample_frames;
       pcm_aiff_data.sampleSize        = 16;
       pcm_aiff_data.sampleRate        = s_freq[info.sampling_frequency]*1000;
#ifdef IFF_LONG
       pcm_aiff_data.sampleType        = IFF_ID_SSND;
#else
       strncpy(&pcm_aiff_data.sampleType,IFF_ID_SSND,4);
#endif
       pcm_aiff_data.blkAlgn.offset    = 0;
       pcm_aiff_data.blkAlgn.blockSize = 0;

       if (aiff_write_headers(musicout, &pcm_aiff_data) == -1) {
          printf("Could not write AIFF headers to \"%s\"\n",
                 decoded_file_name);
          exit(2);
       }
    }

    printf("Avg slots/frame = %.3f; b/smp = %.2f; br = %.3f kbps\n",
           (FLOAT) gotBits / (frameNum * bitsPerSlot),
           (FLOAT) gotBits / (frameNum * samplesPerFrame),
           (FLOAT) gotBits / (frameNum * samplesPerFrame) *
           s_freq[info.sampling_frequency]);

    close_bit_stream_r(&bs);
    fclose(musicout);

    /* for the correct AIFF header information */
    /*             on the Macintosh            */
    /* the file type and the file creator for  */
    /* Macintosh compatible Digidesign is set  */

#ifdef  MACINTOSH
    if (need_aiff) set_mac_file_attr(decoded_file_name, VOL_REF_NUM,
                                     CREATR_DEC_AIFF, FILTYP_DEC_AIFF);
    else           set_mac_file_attr(decoded_file_name, VOL_REF_NUM,
                                     CREATR_DEC_BNRY, FILTYP_DEC_BNRY);
#endif

    printf("Decoding of \"%s\" is finished\n", encoded_file_name);
    printf("The decoded PCM output file name is \"%s\"\n", decoded_file_name);
    if (need_aiff)
       printf("\"%s\" has been written with AIFF header information\n",
              decoded_file_name);

    exit( 0 );
}

static void usage()  /* print syntax & exit */
{
   fprintf(stderr,
      "usage: %s                         queries for all arguments, or\n",
       programName);
   fprintf(stderr,
      "       %s [-A][-s sb] inputBS [outPCM]\n", programName);
   fprintf(stderr,"where\n");
   fprintf(stderr," -A       write an AIFF output PCM sound file\n");
   fprintf(stderr," -s sb    resynth only up to this sb (debugging only)\n");
   fprintf(stderr," inputBS  input bit stream of encoded audio\n");
   fprintf(stderr," outPCM   output PCM sound file (dflt inName+%s)\n",
           DFLT_OPEXT);
   exit(1);
}

⌨️ 快捷键说明

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