Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(512)

Side by Side Diff: media/base/sinc_resampler_unittest.cc

Issue 14189035: Reduce jitter from uneven SincResampler buffer size requests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comments. Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « media/base/sinc_resampler.cc ('k') | remoting/codec/audio_encoder_opus.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
(...skipping 15 matching lines...) Expand all
26 26
27 static const double kSampleRateRatio = 192000.0 / 44100.0; 27 static const double kSampleRateRatio = 192000.0 / 44100.0;
28 static const double kKernelInterpolationFactor = 0.5; 28 static const double kKernelInterpolationFactor = 0.5;
29 29
30 // Command line switch for runtime adjustment of ConvolveBenchmark iterations. 30 // Command line switch for runtime adjustment of ConvolveBenchmark iterations.
31 static const char kConvolveIterations[] = "convolve-iterations"; 31 static const char kConvolveIterations[] = "convolve-iterations";
32 32
33 // Helper class to ensure ChunkedResample() functions properly. 33 // Helper class to ensure ChunkedResample() functions properly.
34 class MockSource { 34 class MockSource {
35 public: 35 public:
36 MOCK_METHOD2(ProvideInput, void(float* destination, int frames)); 36 MOCK_METHOD2(ProvideInput, void(int frames, float* destination));
37 }; 37 };
38 38
39 ACTION(ClearBuffer) { 39 ACTION(ClearBuffer) {
40 memset(arg0, 0, arg1 * sizeof(float)); 40 memset(arg1, 0, arg0 * sizeof(float));
41 } 41 }
42 42
43 ACTION(FillBuffer) { 43 ACTION(FillBuffer) {
44 // Value chosen arbitrarily such that SincResampler resamples it to something 44 // Value chosen arbitrarily such that SincResampler resamples it to something
45 // easily representable on all platforms; e.g., using kSampleRateRatio this 45 // easily representable on all platforms; e.g., using kSampleRateRatio this
46 // becomes 1.81219. 46 // becomes 1.81219.
47 memset(arg0, 64, arg1 * sizeof(float)); 47 memset(arg1, 64, arg0 * sizeof(float));
48 } 48 }
49 49
50 // Test requesting multiples of ChunkSize() frames results in the proper number 50 // Test requesting multiples of ChunkSize() frames results in the proper number
51 // of callbacks. 51 // of callbacks.
52 TEST(SincResamplerTest, ChunkedResample) { 52 TEST(SincResamplerTest, ChunkedResample) {
53 MockSource mock_source; 53 MockSource mock_source;
54 54
55 // Choose a high ratio of input to output samples which will result in quick 55 // Choose a high ratio of input to output samples which will result in quick
56 // exhaustion of SincResampler's internal buffers. 56 // exhaustion of SincResampler's internal buffers.
57 SincResampler resampler( 57 SincResampler resampler(
58 kSampleRateRatio, 58 kSampleRateRatio, SincResampler::kDefaultRequestSize,
59 base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source))); 59 base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source)));
60 60
61 static const int kChunks = 2; 61 static const int kChunks = 2;
62 int max_chunk_size = resampler.ChunkSize() * kChunks; 62 int max_chunk_size = resampler.ChunkSize() * kChunks;
63 scoped_ptr<float[]> resampled_destination(new float[max_chunk_size]); 63 scoped_ptr<float[]> resampled_destination(new float[max_chunk_size]);
64 64
65 // Verify requesting ChunkSize() frames causes a single callback. 65 // Verify requesting ChunkSize() frames causes a single callback.
66 EXPECT_CALL(mock_source, ProvideInput(_, _)) 66 EXPECT_CALL(mock_source, ProvideInput(_, _))
67 .Times(1).WillOnce(ClearBuffer()); 67 .Times(1).WillOnce(ClearBuffer());
68 resampler.Resample(resampled_destination.get(), resampler.ChunkSize()); 68 resampler.Resample(resampler.ChunkSize(), resampled_destination.get());
69 69
70 // Verify requesting kChunks * ChunkSize() frames causes kChunks callbacks. 70 // Verify requesting kChunks * ChunkSize() frames causes kChunks callbacks.
71 testing::Mock::VerifyAndClear(&mock_source); 71 testing::Mock::VerifyAndClear(&mock_source);
72 EXPECT_CALL(mock_source, ProvideInput(_, _)) 72 EXPECT_CALL(mock_source, ProvideInput(_, _))
73 .Times(kChunks).WillRepeatedly(ClearBuffer()); 73 .Times(kChunks).WillRepeatedly(ClearBuffer());
74 resampler.Resample(resampled_destination.get(), max_chunk_size); 74 resampler.Resample(max_chunk_size, resampled_destination.get());
75 } 75 }
76 76
77 // Test flush resets the internal state properly. 77 // Test flush resets the internal state properly.
78 TEST(SincResamplerTest, Flush) { 78 TEST(SincResamplerTest, Flush) {
79 MockSource mock_source; 79 MockSource mock_source;
80 SincResampler resampler( 80 SincResampler resampler(
81 kSampleRateRatio, 81 kSampleRateRatio, SincResampler::kDefaultRequestSize,
82 base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source))); 82 base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source)));
83 scoped_ptr<float[]> resampled_destination(new float[resampler.ChunkSize()]); 83 scoped_ptr<float[]> resampled_destination(new float[resampler.ChunkSize()]);
84 84
85 // Fill the resampler with junk data. 85 // Fill the resampler with junk data.
86 EXPECT_CALL(mock_source, ProvideInput(_, _)) 86 EXPECT_CALL(mock_source, ProvideInput(_, _))
87 .Times(1).WillOnce(FillBuffer()); 87 .Times(1).WillOnce(FillBuffer());
88 resampler.Resample(resampled_destination.get(), resampler.ChunkSize() / 2); 88 resampler.Resample(resampler.ChunkSize() / 2, resampled_destination.get());
89 ASSERT_NE(resampled_destination[0], 0); 89 ASSERT_NE(resampled_destination[0], 0);
90 90
91 // Flush and request more data, which should all be zeros now. 91 // Flush and request more data, which should all be zeros now.
92 resampler.Flush(); 92 resampler.Flush();
93 testing::Mock::VerifyAndClear(&mock_source); 93 testing::Mock::VerifyAndClear(&mock_source);
94 EXPECT_CALL(mock_source, ProvideInput(_, _)) 94 EXPECT_CALL(mock_source, ProvideInput(_, _))
95 .Times(1).WillOnce(ClearBuffer()); 95 .Times(1).WillOnce(ClearBuffer());
96 resampler.Resample(resampled_destination.get(), resampler.ChunkSize() / 2); 96 resampler.Resample(resampler.ChunkSize() / 2, resampled_destination.get());
97 for (int i = 0; i < resampler.ChunkSize() / 2; ++i) 97 for (int i = 0; i < resampler.ChunkSize() / 2; ++i)
98 ASSERT_FLOAT_EQ(resampled_destination[i], 0); 98 ASSERT_FLOAT_EQ(resampled_destination[i], 0);
99 } 99 }
100 100
101 // Test flush resets the internal state properly. 101 // Test flush resets the internal state properly.
102 TEST(SincResamplerTest, DISABLED_SetRatioBench) { 102 TEST(SincResamplerTest, DISABLED_SetRatioBench) {
103 MockSource mock_source; 103 MockSource mock_source;
104 SincResampler resampler( 104 SincResampler resampler(
105 kSampleRateRatio, 105 kSampleRateRatio, SincResampler::kDefaultRequestSize,
106 base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source))); 106 base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source)));
107 107
108 base::TimeTicks start = base::TimeTicks::HighResNow(); 108 base::TimeTicks start = base::TimeTicks::HighResNow();
109 for (int i = 1; i < 10000; ++i) 109 for (int i = 1; i < 10000; ++i)
110 resampler.SetRatio(1.0 / i); 110 resampler.SetRatio(1.0 / i);
111 double total_time_c_ms = 111 double total_time_c_ms =
112 (base::TimeTicks::HighResNow() - start).InMillisecondsF(); 112 (base::TimeTicks::HighResNow() - start).InMillisecondsF();
113 printf("SetRatio() took %.2fms.\n", total_time_c_ms); 113 printf("SetRatio() took %.2fms.\n", total_time_c_ms);
114 } 114 }
115 115
(...skipping 10 matching lines...) Expand all
126 // will be tested by the parameterized SincResampler tests below. 126 // will be tested by the parameterized SincResampler tests below.
127 #if defined(CONVOLVE_FUNC) 127 #if defined(CONVOLVE_FUNC)
128 TEST(SincResamplerTest, Convolve) { 128 TEST(SincResamplerTest, Convolve) {
129 #if defined(ARCH_CPU_X86_FAMILY) 129 #if defined(ARCH_CPU_X86_FAMILY)
130 ASSERT_TRUE(base::CPU().has_sse()); 130 ASSERT_TRUE(base::CPU().has_sse());
131 #endif 131 #endif
132 132
133 // Initialize a dummy resampler. 133 // Initialize a dummy resampler.
134 MockSource mock_source; 134 MockSource mock_source;
135 SincResampler resampler( 135 SincResampler resampler(
136 kSampleRateRatio, 136 kSampleRateRatio, SincResampler::kDefaultRequestSize,
137 base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source))); 137 base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source)));
138 138
139 // The optimized Convolve methods are slightly more precise than Convolve_C(), 139 // The optimized Convolve methods are slightly more precise than Convolve_C(),
140 // so comparison must be done using an epsilon. 140 // so comparison must be done using an epsilon.
141 static const double kEpsilon = 0.00000005; 141 static const double kEpsilon = 0.00000005;
142 142
143 // Use a kernel from SincResampler as input and kernel data, this has the 143 // Use a kernel from SincResampler as input and kernel data, this has the
144 // benefit of already being properly sized and aligned for Convolve_SSE(). 144 // benefit of already being properly sized and aligned for Convolve_SSE().
145 double result = resampler.Convolve_C( 145 double result = resampler.Convolve_C(
146 resampler.kernel_storage_.get(), resampler.kernel_storage_.get(), 146 resampler.kernel_storage_.get(), resampler.kernel_storage_.get(),
(...skipping 14 matching lines...) Expand all
161 } 161 }
162 #endif 162 #endif
163 163
164 // Benchmark for the various Convolve() methods. Make sure to build with 164 // Benchmark for the various Convolve() methods. Make sure to build with
165 // branding=Chrome so that DCHECKs are compiled out when benchmarking. Original 165 // branding=Chrome so that DCHECKs are compiled out when benchmarking. Original
166 // benchmarks were run with --convolve-iterations=50000000. 166 // benchmarks were run with --convolve-iterations=50000000.
167 TEST(SincResamplerTest, ConvolveBenchmark) { 167 TEST(SincResamplerTest, ConvolveBenchmark) {
168 // Initialize a dummy resampler. 168 // Initialize a dummy resampler.
169 MockSource mock_source; 169 MockSource mock_source;
170 SincResampler resampler( 170 SincResampler resampler(
171 kSampleRateRatio, 171 kSampleRateRatio, SincResampler::kDefaultRequestSize,
172 base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source))); 172 base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source)));
173 173
174 // Retrieve benchmark iterations from command line. 174 // Retrieve benchmark iterations from command line.
175 int convolve_iterations = 10; 175 int convolve_iterations = 10;
176 std::string iterations(CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 176 std::string iterations(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
177 kConvolveIterations)); 177 kConvolveIterations));
178 if (!iterations.empty()) 178 if (!iterations.empty())
179 base::StringToInt(iterations, &convolve_iterations); 179 base::StringToInt(iterations, &convolve_iterations);
180 180
181 printf("Benchmarking %d iterations:\n", convolve_iterations); 181 printf("Benchmarking %d iterations:\n", convolve_iterations);
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 #endif 227 #endif
228 } 228 }
229 229
230 #undef CONVOLVE_FUNC 230 #undef CONVOLVE_FUNC
231 231
232 // Fake audio source for testing the resampler. Generates a sinusoidal linear 232 // Fake audio source for testing the resampler. Generates a sinusoidal linear
233 // chirp (http://en.wikipedia.org/wiki/Chirp) which can be tuned to stress the 233 // chirp (http://en.wikipedia.org/wiki/Chirp) which can be tuned to stress the
234 // resampler for the specific sample rate conversion being used. 234 // resampler for the specific sample rate conversion being used.
235 class SinusoidalLinearChirpSource { 235 class SinusoidalLinearChirpSource {
236 public: 236 public:
237 SinusoidalLinearChirpSource(int sample_rate, int samples, 237 SinusoidalLinearChirpSource(int sample_rate,
238 int samples,
238 double max_frequency) 239 double max_frequency)
239 : sample_rate_(sample_rate), 240 : sample_rate_(sample_rate),
240 total_samples_(samples), 241 total_samples_(samples),
241 max_frequency_(max_frequency), 242 max_frequency_(max_frequency),
242 current_index_(0) { 243 current_index_(0) {
243 // Chirp rate. 244 // Chirp rate.
244 double duration = static_cast<double>(total_samples_) / sample_rate_; 245 double duration = static_cast<double>(total_samples_) / sample_rate_;
245 k_ = (max_frequency_ - kMinFrequency) / duration; 246 k_ = (max_frequency_ - kMinFrequency) / duration;
246 } 247 }
247 248
248 virtual ~SinusoidalLinearChirpSource() {} 249 virtual ~SinusoidalLinearChirpSource() {}
249 250
250 void ProvideInput(float* destination, int frames) { 251 void ProvideInput(int frames, float* destination) {
251 for (int i = 0; i < frames; ++i, ++current_index_) { 252 for (int i = 0; i < frames; ++i, ++current_index_) {
252 // Filter out frequencies higher than Nyquist. 253 // Filter out frequencies higher than Nyquist.
253 if (Frequency(current_index_) > 0.5 * sample_rate_) { 254 if (Frequency(current_index_) > 0.5 * sample_rate_) {
254 destination[i] = 0; 255 destination[i] = 0;
255 } else { 256 } else {
256 // Calculate time in seconds. 257 // Calculate time in seconds.
257 double t = static_cast<double>(current_index_) / sample_rate_; 258 double t = static_cast<double>(current_index_) / sample_rate_;
258 259
259 // Sinusoidal linear chirp. 260 // Sinusoidal linear chirp.
260 destination[i] = sin(2 * M_PI * (kMinFrequency * t + (k_ / 2) * t * t)); 261 destination[i] = sin(2 * M_PI * (kMinFrequency * t + (k_ / 2) * t * t));
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 311
311 // Nyquist frequency for the input sampling rate. 312 // Nyquist frequency for the input sampling rate.
312 double input_nyquist_freq = 0.5 * input_rate_; 313 double input_nyquist_freq = 0.5 * input_rate_;
313 314
314 // Source for data to be resampled. 315 // Source for data to be resampled.
315 SinusoidalLinearChirpSource resampler_source( 316 SinusoidalLinearChirpSource resampler_source(
316 input_rate_, input_samples, input_nyquist_freq); 317 input_rate_, input_samples, input_nyquist_freq);
317 318
318 const double io_ratio = input_rate_ / static_cast<double>(output_rate_); 319 const double io_ratio = input_rate_ / static_cast<double>(output_rate_);
319 SincResampler resampler( 320 SincResampler resampler(
320 io_ratio, 321 io_ratio, SincResampler::kDefaultRequestSize,
321 base::Bind(&SinusoidalLinearChirpSource::ProvideInput, 322 base::Bind(&SinusoidalLinearChirpSource::ProvideInput,
322 base::Unretained(&resampler_source))); 323 base::Unretained(&resampler_source)));
323 324
324 // Force an update to the sample rate ratio to ensure dyanmic sample rate 325 // Force an update to the sample rate ratio to ensure dyanmic sample rate
325 // changes are working correctly. 326 // changes are working correctly.
326 scoped_ptr<float[]> kernel(new float[SincResampler::kKernelStorageSize]); 327 scoped_ptr<float[]> kernel(new float[SincResampler::kKernelStorageSize]);
327 memcpy(kernel.get(), resampler.get_kernel_for_testing(), 328 memcpy(kernel.get(), resampler.get_kernel_for_testing(),
328 SincResampler::kKernelStorageSize); 329 SincResampler::kKernelStorageSize);
329 resampler.SetRatio(M_PI); 330 resampler.SetRatio(M_PI);
330 ASSERT_NE(0, memcmp(kernel.get(), resampler.get_kernel_for_testing(), 331 ASSERT_NE(0, memcmp(kernel.get(), resampler.get_kernel_for_testing(),
331 SincResampler::kKernelStorageSize)); 332 SincResampler::kKernelStorageSize));
332 resampler.SetRatio(io_ratio); 333 resampler.SetRatio(io_ratio);
333 ASSERT_EQ(0, memcmp(kernel.get(), resampler.get_kernel_for_testing(), 334 ASSERT_EQ(0, memcmp(kernel.get(), resampler.get_kernel_for_testing(),
334 SincResampler::kKernelStorageSize)); 335 SincResampler::kKernelStorageSize));
335 336
336 // TODO(dalecurtis): If we switch to AVX/SSE optimization, we'll need to 337 // TODO(dalecurtis): If we switch to AVX/SSE optimization, we'll need to
337 // allocate these on 32-byte boundaries and ensure they're sized % 32 bytes. 338 // allocate these on 32-byte boundaries and ensure they're sized % 32 bytes.
338 scoped_ptr<float[]> resampled_destination(new float[output_samples]); 339 scoped_ptr<float[]> resampled_destination(new float[output_samples]);
339 scoped_ptr<float[]> pure_destination(new float[output_samples]); 340 scoped_ptr<float[]> pure_destination(new float[output_samples]);
340 341
341 // Generate resampled signal. 342 // Generate resampled signal.
342 resampler.Resample(resampled_destination.get(), output_samples); 343 resampler.Resample(output_samples, resampled_destination.get());
343 344
344 // Generate pure signal. 345 // Generate pure signal.
345 SinusoidalLinearChirpSource pure_source( 346 SinusoidalLinearChirpSource pure_source(
346 output_rate_, output_samples, input_nyquist_freq); 347 output_rate_, output_samples, input_nyquist_freq);
347 pure_source.ProvideInput(pure_destination.get(), output_samples); 348 pure_source.ProvideInput(output_samples, pure_destination.get());
348 349
349 // Range of the Nyquist frequency (0.5 * min(input rate, output_rate)) which 350 // Range of the Nyquist frequency (0.5 * min(input rate, output_rate)) which
350 // we refer to as low and high. 351 // we refer to as low and high.
351 static const double kLowFrequencyNyquistRange = 0.7; 352 static const double kLowFrequencyNyquistRange = 0.7;
352 static const double kHighFrequencyNyquistRange = 0.9; 353 static const double kHighFrequencyNyquistRange = 0.9;
353 354
354 // Calculate Root-Mean-Square-Error and maximum error for the resampling. 355 // Calculate Root-Mean-Square-Error and maximum error for the resampling.
355 double sum_of_squares = 0; 356 double sum_of_squares = 0;
356 double low_freq_max_error = 0; 357 double low_freq_max_error = 0;
357 double high_freq_max_error = 0; 358 double high_freq_max_error = 0;
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
434 std::tr1::make_tuple(11025, 192000, kResamplingRMSError, -62.61), 435 std::tr1::make_tuple(11025, 192000, kResamplingRMSError, -62.61),
435 std::tr1::make_tuple(16000, 192000, kResamplingRMSError, -63.14), 436 std::tr1::make_tuple(16000, 192000, kResamplingRMSError, -63.14),
436 std::tr1::make_tuple(22050, 192000, kResamplingRMSError, -62.42), 437 std::tr1::make_tuple(22050, 192000, kResamplingRMSError, -62.42),
437 std::tr1::make_tuple(32000, 192000, kResamplingRMSError, -63.38), 438 std::tr1::make_tuple(32000, 192000, kResamplingRMSError, -63.38),
438 std::tr1::make_tuple(44100, 192000, kResamplingRMSError, -62.63), 439 std::tr1::make_tuple(44100, 192000, kResamplingRMSError, -62.63),
439 std::tr1::make_tuple(48000, 192000, kResamplingRMSError, -73.44), 440 std::tr1::make_tuple(48000, 192000, kResamplingRMSError, -73.44),
440 std::tr1::make_tuple(96000, 192000, kResamplingRMSError, -73.52), 441 std::tr1::make_tuple(96000, 192000, kResamplingRMSError, -73.52),
441 std::tr1::make_tuple(192000, 192000, kResamplingRMSError, -73.52))); 442 std::tr1::make_tuple(192000, 192000, kResamplingRMSError, -73.52)));
442 443
443 } // namespace media 444 } // namespace media
OLDNEW
« no previous file with comments | « media/base/sinc_resampler.cc ('k') | remoting/codec/audio_encoder_opus.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698