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.

377 lines
10KB

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