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.

272 lines
10KB

  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 SAMPLE float
  24. #define RENAME(x) x ## _float
  25. #include "rematrix_template.c"
  26. #undef SAMPLE
  27. #undef RENAME
  28. #define SAMPLE int16_t
  29. #define RENAME(x) x ## _s16
  30. #include "rematrix_template.c"
  31. #define FRONT_LEFT 0
  32. #define FRONT_RIGHT 1
  33. #define FRONT_CENTER 2
  34. #define LOW_FREQUENCY 3
  35. #define BACK_LEFT 4
  36. #define BACK_RIGHT 5
  37. #define FRONT_LEFT_OF_CENTER 6
  38. #define FRONT_RIGHT_OF_CENTER 7
  39. #define BACK_CENTER 8
  40. #define SIDE_LEFT 9
  41. #define SIDE_RIGHT 10
  42. #define TOP_CENTER 11
  43. #define TOP_FRONT_LEFT 12
  44. #define TOP_FRONT_CENTER 13
  45. #define TOP_FRONT_RIGHT 14
  46. #define TOP_BACK_LEFT 15
  47. #define TOP_BACK_CENTER 16
  48. #define TOP_BACK_RIGHT 17
  49. static int even(int64_t layout){
  50. if(!layout) return 1;
  51. if(layout&(layout-1)) return 1;
  52. return 0;
  53. }
  54. static int sane_layout(int64_t layout){
  55. if(!(layout & AV_CH_LAYOUT_SURROUND)) // at least 1 front speaker
  56. return 0;
  57. if(!even(layout & (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT))) // no asymetric front
  58. return 0;
  59. if(!even(layout & (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT))) // no asymetric side
  60. return 0;
  61. if(!even(layout & (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)))
  62. return 0;
  63. if(!even(layout & (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)))
  64. return 0;
  65. if(av_get_channel_layout_nb_channels(layout) >= SWR_CH_MAX)
  66. return 0;
  67. return 1;
  68. }
  69. int swr_rematrix_init(SwrContext *s){
  70. int i, j, in_i, out_i;
  71. float matrix[64][64]={0};
  72. int64_t unaccounted= s->in_ch_layout & ~s->out_ch_layout;
  73. float maxcoef=0;
  74. for(i=0; i<64; i++){
  75. if(s->in_ch_layout & s->out_ch_layout & (1LL<<i))
  76. matrix[i][i]= 1.0;
  77. }
  78. if(!sane_layout(s->in_ch_layout)){
  79. av_log(s, AV_LOG_ERROR, "Input channel layout isnt supported\n");
  80. return AVERROR(EINVAL);
  81. }
  82. if(!sane_layout(s->out_ch_layout)){
  83. av_log(s, AV_LOG_ERROR, "Output channel layout isnt supported\n");
  84. return AVERROR(EINVAL);
  85. }
  86. //FIXME implement dolby surround
  87. //FIXME implement full ac3
  88. if(unaccounted & AV_CH_FRONT_CENTER){
  89. if((s->out_ch_layout & AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO){
  90. matrix[ FRONT_LEFT][FRONT_CENTER]+= M_SQRT1_2;
  91. matrix[FRONT_RIGHT][FRONT_CENTER]+= M_SQRT1_2;
  92. }else
  93. av_assert0(0);
  94. }
  95. if(unaccounted & AV_CH_LAYOUT_STEREO){
  96. if(s->out_ch_layout & AV_CH_FRONT_CENTER){
  97. matrix[FRONT_CENTER][ FRONT_LEFT]+= M_SQRT1_2;
  98. matrix[FRONT_CENTER][FRONT_RIGHT]+= M_SQRT1_2;
  99. if(s->in_ch_layout & AV_CH_FRONT_CENTER)
  100. matrix[FRONT_CENTER][ FRONT_CENTER] = s->clev*sqrt(2);
  101. }else
  102. av_assert0(0);
  103. }
  104. if(unaccounted & AV_CH_BACK_CENTER){
  105. if(s->out_ch_layout & AV_CH_BACK_LEFT){
  106. matrix[ BACK_LEFT][BACK_CENTER]+= M_SQRT1_2;
  107. matrix[BACK_RIGHT][BACK_CENTER]+= M_SQRT1_2;
  108. }else if(s->out_ch_layout & AV_CH_SIDE_LEFT){
  109. matrix[ SIDE_LEFT][BACK_CENTER]+= M_SQRT1_2;
  110. matrix[SIDE_RIGHT][BACK_CENTER]+= M_SQRT1_2;
  111. }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
  112. matrix[ FRONT_LEFT][BACK_CENTER]+= s->slev*M_SQRT1_2;
  113. matrix[FRONT_RIGHT][BACK_CENTER]+= s->slev*M_SQRT1_2;
  114. }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
  115. matrix[ FRONT_CENTER][BACK_CENTER]+= s->slev*M_SQRT1_2;
  116. }else
  117. av_assert0(0);
  118. }
  119. if(unaccounted & AV_CH_BACK_LEFT){
  120. if(s->out_ch_layout & AV_CH_BACK_CENTER){
  121. matrix[BACK_CENTER][ BACK_LEFT]+= M_SQRT1_2;
  122. matrix[BACK_CENTER][BACK_RIGHT]+= M_SQRT1_2;
  123. }else if(s->out_ch_layout & AV_CH_SIDE_LEFT){
  124. if(s->in_ch_layout & AV_CH_SIDE_LEFT){
  125. matrix[ SIDE_LEFT][ BACK_LEFT]+= M_SQRT1_2;
  126. matrix[SIDE_RIGHT][BACK_RIGHT]+= M_SQRT1_2;
  127. }else{
  128. matrix[ SIDE_LEFT][ BACK_LEFT]+= 1.0;
  129. matrix[SIDE_RIGHT][BACK_RIGHT]+= 1.0;
  130. }
  131. }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
  132. matrix[ FRONT_LEFT][ BACK_LEFT]+= s->slev;
  133. matrix[FRONT_RIGHT][BACK_RIGHT]+= s->slev;
  134. }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
  135. matrix[ FRONT_CENTER][BACK_LEFT ]+= s->slev*M_SQRT1_2;
  136. matrix[ FRONT_CENTER][BACK_RIGHT]+= s->slev*M_SQRT1_2;
  137. }else
  138. av_assert0(0);
  139. }
  140. if(unaccounted & AV_CH_SIDE_LEFT){
  141. if(s->out_ch_layout & AV_CH_BACK_LEFT){
  142. matrix[ BACK_LEFT][ SIDE_LEFT]+= 1.0;
  143. matrix[BACK_RIGHT][SIDE_RIGHT]+= 1.0;
  144. }else if(s->out_ch_layout & AV_CH_BACK_CENTER){
  145. matrix[BACK_CENTER][ SIDE_LEFT]+= M_SQRT1_2;
  146. matrix[BACK_CENTER][SIDE_RIGHT]+= M_SQRT1_2;
  147. }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
  148. matrix[ FRONT_LEFT][ SIDE_LEFT]+= s->slev;
  149. matrix[FRONT_RIGHT][SIDE_RIGHT]+= s->slev;
  150. }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
  151. matrix[ FRONT_CENTER][SIDE_LEFT ]+= s->slev*M_SQRT1_2;
  152. matrix[ FRONT_CENTER][SIDE_RIGHT]+= s->slev*M_SQRT1_2;
  153. }else
  154. av_assert0(0);
  155. }
  156. if(unaccounted & AV_CH_FRONT_LEFT_OF_CENTER){
  157. if(s->out_ch_layout & AV_CH_FRONT_LEFT){
  158. matrix[ FRONT_LEFT][ FRONT_LEFT_OF_CENTER]+= 1.0;
  159. matrix[FRONT_RIGHT][FRONT_RIGHT_OF_CENTER]+= 1.0;
  160. }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
  161. matrix[ FRONT_CENTER][ FRONT_LEFT_OF_CENTER]+= M_SQRT1_2;
  162. matrix[ FRONT_CENTER][FRONT_RIGHT_OF_CENTER]+= M_SQRT1_2;
  163. }else
  164. av_assert0(0);
  165. }
  166. //FIXME quantize for integeres
  167. for(out_i=i=0; i<64; i++){
  168. double sum=0;
  169. int in_i=0;
  170. int ch_in=0;
  171. for(j=0; j<64; j++){
  172. s->matrix[out_i][in_i]= matrix[i][j];
  173. if(matrix[i][j]){
  174. s->matrix_ch[out_i][++ch_in]= in_i;
  175. sum += fabs(matrix[i][j]);
  176. }
  177. if(s->in_ch_layout & (1ULL<<j))
  178. in_i++;
  179. }
  180. s->matrix_ch[out_i][0]= ch_in;
  181. maxcoef= FFMAX(maxcoef, sum);
  182. if(s->out_ch_layout & (1ULL<<i))
  183. out_i++;
  184. }
  185. if(( s->out_sample_fmt < AV_SAMPLE_FMT_FLT
  186. || s->int_sample_fmt < AV_SAMPLE_FMT_FLT) && maxcoef > 1.0){
  187. for(i=0; i<SWR_CH_MAX; i++)
  188. for(j=0; j<SWR_CH_MAX; j++)
  189. s->matrix[i][j] /= maxcoef;
  190. }
  191. for(i=0; i<av_get_channel_layout_nb_channels(s->out_ch_layout); i++){
  192. for(j=0; j<av_get_channel_layout_nb_channels(s->in_ch_layout); j++){
  193. av_log(NULL, AV_LOG_ERROR, "%f ", s->matrix[i][j]);
  194. }
  195. av_log(NULL, AV_LOG_ERROR, "\n");
  196. }
  197. return 0;
  198. }
  199. int swr_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy){
  200. int out_i, in_i, i, j;
  201. av_assert0(out->ch_count == av_get_channel_layout_nb_channels(s->out_ch_layout));
  202. av_assert0(in ->ch_count == av_get_channel_layout_nb_channels(s-> in_ch_layout));
  203. for(out_i=0; out_i<out->ch_count; out_i++){
  204. switch(s->matrix_ch[out_i][0]){
  205. case 1:
  206. in_i= s->matrix_ch[out_i][1];
  207. if(mustcopy || s->matrix[out_i][in_i]!=1.0){
  208. if(s->int_sample_fmt == AV_SAMPLE_FMT_FLT){
  209. copy_float(out->ch[out_i], in->ch[in_i], s->matrix[out_i][in_i], len);
  210. }else
  211. copy_s16 (out->ch[out_i], in->ch[in_i], s->matrix[out_i][in_i], len);
  212. }else{
  213. out->ch[out_i]= in->ch[in_i];
  214. }
  215. break;
  216. case 2:
  217. if(s->int_sample_fmt == AV_SAMPLE_FMT_FLT){
  218. sum2_float(out->ch[out_i], in->ch[ s->matrix_ch[out_i][1] ], in->ch[ s->matrix_ch[out_i][2] ],
  219. s->matrix[out_i][ s->matrix_ch[out_i][1] ], s->matrix[out_i][ s->matrix_ch[out_i][2] ],
  220. len);
  221. }else{
  222. sum2_s16 (out->ch[out_i], in->ch[ s->matrix_ch[out_i][1] ], in->ch[ s->matrix_ch[out_i][2] ],
  223. s->matrix[out_i][ s->matrix_ch[out_i][1] ], s->matrix[out_i][ s->matrix_ch[out_i][2] ],
  224. len);
  225. }
  226. break;
  227. default:
  228. if(s->int_sample_fmt == AV_SAMPLE_FMT_FLT){
  229. for(i=0; i<len; i++){
  230. float v=0;
  231. for(j=0; j<s->matrix_ch[out_i][0]; j++){
  232. in_i= s->matrix_ch[out_i][1+j];
  233. v+= ((float*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
  234. }
  235. ((float*)out->ch[out_i])[i]= v;
  236. }
  237. }else{
  238. for(i=0; i<len; i++){
  239. int v=0;
  240. for(j=0; j<s->matrix_ch[out_i][0]; j++){
  241. in_i= s->matrix_ch[out_i][1+j];
  242. v+= ((int16_t*)in->ch[in_i])[i] * s->matrix[out_i][in_i]; //FIXME use int16 coeffs
  243. }
  244. ((int16_t*)out->ch[out_i])[i]= v;
  245. }
  246. }
  247. }
  248. }
  249. return 0;
  250. }