rematrix.c 19 KB

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