af_mcompand.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /*
  2. * COpyright (c) 2002 Daniel Pouzzner
  3. * Copyright (c) 1999 Chris Bagwell
  4. * Copyright (c) 1999 Nick Bailey
  5. * Copyright (c) 2007 Rob Sykes <robs@users.sourceforge.net>
  6. * Copyright (c) 2013 Paul B Mahol
  7. * Copyright (c) 2014 Andrew Kelley
  8. *
  9. * This file is part of FFmpeg.
  10. *
  11. * FFmpeg is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * FFmpeg is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with FFmpeg; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. /**
  26. * @file
  27. * audio multiband compand filter
  28. */
  29. #include "libavutil/avassert.h"
  30. #include "libavutil/avstring.h"
  31. #include "libavutil/ffmath.h"
  32. #include "libavutil/opt.h"
  33. #include "libavutil/samplefmt.h"
  34. #include "audio.h"
  35. #include "avfilter.h"
  36. #include "internal.h"
  37. typedef struct CompandSegment {
  38. double x, y;
  39. double a, b;
  40. } CompandSegment;
  41. typedef struct CompandT {
  42. CompandSegment *segments;
  43. int nb_segments;
  44. double in_min_lin;
  45. double out_min_lin;
  46. double curve_dB;
  47. double gain_dB;
  48. } CompandT;
  49. #define N 4
  50. typedef struct PrevCrossover {
  51. double in;
  52. double out_low;
  53. double out_high;
  54. } PrevCrossover[N * 2];
  55. typedef struct Crossover {
  56. PrevCrossover *previous;
  57. size_t pos;
  58. double coefs[3 *(N+1)];
  59. } Crossover;
  60. typedef struct CompBand {
  61. CompandT transfer_fn;
  62. double *attack_rate;
  63. double *decay_rate;
  64. double *volume;
  65. double delay;
  66. double topfreq;
  67. Crossover filter;
  68. AVFrame *delay_buf;
  69. size_t delay_size;
  70. ptrdiff_t delay_buf_ptr;
  71. size_t delay_buf_cnt;
  72. } CompBand;
  73. typedef struct MCompandContext {
  74. const AVClass *class;
  75. char *args;
  76. int nb_bands;
  77. CompBand *bands;
  78. AVFrame *band_buf1, *band_buf2, *band_buf3;
  79. int band_samples;
  80. size_t delay_buf_size;
  81. } MCompandContext;
  82. #define OFFSET(x) offsetof(MCompandContext, x)
  83. #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  84. static const AVOption mcompand_options[] = {
  85. { "args", "set parameters for each band", OFFSET(args), AV_OPT_TYPE_STRING, { .str = "0.005,0.1 6 -47/-40,-34/-34,-17/-33 100 | 0.003,0.05 6 -47/-40,-34/-34,-17/-33 400 | 0.000625,0.0125 6 -47/-40,-34/-34,-15/-33 1600 | 0.0001,0.025 6 -47/-40,-34/-34,-31/-31,-0/-30 6400 | 0,0.025 6 -38/-31,-28/-28,-0/-25 22000" }, 0, 0, A },
  86. { NULL }
  87. };
  88. AVFILTER_DEFINE_CLASS(mcompand);
  89. static av_cold void uninit(AVFilterContext *ctx)
  90. {
  91. MCompandContext *s = ctx->priv;
  92. int i;
  93. av_frame_free(&s->band_buf1);
  94. av_frame_free(&s->band_buf2);
  95. av_frame_free(&s->band_buf3);
  96. if (s->bands) {
  97. for (i = 0; i < s->nb_bands; i++) {
  98. av_freep(&s->bands[i].attack_rate);
  99. av_freep(&s->bands[i].decay_rate);
  100. av_freep(&s->bands[i].volume);
  101. av_freep(&s->bands[i].transfer_fn.segments);
  102. av_freep(&s->bands[i].filter.previous);
  103. av_frame_free(&s->bands[i].delay_buf);
  104. }
  105. }
  106. av_freep(&s->bands);
  107. }
  108. static int query_formats(AVFilterContext *ctx)
  109. {
  110. AVFilterChannelLayouts *layouts;
  111. AVFilterFormats *formats;
  112. static const enum AVSampleFormat sample_fmts[] = {
  113. AV_SAMPLE_FMT_DBLP,
  114. AV_SAMPLE_FMT_NONE
  115. };
  116. int ret;
  117. layouts = ff_all_channel_counts();
  118. if (!layouts)
  119. return AVERROR(ENOMEM);
  120. ret = ff_set_common_channel_layouts(ctx, layouts);
  121. if (ret < 0)
  122. return ret;
  123. formats = ff_make_format_list(sample_fmts);
  124. if (!formats)
  125. return AVERROR(ENOMEM);
  126. ret = ff_set_common_formats(ctx, formats);
  127. if (ret < 0)
  128. return ret;
  129. formats = ff_all_samplerates();
  130. if (!formats)
  131. return AVERROR(ENOMEM);
  132. return ff_set_common_samplerates(ctx, formats);
  133. }
  134. static void count_items(char *item_str, int *nb_items, char delimiter)
  135. {
  136. char *p;
  137. *nb_items = 1;
  138. for (p = item_str; *p; p++) {
  139. if (*p == delimiter)
  140. (*nb_items)++;
  141. }
  142. }
  143. static void update_volume(CompBand *cb, double in, int ch)
  144. {
  145. double delta = in - cb->volume[ch];
  146. if (delta > 0.0)
  147. cb->volume[ch] += delta * cb->attack_rate[ch];
  148. else
  149. cb->volume[ch] += delta * cb->decay_rate[ch];
  150. }
  151. static double get_volume(CompandT *s, double in_lin)
  152. {
  153. CompandSegment *cs;
  154. double in_log, out_log;
  155. int i;
  156. if (in_lin <= s->in_min_lin)
  157. return s->out_min_lin;
  158. in_log = log(in_lin);
  159. for (i = 1; i < s->nb_segments; i++)
  160. if (in_log <= s->segments[i].x)
  161. break;
  162. cs = &s->segments[i - 1];
  163. in_log -= cs->x;
  164. out_log = cs->y + in_log * (cs->a * in_log + cs->b);
  165. return exp(out_log);
  166. }
  167. static int parse_points(char *points, int nb_points, double radius,
  168. CompandT *s, AVFilterContext *ctx)
  169. {
  170. int new_nb_items, num;
  171. char *saveptr = NULL;
  172. char *p = points;
  173. int i;
  174. #define S(x) s->segments[2 * ((x) + 1)]
  175. for (i = 0, new_nb_items = 0; i < nb_points; i++) {
  176. char *tstr = av_strtok(p, ",", &saveptr);
  177. p = NULL;
  178. if (!tstr || sscanf(tstr, "%lf/%lf", &S(i).x, &S(i).y) != 2) {
  179. av_log(ctx, AV_LOG_ERROR,
  180. "Invalid and/or missing input/output value.\n");
  181. return AVERROR(EINVAL);
  182. }
  183. if (i && S(i - 1).x > S(i).x) {
  184. av_log(ctx, AV_LOG_ERROR,
  185. "Transfer function input values must be increasing.\n");
  186. return AVERROR(EINVAL);
  187. }
  188. S(i).y -= S(i).x;
  189. av_log(ctx, AV_LOG_DEBUG, "%d: x=%f y=%f\n", i, S(i).x, S(i).y);
  190. new_nb_items++;
  191. }
  192. num = new_nb_items;
  193. /* Add 0,0 if necessary */
  194. if (num == 0 || S(num - 1).x)
  195. num++;
  196. #undef S
  197. #define S(x) s->segments[2 * (x)]
  198. /* Add a tail off segment at the start */
  199. S(0).x = S(1).x - 2 * s->curve_dB;
  200. S(0).y = S(1).y;
  201. num++;
  202. /* Join adjacent colinear segments */
  203. for (i = 2; i < num; i++) {
  204. double g1 = (S(i - 1).y - S(i - 2).y) * (S(i - 0).x - S(i - 1).x);
  205. double g2 = (S(i - 0).y - S(i - 1).y) * (S(i - 1).x - S(i - 2).x);
  206. int j;
  207. if (fabs(g1 - g2))
  208. continue;
  209. num--;
  210. for (j = --i; j < num; j++)
  211. S(j) = S(j + 1);
  212. }
  213. for (i = 0; i < s->nb_segments; i += 2) {
  214. s->segments[i].y += s->gain_dB;
  215. s->segments[i].x *= M_LN10 / 20;
  216. s->segments[i].y *= M_LN10 / 20;
  217. }
  218. #define L(x) s->segments[i - (x)]
  219. for (i = 4; i < s->nb_segments; i += 2) {
  220. double x, y, cx, cy, in1, in2, out1, out2, theta, len, r;
  221. L(4).a = 0;
  222. L(4).b = (L(2).y - L(4).y) / (L(2).x - L(4).x);
  223. L(2).a = 0;
  224. L(2).b = (L(0).y - L(2).y) / (L(0).x - L(2).x);
  225. theta = atan2(L(2).y - L(4).y, L(2).x - L(4).x);
  226. len = hypot(L(2).x - L(4).x, L(2).y - L(4).y);
  227. r = FFMIN(radius, len);
  228. L(3).x = L(2).x - r * cos(theta);
  229. L(3).y = L(2).y - r * sin(theta);
  230. theta = atan2(L(0).y - L(2).y, L(0).x - L(2).x);
  231. len = hypot(L(0).x - L(2).x, L(0).y - L(2).y);
  232. r = FFMIN(radius, len / 2);
  233. x = L(2).x + r * cos(theta);
  234. y = L(2).y + r * sin(theta);
  235. cx = (L(3).x + L(2).x + x) / 3;
  236. cy = (L(3).y + L(2).y + y) / 3;
  237. L(2).x = x;
  238. L(2).y = y;
  239. in1 = cx - L(3).x;
  240. out1 = cy - L(3).y;
  241. in2 = L(2).x - L(3).x;
  242. out2 = L(2).y - L(3).y;
  243. L(3).a = (out2 / in2 - out1 / in1) / (in2 - in1);
  244. L(3).b = out1 / in1 - L(3).a * in1;
  245. }
  246. L(3).x = 0;
  247. L(3).y = L(2).y;
  248. s->in_min_lin = exp(s->segments[1].x);
  249. s->out_min_lin = exp(s->segments[1].y);
  250. return 0;
  251. }
  252. static void square_quadratic(double const *x, double *y)
  253. {
  254. y[0] = x[0] * x[0];
  255. y[1] = 2 * x[0] * x[1];
  256. y[2] = 2 * x[0] * x[2] + x[1] * x[1];
  257. y[3] = 2 * x[1] * x[2];
  258. y[4] = x[2] * x[2];
  259. }
  260. static int crossover_setup(AVFilterLink *outlink, Crossover *p, double frequency)
  261. {
  262. double w0 = 2 * M_PI * frequency / outlink->sample_rate;
  263. double Q = sqrt(.5), alpha = sin(w0) / (2*Q);
  264. double x[9], norm;
  265. int i;
  266. if (w0 > M_PI)
  267. return AVERROR(EINVAL);
  268. x[0] = (1 - cos(w0))/2; /* Cf. filter_LPF in biquads.c */
  269. x[1] = 1 - cos(w0);
  270. x[2] = (1 - cos(w0))/2;
  271. x[3] = (1 + cos(w0))/2; /* Cf. filter_HPF in biquads.c */
  272. x[4] = -(1 + cos(w0));
  273. x[5] = (1 + cos(w0))/2;
  274. x[6] = 1 + alpha;
  275. x[7] = -2*cos(w0);
  276. x[8] = 1 - alpha;
  277. for (norm = x[6], i = 0; i < 9; ++i)
  278. x[i] /= norm;
  279. square_quadratic(x , p->coefs);
  280. square_quadratic(x + 3, p->coefs + 5);
  281. square_quadratic(x + 6, p->coefs + 10);
  282. p->previous = av_calloc(outlink->channels, sizeof(*p->previous));
  283. if (!p->previous)
  284. return AVERROR(ENOMEM);
  285. return 0;
  286. }
  287. static int config_output(AVFilterLink *outlink)
  288. {
  289. AVFilterContext *ctx = outlink->src;
  290. MCompandContext *s = ctx->priv;
  291. int ret, ch, i, k, new_nb_items, nb_bands;
  292. char *p = s->args, *saveptr = NULL;
  293. int max_delay_size = 0;
  294. count_items(s->args, &nb_bands, '|');
  295. s->nb_bands = FFMAX(1, nb_bands);
  296. s->bands = av_calloc(nb_bands, sizeof(*s->bands));
  297. if (!s->bands)
  298. return AVERROR(ENOMEM);
  299. for (i = 0, new_nb_items = 0; i < nb_bands; i++) {
  300. int nb_points, nb_attacks, nb_items = 0;
  301. char *tstr2, *tstr = av_strtok(p, "|", &saveptr);
  302. char *p2, *p3, *saveptr2 = NULL, *saveptr3 = NULL;
  303. double radius;
  304. if (!tstr) {
  305. uninit(ctx);
  306. return AVERROR(EINVAL);
  307. }
  308. p = NULL;
  309. p2 = tstr;
  310. count_items(tstr, &nb_items, ' ');
  311. tstr2 = av_strtok(p2, " ", &saveptr2);
  312. if (!tstr2) {
  313. av_log(ctx, AV_LOG_ERROR, "at least one attacks/decays rate is mandatory\n");
  314. uninit(ctx);
  315. return AVERROR(EINVAL);
  316. }
  317. p2 = NULL;
  318. p3 = tstr2;
  319. count_items(tstr2, &nb_attacks, ',');
  320. if (!nb_attacks || nb_attacks & 1) {
  321. av_log(ctx, AV_LOG_ERROR, "number of attacks rate plus decays rate must be even\n");
  322. uninit(ctx);
  323. return AVERROR(EINVAL);
  324. }
  325. s->bands[i].attack_rate = av_calloc(outlink->channels, sizeof(double));
  326. s->bands[i].decay_rate = av_calloc(outlink->channels, sizeof(double));
  327. s->bands[i].volume = av_calloc(outlink->channels, sizeof(double));
  328. for (k = 0; k < FFMIN(nb_attacks / 2, outlink->channels); k++) {
  329. char *tstr3 = av_strtok(p3, ",", &saveptr3);
  330. p3 = NULL;
  331. sscanf(tstr3, "%lf", &s->bands[i].attack_rate[k]);
  332. tstr3 = av_strtok(p3, ",", &saveptr3);
  333. sscanf(tstr3, "%lf", &s->bands[i].decay_rate[k]);
  334. if (s->bands[i].attack_rate[k] > 1.0 / outlink->sample_rate) {
  335. s->bands[i].attack_rate[k] = 1.0 - exp(-1.0 / (outlink->sample_rate * s->bands[i].attack_rate[k]));
  336. } else {
  337. s->bands[i].attack_rate[k] = 1.0;
  338. }
  339. if (s->bands[i].decay_rate[k] > 1.0 / outlink->sample_rate) {
  340. s->bands[i].decay_rate[k] = 1.0 - exp(-1.0 / (outlink->sample_rate * s->bands[i].decay_rate[k]));
  341. } else {
  342. s->bands[i].decay_rate[k] = 1.0;
  343. }
  344. }
  345. for (ch = k; ch < outlink->channels; ch++) {
  346. s->bands[i].attack_rate[ch] = s->bands[i].attack_rate[k - 1];
  347. s->bands[i].decay_rate[ch] = s->bands[i].decay_rate[k - 1];
  348. }
  349. tstr2 = av_strtok(p2, " ", &saveptr2);
  350. if (!tstr2) {
  351. av_log(ctx, AV_LOG_ERROR, "transfer function curve in dB must be set\n");
  352. uninit(ctx);
  353. return AVERROR(EINVAL);
  354. }
  355. sscanf(tstr2, "%lf", &s->bands[i].transfer_fn.curve_dB);
  356. radius = s->bands[i].transfer_fn.curve_dB * M_LN10 / 20.0;
  357. tstr2 = av_strtok(p2, " ", &saveptr2);
  358. if (!tstr2) {
  359. av_log(ctx, AV_LOG_ERROR, "transfer points missing\n");
  360. uninit(ctx);
  361. return AVERROR(EINVAL);
  362. }
  363. count_items(tstr2, &nb_points, ',');
  364. s->bands[i].transfer_fn.nb_segments = (nb_points + 4) * 2;
  365. s->bands[i].transfer_fn.segments = av_calloc(s->bands[i].transfer_fn.nb_segments,
  366. sizeof(CompandSegment));
  367. if (!s->bands[i].transfer_fn.segments) {
  368. uninit(ctx);
  369. return AVERROR(ENOMEM);
  370. }
  371. ret = parse_points(tstr2, nb_points, radius, &s->bands[i].transfer_fn, ctx);
  372. if (ret < 0) {
  373. av_log(ctx, AV_LOG_ERROR, "transfer points parsing failed\n");
  374. uninit(ctx);
  375. return ret;
  376. }
  377. tstr2 = av_strtok(p2, " ", &saveptr2);
  378. if (!tstr2) {
  379. av_log(ctx, AV_LOG_ERROR, "crossover_frequency is missing\n");
  380. uninit(ctx);
  381. return AVERROR(EINVAL);
  382. }
  383. new_nb_items += sscanf(tstr2, "%lf", &s->bands[i].topfreq) == 1;
  384. if (s->bands[i].topfreq < 0 || s->bands[i].topfreq >= outlink->sample_rate / 2) {
  385. av_log(ctx, AV_LOG_ERROR, "crossover_frequency: %f, should be >=0 and lower than half of sample rate: %d.\n", s->bands[i].topfreq, outlink->sample_rate / 2);
  386. uninit(ctx);
  387. return AVERROR(EINVAL);
  388. }
  389. if (s->bands[i].topfreq != 0) {
  390. ret = crossover_setup(outlink, &s->bands[i].filter, s->bands[i].topfreq);
  391. if (ret < 0) {
  392. uninit(ctx);
  393. return ret;
  394. }
  395. }
  396. tstr2 = av_strtok(p2, " ", &saveptr2);
  397. if (tstr2) {
  398. sscanf(tstr2, "%lf", &s->bands[i].delay);
  399. max_delay_size = FFMAX(max_delay_size, s->bands[i].delay * outlink->sample_rate);
  400. tstr2 = av_strtok(p2, " ", &saveptr2);
  401. if (tstr2) {
  402. double initial_volume;
  403. sscanf(tstr2, "%lf", &initial_volume);
  404. initial_volume = pow(10.0, initial_volume / 20);
  405. for (k = 0; k < outlink->channels; k++) {
  406. s->bands[i].volume[k] = initial_volume;
  407. }
  408. tstr2 = av_strtok(p2, " ", &saveptr2);
  409. if (tstr2) {
  410. sscanf(tstr2, "%lf", &s->bands[i].transfer_fn.gain_dB);
  411. }
  412. }
  413. }
  414. }
  415. s->nb_bands = new_nb_items;
  416. for (i = 0; max_delay_size > 0 && i < s->nb_bands; i++) {
  417. s->bands[i].delay_buf = ff_get_audio_buffer(outlink, max_delay_size);
  418. if (!s->bands[i].delay_buf)
  419. return AVERROR(ENOMEM);
  420. }
  421. s->delay_buf_size = max_delay_size;
  422. return 0;
  423. }
  424. #define CONVOLVE _ _ _ _
  425. static void crossover(int ch, Crossover *p,
  426. double *ibuf, double *obuf_low,
  427. double *obuf_high, size_t len)
  428. {
  429. double out_low, out_high;
  430. while (len--) {
  431. p->pos = p->pos ? p->pos - 1 : N - 1;
  432. #define _ out_low += p->coefs[j] * p->previous[ch][p->pos + j].in \
  433. - p->coefs[2*N+2 + j] * p->previous[ch][p->pos + j].out_low, j++;
  434. {
  435. int j = 1;
  436. out_low = p->coefs[0] * *ibuf;
  437. CONVOLVE
  438. *obuf_low++ = out_low;
  439. }
  440. #undef _
  441. #define _ out_high += p->coefs[j+N+1] * p->previous[ch][p->pos + j].in \
  442. - p->coefs[2*N+2 + j] * p->previous[ch][p->pos + j].out_high, j++;
  443. {
  444. int j = 1;
  445. out_high = p->coefs[N+1] * *ibuf;
  446. CONVOLVE
  447. *obuf_high++ = out_high;
  448. }
  449. p->previous[ch][p->pos + N].in = p->previous[ch][p->pos].in = *ibuf++;
  450. p->previous[ch][p->pos + N].out_low = p->previous[ch][p->pos].out_low = out_low;
  451. p->previous[ch][p->pos + N].out_high = p->previous[ch][p->pos].out_high = out_high;
  452. }
  453. }
  454. static int mcompand_channel(MCompandContext *c, CompBand *l, double *ibuf, double *obuf, int len, int ch)
  455. {
  456. int i;
  457. for (i = 0; i < len; i++) {
  458. double level_in_lin, level_out_lin, checkbuf;
  459. /* Maintain the volume fields by simulating a leaky pump circuit */
  460. update_volume(l, fabs(ibuf[i]), ch);
  461. /* Volume memory is updated: perform compand */
  462. level_in_lin = l->volume[ch];
  463. level_out_lin = get_volume(&l->transfer_fn, level_in_lin);
  464. if (c->delay_buf_size <= 0) {
  465. checkbuf = ibuf[i] * level_out_lin;
  466. obuf[i] = checkbuf;
  467. } else {
  468. double *delay_buf = (double *)l->delay_buf->extended_data[ch];
  469. /* FIXME: note that this lookahead algorithm is really lame:
  470. the response to a peak is released before the peak
  471. arrives. */
  472. /* because volume application delays differ band to band, but
  473. total delay doesn't, the volume is applied in an iteration
  474. preceding that in which the sample goes to obuf, except in
  475. the band(s) with the longest vol app delay.
  476. the offset between delay_buf_ptr and the sample to apply
  477. vol to, is a constant equal to the difference between this
  478. band's delay and the longest delay of all the bands. */
  479. if (l->delay_buf_cnt >= l->delay_size) {
  480. checkbuf =
  481. delay_buf[(l->delay_buf_ptr +
  482. c->delay_buf_size -
  483. l->delay_size) % c->delay_buf_size] * level_out_lin;
  484. delay_buf[(l->delay_buf_ptr + c->delay_buf_size -
  485. l->delay_size) % c->delay_buf_size] = checkbuf;
  486. }
  487. if (l->delay_buf_cnt >= c->delay_buf_size) {
  488. obuf[i] = delay_buf[l->delay_buf_ptr];
  489. } else {
  490. l->delay_buf_cnt++;
  491. }
  492. delay_buf[l->delay_buf_ptr++] = ibuf[i];
  493. l->delay_buf_ptr %= c->delay_buf_size;
  494. }
  495. }
  496. return 0;
  497. }
  498. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  499. {
  500. AVFilterContext *ctx = inlink->dst;
  501. AVFilterLink *outlink = ctx->outputs[0];
  502. MCompandContext *s = ctx->priv;
  503. AVFrame *out, *abuf, *bbuf, *cbuf;
  504. int ch, band, i;
  505. out = ff_get_audio_buffer(outlink, in->nb_samples);
  506. if (!out) {
  507. av_frame_free(&in);
  508. return AVERROR(ENOMEM);
  509. }
  510. if (s->band_samples < in->nb_samples) {
  511. av_frame_free(&s->band_buf1);
  512. av_frame_free(&s->band_buf2);
  513. av_frame_free(&s->band_buf3);
  514. s->band_buf1 = ff_get_audio_buffer(outlink, in->nb_samples);
  515. s->band_buf2 = ff_get_audio_buffer(outlink, in->nb_samples);
  516. s->band_buf3 = ff_get_audio_buffer(outlink, in->nb_samples);
  517. s->band_samples = in->nb_samples;
  518. }
  519. for (ch = 0; ch < outlink->channels; ch++) {
  520. double *a, *dst = (double *)out->extended_data[ch];
  521. for (band = 0, abuf = in, bbuf = s->band_buf2, cbuf = s->band_buf1; band < s->nb_bands; band++) {
  522. CompBand *b = &s->bands[band];
  523. if (b->topfreq) {
  524. crossover(ch, &b->filter, (double *)abuf->extended_data[ch],
  525. (double *)bbuf->extended_data[ch], (double *)cbuf->extended_data[ch], in->nb_samples);
  526. } else {
  527. bbuf = abuf;
  528. abuf = cbuf;
  529. }
  530. if (abuf == in)
  531. abuf = s->band_buf3;
  532. mcompand_channel(s, b, (double *)bbuf->extended_data[ch], (double *)abuf->extended_data[ch], out->nb_samples, ch);
  533. a = (double *)abuf->extended_data[ch];
  534. for (i = 0; i < out->nb_samples; i++) {
  535. dst[i] += a[i];
  536. }
  537. FFSWAP(AVFrame *, abuf, cbuf);
  538. }
  539. }
  540. out->pts = in->pts;
  541. av_frame_free(&in);
  542. return ff_filter_frame(outlink, out);
  543. }
  544. static int request_frame(AVFilterLink *outlink)
  545. {
  546. AVFilterContext *ctx = outlink->src;
  547. int ret;
  548. ret = ff_request_frame(ctx->inputs[0]);
  549. return ret;
  550. }
  551. static const AVFilterPad mcompand_inputs[] = {
  552. {
  553. .name = "default",
  554. .type = AVMEDIA_TYPE_AUDIO,
  555. .filter_frame = filter_frame,
  556. },
  557. { NULL }
  558. };
  559. static const AVFilterPad mcompand_outputs[] = {
  560. {
  561. .name = "default",
  562. .type = AVMEDIA_TYPE_AUDIO,
  563. .request_frame = request_frame,
  564. .config_props = config_output,
  565. },
  566. { NULL }
  567. };
  568. AVFilter ff_af_mcompand = {
  569. .name = "mcompand",
  570. .description = NULL_IF_CONFIG_SMALL(
  571. "Multiband Compress or expand audio dynamic range."),
  572. .query_formats = query_formats,
  573. .priv_size = sizeof(MCompandContext),
  574. .priv_class = &mcompand_class,
  575. .uninit = uninit,
  576. .inputs = mcompand_inputs,
  577. .outputs = mcompand_outputs,
  578. };