rematrix.c 21 KB

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