af_aemphasis.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * Copyright (c) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen, Damien Zammit and others
  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/opt.h"
  21. #include "avfilter.h"
  22. #include "internal.h"
  23. #include "audio.h"
  24. typedef struct BiquadCoeffs {
  25. double a0, a1, a2, b1, b2;
  26. } BiquadCoeffs;
  27. typedef struct BiquadD2 {
  28. double a0, a1, a2, b1, b2, w1, w2;
  29. } BiquadD2;
  30. typedef struct RIAACurve {
  31. BiquadD2 r1;
  32. BiquadD2 brickw;
  33. int use_brickw;
  34. } RIAACurve;
  35. typedef struct AudioEmphasisContext {
  36. const AVClass *class;
  37. int mode, type;
  38. double level_in, level_out;
  39. RIAACurve *rc;
  40. } AudioEmphasisContext;
  41. #define OFFSET(x) offsetof(AudioEmphasisContext, x)
  42. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  43. static const AVOption aemphasis_options[] = {
  44. { "level_in", "set input gain", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 64, FLAGS },
  45. { "level_out", "set output gain", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 64, FLAGS },
  46. { "mode", "set filter mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "mode" },
  47. { "reproduction", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode" },
  48. { "production", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode" },
  49. { "type", "set filter type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=4}, 0, 8, FLAGS, "type" },
  50. { "col", "Columbia", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "type" },
  51. { "emi", "EMI", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "type" },
  52. { "bsi", "BSI (78RPM)", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "type" },
  53. { "riaa", "RIAA", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, FLAGS, "type" },
  54. { "cd", "Compact Disc (CD)", 0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, FLAGS, "type" },
  55. { "50fm", "50µs (FM)", 0, AV_OPT_TYPE_CONST, {.i64=5}, 0, 0, FLAGS, "type" },
  56. { "75fm", "75µs (FM)", 0, AV_OPT_TYPE_CONST, {.i64=6}, 0, 0, FLAGS, "type" },
  57. { "50kf", "50µs (FM-KF)", 0, AV_OPT_TYPE_CONST, {.i64=7}, 0, 0, FLAGS, "type" },
  58. { "75kf", "75µs (FM-KF)", 0, AV_OPT_TYPE_CONST, {.i64=8}, 0, 0, FLAGS, "type" },
  59. { NULL }
  60. };
  61. AVFILTER_DEFINE_CLASS(aemphasis);
  62. static inline double biquad(BiquadD2 *bq, double in)
  63. {
  64. double n = in;
  65. double tmp = n - bq->w1 * bq->b1 - bq->w2 * bq->b2;
  66. double out = tmp * bq->a0 + bq->w1 * bq->a1 + bq->w2 * bq->a2;
  67. bq->w2 = bq->w1;
  68. bq->w1 = tmp;
  69. return out;
  70. }
  71. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  72. {
  73. AVFilterContext *ctx = inlink->dst;
  74. AVFilterLink *outlink = ctx->outputs[0];
  75. AudioEmphasisContext *s = ctx->priv;
  76. const double *src = (const double *)in->data[0];
  77. const double level_out = s->level_out;
  78. const double level_in = s->level_in;
  79. AVFrame *out;
  80. double *dst;
  81. int n, c;
  82. if (av_frame_is_writable(in)) {
  83. out = in;
  84. } else {
  85. out = ff_get_audio_buffer(inlink, in->nb_samples);
  86. if (!out) {
  87. av_frame_free(&in);
  88. return AVERROR(ENOMEM);
  89. }
  90. av_frame_copy_props(out, in);
  91. }
  92. dst = (double *)out->data[0];
  93. for (n = 0; n < in->nb_samples; n++) {
  94. for (c = 0; c < inlink->channels; c++)
  95. dst[c] = level_out * biquad(&s->rc[c].r1, s->rc[c].use_brickw ? biquad(&s->rc[c].brickw, src[c] * level_in) : src[c] * level_in);
  96. dst += inlink->channels;
  97. src += inlink->channels;
  98. }
  99. if (in != out)
  100. av_frame_free(&in);
  101. return ff_filter_frame(outlink, out);
  102. }
  103. static int query_formats(AVFilterContext *ctx)
  104. {
  105. AVFilterChannelLayouts *layouts;
  106. AVFilterFormats *formats;
  107. static const enum AVSampleFormat sample_fmts[] = {
  108. AV_SAMPLE_FMT_DBL,
  109. AV_SAMPLE_FMT_NONE
  110. };
  111. int ret;
  112. layouts = ff_all_channel_counts();
  113. if (!layouts)
  114. return AVERROR(ENOMEM);
  115. ret = ff_set_common_channel_layouts(ctx, layouts);
  116. if (ret < 0)
  117. return ret;
  118. formats = ff_make_format_list(sample_fmts);
  119. if (!formats)
  120. return AVERROR(ENOMEM);
  121. ret = ff_set_common_formats(ctx, formats);
  122. if (ret < 0)
  123. return ret;
  124. formats = ff_all_samplerates();
  125. if (!formats)
  126. return AVERROR(ENOMEM);
  127. return ff_set_common_samplerates(ctx, formats);
  128. }
  129. static inline void set_highshelf_rbj(BiquadD2 *bq, double freq, double q, double peak, double sr)
  130. {
  131. double A = sqrt(peak);
  132. double w0 = freq * 2 * M_PI / sr;
  133. double alpha = sin(w0) / (2 * q);
  134. double cw0 = cos(w0);
  135. double tmp = 2 * sqrt(A) * alpha;
  136. double b0 = 0, ib0 = 0;
  137. bq->a0 = A*( (A+1) + (A-1)*cw0 + tmp);
  138. bq->a1 = -2*A*( (A-1) + (A+1)*cw0);
  139. bq->a2 = A*( (A+1) + (A-1)*cw0 - tmp);
  140. b0 = (A+1) - (A-1)*cw0 + tmp;
  141. bq->b1 = 2*( (A-1) - (A+1)*cw0);
  142. bq->b2 = (A+1) - (A-1)*cw0 - tmp;
  143. ib0 = 1 / b0;
  144. bq->b1 *= ib0;
  145. bq->b2 *= ib0;
  146. bq->a0 *= ib0;
  147. bq->a1 *= ib0;
  148. bq->a2 *= ib0;
  149. }
  150. static inline void set_lp_rbj(BiquadD2 *bq, double fc, double q, double sr, double gain)
  151. {
  152. double omega = 2.0 * M_PI * fc / sr;
  153. double sn = sin(omega);
  154. double cs = cos(omega);
  155. double alpha = sn/(2 * q);
  156. double inv = 1.0/(1.0 + alpha);
  157. bq->a2 = bq->a0 = gain * inv * (1.0 - cs) * 0.5;
  158. bq->a1 = bq->a0 + bq->a0;
  159. bq->b1 = (-2.0 * cs * inv);
  160. bq->b2 = ((1.0 - alpha) * inv);
  161. }
  162. static double freq_gain(BiquadCoeffs *c, double freq, double sr)
  163. {
  164. double zr, zi;
  165. freq *= 2.0 * M_PI / sr;
  166. zr = cos(freq);
  167. zi = -sin(freq);
  168. /* |(a0 + a1*z + a2*z^2)/(1 + b1*z + b2*z^2)| */
  169. return hypot(c->a0 + c->a1*zr + c->a2*(zr*zr-zi*zi), c->a1*zi + 2*c->a2*zr*zi) /
  170. hypot(1 + c->b1*zr + c->b2*(zr*zr-zi*zi), c->b1*zi + 2*c->b2*zr*zi);
  171. }
  172. static int config_input(AVFilterLink *inlink)
  173. {
  174. double i, j, k, g, t, a0, a1, a2, b1, b2, tau1, tau2, tau3;
  175. double cutfreq, gain1kHz, gc, sr = inlink->sample_rate;
  176. AVFilterContext *ctx = inlink->dst;
  177. AudioEmphasisContext *s = ctx->priv;
  178. BiquadCoeffs coeffs;
  179. int ch;
  180. s->rc = av_calloc(inlink->channels, sizeof(*s->rc));
  181. if (!s->rc)
  182. return AVERROR(ENOMEM);
  183. switch (s->type) {
  184. case 0: //"Columbia"
  185. i = 100.;
  186. j = 500.;
  187. k = 1590.;
  188. break;
  189. case 1: //"EMI"
  190. i = 70.;
  191. j = 500.;
  192. k = 2500.;
  193. break;
  194. case 2: //"BSI(78rpm)"
  195. i = 50.;
  196. j = 353.;
  197. k = 3180.;
  198. break;
  199. case 3: //"RIAA"
  200. default:
  201. tau1 = 0.003180;
  202. tau2 = 0.000318;
  203. tau3 = 0.000075;
  204. i = 1. / (2. * M_PI * tau1);
  205. j = 1. / (2. * M_PI * tau2);
  206. k = 1. / (2. * M_PI * tau3);
  207. break;
  208. case 4: //"CD Mastering"
  209. tau1 = 0.000050;
  210. tau2 = 0.000015;
  211. tau3 = 0.0000001;// 1.6MHz out of audible range for null impact
  212. i = 1. / (2. * M_PI * tau1);
  213. j = 1. / (2. * M_PI * tau2);
  214. k = 1. / (2. * M_PI * tau3);
  215. break;
  216. case 5: //"50µs FM (Europe)"
  217. tau1 = 0.000050;
  218. tau2 = tau1 / 20;// not used
  219. tau3 = tau1 / 50;//
  220. i = 1. / (2. * M_PI * tau1);
  221. j = 1. / (2. * M_PI * tau2);
  222. k = 1. / (2. * M_PI * tau3);
  223. break;
  224. case 6: //"75µs FM (US)"
  225. tau1 = 0.000075;
  226. tau2 = tau1 / 20;// not used
  227. tau3 = tau1 / 50;//
  228. i = 1. / (2. * M_PI * tau1);
  229. j = 1. / (2. * M_PI * tau2);
  230. k = 1. / (2. * M_PI * tau3);
  231. break;
  232. }
  233. i *= 2 * M_PI;
  234. j *= 2 * M_PI;
  235. k *= 2 * M_PI;
  236. t = 1. / sr;
  237. //swap a1 b1, a2 b2
  238. if (s->type == 7 || s->type == 8) {
  239. double tau = (s->type == 7 ? 0.000050 : 0.000075);
  240. double f = 1.0 / (2 * M_PI * tau);
  241. double nyq = sr * 0.5;
  242. double gain = sqrt(1.0 + nyq * nyq / (f * f)); // gain at Nyquist
  243. double cfreq = sqrt((gain - 1.0) * f * f); // frequency
  244. double q = 1.0;
  245. if (s->type == 8)
  246. q = pow((sr / 3269.0) + 19.5, -0.25); // somewhat poor curve-fit
  247. if (s->type == 7)
  248. q = pow((sr / 4750.0) + 19.5, -0.25);
  249. if (s->mode == 0)
  250. set_highshelf_rbj(&s->rc[0].r1, cfreq, q, 1. / gain, sr);
  251. else
  252. set_highshelf_rbj(&s->rc[0].r1, cfreq, q, gain, sr);
  253. s->rc[0].use_brickw = 0;
  254. } else {
  255. s->rc[0].use_brickw = 1;
  256. if (s->mode == 0) { // Reproduction
  257. g = 1. / (4.+2.*i*t+2.*k*t+i*k*t*t);
  258. a0 = (2.*t+j*t*t)*g;
  259. a1 = (2.*j*t*t)*g;
  260. a2 = (-2.*t+j*t*t)*g;
  261. b1 = (-8.+2.*i*k*t*t)*g;
  262. b2 = (4.-2.*i*t-2.*k*t+i*k*t*t)*g;
  263. } else { // Production
  264. g = 1. / (2.*t+j*t*t);
  265. a0 = (4.+2.*i*t+2.*k*t+i*k*t*t)*g;
  266. a1 = (-8.+2.*i*k*t*t)*g;
  267. a2 = (4.-2.*i*t-2.*k*t+i*k*t*t)*g;
  268. b1 = (2.*j*t*t)*g;
  269. b2 = (-2.*t+j*t*t)*g;
  270. }
  271. coeffs.a0 = a0;
  272. coeffs.a1 = a1;
  273. coeffs.a2 = a2;
  274. coeffs.b1 = b1;
  275. coeffs.b2 = b2;
  276. // the coeffs above give non-normalized value, so it should be normalized to produce 0dB at 1 kHz
  277. // find actual gain
  278. // Note: for FM emphasis, use 100 Hz for normalization instead
  279. gain1kHz = freq_gain(&coeffs, 1000.0, sr);
  280. // divide one filter's x[n-m] coefficients by that value
  281. gc = 1.0 / gain1kHz;
  282. s->rc[0].r1.a0 = coeffs.a0 * gc;
  283. s->rc[0].r1.a1 = coeffs.a1 * gc;
  284. s->rc[0].r1.a2 = coeffs.a2 * gc;
  285. s->rc[0].r1.b1 = coeffs.b1;
  286. s->rc[0].r1.b2 = coeffs.b2;
  287. }
  288. cutfreq = FFMIN(0.45 * sr, 21000.);
  289. set_lp_rbj(&s->rc[0].brickw, cutfreq, 0.707, sr, 1.);
  290. for (ch = 1; ch < inlink->channels; ch++) {
  291. memcpy(&s->rc[ch], &s->rc[0], sizeof(RIAACurve));
  292. }
  293. return 0;
  294. }
  295. static av_cold void uninit(AVFilterContext *ctx)
  296. {
  297. AudioEmphasisContext *s = ctx->priv;
  298. av_freep(&s->rc);
  299. }
  300. static const AVFilterPad avfilter_af_aemphasis_inputs[] = {
  301. {
  302. .name = "default",
  303. .type = AVMEDIA_TYPE_AUDIO,
  304. .config_props = config_input,
  305. .filter_frame = filter_frame,
  306. },
  307. { NULL }
  308. };
  309. static const AVFilterPad avfilter_af_aemphasis_outputs[] = {
  310. {
  311. .name = "default",
  312. .type = AVMEDIA_TYPE_AUDIO,
  313. },
  314. { NULL }
  315. };
  316. AVFilter ff_af_aemphasis = {
  317. .name = "aemphasis",
  318. .description = NULL_IF_CONFIG_SMALL("Audio emphasis."),
  319. .priv_size = sizeof(AudioEmphasisContext),
  320. .priv_class = &aemphasis_class,
  321. .uninit = uninit,
  322. .query_formats = query_formats,
  323. .inputs = avfilter_af_aemphasis_inputs,
  324. .outputs = avfilter_af_aemphasis_outputs,
  325. };