Move channel expansion/contraction functions to audio_utils/channels.h/.c

Change-Id: I967a062f6c1cb5ae6acb6e92f4f3df00a0336733
diff --git a/modules/usbaudio/Android.mk b/modules/usbaudio/Android.mk
index 2af7897..89c498b 100644
--- a/modules/usbaudio/Android.mk
+++ b/modules/usbaudio/Android.mk
@@ -21,8 +21,9 @@
 LOCAL_SRC_FILES := \
 	audio_hw.c
 LOCAL_C_INCLUDES += \
-	external/tinyalsa/include
-LOCAL_SHARED_LIBRARIES := liblog libcutils libtinyalsa
+	external/tinyalsa/include \
+	$(call include-path-for, audio-utils)
+LOCAL_SHARED_LIBRARIES := liblog libcutils libtinyalsa libaudioutils
 LOCAL_MODULE_TAGS := optional
 LOCAL_CFLAGS := -Wno-unused-parameter
 
diff --git a/modules/usbaudio/audio_hw.c b/modules/usbaudio/audio_hw.c
index 8945791..edbbc1f 100644
--- a/modules/usbaudio/audio_hw.c
+++ b/modules/usbaudio/audio_hw.c
@@ -37,6 +37,8 @@
 
 #include <tinyalsa/asoundlib.h>
 
