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.

471 lines
14KB

  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 b0, b1, b2;
  38. double a1, a2;
  39. double z1, z2;
  40. } BiquadContext;
  41. typedef struct CrossoverChannel {
  42. BiquadContext lp[MAX_BANDS][20];
  43. BiquadContext hp[MAX_BANDS][20];
  44. BiquadContext ap[MAX_BANDS][MAX_BANDS][20];
  45. } CrossoverChannel;
  46. typedef struct AudioCrossoverContext {
  47. const AVClass *class;
  48. char *splits_str;
  49. int order_opt;
  50. int order;
  51. int filter_count;
  52. int first_order;
  53. int ap_filter_count;
  54. int nb_splits;
  55. float *splits;
  56. CrossoverChannel *xover;
  57. AVFrame *input_frame;
  58. AVFrame *frames[MAX_BANDS];
  59. } AudioCrossoverContext;
  60. #define OFFSET(x) offsetof(AudioCrossoverContext, x)
  61. #define AF AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  62. static const AVOption acrossover_options[] = {
  63. { "split", "set split frequencies", OFFSET(splits_str), AV_OPT_TYPE_STRING, {.str="500"}, 0, 0, AF },
  64. { "order", "set order", OFFSET(order_opt), AV_OPT_TYPE_INT, {.i64=1}, 0, 9, AF, "m" },
  65. { "2nd", "2nd order", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "m" },
  66. { "4th", "4th order", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "m" },
  67. { "6th", "6th order", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "m" },
  68. { "8th", "8th order", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF, "m" },
  69. { "10th", "10th order", 0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, AF, "m" },
  70. { "12th", "12th order", 0, AV_OPT_TYPE_CONST, {.i64=5}, 0, 0, AF, "m" },
  71. { "14th", "14th order", 0, AV_OPT_TYPE_CONST, {.i64=6}, 0, 0, AF, "m" },
  72. { "16th", "16th order", 0, AV_OPT_TYPE_CONST, {.i64=7}, 0, 0, AF, "m" },
  73. { "18th", "18th order", 0, AV_OPT_TYPE_CONST, {.i64=8}, 0, 0, AF, "m" },
  74. { "20th", "20th order", 0, AV_OPT_TYPE_CONST, {.i64=9}, 0, 0, AF, "m" },
  75. { NULL }
  76. };
  77. AVFILTER_DEFINE_CLASS(acrossover);
  78. static av_cold int init(AVFilterContext *ctx)
  79. {
  80. AudioCrossoverContext *s = ctx->priv;
  81. char *p, *arg, *saveptr = NULL;
  82. int i, ret = 0;
  83. s->splits = av_calloc(MAX_SPLITS, sizeof(*s->splits));
  84. if (!s->splits)
  85. return AVERROR(ENOMEM);
  86. p = s->splits_str;
  87. for (i = 0; i < MAX_SPLITS; i++) {
  88. float freq;
  89. if (!(arg = av_strtok(p, " |", &saveptr)))
  90. break;
  91. p = NULL;
  92. if (av_sscanf(arg, "%f", &freq) != 1) {
  93. av_log(ctx, AV_LOG_ERROR, "Invalid syntax for frequency[%d].\n", i);
  94. return AVERROR(EINVAL);
  95. }
  96. if (freq <= 0) {
  97. av_log(ctx, AV_LOG_ERROR, "Frequency %f must be positive number.\n", freq);
  98. return AVERROR(EINVAL);
  99. }
  100. if (i > 0 && freq <= s->splits[i-1]) {
  101. av_log(ctx, AV_LOG_ERROR, "Frequency %f must be in increasing order.\n", freq);
  102. return AVERROR(EINVAL);
  103. }
  104. s->splits[i] = freq;
  105. }
  106. s->nb_splits = i;
  107. for (i = 0; i <= s->nb_splits; i++) {
  108. AVFilterPad pad = { 0 };
  109. char *name;
  110. pad.type = AVMEDIA_TYPE_AUDIO;
  111. name = av_asprintf("out%d", ctx->nb_outputs);
  112. if (!name)
  113. return AVERROR(ENOMEM);
  114. pad.name = name;
  115. if ((ret = ff_insert_outpad(ctx, i, &pad)) < 0) {
  116. av_freep(&pad.name);
  117. return ret;
  118. }
  119. }
  120. return ret;
  121. }
  122. static void set_lp(BiquadContext *b, double fc, double q, double sr)
  123. {
  124. double omega = M_PI * fc / sr;
  125. double cosine = cos(omega);
  126. double alpha = sin(omega) / (2. * q);
  127. double b0 = (1. - cosine) / 2.;
  128. double b1 = 1. - cosine;
  129. double b2 = (1. - cosine) / 2.;
  130. double a0 = 1. + alpha;
  131. double a1 = -2. * cosine;
  132. double a2 = 1. - alpha;
  133. b->b0 = b0 / a0;
  134. b->b1 = b1 / a0;
  135. b->b2 = b2 / a0;
  136. b->a1 = -a1 / a0;
  137. b->a2 = -a2 / a0;
  138. }
  139. static void set_hp(BiquadContext *b, double fc, double q, double sr)
  140. {
  141. double omega = M_PI * fc / sr;
  142. double cosine = cos(omega);
  143. double alpha = sin(omega) / (2. * q);
  144. double b0 = (1. + cosine) / 2.;
  145. double b1 = -1. - cosine;
  146. double b2 = (1. + cosine) / 2.;
  147. double a0 = 1. + alpha;
  148. double a1 = -2. * cosine;
  149. double a2 = 1. - alpha;
  150. b->b0 = b0 / a0;
  151. b->b1 = b1 / a0;
  152. b->b2 = b2 / a0;
  153. b->a1 = -a1 / a0;
  154. b->a2 = -a2 / a0;
  155. }
  156. static void set_ap(BiquadContext *b, double fc, double q, double sr)
  157. {
  158. double omega = M_PI * fc / sr;
  159. double cosine = cos(omega);
  160. double alpha = sin(omega) / (2. * q);
  161. double a0 = 1. + alpha;
  162. double a1 = -2. * cosine;
  163. double a2 = 1. - alpha;
  164. double b0 = a2;
  165. double b1 = a1;
  166. double b2 = a0;
  167. b->b0 = b0 / a0;
  168. b->b1 = b1 / a0;
  169. b->b2 = b2 / a0;
  170. b->a1 = -a1 / a0;
  171. b->a2 = -a2 / a0;
  172. }
  173. static void set_ap1(BiquadContext *b, double fc, double sr)
  174. {
  175. double omega = M_PI * fc / sr;
  176. double K = tan(omega);
  177. b->a1 = -(1. - K) / (1. + K);
  178. b->a2 = 0.;
  179. b->b0 = -b->a1;
  180. b->b1 = 1.;
  181. b->b2 = 0.;
  182. }
  183. static void calc_q_factors(int order, double *q)
  184. {
  185. double n = order / 2.;
  186. for (int i = 0; i < n / 2; i++)
  187. q[i] = 1. / (-2. * cos(M_PI * (2. * (i + 1) + n - 1.) / (2. * n)));
  188. }
  189. static int config_input(AVFilterLink *inlink)
  190. {
  191. AVFilterContext *ctx = inlink->dst;
  192. AudioCrossoverContext *s = ctx->priv;
  193. int sample_rate = inlink->sample_rate;
  194. double q[16];
  195. s->xover = av_calloc(inlink->channels, sizeof(*s->xover));
  196. if (!s->xover)
  197. return AVERROR(ENOMEM);
  198. s->order = (s->order_opt + 1) * 2;
  199. s->filter_count = s->order / 2;
  200. s->first_order = s->filter_count & 1;
  201. s->ap_filter_count = s->filter_count / 2 + s->first_order;
  202. calc_q_factors(s->order, q);
  203. for (int ch = 0; ch < inlink->channels; ch++) {
  204. for (int band = 0; band <= s->nb_splits; band++) {
  205. if (s->first_order) {
  206. set_lp(&s->xover[ch].lp[band][0], s->splits[band], 0.5, sample_rate);
  207. set_hp(&s->xover[ch].hp[band][0], s->splits[band], 0.5, sample_rate);
  208. }
  209. for (int n = s->first_order; n < s->filter_count; n++) {
  210. const int idx = s->filter_count / 2 - ((n + s->first_order) / 2 - s->first_order) - 1;
  211. set_lp(&s->xover[ch].lp[band][n], s->splits[band], q[idx], sample_rate);
  212. set_hp(&s->xover[ch].hp[band][n], s->splits[band], q[idx], sample_rate);
  213. }
  214. for (int x = 0; x <= s->nb_splits && s->first_order; x++)
  215. set_ap1(&s->xover[ch].ap[x][band][0], s->splits[band], sample_rate);
  216. for (int n = s->first_order; n < s->ap_filter_count; n++) {
  217. const int idx = (s->filter_count / 2 - ((n * 2 + s->first_order) / 2 - s->first_order) - 1);
  218. for (int x = 0; x <= s->nb_splits; x++)
  219. set_ap(&s->xover[ch].ap[x][band][n], s->splits[band], q[idx], sample_rate);
  220. }
  221. }
  222. }
  223. return 0;
  224. }
  225. static int query_formats(AVFilterContext *ctx)
  226. {
  227. AVFilterFormats *formats;
  228. AVFilterChannelLayouts *layouts;
  229. static const enum AVSampleFormat sample_fmts[] = {
  230. AV_SAMPLE_FMT_DBLP,
  231. AV_SAMPLE_FMT_NONE
  232. };
  233. int ret;
  234. layouts = ff_all_channel_counts();
  235. if (!layouts)
  236. return AVERROR(ENOMEM);
  237. ret = ff_set_common_channel_layouts(ctx, layouts);
  238. if (ret < 0)
  239. return ret;
  240. formats = ff_make_format_list(sample_fmts);
  241. if (!formats)
  242. return AVERROR(ENOMEM);
  243. ret = ff_set_common_formats(ctx, formats);
  244. if (ret < 0)
  245. return ret;
  246. formats = ff_all_samplerates();
  247. if (!formats)
  248. return AVERROR(ENOMEM);
  249. return ff_set_common_samplerates(ctx, formats);
  250. }
  251. static void biquad_process(BiquadContext *b,
  252. double *dst, const double *src,
  253. int nb_samples)
  254. {
  255. const double b0 = b->b0;
  256. const double b1 = b->b1;
  257. const double b2 = b->b2;
  258. const double a1 = b->a1;
  259. const double a2 = b->a2;
  260. double z1 = b->z1;
  261. double z2 = b->z2;
  262. for (int n = 0; n < nb_samples; n++) {
  263. const double in = src[n];
  264. double out;
  265. out = in * b0 + z1;
  266. z1 = b1 * in + z2 + a1 * out;
  267. z2 = b2 * in + a2 * out;
  268. dst[n] = out;
  269. }
  270. b->z1 = z1;
  271. b->z2 = z2;
  272. }
  273. static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  274. {
  275. AudioCrossoverContext *s = ctx->priv;
  276. AVFrame *in = s->input_frame;
  277. AVFrame **frames = s->frames;
  278. const int start = (in->channels * jobnr) / nb_jobs;
  279. const int end = (in->channels * (jobnr+1)) / nb_jobs;
  280. const int nb_samples = in->nb_samples;
  281. for (int ch = start; ch < end; ch++) {
  282. CrossoverChannel *xover = &s->xover[ch];
  283. for (int band = 0; band < ctx->nb_outputs; band++) {
  284. for (int f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) {
  285. const double *src = band == 0 ? (const double *)in->extended_data[ch] : (const double *)frames[band]->extended_data[ch];
  286. double *dst = (double *)frames[band + 1]->extended_data[ch];
  287. const double *hsrc = f == 0 ? src : dst;
  288. BiquadContext *hp = &xover->hp[band][f];
  289. biquad_process(hp, dst, hsrc, nb_samples);
  290. }
  291. for (int f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) {
  292. const double *src = band == 0 ? (const double *)in->extended_data[ch] : (const double *)frames[band]->extended_data[ch];
  293. double *dst = (double *)frames[band]->extended_data[ch];
  294. const double *lsrc = f == 0 ? src : dst;
  295. BiquadContext *lp = &xover->lp[band][f];
  296. biquad_process(lp, dst, lsrc, nb_samples);
  297. }
  298. for (int aband = band + 1; aband < ctx->nb_outputs; aband++) {
  299. if (s->first_order) {
  300. const double *src = (const double *)frames[band]->extended_data[ch];
  301. double *dst = (double *)frames[band]->extended_data[ch];
  302. BiquadContext *ap = &xover->ap[band][aband][0];
  303. biquad_process(ap, dst, src, nb_samples);
  304. }
  305. for (int f = s->first_order; f < s->ap_filter_count; f++) {
  306. const double *src = (const double *)frames[band]->extended_data[ch];
  307. double *dst = (double *)frames[band]->extended_data[ch];
  308. BiquadContext *ap = &xover->ap[band][aband][f];
  309. biquad_process(ap, dst, src, nb_samples);
  310. }
  311. }
  312. }
  313. for (int band = 0; band < ctx->nb_outputs && s->first_order; band++) {
  314. if (band & 1) {
  315. double *dst = (double *)frames[band]->extended_data[ch];
  316. for (int n = 0; n < nb_samples; n++)
  317. dst[n] *= -1.;
  318. }
  319. }
  320. }
  321. return 0;
  322. }
  323. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  324. {
  325. AVFilterContext *ctx = inlink->dst;
  326. AudioCrossoverContext *s = ctx->priv;
  327. AVFrame **frames = s->frames;
  328. int i, ret = 0;
  329. for (i = 0; i < ctx->nb_outputs; i++) {
  330. frames[i] = ff_get_audio_buffer(ctx->outputs[i], in->nb_samples);
  331. if (!frames[i]) {
  332. ret = AVERROR(ENOMEM);
  333. break;
  334. }
  335. frames[i]->pts = in->pts;
  336. }
  337. if (ret < 0)
  338. goto fail;
  339. s->input_frame = in;
  340. ctx->internal->execute(ctx, filter_channels, NULL, NULL, FFMIN(inlink->channels,
  341. ff_filter_get_nb_threads(ctx)));
  342. for (i = 0; i < ctx->nb_outputs; i++) {
  343. ret = ff_filter_frame(ctx->outputs[i], frames[i]);
  344. frames[i] = NULL;
  345. if (ret < 0)
  346. break;
  347. }
  348. fail:
  349. for (i = 0; i < ctx->nb_outputs; i++)
  350. av_frame_free(&frames[i]);
  351. av_frame_free(&in);
  352. s->input_frame = NULL;
  353. return ret;
  354. }
  355. static av_cold void uninit(AVFilterContext *ctx)
  356. {
  357. AudioCrossoverContext *s = ctx->priv;
  358. int i;
  359. av_freep(&s->splits);
  360. av_freep(&s->xover);
  361. for (i = 0; i < ctx->nb_outputs; i++)
  362. av_freep(&ctx->output_pads[i].name);
  363. }
  364. static const AVFilterPad inputs[] = {
  365. {
  366. .name = "default",
  367. .type = AVMEDIA_TYPE_AUDIO,
  368. .filter_frame = filter_frame,
  369. .config_props = config_input,
  370. },
  371. { NULL }
  372. };
  373. AVFilter ff_af_acrossover = {
  374. .name = "acrossover",
  375. .description = NULL_IF_CONFIG_SMALL("Split audio into per-bands streams."),
  376. .priv_size = sizeof(AudioCrossoverContext),
  377. .priv_class = &acrossover_class,
  378. .init = init,
  379. .uninit = uninit,
  380. .query_formats = query_formats,
  381. .inputs = inputs,
  382. .outputs = NULL,
  383. .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS |
  384. AVFILTER_FLAG_SLICE_THREADS,
  385. };