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.

508 lines
20KB

  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 < nb_samples; n++) { \
  242. const 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. \
  251. b->z1 = z1; \
  252. b->z2 = z2; \
  253. }
  254. BIQUAD_PROCESS(fltp, float)
  255. BIQUAD_PROCESS(dblp, double)
  256. #define XOVER_PROCESS(name, type, one, ff) \
  257. static int filter_channels_## name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
  258. { \
  259. AudioCrossoverContext *s = ctx->priv; \
  260. AVFrame *in = s->input_frame; \
  261. AVFrame **frames = s->frames; \
  262. const int start = (in->channels * jobnr) / nb_jobs; \
  263. const int end = (in->channels * (jobnr+1)) / nb_jobs; \
  264. const int nb_samples = in->nb_samples; \
  265. \
  266. for (int ch = start; ch < end; ch++) { \
  267. const type *src = (const type *)in->extended_data[ch]; \
  268. CrossoverChannel *xover = &s->xover[ch]; \
  269. \
  270. s->fdsp->vector_## ff ##mul_scalar((type *)frames[0]->extended_data[ch], src, \
  271. s->level_in, nb_samples); \
  272. emms_c(); \
  273. \
  274. for (int band = 0; band < ctx->nb_outputs; band++) { \
  275. for (int f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) { \
  276. const type *prv = (const type *)frames[band]->extended_data[ch]; \
  277. type *dst = (type *)frames[band + 1]->extended_data[ch]; \
  278. const type *hsrc = f == 0 ? prv : dst; \
  279. BiquadContext *hp = &xover->hp[band][f]; \
  280. BiquadCoeffs *hpc = &s->hp[band][f]; \
  281. \
  282. biquad_process_## name(hpc, hp, dst, hsrc, nb_samples); \
  283. } \
  284. \
  285. for (int f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) { \
  286. type *dst = (type *)frames[band]->extended_data[ch]; \
  287. const type *lsrc = dst; \
  288. BiquadContext *lp = &xover->lp[band][f]; \
  289. BiquadCoeffs *lpc = &s->lp[band][f]; \
  290. \
  291. biquad_process_## name(lpc, lp, dst, lsrc, nb_samples); \
  292. } \
  293. \
  294. for (int aband = band + 1; aband + 1 < ctx->nb_outputs; aband++) { \
  295. if (s->first_order) { \
  296. const type *asrc = (const type *)frames[band]->extended_data[ch]; \
  297. type *dst = (type *)frames[band]->extended_data[ch]; \
  298. BiquadContext *ap = &xover->ap[band][aband][0]; \
  299. BiquadCoeffs *apc = &s->ap[aband][0]; \
  300. \
  301. biquad_process_## name(apc, ap, dst, asrc, nb_samples); \
  302. } \
  303. \
  304. for (int f = s->first_order; f < s->ap_filter_count; f++) { \
  305. const type *asrc = (const type *)frames[band]->extended_data[ch]; \
  306. type *dst = (type *)frames[band]->extended_data[ch]; \
  307. BiquadContext *ap = &xover->ap[band][aband][f]; \
  308. BiquadCoeffs *apc = &s->ap[aband][f]; \
  309. \
  310. biquad_process_## name(apc, ap, dst, asrc, nb_samples); \
  311. } \
  312. } \
  313. } \
  314. \
  315. for (int band = 0; band < ctx->nb_outputs && s->first_order; band++) { \
  316. if (band & 1) { \
  317. type *dst = (type *)frames[band]->extended_data[ch]; \
  318. \
  319. for (int n = 0; n < nb_samples; n++) \
  320. dst[n] *= -one; \
  321. } \
  322. } \
  323. } \
  324. \
  325. return 0; \
  326. }
  327. XOVER_PROCESS(fltp, float, 1.f, f)
  328. XOVER_PROCESS(dblp, double, 1.0, d)
  329. static int config_input(AVFilterLink *inlink)
  330. {
  331. AVFilterContext *ctx = inlink->dst;
  332. AudioCrossoverContext *s = ctx->priv;
  333. int sample_rate = inlink->sample_rate;
  334. double q[16];
  335. s->xover = av_calloc(inlink->channels, sizeof(*s->xover));
  336. if (!s->xover)
  337. return AVERROR(ENOMEM);
  338. s->order = (s->order_opt + 1) * 2;
  339. s->filter_count = s->order / 2;
  340. s->first_order = s->filter_count & 1;
  341. s->ap_filter_count = s->filter_count / 2 + s->first_order;
  342. calc_q_factors(s->order, q);
  343. for (int band = 0; band <= s->nb_splits; band++) {
  344. if (s->first_order) {
  345. set_lp(&s->lp[band][0], s->splits[band], 0.5, sample_rate);
  346. set_hp(&s->hp[band][0], s->splits[band], 0.5, sample_rate);
  347. }
  348. for (int n = s->first_order; n < s->filter_count; n++) {
  349. const int idx = s->filter_count / 2 - ((n + s->first_order) / 2 - s->first_order) - 1;
  350. set_lp(&s->lp[band][n], s->splits[band], q[idx], sample_rate);
  351. set_hp(&s->hp[band][n], s->splits[band], q[idx], sample_rate);
  352. }
  353. if (s->first_order)
  354. set_ap1(&s->ap[band][0], s->splits[band], sample_rate);
  355. for (int n = s->first_order; n < s->ap_filter_count; n++) {
  356. const int idx = (s->filter_count / 2 - ((n * 2 + s->first_order) / 2 - s->first_order) - 1);
  357. set_ap(&s->ap[band][n], s->splits[band], q[idx], sample_rate);
  358. }
  359. }
  360. switch (inlink->format) {
  361. case AV_SAMPLE_FMT_FLTP: s->filter_channels = filter_channels_fltp; break;
  362. case AV_SAMPLE_FMT_DBLP: s->filter_channels = filter_channels_dblp; break;
  363. }
  364. return 0;
  365. }
  366. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  367. {
  368. AVFilterContext *ctx = inlink->dst;
  369. AudioCrossoverContext *s = ctx->priv;
  370. AVFrame **frames = s->frames;
  371. int i, ret = 0;
  372. for (i = 0; i < ctx->nb_outputs; i++) {
  373. frames[i] = ff_get_audio_buffer(ctx->outputs[i], in->nb_samples);
  374. if (!frames[i]) {
  375. ret = AVERROR(ENOMEM);
  376. break;
  377. }
  378. frames[i]->pts = in->pts;
  379. }
  380. if (ret < 0)
  381. goto fail;
  382. s->input_frame = in;
  383. ctx->internal->execute(ctx, s->filter_channels, NULL, NULL, FFMIN(inlink->channels,
  384. ff_filter_get_nb_threads(ctx)));
  385. for (i = 0; i < ctx->nb_outputs; i++) {
  386. ret = ff_filter_frame(ctx->outputs[i], frames[i]);
  387. frames[i] = NULL;
  388. if (ret < 0)
  389. break;
  390. }
  391. fail:
  392. for (i = 0; i < ctx->nb_outputs; i++)
  393. av_frame_free(&frames[i]);
  394. av_frame_free(&in);
  395. s->input_frame = NULL;
  396. return ret;
  397. }
  398. static av_cold void uninit(AVFilterContext *ctx)
  399. {
  400. AudioCrossoverContext *s = ctx->priv;
  401. int i;
  402. av_freep(&s->fdsp);
  403. av_freep(&s->splits);
  404. av_freep(&s->xover);
  405. for (i = 0; i < ctx->nb_outputs; i++)
  406. av_freep(&ctx->output_pads[i].name);
  407. }
  408. static const AVFilterPad inputs[] = {
  409. {
  410. .name = "default",
  411. .type = AVMEDIA_TYPE_AUDIO,
  412. .filter_frame = filter_frame,
  413. .config_props = config_input,
  414. },
  415. { NULL }
  416. };
  417. AVFilter ff_af_acrossover = {
  418. .name = "acrossover",
  419. .description = NULL_IF_CONFIG_SMALL("Split audio into per-bands streams."),
  420. .priv_size = sizeof(AudioCrossoverContext),
  421. .priv_class = &acrossover_class,
  422. .init = init,
  423. .uninit = uninit,
  424. .query_formats = query_formats,
  425. .inputs = inputs,
  426. .outputs = NULL,
  427. .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS |
  428. AVFILTER_FLAG_SLICE_THREADS,
  429. };