af_biquads.c 43 KB

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