rematrix.c 23 KB

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