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.

600 lines
24KB

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