af_biquads.c 26 KB

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