rematrix.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. /*
  2. * Copyright (C) 2011-2012 Michael Niedermayer (michaelni@gmx.at)
  3. *
  4. * This file is part of libswresample
  5. *
  6. * libswresample is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * libswresample is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with libswresample; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "swresample_internal.h"
  21. #include "libavutil/avassert.h"
  22. #include "libavutil/channel_layout.h"
  23. #define TEMPLATE_REMATRIX_FLT
  24. #include "rematrix_template.c"
  25. #undef TEMPLATE_REMATRIX_FLT
  26. #define TEMPLATE_REMATRIX_DBL
  27. #include "rematrix_template.c"
  28. #undef TEMPLATE_REMATRIX_DBL
  29. #define TEMPLATE_REMATRIX_S16
  30. #include "rematrix_template.c"
  31. #define TEMPLATE_CLIP
  32. #include "rematrix_template.c"
  33. #undef TEMPLATE_CLIP
  34. #undef TEMPLATE_REMATRIX_S16
  35. #define TEMPLATE_REMATRIX_S32
  36. #include "rematrix_template.c"
  37. #undef TEMPLATE_REMATRIX_S32
  38. #define FRONT_LEFT 0
  39. #define FRONT_RIGHT 1
  40. #define FRONT_CENTER 2
  41. #define LOW_FREQUENCY 3
  42. #define BACK_LEFT 4
  43. #define BACK_RIGHT 5
  44. #define FRONT_LEFT_OF_CENTER 6
  45. #define FRONT_RIGHT_OF_CENTER 7
  46. #define BACK_CENTER 8
  47. #define SIDE_LEFT 9
  48. #define SIDE_RIGHT 10
  49. #define TOP_CENTER 11
  50. #define TOP_FRONT_LEFT 12
  51. #define TOP_FRONT_CENTER 13
  52. #define TOP_FRONT_RIGHT 14
  53. #define TOP_BACK_LEFT 15
  54. #define TOP_BACK_CENTER 16
  55. #define TOP_BACK_RIGHT 17
  56. #define NUM_NAMED_CHANNELS 18
  57. int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride)
  58. {
  59. int nb_in, nb_out, in, out;
  60. if (!s || s->in_convert) // s needs to be allocated but not initialized
  61. return AVERROR(EINVAL);
  62. memset(s->matrix, 0, sizeof(s->matrix));
  63. memset(s->matrix_flt, 0, sizeof(s->matrix_flt));
  64. nb_in = s->user_in_chlayout.nb_channels;
  65. nb_out = s->user_out_chlayout.nb_channels;
  66. for (out = 0; out < nb_out; out++) {
  67. for (in = 0; in < nb_in; in++)
  68. s->matrix_flt[out][in] = s->matrix[out][in] = matrix[in];
  69. matrix += stride;
  70. }
  71. s->rematrix_custom = 1;
  72. return 0;
  73. }
  74. static int even(int64_t layout){
  75. if(!layout) return 1;
  76. if(layout&(layout-1)) return 1;
  77. return 0;
  78. }
  79. static int clean_layout(AVChannelLayout *out, const AVChannelLayout *in, void *s)
  80. {
  81. int ret = 0;
  82. if (av_channel_layout_index_from_channel(in, AV_CHAN_FRONT_CENTER) < 0 && in->nb_channels == 1) {
  83. char buf[128];
  84. av_channel_layout_describe(in, buf, sizeof(buf));
  85. av_log(s, AV_LOG_VERBOSE, "Treating %s as mono\n", buf);
  86. *out = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
  87. } else
  88. ret = av_channel_layout_copy(out, in);
  89. return ret;
  90. }
  91. static int sane_layout(AVChannelLayout *ch_layout) {
  92. if (ch_layout->order != AV_CHANNEL_ORDER_NATIVE)
  93. return 0;
  94. if(!av_channel_layout_subset(ch_layout, AV_CH_LAYOUT_SURROUND)) // at least 1 front speaker
  95. return 0;
  96. if(!even(av_channel_layout_subset(ch_layout, (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT)))) // no asymetric front
  97. return 0;
  98. if(!even(av_channel_layout_subset(ch_layout, (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)))) // no asymetric side
  99. return 0;
  100. if(!even(av_channel_layout_subset(ch_layout, (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT))))
  101. return 0;
  102. if(!even(av_channel_layout_subset(ch_layout, (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER))))
  103. return 0;
  104. if(!even(av_channel_layout_subset(ch_layout, (AV_CH_TOP_FRONT_LEFT | AV_CH_TOP_FRONT_RIGHT))))
  105. return 0;
  106. if(ch_layout->nb_channels >= SWR_CH_MAX)
  107. return 0;
  108. return 1;
  109. }
  110. av_cold int swr_build_matrix2(const AVChannelLayout *in_layout, const AVChannelLayout *out_layout,
  111. double center_mix_level, double surround_mix_level,
  112. double lfe_mix_level, double maxval,
  113. double rematrix_volume, double *matrix_param,
  114. ptrdiff_t stride, enum AVMatrixEncoding matrix_encoding, void *log_context)
  115. {
  116. int i, j, out_i, ret;
  117. AVChannelLayout in_ch_layout = { 0 }, out_ch_layout = { 0 };
  118. double matrix[NUM_NAMED_CHANNELS][NUM_NAMED_CHANNELS]={{0}};
  119. int64_t unaccounted;
  120. double maxcoef=0;
  121. char buf[128];
  122. ret = clean_layout(&in_ch_layout, in_layout, log_context);
  123. ret |= clean_layout(&out_ch_layout, out_layout, log_context);
  124. if (ret < 0)
  125. goto fail;
  126. if( !av_channel_layout_compare(&out_ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO_DOWNMIX)
  127. && !av_channel_layout_subset(&in_ch_layout, AV_CH_LAYOUT_STEREO_DOWNMIX)
  128. ) {
  129. av_channel_layout_uninit(&out_ch_layout);
  130. out_ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
  131. }
  132. if( !av_channel_layout_compare(&in_ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO_DOWNMIX)
  133. && !av_channel_layout_subset(&out_ch_layout, AV_CH_LAYOUT_STEREO_DOWNMIX)
  134. ) {
  135. av_channel_layout_uninit(&in_ch_layout);
  136. in_ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
  137. }
  138. if (!av_channel_layout_compare(&in_ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_22POINT2) &&
  139. av_channel_layout_compare(&out_ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_22POINT2)) {
  140. av_channel_layout_from_mask(&in_ch_layout, (AV_CH_LAYOUT_7POINT1_WIDE_BACK|AV_CH_BACK_CENTER));
  141. av_channel_layout_describe(&in_ch_layout, buf, sizeof(buf));
  142. av_log(log_context, AV_LOG_WARNING,
  143. "Full-on remixing from 22.2 has not yet been implemented! "
  144. "Processing the input as '%s'\n",
  145. buf);
  146. }
  147. if(!av_channel_layout_check(&in_ch_layout)) {
  148. av_log(log_context, AV_LOG_ERROR, "Input channel layout is invalid\n");
  149. ret = AVERROR(EINVAL);
  150. goto fail;
  151. }
  152. if(!sane_layout(&in_ch_layout)) {
  153. av_channel_layout_describe(&in_ch_layout, buf, sizeof(buf));
  154. av_log(log_context, AV_LOG_ERROR, "Input channel layout '%s' is not supported\n", buf);
  155. ret = AVERROR(EINVAL);
  156. goto fail;
  157. }
  158. if(!av_channel_layout_check(&out_ch_layout)) {
  159. av_log(log_context, AV_LOG_ERROR, "Output channel layout is invalid\n");
  160. ret = AVERROR(EINVAL);
  161. goto fail;
  162. }
  163. if(!sane_layout(&out_ch_layout)) {
  164. av_channel_layout_describe(&out_ch_layout, buf, sizeof(buf));
  165. av_log(log_context, AV_LOG_ERROR, "Output channel layout '%s' is not supported\n", buf);
  166. ret = AVERROR(EINVAL);
  167. goto fail;
  168. }
  169. for(i=0; i<FF_ARRAY_ELEMS(matrix); i++){
  170. if( av_channel_layout_index_from_channel(&in_ch_layout, i) >= 0
  171. && av_channel_layout_index_from_channel(&out_ch_layout, i) >= 0)
  172. matrix[i][i]= 1.0;
  173. }
  174. unaccounted = in_ch_layout.u.mask & ~out_ch_layout.u.mask;
  175. //FIXME implement dolby surround
  176. //FIXME implement full ac3
  177. if(unaccounted & AV_CH_FRONT_CENTER){
  178. if (av_channel_layout_subset(&out_ch_layout, AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO) {
  179. if (av_channel_layout_subset(&in_ch_layout, AV_CH_LAYOUT_STEREO)) {
  180. matrix[ FRONT_LEFT][FRONT_CENTER]+= center_mix_level;
  181. matrix[FRONT_RIGHT][FRONT_CENTER]+= center_mix_level;
  182. } else {
  183. matrix[ FRONT_LEFT][FRONT_CENTER]+= M_SQRT1_2;
  184. matrix[FRONT_RIGHT][FRONT_CENTER]+= M_SQRT1_2;
  185. }
  186. }else
  187. av_assert0(0);
  188. }
  189. if(unaccounted & AV_CH_LAYOUT_STEREO){
  190. if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
  191. matrix[FRONT_CENTER][ FRONT_LEFT]+= M_SQRT1_2;
  192. matrix[FRONT_CENTER][FRONT_RIGHT]+= M_SQRT1_2;
  193. if (av_channel_layout_index_from_channel(&in_ch_layout, AV_CHAN_FRONT_CENTER) >= 0)
  194. matrix[FRONT_CENTER][ FRONT_CENTER] = center_mix_level*sqrt(2);
  195. }else
  196. av_assert0(0);
  197. }
  198. if(unaccounted & AV_CH_BACK_CENTER){
  199. if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_BACK_LEFT) >= 0) {
  200. matrix[ BACK_LEFT][BACK_CENTER]+= M_SQRT1_2;
  201. matrix[BACK_RIGHT][BACK_CENTER]+= M_SQRT1_2;
  202. } else if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_SIDE_LEFT) >= 0) {
  203. matrix[ SIDE_LEFT][BACK_CENTER]+= M_SQRT1_2;
  204. matrix[SIDE_RIGHT][BACK_CENTER]+= M_SQRT1_2;
  205. } else if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
  206. if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY ||
  207. matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
  208. if (unaccounted & (AV_CH_BACK_LEFT | AV_CH_SIDE_LEFT)) {
  209. matrix[FRONT_LEFT ][BACK_CENTER] -= surround_mix_level * M_SQRT1_2;
  210. matrix[FRONT_RIGHT][BACK_CENTER] += surround_mix_level * M_SQRT1_2;
  211. } else {
  212. matrix[FRONT_LEFT ][BACK_CENTER] -= surround_mix_level;
  213. matrix[FRONT_RIGHT][BACK_CENTER] += surround_mix_level;
  214. }
  215. } else {
  216. matrix[ FRONT_LEFT][BACK_CENTER]+= surround_mix_level * M_SQRT1_2;
  217. matrix[FRONT_RIGHT][BACK_CENTER]+= surround_mix_level * M_SQRT1_2;
  218. }
  219. } else if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
  220. matrix[ FRONT_CENTER][BACK_CENTER]+= surround_mix_level * M_SQRT1_2;
  221. }else
  222. av_assert0(0);
  223. }
  224. if(unaccounted & AV_CH_BACK_LEFT){
  225. if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_BACK_CENTER) >= 0) {
  226. matrix[BACK_CENTER][ BACK_LEFT]+= M_SQRT1_2;
  227. matrix[BACK_CENTER][BACK_RIGHT]+= M_SQRT1_2;
  228. } else if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_SIDE_LEFT) >= 0) {
  229. if (av_channel_layout_index_from_channel(&in_ch_layout, AV_CHAN_SIDE_LEFT) >= 0) {
  230. matrix[ SIDE_LEFT][ BACK_LEFT]+= M_SQRT1_2;
  231. matrix[SIDE_RIGHT][BACK_RIGHT]+= M_SQRT1_2;
  232. }else{
  233. matrix[ SIDE_LEFT][ BACK_LEFT]+= 1.0;
  234. matrix[SIDE_RIGHT][BACK_RIGHT]+= 1.0;
  235. }
  236. } else if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
  237. if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
  238. matrix[FRONT_LEFT ][BACK_LEFT ] -= surround_mix_level * M_SQRT1_2;
  239. matrix[FRONT_LEFT ][BACK_RIGHT] -= surround_mix_level * M_SQRT1_2;
  240. matrix[FRONT_RIGHT][BACK_LEFT ] += surround_mix_level * M_SQRT1_2;
  241. matrix[FRONT_RIGHT][BACK_RIGHT] += surround_mix_level * M_SQRT1_2;
  242. } else if (matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
  243. matrix[FRONT_LEFT ][BACK_LEFT ] -= surround_mix_level * SQRT3_2;
  244. matrix[FRONT_LEFT ][BACK_RIGHT] -= surround_mix_level * M_SQRT1_2;
  245. matrix[FRONT_RIGHT][BACK_LEFT ] += surround_mix_level * M_SQRT1_2;
  246. matrix[FRONT_RIGHT][BACK_RIGHT] += surround_mix_level * SQRT3_2;
  247. } else {
  248. matrix[ FRONT_LEFT][ BACK_LEFT] += surround_mix_level;
  249. matrix[FRONT_RIGHT][BACK_RIGHT] += surround_mix_level;
  250. }
  251. } else if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
  252. matrix[ FRONT_CENTER][BACK_LEFT ]+= surround_mix_level*M_SQRT1_2;
  253. matrix[ FRONT_CENTER][BACK_RIGHT]+= surround_mix_level*M_SQRT1_2;
  254. }else
  255. av_assert0(0);
  256. }
  257. if(unaccounted & AV_CH_SIDE_LEFT){
  258. if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_BACK_LEFT) >= 0) {
  259. /* if back channels do not exist in the input, just copy side
  260. channels to back channels, otherwise mix side into back */
  261. if (av_channel_layout_index_from_channel(&in_ch_layout, AV_CHAN_BACK_LEFT) >= 0) {
  262. matrix[BACK_LEFT ][SIDE_LEFT ] += M_SQRT1_2;
  263. matrix[BACK_RIGHT][SIDE_RIGHT] += M_SQRT1_2;
  264. } else {
  265. matrix[BACK_LEFT ][SIDE_LEFT ] += 1.0;
  266. matrix[BACK_RIGHT][SIDE_RIGHT] += 1.0;
  267. }
  268. } else if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_BACK_CENTER) >= 0) {
  269. matrix[BACK_CENTER][ SIDE_LEFT]+= M_SQRT1_2;
  270. matrix[BACK_CENTER][SIDE_RIGHT]+= M_SQRT1_2;
  271. } else if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
  272. if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
  273. matrix[FRONT_LEFT ][SIDE_LEFT ] -= surround_mix_level * M_SQRT1_2;
  274. matrix[FRONT_LEFT ][SIDE_RIGHT] -= surround_mix_level * M_SQRT1_2;
  275. matrix[FRONT_RIGHT][SIDE_LEFT ] += surround_mix_level * M_SQRT1_2;
  276. matrix[FRONT_RIGHT][SIDE_RIGHT] += surround_mix_level * M_SQRT1_2;
  277. } else if (matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
  278. matrix[FRONT_LEFT ][SIDE_LEFT ] -= surround_mix_level * SQRT3_2;
  279. matrix[FRONT_LEFT ][SIDE_RIGHT] -= surround_mix_level * M_SQRT1_2;
  280. matrix[FRONT_RIGHT][SIDE_LEFT ] += surround_mix_level * M_SQRT1_2;
  281. matrix[FRONT_RIGHT][SIDE_RIGHT] += surround_mix_level * SQRT3_2;
  282. } else {
  283. matrix[ FRONT_LEFT][ SIDE_LEFT] += surround_mix_level;
  284. matrix[FRONT_RIGHT][SIDE_RIGHT] += surround_mix_level;
  285. }
  286. } else if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
  287. matrix[ FRONT_CENTER][SIDE_LEFT ]+= surround_mix_level * M_SQRT1_2;
  288. matrix[ FRONT_CENTER][SIDE_RIGHT]+= surround_mix_level * M_SQRT1_2;
  289. }else
  290. av_assert0(0);
  291. }
  292. if(unaccounted & AV_CH_FRONT_LEFT_OF_CENTER){
  293. if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
  294. matrix[ FRONT_LEFT][ FRONT_LEFT_OF_CENTER]+= 1.0;
  295. matrix[FRONT_RIGHT][FRONT_RIGHT_OF_CENTER]+= 1.0;
  296. } else if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
  297. matrix[ FRONT_CENTER][ FRONT_LEFT_OF_CENTER]+= M_SQRT1_2;
  298. matrix[ FRONT_CENTER][FRONT_RIGHT_OF_CENTER]+= M_SQRT1_2;
  299. }else
  300. av_assert0(0);
  301. }
  302. if (unaccounted & AV_CH_TOP_FRONT_LEFT) {
  303. if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_TOP_FRONT_CENTER) >= 0) {
  304. matrix[TOP_FRONT_CENTER][TOP_FRONT_LEFT ] += M_SQRT1_2;
  305. matrix[TOP_FRONT_CENTER][TOP_FRONT_RIGHT] += M_SQRT1_2;
  306. if (av_channel_layout_index_from_channel(&in_ch_layout, AV_CHAN_TOP_FRONT_CENTER) >= 0)
  307. matrix[TOP_FRONT_CENTER][TOP_FRONT_CENTER] = center_mix_level * sqrt(2);
  308. } else if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
  309. if (av_channel_layout_index_from_channel(&in_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
  310. matrix[FRONT_LEFT ][TOP_FRONT_LEFT ] += M_SQRT1_2;
  311. matrix[FRONT_RIGHT][TOP_FRONT_RIGHT] += M_SQRT1_2;
  312. } else {
  313. matrix[FRONT_LEFT ][TOP_FRONT_LEFT ] += 1.0;
  314. matrix[FRONT_RIGHT][TOP_FRONT_RIGHT] += 1.0;
  315. }
  316. } else if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
  317. matrix[FRONT_CENTER][TOP_FRONT_LEFT ] += M_SQRT1_2;
  318. matrix[FRONT_CENTER][TOP_FRONT_RIGHT] += M_SQRT1_2;
  319. } else
  320. av_assert0(0);
  321. }
  322. /* mix LFE into front left/right or center */
  323. if (unaccounted & AV_CH_LOW_FREQUENCY) {
  324. if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
  325. matrix[FRONT_CENTER][LOW_FREQUENCY] += lfe_mix_level;
  326. } else if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
  327. matrix[FRONT_LEFT ][LOW_FREQUENCY] += lfe_mix_level * M_SQRT1_2;
  328. matrix[FRONT_RIGHT][LOW_FREQUENCY] += lfe_mix_level * M_SQRT1_2;
  329. } else
  330. av_assert0(0);
  331. }
  332. for(out_i=i=0; i<64; i++){
  333. double sum=0;
  334. int in_i=0;
  335. if (av_channel_layout_index_from_channel(&out_ch_layout, i) < 0)
  336. continue;
  337. for(j=0; j<64; j++){
  338. if (av_channel_layout_index_from_channel(&in_ch_layout, j) < 0)
  339. continue;
  340. if (i < FF_ARRAY_ELEMS(matrix) && j < FF_ARRAY_ELEMS(matrix[0]))
  341. matrix_param[stride*out_i + in_i] = matrix[i][j];
  342. else
  343. matrix_param[stride*out_i + in_i] = i == j &&
  344. ( av_channel_layout_index_from_channel(&in_ch_layout, i) >= 0
  345. && av_channel_layout_index_from_channel(&out_ch_layout, i) >= 0);
  346. sum += fabs(matrix_param[stride*out_i + in_i]);
  347. in_i++;
  348. }
  349. maxcoef= FFMAX(maxcoef, sum);
  350. out_i++;
  351. }
  352. if(rematrix_volume < 0)
  353. maxcoef = -rematrix_volume;
  354. if(maxcoef > maxval || rematrix_volume < 0){
  355. maxcoef /= maxval;
  356. for(i=0; i<SWR_CH_MAX; i++)
  357. for(j=0; j<SWR_CH_MAX; j++){
  358. matrix_param[stride*i + j] /= maxcoef;
  359. }
  360. }
  361. if(rematrix_volume > 0){
  362. for(i=0; i<SWR_CH_MAX; i++)
  363. for(j=0; j<SWR_CH_MAX; j++){
  364. matrix_param[stride*i + j] *= rematrix_volume;
  365. }
  366. }
  367. av_log(log_context, AV_LOG_DEBUG, "Matrix coefficients:\n");
  368. for (i = 0; i < out_ch_layout.nb_channels; i++){
  369. av_channel_name(buf, sizeof(buf), av_channel_layout_channel_from_index(&out_ch_layout, i));
  370. av_log(log_context, AV_LOG_DEBUG, "%s: ", buf);
  371. for (j = 0; j < in_ch_layout.nb_channels; j++){
  372. av_channel_name(buf, sizeof(buf), av_channel_layout_channel_from_index(&in_ch_layout, j));
  373. av_log(log_context, AV_LOG_DEBUG, "%s:%f ", buf, matrix_param[stride*i + j]);
  374. }
  375. av_log(log_context, AV_LOG_DEBUG, "\n");
  376. }
  377. ret = 0;
  378. fail:
  379. av_channel_layout_uninit(&in_ch_layout);
  380. av_channel_layout_uninit(&out_ch_layout);
  381. return ret;
  382. }
  383. av_cold static int auto_matrix(SwrContext *s)
  384. {
  385. double maxval;
  386. int ret;
  387. if (s->rematrix_maxval > 0) {
  388. maxval = s->rematrix_maxval;
  389. } else if ( av_get_packed_sample_fmt(s->out_sample_fmt) < AV_SAMPLE_FMT_FLT
  390. || av_get_packed_sample_fmt(s->int_sample_fmt) < AV_SAMPLE_FMT_FLT) {
  391. maxval = 1.0;
  392. } else
  393. maxval = INT_MAX;
  394. memset(s->matrix, 0, sizeof(s->matrix));
  395. ret = swr_build_matrix2(&s->in_ch_layout, &s->out_ch_layout,
  396. s->clev, s->slev, s->lfe_mix_level,
  397. maxval, s->rematrix_volume, (double*)s->matrix,
  398. s->matrix[1] - s->matrix[0], s->matrix_encoding, s);
  399. if (ret >= 0 && s->int_sample_fmt == AV_SAMPLE_FMT_FLTP) {
  400. int i, j;
  401. for (i = 0; i < FF_ARRAY_ELEMS(s->matrix[0]); i++)
  402. for (j = 0; j < FF_ARRAY_ELEMS(s->matrix[0]); j++)
  403. s->matrix_flt[i][j] = s->matrix[i][j];
  404. }
  405. return ret;
  406. }
  407. av_cold int swri_rematrix_init(SwrContext *s){
  408. int i, j;
  409. int nb_in = s->used_ch_layout.nb_channels;
  410. int nb_out = s->out.ch_count;
  411. s->mix_any_f = NULL;
  412. if (!s->rematrix_custom) {
  413. int r = auto_matrix(s);
  414. if (r)
  415. return r;
  416. }
  417. if (s->midbuf.fmt == AV_SAMPLE_FMT_S16P){
  418. int maxsum = 0;
  419. s->native_matrix = av_calloc(nb_in * nb_out, sizeof(int));
  420. s->native_one = av_mallocz(sizeof(int));
  421. if (!s->native_matrix || !s->native_one)
  422. return AVERROR(ENOMEM);
  423. for (i = 0; i < nb_out; i++) {
  424. double rem = 0;
  425. int sum = 0;
  426. for (j = 0; j < nb_in; j++) {
  427. double target = s->matrix[i][j] * 32768 + rem;
  428. ((int*)s->native_matrix)[i * nb_in + j] = lrintf(target);
  429. rem += target - ((int*)s->native_matrix)[i * nb_in + j];
  430. sum += FFABS(((int*)s->native_matrix)[i * nb_in + j]);
  431. }
  432. maxsum = FFMAX(maxsum, sum);
  433. }
  434. *((int*)s->native_one) = 32768;
  435. if (maxsum <= 32768) {
  436. s->mix_1_1_f = (mix_1_1_func_type*)copy_s16;
  437. s->mix_2_1_f = (mix_2_1_func_type*)sum2_s16;
  438. s->mix_any_f = (mix_any_func_type*)get_mix_any_func_s16(s);
  439. } else {
  440. s->mix_1_1_f = (mix_1_1_func_type*)copy_clip_s16;
  441. s->mix_2_1_f = (mix_2_1_func_type*)sum2_clip_s16;
  442. s->mix_any_f = (mix_any_func_type*)get_mix_any_func_clip_s16(s);
  443. }
  444. }else if(s->midbuf.fmt == AV_SAMPLE_FMT_FLTP){
  445. s->native_matrix = av_calloc(nb_in * nb_out, sizeof(float));
  446. s->native_one = av_mallocz(sizeof(float));
  447. if (!s->native_matrix || !s->native_one)
  448. return AVERROR(ENOMEM);
  449. for (i = 0; i < nb_out; i++)
  450. for (j = 0; j < nb_in; j++)
  451. ((float*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
  452. *((float*)s->native_one) = 1.0;
  453. s->mix_1_1_f = (mix_1_1_func_type*)copy_float;
  454. s->mix_2_1_f = (mix_2_1_func_type*)sum2_float;
  455. s->mix_any_f = (mix_any_func_type*)get_mix_any_func_float(s);
  456. }else if(s->midbuf.fmt == AV_SAMPLE_FMT_DBLP){
  457. s->native_matrix = av_calloc(nb_in * nb_out, sizeof(double));
  458. s->native_one = av_mallocz(sizeof(double));
  459. if (!s->native_matrix || !s->native_one)
  460. return AVERROR(ENOMEM);
  461. for (i = 0; i < nb_out; i++)
  462. for (j = 0; j < nb_in; j++)
  463. ((double*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
  464. *((double*)s->native_one) = 1.0;
  465. s->mix_1_1_f = (mix_1_1_func_type*)copy_double;
  466. s->mix_2_1_f = (mix_2_1_func_type*)sum2_double;
  467. s->mix_any_f = (mix_any_func_type*)get_mix_any_func_double(s);
  468. }else if(s->midbuf.fmt == AV_SAMPLE_FMT_S32P){
  469. s->native_one = av_mallocz(sizeof(int));
  470. if (!s->native_one)
  471. return AVERROR(ENOMEM);
  472. s->native_matrix = av_calloc(nb_in * nb_out, sizeof(int));
  473. if (!s->native_matrix) {
  474. av_freep(&s->native_one);
  475. return AVERROR(ENOMEM);
  476. }
  477. for (i = 0; i < nb_out; i++) {
  478. double rem = 0;
  479. for (j = 0; j < nb_in; j++) {
  480. double target = s->matrix[i][j] * 32768 + rem;
  481. ((int*)s->native_matrix)[i * nb_in + j] = lrintf(target);
  482. rem += target - ((int*)s->native_matrix)[i * nb_in + j];
  483. }
  484. }
  485. *((int*)s->native_one) = 32768;
  486. s->mix_1_1_f = (mix_1_1_func_type*)copy_s32;
  487. s->mix_2_1_f = (mix_2_1_func_type*)sum2_s32;
  488. s->mix_any_f = (mix_any_func_type*)get_mix_any_func_s32(s);
  489. }else
  490. av_assert0(0);
  491. //FIXME quantize for integeres
  492. for (i = 0; i < SWR_CH_MAX; i++) {
  493. int ch_in=0;
  494. for (j = 0; j < SWR_CH_MAX; j++) {
  495. s->matrix32[i][j]= lrintf(s->matrix[i][j] * 32768);
  496. if(s->matrix[i][j])
  497. s->matrix_ch[i][++ch_in]= j;
  498. }
  499. s->matrix_ch[i][0]= ch_in;
  500. }
  501. #if ARCH_X86 && HAVE_X86ASM && HAVE_MMX
  502. return swri_rematrix_init_x86(s);
  503. #endif
  504. return 0;
  505. }
  506. av_cold void swri_rematrix_free(SwrContext *s){
  507. av_freep(&s->native_matrix);
  508. av_freep(&s->native_one);
  509. av_freep(&s->native_simd_matrix);
  510. av_freep(&s->native_simd_one);
  511. }
  512. int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy){
  513. int out_i, in_i, i, j;
  514. int len1 = 0;
  515. int off = 0;
  516. if(s->mix_any_f) {
  517. s->mix_any_f(out->ch, (const uint8_t **)in->ch, s->native_matrix, len);
  518. return 0;
  519. }
  520. if(s->mix_2_1_simd || s->mix_1_1_simd){
  521. len1= len&~15;
  522. off = len1 * out->bps;
  523. }
  524. av_assert0(s->out_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC || out->ch_count == s->out_ch_layout.nb_channels);
  525. av_assert0(s-> in_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC || in ->ch_count == s->in_ch_layout.nb_channels);
  526. for(out_i=0; out_i<out->ch_count; out_i++){
  527. switch(s->matrix_ch[out_i][0]){
  528. case 0:
  529. if(mustcopy)
  530. memset(out->ch[out_i], 0, len * av_get_bytes_per_sample(s->int_sample_fmt));
  531. break;
  532. case 1:
  533. in_i= s->matrix_ch[out_i][1];
  534. if(s->matrix[out_i][in_i]!=1.0){
  535. if(s->mix_1_1_simd && len1)
  536. s->mix_1_1_simd(out->ch[out_i] , in->ch[in_i] , s->native_simd_matrix, in->ch_count*out_i + in_i, len1);
  537. if(len != len1)
  538. s->mix_1_1_f (out->ch[out_i]+off, in->ch[in_i]+off, s->native_matrix, in->ch_count*out_i + in_i, len-len1);
  539. }else if(mustcopy){
  540. memcpy(out->ch[out_i], in->ch[in_i], len*out->bps);
  541. }else{
  542. out->ch[out_i]= in->ch[in_i];
  543. }
  544. break;
  545. case 2: {
  546. int in_i1 = s->matrix_ch[out_i][1];
  547. int in_i2 = s->matrix_ch[out_i][2];
  548. if(s->mix_2_1_simd && len1)
  549. s->mix_2_1_simd(out->ch[out_i] , in->ch[in_i1] , in->ch[in_i2] , s->native_simd_matrix, in->ch_count*out_i + in_i1, in->ch_count*out_i + in_i2, len1);
  550. else
  551. s->mix_2_1_f (out->ch[out_i] , in->ch[in_i1] , in->ch[in_i2] , s->native_matrix, in->ch_count*out_i + in_i1, in->ch_count*out_i + in_i2, len1);
  552. if(len != len1)
  553. s->mix_2_1_f (out->ch[out_i]+off, in->ch[in_i1]+off, in->ch[in_i2]+off, s->native_matrix, in->ch_count*out_i + in_i1, in->ch_count*out_i + in_i2, len-len1);
  554. break;}
  555. default:
  556. if(s->int_sample_fmt == AV_SAMPLE_FMT_FLTP){
  557. for(i=0; i<len; i++){
  558. float v=0;
  559. for(j=0; j<s->matrix_ch[out_i][0]; j++){
  560. in_i= s->matrix_ch[out_i][1+j];
  561. v+= ((float*)in->ch[in_i])[i] * s->matrix_flt[out_i][in_i];
  562. }
  563. ((float*)out->ch[out_i])[i]= v;
  564. }
  565. }else if(s->int_sample_fmt == AV_SAMPLE_FMT_DBLP){
  566. for(i=0; i<len; i++){
  567. double v=0;
  568. for(j=0; j<s->matrix_ch[out_i][0]; j++){
  569. in_i= s->matrix_ch[out_i][1+j];
  570. v+= ((double*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
  571. }
  572. ((double*)out->ch[out_i])[i]= v;
  573. }
  574. }else{
  575. for(i=0; i<len; i++){
  576. int v=0;
  577. for(j=0; j<s->matrix_ch[out_i][0]; j++){
  578. in_i= s->matrix_ch[out_i][1+j];
  579. v+= ((int16_t*)in->ch[in_i])[i] * s->matrix32[out_i][in_i];
  580. }
  581. ((int16_t*)out->ch[out_i])[i]= (v + 16384)>>15;
  582. }
  583. }
  584. }
  585. }
  586. return 0;
  587. }