2
0

rematrix.c 23 KB

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