/* MuSE - Multiple Streaming Engine * Copyright (C) 2000-2003 Denis Rojo aka jaromil * * This source code is free software; you can redistribute it and/or * modify it under the terms of the GNU Public License as published * by the Free Software Foundation; either version 2 of the License, * or (at your option) any later version. * * This source code is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * Please refer to the GNU Public License for more details. * * You should have received a copy of the GNU Public License along with * this source code; if not, write to: * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * "$Id: out_vorbis.cpp 465 2005-05-25 23:27:24Z xant $" * */ #include #include #include #include #ifdef HAVE_VORBIS #include #include #include OutVorbis::OutVorbis() : OutChannel("vorbis") { func("OutVorbis::OutVorbis() %p",this); headersize = 0; sprintf(name,"Ogg/Vorbis encoder"); sprintf(version," "); tipo = OGG; /* initialize resampler */ pcm = (float*) calloc(INBUF, sizeof(float)); rsmpled = (float*) calloc(RSMPBUF, sizeof(float)); rsmp_state = NULL; encoder_initialized = false; } bool OutVorbis::init() { func("OutVorbis::init() %p",this); // vorbis_info_init(&vi); // act("initializing %s %i",name,vi.version); if(!apply_profile()) { error("problems in setting up codec parameters"); // vorbis_info_clear(&vi); return false; } erbapipa->set_output_type("copy_int16_to_float"); initialized = true; return initialized; } /* note: num is number of samples in the L (or R) channel not the total number of samples in pcm[] */ int OutVorbis::encode() { int num = 0, samples = 0; int rsmperr, res; _pbyte = (int8_t*)buffer; if(headersize) { memcpy(buffer,header,headersize); encoded = headersize; shout(); dump(); } encoded = headersize = 0; if(erbapipa->size() < out_chunk_len) { // bytes, not samples, 16bit stereo stuff // func("OutVorbis::encode : not enough data in pipe"); return -1; } /* this now takes bytes and returns bytes */ num = erbapipa->read(OUT_CHUNK,pcm); // QUAA or?: num = erbapipa->read(OUT_CHUNK*2,pcm); if(numread_float_bidi reads %i samples instead of %i", num,OUT_CHUNK); if(num<1) return num; rsmp_data.data_in = pcm; rsmp_data.input_frames = num; rsmp_data.data_out = rsmpled; rsmp_data.output_frames = RSMPBUF; /* do the resampling */ rsmperr = src_process(rsmp_state,&rsmp_data); if(rsmperr) { error("error in ogg resampler: %s",src_strerror(rsmperr)); if(rsmperr==6) error("resampling ratio is %.4f",rsmp_data.src_ratio); } samples = rsmp_data.output_frames_gen; func("%i frames resampled in %i with ratio %.4f (num:%u OUT_CHUNK:%u)", rsmp_data.input_frames_used, samples, rsmp_data.src_ratio, num, OUT_CHUNK); /* initialize the vorbis encoder to work on the resampled size */ _intbuf = vorbis_analysis_buffer(&vd,samples); /* move resampled pcm into the vorbis float array i tryed to avoid in every way this extra mov of memory but there is no way to predict the resampled buffer size exactly before actually doing the resampling. this prevents from initializing the vorbis buffer BEFORE doing the resampling, to store the resampled pcm directly into it (both the output of the resampler and the input of the vorbis encoder take a bidimensional float array). currently this causes a bit of overhead, but is the only way. */ prepare(rsmpled,_intbuf,samples); res = vorbis_analysis_wrote(&vd,samples); if(res) { /* nonzero is error */ error("OutVorbis::encode : error %i from vorbis_analysis_wrote"); return 0; } /* encode one block at a time... */ while(vorbis_analysis_blockout(&vd,&vb)==1) { /* Do the main analysis, creating a packet */ res = vorbis_analysis(&vb, NULL); /* here as a second argument darkice uses &op while oggenc uses NULL */ if(res) { /* nonzero is error */ error("OutVorbis::encode : error %i from vorbis_analysis_wrote"); return 0; } res = vorbis_bitrate_addblock(&vb); if(res) { /* nonzero is error */ error("OutVorbis::encode : error %i from vorbis_bitrate_addblock"); return 0; } while(vorbis_bitrate_flushpacket(&vd, &op)) { /* Add packet to bitstream */ res = ogg_stream_packetin(&os,&op); if(res) { /* nonzero is error */ error("OutVorbis::encode : error %i from ogg_stream_packetin"); return 0; } while(ogg_stream_pageout(&os,&og)) { memcpy(_pbyte,og.header,og.header_len); _pbyte += og.header_len; memcpy(_pbyte,og.body,og.body_len); _pbyte += og.body_len; encoded += og.header_len + og.body_len; /* TODO: use throw / catch mechanism for EOS */ // if(ogg_page_eos(&og)) { func("Ogg encoder EOS!"); return encoded; } } } } return encoded; } int OutVorbis::prepare(float *buf, float **fbuf, int num) { // this function distributes interlaced data in two l/r arrays int i=0; switch(channels()) { case 1: for(i=num-1; i>=0; i--) fbuf[0][i] = buf[i]; // short buf: (buf[i*2] + buf[i*2+1]) / 65536.0f; // /2 /32768.0f; break; case 2: for(i=0; ibps( tmp ); snprintf(tmp,256,"%u",freq()); ice->freq( tmp ); snprintf(tmp,256,"%u",channels()); ice->channels( tmp ); // snprintf(tmp,256,"%.2f",_quality); ice->quality( tmp ); ice = (Shouter*)ice->next; } /* to save this calculation */ out_chunk_len = OUT_CHUNK * sizeof(float) * 2; /* Turn off management entirely (if it was turned on) */ // vorbis_encode_ctl(&vi, OV_ECTL_RATEMANAGE_SET, NULL); // if( vorbis_encode_setup_managed // (&vi, channels(), freq(), -1, bps()*1000, -1 ) ) { // (&vi, 2, SAMPLE_RATE, -1, bps()*1000, -1 ) ) { // error("vorbis_encode_setup_managed failed: invalid bitrate %i",bps()); // res = false; // } return res; } OutVorbis::~OutVorbis() { func("OutVorbis::~OutVorbis() %p",this); // if(running) flush(); act("closing ogg/vorbis encoder"); if(rsmp_state) src_delete(rsmp_state); free(pcm); free(rsmpled); if(encoder_initialized) { ogg_stream_clear(&os); vorbis_block_clear(&vb); vorbis_dsp_clear(&vd); vorbis_info_clear(&vi); } } #endif