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.

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