audio_mix.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdint.h>
  21. #include "libavutil/libm.h"
  22. #include "libavutil/samplefmt.h"
  23. #include "avresample.h"
  24. #include "internal.h"
  25. #include "audio_data.h"
  26. #include "audio_mix.h"
  27. static const char *coeff_type_names[] = { "q8", "q15", "flt" };
  28. void ff_audio_mix_set_func(AudioMix *am, enum AVSampleFormat fmt,
  29. enum AVMixCoeffType coeff_type, int in_channels,
  30. int out_channels, int ptr_align, int samples_align,
  31. const char *descr, void *mix_func)
  32. {
  33. if (fmt == am->fmt && coeff_type == am->coeff_type &&
  34. ( in_channels == am->in_channels || in_channels == 0) &&
  35. (out_channels == am->out_channels || out_channels == 0)) {
  36. char chan_str[16];
  37. am->mix = mix_func;
  38. am->func_descr = descr;
  39. am->ptr_align = ptr_align;
  40. am->samples_align = samples_align;
  41. if (ptr_align == 1 && samples_align == 1) {
  42. am->mix_generic = mix_func;
  43. am->func_descr_generic = descr;
  44. } else {
  45. am->has_optimized_func = 1;
  46. }
  47. if (in_channels) {
  48. if (out_channels)
  49. snprintf(chan_str, sizeof(chan_str), "[%d to %d] ",
  50. in_channels, out_channels);
  51. else
  52. snprintf(chan_str, sizeof(chan_str), "[%d to any] ",
  53. in_channels);
  54. } else if (out_channels) {
  55. snprintf(chan_str, sizeof(chan_str), "[any to %d] ",
  56. out_channels);
  57. }
  58. av_log(am->avr, AV_LOG_DEBUG, "audio_mix: found function: [fmt=%s] "
  59. "[c=%s] %s(%s)\n", av_get_sample_fmt_name(fmt),
  60. coeff_type_names[coeff_type],
  61. (in_channels || out_channels) ? chan_str : "", descr);
  62. }
  63. }
  64. #define MIX_FUNC_NAME(fmt, cfmt) mix_any_ ## fmt ##_## cfmt ##_c
  65. #define MIX_FUNC_GENERIC(fmt, cfmt, stype, ctype, sumtype, expr) \
  66. static void MIX_FUNC_NAME(fmt, cfmt)(stype **samples, ctype **matrix, \
  67. int len, int out_ch, int in_ch) \
  68. { \
  69. int i, in, out; \
  70. stype temp[AVRESAMPLE_MAX_CHANNELS]; \
  71. for (i = 0; i < len; i++) { \
  72. for (out = 0; out < out_ch; out++) { \
  73. sumtype sum = 0; \
  74. for (in = 0; in < in_ch; in++) \
  75. sum += samples[in][i] * matrix[out][in]; \
  76. temp[out] = expr; \
  77. } \
  78. for (out = 0; out < out_ch; out++) \
  79. samples[out][i] = temp[out]; \
  80. } \
  81. }
  82. MIX_FUNC_GENERIC(FLTP, FLT, float, float, float, sum)
  83. MIX_FUNC_GENERIC(S16P, FLT, int16_t, float, float, av_clip_int16(lrintf(sum)))
  84. MIX_FUNC_GENERIC(S16P, Q15, int16_t, int32_t, int64_t, av_clip_int16(sum >> 15))
  85. MIX_FUNC_GENERIC(S16P, Q8, int16_t, int16_t, int32_t, av_clip_int16(sum >> 8))
  86. /* TODO: templatize the channel-specific C functions */
  87. static void mix_2_to_1_fltp_flt_c(float **samples, float **matrix, int len,
  88. int out_ch, int in_ch)
  89. {
  90. float *src0 = samples[0];
  91. float *src1 = samples[1];
  92. float *dst = src0;
  93. float m0 = matrix[0][0];
  94. float m1 = matrix[0][1];
  95. while (len > 4) {
  96. *dst++ = *src0++ * m0 + *src1++ * m1;
  97. *dst++ = *src0++ * m0 + *src1++ * m1;
  98. *dst++ = *src0++ * m0 + *src1++ * m1;
  99. *dst++ = *src0++ * m0 + *src1++ * m1;
  100. len -= 4;
  101. }
  102. while (len > 0) {
  103. *dst++ = *src0++ * m0 + *src1++ * m1;
  104. len--;
  105. }
  106. }
  107. static void mix_1_to_2_fltp_flt_c(float **samples, float **matrix, int len,
  108. int out_ch, int in_ch)
  109. {
  110. float v;
  111. float *dst0 = samples[0];
  112. float *dst1 = samples[1];
  113. float *src = dst0;
  114. float m0 = matrix[0][0];
  115. float m1 = matrix[1][0];
  116. while (len > 4) {
  117. v = *src++;
  118. *dst0++ = v * m1;
  119. *dst1++ = v * m0;
  120. v = *src++;
  121. *dst0++ = v * m1;
  122. *dst1++ = v * m0;
  123. v = *src++;
  124. *dst0++ = v * m1;
  125. *dst1++ = v * m0;
  126. v = *src++;
  127. *dst0++ = v * m1;
  128. *dst1++ = v * m0;
  129. len -= 4;
  130. }
  131. while (len > 0) {
  132. v = *src++;
  133. *dst0++ = v * m1;
  134. *dst1++ = v * m0;
  135. len--;
  136. }
  137. }
  138. static void mix_6_to_2_fltp_flt_c(float **samples, float **matrix, int len,
  139. int out_ch, int in_ch)
  140. {
  141. float v0, v1;
  142. float *src0 = samples[0];
  143. float *src1 = samples[1];
  144. float *src2 = samples[2];
  145. float *src3 = samples[3];
  146. float *src4 = samples[4];
  147. float *src5 = samples[5];
  148. float *dst0 = src0;
  149. float *dst1 = src1;
  150. float *m0 = matrix[0];
  151. float *m1 = matrix[1];
  152. while (len > 0) {
  153. v0 = *src0++;
  154. v1 = *src1++;
  155. *dst0++ = v0 * m0[0] +
  156. v1 * m0[1] +
  157. *src2 * m0[2] +
  158. *src3 * m0[3] +
  159. *src4 * m0[4] +
  160. *src5 * m0[5];
  161. *dst1++ = v0 * m1[0] +
  162. v1 * m1[1] +
  163. *src2++ * m1[2] +
  164. *src3++ * m1[3] +
  165. *src4++ * m1[4] +
  166. *src5++ * m1[5];
  167. len--;
  168. }
  169. }
  170. static void mix_2_to_6_fltp_flt_c(float **samples, float **matrix, int len,
  171. int out_ch, int in_ch)
  172. {
  173. float v0, v1;
  174. float *dst0 = samples[0];
  175. float *dst1 = samples[1];
  176. float *dst2 = samples[2];
  177. float *dst3 = samples[3];
  178. float *dst4 = samples[4];
  179. float *dst5 = samples[5];
  180. float *src0 = dst0;
  181. float *src1 = dst1;
  182. while (len > 0) {
  183. v0 = *src0++;
  184. v1 = *src1++;
  185. *dst0++ = v0 * matrix[0][0] + v1 * matrix[0][1];
  186. *dst1++ = v0 * matrix[1][0] + v1 * matrix[1][1];
  187. *dst2++ = v0 * matrix[2][0] + v1 * matrix[2][1];
  188. *dst3++ = v0 * matrix[3][0] + v1 * matrix[3][1];
  189. *dst4++ = v0 * matrix[4][0] + v1 * matrix[4][1];
  190. *dst5++ = v0 * matrix[5][0] + v1 * matrix[5][1];
  191. len--;
  192. }
  193. }
  194. static int mix_function_init(AudioMix *am)
  195. {
  196. /* any-to-any C versions */
  197. ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
  198. 0, 0, 1, 1, "C", MIX_FUNC_NAME(FLTP, FLT));
  199. ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_FLT,
  200. 0, 0, 1, 1, "C", MIX_FUNC_NAME(S16P, FLT));
  201. ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_Q15,
  202. 0, 0, 1, 1, "C", MIX_FUNC_NAME(S16P, Q15));
  203. ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_Q8,
  204. 0, 0, 1, 1, "C", MIX_FUNC_NAME(S16P, Q8));
  205. /* channel-specific C versions */
  206. ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
  207. 2, 1, 1, 1, "C", mix_2_to_1_fltp_flt_c);
  208. ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
  209. 1, 2, 1, 1, "C", mix_1_to_2_fltp_flt_c);
  210. ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
  211. 6, 2, 1, 1, "C", mix_6_to_2_fltp_flt_c);
  212. ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
  213. 2, 6, 1, 1, "C", mix_2_to_6_fltp_flt_c);
  214. if (ARCH_X86)
  215. ff_audio_mix_init_x86(am);
  216. if (!am->mix) {
  217. av_log(am->avr, AV_LOG_ERROR, "audio_mix: NO FUNCTION FOUND: [fmt=%s] "
  218. "[c=%s] [%d to %d]\n", av_get_sample_fmt_name(am->fmt),
  219. coeff_type_names[am->coeff_type], am->in_channels,
  220. am->out_channels);
  221. return AVERROR_PATCHWELCOME;
  222. }
  223. return 0;
  224. }
  225. int ff_audio_mix_init(AVAudioResampleContext *avr)
  226. {
  227. int ret;
  228. /* build matrix if the user did not already set one */
  229. if (!avr->am->matrix) {
  230. int i, j;
  231. char in_layout_name[128];
  232. char out_layout_name[128];
  233. double *matrix_dbl = av_mallocz(avr->out_channels * avr->in_channels *
  234. sizeof(*matrix_dbl));
  235. if (!matrix_dbl)
  236. return AVERROR(ENOMEM);
  237. ret = avresample_build_matrix(avr->in_channel_layout,
  238. avr->out_channel_layout,
  239. avr->center_mix_level,
  240. avr->surround_mix_level,
  241. avr->lfe_mix_level, 1, matrix_dbl,
  242. avr->in_channels);
  243. if (ret < 0) {
  244. av_free(matrix_dbl);
  245. return ret;
  246. }
  247. av_get_channel_layout_string(in_layout_name, sizeof(in_layout_name),
  248. avr->in_channels, avr->in_channel_layout);
  249. av_get_channel_layout_string(out_layout_name, sizeof(out_layout_name),
  250. avr->out_channels, avr->out_channel_layout);
  251. av_log(avr, AV_LOG_DEBUG, "audio_mix: %s to %s\n",
  252. in_layout_name, out_layout_name);
  253. for (i = 0; i < avr->out_channels; i++) {
  254. for (j = 0; j < avr->in_channels; j++) {
  255. av_log(avr, AV_LOG_DEBUG, " %0.3f ",
  256. matrix_dbl[i * avr->in_channels + j]);
  257. }
  258. av_log(avr, AV_LOG_DEBUG, "\n");
  259. }
  260. ret = avresample_set_matrix(avr, matrix_dbl, avr->in_channels);
  261. if (ret < 0) {
  262. av_free(matrix_dbl);
  263. return ret;
  264. }
  265. av_free(matrix_dbl);
  266. }
  267. avr->am->fmt = avr->internal_sample_fmt;
  268. avr->am->coeff_type = avr->mix_coeff_type;
  269. avr->am->in_layout = avr->in_channel_layout;
  270. avr->am->out_layout = avr->out_channel_layout;
  271. avr->am->in_channels = avr->in_channels;
  272. avr->am->out_channels = avr->out_channels;
  273. ret = mix_function_init(avr->am);
  274. if (ret < 0)
  275. return ret;
  276. return 0;
  277. }
  278. void ff_audio_mix_close(AudioMix *am)
  279. {
  280. if (!am)
  281. return;
  282. if (am->matrix) {
  283. av_free(am->matrix[0]);
  284. am->matrix = NULL;
  285. }
  286. memset(am->matrix_q8, 0, sizeof(am->matrix_q8 ));
  287. memset(am->matrix_q15, 0, sizeof(am->matrix_q15));
  288. memset(am->matrix_flt, 0, sizeof(am->matrix_flt));
  289. }
  290. int ff_audio_mix(AudioMix *am, AudioData *src)
  291. {
  292. int use_generic = 1;
  293. int len = src->nb_samples;
  294. /* determine whether to use the optimized function based on pointer and
  295. samples alignment in both the input and output */
  296. if (am->has_optimized_func) {
  297. int aligned_len = FFALIGN(len, am->samples_align);
  298. if (!(src->ptr_align % am->ptr_align) &&
  299. src->samples_align >= aligned_len) {
  300. len = aligned_len;
  301. use_generic = 0;
  302. }
  303. }
  304. av_dlog(am->avr, "audio_mix: %d samples - %d to %d channels (%s)\n",
  305. src->nb_samples, am->in_channels, am->out_channels,
  306. use_generic ? am->func_descr_generic : am->func_descr);
  307. if (use_generic)
  308. am->mix_generic(src->data, am->matrix, len, am->out_channels,
  309. am->in_channels);
  310. else
  311. am->mix(src->data, am->matrix, len, am->out_channels, am->in_channels);
  312. ff_audio_data_set_channels(src, am->out_channels);
  313. return 0;
  314. }