rematrix.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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/audioconvert.h"
  22. #include "libavutil/avassert.h"
  23. #define ONE (1.0)
  24. #define R(x) x
  25. #define SAMPLE float
  26. #define COEFF float
  27. #define RENAME(x) x ## _float
  28. #include "rematrix_template.c"
  29. #undef SAMPLE
  30. #undef RENAME
  31. #undef R
  32. #undef ONE
  33. #undef COEFF
  34. #define ONE (1.0)
  35. #define R(x) x
  36. #define SAMPLE double
  37. #define COEFF double
  38. #define RENAME(x) x ## _double
  39. #include "rematrix_template.c"
  40. #undef SAMPLE
  41. #undef RENAME
  42. #undef R
  43. #undef ONE
  44. #undef COEFF
  45. #define ONE (-32768)
  46. #define R(x) (((x) + 16384)>>15)
  47. #define SAMPLE int16_t
  48. #define COEFF int
  49. #define RENAME(x) x ## _s16
  50. #include "rematrix_template.c"
  51. #define FRONT_LEFT 0
  52. #define FRONT_RIGHT 1
  53. #define FRONT_CENTER 2
  54. #define LOW_FREQUENCY 3
  55. #define BACK_LEFT 4
  56. #define BACK_RIGHT 5
  57. #define FRONT_LEFT_OF_CENTER 6
  58. #define FRONT_RIGHT_OF_CENTER 7
  59. #define BACK_CENTER 8
  60. #define SIDE_LEFT 9
  61. #define SIDE_RIGHT 10
  62. #define TOP_CENTER 11
  63. #define TOP_FRONT_LEFT 12
  64. #define TOP_FRONT_CENTER 13
  65. #define TOP_FRONT_RIGHT 14
  66. #define TOP_BACK_LEFT 15
  67. #define TOP_BACK_CENTER 16
  68. #define TOP_BACK_RIGHT 17
  69. int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride)
  70. {
  71. int nb_in, nb_out, in, out;
  72. if (!s || s->in_convert) // s needs to be allocated but not initialized
  73. return AVERROR(EINVAL);
  74. memset(s->matrix, 0, sizeof(s->matrix));
  75. nb_in = av_get_channel_layout_nb_channels(s->in_ch_layout);
  76. nb_out = av_get_channel_layout_nb_channels(s->out_ch_layout);
  77. for (out = 0; out < nb_out; out++) {
  78. for (in = 0; in < nb_in; in++)
  79. s->matrix[out][in] = matrix[in];
  80. matrix += stride;
  81. }
  82. s->rematrix_custom = 1;
  83. return 0;
  84. }
  85. static int even(int64_t layout){
  86. if(!layout) return 1;
  87. if(layout&(layout-1)) return 1;
  88. return 0;
  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. static int auto_matrix(SwrContext *s)
  106. {
  107. int i, j, out_i;
  108. double matrix[64][64]={{0}};
  109. int64_t unaccounted= s->in_ch_layout & ~s->out_ch_layout;
  110. double maxcoef=0;
  111. memset(s->matrix, 0, sizeof(s->matrix));
  112. for(i=0; i<64; i++){
  113. if(s->in_ch_layout & s->out_ch_layout & (1LL<<i))
  114. matrix[i][i]= 1.0;
  115. }
  116. if(!sane_layout(s->in_ch_layout)){
  117. av_log(s, AV_LOG_ERROR, "Input channel layout isnt supported\n");
  118. return AVERROR(EINVAL);
  119. }
  120. if(!sane_layout(s->out_ch_layout)){
  121. av_log(s, AV_LOG_ERROR, "Output channel layout isnt supported\n");
  122. return AVERROR(EINVAL);
  123. }
  124. //FIXME implement dolby surround
  125. //FIXME implement full ac3
  126. if(unaccounted & AV_CH_FRONT_CENTER){
  127. if((s->out_ch_layout & AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO){
  128. matrix[ FRONT_LEFT][FRONT_CENTER]+= M_SQRT1_2;
  129. matrix[FRONT_RIGHT][FRONT_CENTER]+= M_SQRT1_2;
  130. }else
  131. av_assert0(0);
  132. }
  133. if(unaccounted & AV_CH_LAYOUT_STEREO){
  134. if(s->out_ch_layout & AV_CH_FRONT_CENTER){
  135. matrix[FRONT_CENTER][ FRONT_LEFT]+= M_SQRT1_2;
  136. matrix[FRONT_CENTER][FRONT_RIGHT]+= M_SQRT1_2;
  137. if(s->in_ch_layout & AV_CH_FRONT_CENTER)
  138. matrix[FRONT_CENTER][ FRONT_CENTER] = s->clev*sqrt(2);
  139. }else
  140. av_assert0(0);
  141. }
  142. if(unaccounted & AV_CH_BACK_CENTER){
  143. if(s->out_ch_layout & AV_CH_BACK_LEFT){
  144. matrix[ BACK_LEFT][BACK_CENTER]+= M_SQRT1_2;
  145. matrix[BACK_RIGHT][BACK_CENTER]+= M_SQRT1_2;
  146. }else if(s->out_ch_layout & AV_CH_SIDE_LEFT){
  147. matrix[ SIDE_LEFT][BACK_CENTER]+= M_SQRT1_2;
  148. matrix[SIDE_RIGHT][BACK_CENTER]+= M_SQRT1_2;
  149. }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
  150. matrix[ FRONT_LEFT][BACK_CENTER]+= s->slev*M_SQRT1_2;
  151. matrix[FRONT_RIGHT][BACK_CENTER]+= s->slev*M_SQRT1_2;
  152. }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
  153. matrix[ FRONT_CENTER][BACK_CENTER]+= s->slev*M_SQRT1_2;
  154. }else
  155. av_assert0(0);
  156. }
  157. if(unaccounted & AV_CH_BACK_LEFT){
  158. if(s->out_ch_layout & AV_CH_BACK_CENTER){
  159. matrix[BACK_CENTER][ BACK_LEFT]+= M_SQRT1_2;
  160. matrix[BACK_CENTER][BACK_RIGHT]+= M_SQRT1_2;
  161. }else if(s->out_ch_layout & AV_CH_SIDE_LEFT){
  162. if(s->in_ch_layout & AV_CH_SIDE_LEFT){
  163. matrix[ SIDE_LEFT][ BACK_LEFT]+= M_SQRT1_2;
  164. matrix[SIDE_RIGHT][BACK_RIGHT]+= M_SQRT1_2;
  165. }else{
  166. matrix[ SIDE_LEFT][ BACK_LEFT]+= 1.0;
  167. matrix[SIDE_RIGHT][BACK_RIGHT]+= 1.0;
  168. }
  169. }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
  170. matrix[ FRONT_LEFT][ BACK_LEFT]+= s->slev;
  171. matrix[FRONT_RIGHT][BACK_RIGHT]+= s->slev;
  172. }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
  173. matrix[ FRONT_CENTER][BACK_LEFT ]+= s->slev*M_SQRT1_2;
  174. matrix[ FRONT_CENTER][BACK_RIGHT]+= s->slev*M_SQRT1_2;
  175. }else
  176. av_assert0(0);
  177. }
  178. if(unaccounted & AV_CH_SIDE_LEFT){
  179. if(s->out_ch_layout & AV_CH_BACK_LEFT){
  180. /* if back channels do not exist in the input, just copy side
  181. channels to back channels, otherwise mix side into back */
  182. if (s->in_ch_layout & AV_CH_BACK_LEFT) {
  183. matrix[BACK_LEFT ][SIDE_LEFT ] += M_SQRT1_2;
  184. matrix[BACK_RIGHT][SIDE_RIGHT] += M_SQRT1_2;
  185. } else {
  186. matrix[BACK_LEFT ][SIDE_LEFT ] += 1.0;
  187. matrix[BACK_RIGHT][SIDE_RIGHT] += 1.0;
  188. }
  189. }else if(s->out_ch_layout & AV_CH_BACK_CENTER){
  190. matrix[BACK_CENTER][ SIDE_LEFT]+= M_SQRT1_2;
  191. matrix[BACK_CENTER][SIDE_RIGHT]+= M_SQRT1_2;
  192. }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
  193. matrix[ FRONT_LEFT][ SIDE_LEFT]+= s->slev;
  194. matrix[FRONT_RIGHT][SIDE_RIGHT]+= s->slev;
  195. }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
  196. matrix[ FRONT_CENTER][SIDE_LEFT ]+= s->slev*M_SQRT1_2;
  197. matrix[ FRONT_CENTER][SIDE_RIGHT]+= s->slev*M_SQRT1_2;
  198. }else
  199. av_assert0(0);
  200. }
  201. if(unaccounted & AV_CH_FRONT_LEFT_OF_CENTER){
  202. if(s->out_ch_layout & AV_CH_FRONT_LEFT){
  203. matrix[ FRONT_LEFT][ FRONT_LEFT_OF_CENTER]+= 1.0;
  204. matrix[FRONT_RIGHT][FRONT_RIGHT_OF_CENTER]+= 1.0;
  205. }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
  206. matrix[ FRONT_CENTER][ FRONT_LEFT_OF_CENTER]+= M_SQRT1_2;
  207. matrix[ FRONT_CENTER][FRONT_RIGHT_OF_CENTER]+= M_SQRT1_2;
  208. }else
  209. av_assert0(0);
  210. }
  211. /* mix LFE into front left/right or center */
  212. if (unaccounted & AV_CH_LOW_FREQUENCY) {
  213. if (s->out_ch_layout & AV_CH_FRONT_CENTER) {
  214. matrix[FRONT_CENTER][LOW_FREQUENCY] += s->lfe_mix_level;
  215. } else if (s->out_ch_layout & AV_CH_FRONT_LEFT) {
  216. matrix[FRONT_LEFT ][LOW_FREQUENCY] += s->lfe_mix_level * M_SQRT1_2;
  217. matrix[FRONT_RIGHT][LOW_FREQUENCY] += s->lfe_mix_level * M_SQRT1_2;
  218. } else
  219. av_assert0(0);
  220. }
  221. for(out_i=i=0; i<64; i++){
  222. double sum=0;
  223. int in_i=0;
  224. for(j=0; j<64; j++){
  225. s->matrix[out_i][in_i]= matrix[i][j];
  226. if(matrix[i][j]){
  227. sum += fabs(matrix[i][j]);
  228. }
  229. if(s->in_ch_layout & (1ULL<<j))
  230. in_i++;
  231. }
  232. maxcoef= FFMAX(maxcoef, sum);
  233. if(s->out_ch_layout & (1ULL<<i))
  234. out_i++;
  235. }
  236. if(s->rematrix_volume < 0)
  237. maxcoef = -s->rematrix_volume;
  238. if(( av_get_packed_sample_fmt(s->out_sample_fmt) < AV_SAMPLE_FMT_FLT
  239. || av_get_packed_sample_fmt(s->int_sample_fmt) < AV_SAMPLE_FMT_FLT) && maxcoef > 1.0){
  240. for(i=0; i<SWR_CH_MAX; i++)
  241. for(j=0; j<SWR_CH_MAX; j++){
  242. s->matrix[i][j] /= maxcoef;
  243. }
  244. }
  245. if(s->rematrix_volume > 0){
  246. for(i=0; i<SWR_CH_MAX; i++)
  247. for(j=0; j<SWR_CH_MAX; j++){
  248. s->matrix[i][j] *= s->rematrix_volume;
  249. }
  250. }
  251. for(i=0; i<av_get_channel_layout_nb_channels(s->out_ch_layout); i++){
  252. for(j=0; j<av_get_channel_layout_nb_channels(s->in_ch_layout); j++){
  253. av_log(NULL, AV_LOG_DEBUG, "%f ", s->matrix[i][j]);
  254. }
  255. av_log(NULL, AV_LOG_DEBUG, "\n");
  256. }
  257. return 0;
  258. }
  259. int swri_rematrix_init(SwrContext *s){
  260. int i, j;
  261. int nb_in = av_get_channel_layout_nb_channels(s->in_ch_layout);
  262. int nb_out = av_get_channel_layout_nb_channels(s->out_ch_layout);
  263. if (!s->rematrix_custom) {
  264. int r = auto_matrix(s);
  265. if (r)
  266. return r;
  267. }
  268. if (s->midbuf.fmt == AV_SAMPLE_FMT_S16P){
  269. s->native_matrix = av_mallocz(nb_in * nb_out * sizeof(int));
  270. s->native_one = av_mallocz(sizeof(int));
  271. for (i = 0; i < nb_out; i++)
  272. for (j = 0; j < nb_in; j++)
  273. ((int*)s->native_matrix)[i * nb_in + j] = lrintf(s->matrix[i][j] * 32768);
  274. *((int*)s->native_one) = 32768;
  275. s->mix_1_1_f = copy_s16;
  276. s->mix_2_1_f = sum2_s16;
  277. }else if(s->midbuf.fmt == AV_SAMPLE_FMT_FLTP){
  278. s->native_matrix = av_mallocz(nb_in * nb_out * sizeof(float));
  279. s->native_one = av_mallocz(sizeof(float));
  280. for (i = 0; i < nb_out; i++)
  281. for (j = 0; j < nb_in; j++)
  282. ((float*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
  283. *((float*)s->native_one) = 1.0;
  284. s->mix_1_1_f = copy_float;
  285. s->mix_2_1_f = sum2_float;
  286. }else if(s->midbuf.fmt == AV_SAMPLE_FMT_DBLP){
  287. s->native_matrix = av_mallocz(nb_in * nb_out * sizeof(double));
  288. s->native_one = av_mallocz(sizeof(double));
  289. for (i = 0; i < nb_out; i++)
  290. for (j = 0; j < nb_in; j++)
  291. ((double*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
  292. *((double*)s->native_one) = 1.0;
  293. s->mix_1_1_f = copy_double;
  294. s->mix_2_1_f = sum2_double;
  295. }else
  296. av_assert0(0);
  297. //FIXME quantize for integeres
  298. for (i = 0; i < SWR_CH_MAX; i++) {
  299. int ch_in=0;
  300. for (j = 0; j < SWR_CH_MAX; j++) {
  301. s->matrix32[i][j]= lrintf(s->matrix[i][j] * 32768);
  302. if(s->matrix[i][j])
  303. s->matrix_ch[i][++ch_in]= j;
  304. }
  305. s->matrix_ch[i][0]= ch_in;
  306. }
  307. return 0;
  308. }
  309. void swri_rematrix_free(SwrContext *s){
  310. av_freep(&s->native_matrix);
  311. av_freep(&s->native_one);
  312. }
  313. int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy){
  314. int out_i, in_i, i, j;
  315. av_assert0(out->ch_count == av_get_channel_layout_nb_channels(s->out_ch_layout));
  316. av_assert0(in ->ch_count == av_get_channel_layout_nb_channels(s-> in_ch_layout));
  317. for(out_i=0; out_i<out->ch_count; out_i++){
  318. switch(s->matrix_ch[out_i][0]){
  319. case 0:
  320. memset(out->ch[out_i], 0, len * av_get_bytes_per_sample(s->int_sample_fmt));
  321. break;
  322. case 1:
  323. in_i= s->matrix_ch[out_i][1];
  324. if(s->matrix[out_i][in_i]!=1.0){
  325. s->mix_1_1_f(out->ch[out_i], in->ch[in_i], s->native_matrix, in->ch_count*out_i + in_i, len);
  326. }else if(mustcopy){
  327. memcpy(out->ch[out_i], in->ch[in_i], len*out->bps);
  328. }else{
  329. out->ch[out_i]= in->ch[in_i];
  330. }
  331. break;
  332. case 2: {
  333. int in_i1 = s->matrix_ch[out_i][1];
  334. int in_i2 = s->matrix_ch[out_i][2];
  335. 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, len);
  336. break;}
  337. default:
  338. if(s->int_sample_fmt == AV_SAMPLE_FMT_FLTP){
  339. for(i=0; i<len; i++){
  340. float v=0;
  341. for(j=0; j<s->matrix_ch[out_i][0]; j++){
  342. in_i= s->matrix_ch[out_i][1+j];
  343. v+= ((float*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
  344. }
  345. ((float*)out->ch[out_i])[i]= v;
  346. }
  347. }else if(s->int_sample_fmt == AV_SAMPLE_FMT_DBLP){
  348. for(i=0; i<len; i++){
  349. double v=0;
  350. for(j=0; j<s->matrix_ch[out_i][0]; j++){
  351. in_i= s->matrix_ch[out_i][1+j];
  352. v+= ((double*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
  353. }
  354. ((double*)out->ch[out_i])[i]= v;
  355. }
  356. }else{
  357. for(i=0; i<len; i++){
  358. int v=0;
  359. for(j=0; j<s->matrix_ch[out_i][0]; j++){
  360. in_i= s->matrix_ch[out_i][1+j];
  361. v+= ((int16_t*)in->ch[in_i])[i] * s->matrix32[out_i][in_i];
  362. }
  363. ((int16_t*)out->ch[out_i])[i]= (v + 16384)>>15;
  364. }
  365. }
  366. }
  367. }
  368. return 0;
  369. }