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