rematrix.c 27 KB

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