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
11KB

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