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.

785 lines
34KB

  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. bandpass,
  74. bandreject,
  75. allpass,
  76. highpass,
  77. lowpass,
  78. };
  79. enum WidthType {
  80. NONE,
  81. HERTZ,
  82. OCTAVE,
  83. QFACTOR,
  84. SLOPE,
  85. };
  86. typedef struct ChanCache {
  87. double i1, i2;
  88. double o1, o2;
  89. } ChanCache;
  90. typedef struct BiquadsContext {
  91. const AVClass *class;
  92. enum FilterType filter_type;
  93. int width_type;
  94. int poles;
  95. int csg;
  96. double gain;
  97. double frequency;
  98. double width;
  99. uint64_t channels;
  100. double a0, a1, a2;
  101. double b0, b1, b2;
  102. ChanCache *cache;
  103. int clippings;
  104. int block_align;
  105. void (*filter)(struct BiquadsContext *s, const void *ibuf, void *obuf, int len,
  106. double *i1, double *i2, double *o1, double *o2,
  107. double b0, double b1, double b2, double a1, double a2);
  108. } BiquadsContext;
  109. static av_cold int init(AVFilterContext *ctx)
  110. {
  111. BiquadsContext *s = ctx->priv;
  112. if (s->filter_type != biquad) {
  113. if (s->frequency <= 0 || s->width <= 0) {
  114. av_log(ctx, AV_LOG_ERROR, "Invalid frequency %f and/or width %f <= 0\n",
  115. s->frequency, s->width);
  116. return AVERROR(EINVAL);
  117. }
  118. }
  119. return 0;
  120. }
  121. static int query_formats(AVFilterContext *ctx)
  122. {
  123. AVFilterFormats *formats;
  124. AVFilterChannelLayouts *layouts;
  125. static const enum AVSampleFormat sample_fmts[] = {
  126. AV_SAMPLE_FMT_S16P,
  127. AV_SAMPLE_FMT_S32P,
  128. AV_SAMPLE_FMT_FLTP,
  129. AV_SAMPLE_FMT_DBLP,
  130. AV_SAMPLE_FMT_NONE
  131. };
  132. int ret;
  133. layouts = ff_all_channel_counts();
  134. if (!layouts)
  135. return AVERROR(ENOMEM);
  136. ret = ff_set_common_channel_layouts(ctx, layouts);
  137. if (ret < 0)
  138. return ret;
  139. formats = ff_make_format_list(sample_fmts);
  140. if (!formats)
  141. return AVERROR(ENOMEM);
  142. ret = ff_set_common_formats(ctx, formats);
  143. if (ret < 0)
  144. return ret;
  145. formats = ff_all_samplerates();
  146. if (!formats)
  147. return AVERROR(ENOMEM);
  148. return ff_set_common_samplerates(ctx, formats);
  149. }
  150. #define BIQUAD_FILTER(name, type, min, max, need_clipping) \
  151. static void biquad_## name (BiquadsContext *s, \
  152. const void *input, void *output, int len, \
  153. double *in1, double *in2, \
  154. double *out1, double *out2, \
  155. double b0, double b1, double b2, \
  156. double a1, double a2) \
  157. { \
  158. const type *ibuf = input; \
  159. type *obuf = output; \
  160. double i1 = *in1; \
  161. double i2 = *in2; \
  162. double o1 = *out1; \
  163. double o2 = *out2; \
  164. int i; \
  165. a1 = -a1; \
  166. a2 = -a2; \
  167. \
  168. for (i = 0; i+1 < len; i++) { \
  169. o2 = i2 * b2 + i1 * b1 + ibuf[i] * b0 + o2 * a2 + o1 * a1; \
  170. i2 = ibuf[i]; \
  171. if (need_clipping && o2 < min) { \
  172. s->clippings++; \
  173. obuf[i] = min; \
  174. } else if (need_clipping && o2 > max) { \
  175. s->clippings++; \
  176. obuf[i] = max; \
  177. } else { \
  178. obuf[i] = o2; \
  179. } \
  180. i++; \
  181. o1 = i1 * b2 + i2 * b1 + ibuf[i] * b0 + o1 * a2 + o2 * a1; \
  182. i1 = ibuf[i]; \
  183. if (need_clipping && o1 < min) { \
  184. s->clippings++; \
  185. obuf[i] = min; \
  186. } else if (need_clipping && o1 > max) { \
  187. s->clippings++; \
  188. obuf[i] = max; \
  189. } else { \
  190. obuf[i] = o1; \
  191. } \
  192. } \
  193. if (i < len) { \
  194. double o0 = ibuf[i] * b0 + i1 * b1 + i2 * b2 + o1 * a1 + o2 * a2; \
  195. i2 = i1; \
  196. i1 = ibuf[i]; \
  197. o2 = o1; \
  198. o1 = o0; \
  199. if (need_clipping && o0 < min) { \
  200. s->clippings++; \
  201. obuf[i] = min; \
  202. } else if (need_clipping && o0 > max) { \
  203. s->clippings++; \
  204. obuf[i] = max; \
  205. } else { \
  206. obuf[i] = o0; \
  207. } \
  208. } \
  209. *in1 = i1; \
  210. *in2 = i2; \
  211. *out1 = o1; \
  212. *out2 = o2; \
  213. }
  214. BIQUAD_FILTER(s16, int16_t, INT16_MIN, INT16_MAX, 1)
  215. BIQUAD_FILTER(s32, int32_t, INT32_MIN, INT32_MAX, 1)
  216. BIQUAD_FILTER(flt, float, -1., 1., 0)
  217. BIQUAD_FILTER(dbl, double, -1., 1., 0)
  218. static int config_filter(AVFilterLink *outlink, int reset)
  219. {
  220. AVFilterContext *ctx = outlink->src;
  221. BiquadsContext *s = ctx->priv;
  222. AVFilterLink *inlink = ctx->inputs[0];
  223. double A = exp(s->gain / 40 * log(10.));
  224. double w0 = 2 * M_PI * s->frequency / inlink->sample_rate;
  225. double alpha;
  226. if (w0 > M_PI) {
  227. av_log(ctx, AV_LOG_ERROR,
  228. "Invalid frequency %f. Frequency must be less than half the sample-rate %d.\n",
  229. s->frequency, inlink->sample_rate);
  230. return AVERROR(EINVAL);
  231. }
  232. switch (s->width_type) {
  233. case NONE:
  234. alpha = 0.0;
  235. break;
  236. case HERTZ:
  237. alpha = sin(w0) / (2 * s->frequency / s->width);
  238. break;
  239. case OCTAVE:
  240. alpha = sin(w0) * sinh(log(2.) / 2 * s->width * w0 / sin(w0));
  241. break;
  242. case QFACTOR:
  243. alpha = sin(w0) / (2 * s->width);
  244. break;
  245. case SLOPE:
  246. alpha = sin(w0) / 2 * sqrt((A + 1 / A) * (1 / s->width - 1) + 2);
  247. break;
  248. default:
  249. av_assert0(0);
  250. }
  251. switch (s->filter_type) {
  252. case biquad:
  253. break;
  254. case equalizer:
  255. s->a0 = 1 + alpha / A;
  256. s->a1 = -2 * cos(w0);
  257. s->a2 = 1 - alpha / A;
  258. s->b0 = 1 + alpha * A;
  259. s->b1 = -2 * cos(w0);
  260. s->b2 = 1 - alpha * A;
  261. break;
  262. case bass:
  263. s->a0 = (A + 1) + (A - 1) * cos(w0) + 2 * sqrt(A) * alpha;
  264. s->a1 = -2 * ((A - 1) + (A + 1) * cos(w0));
  265. s->a2 = (A + 1) + (A - 1) * cos(w0) - 2 * sqrt(A) * alpha;
  266. s->b0 = A * ((A + 1) - (A - 1) * cos(w0) + 2 * sqrt(A) * alpha);
  267. s->b1 = 2 * A * ((A - 1) - (A + 1) * cos(w0));
  268. s->b2 = A * ((A + 1) - (A - 1) * cos(w0) - 2 * sqrt(A) * alpha);
  269. break;
  270. case treble:
  271. s->a0 = (A + 1) - (A - 1) * cos(w0) + 2 * sqrt(A) * alpha;
  272. s->a1 = 2 * ((A - 1) - (A + 1) * cos(w0));
  273. s->a2 = (A + 1) - (A - 1) * cos(w0) - 2 * sqrt(A) * alpha;
  274. s->b0 = A * ((A + 1) + (A - 1) * cos(w0) + 2 * sqrt(A) * alpha);
  275. s->b1 =-2 * A * ((A - 1) + (A + 1) * cos(w0));
  276. s->b2 = A * ((A + 1) + (A - 1) * cos(w0) - 2 * sqrt(A) * alpha);
  277. break;
  278. case bandpass:
  279. if (s->csg) {
  280. s->a0 = 1 + alpha;
  281. s->a1 = -2 * cos(w0);
  282. s->a2 = 1 - alpha;
  283. s->b0 = sin(w0) / 2;
  284. s->b1 = 0;
  285. s->b2 = -sin(w0) / 2;
  286. } else {
  287. s->a0 = 1 + alpha;
  288. s->a1 = -2 * cos(w0);
  289. s->a2 = 1 - alpha;
  290. s->b0 = alpha;
  291. s->b1 = 0;
  292. s->b2 = -alpha;
  293. }
  294. break;
  295. case bandreject:
  296. s->a0 = 1 + alpha;
  297. s->a1 = -2 * cos(w0);
  298. s->a2 = 1 - alpha;
  299. s->b0 = 1;
  300. s->b1 = -2 * cos(w0);
  301. s->b2 = 1;
  302. break;
  303. case lowpass:
  304. if (s->poles == 1) {
  305. s->a0 = 1;
  306. s->a1 = -exp(-w0);
  307. s->a2 = 0;
  308. s->b0 = 1 + s->a1;
  309. s->b1 = 0;
  310. s->b2 = 0;
  311. } else {
  312. s->a0 = 1 + alpha;
  313. s->a1 = -2 * cos(w0);
  314. s->a2 = 1 - alpha;
  315. s->b0 = (1 - cos(w0)) / 2;
  316. s->b1 = 1 - cos(w0);
  317. s->b2 = (1 - cos(w0)) / 2;
  318. }
  319. break;
  320. case highpass:
  321. if (s->poles == 1) {
  322. s->a0 = 1;
  323. s->a1 = -exp(-w0);
  324. s->a2 = 0;
  325. s->b0 = (1 - s->a1) / 2;
  326. s->b1 = -s->b0;
  327. s->b2 = 0;
  328. } else {
  329. s->a0 = 1 + alpha;
  330. s->a1 = -2 * cos(w0);
  331. s->a2 = 1 - alpha;
  332. s->b0 = (1 + cos(w0)) / 2;
  333. s->b1 = -(1 + cos(w0));
  334. s->b2 = (1 + cos(w0)) / 2;
  335. }
  336. break;
  337. case allpass:
  338. s->a0 = 1 + alpha;
  339. s->a1 = -2 * cos(w0);
  340. s->a2 = 1 - alpha;
  341. s->b0 = 1 - alpha;
  342. s->b1 = -2 * cos(w0);
  343. s->b2 = 1 + alpha;
  344. break;
  345. default:
  346. av_assert0(0);
  347. }
  348. s->a1 /= s->a0;
  349. s->a2 /= s->a0;
  350. s->b0 /= s->a0;
  351. s->b1 /= s->a0;
  352. s->b2 /= s->a0;
  353. s->cache = av_realloc_f(s->cache, sizeof(ChanCache), inlink->channels);
  354. if (!s->cache)
  355. return AVERROR(ENOMEM);
  356. if (reset)
  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 config_output(AVFilterLink *outlink)
  369. {
  370. return config_filter(outlink, 1);
  371. }
  372. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  373. {
  374. AVFilterContext *ctx = inlink->dst;
  375. BiquadsContext *s = ctx->priv;
  376. AVFilterLink *outlink = ctx->outputs[0];
  377. AVFrame *out_buf;
  378. int nb_samples = buf->nb_samples;
  379. int ch;
  380. if (av_frame_is_writable(buf)) {
  381. out_buf = buf;
  382. } else {
  383. out_buf = ff_get_audio_buffer(inlink, nb_samples);
  384. if (!out_buf) {
  385. av_frame_free(&buf);
  386. return AVERROR(ENOMEM);
  387. }
  388. av_frame_copy_props(out_buf, buf);
  389. }
  390. for (ch = 0; ch < buf->channels; ch++) {
  391. if (!((av_channel_layout_extract_channel(inlink->channel_layout, ch) & s->channels))) {
  392. if (buf != out_buf)
  393. memcpy(out_buf->extended_data[ch], buf->extended_data[ch], nb_samples * s->block_align);
  394. continue;
  395. }
  396. s->filter(s, buf->extended_data[ch],
  397. out_buf->extended_data[ch], nb_samples,
  398. &s->cache[ch].i1, &s->cache[ch].i2,
  399. &s->cache[ch].o1, &s->cache[ch].o2,
  400. s->b0, s->b1, s->b2, s->a1, s->a2);
  401. }
  402. if (s->clippings > 0)
  403. av_log(ctx, AV_LOG_WARNING, "clipping %d times. Please reduce gain.\n", s->clippings);
  404. s->clippings = 0;
  405. if (buf != out_buf)
  406. av_frame_free(&buf);
  407. return ff_filter_frame(outlink, out_buf);
  408. }
  409. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  410. char *res, int res_len, int flags)
  411. {
  412. BiquadsContext *s = ctx->priv;
  413. AVFilterLink *outlink = ctx->outputs[0];
  414. if ((!strcmp(cmd, "frequency") || !strcmp(cmd, "f")) &&
  415. (s->filter_type == equalizer ||
  416. s->filter_type == bass ||
  417. s->filter_type == treble ||
  418. s->filter_type == bandpass ||
  419. s->filter_type == bandreject||
  420. s->filter_type == lowpass ||
  421. s->filter_type == highpass ||
  422. s->filter_type == allpass)) {
  423. double freq;
  424. if (sscanf(args, "%lf", &freq) != 1) {
  425. av_log(ctx, AV_LOG_ERROR, "Invalid frequency value.\n");
  426. return AVERROR(EINVAL);
  427. }
  428. s->frequency = freq;
  429. } else if ((!strcmp(cmd, "gain") || !strcmp(cmd, "g")) &&
  430. (s->filter_type == equalizer ||
  431. s->filter_type == bass ||
  432. s->filter_type == treble)) {
  433. double gain;
  434. if (sscanf(args, "%lf", &gain) != 1) {
  435. av_log(ctx, AV_LOG_ERROR, "Invalid gain value.\n");
  436. return AVERROR(EINVAL);
  437. }
  438. s->gain = gain;
  439. } else if ((!strcmp(cmd, "width") || !strcmp(cmd, "w")) &&
  440. (s->filter_type == equalizer ||
  441. s->filter_type == bass ||
  442. s->filter_type == treble ||
  443. s->filter_type == bandpass ||
  444. s->filter_type == bandreject||
  445. s->filter_type == lowpass ||
  446. s->filter_type == highpass ||
  447. s->filter_type == allpass)) {
  448. double width;
  449. if (sscanf(args, "%lf", &width) != 1) {
  450. av_log(ctx, AV_LOG_ERROR, "Invalid width value.\n");
  451. return AVERROR(EINVAL);
  452. }
  453. s->width = width;
  454. } else if ((!strcmp(cmd, "width_type") || !strcmp(cmd, "t")) &&
  455. (s->filter_type == equalizer ||
  456. s->filter_type == bass ||
  457. s->filter_type == treble ||
  458. s->filter_type == bandpass ||
  459. s->filter_type == bandreject||
  460. s->filter_type == lowpass ||
  461. s->filter_type == highpass ||
  462. s->filter_type == allpass)) {
  463. char width_type;
  464. if (sscanf(args, "%c", &width_type) != 1) {
  465. av_log(ctx, AV_LOG_ERROR, "Invalid width_type value.\n");
  466. return AVERROR(EINVAL);
  467. }
  468. switch (width_type) {
  469. case 'h': width_type = HERTZ; break;
  470. case 'q': width_type = QFACTOR; break;
  471. case 'o': width_type = OCTAVE; break;
  472. case 's': width_type = SLOPE; break;
  473. default:
  474. av_log(ctx, AV_LOG_ERROR, "Invalid width_type value: %c\n", width_type);
  475. return AVERROR(EINVAL);
  476. }
  477. s->width_type = width_type;
  478. } else if ((!strcmp(cmd, "a0") ||
  479. !strcmp(cmd, "a1") ||
  480. !strcmp(cmd, "a2") ||
  481. !strcmp(cmd, "b0") ||
  482. !strcmp(cmd, "b1") ||
  483. !strcmp(cmd, "b2")) &&
  484. s->filter_type == biquad) {
  485. double value;
  486. if (sscanf(args, "%lf", &value) != 1) {
  487. av_log(ctx, AV_LOG_ERROR, "Invalid biquad value.\n");
  488. return AVERROR(EINVAL);
  489. }
  490. if (!strcmp(cmd, "a0"))
  491. s->a0 = value;
  492. else if (!strcmp(cmd, "a1"))
  493. s->a1 = value;
  494. else if (!strcmp(cmd, "a2"))
  495. s->a2 = value;
  496. else if (!strcmp(cmd, "b0"))
  497. s->b0 = value;
  498. else if (!strcmp(cmd, "b1"))
  499. s->b1 = value;
  500. else if (!strcmp(cmd, "b2"))
  501. s->b2 = value;
  502. }
  503. return config_filter(outlink, 0);
  504. }
  505. static av_cold void uninit(AVFilterContext *ctx)
  506. {
  507. BiquadsContext *s = ctx->priv;
  508. av_freep(&s->cache);
  509. }
  510. static const AVFilterPad inputs[] = {
  511. {
  512. .name = "default",
  513. .type = AVMEDIA_TYPE_AUDIO,
  514. .filter_frame = filter_frame,
  515. },
  516. { NULL }
  517. };
  518. static const AVFilterPad outputs[] = {
  519. {
  520. .name = "default",
  521. .type = AVMEDIA_TYPE_AUDIO,
  522. .config_props = config_output,
  523. },
  524. { NULL }
  525. };
  526. #define OFFSET(x) offsetof(BiquadsContext, x)
  527. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  528. #define DEFINE_BIQUAD_FILTER(name_, description_) \
  529. AVFILTER_DEFINE_CLASS(name_); \
  530. static av_cold int name_##_init(AVFilterContext *ctx) \
  531. { \
  532. BiquadsContext *s = ctx->priv; \
  533. s->class = &name_##_class; \
  534. s->filter_type = name_; \
  535. return init(ctx); \
  536. } \
  537. \
  538. AVFilter ff_af_##name_ = { \
  539. .name = #name_, \
  540. .description = NULL_IF_CONFIG_SMALL(description_), \
  541. .priv_size = sizeof(BiquadsContext), \
  542. .init = name_##_init, \
  543. .uninit = uninit, \
  544. .query_formats = query_formats, \
  545. .inputs = inputs, \
  546. .outputs = outputs, \
  547. .priv_class = &name_##_class, \
  548. .process_command = process_command, \
  549. }
  550. #if CONFIG_EQUALIZER_FILTER
  551. static const AVOption equalizer_options[] = {
  552. {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 999999, FLAGS},
  553. {"f", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 999999, FLAGS},
  554. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
  555. {"t", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
  556. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
  557. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  558. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  559. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  560. {"width", "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 999, FLAGS},
  561. {"w", "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 999, FLAGS},
  562. {"gain", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
  563. {"g", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
  564. {"channels", "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
  565. {"c", "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
  566. {NULL}
  567. };
  568. DEFINE_BIQUAD_FILTER(equalizer, "Apply two-pole peaking equalization (EQ) filter.");
  569. #endif /* CONFIG_EQUALIZER_FILTER */
  570. #if CONFIG_BASS_FILTER
  571. static const AVOption bass_options[] = {
  572. {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=100}, 0, 999999, FLAGS},
  573. {"f", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=100}, 0, 999999, FLAGS},
  574. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
  575. {"t", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
  576. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
  577. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  578. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  579. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  580. {"width", "set shelf transition steep", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 99999, FLAGS},
  581. {"w", "set shelf transition steep", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 99999, FLAGS},
  582. {"gain", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
  583. {"g", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
  584. {"channels", "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
  585. {"c", "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
  586. {NULL}
  587. };
  588. DEFINE_BIQUAD_FILTER(bass, "Boost or cut lower frequencies.");
  589. #endif /* CONFIG_BASS_FILTER */
  590. #if CONFIG_TREBLE_FILTER
  591. static const AVOption treble_options[] = {
  592. {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  593. {"f", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  594. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
  595. {"t", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
  596. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
  597. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  598. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  599. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  600. {"width", "set shelf transition steep", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 99999, FLAGS},
  601. {"w", "set shelf transition steep", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 99999, FLAGS},
  602. {"gain", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
  603. {"g", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
  604. {"channels", "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
  605. {"c", "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
  606. {NULL}
  607. };
  608. DEFINE_BIQUAD_FILTER(treble, "Boost or cut upper frequencies.");
  609. #endif /* CONFIG_TREBLE_FILTER */
  610. #if CONFIG_BANDPASS_FILTER
  611. static const AVOption bandpass_options[] = {
  612. {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  613. {"f", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  614. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
  615. {"t", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
  616. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
  617. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  618. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  619. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  620. {"width", "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 999, FLAGS},
  621. {"w", "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 999, FLAGS},
  622. {"csg", "use constant skirt gain", OFFSET(csg), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
  623. {"channels", "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
  624. {"c", "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
  625. {NULL}
  626. };
  627. DEFINE_BIQUAD_FILTER(bandpass, "Apply a two-pole Butterworth band-pass filter.");
  628. #endif /* CONFIG_BANDPASS_FILTER */
  629. #if CONFIG_BANDREJECT_FILTER
  630. static const AVOption bandreject_options[] = {
  631. {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  632. {"f", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  633. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
  634. {"t", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
  635. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
  636. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  637. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  638. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  639. {"width", "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 999, FLAGS},
  640. {"w", "set band-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 999, FLAGS},
  641. {"channels", "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
  642. {"c", "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
  643. {NULL}
  644. };
  645. DEFINE_BIQUAD_FILTER(bandreject, "Apply a two-pole Butterworth band-reject filter.");
  646. #endif /* CONFIG_BANDREJECT_FILTER */
  647. #if CONFIG_LOWPASS_FILTER
  648. static const AVOption lowpass_options[] = {
  649. {"frequency", "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=500}, 0, 999999, FLAGS},
  650. {"f", "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=500}, 0, 999999, FLAGS},
  651. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
  652. {"t", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
  653. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
  654. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  655. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  656. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  657. {"width", "set width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.707}, 0, 99999, FLAGS},
  658. {"w", "set width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.707}, 0, 99999, FLAGS},
  659. {"poles", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, FLAGS},
  660. {"p", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, FLAGS},
  661. {"channels", "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
  662. {"c", "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
  663. {NULL}
  664. };
  665. DEFINE_BIQUAD_FILTER(lowpass, "Apply a low-pass filter with 3dB point frequency.");
  666. #endif /* CONFIG_LOWPASS_FILTER */
  667. #if CONFIG_HIGHPASS_FILTER
  668. static const AVOption highpass_options[] = {
  669. {"frequency", "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  670. {"f", "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  671. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
  672. {"t", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=QFACTOR}, HERTZ, SLOPE, FLAGS, "width_type"},
  673. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
  674. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  675. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  676. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  677. {"width", "set width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.707}, 0, 99999, FLAGS},
  678. {"w", "set width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=0.707}, 0, 99999, FLAGS},
  679. {"poles", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, FLAGS},
  680. {"p", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, FLAGS},
  681. {"channels", "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
  682. {"c", "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
  683. {NULL}
  684. };
  685. DEFINE_BIQUAD_FILTER(highpass, "Apply a high-pass filter with 3dB point frequency.");
  686. #endif /* CONFIG_HIGHPASS_FILTER */
  687. #if CONFIG_ALLPASS_FILTER
  688. static const AVOption allpass_options[] = {
  689. {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  690. {"f", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
  691. {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=HERTZ}, HERTZ, SLOPE, FLAGS, "width_type"},
  692. {"t", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=HERTZ}, HERTZ, SLOPE, FLAGS, "width_type"},
  693. {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, "width_type"},
  694. {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, "width_type"},
  695. {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, "width_type"},
  696. {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, "width_type"},
  697. {"width", "set filter-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=707.1}, 0, 99999, FLAGS},
  698. {"w", "set filter-width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=707.1}, 0, 99999, FLAGS},
  699. {"channels", "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
  700. {"c", "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
  701. {NULL}
  702. };
  703. DEFINE_BIQUAD_FILTER(allpass, "Apply a two-pole all-pass filter.");
  704. #endif /* CONFIG_ALLPASS_FILTER */
  705. #if CONFIG_BIQUAD_FILTER
  706. static const AVOption biquad_options[] = {
  707. {"a0", NULL, OFFSET(a0), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MIN, INT16_MAX, FLAGS},
  708. {"a1", NULL, OFFSET(a1), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MIN, INT16_MAX, FLAGS},
  709. {"a2", NULL, OFFSET(a2), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MIN, INT16_MAX, FLAGS},
  710. {"b0", NULL, OFFSET(b0), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MIN, INT16_MAX, FLAGS},
  711. {"b1", NULL, OFFSET(b1), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MIN, INT16_MAX, FLAGS},
  712. {"b2", NULL, OFFSET(b2), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MIN, INT16_MAX, FLAGS},
  713. {"channels", "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
  714. {"c", "set channels to filter", OFFSET(channels), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64=-1}, INT64_MIN, INT64_MAX, FLAGS},
  715. {NULL}
  716. };
  717. DEFINE_BIQUAD_FILTER(biquad, "Apply a biquad IIR filter with the given coefficients.");
  718. #endif /* CONFIG_BIQUAD_FILTER */