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.

410 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 level;
  32. double c[NB_COEFS];
  33. int64_t in_samples;
  34. AVFrame *i1, *o1;
  35. AVFrame *i2, *o2;
  36. void (*filter_channel)(AVFilterContext *ctx,
  37. int nb_samples,
  38. int sample_rate,
  39. const double *src, double *dst,
  40. double *i1, double *o1,
  41. double *i2, double *o2);
  42. } AFreqShift;
  43. static int query_formats(AVFilterContext *ctx)
  44. {
  45. AVFilterFormats *formats = NULL;
  46. AVFilterChannelLayouts *layouts = NULL;
  47. static const enum AVSampleFormat sample_fmts[] = {
  48. AV_SAMPLE_FMT_DBLP,
  49. AV_SAMPLE_FMT_NONE
  50. };
  51. int ret;
  52. formats = ff_make_format_list(sample_fmts);
  53. if (!formats)
  54. return AVERROR(ENOMEM);
  55. ret = ff_set_common_formats(ctx, formats);
  56. if (ret < 0)
  57. return ret;
  58. layouts = ff_all_channel_counts();
  59. if (!layouts)
  60. return AVERROR(ENOMEM);
  61. ret = ff_set_common_channel_layouts(ctx, layouts);
  62. if (ret < 0)
  63. return ret;
  64. formats = ff_all_samplerates();
  65. return ff_set_common_samplerates(ctx, formats);
  66. }
  67. static void pfilter_channel(AVFilterContext *ctx,
  68. int nb_samples,
  69. int sample_rate,
  70. const double *src, double *dst,
  71. double *i1, double *o1,
  72. double *i2, double *o2)
  73. {
  74. AFreqShift *s = ctx->priv;
  75. const double *c = s->c;
  76. const double level = s->level;
  77. double shift = s->shift * M_PI;
  78. double cos_theta = cos(shift);
  79. double sin_theta = sin(shift);
  80. for (int n = 0; n < nb_samples; n++) {
  81. double xn1 = src[n], xn2 = src[n];
  82. double I, Q;
  83. for (int j = 0; j < NB_COEFS / 2; j++) {
  84. I = c[j] * (xn1 + o2[j]) - i2[j];
  85. i2[j] = i1[j];
  86. i1[j] = xn1;
  87. o2[j] = o1[j];
  88. o1[j] = I;
  89. xn1 = I;
  90. }
  91. for (int j = NB_COEFS / 2; j < NB_COEFS; j++) {
  92. Q = c[j] * (xn2 + o2[j]) - i2[j];
  93. i2[j] = i1[j];
  94. i1[j] = xn2;
  95. o2[j] = o1[j];
  96. o1[j] = Q;
  97. xn2 = Q;
  98. }
  99. Q = o2[NB_COEFS - 1];
  100. dst[n] = (I * cos_theta - Q * sin_theta) * level;
  101. }
  102. }
  103. static void ffilter_channel(AVFilterContext *ctx,
  104. int nb_samples,
  105. int sample_rate,
  106. const double *src, double *dst,
  107. double *i1, double *o1,
  108. double *i2, double *o2)
  109. {
  110. AFreqShift *s = ctx->priv;
  111. const double *c = s->c;
  112. const double level = s->level;
  113. double ts = 1. / sample_rate;
  114. double shift = s->shift;
  115. int64_t N = s->in_samples;
  116. for (int n = 0; n < nb_samples; n++) {
  117. double xn1 = src[n], xn2 = src[n];
  118. double I, Q, theta;
  119. for (int j = 0; j < NB_COEFS / 2; j++) {
  120. I = c[j] * (xn1 + o2[j]) - i2[j];
  121. i2[j] = i1[j];
  122. i1[j] = xn1;
  123. o2[j] = o1[j];
  124. o1[j] = I;
  125. xn1 = I;
  126. }
  127. for (int j = NB_COEFS / 2; j < NB_COEFS; j++) {
  128. Q = c[j] * (xn2 + o2[j]) - i2[j];
  129. i2[j] = i1[j];
  130. i1[j] = xn2;
  131. o2[j] = o1[j];
  132. o1[j] = Q;
  133. xn2 = Q;
  134. }
  135. Q = o2[NB_COEFS - 1];
  136. theta = 2. * M_PI * fmod(shift * (N + n) * ts, 1.);
  137. dst[n] = (I * cos(theta) - Q * sin(theta)) * level;
  138. }
  139. }
  140. static void compute_transition_param(double *K, double *Q, double transition)
  141. {
  142. double kksqrt, e, e2, e4, k, q;
  143. k = tan((1. - transition * 2.) * M_PI / 4.);
  144. k *= k;
  145. kksqrt = pow(1 - k * k, 0.25);
  146. e = 0.5 * (1. - kksqrt) / (1. + kksqrt);
  147. e2 = e * e;
  148. e4 = e2 * e2;
  149. q = e * (1. + e4 * (2. + e4 * (15. + 150. * e4)));
  150. *Q = q;
  151. *K = k;
  152. }
  153. static double ipowp(double x, int64_t n)
  154. {
  155. double z = 1.;
  156. while (n != 0) {
  157. if (n & 1)
  158. z *= x;
  159. n >>= 1;
  160. x *= x;
  161. }
  162. return z;
  163. }
  164. static double compute_acc_num(double q, int order, int c)
  165. {
  166. int64_t i = 0;
  167. int j = 1;
  168. double acc = 0.;
  169. double q_ii1;
  170. do {
  171. q_ii1 = ipowp(q, i * (i + 1));
  172. q_ii1 *= sin((i * 2 + 1) * c * M_PI / order) * j;
  173. acc += q_ii1;
  174. j = -j;
  175. i++;
  176. } while (fabs(q_ii1) > 1e-100);
  177. return acc;
  178. }
  179. static double compute_acc_den(double q, int order, int c)
  180. {
  181. int64_t i = 1;
  182. int j = -1;
  183. double acc = 0.;
  184. double q_i2;
  185. do {
  186. q_i2 = ipowp(q, i * i);
  187. q_i2 *= cos(i * 2 * c * M_PI / order) * j;
  188. acc += q_i2;
  189. j = -j;
  190. i++;
  191. } while (fabs(q_i2) > 1e-100);
  192. return acc;
  193. }
  194. static double compute_coef(int index, double k, double q, int order)
  195. {
  196. const int c = index + 1;
  197. const double num = compute_acc_num(q, order, c) * pow(q, 0.25);
  198. const double den = compute_acc_den(q, order, c) + 0.5;
  199. const double ww = num / den;
  200. const double wwsq = ww * ww;
  201. const double x = sqrt((1 - wwsq * k) * (1 - wwsq / k)) / (1 + wwsq);
  202. const double coef = (1 - x) / (1 + x);
  203. return coef;
  204. }
  205. static void compute_coefs(double *coef_arr, int nbr_coefs, double transition)
  206. {
  207. const int order = nbr_coefs * 2 + 1;
  208. double k, q;
  209. compute_transition_param(&k, &q, transition);
  210. for (int n = 0; n < nbr_coefs; n++)
  211. coef_arr[(n / 2) + (n & 1) * nbr_coefs / 2] = compute_coef(n, k, q, order);
  212. }
  213. static int config_input(AVFilterLink *inlink)
  214. {
  215. AVFilterContext *ctx = inlink->dst;
  216. AFreqShift *s = ctx->priv;
  217. compute_coefs(s->c, NB_COEFS, 2. * 20. / inlink->sample_rate);
  218. s->i1 = ff_get_audio_buffer(inlink, NB_COEFS);
  219. s->o1 = ff_get_audio_buffer(inlink, NB_COEFS);
  220. s->i2 = ff_get_audio_buffer(inlink, NB_COEFS);
  221. s->o2 = ff_get_audio_buffer(inlink, NB_COEFS);
  222. if (!s->i1 || !s->o1 || !s->i2 || !s->o2)
  223. return AVERROR(ENOMEM);
  224. if (!strcmp(ctx->filter->name, "afreqshift"))
  225. s->filter_channel = ffilter_channel;
  226. else
  227. s->filter_channel = pfilter_channel;
  228. return 0;
  229. }
  230. typedef struct ThreadData {
  231. AVFrame *in, *out;
  232. } ThreadData;
  233. static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  234. {
  235. AFreqShift *s = ctx->priv;
  236. ThreadData *td = arg;
  237. AVFrame *out = td->out;
  238. AVFrame *in = td->in;
  239. const int start = (in->channels * jobnr) / nb_jobs;
  240. const int end = (in->channels * (jobnr+1)) / nb_jobs;
  241. for (int ch = start; ch < end; ch++) {
  242. s->filter_channel(ctx, in->nb_samples,
  243. in->sample_rate,
  244. (const double *)in->extended_data[ch],
  245. (double *)out->extended_data[ch],
  246. (double *)s->i1->extended_data[ch],
  247. (double *)s->o1->extended_data[ch],
  248. (double *)s->i2->extended_data[ch],
  249. (double *)s->o2->extended_data[ch]);
  250. }
  251. return 0;
  252. }
  253. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  254. {
  255. AVFilterContext *ctx = inlink->dst;
  256. AVFilterLink *outlink = ctx->outputs[0];
  257. AFreqShift *s = ctx->priv;
  258. AVFrame *out;
  259. ThreadData td;
  260. if (av_frame_is_writable(in)) {
  261. out = in;
  262. } else {
  263. out = ff_get_audio_buffer(outlink, in->nb_samples);
  264. if (!out) {
  265. av_frame_free(&in);
  266. return AVERROR(ENOMEM);
  267. }
  268. av_frame_copy_props(out, in);
  269. }
  270. td.in = in; td.out = out;
  271. ctx->internal->execute(ctx, filter_channels, &td, NULL, FFMIN(inlink->channels,
  272. ff_filter_get_nb_threads(ctx)));
  273. s->in_samples += in->nb_samples;
  274. if (out != in)
  275. av_frame_free(&in);
  276. return ff_filter_frame(outlink, out);
  277. }
  278. static av_cold void uninit(AVFilterContext *ctx)
  279. {
  280. AFreqShift *s = ctx->priv;
  281. av_frame_free(&s->i1);
  282. av_frame_free(&s->o1);
  283. av_frame_free(&s->i2);
  284. av_frame_free(&s->o2);
  285. }
  286. #define OFFSET(x) offsetof(AFreqShift, x)
  287. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
  288. static const AVOption afreqshift_options[] = {
  289. { "shift", "set frequency shift", OFFSET(shift), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -INT_MAX, INT_MAX, FLAGS },
  290. { "level", "set output level", OFFSET(level), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.0, 1.0, FLAGS },
  291. { NULL }
  292. };
  293. AVFILTER_DEFINE_CLASS(afreqshift);
  294. static const AVFilterPad inputs[] = {
  295. {
  296. .name = "default",
  297. .type = AVMEDIA_TYPE_AUDIO,
  298. .filter_frame = filter_frame,
  299. .config_props = config_input,
  300. },
  301. { NULL }
  302. };
  303. static const AVFilterPad outputs[] = {
  304. {
  305. .name = "default",
  306. .type = AVMEDIA_TYPE_AUDIO,
  307. },
  308. { NULL }
  309. };
  310. AVFilter ff_af_afreqshift = {
  311. .name = "afreqshift",
  312. .description = NULL_IF_CONFIG_SMALL("Apply frequency shifting to input audio."),
  313. .query_formats = query_formats,
  314. .priv_size = sizeof(AFreqShift),
  315. .priv_class = &afreqshift_class,
  316. .uninit = uninit,
  317. .inputs = inputs,
  318. .outputs = outputs,
  319. .process_command = ff_filter_process_command,
  320. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
  321. AVFILTER_FLAG_SLICE_THREADS,
  322. };
  323. static const AVOption aphaseshift_options[] = {
  324. { "shift", "set phase shift", OFFSET(shift), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1.0, 1.0, FLAGS },
  325. { "level", "set output level",OFFSET(level), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.0, 1.0, FLAGS },
  326. { NULL }
  327. };
  328. AVFILTER_DEFINE_CLASS(aphaseshift);
  329. AVFilter ff_af_aphaseshift = {
  330. .name = "aphaseshift",
  331. .description = NULL_IF_CONFIG_SMALL("Apply phase shifting to input audio."),
  332. .query_formats = query_formats,
  333. .priv_size = sizeof(AFreqShift),
  334. .priv_class = &aphaseshift_class,
  335. .uninit = uninit,
  336. .inputs = inputs,
  337. .outputs = outputs,
  338. .process_command = ff_filter_process_command,
  339. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
  340. AVFILTER_FLAG_SLICE_THREADS,
  341. };