Chromium Code Reviews| 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 // Note: 1/sqrt(2) is shown as 0.707. | |
| 107 // | |
| 108 // Input: 5.1 | |
| 109 // LEFT RIGHT CENTER LFE SIDE_LEFT SIDE_RIGHT | |
| 110 // Output: mono CENTER 0.707 0.707 1 0.707 0.707 0.707 | |
|
wtc
2014/10/23 16:55:13
Dale: I added graphical representations of the mat
DaleCurtis
2014/10/23 17:15:27
By all means, please analyze :) I intuitively chos
wtc
2014/10/23 18:39:45
That's a good rationale and it's supported by your
| |
| 111 // | |
| 112 EXPECT_FALSE(remapping); | |
| 113 EXPECT_EQ(1u, matrix.size()); | |
| 114 EXPECT_EQ(6u, matrix[0].size()); | |
| 115 EXPECT_EQ(static_cast<float>(M_SQRT1_2), matrix[0][0]); | |
| 116 EXPECT_EQ(static_cast<float>(M_SQRT1_2), matrix[0][1]); | |
| 117 // The center channel will be mixed at scale 1. | |
| 118 EXPECT_EQ(1.0f, matrix[0][2]); | |
| 119 EXPECT_EQ(static_cast<float>(M_SQRT1_2), matrix[0][3]); | |
| 120 EXPECT_EQ(static_cast<float>(M_SQRT1_2), matrix[0][4]); | |
| 121 EXPECT_EQ(static_cast<float>(M_SQRT1_2), matrix[0][5]); | |
| 122 } | |
| 123 | |
| 124 TEST(ChannelMixingMatrixTest, DiscreteToDiscrete) { | |
| 125 const struct { | |
| 126 int input_channels; | |
| 127 int output_channels; | |
| 128 } test_case[] = { | |
| 129 {2, 2}, {2, 5}, {5, 2}, | |
| 130 }; | |
| 131 | |
| 132 for (size_t n = 0; n < arraysize(test_case); n++) { | |
| 133 int input_channels = test_case[n].input_channels; | |
| 134 int output_channels = test_case[n].output_channels; | |
| 135 ChannelMixingMatrix matrix_builder(CHANNEL_LAYOUT_DISCRETE, | |
| 136 input_channels, | |
| 137 CHANNEL_LAYOUT_DISCRETE, | |
| 138 output_channels); | |
| 139 std::vector<std::vector<float>> matrix; | |
| 140 bool remapping = matrix_builder.CreateTransformationMatrix(&matrix); | |
| 141 EXPECT_TRUE(remapping); | |
| 142 EXPECT_EQ(static_cast<size_t>(output_channels), matrix.size()); | |
| 143 for (int i = 0; i < output_channels; i++) { | |
| 144 EXPECT_EQ(static_cast<size_t>(input_channels), matrix[i].size()); | |
| 145 for (int j = 0; j < input_channels; j++) { | |
| 146 if (i == j) { | |
| 147 EXPECT_EQ(1.0f, matrix[i][j]); | |
| 148 } else { | |
| 149 EXPECT_EQ(0.0f, matrix[i][j]); | |
| 150 } | |
| 151 } | |
| 152 } | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 } // namespace media | |
| OLD | NEW |