Permalink
Browse files

Grow output buffer to take draining into account

When draining the resampler we don't know how many output samples will
be generated.  Grow the output buffer as needed.

This solves cliplogcvt failures with "resample exhausted double-sized
output buffer!" error messages.

Signed-off-by: Stefan Hajnoczi <stefanha@gmail.com>
  • Loading branch information...
stefanha committed Jun 3, 2015
1 parent 2d4490e commit e7ce7127efe7f01c6c444b0f91d9a2f0af79c927
Showing with 18 additions and 9 deletions.
  1. +18 −9 cliplogcvt/cliplogcvt.cpp
View
@@ -176,27 +176,36 @@ void AudioBuffer::resample(void *resampleState, double factor, bool drain)
{
assert(channels == 1);
int newFrames = (frames + 1) * factor * 2; /* double size just to make sure */
float *newSamples = new float[newFrames];
int newFrames = (frames + 1) * factor + 1; /* round up */
float *newSamples = NULL;
int inFrames = 0;
int outFrames = 0;
while (inFrames < frames) {
while (inFrames < frames || outFrames == newFrames) {
int inUsed = 0;
int ret;
/* Resize output buffer. When draining, samples may be generated beyond
* the original size we anticipated.
*/
if (!newSamples || outFrames == newFrames) {
float *tmp = newSamples;
newFrames *= 2;
newSamples = new float[newFrames];
if (tmp) {
memcpy(newSamples, tmp, newFrames / 2 * sizeof(newSamples[0]));
delete [] tmp;
}
}
ret = resample_process(resampleState, factor,
&samples[inFrames], frames - inFrames,
drain, &inUsed,
&newSamples[outFrames], newFrames - outFrames);
inFrames += inUsed;
outFrames += ret;
/* We should never exhaust the buffer since we double-sized it */
if (outFrames >= newFrames) {
fprintf(stderr, "resample exhausted double-sized output buffer!\n");
exit(1);
}
}
/* Cap buffer size */

0 comments on commit e7ce712

Please sign in to comment.