rematrix.c 29 KB

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