channel_mixing_matrix.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. #include "media/base/channel_mixing_matrix.h"
  5. #include <stddef.h>
  6. #include <algorithm>
  7. #include "base/check_op.h"
  8. #include "media/base/channel_mixer.h"
  9. namespace media {
  10. static void ValidateLayout(ChannelLayout layout) {
  11. CHECK_NE(layout, CHANNEL_LAYOUT_NONE);
  12. CHECK_LE(layout, CHANNEL_LAYOUT_MAX);
  13. CHECK_NE(layout, CHANNEL_LAYOUT_UNSUPPORTED);
  14. CHECK_NE(layout, CHANNEL_LAYOUT_DISCRETE);
  15. CHECK_NE(layout, CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC);
  16. // Verify there's at least one channel. Should always be true here by virtue
  17. // of not being one of the invalid layouts, but lets double check to be sure.
  18. int channel_count = ChannelLayoutToChannelCount(layout);
  19. DCHECK_GT(channel_count, 0);
  20. // If we have more than one channel, verify a symmetric layout for sanity.
  21. // The unit test will verify all possible layouts, so this can be a DCHECK.
  22. // Symmetry allows simplifying the matrix building code by allowing us to
  23. // assume that if one channel of a pair exists, the other will too.
  24. if (channel_count > 1) {
  25. // Assert that LEFT exists if and only if RIGHT exists, and so on.
  26. DCHECK_EQ(ChannelOrder(layout, LEFT) >= 0,
  27. ChannelOrder(layout, RIGHT) >= 0);
  28. DCHECK_EQ(ChannelOrder(layout, SIDE_LEFT) >= 0,
  29. ChannelOrder(layout, SIDE_RIGHT) >= 0);
  30. DCHECK_EQ(ChannelOrder(layout, BACK_LEFT) >= 0,
  31. ChannelOrder(layout, BACK_RIGHT) >= 0);
  32. DCHECK_EQ(ChannelOrder(layout, LEFT_OF_CENTER) >= 0,
  33. ChannelOrder(layout, RIGHT_OF_CENTER) >= 0);
  34. } else {
  35. DCHECK_EQ(layout, CHANNEL_LAYOUT_MONO);
  36. }
  37. }
  38. ChannelMixingMatrix::ChannelMixingMatrix(ChannelLayout input_layout,
  39. int input_channels,
  40. ChannelLayout output_layout,
  41. int output_channels)
  42. : input_layout_(input_layout),
  43. input_channels_(input_channels),
  44. output_layout_(output_layout),
  45. output_channels_(output_channels) {
  46. // Stereo down mix should never be the output layout.
  47. CHECK_NE(output_layout, CHANNEL_LAYOUT_STEREO_DOWNMIX);
  48. // Verify that the layouts are supported
  49. if (input_layout != CHANNEL_LAYOUT_DISCRETE)
  50. ValidateLayout(input_layout);
  51. if (output_layout != CHANNEL_LAYOUT_DISCRETE)
  52. ValidateLayout(output_layout);
  53. // Special case for 5.0, 5.1 with back channels when upmixed to 7.0, 7.1,
  54. // which should map the back LR to side LR.
  55. if (input_layout_ == CHANNEL_LAYOUT_5_0_BACK &&
  56. output_layout_ == CHANNEL_LAYOUT_7_0) {
  57. input_layout_ = CHANNEL_LAYOUT_5_0;
  58. } else if (input_layout_ == CHANNEL_LAYOUT_5_1_BACK &&
  59. output_layout_ == CHANNEL_LAYOUT_7_1) {
  60. input_layout_ = CHANNEL_LAYOUT_5_1;
  61. }
  62. }
  63. ChannelMixingMatrix::~ChannelMixingMatrix() = default;
  64. bool ChannelMixingMatrix::CreateTransformationMatrix(
  65. std::vector<std::vector<float>>* matrix) {
  66. matrix_ = matrix;
  67. // Size out the initial matrix.
  68. matrix_->reserve(output_channels_);
  69. for (int output_ch = 0; output_ch < output_channels_; ++output_ch)
  70. matrix_->push_back(std::vector<float>(input_channels_, 0));
  71. // First check for discrete case.
  72. if (input_layout_ == CHANNEL_LAYOUT_DISCRETE ||
  73. output_layout_ == CHANNEL_LAYOUT_DISCRETE) {
  74. // If the number of input channels is more than output channels, then
  75. // copy as many as we can then drop the remaining input channels.
  76. // If the number of input channels is less than output channels, then
  77. // copy them all, then zero out the remaining output channels.
  78. int passthrough_channels = std::min(input_channels_, output_channels_);
  79. for (int i = 0; i < passthrough_channels; ++i)
  80. (*matrix_)[i][i] = 1;
  81. return true;
  82. }
  83. // Route matching channels and figure out which ones aren't accounted for.
  84. for (Channels ch = LEFT; ch < CHANNELS_MAX + 1;
  85. ch = static_cast<Channels>(ch + 1)) {
  86. int input_ch_index = ChannelOrder(input_layout_, ch);
  87. if (input_ch_index < 0)
  88. continue;
  89. int output_ch_index = ChannelOrder(output_layout_, ch);
  90. if (output_ch_index < 0 ||
  91. (ch == CENTER && input_layout_ == CHANNEL_LAYOUT_MONO &&
  92. input_layout_ != output_layout_)) {
  93. unaccounted_inputs_.push_back(ch);
  94. continue;
  95. }
  96. DCHECK_LT(static_cast<size_t>(output_ch_index), matrix_->size());
  97. DCHECK_LT(static_cast<size_t>(input_ch_index),
  98. (*matrix_)[output_ch_index].size());
  99. (*matrix_)[output_ch_index][input_ch_index] = 1;
  100. }
  101. // If all input channels are accounted for, there's nothing left to do.
  102. if (unaccounted_inputs_.empty()) {
  103. // Since all output channels map directly to inputs we can optimize.
  104. return true;
  105. }
  106. // Mix front LR into center.
  107. if (IsUnaccounted(LEFT)) {
  108. // When down mixing to mono from stereo, we need to be careful of full scale
  109. // stereo mixes. Scaling by 1 / sqrt(2) here will likely lead to clipping
  110. // so we use 1 / 2 instead.
  111. float scale =
  112. (output_layout_ == CHANNEL_LAYOUT_MONO && input_channels_ == 2)
  113. ? 0.5
  114. : ChannelMixer::kHalfPower;
  115. Mix(LEFT, CENTER, scale);
  116. Mix(RIGHT, CENTER, scale);
  117. }
  118. // Mix center into front LR.
  119. if (IsUnaccounted(CENTER)) {
  120. // When up mixing from mono, just do a copy to front LR.
  121. float scale =
  122. (input_layout_ == CHANNEL_LAYOUT_MONO) ? 1 : ChannelMixer::kHalfPower;
  123. MixWithoutAccounting(CENTER, LEFT, scale);
  124. Mix(CENTER, RIGHT, scale);
  125. }
  126. // Mix back LR into: side LR || back center || front LR || front center.
  127. if (IsUnaccounted(BACK_LEFT)) {
  128. if (HasOutputChannel(SIDE_LEFT)) {
  129. // If the input has side LR, mix back LR into side LR, but instead if the
  130. // input doesn't have side LR (but output does) copy back LR to side LR.
  131. float scale = HasInputChannel(SIDE_LEFT) ? ChannelMixer::kHalfPower : 1;
  132. Mix(BACK_LEFT, SIDE_LEFT, scale);
  133. Mix(BACK_RIGHT, SIDE_RIGHT, scale);
  134. } else if (HasOutputChannel(BACK_CENTER)) {
  135. // Mix back LR into back center.
  136. Mix(BACK_LEFT, BACK_CENTER, ChannelMixer::kHalfPower);
  137. Mix(BACK_RIGHT, BACK_CENTER, ChannelMixer::kHalfPower);
  138. } else if (output_layout_ > CHANNEL_LAYOUT_MONO) {
  139. // Mix back LR into front LR.
  140. Mix(BACK_LEFT, LEFT, ChannelMixer::kHalfPower);
  141. Mix(BACK_RIGHT, RIGHT, ChannelMixer::kHalfPower);
  142. } else {
  143. // Mix back LR into front center.
  144. Mix(BACK_LEFT, CENTER, ChannelMixer::kHalfPower);
  145. Mix(BACK_RIGHT, CENTER, ChannelMixer::kHalfPower);
  146. }
  147. }
  148. // Mix side LR into: back LR || back center || front LR || front center.
  149. if (IsUnaccounted(SIDE_LEFT)) {
  150. if (HasOutputChannel(BACK_LEFT)) {
  151. // If the input has back LR, mix side LR into back LR, but instead if the
  152. // input doesn't have back LR (but output does) copy side LR to back LR.
  153. float scale = HasInputChannel(BACK_LEFT) ? ChannelMixer::kHalfPower : 1;
  154. Mix(SIDE_LEFT, BACK_LEFT, scale);
  155. Mix(SIDE_RIGHT, BACK_RIGHT, scale);
  156. } else if (HasOutputChannel(BACK_CENTER)) {
  157. // Mix side LR into back center.
  158. Mix(SIDE_LEFT, BACK_CENTER, ChannelMixer::kHalfPower);
  159. Mix(SIDE_RIGHT, BACK_CENTER, ChannelMixer::kHalfPower);
  160. } else if (output_layout_ > CHANNEL_LAYOUT_MONO) {
  161. // Mix side LR into front LR.
  162. Mix(SIDE_LEFT, LEFT, ChannelMixer::kHalfPower);
  163. Mix(SIDE_RIGHT, RIGHT, ChannelMixer::kHalfPower);
  164. } else {
  165. // Mix side LR into front center.
  166. Mix(SIDE_LEFT, CENTER, ChannelMixer::kHalfPower);
  167. Mix(SIDE_RIGHT, CENTER, ChannelMixer::kHalfPower);
  168. }
  169. }
  170. // Mix back center into: back LR || side LR || front LR || front center.
  171. if (IsUnaccounted(BACK_CENTER)) {
  172. if (HasOutputChannel(BACK_LEFT)) {
  173. // Mix back center into back LR.
  174. MixWithoutAccounting(BACK_CENTER, BACK_LEFT, ChannelMixer::kHalfPower);
  175. Mix(BACK_CENTER, BACK_RIGHT, ChannelMixer::kHalfPower);
  176. } else if (HasOutputChannel(SIDE_LEFT)) {
  177. // Mix back center into side LR.
  178. MixWithoutAccounting(BACK_CENTER, SIDE_LEFT, ChannelMixer::kHalfPower);
  179. Mix(BACK_CENTER, SIDE_RIGHT, ChannelMixer::kHalfPower);
  180. } else if (output_layout_ > CHANNEL_LAYOUT_MONO) {
  181. // Mix back center into front LR.
  182. // TODO(dalecurtis): Not sure about these values?
  183. MixWithoutAccounting(BACK_CENTER, LEFT, ChannelMixer::kHalfPower);
  184. Mix(BACK_CENTER, RIGHT, ChannelMixer::kHalfPower);
  185. } else {
  186. // Mix back center into front center.
  187. // TODO(dalecurtis): Not sure about these values?
  188. Mix(BACK_CENTER, CENTER, ChannelMixer::kHalfPower);
  189. }
  190. }
  191. // Mix LR of center into: front LR || front center.
  192. if (IsUnaccounted(LEFT_OF_CENTER)) {
  193. if (HasOutputChannel(LEFT)) {
  194. // Mix LR of center into front LR.
  195. Mix(LEFT_OF_CENTER, LEFT, ChannelMixer::kHalfPower);
  196. Mix(RIGHT_OF_CENTER, RIGHT, ChannelMixer::kHalfPower);
  197. } else {
  198. // Mix LR of center into front center.
  199. Mix(LEFT_OF_CENTER, CENTER, ChannelMixer::kHalfPower);
  200. Mix(RIGHT_OF_CENTER, CENTER, ChannelMixer::kHalfPower);
  201. }
  202. }
  203. // Mix LFE into: front center || front LR.
  204. if (IsUnaccounted(LFE)) {
  205. if (!HasOutputChannel(CENTER)) {
  206. // Mix LFE into front LR.
  207. MixWithoutAccounting(LFE, LEFT, ChannelMixer::kHalfPower);
  208. Mix(LFE, RIGHT, ChannelMixer::kHalfPower);
  209. } else {
  210. // Mix LFE into front center.
  211. Mix(LFE, CENTER, ChannelMixer::kHalfPower);
  212. }
  213. }
  214. // All channels should now be accounted for.
  215. DCHECK(unaccounted_inputs_.empty());
  216. // See if the output |matrix_| is simply a remapping matrix. If each input
  217. // channel maps to a single output channel we can simply remap. Doing this
  218. // programmatically is less fragile than logic checks on channel mappings.
  219. for (int output_ch = 0; output_ch < output_channels_; ++output_ch) {
  220. int input_mappings = 0;
  221. for (int input_ch = 0; input_ch < input_channels_; ++input_ch) {
  222. // We can only remap if each row contains a single scale of 1. I.e., each
  223. // output channel is mapped from a single unscaled input channel.
  224. if ((*matrix_)[output_ch][input_ch] != 1 || ++input_mappings > 1)
  225. return false;
  226. }
  227. }
  228. // If we've gotten here, |matrix_| is simply a remapping.
  229. return true;
  230. }
  231. void ChannelMixingMatrix::AccountFor(Channels ch) {
  232. unaccounted_inputs_.erase(std::find(
  233. unaccounted_inputs_.begin(), unaccounted_inputs_.end(), ch));
  234. }
  235. bool ChannelMixingMatrix::IsUnaccounted(Channels ch) const {
  236. return std::find(unaccounted_inputs_.begin(), unaccounted_inputs_.end(),
  237. ch) != unaccounted_inputs_.end();
  238. }
  239. bool ChannelMixingMatrix::HasInputChannel(Channels ch) const {
  240. return ChannelOrder(input_layout_, ch) >= 0;
  241. }
  242. bool ChannelMixingMatrix::HasOutputChannel(Channels ch) const {
  243. return ChannelOrder(output_layout_, ch) >= 0;
  244. }
  245. void ChannelMixingMatrix::Mix(Channels input_ch,
  246. Channels output_ch,
  247. float scale) {
  248. MixWithoutAccounting(input_ch, output_ch, scale);
  249. AccountFor(input_ch);
  250. }
  251. void ChannelMixingMatrix::MixWithoutAccounting(Channels input_ch,
  252. Channels output_ch,
  253. float scale) {
  254. int input_ch_index = ChannelOrder(input_layout_, input_ch);
  255. int output_ch_index = ChannelOrder(output_layout_, output_ch);
  256. DCHECK(IsUnaccounted(input_ch));
  257. DCHECK_GE(input_ch_index, 0);
  258. DCHECK_GE(output_ch_index, 0);
  259. DCHECK_EQ((*matrix_)[output_ch_index][input_ch_index], 0);
  260. (*matrix_)[output_ch_index][input_ch_index] = scale;
  261. }
  262. } // namespace media