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.

565 lines
23KB

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