You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

552 lines
21KB

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