You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

435 lines
15KB

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