resample.c 22 KB

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