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.

585 lines
22KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * @file
  20. * Crossover filter
  21. *
  22. * Split an audio stream into several bands.
  23. */
  24. #include "libavutil/attributes.h"
  25. #include "libavutil/avstring.h"
  26. #include "libavutil/channel_layout.h"
  27. #include "libavutil/eval.h"
  28. #include "libavutil/float_dsp.h"
  29. #include "libavutil/internal.h"
  30. #include "libavutil/opt.h"
  31. #include "audio.h"
  32. #include "avfilter.h"
  33. #include "formats.h"
  34. #include "internal.h"
  35. #define MAX_SPLITS 16
  36. #define MAX_BANDS MAX_SPLITS + 1
  37. #define B0 0
  38. #define B1 1
  39. #define B2 2
  40. #define A1 3
  41. #define A2 4
  42. typedef struct BiquadCoeffs {
  43. double cd[5];
  44. float cf[5];
  45. } BiquadCoeffs;
  46. typedef struct AudioCrossoverContext {
  47. const AVClass *class;
  48. char *splits_str;
  49. char *gains_str;
  50. int order_opt;
  51. float level_in;
  52. int order;
  53. int filter_count;
  54. int first_order;
  55. int ap_filter_count;
  56. int nb_splits;
  57. float splits[MAX_SPLITS];
  58. float gains[MAX_BANDS];
  59. BiquadCoeffs lp[MAX_BANDS][20];
  60. BiquadCoeffs hp[MAX_BANDS][20];
  61. BiquadCoeffs ap[MAX_BANDS][20];
  62. AVFrame *xover;
  63. AVFrame *input_frame;
  64. AVFrame *frames[MAX_BANDS];
  65. int (*filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
  66. AVFloatDSPContext *fdsp;
  67. } AudioCrossoverContext;
  68. #define OFFSET(x) offsetof(AudioCrossoverContext, x)
  69. #define AF AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  70. static const AVOption acrossover_options[] = {
  71. { "split", "set split frequencies", OFFSET(splits_str), AV_OPT_TYPE_STRING, {.str="500"}, 0, 0, AF },
  72. { "order", "set filter order", OFFSET(order_opt), AV_OPT_TYPE_INT, {.i64=1}, 0, 9, AF, "m" },
  73. { "2nd", "2nd order (12 dB/8ve)", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "m" },
  74. { "4th", "4th order (24 dB/8ve)", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "m" },
  75. { "6th", "6th order (36 dB/8ve)", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "m" },
  76. { "8th", "8th order (48 dB/8ve)", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF, "m" },
  77. { "10th", "10th order (60 dB/8ve)",0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, AF, "m" },
  78. { "12th", "12th order (72 dB/8ve)",0, AV_OPT_TYPE_CONST, {.i64=5}, 0, 0, AF, "m" },
  79. { "14th", "14th order (84 dB/8ve)",0, AV_OPT_TYPE_CONST, {.i64=6}, 0, 0, AF, "m" },
  80. { "16th", "16th order (96 dB/8ve)",0, AV_OPT_TYPE_CONST, {.i64=7}, 0, 0, AF, "m" },
  81. { "18th", "18th order (108 dB/8ve)",0, AV_OPT_TYPE_CONST, {.i64=8}, 0, 0, AF, "m" },
  82. { "20th", "20th order (120 dB/8ve)",0, AV_OPT_TYPE_CONST, {.i64=9}, 0, 0, AF, "m" },
  83. { "level", "set input gain", OFFSET(level_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 1, AF },
  84. { "gain", "set output bands gain", OFFSET(gains_str), AV_OPT_TYPE_STRING, {.str="1.f"}, 0, 0, AF },
  85. { NULL }
  86. };
  87. AVFILTER_DEFINE_CLASS(acrossover);
  88. static int parse_gains(AVFilterContext *ctx)
  89. {
  90. AudioCrossoverContext *s = ctx->priv;
  91. char *p, *arg, *saveptr = NULL;
  92. int i, ret = 0;
  93. saveptr = NULL;
  94. p = s->gains_str;
  95. for (i = 0; i < MAX_BANDS; i++) {
  96. float gain;
  97. char c[3] = { 0 };
  98. if (!(arg = av_strtok(p, " |", &saveptr)))
  99. break;
  100. p = NULL;
  101. if (av_sscanf(arg, "%f%2s", &gain, c) < 1) {
  102. av_log(ctx, AV_LOG_ERROR, "Invalid syntax for gain[%d].\n", i);
  103. ret = AVERROR(EINVAL);
  104. break;
  105. }
  106. if (c[0] == 'd' && c[1] == 'B')
  107. s->gains[i] = expf(gain * M_LN10 / 20.f);
  108. else
  109. s->gains[i] = gain;
  110. }
  111. for (; i < MAX_BANDS; i++)
  112. s->gains[i] = 1.f;
  113. return ret;
  114. }
  115. static av_cold int init(AVFilterContext *ctx)
  116. {
  117. AudioCrossoverContext *s = ctx->priv;
  118. char *p, *arg, *saveptr = NULL;
  119. int i, ret = 0;
  120. s->fdsp = avpriv_float_dsp_alloc(0);
  121. if (!s->fdsp)
  122. return AVERROR(ENOMEM);
  123. p = s->splits_str;
  124. for (i = 0; i < MAX_SPLITS; i++) {
  125. float freq;
  126. if (!(arg = av_strtok(p, " |", &saveptr)))
  127. break;
  128. p = NULL;
  129. if (av_sscanf(arg, "%f", &freq) != 1) {
  130. av_log(ctx, AV_LOG_ERROR, "Invalid syntax for frequency[%d].\n", i);
  131. return AVERROR(EINVAL);
  132. }
  133. if (freq <= 0) {
  134. av_log(ctx, AV_LOG_ERROR, "Frequency %f must be positive number.\n", freq);
  135. return AVERROR(EINVAL);
  136. }
  137. if (i > 0 && freq <= s->splits[i-1]) {
  138. av_log(ctx, AV_LOG_ERROR, "Frequency %f must be in increasing order.\n", freq);
  139. return AVERROR(EINVAL);
  140. }
  141. s->splits[i] = freq;
  142. }
  143. s->nb_splits = i;
  144. ret = parse_gains(ctx);
  145. if (ret < 0)
  146. return ret;
  147. for (i = 0; i <= s->nb_splits; i++) {
  148. AVFilterPad pad = { 0 };
  149. char *name;
  150. pad.type = AVMEDIA_TYPE_AUDIO;
  151. name = av_asprintf("out%d", ctx->nb_outputs);
  152. if (!name)
  153. return AVERROR(ENOMEM);
  154. pad.name = name;
  155. if ((ret = ff_insert_outpad(ctx, i, &pad)) < 0) {
  156. av_freep(&pad.name);
  157. return ret;
  158. }
  159. }
  160. return ret;
  161. }
  162. static void set_lp(BiquadCoeffs *b, double fc, double q, double sr)
  163. {
  164. double omega = 2. * M_PI * fc / sr;
  165. double cosine = cos(omega);
  166. double alpha = sin(omega) / (2. * q);
  167. double b0 = (1. - cosine) / 2.;
  168. double b1 = 1. - cosine;
  169. double b2 = (1. - cosine) / 2.;
  170. double a0 = 1. + alpha;
  171. double a1 = -2. * cosine;
  172. double a2 = 1. - alpha;
  173. b->cd[B0] = b0 / a0;
  174. b->cd[B1] = b1 / a0;
  175. b->cd[B2] = b2 / a0;
  176. b->cd[A1] = -a1 / a0;
  177. b->cd[A2] = -a2 / a0;
  178. b->cf[B0] = b->cd[B0];
  179. b->cf[B1] = b->cd[B1];
  180. b->cf[B2] = b->cd[B2];
  181. b->cf[A1] = b->cd[A1];
  182. b->cf[A2] = b->cd[A2];
  183. }
  184. static void set_hp(BiquadCoeffs *b, double fc, double q, double sr)
  185. {
  186. double omega = 2. * M_PI * fc / sr;
  187. double cosine = cos(omega);
  188. double alpha = sin(omega) / (2. * q);
  189. double b0 = (1. + cosine) / 2.;
  190. double b1 = -1. - cosine;
  191. double b2 = (1. + cosine) / 2.;
  192. double a0 = 1. + alpha;
  193. double a1 = -2. * cosine;
  194. double a2 = 1. - alpha;
  195. b->cd[B0] = b0 / a0;
  196. b->cd[B1] = b1 / a0;
  197. b->cd[B2] = b2 / a0;
  198. b->cd[A1] = -a1 / a0;
  199. b->cd[A2] = -a2 / a0;
  200. b->cf[B0] = b->cd[B0];
  201. b->cf[B1] = b->cd[B1];
  202. b->cf[B2] = b->cd[B2];
  203. b->cf[A1] = b->cd[A1];
  204. b->cf[A2] = b->cd[A2];
  205. }
  206. static void set_ap(BiquadCoeffs *b, double fc, double q, double sr)
  207. {
  208. double omega = 2. * M_PI * fc / sr;
  209. double cosine = cos(omega);
  210. double alpha = sin(omega) / (2. * q);
  211. double a0 = 1. + alpha;
  212. double a1 = -2. * cosine;
  213. double a2 = 1. - alpha;
  214. double b0 = a2;
  215. double b1 = a1;
  216. double b2 = a0;
  217. b->cd[B0] = b0 / a0;
  218. b->cd[B1] = b1 / a0;
  219. b->cd[B2] = b2 / a0;
  220. b->cd[A1] = -a1 / a0;
  221. b->cd[A2] = -a2 / a0;
  222. b->cf[B0] = b->cd[B0];
  223. b->cf[B1] = b->cd[B1];
  224. b->cf[B2] = b->cd[B2];
  225. b->cf[A1] = b->cd[A1];
  226. b->cf[A2] = b->cd[A2];
  227. }
  228. static void set_ap1(BiquadCoeffs *b, double fc, double sr)
  229. {
  230. double omega = 2. * M_PI * fc / sr;
  231. b->cd[A1] = exp(-omega);
  232. b->cd[A2] = 0.;
  233. b->cd[B0] = -b->cd[A1];
  234. b->cd[B1] = 1.;
  235. b->cd[B2] = 0.;
  236. b->cf[B0] = b->cd[B0];
  237. b->cf[B1] = b->cd[B1];
  238. b->cf[B2] = b->cd[B2];
  239. b->cf[A1] = b->cd[A1];
  240. b->cf[A2] = b->cd[A2];
  241. }
  242. static void calc_q_factors(int order, double *q)
  243. {
  244. double n = order / 2.;
  245. for (int i = 0; i < n / 2; i++)
  246. q[i] = 1. / (-2. * cos(M_PI * (2. * (i + 1) + n - 1.) / (2. * n)));
  247. }
  248. static int query_formats(AVFilterContext *ctx)
  249. {
  250. AVFilterFormats *formats;
  251. AVFilterChannelLayouts *layouts;
  252. static const enum AVSampleFormat sample_fmts[] = {
  253. AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP,
  254. AV_SAMPLE_FMT_NONE
  255. };
  256. int ret;
  257. layouts = ff_all_channel_counts();
  258. if (!layouts)
  259. return AVERROR(ENOMEM);
  260. ret = ff_set_common_channel_layouts(ctx, layouts);
  261. if (ret < 0)
  262. return ret;
  263. formats = ff_make_format_list(sample_fmts);
  264. if (!formats)
  265. return AVERROR(ENOMEM);
  266. ret = ff_set_common_formats(ctx, formats);
  267. if (ret < 0)
  268. return ret;
  269. formats = ff_all_samplerates();
  270. if (!formats)
  271. return AVERROR(ENOMEM);
  272. return ff_set_common_samplerates(ctx, formats);
  273. }
  274. #define BIQUAD_PROCESS(name, type) \
  275. static void biquad_process_## name(const type *const c, \
  276. type *b, \
  277. type *dst, const type *src, \
  278. int nb_samples) \
  279. { \
  280. const type b0 = c[B0]; \
  281. const type b1 = c[B1]; \
  282. const type b2 = c[B2]; \
  283. const type a1 = c[A1]; \
  284. const type a2 = c[A2]; \
  285. type z1 = b[0]; \
  286. type z2 = b[1]; \
  287. \
  288. for (int n = 0; n + 1 < nb_samples; n++) { \
  289. type in = src[n]; \
  290. type out; \
  291. \
  292. out = in * b0 + z1; \
  293. z1 = b1 * in + z2 + a1 * out; \
  294. z2 = b2 * in + a2 * out; \
  295. dst[n] = out; \
  296. \
  297. n++; \
  298. in = src[n]; \
  299. out = in * b0 + z1; \
  300. z1 = b1 * in + z2 + a1 * out; \
  301. z2 = b2 * in + a2 * out; \
  302. dst[n] = out; \
  303. } \
  304. \
  305. if (nb_samples & 1) { \
  306. const int n = nb_samples - 1; \
  307. const type in = src[n]; \
  308. type out; \
  309. \
  310. out = in * b0 + z1; \
  311. z1 = b1 * in + z2 + a1 * out; \
  312. z2 = b2 * in + a2 * out; \
  313. dst[n] = out; \
  314. } \
  315. \
  316. b[0] = z1; \
  317. b[1] = z2; \
  318. }
  319. BIQUAD_PROCESS(fltp, float)
  320. BIQUAD_PROCESS(dblp, double)
  321. #define XOVER_PROCESS(name, type, one, ff) \
  322. static int filter_channels_## name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
  323. { \
  324. AudioCrossoverContext *s = ctx->priv; \
  325. AVFrame *in = s->input_frame; \
  326. AVFrame **frames = s->frames; \
  327. const int start = (in->channels * jobnr) / nb_jobs; \
  328. const int end = (in->channels * (jobnr+1)) / nb_jobs; \
  329. const int nb_samples = in->nb_samples; \
  330. const int nb_outs = ctx->nb_outputs; \
  331. const int first_order = s->first_order; \
  332. \
  333. for (int ch = start; ch < end; ch++) { \
  334. const type *src = (const type *)in->extended_data[ch]; \
  335. type *xover = (type *)s->xover->extended_data[ch]; \
  336. \
  337. s->fdsp->vector_## ff ##mul_scalar((type *)frames[0]->extended_data[ch], src, \
  338. s->level_in, FFALIGN(nb_samples, sizeof(type))); \
  339. \
  340. for (int band = 0; band < nb_outs; band++) { \
  341. for (int f = 0; band + 1 < nb_outs && f < s->filter_count; f++) { \
  342. const type *prv = (const type *)frames[band]->extended_data[ch]; \
  343. type *dst = (type *)frames[band + 1]->extended_data[ch]; \
  344. const type *hsrc = f == 0 ? prv : dst; \
  345. type *hp = xover + nb_outs * 20 + band * 20 + f * 2; \
  346. const type *const hpc = (type *)&s->hp[band][f].c ## ff; \
  347. \
  348. biquad_process_## name(hpc, hp, dst, hsrc, nb_samples); \
  349. } \
  350. \
  351. for (int f = 0; band + 1 < nb_outs && f < s->filter_count; f++) { \
  352. type *dst = (type *)frames[band]->extended_data[ch]; \
  353. const type *lsrc = dst; \
  354. type *lp = xover + band * 20 + f * 2; \
  355. const type *const lpc = (type *)&s->lp[band][f].c ## ff; \
  356. \
  357. biquad_process_## name(lpc, lp, dst, lsrc, nb_samples); \
  358. } \
  359. \
  360. for (int aband = band + 1; aband + 1 < nb_outs; aband++) { \
  361. if (first_order) { \
  362. const type *asrc = (const type *)frames[band]->extended_data[ch]; \
  363. type *dst = (type *)frames[band]->extended_data[ch]; \
  364. type *ap = xover + nb_outs * 40 + (aband * nb_outs + band) * 20; \
  365. const type *const apc = (type *)&s->ap[aband][0].c ## ff; \
  366. \
  367. biquad_process_## name(apc, ap, dst, asrc, nb_samples); \
  368. } \
  369. \
  370. for (int f = first_order; f < s->ap_filter_count; f++) { \
  371. const type *asrc = (const type *)frames[band]->extended_data[ch]; \
  372. type *dst = (type *)frames[band]->extended_data[ch]; \
  373. type *ap = xover + nb_outs * 40 + (aband * nb_outs + band) * 20 + f * 2;\
  374. const type *const apc = (type *)&s->ap[aband][f].c ## ff; \
  375. \
  376. biquad_process_## name(apc, ap, dst, asrc, nb_samples); \
  377. } \
  378. } \
  379. } \
  380. \
  381. for (int band = 0; band < nb_outs; band++) { \
  382. const type gain = s->gains[band] * ((band & 1 && first_order) ? -one : one); \
  383. type *dst = (type *)frames[band]->extended_data[ch]; \
  384. \
  385. s->fdsp->vector_## ff ##mul_scalar(dst, dst, gain, \
  386. FFALIGN(nb_samples, sizeof(type))); \
  387. } \
  388. } \
  389. \
  390. return 0; \
  391. }
  392. XOVER_PROCESS(fltp, float, 1.f, f)
  393. XOVER_PROCESS(dblp, double, 1.0, d)
  394. static int config_input(AVFilterLink *inlink)
  395. {
  396. AVFilterContext *ctx = inlink->dst;
  397. AudioCrossoverContext *s = ctx->priv;
  398. int sample_rate = inlink->sample_rate;
  399. double q[16];
  400. s->order = (s->order_opt + 1) * 2;
  401. s->filter_count = s->order / 2;
  402. s->first_order = s->filter_count & 1;
  403. s->ap_filter_count = s->filter_count / 2 + s->first_order;
  404. calc_q_factors(s->order, q);
  405. for (int band = 0; band <= s->nb_splits; band++) {
  406. if (s->first_order) {
  407. set_lp(&s->lp[band][0], s->splits[band], 0.5, sample_rate);
  408. set_hp(&s->hp[band][0], s->splits[band], 0.5, sample_rate);
  409. }
  410. for (int n = s->first_order; n < s->filter_count; n++) {
  411. const int idx = s->filter_count / 2 - ((n + s->first_order) / 2 - s->first_order) - 1;
  412. set_lp(&s->lp[band][n], s->splits[band], q[idx], sample_rate);
  413. set_hp(&s->hp[band][n], s->splits[band], q[idx], sample_rate);
  414. }
  415. if (s->first_order)
  416. set_ap1(&s->ap[band][0], s->splits[band], sample_rate);
  417. for (int n = s->first_order; n < s->ap_filter_count; n++) {
  418. const int idx = (s->filter_count / 2 - ((n * 2 + s->first_order) / 2 - s->first_order) - 1);
  419. set_ap(&s->ap[band][n], s->splits[band], q[idx], sample_rate);
  420. }
  421. }
  422. switch (inlink->format) {
  423. case AV_SAMPLE_FMT_FLTP: s->filter_channels = filter_channels_fltp; break;
  424. case AV_SAMPLE_FMT_DBLP: s->filter_channels = filter_channels_dblp; break;
  425. }
  426. s->xover = ff_get_audio_buffer(inlink, 2 * (ctx->nb_outputs * 10 + ctx->nb_outputs * 10 +
  427. ctx->nb_outputs * ctx->nb_outputs * 10));
  428. if (!s->xover)
  429. return AVERROR(ENOMEM);
  430. return 0;
  431. }
  432. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  433. {
  434. AVFilterContext *ctx = inlink->dst;
  435. AudioCrossoverContext *s = ctx->priv;
  436. AVFrame **frames = s->frames;
  437. int i, ret = 0;
  438. for (i = 0; i < ctx->nb_outputs; i++) {
  439. frames[i] = ff_get_audio_buffer(ctx->outputs[i], in->nb_samples);
  440. if (!frames[i]) {
  441. ret = AVERROR(ENOMEM);
  442. break;
  443. }
  444. frames[i]->pts = in->pts;
  445. }
  446. if (ret < 0)
  447. goto fail;
  448. s->input_frame = in;
  449. ctx->internal->execute(ctx, s->filter_channels, NULL, NULL, FFMIN(inlink->channels,
  450. ff_filter_get_nb_threads(ctx)));
  451. for (i = 0; i < ctx->nb_outputs; i++) {
  452. ret = ff_filter_frame(ctx->outputs[i], frames[i]);
  453. frames[i] = NULL;
  454. if (ret < 0)
  455. break;
  456. }
  457. fail:
  458. for (i = 0; i < ctx->nb_outputs; i++)
  459. av_frame_free(&frames[i]);
  460. av_frame_free(&in);
  461. s->input_frame = NULL;
  462. return ret;
  463. }
  464. static av_cold void uninit(AVFilterContext *ctx)
  465. {
  466. AudioCrossoverContext *s = ctx->priv;
  467. int i;
  468. av_freep(&s->fdsp);
  469. av_frame_free(&s->xover);
  470. for (i = 0; i < ctx->nb_outputs; i++)
  471. av_freep(&ctx->output_pads[i].name);
  472. }
  473. static const AVFilterPad inputs[] = {
  474. {
  475. .name = "default",
  476. .type = AVMEDIA_TYPE_AUDIO,
  477. .filter_frame = filter_frame,
  478. .config_props = config_input,
  479. },
  480. { NULL }
  481. };
  482. AVFilter ff_af_acrossover = {
  483. .name = "acrossover",
  484. .description = NULL_IF_CONFIG_SMALL("Split audio into per-bands streams."),
  485. .priv_size = sizeof(AudioCrossoverContext),
  486. .priv_class = &acrossover_class,
  487. .init = init,
  488. .uninit = uninit,
  489. .query_formats = query_formats,
  490. .inputs = inputs,
  491. .outputs = NULL,
  492. .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS |
  493. AVFILTER_FLAG_SLICE_THREADS,
  494. };