af_acrossover.c 9.4 KB

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