shout: fixed the lame input buffer allocation

"float (*lamebuf)[2] = g_malloc()" does NOT allocate two float*
buffers.  The formula is even wrong: it should be applied to LAME's
output buffer, not its input buffer.

Converted "lamebuf" to the two variables "left" and "right", and
allocate them independently with the exact buffer size.  Set
right=left if mono output is configured.
This commit is contained in:
Max Kellermann 2008-11-30 14:25:56 +01:00
parent a7b692a1df
commit f600f4a256

View File

@ -133,8 +133,7 @@ static int shout_mp3_encoder_encode(struct shout_data *sd,
{ {
const int16_t *src = (const int16_t*)chunk; const int16_t *src = (const int16_t*)chunk;
unsigned int i; unsigned int i;
int j; float *left, *right;
float (*lamebuf)[2];
struct shout_buffer *buf = &(sd->buf); struct shout_buffer *buf = &(sd->buf);
unsigned int samples; unsigned int samples;
int bytes = audio_format_sample_size(&sd->audio_format); int bytes = audio_format_sample_size(&sd->audio_format);
@ -142,21 +141,28 @@ static int shout_mp3_encoder_encode(struct shout_data *sd,
int bytes_out; int bytes_out;
samples = len / (bytes * sd->audio_format.channels); samples = len / (bytes * sd->audio_format.channels);
/* rough estimate, from lame.h */ left = g_malloc(sizeof(left[0]) * samples);
lamebuf = g_malloc(sizeof(float) * (1.25 * samples + 7200)); if (sd->audio_format.channels > 1)
right = g_malloc(sizeof(left[0]) * samples);
else
right = left;
/* this is for only 16-bit audio */ /* this is for only 16-bit audio */
for (i = 0; i < samples; i++) { for (i = 0; i < samples; i++) {
for (j = 0; j < sd->audio_format.channels; j++) { left[i] = src[0];
lamebuf[j][i] = *src++; if (right != left)
} right[i] = src[1];
src += sd->audio_format.channels;
} }
bytes_out = lame_encode_buffer_float(ld->gfp, lamebuf[0], lamebuf[1], bytes_out = lame_encode_buffer_float(ld->gfp, left, right,
samples, buf->data, samples, buf->data,
sizeof(buf->data) - buf->len); sizeof(buf->data) - buf->len);
free(lamebuf);
g_free(left);
if (right != left)
g_free(right);
if (0 > bytes_out) { if (0 > bytes_out) {
g_warning("error encoding lame buffer for shout\n"); g_warning("error encoding lame buffer for shout\n");