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.

669 lines
31KB

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