rematrix.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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. #include "libavutil/mem.h"
  24. #define TEMPLATE_REMATRIX_FLT
  25. #include "rematrix_template.c"
  26. #undef TEMPLATE_REMATRIX_FLT
  27. #define TEMPLATE_REMATRIX_DBL
  28. #include "rematrix_template.c"
  29. #undef TEMPLATE_REMATRIX_DBL
  30. #define TEMPLATE_REMATRIX_S16
  31. #include "rematrix_template.c"
  32. #define TEMPLATE_CLIP
  33. #include "rematrix_template.c"
  34. #undef TEMPLATE_CLIP
  35. #undef TEMPLATE_REMATRIX_S16
  36. #define TEMPLATE_REMATRIX_S32
  37. #include "rematrix_template.c"
  38. #undef TEMPLATE_REMATRIX_S32
  39. #define FRONT_LEFT 0
  40. #define FRONT_RIGHT 1
  41. #define FRONT_CENTER 2
  42. #define LOW_FREQUENCY 3
  43. #define BACK_LEFT 4
  44. #define BACK_RIGHT 5
  45. #define FRONT_LEFT_OF_CENTER 6
  46. #define FRONT_RIGHT_OF_CENTER 7
  47. #define BACK_CENTER 8
  48. #define SIDE_LEFT 9
  49. #define SIDE_RIGHT 10
  50. #define TOP_CENTER 11
  51. #define TOP_FRONT_LEFT 12
  52. #define TOP_FRONT_CENTER 13
  53. #define TOP_FRONT_RIGHT 14
  54. #define TOP_BACK_LEFT 15
  55. #define TOP_BACK_CENTER 16
  56. #define TOP_BACK_RIGHT 17
  57. #define NUM_NAMED_CHANNELS 18
  58. int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride)
  59. {
  60. int nb_in, nb_out, in, out;
  61. if (!s || s->in_convert) // s needs to be allocated but not initialized
  62. return AVERROR(EINVAL);
  63. memset(s->matrix, 0, sizeof(s->matrix));
  64. memset(s->matrix_flt, 0, sizeof(s->matrix_flt));
  65. nb_in = s->user_in_chlayout.nb_channels;
  66. nb_out = s->user_out_chlayout.nb_channels;
  67. for (out = 0; out < nb_out; out++) {
  68. for (in = 0; in < nb_in; in++)
  69. s->matrix_flt[out][in] = s->matrix[out][in] = matrix[in];
  70. matrix += stride;
  71. }
  72. s->rematrix_custom = 1;
  73. return 0;
  74. }
  75. static int even(int64_t layout){
  76. if(!layout) return 1;
  77. if(layout&(layout-1)) return 1;
  78. return 0;
  79. }
  80. static int clean_layout(AVChannelLayout *out, const AVChannelLayout *in, void *s)
  81. {
  82. int ret = 0;
  83. if (av_channel_layout_index_from_channel(in, AV_CHAN_FRONT_CENTER) < 0 && in->nb_channels == 1) {
  84. char buf[128];
  85. av_channel_layout_describe(in, buf, sizeof(buf));
  86. av_log(s, AV_LOG_VERBOSE, "Treating %s as mono\n", buf);
  87. *out = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
  88. } else
  89. ret = av_channel_layout_copy(out, in);
  90. return ret;
  91. }
  92. static int sane_layout(AVChannelLayout *ch_layout) {
  93. if(ch_layout->nb_channels >= SWR_CH_MAX)
  94. return 0;
  95. if(ch_layout->order == AV_CHANNEL_ORDER_CUSTOM)
  96. for (int i = 0; i < ch_layout->nb_channels; i++) {
  97. if (ch_layout->u.map[i].id >= 64)
  98. return 0;
  99. }
  100. else if (ch_layout->order != AV_CHANNEL_ORDER_NATIVE)
  101. return 0;
  102. if(!av_channel_layout_subset(ch_layout, AV_CH_LAYOUT_SURROUND)) // at least 1 front speaker
  103. return 0;
  104. if(!even(av_channel_layout_subset(ch_layout, (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT)))) // no asymetric front
  105. return 0;
  106. if(!even(av_channel_layout_subset(ch_layout, (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)))) // no asymetric side
  107. return 0;
  108. if(!even(av_channel_layout_subset(ch_layout, (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT))))
  109. return 0;
  110. if(!even(av_channel_layout_subset(ch_layout, (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER))))
  111. return 0;
  112. if(!even(av_channel_layout_subset(ch_layout, (AV_CH_TOP_FRONT_LEFT | AV_CH_TOP_FRONT_RIGHT))))
  113. return 0;
  114. return 1;
  115. }
  116. static void build_matrix(const AVChannelLayout *in_ch_layout, const AVChannelLayout *out_ch_layout,
  117. double center_mix_level, double surround_mix_level,
  118. double lfe_mix_level, double maxval, double rematrix_volume, double *matrix_param,
  119. ptrdiff_t stride, enum AVMatrixEncoding matrix_encoding)
  120. {
  121. double matrix[NUM_NAMED_CHANNELS][NUM_NAMED_CHANNELS]={{0}};
  122. uint64_t unaccounted = av_channel_layout_subset(in_ch_layout, UINT64_MAX) &
  123. ~av_channel_layout_subset(out_ch_layout, UINT64_MAX);
  124. double maxcoef=0;
  125. int i, j;
  126. for(i=0; i<FF_ARRAY_ELEMS(matrix); i++){
  127. if( av_channel_layout_index_from_channel(in_ch_layout, i) >= 0
  128. && av_channel_layout_index_from_channel(out_ch_layout, i) >= 0)
  129. matrix[i][i]= 1.0;
  130. }
  131. //FIXME implement dolby surround
  132. //FIXME implement full ac3
  133. if(unaccounted & AV_CH_FRONT_CENTER){
  134. if (av_channel_layout_subset(out_ch_layout, AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO) {
  135. if (av_channel_layout_subset(in_ch_layout, AV_CH_LAYOUT_STEREO)) {
  136. matrix[ FRONT_LEFT][FRONT_CENTER]+= center_mix_level;
  137. matrix[FRONT_RIGHT][FRONT_CENTER]+= center_mix_level;
  138. } else {
  139. matrix[ FRONT_LEFT][FRONT_CENTER]+= M_SQRT1_2;
  140. matrix[FRONT_RIGHT][FRONT_CENTER]+= M_SQRT1_2;
  141. }
  142. }else
  143. av_assert0(0);
  144. }
  145. if(unaccounted & AV_CH_LAYOUT_STEREO){
  146. if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
  147. matrix[FRONT_CENTER][ FRONT_LEFT]+= M_SQRT1_2;
  148. matrix[FRONT_CENTER][FRONT_RIGHT]+= M_SQRT1_2;
  149. if (av_channel_layout_index_from_channel(in_ch_layout, AV_CHAN_FRONT_CENTER) >= 0)
  150. matrix[FRONT_CENTER][ FRONT_CENTER] = center_mix_level*sqrt(2);
  151. }else
  152. av_assert0(0);
  153. }
  154. if(unaccounted & AV_CH_BACK_CENTER){
  155. if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_BACK_LEFT) >= 0) {
  156. matrix[ BACK_LEFT][BACK_CENTER]+= M_SQRT1_2;
  157. matrix[BACK_RIGHT][BACK_CENTER]+= M_SQRT1_2;
  158. } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_SIDE_LEFT) >= 0) {
  159. matrix[ SIDE_LEFT][BACK_CENTER]+= M_SQRT1_2;
  160. matrix[SIDE_RIGHT][BACK_CENTER]+= M_SQRT1_2;
  161. } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
  162. if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY ||
  163. matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
  164. if (unaccounted & (AV_CH_BACK_LEFT | AV_CH_SIDE_LEFT)) {
  165. matrix[FRONT_LEFT ][BACK_CENTER] -= surround_mix_level * M_SQRT1_2;
  166. matrix[FRONT_RIGHT][BACK_CENTER] += surround_mix_level * M_SQRT1_2;
  167. } else {
  168. matrix[FRONT_LEFT ][BACK_CENTER] -= surround_mix_level;
  169. matrix[FRONT_RIGHT][BACK_CENTER] += surround_mix_level;
  170. }
  171. } else {
  172. matrix[ FRONT_LEFT][BACK_CENTER]+= surround_mix_level * M_SQRT1_2;
  173. matrix[FRONT_RIGHT][BACK_CENTER]+= surround_mix_level * M_SQRT1_2;
  174. }
  175. } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
  176. matrix[ FRONT_CENTER][BACK_CENTER]+= surround_mix_level * M_SQRT1_2;
  177. }else
  178. av_assert0(0);
  179. }
  180. if(unaccounted & AV_CH_BACK_LEFT){
  181. if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_BACK_CENTER) >= 0) {
  182. matrix[BACK_CENTER][ BACK_LEFT]+= M_SQRT1_2;
  183. matrix[BACK_CENTER][BACK_RIGHT]+= M_SQRT1_2;
  184. } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_SIDE_LEFT) >= 0) {
  185. if (av_channel_layout_index_from_channel(in_ch_layout, AV_CHAN_SIDE_LEFT) >= 0) {
  186. matrix[ SIDE_LEFT][ BACK_LEFT]+= M_SQRT1_2;
  187. matrix[SIDE_RIGHT][BACK_RIGHT]+= M_SQRT1_2;
  188. }else{
  189. matrix[ SIDE_LEFT][ BACK_LEFT]+= 1.0;
  190. matrix[SIDE_RIGHT][BACK_RIGHT]+= 1.0;
  191. }
  192. } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
  193. if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
  194. matrix[FRONT_LEFT ][BACK_LEFT ] -= surround_mix_level * M_SQRT1_2;
  195. matrix[FRONT_LEFT ][BACK_RIGHT] -= surround_mix_level * M_SQRT1_2;
  196. matrix[FRONT_RIGHT][BACK_LEFT ] += surround_mix_level * M_SQRT1_2;
  197. matrix[FRONT_RIGHT][BACK_RIGHT] += surround_mix_level * M_SQRT1_2;
  198. } else if (matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
  199. matrix[FRONT_LEFT ][BACK_LEFT ] -= surround_mix_level * SQRT3_2;
  200. matrix[FRONT_LEFT ][BACK_RIGHT] -= surround_mix_level * M_SQRT1_2;
  201. matrix[FRONT_RIGHT][BACK_LEFT ] += surround_mix_level * M_SQRT1_2;
  202. matrix[FRONT_RIGHT][BACK_RIGHT] += surround_mix_level * SQRT3_2;
  203. } else {
  204. matrix[ FRONT_LEFT][ BACK_LEFT] += surround_mix_level;
  205. matrix[FRONT_RIGHT][BACK_RIGHT] += surround_mix_level;
  206. }
  207. } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
  208. matrix[ FRONT_CENTER][BACK_LEFT ]+= surround_mix_level*M_SQRT1_2;
  209. matrix[ FRONT_CENTER][BACK_RIGHT]+= surround_mix_level*M_SQRT1_2;
  210. }else
  211. av_assert0(0);
  212. }
  213. if(unaccounted & AV_CH_SIDE_LEFT){
  214. if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_BACK_LEFT) >= 0) {
  215. /* if back channels do not exist in the input, just copy side
  216. channels to back channels, otherwise mix side into back */
  217. if (av_channel_layout_index_from_channel(in_ch_layout, AV_CHAN_BACK_LEFT) >= 0) {
  218. matrix[BACK_LEFT ][SIDE_LEFT ] += M_SQRT1_2;
  219. matrix[BACK_RIGHT][SIDE_RIGHT] += M_SQRT1_2;
  220. } else {
  221. matrix[BACK_LEFT ][SIDE_LEFT ] += 1.0;
  222. matrix[BACK_RIGHT][SIDE_RIGHT] += 1.0;
  223. }
  224. } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_BACK_CENTER) >= 0) {
  225. matrix[BACK_CENTER][ SIDE_LEFT]+= M_SQRT1_2;
  226. matrix[BACK_CENTER][SIDE_RIGHT]+= M_SQRT1_2;
  227. } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
  228. if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
  229. matrix[FRONT_LEFT ][SIDE_LEFT ] -= surround_mix_level * M_SQRT1_2;
  230. matrix[FRONT_LEFT ][SIDE_RIGHT] -= surround_mix_level * M_SQRT1_2;
  231. matrix[FRONT_RIGHT][SIDE_LEFT ] += surround_mix_level * M_SQRT1_2;
  232. matrix[FRONT_RIGHT][SIDE_RIGHT] += surround_mix_level * M_SQRT1_2;
  233. } else if (matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
  234. matrix[FRONT_LEFT ][SIDE_LEFT ] -= surround_mix_level * SQRT3_2;
  235. matrix[FRONT_LEFT ][SIDE_RIGHT] -= surround_mix_level * M_SQRT1_2;
  236. matrix[FRONT_RIGHT][SIDE_LEFT ] += surround_mix_level * M_SQRT1_2;
  237. matrix[FRONT_RIGHT][SIDE_RIGHT] += surround_mix_level * SQRT3_2;
  238. } else {
  239. matrix[ FRONT_LEFT][ SIDE_LEFT] += surround_mix_level;
  240. matrix[FRONT_RIGHT][SIDE_RIGHT] += surround_mix_level;
  241. }
  242. } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
  243. matrix[ FRONT_CENTER][SIDE_LEFT ]+= surround_mix_level * M_SQRT1_2;
  244. matrix[ FRONT_CENTER][SIDE_RIGHT]+= surround_mix_level * M_SQRT1_2;
  245. }else
  246. av_assert0(0);
  247. }
  248. if(unaccounted & AV_CH_FRONT_LEFT_OF_CENTER){
  249. if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
  250. matrix[ FRONT_LEFT][ FRONT_LEFT_OF_CENTER]+= 1.0;
  251. matrix[FRONT_RIGHT][FRONT_RIGHT_OF_CENTER]+= 1.0;
  252. } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
  253. matrix[ FRONT_CENTER][ FRONT_LEFT_OF_CENTER]+= M_SQRT1_2;
  254. matrix[ FRONT_CENTER][FRONT_RIGHT_OF_CENTER]+= M_SQRT1_2;
  255. }else
  256. av_assert0(0);
  257. }
  258. if (unaccounted & AV_CH_TOP_FRONT_LEFT) {
  259. if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_TOP_FRONT_CENTER) >= 0) {
  260. matrix[TOP_FRONT_CENTER][TOP_FRONT_LEFT ] += M_SQRT1_2;
  261. matrix[TOP_FRONT_CENTER][TOP_FRONT_RIGHT] += M_SQRT1_2;
  262. if (av_channel_layout_index_from_channel(in_ch_layout, AV_CHAN_TOP_FRONT_CENTER) >= 0)
  263. matrix[TOP_FRONT_CENTER][TOP_FRONT_CENTER] = center_mix_level * sqrt(2);
  264. } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
  265. if (av_channel_layout_index_from_channel(in_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
  266. matrix[FRONT_LEFT ][TOP_FRONT_LEFT ] += M_SQRT1_2;
  267. matrix[FRONT_RIGHT][TOP_FRONT_RIGHT] += M_SQRT1_2;
  268. } else {
  269. matrix[FRONT_LEFT ][TOP_FRONT_LEFT ] += 1.0;
  270. matrix[FRONT_RIGHT][TOP_FRONT_RIGHT] += 1.0;
  271. }
  272. } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
  273. matrix[FRONT_CENTER][TOP_FRONT_LEFT ] += M_SQRT1_2;
  274. matrix[FRONT_CENTER][TOP_FRONT_RIGHT] += M_SQRT1_2;
  275. } else
  276. av_assert0(0);
  277. }
  278. /* mix LFE into front left/right or center */
  279. if (unaccounted & AV_CH_LOW_FREQUENCY) {
  280. if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
  281. matrix[FRONT_CENTER][LOW_FREQUENCY] += lfe_mix_level;
  282. } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
  283. matrix[FRONT_LEFT ][LOW_FREQUENCY] += lfe_mix_level * M_SQRT1_2;
  284. matrix[FRONT_RIGHT][LOW_FREQUENCY] += lfe_mix_level * M_SQRT1_2;
  285. } else
  286. av_assert0(0);
  287. }
  288. for (i = 0; i < 64; i++) {
  289. double sum=0;
  290. int out_i = av_channel_layout_index_from_channel(out_ch_layout, i);
  291. if (out_i < 0)
  292. continue;
  293. for(j=0; j<64; j++){
  294. int in_i = av_channel_layout_index_from_channel(in_ch_layout, j);
  295. if (in_i < 0)
  296. continue;
  297. if (i < FF_ARRAY_ELEMS(matrix) && j < FF_ARRAY_ELEMS(matrix[0]))
  298. matrix_param[stride*out_i + in_i] = matrix[i][j];
  299. else
  300. matrix_param[stride*out_i + in_i] = i == j &&
  301. ( av_channel_layout_index_from_channel(in_ch_layout, i) >= 0
  302. && av_channel_layout_index_from_channel(out_ch_layout, i) >= 0);
  303. sum += fabs(matrix_param[stride*out_i + in_i]);
  304. }
  305. maxcoef= FFMAX(maxcoef, sum);
  306. }
  307. if(rematrix_volume < 0)
  308. maxcoef = -rematrix_volume;
  309. if(maxcoef > maxval || rematrix_volume < 0){
  310. maxcoef /= maxval;
  311. for(i=0; i<SWR_CH_MAX; i++)
  312. for(j=0; j<SWR_CH_MAX; j++){
  313. matrix_param[stride*i + j] /= maxcoef;
  314. }
  315. }
  316. }
  317. av_cold int swr_build_matrix2(const AVChannelLayout *in_layout, const AVChannelLayout *out_layout,
  318. double center_mix_level, double surround_mix_level,
  319. double lfe_mix_level, double maxval,
  320. double rematrix_volume, double *matrix_param,
  321. ptrdiff_t stride, enum AVMatrixEncoding matrix_encoding, void *log_context)
  322. {
  323. int i, j, ret;
  324. AVChannelLayout in_ch_layout = { 0 }, out_ch_layout = { 0 };
  325. char buf[128];
  326. ret = clean_layout(&in_ch_layout, in_layout, log_context);
  327. ret |= clean_layout(&out_ch_layout, out_layout, log_context);
  328. if (ret < 0)
  329. goto fail;
  330. if( !av_channel_layout_compare(&out_ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO_DOWNMIX)
  331. && !av_channel_layout_subset(&in_ch_layout, AV_CH_LAYOUT_STEREO_DOWNMIX)
  332. ) {
  333. av_channel_layout_uninit(&out_ch_layout);
  334. out_ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
  335. }
  336. if( !av_channel_layout_compare(&in_ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO_DOWNMIX)
  337. && !av_channel_layout_subset(&out_ch_layout, AV_CH_LAYOUT_STEREO_DOWNMIX)
  338. ) {
  339. av_channel_layout_uninit(&in_ch_layout);
  340. in_ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
  341. }
  342. if (!av_channel_layout_compare(&in_ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_22POINT2) &&
  343. av_channel_layout_compare(&out_ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_22POINT2)) {
  344. av_channel_layout_from_mask(&in_ch_layout, (AV_CH_LAYOUT_7POINT1_WIDE_BACK|AV_CH_BACK_CENTER));
  345. av_channel_layout_describe(&in_ch_layout, buf, sizeof(buf));
  346. av_log(log_context, AV_LOG_WARNING,
  347. "Full-on remixing from 22.2 has not yet been implemented! "
  348. "Processing the input as '%s'\n",
  349. buf);
  350. }
  351. if(!av_channel_layout_check(&in_ch_layout)) {
  352. av_log(log_context, AV_LOG_ERROR, "Input channel layout is invalid\n");
  353. ret = AVERROR(EINVAL);
  354. goto fail;
  355. }
  356. if(!sane_layout(&in_ch_layout)) {
  357. av_channel_layout_describe(&in_ch_layout, buf, sizeof(buf));
  358. av_log(log_context, AV_LOG_ERROR, "Input channel layout '%s' is not supported\n", buf);
  359. ret = AVERROR(EINVAL);
  360. goto fail;
  361. }
  362. if(!av_channel_layout_check(&out_ch_layout)) {
  363. av_log(log_context, AV_LOG_ERROR, "Output channel layout is invalid\n");
  364. ret = AVERROR(EINVAL);
  365. goto fail;
  366. }
  367. if(!sane_layout(&out_ch_layout)) {
  368. av_channel_layout_describe(&out_ch_layout, buf, sizeof(buf));
  369. av_log(log_context, AV_LOG_ERROR, "Output channel layout '%s' is not supported\n", buf);
  370. ret = AVERROR(EINVAL);
  371. goto fail;
  372. }
  373. build_matrix(&in_ch_layout, &out_ch_layout, center_mix_level,
  374. surround_mix_level, lfe_mix_level, maxval, rematrix_volume,
  375. matrix_param, stride, matrix_encoding);
  376. if(rematrix_volume > 0){
  377. for(i=0; i<SWR_CH_MAX; i++)
  378. for(j=0; j<SWR_CH_MAX; j++){
  379. matrix_param[stride*i + j] *= rematrix_volume;
  380. }
  381. }
  382. av_log(log_context, AV_LOG_DEBUG, "Matrix coefficients:\n");
  383. for (i = 0; i < out_ch_layout.nb_channels; i++){
  384. av_channel_name(buf, sizeof(buf), av_channel_layout_channel_from_index(&out_ch_layout, i));
  385. av_log(log_context, AV_LOG_DEBUG, "%s: ", buf);
  386. for (j = 0; j < in_ch_layout.nb_channels; j++){
  387. av_channel_name(buf, sizeof(buf), av_channel_layout_channel_from_index(&in_ch_layout, j));
  388. av_log(log_context, AV_LOG_DEBUG, "%s:%f ", buf, matrix_param[stride*i + j]);
  389. }
  390. av_log(log_context, AV_LOG_DEBUG, "\n");
  391. }
  392. ret = 0;
  393. fail:
  394. av_channel_layout_uninit(&in_ch_layout);
  395. av_channel_layout_uninit(&out_ch_layout);
  396. return ret;
  397. }
  398. av_cold static int auto_matrix(SwrContext *s)
  399. {
  400. double maxval;
  401. int ret;
  402. if (s->rematrix_maxval > 0) {
  403. maxval = s->rematrix_maxval;
  404. } else if ( av_get_packed_sample_fmt(s->out_sample_fmt) < AV_SAMPLE_FMT_FLT
  405. || av_get_packed_sample_fmt(s->int_sample_fmt) < AV_SAMPLE_FMT_FLT) {
  406. maxval = 1.0;
  407. } else
  408. maxval = INT_MAX;
  409. memset(s->matrix, 0, sizeof(s->matrix));
  410. ret = swr_build_matrix2(&s->in_ch_layout, &s->out_ch_layout,
  411. s->clev, s->slev, s->lfe_mix_level,
  412. maxval, s->rematrix_volume, (double*)s->matrix,
  413. s->matrix[1] - s->matrix[0], s->matrix_encoding, s);
  414. if (ret >= 0 && s->int_sample_fmt == AV_SAMPLE_FMT_FLTP) {
  415. int i, j;
  416. for (i = 0; i < FF_ARRAY_ELEMS(s->matrix[0]); i++)
  417. for (j = 0; j < FF_ARRAY_ELEMS(s->matrix[0]); j++)
  418. s->matrix_flt[i][j] = s->matrix[i][j];
  419. }
  420. return ret;
  421. }
  422. av_cold int swri_rematrix_init(SwrContext *s){
  423. int i, j;
  424. int nb_in = s->used_ch_layout.nb_channels;
  425. int nb_out = s->out.ch_count;
  426. s->mix_any_f = NULL;
  427. if (!s->rematrix_custom) {
  428. int r = auto_matrix(s);
  429. if (r)
  430. return r;
  431. }
  432. if (s->midbuf.fmt == AV_SAMPLE_FMT_S16P){
  433. int maxsum = 0;
  434. s->native_matrix = av_calloc(nb_in * nb_out, sizeof(int));
  435. s->native_one = av_mallocz(sizeof(int));
  436. if (!s->native_matrix || !s->native_one)
  437. return AVERROR(ENOMEM);
  438. for (i = 0; i < nb_out; i++) {
  439. double rem = 0;
  440. int sum = 0;
  441. for (j = 0; j < nb_in; j++) {
  442. double target = s->matrix[i][j] * 32768 + rem;
  443. ((int*)s->native_matrix)[i * nb_in + j] = lrintf(target);
  444. rem += target - ((int*)s->native_matrix)[i * nb_in + j];
  445. sum += FFABS(((int*)s->native_matrix)[i * nb_in + j]);
  446. }
  447. maxsum = FFMAX(maxsum, sum);
  448. }
  449. *((int*)s->native_one) = 32768;
  450. if (maxsum <= 32768) {
  451. s->mix_1_1_f = (mix_1_1_func_type*)copy_s16;
  452. s->mix_2_1_f = (mix_2_1_func_type*)sum2_s16;
  453. s->mix_any_f = (mix_any_func_type*)get_mix_any_func_s16(s);
  454. } else {
  455. s->mix_1_1_f = (mix_1_1_func_type*)copy_clip_s16;
  456. s->mix_2_1_f = (mix_2_1_func_type*)sum2_clip_s16;
  457. s->mix_any_f = (mix_any_func_type*)get_mix_any_func_clip_s16(s);
  458. }
  459. }else if(s->midbuf.fmt == AV_SAMPLE_FMT_FLTP){
  460. s->native_matrix = av_calloc(nb_in * nb_out, sizeof(float));
  461. s->native_one = av_mallocz(sizeof(float));
  462. if (!s->native_matrix || !s->native_one)
  463. return AVERROR(ENOMEM);
  464. for (i = 0; i < nb_out; i++)
  465. for (j = 0; j < nb_in; j++)
  466. ((float*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
  467. *((float*)s->native_one) = 1.0;
  468. s->mix_1_1_f = (mix_1_1_func_type*)copy_float;
  469. s->mix_2_1_f = (mix_2_1_func_type*)sum2_float;
  470. s->mix_any_f = (mix_any_func_type*)get_mix_any_func_float(s);
  471. }else if(s->midbuf.fmt == AV_SAMPLE_FMT_DBLP){
  472. s->native_matrix = av_calloc(nb_in * nb_out, sizeof(double));
  473. s->native_one = av_mallocz(sizeof(double));
  474. if (!s->native_matrix || !s->native_one)
  475. return AVERROR(ENOMEM);
  476. for (i = 0; i < nb_out; i++)
  477. for (j = 0; j < nb_in; j++)
  478. ((double*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
  479. *((double*)s->native_one) = 1.0;
  480. s->mix_1_1_f = (mix_1_1_func_type*)copy_double;
  481. s->mix_2_1_f = (mix_2_1_func_type*)sum2_double;
  482. s->mix_any_f = (mix_any_func_type*)get_mix_any_func_double(s);
  483. }else if(s->midbuf.fmt == AV_SAMPLE_FMT_S32P){
  484. s->native_one = av_mallocz(sizeof(int));
  485. if (!s->native_one)
  486. return AVERROR(ENOMEM);
  487. s->native_matrix = av_calloc(nb_in * nb_out, sizeof(int));
  488. if (!s->native_matrix) {
  489. av_freep(&s->native_one);
  490. return AVERROR(ENOMEM);
  491. }
  492. for (i = 0; i < nb_out; i++) {
  493. double rem = 0;
  494. for (j = 0; j < nb_in; j++) {
  495. double target = s->matrix[i][j] * 32768 + rem;
  496. ((int*)s->native_matrix)[i * nb_in + j] = lrintf(target);
  497. rem += target - ((int*)s->native_matrix)[i * nb_in + j];
  498. }
  499. }
  500. *((int*)s->native_one) = 32768;
  501. s->mix_1_1_f = (mix_1_1_func_type*)copy_s32;
  502. s->mix_2_1_f = (mix_2_1_func_type*)sum2_s32;
  503. s->mix_any_f = (mix_any_func_type*)get_mix_any_func_s32(s);
  504. }else
  505. av_assert0(0);
  506. //FIXME quantize for integeres
  507. for (i = 0; i < SWR_CH_MAX; i++) {
  508. int ch_in=0;
  509. for (j = 0; j < SWR_CH_MAX; j++) {
  510. s->matrix32[i][j]= lrintf(s->matrix[i][j] * 32768);
  511. if(s->matrix[i][j])
  512. s->matrix_ch[i][++ch_in]= j;
  513. }
  514. s->matrix_ch[i][0]= ch_in;
  515. }
  516. #if ARCH_X86 && HAVE_X86ASM && HAVE_MMX
  517. return swri_rematrix_init_x86(s);
  518. #endif
  519. return 0;
  520. }
  521. av_cold void swri_rematrix_free(SwrContext *s){
  522. av_freep(&s->native_matrix);
  523. av_freep(&s->native_one);
  524. av_freep(&s->native_simd_matrix);
  525. av_freep(&s->native_simd_one);
  526. }
  527. int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy){
  528. int out_i, in_i, i, j;
  529. int len1 = 0;
  530. int off = 0;
  531. if(s->mix_any_f) {
  532. s->mix_any_f(out->ch, (const uint8_t **)in->ch, s->native_matrix, len);
  533. return 0;
  534. }
  535. if(s->mix_2_1_simd || s->mix_1_1_simd){
  536. len1= len&~15;
  537. off = len1 * out->bps;
  538. }
  539. av_assert0(s->out_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC || out->ch_count == s->out_ch_layout.nb_channels);
  540. av_assert0(s-> in_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC || in ->ch_count == s->in_ch_layout.nb_channels);
  541. for(out_i=0; out_i<out->ch_count; out_i++){
  542. switch(s->matrix_ch[out_i][0]){
  543. case 0:
  544. if(mustcopy)
  545. memset(out->ch[out_i], 0, len * av_get_bytes_per_sample(s->int_sample_fmt));
  546. break;
  547. case 1:
  548. in_i= s->matrix_ch[out_i][1];
  549. if(s->matrix[out_i][in_i]!=1.0){
  550. if(s->mix_1_1_simd && len1)
  551. 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);
  552. if(len != len1)
  553. 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);
  554. }else if(mustcopy){
  555. memcpy(out->ch[out_i], in->ch[in_i], len*out->bps);
  556. }else{
  557. out->ch[out_i]= in->ch[in_i];
  558. }
  559. break;
  560. case 2: {
  561. int in_i1 = s->matrix_ch[out_i][1];
  562. int in_i2 = s->matrix_ch[out_i][2];
  563. if(s->mix_2_1_simd && len1)
  564. 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);
  565. else
  566. 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);
  567. if(len != len1)
  568. 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);
  569. break;}
  570. default:
  571. if(s->int_sample_fmt == AV_SAMPLE_FMT_FLTP){
  572. for(i=0; i<len; i++){
  573. float v=0;
  574. for(j=0; j<s->matrix_ch[out_i][0]; j++){
  575. in_i= s->matrix_ch[out_i][1+j];
  576. v+= ((float*)in->ch[in_i])[i] * s->matrix_flt[out_i][in_i];
  577. }
  578. ((float*)out->ch[out_i])[i]= v;
  579. }
  580. }else if(s->int_sample_fmt == AV_SAMPLE_FMT_DBLP){
  581. for(i=0; i<len; i++){
  582. double v=0;
  583. for(j=0; j<s->matrix_ch[out_i][0]; j++){
  584. in_i= s->matrix_ch[out_i][1+j];
  585. v+= ((double*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
  586. }
  587. ((double*)out->ch[out_i])[i]= v;
  588. }
  589. }else{
  590. for(i=0; i<len; i++){
  591. int v=0;
  592. for(j=0; j<s->matrix_ch[out_i][0]; j++){
  593. in_i= s->matrix_ch[out_i][1+j];
  594. v+= ((int16_t*)in->ch[in_i])[i] * s->matrix32[out_i][in_i];
  595. }
  596. ((int16_t*)out->ch[out_i])[i]= (v + 16384)>>15;
  597. }
  598. }
  599. }
  600. }
  601. return 0;
  602. }