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.

407 lines
14KB

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