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.

624 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)(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_layouts();
  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 (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 (need_clipping && o2 < min) { \
  169. av_log(NULL, AV_LOG_WARNING, "clipping\n"); \
  170. obuf[i] = min; \
  171. } else if (need_clipping && 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 (need_clipping && o1 < min) { \
  181. av_log(NULL, AV_LOG_WARNING, "clipping\n"); \
  182. obuf[i] = min; \
  183. } else if (need_clipping && 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 (need_clipping && o0 < min) { \
  197. av_log(NULL, AV_LOG_WARNING, "clipping\n"); \
  198. obuf[i] = min; \
  199. } else if (need_clipping && 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, 1)
  212. BIQUAD_FILTER(s32, int32_t, INT32_MIN, INT32_MAX, 1)
  213. BIQUAD_FILTER(flt, float, -1., 1., 0)
  214. BIQUAD_FILTER(dbl, double, -1., 1., 0)
  215. static int config_output(AVFilterLink *outlink)
  216. {
  217. AVFilterContext *ctx = outlink->src;
  218. BiquadsContext *s = ctx->priv;
  219. AVFilterLink *inlink = ctx->inputs[0];
  220. double A = exp(s->gain / 40 * log(10.));
  221. double w0 = 2 * M_PI * s->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. s->frequency, inlink->sample_rate);
  227. return AVERROR(EINVAL);
  228. }
  229. switch (s->width_type) {
  230. case NONE:
  231. alpha = 0.0;
  232. break;
  233. case HERTZ:
  234. alpha = sin(w0) / (2 * s->frequency / s->width);
  235. break;
  236. case OCTAVE:
  237. alpha = sin(w0) * sinh(log(2.) / 2 * s->width * w0 / sin(w0));
  238. break;
  239. case QFACTOR:
  240. alpha = sin(w0) / (2 * s->width);
  241. break;
  242. case SLOPE:
  243. alpha = sin(w0) / 2 * sqrt((A + 1 / A) * (1 / s->width - 1) + 2);
  244. break;
  245. default:
  246. av_assert0(0);
  247. }
  248. switch (s->filter_type) {
  249. case biquad:
  250. break;
  251. case equalizer:
  252. s->a0 = 1 + alpha / A;
  253. s->a1 = -2 * cos(w0);
  254. s->a2 = 1 - alpha / A;
  255. s->b0 = 1 + alpha * A;
  256. s->b1 = -2 * cos(w0);
  257. s->b2 = 1 - alpha * A;
  258. break;
  259. case bass:
  260. s->a0 = (A + 1) + (A - 1) * cos(w0) + 2 * sqrt(A) * alpha;
  261. s->a1 = -2 * ((A - 1) + (A + 1) * cos(w0));
  262. s->a2 = (A + 1) + (A - 1) * cos(w0) - 2 * sqrt(A) * alpha;
  263. s->b0 = A * ((A + 1) - (A - 1) * cos(w0) + 2 * sqrt(A) * alpha);
  264. s->b1 = 2 * A * ((A - 1) - (A + 1) * cos(w0));
  265. s->b2 = A * ((A + 1) - (A - 1) * cos(w0) - 2 * sqrt(A) * alpha);
  266. break;
  267. case treble:
  268. s->a0 = (A + 1) - (A - 1) * cos(w0) + 2 * sqrt(A) * alpha;
  269. s->a1 = 2 * ((A - 1) - (A + 1) * cos(w0));
  270. s->a2 = (A + 1) - (A - 1) * cos(w0) - 2 * sqrt(A) * alpha;
  271. s->b0 = A * ((A + 1) + (A - 1) * cos(w0) + 2 * sqrt(A) * alpha);
  272. s->b1 =-2 * A * ((A - 1) + (A + 1) * cos(w0));
  273. s->b2 = A * ((A + 1) + (A - 1) * cos(w0) - 2 * sqrt(A) * alpha);
  274. break;
  275. case bandpass:
  276. if (s->csg) {
  277. s->a0 = 1 + alpha;
  278. s->a1 = -2 * cos(w0);
  279. s->a2 = 1 - alpha;
  280. s->b0 = sin(w0) / 2;
  281. s->b1 = 0;
  282. s->b2 = -sin(w0) / 2;
  283. } else {
  284. s->a0 = 1 + alpha;
  285. s->a1 = -2 * cos(w0);
  286. s->a2 = 1 - alpha;
  287. s->b0 = alpha;
  288. s->b1 = 0;
  289. s->b2 = -alpha;
  290. }
  291. break;
  292. case bandreject:
  293. s->a0 = 1 + alpha;
  294. s->a1 = -2 * cos(w0);
  295. s->a2 = 1 - alpha;
  296. s->b0 = 1;
  297. s->b1 = -2 * cos(w0);
  298. s->b2 = 1;
  299. break;
  300. case lowpass:
  301. if (s->poles == 1) {
  302. s->a0 = 1;
  303. s->a1 = -exp(-w0);
  304. s->a2 = 0;
  305. s->b0 = 1 + s->a1;
  306. s->b1 = 0;
  307. s->b2 = 0;
  308. } else {
  309. s->a0 = 1 + alpha;
  310. s->a1 = -2 * cos(w0);
  311. s->a2 = 1 - alpha;
  312. s->b0 = (1 - cos(w0)) / 2;
  313. s->b1 = 1 - cos(w0);
  314. s->b2 = (1 - cos(w0)) / 2;
  315. }
  316. break;
  317. case highpass:
  318. if (s->poles == 1) {
  319. s->a0 = 1;
  320. s->a1 = -exp(-w0);
  321. s->a2 = 0;
  322. s->b0 = (1 - s->a1) / 2;
  323. s->b1 = -s->b0;
  324. s->b2 = 0;
  325. } else {
  326. s->a0 = 1 + alpha;
  327. s->a1 = -2 * cos(w0);
  328. s->a2 = 1 - alpha;
  329. s->b0 = (1 + cos(w0)) / 2;
  330. s->b1 = -(1 + cos(w0));
  331. s->b2 = (1 + cos(w0)) / 2;
  332. }
  333. break;
  334. case allpass:
  335. s->a0 = 1 + alpha;
  336. s->a1 = -2 * cos(w0);
  337. s->a2 = 1 - alpha;
  338. s->b0 = 1 - alpha;
  339. s->b1 = -2 * cos(w0);
  340. s->b2 = 1 + alpha;
  341. break;
  342. default:
  343. av_assert0(0);
  344. }
  345. s->a1 /= s->a0;
  346. s->a2 /= s->a0;
  347. s->b0 /= s->a0;
  348. s->b1 /= s->a0;
  349. s->b2 /= s->a0;
  350. s->cache = av_realloc_f(s->cache, sizeof(ChanCache), inlink->channels);
  351. if (!s->cache)
  352. return AVERROR(ENOMEM);
  353. memset(s->cache, 0, sizeof(ChanCache) * inlink->channels);
  354. switch (inlink->format) {
  355. case AV_SAMPLE_FMT_S16P: s->filter = biquad_s16; break;
  356. case AV_SAMPLE_FMT_S32P: s->filter = biquad_s32; break;
  357. case AV_SAMPLE_FMT_FLTP: s->filter = biquad_flt; break;
  358. case AV_SAMPLE_FMT_DBLP: s->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 *s = 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. av_frame_copy_props(out_buf, buf);
  377. }
  378. for (ch = 0; ch < av_frame_get_channels(buf); ch++)
  379. s->filter(buf->extended_data[ch],
  380. out_buf->extended_data[ch], nb_samples,
  381. &s->cache[ch].i1, &s->cache[ch].i2,
  382. &s->cache[ch].o1, &s->cache[ch].o2,
  383. s->b0, s->b1, s->b2, s->a1, s->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 *s = ctx->priv;
  391. av_freep(&s->cache);
  392. }
  393. static const AVFilterPad inputs[] = {
  394. {
  395. .name = "default",
  396. .type = AVMEDIA_TYPE_AUDIO,
  397. .filter_frame = filter_frame,
  398. },
  399. { NULL }
  400. };
  401. static const AVFilterPad outputs[] = {
  402. {
  403. .name = "default",
  404. .type = AVMEDIA_TYPE_AUDIO,
  405. .config_props = config_output,
  406. },
  407. { NULL }
  408. };
  409. #define OFFSET(x) offsetof(BiquadsContext, x)
  410. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  411. #define DEFINE_BIQUAD_FILTER(name_, description_) \
  412. AVFILTER_DEFINE_CLASS(name_); \
  413. static av_cold int name_##_init(AVFilterContext *ctx) \
  414. { \
  415. BiquadsContext *s = ctx->priv; \
  416. s->class = &name_##_class; \
  417. s->filter_type = name_; \
  418. return init(ctx); \
  419. } \
  420. \
  421. AVFilter ff_af_##name_ = { \
  422. .name = #name_, \
  423. .description = NULL_IF_CONFIG_SMALL(description_), \
  424. .priv_size = sizeof(BiquadsContext), \
  425. .init = name_##_init, \
  426. .uninit = uninit, \
  427. .query_formats = query_formats, \
  428. .inputs = inputs, \
  429. .outputs = outputs, \
  430. .priv_class = &name_##_class, \
  431. }
  432. #if CONFIG_EQUALIZER_FILTER
  433. static const AVOption equalizer_options[] = {
  434. {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 999999, FLAGS},
  435. {"f", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 999999, FLAGS},
  436. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
  437. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
  438. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  439. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  440. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  441. {"width", "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 999, FLAGS},
  442. {"w", "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 999, FLAGS},
  443. {"gain", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
  444. {"g", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
  445. {NULL}
  446. };
  447. DEFINE_BIQUAD_FILTER(equalizer, "Apply two-pole peaking equalization (EQ) filter.");
  448. #endif /* CONFIG_EQUALIZER_FILTER */
  449. #if CONFIG_BASS_FILTER
  450. static const AVOption bass_options[] = {
  451. {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=100}, 0, 999999, FLAGS},
  452. {"f", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=100}, 0, 999999, FLAGS},
  453. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
  454. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
  455. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  456. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  457. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  458. {"width", "set shelf transition steep", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 99999, FLAGS},
  459. {"w", "set shelf transition steep", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 99999, FLAGS},
  460. {"gain", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
  461. {"g", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
  462. {NULL}
  463. };
  464. DEFINE_BIQUAD_FILTER(bass, "Boost or cut lower frequencies.");
  465. #endif /* CONFIG_BASS_FILTER */
  466. #if CONFIG_TREBLE_FILTER
  467. static const AVOption treble_options[] = {
  468. {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  469. {"f", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  470. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
  471. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
  472. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  473. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  474. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  475. {"width", "set shelf transition steep", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 99999, FLAGS},
  476. {"w", "set shelf transition steep", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 99999, FLAGS},
  477. {"gain", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
  478. {"g", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
  479. {NULL}
  480. };
  481. DEFINE_BIQUAD_FILTER(treble, "Boost or cut upper frequencies.");
  482. #endif /* CONFIG_TREBLE_FILTER */
  483. #if CONFIG_BANDPASS_FILTER
  484. static const AVOption bandpass_options[] = {
  485. {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  486. {"f", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  487. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
  488. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
  489. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  490. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  491. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  492. {"width", "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 999, FLAGS},
  493. {"w", "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 999, FLAGS},
  494. {"csg", "use constant skirt gain", OFFSET(csg), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
  495. {NULL}
  496. };
  497. DEFINE_BIQUAD_FILTER(bandpass, "Apply a two-pole Butterworth band-pass filter.");
  498. #endif /* CONFIG_BANDPASS_FILTER */
  499. #if CONFIG_BANDREJECT_FILTER
  500. static const AVOption bandreject_options[] = {
  501. {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  502. {"f", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  503. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
  504. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
  505. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  506. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  507. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  508. {"width", "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 999, FLAGS},
  509. {"w", "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 999, FLAGS},
  510. {NULL}
  511. };
  512. DEFINE_BIQUAD_FILTER(bandreject, "Apply a two-pole Butterworth band-reject filter.");
  513. #endif /* CONFIG_BANDREJECT_FILTER */
  514. #if CONFIG_LOWPASS_FILTER
  515. static const AVOption lowpass_options[] = {
  516. {"frequency", "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=500}, 0, 999999, FLAGS},
  517. {"f", "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=500}, 0, 999999, FLAGS},
  518. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
  519. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
  520. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  521. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  522. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  523. {"width", "set width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.707}, 0, 99999, FLAGS},
  524. {"w", "set width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.707}, 0, 99999, FLAGS},
  525. {"poles", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, FLAGS},
  526. {"p", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, FLAGS},
  527. {NULL}
  528. };
  529. DEFINE_BIQUAD_FILTER(lowpass, "Apply a low-pass filter with 3dB point frequency.");
  530. #endif /* CONFIG_LOWPASS_FILTER */
  531. #if CONFIG_HIGHPASS_FILTER
  532. static const AVOption highpass_options[] = {
  533. {"frequency", "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  534. {"f", "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  535. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
  536. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
  537. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  538. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  539. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  540. {"width", "set width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.707}, 0, 99999, FLAGS},
  541. {"w", "set width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.707}, 0, 99999, FLAGS},
  542. {"poles", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, FLAGS},
  543. {"p", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, FLAGS},
  544. {NULL}
  545. };
  546. DEFINE_BIQUAD_FILTER(highpass, "Apply a high-pass filter with 3dB point frequency.");
  547. #endif /* CONFIG_HIGHPASS_FILTER */
  548. #if CONFIG_ALLPASS_FILTER
  549. static const AVOption allpass_options[] = {
  550. {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  551. {"f", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  552. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=HERTZ}, HERTZ, SLOPE, FLAGS, "width_type"},
  553. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
  554. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  555. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  556. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  557. {"width", "set filter-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=707.1}, 0, 99999, FLAGS},
  558. {"w", "set filter-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=707.1}, 0, 99999, FLAGS},
  559. {NULL}
  560. };
  561. DEFINE_BIQUAD_FILTER(allpass, "Apply a two-pole all-pass filter.");
  562. #endif /* CONFIG_ALLPASS_FILTER */
  563. #if CONFIG_BIQUAD_FILTER
  564. static const AVOption biquad_options[] = {
  565. {"a0", NULL, OFFSET(a0), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MIN, INT16_MAX, FLAGS},
  566. {"a1", NULL, OFFSET(a1), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MIN, INT16_MAX, FLAGS},
  567. {"a2", NULL, OFFSET(a2), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MIN, INT16_MAX, FLAGS},
  568. {"b0", NULL, OFFSET(b0), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MIN, INT16_MAX, FLAGS},
  569. {"b1", NULL, OFFSET(b1), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MIN, INT16_MAX, FLAGS},
  570. {"b2", NULL, OFFSET(b2), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MIN, INT16_MAX, FLAGS},
  571. {NULL}
  572. };
  573. DEFINE_BIQUAD_FILTER(biquad, "Apply a biquad IIR filter with the given coefficients.");
  574. #endif /* CONFIG_BIQUAD_FILTER */