resample.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /*
  2. * audio resampling
  3. * Copyright (c) 2004-2012 Michael Niedermayer <michaelni@gmx.at>
  4. * bessel function: Copyright (c) 2006 Xiaogang Zhang
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * audio resampling
  25. * @author Michael Niedermayer <michaelni@gmx.at>
  26. */
  27. #include "libavutil/avassert.h"
  28. #include "libavutil/cpu.h"
  29. #include "resample.h"
  30. static inline double eval_poly(const double *coeff, int size, double x) {
  31. double sum = coeff[size-1];
  32. int i;
  33. for (i = size-2; i >= 0; --i) {
  34. sum *= x;
  35. sum += coeff[i];
  36. }
  37. return sum;
  38. }
  39. /**
  40. * 0th order modified bessel function of the first kind.
  41. * Algorithm taken from the Boost project, source:
  42. * https://searchcode.com/codesearch/view/14918379/
  43. * Use, modification and distribution are subject to the
  44. * Boost Software License, Version 1.0 (see notice below).
  45. * Boost Software License - Version 1.0 - August 17th, 2003
  46. Permission is hereby granted, free of charge, to any person or organization
  47. obtaining a copy of the software and accompanying documentation covered by
  48. this license (the "Software") to use, reproduce, display, distribute,
  49. execute, and transmit the Software, and to prepare derivative works of the
  50. Software, and to permit third-parties to whom the Software is furnished to
  51. do so, all subject to the following:
  52. The copyright notices in the Software and this entire statement, including
  53. the above license grant, this restriction and the following disclaimer,
  54. must be included in all copies of the Software, in whole or in part, and
  55. all derivative works of the Software, unless such copies or derivative
  56. works are solely in the form of machine-executable object code generated by
  57. a source language processor.
  58. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  59. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  60. FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  61. SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  62. FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  63. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  64. DEALINGS IN THE SOFTWARE.
  65. */
  66. static double bessel(double x) {
  67. // Modified Bessel function of the first kind of order zero
  68. // minimax rational approximations on intervals, see
  69. // Blair and Edwards, Chalk River Report AECL-4928, 1974
  70. static const double p1[] = {
  71. -2.2335582639474375249e+15,
  72. -5.5050369673018427753e+14,
  73. -3.2940087627407749166e+13,
  74. -8.4925101247114157499e+11,
  75. -1.1912746104985237192e+10,
  76. -1.0313066708737980747e+08,
  77. -5.9545626019847898221e+05,
  78. -2.4125195876041896775e+03,
  79. -7.0935347449210549190e+00,
  80. -1.5453977791786851041e-02,
  81. -2.5172644670688975051e-05,
  82. -3.0517226450451067446e-08,
  83. -2.6843448573468483278e-11,
  84. -1.5982226675653184646e-14,
  85. -5.2487866627945699800e-18,
  86. };
  87. static const double q1[] = {
  88. -2.2335582639474375245e+15,
  89. 7.8858692566751002988e+12,
  90. -1.2207067397808979846e+10,
  91. 1.0377081058062166144e+07,
  92. -4.8527560179962773045e+03,
  93. 1.0,
  94. };
  95. static const double p2[] = {
  96. -2.2210262233306573296e-04,
  97. 1.3067392038106924055e-02,
  98. -4.4700805721174453923e-01,
  99. 5.5674518371240761397e+00,
  100. -2.3517945679239481621e+01,
  101. 3.1611322818701131207e+01,
  102. -9.6090021968656180000e+00,
  103. };
  104. static const double q2[] = {
  105. -5.5194330231005480228e-04,
  106. 3.2547697594819615062e-02,
  107. -1.1151759188741312645e+00,
  108. 1.3982595353892851542e+01,
  109. -6.0228002066743340583e+01,
  110. 8.5539563258012929600e+01,
  111. -3.1446690275135491500e+01,
  112. 1.0,
  113. };
  114. double y, r, factor;
  115. if (x == 0)
  116. return 1.0;
  117. x = fabs(x);
  118. if (x <= 15) {
  119. y = x * x;
  120. return eval_poly(p1, FF_ARRAY_ELEMS(p1), y) / eval_poly(q1, FF_ARRAY_ELEMS(q1), y);
  121. }
  122. else {
  123. y = 1 / x - 1.0 / 15;
  124. r = eval_poly(p2, FF_ARRAY_ELEMS(p2), y) / eval_poly(q2, FF_ARRAY_ELEMS(q2), y);
  125. factor = exp(x) / sqrt(x);
  126. return factor * r;
  127. }
  128. }
  129. /**
  130. * builds a polyphase filterbank.
  131. * @param factor resampling factor
  132. * @param scale wanted sum of coefficients for each filter
  133. * @param filter_type filter type
  134. * @param kaiser_beta kaiser window beta
  135. * @return 0 on success, negative on error
  136. */
  137. static int build_filter(ResampleContext *c, void *filter, double factor, int tap_count, int alloc, int phase_count, int scale,
  138. int filter_type, double kaiser_beta){
  139. int ph, i;
  140. int ph_nb = phase_count % 2 ? phase_count : phase_count / 2 + 1;
  141. double x, y, w, t, s;
  142. double *tab = av_malloc_array(tap_count+1, sizeof(*tab));
  143. double *sin_lut = av_malloc_array(ph_nb, sizeof(*sin_lut));
  144. const int center= (tap_count-1)/2;
  145. double norm = 0;
  146. int ret = AVERROR(ENOMEM);
  147. if (!tab || !sin_lut)
  148. goto fail;
  149. av_assert0(tap_count == 1 || tap_count % 2 == 0);
  150. /* if upsampling, only need to interpolate, no filter */
  151. if (factor > 1.0)
  152. factor = 1.0;
  153. if (factor == 1.0) {
  154. for (ph = 0; ph < ph_nb; ph++)
  155. sin_lut[ph] = sin(M_PI * ph / phase_count) * (center & 1 ? 1 : -1);
  156. }
  157. for(ph = 0; ph < ph_nb; ph++) {
  158. s = sin_lut[ph];
  159. for(i=0;i<tap_count;i++) {
  160. x = M_PI * ((double)(i - center) - (double)ph / phase_count) * factor;
  161. if (x == 0) y = 1.0;
  162. else if (factor == 1.0)
  163. y = s / x;
  164. else
  165. y = sin(x) / x;
  166. switch(filter_type){
  167. case SWR_FILTER_TYPE_CUBIC:{
  168. const float d= -0.5; //first order derivative = -0.5
  169. x = fabs(((double)(i - center) - (double)ph / phase_count) * factor);
  170. if(x<1.0) y= 1 - 3*x*x + 2*x*x*x + d*( -x*x + x*x*x);
  171. else y= d*(-4 + 8*x - 5*x*x + x*x*x);
  172. break;}
  173. case SWR_FILTER_TYPE_BLACKMAN_NUTTALL:
  174. w = 2.0*x / (factor*tap_count);
  175. t = -cos(w);
  176. y *= 0.3635819 - 0.4891775 * t + 0.1365995 * (2*t*t-1) - 0.0106411 * (4*t*t*t - 3*t);
  177. break;
  178. case SWR_FILTER_TYPE_KAISER:
  179. w = 2.0*x / (factor*tap_count*M_PI);
  180. y *= bessel(kaiser_beta*sqrt(FFMAX(1-w*w, 0)));
  181. break;
  182. default:
  183. av_assert0(0);
  184. }
  185. tab[i] = y;
  186. s = -s;
  187. if (!ph)
  188. norm += y;
  189. }
  190. /* normalize so that an uniform color remains the same */
  191. switch(c->format){
  192. case AV_SAMPLE_FMT_S16P:
  193. for(i=0;i<tap_count;i++)
  194. ((int16_t*)filter)[ph * alloc + i] = av_clip_int16(lrintf(tab[i] * scale / norm));
  195. if (phase_count % 2) break;
  196. for (i = 0; i < tap_count; i++)
  197. ((int16_t*)filter)[(phase_count-ph) * alloc + tap_count-1-i] = ((int16_t*)filter)[ph * alloc + i];
  198. break;
  199. case AV_SAMPLE_FMT_S32P:
  200. for(i=0;i<tap_count;i++)
  201. ((int32_t*)filter)[ph * alloc + i] = av_clipl_int32(llrint(tab[i] * scale / norm));
  202. if (phase_count % 2) break;
  203. for (i = 0; i < tap_count; i++)
  204. ((int32_t*)filter)[(phase_count-ph) * alloc + tap_count-1-i] = ((int32_t*)filter)[ph * alloc + i];
  205. break;
  206. case AV_SAMPLE_FMT_FLTP:
  207. for(i=0;i<tap_count;i++)
  208. ((float*)filter)[ph * alloc + i] = tab[i] * scale / norm;
  209. if (phase_count % 2) break;
  210. for (i = 0; i < tap_count; i++)
  211. ((float*)filter)[(phase_count-ph) * alloc + tap_count-1-i] = ((float*)filter)[ph * alloc + i];
  212. break;
  213. case AV_SAMPLE_FMT_DBLP:
  214. for(i=0;i<tap_count;i++)
  215. ((double*)filter)[ph * alloc + i] = tab[i] * scale / norm;
  216. if (phase_count % 2) break;
  217. for (i = 0; i < tap_count; i++)
  218. ((double*)filter)[(phase_count-ph) * alloc + tap_count-1-i] = ((double*)filter)[ph * alloc + i];
  219. break;
  220. }
  221. }
  222. #if 0
  223. {
  224. #define LEN 1024
  225. int j,k;
  226. double sine[LEN + tap_count];
  227. double filtered[LEN];
  228. double maxff=-2, minff=2, maxsf=-2, minsf=2;
  229. for(i=0; i<LEN; i++){
  230. double ss=0, sf=0, ff=0;
  231. for(j=0; j<LEN+tap_count; j++)
  232. sine[j]= cos(i*j*M_PI/LEN);
  233. for(j=0; j<LEN; j++){
  234. double sum=0;
  235. ph=0;
  236. for(k=0; k<tap_count; k++)
  237. sum += filter[ph * tap_count + k] * sine[k+j];
  238. filtered[j]= sum / (1<<FILTER_SHIFT);
  239. ss+= sine[j + center] * sine[j + center];
  240. ff+= filtered[j] * filtered[j];
  241. sf+= sine[j + center] * filtered[j];
  242. }
  243. ss= sqrt(2*ss/LEN);
  244. ff= sqrt(2*ff/LEN);
  245. sf= 2*sf/LEN;
  246. maxff= FFMAX(maxff, ff);
  247. minff= FFMIN(minff, ff);
  248. maxsf= FFMAX(maxsf, sf);
  249. minsf= FFMIN(minsf, sf);
  250. if(i%11==0){
  251. av_log(NULL, AV_LOG_ERROR, "i:%4d ss:%f ff:%13.6e-%13.6e sf:%13.6e-%13.6e\n", i, ss, maxff, minff, maxsf, minsf);
  252. minff=minsf= 2;
  253. maxff=maxsf= -2;
  254. }
  255. }
  256. }
  257. #endif
  258. ret = 0;
  259. fail:
  260. av_free(tab);
  261. av_free(sin_lut);
  262. return ret;
  263. }
  264. static void resample_free(ResampleContext **cc){
  265. ResampleContext *c = *cc;
  266. if(!c)
  267. return;
  268. av_freep(&c->filter_bank);
  269. av_freep(cc);
  270. }
  271. static ResampleContext *resample_init(ResampleContext *c, int out_rate, int in_rate, int filter_size, int phase_shift, int linear,
  272. double cutoff0, enum AVSampleFormat format, enum SwrFilterType filter_type, double kaiser_beta,
  273. double precision, int cheby, int exact_rational)
  274. {
  275. double cutoff = cutoff0? cutoff0 : 0.97;
  276. double factor= FFMIN(out_rate * cutoff / in_rate, 1.0);
  277. int phase_count= 1<<phase_shift;
  278. int phase_count_compensation = phase_count;
  279. int filter_length = FFMAX((int)ceil(filter_size/factor), 1);
  280. if (filter_length > 1)
  281. filter_length = FFALIGN(filter_length, 2);
  282. if (exact_rational) {
  283. int phase_count_exact, phase_count_exact_den;
  284. av_reduce(&phase_count_exact, &phase_count_exact_den, out_rate, in_rate, INT_MAX);
  285. if (phase_count_exact <= phase_count) {
  286. phase_count_compensation = phase_count_exact * (phase_count / phase_count_exact);
  287. phase_count = phase_count_exact;
  288. }
  289. }
  290. if (!c || c->phase_count != phase_count || c->linear!=linear || c->factor != factor
  291. || c->filter_length != filter_length || c->format != format
  292. || c->filter_type != filter_type || c->kaiser_beta != kaiser_beta) {
  293. resample_free(&c);
  294. c = av_mallocz(sizeof(*c));
  295. if (!c)
  296. return NULL;
  297. c->format= format;
  298. c->felem_size= av_get_bytes_per_sample(c->format);
  299. switch(c->format){
  300. case AV_SAMPLE_FMT_S16P:
  301. c->filter_shift = 15;
  302. break;
  303. case AV_SAMPLE_FMT_S32P:
  304. c->filter_shift = 30;
  305. break;
  306. case AV_SAMPLE_FMT_FLTP:
  307. case AV_SAMPLE_FMT_DBLP:
  308. c->filter_shift = 0;
  309. break;
  310. default:
  311. av_log(NULL, AV_LOG_ERROR, "Unsupported sample format\n");
  312. av_assert0(0);
  313. }
  314. if (filter_size/factor > INT32_MAX/256) {
  315. av_log(NULL, AV_LOG_ERROR, "Filter length too large\n");
  316. goto error;
  317. }
  318. c->phase_count = phase_count;
  319. c->linear = linear;
  320. c->factor = factor;
  321. c->filter_length = filter_length;
  322. c->filter_alloc = FFALIGN(c->filter_length, 8);
  323. c->filter_bank = av_calloc(c->filter_alloc, (phase_count+1)*c->felem_size);
  324. c->filter_type = filter_type;
  325. c->kaiser_beta = kaiser_beta;
  326. c->phase_count_compensation = phase_count_compensation;
  327. if (!c->filter_bank)
  328. goto error;
  329. if (build_filter(c, (void*)c->filter_bank, factor, c->filter_length, c->filter_alloc, phase_count, 1<<c->filter_shift, filter_type, kaiser_beta))
  330. goto error;
  331. memcpy(c->filter_bank + (c->filter_alloc*phase_count+1)*c->felem_size, c->filter_bank, (c->filter_alloc-1)*c->felem_size);
  332. memcpy(c->filter_bank + (c->filter_alloc*phase_count )*c->felem_size, c->filter_bank + (c->filter_alloc - 1)*c->felem_size, c->felem_size);
  333. }
  334. c->compensation_distance= 0;
  335. if(!av_reduce(&c->src_incr, &c->dst_incr, out_rate, in_rate * (int64_t)phase_count, INT32_MAX/2))
  336. goto error;
  337. while (c->dst_incr < (1<<20) && c->src_incr < (1<<20)) {
  338. c->dst_incr *= 2;
  339. c->src_incr *= 2;
  340. }
  341. c->ideal_dst_incr = c->dst_incr;
  342. c->dst_incr_div = c->dst_incr / c->src_incr;
  343. c->dst_incr_mod = c->dst_incr % c->src_incr;
  344. c->index= -phase_count*((c->filter_length-1)/2);
  345. c->frac= 0;
  346. swri_resample_dsp_init(c);
  347. return c;
  348. error:
  349. av_freep(&c->filter_bank);
  350. av_free(c);
  351. return NULL;
  352. }
  353. static int rebuild_filter_bank_with_compensation(ResampleContext *c)
  354. {
  355. uint8_t *new_filter_bank;
  356. int new_src_incr, new_dst_incr;
  357. int phase_count = c->phase_count_compensation;
  358. int ret;
  359. if (phase_count == c->phase_count)
  360. return 0;
  361. av_assert0(!c->frac && !c->dst_incr_mod);
  362. new_filter_bank = av_calloc(c->filter_alloc, (phase_count + 1) * c->felem_size);
  363. if (!new_filter_bank)
  364. return AVERROR(ENOMEM);
  365. ret = build_filter(c, new_filter_bank, c->factor, c->filter_length, c->filter_alloc,
  366. phase_count, 1 << c->filter_shift, c->filter_type, c->kaiser_beta);
  367. if (ret < 0) {
  368. av_freep(&new_filter_bank);
  369. return ret;
  370. }
  371. memcpy(new_filter_bank + (c->filter_alloc*phase_count+1)*c->felem_size, new_filter_bank, (c->filter_alloc-1)*c->felem_size);
  372. memcpy(new_filter_bank + (c->filter_alloc*phase_count )*c->felem_size, new_filter_bank + (c->filter_alloc - 1)*c->felem_size, c->felem_size);
  373. if (!av_reduce(&new_src_incr, &new_dst_incr, c->src_incr,
  374. c->dst_incr * (int64_t)(phase_count/c->phase_count), INT32_MAX/2))
  375. {
  376. av_freep(&new_filter_bank);
  377. return AVERROR(EINVAL);
  378. }
  379. c->src_incr = new_src_incr;
  380. c->dst_incr = new_dst_incr;
  381. while (c->dst_incr < (1<<20) && c->src_incr < (1<<20)) {
  382. c->dst_incr *= 2;
  383. c->src_incr *= 2;
  384. }
  385. c->ideal_dst_incr = c->dst_incr;
  386. c->dst_incr_div = c->dst_incr / c->src_incr;
  387. c->dst_incr_mod = c->dst_incr % c->src_incr;
  388. c->index *= phase_count / c->phase_count;
  389. c->phase_count = phase_count;
  390. av_freep(&c->filter_bank);
  391. c->filter_bank = new_filter_bank;
  392. return 0;
  393. }
  394. static int set_compensation(ResampleContext *c, int sample_delta, int compensation_distance){
  395. int ret;
  396. if (compensation_distance && sample_delta) {
  397. ret = rebuild_filter_bank_with_compensation(c);
  398. if (ret < 0)
  399. return ret;
  400. }
  401. c->compensation_distance= compensation_distance;
  402. if (compensation_distance)
  403. c->dst_incr = c->ideal_dst_incr - c->ideal_dst_incr * (int64_t)sample_delta / compensation_distance;
  404. else
  405. c->dst_incr = c->ideal_dst_incr;
  406. c->dst_incr_div = c->dst_incr / c->src_incr;
  407. c->dst_incr_mod = c->dst_incr % c->src_incr;
  408. return 0;
  409. }
  410. static int multiple_resample(ResampleContext *c, AudioData *dst, int dst_size, AudioData *src, int src_size, int *consumed){
  411. int i;
  412. int64_t max_src_size = (INT64_MAX/2 / c->phase_count) / c->src_incr;
  413. if (c->compensation_distance)
  414. dst_size = FFMIN(dst_size, c->compensation_distance);
  415. src_size = FFMIN(src_size, max_src_size);
  416. *consumed = 0;
  417. if (c->filter_length == 1 && c->phase_count == 1) {
  418. int64_t index2= (1LL<<32)*c->frac/c->src_incr + (1LL<<32)*c->index + 1;
  419. int64_t incr= (1LL<<32) * c->dst_incr / c->src_incr + 1;
  420. int new_size = (src_size * (int64_t)c->src_incr - c->frac + c->dst_incr - 1) / c->dst_incr;
  421. dst_size = FFMAX(FFMIN(dst_size, new_size), 0);
  422. if (dst_size > 0) {
  423. for (i = 0; i < dst->ch_count; i++) {
  424. c->dsp.resample_one(dst->ch[i], src->ch[i], dst_size, index2, incr);
  425. if (i+1 == dst->ch_count) {
  426. c->index += dst_size * c->dst_incr_div;
  427. c->index += (c->frac + dst_size * (int64_t)c->dst_incr_mod) / c->src_incr;
  428. av_assert2(c->index >= 0);
  429. *consumed = c->index;
  430. c->frac = (c->frac + dst_size * (int64_t)c->dst_incr_mod) % c->src_incr;
  431. c->index = 0;
  432. }
  433. }
  434. }
  435. } else {
  436. int64_t end_index = (1LL + src_size - c->filter_length) * c->phase_count;
  437. int64_t delta_frac = (end_index - c->index) * c->src_incr - c->frac;
  438. int delta_n = (delta_frac + c->dst_incr - 1) / c->dst_incr;
  439. int (*resample_func)(struct ResampleContext *c, void *dst,
  440. const void *src, int n, int update_ctx);
  441. dst_size = FFMAX(FFMIN(dst_size, delta_n), 0);
  442. if (dst_size > 0) {
  443. /* resample_linear and resample_common should have same behavior
  444. * when frac and dst_incr_mod are zero */
  445. resample_func = (c->linear && (c->frac || c->dst_incr_mod)) ?
  446. c->dsp.resample_linear : c->dsp.resample_common;
  447. for (i = 0; i < dst->ch_count; i++)
  448. *consumed = resample_func(c, dst->ch[i], src->ch[i], dst_size, i+1 == dst->ch_count);
  449. }
  450. }
  451. if (c->compensation_distance) {
  452. c->compensation_distance -= dst_size;
  453. if (!c->compensation_distance) {
  454. c->dst_incr = c->ideal_dst_incr;
  455. c->dst_incr_div = c->dst_incr / c->src_incr;
  456. c->dst_incr_mod = c->dst_incr % c->src_incr;
  457. }
  458. }
  459. return dst_size;
  460. }
  461. static int64_t get_delay(struct SwrContext *s, int64_t base){
  462. ResampleContext *c = s->resample;
  463. int64_t num = s->in_buffer_count - (c->filter_length-1)/2;
  464. num *= c->phase_count;
  465. num -= c->index;
  466. num *= c->src_incr;
  467. num -= c->frac;
  468. return av_rescale(num, base, s->in_sample_rate*(int64_t)c->src_incr * c->phase_count);
  469. }
  470. static int64_t get_out_samples(struct SwrContext *s, int in_samples) {
  471. ResampleContext *c = s->resample;
  472. // The + 2 are added to allow implementations to be slightly inaccurate, they should not be needed currently.
  473. // They also make it easier to proof that changes and optimizations do not
  474. // break the upper bound.
  475. int64_t num = s->in_buffer_count + 2LL + in_samples;
  476. num *= c->phase_count;
  477. num -= c->index;
  478. num = av_rescale_rnd(num, s->out_sample_rate, ((int64_t)s->in_sample_rate) * c->phase_count, AV_ROUND_UP) + 2;
  479. if (c->compensation_distance) {
  480. if (num > INT_MAX)
  481. return AVERROR(EINVAL);
  482. num = FFMAX(num, (num * c->ideal_dst_incr - 1) / c->dst_incr + 1);
  483. }
  484. return num;
  485. }
  486. static int resample_flush(struct SwrContext *s) {
  487. ResampleContext *c = s->resample;
  488. AudioData *a= &s->in_buffer;
  489. int i, j, ret;
  490. int reflection = (FFMIN(s->in_buffer_count, c->filter_length) + 1) / 2;
  491. if((ret = swri_realloc_audio(a, s->in_buffer_index + s->in_buffer_count + reflection)) < 0)
  492. return ret;
  493. av_assert0(a->planar);
  494. for(i=0; i<a->ch_count; i++){
  495. for(j=0; j<reflection; j++){
  496. memcpy(a->ch[i] + (s->in_buffer_index+s->in_buffer_count+j )*a->bps,
  497. a->ch[i] + (s->in_buffer_index+s->in_buffer_count-j-1)*a->bps, a->bps);
  498. }
  499. }
  500. s->in_buffer_count += reflection;
  501. return 0;
  502. }
  503. // in fact the whole handle multiple ridiculously small buffers might need more thinking...
  504. static int invert_initial_buffer(ResampleContext *c, AudioData *dst, const AudioData *src,
  505. int in_count, int *out_idx, int *out_sz)
  506. {
  507. int n, ch, num = FFMIN(in_count + *out_sz, c->filter_length + 1), res;
  508. if (c->index >= 0)
  509. return 0;
  510. if ((res = swri_realloc_audio(dst, c->filter_length * 2 + 1)) < 0)
  511. return res;
  512. // copy
  513. for (n = *out_sz; n < num; n++) {
  514. for (ch = 0; ch < src->ch_count; ch++) {
  515. memcpy(dst->ch[ch] + ((c->filter_length + n) * c->felem_size),
  516. src->ch[ch] + ((n - *out_sz) * c->felem_size), c->felem_size);
  517. }
  518. }
  519. // if not enough data is in, return and wait for more
  520. if (num < c->filter_length + 1) {
  521. *out_sz = num;
  522. *out_idx = c->filter_length;
  523. return INT_MAX;
  524. }
  525. // else invert
  526. for (n = 1; n <= c->filter_length; n++) {
  527. for (ch = 0; ch < src->ch_count; ch++) {
  528. memcpy(dst->ch[ch] + ((c->filter_length - n) * c->felem_size),
  529. dst->ch[ch] + ((c->filter_length + n) * c->felem_size),
  530. c->felem_size);
  531. }
  532. }
  533. res = num - *out_sz;
  534. *out_idx = c->filter_length;
  535. while (c->index < 0) {
  536. --*out_idx;
  537. c->index += c->phase_count;
  538. }
  539. *out_sz = FFMAX(*out_sz + c->filter_length,
  540. 1 + c->filter_length * 2) - *out_idx;
  541. return FFMAX(res, 0);
  542. }
  543. struct Resampler const swri_resampler={
  544. resample_init,
  545. resample_free,
  546. multiple_resample,
  547. resample_flush,
  548. set_compensation,
  549. get_delay,
  550. invert_initial_buffer,
  551. get_out_samples,
  552. };