asrc_sinc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * Copyright (c) 2008-2009 Rob Sykes <robs@users.sourceforge.net>
  3. * Copyright (c) 2017 Paul B Mahol
  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/avassert.h"
  22. #include "libavutil/opt.h"
  23. #include "libavcodec/avfft.h"
  24. #include "audio.h"
  25. #include "avfilter.h"
  26. #include "internal.h"
  27. typedef struct SincContext {
  28. const AVClass *class;
  29. int sample_rate, nb_samples;
  30. float att, beta, phase, Fc0, Fc1, tbw0, tbw1;
  31. int num_taps[2];
  32. int round;
  33. int n, rdft_len;
  34. float *coeffs;
  35. int64_t pts;
  36. RDFTContext *rdft, *irdft;
  37. } SincContext;
  38. static int request_frame(AVFilterLink *outlink)
  39. {
  40. AVFilterContext *ctx = outlink->src;
  41. SincContext *s = ctx->priv;
  42. const float *coeffs = s->coeffs;
  43. AVFrame *frame = NULL;
  44. int nb_samples;
  45. nb_samples = FFMIN(s->nb_samples, s->n - s->pts);
  46. if (nb_samples <= 0)
  47. return AVERROR_EOF;
  48. if (!(frame = ff_get_audio_buffer(outlink, nb_samples)))
  49. return AVERROR(ENOMEM);
  50. memcpy(frame->data[0], coeffs + s->pts, nb_samples * sizeof(float));
  51. frame->pts = s->pts;
  52. s->pts += nb_samples;
  53. return ff_filter_frame(outlink, frame);
  54. }
  55. static int query_formats(AVFilterContext *ctx)
  56. {
  57. SincContext *s = ctx->priv;
  58. static const int64_t chlayouts[] = { AV_CH_LAYOUT_MONO, -1 };
  59. int sample_rates[] = { s->sample_rate, -1 };
  60. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLT,
  61. AV_SAMPLE_FMT_NONE };
  62. AVFilterFormats *formats;
  63. AVFilterChannelLayouts *layouts;
  64. int ret;
  65. formats = ff_make_format_list(sample_fmts);
  66. if (!formats)
  67. return AVERROR(ENOMEM);
  68. ret = ff_set_common_formats (ctx, formats);
  69. if (ret < 0)
  70. return ret;
  71. layouts = avfilter_make_format64_list(chlayouts);
  72. if (!layouts)
  73. return AVERROR(ENOMEM);
  74. ret = ff_set_common_channel_layouts(ctx, layouts);
  75. if (ret < 0)
  76. return ret;
  77. formats = ff_make_format_list(sample_rates);
  78. if (!formats)
  79. return AVERROR(ENOMEM);
  80. return ff_set_common_samplerates(ctx, formats);
  81. }
  82. static float bessel_I_0(float x)
  83. {
  84. float term = 1, sum = 1, last_sum, x2 = x / 2;
  85. int i = 1;
  86. do {
  87. float y = x2 / i++;
  88. last_sum = sum;
  89. sum += term *= y * y;
  90. } while (sum != last_sum);
  91. return sum;
  92. }
  93. static float *make_lpf(int num_taps, float Fc, float beta, float rho,
  94. float scale, int dc_norm)
  95. {
  96. int i, m = num_taps - 1;
  97. float *h = av_calloc(num_taps, sizeof(*h)), sum = 0;
  98. float mult = scale / bessel_I_0(beta), mult1 = 1.f / (.5f * m + rho);
  99. av_assert0(Fc >= 0 && Fc <= 1);
  100. for (i = 0; i <= m / 2; i++) {
  101. float z = i - .5f * m, x = z * M_PI, y = z * mult1;
  102. h[i] = x ? sinf(Fc * x) / x : Fc;
  103. sum += h[i] *= bessel_I_0(beta * sqrtf(1.f - y * y)) * mult;
  104. if (m - i != i) {
  105. h[m - i] = h[i];
  106. sum += h[i];
  107. }
  108. }
  109. for (i = 0; dc_norm && i < num_taps; i++)
  110. h[i] *= scale / sum;
  111. return h;
  112. }
  113. static float kaiser_beta(float att, float tr_bw)
  114. {
  115. if (att >= 60.f) {
  116. static const float coefs[][4] = {
  117. {-6.784957e-10, 1.02856e-05, 0.1087556, -0.8988365 + .001},
  118. {-6.897885e-10, 1.027433e-05, 0.10876, -0.8994658 + .002},
  119. {-1.000683e-09, 1.030092e-05, 0.1087677, -0.9007898 + .003},
  120. {-3.654474e-10, 1.040631e-05, 0.1087085, -0.8977766 + .006},
  121. {8.106988e-09, 6.983091e-06, 0.1091387, -0.9172048 + .015},
  122. {9.519571e-09, 7.272678e-06, 0.1090068, -0.9140768 + .025},
  123. {-5.626821e-09, 1.342186e-05, 0.1083999, -0.9065452 + .05},
  124. {-9.965946e-08, 5.073548e-05, 0.1040967, -0.7672778 + .085},
  125. {1.604808e-07, -5.856462e-05, 0.1185998, -1.34824 + .1},
  126. {-1.511964e-07, 6.363034e-05, 0.1064627, -0.9876665 + .18},
  127. };
  128. float realm = logf(tr_bw / .0005f) / logf(2.f);
  129. float const *c0 = coefs[av_clip((int)realm, 0, FF_ARRAY_ELEMS(coefs) - 1)];
  130. float const *c1 = coefs[av_clip(1 + (int)realm, 0, FF_ARRAY_ELEMS(coefs) - 1)];
  131. float b0 = ((c0[0] * att + c0[1]) * att + c0[2]) * att + c0[3];
  132. float b1 = ((c1[0] * att + c1[1]) * att + c1[2]) * att + c1[3];
  133. return b0 + (b1 - b0) * (realm - (int)realm);
  134. }
  135. if (att > 50.f)
  136. return .1102f * (att - 8.7f);
  137. if (att > 20.96f)
  138. return .58417f * powf(att - 20.96f, .4f) + .07886f * (att - 20.96f);
  139. return 0;
  140. }
  141. static void kaiser_params(float att, float Fc, float tr_bw, float *beta, int *num_taps)
  142. {
  143. *beta = *beta < 0.f ? kaiser_beta(att, tr_bw * .5f / Fc): *beta;
  144. att = att < 60.f ? (att - 7.95f) / (2.285f * M_PI * 2.f) :
  145. ((.0007528358f-1.577737e-05 * *beta) * *beta + 0.6248022f) * *beta + .06186902f;
  146. *num_taps = !*num_taps ? ceilf(att/tr_bw + 1) : *num_taps;
  147. }
  148. static float *lpf(float Fn, float Fc, float tbw, int *num_taps, float att, float *beta, int round)
  149. {
  150. int n = *num_taps;
  151. if ((Fc /= Fn) <= 0.f || Fc >= 1.f) {
  152. *num_taps = 0;
  153. return NULL;
  154. }
  155. att = att ? att : 120.f;
  156. kaiser_params(att, Fc, (tbw ? tbw / Fn : .05f) * .5f, beta, num_taps);
  157. if (!n) {
  158. n = *num_taps;
  159. *num_taps = av_clip(n, 11, 32767);
  160. if (round)
  161. *num_taps = 1 + 2 * (int)((int)((*num_taps / 2) * Fc + .5f) / Fc + .5f);
  162. }
  163. return make_lpf(*num_taps |= 1, Fc, *beta, 0.f, 1.f, 0);
  164. }
  165. static void invert(float *h, int n)
  166. {
  167. for (int i = 0; i < n; i++)
  168. h[i] = -h[i];
  169. h[(n - 1) / 2] += 1;
  170. }
  171. #define PACK(h, n) h[1] = h[n]
  172. #define UNPACK(h, n) h[n] = h[1], h[n + 1] = h[1] = 0;
  173. #define SQR(a) ((a) * (a))
  174. static float safe_log(float x)
  175. {
  176. av_assert0(x >= 0);
  177. if (x)
  178. return logf(x);
  179. return -26;
  180. }
  181. static int fir_to_phase(SincContext *s, float **h, int *len, int *post_len, float phase)
  182. {
  183. float *pi_wraps, *work, phase1 = (phase > 50.f ? 100.f - phase : phase) / 50.f;
  184. int i, work_len, begin, end, imp_peak = 0, peak = 0;
  185. float imp_sum = 0, peak_imp_sum = 0;
  186. float prev_angle2 = 0, cum_2pi = 0, prev_angle1 = 0, cum_1pi = 0;
  187. for (i = *len, work_len = 2 * 2 * 8; i > 1; work_len <<= 1, i >>= 1);
  188. work = av_calloc(work_len + 2, sizeof(*work)); /* +2: (UN)PACK */
  189. pi_wraps = av_calloc(((work_len + 2) / 2), sizeof(*pi_wraps));
  190. if (!work || !pi_wraps)
  191. return AVERROR(ENOMEM);
  192. memcpy(work, *h, *len * sizeof(*work));
  193. av_rdft_end(s->rdft);
  194. av_rdft_end(s->irdft);
  195. s->rdft = s->irdft = NULL;
  196. s->rdft = av_rdft_init(av_log2(work_len), DFT_R2C);
  197. s->irdft = av_rdft_init(av_log2(work_len), IDFT_C2R);
  198. if (!s->rdft || !s->irdft)
  199. return AVERROR(ENOMEM);
  200. av_rdft_calc(s->rdft, work); /* Cepstral: */
  201. UNPACK(work, work_len);
  202. for (i = 0; i <= work_len; i += 2) {
  203. float angle = atan2f(work[i + 1], work[i]);
  204. float detect = 2 * M_PI;
  205. float delta = angle - prev_angle2;
  206. float adjust = detect * ((delta < -detect * .7f) - (delta > detect * .7f));
  207. prev_angle2 = angle;
  208. cum_2pi += adjust;
  209. angle += cum_2pi;
  210. detect = M_PI;
  211. delta = angle - prev_angle1;
  212. adjust = detect * ((delta < -detect * .7f) - (delta > detect * .7f));
  213. prev_angle1 = angle;
  214. cum_1pi += fabsf(adjust); /* fabs for when 2pi and 1pi have combined */
  215. pi_wraps[i >> 1] = cum_1pi;
  216. work[i] = safe_log(sqrtf(SQR(work[i]) + SQR(work[i + 1])));
  217. work[i + 1] = 0;
  218. }
  219. PACK(work, work_len);
  220. av_rdft_calc(s->irdft, work);
  221. for (i = 0; i < work_len; i++)
  222. work[i] *= 2.f / work_len;
  223. for (i = 1; i < work_len / 2; i++) { /* Window to reject acausal components */
  224. work[i] *= 2;
  225. work[i + work_len / 2] = 0;
  226. }
  227. av_rdft_calc(s->rdft, work);
  228. for (i = 2; i < work_len; i += 2) /* Interpolate between linear & min phase */
  229. work[i + 1] = phase1 * i / work_len * pi_wraps[work_len >> 1] + (1 - phase1) * (work[i + 1] + pi_wraps[i >> 1]) - pi_wraps[i >> 1];
  230. work[0] = exp(work[0]);
  231. work[1] = exp(work[1]);
  232. for (i = 2; i < work_len; i += 2) {
  233. float x = expf(work[i]);
  234. work[i ] = x * cosf(work[i + 1]);
  235. work[i + 1] = x * sinf(work[i + 1]);
  236. }
  237. av_rdft_calc(s->irdft, work);
  238. for (i = 0; i < work_len; i++)
  239. work[i] *= 2.f / work_len;
  240. /* Find peak pos. */
  241. for (i = 0; i <= (int) (pi_wraps[work_len >> 1] / M_PI + .5f); i++) {
  242. imp_sum += work[i];
  243. if (fabs(imp_sum) > fabs(peak_imp_sum)) {
  244. peak_imp_sum = imp_sum;
  245. peak = i;
  246. }
  247. if (work[i] > work[imp_peak]) /* For debug check only */
  248. imp_peak = i;
  249. }
  250. while (peak && fabsf(work[peak - 1]) > fabsf(work[peak]) && (work[peak - 1] * work[peak] > 0)) {
  251. peak--;
  252. }
  253. if (!phase1) {
  254. begin = 0;
  255. } else if (phase1 == 1) {
  256. begin = peak - *len / 2;
  257. } else {
  258. begin = (.997f - (2 - phase1) * .22f) * *len + .5f;
  259. end = (.997f + (0 - phase1) * .22f) * *len + .5f;
  260. begin = peak - (begin & ~3);
  261. end = peak + 1 + ((end + 3) & ~3);
  262. *len = end - begin;
  263. *h = av_realloc_f(*h, *len, sizeof(**h));
  264. if (!*h) {
  265. av_free(pi_wraps);
  266. av_free(work);
  267. return AVERROR(ENOMEM);
  268. }
  269. }
  270. for (i = 0; i < *len; i++) {
  271. (*h)[i] = work[(begin + (phase > 50.f ? *len - 1 - i : i) + work_len) & (work_len - 1)];
  272. }
  273. *post_len = phase > 50 ? peak - begin : begin + *len - (peak + 1);
  274. av_log(s, AV_LOG_DEBUG, "%d nPI=%g peak-sum@%i=%g (val@%i=%g); len=%i post=%i (%g%%)\n",
  275. work_len, pi_wraps[work_len >> 1] / M_PI, peak, peak_imp_sum, imp_peak,
  276. work[imp_peak], *len, *post_len, 100.f - 100.f * *post_len / (*len - 1));
  277. av_free(pi_wraps);
  278. av_free(work);
  279. return 0;
  280. }
  281. static int config_output(AVFilterLink *outlink)
  282. {
  283. AVFilterContext *ctx = outlink->src;
  284. SincContext *s = ctx->priv;
  285. float Fn = s->sample_rate * .5f;
  286. float *h[2];
  287. int i, n, post_peak, longer;
  288. outlink->sample_rate = s->sample_rate;
  289. s->pts = 0;
  290. if (s->Fc0 >= Fn || s->Fc1 >= Fn) {
  291. av_log(ctx, AV_LOG_ERROR,
  292. "filter frequency must be less than %d/2.\n", s->sample_rate);
  293. return AVERROR(EINVAL);
  294. }
  295. h[0] = lpf(Fn, s->Fc0, s->tbw0, &s->num_taps[0], s->att, &s->beta, s->round);
  296. h[1] = lpf(Fn, s->Fc1, s->tbw1, &s->num_taps[1], s->att, &s->beta, s->round);
  297. if (h[0])
  298. invert(h[0], s->num_taps[0]);
  299. longer = s->num_taps[1] > s->num_taps[0];
  300. n = s->num_taps[longer];
  301. if (h[0] && h[1]) {
  302. for (i = 0; i < s->num_taps[!longer]; i++)
  303. h[longer][i + (n - s->num_taps[!longer]) / 2] += h[!longer][i];
  304. if (s->Fc0 < s->Fc1)
  305. invert(h[longer], n);
  306. av_free(h[!longer]);
  307. }
  308. if (s->phase != 50.f) {
  309. int ret = fir_to_phase(s, &h[longer], &n, &post_peak, s->phase);
  310. if (ret < 0)
  311. return ret;
  312. } else {
  313. post_peak = n >> 1;
  314. }
  315. s->n = 1 << (av_log2(n) + 1);
  316. s->rdft_len = 1 << av_log2(n);
  317. s->coeffs = av_calloc(s->n, sizeof(*s->coeffs));
  318. if (!s->coeffs)
  319. return AVERROR(ENOMEM);
  320. for (i = 0; i < n; i++)
  321. s->coeffs[i] = h[longer][i];
  322. av_free(h[longer]);
  323. av_rdft_end(s->rdft);
  324. av_rdft_end(s->irdft);
  325. s->rdft = s->irdft = NULL;
  326. return 0;
  327. }
  328. static av_cold void uninit(AVFilterContext *ctx)
  329. {
  330. SincContext *s = ctx->priv;
  331. av_freep(&s->coeffs);
  332. av_rdft_end(s->rdft);
  333. av_rdft_end(s->irdft);
  334. s->rdft = s->irdft = NULL;
  335. }
  336. static const AVFilterPad sinc_outputs[] = {
  337. {
  338. .name = "default",
  339. .type = AVMEDIA_TYPE_AUDIO,
  340. .config_props = config_output,
  341. .request_frame = request_frame,
  342. },
  343. { NULL }
  344. };
  345. #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  346. #define OFFSET(x) offsetof(SincContext, x)
  347. static const AVOption sinc_options[] = {
  348. { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT_MAX, AF },
  349. { "r", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT_MAX, AF },
  350. { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, AF },
  351. { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, AF },
  352. { "hp", "set high-pass filter frequency", OFFSET(Fc0), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, INT_MAX, AF },
  353. { "lp", "set low-pass filter frequency", OFFSET(Fc1), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, INT_MAX, AF },
  354. { "phase", "set filter phase response", OFFSET(phase), AV_OPT_TYPE_FLOAT, {.dbl=50}, 0, 100, AF },
  355. { "beta", "set kaiser window beta", OFFSET(beta), AV_OPT_TYPE_FLOAT, {.dbl=-1}, -1, 256, AF },
  356. { "att", "set stop-band attenuation", OFFSET(att), AV_OPT_TYPE_FLOAT, {.dbl=120}, 40, 180, AF },
  357. { "round", "enable rounding", OFFSET(round), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, AF },
  358. { "hptaps", "set number of taps for high-pass filter", OFFSET(num_taps[0]), AV_OPT_TYPE_INT, {.i64=0}, 0, 32768, AF },
  359. { "lptaps", "set number of taps for low-pass filter", OFFSET(num_taps[1]), AV_OPT_TYPE_INT, {.i64=0}, 0, 32768, AF },
  360. { NULL }
  361. };
  362. AVFILTER_DEFINE_CLASS(sinc);
  363. AVFilter ff_asrc_sinc = {
  364. .name = "sinc",
  365. .description = NULL_IF_CONFIG_SMALL("Generate a sinc kaiser-windowed low-pass, high-pass, band-pass, or band-reject FIR coefficients."),
  366. .priv_size = sizeof(SincContext),
  367. .priv_class = &sinc_class,
  368. .query_formats = query_formats,
  369. .uninit = uninit,
  370. .inputs = NULL,
  371. .outputs = sinc_outputs,
  372. };