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.

706 lines
25KB

  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. 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 * m1;
  186. *dst1++ = v * m0;
  187. v = *src++;
  188. *dst0++ = v * m1;
  189. *dst1++ = v * m0;
  190. v = *src++;
  191. *dst0++ = v * m1;
  192. *dst1++ = v * m0;
  193. v = *src++;
  194. *dst0++ = v * m1;
  195. *dst1++ = v * m0;
  196. len -= 4;
  197. }
  198. while (len > 0) {
  199. v = *src++;
  200. *dst0++ = v * m1;
  201. *dst1++ = v * m0;
  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 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. int i, j;
  330. char in_layout_name[128];
  331. char out_layout_name[128];
  332. double *matrix_dbl = av_mallocz(avr->out_channels * avr->in_channels *
  333. sizeof(*matrix_dbl));
  334. if (!matrix_dbl)
  335. goto error;
  336. ret = avresample_build_matrix(avr->in_channel_layout,
  337. avr->out_channel_layout,
  338. avr->center_mix_level,
  339. avr->surround_mix_level,
  340. avr->lfe_mix_level,
  341. avr->normalize_mix_level,
  342. matrix_dbl,
  343. avr->in_channels,
  344. avr->matrix_encoding);
  345. if (ret < 0) {
  346. av_free(matrix_dbl);
  347. goto error;
  348. }
  349. ret = ff_audio_mix_set_matrix(am, matrix_dbl, avr->in_channels);
  350. if (ret < 0) {
  351. av_log(avr, AV_LOG_ERROR, "error setting mix matrix\n");
  352. av_free(matrix_dbl);
  353. goto error;
  354. }
  355. av_get_channel_layout_string(in_layout_name, sizeof(in_layout_name),
  356. avr->in_channels, avr->in_channel_layout);
  357. av_get_channel_layout_string(out_layout_name, sizeof(out_layout_name),
  358. avr->out_channels, avr->out_channel_layout);
  359. av_log(avr, AV_LOG_DEBUG, "audio_mix: %s to %s\n",
  360. in_layout_name, out_layout_name);
  361. av_log(avr, AV_LOG_DEBUG, "matrix size: %d x %d\n",
  362. am->in_matrix_channels, am->out_matrix_channels);
  363. for (i = 0; i < avr->out_channels; i++) {
  364. for (j = 0; j < avr->in_channels; j++) {
  365. if (am->output_zero[i])
  366. av_log(avr, AV_LOG_DEBUG, " (ZERO)");
  367. else if (am->input_skip[j] || am->output_skip[i])
  368. av_log(avr, AV_LOG_DEBUG, " (SKIP)");
  369. else
  370. av_log(avr, AV_LOG_DEBUG, " %0.3f ",
  371. matrix_dbl[i * avr->in_channels + j]);
  372. }
  373. av_log(avr, AV_LOG_DEBUG, "\n");
  374. }
  375. av_free(matrix_dbl);
  376. }
  377. return am;
  378. error:
  379. av_free(am);
  380. return NULL;
  381. }
  382. void ff_audio_mix_free(AudioMix **am_p)
  383. {
  384. AudioMix *am;
  385. if (!*am_p)
  386. return;
  387. am = *am_p;
  388. if (am->matrix) {
  389. av_free(am->matrix[0]);
  390. am->matrix = NULL;
  391. }
  392. memset(am->matrix_q8, 0, sizeof(am->matrix_q8 ));
  393. memset(am->matrix_q15, 0, sizeof(am->matrix_q15));
  394. memset(am->matrix_flt, 0, sizeof(am->matrix_flt));
  395. av_freep(am_p);
  396. }
  397. int ff_audio_mix(AudioMix *am, AudioData *src)
  398. {
  399. int use_generic = 1;
  400. int len = src->nb_samples;
  401. int i, j;
  402. /* determine whether to use the optimized function based on pointer and
  403. samples alignment in both the input and output */
  404. if (am->has_optimized_func) {
  405. int aligned_len = FFALIGN(len, am->samples_align);
  406. if (!(src->ptr_align % am->ptr_align) &&
  407. src->samples_align >= aligned_len) {
  408. len = aligned_len;
  409. use_generic = 0;
  410. }
  411. }
  412. av_dlog(am->avr, "audio_mix: %d samples - %d to %d channels (%s)\n",
  413. src->nb_samples, am->in_channels, am->out_channels,
  414. use_generic ? am->func_descr_generic : am->func_descr);
  415. if (am->in_matrix_channels && am->out_matrix_channels) {
  416. uint8_t **data;
  417. uint8_t *data0[AVRESAMPLE_MAX_CHANNELS];
  418. if (am->out_matrix_channels < am->out_channels ||
  419. am->in_matrix_channels < am->in_channels) {
  420. for (i = 0, j = 0; i < FFMAX(am->in_channels, am->out_channels); i++) {
  421. if (am->input_skip[i] || am->output_skip[i] || am->output_zero[i])
  422. continue;
  423. data0[j++] = src->data[i];
  424. }
  425. data = data0;
  426. } else {
  427. data = src->data;
  428. }
  429. if (use_generic)
  430. am->mix_generic(data, am->matrix, len, am->out_matrix_channels,
  431. am->in_matrix_channels);
  432. else
  433. am->mix(data, am->matrix, len, am->out_matrix_channels,
  434. am->in_matrix_channels);
  435. }
  436. if (am->out_matrix_channels < am->out_channels) {
  437. for (i = 0; i < am->out_channels; i++)
  438. if (am->output_zero[i])
  439. av_samples_set_silence(&src->data[i], 0, len, 1, am->fmt);
  440. }
  441. ff_audio_data_set_channels(src, am->out_channels);
  442. return 0;
  443. }
  444. int ff_audio_mix_get_matrix(AudioMix *am, double *matrix, int stride)
  445. {
  446. int i, o, i0, o0;
  447. if ( am->in_channels <= 0 || am->in_channels > AVRESAMPLE_MAX_CHANNELS ||
  448. am->out_channels <= 0 || am->out_channels > AVRESAMPLE_MAX_CHANNELS) {
  449. av_log(am->avr, AV_LOG_ERROR, "Invalid channel counts\n");
  450. return AVERROR(EINVAL);
  451. }
  452. #define GET_MATRIX_CONVERT(suffix, scale) \
  453. if (!am->matrix_ ## suffix[0]) { \
  454. av_log(am->avr, AV_LOG_ERROR, "matrix is not set\n"); \
  455. return AVERROR(EINVAL); \
  456. } \
  457. for (o = 0, o0 = 0; o < am->out_channels; o++) { \
  458. for (i = 0, i0 = 0; i < am->in_channels; i++) { \
  459. if (am->input_skip[i] || am->output_zero[o]) \
  460. matrix[o * stride + i] = 0.0; \
  461. else \
  462. matrix[o * stride + i] = am->matrix_ ## suffix[o0][i0] * \
  463. (scale); \
  464. if (!am->input_skip[i]) \
  465. i0++; \
  466. } \
  467. if (!am->output_zero[o]) \
  468. o0++; \
  469. }
  470. switch (am->coeff_type) {
  471. case AV_MIX_COEFF_TYPE_Q8:
  472. GET_MATRIX_CONVERT(q8, 1.0 / 256.0);
  473. break;
  474. case AV_MIX_COEFF_TYPE_Q15:
  475. GET_MATRIX_CONVERT(q15, 1.0 / 32768.0);
  476. break;
  477. case AV_MIX_COEFF_TYPE_FLT:
  478. GET_MATRIX_CONVERT(flt, 1.0);
  479. break;
  480. default:
  481. av_log(am->avr, AV_LOG_ERROR, "Invalid mix coeff type\n");
  482. return AVERROR(EINVAL);
  483. }
  484. return 0;
  485. }
  486. int ff_audio_mix_set_matrix(AudioMix *am, const double *matrix, int stride)
  487. {
  488. int i, o, i0, o0;
  489. if ( am->in_channels <= 0 || am->in_channels > AVRESAMPLE_MAX_CHANNELS ||
  490. am->out_channels <= 0 || am->out_channels > AVRESAMPLE_MAX_CHANNELS) {
  491. av_log(am->avr, AV_LOG_ERROR, "Invalid channel counts\n");
  492. return AVERROR(EINVAL);
  493. }
  494. if (am->matrix) {
  495. av_free(am->matrix[0]);
  496. am->matrix = NULL;
  497. }
  498. memset(am->output_zero, 0, sizeof(am->output_zero));
  499. memset(am->input_skip, 0, sizeof(am->input_skip));
  500. memset(am->output_skip, 0, sizeof(am->output_zero));
  501. am->in_matrix_channels = am->in_channels;
  502. am->out_matrix_channels = am->out_channels;
  503. /* exclude output channels if they can be zeroed instead of mixed */
  504. for (o = 0; o < am->out_channels; o++) {
  505. int zero = 1;
  506. /* check if the output is always silent */
  507. for (i = 0; i < am->in_channels; i++) {
  508. if (matrix[o * stride + i] != 0.0) {
  509. zero = 0;
  510. break;
  511. }
  512. }
  513. /* check if the corresponding input channel makes a contribution to
  514. any output channel */
  515. if (o < am->in_channels) {
  516. for (i = 0; i < am->out_channels; i++) {
  517. if (matrix[i * stride + o] != 0.0) {
  518. zero = 0;
  519. break;
  520. }
  521. }
  522. }
  523. if (zero) {
  524. am->output_zero[o] = 1;
  525. am->out_matrix_channels--;
  526. }
  527. }
  528. if (am->out_matrix_channels == 0) {
  529. am->in_matrix_channels = 0;
  530. return 0;
  531. }
  532. /* skip input channels that contribute fully only to the corresponding
  533. output channel */
  534. for (i = 0; i < FFMIN(am->in_channels, am->out_channels); i++) {
  535. int skip = 1;
  536. for (o = 0; o < am->out_channels; o++) {
  537. if ((o != i && matrix[o * stride + i] != 0.0) ||
  538. (o == i && matrix[o * stride + i] != 1.0)) {
  539. skip = 0;
  540. break;
  541. }
  542. }
  543. if (skip) {
  544. am->input_skip[i] = 1;
  545. am->in_matrix_channels--;
  546. }
  547. }
  548. /* skip input channels that do not contribute to any output channel */
  549. for (; i < am->in_channels; i++) {
  550. int contrib = 0;
  551. for (o = 0; o < am->out_channels; o++) {
  552. if (matrix[o * stride + i] != 0.0) {
  553. contrib = 1;
  554. break;
  555. }
  556. }
  557. if (!contrib) {
  558. am->input_skip[i] = 1;
  559. am->in_matrix_channels--;
  560. }
  561. }
  562. if (am->in_matrix_channels == 0) {
  563. am->out_matrix_channels = 0;
  564. return 0;
  565. }
  566. /* skip output channels that only get full contribution from the
  567. corresponding input channel */
  568. for (o = 0; o < FFMIN(am->in_channels, am->out_channels); o++) {
  569. int skip = 1;
  570. for (i = 0; i < am->in_channels; i++) {
  571. if ((o != i && matrix[o * stride + i] != 0.0) ||
  572. (o == i && matrix[o * stride + i] != 1.0)) {
  573. skip = 0;
  574. break;
  575. }
  576. }
  577. if (skip) {
  578. am->output_skip[o] = 1;
  579. am->out_matrix_channels--;
  580. }
  581. }
  582. if (am->out_matrix_channels == 0) {
  583. am->in_matrix_channels = 0;
  584. return 0;
  585. }
  586. #define CONVERT_MATRIX(type, expr) \
  587. am->matrix_## type[0] = av_mallocz(am->out_matrix_channels * \
  588. am->in_matrix_channels * \
  589. sizeof(*am->matrix_## type[0])); \
  590. if (!am->matrix_## type[0]) \
  591. return AVERROR(ENOMEM); \
  592. for (o = 0, o0 = 0; o < am->out_channels; o++) { \
  593. if (am->output_zero[o] || am->output_skip[o]) \
  594. continue; \
  595. if (o0 > 0) \
  596. am->matrix_## type[o0] = am->matrix_## type[o0 - 1] + \
  597. am->in_matrix_channels; \
  598. for (i = 0, i0 = 0; i < am->in_channels; i++) { \
  599. double v; \
  600. if (am->input_skip[i]) \
  601. continue; \
  602. v = matrix[o * stride + i]; \
  603. am->matrix_## type[o0][i0] = expr; \
  604. i0++; \
  605. } \
  606. o0++; \
  607. } \
  608. am->matrix = (void **)am->matrix_## type;
  609. switch (am->coeff_type) {
  610. case AV_MIX_COEFF_TYPE_Q8:
  611. CONVERT_MATRIX(q8, av_clip_int16(lrint(256.0 * v)))
  612. break;
  613. case AV_MIX_COEFF_TYPE_Q15:
  614. CONVERT_MATRIX(q15, av_clipl_int32(llrint(32768.0 * v)))
  615. break;
  616. case AV_MIX_COEFF_TYPE_FLT:
  617. CONVERT_MATRIX(flt, v)
  618. break;
  619. default:
  620. av_log(am->avr, AV_LOG_ERROR, "Invalid mix coeff type\n");
  621. return AVERROR(EINVAL);
  622. }
  623. return mix_function_init(am);
  624. }