audio_mix.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. /*
  2. * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 * const 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. int in_matrix_channels;
  45. int out_matrix_channels;
  46. int output_zero[AVRESAMPLE_MAX_CHANNELS];
  47. int input_skip[AVRESAMPLE_MAX_CHANNELS];
  48. int output_skip[AVRESAMPLE_MAX_CHANNELS];
  49. int16_t *matrix_q8[AVRESAMPLE_MAX_CHANNELS];
  50. int32_t *matrix_q15[AVRESAMPLE_MAX_CHANNELS];
  51. float *matrix_flt[AVRESAMPLE_MAX_CHANNELS];
  52. void **matrix;
  53. };
  54. void ff_audio_mix_set_func(AudioMix *am, enum AVSampleFormat fmt,
  55. enum AVMixCoeffType coeff_type, int in_channels,
  56. int out_channels, int ptr_align, int samples_align,
  57. const char *descr, void *mix_func)
  58. {
  59. if (fmt == am->fmt && coeff_type == am->coeff_type &&
  60. ( in_channels == am->in_matrix_channels || in_channels == 0) &&
  61. (out_channels == am->out_matrix_channels || out_channels == 0)) {
  62. char chan_str[16];
  63. am->mix = mix_func;
  64. am->func_descr = descr;
  65. am->ptr_align = ptr_align;
  66. am->samples_align = samples_align;
  67. if (ptr_align == 1 && samples_align == 1) {
  68. am->mix_generic = mix_func;
  69. am->func_descr_generic = descr;
  70. } else {
  71. am->has_optimized_func = 1;
  72. }
  73. if (in_channels) {
  74. if (out_channels)
  75. snprintf(chan_str, sizeof(chan_str), "[%d to %d] ",
  76. in_channels, out_channels);
  77. else
  78. snprintf(chan_str, sizeof(chan_str), "[%d to any] ",
  79. in_channels);
  80. } else if (out_channels) {
  81. snprintf(chan_str, sizeof(chan_str), "[any to %d] ",
  82. out_channels);
  83. } else {
  84. snprintf(chan_str, sizeof(chan_str), "[any to any] ");
  85. }
  86. av_log(am->avr, AV_LOG_DEBUG, "audio_mix: found function: [fmt=%s] "
  87. "[c=%s] %s(%s)\n", av_get_sample_fmt_name(fmt),
  88. coeff_type_names[coeff_type], chan_str, descr);
  89. }
  90. }
  91. #define MIX_FUNC_NAME(fmt, cfmt) mix_any_ ## fmt ##_## cfmt ##_c
  92. #define MIX_FUNC_GENERIC(fmt, cfmt, stype, ctype, sumtype, expr) \
  93. static void MIX_FUNC_NAME(fmt, cfmt)(stype **samples, ctype **matrix, \
  94. int len, int out_ch, int in_ch) \
  95. { \
  96. int i, in, out; \
  97. stype temp[AVRESAMPLE_MAX_CHANNELS]; \
  98. for (i = 0; i < len; i++) { \
  99. for (out = 0; out < out_ch; out++) { \
  100. sumtype sum = 0; \
  101. for (in = 0; in < in_ch; in++) \
  102. sum += samples[in][i] * matrix[out][in]; \
  103. temp[out] = expr; \
  104. } \
  105. for (out = 0; out < out_ch; out++) \
  106. samples[out][i] = temp[out]; \
  107. } \
  108. }
  109. MIX_FUNC_GENERIC(FLTP, FLT, float, float, float, sum)
  110. MIX_FUNC_GENERIC(S16P, FLT, int16_t, float, float, av_clip_int16(lrintf(sum)))
  111. MIX_FUNC_GENERIC(S16P, Q15, int16_t, int32_t, int64_t, av_clip_int16(sum >> 15))
  112. MIX_FUNC_GENERIC(S16P, Q8, int16_t, int16_t, int32_t, av_clip_int16(sum >> 8))
  113. /* TODO: templatize the channel-specific C functions */
  114. static void mix_2_to_1_fltp_flt_c(float **samples, float **matrix, int len,
  115. int out_ch, int in_ch)
  116. {
  117. float *src0 = samples[0];
  118. float *src1 = samples[1];
  119. float *dst = src0;
  120. float m0 = matrix[0][0];
  121. float m1 = matrix[0][1];
  122. while (len > 4) {
  123. *dst++ = *src0++ * m0 + *src1++ * m1;
  124. *dst++ = *src0++ * m0 + *src1++ * m1;
  125. *dst++ = *src0++ * m0 + *src1++ * m1;
  126. *dst++ = *src0++ * m0 + *src1++ * m1;
  127. len -= 4;
  128. }
  129. while (len > 0) {
  130. *dst++ = *src0++ * m0 + *src1++ * m1;
  131. len--;
  132. }
  133. }
  134. static void mix_2_to_1_s16p_flt_c(int16_t **samples, float **matrix, int len,
  135. int out_ch, int in_ch)
  136. {
  137. int16_t *src0 = samples[0];
  138. int16_t *src1 = samples[1];
  139. int16_t *dst = src0;
  140. float m0 = matrix[0][0];
  141. float m1 = matrix[0][1];
  142. while (len > 4) {
  143. *dst++ = av_clip_int16(lrintf(*src0++ * m0 + *src1++ * m1));
  144. *dst++ = av_clip_int16(lrintf(*src0++ * m0 + *src1++ * m1));
  145. *dst++ = av_clip_int16(lrintf(*src0++ * m0 + *src1++ * m1));
  146. *dst++ = av_clip_int16(lrintf(*src0++ * m0 + *src1++ * m1));
  147. len -= 4;
  148. }
  149. while (len > 0) {
  150. *dst++ = av_clip_int16(lrintf(*src0++ * m0 + *src1++ * m1));
  151. len--;
  152. }
  153. }
  154. static void mix_2_to_1_s16p_q8_c(int16_t **samples, int16_t **matrix, int len,
  155. int out_ch, int in_ch)
  156. {
  157. int16_t *src0 = samples[0];
  158. int16_t *src1 = samples[1];
  159. int16_t *dst = src0;
  160. int16_t m0 = matrix[0][0];
  161. int16_t m1 = matrix[0][1];
  162. while (len > 4) {
  163. *dst++ = (*src0++ * m0 + *src1++ * m1) >> 8;
  164. *dst++ = (*src0++ * m0 + *src1++ * m1) >> 8;
  165. *dst++ = (*src0++ * m0 + *src1++ * m1) >> 8;
  166. *dst++ = (*src0++ * m0 + *src1++ * m1) >> 8;
  167. len -= 4;
  168. }
  169. while (len > 0) {
  170. *dst++ = (*src0++ * m0 + *src1++ * m1) >> 8;
  171. len--;
  172. }
  173. }
  174. static void mix_1_to_2_fltp_flt_c(float **samples, float **matrix, int len,
  175. int out_ch, int in_ch)
  176. {
  177. float v;
  178. float *dst0 = samples[0];
  179. float *dst1 = samples[1];
  180. float *src = dst0;
  181. float m0 = matrix[0][0];
  182. float m1 = matrix[1][0];
  183. while (len > 4) {
  184. v = *src++;
  185. *dst0++ = v * m0;
  186. *dst1++ = v * m1;
  187. v = *src++;
  188. *dst0++ = v * m0;
  189. *dst1++ = v * m1;
  190. v = *src++;
  191. *dst0++ = v * m0;
  192. *dst1++ = v * m1;
  193. v = *src++;
  194. *dst0++ = v * m0;
  195. *dst1++ = v * m1;
  196. len -= 4;
  197. }
  198. while (len > 0) {
  199. v = *src++;
  200. *dst0++ = v * m0;
  201. *dst1++ = v * m1;
  202. len--;
  203. }
  204. }
  205. static void mix_6_to_2_fltp_flt_c(float **samples, float **matrix, int len,
  206. int out_ch, int in_ch)
  207. {
  208. float v0, v1;
  209. float *src0 = samples[0];
  210. float *src1 = samples[1];
  211. float *src2 = samples[2];
  212. float *src3 = samples[3];
  213. float *src4 = samples[4];
  214. float *src5 = samples[5];
  215. float *dst0 = src0;
  216. float *dst1 = src1;
  217. float *m0 = matrix[0];
  218. float *m1 = matrix[1];
  219. while (len > 0) {
  220. v0 = *src0++;
  221. v1 = *src1++;
  222. *dst0++ = v0 * m0[0] +
  223. v1 * m0[1] +
  224. *src2 * m0[2] +
  225. *src3 * m0[3] +
  226. *src4 * m0[4] +
  227. *src5 * m0[5];
  228. *dst1++ = v0 * m1[0] +
  229. v1 * m1[1] +
  230. *src2++ * m1[2] +
  231. *src3++ * m1[3] +
  232. *src4++ * m1[4] +
  233. *src5++ * m1[5];
  234. len--;
  235. }
  236. }
  237. static void mix_2_to_6_fltp_flt_c(float **samples, float **matrix, int len,
  238. int out_ch, int in_ch)
  239. {
  240. float v0, v1;
  241. float *dst0 = samples[0];
  242. float *dst1 = samples[1];
  243. float *dst2 = samples[2];
  244. float *dst3 = samples[3];
  245. float *dst4 = samples[4];
  246. float *dst5 = samples[5];
  247. float *src0 = dst0;
  248. float *src1 = dst1;
  249. while (len > 0) {
  250. v0 = *src0++;
  251. v1 = *src1++;
  252. *dst0++ = v0 * matrix[0][0] + v1 * matrix[0][1];
  253. *dst1++ = v0 * matrix[1][0] + v1 * matrix[1][1];
  254. *dst2++ = v0 * matrix[2][0] + v1 * matrix[2][1];
  255. *dst3++ = v0 * matrix[3][0] + v1 * matrix[3][1];
  256. *dst4++ = v0 * matrix[4][0] + v1 * matrix[4][1];
  257. *dst5++ = v0 * matrix[5][0] + v1 * matrix[5][1];
  258. len--;
  259. }
  260. }
  261. static av_cold int mix_function_init(AudioMix *am)
  262. {
  263. am->func_descr = am->func_descr_generic = "n/a";
  264. am->mix = am->mix_generic = NULL;
  265. /* no need to set a mix function when we're skipping mixing */
  266. if (!am->in_matrix_channels || !am->out_matrix_channels)
  267. return 0;
  268. /* any-to-any C versions */
  269. ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
  270. 0, 0, 1, 1, "C", MIX_FUNC_NAME(FLTP, FLT));
  271. ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_FLT,
  272. 0, 0, 1, 1, "C", MIX_FUNC_NAME(S16P, FLT));
  273. ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_Q15,
  274. 0, 0, 1, 1, "C", MIX_FUNC_NAME(S16P, Q15));
  275. ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_Q8,
  276. 0, 0, 1, 1, "C", MIX_FUNC_NAME(S16P, Q8));
  277. /* channel-specific C versions */
  278. ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
  279. 2, 1, 1, 1, "C", mix_2_to_1_fltp_flt_c);
  280. ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_FLT,
  281. 2, 1, 1, 1, "C", mix_2_to_1_s16p_flt_c);
  282. ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_Q8,
  283. 2, 1, 1, 1, "C", mix_2_to_1_s16p_q8_c);
  284. ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
  285. 1, 2, 1, 1, "C", mix_1_to_2_fltp_flt_c);
  286. ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
  287. 6, 2, 1, 1, "C", mix_6_to_2_fltp_flt_c);
  288. ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
  289. 2, 6, 1, 1, "C", mix_2_to_6_fltp_flt_c);
  290. if (ARCH_X86)
  291. ff_audio_mix_init_x86(am);
  292. if (!am->mix) {
  293. av_log(am->avr, AV_LOG_ERROR, "audio_mix: NO FUNCTION FOUND: [fmt=%s] "
  294. "[c=%s] [%d to %d]\n", av_get_sample_fmt_name(am->fmt),
  295. coeff_type_names[am->coeff_type], am->in_channels,
  296. am->out_channels);
  297. return AVERROR_PATCHWELCOME;
  298. }
  299. return 0;
  300. }
  301. AudioMix *ff_audio_mix_alloc(AVAudioResampleContext *avr)
  302. {
  303. AudioMix *am;
  304. int ret;
  305. am = av_mallocz(sizeof(*am));
  306. if (!am)
  307. return NULL;
  308. am->avr = avr;
  309. if (avr->internal_sample_fmt != AV_SAMPLE_FMT_S16P &&
  310. avr->internal_sample_fmt != AV_SAMPLE_FMT_FLTP) {
  311. av_log(avr, AV_LOG_ERROR, "Unsupported internal format for "
  312. "mixing: %s\n",
  313. av_get_sample_fmt_name(avr->internal_sample_fmt));
  314. goto error;
  315. }
  316. am->fmt = avr->internal_sample_fmt;
  317. am->coeff_type = avr->mix_coeff_type;
  318. am->in_layout = avr->in_channel_layout;
  319. am->out_layout = avr->out_channel_layout;
  320. am->in_channels = avr->in_channels;
  321. am->out_channels = avr->out_channels;
  322. /* build matrix if the user did not already set one */
  323. if (avr->mix_matrix) {
  324. ret = ff_audio_mix_set_matrix(am, avr->mix_matrix, avr->in_channels);
  325. if (ret < 0)
  326. goto error;
  327. av_freep(&avr->mix_matrix);
  328. } else {
  329. double *matrix_dbl = av_mallocz(avr->out_channels * avr->in_channels *
  330. sizeof(*matrix_dbl));
  331. if (!matrix_dbl)
  332. goto error;
  333. ret = avresample_build_matrix(avr->in_channel_layout,
  334. avr->out_channel_layout,
  335. avr->center_mix_level,
  336. avr->surround_mix_level,
  337. avr->lfe_mix_level,
  338. avr->normalize_mix_level,
  339. matrix_dbl,
  340. avr->in_channels,
  341. avr->matrix_encoding);
  342. if (ret < 0) {
  343. av_free(matrix_dbl);
  344. goto error;
  345. }
  346. ret = ff_audio_mix_set_matrix(am, matrix_dbl, avr->in_channels);
  347. if (ret < 0) {
  348. av_log(avr, AV_LOG_ERROR, "error setting mix matrix\n");
  349. av_free(matrix_dbl);
  350. goto error;
  351. }
  352. av_free(matrix_dbl);
  353. }
  354. return am;
  355. error:
  356. av_free(am);
  357. return NULL;
  358. }
  359. void ff_audio_mix_free(AudioMix **am_p)
  360. {
  361. AudioMix *am;
  362. if (!*am_p)
  363. return;
  364. am = *am_p;
  365. if (am->matrix) {
  366. av_free(am->matrix[0]);
  367. am->matrix = NULL;
  368. }
  369. memset(am->matrix_q8, 0, sizeof(am->matrix_q8 ));
  370. memset(am->matrix_q15, 0, sizeof(am->matrix_q15));
  371. memset(am->matrix_flt, 0, sizeof(am->matrix_flt));
  372. av_freep(am_p);
  373. }
  374. int ff_audio_mix(AudioMix *am, AudioData *src)
  375. {
  376. int use_generic = 1;
  377. int len = src->nb_samples;
  378. int i, j;
  379. /* determine whether to use the optimized function based on pointer and
  380. samples alignment in both the input and output */
  381. if (am->has_optimized_func) {
  382. int aligned_len = FFALIGN(len, am->samples_align);
  383. if (!(src->ptr_align % am->ptr_align) &&
  384. src->samples_align >= aligned_len) {
  385. len = aligned_len;
  386. use_generic = 0;
  387. }
  388. }
  389. av_log(am->avr, AV_LOG_TRACE, "audio_mix: %d samples - %d to %d channels (%s)\n",
  390. src->nb_samples, am->in_channels, am->out_channels,
  391. use_generic ? am->func_descr_generic : am->func_descr);
  392. if (am->in_matrix_channels && am->out_matrix_channels) {
  393. uint8_t **data;
  394. uint8_t *data0[AVRESAMPLE_MAX_CHANNELS] = { NULL };
  395. if (am->out_matrix_channels < am->out_channels ||
  396. am->in_matrix_channels < am->in_channels) {
  397. for (i = 0, j = 0; i < FFMAX(am->in_channels, am->out_channels); i++) {
  398. if (am->input_skip[i] || am->output_skip[i] || am->output_zero[i])
  399. continue;
  400. data0[j++] = src->data[i];
  401. }
  402. data = data0;
  403. } else {
  404. data = src->data;
  405. }
  406. if (use_generic)
  407. am->mix_generic(data, am->matrix, len, am->out_matrix_channels,
  408. am->in_matrix_channels);
  409. else
  410. am->mix(data, am->matrix, len, am->out_matrix_channels,
  411. am->in_matrix_channels);
  412. }
  413. if (am->out_matrix_channels < am->out_channels) {
  414. for (i = 0; i < am->out_channels; i++)
  415. if (am->output_zero[i])
  416. av_samples_set_silence(&src->data[i], 0, len, 1, am->fmt);
  417. }
  418. ff_audio_data_set_channels(src, am->out_channels);
  419. return 0;
  420. }
  421. int ff_audio_mix_get_matrix(AudioMix *am, double *matrix, int stride)
  422. {
  423. int i, o, i0, o0;
  424. if ( am->in_channels <= 0 || am->in_channels > AVRESAMPLE_MAX_CHANNELS ||
  425. am->out_channels <= 0 || am->out_channels > AVRESAMPLE_MAX_CHANNELS) {
  426. av_log(am->avr, AV_LOG_ERROR, "Invalid channel counts\n");
  427. return AVERROR(EINVAL);
  428. }
  429. #define GET_MATRIX_CONVERT(suffix, scale) \
  430. if (!am->matrix_ ## suffix[0]) { \
  431. av_log(am->avr, AV_LOG_ERROR, "matrix is not set\n"); \
  432. return AVERROR(EINVAL); \
  433. } \
  434. for (o = 0, o0 = 0; o < am->out_channels; o++) { \
  435. for (i = 0, i0 = 0; i < am->in_channels; i++) { \
  436. if (am->input_skip[i] || am->output_zero[o]) \
  437. matrix[o * stride + i] = 0.0; \
  438. else \
  439. matrix[o * stride + i] = am->matrix_ ## suffix[o0][i0] * \
  440. (scale); \
  441. if (!am->input_skip[i]) \
  442. i0++; \
  443. } \
  444. if (!am->output_zero[o]) \
  445. o0++; \
  446. }
  447. switch (am->coeff_type) {
  448. case AV_MIX_COEFF_TYPE_Q8:
  449. GET_MATRIX_CONVERT(q8, 1.0 / 256.0);
  450. break;
  451. case AV_MIX_COEFF_TYPE_Q15:
  452. GET_MATRIX_CONVERT(q15, 1.0 / 32768.0);
  453. break;
  454. case AV_MIX_COEFF_TYPE_FLT:
  455. GET_MATRIX_CONVERT(flt, 1.0);
  456. break;
  457. default:
  458. av_log(am->avr, AV_LOG_ERROR, "Invalid mix coeff type\n");
  459. return AVERROR(EINVAL);
  460. }
  461. return 0;
  462. }
  463. static void reduce_matrix(AudioMix *am, const double *matrix, int stride)
  464. {
  465. int i, o;
  466. memset(am->output_zero, 0, sizeof(am->output_zero));
  467. memset(am->input_skip, 0, sizeof(am->input_skip));
  468. memset(am->output_skip, 0, sizeof(am->output_skip));
  469. /* exclude output channels if they can be zeroed instead of mixed */
  470. for (o = 0; o < am->out_channels; o++) {
  471. int zero = 1;
  472. /* check if the output is always silent */
  473. for (i = 0; i < am->in_channels; i++) {
  474. if (matrix[o * stride + i] != 0.0) {
  475. zero = 0;
  476. break;
  477. }
  478. }
  479. /* check if the corresponding input channel makes a contribution to
  480. any output channel */
  481. if (o < am->in_channels) {
  482. for (i = 0; i < am->out_channels; i++) {
  483. if (matrix[i * stride + o] != 0.0) {
  484. zero = 0;
  485. break;
  486. }
  487. }
  488. }
  489. if (zero) {
  490. am->output_zero[o] = 1;
  491. am->out_matrix_channels--;
  492. if (o < am->in_channels)
  493. am->in_matrix_channels--;
  494. }
  495. }
  496. if (am->out_matrix_channels == 0 || am->in_matrix_channels == 0) {
  497. am->out_matrix_channels = 0;
  498. am->in_matrix_channels = 0;
  499. return;
  500. }
  501. /* skip input channels that contribute fully only to the corresponding
  502. output channel */
  503. for (i = 0; i < FFMIN(am->in_channels, am->out_channels); i++) {
  504. int skip = 1;
  505. for (o = 0; o < am->out_channels; o++) {
  506. int i0;
  507. if ((o != i && matrix[o * stride + i] != 0.0) ||
  508. (o == i && matrix[o * stride + i] != 1.0)) {
  509. skip = 0;
  510. break;
  511. }
  512. /* if the input contributes fully to the output, also check that no
  513. other inputs contribute to this output */
  514. if (o == i) {
  515. for (i0 = 0; i0 < am->in_channels; i0++) {
  516. if (i0 != i && matrix[o * stride + i0] != 0.0) {
  517. skip = 0;
  518. break;
  519. }
  520. }
  521. }
  522. }
  523. if (skip) {
  524. am->input_skip[i] = 1;
  525. am->in_matrix_channels--;
  526. }
  527. }
  528. /* skip input channels that do not contribute to any output channel */
  529. for (; i < am->in_channels; i++) {
  530. int contrib = 0;
  531. for (o = 0; o < am->out_channels; o++) {
  532. if (matrix[o * stride + i] != 0.0) {
  533. contrib = 1;
  534. break;
  535. }
  536. }
  537. if (!contrib) {
  538. am->input_skip[i] = 1;
  539. am->in_matrix_channels--;
  540. }
  541. }
  542. if (am->in_matrix_channels == 0) {
  543. am->out_matrix_channels = 0;
  544. return;
  545. }
  546. /* skip output channels that only get full contribution from the
  547. corresponding input channel */
  548. for (o = 0; o < FFMIN(am->in_channels, am->out_channels); o++) {
  549. int skip = 1;
  550. int o0;
  551. for (i = 0; i < am->in_channels; i++) {
  552. if ((o != i && matrix[o * stride + i] != 0.0) ||
  553. (o == i && matrix[o * stride + i] != 1.0)) {
  554. skip = 0;
  555. break;
  556. }
  557. }
  558. /* check if the corresponding input channel makes a contribution to
  559. any other output channel */
  560. i = o;
  561. for (o0 = 0; o0 < am->out_channels; o0++) {
  562. if (o0 != i && matrix[o0 * stride + i] != 0.0) {
  563. skip = 0;
  564. break;
  565. }
  566. }
  567. if (skip) {
  568. am->output_skip[o] = 1;
  569. am->out_matrix_channels--;
  570. }
  571. }
  572. if (am->out_matrix_channels == 0) {
  573. am->in_matrix_channels = 0;
  574. return;
  575. }
  576. }
  577. int ff_audio_mix_set_matrix(AudioMix *am, const double *matrix, int stride)
  578. {
  579. int i, o, i0, o0, ret;
  580. char in_layout_name[128];
  581. char out_layout_name[128];
  582. if ( am->in_channels <= 0 || am->in_channels > AVRESAMPLE_MAX_CHANNELS ||
  583. am->out_channels <= 0 || am->out_channels > AVRESAMPLE_MAX_CHANNELS) {
  584. av_log(am->avr, AV_LOG_ERROR, "Invalid channel counts\n");
  585. return AVERROR(EINVAL);
  586. }
  587. if (am->matrix) {
  588. av_free(am->matrix[0]);
  589. am->matrix = NULL;
  590. }
  591. am->in_matrix_channels = am->in_channels;
  592. am->out_matrix_channels = am->out_channels;
  593. reduce_matrix(am, matrix, stride);
  594. #define CONVERT_MATRIX(type, expr) \
  595. am->matrix_## type[0] = av_mallocz(am->out_matrix_channels * \
  596. am->in_matrix_channels * \
  597. sizeof(*am->matrix_## type[0])); \
  598. if (!am->matrix_## type[0]) \
  599. return AVERROR(ENOMEM); \
  600. for (o = 0, o0 = 0; o < am->out_channels; o++) { \
  601. if (am->output_zero[o] || am->output_skip[o]) \
  602. continue; \
  603. if (o0 > 0) \
  604. am->matrix_## type[o0] = am->matrix_## type[o0 - 1] + \
  605. am->in_matrix_channels; \
  606. for (i = 0, i0 = 0; i < am->in_channels; i++) { \
  607. double v; \
  608. if (am->input_skip[i] || am->output_zero[i]) \
  609. continue; \
  610. v = matrix[o * stride + i]; \
  611. am->matrix_## type[o0][i0] = expr; \
  612. i0++; \
  613. } \
  614. o0++; \
  615. } \
  616. am->matrix = (void **)am->matrix_## type;
  617. if (am->in_matrix_channels && am->out_matrix_channels) {
  618. switch (am->coeff_type) {
  619. case AV_MIX_COEFF_TYPE_Q8:
  620. CONVERT_MATRIX(q8, av_clip_int16(lrint(256.0 * v)))
  621. break;
  622. case AV_MIX_COEFF_TYPE_Q15:
  623. CONVERT_MATRIX(q15, av_clipl_int32(llrint(32768.0 * v)))
  624. break;
  625. case AV_MIX_COEFF_TYPE_FLT:
  626. CONVERT_MATRIX(flt, v)
  627. break;
  628. default:
  629. av_log(am->avr, AV_LOG_ERROR, "Invalid mix coeff type\n");
  630. return AVERROR(EINVAL);
  631. }
  632. }
  633. ret = mix_function_init(am);
  634. if (ret < 0)
  635. return ret;
  636. av_get_channel_layout_string(in_layout_name, sizeof(in_layout_name),
  637. am->in_channels, am->in_layout);
  638. av_get_channel_layout_string(out_layout_name, sizeof(out_layout_name),
  639. am->out_channels, am->out_layout);
  640. av_log(am->avr, AV_LOG_DEBUG, "audio_mix: %s to %s\n",
  641. in_layout_name, out_layout_name);
  642. av_log(am->avr, AV_LOG_DEBUG, "matrix size: %d x %d\n",
  643. am->in_matrix_channels, am->out_matrix_channels);
  644. for (o = 0; o < am->out_channels; o++) {
  645. for (i = 0; i < am->in_channels; i++) {
  646. if (am->output_zero[o])
  647. av_log(am->avr, AV_LOG_DEBUG, " (ZERO)");
  648. else if (am->input_skip[i] || am->output_zero[i] || am->output_skip[o])
  649. av_log(am->avr, AV_LOG_DEBUG, " (SKIP)");
  650. else
  651. av_log(am->avr, AV_LOG_DEBUG, " %0.3f ",
  652. matrix[o * am->in_channels + i]);
  653. }
  654. av_log(am->avr, AV_LOG_DEBUG, "\n");
  655. }
  656. return 0;
  657. }