pcm_utils: pass output channel count to pcm_convertChannels()

In order to be able to deal with non-trivial conversions,
pcm_convertChannels() needs to know both the input and the output
channel count.  Simplify buffer allocation in that function.
This commit is contained in:
Max Kellermann 2008-10-12 11:51:19 +02:00
parent a7924d141d
commit 52949453df

View File

@ -386,47 +386,33 @@ pcm_convert_channels_2_to_1(int16_t *dest, const int16_t *src,
} }
static const int16_t * static const int16_t *
pcm_convertChannels(int8_t channels, const int16_t *inBuffer, pcm_convertChannels(int8_t dest_channels,
size_t inSize, size_t *outSize) int8_t src_channels, const int16_t *src,
size_t src_size, size_t *dest_size_r)
{ {
static int16_t *buf; static int16_t *buf;
static size_t len; static size_t len;
int16_t *outBuffer = NULL; unsigned num_frames = src_size / src_channels / sizeof(*src);
unsigned dest_size = num_frames * dest_channels * sizeof(*src);
switch (channels) { if (dest_size > len) {
/* convert from 1 -> 2 channels */ len = dest_size;
case 1: buf = xrealloc(buf, len);
*outSize = (inSize >> 1) << 2;
if (*outSize > len) {
len = *outSize;
buf = xrealloc(buf, len);
}
outBuffer = buf;
pcm_convert_channels_1_to_2((int16_t *)buf,
(const int16_t *)inBuffer,
inSize >> 1);
break;
/* convert from 2 -> 1 channels */
case 2:
*outSize = inSize >> 1;
if (*outSize > len) {
len = *outSize;
buf = xrealloc(buf, len);
}
outBuffer = buf;
pcm_convert_channels_2_to_1((int16_t *)buf,
(const int16_t *)inBuffer,
inSize >> 2);
break;
default:
ERROR("only 1 or 2 channels are supported for conversion!\n");
} }
return outBuffer; *dest_size_r = dest_size;
if (src_channels == 1 && dest_channels == 2)
pcm_convert_channels_1_to_2(buf, src, num_frames);
else if (src_channels == 2 && dest_channels == 1)
pcm_convert_channels_2_to_1(buf, src, num_frames);
else {
ERROR("conversion %u->%u channels is not supported\n",
src_channels, dest_channels);
return NULL;
}
return buf;
} }
static void static void
@ -504,7 +490,6 @@ size_t pcm_convertAudioFormat(const struct audio_format *inFormat,
size_t outSize = pcm_sizeOfConvBuffer(inFormat, inSize, outFormat); size_t outSize = pcm_sizeOfConvBuffer(inFormat, inSize, outFormat);
assert(outFormat->bits == 16); assert(outFormat->bits == 16);
assert(outFormat->channels == 2 || outFormat->channels == 1);
/* everything else supports 16 bit only, so convert to that first */ /* everything else supports 16 bit only, so convert to that first */
buf = pcm_convertTo16bit(inFormat->bits, inBuffer, inSize, &len); buf = pcm_convertTo16bit(inFormat->bits, inBuffer, inSize, &len);
@ -512,7 +497,9 @@ size_t pcm_convertAudioFormat(const struct audio_format *inFormat,
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
if (inFormat->channels != outFormat->channels) { if (inFormat->channels != outFormat->channels) {
buf = pcm_convertChannels(inFormat->channels, buf, len, &len); buf = pcm_convertChannels(outFormat->channels,
inFormat->channels,
buf, len, &len);
if (!buf) if (!buf)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }