af_anequalizer.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. /*
  2. * Copyright (c) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen and others
  3. * Copyright (c) 2015 Paul B Mahol
  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. #include "libavutil/intreadwrite.h"
  22. #include "libavutil/avstring.h"
  23. #include "libavutil/ffmath.h"
  24. #include "libavutil/opt.h"
  25. #include "libavutil/parseutils.h"
  26. #include "avfilter.h"
  27. #include "internal.h"
  28. #include "audio.h"
  29. #define FILTER_ORDER 4
  30. enum FilterType {
  31. BUTTERWORTH,
  32. CHEBYSHEV1,
  33. CHEBYSHEV2,
  34. NB_TYPES
  35. };
  36. typedef struct FoSection {
  37. double a0, a1, a2, a3, a4;
  38. double b0, b1, b2, b3, b4;
  39. double num[4];
  40. double denum[4];
  41. } FoSection;
  42. typedef struct EqualizatorFilter {
  43. int ignore;
  44. int channel;
  45. int type;
  46. double freq;
  47. double gain;
  48. double width;
  49. FoSection section[2];
  50. } EqualizatorFilter;
  51. typedef struct AudioNEqualizerContext {
  52. const AVClass *class;
  53. char *args;
  54. char *colors;
  55. int draw_curves;
  56. int w, h;
  57. double mag;
  58. int fscale;
  59. int nb_filters;
  60. int nb_allocated;
  61. EqualizatorFilter *filters;
  62. AVFrame *video;
  63. } AudioNEqualizerContext;
  64. #define OFFSET(x) offsetof(AudioNEqualizerContext, x)
  65. #define A AV_OPT_FLAG_AUDIO_PARAM
  66. #define V AV_OPT_FLAG_VIDEO_PARAM
  67. #define F AV_OPT_FLAG_FILTERING_PARAM
  68. static const AVOption anequalizer_options[] = {
  69. { "params", NULL, OFFSET(args), AV_OPT_TYPE_STRING, {.str=""}, 0, 0, A|F },
  70. { "curves", "draw frequency response curves", OFFSET(draw_curves), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, V|F },
  71. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "hd720"}, 0, 0, V|F },
  72. { "mgain", "set max gain", OFFSET(mag), AV_OPT_TYPE_DOUBLE, {.dbl=60}, -900, 900, V|F },
  73. { "fscale", "set frequency scale", OFFSET(fscale), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, V|F, "fscale" },
  74. { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, V|F, "fscale" },
  75. { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, V|F, "fscale" },
  76. { "colors", "set channels curves colors", OFFSET(colors), AV_OPT_TYPE_STRING, {.str = "red|green|blue|yellow|orange|lime|pink|magenta|brown" }, 0, 0, V|F },
  77. { NULL }
  78. };
  79. AVFILTER_DEFINE_CLASS(anequalizer);
  80. static void draw_curves(AVFilterContext *ctx, AVFilterLink *inlink, AVFrame *out)
  81. {
  82. AudioNEqualizerContext *s = ctx->priv;
  83. char *colors, *color, *saveptr = NULL;
  84. int ch, i, n;
  85. colors = av_strdup(s->colors);
  86. if (!colors)
  87. return;
  88. memset(out->data[0], 0, s->h * out->linesize[0]);
  89. for (ch = 0; ch < inlink->channels; ch++) {
  90. uint8_t fg[4] = { 0xff, 0xff, 0xff, 0xff };
  91. int prev_v = -1;
  92. double f;
  93. color = av_strtok(ch == 0 ? colors : NULL, " |", &saveptr);
  94. if (color)
  95. av_parse_color(fg, color, -1, ctx);
  96. for (f = 0; f < s->w; f++) {
  97. double zr, zi, zr2, zi2;
  98. double Hr, Hi;
  99. double Hmag = 1;
  100. double w;
  101. int v, y, x;
  102. w = M_PI * (s->fscale ? pow(s->w - 1, f / s->w) : f) / (s->w - 1);
  103. zr = cos(w);
  104. zr2 = zr * zr;
  105. zi = -sin(w);
  106. zi2 = zi * zi;
  107. for (n = 0; n < s->nb_filters; n++) {
  108. if (s->filters[n].channel != ch ||
  109. s->filters[n].ignore)
  110. continue;
  111. for (i = 0; i < FILTER_ORDER / 2; i++) {
  112. FoSection *S = &s->filters[n].section[i];
  113. /* H *= (((((S->b4 * z + S->b3) * z + S->b2) * z + S->b1) * z + S->b0) /
  114. ((((S->a4 * z + S->a3) * z + S->a2) * z + S->a1) * z + S->a0)); */
  115. Hr = S->b4*(1-8*zr2*zi2) + S->b2*(zr2-zi2) + zr*(S->b1+S->b3*(zr2-3*zi2))+ S->b0;
  116. Hi = zi*(S->b3*(3*zr2-zi2) + S->b1 + 2*zr*(2*S->b4*(zr2-zi2) + S->b2));
  117. Hmag *= hypot(Hr, Hi);
  118. Hr = S->a4*(1-8*zr2*zi2) + S->a2*(zr2-zi2) + zr*(S->a1+S->a3*(zr2-3*zi2))+ S->a0;
  119. Hi = zi*(S->a3*(3*zr2-zi2) + S->a1 + 2*zr*(2*S->a4*(zr2-zi2) + S->a2));
  120. Hmag /= hypot(Hr, Hi);
  121. }
  122. }
  123. v = av_clip((1. + -20 * log10(Hmag) / s->mag) * s->h / 2, 0, s->h - 1);
  124. x = lrint(f);
  125. if (prev_v == -1)
  126. prev_v = v;
  127. if (v <= prev_v) {
  128. for (y = v; y <= prev_v; y++)
  129. AV_WL32(out->data[0] + y * out->linesize[0] + x * 4, AV_RL32(fg));
  130. } else {
  131. for (y = prev_v; y <= v; y++)
  132. AV_WL32(out->data[0] + y * out->linesize[0] + x * 4, AV_RL32(fg));
  133. }
  134. prev_v = v;
  135. }
  136. }
  137. av_free(colors);
  138. }
  139. static int config_video(AVFilterLink *outlink)
  140. {
  141. AVFilterContext *ctx = outlink->src;
  142. AudioNEqualizerContext *s = ctx->priv;
  143. AVFilterLink *inlink = ctx->inputs[0];
  144. AVFrame *out;
  145. outlink->w = s->w;
  146. outlink->h = s->h;
  147. av_frame_free(&s->video);
  148. s->video = out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  149. if (!out)
  150. return AVERROR(ENOMEM);
  151. outlink->sample_aspect_ratio = (AVRational){1,1};
  152. draw_curves(ctx, inlink, out);
  153. return 0;
  154. }
  155. static av_cold int init(AVFilterContext *ctx)
  156. {
  157. AudioNEqualizerContext *s = ctx->priv;
  158. AVFilterPad pad, vpad;
  159. int ret;
  160. pad = (AVFilterPad){
  161. .name = av_strdup("out0"),
  162. .type = AVMEDIA_TYPE_AUDIO,
  163. };
  164. if (!pad.name)
  165. return AVERROR(ENOMEM);
  166. if (s->draw_curves) {
  167. vpad = (AVFilterPad){
  168. .name = av_strdup("out1"),
  169. .type = AVMEDIA_TYPE_VIDEO,
  170. .config_props = config_video,
  171. };
  172. if (!vpad.name)
  173. return AVERROR(ENOMEM);
  174. }
  175. ret = ff_insert_outpad(ctx, 0, &pad);
  176. if (ret < 0) {
  177. av_freep(&pad.name);
  178. return ret;
  179. }
  180. if (s->draw_curves) {
  181. ret = ff_insert_outpad(ctx, 1, &vpad);
  182. if (ret < 0) {
  183. av_freep(&vpad.name);
  184. return ret;
  185. }
  186. }
  187. return 0;
  188. }
  189. static int query_formats(AVFilterContext *ctx)
  190. {
  191. AVFilterLink *inlink = ctx->inputs[0];
  192. AVFilterLink *outlink = ctx->outputs[0];
  193. AudioNEqualizerContext *s = ctx->priv;
  194. AVFilterFormats *formats;
  195. AVFilterChannelLayouts *layouts;
  196. static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
  197. static const enum AVSampleFormat sample_fmts[] = {
  198. AV_SAMPLE_FMT_DBLP,
  199. AV_SAMPLE_FMT_NONE
  200. };
  201. int ret;
  202. if (s->draw_curves) {
  203. AVFilterLink *videolink = ctx->outputs[1];
  204. formats = ff_make_format_list(pix_fmts);
  205. if ((ret = ff_formats_ref(formats, &videolink->in_formats)) < 0)
  206. return ret;
  207. }
  208. formats = ff_make_format_list(sample_fmts);
  209. if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0 ||
  210. (ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
  211. return ret;
  212. layouts = ff_all_channel_counts();
  213. if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0 ||
  214. (ret = ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts)) < 0)
  215. return ret;
  216. formats = ff_all_samplerates();
  217. if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0 ||
  218. (ret = ff_formats_ref(formats, &outlink->in_samplerates)) < 0)
  219. return ret;
  220. return 0;
  221. }
  222. static av_cold void uninit(AVFilterContext *ctx)
  223. {
  224. AudioNEqualizerContext *s = ctx->priv;
  225. for (int i = 0; i < ctx->nb_outputs; i++)
  226. av_freep(&ctx->output_pads[i].name);
  227. av_frame_free(&s->video);
  228. av_freep(&s->filters);
  229. s->nb_filters = 0;
  230. s->nb_allocated = 0;
  231. }
  232. static void butterworth_fo_section(FoSection *S, double beta,
  233. double si, double g, double g0,
  234. double D, double c0)
  235. {
  236. if (c0 == 1 || c0 == -1) {
  237. S->b0 = (g*g*beta*beta + 2*g*g0*si*beta + g0*g0)/D;
  238. S->b1 = 2*c0*(g*g*beta*beta - g0*g0)/D;
  239. S->b2 = (g*g*beta*beta - 2*g0*g*beta*si + g0*g0)/D;
  240. S->b3 = 0;
  241. S->b4 = 0;
  242. S->a0 = 1;
  243. S->a1 = 2*c0*(beta*beta - 1)/D;
  244. S->a2 = (beta*beta - 2*beta*si + 1)/D;
  245. S->a3 = 0;
  246. S->a4 = 0;
  247. } else {
  248. S->b0 = (g*g*beta*beta + 2*g*g0*si*beta + g0*g0)/D;
  249. S->b1 = -4*c0*(g0*g0 + g*g0*si*beta)/D;
  250. S->b2 = 2*(g0*g0*(1 + 2*c0*c0) - g*g*beta*beta)/D;
  251. S->b3 = -4*c0*(g0*g0 - g*g0*si*beta)/D;
  252. S->b4 = (g*g*beta*beta - 2*g*g0*si*beta + g0*g0)/D;
  253. S->a0 = 1;
  254. S->a1 = -4*c0*(1 + si*beta)/D;
  255. S->a2 = 2*(1 + 2*c0*c0 - beta*beta)/D;
  256. S->a3 = -4*c0*(1 - si*beta)/D;
  257. S->a4 = (beta*beta - 2*si*beta + 1)/D;
  258. }
  259. }
  260. static void butterworth_bp_filter(EqualizatorFilter *f,
  261. int N, double w0, double wb,
  262. double G, double Gb, double G0)
  263. {
  264. double g, c0, g0, beta;
  265. double epsilon;
  266. int r = N % 2;
  267. int L = (N - r) / 2;
  268. int i;
  269. if (G == 0 && G0 == 0) {
  270. f->section[0].a0 = 1;
  271. f->section[0].b0 = 1;
  272. f->section[1].a0 = 1;
  273. f->section[1].b0 = 1;
  274. return;
  275. }
  276. G = ff_exp10(G/20);
  277. Gb = ff_exp10(Gb/20);
  278. G0 = ff_exp10(G0/20);
  279. epsilon = sqrt((G * G - Gb * Gb) / (Gb * Gb - G0 * G0));
  280. g = pow(G, 1.0 / N);
  281. g0 = pow(G0, 1.0 / N);
  282. beta = pow(epsilon, -1.0 / N) * tan(wb/2);
  283. c0 = cos(w0);
  284. for (i = 1; i <= L; i++) {
  285. double ui = (2.0 * i - 1) / N;
  286. double si = sin(M_PI * ui / 2.0);
  287. double Di = beta * beta + 2 * si * beta + 1;
  288. butterworth_fo_section(&f->section[i - 1], beta, si, g, g0, Di, c0);
  289. }
  290. }
  291. static void chebyshev1_fo_section(FoSection *S, double a,
  292. double c, double tetta_b,
  293. double g0, double si, double b,
  294. double D, double c0)
  295. {
  296. if (c0 == 1 || c0 == -1) {
  297. S->b0 = (tetta_b*tetta_b*(b*b+g0*g0*c*c) + 2*g0*b*si*tetta_b*tetta_b + g0*g0)/D;
  298. S->b1 = 2*c0*(tetta_b*tetta_b*(b*b+g0*g0*c*c) - g0*g0)/D;
  299. S->b2 = (tetta_b*tetta_b*(b*b+g0*g0*c*c) - 2*g0*b*si*tetta_b + g0*g0)/D;
  300. S->b3 = 0;
  301. S->b4 = 0;
  302. S->a0 = 1;
  303. S->a1 = 2*c0*(tetta_b*tetta_b*(a*a+c*c) - 1)/D;
  304. S->a2 = (tetta_b*tetta_b*(a*a+c*c) - 2*a*si*tetta_b + 1)/D;
  305. S->a3 = 0;
  306. S->a4 = 0;
  307. } else {
  308. S->b0 = ((b*b + g0*g0*c*c)*tetta_b*tetta_b + 2*g0*b*si*tetta_b + g0*g0)/D;
  309. S->b1 = -4*c0*(g0*g0 + g0*b*si*tetta_b)/D;
  310. S->b2 = 2*(g0*g0*(1 + 2*c0*c0) - (b*b + g0*g0*c*c)*tetta_b*tetta_b)/D;
  311. S->b3 = -4*c0*(g0*g0 - g0*b*si*tetta_b)/D;
  312. S->b4 = ((b*b + g0*g0*c*c)*tetta_b*tetta_b - 2*g0*b*si*tetta_b + g0*g0)/D;
  313. S->a0 = 1;
  314. S->a1 = -4*c0*(1 + a*si*tetta_b)/D;
  315. S->a2 = 2*(1 + 2*c0*c0 - (a*a + c*c)*tetta_b*tetta_b)/D;
  316. S->a3 = -4*c0*(1 - a*si*tetta_b)/D;
  317. S->a4 = ((a*a + c*c)*tetta_b*tetta_b - 2*a*si*tetta_b + 1)/D;
  318. }
  319. }
  320. static void chebyshev1_bp_filter(EqualizatorFilter *f,
  321. int N, double w0, double wb,
  322. double G, double Gb, double G0)
  323. {
  324. double a, b, c0, g0, alfa, beta, tetta_b;
  325. double epsilon;
  326. int r = N % 2;
  327. int L = (N - r) / 2;
  328. int i;
  329. if (G == 0 && G0 == 0) {
  330. f->section[0].a0 = 1;
  331. f->section[0].b0 = 1;
  332. f->section[1].a0 = 1;
  333. f->section[1].b0 = 1;
  334. return;
  335. }
  336. G = ff_exp10(G/20);
  337. Gb = ff_exp10(Gb/20);
  338. G0 = ff_exp10(G0/20);
  339. epsilon = sqrt((G*G - Gb*Gb) / (Gb*Gb - G0*G0));
  340. g0 = pow(G0,1.0/N);
  341. alfa = pow(1.0/epsilon + sqrt(1 + 1/(epsilon*epsilon)), 1.0/N);
  342. beta = pow(G/epsilon + Gb * sqrt(1 + 1/(epsilon*epsilon)), 1.0/N);
  343. a = 0.5 * (alfa - 1.0/alfa);
  344. b = 0.5 * (beta - g0*g0*(1/beta));
  345. tetta_b = tan(wb/2);
  346. c0 = cos(w0);
  347. for (i = 1; i <= L; i++) {
  348. double ui = (2.0*i-1.0)/N;
  349. double ci = cos(M_PI*ui/2.0);
  350. double si = sin(M_PI*ui/2.0);
  351. double Di = (a*a + ci*ci)*tetta_b*tetta_b + 2.0*a*si*tetta_b + 1;
  352. chebyshev1_fo_section(&f->section[i - 1], a, ci, tetta_b, g0, si, b, Di, c0);
  353. }
  354. }
  355. static void chebyshev2_fo_section(FoSection *S, double a,
  356. double c, double tetta_b,
  357. double g, double si, double b,
  358. double D, double c0)
  359. {
  360. if (c0 == 1 || c0 == -1) {
  361. S->b0 = (g*g*tetta_b*tetta_b + 2*tetta_b*g*b*si + b*b + g*g*c*c)/D;
  362. S->b1 = 2*c0*(g*g*tetta_b*tetta_b - b*b - g*g*c*c)/D;
  363. S->b2 = (g*g*tetta_b*tetta_b - 2*tetta_b*g*b*si + b*b + g*g*c*c)/D;
  364. S->b3 = 0;
  365. S->b4 = 0;
  366. S->a0 = 1;
  367. S->a1 = 2*c0*(tetta_b*tetta_b - a*a - c*c)/D;
  368. S->a2 = (tetta_b*tetta_b - 2*tetta_b*a*si + a*a + c*c)/D;
  369. S->a3 = 0;
  370. S->a4 = 0;
  371. } else {
  372. S->b0 = (g*g*tetta_b*tetta_b + 2*g*b*si*tetta_b + b*b + g*g*c*c)/D;
  373. S->b1 = -4*c0*(b*b + g*g*c*c + g*b*si*tetta_b)/D;
  374. S->b2 = 2*((b*b + g*g*c*c)*(1 + 2*c0*c0) - g*g*tetta_b*tetta_b)/D;
  375. S->b3 = -4*c0*(b*b + g*g*c*c - g*b*si*tetta_b)/D;
  376. S->b4 = (g*g*tetta_b*tetta_b - 2*g*b*si*tetta_b + b*b + g*g*c*c)/D;
  377. S->a0 = 1;
  378. S->a1 = -4*c0*(a*a + c*c + a*si*tetta_b)/D;
  379. S->a2 = 2*((a*a + c*c)*(1 + 2*c0*c0) - tetta_b*tetta_b)/D;
  380. S->a3 = -4*c0*(a*a + c*c - a*si*tetta_b)/D;
  381. S->a4 = (tetta_b*tetta_b - 2*a*si*tetta_b + a*a + c*c)/D;
  382. }
  383. }
  384. static void chebyshev2_bp_filter(EqualizatorFilter *f,
  385. int N, double w0, double wb,
  386. double G, double Gb, double G0)
  387. {
  388. double a, b, c0, tetta_b;
  389. double epsilon, g, eu, ew;
  390. int r = N % 2;
  391. int L = (N - r) / 2;
  392. int i;
  393. if (G == 0 && G0 == 0) {
  394. f->section[0].a0 = 1;
  395. f->section[0].b0 = 1;
  396. f->section[1].a0 = 1;
  397. f->section[1].b0 = 1;
  398. return;
  399. }
  400. G = ff_exp10(G/20);
  401. Gb = ff_exp10(Gb/20);
  402. G0 = ff_exp10(G0/20);
  403. epsilon = sqrt((G*G - Gb*Gb) / (Gb*Gb - G0*G0));
  404. g = pow(G, 1.0 / N);
  405. eu = pow(epsilon + sqrt(1 + epsilon*epsilon), 1.0/N);
  406. ew = pow(G0*epsilon + Gb*sqrt(1 + epsilon*epsilon), 1.0/N);
  407. a = (eu - 1.0/eu)/2.0;
  408. b = (ew - g*g/ew)/2.0;
  409. tetta_b = tan(wb/2);
  410. c0 = cos(w0);
  411. for (i = 1; i <= L; i++) {
  412. double ui = (2.0 * i - 1.0)/N;
  413. double ci = cos(M_PI * ui / 2.0);
  414. double si = sin(M_PI * ui / 2.0);
  415. double Di = tetta_b*tetta_b + 2*a*si*tetta_b + a*a + ci*ci;
  416. chebyshev2_fo_section(&f->section[i - 1], a, ci, tetta_b, g, si, b, Di, c0);
  417. }
  418. }
  419. static double butterworth_compute_bw_gain_db(double gain)
  420. {
  421. double bw_gain = 0;
  422. if (gain <= -6)
  423. bw_gain = gain + 3;
  424. else if(gain > -6 && gain < 6)
  425. bw_gain = gain * 0.5;
  426. else if(gain >= 6)
  427. bw_gain = gain - 3;
  428. return bw_gain;
  429. }
  430. static double chebyshev1_compute_bw_gain_db(double gain)
  431. {
  432. double bw_gain = 0;
  433. if (gain <= -6)
  434. bw_gain = gain + 1;
  435. else if(gain > -6 && gain < 6)
  436. bw_gain = gain * 0.9;
  437. else if(gain >= 6)
  438. bw_gain = gain - 1;
  439. return bw_gain;
  440. }
  441. static double chebyshev2_compute_bw_gain_db(double gain)
  442. {
  443. double bw_gain = 0;
  444. if (gain <= -6)
  445. bw_gain = -3;
  446. else if(gain > -6 && gain < 6)
  447. bw_gain = gain * 0.3;
  448. else if(gain >= 6)
  449. bw_gain = 3;
  450. return bw_gain;
  451. }
  452. static inline double hz_2_rad(double x, double fs)
  453. {
  454. return 2 * M_PI * x / fs;
  455. }
  456. static void equalizer(EqualizatorFilter *f, double sample_rate)
  457. {
  458. double w0 = hz_2_rad(f->freq, sample_rate);
  459. double wb = hz_2_rad(f->width, sample_rate);
  460. double bw_gain;
  461. switch (f->type) {
  462. case BUTTERWORTH:
  463. bw_gain = butterworth_compute_bw_gain_db(f->gain);
  464. butterworth_bp_filter(f, FILTER_ORDER, w0, wb, f->gain, bw_gain, 0);
  465. break;
  466. case CHEBYSHEV1:
  467. bw_gain = chebyshev1_compute_bw_gain_db(f->gain);
  468. chebyshev1_bp_filter(f, FILTER_ORDER, w0, wb, f->gain, bw_gain, 0);
  469. break;
  470. case CHEBYSHEV2:
  471. bw_gain = chebyshev2_compute_bw_gain_db(f->gain);
  472. chebyshev2_bp_filter(f, FILTER_ORDER, w0, wb, f->gain, bw_gain, 0);
  473. break;
  474. }
  475. }
  476. static int add_filter(AudioNEqualizerContext *s, AVFilterLink *inlink)
  477. {
  478. equalizer(&s->filters[s->nb_filters], inlink->sample_rate);
  479. if (s->nb_filters >= s->nb_allocated) {
  480. EqualizatorFilter *filters;
  481. filters = av_calloc(s->nb_allocated, 2 * sizeof(*s->filters));
  482. if (!filters)
  483. return AVERROR(ENOMEM);
  484. memcpy(filters, s->filters, sizeof(*s->filters) * s->nb_allocated);
  485. av_free(s->filters);
  486. s->filters = filters;
  487. s->nb_allocated *= 2;
  488. }
  489. s->nb_filters++;
  490. return 0;
  491. }
  492. static int config_input(AVFilterLink *inlink)
  493. {
  494. AVFilterContext *ctx = inlink->dst;
  495. AudioNEqualizerContext *s = ctx->priv;
  496. char *args = av_strdup(s->args);
  497. char *saveptr = NULL;
  498. int ret = 0;
  499. if (!args)
  500. return AVERROR(ENOMEM);
  501. s->nb_allocated = 32 * inlink->channels;
  502. s->filters = av_calloc(inlink->channels, 32 * sizeof(*s->filters));
  503. if (!s->filters) {
  504. s->nb_allocated = 0;
  505. av_free(args);
  506. return AVERROR(ENOMEM);
  507. }
  508. while (1) {
  509. char *arg = av_strtok(s->nb_filters == 0 ? args : NULL, "|", &saveptr);
  510. if (!arg)
  511. break;
  512. s->filters[s->nb_filters].type = 0;
  513. if (sscanf(arg, "c%d f=%lf w=%lf g=%lf t=%d", &s->filters[s->nb_filters].channel,
  514. &s->filters[s->nb_filters].freq,
  515. &s->filters[s->nb_filters].width,
  516. &s->filters[s->nb_filters].gain,
  517. &s->filters[s->nb_filters].type) != 5 &&
  518. sscanf(arg, "c%d f=%lf w=%lf g=%lf", &s->filters[s->nb_filters].channel,
  519. &s->filters[s->nb_filters].freq,
  520. &s->filters[s->nb_filters].width,
  521. &s->filters[s->nb_filters].gain) != 4 ) {
  522. av_free(args);
  523. return AVERROR(EINVAL);
  524. }
  525. if (s->filters[s->nb_filters].freq < 0 ||
  526. s->filters[s->nb_filters].freq > inlink->sample_rate / 2.0)
  527. s->filters[s->nb_filters].ignore = 1;
  528. if (s->filters[s->nb_filters].channel < 0 ||
  529. s->filters[s->nb_filters].channel >= inlink->channels)
  530. s->filters[s->nb_filters].ignore = 1;
  531. s->filters[s->nb_filters].type = av_clip(s->filters[s->nb_filters].type, 0, NB_TYPES - 1);
  532. ret = add_filter(s, inlink);
  533. if (ret < 0)
  534. break;
  535. }
  536. av_free(args);
  537. return ret;
  538. }
  539. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  540. char *res, int res_len, int flags)
  541. {
  542. AudioNEqualizerContext *s = ctx->priv;
  543. AVFilterLink *inlink = ctx->inputs[0];
  544. int ret = AVERROR(ENOSYS);
  545. if (!strcmp(cmd, "change")) {
  546. double freq, width, gain;
  547. int filter;
  548. if (sscanf(args, "%d|f=%lf|w=%lf|g=%lf", &filter, &freq, &width, &gain) != 4)
  549. return AVERROR(EINVAL);
  550. if (filter < 0 || filter >= s->nb_filters)
  551. return AVERROR(EINVAL);
  552. if (freq < 0 || freq > inlink->sample_rate / 2.0)
  553. return AVERROR(EINVAL);
  554. s->filters[filter].freq = freq;
  555. s->filters[filter].width = width;
  556. s->filters[filter].gain = gain;
  557. equalizer(&s->filters[filter], inlink->sample_rate);
  558. if (s->draw_curves)
  559. draw_curves(ctx, inlink, s->video);
  560. ret = 0;
  561. }
  562. return ret;
  563. }
  564. static inline double section_process(FoSection *S, double in)
  565. {
  566. double out;
  567. out = S->b0 * in;
  568. out+= S->b1 * S->num[0] - S->denum[0] * S->a1;
  569. out+= S->b2 * S->num[1] - S->denum[1] * S->a2;
  570. out+= S->b3 * S->num[2] - S->denum[2] * S->a3;
  571. out+= S->b4 * S->num[3] - S->denum[3] * S->a4;
  572. S->num[3] = S->num[2];
  573. S->num[2] = S->num[1];
  574. S->num[1] = S->num[0];
  575. S->num[0] = in;
  576. S->denum[3] = S->denum[2];
  577. S->denum[2] = S->denum[1];
  578. S->denum[1] = S->denum[0];
  579. S->denum[0] = out;
  580. return out;
  581. }
  582. static double process_sample(FoSection *s1, double in)
  583. {
  584. double p0 = in, p1;
  585. int i;
  586. for (i = 0; i < FILTER_ORDER / 2; i++) {
  587. p1 = section_process(&s1[i], p0);
  588. p0 = p1;
  589. }
  590. return p1;
  591. }
  592. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  593. {
  594. AVFilterContext *ctx = inlink->dst;
  595. AudioNEqualizerContext *s = ctx->priv;
  596. AVFilterLink *outlink = ctx->outputs[0];
  597. double *bptr;
  598. int i, n;
  599. for (i = 0; i < s->nb_filters; i++) {
  600. EqualizatorFilter *f = &s->filters[i];
  601. if (f->gain == 0. || f->ignore)
  602. continue;
  603. bptr = (double *)buf->extended_data[f->channel];
  604. for (n = 0; n < buf->nb_samples; n++) {
  605. double sample = bptr[n];
  606. sample = process_sample(f->section, sample);
  607. bptr[n] = sample;
  608. }
  609. }
  610. if (s->draw_curves) {
  611. const int64_t pts = buf->pts +
  612. av_rescale_q(buf->nb_samples, (AVRational){ 1, inlink->sample_rate },
  613. outlink->time_base);
  614. int ret;
  615. s->video->pts = pts;
  616. ret = ff_filter_frame(ctx->outputs[1], av_frame_clone(s->video));
  617. if (ret < 0)
  618. return ret;
  619. }
  620. return ff_filter_frame(outlink, buf);
  621. }
  622. static const AVFilterPad inputs[] = {
  623. {
  624. .name = "default",
  625. .type = AVMEDIA_TYPE_AUDIO,
  626. .config_props = config_input,
  627. .filter_frame = filter_frame,
  628. .needs_writable = 1,
  629. },
  630. { NULL }
  631. };
  632. AVFilter ff_af_anequalizer = {
  633. .name = "anequalizer",
  634. .description = NULL_IF_CONFIG_SMALL("Apply high-order audio parametric multi band equalizer."),
  635. .priv_size = sizeof(AudioNEqualizerContext),
  636. .priv_class = &anequalizer_class,
  637. .init = init,
  638. .uninit = uninit,
  639. .query_formats = query_formats,
  640. .inputs = inputs,
  641. .outputs = NULL,
  642. .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  643. .process_command = process_command,
  644. };