audio_mix.c 15 KB

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