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.

349 lines
9.2KB

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