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.

371 lines
11KB

  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 <complex.h>
  21. #include "libavutil/opt.h"
  22. #include "avfilter.h"
  23. #include "internal.h"
  24. #include "audio.h"
  25. typedef struct BiquadCoeffs {
  26. double a0, a1, a2, b1, b2;
  27. } BiquadCoeffs;
  28. typedef struct BiquadD2 {
  29. double a0, a1, a2, b1, b2, w1, w2;
  30. } BiquadD2;
  31. typedef struct RIAACurve {
  32. BiquadD2 r1;
  33. BiquadD2 brickw;
  34. int use_brickw;
  35. } RIAACurve;
  36. typedef struct AudioEmphasisContext {
  37. const AVClass *class;
  38. int mode, type;
  39. double level_in, level_out;
  40. RIAACurve *rc;
  41. } AudioEmphasisContext;
  42. #define OFFSET(x) offsetof(AudioEmphasisContext, x)
  43. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  44. static const AVOption aemphasis_options[] = {
  45. { "level_in", "set input gain", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 64, FLAGS },
  46. { "level_out", "set output gain", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 64, FLAGS },
  47. { "mode", "set filter mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "mode" },
  48. { "reproduction", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode" },
  49. { "production", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode" },
  50. { "type", "set filter type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=4}, 0, 8, FLAGS, "type" },
  51. { "col", "Columbia", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "type" },
  52. { "emi", "EMI", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "type" },
  53. { "bsi", "BSI (78RPM)", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "type" },
  54. { "riaa", "RIAA", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, FLAGS, "type" },
  55. { "cd", "Compact Disc (CD)", 0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, FLAGS, "type" },
  56. { "50fm", "50µs (FM)", 0, AV_OPT_TYPE_CONST, {.i64=5}, 0, 0, FLAGS, "type" },
  57. { "75fm", "75µs (FM)", 0, AV_OPT_TYPE_CONST, {.i64=6}, 0, 0, FLAGS, "type" },
  58. { "50kf", "50µs (FM-KF)", 0, AV_OPT_TYPE_CONST, {.i64=7}, 0, 0, FLAGS, "type" },
  59. { "75kf", "75µs (FM-KF)", 0, AV_OPT_TYPE_CONST, {.i64=8}, 0, 0, FLAGS, "type" },
  60. { NULL }
  61. };
  62. AVFILTER_DEFINE_CLASS(aemphasis);
  63. static inline double biquad(BiquadD2 *bq, double in)
  64. {
  65. double n = in;
  66. double tmp = n - bq->w1 * bq->b1 - bq->w2 * bq->b2;
  67. double out = tmp * bq->a0 + bq->w1 * bq->a1 + bq->w2 * bq->a2;
  68. bq->w2 = bq->w1;
  69. bq->w1 = tmp;
  70. return out;
  71. }
  72. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  73. {
  74. AVFilterContext *ctx = inlink->dst;
  75. AVFilterLink *outlink = ctx->outputs[0];
  76. AudioEmphasisContext *s = ctx->priv;
  77. const double *src = (const double *)in->data[0];
  78. const double level_out = s->level_out;
  79. const double level_in = s->level_in;
  80. AVFrame *out;
  81. double *dst;
  82. int n, c;
  83. if (av_frame_is_writable(in)) {
  84. out = in;
  85. } else {
  86. out = ff_get_audio_buffer(inlink, in->nb_samples);
  87. if (!out) {
  88. av_frame_free(&in);
  89. return AVERROR(ENOMEM);
  90. }
  91. av_frame_copy_props(out, in);
  92. }
  93. dst = (double *)out->data[0];
  94. for (n = 0; n < in->nb_samples; n++) {
  95. for (c = 0; c < inlink->channels; c++)
  96. 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);
  97. dst += inlink->channels;
  98. src += inlink->channels;
  99. }
  100. if (in != out)
  101. av_frame_free(&in);
  102. return ff_filter_frame(outlink, out);
  103. }
  104. static int query_formats(AVFilterContext *ctx)
  105. {
  106. AVFilterChannelLayouts *layouts;
  107. AVFilterFormats *formats;
  108. static const enum AVSampleFormat sample_fmts[] = {
  109. AV_SAMPLE_FMT_DBL,
  110. AV_SAMPLE_FMT_NONE
  111. };
  112. int ret;
  113. layouts = ff_all_channel_counts();
  114. if (!layouts)
  115. return AVERROR(ENOMEM);
  116. ret = ff_set_common_channel_layouts(ctx, layouts);
  117. if (ret < 0)
  118. return ret;
  119. formats = ff_make_format_list(sample_fmts);
  120. if (!formats)
  121. return AVERROR(ENOMEM);
  122. ret = ff_set_common_formats(ctx, formats);
  123. if (ret < 0)
  124. return ret;
  125. formats = ff_all_samplerates();
  126. if (!formats)
  127. return AVERROR(ENOMEM);
  128. return ff_set_common_samplerates(ctx, formats);
  129. }
  130. static inline void set_highshelf_rbj(BiquadD2 *bq, double freq, double q, double peak, double sr)
  131. {
  132. double A = sqrt(peak);
  133. double w0 = freq * 2 * M_PI / sr;
  134. double alpha = sin(w0) / (2 * q);
  135. double cw0 = cos(w0);
  136. double tmp = 2 * sqrt(A) * alpha;
  137. double b0 = 0, ib0 = 0;
  138. bq->a0 = A*( (A+1) + (A-1)*cw0 + tmp);
  139. bq->a1 = -2*A*( (A-1) + (A+1)*cw0);
  140. bq->a2 = A*( (A+1) + (A-1)*cw0 - tmp);
  141. b0 = (A+1) - (A-1)*cw0 + tmp;
  142. bq->b1 = 2*( (A-1) - (A+1)*cw0);
  143. bq->b2 = (A+1) - (A-1)*cw0 - tmp;
  144. ib0 = 1 / b0;
  145. bq->b1 *= ib0;
  146. bq->b2 *= ib0;
  147. bq->a0 *= ib0;
  148. bq->a1 *= ib0;
  149. bq->a2 *= ib0;
  150. }
  151. static inline void set_lp_rbj(BiquadD2 *bq, double fc, double q, double sr, double gain)
  152. {
  153. double omega = 2.0 * M_PI * fc / sr;
  154. double sn = sin(omega);
  155. double cs = cos(omega);
  156. double alpha = sn/(2 * q);
  157. double inv = 1.0/(1.0 + alpha);
  158. bq->a2 = bq->a0 = gain * inv * (1.0 - cs) * 0.5;
  159. bq->a1 = bq->a0 + bq->a0;
  160. bq->b1 = (-2.0 * cs * inv);
  161. bq->b2 = ((1.0 - alpha) * inv);
  162. }
  163. static double freq_gain(BiquadCoeffs *c, double freq, double sr)
  164. {
  165. double complex z, w;
  166. freq *= 2.0 * M_PI / sr;
  167. w = 0 + I * freq;
  168. z = 1.0 / cexp(w);
  169. return cabs(((double complex)c->a0 + c->a1 * z + c->a2 * z*z) /
  170. ((double complex)1.0 + c->b1 * z + c->b2 * z*z));
  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. };