audio_mix_matrix.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. if ((in_layout & AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO) {
  113. matrix[FRONT_LEFT ][FRONT_CENTER] += center_mix_level;
  114. matrix[FRONT_RIGHT][FRONT_CENTER] += center_mix_level;
  115. } else {
  116. matrix[FRONT_LEFT ][FRONT_CENTER] += M_SQRT1_2;
  117. matrix[FRONT_RIGHT][FRONT_CENTER] += M_SQRT1_2;
  118. }
  119. } else
  120. return AVERROR_PATCHWELCOME;
  121. }
  122. /* mix front left/right to center */
  123. if (unaccounted & AV_CH_LAYOUT_STEREO) {
  124. if (out_layout & AV_CH_FRONT_CENTER) {
  125. matrix[FRONT_CENTER][FRONT_LEFT ] += M_SQRT1_2;
  126. matrix[FRONT_CENTER][FRONT_RIGHT] += M_SQRT1_2;
  127. /* mix left/right/center to center */
  128. if (in_layout & AV_CH_FRONT_CENTER)
  129. matrix[FRONT_CENTER][FRONT_CENTER] = center_mix_level * M_SQRT2;
  130. } else
  131. return AVERROR_PATCHWELCOME;
  132. }
  133. /* mix back center to back, side, or front */
  134. if (unaccounted & AV_CH_BACK_CENTER) {
  135. if (out_layout & AV_CH_BACK_LEFT) {
  136. matrix[BACK_LEFT ][BACK_CENTER] += M_SQRT1_2;
  137. matrix[BACK_RIGHT][BACK_CENTER] += M_SQRT1_2;
  138. } else if (out_layout & AV_CH_SIDE_LEFT) {
  139. matrix[SIDE_LEFT ][BACK_CENTER] += M_SQRT1_2;
  140. matrix[SIDE_RIGHT][BACK_CENTER] += M_SQRT1_2;
  141. } else if (out_layout & AV_CH_FRONT_LEFT) {
  142. if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY ||
  143. matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
  144. if (unaccounted & (AV_CH_BACK_LEFT | AV_CH_SIDE_LEFT)) {
  145. matrix[FRONT_LEFT ][BACK_CENTER] -= surround_mix_level * M_SQRT1_2;
  146. matrix[FRONT_RIGHT][BACK_CENTER] += surround_mix_level * M_SQRT1_2;
  147. } else {
  148. matrix[FRONT_LEFT ][BACK_CENTER] -= surround_mix_level;
  149. matrix[FRONT_RIGHT][BACK_CENTER] += surround_mix_level;
  150. }
  151. } else {
  152. matrix[FRONT_LEFT ][BACK_CENTER] += surround_mix_level * M_SQRT1_2;
  153. matrix[FRONT_RIGHT][BACK_CENTER] += surround_mix_level * M_SQRT1_2;
  154. }
  155. } else if (out_layout & AV_CH_FRONT_CENTER) {
  156. matrix[FRONT_CENTER][BACK_CENTER] += surround_mix_level * M_SQRT1_2;
  157. } else
  158. return AVERROR_PATCHWELCOME;
  159. }
  160. /* mix back left/right to back center, side, or front */
  161. if (unaccounted & AV_CH_BACK_LEFT) {
  162. if (out_layout & AV_CH_BACK_CENTER) {
  163. matrix[BACK_CENTER][BACK_LEFT ] += M_SQRT1_2;
  164. matrix[BACK_CENTER][BACK_RIGHT] += M_SQRT1_2;
  165. } else if (out_layout & AV_CH_SIDE_LEFT) {
  166. /* if side channels do not exist in the input, just copy back
  167. channels to side channels, otherwise mix back into side */
  168. if (in_layout & AV_CH_SIDE_LEFT) {
  169. matrix[SIDE_LEFT ][BACK_LEFT ] += M_SQRT1_2;
  170. matrix[SIDE_RIGHT][BACK_RIGHT] += M_SQRT1_2;
  171. } else {
  172. matrix[SIDE_LEFT ][BACK_LEFT ] += 1.0;
  173. matrix[SIDE_RIGHT][BACK_RIGHT] += 1.0;
  174. }
  175. } else if (out_layout & AV_CH_FRONT_LEFT) {
  176. if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
  177. matrix[FRONT_LEFT ][BACK_LEFT ] -= surround_mix_level * M_SQRT1_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 * M_SQRT1_2;
  181. } else if (matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
  182. matrix[FRONT_LEFT ][BACK_LEFT ] -= surround_mix_level * SQRT3_2;
  183. matrix[FRONT_LEFT ][BACK_RIGHT] -= surround_mix_level * M_SQRT1_2;
  184. matrix[FRONT_RIGHT][BACK_LEFT ] += surround_mix_level * M_SQRT1_2;
  185. matrix[FRONT_RIGHT][BACK_RIGHT] += surround_mix_level * SQRT3_2;
  186. } else {
  187. matrix[FRONT_LEFT ][BACK_LEFT ] += surround_mix_level;
  188. matrix[FRONT_RIGHT][BACK_RIGHT] += surround_mix_level;
  189. }
  190. } else if (out_layout & AV_CH_FRONT_CENTER) {
  191. matrix[FRONT_CENTER][BACK_LEFT ] += surround_mix_level * M_SQRT1_2;
  192. matrix[FRONT_CENTER][BACK_RIGHT] += surround_mix_level * M_SQRT1_2;
  193. } else
  194. return AVERROR_PATCHWELCOME;
  195. }
  196. /* mix side left/right into back or front */
  197. if (unaccounted & AV_CH_SIDE_LEFT) {
  198. if (out_layout & AV_CH_BACK_LEFT) {
  199. /* if back channels do not exist in the input, just copy side
  200. channels to back channels, otherwise mix side into back */
  201. if (in_layout & AV_CH_BACK_LEFT) {
  202. matrix[BACK_LEFT ][SIDE_LEFT ] += M_SQRT1_2;
  203. matrix[BACK_RIGHT][SIDE_RIGHT] += M_SQRT1_2;
  204. } else {
  205. matrix[BACK_LEFT ][SIDE_LEFT ] += 1.0;
  206. matrix[BACK_RIGHT][SIDE_RIGHT] += 1.0;
  207. }
  208. } else if (out_layout & AV_CH_BACK_CENTER) {
  209. matrix[BACK_CENTER][SIDE_LEFT ] += M_SQRT1_2;
  210. matrix[BACK_CENTER][SIDE_RIGHT] += M_SQRT1_2;
  211. } else if (out_layout & AV_CH_FRONT_LEFT) {
  212. if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
  213. matrix[FRONT_LEFT ][SIDE_LEFT ] -= surround_mix_level * M_SQRT1_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 * M_SQRT1_2;
  217. } else if (matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
  218. matrix[FRONT_LEFT ][SIDE_LEFT ] -= surround_mix_level * SQRT3_2;
  219. matrix[FRONT_LEFT ][SIDE_RIGHT] -= surround_mix_level * M_SQRT1_2;
  220. matrix[FRONT_RIGHT][SIDE_LEFT ] += surround_mix_level * M_SQRT1_2;
  221. matrix[FRONT_RIGHT][SIDE_RIGHT] += surround_mix_level * SQRT3_2;
  222. } else {
  223. matrix[FRONT_LEFT ][SIDE_LEFT ] += surround_mix_level;
  224. matrix[FRONT_RIGHT][SIDE_RIGHT] += surround_mix_level;
  225. }
  226. } else if (out_layout & AV_CH_FRONT_CENTER) {
  227. matrix[FRONT_CENTER][SIDE_LEFT ] += surround_mix_level * M_SQRT1_2;
  228. matrix[FRONT_CENTER][SIDE_RIGHT] += surround_mix_level * M_SQRT1_2;
  229. } else
  230. return AVERROR_PATCHWELCOME;
  231. }
  232. /* mix left-of-center/right-of-center into front left/right or center */
  233. if (unaccounted & AV_CH_FRONT_LEFT_OF_CENTER) {
  234. if (out_layout & AV_CH_FRONT_LEFT) {
  235. matrix[FRONT_LEFT ][FRONT_LEFT_OF_CENTER ] += 1.0;
  236. matrix[FRONT_RIGHT][FRONT_RIGHT_OF_CENTER] += 1.0;
  237. } else if (out_layout & AV_CH_FRONT_CENTER) {
  238. matrix[FRONT_CENTER][FRONT_LEFT_OF_CENTER ] += M_SQRT1_2;
  239. matrix[FRONT_CENTER][FRONT_RIGHT_OF_CENTER] += M_SQRT1_2;
  240. } else
  241. return AVERROR_PATCHWELCOME;
  242. }
  243. /* mix LFE into front left/right or center */
  244. if (unaccounted & AV_CH_LOW_FREQUENCY) {
  245. if (out_layout & AV_CH_FRONT_CENTER) {
  246. matrix[FRONT_CENTER][LOW_FREQUENCY] += lfe_mix_level;
  247. } else if (out_layout & AV_CH_FRONT_LEFT) {
  248. matrix[FRONT_LEFT ][LOW_FREQUENCY] += lfe_mix_level * M_SQRT1_2;
  249. matrix[FRONT_RIGHT][LOW_FREQUENCY] += lfe_mix_level * M_SQRT1_2;
  250. } else
  251. return AVERROR_PATCHWELCOME;
  252. }
  253. /* transfer internal matrix to output matrix and calculate maximum
  254. per-channel coefficient sum */
  255. for (out_i = i = 0; out_i < out_channels && i < 64; i++) {
  256. double sum = 0;
  257. for (out_j = j = 0; out_j < in_channels && j < 64; j++) {
  258. matrix_out[out_i * stride + out_j] = matrix[i][j];
  259. sum += fabs(matrix[i][j]);
  260. if (in_layout & (1ULL << j))
  261. out_j++;
  262. }
  263. maxcoef = FFMAX(maxcoef, sum);
  264. if (out_layout & (1ULL << i))
  265. out_i++;
  266. }
  267. /* normalize */
  268. if (normalize && maxcoef > 1.0) {
  269. for (i = 0; i < out_channels; i++)
  270. for (j = 0; j < in_channels; j++)
  271. matrix_out[i * stride + j] /= maxcoef;
  272. }
  273. return 0;
  274. }