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.

487 lines
19KB

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