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.

330 lines
11KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "libavutil/channel_layout.h"
  19. #include "libavutil/ffmath.h"
  20. #include "libavutil/opt.h"
  21. #include "avfilter.h"
  22. #include "audio.h"
  23. #include "formats.h"
  24. typedef struct BiquadCoeffs {
  25. double a1, a2;
  26. double b0, b1, b2;
  27. } BiquadCoeffs;
  28. typedef struct ASuperCutContext {
  29. const AVClass *class;
  30. double cutoff;
  31. double level;
  32. int order;
  33. int filter_count;
  34. int bypass;
  35. BiquadCoeffs coeffs[10];
  36. AVFrame *w;
  37. int (*filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
  38. } ASuperCutContext;
  39. static int query_formats(AVFilterContext *ctx)
  40. {
  41. AVFilterFormats *formats = NULL;
  42. AVFilterChannelLayouts *layouts = NULL;
  43. static const enum AVSampleFormat sample_fmts[] = {
  44. AV_SAMPLE_FMT_FLTP,
  45. AV_SAMPLE_FMT_DBLP,
  46. AV_SAMPLE_FMT_NONE
  47. };
  48. int ret;
  49. formats = ff_make_format_list(sample_fmts);
  50. if (!formats)
  51. return AVERROR(ENOMEM);
  52. ret = ff_set_common_formats(ctx, formats);
  53. if (ret < 0)
  54. return ret;
  55. layouts = ff_all_channel_counts();
  56. if (!layouts)
  57. return AVERROR(ENOMEM);
  58. ret = ff_set_common_channel_layouts(ctx, layouts);
  59. if (ret < 0)
  60. return ret;
  61. formats = ff_all_samplerates();
  62. return ff_set_common_samplerates(ctx, formats);
  63. }
  64. static void calc_q_factors(int n, double *q)
  65. {
  66. for (int i = 0; i < n / 2; i++)
  67. q[i] = 1. / (-2. * cos(M_PI * (2. * (i + 1) + n - 1.) / (2. * n)));
  68. }
  69. static int get_coeffs(AVFilterContext *ctx)
  70. {
  71. ASuperCutContext *s = ctx->priv;
  72. AVFilterLink *inlink = ctx->inputs[0];
  73. double w0 = s->cutoff / inlink->sample_rate;
  74. double K = tan(M_PI * w0);
  75. double q[10];
  76. s->bypass = w0 >= 0.5;
  77. if (s->bypass)
  78. return 0;
  79. s->filter_count = s->order / 2 + (s->order & 1);
  80. calc_q_factors(s->order, q);
  81. if (!strcmp(ctx->filter->name, "asubcut")) {
  82. if (s->order & 1) {
  83. BiquadCoeffs *coeffs = &s->coeffs[0];
  84. double omega = 2. * tan(M_PI * w0);
  85. coeffs->b0 = 2. / (2. + omega);
  86. coeffs->b1 = -coeffs->b0;
  87. coeffs->b2 = 0.;
  88. coeffs->a1 = -(omega - 2.) / (2. + omega);
  89. coeffs->a2 = 0.;
  90. }
  91. for (int b = (s->order & 1); b < s->filter_count; b++) {
  92. BiquadCoeffs *coeffs = &s->coeffs[b];
  93. const int idx = b - (s->order & 1);
  94. double norm = 1.0 / (1.0 + K / q[idx] + K * K);
  95. coeffs->b0 = norm;
  96. coeffs->b1 = -2.0 * coeffs->b0;
  97. coeffs->b2 = coeffs->b0;
  98. coeffs->a1 = -2.0 * (K * K - 1.0) * norm;
  99. coeffs->a2 = -(1.0 - K / q[idx] + K * K) * norm;
  100. }
  101. } else {
  102. if (s->order & 1) {
  103. BiquadCoeffs *coeffs = &s->coeffs[0];
  104. double omega = 2. * tan(M_PI * w0);
  105. coeffs->b0 = omega / (2. + omega);
  106. coeffs->b1 = coeffs->b0;
  107. coeffs->b2 = 0.;
  108. coeffs->a1 = -(omega - 2.) / (2. + omega);
  109. coeffs->a2 = 0.;
  110. }
  111. for (int b = (s->order & 1); b < s->filter_count; b++) {
  112. BiquadCoeffs *coeffs = &s->coeffs[b];
  113. const int idx = b - (s->order & 1);
  114. double norm = 1.0 / (1.0 + K / q[idx] + K * K);
  115. coeffs->b0 = K * K * norm;
  116. coeffs->b1 = 2.0 * coeffs->b0;
  117. coeffs->b2 = coeffs->b0;
  118. coeffs->a1 = -2.0 * (K * K - 1.0) * norm;
  119. coeffs->a2 = -(1.0 - K / q[idx] + K * K) * norm;
  120. }
  121. }
  122. return 0;
  123. }
  124. typedef struct ThreadData {
  125. AVFrame *in, *out;
  126. } ThreadData;
  127. #define FILTER(name, type) \
  128. static int filter_channels_## name(AVFilterContext *ctx, void *arg, \
  129. int jobnr, int nb_jobs) \
  130. { \
  131. ASuperCutContext *s = ctx->priv; \
  132. ThreadData *td = arg; \
  133. AVFrame *out = td->out; \
  134. AVFrame *in = td->in; \
  135. const int start = (in->channels * jobnr) / nb_jobs; \
  136. const int end = (in->channels * (jobnr+1)) / nb_jobs; \
  137. const double level = s->level; \
  138. \
  139. for (int ch = start; ch < end; ch++) { \
  140. const type *src = (const type *)in->extended_data[ch]; \
  141. type *dst = (type *)out->extended_data[ch]; \
  142. \
  143. for (int b = 0; b < s->filter_count; b++) { \
  144. BiquadCoeffs *coeffs = &s->coeffs[b]; \
  145. const type a1 = coeffs->a1; \
  146. const type a2 = coeffs->a2; \
  147. const type b0 = coeffs->b0; \
  148. const type b1 = coeffs->b1; \
  149. const type b2 = coeffs->b2; \
  150. type *w = ((type *)s->w->extended_data[ch]) + b * 2; \
  151. \
  152. for (int n = 0; n < in->nb_samples; n++) { \
  153. type sin = b ? dst[n] : src[n] * level; \
  154. type sout = sin * b0 + w[0]; \
  155. \
  156. w[0] = b1 * sin + w[1] + a1 * sout; \
  157. w[1] = b2 * sin + a2 * sout; \
  158. \
  159. dst[n] = sout; \
  160. } \
  161. } \
  162. } \
  163. \
  164. return 0; \
  165. }
  166. FILTER(fltp, float)
  167. FILTER(dblp, double)
  168. static int config_input(AVFilterLink *inlink)
  169. {
  170. AVFilterContext *ctx = inlink->dst;
  171. ASuperCutContext *s = ctx->priv;
  172. switch (inlink->format) {
  173. case AV_SAMPLE_FMT_FLTP: s->filter_channels = filter_channels_fltp; break;
  174. case AV_SAMPLE_FMT_DBLP: s->filter_channels = filter_channels_dblp; break;
  175. }
  176. s->w = ff_get_audio_buffer(inlink, 2 * 10);
  177. if (!s->w)
  178. return AVERROR(ENOMEM);
  179. return get_coeffs(ctx);
  180. }
  181. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  182. {
  183. AVFilterContext *ctx = inlink->dst;
  184. ASuperCutContext *s = ctx->priv;
  185. AVFilterLink *outlink = ctx->outputs[0];
  186. ThreadData td;
  187. AVFrame *out;
  188. if (s->bypass)
  189. return ff_filter_frame(outlink, in);
  190. if (av_frame_is_writable(in)) {
  191. out = in;
  192. } else {
  193. out = ff_get_audio_buffer(outlink, in->nb_samples);
  194. if (!out) {
  195. av_frame_free(&in);
  196. return AVERROR(ENOMEM);
  197. }
  198. av_frame_copy_props(out, in);
  199. }
  200. td.in = in; td.out = out;
  201. ctx->internal->execute(ctx, s->filter_channels, &td, NULL, FFMIN(inlink->channels,
  202. ff_filter_get_nb_threads(ctx)));
  203. if (out != in)
  204. av_frame_free(&in);
  205. return ff_filter_frame(outlink, out);
  206. }
  207. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  208. char *res, int res_len, int flags)
  209. {
  210. int ret;
  211. ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
  212. if (ret < 0)
  213. return ret;
  214. return get_coeffs(ctx);
  215. }
  216. static av_cold void uninit(AVFilterContext *ctx)
  217. {
  218. ASuperCutContext *s = ctx->priv;
  219. av_frame_free(&s->w);
  220. }
  221. #define OFFSET(x) offsetof(ASuperCutContext, x)
  222. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
  223. static const AVOption asupercut_options[] = {
  224. { "cutoff", "set cutoff frequency", OFFSET(cutoff), AV_OPT_TYPE_DOUBLE, {.dbl=20000}, 20000, 192000, FLAGS },
  225. { "order", "set filter order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=10}, 3, 20, FLAGS },
  226. { "level", "set input level", OFFSET(level), AV_OPT_TYPE_DOUBLE, {.dbl=1.}, 0., 1., FLAGS },
  227. { NULL }
  228. };
  229. AVFILTER_DEFINE_CLASS(asupercut);
  230. static const AVFilterPad inputs[] = {
  231. {
  232. .name = "default",
  233. .type = AVMEDIA_TYPE_AUDIO,
  234. .filter_frame = filter_frame,
  235. .config_props = config_input,
  236. },
  237. { NULL }
  238. };
  239. static const AVFilterPad outputs[] = {
  240. {
  241. .name = "default",
  242. .type = AVMEDIA_TYPE_AUDIO,
  243. },
  244. { NULL }
  245. };
  246. AVFilter ff_af_asupercut = {
  247. .name = "asupercut",
  248. .description = NULL_IF_CONFIG_SMALL("Cut super frequencies."),
  249. .query_formats = query_formats,
  250. .priv_size = sizeof(ASuperCutContext),
  251. .priv_class = &asupercut_class,
  252. .uninit = uninit,
  253. .inputs = inputs,
  254. .outputs = outputs,
  255. .process_command = process_command,
  256. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
  257. AVFILTER_FLAG_SLICE_THREADS,
  258. };
  259. static const AVOption asubcut_options[] = {
  260. { "cutoff", "set cutoff frequency", OFFSET(cutoff), AV_OPT_TYPE_DOUBLE, {.dbl=20}, 2, 200, FLAGS },
  261. { "order", "set filter order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=10}, 3, 20, FLAGS },
  262. { "level", "set input level", OFFSET(level), AV_OPT_TYPE_DOUBLE, {.dbl=1.}, 0., 1., FLAGS },
  263. { NULL }
  264. };
  265. AVFILTER_DEFINE_CLASS(asubcut);
  266. AVFilter ff_af_asubcut = {
  267. .name = "asubcut",
  268. .description = NULL_IF_CONFIG_SMALL("Cut subwoofer frequencies."),
  269. .query_formats = query_formats,
  270. .priv_size = sizeof(ASuperCutContext),
  271. .priv_class = &asubcut_class,
  272. .uninit = uninit,
  273. .inputs = inputs,
  274. .outputs = outputs,
  275. .process_command = process_command,
  276. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
  277. AVFILTER_FLAG_SLICE_THREADS,
  278. };