home *** CD-ROM | disk | FTP | other *** search
- /**********************************************************************
- Copyright (c) 1991 MPEG/audio software simulation group, All Rights Reserved
- musicout.c
- **********************************************************************/
- /**********************************************************************
- * MPEG/audio coding/decoding software, work in progress *
- * NOT for public distribution until verified and approved by the *
- * MPEG/audio committee. For further information, please contact *
- * Davis Pan, 508-493-2241, e-mail: pan@3d.enet.dec.com *
- * *
- * VERSION 3.9 *
- * changes made since last update: *
- * date programmers comment *
- * 2/25/91 Douglas Wong start of version 1.0 records *
- * 3/06/91 Douglas Wong rename setup.h to dedef.h *
- * removed extraneous variables *
- * removed window_samples (now part of *
- * filter_samples) *
- * 3/07/91 Davis Pan changed output file to "codmusic" *
- * 5/10/91 Vish (PRISM) Ported to Macintosh and Unix. *
- * Incorporated new "out_fifo()" which *
- * writes out last incomplete buffer. *
- * Incorporated all AIFF routines which *
- * are also compatible with SUN. *
- * Incorporated user interface for *
- * specifying sound file names. *
- * Also incorporated user interface for *
- * writing AIFF compatible sound files. *
- * 27jun91 dpwe (Aware) Added musicout and &sample_frames as *
- * args to out_fifo (were glob refs). *
- * Used new 'frame_params' struct. *
- * Clean,simplify, track clipped output *
- * and total bits/frame received. *
- * 7/10/91 Earle Jennings changed to floats to FLOAT *
- *10/ 1/91 S.I. Sudharsanan, Ported to IBM AIX platform. *
- * Don H. Lee, *
- * Peter W. Farrett *
- *10/ 3/91 Don H. Lee implemented CRC-16 error protection *
- * newly introduced functions are *
- * buffer_CRC and recover_CRC_error *
- * Additions and revisions are marked *
- * with "dhl" for clarity *
- * 2/11/92 W. Joseph Carter Ported new code to Macintosh. Most *
- * important fixes involved changing *
- * 16-bit ints to long or unsigned in *
- * bit alloc routines for quant of 65535 *
- * and passing proper function args. *
- * Removed "Other Joint Stereo" option *
- * and made bitrate be total channel *
- * bitrate, irrespective of the mode. *
- * Fixed many small bugs & reorganized. *
- *19 aug 92 Soren H. Nielsen Changed MS-DOS file name extensions. *
- * 8/27/93 Seymour Shlien, Fixes in Unix and MSDOS ports, *
- * Daniel Lauzon, and *
- * Bill Truerniet *
- **********************************************************************/
-
- #include "common.h"
- #include "decoder.h"
-
-
- int highbytefirst = 0;
-
- /********************************************************************
- *
- * This part contains the MPEG I decoder for Layers I & II.
- *
- *********************************************************************/
-
- /****************************************************************
- *
- * For MS-DOS user (Turbo c) change all instance of malloc
- * to _farmalloc and free to _farfree. Compiler model hugh
- * Also make sure all the pointer specified are changed to far.
- *
- *****************************************************************/
-
- /*********************************************************************
- *
- * Core of the Layer II decoder. Default layer is Layer II.
- *
- *********************************************************************/
-
- /* Global variable definitions for "musicout.c" */
-
- char *programName;
-
- /* Implementations */
-
- main(argc, argv)
- int argc;
- char **argv;
- {
- typedef short PCM[2][3][SBLIMIT];
- PCM FAR *pcm_sample;
- typedef unsigned int SAM[2][3][SBLIMIT];
- SAM FAR *sample;
- typedef double FRA[2][3][SBLIMIT];
- FRA FAR *fraction;
- typedef double VE[2][HAN_SIZE];
- VE FAR *w;
-
- Bit_stream_struc bs;
- frame_params fr_ps;
- layer info;
- FILE *musicout;
- unsigned long sample_frames;
-
- int i, j, k, stereo, done=FALSE, clip, sync;
- int error_protection, crc_error_count, total_error_count;
- unsigned int old_crc, new_crc;
- unsigned int bit_alloc[2][SBLIMIT], scfsi[2][SBLIMIT],
- scale_index[2][3][SBLIMIT];
- unsigned long bitsPerSlot, samplesPerFrame, frameNum = 0;
- unsigned long frameBits, gotBits = 0;
- char encoded_file_name[MAX_NAME_SIZE];
- char decoded_file_name[MAX_NAME_SIZE];
- char t[50];
- int topSb = 0;
-
- /* Most large variables are declared dynamically to ensure
- compatibility with smaller machines */
-
- pcm_sample = (PCM FAR *) mem_alloc((long) sizeof(PCM), "PCM Samp");
- sample = (SAM FAR *) mem_alloc((long) sizeof(SAM), "Sample");
- fraction = (FRA FAR *) mem_alloc((long) sizeof(FRA), "fraction");
- w = (VE FAR *) mem_alloc((long) sizeof(VE), "w");
-
- fr_ps.header = &info;
- fr_ps.tab_num = -1; /* no table loaded */
- fr_ps.alloc = NULL;
- for (i=0;i<HAN_SIZE;i++) for (j=0;j<2;j++) (*w)[j][i] = 0.0;
-
- programName = argv[0];
- if(argc==1) usage();
-
- strcpy(encoded_file_name, argv[argc-2]);
- strcpy(decoded_file_name, argv[argc-1]);
- if ((argc == 4) && (stricmp(argv[2], "-h") != 0)) highbytefirst = 1;
-
- 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);
-
- 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) {
- done = TRUE;
- /* finally write out the buffer */
- if (info.lay == 2) 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;
-
- fprintf(stderr, "{%4lu}\r", 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);
- }
- 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);
- }
- break;
- }
-
- }
-
- }
-
- close_bit_stream_r(&bs);
- fclose(musicout);
-
- exit( 0 );
- }
-
-
-
- void usage() /* print syntax & exit */
- {
- fprintf(stderr, "MPEG-1 Audio decoder 0.01\n\n");
- fprintf(stderr, "Ported by Henrik Bjerregaard Pedersen, 14-May-1995\n\n");
- fprintf(stderr, "Usage: Decode [switches] <infile> <outfile>\n\n");
- fprintf(stderr, "Switches: -h output high byte first\n");
- exit(1);
- }
-