OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // MSVC++ requires this to be set before any other includes to get M_SQRT1_2. |
| 6 #define _USE_MATH_DEFINES |
| 7 |
| 8 #include "media/base/channel_mixing_matrix.h" |
| 9 |
| 10 #include <cmath> |
| 11 |
| 12 #include "base/strings/stringprintf.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace media { |
| 16 |
| 17 // Test all possible layout conversions can be constructed and mixed. |
| 18 TEST(ChannelMixingMatrixTest, ConstructAllPossibleLayouts) { |
| 19 for (ChannelLayout input_layout = CHANNEL_LAYOUT_MONO; |
| 20 input_layout <= CHANNEL_LAYOUT_MAX; |
| 21 input_layout = static_cast<ChannelLayout>(input_layout + 1)) { |
| 22 for (ChannelLayout output_layout = CHANNEL_LAYOUT_MONO; |
| 23 // TODO(wtc): why do we only test up to CHANNEL_LAYOUT_STEREO_DOWNMIX? |
| 24 output_layout < CHANNEL_LAYOUT_STEREO_DOWNMIX; |
| 25 output_layout = static_cast<ChannelLayout>(output_layout + 1)) { |
| 26 // DISCRETE can't be tested here based on the current approach. |
| 27 // CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC is not mixable. |
| 28 if (input_layout == CHANNEL_LAYOUT_DISCRETE || |
| 29 input_layout == CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC || |
| 30 output_layout == CHANNEL_LAYOUT_DISCRETE || |
| 31 output_layout == CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC) { |
| 32 continue; |
| 33 } |
| 34 |
| 35 SCOPED_TRACE(base::StringPrintf( |
| 36 "Input Layout: %d, Output Layout: %d", input_layout, output_layout)); |
| 37 ChannelMixingMatrix matrix_builder( |
| 38 input_layout, |
| 39 ChannelLayoutToChannelCount(input_layout), |
| 40 output_layout, |
| 41 ChannelLayoutToChannelCount(output_layout)); |
| 42 std::vector<std::vector<float>> matrix; |
| 43 matrix_builder.CreateTransformationMatrix(&matrix); |
| 44 } |
| 45 } |
| 46 } |
| 47 |
| 48 // Verify channels are mixed and scaled correctly. |
| 49 TEST(ChannelMixingMatrixTest, StereoToMono) { |
| 50 ChannelLayout input_layout = CHANNEL_LAYOUT_STEREO; |
| 51 ChannelLayout output_layout = CHANNEL_LAYOUT_MONO; |
| 52 ChannelMixingMatrix matrix_builder( |
| 53 input_layout, |
| 54 ChannelLayoutToChannelCount(input_layout), |
| 55 output_layout, |
| 56 ChannelLayoutToChannelCount(output_layout)); |
| 57 std::vector<std::vector<float>> matrix; |
| 58 bool remapping = matrix_builder.CreateTransformationMatrix(&matrix); |
| 59 |
| 60 // Input: stereo |
| 61 // LEFT RIGHT |
| 62 // Output: mono CENTER 0.5 0.5 |
| 63 // |
| 64 EXPECT_FALSE(remapping); |
| 65 EXPECT_EQ(1u, matrix.size()); |
| 66 EXPECT_EQ(2u, matrix[0].size()); |
| 67 EXPECT_EQ(0.5f, matrix[0][0]); |
| 68 EXPECT_EQ(0.5f, matrix[0][1]); |
| 69 } |
| 70 |
| 71 TEST(ChannelMixingMatrixTest, MonoToStereo) { |
| 72 ChannelLayout input_layout = CHANNEL_LAYOUT_MONO; |
| 73 ChannelLayout output_layout = CHANNEL_LAYOUT_STEREO; |
| 74 ChannelMixingMatrix matrix_builder( |
| 75 input_layout, |
| 76 ChannelLayoutToChannelCount(input_layout), |
| 77 output_layout, |
| 78 ChannelLayoutToChannelCount(output_layout)); |
| 79 std::vector<std::vector<float>> matrix; |
| 80 bool remapping = matrix_builder.CreateTransformationMatrix(&matrix); |
| 81 |
| 82 // Input: mono |
| 83 // CENTER |
| 84 // Output: stereo LEFT 1 |
| 85 // RIGHT 1 |
| 86 // |
| 87 EXPECT_TRUE(remapping); |
| 88 EXPECT_EQ(2u, matrix.size()); |
| 89 EXPECT_EQ(1u, matrix[0].size()); |
| 90 EXPECT_EQ(1.0f, matrix[0][0]); |
| 91 EXPECT_EQ(1u, matrix[1].size()); |
| 92 EXPECT_EQ(1.0f, matrix[1][0]); |
| 93 } |
| 94 |
| 95 TEST(ChannelMixingMatrixTest, FiveOneToMono) { |
| 96 ChannelLayout input_layout = CHANNEL_LAYOUT_5_1; |
| 97 ChannelLayout output_layout = CHANNEL_LAYOUT_MONO; |
| 98 ChannelMixingMatrix matrix_builder( |
| 99 input_layout, |
| 100 ChannelLayoutToChannelCount(input_layout), |
| 101 output_layout, |
| 102 ChannelLayoutToChannelCount(output_layout)); |
| 103 std::vector<std::vector<float>> matrix; |
| 104 bool remapping = matrix_builder.CreateTransformationMatrix(&matrix); |
| 105 |
| 106 // Input: 5.1 |
| 107 // LEFT RIGHT CENTER SIDE_LEFT SIDE_RIGHT |
| 108 // Output: mono CENTER 1/sqrt(2) 1/sqrt(2) 1 1/sqrt(2) 1/sqrt(2) |
| 109 // |
| 110 EXPECT_FALSE(remapping); |
| 111 EXPECT_EQ(1u, matrix.size()); |
| 112 EXPECT_EQ(6u, matrix[0].size()); |
| 113 EXPECT_EQ(static_cast<float>(M_SQRT1_2), matrix[0][0]); |
| 114 EXPECT_EQ(static_cast<float>(M_SQRT1_2), matrix[0][1]); |
| 115 // The center channel will be mixed at scale 1. |
| 116 EXPECT_EQ(1.0f, matrix[0][2]); |
| 117 EXPECT_EQ(static_cast<float>(M_SQRT1_2), matrix[0][3]); |
| 118 EXPECT_EQ(static_cast<float>(M_SQRT1_2), matrix[0][4]); |
| 119 EXPECT_EQ(static_cast<float>(M_SQRT1_2), matrix[0][5]); |
| 120 } |
| 121 |
| 122 TEST(ChannelMixingMatrixTest, DiscreteToDiscrete) { |
| 123 const struct { |
| 124 int input_channels; |
| 125 int output_channels; |
| 126 } test_case[] = { |
| 127 {2, 2}, {2, 5}, {5, 2}, |
| 128 }; |
| 129 |
| 130 for (size_t n = 0; n < arraysize(test_case); n++) { |
| 131 int input_channels = test_case[n].input_channels; |
| 132 int output_channels = test_case[n].output_channels; |
| 133 ChannelMixingMatrix matrix_builder(CHANNEL_LAYOUT_DISCRETE, |
| 134 input_channels, |
| 135 CHANNEL_LAYOUT_DISCRETE, |
| 136 output_channels); |
| 137 std::vector<std::vector<float>> matrix; |
| 138 bool remapping = matrix_builder.CreateTransformationMatrix(&matrix); |
| 139 EXPECT_TRUE(remapping); |
| 140 EXPECT_EQ(static_cast<size_t>(output_channels), matrix.size()); |
| 141 for (int i = 0; i < output_channels; i++) { |
| 142 EXPECT_EQ(static_cast<size_t>(input_channels), matrix[i].size()); |
| 143 for (int j = 0; j < input_channels; j++) { |
| 144 if (i == j) { |
| 145 EXPECT_EQ(1.0f, matrix[i][j]); |
| 146 } else { |
| 147 EXPECT_EQ(0.0f, matrix[i][j]); |
| 148 } |
| 149 } |
| 150 } |
| 151 } |
| 152 } |
| 153 |
| 154 } // namespace media |
OLD | NEW |