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

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

Issue 1854433002: Clamp AudioBuffer float to int{16,32} conversion (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix handling of 32-bit, fix asymmetric scale Created 4 years, 8 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
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 #include <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 7
8 #include <limits> 8 #include <limits>
9 9
10 #include "base/macros.h" 10 #include "base/macros.h"
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 bus->Zero(); 245 bus->Zero();
246 for (int i = 0; i < bus->channels(); ++i) { 246 for (int i = 0; i < bus->channels(); ++i) {
247 SCOPED_TRACE("All Zero"); 247 SCOPED_TRACE("All Zero");
248 VerifyValue(bus->channel(i), bus->frames(), 0); 248 VerifyValue(bus->channel(i), bus->frames(), 0);
249 } 249 }
250 EXPECT_TRUE(bus->AreFramesZero()); 250 EXPECT_TRUE(bus->AreFramesZero());
251 } 251 }
252 252
253 // Each test vector represents two channels of data in the following arbitrary 253 // Each test vector represents two channels of data in the following arbitrary
254 // layout: <min, zero, max, min, max / 2, min / 2, zero, max, zero, zero>. 254 // layout: <min, zero, max, min, max / 2, min / 2, zero, max, zero, zero>.
255 // The max / 2 values have 0.5 added to them to round them to the nearest int.
255 static const int kTestVectorSize = 10; 256 static const int kTestVectorSize = 10;
256 static const uint8_t kTestVectorUint8[kTestVectorSize] = { 257 static const uint8_t kTestVectorUint8[kTestVectorSize] = {
257 0, -INT8_MIN, UINT8_MAX, 258 0,
258 0, INT8_MAX / 2 + 128, INT8_MIN / 2 + 128, 259 -INT8_MIN,
259 -INT8_MIN, UINT8_MAX, -INT8_MIN, 260 UINT8_MAX,
261 0,
262 (INT8_MAX / 2.0 + 0.5) + 128,
263 (INT8_MIN / 2.0) + 128,
264 -INT8_MIN,
265 UINT8_MAX,
266 -INT8_MIN,
260 -INT8_MIN}; 267 -INT8_MIN};
261 static const int16_t kTestVectorInt16[kTestVectorSize] = { 268 static const int16_t kTestVectorInt16[kTestVectorSize] = {
262 INT16_MIN, 0, INT16_MAX, INT16_MIN, INT16_MAX / 2, 269 INT16_MIN, 0, INT16_MAX, INT16_MIN, INT16_MAX / 2.0 + 0.5,
263 INT16_MIN / 2, 0, INT16_MAX, 0, 0}; 270 INT16_MIN / 2.0, 0, INT16_MAX, 0, 0};
264 static const int32_t kTestVectorInt32[kTestVectorSize] = { 271 static const int32_t kTestVectorInt32[kTestVectorSize] = {
265 INT32_MIN, 0, INT32_MAX, INT32_MIN, INT32_MAX / 2, 272 INT32_MIN, 0, INT32_MAX, INT32_MIN, INT32_MAX / 2.0 + 0.5,
266 INT32_MIN / 2, 0, INT32_MAX, 0, 0}; 273 INT32_MIN / 2.0, 0, INT32_MAX, 0, 0};
267 274
268 // Expected results. 275 // Expected results.
269 static const int kTestVectorFrames = kTestVectorSize / 2; 276 static const int kTestVectorFrames = kTestVectorSize / 2;
270 static const float kTestVectorResult[][kTestVectorFrames] = { 277 static const float kTestVectorResult[][kTestVectorFrames] = {
271 { -1, 1, 0.5, 0, 0 }, { 0, -1, -0.5, 1, 0 }}; 278 { -1, 1, 0.5, 0, 0 }, { 0, -1, -0.5, 1, 0 }};
272 static const int kTestVectorChannels = arraysize(kTestVectorResult); 279 static const int kTestVectorChannels = arraysize(kTestVectorResult);
273 280
274 // Verify FromInterleaved() deinterleaves audio in supported formats correctly. 281 // Verify FromInterleaved() deinterleaves audio in supported formats correctly.
275 TEST_F(AudioBusTest, FromInterleaved) { 282 TEST_F(AudioBusTest, FromInterleaved) {
276 scoped_ptr<AudioBus> bus = AudioBus::Create( 283 scoped_ptr<AudioBus> bus = AudioBus::Create(
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 } 366 }
360 { 367 {
361 SCOPED_TRACE("int32_t"); 368 SCOPED_TRACE("int32_t");
362 int32_t test_array[arraysize(kTestVectorInt32)]; 369 int32_t test_array[arraysize(kTestVectorInt32)];
363 bus->ToInterleaved(bus->frames(), sizeof(*kTestVectorInt32), test_array); 370 bus->ToInterleaved(bus->frames(), sizeof(*kTestVectorInt32), test_array);
364 371
365 // Some compilers get better precision than others on the half-max test, so 372 // Some compilers get better precision than others on the half-max test, so
366 // let the test pass with an off by one check on the half-max. 373 // let the test pass with an off by one check on the half-max.
367 int32_t fixed_test_array[arraysize(kTestVectorInt32)]; 374 int32_t fixed_test_array[arraysize(kTestVectorInt32)];
368 memcpy(fixed_test_array, kTestVectorInt32, sizeof(kTestVectorInt32)); 375 memcpy(fixed_test_array, kTestVectorInt32, sizeof(kTestVectorInt32));
369 ASSERT_EQ(fixed_test_array[4], std::numeric_limits<int32_t>::max() / 2); 376 ASSERT_EQ(
377 fixed_test_array[4],
378 round(static_cast<double>(std::numeric_limits<int32_t>::max() / 2.0)));
370 fixed_test_array[4]++; 379 fixed_test_array[4]++;
371 380
372 ASSERT_TRUE( 381 ASSERT_TRUE(
373 memcmp(test_array, kTestVectorInt32, sizeof(kTestVectorInt32)) == 0 || 382 memcmp(test_array, kTestVectorInt32, sizeof(kTestVectorInt32)) == 0 ||
374 memcmp(test_array, fixed_test_array, sizeof(fixed_test_array)) == 0); 383 memcmp(test_array, fixed_test_array, sizeof(fixed_test_array)) == 0);
375 } 384 }
376 } 385 }
377 386
378 // Verify ToInterleavedPartial() interleaves audio correctly. 387 // Verify ToInterleavedPartial() interleaves audio correctly.
379 TEST_F(AudioBusTest, ToInterleavedPartial) { 388 TEST_F(AudioBusTest, ToInterleavedPartial) {
380 // Only interleave the middle two frames in each channel. 389 // Only interleave the middle two frames in each channel.
381 static const int kPartialStart = 1; 390 static const int kPartialStart = 1;
382 static const int kPartialFrames = 2; 391 static const int kPartialFrames = 2;
383 ASSERT_LE(kPartialStart + kPartialFrames, kTestVectorFrames); 392 ASSERT_LE(kPartialStart + kPartialFrames, kTestVectorFrames);
384 393
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 431
423 // Verify zero volume case. 432 // Verify zero volume case.
424 bus->Scale(0); 433 bus->Scale(0);
425 for (int i = 0; i < bus->channels(); ++i) { 434 for (int i = 0; i < bus->channels(); ++i) {
426 SCOPED_TRACE("Zero Scale"); 435 SCOPED_TRACE("Zero Scale");
427 VerifyValue(bus->channel(i), bus->frames(), 0); 436 VerifyValue(bus->channel(i), bus->frames(), 0);
428 } 437 }
429 } 438 }
430 439
431 } // namespace media 440 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698