rematrix.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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;
  342. for (i = 0; i < FF_ARRAY_ELEMS(s->matrix[0])*FF_ARRAY_ELEMS(s->matrix[0]); i++)
  343. s->matrix_flt[0][i] = s->matrix[0][i];
  344. }
  345. return ret;
  346. }
  347. av_cold int swri_rematrix_init(SwrContext *s){
  348. int i, j;
  349. int nb_in = av_get_channel_layout_nb_channels(s->in_ch_layout);
  350. int nb_out = av_get_channel_layout_nb_channels(s->out_ch_layout);
  351. s->mix_any_f = NULL;
  352. if (!s->rematrix_custom) {
  353. int r = auto_matrix(s);
  354. if (r)
  355. return r;
  356. }
  357. if (s->midbuf.fmt == AV_SAMPLE_FMT_S16P){
  358. int maxsum = 0;
  359. s->native_matrix = av_calloc(nb_in * nb_out, sizeof(int));
  360. s->native_one = av_mallocz(sizeof(int));
  361. if (!s->native_matrix || !s->native_one)
  362. return AVERROR(ENOMEM);
  363. for (i = 0; i < nb_out; i++) {
  364. double rem = 0;
  365. int sum = 0;
  366. for (j = 0; j < nb_in; j++) {
  367. double target = s->matrix[i][j] * 32768 + rem;
  368. ((int*)s->native_matrix)[i * nb_in + j] = lrintf(target);
  369. rem += target - ((int*)s->native_matrix)[i * nb_in + j];
  370. sum += FFABS(((int*)s->native_matrix)[i * nb_in + j]);
  371. }
  372. maxsum = FFMAX(maxsum, sum);
  373. }
  374. *((int*)s->native_one) = 32768;
  375. if (maxsum <= 32768) {
  376. s->mix_1_1_f = (mix_1_1_func_type*)copy_s16;
  377. s->mix_2_1_f = (mix_2_1_func_type*)sum2_s16;
  378. s->mix_any_f = (mix_any_func_type*)get_mix_any_func_s16(s);
  379. } else {
  380. s->mix_1_1_f = (mix_1_1_func_type*)copy_clip_s16;
  381. s->mix_2_1_f = (mix_2_1_func_type*)sum2_clip_s16;
  382. s->mix_any_f = (mix_any_func_type*)get_mix_any_func_clip_s16(s);
  383. }
  384. }else if(s->midbuf.fmt == AV_SAMPLE_FMT_FLTP){
  385. s->native_matrix = av_calloc(nb_in * nb_out, sizeof(float));
  386. s->native_one = av_mallocz(sizeof(float));
  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. ((float*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
  392. *((float*)s->native_one) = 1.0;
  393. s->mix_1_1_f = (mix_1_1_func_type*)copy_float;
  394. s->mix_2_1_f = (mix_2_1_func_type*)sum2_float;
  395. s->mix_any_f = (mix_any_func_type*)get_mix_any_func_float(s);
  396. }else if(s->midbuf.fmt == AV_SAMPLE_FMT_DBLP){
  397. s->native_matrix = av_calloc(nb_in * nb_out, sizeof(double));
  398. s->native_one = av_mallocz(sizeof(double));
  399. if (!s->native_matrix || !s->native_one)
  400. return AVERROR(ENOMEM);
  401. for (i = 0; i < nb_out; i++)
  402. for (j = 0; j < nb_in; j++)
  403. ((double*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
  404. *((double*)s->native_one) = 1.0;
  405. s->mix_1_1_f = (mix_1_1_func_type*)copy_double;
  406. s->mix_2_1_f = (mix_2_1_func_type*)sum2_double;
  407. s->mix_any_f = (mix_any_func_type*)get_mix_any_func_double(s);
  408. }else if(s->midbuf.fmt == AV_SAMPLE_FMT_S32P){
  409. // Only for dithering currently
  410. // s->native_matrix = av_calloc(nb_in * nb_out, sizeof(double));
  411. s->native_one = av_mallocz(sizeof(int));
  412. if (!s->native_one)
  413. return AVERROR(ENOMEM);
  414. // for (i = 0; i < nb_out; i++)
  415. // for (j = 0; j < nb_in; j++)
  416. // ((double*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
  417. *((int*)s->native_one) = 32768;
  418. s->mix_1_1_f = (mix_1_1_func_type*)copy_s32;
  419. s->mix_2_1_f = (mix_2_1_func_type*)sum2_s32;
  420. s->mix_any_f = (mix_any_func_type*)get_mix_any_func_s32(s);
  421. }else
  422. av_assert0(0);
  423. //FIXME quantize for integeres
  424. for (i = 0; i < SWR_CH_MAX; i++) {
  425. int ch_in=0;
  426. for (j = 0; j < SWR_CH_MAX; j++) {
  427. s->matrix32[i][j]= lrintf(s->matrix[i][j] * 32768);
  428. if(s->matrix[i][j])
  429. s->matrix_ch[i][++ch_in]= j;
  430. }
  431. s->matrix_ch[i][0]= ch_in;
  432. }
  433. if(HAVE_YASM && HAVE_MMX)
  434. return swri_rematrix_init_x86(s);
  435. return 0;
  436. }
  437. av_cold void swri_rematrix_free(SwrContext *s){
  438. av_freep(&s->native_matrix);
  439. av_freep(&s->native_one);
  440. av_freep(&s->native_simd_matrix);
  441. av_freep(&s->native_simd_one);
  442. }
  443. int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy){
  444. int out_i, in_i, i, j;
  445. int len1 = 0;
  446. int off = 0;
  447. if(s->mix_any_f) {
  448. s->mix_any_f(out->ch, (const uint8_t **)in->ch, s->native_matrix, len);
  449. return 0;
  450. }
  451. if(s->mix_2_1_simd || s->mix_1_1_simd){
  452. len1= len&~15;
  453. off = len1 * out->bps;
  454. }
  455. av_assert0(!s->out_ch_layout || out->ch_count == av_get_channel_layout_nb_channels(s->out_ch_layout));
  456. av_assert0(!s-> in_ch_layout || in ->ch_count == av_get_channel_layout_nb_channels(s-> in_ch_layout));
  457. for(out_i=0; out_i<out->ch_count; out_i++){
  458. switch(s->matrix_ch[out_i][0]){
  459. case 0:
  460. if(mustcopy)
  461. memset(out->ch[out_i], 0, len * av_get_bytes_per_sample(s->int_sample_fmt));
  462. break;
  463. case 1:
  464. in_i= s->matrix_ch[out_i][1];
  465. if(s->matrix[out_i][in_i]!=1.0){
  466. if(s->mix_1_1_simd && len1)
  467. 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);
  468. if(len != len1)
  469. 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);
  470. }else if(mustcopy){
  471. memcpy(out->ch[out_i], in->ch[in_i], len*out->bps);
  472. }else{
  473. out->ch[out_i]= in->ch[in_i];
  474. }
  475. break;
  476. case 2: {
  477. int in_i1 = s->matrix_ch[out_i][1];
  478. int in_i2 = s->matrix_ch[out_i][2];
  479. if(s->mix_2_1_simd && len1)
  480. 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);
  481. else
  482. 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);
  483. if(len != len1)
  484. 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);
  485. break;}
  486. default:
  487. if(s->int_sample_fmt == AV_SAMPLE_FMT_FLTP){
  488. for(i=0; i<len; i++){
  489. float v=0;
  490. for(j=0; j<s->matrix_ch[out_i][0]; j++){
  491. in_i= s->matrix_ch[out_i][1+j];
  492. v+= ((float*)in->ch[in_i])[i] * s->matrix_flt[out_i][in_i];
  493. }
  494. ((float*)out->ch[out_i])[i]= v;
  495. }
  496. }else if(s->int_sample_fmt == AV_SAMPLE_FMT_DBLP){
  497. for(i=0; i<len; i++){
  498. double 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+= ((double*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
  502. }
  503. ((double*)out->ch[out_i])[i]= v;
  504. }
  505. }else{
  506. for(i=0; i<len; i++){
  507. int 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+= ((int16_t*)in->ch[in_i])[i] * s->matrix32[out_i][in_i];
  511. }
  512. ((int16_t*)out->ch[out_i])[i]= (v + 16384)>>15;
  513. }
  514. }
  515. }
  516. }
  517. return 0;
  518. }