1
0

mapping_matrix.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /* Copyright (c) 2017 Google Inc.
  2. Written by Andrew Allen */
  3. /*
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. - Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  13. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  14. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  15. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  16. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  17. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  20. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  21. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif
  27. #include "arch.h"
  28. #include "float_cast.h"
  29. #include "opus_private.h"
  30. #include "opus_defines.h"
  31. #include "mapping_matrix.h"
  32. #define MATRIX_INDEX(nb_rows, row, col) (nb_rows * col + row)
  33. opus_int32 mapping_matrix_get_size(int rows, int cols)
  34. {
  35. opus_int32 size;
  36. /* Mapping Matrix must only support up to 255 channels in or out.
  37. * Additionally, the total cell count must be <= 65004 octets in order
  38. * for the matrix to be stored in an OGG header.
  39. */
  40. if (rows > 255 || cols > 255)
  41. return 0;
  42. size = rows * (opus_int32)cols * sizeof(opus_int16);
  43. if (size > 65004)
  44. return 0;
  45. return align(sizeof(MappingMatrix)) + align(size);
  46. }
  47. opus_int16 *mapping_matrix_get_data(const MappingMatrix *matrix)
  48. {
  49. /* void* cast avoids clang -Wcast-align warning */
  50. return (opus_int16*)(void*)((char*)matrix + align(sizeof(MappingMatrix)));
  51. }
  52. void mapping_matrix_init(MappingMatrix * const matrix,
  53. int rows, int cols, int gain, const opus_int16 *data, opus_int32 data_size)
  54. {
  55. int i;
  56. opus_int16 *ptr;
  57. #if !defined(ENABLE_ASSERTIONS)
  58. (void)data_size;
  59. #endif
  60. celt_assert(align(data_size) == align(rows * cols * sizeof(opus_int16)));
  61. matrix->rows = rows;
  62. matrix->cols = cols;
  63. matrix->gain = gain;
  64. ptr = mapping_matrix_get_data(matrix);
  65. for (i = 0; i < rows * cols; i++)
  66. {
  67. ptr[i] = data[i];
  68. }
  69. }
  70. #ifndef DISABLE_FLOAT_API
  71. void mapping_matrix_multiply_channel_in_float(
  72. const MappingMatrix *matrix,
  73. const float *input,
  74. int input_rows,
  75. opus_val16 *output,
  76. int output_row,
  77. int output_rows,
  78. int frame_size)
  79. {
  80. /* Matrix data is ordered col-wise. */
  81. opus_int16* matrix_data;
  82. int i, col;
  83. celt_assert(input_rows <= matrix->cols && output_rows <= matrix->rows);
  84. matrix_data = mapping_matrix_get_data(matrix);
  85. for (i = 0; i < frame_size; i++)
  86. {
  87. float tmp = 0;
  88. for (col = 0; col < input_rows; col++)
  89. {
  90. tmp +=
  91. matrix_data[MATRIX_INDEX(matrix->rows, output_row, col)] *
  92. input[MATRIX_INDEX(input_rows, col, i)];
  93. }
  94. #if defined(FIXED_POINT)
  95. output[output_rows * i] = FLOAT2INT16((1/32768.f)*tmp);
  96. #else
  97. output[output_rows * i] = (1/32768.f)*tmp;
  98. #endif
  99. }
  100. }
  101. void mapping_matrix_multiply_channel_out_float(
  102. const MappingMatrix *matrix,
  103. const opus_val16 *input,
  104. int input_row,
  105. int input_rows,
  106. float *output,
  107. int output_rows,
  108. int frame_size
  109. )
  110. {
  111. /* Matrix data is ordered col-wise. */
  112. opus_int16* matrix_data;
  113. int i, row;
  114. float input_sample;
  115. celt_assert(input_rows <= matrix->cols && output_rows <= matrix->rows);
  116. matrix_data = mapping_matrix_get_data(matrix);
  117. for (i = 0; i < frame_size; i++)
  118. {
  119. #if defined(FIXED_POINT)
  120. input_sample = (1/32768.f)*input[input_rows * i];
  121. #else
  122. input_sample = input[input_rows * i];
  123. #endif
  124. for (row = 0; row < output_rows; row++)
  125. {
  126. float tmp =
  127. (1/32768.f)*matrix_data[MATRIX_INDEX(matrix->rows, row, input_row)] *
  128. input_sample;
  129. output[MATRIX_INDEX(output_rows, row, i)] += tmp;
  130. }
  131. }
  132. }
  133. #endif /* DISABLE_FLOAT_API */
  134. void mapping_matrix_multiply_channel_in_short(
  135. const MappingMatrix *matrix,
  136. const opus_int16 *input,
  137. int input_rows,
  138. opus_val16 *output,
  139. int output_row,
  140. int output_rows,
  141. int frame_size)
  142. {
  143. /* Matrix data is ordered col-wise. */
  144. opus_int16* matrix_data;
  145. int i, col;
  146. celt_assert(input_rows <= matrix->cols && output_rows <= matrix->rows);
  147. matrix_data = mapping_matrix_get_data(matrix);
  148. for (i = 0; i < frame_size; i++)
  149. {
  150. opus_val32 tmp = 0;
  151. for (col = 0; col < input_rows; col++)
  152. {
  153. #if defined(FIXED_POINT)
  154. tmp +=
  155. ((opus_int32)matrix_data[MATRIX_INDEX(matrix->rows, output_row, col)] *
  156. (opus_int32)input[MATRIX_INDEX(input_rows, col, i)]) >> 8;
  157. #else
  158. tmp +=
  159. matrix_data[MATRIX_INDEX(matrix->rows, output_row, col)] *
  160. input[MATRIX_INDEX(input_rows, col, i)];
  161. #endif
  162. }
  163. #if defined(FIXED_POINT)
  164. output[output_rows * i] = (opus_int16)((tmp + 64) >> 7);
  165. #else
  166. output[output_rows * i] = (1/(32768.f*32768.f))*tmp;
  167. #endif
  168. }
  169. }
  170. void mapping_matrix_multiply_channel_out_short(
  171. const MappingMatrix *matrix,
  172. const opus_val16 *input,
  173. int input_row,
  174. int input_rows,
  175. opus_int16 *output,
  176. int output_rows,
  177. int frame_size)
  178. {
  179. /* Matrix data is ordered col-wise. */
  180. opus_int16* matrix_data;
  181. int i, row;
  182. opus_int32 input_sample;
  183. celt_assert(input_rows <= matrix->cols && output_rows <= matrix->rows);
  184. matrix_data = mapping_matrix_get_data(matrix);
  185. for (i = 0; i < frame_size; i++)
  186. {
  187. #if defined(FIXED_POINT)
  188. input_sample = (opus_int32)input[input_rows * i];
  189. #else
  190. input_sample = (opus_int32)FLOAT2INT16(input[input_rows * i]);
  191. #endif
  192. for (row = 0; row < output_rows; row++)
  193. {
  194. opus_int32 tmp =
  195. (opus_int32)matrix_data[MATRIX_INDEX(matrix->rows, row, input_row)] *
  196. input_sample;
  197. output[MATRIX_INDEX(output_rows, row, i)] += (tmp + 16384) >> 15;
  198. }
  199. }
  200. }
  201. const MappingMatrix mapping_matrix_foa_mixing = { 6, 6, 0 };
  202. const opus_int16 mapping_matrix_foa_mixing_data[36] = {
  203. 16384, 0, -16384, 23170, 0, 0, 16384, 23170,
  204. 16384, 0, 0, 0, 16384, 0, -16384, -23170,
  205. 0, 0, 16384, -23170, 16384, 0, 0, 0,
  206. 0, 0, 0, 0, 32767, 0, 0, 0,
  207. 0, 0, 0, 32767
  208. };
  209. const MappingMatrix mapping_matrix_soa_mixing = { 11, 11, 0 };
  210. const opus_int16 mapping_matrix_soa_mixing_data[121] = {
  211. 10923, 7723, 13377, -13377, 11585, 9459, 7723, -16384,
  212. -6689, 0, 0, 10923, 7723, 13377, 13377, -11585,
  213. 9459, 7723, 16384, -6689, 0, 0, 10923, -15447,
  214. 13377, 0, 0, -18919, 7723, 0, 13377, 0,
  215. 0, 10923, 7723, -13377, -13377, 11585, -9459, 7723,
  216. 16384, -6689, 0, 0, 10923, -7723, 0, 13377,
  217. -16384, 0, -15447, 0, 9459, 0, 0, 10923,
  218. -7723, 0, -13377, 16384, 0, -15447, 0, 9459,
  219. 0, 0, 10923, 15447, 0, 0, 0, 0,
  220. -15447, 0, -18919, 0, 0, 10923, 7723, -13377,
  221. 13377, -11585, -9459, 7723, -16384, -6689, 0, 0,
  222. 10923, -15447, -13377, 0, 0, 18919, 7723, 0,
  223. 13377, 0, 0, 0, 0, 0, 0, 0,
  224. 0, 0, 0, 0, 32767, 0, 0, 0,
  225. 0, 0, 0, 0, 0, 0, 0, 0,
  226. 32767
  227. };
  228. const MappingMatrix mapping_matrix_toa_mixing = { 18, 18, 0 };
  229. const opus_int16 mapping_matrix_toa_mixing_data[324] = {
  230. 8208, 0, -881, 14369, 0, 0, -8192, -4163,
  231. 13218, 0, 0, 0, 11095, -8836, -6218, 14833,
  232. 0, 0, 8208, -10161, 881, 10161, -13218, -2944,
  233. -8192, 2944, 0, -10488, -6218, 6248, -11095, -6248,
  234. 0, -10488, 0, 0, 8208, 10161, 881, -10161,
  235. -13218, 2944, -8192, -2944, 0, 10488, -6218, -6248,
  236. -11095, 6248, 0, 10488, 0, 0, 8176, 5566,
  237. -11552, 5566, 9681, -11205, 8192, -11205, 0, 4920,
  238. -15158, 9756, -3334, 9756, 0, -4920, 0, 0,
  239. 8176, 7871, 11552, 0, 0, 15846, 8192, 0,
  240. -9681, -6958, 0, 13797, 3334, 0, -15158, 0,
  241. 0, 0, 8176, 0, 11552, 7871, 0, 0,
  242. 8192, 15846, 9681, 0, 0, 0, 3334, 13797,
  243. 15158, 6958, 0, 0, 8176, 5566, -11552, -5566,
  244. -9681, -11205, 8192, 11205, 0, 4920, 15158, 9756,
  245. -3334, -9756, 0, 4920, 0, 0, 8208, 14369,
  246. -881, 0, 0, -4163, -8192, 0, -13218, -14833,
  247. 0, -8836, 11095, 0, 6218, 0, 0, 0,
  248. 8208, 10161, 881, 10161, 13218, 2944, -8192, 2944,
  249. 0, 10488, 6218, -6248, -11095, -6248, 0, -10488,
  250. 0, 0, 8208, -14369, -881, 0, 0, 4163,
  251. -8192, 0, -13218, 14833, 0, 8836, 11095, 0,
  252. 6218, 0, 0, 0, 8208, 0, -881, -14369,
  253. 0, 0, -8192, 4163, 13218, 0, 0, 0,
  254. 11095, 8836, -6218, -14833, 0, 0, 8176, -5566,
  255. -11552, 5566, -9681, 11205, 8192, -11205, 0, -4920,
  256. 15158, -9756, -3334, 9756, 0, -4920, 0, 0,
  257. 8176, 0, 11552, -7871, 0, 0, 8192, -15846,
  258. 9681, 0, 0, 0, 3334, -13797, 15158, -6958,
  259. 0, 0, 8176, -7871, 11552, 0, 0, -15846,
  260. 8192, 0, -9681, 6958, 0, -13797, 3334, 0,
  261. -15158, 0, 0, 0, 8176, -5566, -11552, -5566,
  262. 9681, 11205, 8192, 11205, 0, -4920, -15158, -9756,
  263. -3334, -9756, 0, 4920, 0, 0, 8208, -10161,
  264. 881, -10161, 13218, -2944, -8192, -2944, 0, -10488,
  265. 6218, 6248, -11095, 6248, 0, 10488, 0, 0,
  266. 0, 0, 0, 0, 0, 0, 0, 0,
  267. 0, 0, 0, 0, 0, 0, 0, 0,
  268. 32767, 0, 0, 0, 0, 0, 0, 0,
  269. 0, 0, 0, 0, 0, 0, 0, 0,
  270. 0, 0, 0, 32767
  271. };
  272. const MappingMatrix mapping_matrix_foa_demixing = { 6, 6, 0 };
  273. const opus_int16 mapping_matrix_foa_demixing_data[36] = {
  274. 16384, 16384, 16384, 16384, 0, 0, 0, 23170,
  275. 0, -23170, 0, 0, -16384, 16384, -16384, 16384,
  276. 0, 0, 23170, 0, -23170, 0, 0, 0,
  277. 0, 0, 0, 0, 32767, 0, 0, 0,
  278. 0, 0, 0, 32767
  279. };
  280. const MappingMatrix mapping_matrix_soa_demixing = { 11, 11, 3050 };
  281. const opus_int16 mapping_matrix_soa_demixing_data[121] = {
  282. 2771, 2771, 2771, 2771, 2771, 2771, 2771, 2771,
  283. 2771, 0, 0, 10033, 10033, -20066, 10033, 14189,
  284. 14189, -28378, 10033, -20066, 0, 0, 3393, 3393,
  285. 3393, -3393, 0, 0, 0, -3393, -3393, 0,
  286. 0, -17378, 17378, 0, -17378, -24576, 24576, 0,
  287. 17378, 0, 0, 0, -14189, 14189, 0, -14189,
  288. -28378, 28378, 0, 14189, 0, 0, 0, 2399,
  289. 2399, -4799, -2399, 0, 0, 0, -2399, 4799,
  290. 0, 0, 1959, 1959, 1959, 1959, -3918, -3918,
  291. -3918, 1959, 1959, 0, 0, -4156, 4156, 0,
  292. 4156, 0, 0, 0, -4156, 0, 0, 0,
  293. 8192, 8192, -16384, 8192, 16384, 16384, -32768, 8192,
  294. -16384, 0, 0, 0, 0, 0, 0, 0,
  295. 0, 0, 0, 0, 8312, 0, 0, 0,
  296. 0, 0, 0, 0, 0, 0, 0, 0,
  297. 8312
  298. };
  299. const MappingMatrix mapping_matrix_toa_demixing = { 18, 18, 0 };
  300. const opus_int16 mapping_matrix_toa_demixing_data[324] = {
  301. 8192, 8192, 8192, 8192, 8192, 8192, 8192, 8192,
  302. 8192, 8192, 8192, 8192, 8192, 8192, 8192, 8192,
  303. 0, 0, 0, -9779, 9779, 6263, 8857, 0,
  304. 6263, 13829, 9779, -13829, 0, -6263, 0, -8857,
  305. -6263, -9779, 0, 0, -3413, 3413, 3413, -11359,
  306. 11359, 11359, -11359, -3413, 3413, -3413, -3413, -11359,
  307. 11359, 11359, -11359, 3413, 0, 0, 13829, 9779,
  308. -9779, 6263, 0, 8857, -6263, 0, 9779, 0,
  309. -13829, 6263, -8857, 0, -6263, -9779, 0, 0,
  310. 0, -15617, -15617, 6406, 0, 0, -6406, 0,
  311. 15617, 0, 0, -6406, 0, 0, 6406, 15617,
  312. 0, 0, 0, -5003, 5003, -10664, 15081, 0,
  313. -10664, -7075, 5003, 7075, 0, 10664, 0, -15081,
  314. 10664, -5003, 0, 0, -8176, -8176, -8176, 8208,
  315. 8208, 8208, 8208, -8176, -8176, -8176, -8176, 8208,
  316. 8208, 8208, 8208, -8176, 0, 0, -7075, 5003,
  317. -5003, -10664, 0, 15081, 10664, 0, 5003, 0,
  318. 7075, -10664, -15081, 0, 10664, -5003, 0, 0,
  319. 15617, 0, 0, 0, -6406, 6406, 0, -15617,
  320. 0, -15617, 15617, 0, 6406, -6406, 0, 0,
  321. 0, 0, 0, -11393, 11393, 2993, -4233, 0,
  322. 2993, -16112, 11393, 16112, 0, -2993, 0, 4233,
  323. -2993, -11393, 0, 0, 0, -9974, -9974, -13617,
  324. 0, 0, 13617, 0, 9974, 0, 0, 13617,
  325. 0, 0, -13617, 9974, 0, 0, 0, 5579,
  326. -5579, 10185, 14403, 0, 10185, -7890, -5579, 7890,
  327. 0, -10185, 0, -14403, -10185, 5579, 0, 0,
  328. 11826, -11826, -11826, -901, 901, 901, -901, 11826,
  329. -11826, 11826, 11826, -901, 901, 901, -901, -11826,
  330. 0, 0, -7890, -5579, 5579, 10185, 0, 14403,
  331. -10185, 0, -5579, 0, 7890, 10185, -14403, 0,
  332. -10185, 5579, 0, 0, -9974, 0, 0, 0,
  333. -13617, 13617, 0, 9974, 0, 9974, -9974, 0,
  334. 13617, -13617, 0, 0, 0, 0, 16112, -11393,
  335. 11393, -2993, 0, 4233, 2993, 0, -11393, 0,
  336. -16112, -2993, -4233, 0, 2993, 11393, 0, 0,
  337. 0, 0, 0, 0, 0, 0, 0, 0,
  338. 0, 0, 0, 0, 0, 0, 0, 0,
  339. 32767, 0, 0, 0, 0, 0, 0, 0,
  340. 0, 0, 0, 0, 0, 0, 0, 0,
  341. 0, 0, 0, 32767
  342. };