audio_mix_matrix.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright (C) 2011 Michael Niedermayer (michaelni@gmx.at)
  3. * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <stdint.h>
  22. #include "libavutil/common.h"
  23. #include "libavutil/libm.h"
  24. #include "libavutil/samplefmt.h"
  25. #include "avresample.h"
  26. #include "internal.h"
  27. #include "audio_data.h"
  28. #include "audio_mix.h"
  29. /* channel positions */
  30. #define FRONT_LEFT 0
  31. #define FRONT_RIGHT 1
  32. #define FRONT_CENTER 2
  33. #define LOW_FREQUENCY 3
  34. #define BACK_LEFT 4
  35. #define BACK_RIGHT 5
  36. #define FRONT_LEFT_OF_CENTER 6
  37. #define FRONT_RIGHT_OF_CENTER 7
  38. #define BACK_CENTER 8
  39. #define SIDE_LEFT 9
  40. #define SIDE_RIGHT 10
  41. #define TOP_CENTER 11
  42. #define TOP_FRONT_LEFT 12
  43. #define TOP_FRONT_CENTER 13
  44. #define TOP_FRONT_RIGHT 14
  45. #define TOP_BACK_LEFT 15
  46. #define TOP_BACK_CENTER 16
  47. #define TOP_BACK_RIGHT 17
  48. #define STEREO_LEFT 29
  49. #define STEREO_RIGHT 30
  50. #define WIDE_LEFT 31
  51. #define WIDE_RIGHT 32
  52. #define SURROUND_DIRECT_LEFT 33
  53. #define SURROUND_DIRECT_RIGHT 34
  54. #define LOW_FREQUENCY_2 35
  55. #define SQRT3_2 1.22474487139158904909 /* sqrt(3/2) */
  56. static av_always_inline int even(uint64_t layout)
  57. {
  58. return (!layout || (layout & (layout - 1)));
  59. }
  60. static int sane_layout(uint64_t layout)
  61. {
  62. /* check that there is at least 1 front speaker */
  63. if (!(layout & AV_CH_LAYOUT_SURROUND))
  64. return 0;
  65. /* check for left/right symmetry */
  66. if (!even(layout & (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT)) ||
  67. !even(layout & (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)) ||
  68. !even(layout & (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)) ||
  69. !even(layout & (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)) ||
  70. !even(layout & (AV_CH_TOP_FRONT_LEFT | AV_CH_TOP_FRONT_RIGHT)) ||
  71. !even(layout & (AV_CH_TOP_BACK_LEFT | AV_CH_TOP_BACK_RIGHT)) ||
  72. !even(layout & (AV_CH_STEREO_LEFT | AV_CH_STEREO_RIGHT)) ||
  73. !even(layout & (AV_CH_WIDE_LEFT | AV_CH_WIDE_RIGHT)) ||
  74. !even(layout & (AV_CH_SURROUND_DIRECT_LEFT | AV_CH_SURROUND_DIRECT_RIGHT)))
  75. return 0;
  76. return 1;
  77. }
  78. int avresample_build_matrix(uint64_t in_layout, uint64_t out_layout,
  79. double center_mix_level, double surround_mix_level,
  80. double lfe_mix_level, int normalize,
  81. double *matrix_out, int stride,
  82. enum AVMatrixEncoding matrix_encoding)
  83. {
  84. int i, j, out_i, out_j;
  85. double matrix[64][64] = {{0}};
  86. int64_t unaccounted;
  87. double maxcoef = 0;
  88. int in_channels, out_channels;
  89. if ((out_layout & AV_CH_LAYOUT_STEREO_DOWNMIX) == AV_CH_LAYOUT_STEREO_DOWNMIX) {
  90. out_layout = AV_CH_LAYOUT_STEREO;
  91. }
  92. unaccounted = in_layout & ~out_layout;
  93. in_channels = av_get_channel_layout_nb_channels( in_layout);
  94. out_channels = av_get_channel_layout_nb_channels(out_layout);
  95. memset(matrix_out, 0, out_channels * stride * sizeof(*matrix_out));
  96. /* check if layouts are supported */
  97. if (!in_layout || in_channels > AVRESAMPLE_MAX_CHANNELS)
  98. return AVERROR(EINVAL);
  99. if (!out_layout || out_channels > AVRESAMPLE_MAX_CHANNELS)
  100. return AVERROR(EINVAL);
  101. /* check if layouts are unbalanced or abnormal */
  102. if (!sane_layout(in_layout) || !sane_layout(out_layout))
  103. return AVERROR_PATCHWELCOME;
  104. /* route matching input/output channels */
  105. for (i = 0; i < 64; i++) {
  106. if (in_layout & out_layout & (1ULL << i))
  107. matrix[i][i] = 1.0;
  108. }
  109. /* mix front center to front left/right */
  110. if (unaccounted & AV_CH_FRONT_CENTER) {
  111. if ((out_layout & AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO) {
  112. matrix[FRONT_LEFT ][FRONT_CENTER] += M_SQRT1_2;
  113. matrix[FRONT_RIGHT][FRONT_CENTER] += M_SQRT1_2;
  114. } else
  115. return AVERROR_PATCHWELCOME;
  116. }
  117. /* mix front left/right to center */
  118. if (unaccounted & AV_CH_LAYOUT_STEREO) {
  119. if (out_layout & AV_CH_FRONT_CENTER) {
  120. matrix[FRONT_CENTER][FRONT_LEFT ] += M_SQRT1_2;
  121. matrix[FRONT_CENTER][FRONT_RIGHT] += M_SQRT1_2;
  122. /* mix left/right/center to center */
  123. if (in_layout & AV_CH_FRONT_CENTER)
  124. matrix[FRONT_CENTER][FRONT_CENTER] = center_mix_level * M_SQRT2;
  125. } else
  126. return AVERROR_PATCHWELCOME;
  127. }
  128. /* mix back center to back, side, or front */
  129. if (unaccounted & AV_CH_BACK_CENTER) {
  130. if (out_layout & AV_CH_BACK_LEFT) {
  131. matrix[BACK_LEFT ][BACK_CENTER] += M_SQRT1_2;
  132. matrix[BACK_RIGHT][BACK_CENTER] += M_SQRT1_2;
  133. } else if (out_layout & AV_CH_SIDE_LEFT) {
  134. matrix[SIDE_LEFT ][BACK_CENTER] += M_SQRT1_2;
  135. matrix[SIDE_RIGHT][BACK_CENTER] += M_SQRT1_2;
  136. } else if (out_layout & AV_CH_FRONT_LEFT) {
  137. if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY ||
  138. matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
  139. if (unaccounted & (AV_CH_BACK_LEFT | AV_CH_SIDE_LEFT)) {
  140. matrix[FRONT_LEFT ][BACK_CENTER] -= surround_mix_level * M_SQRT1_2;
  141. matrix[FRONT_RIGHT][BACK_CENTER] += surround_mix_level * M_SQRT1_2;
  142. } else {
  143. matrix[FRONT_LEFT ][BACK_CENTER] -= surround_mix_level;
  144. matrix[FRONT_RIGHT][BACK_CENTER] += surround_mix_level;
  145. }
  146. } else {
  147. matrix[FRONT_LEFT ][BACK_CENTER] += surround_mix_level * M_SQRT1_2;
  148. matrix[FRONT_RIGHT][BACK_CENTER] += surround_mix_level * M_SQRT1_2;
  149. }
  150. } else if (out_layout & AV_CH_FRONT_CENTER) {
  151. matrix[FRONT_CENTER][BACK_CENTER] += surround_mix_level * M_SQRT1_2;
  152. } else
  153. return AVERROR_PATCHWELCOME;
  154. }
  155. /* mix back left/right to back center, side, or front */
  156. if (unaccounted & AV_CH_BACK_LEFT) {
  157. if (out_layout & AV_CH_BACK_CENTER) {
  158. matrix[BACK_CENTER][BACK_LEFT ] += M_SQRT1_2;
  159. matrix[BACK_CENTER][BACK_RIGHT] += M_SQRT1_2;
  160. } else if (out_layout & AV_CH_SIDE_LEFT) {
  161. /* if side channels do not exist in the input, just copy back
  162. channels to side channels, otherwise mix back into side */
  163. if (in_layout & AV_CH_SIDE_LEFT) {
  164. matrix[SIDE_LEFT ][BACK_LEFT ] += M_SQRT1_2;
  165. matrix[SIDE_RIGHT][BACK_RIGHT] += M_SQRT1_2;
  166. } else {
  167. matrix[SIDE_LEFT ][BACK_LEFT ] += 1.0;
  168. matrix[SIDE_RIGHT][BACK_RIGHT] += 1.0;
  169. }
  170. } else if (out_layout & AV_CH_FRONT_LEFT) {
  171. if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
  172. matrix[FRONT_LEFT ][BACK_LEFT ] -= surround_mix_level * M_SQRT1_2;
  173. matrix[FRONT_LEFT ][BACK_RIGHT] -= surround_mix_level * M_SQRT1_2;
  174. matrix[FRONT_RIGHT][BACK_LEFT ] += surround_mix_level * M_SQRT1_2;
  175. matrix[FRONT_RIGHT][BACK_RIGHT] += surround_mix_level * M_SQRT1_2;
  176. } else if (matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
  177. matrix[FRONT_LEFT ][BACK_LEFT ] -= surround_mix_level * SQRT3_2;
  178. matrix[FRONT_LEFT ][BACK_RIGHT] -= surround_mix_level * M_SQRT1_2;
  179. matrix[FRONT_RIGHT][BACK_LEFT ] += surround_mix_level * M_SQRT1_2;
  180. matrix[FRONT_RIGHT][BACK_RIGHT] += surround_mix_level * SQRT3_2;
  181. } else {
  182. matrix[FRONT_LEFT ][BACK_LEFT ] += surround_mix_level;
  183. matrix[FRONT_RIGHT][BACK_RIGHT] += surround_mix_level;
  184. }
  185. } else if (out_layout & AV_CH_FRONT_CENTER) {
  186. matrix[FRONT_CENTER][BACK_LEFT ] += surround_mix_level * M_SQRT1_2;
  187. matrix[FRONT_CENTER][BACK_RIGHT] += surround_mix_level * M_SQRT1_2;
  188. } else
  189. return AVERROR_PATCHWELCOME;
  190. }
  191. /* mix side left/right into back or front */
  192. if (unaccounted & AV_CH_SIDE_LEFT) {
  193. if (out_layout & AV_CH_BACK_LEFT) {
  194. /* if back channels do not exist in the input, just copy side
  195. channels to back channels, otherwise mix side into back */
  196. if (in_layout & AV_CH_BACK_LEFT) {
  197. matrix[BACK_LEFT ][SIDE_LEFT ] += M_SQRT1_2;
  198. matrix[BACK_RIGHT][SIDE_RIGHT] += M_SQRT1_2;
  199. } else {
  200. matrix[BACK_LEFT ][SIDE_LEFT ] += 1.0;
  201. matrix[BACK_RIGHT][SIDE_RIGHT] += 1.0;
  202. }
  203. } else if (out_layout & AV_CH_BACK_CENTER) {
  204. matrix[BACK_CENTER][SIDE_LEFT ] += M_SQRT1_2;
  205. matrix[BACK_CENTER][SIDE_RIGHT] += M_SQRT1_2;
  206. } else if (out_layout & AV_CH_FRONT_LEFT) {
  207. if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
  208. matrix[FRONT_LEFT ][SIDE_LEFT ] -= surround_mix_level * M_SQRT1_2;
  209. matrix[FRONT_LEFT ][SIDE_RIGHT] -= surround_mix_level * M_SQRT1_2;
  210. matrix[FRONT_RIGHT][SIDE_LEFT ] += surround_mix_level * M_SQRT1_2;
  211. matrix[FRONT_RIGHT][SIDE_RIGHT] += surround_mix_level * M_SQRT1_2;
  212. } else if (matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
  213. matrix[FRONT_LEFT ][SIDE_LEFT ] -= surround_mix_level * SQRT3_2;
  214. matrix[FRONT_LEFT ][SIDE_RIGHT] -= surround_mix_level * M_SQRT1_2;
  215. matrix[FRONT_RIGHT][SIDE_LEFT ] += surround_mix_level * M_SQRT1_2;
  216. matrix[FRONT_RIGHT][SIDE_RIGHT] += surround_mix_level * SQRT3_2;
  217. } else {
  218. matrix[FRONT_LEFT ][SIDE_LEFT ] += surround_mix_level;
  219. matrix[FRONT_RIGHT][SIDE_RIGHT] += surround_mix_level;
  220. }
  221. } else if (out_layout & AV_CH_FRONT_CENTER) {
  222. matrix[FRONT_CENTER][SIDE_LEFT ] += surround_mix_level * M_SQRT1_2;
  223. matrix[FRONT_CENTER][SIDE_RIGHT] += surround_mix_level * M_SQRT1_2;
  224. } else
  225. return AVERROR_PATCHWELCOME;
  226. }
  227. /* mix left-of-center/right-of-center into front left/right or center */
  228. if (unaccounted & AV_CH_FRONT_LEFT_OF_CENTER) {
  229. if (out_layout & AV_CH_FRONT_LEFT) {
  230. matrix[FRONT_LEFT ][FRONT_LEFT_OF_CENTER ] += 1.0;
  231. matrix[FRONT_RIGHT][FRONT_RIGHT_OF_CENTER] += 1.0;
  232. } else if (out_layout & AV_CH_FRONT_CENTER) {
  233. matrix[FRONT_CENTER][FRONT_LEFT_OF_CENTER ] += M_SQRT1_2;
  234. matrix[FRONT_CENTER][FRONT_RIGHT_OF_CENTER] += M_SQRT1_2;
  235. } else
  236. return AVERROR_PATCHWELCOME;
  237. }
  238. /* mix LFE into front left/right or center */
  239. if (unaccounted & AV_CH_LOW_FREQUENCY) {
  240. if (out_layout & AV_CH_FRONT_CENTER) {
  241. matrix[FRONT_CENTER][LOW_FREQUENCY] += lfe_mix_level;
  242. } else if (out_layout & AV_CH_FRONT_LEFT) {
  243. matrix[FRONT_LEFT ][LOW_FREQUENCY] += lfe_mix_level * M_SQRT1_2;
  244. matrix[FRONT_RIGHT][LOW_FREQUENCY] += lfe_mix_level * M_SQRT1_2;
  245. } else
  246. return AVERROR_PATCHWELCOME;
  247. }
  248. /* transfer internal matrix to output matrix and calculate maximum
  249. per-channel coefficient sum */
  250. for (out_i = i = 0; out_i < out_channels && i < 64; i++) {
  251. double sum = 0;
  252. for (out_j = j = 0; out_j < in_channels && j < 64; j++) {
  253. matrix_out[out_i * stride + out_j] = matrix[i][j];
  254. sum += fabs(matrix[i][j]);
  255. if (in_layout & (1ULL << j))
  256. out_j++;
  257. }
  258. maxcoef = FFMAX(maxcoef, sum);
  259. if (out_layout & (1ULL << i))
  260. out_i++;
  261. }
  262. /* normalize */
  263. if (normalize && maxcoef > 1.0) {
  264. for (i = 0; i < out_channels; i++)
  265. for (j = 0; j < in_channels; j++)
  266. matrix_out[i * stride + j] /= maxcoef;
  267. }
  268. return 0;
  269. }