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

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

Issue 12387006: Pass more detailed audio hardware configuration information to the renderer (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 9 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
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_SQRT1_2. 5 // MSVC++ requires this to be set before any other includes to get M_SQRT1_2.
6 #define _USE_MATH_DEFINES 6 #define _USE_MATH_DEFINES
7 7
8 #include "media/base/channel_mixer.h" 8 #include "media/base/channel_mixer.h"
9 9
10 #include <algorithm> 10 #include <algorithm>
11 #include <cmath> 11 #include <cmath>
12 12
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "media/audio/audio_parameters.h"
14 #include "media/base/audio_bus.h" 15 #include "media/base/audio_bus.h"
15 #include "media/base/vector_math.h" 16 #include "media/base/vector_math.h"
16 17
17 namespace media { 18 namespace media {
18 19
19 // Default scale factor for mixing two channels together. We use a different 20 // Default scale factor for mixing two channels together. We use a different
20 // value for stereo -> mono and mono -> stereo mixes. 21 // value for stereo -> mono and mono -> stereo mixes.
21 static const float kEqualPowerScale = static_cast<float>(M_SQRT1_2); 22 static const float kEqualPowerScale = static_cast<float>(M_SQRT1_2);
22 23
23 static int ValidateLayout(ChannelLayout layout) { 24 static void ValidateLayout(ChannelLayout layout) {
24 CHECK_NE(layout, CHANNEL_LAYOUT_NONE); 25 CHECK_NE(layout, CHANNEL_LAYOUT_NONE);
25 CHECK_NE(layout, CHANNEL_LAYOUT_MAX); 26 CHECK_NE(layout, CHANNEL_LAYOUT_MAX);
27 CHECK_NE(layout, CHANNEL_LAYOUT_UNSUPPORTED);
26 28
27 // TODO(dalecurtis, crogers): We will eventually handle unsupported layouts by 29 // Channels must be received externally with discrete layout.
DaleCurtis 2013/03/11 19:21:50 Add this to the CHECK_NE above and then move this
Chris Rogers 2013/03/12 22:32:31 Done.
28 // simply copying the input channels to the output channels, similar to if the 30 if (layout == CHANNEL_LAYOUT_DISCRETE)
29 // user requests identical input and output layouts today. 31 return;
30 CHECK_NE(layout, CHANNEL_LAYOUT_UNSUPPORTED);
31 32
32 // Verify there's at least one channel. Should always be true here by virtue 33 // Verify there's at least one channel. Should always be true here by virtue
33 // of not being one of the invalid layouts, but lets double check to be sure. 34 // of not being one of the invalid layouts, but lets double check to be sure.
34 int channel_count = ChannelLayoutToChannelCount(layout); 35 int channel_count = ChannelLayoutToChannelCount(layout);
35 DCHECK_GT(channel_count, 0); 36 DCHECK_GT(channel_count, 0);
36 37
37 // If we have more than one channel, verify a symmetric layout for sanity. 38 // If we have more than one channel, verify a symmetric layout for sanity.
38 // The unit test will verify all possible layouts, so this can be a DCHECK. 39 // The unit test will verify all possible layouts, so this can be a DCHECK.
39 // Symmetry allows simplifying the matrix building code by allowing us to 40 // Symmetry allows simplifying the matrix building code by allowing us to
40 // assume that if one channel of a pair exists, the other will too. 41 // assume that if one channel of a pair exists, the other will too.
41 if (channel_count > 1) { 42 if (channel_count > 1) {
42 DCHECK((ChannelOrder(layout, LEFT) >= 0 && 43 DCHECK((ChannelOrder(layout, LEFT) >= 0 &&
43 ChannelOrder(layout, RIGHT) >= 0) || 44 ChannelOrder(layout, RIGHT) >= 0) ||
44 (ChannelOrder(layout, SIDE_LEFT) >= 0 && 45 (ChannelOrder(layout, SIDE_LEFT) >= 0 &&
45 ChannelOrder(layout, SIDE_RIGHT) >= 0) || 46 ChannelOrder(layout, SIDE_RIGHT) >= 0) ||
46 (ChannelOrder(layout, BACK_LEFT) >= 0 && 47 (ChannelOrder(layout, BACK_LEFT) >= 0 &&
47 ChannelOrder(layout, BACK_RIGHT) >= 0) || 48 ChannelOrder(layout, BACK_RIGHT) >= 0) ||
48 (ChannelOrder(layout, LEFT_OF_CENTER) >= 0 && 49 (ChannelOrder(layout, LEFT_OF_CENTER) >= 0 &&
49 ChannelOrder(layout, RIGHT_OF_CENTER) >= 0)) 50 ChannelOrder(layout, RIGHT_OF_CENTER) >= 0))
50 << "Non-symmetric channel layout encountered."; 51 << "Non-symmetric channel layout encountered.";
51 } else { 52 } else {
52 DCHECK_EQ(layout, CHANNEL_LAYOUT_MONO); 53 DCHECK_EQ(layout, CHANNEL_LAYOUT_MONO);
53 } 54 }
54 55
55 return channel_count; 56 return;
56 } 57 }
57 58
58 ChannelMixer::ChannelMixer(ChannelLayout input, ChannelLayout output) 59 ChannelMixer::ChannelMixer(ChannelLayout input_layout,
59 : input_layout_(input), 60 ChannelLayout output_layout) {
60 output_layout_(output), 61 AudioParameters input_params(
61 remapping_(false) { 62 AudioParameters::AUDIO_PCM_LOW_LATENCY,
63 input_layout,
64 44100,
65 16,
66 128);
67 AudioParameters output_params(
68 AudioParameters::AUDIO_PCM_LOW_LATENCY,
69 output_layout,
70 44100,
71 16,
72 128);
73 Initialize(input_params, output_params);
74 }
75
76 ChannelMixer::ChannelMixer(
77 const AudioParameters& input, const AudioParameters& output) {
78 Initialize(input, output);
79 }
80
81 void ChannelMixer::Initialize(
82 const AudioParameters& input, const AudioParameters& output) {
83 input_layout_ = input.channel_layout();
84 output_layout_ = output.channel_layout();
85 remapping_ = false;
86
62 // Stereo down mix should never be the output layout. 87 // Stereo down mix should never be the output layout.
63 CHECK_NE(output_layout_, CHANNEL_LAYOUT_STEREO_DOWNMIX); 88 CHECK_NE(output_layout_, CHANNEL_LAYOUT_STEREO_DOWNMIX);
64 89
65 int input_channels = ValidateLayout(input_layout_); 90 int input_channels = input.channels();
66 int output_channels = ValidateLayout(output_layout_); 91 int output_channels = output.channels();
92 ValidateLayout(input_layout_);
93 ValidateLayout(output_layout_);
67 94
68 // Size out the initial matrix. 95 // Size out the initial matrix.
69 matrix_.reserve(output_channels); 96 matrix_.reserve(output_channels);
70 for (int output_ch = 0; output_ch < output_channels; ++output_ch) 97 for (int output_ch = 0; output_ch < output_channels; ++output_ch)
71 matrix_.push_back(std::vector<float>(input_channels, 0)); 98 matrix_.push_back(std::vector<float>(input_channels, 0));
72 99
100 // First check for discrete case.
101 if (input_layout_ == CHANNEL_LAYOUT_DISCRETE ||
102 output_layout_ == CHANNEL_LAYOUT_DISCRETE) {
103 // If the number of input channels is more than output channels, then
104 // copy as many as we can then drop the remaining input channels.
105 // If the number of input channels is less than output channels, then
106 // copy them all, then zero out the remaining output channels.
107 int passthrough_channels = std::min(input_channels, output_channels);
108 for (int i = 0; i < passthrough_channels; ++i)
109 matrix_[i][i] = 1;
110
111 remapping_ = true;
112 return;
113 }
114
73 // Route matching channels and figure out which ones aren't accounted for. 115 // Route matching channels and figure out which ones aren't accounted for.
74 for (Channels ch = LEFT; ch < CHANNELS_MAX; 116 for (Channels ch = LEFT; ch < CHANNELS_MAX;
75 ch = static_cast<Channels>(ch + 1)) { 117 ch = static_cast<Channels>(ch + 1)) {
76 int input_ch_index = ChannelOrder(input_layout_, ch); 118 int input_ch_index = ChannelOrder(input_layout_, ch);
77 int output_ch_index = ChannelOrder(output_layout_, ch); 119 int output_ch_index = ChannelOrder(output_layout_, ch);
78 120
79 if (input_ch_index < 0) 121 if (input_ch_index < 0)
80 continue; 122 continue;
81 123
82 if (output_ch_index < 0) { 124 if (output_ch_index < 0) {
(...skipping 12 matching lines...) Expand all
95 // Since all output channels map directly to inputs we can optimize. 137 // Since all output channels map directly to inputs we can optimize.
96 remapping_ = true; 138 remapping_ = true;
97 return; 139 return;
98 } 140 }
99 141
100 // Mix front LR into center. 142 // Mix front LR into center.
101 if (IsUnaccounted(LEFT)) { 143 if (IsUnaccounted(LEFT)) {
102 // When down mixing to mono from stereo, we need to be careful of full scale 144 // When down mixing to mono from stereo, we need to be careful of full scale
103 // stereo mixes. Scaling by 1 / sqrt(2) here will likely lead to clipping 145 // stereo mixes. Scaling by 1 / sqrt(2) here will likely lead to clipping
104 // so we use 1 / 2 instead. 146 // so we use 1 / 2 instead.
105 float scale = (output == CHANNEL_LAYOUT_MONO && input_channels == 2) ? 147 float scale =
148 (output_layout_ == CHANNEL_LAYOUT_MONO && input_channels == 2) ?
106 0.5 : kEqualPowerScale; 149 0.5 : kEqualPowerScale;
107 Mix(LEFT, CENTER, scale); 150 Mix(LEFT, CENTER, scale);
108 Mix(RIGHT, CENTER, scale); 151 Mix(RIGHT, CENTER, scale);
109 } 152 }
110 153
111 // Mix center into front LR. 154 // Mix center into front LR.
112 if (IsUnaccounted(CENTER)) { 155 if (IsUnaccounted(CENTER)) {
113 // When up mixing from mono, just do a copy to front LR. 156 // When up mixing from mono, just do a copy to front LR.
114 float scale = (input == CHANNEL_LAYOUT_MONO) ? 1 : kEqualPowerScale; 157 float scale =
158 (input_layout_ == CHANNEL_LAYOUT_MONO) ? 1 : kEqualPowerScale;
115 MixWithoutAccounting(CENTER, LEFT, scale); 159 MixWithoutAccounting(CENTER, LEFT, scale);
116 Mix(CENTER, RIGHT, scale); 160 Mix(CENTER, RIGHT, scale);
117 } 161 }
118 162
119 // Mix back LR into: side LR || back center || front LR || front center. 163 // Mix back LR into: side LR || back center || front LR || front center.
120 if (IsUnaccounted(BACK_LEFT)) { 164 if (IsUnaccounted(BACK_LEFT)) {
121 if (HasOutputChannel(SIDE_LEFT)) { 165 if (HasOutputChannel(SIDE_LEFT)) {
122 // If we have side LR, mix back LR into side LR, but instead if the input 166 // If we have side LR, mix back LR into side LR, but instead if the input
123 // doesn't have side LR (but output does) copy back LR to side LR. 167 // doesn't have side LR (but output does) copy back LR to side LR.
124 float scale = HasInputChannel(SIDE_LEFT) ? kEqualPowerScale : 1; 168 float scale = HasInputChannel(SIDE_LEFT) ? kEqualPowerScale : 1;
125 Mix(BACK_LEFT, SIDE_LEFT, scale); 169 Mix(BACK_LEFT, SIDE_LEFT, scale);
126 Mix(BACK_RIGHT, SIDE_RIGHT, scale); 170 Mix(BACK_RIGHT, SIDE_RIGHT, scale);
127 } else if (HasOutputChannel(BACK_CENTER)) { 171 } else if (HasOutputChannel(BACK_CENTER)) {
128 // Mix back LR into back center. 172 // Mix back LR into back center.
129 Mix(BACK_LEFT, BACK_CENTER, kEqualPowerScale); 173 Mix(BACK_LEFT, BACK_CENTER, kEqualPowerScale);
130 Mix(BACK_RIGHT, BACK_CENTER, kEqualPowerScale); 174 Mix(BACK_RIGHT, BACK_CENTER, kEqualPowerScale);
131 } else if (output > CHANNEL_LAYOUT_MONO) { 175 } else if (output_layout_ > CHANNEL_LAYOUT_MONO) {
132 // Mix back LR into front LR. 176 // Mix back LR into front LR.
133 Mix(BACK_LEFT, LEFT, kEqualPowerScale); 177 Mix(BACK_LEFT, LEFT, kEqualPowerScale);
134 Mix(BACK_RIGHT, RIGHT, kEqualPowerScale); 178 Mix(BACK_RIGHT, RIGHT, kEqualPowerScale);
135 } else { 179 } else {
136 // Mix back LR into front center. 180 // Mix back LR into front center.
137 Mix(BACK_LEFT, CENTER, kEqualPowerScale); 181 Mix(BACK_LEFT, CENTER, kEqualPowerScale);
138 Mix(BACK_RIGHT, CENTER, kEqualPowerScale); 182 Mix(BACK_RIGHT, CENTER, kEqualPowerScale);
139 } 183 }
140 } 184 }
141 185
142 // Mix side LR into: back LR || back center || front LR || front center. 186 // Mix side LR into: back LR || back center || front LR || front center.
143 if (IsUnaccounted(SIDE_LEFT)) { 187 if (IsUnaccounted(SIDE_LEFT)) {
144 if (HasOutputChannel(BACK_LEFT)) { 188 if (HasOutputChannel(BACK_LEFT)) {
145 // If we have back LR, mix side LR into back LR, but instead if the input 189 // If we have back LR, mix side LR into back LR, but instead if the input
146 // doesn't have back LR (but output does) copy side LR to back LR. 190 // doesn't have back LR (but output does) copy side LR to back LR.
147 float scale = HasInputChannel(BACK_LEFT) ? kEqualPowerScale : 1; 191 float scale = HasInputChannel(BACK_LEFT) ? kEqualPowerScale : 1;
148 Mix(SIDE_LEFT, BACK_LEFT, scale); 192 Mix(SIDE_LEFT, BACK_LEFT, scale);
149 Mix(SIDE_RIGHT, BACK_RIGHT, scale); 193 Mix(SIDE_RIGHT, BACK_RIGHT, scale);
150 } else if (HasOutputChannel(BACK_CENTER)) { 194 } else if (HasOutputChannel(BACK_CENTER)) {
151 // Mix side LR into back center. 195 // Mix side LR into back center.
152 Mix(SIDE_LEFT, BACK_CENTER, kEqualPowerScale); 196 Mix(SIDE_LEFT, BACK_CENTER, kEqualPowerScale);
153 Mix(SIDE_RIGHT, BACK_CENTER, kEqualPowerScale); 197 Mix(SIDE_RIGHT, BACK_CENTER, kEqualPowerScale);
154 } else if (output > CHANNEL_LAYOUT_MONO) { 198 } else if (output_layout_ > CHANNEL_LAYOUT_MONO) {
155 // Mix side LR into front LR. 199 // Mix side LR into front LR.
156 Mix(SIDE_LEFT, LEFT, kEqualPowerScale); 200 Mix(SIDE_LEFT, LEFT, kEqualPowerScale);
157 Mix(SIDE_RIGHT, RIGHT, kEqualPowerScale); 201 Mix(SIDE_RIGHT, RIGHT, kEqualPowerScale);
158 } else { 202 } else {
159 // Mix side LR into front center. 203 // Mix side LR into front center.
160 Mix(SIDE_LEFT, CENTER, kEqualPowerScale); 204 Mix(SIDE_LEFT, CENTER, kEqualPowerScale);
161 Mix(SIDE_RIGHT, CENTER, kEqualPowerScale); 205 Mix(SIDE_RIGHT, CENTER, kEqualPowerScale);
162 } 206 }
163 } 207 }
164 208
165 // Mix back center into: back LR || side LR || front LR || front center. 209 // Mix back center into: back LR || side LR || front LR || front center.
166 if (IsUnaccounted(BACK_CENTER)) { 210 if (IsUnaccounted(BACK_CENTER)) {
167 if (HasOutputChannel(BACK_LEFT)) { 211 if (HasOutputChannel(BACK_LEFT)) {
168 // Mix back center into back LR. 212 // Mix back center into back LR.
169 MixWithoutAccounting(BACK_CENTER, BACK_LEFT, kEqualPowerScale); 213 MixWithoutAccounting(BACK_CENTER, BACK_LEFT, kEqualPowerScale);
170 Mix(BACK_CENTER, BACK_RIGHT, kEqualPowerScale); 214 Mix(BACK_CENTER, BACK_RIGHT, kEqualPowerScale);
171 } else if (HasOutputChannel(SIDE_LEFT)) { 215 } else if (HasOutputChannel(SIDE_LEFT)) {
172 // Mix back center into side LR. 216 // Mix back center into side LR.
173 MixWithoutAccounting(BACK_CENTER, SIDE_LEFT, kEqualPowerScale); 217 MixWithoutAccounting(BACK_CENTER, SIDE_LEFT, kEqualPowerScale);
174 Mix(BACK_CENTER, SIDE_RIGHT, kEqualPowerScale); 218 Mix(BACK_CENTER, SIDE_RIGHT, kEqualPowerScale);
175 } else if (output > CHANNEL_LAYOUT_MONO) { 219 } else if (output_layout_ > CHANNEL_LAYOUT_MONO) {
176 // Mix back center into front LR. 220 // Mix back center into front LR.
177 // TODO(dalecurtis): Not sure about these values? 221 // TODO(dalecurtis): Not sure about these values?
178 MixWithoutAccounting(BACK_CENTER, LEFT, kEqualPowerScale); 222 MixWithoutAccounting(BACK_CENTER, LEFT, kEqualPowerScale);
179 Mix(BACK_CENTER, RIGHT, kEqualPowerScale); 223 Mix(BACK_CENTER, RIGHT, kEqualPowerScale);
180 } else { 224 } else {
181 // Mix back center into front center. 225 // Mix back center into front center.
182 // TODO(dalecurtis): Not sure about these values? 226 // TODO(dalecurtis): Not sure about these values?
183 Mix(BACK_CENTER, CENTER, kEqualPowerScale); 227 Mix(BACK_CENTER, CENTER, kEqualPowerScale);
184 } 228 }
185 } 229 }
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 342
299 DCHECK(IsUnaccounted(input_ch)); 343 DCHECK(IsUnaccounted(input_ch));
300 DCHECK_GE(input_ch_index, 0); 344 DCHECK_GE(input_ch_index, 0);
301 DCHECK_GE(output_ch_index, 0); 345 DCHECK_GE(output_ch_index, 0);
302 346
303 DCHECK_EQ(matrix_[output_ch_index][input_ch_index], 0); 347 DCHECK_EQ(matrix_[output_ch_index][input_ch_index], 0);
304 matrix_[output_ch_index][input_ch_index] = scale; 348 matrix_[output_ch_index][input_ch_index] = scale;
305 } 349 }
306 350
307 } // namespace media 351 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698