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.

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