af_stereotools.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * Copyright (C) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/channel_layout.h"
  21. #include "libavutil/opt.h"
  22. #include "avfilter.h"
  23. #include "audio.h"
  24. #include "formats.h"
  25. typedef struct StereoToolsContext {
  26. const AVClass *class;
  27. int softclip;
  28. int mute_l;
  29. int mute_r;
  30. int phase_l;
  31. int phase_r;
  32. int mode;
  33. int bmode_in;
  34. int bmode_out;
  35. double slev;
  36. double sbal;
  37. double mlev;
  38. double mpan;
  39. double phase;
  40. double base;
  41. double delay;
  42. double balance_in;
  43. double balance_out;
  44. double phase_sin_coef;
  45. double phase_cos_coef;
  46. double sc_level;
  47. double inv_atan_shape;
  48. double level_in;
  49. double level_out;
  50. double *buffer;
  51. int length;
  52. int pos;
  53. } StereoToolsContext;
  54. #define OFFSET(x) offsetof(StereoToolsContext, x)
  55. #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  56. static const AVOption stereotools_options[] = {
  57. { "level_in", "set level in", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
  58. { "level_out", "set level out", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
  59. { "balance_in", "set balance in", OFFSET(balance_in), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, A },
  60. { "balance_out", "set balance out", OFFSET(balance_out), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, A },
  61. { "softclip", "enable softclip", OFFSET(softclip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
  62. { "mutel", "mute L", OFFSET(mute_l), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
  63. { "muter", "mute R", OFFSET(mute_r), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
  64. { "phasel", "phase L", OFFSET(phase_l), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
  65. { "phaser", "phase R", OFFSET(phase_r), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
  66. { "mode", "set stereo mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 8, A, "mode" },
  67. { "lr>lr", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A, "mode" },
  68. { "lr>ms", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A, "mode" },
  69. { "ms>lr", 0, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, A, "mode" },
  70. { "lr>ll", 0, 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, A, "mode" },
  71. { "lr>rr", 0, 0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, A, "mode" },
  72. { "lr>l+r", 0, 0, AV_OPT_TYPE_CONST, {.i64=5}, 0, 0, A, "mode" },
  73. { "lr>rl", 0, 0, AV_OPT_TYPE_CONST, {.i64=6}, 0, 0, A, "mode" },
  74. { "ms>ll", 0, 0, AV_OPT_TYPE_CONST, {.i64=7}, 0, 0, A, "mode" },
  75. { "ms>rr", 0, 0, AV_OPT_TYPE_CONST, {.i64=8}, 0, 0, A, "mode" },
  76. { "slev", "set side level", OFFSET(slev), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
  77. { "sbal", "set side balance", OFFSET(sbal), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, A },
  78. { "mlev", "set middle level", OFFSET(mlev), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
  79. { "mpan", "set middle pan", OFFSET(mpan), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, A },
  80. { "base", "set stereo base", OFFSET(base), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, A },
  81. { "delay", "set delay", OFFSET(delay), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -20, 20, A },
  82. { "sclevel", "set S/C level", OFFSET(sc_level), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 1, 100, A },
  83. { "phase", "set stereo phase", OFFSET(phase), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 360, A },
  84. { "bmode_in", "set balance in mode", OFFSET(bmode_in), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, A, "bmode" },
  85. { "balance", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A, "bmode" },
  86. { "amplitude", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A, "bmode" },
  87. { "power", 0, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, A, "bmode" },
  88. { "bmode_out", "set balance out mode", OFFSET(bmode_out), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, A, "bmode" },
  89. { NULL }
  90. };
  91. AVFILTER_DEFINE_CLASS(stereotools);
  92. static int query_formats(AVFilterContext *ctx)
  93. {
  94. AVFilterFormats *formats = NULL;
  95. AVFilterChannelLayouts *layout = NULL;
  96. int ret;
  97. if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_DBL )) < 0 ||
  98. (ret = ff_set_common_formats (ctx , formats )) < 0 ||
  99. (ret = ff_add_channel_layout (&layout , AV_CH_LAYOUT_STEREO)) < 0 ||
  100. (ret = ff_set_common_channel_layouts (ctx , layout )) < 0)
  101. return ret;
  102. formats = ff_all_samplerates();
  103. return ff_set_common_samplerates(ctx, formats);
  104. }
  105. static int config_input(AVFilterLink *inlink)
  106. {
  107. AVFilterContext *ctx = inlink->dst;
  108. StereoToolsContext *s = ctx->priv;
  109. s->length = 2 * inlink->sample_rate * 0.05;
  110. if (s->length <= 1 || s->length & 1) {
  111. av_log(ctx, AV_LOG_ERROR, "sample rate is too small\n");
  112. return AVERROR(EINVAL);
  113. }
  114. s->buffer = av_calloc(s->length, sizeof(*s->buffer));
  115. if (!s->buffer)
  116. return AVERROR(ENOMEM);
  117. s->inv_atan_shape = 1.0 / atan(s->sc_level);
  118. s->phase_cos_coef = cos(s->phase / 180 * M_PI);
  119. s->phase_sin_coef = sin(s->phase / 180 * M_PI);
  120. return 0;
  121. }
  122. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  123. {
  124. AVFilterContext *ctx = inlink->dst;
  125. AVFilterLink *outlink = ctx->outputs[0];
  126. StereoToolsContext *s = ctx->priv;
  127. const double *src = (const double *)in->data[0];
  128. const double sb = s->base < 0 ? s->base * 0.5 : s->base;
  129. const double sbal = 1 + s->sbal;
  130. const double mpan = 1 + s->mpan;
  131. const double slev = s->slev;
  132. const double mlev = s->mlev;
  133. const double balance_in = s->balance_in;
  134. const double balance_out = s->balance_out;
  135. const double level_in = s->level_in;
  136. const double level_out = s->level_out;
  137. const double sc_level = s->sc_level;
  138. const double delay = s->delay;
  139. const int length = s->length;
  140. const int mute_l = s->mute_l;
  141. const int mute_r = s->mute_r;
  142. const int phase_l = s->phase_l;
  143. const int phase_r = s->phase_r;
  144. double *buffer = s->buffer;
  145. AVFrame *out;
  146. double *dst;
  147. int nbuf = inlink->sample_rate * (fabs(delay) / 1000.);
  148. int n;
  149. nbuf -= nbuf % 2;
  150. if (av_frame_is_writable(in)) {
  151. out = in;
  152. } else {
  153. out = ff_get_audio_buffer(outlink, in->nb_samples);
  154. if (!out) {
  155. av_frame_free(&in);
  156. return AVERROR(ENOMEM);
  157. }
  158. av_frame_copy_props(out, in);
  159. }
  160. dst = (double *)out->data[0];
  161. for (n = 0; n < in->nb_samples; n++, src += 2, dst += 2) {
  162. double L = src[0], R = src[1], l, r, m, S, gl, gr, gd;
  163. L *= level_in;
  164. R *= level_in;
  165. gl = 1. - FFMAX(0., balance_in);
  166. gr = 1. + FFMIN(0., balance_in);
  167. switch (s->bmode_in) {
  168. case 1:
  169. gd = gl - gr;
  170. gl = 1. + gd;
  171. gr = 1. - gd;
  172. break;
  173. case 2:
  174. if (balance_in < 0.) {
  175. gr = FFMAX(0.5, gr);
  176. gl = 1. / gr;
  177. } else if (balance_in > 0.) {
  178. gl = FFMAX(0.5, gl);
  179. gr = 1. / gl;
  180. }
  181. break;
  182. }
  183. L *= gl;
  184. R *= gr;
  185. if (s->softclip) {
  186. R = s->inv_atan_shape * atan(R * sc_level);
  187. L = s->inv_atan_shape * atan(L * sc_level);
  188. }
  189. switch (s->mode) {
  190. case 0:
  191. m = (L + R) * 0.5;
  192. S = (L - R) * 0.5;
  193. l = m * mlev * FFMIN(1., 2. - mpan) + S * slev * FFMIN(1., 2. - sbal);
  194. r = m * mlev * FFMIN(1., mpan) - S * slev * FFMIN(1., sbal);
  195. L = l;
  196. R = r;
  197. break;
  198. case 1:
  199. l = L * FFMIN(1., 2. - sbal);
  200. r = R * FFMIN(1., sbal);
  201. L = 0.5 * (l + r) * mlev;
  202. R = 0.5 * (l - r) * slev;
  203. break;
  204. case 2:
  205. l = L * mlev * FFMIN(1., 2. - mpan) + R * slev * FFMIN(1., 2. - sbal);
  206. r = L * mlev * FFMIN(1., mpan) - R * slev * FFMIN(1., sbal);
  207. L = l;
  208. R = r;
  209. break;
  210. case 3:
  211. R = L;
  212. break;
  213. case 4:
  214. L = R;
  215. break;
  216. case 5:
  217. L = (L + R) / 2;
  218. R = L;
  219. break;
  220. case 6:
  221. l = L;
  222. L = R;
  223. R = l;
  224. m = (L + R) * 0.5;
  225. S = (L - R) * 0.5;
  226. l = m * mlev * FFMIN(1., 2. - mpan) + S * slev * FFMIN(1., 2. - sbal);
  227. r = m * mlev * FFMIN(1., mpan) - S * slev * FFMIN(1., sbal);
  228. L = l;
  229. R = r;
  230. break;
  231. case 7:
  232. l = L * mlev * FFMIN(1., 2. - mpan) + R * slev * FFMIN(1., 2. - sbal);
  233. L = l;
  234. R = l;
  235. break;
  236. case 8:
  237. r = L * mlev * FFMIN(1., mpan) - R * slev * FFMIN(1., sbal);
  238. L = r;
  239. R = r;
  240. break;
  241. }
  242. L *= 1. - mute_l;
  243. R *= 1. - mute_r;
  244. L *= (2. * (1. - phase_l)) - 1.;
  245. R *= (2. * (1. - phase_r)) - 1.;
  246. buffer[s->pos ] = L;
  247. buffer[s->pos+1] = R;
  248. if (delay > 0.) {
  249. R = buffer[(s->pos - (int)nbuf + 1 + length) % length];
  250. } else if (delay < 0.) {
  251. L = buffer[(s->pos - (int)nbuf + length) % length];
  252. }
  253. l = L + sb * L - sb * R;
  254. r = R + sb * R - sb * L;
  255. L = l;
  256. R = r;
  257. l = L * s->phase_cos_coef - R * s->phase_sin_coef;
  258. r = L * s->phase_sin_coef + R * s->phase_cos_coef;
  259. L = l;
  260. R = r;
  261. s->pos = (s->pos + 2) % s->length;
  262. gl = 1. - FFMAX(0., balance_out);
  263. gr = 1. + FFMIN(0., balance_out);
  264. switch (s->bmode_out) {
  265. case 1:
  266. gd = gl - gr;
  267. gl = 1. + gd;
  268. gr = 1. - gd;
  269. break;
  270. case 2:
  271. if (balance_out < 0.) {
  272. gr = FFMAX(0.5, gr);
  273. gl = 1. / gr;
  274. } else if (balance_out > 0.) {
  275. gl = FFMAX(0.5, gl);
  276. gr = 1. / gl;
  277. }
  278. break;
  279. }
  280. L *= gl;
  281. R *= gr;
  282. L *= level_out;
  283. R *= level_out;
  284. dst[0] = L;
  285. dst[1] = R;
  286. }
  287. if (out != in)
  288. av_frame_free(&in);
  289. return ff_filter_frame(outlink, out);
  290. }
  291. static av_cold void uninit(AVFilterContext *ctx)
  292. {
  293. StereoToolsContext *s = ctx->priv;
  294. av_freep(&s->buffer);
  295. }
  296. static const AVFilterPad inputs[] = {
  297. {
  298. .name = "default",
  299. .type = AVMEDIA_TYPE_AUDIO,
  300. .filter_frame = filter_frame,
  301. .config_props = config_input,
  302. },
  303. { NULL }
  304. };
  305. static const AVFilterPad outputs[] = {
  306. {
  307. .name = "default",
  308. .type = AVMEDIA_TYPE_AUDIO,
  309. },
  310. { NULL }
  311. };
  312. AVFilter ff_af_stereotools = {
  313. .name = "stereotools",
  314. .description = NULL_IF_CONFIG_SMALL("Apply various stereo tools."),
  315. .query_formats = query_formats,
  316. .priv_size = sizeof(StereoToolsContext),
  317. .priv_class = &stereotools_class,
  318. .uninit = uninit,
  319. .inputs = inputs,
  320. .outputs = outputs,
  321. };