convert blocks until the buffer is full

Move the inner loop which converts samples to flac_convert().  There
it is isolated and easier to optimize.  This function does not have to
worry about buffer boundaries; the caller (i.e. flacWrite())
calculates how much is left and is responsible for flushing.  That
saves a lot of superfluous range checks within the loop.

git-svn-id: https://svn.musicpd.org/mpd/trunk@7327 09075e82-0dd4-0310-85a5-a0d7c8717e4f
This commit is contained in:
Max Kellermann 2008-04-12 04:20:13 +00:00 committed by Eric Wong
parent 0673c9a84d
commit ca1090f93b

View File

@ -206,6 +206,27 @@ static void flacMetadata(const flac_decoder * dec,
flac_metadata_common_cb(block, (FlacData *) vdata); flac_metadata_common_cb(block, (FlacData *) vdata);
} }
static void flac_convert(unsigned char *dest,
const FLAC__Frame * frame,
unsigned int bytes_per_sample,
const FLAC__int32 * const buf[],
unsigned int position, unsigned int end)
{
unsigned int c_chan, i;
FLAC__uint16 u16;
unsigned char *uc;
for (; position < end; ++position) {
for (c_chan = 0; c_chan < frame->header.channels; c_chan++) {
u16 = buf[c_chan][position];
uc = (unsigned char *)&u16;
for (i = 0; i < bytes_per_sample; i++) {
*dest++ = *uc++;
}
}
}
}
static FLAC__StreamDecoderWriteStatus flacWrite(const flac_decoder *dec, static FLAC__StreamDecoderWriteStatus flacWrite(const flac_decoder *dec,
const FLAC__Frame * frame, const FLAC__Frame * frame,
const FLAC__int32 * const buf[], const FLAC__int32 * const buf[],
@ -213,13 +234,11 @@ static FLAC__StreamDecoderWriteStatus flacWrite(const flac_decoder *dec,
{ {
FlacData *data = (FlacData *) vdata; FlacData *data = (FlacData *) vdata;
FLAC__uint32 samples = frame->header.blocksize; FLAC__uint32 samples = frame->header.blocksize;
FLAC__uint16 u16; unsigned int c_samp;
unsigned char *uc;
unsigned int c_samp, c_chan;
const unsigned int bytes_per_sample = (data->dc->audioFormat.bits / 8); const unsigned int bytes_per_sample = (data->dc->audioFormat.bits / 8);
const unsigned int bytes_per_channel = const unsigned int bytes_per_channel =
bytes_per_sample * frame->header.channels; bytes_per_sample * frame->header.channels;
unsigned int i; unsigned int num_samples, max_samples;
float timeChange; float timeChange;
FLAC__uint64 newPosition = 0; FLAC__uint64 newPosition = 0;
@ -239,8 +258,19 @@ static FLAC__StreamDecoderWriteStatus flacWrite(const flac_decoder *dec,
} }
data->position = newPosition; data->position = newPosition;
for (c_samp = 0; c_samp < frame->header.blocksize; c_samp++) { for (c_samp = 0; c_samp < frame->header.blocksize;
if (data->chunk_length + bytes_per_channel >= FLAC_CHUNK_SIZE) { c_samp += num_samples) {
num_samples = frame->header.blocksize - c_samp;
max_samples = (FLAC_CHUNK_SIZE - data->chunk_length) /
bytes_per_channel;
if (num_samples > max_samples)
num_samples = max_samples;
flac_convert(data->chunk + data->chunk_length,
frame, bytes_per_sample, buf,
c_samp, c_samp + num_samples);
data->chunk_length += num_samples;
if (flacSendChunk(data) < 0) { if (flacSendChunk(data) < 0) {
return return
FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
@ -252,16 +282,6 @@ static FLAC__StreamDecoderWriteStatus flacWrite(const flac_decoder *dec,
} }
} }
for (c_chan = 0; c_chan < frame->header.channels;
c_chan++) {
u16 = buf[c_chan][c_samp];
uc = (unsigned char *)&u16;
for (i = 0; i < bytes_per_sample; i++) {
data->chunk[data->chunk_length++] = *(uc++);
}
}
}
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
} }