rematrix.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. static int even(int64_t layout){
  59. if(!layout) return 1;
  60. if(layout&(layout-1)) return 1;
  61. return 0;
  62. }
  63. static int sane_layout(int64_t layout){
  64. if(!(layout & AV_CH_LAYOUT_SURROUND)) // at least 1 front speaker
  65. return 0;
  66. if(!even(layout & (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT))) // no asymetric front
  67. return 0;
  68. if(!even(layout & (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT))) // no asymetric side
  69. return 0;
  70. if(!even(layout & (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)))
  71. return 0;
  72. if(!even(layout & (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)))
  73. return 0;
  74. if(av_get_channel_layout_nb_channels(layout) >= SWR_CH_MAX)
  75. return 0;
  76. return 1;
  77. }
  78. int swri_rematrix_init(SwrContext *s){
  79. int i, j, out_i;
  80. double matrix[64][64]={{0}};
  81. int64_t unaccounted= s->in_ch_layout & ~s->out_ch_layout;
  82. double maxcoef=0;
  83. for(i=0; i<64; i++){
  84. if(s->in_ch_layout & s->out_ch_layout & (1LL<<i))
  85. matrix[i][i]= 1.0;
  86. }
  87. if(!sane_layout(s->in_ch_layout)){
  88. av_log(s, AV_LOG_ERROR, "Input channel layout isnt supported\n");
  89. return AVERROR(EINVAL);
  90. }
  91. if(!sane_layout(s->out_ch_layout)){
  92. av_log(s, AV_LOG_ERROR, "Output channel layout isnt supported\n");
  93. return AVERROR(EINVAL);
  94. }
  95. //FIXME implement dolby surround
  96. //FIXME implement full ac3
  97. if(unaccounted & AV_CH_FRONT_CENTER){
  98. if((s->out_ch_layout & AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO){
  99. matrix[ FRONT_LEFT][FRONT_CENTER]+= M_SQRT1_2;
  100. matrix[FRONT_RIGHT][FRONT_CENTER]+= M_SQRT1_2;
  101. }else
  102. av_assert0(0);
  103. }
  104. if(unaccounted & AV_CH_LAYOUT_STEREO){
  105. if(s->out_ch_layout & AV_CH_FRONT_CENTER){
  106. matrix[FRONT_CENTER][ FRONT_LEFT]+= M_SQRT1_2;
  107. matrix[FRONT_CENTER][FRONT_RIGHT]+= M_SQRT1_2;
  108. if(s->in_ch_layout & AV_CH_FRONT_CENTER)
  109. matrix[FRONT_CENTER][ FRONT_CENTER] = s->clev*sqrt(2);
  110. }else
  111. av_assert0(0);
  112. }
  113. if(unaccounted & AV_CH_BACK_CENTER){
  114. if(s->out_ch_layout & AV_CH_BACK_LEFT){
  115. matrix[ BACK_LEFT][BACK_CENTER]+= M_SQRT1_2;
  116. matrix[BACK_RIGHT][BACK_CENTER]+= M_SQRT1_2;
  117. }else if(s->out_ch_layout & AV_CH_SIDE_LEFT){
  118. matrix[ SIDE_LEFT][BACK_CENTER]+= M_SQRT1_2;
  119. matrix[SIDE_RIGHT][BACK_CENTER]+= M_SQRT1_2;
  120. }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
  121. matrix[ FRONT_LEFT][BACK_CENTER]+= s->slev*M_SQRT1_2;
  122. matrix[FRONT_RIGHT][BACK_CENTER]+= s->slev*M_SQRT1_2;
  123. }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
  124. matrix[ FRONT_CENTER][BACK_CENTER]+= s->slev*M_SQRT1_2;
  125. }else
  126. av_assert0(0);
  127. }
  128. if(unaccounted & AV_CH_BACK_LEFT){
  129. if(s->out_ch_layout & AV_CH_BACK_CENTER){
  130. matrix[BACK_CENTER][ BACK_LEFT]+= M_SQRT1_2;
  131. matrix[BACK_CENTER][BACK_RIGHT]+= M_SQRT1_2;
  132. }else if(s->out_ch_layout & AV_CH_SIDE_LEFT){
  133. if(s->in_ch_layout & AV_CH_SIDE_LEFT){
  134. matrix[ SIDE_LEFT][ BACK_LEFT]+= M_SQRT1_2;
  135. matrix[SIDE_RIGHT][BACK_RIGHT]+= M_SQRT1_2;
  136. }else{
  137. matrix[ SIDE_LEFT][ BACK_LEFT]+= 1.0;
  138. matrix[SIDE_RIGHT][BACK_RIGHT]+= 1.0;
  139. }
  140. }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
  141. matrix[ FRONT_LEFT][ BACK_LEFT]+= s->slev;
  142. matrix[FRONT_RIGHT][BACK_RIGHT]+= s->slev;
  143. }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
  144. matrix[ FRONT_CENTER][BACK_LEFT ]+= s->slev*M_SQRT1_2;
  145. matrix[ FRONT_CENTER][BACK_RIGHT]+= s->slev*M_SQRT1_2;
  146. }else
  147. av_assert0(0);
  148. }
  149. if(unaccounted & AV_CH_SIDE_LEFT){
  150. if(s->out_ch_layout & AV_CH_BACK_LEFT){
  151. matrix[ BACK_LEFT][ SIDE_LEFT]+= 1.0;
  152. matrix[BACK_RIGHT][SIDE_RIGHT]+= 1.0;
  153. }else if(s->out_ch_layout & AV_CH_BACK_CENTER){
  154. matrix[BACK_CENTER][ SIDE_LEFT]+= M_SQRT1_2;
  155. matrix[BACK_CENTER][SIDE_RIGHT]+= M_SQRT1_2;
  156. }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
  157. matrix[ FRONT_LEFT][ SIDE_LEFT]+= s->slev;
  158. matrix[FRONT_RIGHT][SIDE_RIGHT]+= s->slev;
  159. }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
  160. matrix[ FRONT_CENTER][SIDE_LEFT ]+= s->slev*M_SQRT1_2;
  161. matrix[ FRONT_CENTER][SIDE_RIGHT]+= s->slev*M_SQRT1_2;
  162. }else
  163. av_assert0(0);
  164. }
  165. if(unaccounted & AV_CH_FRONT_LEFT_OF_CENTER){
  166. if(s->out_ch_layout & AV_CH_FRONT_LEFT){
  167. matrix[ FRONT_LEFT][ FRONT_LEFT_OF_CENTER]+= 1.0;
  168. matrix[FRONT_RIGHT][FRONT_RIGHT_OF_CENTER]+= 1.0;
  169. }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
  170. matrix[ FRONT_CENTER][ FRONT_LEFT_OF_CENTER]+= M_SQRT1_2;
  171. matrix[ FRONT_CENTER][FRONT_RIGHT_OF_CENTER]+= M_SQRT1_2;
  172. }else
  173. av_assert0(0);
  174. }
  175. //FIXME quantize for integeres
  176. for(out_i=i=0; i<64; i++){
  177. double sum=0;
  178. int in_i=0;
  179. int ch_in=0;
  180. for(j=0; j<64; j++){
  181. s->matrix[out_i][in_i]= matrix[i][j];
  182. s->matrix32[out_i][in_i]= lrintf(matrix[i][j] * 32768);
  183. if(matrix[i][j]){
  184. s->matrix_ch[out_i][++ch_in]= in_i;
  185. sum += fabs(matrix[i][j]);
  186. }
  187. if(s->in_ch_layout & (1ULL<<j))
  188. in_i++;
  189. }
  190. s->matrix_ch[out_i][0]= ch_in;
  191. maxcoef= FFMAX(maxcoef, sum);
  192. if(s->out_ch_layout & (1ULL<<i))
  193. out_i++;
  194. }
  195. if(s->rematrix_volume < 0)
  196. maxcoef = -s->rematrix_volume;
  197. if(( s->out_sample_fmt < AV_SAMPLE_FMT_FLT
  198. || s->int_sample_fmt < AV_SAMPLE_FMT_FLT) && maxcoef > 1.0){
  199. for(i=0; i<SWR_CH_MAX; i++)
  200. for(j=0; j<SWR_CH_MAX; j++){
  201. s->matrix[i][j] /= maxcoef;
  202. s->matrix32[i][j]= lrintf(s->matrix[i][j] * 32768);
  203. }
  204. }
  205. if(s->rematrix_volume > 0){
  206. for(i=0; i<SWR_CH_MAX; i++)
  207. for(j=0; j<SWR_CH_MAX; j++){
  208. s->matrix[i][j] *= s->rematrix_volume;
  209. s->matrix32[i][j]= lrintf(s->matrix[i][j] * 32768);
  210. }
  211. }
  212. for(i=0; i<av_get_channel_layout_nb_channels(s->out_ch_layout); i++){
  213. for(j=0; j<av_get_channel_layout_nb_channels(s->in_ch_layout); j++){
  214. av_log(NULL, AV_LOG_DEBUG, "%f ", s->matrix[i][j]);
  215. }
  216. av_log(NULL, AV_LOG_DEBUG, "\n");
  217. }
  218. return 0;
  219. }
  220. int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy){
  221. int out_i, in_i, i, j;
  222. av_assert0(out->ch_count == av_get_channel_layout_nb_channels(s->out_ch_layout));
  223. av_assert0(in ->ch_count == av_get_channel_layout_nb_channels(s-> in_ch_layout));
  224. for(out_i=0; out_i<out->ch_count; out_i++){
  225. switch(s->matrix_ch[out_i][0]){
  226. case 1:
  227. in_i= s->matrix_ch[out_i][1];
  228. if(mustcopy || s->matrix[out_i][in_i]!=1.0){
  229. if(s->int_sample_fmt == AV_SAMPLE_FMT_FLT){
  230. copy_float((float *)out->ch[out_i], (const float *)in->ch[in_i], s->matrix [out_i][in_i], len);
  231. }else
  232. copy_s16 ((int16_t*)out->ch[out_i], (const int16_t*)in->ch[in_i], s->matrix32[out_i][in_i], len);
  233. }else{
  234. out->ch[out_i]= in->ch[in_i];
  235. }
  236. break;
  237. case 2:
  238. if(s->int_sample_fmt == AV_SAMPLE_FMT_FLT){
  239. 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] ],
  240. s->matrix[out_i][ s->matrix_ch[out_i][1] ], s->matrix[out_i][ s->matrix_ch[out_i][2] ],
  241. len);
  242. }else{
  243. 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] ],
  244. s->matrix32[out_i][ s->matrix_ch[out_i][1] ], s->matrix32[out_i][ s->matrix_ch[out_i][2] ],
  245. len);
  246. }
  247. break;
  248. default:
  249. if(s->int_sample_fmt == AV_SAMPLE_FMT_FLT){
  250. for(i=0; i<len; i++){
  251. float v=0;
  252. for(j=0; j<s->matrix_ch[out_i][0]; j++){
  253. in_i= s->matrix_ch[out_i][1+j];
  254. v+= ((float*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
  255. }
  256. ((float*)out->ch[out_i])[i]= v;
  257. }
  258. }else{
  259. for(i=0; i<len; i++){
  260. int v=0;
  261. for(j=0; j<s->matrix_ch[out_i][0]; j++){
  262. in_i= s->matrix_ch[out_i][1+j];
  263. v+= ((int16_t*)in->ch[in_i])[i] * s->matrix32[out_i][in_i];
  264. }
  265. ((int16_t*)out->ch[out_i])[i]= (v + 16384)>>15;
  266. }
  267. }
  268. }
  269. }
  270. return 0;
  271. }