rematrix.c 10 KB

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