You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

529 lines
18KB

  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. AudioMix *ff_audio_mix_alloc(AVAudioResampleContext *avr)
  271. {
  272. AudioMix *am;
  273. int ret;
  274. am = av_mallocz(sizeof(*am));
  275. if (!am)
  276. return NULL;
  277. am->avr = avr;
  278. if (avr->internal_sample_fmt != AV_SAMPLE_FMT_S16P &&
  279. avr->internal_sample_fmt != AV_SAMPLE_FMT_FLTP) {
  280. av_log(avr, AV_LOG_ERROR, "Unsupported internal format for "
  281. "mixing: %s\n",
  282. av_get_sample_fmt_name(avr->internal_sample_fmt));
  283. goto error;
  284. }
  285. am->fmt = avr->internal_sample_fmt;
  286. am->coeff_type = avr->mix_coeff_type;
  287. am->in_layout = avr->in_channel_layout;
  288. am->out_layout = avr->out_channel_layout;
  289. am->in_channels = avr->in_channels;
  290. am->out_channels = avr->out_channels;
  291. /* build matrix if the user did not already set one */
  292. if (avr->mix_matrix) {
  293. ret = ff_audio_mix_set_matrix(am, avr->mix_matrix, avr->in_channels);
  294. if (ret < 0)
  295. goto error;
  296. av_freep(&avr->mix_matrix);
  297. } else {
  298. int i, j;
  299. char in_layout_name[128];
  300. char out_layout_name[128];
  301. double *matrix_dbl = av_mallocz(avr->out_channels * avr->in_channels *
  302. sizeof(*matrix_dbl));
  303. if (!matrix_dbl)
  304. goto error;
  305. ret = avresample_build_matrix(avr->in_channel_layout,
  306. avr->out_channel_layout,
  307. avr->center_mix_level,
  308. avr->surround_mix_level,
  309. avr->lfe_mix_level,
  310. avr->normalize_mix_level,
  311. matrix_dbl,
  312. avr->in_channels,
  313. avr->matrix_encoding);
  314. if (ret < 0) {
  315. av_free(matrix_dbl);
  316. goto error;
  317. }
  318. av_get_channel_layout_string(in_layout_name, sizeof(in_layout_name),
  319. avr->in_channels, avr->in_channel_layout);
  320. av_get_channel_layout_string(out_layout_name, sizeof(out_layout_name),
  321. avr->out_channels, avr->out_channel_layout);
  322. av_log(avr, AV_LOG_DEBUG, "audio_mix: %s to %s\n",
  323. in_layout_name, out_layout_name);
  324. for (i = 0; i < avr->out_channels; i++) {
  325. for (j = 0; j < avr->in_channels; j++) {
  326. av_log(avr, AV_LOG_DEBUG, " %0.3f ",
  327. matrix_dbl[i * avr->in_channels + j]);
  328. }
  329. av_log(avr, AV_LOG_DEBUG, "\n");
  330. }
  331. ret = ff_audio_mix_set_matrix(am, matrix_dbl, avr->in_channels);
  332. if (ret < 0) {
  333. av_free(matrix_dbl);
  334. goto error;
  335. }
  336. av_free(matrix_dbl);
  337. }
  338. ret = mix_function_init(am);
  339. if (ret < 0)
  340. goto error;
  341. return am;
  342. error:
  343. av_free(am);
  344. return NULL;
  345. }
  346. void ff_audio_mix_free(AudioMix **am_p)
  347. {
  348. AudioMix *am;
  349. if (!*am_p)
  350. return;
  351. am = *am_p;
  352. if (am->matrix) {
  353. av_free(am->matrix[0]);
  354. am->matrix = NULL;
  355. }
  356. memset(am->matrix_q8, 0, sizeof(am->matrix_q8 ));
  357. memset(am->matrix_q15, 0, sizeof(am->matrix_q15));
  358. memset(am->matrix_flt, 0, sizeof(am->matrix_flt));
  359. av_freep(am_p);
  360. }
  361. int ff_audio_mix(AudioMix *am, AudioData *src)
  362. {
  363. int use_generic = 1;
  364. int len = src->nb_samples;
  365. /* determine whether to use the optimized function based on pointer and
  366. samples alignment in both the input and output */
  367. if (am->has_optimized_func) {
  368. int aligned_len = FFALIGN(len, am->samples_align);
  369. if (!(src->ptr_align % am->ptr_align) &&
  370. src->samples_align >= aligned_len) {
  371. len = aligned_len;
  372. use_generic = 0;
  373. }
  374. }
  375. av_dlog(am->avr, "audio_mix: %d samples - %d to %d channels (%s)\n",
  376. src->nb_samples, am->in_channels, am->out_channels,
  377. use_generic ? am->func_descr_generic : am->func_descr);
  378. if (use_generic)
  379. am->mix_generic(src->data, am->matrix, len, am->out_channels,
  380. am->in_channels);
  381. else
  382. am->mix(src->data, am->matrix, len, am->out_channels, am->in_channels);
  383. ff_audio_data_set_channels(src, am->out_channels);
  384. return 0;
  385. }
  386. int ff_audio_mix_get_matrix(AudioMix *am, double *matrix, int stride)
  387. {
  388. int i, o;
  389. if ( am->in_channels <= 0 || am->in_channels > AVRESAMPLE_MAX_CHANNELS ||
  390. am->out_channels <= 0 || am->out_channels > AVRESAMPLE_MAX_CHANNELS) {
  391. av_log(am, AV_LOG_ERROR, "Invalid channel counts\n");
  392. return AVERROR(EINVAL);
  393. }
  394. #define GET_MATRIX_CONVERT(suffix, scale) \
  395. if (!am->matrix_ ## suffix[0]) { \
  396. av_log(am, AV_LOG_ERROR, "matrix is not set\n"); \
  397. return AVERROR(EINVAL); \
  398. } \
  399. for (o = 0; o < am->out_channels; o++) \
  400. for (i = 0; i < am->in_channels; i++) \
  401. matrix[o * stride + i] = am->matrix_ ## suffix[o][i] * (scale);
  402. switch (am->coeff_type) {
  403. case AV_MIX_COEFF_TYPE_Q8:
  404. GET_MATRIX_CONVERT(q8, 1.0 / 256.0);
  405. break;
  406. case AV_MIX_COEFF_TYPE_Q15:
  407. GET_MATRIX_CONVERT(q15, 1.0 / 32768.0);
  408. break;
  409. case AV_MIX_COEFF_TYPE_FLT:
  410. GET_MATRIX_CONVERT(flt, 1.0);
  411. break;
  412. default:
  413. av_log(am, AV_LOG_ERROR, "Invalid mix coeff type\n");
  414. return AVERROR(EINVAL);
  415. }
  416. return 0;
  417. }
  418. int ff_audio_mix_set_matrix(AudioMix *am, const double *matrix, int stride)
  419. {
  420. int i, o;
  421. if ( am->in_channels <= 0 || am->in_channels > AVRESAMPLE_MAX_CHANNELS ||
  422. am->out_channels <= 0 || am->out_channels > AVRESAMPLE_MAX_CHANNELS) {
  423. av_log(am, AV_LOG_ERROR, "Invalid channel counts\n");
  424. return AVERROR(EINVAL);
  425. }
  426. if (am->matrix) {
  427. av_free(am->matrix[0]);
  428. am->matrix = NULL;
  429. }
  430. #define CONVERT_MATRIX(type, expr) \
  431. am->matrix_## type[0] = av_mallocz(am->out_channels * am->in_channels * \
  432. sizeof(*am->matrix_## type[0])); \
  433. if (!am->matrix_## type[0]) \
  434. return AVERROR(ENOMEM); \
  435. for (o = 0; o < am->out_channels; o++) { \
  436. if (o > 0) \
  437. am->matrix_## type[o] = am->matrix_## type[o - 1] + \
  438. am->in_channels; \
  439. for (i = 0; i < am->in_channels; i++) { \
  440. double v = matrix[o * stride + i]; \
  441. am->matrix_## type[o][i] = expr; \
  442. } \
  443. } \
  444. am->matrix = (void **)am->matrix_## type;
  445. switch (am->coeff_type) {
  446. case AV_MIX_COEFF_TYPE_Q8:
  447. CONVERT_MATRIX(q8, av_clip_int16(lrint(256.0 * v)))
  448. break;
  449. case AV_MIX_COEFF_TYPE_Q15:
  450. CONVERT_MATRIX(q15, av_clipl_int32(llrint(32768.0 * v)))
  451. break;
  452. case AV_MIX_COEFF_TYPE_FLT:
  453. CONVERT_MATRIX(flt, v)
  454. break;
  455. default:
  456. av_log(am, AV_LOG_ERROR, "Invalid mix coeff type\n");
  457. return AVERROR(EINVAL);
  458. }
  459. /* TODO: detect situations where we can just swap around pointers
  460. instead of doing matrix multiplications with 0.0 and 1.0 */
  461. return 0;
  462. }