rematrix.c 19 KB

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