+#include <audio_utils/channels.h>
+
 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
 
 /* This is the default configuration to hand to The Framework on the initial
@@ -225,263 +227,6 @@
 }
 
 /*
- * Convert a buffer of N-channel, interleaved PCM16 samples to M-channel PCM16 channels
- * (where N < M).
- *   in_buff points to the buffer of PCM16 samples
- *   in_buff_channels Specifies the number of channels in the input buffer.
- *   out_buff points to the buffer to receive converted PCM16 samples.
- *   out_buff_channels Specifies the number of channels in the output buffer.
- *   num_in_bytes size of input buffer in BYTES
- * returns
- *   the number of BYTES of output data.
- * NOTE
- *   channels > N are filled with silence.
- *   This conversion is safe to do in-place (in_buff == out_buff)
- * We are doing this since we *always* present to The Framework as STEREO device, but need to
- * support 4-channel devices.
- * TODO Move this to a utilities module.
- */
-static size_t expand_channels_16(const int16_t* in_buff, int in_buff_chans,
-                                 int16_t* out_buff, int out_buff_chans,
-                                 size_t num_in_bytes)
-{
-    /*
-     * Move from back to front so that the conversion can be done in-place
-     * i.e. in_buff == out_buff
-     * NOTE: num_in_samples * out_buff_channels must be an even multiple of in_buff_chans
-     */
-    size_t num_in_samples = num_in_bytes / sizeof(int16_t);
-
-    size_t num_out_samples = (num_in_samples * out_buff_chans) / in_buff_chans;
-
-    short* dst_ptr = out_buff + num_out_samples - 1;
-    size_t src_index;
-    const short* src_ptr = in_buff + num_in_samples - 1;
-    int num_zero_chans = out_buff_chans - in_buff_chans;
-    for (src_index = 0; src_index < num_in_samples; src_index += in_buff_chans) {
-        int dst_offset;
-        for (dst_offset = 0; dst_offset < num_zero_chans; dst_offset++) {
-            *dst_ptr-- = 0;
-        }
-        for (; dst_offset < out_buff_chans; dst_offset++) {
-            *dst_ptr-- = *src_ptr--;
-        }
-    }
-
-    /* return number of *bytes* generated */
-    return num_out_samples * sizeof(int16_t);
-}
-
-/*
- * Convert a buffer of N-channel, interleaved PCM16 samples to M-channel PCM16 channels
- * (where N > M).
- *   in_buff points to the buffer of PCM16 samples
- *   in_buff_channels Specifies the number of channels in the input buffer.
- *   out_buff points to the buffer to receive converted PCM16 samples.
- *   out_buff_channels Specifies the number of channels in the output buffer.
- *   num_in_bytes size of input buffer in BYTES
- * returns
- *   the number of BYTES of output data.
- * NOTE
- *   channels > N are thrown away.
- *   This conversion is safe to do in-place (in_buff == out_buff)
- * We are doing this since we *always* present to The Framework as STEREO device, but need to
- * support 4-channel devices.
- * TODO Move this to a utilities module.
- */
-static size_t contract_channels_16(const int16_t* in_buff, size_t in_buff_chans,
-                                   int16_t* out_buff, size_t out_buff_chans,
-                                   size_t num_in_bytes)
-{
-    /*
-     * Move from front to back so that the conversion can be done in-place
-     * i.e. in_buff == out_buff
-     * NOTE: num_in_samples * out_buff_channels must be an even multiple of in_buff_chans
-     */
-    size_t num_in_samples = num_in_bytes / sizeof(int16_t);
-
-    size_t num_out_samples = (num_in_samples * out_buff_chans) / in_buff_chans;
-
-    size_t num_skip_samples = in_buff_chans - out_buff_chans;
-
-    int16_t* dst_ptr = out_buff;
-    const int16_t* src_ptr = in_buff;
-    size_t src_index;
-    for (src_index = 0; src_index < num_in_samples; src_index += in_buff_chans) {
-        size_t dst_offset;
-        for (dst_offset = 0; dst_offset < out_buff_chans; dst_offset++) {
-            *dst_ptr++ = *src_ptr++;
-        }
-        src_ptr += num_skip_samples;
-    }
-
-    /* return number of *bytes* generated */
-    return num_out_samples * sizeof(int16_t);
-}
-
-/*
- * Convert a buffer of N-channel, interleaved PCM32 samples to M-channel PCM32 channels
- * (where N < M).
- *   in_buff points to the buffer of PCM32 samples
- *   in_buff_channels Specifies the number of channels in the input buffer.
- *   out_buff points to the buffer to receive converted PCM32 samples.
- *   out_buff_channels Specifies the number of channels in the output buffer.
- *   num_in_bytes size of input buffer in BYTES
- * returns
- *   the number of BYTES of output data.
- * NOTE
- *   channels > N are filled with silence.
- *   This conversion is safe to do in-place (in_buff == out_buff)
- * We are doing this since we *always* present to The Framework as STEREO device, but need to
- * support 4-channel devices.
- * TODO Move this to a utilities module.
- */
-static size_t expand_channels_32(const int32_t* in_buff, size_t in_buff_chans,
-                                 int32_t* out_buff, size_t out_buff_chans,
-                                 size_t num_in_bytes)
-{
-    /*
-     * Move from back to front so that the conversion can be done in-place
-     * i.e. in_buff == out_buff
-     * NOTE: num_in_samples * out_buff_channels must be an even multiple of in_buff_chans
-     */
-    size_t num_in_samples = num_in_bytes / sizeof(int32_t);
-
-    size_t num_out_samples = (num_in_samples * out_buff_chans) / in_buff_chans;
-
-    int32_t* dst_ptr = out_buff + num_out_samples - 1;
-    const int32_t* src_ptr = in_buff + num_in_samples - 1;
-    size_t num_zero_chans = out_buff_chans - in_buff_chans;
-    size_t src_index;
-    for (src_index = 0; src_index < num_in_samples; src_index += in_buff_chans) {
-        size_t dst_offset;
-        for (dst_offset = 0; dst_offset < num_zero_chans; dst_offset++) {
-            *dst_ptr-- = 0;
-        }
-        for (; dst_offset < out_buff_chans; dst_offset++) {
-            *dst_ptr-- = *src_ptr--;
-        }
-    }
-
-    /* return number of *bytes* generated */
-    return num_out_samples * sizeof(int32_t);
-}
-
-/*
- * Convert a buffer of N-channel, interleaved PCM32 samples to M-channel PCM16 channels
- * (where N > M).
- *   in_buff points to the buffer of PCM32 samples
- *   in_buff_channels Specifies the number of channels in the input buffer.
- *   out_buff points to the buffer to receive converted PCM16 samples.
- *   out_buff_channels Specifies the number of channels in the output buffer.
- *   num_in_bytes size of input buffer in BYTES
- * returns
- *   the number of BYTES of output data.
- * NOTE
- *   channels > N are thrown away.
- *   This conversion is safe to do in-place (in_buff == out_buff)
- * We are doing this since we *always* present to The Framework as STEREO device, but need to
- * support 4-channel devices.
- * TODO Move this to a utilities module.
- */
-static size_t contract_channels_32(const int32_t* in_buff, size_t in_buff_chans,
-                                   int32_t* out_buff, size_t out_buff_chans,
-                                   size_t num_in_bytes)
-{
-    /*
-     * Move from front to back so that the conversion can be done in-place
-     * i.e. in_buff == out_buff
-     * NOTE: num_in_samples * out_buff_channels must be an even multiple of in_buff_chans
-     */
-    size_t num_in_samples = num_in_bytes / sizeof(int32_t);
-
-    size_t num_out_samples = (num_in_samples * out_buff_chans) / in_buff_chans;
-
-    size_t num_skip_samples = in_buff_chans - out_buff_chans;
-
-    int32_t* dst_ptr = out_buff;
-    const int32_t* src_ptr = in_buff;
-    size_t src_index;
-    for (src_index = 0; src_index < num_in_samples; src_index += in_buff_chans) {
-        size_t dst_offset;
-        for (dst_offset = 0; dst_offset < out_buff_chans; dst_offset++) {
-            *dst_ptr++ = *src_ptr++;
-        }
-        src_ptr += num_skip_samples;
-    }
-
-    /* return number of *bytes* generated */
-    return num_out_samples * sizeof(int32_t);
-}
-
-static size_t contract_channels(const void* in_buff, size_t in_buff_chans,
-                                void* out_buff, size_t out_buff_chans,
-                                unsigned sample_size_in_bytes, size_t num_in_bytes)
-{
-    switch (sample_size_in_bytes) {
-    case 2:
-        return contract_channels_16((const int16_t*)in_buff, in_buff_chans,
-                                    (int16_t*)out_buff, out_buff_chans,
-                                    num_in_bytes);
-
-    /* TODO - do this conversion when we have a device to test it with */
-    case 3:
-        ALOGE("24-bit channel contraction not supported.");
-        return 0;
-
-    case 4:
-        return contract_channels_32((const int32_t*)in_buff, in_buff_chans,
-                                    (int32_t*)out_buff, out_buff_chans,
-                                    num_in_bytes);
-
-    default:
-        return 0;
-    }
-}
-
-static size_t expand_channels(const void* in_buff, size_t in_buff_chans,
-                                void* out_buff, size_t out_buff_chans,
-                                unsigned sample_size_in_bytes, size_t num_in_bytes)
-{
-    switch (sample_size_in_bytes) {
-    case 2:
-        return expand_channels_16((const int16_t*)in_buff, in_buff_chans,
-                                  (int16_t*)out_buff, out_buff_chans,
-                                  num_in_bytes);
-
-    /* TODO - do this conversion when we have a device to test it with */
-    case 3:
-        ALOGE("24-bit channel expansion not supported.");
-        return 0;
-
-    case 4:
-        return expand_channels_32((const int32_t*)in_buff, in_buff_chans,
-                                  (int32_t*)out_buff, out_buff_chans,
-                                  num_in_bytes);
-
-    default:
-        return 0;
-    }
-}
-
-static size_t adjust_channels(const void* in_buff, size_t in_buff_chans,
-                              void* out_buff, size_t out_buff_chans,
-                              unsigned sample_size_in_bytes, size_t num_in_bytes)
-{
-    if (out_buff_chans > in_buff_chans) {
-        return expand_channels(in_buff, in_buff_chans, out_buff,  out_buff_chans,
-                               sample_size_in_bytes, num_in_bytes);
-    } else if (out_buff_chans < in_buff_chans) {
-        return contract_channels(in_buff, in_buff_chans, out_buff,  out_buff_chans,
-                                 sample_size_in_bytes, num_in_bytes);
-    } else if (in_buff != out_buff) {
-        memcpy(out_buff, in_buff, num_in_bytes);
-    }
-
-    return num_in_bytes;
-}
-
-/*
  * ALSA Utilities
  */
 /*TODO This table and the function that uses it should be moved to a utilities module (probably) */