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.

416 lines
13KB

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