rematrix.c 27 KB

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