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.

1075 lines
56KB

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