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.

344 lines
9.3KB

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