audio_mix.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. struct AudioMix {
  30. AVAudioResampleContext *avr;
  31. enum AVSampleFormat fmt;
  32. enum AVMixCoeffType coeff_type;
  33. uint64_t in_layout;
  34. uint64_t out_layout;
  35. int in_channels;
  36. int out_channels;
  37. int ptr_align;
  38. int samples_align;
  39. int has_optimized_func;
  40. const char *func_descr;
  41. const char *func_descr_generic;
  42. mix_func *mix;
  43. mix_func *mix_generic;
  44. int16_t *matrix_q8[AVRESAMPLE_MAX_CHANNELS];
  45. int32_t *matrix_q15[AVRESAMPLE_MAX_CHANNELS];
  46. float *matrix_flt[AVRESAMPLE_MAX_CHANNELS];
  47. void **matrix;
  48. };
  49. void ff_audio_mix_set_func(AudioMix *am, enum AVSampleFormat fmt,
  50. enum AVMixCoeffType coeff_type, int in_channels,
  51. int out_channels, int ptr_align, int samples_align,
  52. const char *descr, void *mix_func)
  53. {
  54. if (fmt == am->fmt && coeff_type == am->coeff_type &&
  55. ( in_channels == am->in_channels || in_channels == 0) &&
  56. (out_channels == am->out_channels || out_channels == 0)) {
  57. char chan_str[16];
  58. am->mix = mix_func;
  59. am->func_descr = descr;
  60. am->ptr_align = ptr_align;
  61. am->samples_align = samples_align;
  62. if (ptr_align == 1 && samples_align == 1) {
  63. am->mix_generic = mix_func;
  64. am->func_descr_generic = descr;
  65. } else {
  66. am->has_optimized_func = 1;
  67. }
  68. if (in_channels) {
  69. if (out_channels)
  70. snprintf(chan_str, sizeof(chan_str), "[%d to %d] ",
  71. in_channels, out_channels);
  72. else
  73. snprintf(chan_str, sizeof(chan_str), "[%d to any] ",
  74. in_channels);
  75. } else if (out_channels) {
  76. snprintf(chan_str, sizeof(chan_str), "[any to %d] ",
  77. out_channels);
  78. }
  79. av_log(am->avr, AV_LOG_DEBUG, "audio_mix: found function: [fmt=%s] "
  80. "[c=%s] %s(%s)\n", av_get_sample_fmt_name(fmt),
  81. coeff_type_names[coeff_type],
  82. (in_channels || out_channels) ? chan_str : "", descr);
  83. }
  84. }
  85. #define MIX_FUNC_NAME(fmt, cfmt) mix_any_ ## fmt ##_## cfmt ##_c
  86. #define MIX_FUNC_GENERIC(fmt, cfmt, stype, ctype, sumtype, expr) \
  87. static void MIX_FUNC_NAME(fmt, cfmt)(stype **samples, ctype **matrix, \
  88. int len, int out_ch, int in_ch) \
  89. { \
  90. int i, in, out; \
  91. stype temp[AVRESAMPLE_MAX_CHANNELS]; \
  92. for (i = 0; i < len; i++) { \
  93. for (out = 0; out < out_ch; out++) { \
  94. sumtype sum = 0; \
  95. for (in = 0; in < in_ch; in++) \
  96. sum += samples[in][i] * matrix[out][in]; \
  97. temp[out] = expr; \
  98. } \
  99. for (out = 0; out < out_ch; out++) \
  100. samples[out][i] = temp[out]; \
  101. } \
  102. }
  103. MIX_FUNC_GENERIC(FLTP, FLT, float, float, float, sum)
  104. MIX_FUNC_GENERIC(S16P, FLT, int16_t, float, float, av_clip_int16(lrintf(sum)))
  105. MIX_FUNC_GENERIC(S16P, Q15, int16_t, int32_t, int64_t, av_clip_int16(sum >> 15))
  106. MIX_FUNC_GENERIC(S16P, Q8, int16_t, int16_t, int32_t, av_clip_int16(sum >> 8))
  107. /* TODO: templatize the channel-specific C functions */
  108. static void mix_2_to_1_fltp_flt_c(float **samples, float **matrix, int len,
  109. int out_ch, int in_ch)
  110. {
  111. float *src0 = samples[0];
  112. float *src1 = samples[1];
  113. float *dst = src0;
  114. float m0 = matrix[0][0];
  115. float m1 = matrix[0][1];
  116. while (len > 4) {
  117. *dst++ = *src0++ * m0 + *src1++ * m1;
  118. *dst++ = *src0++ * m0 + *src1++ * m1;
  119. *dst++ = *src0++ * m0 + *src1++ * m1;
  120. *dst++ = *src0++ * m0 + *src1++ * m1;
  121. len -= 4;
  122. }
  123. while (len > 0) {
  124. *dst++ = *src0++ * m0 + *src1++ * m1;
  125. len--;
  126. }
  127. }
  128. static void mix_2_to_1_s16p_flt_c(int16_t **samples, float **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. float m0 = matrix[0][0];
  135. float m1 = matrix[0][1];
  136. while (len > 4) {
  137. *dst++ = av_clip_int16(lrintf(*src0++ * m0 + *src1++ * m1));
  138. *dst++ = av_clip_int16(lrintf(*src0++ * m0 + *src1++ * m1));
  139. *dst++ = av_clip_int16(lrintf(*src0++ * m0 + *src1++ * m1));
  140. *dst++ = av_clip_int16(lrintf(*src0++ * m0 + *src1++ * m1));
  141. len -= 4;
  142. }
  143. while (len > 0) {
  144. *dst++ = av_clip_int16(lrintf(*src0++ * m0 + *src1++ * m1));
  145. len--;
  146. }
  147. }
  148. static void mix_2_to_1_s16p_q8_c(int16_t **samples, int16_t **matrix, int len,
  149. int out_ch, int in_ch)
  150. {
  151. int16_t *src0 = samples[0];
  152. int16_t *src1 = samples[1];
  153. int16_t *dst = src0;
  154. int16_t m0 = matrix[0][0];
  155. int16_t m1 = matrix[0][1];
  156. while (len > 4) {
  157. *dst++ = (*src0++ * m0 + *src1++ * m1) >> 8;
  158. *dst++ = (*src0++ * m0 + *src1++ * m1) >> 8;
  159. *dst++ = (*src0++ * m0 + *src1++ * m1) >> 8;
  160. *dst++ = (*src0++ * m0 + *src1++ * m1) >> 8;
  161. len -= 4;
  162. }
  163. while (len > 0) {
  164. *dst++ = (*src0++ * m0 + *src1++ * m1) >> 8;
  165. len--;
  166. }
  167. }
  168. static void mix_1_to_2_fltp_flt_c(float **samples, float **matrix, int len,
  169. int out_ch, int in_ch)
  170. {
  171. float v;
  172. float *dst0 = samples[0];
  173. float *dst1 = samples[1];
  174. float *src = dst0;
  175. float m0 = matrix[0][0];
  176. float m1 = matrix[1][0];
  177. while (len > 4) {
  178. v = *src++;
  179. *dst0++ = v * m0;
  180. *dst1++ = v * m1;
  181. v = *src++;
  182. *dst0++ = v * m0;
  183. *dst1++ = v * m1;
  184. v = *src++;
  185. *dst0++ = v * m0;
  186. *dst1++ = v * m1;
  187. v = *src++;
  188. *dst0++ = v * m0;
  189. *dst1++ = v * m1;
  190. len -= 4;
  191. }
  192. while (len > 0) {
  193. v = *src++;
  194. *dst0++ = v * m0;
  195. *dst1++ = v * m1;
  196. len--;
  197. }
  198. }
  199. static void mix_6_to_2_fltp_flt_c(float **samples, float **matrix, int len,
  200. int out_ch, int in_ch)
  201. {
  202. float v0, v1;
  203. float *src0 = samples[0];
  204. float *src1 = samples[1];
  205. float *src2 = samples[2];
  206. float *src3 = samples[3];
  207. float *src4 = samples[4];
  208. float *src5 = samples[5];
  209. float *dst0 = src0;
  210. float *dst1 = src1;
  211. float *m0 = matrix[0];
  212. float *m1 = matrix[1];
  213. while (len > 0) {
  214. v0 = *src0++;
  215. v1 = *src1++;
  216. *dst0++ = v0 * m0[0] +
  217. v1 * m0[1] +
  218. *src2 * m0[2] +
  219. *src3 * m0[3] +
  220. *src4 * m0[4] +
  221. *src5 * m0[5];
  222. *dst1++ = v0 * m1[0] +
  223. v1 * m1[1] +
  224. *src2++ * m1[2] +
  225. *src3++ * m1[3] +
  226. *src4++ * m1[4] +
  227. *src5++ * m1[5];
  228. len--;
  229. }
  230. }
  231. static void mix_2_to_6_fltp_flt_c(float **samples, float **matrix, int len,
  232. int out_ch, int in_ch)
  233. {
  234. float v0, v1;
  235. float *dst0 = samples[0];
  236. float *dst1 = samples[1];
  237. float *dst2 = samples[2];
  238. float *dst3 = samples[3];
  239. float *dst4 = samples[4];
  240. float *dst5 = samples[5];
  241. float *src0 = dst0;
  242. float *src1 = dst1;
  243. while (len > 0) {
  244. v0 = *src0++;
  245. v1 = *src1++;
  246. *dst0++ = v0 * matrix[0][0] + v1 * matrix[0][1];
  247. *dst1++ = v0 * matrix[1][0] + v1 * matrix[1][1];
  248. *dst2++ = v0 * matrix[2][0] + v1 * matrix[2][1];
  249. *dst3++ = v0 * matrix[3][0] + v1 * matrix[3][1];
  250. *dst4++ = v0 * matrix[4][0] + v1 * matrix[4][1];
  251. *dst5++ = v0 * matrix[5][0] + v1 * matrix[5][1];
  252. len--;
  253. }
  254. }
  255. static int mix_function_init(AudioMix *am)
  256. {
  257. /* any-to-any C versions */
  258. ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
  259. 0, 0, 1, 1, "C", MIX_FUNC_NAME(FLTP, FLT));
  260. ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_FLT,
  261. 0, 0, 1, 1, "C", MIX_FUNC_NAME(S16P, FLT));
  262. ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_Q15,
  263. 0, 0, 1, 1, "C", MIX_FUNC_NAME(S16P, Q15));
  264. ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_Q8,
  265. 0, 0, 1, 1, "C", MIX_FUNC_NAME(S16P, Q8));
  266. /* channel-specific C versions */
  267. ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
  268. 2, 1, 1, 1, "C", mix_2_to_1_fltp_flt_c);
  269. ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_FLT,
  270. 2, 1, 1, 1, "C", mix_2_to_1_s16p_flt_c);
  271. ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_Q8,
  272. 2, 1, 1, 1, "C", mix_2_to_1_s16p_q8_c);
  273. ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
  274. 1, 2, 1, 1, "C", mix_1_to_2_fltp_flt_c);
  275. ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
  276. 6, 2, 1, 1, "C", mix_6_to_2_fltp_flt_c);
  277. ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
  278. 2, 6, 1, 1, "C", mix_2_to_6_fltp_flt_c);
  279. if (ARCH_X86)
  280. ff_audio_mix_init_x86(am);
  281. if (!am->mix) {
  282. av_log(am->avr, AV_LOG_ERROR, "audio_mix: NO FUNCTION FOUND: [fmt=%s] "
  283. "[c=%s] [%d to %d]\n", av_get_sample_fmt_name(am->fmt),
  284. coeff_type_names[am->coeff_type], am->in_channels,
  285. am->out_channels);
  286. return AVERROR_PATCHWELCOME;
  287. }
  288. return 0;
  289. }
  290. AudioMix *ff_audio_mix_alloc(AVAudioResampleContext *avr)
  291. {
  292. AudioMix *am;
  293. int ret;
  294. am = av_mallocz(sizeof(*am));
  295. if (!am)
  296. return NULL;
  297. am->avr = avr;
  298. if (avr->internal_sample_fmt != AV_SAMPLE_FMT_S16P &&
  299. avr->internal_sample_fmt != AV_SAMPLE_FMT_FLTP) {
  300. av_log(avr, AV_LOG_ERROR, "Unsupported internal format for "
  301. "mixing: %s\n",
  302. av_get_sample_fmt_name(avr->internal_sample_fmt));
  303. goto error;
  304. }
  305. am->fmt = avr->internal_sample_fmt;
  306. am->coeff_type = avr->mix_coeff_type;
  307. am->in_layout = avr->in_channel_layout;
  308. am->out_layout = avr->out_channel_layout;
  309. am->in_channels = avr->in_channels;
  310. am->out_channels = avr->out_channels;
  311. /* build matrix if the user did not already set one */
  312. if (avr->mix_matrix) {
  313. ret = ff_audio_mix_set_matrix(am, avr->mix_matrix, avr->in_channels);
  314. if (ret < 0)
  315. goto error;
  316. av_freep(&avr->mix_matrix);
  317. } else {
  318. int i, j;
  319. char in_layout_name[128];
  320. char out_layout_name[128];
  321. double *matrix_dbl = av_mallocz(avr->out_channels * avr->in_channels *
  322. sizeof(*matrix_dbl));
  323. if (!matrix_dbl)
  324. goto error;
  325. ret = avresample_build_matrix(avr->in_channel_layout,
  326. avr->out_channel_layout,
  327. avr->center_mix_level,
  328. avr->surround_mix_level,
  329. avr->lfe_mix_level,
  330. avr->normalize_mix_level,
  331. matrix_dbl,
  332. avr->in_channels,
  333. avr->matrix_encoding);
  334. if (ret < 0) {
  335. av_free(matrix_dbl);
  336. goto error;
  337. }
  338. av_get_channel_layout_string(in_layout_name, sizeof(in_layout_name),
  339. avr->in_channels, avr->in_channel_layout);
  340. av_get_channel_layout_string(out_layout_name, sizeof(out_layout_name),
  341. avr->out_channels, avr->out_channel_layout);
  342. av_log(avr, AV_LOG_DEBUG, "audio_mix: %s to %s\n",
  343. in_layout_name, out_layout_name);
  344. for (i = 0; i < avr->out_channels; i++) {
  345. for (j = 0; j < avr->in_channels; j++) {
  346. av_log(avr, AV_LOG_DEBUG, " %0.3f ",
  347. matrix_dbl[i * avr->in_channels + j]);
  348. }
  349. av_log(avr, AV_LOG_DEBUG, "\n");
  350. }
  351. ret = ff_audio_mix_set_matrix(am, matrix_dbl, avr->in_channels);
  352. if (ret < 0) {
  353. av_free(matrix_dbl);
  354. goto error;
  355. }
  356. av_free(matrix_dbl);
  357. }
  358. return am;
  359. error:
  360. av_free(am);
  361. return NULL;
  362. }
  363. void ff_audio_mix_free(AudioMix **am_p)
  364. {
  365. AudioMix *am;
  366. if (!*am_p)
  367. return;
  368. am = *am_p;
  369. if (am->matrix) {
  370. av_free(am->matrix[0]);
  371. am->matrix = NULL;
  372. }
  373. memset(am->matrix_q8, 0, sizeof(am->matrix_q8 ));
  374. memset(am->matrix_q15, 0, sizeof(am->matrix_q15));
  375. memset(am->matrix_flt, 0, sizeof(am->matrix_flt));
  376. av_freep(am_p);
  377. }
  378. int ff_audio_mix(AudioMix *am, AudioData *src)
  379. {
  380. int use_generic = 1;
  381. int len = src->nb_samples;
  382. /* determine whether to use the optimized function based on pointer and
  383. samples alignment in both the input and output */
  384. if (am->has_optimized_func) {
  385. int aligned_len = FFALIGN(len, am->samples_align);
  386. if (!(src->ptr_align % am->ptr_align) &&
  387. src->samples_align >= aligned_len) {
  388. len = aligned_len;
  389. use_generic = 0;
  390. }
  391. }
  392. av_dlog(am->avr, "audio_mix: %d samples - %d to %d channels (%s)\n",
  393. src->nb_samples, am->in_channels, am->out_channels,
  394. use_generic ? am->func_descr_generic : am->func_descr);
  395. if (use_generic)
  396. am->mix_generic(src->data, am->matrix, len, am->out_channels,
  397. am->in_channels);
  398. else
  399. am->mix(src->data, am->matrix, len, am->out_channels, am->in_channels);
  400. ff_audio_data_set_channels(src, am->out_channels);
  401. return 0;
  402. }
  403. int ff_audio_mix_get_matrix(AudioMix *am, double *matrix, int stride)
  404. {
  405. int i, o;
  406. if ( am->in_channels <= 0 || am->in_channels > AVRESAMPLE_MAX_CHANNELS ||
  407. am->out_channels <= 0 || am->out_channels > AVRESAMPLE_MAX_CHANNELS) {
  408. av_log(am->avr, AV_LOG_ERROR, "Invalid channel counts\n");
  409. return AVERROR(EINVAL);
  410. }
  411. #define GET_MATRIX_CONVERT(suffix, scale) \
  412. if (!am->matrix_ ## suffix[0]) { \
  413. av_log(am->avr, AV_LOG_ERROR, "matrix is not set\n"); \
  414. return AVERROR(EINVAL); \
  415. } \
  416. for (o = 0; o < am->out_channels; o++) \
  417. for (i = 0; i < am->in_channels; i++) \
  418. matrix[o * stride + i] = am->matrix_ ## suffix[o][i] * (scale);
  419. switch (am->coeff_type) {
  420. case AV_MIX_COEFF_TYPE_Q8:
  421. GET_MATRIX_CONVERT(q8, 1.0 / 256.0);
  422. break;
  423. case AV_MIX_COEFF_TYPE_Q15:
  424. GET_MATRIX_CONVERT(q15, 1.0 / 32768.0);
  425. break;
  426. case AV_MIX_COEFF_TYPE_FLT:
  427. GET_MATRIX_CONVERT(flt, 1.0);
  428. break;
  429. default:
  430. av_log(am->avr, AV_LOG_ERROR, "Invalid mix coeff type\n");
  431. return AVERROR(EINVAL);
  432. }
  433. return 0;
  434. }
  435. int ff_audio_mix_set_matrix(AudioMix *am, const double *matrix, int stride)
  436. {
  437. int i, o;
  438. if ( am->in_channels <= 0 || am->in_channels > AVRESAMPLE_MAX_CHANNELS ||
  439. am->out_channels <= 0 || am->out_channels > AVRESAMPLE_MAX_CHANNELS) {
  440. av_log(am->avr, AV_LOG_ERROR, "Invalid channel counts\n");
  441. return AVERROR(EINVAL);
  442. }
  443. if (am->matrix) {
  444. av_free(am->matrix[0]);
  445. am->matrix = NULL;
  446. }
  447. #define CONVERT_MATRIX(type, expr) \
  448. am->matrix_## type[0] = av_mallocz(am->out_channels * am->in_channels * \
  449. sizeof(*am->matrix_## type[0])); \
  450. if (!am->matrix_## type[0]) \
  451. return AVERROR(ENOMEM); \
  452. for (o = 0; o < am->out_channels; o++) { \
  453. if (o > 0) \
  454. am->matrix_## type[o] = am->matrix_## type[o - 1] + \
  455. am->in_channels; \
  456. for (i = 0; i < am->in_channels; i++) { \
  457. double v = matrix[o * stride + i]; \
  458. am->matrix_## type[o][i] = expr; \
  459. } \
  460. } \
  461. am->matrix = (void **)am->matrix_## type;
  462. switch (am->coeff_type) {
  463. case AV_MIX_COEFF_TYPE_Q8:
  464. CONVERT_MATRIX(q8, av_clip_int16(lrint(256.0 * v)))
  465. break;
  466. case AV_MIX_COEFF_TYPE_Q15:
  467. CONVERT_MATRIX(q15, av_clipl_int32(llrint(32768.0 * v)))
  468. break;
  469. case AV_MIX_COEFF_TYPE_FLT:
  470. CONVERT_MATRIX(flt, v)
  471. break;
  472. default:
  473. av_log(am->avr, AV_LOG_ERROR, "Invalid mix coeff type\n");
  474. return AVERROR(EINVAL);
  475. }
  476. return mix_function_init(am);
  477. }