af_biquads.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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/avassert.h"
  64. #include "libavutil/opt.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. HERTZ,
  83. OCTAVE,
  84. QFACTOR,
  85. SLOPE,
  86. };
  87. typedef struct ChanCache {
  88. double i1, i2;
  89. double o1, o2;
  90. } ChanCache;
  91. typedef struct BiquadsContext {
  92. const AVClass *class;
  93. enum FilterType filter_type;
  94. int 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. int clippings;
  104. void (*filter)(struct BiquadsContext *s, const void *ibuf, void *obuf, int len,
  105. double *i1, double *i2, double *o1, double *o2,
  106. double b0, double b1, double b2, double a1, double a2);
  107. } BiquadsContext;
  108. static av_cold int init(AVFilterContext *ctx)
  109. {
  110. BiquadsContext *s = ctx->priv;
  111. if (s->filter_type != biquad) {
  112. if (s->frequency <= 0 || s->width <= 0) {
  113. av_log(ctx, AV_LOG_ERROR, "Invalid frequency %f and/or width %f <= 0\n",
  114. s->frequency, s->width);
  115. return AVERROR(EINVAL);
  116. }
  117. }
  118. return 0;
  119. }
  120. static int query_formats(AVFilterContext *ctx)
  121. {
  122. AVFilterFormats *formats;
  123. AVFilterChannelLayouts *layouts;
  124. static const enum AVSampleFormat sample_fmts[] = {
  125. AV_SAMPLE_FMT_S16P,
  126. AV_SAMPLE_FMT_S32P,
  127. AV_SAMPLE_FMT_FLTP,
  128. AV_SAMPLE_FMT_DBLP,
  129. AV_SAMPLE_FMT_NONE
  130. };
  131. int ret;
  132. layouts = ff_all_channel_counts();
  133. if (!layouts)
  134. return AVERROR(ENOMEM);
  135. ret = ff_set_common_channel_layouts(ctx, layouts);
  136. if (ret < 0)
  137. return ret;
  138. formats = ff_make_format_list(sample_fmts);
  139. if (!formats)
  140. return AVERROR(ENOMEM);
  141. ret = ff_set_common_formats(ctx, formats);
  142. if (ret < 0)
  143. return ret;
  144. formats = ff_all_samplerates();
  145. if (!formats)
  146. return AVERROR(ENOMEM);
  147. return ff_set_common_samplerates(ctx, formats);
  148. }
  149. #define BIQUAD_FILTER(name, type, min, max, need_clipping) \
  150. static void biquad_## name (BiquadsContext *s, \
  151. const void *input, void *output, int len, \
  152. double *in1, double *in2, \
  153. double *out1, double *out2, \
  154. double b0, double b1, double b2, \
  155. double a1, double a2) \
  156. { \
  157. const type *ibuf = input; \
  158. type *obuf = output; \
  159. double i1 = *in1; \
  160. double i2 = *in2; \
  161. double o1 = *out1; \
  162. double o2 = *out2; \
  163. int i; \
  164. a1 = -a1; \
  165. a2 = -a2; \
  166. \
  167. for (i = 0; i+1 < len; i++) { \
  168. o2 = i2 * b2 + i1 * b1 + ibuf[i] * b0 + o2 * a2 + o1 * a1; \
  169. i2 = ibuf[i]; \
  170. if (need_clipping && o2 < min) { \
  171. s->clippings++; \
  172. obuf[i] = min; \
  173. } else if (need_clipping && o2 > max) { \
  174. s->clippings++; \
  175. obuf[i] = max; \
  176. } else { \
  177. obuf[i] = o2; \
  178. } \
  179. i++; \
  180. o1 = i1 * b2 + i2 * b1 + ibuf[i] * b0 + o1 * a2 + o2 * a1; \
  181. i1 = ibuf[i]; \
  182. if (need_clipping && o1 < min) { \
  183. s->clippings++; \
  184. obuf[i] = min; \
  185. } else if (need_clipping && o1 > max) { \
  186. s->clippings++; \
  187. obuf[i] = max; \
  188. } else { \
  189. obuf[i] = o1; \
  190. } \
  191. } \
  192. if (i < len) { \
  193. double o0 = ibuf[i] * b0 + i1 * b1 + i2 * b2 + o1 * a1 + o2 * a2; \
  194. i2 = i1; \
  195. i1 = ibuf[i]; \
  196. o2 = o1; \
  197. o1 = o0; \
  198. if (need_clipping && o0 < min) { \
  199. s->clippings++; \
  200. obuf[i] = min; \
  201. } else if (need_clipping && o0 > max) { \
  202. s->clippings++; \
  203. obuf[i] = max; \
  204. } else { \
  205. obuf[i] = o0; \
  206. } \
  207. } \
  208. *in1 = i1; \
  209. *in2 = i2; \
  210. *out1 = o1; \
  211. *out2 = o2; \
  212. }
  213. BIQUAD_FILTER(s16, int16_t, INT16_MIN, INT16_MAX, 1)
  214. BIQUAD_FILTER(s32, int32_t, INT32_MIN, INT32_MAX, 1)
  215. BIQUAD_FILTER(flt, float, -1., 1., 0)
  216. BIQUAD_FILTER(dbl, double, -1., 1., 0)
  217. static int config_output(AVFilterLink *outlink)
  218. {
  219. AVFilterContext *ctx = outlink->src;
  220. BiquadsContext *s = ctx->priv;
  221. AVFilterLink *inlink = ctx->inputs[0];
  222. double A = exp(s->gain / 40 * log(10.));
  223. double w0 = 2 * M_PI * s->frequency / inlink->sample_rate;
  224. double alpha;
  225. if (w0 > M_PI) {
  226. av_log(ctx, AV_LOG_ERROR,
  227. "Invalid frequency %f. Frequency must be less than half the sample-rate %d.\n",
  228. s->frequency, inlink->sample_rate);
  229. return AVERROR(EINVAL);
  230. }
  231. switch (s->width_type) {
  232. case NONE:
  233. alpha = 0.0;
  234. break;
  235. case HERTZ:
  236. alpha = sin(w0) / (2 * s->frequency / s->width);
  237. break;
  238. case OCTAVE:
  239. alpha = sin(w0) * sinh(log(2.) / 2 * s->width * w0 / sin(w0));
  240. break;
  241. case QFACTOR:
  242. alpha = sin(w0) / (2 * s->width);
  243. break;
  244. case SLOPE:
  245. alpha = sin(w0) / 2 * sqrt((A + 1 / A) * (1 / s->width - 1) + 2);
  246. break;
  247. default:
  248. av_assert0(0);
  249. }
  250. switch (s->filter_type) {
  251. case biquad:
  252. break;
  253. case equalizer:
  254. s->a0 = 1 + alpha / A;
  255. s->a1 = -2 * cos(w0);
  256. s->a2 = 1 - alpha / A;
  257. s->b0 = 1 + alpha * A;
  258. s->b1 = -2 * cos(w0);
  259. s->b2 = 1 - alpha * A;
  260. break;
  261. case bass:
  262. s->a0 = (A + 1) + (A - 1) * cos(w0) + 2 * sqrt(A) * alpha;
  263. s->a1 = -2 * ((A - 1) + (A + 1) * cos(w0));
  264. s->a2 = (A + 1) + (A - 1) * cos(w0) - 2 * sqrt(A) * alpha;
  265. s->b0 = A * ((A + 1) - (A - 1) * cos(w0) + 2 * sqrt(A) * alpha);
  266. s->b1 = 2 * A * ((A - 1) - (A + 1) * cos(w0));
  267. s->b2 = A * ((A + 1) - (A - 1) * cos(w0) - 2 * sqrt(A) * alpha);
  268. break;
  269. case treble:
  270. s->a0 = (A + 1) - (A - 1) * cos(w0) + 2 * sqrt(A) * alpha;
  271. s->a1 = 2 * ((A - 1) - (A + 1) * cos(w0));
  272. s->a2 = (A + 1) - (A - 1) * cos(w0) - 2 * sqrt(A) * alpha;
  273. s->b0 = A * ((A + 1) + (A - 1) * cos(w0) + 2 * sqrt(A) * alpha);
  274. s->b1 =-2 * A * ((A - 1) + (A + 1) * cos(w0));
  275. s->b2 = A * ((A + 1) + (A - 1) * cos(w0) - 2 * sqrt(A) * alpha);
  276. break;
  277. case bandpass:
  278. if (s->csg) {
  279. s->a0 = 1 + alpha;
  280. s->a1 = -2 * cos(w0);
  281. s->a2 = 1 - alpha;
  282. s->b0 = sin(w0) / 2;
  283. s->b1 = 0;
  284. s->b2 = -sin(w0) / 2;
  285. } else {
  286. s->a0 = 1 + alpha;
  287. s->a1 = -2 * cos(w0);
  288. s->a2 = 1 - alpha;
  289. s->b0 = alpha;
  290. s->b1 = 0;
  291. s->b2 = -alpha;
  292. }
  293. break;
  294. case bandreject:
  295. s->a0 = 1 + alpha;
  296. s->a1 = -2 * cos(w0);
  297. s->a2 = 1 - alpha;
  298. s->b0 = 1;
  299. s->b1 = -2 * cos(w0);
  300. s->b2 = 1;
  301. break;
  302. case lowpass:
  303. if (s->poles == 1) {
  304. s->a0 = 1;
  305. s->a1 = -exp(-w0);
  306. s->a2 = 0;
  307. s->b0 = 1 + s->a1;
  308. s->b1 = 0;
  309. s->b2 = 0;
  310. } else {
  311. s->a0 = 1 + alpha;
  312. s->a1 = -2 * cos(w0);
  313. s->a2 = 1 - alpha;
  314. s->b0 = (1 - cos(w0)) / 2;
  315. s->b1 = 1 - cos(w0);
  316. s->b2 = (1 - cos(w0)) / 2;
  317. }
  318. break;
  319. case highpass:
  320. if (s->poles == 1) {
  321. s->a0 = 1;
  322. s->a1 = -exp(-w0);
  323. s->a2 = 0;
  324. s->b0 = (1 - s->a1) / 2;
  325. s->b1 = -s->b0;
  326. s->b2 = 0;
  327. } else {
  328. s->a0 = 1 + alpha;
  329. s->a1 = -2 * cos(w0);
  330. s->a2 = 1 - alpha;
  331. s->b0 = (1 + cos(w0)) / 2;
  332. s->b1 = -(1 + cos(w0));
  333. s->b2 = (1 + cos(w0)) / 2;
  334. }
  335. break;
  336. case allpass:
  337. s->a0 = 1 + alpha;
  338. s->a1 = -2 * cos(w0);
  339. s->a2 = 1 - alpha;
  340. s->b0 = 1 - alpha;
  341. s->b1 = -2 * cos(w0);
  342. s->b2 = 1 + alpha;
  343. break;
  344. default:
  345. av_assert0(0);
  346. }
  347. s->a1 /= s->a0;
  348. s->a2 /= s->a0;
  349. s->b0 /= s->a0;
  350. s->b1 /= s->a0;
  351. s->b2 /= s->a0;
  352. s->cache = av_realloc_f(s->cache, sizeof(ChanCache), inlink->channels);
  353. if (!s->cache)
  354. return AVERROR(ENOMEM);
  355. memset(s->cache, 0, sizeof(ChanCache) * inlink->channels);
  356. switch (inlink->format) {
  357. case AV_SAMPLE_FMT_S16P: s->filter = biquad_s16; break;
  358. case AV_SAMPLE_FMT_S32P: s->filter = biquad_s32; break;
  359. case AV_SAMPLE_FMT_FLTP: s->filter = biquad_flt; break;
  360. case AV_SAMPLE_FMT_DBLP: s->filter = biquad_dbl; break;
  361. default: av_assert0(0);
  362. }
  363. return 0;
  364. }
  365. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  366. {
  367. AVFilterContext *ctx = inlink->dst;
  368. BiquadsContext *s = ctx->priv;
  369. AVFilterLink *outlink = ctx->outputs[0];
  370. AVFrame *out_buf;
  371. int nb_samples = buf->nb_samples;
  372. int ch;
  373. if (av_frame_is_writable(buf)) {
  374. out_buf = buf;
  375. } else {
  376. out_buf = ff_get_audio_buffer(inlink, nb_samples);
  377. if (!out_buf) {
  378. av_frame_free(&buf);
  379. return AVERROR(ENOMEM);
  380. }
  381. av_frame_copy_props(out_buf, buf);
  382. }
  383. for (ch = 0; ch < av_frame_get_channels(buf); ch++)
  384. s->filter(s, buf->extended_data[ch],
  385. out_buf->extended_data[ch], nb_samples,
  386. &s->cache[ch].i1, &s->cache[ch].i2,
  387. &s->cache[ch].o1, &s->cache[ch].o2,
  388. s->b0, s->b1, s->b2, s->a1, s->a2);
  389. if (s->clippings > 0)
  390. av_log(ctx, AV_LOG_WARNING, "clipping %d times. Please reduce gain.\n", s->clippings);
  391. if (buf != out_buf)
  392. av_frame_free(&buf);
  393. return ff_filter_frame(outlink, out_buf);
  394. }
  395. static av_cold void uninit(AVFilterContext *ctx)
  396. {
  397. BiquadsContext *s = ctx->priv;
  398. av_freep(&s->cache);
  399. }
  400. static const AVFilterPad inputs[] = {
  401. {
  402. .name = "default",
  403. .type = AVMEDIA_TYPE_AUDIO,
  404. .filter_frame = filter_frame,
  405. },
  406. { NULL }
  407. };
  408. static const AVFilterPad outputs[] = {
  409. {
  410. .name = "default",
  411. .type = AVMEDIA_TYPE_AUDIO,
  412. .config_props = config_output,
  413. },
  414. { NULL }
  415. };
  416. #define OFFSET(x) offsetof(BiquadsContext, x)
  417. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  418. #define DEFINE_BIQUAD_FILTER(name_, description_) \
  419. AVFILTER_DEFINE_CLASS(name_); \
  420. static av_cold int name_##_init(AVFilterContext *ctx) \
  421. { \
  422. BiquadsContext *s = ctx->priv; \
  423. s->class = &name_##_class; \
  424. s->filter_type = name_; \
  425. return init(ctx); \
  426. } \
  427. \
  428. AVFilter ff_af_##name_ = { \
  429. .name = #name_, \
  430. .description = NULL_IF_CONFIG_SMALL(description_), \
  431. .priv_size = sizeof(BiquadsContext), \
  432. .init = name_##_init, \
  433. .uninit = uninit, \
  434. .query_formats = query_formats, \
  435. .inputs = inputs, \
  436. .outputs = outputs, \
  437. .priv_class = &name_##_class, \
  438. }
  439. #if CONFIG_EQUALIZER_FILTER
  440. static const AVOption equalizer_options[] = {
  441. {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 999999, FLAGS},
  442. {"f", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 999999, FLAGS},
  443. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
  444. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
  445. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  446. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  447. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  448. {"width", "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 999, FLAGS},
  449. {"w", "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 999, FLAGS},
  450. {"gain", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
  451. {"g", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
  452. {NULL}
  453. };
  454. DEFINE_BIQUAD_FILTER(equalizer, "Apply two-pole peaking equalization (EQ) filter.");
  455. #endif /* CONFIG_EQUALIZER_FILTER */
  456. #if CONFIG_BASS_FILTER
  457. static const AVOption bass_options[] = {
  458. {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=100}, 0, 999999, FLAGS},
  459. {"f", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=100}, 0, 999999, FLAGS},
  460. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
  461. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
  462. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  463. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  464. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  465. {"width", "set shelf transition steep", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 99999, FLAGS},
  466. {"w", "set shelf transition steep", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 99999, FLAGS},
  467. {"gain", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
  468. {"g", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
  469. {NULL}
  470. };
  471. DEFINE_BIQUAD_FILTER(bass, "Boost or cut lower frequencies.");
  472. #endif /* CONFIG_BASS_FILTER */
  473. #if CONFIG_TREBLE_FILTER
  474. static const AVOption treble_options[] = {
  475. {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  476. {"f", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  477. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
  478. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
  479. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  480. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  481. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  482. {"width", "set shelf transition steep", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 99999, FLAGS},
  483. {"w", "set shelf transition steep", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 99999, FLAGS},
  484. {"gain", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
  485. {"g", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
  486. {NULL}
  487. };
  488. DEFINE_BIQUAD_FILTER(treble, "Boost or cut upper frequencies.");
  489. #endif /* CONFIG_TREBLE_FILTER */
  490. #if CONFIG_BANDPASS_FILTER
  491. static const AVOption bandpass_options[] = {
  492. {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  493. {"f", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  494. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
  495. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
  496. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  497. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  498. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  499. {"width", "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 999, FLAGS},
  500. {"w", "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 999, FLAGS},
  501. {"csg", "use constant skirt gain", OFFSET(csg), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
  502. {NULL}
  503. };
  504. DEFINE_BIQUAD_FILTER(bandpass, "Apply a two-pole Butterworth band-pass filter.");
  505. #endif /* CONFIG_BANDPASS_FILTER */
  506. #if CONFIG_BANDREJECT_FILTER
  507. static const AVOption bandreject_options[] = {
  508. {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  509. {"f", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  510. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
  511. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
  512. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  513. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  514. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  515. {"width", "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 999, FLAGS},
  516. {"w", "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 999, FLAGS},
  517. {NULL}
  518. };
  519. DEFINE_BIQUAD_FILTER(bandreject, "Apply a two-pole Butterworth band-reject filter.");
  520. #endif /* CONFIG_BANDREJECT_FILTER */
  521. #if CONFIG_LOWPASS_FILTER
  522. static const AVOption lowpass_options[] = {
  523. {"frequency", "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=500}, 0, 999999, FLAGS},
  524. {"f", "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=500}, 0, 999999, FLAGS},
  525. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
  526. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
  527. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  528. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  529. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  530. {"width", "set width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.707}, 0, 99999, FLAGS},
  531. {"w", "set width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.707}, 0, 99999, FLAGS},
  532. {"poles", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, FLAGS},
  533. {"p", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, FLAGS},
  534. {NULL}
  535. };
  536. DEFINE_BIQUAD_FILTER(lowpass, "Apply a low-pass filter with 3dB point frequency.");
  537. #endif /* CONFIG_LOWPASS_FILTER */
  538. #if CONFIG_HIGHPASS_FILTER
  539. static const AVOption highpass_options[] = {
  540. {"frequency", "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  541. {"f", "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  542. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
  543. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
  544. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  545. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  546. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  547. {"width", "set width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.707}, 0, 99999, FLAGS},
  548. {"w", "set width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.707}, 0, 99999, FLAGS},
  549. {"poles", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, FLAGS},
  550. {"p", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, FLAGS},
  551. {NULL}
  552. };
  553. DEFINE_BIQUAD_FILTER(highpass, "Apply a high-pass filter with 3dB point frequency.");
  554. #endif /* CONFIG_HIGHPASS_FILTER */
  555. #if CONFIG_ALLPASS_FILTER
  556. static const AVOption allpass_options[] = {
  557. {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  558. {"f", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  559. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=HERTZ}, HERTZ, SLOPE, FLAGS, "width_type"},
  560. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
  561. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  562. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  563. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  564. {"width", "set filter-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=707.1}, 0, 99999, FLAGS},
  565. {"w", "set filter-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=707.1}, 0, 99999, FLAGS},
  566. {NULL}
  567. };
  568. DEFINE_BIQUAD_FILTER(allpass, "Apply a two-pole all-pass filter.");
  569. #endif /* CONFIG_ALLPASS_FILTER */
  570. #if CONFIG_BIQUAD_FILTER
  571. static const AVOption biquad_options[] = {
  572. {"a0", NULL, OFFSET(a0), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MIN, INT16_MAX, FLAGS},
  573. {"a1", NULL, OFFSET(a1), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MIN, INT16_MAX, FLAGS},
  574. {"a2", NULL, OFFSET(a2), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MIN, INT16_MAX, FLAGS},
  575. {"b0", NULL, OFFSET(b0), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MIN, INT16_MAX, FLAGS},
  576. {"b1", NULL, OFFSET(b1), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MIN, INT16_MAX, FLAGS},
  577. {"b2", NULL, OFFSET(b2), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MIN, INT16_MAX, FLAGS},
  578. {NULL}
  579. };
  580. DEFINE_BIQUAD_FILTER(biquad, "Apply a biquad IIR filter with the given coefficients.");
  581. #endif /* CONFIG_BIQUAD_FILTER */