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.

546 lines
21KB

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