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.

628 lines
27KB

  1. /*
  2. * Copyright (c) 2013 Paul B Mahol
  3. * Copyright (c) 2006-2008 Rob Sykes <robs@users.sourceforge.net>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /*
  22. * 2-pole filters designed by Robert Bristow-Johnson <rbj@audioimagination.com>
  23. * see http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt
  24. *
  25. * 1-pole filters based on code (c) 2000 Chris Bagwell <cbagwell@sprynet.com>
  26. * Algorithms: Recursive single pole low/high pass filter
  27. * Reference: The Scientist and Engineer's Guide to Digital Signal Processing
  28. *
  29. * low-pass: output[N] = input[N] * A + output[N-1] * B
  30. * X = exp(-2.0 * pi * Fc)
  31. * A = 1 - X
  32. * B = X
  33. * Fc = cutoff freq / sample rate
  34. *
  35. * Mimics an RC low-pass filter:
  36. *
  37. * ---/\/\/\/\----------->
  38. * |
  39. * --- C
  40. * ---
  41. * |
  42. * |
  43. * V
  44. *
  45. * high-pass: output[N] = A0 * input[N] + A1 * input[N-1] + B1 * output[N-1]
  46. * X = exp(-2.0 * pi * Fc)
  47. * A0 = (1 + X) / 2
  48. * A1 = -(1 + X) / 2
  49. * B1 = X
  50. * Fc = cutoff freq / sample rate
  51. *
  52. * Mimics an RC high-pass filter:
  53. *
  54. * || C
  55. * ----||--------->
  56. * || |
  57. * <
  58. * > R
  59. * <
  60. * |
  61. * V
  62. */
  63. #include "libavutil/opt.h"
  64. #include "libavutil/avassert.h"
  65. #include "audio.h"
  66. #include "avfilter.h"
  67. #include "internal.h"
  68. enum FilterType {
  69. biquad,
  70. equalizer,
  71. bass,
  72. treble,
  73. band,
  74. bandpass,
  75. bandreject,
  76. allpass,
  77. highpass,
  78. lowpass,
  79. };
  80. enum WidthType {
  81. NONE,
  82. HZ,
  83. OCTAVE,
  84. QFACTOR,
  85. SLOPE,
  86. };
  87. typedef struct ChanCache {
  88. double i1, i2;
  89. double o1, o2;
  90. } ChanCache;
  91. typedef struct {
  92. const AVClass *class;
  93. enum FilterType filter_type;
  94. enum WidthType width_type;
  95. int poles;
  96. int csg;
  97. double gain;
  98. double frequency;
  99. double width;
  100. double a0, a1, a2;
  101. double b0, b1, b2;
  102. ChanCache *cache;
  103. void (*filter)(const void *ibuf, void *obuf, int len,
  104. double *i1, double *i2, double *o1, double *o2,
  105. double b0, double b1, double b2, double a1, double a2);
  106. } BiquadsContext;
  107. static av_cold int init(AVFilterContext *ctx, const char *args)
  108. {
  109. BiquadsContext *p = ctx->priv;
  110. int ret;
  111. av_opt_set_defaults(p);
  112. if ((ret = av_set_options_string(p, args, "=", ":")) < 0)
  113. return ret;
  114. if (p->filter_type != biquad) {
  115. if (p->frequency <= 0 || p->width <= 0) {
  116. av_log(ctx, AV_LOG_ERROR, "Invalid frequency %f and/or width %f <= 0\n",
  117. p->frequency, p->width);
  118. return AVERROR(EINVAL);
  119. }
  120. }
  121. return 0;
  122. }
  123. static int query_formats(AVFilterContext *ctx)
  124. {
  125. AVFilterFormats *formats;
  126. AVFilterChannelLayouts *layouts;
  127. static const enum AVSampleFormat sample_fmts[] = {
  128. AV_SAMPLE_FMT_S16P,
  129. AV_SAMPLE_FMT_S32P,
  130. AV_SAMPLE_FMT_FLTP,
  131. AV_SAMPLE_FMT_DBLP,
  132. AV_SAMPLE_FMT_NONE
  133. };
  134. layouts = ff_all_channel_layouts();
  135. if (!layouts)
  136. return AVERROR(ENOMEM);
  137. ff_set_common_channel_layouts(ctx, layouts);
  138. formats = ff_make_format_list(sample_fmts);
  139. if (!formats)
  140. return AVERROR(ENOMEM);
  141. ff_set_common_formats(ctx, formats);
  142. formats = ff_all_samplerates();
  143. if (!formats)
  144. return AVERROR(ENOMEM);
  145. ff_set_common_samplerates(ctx, formats);
  146. return 0;
  147. }
  148. #define BIQUAD_FILTER(name, type, min, max) \
  149. static void biquad_## name (const void *input, void *output, int len, \
  150. double *in1, double *in2, \
  151. double *out1, double *out2, \
  152. double b0, double b1, double b2, \
  153. double a1, double a2) \
  154. { \
  155. const type *ibuf = input; \
  156. type *obuf = output; \
  157. double i1 = *in1; \
  158. double i2 = *in2; \
  159. double o1 = *out1; \
  160. double o2 = *out2; \
  161. int i; \
  162. a1 = -a1; \
  163. a2 = -a2; \
  164. \
  165. for (i = 0; i+1 < len; i++) { \
  166. o2 = i2 * b2 + i1 * b1 + ibuf[i] * b0 + o2 * a2 + o1 * a1; \
  167. i2 = ibuf[i]; \
  168. if (o2 < min) { \
  169. av_log(NULL, AV_LOG_WARNING, "clipping\n"); \
  170. obuf[i] = min; \
  171. } else if (o2 > max) { \
  172. av_log(NULL, AV_LOG_WARNING, "clipping\n"); \
  173. obuf[i] = max; \
  174. } else { \
  175. obuf[i] = o2; \
  176. } \
  177. i++; \
  178. o1 = i1 * b2 + i2 * b1 + ibuf[i] * b0 + o1 * a2 + o2 * a1; \
  179. i1 = ibuf[i]; \
  180. if (o1 < min) { \
  181. av_log(NULL, AV_LOG_WARNING, "clipping\n"); \
  182. obuf[i] = min; \
  183. } else if (o1 > max) { \
  184. av_log(NULL, AV_LOG_WARNING, "clipping\n"); \
  185. obuf[i] = max; \
  186. } else { \
  187. obuf[i] = o1; \
  188. } \
  189. } \
  190. if (i < len) { \
  191. double o0 = ibuf[i] * b0 + i1 * b1 + i2 * b2 + o1 * a1 + o2 * a2; \
  192. i2 = i1; \
  193. i1 = ibuf[i]; \
  194. o2 = o1; \
  195. o1 = o0; \
  196. if (o0 < min) { \
  197. av_log(NULL, AV_LOG_WARNING, "clipping\n"); \
  198. obuf[i] = min; \
  199. } else if (o0 > max) { \
  200. av_log(NULL, AV_LOG_WARNING, "clipping\n"); \
  201. obuf[i] = max; \
  202. } else { \
  203. obuf[i] = o0; \
  204. } \
  205. } \
  206. *in1 = i1; \
  207. *in2 = i2; \
  208. *out1 = o1; \
  209. *out2 = o2; \
  210. }
  211. BIQUAD_FILTER(s16, int16_t, INT16_MIN, INT16_MAX)
  212. BIQUAD_FILTER(s32, int32_t, INT32_MIN, INT32_MAX)
  213. BIQUAD_FILTER(flt, float, -1., 1.)
  214. BIQUAD_FILTER(dbl, double, -1., 1.)
  215. static int config_output(AVFilterLink *outlink)
  216. {
  217. AVFilterContext *ctx = outlink->src;
  218. BiquadsContext *p = ctx->priv;
  219. AVFilterLink *inlink = ctx->inputs[0];
  220. double A = exp(p->gain / 40 * log(10.));
  221. double w0 = 2 * M_PI * p->frequency / inlink->sample_rate;
  222. double alpha;
  223. if (w0 > M_PI) {
  224. av_log(ctx, AV_LOG_ERROR,
  225. "Invalid frequency %f. Frequency must be less than half the sample-rate %d.\n",
  226. p->frequency, inlink->sample_rate);
  227. return AVERROR(EINVAL);
  228. }
  229. switch (p->width_type) {
  230. case NONE:
  231. alpha = 0.0;
  232. break;
  233. case HZ:
  234. alpha = sin(w0) / (2 * p->frequency / p->width);
  235. break;
  236. case OCTAVE:
  237. alpha = sin(w0) * sinh(log(2.) / 2 * p->width * w0 / sin(w0));
  238. break;
  239. case QFACTOR:
  240. alpha = sin(w0) / (2 * p->width);
  241. break;
  242. case SLOPE:
  243. alpha = sin(w0) / 2 * sqrt((A + 1 / A) * (1 / p->width - 1) + 2);
  244. break;
  245. default:
  246. av_assert0(0);
  247. }
  248. switch (p->filter_type) {
  249. case biquad:
  250. break;
  251. case equalizer:
  252. p->a0 = 1 + alpha / A;
  253. p->a1 = -2 * cos(w0);
  254. p->a2 = 1 - alpha / A;
  255. p->b0 = 1 + alpha * A;
  256. p->b1 = -2 * cos(w0);
  257. p->b2 = 1 - alpha * A;
  258. break;
  259. case bass:
  260. p->a0 = (A + 1) + (A - 1) * cos(w0) + 2 * sqrt(A) * alpha;
  261. p->a1 = -2 * ((A - 1) + (A + 1) * cos(w0));
  262. p->a2 = (A + 1) + (A - 1) * cos(w0) - 2 * sqrt(A) * alpha;
  263. p->b0 = A * ((A + 1) - (A - 1) * cos(w0) + 2 * sqrt(A) * alpha);
  264. p->b1 = 2 * A * ((A - 1) - (A + 1) * cos(w0));
  265. p->b2 = A * ((A + 1) - (A - 1) * cos(w0) - 2 * sqrt(A) * alpha);
  266. break;
  267. case treble:
  268. p->a0 = (A + 1) - (A - 1) * cos(w0) + 2 * sqrt(A) * alpha;
  269. p->a1 = 2 * ((A - 1) - (A + 1) * cos(w0));
  270. p->a2 = (A + 1) - (A - 1) * cos(w0) - 2 * sqrt(A) * alpha;
  271. p->b0 = A * ((A + 1) + (A - 1) * cos(w0) + 2 * sqrt(A) * alpha);
  272. p->b1 =-2 * A * ((A - 1) + (A + 1) * cos(w0));
  273. p->b2 = A * ((A + 1) + (A - 1) * cos(w0) - 2 * sqrt(A) * alpha);
  274. break;
  275. case bandpass:
  276. if (p->csg) {
  277. p->a0 = 1 + alpha;
  278. p->a1 = -2 * cos(w0);
  279. p->a2 = 1 - alpha;
  280. p->b0 = sin(w0) / 2;
  281. p->b1 = 0;
  282. p->b2 = -sin(w0) / 2;
  283. } else {
  284. p->a0 = 1 + alpha;
  285. p->a1 = -2 * cos(w0);
  286. p->a2 = 1 - alpha;
  287. p->b0 = alpha;
  288. p->b1 = 0;
  289. p->b2 = -alpha;
  290. }
  291. break;
  292. case bandreject:
  293. p->a0 = 1 + alpha;
  294. p->a1 = -2 * cos(w0);
  295. p->a2 = 1 - alpha;
  296. p->b0 = 1;
  297. p->b1 = -2 * cos(w0);
  298. p->b2 = 1;
  299. break;
  300. case lowpass:
  301. if (p->poles == 1) {
  302. p->a0 = 1;
  303. p->a1 = -exp(-w0);
  304. p->a2 = 0;
  305. p->b0 = 1 + p->a1;
  306. p->b1 = 0;
  307. p->b2 = 0;
  308. } else {
  309. p->a0 = 1 + alpha;
  310. p->a1 = -2 * cos(w0);
  311. p->a2 = 1 - alpha;
  312. p->b0 = (1 - cos(w0)) / 2;
  313. p->b1 = 1 - cos(w0);
  314. p->b2 = (1 - cos(w0)) / 2;
  315. }
  316. break;
  317. case highpass:
  318. if (p->poles == 1) {
  319. p->a0 = 1;
  320. p->a1 = -exp(-w0);
  321. p->a2 = 0;
  322. p->b0 = (1 - p->a1) / 2;
  323. p->b1 = -p->b0;
  324. p->b2 = 0;
  325. } else {
  326. p->a0 = 1 + alpha;
  327. p->a1 = -2 * cos(w0);
  328. p->a2 = 1 - alpha;
  329. p->b0 = (1 + cos(w0)) / 2;
  330. p->b1 = -(1 + cos(w0));
  331. p->b2 = (1 + cos(w0)) / 2;
  332. }
  333. break;
  334. case allpass:
  335. p->a0 = 1 + alpha;
  336. p->a1 = -2 * cos(w0);
  337. p->a2 = 1 - alpha;
  338. p->b0 = 1 - alpha;
  339. p->b1 = -2 * cos(w0);
  340. p->b2 = 1 + alpha;
  341. break;
  342. default:
  343. av_assert0(0);
  344. }
  345. p->a1 /= p->a0;
  346. p->a2 /= p->a0;
  347. p->b0 /= p->a0;
  348. p->b1 /= p->a0;
  349. p->b2 /= p->a0;
  350. p->cache = av_realloc_f(p->cache, sizeof(ChanCache), inlink->channels);
  351. if (!p->cache)
  352. return AVERROR(ENOMEM);
  353. memset(p->cache, 0, sizeof(ChanCache) * inlink->channels);
  354. switch (inlink->format) {
  355. case AV_SAMPLE_FMT_S16P: p->filter = biquad_s16; break;
  356. case AV_SAMPLE_FMT_S32P: p->filter = biquad_s32; break;
  357. case AV_SAMPLE_FMT_FLTP: p->filter = biquad_flt; break;
  358. case AV_SAMPLE_FMT_DBLP: p->filter = biquad_dbl; break;
  359. default: av_assert0(0);
  360. }
  361. return 0;
  362. }
  363. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  364. {
  365. BiquadsContext *p = inlink->dst->priv;
  366. AVFilterLink *outlink = inlink->dst->outputs[0];
  367. AVFrame *out_buf;
  368. int nb_samples = buf->nb_samples;
  369. int ch;
  370. if (av_frame_is_writable(buf)) {
  371. out_buf = buf;
  372. } else {
  373. out_buf = ff_get_audio_buffer(inlink, nb_samples);
  374. if (!out_buf)
  375. return AVERROR(ENOMEM);
  376. out_buf->pts = buf->pts;
  377. }
  378. for (ch = 0; ch < buf->channels; ch++)
  379. p->filter(buf->extended_data[ch],
  380. out_buf->extended_data[ch], nb_samples,
  381. &p->cache[ch].i1, &p->cache[ch].i2,
  382. &p->cache[ch].o1, &p->cache[ch].o2,
  383. p->b0, p->b1, p->b2, p->a1, p->a2);
  384. if (buf != out_buf)
  385. av_frame_free(&buf);
  386. return ff_filter_frame(outlink, out_buf);
  387. }
  388. static av_cold void uninit(AVFilterContext *ctx)
  389. {
  390. BiquadsContext *p = ctx->priv;
  391. av_freep(&p->cache);
  392. av_opt_free(p);
  393. }
  394. static const AVFilterPad inputs[] = {
  395. {
  396. .name = "default",
  397. .type = AVMEDIA_TYPE_AUDIO,
  398. .filter_frame = filter_frame,
  399. },
  400. { NULL }
  401. };
  402. static const AVFilterPad outputs[] = {
  403. {
  404. .name = "default",
  405. .type = AVMEDIA_TYPE_AUDIO,
  406. .config_props = config_output,
  407. },
  408. { NULL }
  409. };
  410. #define OFFSET(x) offsetof(BiquadsContext, x)
  411. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  412. #define DEFINE_BIQUAD_FILTER(name_, description_) \
  413. AVFILTER_DEFINE_CLASS(name_); \
  414. static av_cold int name_##_init(AVFilterContext *ctx, const char *args) \
  415. { \
  416. BiquadsContext *p = ctx->priv; \
  417. p->class = &name_##_class; \
  418. p->filter_type = name_; \
  419. return init(ctx, args); \
  420. } \
  421. \
  422. AVFilter avfilter_af_##name_ = { \
  423. .name = #name_, \
  424. .description = NULL_IF_CONFIG_SMALL(description_), \
  425. .priv_size = sizeof(BiquadsContext), \
  426. .init = name_##_init, \
  427. .uninit = uninit, \
  428. .query_formats = query_formats, \
  429. .inputs = inputs, \
  430. .outputs = outputs, \
  431. .priv_class = &name_##_class, \
  432. }
  433. #if CONFIG_EQUALIZER_FILTER
  434. static const AVOption equalizer_options[] = {
  435. {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 999999, FLAGS},
  436. {"f", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 999999, FLAGS},
  437. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HZ, SLOPE, FLAGS, "width_type"},
  438. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HZ}, 0, 0, FLAGS, "width_type"},
  439. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  440. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  441. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  442. {"width", "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 999, FLAGS},
  443. {"w", "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 999, FLAGS},
  444. {"gain", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
  445. {"g", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
  446. {NULL},
  447. };
  448. DEFINE_BIQUAD_FILTER(equalizer, "Apply two-pole peaking equalization (EQ) filter.");
  449. #endif /* CONFIG_EQUALIZER_FILTER */
  450. #if CONFIG_BASS_FILTER
  451. static const AVOption bass_options[] = {
  452. {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=100}, 0, 999999, FLAGS},
  453. {"f", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=100}, 0, 999999, FLAGS},
  454. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HZ, SLOPE, FLAGS, "width_type"},
  455. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HZ}, 0, 0, FLAGS, "width_type"},
  456. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  457. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  458. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  459. {"width", "set shelf transition steep", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 99999, FLAGS},
  460. {"w", "set shelf transition steep", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 99999, FLAGS},
  461. {"gain", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
  462. {"g", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
  463. {NULL},
  464. };
  465. DEFINE_BIQUAD_FILTER(bass, "Boost or cut lower frequencies.");
  466. #endif /* CONFIG_BASS_FILTER */
  467. #if CONFIG_TREBLE_FILTER
  468. static const AVOption treble_options[] = {
  469. {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  470. {"f", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  471. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HZ, SLOPE, FLAGS, "width_type"},
  472. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HZ}, 0, 0, FLAGS, "width_type"},
  473. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  474. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  475. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  476. {"width", "set shelf transition steep", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 99999, FLAGS},
  477. {"w", "set shelf transition steep", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 99999, FLAGS},
  478. {"gain", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
  479. {"g", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
  480. {NULL},
  481. };
  482. DEFINE_BIQUAD_FILTER(treble, "Boost or cut upper frequencies.");
  483. #endif /* CONFIG_TREBLE_FILTER */
  484. #if CONFIG_BANDPASS_FILTER
  485. static const AVOption bandpass_options[] = {
  486. {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  487. {"f", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  488. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HZ, SLOPE, FLAGS, "width_type"},
  489. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HZ}, 0, 0, FLAGS, "width_type"},
  490. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  491. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  492. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  493. {"width", "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 999, FLAGS},
  494. {"w", "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 999, FLAGS},
  495. {"csg", "use constant skirt gain", OFFSET(csg), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
  496. {NULL},
  497. };
  498. DEFINE_BIQUAD_FILTER(bandpass, "Apply a two-pole Butterworth band-pass filter.");
  499. #endif /* CONFIG_BANDPASS_FILTER */
  500. #if CONFIG_BANDREJECT_FILTER
  501. static const AVOption bandreject_options[] = {
  502. {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  503. {"f", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  504. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HZ, SLOPE, FLAGS, "width_type"},
  505. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HZ}, 0, 0, FLAGS, "width_type"},
  506. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  507. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  508. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  509. {"width", "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 999, FLAGS},
  510. {"w", "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 999, FLAGS},
  511. {NULL},
  512. };
  513. DEFINE_BIQUAD_FILTER(bandreject, "Apply a two-pole Butterworth band-reject filter.");
  514. #endif /* CONFIG_BANDREJECT_FILTER */
  515. #if CONFIG_LOWPASS_FILTER
  516. static const AVOption lowpass_options[] = {
  517. {"frequency", "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=500}, 0, 999999, FLAGS},
  518. {"f", "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=500}, 0, 999999, FLAGS},
  519. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HZ, SLOPE, FLAGS, "width_type"},
  520. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HZ}, 0, 0, FLAGS, "width_type"},
  521. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  522. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  523. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  524. {"width", "set width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.707}, 0, 99999, FLAGS},
  525. {"w", "set width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.707}, 0, 99999, FLAGS},
  526. {"poles", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, FLAGS},
  527. {"p", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, FLAGS},
  528. {NULL},
  529. };
  530. DEFINE_BIQUAD_FILTER(lowpass, "Apply a low-pass filter with 3dB point frequency.");
  531. #endif /* CONFIG_LOWPASS_FILTER */
  532. #if CONFIG_HIGHPASS_FILTER
  533. static const AVOption highpass_options[] = {
  534. {"frequency", "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  535. {"f", "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  536. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HZ, SLOPE, FLAGS, "width_type"},
  537. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HZ}, 0, 0, FLAGS, "width_type"},
  538. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  539. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  540. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  541. {"width", "set width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.707}, 0, 99999, FLAGS},
  542. {"w", "set width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.707}, 0, 99999, FLAGS},
  543. {"poles", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, FLAGS},
  544. {"p", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, FLAGS},
  545. {NULL},
  546. };
  547. DEFINE_BIQUAD_FILTER(highpass, "Apply a high-pass filter with 3dB point frequency.");
  548. #endif /* CONFIG_HIGHPASS_FILTER */
  549. #if CONFIG_ALLPASS_FILTER
  550. static const AVOption allpass_options[] = {
  551. {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  552. {"f", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  553. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=HZ}, HZ, SLOPE, FLAGS, "width_type"},
  554. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HZ}, 0, 0, FLAGS, "width_type"},
  555. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  556. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  557. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  558. {"width", "set filter-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=707.1}, 0, 99999, FLAGS},
  559. {"w", "set filter-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=707.1}, 0, 99999, FLAGS},
  560. {NULL},
  561. };
  562. DEFINE_BIQUAD_FILTER(allpass, "Apply a two-pole all-pass filter.");
  563. #endif /* CONFIG_ALLPASS_FILTER */
  564. #if CONFIG_BIQUAD_FILTER
  565. static const AVOption biquad_options[] = {
  566. {"a0", NULL, OFFSET(a0), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MAX, INT16_MAX, FLAGS},
  567. {"a1", NULL, OFFSET(a1), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MAX, INT16_MAX, FLAGS},
  568. {"a2", NULL, OFFSET(a2), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MAX, INT16_MAX, FLAGS},
  569. {"b0", NULL, OFFSET(b0), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MAX, INT16_MAX, FLAGS},
  570. {"b1", NULL, OFFSET(b1), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MAX, INT16_MAX, FLAGS},
  571. {"b2", NULL, OFFSET(b2), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MAX, INT16_MAX, FLAGS},
  572. {NULL},
  573. };
  574. DEFINE_BIQUAD_FILTER(biquad, "Apply a biquad IIR filter with the given coefficients.");
  575. #endif /* CONFIG_BIQUAD_FILTER */