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.

428 lines
14KB

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