Allow same src and dst in InputAudioFile::DuplicateInterleaved
This change allows the input and output to the static method
InputAudioFile::DuplicateInterleaved to be the same array. That is,
in-place manipulation is now possible. A unit test is also added.
R=tina.legrand@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/20239004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@7008 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/webrtc/modules/audio_coding/neteq/tools/input_audio_file.cc b/webrtc/modules/audio_coding/neteq/tools/input_audio_file.cc
index 8063173..6bbb328 100644
--- a/webrtc/modules/audio_coding/neteq/tools/input_audio_file.cc
+++ b/webrtc/modules/audio_coding/neteq/tools/input_audio_file.cc
@@ -40,8 +40,11 @@
void InputAudioFile::DuplicateInterleaved(const int16_t* source, size_t samples,
size_t channels,
int16_t* destination) {
- for (size_t i = 0; i < samples; ++i) {
- for (size_t j = 0; j < channels; ++j) {
+ // Start from the end of |source| and |destination|, and work towards the
+ // beginning. This is to allow in-place interleaving of the same array (i.e.,
+ // |source| and |destination| are the same array).
+ for (int i = static_cast<int>(samples - 1); i >= 0; --i) {
+ for (int j = static_cast<int>(channels - 1); j >= 0; --j) {
destination[i * channels + j] = source[i];
}
}
diff --git a/webrtc/modules/audio_coding/neteq/tools/input_audio_file.h b/webrtc/modules/audio_coding/neteq/tools/input_audio_file.h
index 274f8ea..f546119 100644
--- a/webrtc/modules/audio_coding/neteq/tools/input_audio_file.h
+++ b/webrtc/modules/audio_coding/neteq/tools/input_audio_file.h
@@ -37,7 +37,8 @@
// Creates a multi-channel signal from a mono signal. Each sample is repeated
// |channels| times to create an interleaved multi-channel signal where all
// channels are identical. The output |destination| must have the capacity to
- // hold samples * channels elements.
+ // hold samples * channels elements. Note that |source| and |destination| can
+ // be the same array (i.e., point to the same address).
static void DuplicateInterleaved(const int16_t* source, size_t samples,
size_t channels, int16_t* destination);
diff --git a/webrtc/modules/audio_coding/neteq/tools/input_audio_file_unittest.cc b/webrtc/modules/audio_coding/neteq/tools/input_audio_file_unittest.cc
new file mode 100644
index 0000000..67448fe
--- /dev/null
+++ b/webrtc/modules/audio_coding/neteq/tools/input_audio_file_unittest.cc
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+// Unit tests for test InputAudioFile class.
+
+#include "webrtc/modules/audio_coding/neteq/tools/input_audio_file.h"
+
+#include "gtest/gtest.h"
+
+namespace webrtc {
+namespace test {
+
+TEST(TestInputAudioFile, DuplicateInterleaveSeparateSrcDst) {
+ static const size_t kSamples = 10;
+ static const size_t kChannels = 2;
+ int16_t input[kSamples];
+ for (size_t i = 0; i < kSamples; ++i) {
+ input[i] = i;
+ }
+ int16_t output[kSamples * kChannels];
+ InputAudioFile::DuplicateInterleaved(input, kSamples, kChannels, output);
+
+ // Verify output
+ int16_t* output_ptr = output;
+ for (size_t i = 0; i < kSamples; ++i) {
+ for (size_t j = 0; j < kChannels; ++j) {
+ EXPECT_EQ(static_cast<int16_t>(i), *output_ptr++);
+ }
+ }
+}
+
+TEST(TestInputAudioFile, DuplicateInterleaveSameSrcDst) {
+ static const size_t kSamples = 10;
+ static const size_t kChannels = 5;
+ int16_t input[kSamples * kChannels];
+ for (size_t i = 0; i < kSamples; ++i) {
+ input[i] = i;
+ }
+ InputAudioFile::DuplicateInterleaved(input, kSamples, kChannels, input);
+
+ // Verify output
+ int16_t* output_ptr = input;
+ for (size_t i = 0; i < kSamples; ++i) {
+ for (size_t j = 0; j < kChannels; ++j) {
+ EXPECT_EQ(static_cast<int16_t>(i), *output_ptr++);
+ }
+ }
+}
+
+} // namespace test
+} // namespace webrtc
diff --git a/webrtc/modules/modules.gyp b/webrtc/modules/modules.gyp
index f68ea7c..138c73b 100644
--- a/webrtc/modules/modules.gyp
+++ b/webrtc/modules/modules.gyp
@@ -153,6 +153,7 @@
'audio_coding/neteq/mock/mock_external_decoder_pcm16b.h',
'audio_coding/neteq/mock/mock_packet_buffer.h',
'audio_coding/neteq/mock/mock_payload_splitter.h',
+ 'audio_coding/neteq/tools/input_audio_file_unittest.cc',
'audio_coding/neteq/tools/packet_unittest.cc',
'audio_processing/aec/system_delay_unittest.cc',
'audio_processing/aec/echo_cancellation_unittest.cc',