OSDN Git Service

audio_utils: add memcpy_to_i32_from_u8
authorGlenn Kasten <gkasten@google.com>
Tue, 30 Oct 2018 16:02:38 +0000 (09:02 -0700)
committerGlenn Kasten <gkasten@google.com>
Fri, 2 Nov 2018 21:21:31 +0000 (14:21 -0700)
Test: primitives_tests
Change-Id: Ia23393b9832bda2968096adbb91ca165d788d8e7

audio_utils/include/audio_utils/primitives.h
audio_utils/primitives.c
audio_utils/tests/primitives_tests.cpp

index a3727d7..6b9ca3d 100644 (file)
@@ -364,6 +364,18 @@ void memcpy_to_i16_from_q8_23(int16_t *dst, const int32_t *src, size_t count);
 void memcpy_to_float_from_q8_23(float *dst, const int32_t *src, size_t count);
 
 /**
+ * Expand and copy samples from unsigned 8-bit offset by 0x80 to signed 32-bit.
+ *
+ *  \param dst     Destination buffer
+ *  \param src     Source buffer
+ *  \param count   Number of samples to copy
+ *
+ * The destination and source buffers must either be completely separate (non-overlapping), or
+ * they must both start at the same address.  Partially overlapping buffers are not supported.
+ */
+void memcpy_to_i32_from_u8(int32_t *dst, const uint8_t *src, size_t count);
+
+/**
  * Copy samples from signed fixed point 16-bit Q0.15 to signed fixed-point 32-bit Q0.31.
  * The output data range is [0x80000000, 0x7fff0000] at intervals of 0x10000.
  *
index 594f1c5..7b1ce75 100644 (file)
@@ -253,6 +253,15 @@ void memcpy_to_float_from_q8_23(float *dst, const int32_t *src, size_t count)
     }
 }
 
+void memcpy_to_i32_from_u8(int32_t *dst, const uint8_t *src, size_t count)
+{
+    dst += count;
+    src += count;
+    for (; count > 0; --count) {
+        *--dst = ((int32_t)(*--src) - 0x80) << 24;
+    }
+}
+
 void memcpy_to_i32_from_i16(int32_t *dst, const int16_t *src, size_t count)
 {
     dst += count;
index d28516a..7eebb06 100644 (file)
@@ -341,10 +341,16 @@ TEST(audio_utils_primitives, memcpy) {
     }
 
     constexpr size_t testsize = std::min(u8size, size);
+    zeroFill(fary);
     memcpy_to_float_from_u8(fary.data(), u8ref.data(), testsize);
     memcpy_to_u8_from_float(u8ary.data(), fary.data(), testsize);
 
     EXPECT_EQ(0, memcmp(u8ary.data(), u8ref.data(), u8ary.size() * sizeof(u8ary[0])));
+
+    // test conversion from u8 to i32
+    zeroFill(i32ary);
+    memcpy_to_i32_from_u8(i32ary.data(), u8ref.data(), testsize);
+    checkMonotone(i32ary.data(), testsize);
 }
 
 template<typename T>