rematrix.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. #undef TEMPLATE_REMATRIX_S16
  32. #define TEMPLATE_REMATRIX_S32
  33. #include "rematrix_template.c"
  34. #undef TEMPLATE_REMATRIX_S32
  35. #define FRONT_LEFT 0
  36. #define FRONT_RIGHT 1
  37. #define FRONT_CENTER 2
  38. #define LOW_FREQUENCY 3
  39. #define BACK_LEFT 4
  40. #define BACK_RIGHT 5
  41. #define FRONT_LEFT_OF_CENTER 6
  42. #define FRONT_RIGHT_OF_CENTER 7
  43. #define BACK_CENTER 8
  44. #define SIDE_LEFT 9
  45. #define SIDE_RIGHT 10
  46. #define TOP_CENTER 11
  47. #define TOP_FRONT_LEFT 12
  48. #define TOP_FRONT_CENTER 13
  49. #define TOP_FRONT_RIGHT 14
  50. #define TOP_BACK_LEFT 15
  51. #define TOP_BACK_CENTER 16
  52. #define TOP_BACK_RIGHT 17
  53. #define NUM_NAMED_CHANNELS 18
  54. int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride)
  55. {
  56. int nb_in, nb_out, in, out;
  57. if (!s || s->in_convert) // s needs to be allocated but not initialized
  58. return AVERROR(EINVAL);
  59. memset(s->matrix, 0, sizeof(s->matrix));
  60. nb_in = av_get_channel_layout_nb_channels(s->user_in_ch_layout);
  61. nb_out = av_get_channel_layout_nb_channels(s->user_out_ch_layout);
  62. for (out = 0; out < nb_out; out++) {
  63. for (in = 0; in < nb_in; in++)
  64. s->matrix[out][in] = matrix[in];
  65. matrix += stride;
  66. }
  67. s->rematrix_custom = 1;
  68. return 0;
  69. }
  70. static int even(int64_t layout){
  71. if(!layout) return 1;
  72. if(layout&(layout-1)) return 1;
  73. return 0;
  74. }
  75. static int clean_layout(SwrContext *s, int64_t layout){
  76. if(layout && layout != AV_CH_FRONT_CENTER && !(layout&(layout-1))) {
  77. char buf[128];
  78. av_get_channel_layout_string(buf, sizeof(buf), -1, layout);
  79. av_log(s, AV_LOG_VERBOSE, "Treating %s as mono\n", buf);
  80. return AV_CH_FRONT_CENTER;
  81. }
  82. return layout;
  83. }
  84. static int sane_layout(int64_t layout){
  85. if(!(layout & AV_CH_LAYOUT_SURROUND)) // at least 1 front speaker
  86. return 0;
  87. if(!even(layout & (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT))) // no asymetric front
  88. return 0;
  89. if(!even(layout & (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT))) // no asymetric side
  90. return 0;
  91. if(!even(layout & (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)))
  92. return 0;
  93. if(!even(layout & (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)))
  94. return 0;
  95. if(av_get_channel_layout_nb_channels(layout) >= SWR_CH_MAX)
  96. return 0;
  97. return 1;
  98. }
  99. av_cold static int auto_matrix(SwrContext *s)
  100. {
  101. int i, j, out_i;
  102. double matrix[NUM_NAMED_CHANNELS][NUM_NAMED_CHANNELS]={{0}};
  103. int64_t unaccounted, in_ch_layout, out_ch_layout;
  104. double maxcoef=0;
  105. char buf[128];
  106. const int matrix_encoding = s->matrix_encoding;
  107. float maxval;
  108. in_ch_layout = clean_layout(s, s->in_ch_layout);
  109. out_ch_layout = clean_layout(s, s->out_ch_layout);
  110. if( out_ch_layout == AV_CH_LAYOUT_STEREO_DOWNMIX
  111. && (in_ch_layout & AV_CH_LAYOUT_STEREO_DOWNMIX) == 0
  112. )
  113. out_ch_layout = AV_CH_LAYOUT_STEREO;
  114. if( in_ch_layout == AV_CH_LAYOUT_STEREO_DOWNMIX
  115. && (out_ch_layout & AV_CH_LAYOUT_STEREO_DOWNMIX) == 0
  116. )
  117. in_ch_layout = AV_CH_LAYOUT_STEREO;
  118. if(!sane_layout(in_ch_layout)){
  119. av_get_channel_layout_string(buf, sizeof(buf), -1, s->in_ch_layout);
  120. av_log(s, AV_LOG_ERROR, "Input channel layout '%s' is not supported\n", buf);
  121. return AVERROR(EINVAL);
  122. }
  123. if(!sane_layout(out_ch_layout)){
  124. av_get_channel_layout_string(buf, sizeof(buf), -1, s->out_ch_layout);
  125. av_log(s, AV_LOG_ERROR, "Output channel layout '%s' is not supported\n", buf);
  126. return AVERROR(EINVAL);
  127. }
  128. memset(s->matrix, 0, sizeof(s->matrix));
  129. for(i=0; i<FF_ARRAY_ELEMS(matrix); i++){
  130. if(in_ch_layout & out_ch_layout & (1ULL<<i))
  131. matrix[i][i]= 1.0;
  132. }
  133. unaccounted= in_ch_layout & ~out_ch_layout;
  134. //FIXME implement dolby surround
  135. //FIXME implement full ac3
  136. if(unaccounted & AV_CH_FRONT_CENTER){
  137. if((out_ch_layout & AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO){
  138. if(in_ch_layout & AV_CH_LAYOUT_STEREO) {
  139. matrix[ FRONT_LEFT][FRONT_CENTER]+= s->clev;
  140. matrix[FRONT_RIGHT][FRONT_CENTER]+= s->clev;
  141. } else {
  142. matrix[ FRONT_LEFT][FRONT_CENTER]+= M_SQRT1_2;
  143. matrix[FRONT_RIGHT][FRONT_CENTER]+= M_SQRT1_2;
  144. }
  145. }else
  146. av_assert0(0);
  147. }
  148. if(unaccounted & AV_CH_LAYOUT_STEREO){
  149. if(out_ch_layout & AV_CH_FRONT_CENTER){
  150. matrix[FRONT_CENTER][ FRONT_LEFT]+= M_SQRT1_2;
  151. matrix[FRONT_CENTER][FRONT_RIGHT]+= M_SQRT1_2;
  152. if(in_ch_layout & AV_CH_FRONT_CENTER)
  153. matrix[FRONT_CENTER][ FRONT_CENTER] = s->clev*sqrt(2);
  154. }else
  155. av_assert0(0);
  156. }
  157. if(unaccounted & AV_CH_BACK_CENTER){
  158. if(out_ch_layout & AV_CH_BACK_LEFT){
  159. matrix[ BACK_LEFT][BACK_CENTER]+= M_SQRT1_2;
  160. matrix[BACK_RIGHT][BACK_CENTER]+= M_SQRT1_2;
  161. }else if(out_ch_layout & AV_CH_SIDE_LEFT){
  162. matrix[ SIDE_LEFT][BACK_CENTER]+= M_SQRT1_2;
  163. matrix[SIDE_RIGHT][BACK_CENTER]+= M_SQRT1_2;
  164. }else if(out_ch_layout & AV_CH_FRONT_LEFT){
  165. if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY ||
  166. matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
  167. if (unaccounted & (AV_CH_BACK_LEFT | AV_CH_SIDE_LEFT)) {
  168. matrix[FRONT_LEFT ][BACK_CENTER] -= s->slev * M_SQRT1_2;
  169. matrix[FRONT_RIGHT][BACK_CENTER] += s->slev * M_SQRT1_2;
  170. } else {
  171. matrix[FRONT_LEFT ][BACK_CENTER] -= s->slev;
  172. matrix[FRONT_RIGHT][BACK_CENTER] += s->slev;
  173. }
  174. } else {
  175. matrix[ FRONT_LEFT][BACK_CENTER]+= s->slev*M_SQRT1_2;
  176. matrix[FRONT_RIGHT][BACK_CENTER]+= s->slev*M_SQRT1_2;
  177. }
  178. }else if(out_ch_layout & AV_CH_FRONT_CENTER){
  179. matrix[ FRONT_CENTER][BACK_CENTER]+= s->slev*M_SQRT1_2;
  180. }else
  181. av_assert0(0);
  182. }
  183. if(unaccounted & AV_CH_BACK_LEFT){
  184. if(out_ch_layout & AV_CH_BACK_CENTER){
  185. matrix[BACK_CENTER][ BACK_LEFT]+= M_SQRT1_2;
  186. matrix[BACK_CENTER][BACK_RIGHT]+= M_SQRT1_2;
  187. }else if(out_ch_layout & AV_CH_SIDE_LEFT){
  188. if(in_ch_layout & AV_CH_SIDE_LEFT){
  189. matrix[ SIDE_LEFT][ BACK_LEFT]+= M_SQRT1_2;
  190. matrix[SIDE_RIGHT][BACK_RIGHT]+= M_SQRT1_2;
  191. }else{
  192. matrix[ SIDE_LEFT][ BACK_LEFT]+= 1.0;
  193. matrix[SIDE_RIGHT][BACK_RIGHT]+= 1.0;
  194. }
  195. }else if(out_ch_layout & AV_CH_FRONT_LEFT){
  196. if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
  197. matrix[FRONT_LEFT ][BACK_LEFT ] -= s->slev * M_SQRT1_2;
  198. matrix[FRONT_LEFT ][BACK_RIGHT] -= s->slev * M_SQRT1_2;
  199. matrix[FRONT_RIGHT][BACK_LEFT ] += s->slev * M_SQRT1_2;
  200. matrix[FRONT_RIGHT][BACK_RIGHT] += s->slev * M_SQRT1_2;
  201. } else if (matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
  202. matrix[FRONT_LEFT ][BACK_LEFT ] -= s->slev * SQRT3_2;
  203. matrix[FRONT_LEFT ][BACK_RIGHT] -= s->slev * M_SQRT1_2;
  204. matrix[FRONT_RIGHT][BACK_LEFT ] += s->slev * M_SQRT1_2;
  205. matrix[FRONT_RIGHT][BACK_RIGHT] += s->slev * SQRT3_2;
  206. } else {
  207. matrix[ FRONT_LEFT][ BACK_LEFT] += s->slev;
  208. matrix[FRONT_RIGHT][BACK_RIGHT] += s->slev;
  209. }
  210. }else if(out_ch_layout & AV_CH_FRONT_CENTER){
  211. matrix[ FRONT_CENTER][BACK_LEFT ]+= s->slev*M_SQRT1_2;
  212. matrix[ FRONT_CENTER][BACK_RIGHT]+= s->slev*M_SQRT1_2;
  213. }else
  214. av_assert0(0);
  215. }
  216. if(unaccounted & AV_CH_SIDE_LEFT){
  217. if(out_ch_layout & AV_CH_BACK_LEFT){
  218. /* if back channels do not exist in the input, just copy side
  219. channels to back channels, otherwise mix side into back */
  220. if (in_ch_layout & AV_CH_BACK_LEFT) {
  221. matrix[BACK_LEFT ][SIDE_LEFT ] += M_SQRT1_2;
  222. matrix[BACK_RIGHT][SIDE_RIGHT] += M_SQRT1_2;
  223. } else {
  224. matrix[BACK_LEFT ][SIDE_LEFT ] += 1.0;
  225. matrix[BACK_RIGHT][SIDE_RIGHT] += 1.0;
  226. }
  227. }else if(out_ch_layout & AV_CH_BACK_CENTER){
  228. matrix[BACK_CENTER][ SIDE_LEFT]+= M_SQRT1_2;
  229. matrix[BACK_CENTER][SIDE_RIGHT]+= M_SQRT1_2;
  230. }else if(out_ch_layout & AV_CH_FRONT_LEFT){
  231. if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
  232. matrix[FRONT_LEFT ][SIDE_LEFT ] -= s->slev * M_SQRT1_2;
  233. matrix[FRONT_LEFT ][SIDE_RIGHT] -= s->slev * M_SQRT1_2;
  234. matrix[FRONT_RIGHT][SIDE_LEFT ] += s->slev * M_SQRT1_2;
  235. matrix[FRONT_RIGHT][SIDE_RIGHT] += s->slev * M_SQRT1_2;
  236. } else if (matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
  237. matrix[FRONT_LEFT ][SIDE_LEFT ] -= s->slev * SQRT3_2;
  238. matrix[FRONT_LEFT ][SIDE_RIGHT] -= s->slev * M_SQRT1_2;
  239. matrix[FRONT_RIGHT][SIDE_LEFT ] += s->slev * M_SQRT1_2;
  240. matrix[FRONT_RIGHT][SIDE_RIGHT] += s->slev * SQRT3_2;
  241. } else {
  242. matrix[ FRONT_LEFT][ SIDE_LEFT] += s->slev;
  243. matrix[FRONT_RIGHT][SIDE_RIGHT] += s->slev;
  244. }
  245. }else if(out_ch_layout & AV_CH_FRONT_CENTER){
  246. matrix[ FRONT_CENTER][SIDE_LEFT ]+= s->slev*M_SQRT1_2;
  247. matrix[ FRONT_CENTER][SIDE_RIGHT]+= s->slev*M_SQRT1_2;
  248. }else
  249. av_assert0(0);
  250. }
  251. if(unaccounted & AV_CH_FRONT_LEFT_OF_CENTER){
  252. if(out_ch_layout & AV_CH_FRONT_LEFT){
  253. matrix[ FRONT_LEFT][ FRONT_LEFT_OF_CENTER]+= 1.0;
  254. matrix[FRONT_RIGHT][FRONT_RIGHT_OF_CENTER]+= 1.0;
  255. }else if(out_ch_layout & AV_CH_FRONT_CENTER){
  256. matrix[ FRONT_CENTER][ FRONT_LEFT_OF_CENTER]+= M_SQRT1_2;
  257. matrix[ FRONT_CENTER][FRONT_RIGHT_OF_CENTER]+= M_SQRT1_2;
  258. }else
  259. av_assert0(0);
  260. }
  261. /* mix LFE into front left/right or center */
  262. if (unaccounted & AV_CH_LOW_FREQUENCY) {
  263. if (out_ch_layout & AV_CH_FRONT_CENTER) {
  264. matrix[FRONT_CENTER][LOW_FREQUENCY] += s->lfe_mix_level;
  265. } else if (out_ch_layout & AV_CH_FRONT_LEFT) {
  266. matrix[FRONT_LEFT ][LOW_FREQUENCY] += s->lfe_mix_level * M_SQRT1_2;
  267. matrix[FRONT_RIGHT][LOW_FREQUENCY] += s->lfe_mix_level * M_SQRT1_2;
  268. } else
  269. av_assert0(0);
  270. }
  271. for(out_i=i=0; i<64; i++){
  272. double sum=0;
  273. int in_i=0;
  274. if((out_ch_layout & (1ULL<<i)) == 0)
  275. continue;
  276. for(j=0; j<64; j++){
  277. if((in_ch_layout & (1ULL<<j)) == 0)
  278. continue;
  279. if (i < FF_ARRAY_ELEMS(matrix) && j < FF_ARRAY_ELEMS(matrix[0]))
  280. s->matrix[out_i][in_i]= matrix[i][j];
  281. else
  282. s->matrix[out_i][in_i]= i == j && (in_ch_layout & out_ch_layout & (1ULL<<i));
  283. sum += fabs(s->matrix[out_i][in_i]);
  284. in_i++;
  285. }
  286. maxcoef= FFMAX(maxcoef, sum);
  287. out_i++;
  288. }
  289. if(s->rematrix_volume < 0)
  290. maxcoef = -s->rematrix_volume;
  291. if (s->rematrix_maxval > 0) {
  292. maxval = s->rematrix_maxval;
  293. } else if ( av_get_packed_sample_fmt(s->out_sample_fmt) < AV_SAMPLE_FMT_FLT
  294. || av_get_packed_sample_fmt(s->int_sample_fmt) < AV_SAMPLE_FMT_FLT) {
  295. maxval = 1.0;
  296. } else
  297. maxval = INT_MAX;
  298. if(maxcoef > maxval || s->rematrix_volume < 0){
  299. maxcoef /= maxval;
  300. for(i=0; i<SWR_CH_MAX; i++)
  301. for(j=0; j<SWR_CH_MAX; j++){
  302. s->matrix[i][j] /= maxcoef;
  303. }
  304. }
  305. if(s->rematrix_volume > 0){
  306. for(i=0; i<SWR_CH_MAX; i++)
  307. for(j=0; j<SWR_CH_MAX; j++){
  308. s->matrix[i][j] *= s->rematrix_volume;
  309. }
  310. }
  311. for(i=0; i<av_get_channel_layout_nb_channels(out_ch_layout); i++){
  312. for(j=0; j<av_get_channel_layout_nb_channels(in_ch_layout); j++){
  313. av_log(NULL, AV_LOG_DEBUG, "%f ", s->matrix[i][j]);
  314. }
  315. av_log(NULL, AV_LOG_DEBUG, "\n");
  316. }
  317. return 0;
  318. }
  319. av_cold int swri_rematrix_init(SwrContext *s){
  320. int i, j;
  321. int nb_in = av_get_channel_layout_nb_channels(s->in_ch_layout);
  322. int nb_out = av_get_channel_layout_nb_channels(s->out_ch_layout);
  323. s->mix_any_f = NULL;
  324. if (!s->rematrix_custom) {
  325. int r = auto_matrix(s);
  326. if (r)
  327. return r;
  328. }
  329. if (s->midbuf.fmt == AV_SAMPLE_FMT_S16P){
  330. s->native_matrix = av_calloc(nb_in * nb_out, sizeof(int));
  331. s->native_one = av_mallocz(sizeof(int));
  332. for (i = 0; i < nb_out; i++)
  333. for (j = 0; j < nb_in; j++)
  334. ((int*)s->native_matrix)[i * nb_in + j] = lrintf(s->matrix[i][j] * 32768);
  335. *((int*)s->native_one) = 32768;
  336. s->mix_1_1_f = (mix_1_1_func_type*)copy_s16;
  337. s->mix_2_1_f = (mix_2_1_func_type*)sum2_s16;
  338. s->mix_any_f = (mix_any_func_type*)get_mix_any_func_s16(s);
  339. }else if(s->midbuf.fmt == AV_SAMPLE_FMT_FLTP){
  340. s->native_matrix = av_calloc(nb_in * nb_out, sizeof(float));
  341. s->native_one = av_mallocz(sizeof(float));
  342. for (i = 0; i < nb_out; i++)
  343. for (j = 0; j < nb_in; j++)
  344. ((float*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
  345. *((float*)s->native_one) = 1.0;
  346. s->mix_1_1_f = (mix_1_1_func_type*)copy_float;
  347. s->mix_2_1_f = (mix_2_1_func_type*)sum2_float;
  348. s->mix_any_f = (mix_any_func_type*)get_mix_any_func_float(s);
  349. }else if(s->midbuf.fmt == AV_SAMPLE_FMT_DBLP){
  350. s->native_matrix = av_calloc(nb_in * nb_out, sizeof(double));
  351. s->native_one = av_mallocz(sizeof(double));
  352. for (i = 0; i < nb_out; i++)
  353. for (j = 0; j < nb_in; j++)
  354. ((double*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
  355. *((double*)s->native_one) = 1.0;
  356. s->mix_1_1_f = (mix_1_1_func_type*)copy_double;
  357. s->mix_2_1_f = (mix_2_1_func_type*)sum2_double;
  358. s->mix_any_f = (mix_any_func_type*)get_mix_any_func_double(s);
  359. }else if(s->midbuf.fmt == AV_SAMPLE_FMT_S32P){
  360. // Only for dithering currently
  361. // s->native_matrix = av_calloc(nb_in * nb_out, sizeof(double));
  362. s->native_one = av_mallocz(sizeof(int));
  363. // for (i = 0; i < nb_out; i++)
  364. // for (j = 0; j < nb_in; j++)
  365. // ((double*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
  366. *((int*)s->native_one) = 32768;
  367. s->mix_1_1_f = (mix_1_1_func_type*)copy_s32;
  368. s->mix_2_1_f = (mix_2_1_func_type*)sum2_s32;
  369. s->mix_any_f = (mix_any_func_type*)get_mix_any_func_s32(s);
  370. }else
  371. av_assert0(0);
  372. //FIXME quantize for integeres
  373. for (i = 0; i < SWR_CH_MAX; i++) {
  374. int ch_in=0;
  375. for (j = 0; j < SWR_CH_MAX; j++) {
  376. s->matrix32[i][j]= lrintf(s->matrix[i][j] * 32768);
  377. if(s->matrix[i][j])
  378. s->matrix_ch[i][++ch_in]= j;
  379. }
  380. s->matrix_ch[i][0]= ch_in;
  381. }
  382. if(HAVE_YASM && HAVE_MMX)
  383. return swri_rematrix_init_x86(s);
  384. return 0;
  385. }
  386. av_cold void swri_rematrix_free(SwrContext *s){
  387. av_freep(&s->native_matrix);
  388. av_freep(&s->native_one);
  389. av_freep(&s->native_simd_matrix);
  390. av_freep(&s->native_simd_one);
  391. }
  392. int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy){
  393. int out_i, in_i, i, j;
  394. int len1 = 0;
  395. int off = 0;
  396. if(s->mix_any_f) {
  397. s->mix_any_f(out->ch, (const uint8_t **)in->ch, s->native_matrix, len);
  398. return 0;
  399. }
  400. if(s->mix_2_1_simd || s->mix_1_1_simd){
  401. len1= len&~15;
  402. off = len1 * out->bps;
  403. }
  404. av_assert0(!s->out_ch_layout || out->ch_count == av_get_channel_layout_nb_channels(s->out_ch_layout));
  405. av_assert0(!s-> in_ch_layout || in ->ch_count == av_get_channel_layout_nb_channels(s-> in_ch_layout));
  406. for(out_i=0; out_i<out->ch_count; out_i++){
  407. switch(s->matrix_ch[out_i][0]){
  408. case 0:
  409. if(mustcopy)
  410. memset(out->ch[out_i], 0, len * av_get_bytes_per_sample(s->int_sample_fmt));
  411. break;
  412. case 1:
  413. in_i= s->matrix_ch[out_i][1];
  414. if(s->matrix[out_i][in_i]!=1.0){
  415. if(s->mix_1_1_simd && len1)
  416. 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);
  417. if(len != len1)
  418. 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);
  419. }else if(mustcopy){
  420. memcpy(out->ch[out_i], in->ch[in_i], len*out->bps);
  421. }else{
  422. out->ch[out_i]= in->ch[in_i];
  423. }
  424. break;
  425. case 2: {
  426. int in_i1 = s->matrix_ch[out_i][1];
  427. int in_i2 = s->matrix_ch[out_i][2];
  428. if(s->mix_2_1_simd && len1)
  429. 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);
  430. else
  431. 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);
  432. if(len != len1)
  433. 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);
  434. break;}
  435. default:
  436. if(s->int_sample_fmt == AV_SAMPLE_FMT_FLTP){
  437. for(i=0; i<len; i++){
  438. float v=0;
  439. for(j=0; j<s->matrix_ch[out_i][0]; j++){
  440. in_i= s->matrix_ch[out_i][1+j];
  441. v+= ((float*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
  442. }
  443. ((float*)out->ch[out_i])[i]= v;
  444. }
  445. }else if(s->int_sample_fmt == AV_SAMPLE_FMT_DBLP){
  446. for(i=0; i<len; i++){
  447. double v=0;
  448. for(j=0; j<s->matrix_ch[out_i][0]; j++){
  449. in_i= s->matrix_ch[out_i][1+j];
  450. v+= ((double*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
  451. }
  452. ((double*)out->ch[out_i])[i]= v;
  453. }
  454. }else{
  455. for(i=0; i<len; i++){
  456. int v=0;
  457. for(j=0; j<s->matrix_ch[out_i][0]; j++){
  458. in_i= s->matrix_ch[out_i][1+j];
  459. v+= ((int16_t*)in->ch[in_i])[i] * s->matrix32[out_i][in_i];
  460. }
  461. ((int16_t*)out->ch[out_i])[i]= (v + 16384)>>15;
  462. }
  463. }
  464. }
  465. }
  466. return 0;
  467. }