int toolame_set_bitrate(int brate) { int err = 0; /* check for a valid bitrate */ if (brate == 0) brate = bitrate[header.version][10]; /* Check to see we have a sane value for the bitrate for this version */ if ((header.bitrate_index = BitrateIndex (brate, header.version)) < 0) { err = 1; } if (header.dab_extension) { /* in 48 kHz (= MPEG-1) */ /* if the bit rate per channel is less then 56 kbit/s, we have 2 scf-crc */ /* else we have 4 scf-crc */ /* in 24 kHz (= MPEG-2), we have 4 scf-crc */ if (header.version == MPEG_AUDIO_ID && (brate / (header.mode == MPG_MD_MONO ? 1 : 2) < 56)) header.dab_extension = 2; } open_bit_stream_w(&bs, BUFFER_SIZE); return err; }
/*! Compress a given smplFrame at a given bitRate * * \param smplFrame a buffer containing samples to be compressed to layer3 * \param cfg pointer to an initialized config_t structure * \param bitRate bitRate to encode sample at * \return status : * - 0 => Success * < 0 => any error code */ int kb_mp3_Compress( unsigned short *smplFrame, unsigned long smplNmbr, struct config_t *cfg, int bitRate, char *filePath ) { int frames_processed; int channel; int i; int gr; double pe[2][2]; short *buffer_window[2]; double avg_slots_per_frame; double frac_slots_per_frame; long whole_slots_per_frame; double slot_lag; int mean_bits; int sideinfo_len; static short buffer[2][samp_per_frame]; static int l3_enc[2][2][samp_per_frame2]; static long l3_sb_sample[2][3][18][SBLIMIT]; static long mdct_freq[2][2][samp_per_frame2]; static L3_psy_ratio_t ratio; static L3_side_info_t side_info; static L3_scalefac_t scalefactor; static bitstream_t bs; if(cfg) cfg->mpeg.bitr = bitRate; /* Override the default bitRate */ if(filePath && cfg) cfg->outfile = filePath; else return -1; /* outfile related error */ open_bit_stream_w(&bs, cfg->outfile, BUFFER_SIZE); memset((char *)&side_info,0,sizeof(L3_side_info_t)); /* some mpeg layer 3 initialisations */ L3_subband_initialise(); L3_mdct_initialise(); L3_loop_initialise(); cfg->mpeg.samples_per_frame = samp_per_frame; cfg->mpeg.total_frames = smplNmbr /cfg->mpeg.samples_per_frame; cfg->mpeg.bits_per_slot = 8; frames_processed = 0; sideinfo_len = 288; /* Stereo assumed */ /* (config.wave.channels==1) ? 168 : 288; */ /* Figure average number of 'slots' per frame. */ avg_slots_per_frame = ((double)cfg->mpeg.samples_per_frame / ((double)smplNmbr/1000)) * ((double)cfg->mpeg.bitr / (double)cfg->mpeg.bits_per_slot); whole_slots_per_frame = (int)avg_slots_per_frame; frac_slots_per_frame = avg_slots_per_frame - (double)whole_slots_per_frame; slot_lag = -frac_slots_per_frame; if(frac_slots_per_frame==0) cfg->mpeg.padding = 0; //while(wave_get(buffer)) for( i = 0; i < smplNmbr; i++) { kb_mp3_updStatus(cfg, ++frames_processed); buffer_window[0] = buffer[0]; buffer_window[1] = buffer[1]; if(frac_slots_per_frame) if(slot_lag>(frac_slots_per_frame-1.0)) { /* No padding for this frame */ slot_lag -= frac_slots_per_frame; cfg->mpeg.padding = 0; } else { /* Padding for this frame */ slot_lag += (1-frac_slots_per_frame); cfg->mpeg.padding = 1; } cfg->mpeg.bits_per_frame = 8*(whole_slots_per_frame + cfg->mpeg.padding); mean_bits = (cfg->mpeg.bits_per_frame - sideinfo_len)>>1; /* polyphase filtering */ for(gr=0;gr<2;gr++) for(channel=2; channel-- ; ) /* assumed stereo */ /* for(channel=cfg->wave.channels; channel--; ) */ for(i=0;i<18;i++) L3_window_filter_subband(&buffer_window[channel], &l3_sb_sample[channel][gr+1][i][0] ,channel); /* apply mdct to the polyphase output */ L3_mdct_sub(cfg, l3_sb_sample, mdct_freq); /* bit and noise allocation */ L3_iteration_loop(cfg, pe,mdct_freq,&ratio,&side_info, l3_enc, mean_bits,&scalefactor); /* write the frame to the bitstream */ L3_format_bitstream(cfg, l3_enc,&side_info,&scalefactor, &bs,mdct_freq,NULL,0); } close_bit_stream(&bs); }
/* * L3_compress: * ------------ */ void L3_compress(config_t *config) { int frames_processed; int channel; int i; int gr; double pe[2][2]; short *buffer_window[2]; double avg_slots_per_frame; double frac_slots_per_frame; long whole_slots_per_frame; double slot_lag; int mean_bits; int sideinfo_len; static short buffer[2][samp_per_frame]; static int l3_enc[2][2][samp_per_frame2]; static long l3_sb_sample[2][3][18][SBLIMIT]; static long mdct_freq[2][2][samp_per_frame2]; static L3_psy_ratio_t ratio; static L3_side_info_t side_info; static L3_scalefac_t scalefactor; static bitstream_t bs; open_bit_stream_w(&bs, config->outfile, BUFFER_SIZE); memset((char *)&side_info,0,sizeof(L3_side_info_t)); L3_subband_initialise(); L3_mdct_initialise(); L3_loop_initialise(); config->mpeg.samples_per_frame = samp_per_frame; config->mpeg.total_frames = config->wave.total_samples/config->mpeg.samples_per_frame; config->mpeg.bits_per_slot = 8; frames_processed = 0; sideinfo_len = (config->wave.channels==1) ? 168 : 288; /* Figure average number of 'slots' per frame. */ avg_slots_per_frame = ((double)config->mpeg.samples_per_frame / ((double)config->wave.samplerate/1000)) * ((double)config->mpeg.bitr / (double)config->mpeg.bits_per_slot); whole_slots_per_frame = (int)avg_slots_per_frame; frac_slots_per_frame = avg_slots_per_frame - (double)whole_slots_per_frame; slot_lag = -frac_slots_per_frame; if(frac_slots_per_frame==0) config->mpeg.padding = 0; while(config->get_pcm(buffer, config)) { update_status(++frames_processed, config); buffer_window[0] = buffer[0]; buffer_window[1] = buffer[1]; if(frac_slots_per_frame) { if(slot_lag>(frac_slots_per_frame-1.0)) { /* No padding for this frame */ slot_lag -= frac_slots_per_frame; config->mpeg.padding = 0; } else { /* Padding for this frame */ slot_lag += (1-frac_slots_per_frame); config->mpeg.padding = 1; } } config->mpeg.bits_per_frame = 8*(whole_slots_per_frame + config->mpeg.padding); mean_bits = (config->mpeg.bits_per_frame - sideinfo_len)>>1; /* polyphase filtering */ for(gr=0;gr<2;gr++) for(channel=config->wave.channels; channel--; ) for(i=0;i<18;i++) L3_window_filter_subband(&buffer_window[channel], &l3_sb_sample[channel][gr+1][i][0] ,channel); /* apply mdct to the polyphase output */ L3_mdct_sub(l3_sb_sample, mdct_freq, config); /* bit and noise allocation */ L3_iteration_loop(pe,mdct_freq,&ratio,&side_info, l3_enc, mean_bits,&scalefactor, config); /* write the frame to the bitstream */ L3_format_bitstream(l3_enc,&side_info,&scalefactor, &bs,mdct_freq,NULL,0, config); } close_bit_stream(&bs); }
void parse_args (int argc, char **argv, frame_info * frame, int *psy, unsigned long *num_samples, char inPath[MAX_NAME_SIZE], char outPath[MAX_NAME_SIZE], char **mot_file, char **icy_file) { FLOAT srate; int brate; frame_header *header = frame->header; int err = 0, i = 0; long samplerate = 0; /* preset defaults */ inPath[0] = '\0'; outPath[0] = '\0'; header->lay = DFLT_LAY; switch (DFLT_MOD) { case 's': header->mode = MPG_MD_STEREO; header->mode_ext = 0; break; case 'd': header->mode = MPG_MD_DUAL_CHANNEL; header->mode_ext = 0; break; /* in j-stereo mode, no default header->mode_ext was defined, gave error.. now default = 2 added by MFC 14 Dec 1999. */ case 'j': header->mode = MPG_MD_JOINT_STEREO; header->mode_ext = 2; break; case 'm': header->mode = MPG_MD_MONO; header->mode_ext = 0; break; default: fprintf (stderr, "%s: Bad mode dflt %c\n", programName, DFLT_MOD); abort (); } *psy = DFLT_PSY; if ((header->sampling_frequency = SmpFrqIndex ((long) (1000 * DFLT_SFQ), &header->version)) < 0) { fprintf (stderr, "%s: bad sfrq default %.2f\n", programName, DFLT_SFQ); abort (); } header->bitrate_index = 14; brate = 0; switch (DFLT_EMP) { case 'n': header->emphasis = 0; break; case '5': header->emphasis = 1; break; case 'c': header->emphasis = 3; break; default: fprintf (stderr, "%s: Bad emph dflt %c\n", programName, DFLT_EMP); abort (); } header->copyright = 0; header->original = 0; header->error_protection = FALSE; header->dab_extension = 0; glopts.input_select = INPUT_SELECT_WAV; /* process args */ 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; if (!*token) { /* The user wants to use stdin and/or stdout. */ if (inPath[0] == '\0') strncpy (inPath, argv[i], MAX_NAME_SIZE); else if (outPath[0] == '\0') strncpy (outPath, argv[i], MAX_NAME_SIZE); } while ((c = *token++)) { if (*token /* NumericQ(token) */ ) arg = token; else arg = nextArg; switch (c) { case 'm': argUsed = 1; if (*arg == 's') { header->mode = MPG_MD_STEREO; header->mode_ext = 0; } else if (*arg == 'd') { header->mode = MPG_MD_DUAL_CHANNEL; header->mode_ext = 0; } else if (*arg == 'j') { header->mode = MPG_MD_JOINT_STEREO; } else if (*arg == 'm') { header->mode = MPG_MD_MONO; header->mode_ext = 0; } else { fprintf (stderr, "%s: -m mode must be s/d/j/m not %s\n", programName, arg); err = 1; } break; case 'y': *psy = atoi (arg); argUsed = 1; break; case 'L': glopts.show_level = 1; break; case 's': argUsed = 1; srate = atof (arg); /* samplerate = rint( 1000.0 * srate ); $A */ samplerate = (long) ((1000.0 * srate) + 0.5); if ((header->sampling_frequency = SmpFrqIndex ((long) samplerate, &header->version)) < 0) err = 1; break; case 'j': glopts.input_select = INPUT_SELECT_JACK; break; case 'b': argUsed = 1; brate = atoi (arg); break; case 'd': argUsed = 1; if (*arg == 'n') header->emphasis = 0; else if (*arg == '5') header->emphasis = 1; else if (*arg == 'c') header->emphasis = 3; else { fprintf (stderr, "%s: -d emp must be n/5/c not %s\n", programName, arg); err = 1; } break; case 'P': argUsed = 1; *mot_file = arg; break; case 'p': argUsed = 1; header->dab_length = atoi(arg); break; case 'c': header->copyright = 1; break; case 'o': header->original = 1; break; case 'e': header->error_protection = TRUE; break; case 'r': glopts.usepadbit = FALSE; header->padding = 0; break; case 'q': argUsed = 1; glopts.quickmode = TRUE; glopts.usepsy = TRUE; glopts.quickcount = atoi (arg); if (glopts.quickcount == 0) { /* just don't use psy model */ glopts.usepsy = FALSE; glopts.quickcount = FALSE; } break; case 'a': glopts.downmix = TRUE; header->mode = MPG_MD_MONO; header->mode_ext = 0; break; case 'x': glopts.byteswap = TRUE; break; case 'v': argUsed = 1; glopts.vbr = TRUE; glopts.vbrlevel = atof (arg); glopts.usepadbit = FALSE; /* don't use padding for VBR */ header->padding = 0; /* MFC Feb 2003: in VBR mode, joint stereo doesn't make any sense at the moment, as there are no noisy subbands according to bits_for_nonoise in vbr mode */ header->mode = MPG_MD_STEREO; /* force stereo mode */ header->mode_ext = 0; break; case 'V': glopts.input_select = INPUT_SELECT_VLC; break; case 'W': argUsed = 1; *icy_file = arg; break; case 'l': argUsed = 1; glopts.athlevel = atof(arg); break; case 'h': usage (); break; case 'g': glopts.channelswap = TRUE; break; case 't': argUsed = 1; glopts.verbosity = atoi (arg); break; default: fprintf (stderr, "%s: unrec 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 (inPath[0] == '\0') strcpy (inPath, argv[i]); else if (outPath[0] == '\0') strcpy (outPath, argv[i]); else { fprintf (stderr, "%s: excess arg %s\n", programName, argv[i]); err = 1; } } } /* Always enable DAB mode */ header->error_protection = TRUE; header->dab_extension = 2; header->padding = 0; glopts.dab = TRUE; if (header->dab_extension) { /* in 48 kHz */ /* if the bit rate per channel is less then 56 kbit/s, we have 2 scf-crc */ /* else we have 4 scf-crc */ /* in 24 kHz, we have 4 scf-crc, see main loop */ if (brate / (header->mode == MPG_MD_MONO ? 1 : 2) >= 56) header->dab_extension = 4; } if (err) usage (); /* If err has occured, then call usage() */ if (glopts.input_select != INPUT_SELECT_JACK && inPath[0] == '\0') usage (); /* If not in jack-mode and no file specified, then call usage() */ if (outPath[0] == '\0') { /* replace old extension with new one, 1992-08-19, 1995-06-12 shn */ new_ext (inPath, DFLT_EXT, outPath); } if (glopts.input_select == INPUT_SELECT_JACK) { musicin.jack_name = inPath; *num_samples = MAX_U_32_NUM; #if defined(JACK_INPUT) setup_jack(header, musicin.jack_name); #else fprintf(stderr, "JACK input not compiled in\n"); exit(1); #endif } else if (glopts.input_select == INPUT_SELECT_WAV) { if (!strcmp (inPath, "-")) { musicin.wav_input = stdin; /* read from stdin */ *num_samples = MAX_U_32_NUM; } else { if ((musicin.wav_input = fopen (inPath, "rb")) == NULL) { fprintf (stderr, "Could not find \"%s\".\n", inPath); exit (1); } parse_input_file (musicin.wav_input, inPath, header, num_samples); } } else if (glopts.input_select == INPUT_SELECT_VLC) { if (samplerate == 0) { fprintf (stderr, "Samplerate not specified\n"); exit (1); } *num_samples = MAX_U_32_NUM; int channels = (header->mode == MPG_MD_MONO) ? 1 : 2; #if defined(VLC_INPUT) if (vlc_in_prepare(glopts.verbosity, samplerate, inPath, channels, *icy_file) != 0) { fprintf(stderr, "VLC initialisation failed\n"); exit(1); } #else fprintf(stderr, "VLC input not compiled in\n"); exit(1); #endif } else { fprintf(stderr, "INVALID INPUT\n"); exit(1); } /* check for a valid bitrate */ if (brate == 0) brate = bitrate[header->version][10]; /* Check to see we have a sane value for the bitrate for this version */ if ((header->bitrate_index = BitrateIndex (brate, header->version)) < 0) err = 1; bs.zmq_framesize = 3 * brate; /* All options are hunky dory, open the input audio file and return to the main drag */ open_bit_stream_w (&bs, outPath, BUFFER_SIZE); }
MP3Stream::MP3Stream(StreamsManager *pStreamsManager, string name) : BaseAudioStream(pStreamsManager, ST_IN_AUDIO_MP3, name) { BaseAudioDevice *pAudioCapDevice= reinterpret_cast<BaseAudioDevice*> (HardwareManager::GetHardwareInstance(HT_MIC)); _pMP3streamCapabilities= new StreamCapabilities(); //pcmbuf = new uint8_t (PCM_PAGE_SIZE); //mp3 config L3_set_config_mpeg_defaults(&_mp3config.mpeg); //wave config _sampleRate = pAudioCapDevice->GetSampleRate(); _numOfChannel = pAudioCapDevice->GetNumberOfChannels(); _bitsPerSample = pAudioCapDevice->GetBitsPerSample(); _bitRate = _mp3config.mpeg.bitr;// //set wave type, TODO: removed _mp3config.wave.type = WAVE_RIFF_PCM; _mp3config.wave.channels = _numOfChannel; //mono _mp3config.wave.samplerate = _sampleRate; _mp3config.wave.bits = _bitsPerSample; //config sample rate index, and bit rate _mp3config.mpeg.samplerate_index = L3_find_samplerate_index(_sampleRate, _mp3config.mpeg.type); if ( _mp3config.mpeg.samplerate_index < 0) FATAL ("invalid sample rate in mp3stream ctor"); //TODO: should throw an execption _mp3config.mpeg.bitrate_index = L3_find_bitrate_index(_mp3config.mpeg.bitr, _mp3config.mpeg.type); if (_mp3config.mpeg.bitrate_index < 0) FATAL ("invliad bit rate in mpstream ctor"); open_bit_stream_w(&_bs, BITSSIZE); memset ((char*) &_side_info, 0, sizeof(L3_side_info_t)); L3_subband_initialise(); L3_mdct_initialise(); L3_loop_initialise(); _mp3config.mpeg.mode_gr = (_mp3config.mpeg.type==1)? 2: 1; _samplesInput = _mp3config.mpeg.samples_per_frame = (_mp3config.mpeg.type==1)? 1152:576; //config->mpeg.total_frames = (); _mp3config.mpeg.bits_per_slot = 8; _sideinfo_len = 32; if (_mp3config.mpeg.type==1) { if (_numOfChannel==1) _sideinfo_len += 136; else _sideinfo_len += 256; } else { //mpeg2 if (_numOfChannel==1) _sideinfo_len += 72; else _sideinfo_len += 136; } if (_mp3config.mpeg.crc) _sideinfo_len += 16; _avg_slots_per_frame = ((double)_samplesInput / ((double)_sampleRate/1000)) * ((double)_bitRate / (double)_mp3config.mpeg.bits_per_slot); _whole_slots_per_frame = (int)_avg_slots_per_frame; _frac_slots_per_frame = _avg_slots_per_frame - (double)_whole_slots_per_frame; _slot_lag = -_frac_slots_per_frame; if (_frac_slots_per_frame==0) _mp3config.mpeg.padding = 0; DEBUG ("mp3 stream info: _sideinfo_len:%d sampleInput:%d bitRate:%d sampleRate:%d avg slots per frame:%d", _sideinfo_len, _samplesInput, _bitRate, _sampleRate, _avg_slots_per_frame); GETTIMESTAMP(ats); mp3Duration=(1000.0/(double)_sampleRate)*(double)(_samplesInput/_numOfChannel); //m second _pMP3streamCapabilities->aac.InitAACCapability(_sampleRate, _numOfChannel, _bitsPerSample, (samplesInput/_numOfChannel), (_bitRate* 1000 *_numOfChannel), mp3Duration); //bitrate: 2 channel _pMP3streamCapabilities->audioCodecId = CODEC_AUDIO_MP3; #ifdef MP3STREAM_DEBUG if((pfOutputMP3=fopen("MICOut.mp3", "wb")) == NULL){ FATAL("Open output file 'MICOut.mp3' fail... !!\n"); exit(1); } if((pfPCM=fopen("MICOut.wav", "wb")) == NULL){ FATAL("Open output file 'MICOut.mp3' fail... !!\n"); exit(1); } DEBUG("Initial MP3Stream End..!!\n"); #endif }