www.pudn.com > RTP通用开发库(for Linux).rar > musicout.c.patch
5c5 < * $Id: musicout.c.patch,v 1.2 2000/06/29 23:02:54 evangemh Exp $ --- > * $Id: musicout.c.patch,v 1.2 2000/06/29 23:02:54 evangemh Exp $ 7d6 < * $Log: musicout.c.patch,v $ < * Revision 1.2 2000/06/29 23:02:54 evangemh < * . < * 92a92,118 > #include> #include > #include > > #ifdef SOLARIS > #include > #endif > #ifdef LINUX > #include > #endif > > #include > #include > #include > #include > #include > #include > > /* > * Upcall functions needed by RTP library > * to extract MPEG audio frames. > */ > extern void out_audio(); > extern bool aligned(struct rtp *, int); > extern int dataoffset(struct rtp *, int); > extern mediatime_t frameduration(char *, int); > extern int framelength(char *, int); 110,111d135 < static void GetArguments(); < 124a149,151 > /* to implement rebuffering and resetting player on signal for testing */ > extern long totbit, offset,buf_bit_idx,buf_byte_idx; > 127,132c154,155 < main(argc, argv) < int argc; < char **argv; < { < /*typedef short PCM[2][3][SBLIMIT];*/ < typedef short PCM[2][SSLIMIT][SBLIMIT]; --- > int plaympeg(struct synsession *pssn, ssrc_t ssrc, int audiodev) { > typedef short PCM[2][SSLIMIT][SBLIMIT]; 134c157 < typedef unsigned int SAM[2][3][SBLIMIT]; --- > typedef unsigned int SAM[2][3][SBLIMIT]; 136c159 < typedef double FRA[2][3][SBLIMIT]; --- > typedef double FRA[2][3][SBLIMIT]; 138c161 < typedef double VE[2][HAN_SIZE]; --- > typedef double VE[2][HAN_SIZE]; 155d177 < Arguments_t Arguments; 158,159c180,181 < III_scalefac_t III_scalefac; < III_side_info_t III_side_info; --- > III_scalefac_t III_scalefac; > III_side_info_t III_side_info; 161,163c183,184 < #ifdef MACINTOSH < console_options.nrows = MAC_WINDOW_SIZE; < argc = ccommand(&argv); --- > #ifdef SOLARIS > audio_info_t audio_info; 164a186,191 > struct timespec now,packtime; > mediatime_t ts; > struct framereadparam freadparam; > int rv, tmp; > unsigned int span; > layer *rtp_info; 179,183c206,220 < Arguments.topSb = 0; < GetArguments(argc, argv, &Arguments); < if ((musicout = fopen(Arguments.decoded_file_name, "w+b")) == NULL) { < printf ("Could not create \"%s\".\n", Arguments.decoded_file_name); < exit(1); --- > /* get up the bitstream */ > bzero((char *) &freadparam, sizeof(struct framereadparam)); > bzero((char *) &bs, sizeof(bs)); > bs.buf = (char *) malloc(2048); > bs.format = BINARY; > bs.mode = READ_MODE; > bs.eob = FALSE; > bs.eobs = FALSE; > > sample_frames = 0; > > while (TRUE) { > synctl(pssn, SYN_CTL_STREAM_GETBUFSPAN, (char *) &span, ssrc); > if (span == 0) { > synrebuffer(pssn, ssrc); 185d221 < open_bit_stream_r(&bs, Arguments.encoded_file_name, BUFFER_SIZE); 187,191c223,231 < if (Arguments.need_aiff) < if (aiff_seek_to_sound_data(musicout) == -1) { < printf("Could not seek to PCM sound data in \"%s\".\n", < Arguments.decoded_file_name); < exit(1); --- > /* > * Read a frame. > */ > rv = synread(pssn, ssrc, &packtime, &ts, bs.buf, 2048, &freadparam); > > #ifdef LINUX > clock_gettime(CLOCK_REALTIME, &now); > if (timecmp(now, packtime) < 0) { > ioctl(audiodev, SNDCTL_DSP_SYNC); 192a233 > #endif 194c235,236 < sample_frames = 0; --- > if (rv < 0) > break; 196c238,242 < while (!end_bs(&bs)) { --- > bs.buf_size = rv; > bs.buf_byte_idx = 0; > bs.buf_bit_idx = 8; > bs.eob = FALSE; > bs.eobs = FALSE; 200,203d245 < 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); 210,214d251 < /* 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); 232d268 < if(frameNum == 0) WriteHdr(&fr_ps, stdout); /* printout layer/mode */ 234,239c270,293 < #ifdef ESPS < if (frameNum == 0 && Arguments.need_esps) { < esps_write_header(musicout,(long) sample_frames, (double) < s_freq[info.version][info.sampling_frequency] * 1000, < (int) stereo, Arguments.decoded_file_name ); < } /* MI */ --- > if(frameNum == 0) { > > rtp_info = fr_ps.header; > > /* > * Set the audiodevice's clock rate; > * all other parameters should already be set. > */ > > #ifdef SOLARIS > if (ioctl(audiodev, AUDIO_GETINFO, &audio_info) < 0) { > perror("ioctl1"); > exit(1); > } > > audio_info.play.sample_rate = (int) (s_freq[rtp_info->version][rtp_info->sampling_frequency] * 1000); > audio_info.play.channels = fr_ps.stereo; > audio_info.play.precision = 16; > audio_info.play.encoding = AUDIO_ENCODING_LINEAR; > > if (ioctl(audiodev, AUDIO_SETINFO, &audio_info) < 0) { > perror("ioctl2"); > exit(1); > } 240a295,333 > #ifdef LINUX > if (ioctl(audiodev, SNDCTL_DSP_RESET) < 0) { > perror("ioctl DSP_RESET"); > exit(1); > } > > tmp = APF_NETWORK; > if (ioctl(audiodev, SNDCTL_DSP_PROFILE, &tmp) < 0) { > perror("ioctl DSP_PROFILE"); > exit(1); > } > > if (ioctl(audiodev, SNDCTL_DSP_SYNC) < 0) { > perror("ioctl DSP_SAMPLESZ"); > exit(1); > } > > tmp = AFMT_S16_LE; > if (ioctl(audiodev, SNDCTL_DSP_SETFMT, &tmp) < 0) { > perror("ioctl DSP_SETFMT"); > exit(1); > } > > tmp = (int) (s_freq[rtp_info->version][rtp_info->sampling_frequency] * 1000); > if(ioctl(audiodev, SNDCTL_DSP_SPEED, &tmp) < 0) { > printf("ioctl SNDCTL_DSP_SPEED"); > exit(1); > } > > tmp = fr_ps.stereo; > if (ioctl(audiodev, SNDCTL_DSP_CHANNELS, &tmp) < 0) { > printf("ioctl SNDCLT_DSP_CHANNELS"); > exit(1); > } > #endif > printf("Audio device reconfigured.\n"); > } > > frameNum++; 242d334 < fprintf(stderr, "{%4lu}", frameNum++); fflush(stderr); 270,274d361 < if(Arguments.topSb>0) /* clear channels to 0 */ < for(j=Arguments.topSb; j out_audio(audiodev, *pcm_sample, 1, &fr_ps, &sample_frames); 282d367 < if(clip > 0) printf("%d output samples clipped\n", clip); 309,315d393 < if(Arguments.topSb>0) /* debug : clear channels to 0 */ < for(j=Arguments.topSb; j out_audio(audiodev, *pcm_sample, 3, &fr_ps, &sample_frames); 323d399 < if(clip > 0) printf("%d samples clipped\n", clip); 339a416 > 347a425 > 357,358c435 < printf("Not enough main data to decode frame %d. Frame discarded.\n", < frameNum - 1); break; --- > break; 419,420c496 < out_fifo(*pcm_sample, 18, &fr_ps, done, musicout, < &sample_frames); --- > out_audio(audiodev, *pcm_sample, 18, &fr_ps, &sample_frames); 422d497 < if(clip > 0) printf("%d samples clipped.\n", clip); 425c500 < } --- > } /* switch */ 427c502 < /* skip ancillary data HP 22-nov-95 */ --- > /* skip ancillary data HP 22-nov-95 */ 444,486c519,529 < if (Arguments.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.version][info.sampling_frequency]*1000; < pcm_aiff_data.sampleType = IFF_ID_SSND; < 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", < Arguments.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.version][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 (Arguments.need_aiff) < set_mac_file_attr(Arguments.decoded_file_name, VOL_REF_NUM, < CREATR_DEC_AIFF, FILTYP_DEC_AIFF); < else set_mac_file_attr(Arguments.decoded_file_name, VOL_REF_NUM, < CREATR_DEC_BNRY, FILTYP_DEC_BNRY); < #endif < < printf("Decoding of \"%s\" is finished\n", Arguments.encoded_file_name); < printf("The decoded PCM output file name is \"%s\"\n", Arguments.decoded_file_name); < if (Arguments.need_aiff) < printf("\"%s\" has been written with AIFF header information\n", < Arguments.decoded_file_name); --- > /* > * Flush last audio. > */ > out_audio(audiodev, NULL, -1, NULL, NULL); > > free(pcm_sample); > free(sample); > free(fraction); > free(w); > free(bs.buf); > return OK; 488d530 < exit( 0 ); 493,503d534 < 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); 507,629d537 < < static void GetArguments(argc, argv,Arguments) < int argc; < char **argv; < Arguments_t *Arguments; < < { < char t[50]; < < programName = argv[0]; < if(argc==1) { /* no command line args -> interact */ < do { < printf ("Enter encoded file name : "); < gets (Arguments->encoded_file_name); < if (Arguments->encoded_file_name[0] == NULL_CHAR) < printf ("Encoded file name is required. \n"); < } while (Arguments->encoded_file_name[0] == NULL_CHAR); < printf (">>> Encoded file name is: %s \n", Arguments->encoded_file_name); < #ifdef MS_DOS < printf ("Enter MPEG decoded file name <%s>: ", < new_ext(Arguments->encoded_file_name, DFLT_OPEXT)); /* 92-08-19 shn */ < #else < printf ("Enter MPEG decoded file name <%s%s>: ", < Arguments->encoded_file_name, < DFLT_OPEXT); < #endif < gets (Arguments->decoded_file_name); < if (Arguments->decoded_file_name[0] == NULL_CHAR) { < #ifdef MS_DOS < /* replace old extension with new one, 92-08-19 shn */ < strcpy(Arguments->decoded_file_name, < new_ext(Arguments->encoded_file_name, DFLT_OPEXT)); < #else < strcat (strcpy(Arguments->decoded_file_name, < Arguments->encoded_file_name), DFLT_OPEXT); < #endif < } < printf (">>> MPEG decoded file name is: %s \n", < Arguments->decoded_file_name); < < printf( < "Do you wish to write an AIFF compatible sound file ? (y/ ) : "); < gets(t); < if (*t == 'y' || *t == 'Y') Arguments->need_aiff = TRUE; < else Arguments->need_aiff = FALSE; < if (Arguments->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 wish to exit (last chance before decoding) ? (y/ ) : "); < gets(t); < if (*t == 'y' || *t == 'Y') exit(0); < } < else { /* interpret CL Args */ < int i=0, err=0; < < Arguments->need_aiff = FALSE; < Arguments->need_esps = FALSE; /* MI */ < Arguments->encoded_file_name[0] = '\0'; < Arguments->decoded_file_name[0] = '\0'; < < while(++i topSb = atoi(arg); argUsed = 1; < if(Arguments->topSb<1 || Arguments->topSb>SBLIMIT) { < fprintf(stderr, "%s: -s band %s not %d..%d\n", < programName, arg, 1, SBLIMIT); < err = 1; < } < break; < case 'A': Arguments->need_aiff = TRUE; break; < case 'E': Arguments->need_esps = TRUE; break; /* MI */ < 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(Arguments->encoded_file_name[0] == '\0') < strcpy(Arguments->encoded_file_name, argv[i]); < else < if(Arguments->decoded_file_name[0] == '\0') < strcpy(Arguments->decoded_file_name, argv[i]); < else { < fprintf(stderr, < "%s: excess arg %s\n", programName, argv[i]); < err = 1; < } < } < } < < if(err || Arguments->encoded_file_name[0] == '\0') usage(); /* never returns */ < < if(Arguments->decoded_file_name[0] == '\0') { < strcpy(Arguments->decoded_file_name, Arguments->encoded_file_name); < strcat(Arguments->decoded_file_name, DFLT_OPEXT); < } < < } < /* report results of dialog / command line */ < printf("Input file = '%s' output file = '%s'\n", < Arguments->encoded_file_name, Arguments->decoded_file_name); < if(Arguments->need_aiff) printf("Output file written in AIFF format\n"); < if(Arguments->need_esps) printf("Output file written in ESPS format\n"); /* MI */ < }