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.

405 lines
11KB

  1. /*
  2. * Copyright (c) Paul B Mahol
  3. * Copyright (c) Laurent de Soras, 2005
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/channel_layout.h"
  22. #include "libavutil/ffmath.h"
  23. #include "libavutil/opt.h"
  24. #include "avfilter.h"
  25. #include "audio.h"
  26. #include "formats.h"
  27. #define NB_COEFS 16
  28. typedef struct AFreqShift {
  29. const AVClass *class;
  30. double shift;
  31. double c[NB_COEFS];
  32. int64_t in_samples;
  33. AVFrame *i1, *o1;
  34. AVFrame *i2, *o2;
  35. void (*filter_channel)(AVFilterContext *ctx,
  36. int nb_samples,
  37. int sample_rate,
  38. const double *src, double *dst,
  39. double *i1, double *o1,
  40. double *i2, double *o2);
  41. } AFreqShift;
  42. static int query_formats(AVFilterContext *ctx)
  43. {
  44. AVFilterFormats *formats = NULL;
  45. AVFilterChannelLayouts *layouts = NULL;
  46. static const enum AVSampleFormat sample_fmts[] = {
  47. AV_SAMPLE_FMT_DBLP,
  48. AV_SAMPLE_FMT_NONE
  49. };
  50. int ret;
  51. formats = ff_make_format_list(sample_fmts);
  52. if (!formats)
  53. return AVERROR(ENOMEM);
  54. ret = ff_set_common_formats(ctx, formats);
  55. if (ret < 0)
  56. return ret;
  57. layouts = ff_all_channel_counts();
  58. if (!layouts)
  59. return AVERROR(ENOMEM);
  60. ret = ff_set_common_channel_layouts(ctx, layouts);
  61. if (ret < 0)
  62. return ret;
  63. formats = ff_all_samplerates();
  64. return ff_set_common_samplerates(ctx, formats);
  65. }
  66. static void pfilter_channel(AVFilterContext *ctx,
  67. int nb_samples,
  68. int sample_rate,
  69. const double *src, double *dst,
  70. double *i1, double *o1,
  71. double *i2, double *o2)
  72. {
  73. AFreqShift *s = ctx->priv;
  74. double *c = s->c;
  75. double shift = s->shift * M_PI;
  76. double cos_theta = cos(shift);
  77. double sin_theta = sin(shift);
  78. for (int n = 0; n < nb_samples; n++) {
  79. double xn1 = src[n], xn2 = src[n];
  80. double I, Q;
  81. for (int j = 0; j < NB_COEFS / 2; j++) {
  82. I = c[j] * (xn1 + o2[j]) - i2[j];
  83. i2[j] = i1[j];
  84. i1[j] = xn1;
  85. o2[j] = o1[j];
  86. o1[j] = I;
  87. xn1 = I;
  88. }
  89. for (int j = NB_COEFS / 2; j < NB_COEFS; j++) {
  90. Q = c[j] * (xn2 + o2[j]) - i2[j];
  91. i2[j] = i1[j];
  92. i1[j] = xn2;
  93. o2[j] = o1[j];
  94. o1[j] = Q;
  95. xn2 = Q;
  96. }
  97. Q = o2[NB_COEFS - 1];
  98. dst[n] = I * cos_theta - Q * sin_theta;
  99. }
  100. }
  101. static void ffilter_channel(AVFilterContext *ctx,
  102. int nb_samples,
  103. int sample_rate,
  104. const double *src, double *dst,
  105. double *i1, double *o1,
  106. double *i2, double *o2)
  107. {
  108. AFreqShift *s = ctx->priv;
  109. double *c = s->c;
  110. double ts = 1. / sample_rate;
  111. double shift = s->shift;
  112. int64_t N = s->in_samples;
  113. for (int n = 0; n < nb_samples; n++) {
  114. double xn1 = src[n], xn2 = src[n];
  115. double I, Q, theta;
  116. for (int j = 0; j < NB_COEFS / 2; j++) {
  117. I = c[j] * (xn1 + o2[j]) - i2[j];
  118. i2[j] = i1[j];
  119. i1[j] = xn1;
  120. o2[j] = o1[j];
  121. o1[j] = I;
  122. xn1 = I;
  123. }
  124. for (int j = NB_COEFS / 2; j < NB_COEFS; j++) {
  125. Q = c[j] * (xn2 + o2[j]) - i2[j];
  126. i2[j] = i1[j];
  127. i1[j] = xn2;
  128. o2[j] = o1[j];
  129. o1[j] = Q;
  130. xn2 = Q;
  131. }
  132. Q = o2[NB_COEFS - 1];
  133. theta = 2. * M_PI * fmod(shift * (N + n) * ts, 1.);
  134. dst[n] = I * cos(theta) - Q * sin(theta);
  135. }
  136. }
  137. static void compute_transition_param(double *K, double *Q, double transition)
  138. {
  139. double kksqrt, e, e2, e4, k, q;
  140. k = tan((1. - transition * 2.) * M_PI / 4.);
  141. k *= k;
  142. kksqrt = pow(1 - k * k, 0.25);
  143. e = 0.5 * (1. - kksqrt) / (1. + kksqrt);
  144. e2 = e * e;
  145. e4 = e2 * e2;
  146. q = e * (1. + e4 * (2. + e4 * (15. + 150. * e4)));
  147. *Q = q;
  148. *K = k;
  149. }
  150. static double ipowp(double x, int64_t n)
  151. {
  152. double z = 1.;
  153. while (n != 0) {
  154. if (n & 1)
  155. z *= x;
  156. n >>= 1;
  157. x *= x;
  158. }
  159. return z;
  160. }
  161. static double compute_acc_num(double q, int order, int c)
  162. {
  163. int64_t i = 0;
  164. int j = 1;
  165. double acc = 0.;
  166. double q_ii1;
  167. do {
  168. q_ii1 = ipowp(q, i * (i + 1));
  169. q_ii1 *= sin((i * 2 + 1) * c * M_PI / order) * j;
  170. acc += q_ii1;
  171. j = -j;
  172. i++;
  173. } while (fabs(q_ii1) > 1e-100);
  174. return acc;
  175. }
  176. static double compute_acc_den(double q, int order, int c)
  177. {
  178. int64_t i = 1;
  179. int j = -1;
  180. double acc = 0.;
  181. double q_i2;
  182. do {
  183. q_i2 = ipowp(q, i * i);
  184. q_i2 *= cos(i * 2 * c * M_PI / order) * j;
  185. acc += q_i2;
  186. j = -j;
  187. i++;
  188. } while (fabs(q_i2) > 1e-100);
  189. return acc;
  190. }
  191. static double compute_coef(int index, double k, double q, int order)
  192. {
  193. const int c = index + 1;
  194. const double num = compute_acc_num(q, order, c) * pow(q, 0.25);
  195. const double den = compute_acc_den(q, order, c) + 0.5;
  196. const double ww = num / den;
  197. const double wwsq = ww * ww;
  198. const double x = sqrt((1 - wwsq * k) * (1 - wwsq / k)) / (1 + wwsq);
  199. const double coef = (1 - x) / (1 + x);
  200. return coef;
  201. }
  202. static void compute_coefs(double *coef_arr, int nbr_coefs, double transition)
  203. {
  204. const int order = nbr_coefs * 2 + 1;
  205. double k, q;
  206. compute_transition_param(&k, &q, transition);
  207. for (int n = 0; n < nbr_coefs; n++)
  208. coef_arr[(n / 2) + (n & 1) * nbr_coefs / 2] = compute_coef(n, k, q, order);
  209. }
  210. static int config_input(AVFilterLink *inlink)
  211. {
  212. AVFilterContext *ctx = inlink->dst;
  213. AFreqShift *s = ctx->priv;
  214. compute_coefs(s->c, NB_COEFS, 2. * 20. / inlink->sample_rate);
  215. s->i1 = ff_get_audio_buffer(inlink, NB_COEFS);
  216. s->o1 = ff_get_audio_buffer(inlink, NB_COEFS);
  217. s->i2 = ff_get_audio_buffer(inlink, NB_COEFS);
  218. s->o2 = ff_get_audio_buffer(inlink, NB_COEFS);
  219. if (!s->i1 || !s->o1 || !s->i2 || !s->o2)
  220. return AVERROR(ENOMEM);
  221. if (!strcmp(ctx->filter->name, "afreqshift"))
  222. s->filter_channel = ffilter_channel;
  223. else
  224. s->filter_channel = pfilter_channel;
  225. return 0;
  226. }
  227. typedef struct ThreadData {
  228. AVFrame *in, *out;
  229. } ThreadData;
  230. static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  231. {
  232. AFreqShift *s = ctx->priv;
  233. ThreadData *td = arg;
  234. AVFrame *out = td->out;
  235. AVFrame *in = td->in;
  236. const int start = (in->channels * jobnr) / nb_jobs;
  237. const int end = (in->channels * (jobnr+1)) / nb_jobs;
  238. for (int ch = start; ch < end; ch++) {
  239. s->filter_channel(ctx, in->nb_samples,
  240. in->sample_rate,
  241. (const double *)in->extended_data[ch],
  242. (double *)out->extended_data[ch],
  243. (double *)s->i1->extended_data[ch],
  244. (double *)s->o1->extended_data[ch],
  245. (double *)s->i2->extended_data[ch],
  246. (double *)s->o2->extended_data[ch]);
  247. }
  248. return 0;
  249. }
  250. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  251. {
  252. AVFilterContext *ctx = inlink->dst;
  253. AVFilterLink *outlink = ctx->outputs[0];
  254. AFreqShift *s = ctx->priv;
  255. AVFrame *out;
  256. ThreadData td;
  257. if (av_frame_is_writable(in)) {
  258. out = in;
  259. } else {
  260. out = ff_get_audio_buffer(outlink, in->nb_samples);
  261. if (!out) {
  262. av_frame_free(&in);
  263. return AVERROR(ENOMEM);
  264. }
  265. av_frame_copy_props(out, in);
  266. }
  267. td.in = in; td.out = out;
  268. ctx->internal->execute(ctx, filter_channels, &td, NULL, FFMIN(inlink->channels,
  269. ff_filter_get_nb_threads(ctx)));
  270. s->in_samples += in->nb_samples;
  271. if (out != in)
  272. av_frame_free(&in);
  273. return ff_filter_frame(outlink, out);
  274. }
  275. static av_cold void uninit(AVFilterContext *ctx)
  276. {
  277. AFreqShift *s = ctx->priv;
  278. av_frame_free(&s->i1);
  279. av_frame_free(&s->o1);
  280. av_frame_free(&s->i2);
  281. av_frame_free(&s->o2);
  282. }
  283. #define OFFSET(x) offsetof(AFreqShift, x)
  284. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
  285. static const AVOption afreqshift_options[] = {
  286. { "shift", "set frequency shift", OFFSET(shift), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -INT_MAX, INT_MAX, FLAGS },
  287. { NULL }
  288. };
  289. AVFILTER_DEFINE_CLASS(afreqshift);
  290. static const AVFilterPad inputs[] = {
  291. {
  292. .name = "default",
  293. .type = AVMEDIA_TYPE_AUDIO,
  294. .filter_frame = filter_frame,
  295. .config_props = config_input,
  296. },
  297. { NULL }
  298. };
  299. static const AVFilterPad outputs[] = {
  300. {
  301. .name = "default",
  302. .type = AVMEDIA_TYPE_AUDIO,
  303. },
  304. { NULL }
  305. };
  306. AVFilter ff_af_afreqshift = {
  307. .name = "afreqshift",
  308. .description = NULL_IF_CONFIG_SMALL("Apply frequency shifting to input audio."),
  309. .query_formats = query_formats,
  310. .priv_size = sizeof(AFreqShift),
  311. .priv_class = &afreqshift_class,
  312. .uninit = uninit,
  313. .inputs = inputs,
  314. .outputs = outputs,
  315. .process_command = ff_filter_process_command,
  316. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
  317. AVFILTER_FLAG_SLICE_THREADS,
  318. };
  319. static const AVOption aphaseshift_options[] = {
  320. { "shift", "set phase shift", OFFSET(shift), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1.0, 1.0, FLAGS },
  321. { NULL }
  322. };
  323. AVFILTER_DEFINE_CLASS(aphaseshift);
  324. AVFilter ff_af_aphaseshift = {
  325. .name = "aphaseshift",
  326. .description = NULL_IF_CONFIG_SMALL("Apply phase shifting to input audio."),
  327. .query_formats = query_formats,
  328. .priv_size = sizeof(AFreqShift),
  329. .priv_class = &aphaseshift_class,
  330. .uninit = uninit,
  331. .inputs = inputs,
  332. .outputs = outputs,
  333. .process_command = ff_filter_process_command,
  334. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
  335. AVFILTER_FLAG_SLICE_THREADS,
  336. };