rematrix.c 28 KB

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