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.

408 lines
12KB

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