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.

332 lines
12KB

  1. /*
  2. * Copyright (C) 2011 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 (-32768)
  35. #define R(x) (((x) + 16384)>>15)
  36. #define SAMPLE int16_t
  37. #define COEFF int
  38. #define RENAME(x) x ## _s16
  39. #include "rematrix_template.c"
  40. #define FRONT_LEFT 0
  41. #define FRONT_RIGHT 1
  42. #define FRONT_CENTER 2
  43. #define LOW_FREQUENCY 3
  44. #define BACK_LEFT 4
  45. #define BACK_RIGHT 5
  46. #define FRONT_LEFT_OF_CENTER 6
  47. #define FRONT_RIGHT_OF_CENTER 7
  48. #define BACK_CENTER 8
  49. #define SIDE_LEFT 9
  50. #define SIDE_RIGHT 10
  51. #define TOP_CENTER 11
  52. #define TOP_FRONT_LEFT 12
  53. #define TOP_FRONT_CENTER 13
  54. #define TOP_FRONT_RIGHT 14
  55. #define TOP_BACK_LEFT 15
  56. #define TOP_BACK_CENTER 16
  57. #define TOP_BACK_RIGHT 17
  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 = av_get_channel_layout_nb_channels(s->in_ch_layout);
  65. nb_out = av_get_channel_layout_nb_channels(s->out_ch_layout);
  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 sane_layout(int64_t layout){
  80. if(!(layout & AV_CH_LAYOUT_SURROUND)) // at least 1 front speaker
  81. return 0;
  82. if(!even(layout & (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT))) // no asymetric front
  83. return 0;
  84. if(!even(layout & (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT))) // no asymetric side
  85. return 0;
  86. if(!even(layout & (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)))
  87. return 0;
  88. if(!even(layout & (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)))
  89. return 0;
  90. if(av_get_channel_layout_nb_channels(layout) >= SWR_CH_MAX)
  91. return 0;
  92. return 1;
  93. }
  94. static int auto_matrix(SwrContext *s)
  95. {
  96. int i, j, out_i;
  97. double matrix[64][64]={{0}};
  98. int64_t unaccounted= s->in_ch_layout & ~s->out_ch_layout;
  99. double maxcoef=0;
  100. memset(s->matrix, 0, sizeof(s->matrix));
  101. for(i=0; i<64; i++){
  102. if(s->in_ch_layout & s->out_ch_layout & (1LL<<i))
  103. matrix[i][i]= 1.0;
  104. }
  105. if(!sane_layout(s->in_ch_layout)){
  106. av_log(s, AV_LOG_ERROR, "Input channel layout isnt supported\n");
  107. return AVERROR(EINVAL);
  108. }
  109. if(!sane_layout(s->out_ch_layout)){
  110. av_log(s, AV_LOG_ERROR, "Output channel layout isnt supported\n");
  111. return AVERROR(EINVAL);
  112. }
  113. //FIXME implement dolby surround
  114. //FIXME implement full ac3
  115. if(unaccounted & AV_CH_FRONT_CENTER){
  116. if((s->out_ch_layout & AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO){
  117. matrix[ FRONT_LEFT][FRONT_CENTER]+= M_SQRT1_2;
  118. matrix[FRONT_RIGHT][FRONT_CENTER]+= M_SQRT1_2;
  119. }else
  120. av_assert0(0);
  121. }
  122. if(unaccounted & AV_CH_LAYOUT_STEREO){
  123. if(s->out_ch_layout & AV_CH_FRONT_CENTER){
  124. matrix[FRONT_CENTER][ FRONT_LEFT]+= M_SQRT1_2;
  125. matrix[FRONT_CENTER][FRONT_RIGHT]+= M_SQRT1_2;
  126. if(s->in_ch_layout & AV_CH_FRONT_CENTER)
  127. matrix[FRONT_CENTER][ FRONT_CENTER] = s->clev*sqrt(2);
  128. }else
  129. av_assert0(0);
  130. }
  131. if(unaccounted & AV_CH_BACK_CENTER){
  132. if(s->out_ch_layout & AV_CH_BACK_LEFT){
  133. matrix[ BACK_LEFT][BACK_CENTER]+= M_SQRT1_2;
  134. matrix[BACK_RIGHT][BACK_CENTER]+= M_SQRT1_2;
  135. }else if(s->out_ch_layout & AV_CH_SIDE_LEFT){
  136. matrix[ SIDE_LEFT][BACK_CENTER]+= M_SQRT1_2;
  137. matrix[SIDE_RIGHT][BACK_CENTER]+= M_SQRT1_2;
  138. }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
  139. matrix[ FRONT_LEFT][BACK_CENTER]+= s->slev*M_SQRT1_2;
  140. matrix[FRONT_RIGHT][BACK_CENTER]+= s->slev*M_SQRT1_2;
  141. }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
  142. matrix[ FRONT_CENTER][BACK_CENTER]+= s->slev*M_SQRT1_2;
  143. }else
  144. av_assert0(0);
  145. }
  146. if(unaccounted & AV_CH_BACK_LEFT){
  147. if(s->out_ch_layout & AV_CH_BACK_CENTER){
  148. matrix[BACK_CENTER][ BACK_LEFT]+= M_SQRT1_2;
  149. matrix[BACK_CENTER][BACK_RIGHT]+= M_SQRT1_2;
  150. }else if(s->out_ch_layout & AV_CH_SIDE_LEFT){
  151. if(s->in_ch_layout & AV_CH_SIDE_LEFT){
  152. matrix[ SIDE_LEFT][ BACK_LEFT]+= M_SQRT1_2;
  153. matrix[SIDE_RIGHT][BACK_RIGHT]+= M_SQRT1_2;
  154. }else{
  155. matrix[ SIDE_LEFT][ BACK_LEFT]+= 1.0;
  156. matrix[SIDE_RIGHT][BACK_RIGHT]+= 1.0;
  157. }
  158. }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
  159. matrix[ FRONT_LEFT][ BACK_LEFT]+= s->slev;
  160. matrix[FRONT_RIGHT][BACK_RIGHT]+= s->slev;
  161. }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
  162. matrix[ FRONT_CENTER][BACK_LEFT ]+= s->slev*M_SQRT1_2;
  163. matrix[ FRONT_CENTER][BACK_RIGHT]+= s->slev*M_SQRT1_2;
  164. }else
  165. av_assert0(0);
  166. }
  167. if(unaccounted & AV_CH_SIDE_LEFT){
  168. if(s->out_ch_layout & AV_CH_BACK_LEFT){
  169. matrix[ BACK_LEFT][ SIDE_LEFT]+= 1.0;
  170. matrix[BACK_RIGHT][SIDE_RIGHT]+= 1.0;
  171. }else if(s->out_ch_layout & AV_CH_BACK_CENTER){
  172. matrix[BACK_CENTER][ SIDE_LEFT]+= M_SQRT1_2;
  173. matrix[BACK_CENTER][SIDE_RIGHT]+= M_SQRT1_2;
  174. }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
  175. matrix[ FRONT_LEFT][ SIDE_LEFT]+= s->slev;
  176. matrix[FRONT_RIGHT][SIDE_RIGHT]+= s->slev;
  177. }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
  178. matrix[ FRONT_CENTER][SIDE_LEFT ]+= s->slev*M_SQRT1_2;
  179. matrix[ FRONT_CENTER][SIDE_RIGHT]+= s->slev*M_SQRT1_2;
  180. }else
  181. av_assert0(0);
  182. }
  183. if(unaccounted & AV_CH_FRONT_LEFT_OF_CENTER){
  184. if(s->out_ch_layout & AV_CH_FRONT_LEFT){
  185. matrix[ FRONT_LEFT][ FRONT_LEFT_OF_CENTER]+= 1.0;
  186. matrix[FRONT_RIGHT][FRONT_RIGHT_OF_CENTER]+= 1.0;
  187. }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
  188. matrix[ FRONT_CENTER][ FRONT_LEFT_OF_CENTER]+= M_SQRT1_2;
  189. matrix[ FRONT_CENTER][FRONT_RIGHT_OF_CENTER]+= M_SQRT1_2;
  190. }else
  191. av_assert0(0);
  192. }
  193. for(out_i=i=0; i<64; i++){
  194. double sum=0;
  195. int in_i=0;
  196. for(j=0; j<64; j++){
  197. s->matrix[out_i][in_i]= matrix[i][j];
  198. if(matrix[i][j]){
  199. sum += fabs(matrix[i][j]);
  200. }
  201. if(s->in_ch_layout & (1ULL<<j))
  202. in_i++;
  203. }
  204. maxcoef= FFMAX(maxcoef, sum);
  205. if(s->out_ch_layout & (1ULL<<i))
  206. out_i++;
  207. }
  208. if(s->rematrix_volume < 0)
  209. maxcoef = -s->rematrix_volume;
  210. if(( s->out_sample_fmt < AV_SAMPLE_FMT_FLT
  211. || s->int_sample_fmt < AV_SAMPLE_FMT_FLT) && maxcoef > 1.0){
  212. for(i=0; i<SWR_CH_MAX; i++)
  213. for(j=0; j<SWR_CH_MAX; j++){
  214. s->matrix[i][j] /= maxcoef;
  215. }
  216. }
  217. if(s->rematrix_volume > 0){
  218. for(i=0; i<SWR_CH_MAX; i++)
  219. for(j=0; j<SWR_CH_MAX; j++){
  220. s->matrix[i][j] *= s->rematrix_volume;
  221. }
  222. }
  223. for(i=0; i<av_get_channel_layout_nb_channels(s->out_ch_layout); i++){
  224. for(j=0; j<av_get_channel_layout_nb_channels(s->in_ch_layout); j++){
  225. av_log(NULL, AV_LOG_DEBUG, "%f ", s->matrix[i][j]);
  226. }
  227. av_log(NULL, AV_LOG_DEBUG, "\n");
  228. }
  229. return 0;
  230. }
  231. int swri_rematrix_init(SwrContext *s){
  232. int i, j;
  233. if (!s->rematrix_custom) {
  234. int r = auto_matrix(s);
  235. if (r)
  236. return r;
  237. }
  238. //FIXME quantize for integeres
  239. for (i = 0; i < SWR_CH_MAX; i++) {
  240. int ch_in=0;
  241. for (j = 0; j < SWR_CH_MAX; j++) {
  242. s->matrix32[i][j]= lrintf(s->matrix[i][j] * 32768);
  243. if(s->matrix[i][j])
  244. s->matrix_ch[i][++ch_in]= j;
  245. }
  246. s->matrix_ch[i][0]= ch_in;
  247. }
  248. return 0;
  249. }
  250. int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy){
  251. int out_i, in_i, i, j;
  252. av_assert0(out->ch_count == av_get_channel_layout_nb_channels(s->out_ch_layout));
  253. av_assert0(in ->ch_count == av_get_channel_layout_nb_channels(s-> in_ch_layout));
  254. for(out_i=0; out_i<out->ch_count; out_i++){
  255. switch(s->matrix_ch[out_i][0]){
  256. case 0:
  257. memset(out->ch[out_i], 0, len * (s->int_sample_fmt == AV_SAMPLE_FMT_FLT ? sizeof(float) : sizeof(int16_t)));
  258. break;
  259. case 1:
  260. in_i= s->matrix_ch[out_i][1];
  261. if(mustcopy || s->matrix[out_i][in_i]!=1.0){
  262. if(s->int_sample_fmt == AV_SAMPLE_FMT_FLT){
  263. copy_float((float *)out->ch[out_i], (const float *)in->ch[in_i], s->matrix [out_i][in_i], len);
  264. }else
  265. copy_s16 ((int16_t*)out->ch[out_i], (const int16_t*)in->ch[in_i], s->matrix32[out_i][in_i], len);
  266. }else{
  267. out->ch[out_i]= in->ch[in_i];
  268. }
  269. break;
  270. case 2:
  271. if(s->int_sample_fmt == AV_SAMPLE_FMT_FLT){
  272. sum2_float((float *)out->ch[out_i], (const float *)in->ch[ s->matrix_ch[out_i][1] ], (const float *)in->ch[ s->matrix_ch[out_i][2] ],
  273. s->matrix[out_i][ s->matrix_ch[out_i][1] ], s->matrix[out_i][ s->matrix_ch[out_i][2] ],
  274. len);
  275. }else{
  276. sum2_s16 ((int16_t*)out->ch[out_i], (const int16_t*)in->ch[ s->matrix_ch[out_i][1] ], (const int16_t*)in->ch[ s->matrix_ch[out_i][2] ],
  277. s->matrix32[out_i][ s->matrix_ch[out_i][1] ], s->matrix32[out_i][ s->matrix_ch[out_i][2] ],
  278. len);
  279. }
  280. break;
  281. default:
  282. if(s->int_sample_fmt == AV_SAMPLE_FMT_FLT){
  283. for(i=0; i<len; i++){
  284. float v=0;
  285. for(j=0; j<s->matrix_ch[out_i][0]; j++){
  286. in_i= s->matrix_ch[out_i][1+j];
  287. v+= ((float*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
  288. }
  289. ((float*)out->ch[out_i])[i]= v;
  290. }
  291. }else{
  292. for(i=0; i<len; i++){
  293. int v=0;
  294. for(j=0; j<s->matrix_ch[out_i][0]; j++){
  295. in_i= s->matrix_ch[out_i][1+j];
  296. v+= ((int16_t*)in->ch[in_i])[i] * s->matrix32[out_i][in_i];
  297. }
  298. ((int16_t*)out->ch[out_i])[i]= (v + 16384)>>15;
  299. }
  300. }
  301. }
  302. }
  303. return 0;
  304. }