| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // MSVC++ requires this to be set before any other includes to get M_PI. | 5 // MSVC++ requires this to be set before any other includes to get M_PI. |
| 6 #define _USE_MATH_DEFINES | 6 #define _USE_MATH_DEFINES |
| 7 | 7 |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/string_number_conversions.h" | 14 #include "base/string_number_conversions.h" |
| 15 #include "base/stringize_macros.h" |
| 15 #include "base/time.h" | 16 #include "base/time.h" |
| 17 #include "build/build_config.h" |
| 16 #include "media/base/sinc_resampler.h" | 18 #include "media/base/sinc_resampler.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 21 |
| 20 using testing::_; | 22 using testing::_; |
| 21 | 23 |
| 22 namespace media { | 24 namespace media { |
| 23 | 25 |
| 24 static const double kSampleRateRatio = 192000.0 / 44100.0; | 26 static const double kSampleRateRatio = 192000.0 / 44100.0; |
| 25 static const double kKernelInterpolationFactor = 0.5; | 27 static const double kKernelInterpolationFactor = 0.5; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 resampler.Resample(resampled_destination.get(), resampler.ChunkSize() / 2); | 84 resampler.Resample(resampled_destination.get(), resampler.ChunkSize() / 2); |
| 83 ASSERT_NE(resampled_destination[0], 0); | 85 ASSERT_NE(resampled_destination[0], 0); |
| 84 | 86 |
| 85 // Flush and request more data, which should all be zeros now. | 87 // Flush and request more data, which should all be zeros now. |
| 86 resampler.Flush(); | 88 resampler.Flush(); |
| 87 testing::Mock::VerifyAndClear(&mock_source); | 89 testing::Mock::VerifyAndClear(&mock_source); |
| 88 EXPECT_CALL(mock_source, ProvideInput(_, _)) | 90 EXPECT_CALL(mock_source, ProvideInput(_, _)) |
| 89 .Times(1).WillOnce(ClearBuffer()); | 91 .Times(1).WillOnce(ClearBuffer()); |
| 90 resampler.Resample(resampled_destination.get(), resampler.ChunkSize() / 2); | 92 resampler.Resample(resampled_destination.get(), resampler.ChunkSize() / 2); |
| 91 for (int i = 0; i < resampler.ChunkSize() / 2; ++i) | 93 for (int i = 0; i < resampler.ChunkSize() / 2; ++i) |
| 92 ASSERT_EQ(resampled_destination[i], 0); | 94 ASSERT_FLOAT_EQ(resampled_destination[i], 0); |
| 93 } | 95 } |
| 94 | 96 |
| 97 // Define platform independent function name for Convolve* tests. |
| 98 #if defined(ARCH_CPU_X86_FAMILY) && defined(__SSE__) |
| 99 #define CONVOLVE_FUNC Convolve_SSE |
| 100 #elif defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON) |
| 101 #define CONVOLVE_FUNC Convolve_NEON |
| 102 #endif |
| 103 |
| 95 // Ensure various optimized Convolve() methods return the same value. Only run | 104 // Ensure various optimized Convolve() methods return the same value. Only run |
| 96 // this test if other optimized methods exist, otherwise the default Convolve() | 105 // this test if other optimized methods exist, otherwise the default Convolve() |
| 97 // will be tested by the parameterized SincResampler tests below. | 106 // will be tested by the parameterized SincResampler tests below. |
| 98 #if defined(ARCH_CPU_X86_FAMILY) && defined(__SSE__) | 107 #if defined(CONVOLVE_FUNC) |
| 99 TEST(SincResamplerTest, Convolve) { | 108 TEST(SincResamplerTest, Convolve) { |
| 100 // Initialize a dummy resampler. | 109 // Initialize a dummy resampler. |
| 101 MockSource mock_source; | 110 MockSource mock_source; |
| 102 SincResampler resampler( | 111 SincResampler resampler( |
| 103 kSampleRateRatio, | 112 kSampleRateRatio, |
| 104 base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source))); | 113 base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source))); |
| 105 | 114 |
| 106 // Convolve_SSE() is slightly more precise than Convolve_C(), so comparison | 115 // The optimized Convolve methods are slightly more precise than Convolve_C(), |
| 107 // must be done using an epsilon. | 116 // so comparison must be done using an epsilon. |
| 108 static const double kEpsilon = 0.00000005; | 117 static const double kEpsilon = 0.00000005; |
| 109 | 118 |
| 110 // Use a kernel from SincResampler as input and kernel data, this has the | 119 // Use a kernel from SincResampler as input and kernel data, this has the |
| 111 // benefit of already being properly sized and aligned for Convolve_SSE(). | 120 // benefit of already being properly sized and aligned for Convolve_SSE(). |
| 112 double result = resampler.Convolve_C( | 121 double result = resampler.Convolve_C( |
| 113 resampler.kernel_storage_.get(), resampler.kernel_storage_.get(), | 122 resampler.kernel_storage_.get(), resampler.kernel_storage_.get(), |
| 114 resampler.kernel_storage_.get(), kKernelInterpolationFactor); | 123 resampler.kernel_storage_.get(), kKernelInterpolationFactor); |
| 115 double result2 = resampler.Convolve_SSE( | 124 double result2 = resampler.CONVOLVE_FUNC( |
| 116 resampler.kernel_storage_.get(), resampler.kernel_storage_.get(), | 125 resampler.kernel_storage_.get(), resampler.kernel_storage_.get(), |
| 117 resampler.kernel_storage_.get(), kKernelInterpolationFactor); | 126 resampler.kernel_storage_.get(), kKernelInterpolationFactor); |
| 118 EXPECT_NEAR(result2, result, kEpsilon); | 127 EXPECT_NEAR(result2, result, kEpsilon); |
| 119 | 128 |
| 120 // Test Convolve_SSE() w/ unaligned input pointer. | 129 // Test Convolve() w/ unaligned input pointer. |
| 121 result = resampler.Convolve_C( | 130 result = resampler.Convolve_C( |
| 122 resampler.kernel_storage_.get() + 1, resampler.kernel_storage_.get(), | 131 resampler.kernel_storage_.get() + 1, resampler.kernel_storage_.get(), |
| 123 resampler.kernel_storage_.get(), kKernelInterpolationFactor); | 132 resampler.kernel_storage_.get(), kKernelInterpolationFactor); |
| 124 result2 = resampler.Convolve_SSE( | 133 result2 = resampler.CONVOLVE_FUNC( |
| 125 resampler.kernel_storage_.get() + 1, resampler.kernel_storage_.get(), | 134 resampler.kernel_storage_.get() + 1, resampler.kernel_storage_.get(), |
| 126 resampler.kernel_storage_.get(), kKernelInterpolationFactor); | 135 resampler.kernel_storage_.get(), kKernelInterpolationFactor); |
| 127 EXPECT_NEAR(result2, result, kEpsilon); | 136 EXPECT_NEAR(result2, result, kEpsilon); |
| 128 } | 137 } |
| 129 #endif | 138 #endif |
| 130 | 139 |
| 131 // Benchmark for the various Convolve() methods. Make sure to build with | 140 // Benchmark for the various Convolve() methods. Make sure to build with |
| 132 // branding=Chrome so that DCHECKs are compiled out when benchmarking. Original | 141 // branding=Chrome so that DCHECKs are compiled out when benchmarking. Original |
| 133 // benchmarks were run with --convolve-iterations=50000000. | 142 // benchmarks were run with --convolve-iterations=50000000. |
| 134 TEST(SincResamplerTest, ConvolveBenchmark) { | 143 TEST(SincResamplerTest, ConvolveBenchmark) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 151 base::TimeTicks start = base::TimeTicks::HighResNow(); | 160 base::TimeTicks start = base::TimeTicks::HighResNow(); |
| 152 for (int i = 0; i < convolve_iterations; ++i) { | 161 for (int i = 0; i < convolve_iterations; ++i) { |
| 153 resampler.Convolve_C( | 162 resampler.Convolve_C( |
| 154 resampler.kernel_storage_.get(), resampler.kernel_storage_.get(), | 163 resampler.kernel_storage_.get(), resampler.kernel_storage_.get(), |
| 155 resampler.kernel_storage_.get(), kKernelInterpolationFactor); | 164 resampler.kernel_storage_.get(), kKernelInterpolationFactor); |
| 156 } | 165 } |
| 157 double total_time_c_ms = | 166 double total_time_c_ms = |
| 158 (base::TimeTicks::HighResNow() - start).InMillisecondsF(); | 167 (base::TimeTicks::HighResNow() - start).InMillisecondsF(); |
| 159 printf("Convolve_C took %.2fms.\n", total_time_c_ms); | 168 printf("Convolve_C took %.2fms.\n", total_time_c_ms); |
| 160 | 169 |
| 161 #if defined(ARCH_CPU_X86_FAMILY) && defined(__SSE__) | 170 #if defined(CONVOLVE_FUNC) |
| 162 // Benchmark Convolve_SSE() with unaligned input pointer. | 171 // Benchmark with unaligned input pointer. |
| 163 start = base::TimeTicks::HighResNow(); | 172 start = base::TimeTicks::HighResNow(); |
| 164 for (int j = 0; j < convolve_iterations; ++j) { | 173 for (int j = 0; j < convolve_iterations; ++j) { |
| 165 resampler.Convolve_SSE( | 174 resampler.CONVOLVE_FUNC( |
| 166 resampler.kernel_storage_.get() + 1, resampler.kernel_storage_.get(), | 175 resampler.kernel_storage_.get() + 1, resampler.kernel_storage_.get(), |
| 167 resampler.kernel_storage_.get(), kKernelInterpolationFactor); | 176 resampler.kernel_storage_.get(), kKernelInterpolationFactor); |
| 168 } | 177 } |
| 169 double total_time_sse_unaligned_ms = | 178 double total_time_optimized_unaligned_ms = |
| 170 (base::TimeTicks::HighResNow() - start).InMillisecondsF(); | 179 (base::TimeTicks::HighResNow() - start).InMillisecondsF(); |
| 171 printf("Convolve_SSE (unaligned) took %.2fms; which is %.2fx faster than" | 180 printf(STRINGIZE(CONVOLVE_FUNC) "(unaligned) took %.2fms; which is %.2fx " |
| 172 " Convolve_C.\n", total_time_sse_unaligned_ms, | 181 "faster than Convolve_C.\n", total_time_optimized_unaligned_ms, |
| 173 total_time_c_ms / total_time_sse_unaligned_ms); | 182 total_time_c_ms / total_time_optimized_unaligned_ms); |
| 174 | 183 |
| 175 // Benchmark Convolve_SSE() with aligned input pointer. | 184 // Benchmark with aligned input pointer. |
| 176 start = base::TimeTicks::HighResNow(); | 185 start = base::TimeTicks::HighResNow(); |
| 177 for (int j = 0; j < convolve_iterations; ++j) { | 186 for (int j = 0; j < convolve_iterations; ++j) { |
| 178 resampler.Convolve_SSE( | 187 resampler.CONVOLVE_FUNC( |
| 179 resampler.kernel_storage_.get(), resampler.kernel_storage_.get(), | 188 resampler.kernel_storage_.get(), resampler.kernel_storage_.get(), |
| 180 resampler.kernel_storage_.get(), kKernelInterpolationFactor); | 189 resampler.kernel_storage_.get(), kKernelInterpolationFactor); |
| 181 } | 190 } |
| 182 double total_time_sse_aligned_ms = | 191 double total_time_optimized_aligned_ms = |
| 183 (base::TimeTicks::HighResNow() - start).InMillisecondsF(); | 192 (base::TimeTicks::HighResNow() - start).InMillisecondsF(); |
| 184 printf("Convolve_SSE (aligned) took %.2fms; which is %.2fx faster than" | 193 printf(STRINGIZE(CONVOLVE_FUNC) " (aligned) took %.2fms; which is %.2fx " |
| 185 " Convolve_C and %.2fx faster than Convolve_SSE (unaligned).\n", | 194 "faster than Convolve_C and %.2fx faster than " |
| 186 total_time_sse_aligned_ms, total_time_c_ms / total_time_sse_aligned_ms, | 195 STRINGIZE(CONVOLVE_FUNC) " (unaligned).\n", |
| 187 total_time_sse_unaligned_ms / total_time_sse_aligned_ms); | 196 total_time_optimized_aligned_ms, |
| 197 total_time_c_ms / total_time_optimized_aligned_ms, |
| 198 total_time_optimized_unaligned_ms / total_time_optimized_aligned_ms); |
| 188 #endif | 199 #endif |
| 189 } | 200 } |
| 190 | 201 |
| 202 #undef CONVOLVE_FUNC |
| 203 |
| 191 // Fake audio source for testing the resampler. Generates a sinusoidal linear | 204 // Fake audio source for testing the resampler. Generates a sinusoidal linear |
| 192 // chirp (http://en.wikipedia.org/wiki/Chirp) which can be tuned to stress the | 205 // chirp (http://en.wikipedia.org/wiki/Chirp) which can be tuned to stress the |
| 193 // resampler for the specific sample rate conversion being used. | 206 // resampler for the specific sample rate conversion being used. |
| 194 class SinusoidalLinearChirpSource { | 207 class SinusoidalLinearChirpSource { |
| 195 public: | 208 public: |
| 196 SinusoidalLinearChirpSource(int sample_rate, int samples, | 209 SinusoidalLinearChirpSource(int sample_rate, int samples, |
| 197 double max_frequency) | 210 double max_frequency) |
| 198 : sample_rate_(sample_rate), | 211 : sample_rate_(sample_rate), |
| 199 total_samples_(samples), | 212 total_samples_(samples), |
| 200 max_frequency_(max_frequency), | 213 max_frequency_(max_frequency), |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 std::tr1::make_tuple(11025, 192000, kResamplingRMSError, -62.61), | 393 std::tr1::make_tuple(11025, 192000, kResamplingRMSError, -62.61), |
| 381 std::tr1::make_tuple(16000, 192000, kResamplingRMSError, -63.14), | 394 std::tr1::make_tuple(16000, 192000, kResamplingRMSError, -63.14), |
| 382 std::tr1::make_tuple(22050, 192000, kResamplingRMSError, -62.42), | 395 std::tr1::make_tuple(22050, 192000, kResamplingRMSError, -62.42), |
| 383 std::tr1::make_tuple(32000, 192000, kResamplingRMSError, -63.38), | 396 std::tr1::make_tuple(32000, 192000, kResamplingRMSError, -63.38), |
| 384 std::tr1::make_tuple(44100, 192000, kResamplingRMSError, -62.63), | 397 std::tr1::make_tuple(44100, 192000, kResamplingRMSError, -62.63), |
| 385 std::tr1::make_tuple(48000, 192000, kResamplingRMSError, -73.44), | 398 std::tr1::make_tuple(48000, 192000, kResamplingRMSError, -73.44), |
| 386 std::tr1::make_tuple(96000, 192000, kResamplingRMSError, -73.52), | 399 std::tr1::make_tuple(96000, 192000, kResamplingRMSError, -73.52), |
| 387 std::tr1::make_tuple(192000, 192000, kResamplingRMSError, -73.52))); | 400 std::tr1::make_tuple(192000, 192000, kResamplingRMSError, -73.52))); |
| 388 | 401 |
| 389 } // namespace media | 402 } // namespace media |
| OLD | NEW |