From 4d01ace73fdffca366211157b54418516b3a73b6 Mon Sep 17 00:00:00 2001 From: Andree Buschmann Date: Wed, 17 Aug 2011 22:20:09 +0000 Subject: Submit a patch to the VGM codec by Mauricio Gama which saves some more RAM through changes of the buffer configuration and an update of the resampler code. Additionally enable VGM for low memory targets and update the manual. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@30327 a1c6a512-1295-4272-9138-f99709370657 --- apps/codecs/libgme/resampler.c | 426 ++++++++++++++++------------------------- 1 file changed, 163 insertions(+), 263 deletions(-) (limited to 'apps/codecs/libgme/resampler.c') diff --git a/apps/codecs/libgme/resampler.c b/apps/codecs/libgme/resampler.c index a69a92cc53..e2e0734fc9 100644 --- a/apps/codecs/libgme/resampler.c +++ b/apps/codecs/libgme/resampler.c @@ -25,294 +25,194 @@ enum { shift = 14 }; int const unit = 1 << shift; blargg_err_t Resampler_setup( struct Resampler* this, int fm_rate, int fm_gain, int rate, int gain ) - { - this->gain_ = (int)( ((1LL << gain_bits) * fm_gain * gain) / FP_ONE_GAIN ); - this->step = (int)( ((1LL << shift) * fm_rate) / rate + 1); - this->rate_ = this->step; - return 0; - } +{ + this->gain_ = (int)( ((1LL << gain_bits) * fm_gain * gain) / FP_ONE_GAIN ); + this->step = (int)( ((1LL << shift) * fm_rate) / rate + 1); + this->rate_ = this->step; + return 0; +} blargg_err_t Resampler_reset( struct Resampler* this, int pairs ) { - // expand allocations a bit - Resampler_resize( this, pairs ); - this->resampler_size = this->oversamples_per_frame + (this->oversamples_per_frame >> 2); - - this->buffer_size = this->resampler_size; - this->pos = 0; - this->write_pos = 0; - return 0; + // expand allocations a bit + this->sample_buffer_size = (pairs + (pairs >> 2)) * 2; + Resampler_resize( this, pairs ); + this->resampler_size = this->oversamples_per_frame + (this->oversamples_per_frame >> 2); + + this->buffer_size = this->resampler_size; + this->pos = 0; + this->write_pos = 0; + + Resampler_clear( this ); + return 0; } void Resampler_resize( struct Resampler* this, int pairs ) { - int new_sample_buf_size = pairs * 2; - if ( this->sample_buf_size != new_sample_buf_size ) - { - this->sample_buf_size = new_sample_buf_size; - this->oversamples_per_frame = (int) ((pairs * this->rate_ * 2LL) / unit) + 2; - Resampler_clear( this ); - } + int new_sample_buf_size = pairs * 2; + if ( this->sample_buf_size != new_sample_buf_size ) + { + if ( new_sample_buf_size > this->sample_buffer_size ) + { + check(false); + return; + } + + this->sample_buf_size = new_sample_buf_size; + this->oversamples_per_frame = (int) ((pairs * this->rate_ * 2LL) / unit) + 2; + Resampler_clear( this ); + } } -static void mix_mono( struct Resampler* this, struct Stereo_Buffer* stereo_buf, dsample_t* out_ ) +void mix_samples( struct Resampler* this, struct Blip_Buffer* blip_buf, sample_t out_ [] ) { - int const bass = BLIP_READER_BASS( stereo_buf->bufs [0] ); - BLIP_READER_BEGIN( sn, stereo_buf->bufs [0] ); - - int count = this->sample_buf_size >> 1; - BLIP_READER_ADJ_( sn, count ); - - typedef dsample_t stereo_dsample_t [2]; - stereo_dsample_t* BLARGG_RESTRICT out = (stereo_dsample_t*) out_ + count; - stereo_dsample_t const* BLARGG_RESTRICT in = - (stereo_dsample_t const*) this->sample_buf + count; - int offset = -count; - int const gain = this->gain_; - do - { - int s = BLIP_READER_READ_RAW( sn ) >> (blip_sample_bits - 16); - BLIP_READER_NEXT_IDX_( sn, bass, offset ); - - int l = (in [offset] [0] * gain >> gain_bits) + s; - int r = (in [offset] [1] * gain >> gain_bits) + s; - - BLIP_CLAMP( l, l ); - out [offset] [0] = (blip_sample_t) l; - - BLIP_CLAMP( r, r ); - out [offset] [1] = (blip_sample_t) r; - } - while ( ++offset ); - - BLIP_READER_END( sn, stereo_buf->bufs [0] ); + int const bass = BLIP_READER_BASS( *blip_buf ); + BLIP_READER_BEGIN( sn, *blip_buf ); + + int count = this->sample_buf_size >> 1; + BLIP_READER_ADJ_( sn, count ); + + typedef sample_t stereo_dsample_t [2]; + stereo_dsample_t* BLARGG_RESTRICT out = (stereo_dsample_t*) out_ + count; + stereo_dsample_t const* BLARGG_RESTRICT in = + (stereo_dsample_t const*) this->sample_buf + count; + int offset = -count; + int const gain = this->gain_; + do + { + int s = BLIP_READER_READ_RAW( sn ) >> (blip_sample_bits - 16); + BLIP_READER_NEXT_IDX_( sn, bass, offset ); + + int l = (in [offset] [0] * gain >> gain_bits) + s; + int r = (in [offset] [1] * gain >> gain_bits) + s; + + BLIP_CLAMP( l, l ); + out [offset] [0] = (blip_sample_t) l; + + BLIP_CLAMP( r, r ); + out [offset] [1] = (blip_sample_t) r; + } + while ( ++offset ); + + BLIP_READER_END( sn, *blip_buf ); } -static void mix_stereo( struct Resampler* this, struct Stereo_Buffer* stereo_buf, dsample_t* out_ ) +sample_t const* resample_( struct Resampler* this, sample_t** out_, + sample_t const* out_end, sample_t const in [], int in_size ) { - int const bass = BLIP_READER_BASS( stereo_buf->bufs [0] ); - BLIP_READER_BEGIN( snc, stereo_buf->bufs [0] ); - BLIP_READER_BEGIN( snl, stereo_buf->bufs [1] ); - BLIP_READER_BEGIN( snr, stereo_buf->bufs [2] ); - - int count = this->sample_buf_size >> 1; - BLIP_READER_ADJ_( snc, count ); - BLIP_READER_ADJ_( snl, count ); - BLIP_READER_ADJ_( snr, count ); - - typedef dsample_t stereo_dsample_t [2]; - stereo_dsample_t* BLARGG_RESTRICT out = (stereo_dsample_t*) out_ + count; - stereo_dsample_t const* BLARGG_RESTRICT in = - (stereo_dsample_t const*) this->sample_buf + count; - int offset = -count; - int const gain = this->gain_; - do - { - int sc = BLIP_READER_READ_RAW( snc ) >> (blip_sample_bits - 16); - int sl = BLIP_READER_READ_RAW( snl ) >> (blip_sample_bits - 16); - int sr = BLIP_READER_READ_RAW( snr ) >> (blip_sample_bits - 16); - BLIP_READER_NEXT_IDX_( snc, bass, offset ); - BLIP_READER_NEXT_IDX_( snl, bass, offset ); - BLIP_READER_NEXT_IDX_( snr, bass, offset ); - - int l = (in [offset] [0] * gain >> gain_bits) + sl + sc; - int r = (in [offset] [1] * gain >> gain_bits) + sr + sc; - - BLIP_CLAMP( l, l ); - out [offset] [0] = (blip_sample_t) l; - - BLIP_CLAMP( r, r ); - out [offset] [1] = (blip_sample_t) r; - } - while ( ++offset ); - - BLIP_READER_END( snc, stereo_buf->bufs [0] ); - BLIP_READER_END( snl, stereo_buf->bufs [1] ); - BLIP_READER_END( snr, stereo_buf->bufs [2] ); + in_size -= write_offset; + if ( in_size > 0 ) + { + sample_t* BLARGG_RESTRICT out = *out_; + sample_t const* const in_end = in + in_size; + + int const step = this->step; + int pos = this->pos; + + // TODO: IIR filter, then linear resample + // TODO: detect skipped sample, allowing merging of IIR and resample? + + do + { + #define INTERP( i, out )\ + out = (in [0 + i] * (unit - pos) + ((in [2 + i] + in [4 + i] + in [6 + i]) << shift) +\ + in [8 + i] * pos) >> (shift + 2); + + int out_0; + INTERP( 0, out_0 ) + INTERP( 1, out [0] = out_0; out [1] ) + out += stereo; + + pos += step; + in += ((unsigned) pos >> shift) * stereo; + pos &= unit - 1; + } + while ( in < in_end && out < out_end ); + + this->pos = pos; + *out_ = out; + } + return in; } -static void mix_stereo_no_center( struct Resampler* this, struct Stereo_Buffer* stereo_buf, dsample_t* out_ ) +inline int resample_wrapper( struct Resampler* this, sample_t out [], int* out_size, + sample_t const in [], int in_size ) { - int const bass = BLIP_READER_BASS( stereo_buf->bufs [0] ); - BLIP_READER_BEGIN( snl, stereo_buf->bufs [1] ); - BLIP_READER_BEGIN( snr, stereo_buf->bufs [2] ); - - int count = this->sample_buf_size >> 1; - BLIP_READER_ADJ_( snl, count ); - BLIP_READER_ADJ_( snr, count ); - - typedef dsample_t stereo_dsample_t [2]; - stereo_dsample_t* BLARGG_RESTRICT out = (stereo_dsample_t*) out_ + count; - stereo_dsample_t const* BLARGG_RESTRICT in = - (stereo_dsample_t const*) this->sample_buf + count; - int offset = -count; - int const gain = this->gain_; - do - { - int sl = BLIP_READER_READ_RAW( snl ) >> (blip_sample_bits - 16); - int sr = BLIP_READER_READ_RAW( snr ) >> (blip_sample_bits - 16); - BLIP_READER_NEXT_IDX_( snl, bass, offset ); - BLIP_READER_NEXT_IDX_( snr, bass, offset ); - - int l = (in [offset] [0] * gain >> gain_bits) + sl; - int r = (in [offset] [1] * gain >> gain_bits) + sr; - - BLIP_CLAMP( l, l ); - out [offset] [0] = (blip_sample_t) l; - - BLIP_CLAMP( r, r ); - out [offset] [1] = (blip_sample_t) r; - } - while ( ++offset ); - - BLIP_READER_END( snl, stereo_buf->bufs [1] ); - BLIP_READER_END( snr, stereo_buf->bufs [2] ); -} + assert( Resampler_rate( this ) ); -static dsample_t const* resample_( struct Resampler* this, dsample_t** out_, - dsample_t const* out_end, dsample_t const in [], int in_size ) -{ - in_size -= write_offset; - if ( in_size > 0 ) - { - dsample_t* BLIP_RESTRICT out = *out_; - dsample_t const* const in_end = in + in_size; - - int const step = this->step; - int pos = this->pos; - - // TODO: IIR filter, then linear resample - // TODO: detect skipped sample, allowing merging of IIR and resample? - - do - { - #define INTERP( i, out )\ - out = (in [0 + i] * (unit - pos) + ((in [2 + i] + in [4 + i] + in [6 + i]) << shift) +\ - in [8 + i] * pos) >> (shift + 2); - - int out_0; - INTERP( 0, out_0 ) - INTERP( 1, out [0] = out_0; out [1] ) - out += stereo; - - pos += step; - in += ((unsigned) pos >> shift) * stereo; - pos &= unit - 1; - } - while ( in < in_end && out < out_end ); - - this->pos = pos; - *out_ = out; - } - return in; -} + sample_t* out_ = out; + int result = resample_( this, &out_, out + *out_size, in, in_size ) - in; + assert( out_ <= out + *out_size ); + assert( result <= in_size ); -static inline int resample_wrapper( struct Resampler* this, dsample_t out [], int* out_size, - dsample_t const in [], int in_size ) -{ - assert( Resampler_rate( this ) ); - - dsample_t* out_ = out; - int result = resample_( this, &out_, out + *out_size, in, in_size ) - in; - assert( out_ <= out + *out_size ); - assert( result <= in_size ); - - *out_size = out_ - out; - return result; + *out_size = out_ - out; + return result; } -static int skip_input( struct Resampler* this, int count ) +int skip_input( struct Resampler* this, int count ) { - this->write_pos -= count; - if ( this->write_pos < 0 ) // occurs when downsampling - { - count += this->write_pos; - this->write_pos = 0; - } - memmove( this->buf, &this->buf [count], this->write_pos * sizeof this->buf [0] ); - return count; + this->write_pos -= count; + if ( this->write_pos < 0 ) // occurs when downsampling + { + count += this->write_pos; + this->write_pos = 0; + } + memmove( this->buf, &this->buf [count], this->write_pos * sizeof this->buf [0] ); + return count; } -static void play_frame_( struct Resampler* this, struct Stereo_Buffer* stereo_buf, dsample_t* out ) +void play_frame_( struct Resampler* this, struct Blip_Buffer* blip_buf, sample_t* out ) { - long pair_count = this->sample_buf_size >> 1; - blip_time_t blip_time = Blip_count_clocks( &stereo_buf->bufs [0], pair_count ); - int sample_count = this->oversamples_per_frame - this->write_pos + resampler_extra; - - int new_count = this->callback( this->callback_data, blip_time, sample_count, &this->buf [this->write_pos] ); - assert( new_count < resampler_size ); - - Buffer_end_frame( stereo_buf, blip_time ); - /* Blip_end_frame( &stereo_buf->bufs [0], blip_time ); */ - assert( Blip_samples_avail( &stereo_buf->bufs [0] ) == pair_count * 2 ); - - this->write_pos += new_count; - assert( (unsigned) this->write_pos <= this->buffer_size ); - - new_count = this->sample_buf_size; - if ( new_count ) - skip_input( this, resample_wrapper( this, this->sample_buf, &new_count, this->buf, this->write_pos ) ); - assert( new_count == (long) this->sample_buf_size ); - - int bufs_used = stereo_buf->stereo_added | stereo_buf->was_stereo; - if ( bufs_used <= 1 ) { - mix_mono( this, stereo_buf, out ); - Blip_remove_samples( &stereo_buf->bufs [0], pair_count ); - Blip_remove_silence( &stereo_buf->bufs [1], pair_count ); - Blip_remove_silence( &stereo_buf->bufs [2], pair_count ); - } - else if ( bufs_used & 1 ) { - mix_stereo( this, stereo_buf, out ); - Blip_remove_samples( &stereo_buf->bufs [0], pair_count ); - Blip_remove_samples( &stereo_buf->bufs [1], pair_count ); - Blip_remove_samples( &stereo_buf->bufs [2], pair_count ); - } - else { - mix_stereo_no_center( this, stereo_buf, out ); - Blip_remove_silence( &stereo_buf->bufs [0], pair_count ); - Blip_remove_samples( &stereo_buf->bufs [1], pair_count ); - Blip_remove_samples( &stereo_buf->bufs [2], pair_count ); - } - - // to do: this might miss opportunities for optimization - if ( !Blip_samples_avail( &stereo_buf->bufs [0] ) ) - { - stereo_buf->was_stereo = stereo_buf->stereo_added; - stereo_buf->stereo_added = 0; - } - - /* mix_mono( this, stereo_buf, out ); - Blip_remove_samples( &stereo_buf->bufs [0], pair_count ); */ + int pair_count = this->sample_buf_size >> 1; + blip_time_t blip_time = Blip_count_clocks( blip_buf, pair_count ); + int sample_count = this->oversamples_per_frame - this->write_pos + resampler_extra; + + int new_count = this->callback( this->callback_data, blip_time, sample_count, &this->buf [this->write_pos] ); + assert( new_count < this->resampler_size ); + + Blip_end_frame( blip_buf, blip_time ); + assert( Blip_samples_avail( blip_buf ) == pair_count ); + + this->write_pos += new_count; + assert( (unsigned) this->write_pos <= this->buffer_size ); + + int count = this->sample_buf_size; + if ( count ) + skip_input( this, resample_wrapper( this, this->sample_buf, &count, this->buf, this->write_pos ) ); + assert( count == this->sample_buf_size ); + + mix_samples( this, blip_buf, out ); + Blip_remove_samples( blip_buf, pair_count ); } -void Resampler_play( struct Resampler* this, long count, dsample_t* out, struct Stereo_Buffer* stereo_buf ) +void Resampler_play( struct Resampler* this, int count, sample_t* out, struct Blip_Buffer* blip_buf ) { - // empty extra buffer - long remain = this->sample_buf_size - this->buf_pos; - if ( remain ) - { - if ( remain > count ) - remain = count; - count -= remain; - memcpy( out, &this->sample_buf [this->buf_pos], remain * sizeof *out ); - out += remain; - this->buf_pos += remain; - } - - // entire frames - while ( count >= (long) this->sample_buf_size ) - { - play_frame_( this, stereo_buf, out ); - out += this->sample_buf_size; - count -= this->sample_buf_size; - } - - // extra - if ( count ) - { - play_frame_( this, stereo_buf, this->sample_buf ); - this->buf_pos = count; - memcpy( out, this->sample_buf, count * sizeof *out ); - out += count; - } + // empty extra buffer + int remain = this->sample_buf_size - this->buf_pos; + if ( remain ) + { + if ( remain > count ) + remain = count; + count -= remain; + memcpy( out, &this->sample_buf [this->buf_pos], remain * sizeof *out ); + out += remain; + this->buf_pos += remain; + } + + // entire frames + while ( count >= this->sample_buf_size ) + { + play_frame_( this, blip_buf, out ); + out += this->sample_buf_size; + count -= this->sample_buf_size; + } + + // extra + if ( count ) + { + play_frame_( this, blip_buf, this->sample_buf ); + this->buf_pos = count; + memcpy( out, this->sample_buf, count * sizeof *out ); + out += count; + } } -- cgit v1.2.3