resample.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * audio resampling
  3. * Copyright (c) 2004-2012 Michael Niedermayer <michaelni@gmx.at>
  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. /**
  22. * @file
  23. * audio resampling
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include "libavutil/log.h"
  27. #include "libavutil/avassert.h"
  28. #include "swresample_internal.h"
  29. typedef struct ResampleContext {
  30. const AVClass *av_class;
  31. uint8_t *filter_bank;
  32. int filter_length;
  33. int filter_alloc;
  34. int ideal_dst_incr;
  35. int dst_incr;
  36. int index;
  37. int frac;
  38. int src_incr;
  39. int compensation_distance;
  40. int phase_shift;
  41. int phase_mask;
  42. int linear;
  43. enum SwrFilterType filter_type;
  44. int kaiser_beta;
  45. double factor;
  46. enum AVSampleFormat format;
  47. int felem_size;
  48. int filter_shift;
  49. } ResampleContext;
  50. /**
  51. * 0th order modified bessel function of the first kind.
  52. */
  53. static double bessel(double x){
  54. double v=1;
  55. double lastv=0;
  56. double t=1;
  57. int i;
  58. static const double inv[100]={
  59. 1.0/( 1* 1), 1.0/( 2* 2), 1.0/( 3* 3), 1.0/( 4* 4), 1.0/( 5* 5), 1.0/( 6* 6), 1.0/( 7* 7), 1.0/( 8* 8), 1.0/( 9* 9), 1.0/(10*10),
  60. 1.0/(11*11), 1.0/(12*12), 1.0/(13*13), 1.0/(14*14), 1.0/(15*15), 1.0/(16*16), 1.0/(17*17), 1.0/(18*18), 1.0/(19*19), 1.0/(20*20),
  61. 1.0/(21*21), 1.0/(22*22), 1.0/(23*23), 1.0/(24*24), 1.0/(25*25), 1.0/(26*26), 1.0/(27*27), 1.0/(28*28), 1.0/(29*29), 1.0/(30*30),
  62. 1.0/(31*31), 1.0/(32*32), 1.0/(33*33), 1.0/(34*34), 1.0/(35*35), 1.0/(36*36), 1.0/(37*37), 1.0/(38*38), 1.0/(39*39), 1.0/(40*40),
  63. 1.0/(41*41), 1.0/(42*42), 1.0/(43*43), 1.0/(44*44), 1.0/(45*45), 1.0/(46*46), 1.0/(47*47), 1.0/(48*48), 1.0/(49*49), 1.0/(50*50),
  64. 1.0/(51*51), 1.0/(52*52), 1.0/(53*53), 1.0/(54*54), 1.0/(55*55), 1.0/(56*56), 1.0/(57*57), 1.0/(58*58), 1.0/(59*59), 1.0/(60*60),
  65. 1.0/(61*61), 1.0/(62*62), 1.0/(63*63), 1.0/(64*64), 1.0/(65*65), 1.0/(66*66), 1.0/(67*67), 1.0/(68*68), 1.0/(69*69), 1.0/(70*70),
  66. 1.0/(71*71), 1.0/(72*72), 1.0/(73*73), 1.0/(74*74), 1.0/(75*75), 1.0/(76*76), 1.0/(77*77), 1.0/(78*78), 1.0/(79*79), 1.0/(80*80),
  67. 1.0/(81*81), 1.0/(82*82), 1.0/(83*83), 1.0/(84*84), 1.0/(85*85), 1.0/(86*86), 1.0/(87*87), 1.0/(88*88), 1.0/(89*89), 1.0/(90*90),
  68. 1.0/(91*91), 1.0/(92*92), 1.0/(93*93), 1.0/(94*94), 1.0/(95*95), 1.0/(96*96), 1.0/(97*97), 1.0/(98*98), 1.0/(99*99), 1.0/(10000)
  69. };
  70. x= x*x/4;
  71. for(i=0; v != lastv; i++){
  72. lastv=v;
  73. t *= x*inv[i];
  74. v += t;
  75. av_assert2(i<99);
  76. }
  77. return v;
  78. }
  79. /**
  80. * builds a polyphase filterbank.
  81. * @param factor resampling factor
  82. * @param scale wanted sum of coefficients for each filter
  83. * @param filter_type filter type
  84. * @param kaiser_beta kaiser window beta
  85. * @return 0 on success, negative on error
  86. */
  87. static int build_filter(ResampleContext *c, void *filter, double factor, int tap_count, int alloc, int phase_count, int scale,
  88. int filter_type, int kaiser_beta){
  89. int ph, i;
  90. double x, y, w;
  91. double *tab = av_malloc_array(tap_count, sizeof(*tab));
  92. const int center= (tap_count-1)/2;
  93. if (!tab)
  94. return AVERROR(ENOMEM);
  95. /* if upsampling, only need to interpolate, no filter */
  96. if (factor > 1.0)
  97. factor = 1.0;
  98. for(ph=0;ph<phase_count;ph++) {
  99. double norm = 0;
  100. for(i=0;i<tap_count;i++) {
  101. x = M_PI * ((double)(i - center) - (double)ph / phase_count) * factor;
  102. if (x == 0) y = 1.0;
  103. else y = sin(x) / x;
  104. switch(filter_type){
  105. case SWR_FILTER_TYPE_CUBIC:{
  106. const float d= -0.5; //first order derivative = -0.5
  107. x = fabs(((double)(i - center) - (double)ph / phase_count) * factor);
  108. if(x<1.0) y= 1 - 3*x*x + 2*x*x*x + d*( -x*x + x*x*x);
  109. else y= d*(-4 + 8*x - 5*x*x + x*x*x);
  110. break;}
  111. case SWR_FILTER_TYPE_BLACKMAN_NUTTALL:
  112. w = 2.0*x / (factor*tap_count) + M_PI;
  113. y *= 0.3635819 - 0.4891775 * cos(w) + 0.1365995 * cos(2*w) - 0.0106411 * cos(3*w);
  114. break;
  115. case SWR_FILTER_TYPE_KAISER:
  116. w = 2.0*x / (factor*tap_count*M_PI);
  117. y *= bessel(kaiser_beta*sqrt(FFMAX(1-w*w, 0)));
  118. break;
  119. default:
  120. av_assert0(0);
  121. }
  122. tab[i] = y;
  123. norm += y;
  124. }
  125. /* normalize so that an uniform color remains the same */
  126. switch(c->format){
  127. case AV_SAMPLE_FMT_S16P:
  128. for(i=0;i<tap_count;i++)
  129. ((int16_t*)filter)[ph * alloc + i] = av_clip(lrintf(tab[i] * scale / norm), INT16_MIN, INT16_MAX);
  130. break;
  131. case AV_SAMPLE_FMT_S32P:
  132. for(i=0;i<tap_count;i++)
  133. ((int32_t*)filter)[ph * alloc + i] = av_clip(lrintf(tab[i] * scale / norm), INT32_MIN, INT32_MAX);
  134. break;
  135. case AV_SAMPLE_FMT_FLTP:
  136. for(i=0;i<tap_count;i++)
  137. ((float*)filter)[ph * alloc + i] = tab[i] * scale / norm;
  138. break;
  139. case AV_SAMPLE_FMT_DBLP:
  140. for(i=0;i<tap_count;i++)
  141. ((double*)filter)[ph * alloc + i] = tab[i] * scale / norm;
  142. break;
  143. }
  144. }
  145. #if 0
  146. {
  147. #define LEN 1024
  148. int j,k;
  149. double sine[LEN + tap_count];
  150. double filtered[LEN];
  151. double maxff=-2, minff=2, maxsf=-2, minsf=2;
  152. for(i=0; i<LEN; i++){
  153. double ss=0, sf=0, ff=0;
  154. for(j=0; j<LEN+tap_count; j++)
  155. sine[j]= cos(i*j*M_PI/LEN);
  156. for(j=0; j<LEN; j++){
  157. double sum=0;
  158. ph=0;
  159. for(k=0; k<tap_count; k++)
  160. sum += filter[ph * tap_count + k] * sine[k+j];
  161. filtered[j]= sum / (1<<FILTER_SHIFT);
  162. ss+= sine[j + center] * sine[j + center];
  163. ff+= filtered[j] * filtered[j];
  164. sf+= sine[j + center] * filtered[j];
  165. }
  166. ss= sqrt(2*ss/LEN);
  167. ff= sqrt(2*ff/LEN);
  168. sf= 2*sf/LEN;
  169. maxff= FFMAX(maxff, ff);
  170. minff= FFMIN(minff, ff);
  171. maxsf= FFMAX(maxsf, sf);
  172. minsf= FFMIN(minsf, sf);
  173. if(i%11==0){
  174. 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);
  175. minff=minsf= 2;
  176. maxff=maxsf= -2;
  177. }
  178. }
  179. }
  180. #endif
  181. av_free(tab);
  182. return 0;
  183. }
  184. ResampleContext *swri_resample_init(ResampleContext *c, int out_rate, int in_rate, int filter_size, int phase_shift, int linear,
  185. double cutoff, enum AVSampleFormat format, enum SwrFilterType filter_type, int kaiser_beta){
  186. double factor= FFMIN(out_rate * cutoff / in_rate, 1.0);
  187. int phase_count= 1<<phase_shift;
  188. if (!c || c->phase_shift != phase_shift || c->linear!=linear || c->factor != factor
  189. || c->filter_length != FFMAX((int)ceil(filter_size/factor), 1) || c->format != format
  190. || c->filter_type != filter_type || c->kaiser_beta != kaiser_beta) {
  191. c = av_mallocz(sizeof(*c));
  192. if (!c)
  193. return NULL;
  194. c->format= format;
  195. c->felem_size= av_get_bytes_per_sample(c->format);
  196. switch(c->format){
  197. case AV_SAMPLE_FMT_S16P:
  198. c->filter_shift = 15;
  199. break;
  200. case AV_SAMPLE_FMT_S32P:
  201. c->filter_shift = 30;
  202. break;
  203. case AV_SAMPLE_FMT_FLTP:
  204. case AV_SAMPLE_FMT_DBLP:
  205. c->filter_shift = 0;
  206. break;
  207. default:
  208. av_log(NULL, AV_LOG_ERROR, "Unsupported sample format\n");
  209. return NULL;
  210. }
  211. if (filter_size/factor > INT32_MAX/256) {
  212. av_log(NULL, AV_LOG_ERROR, "Filter length too large\n");
  213. goto error;
  214. }
  215. c->phase_shift = phase_shift;
  216. c->phase_mask = phase_count - 1;
  217. c->linear = linear;
  218. c->factor = factor;
  219. c->filter_length = FFMAX((int)ceil(filter_size/factor), 1);
  220. c->filter_alloc = FFALIGN(c->filter_length, 8);
  221. c->filter_bank = av_mallocz(c->filter_alloc*(phase_count+1)*c->felem_size);
  222. c->filter_type = filter_type;
  223. c->kaiser_beta = kaiser_beta;
  224. if (!c->filter_bank)
  225. goto error;
  226. 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))
  227. goto error;
  228. memcpy(c->filter_bank + (c->filter_alloc*phase_count+1)*c->felem_size, c->filter_bank, (c->filter_alloc-1)*c->felem_size);
  229. 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);
  230. }
  231. c->compensation_distance= 0;
  232. if(!av_reduce(&c->src_incr, &c->dst_incr, out_rate, in_rate * (int64_t)phase_count, INT32_MAX/2))
  233. goto error;
  234. c->ideal_dst_incr= c->dst_incr;
  235. c->index= -phase_count*((c->filter_length-1)/2);
  236. c->frac= 0;
  237. return c;
  238. error:
  239. av_free(c->filter_bank);
  240. av_free(c);
  241. return NULL;
  242. }
  243. void swri_resample_free(ResampleContext **c){
  244. if(!*c)
  245. return;
  246. av_freep(&(*c)->filter_bank);
  247. av_freep(c);
  248. }
  249. int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance){
  250. ResampleContext *c;
  251. int ret;
  252. if (!s || compensation_distance < 0)
  253. return AVERROR(EINVAL);
  254. if (!compensation_distance && sample_delta)
  255. return AVERROR(EINVAL);
  256. if (!s->resample) {
  257. s->flags |= SWR_FLAG_RESAMPLE;
  258. ret = swr_init(s);
  259. if (ret < 0)
  260. return ret;
  261. }
  262. c= s->resample;
  263. c->compensation_distance= compensation_distance;
  264. if (compensation_distance)
  265. c->dst_incr = c->ideal_dst_incr - c->ideal_dst_incr * (int64_t)sample_delta / compensation_distance;
  266. else
  267. c->dst_incr = c->ideal_dst_incr;
  268. return 0;
  269. }
  270. #define RENAME(N) N ## _int16
  271. #define FILTER_SHIFT 15
  272. #define DELEM int16_t
  273. #define FELEM int16_t
  274. #define FELEM2 int32_t
  275. #define FELEML int64_t
  276. #define FELEM_MAX INT16_MAX
  277. #define FELEM_MIN INT16_MIN
  278. #define OUT(d, v) v = (v + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;\
  279. d = (unsigned)(v + 32768) > 65535 ? (v>>31) ^ 32767 : v
  280. #include "resample_template.c"
  281. #undef RENAME
  282. #undef FELEM
  283. #undef FELEM2
  284. #undef DELEM
  285. #undef FELEML
  286. #undef OUT
  287. #undef FELEM_MIN
  288. #undef FELEM_MAX
  289. #undef FILTER_SHIFT
  290. #define RENAME(N) N ## _int32
  291. #define FILTER_SHIFT 30
  292. #define DELEM int32_t
  293. #define FELEM int32_t
  294. #define FELEM2 int64_t
  295. #define FELEML int64_t
  296. #define FELEM_MAX INT32_MAX
  297. #define FELEM_MIN INT32_MIN
  298. #define OUT(d, v) v = (v + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;\
  299. d = (uint64_t)(v + 0x80000000) > 0xFFFFFFFF ? (v>>63) ^ 0x7FFFFFFF : v
  300. #include "resample_template.c"
  301. #undef RENAME
  302. #undef FELEM
  303. #undef FELEM2
  304. #undef DELEM
  305. #undef FELEML
  306. #undef OUT
  307. #undef FELEM_MIN
  308. #undef FELEM_MAX
  309. #undef FILTER_SHIFT
  310. #define RENAME(N) N ## _float
  311. #define FILTER_SHIFT 0
  312. #define DELEM float
  313. #define FELEM float
  314. #define FELEM2 float
  315. #define FELEML float
  316. #define OUT(d, v) d = v
  317. #include "resample_template.c"
  318. #undef RENAME
  319. #undef FELEM
  320. #undef FELEM2
  321. #undef DELEM
  322. #undef FELEML
  323. #undef OUT
  324. #undef FELEM_MIN
  325. #undef FELEM_MAX
  326. #undef FILTER_SHIFT
  327. #define RENAME(N) N ## _double
  328. #define FILTER_SHIFT 0
  329. #define DELEM double
  330. #define FELEM double
  331. #define FELEM2 double
  332. #define FELEML double
  333. #define OUT(d, v) d = v
  334. #include "resample_template.c"
  335. #undef RENAME
  336. #undef FELEM
  337. #undef FELEM2
  338. #undef DELEM
  339. #undef FELEML
  340. #undef OUT
  341. #undef FELEM_MIN
  342. #undef FELEM_MAX
  343. #undef FILTER_SHIFT
  344. // XXX FIXME the whole C loop should be written in asm so this x86 specific code here isnt needed
  345. #if HAVE_MMXEXT_INLINE
  346. #include "x86/resample_mmx.h"
  347. #define COMMON_CORE COMMON_CORE_INT16_MMX2
  348. #define RENAME(N) N ## _int16_mmx2
  349. #define FILTER_SHIFT 15
  350. #define DELEM int16_t
  351. #define FELEM int16_t
  352. #define FELEM2 int32_t
  353. #define FELEML int64_t
  354. #define FELEM_MAX INT16_MAX
  355. #define FELEM_MIN INT16_MIN
  356. #define OUT(d, v) v = (v + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;\
  357. d = (unsigned)(v + 32768) > 65535 ? (v>>31) ^ 32767 : v
  358. #include "resample_template.c"
  359. #undef COMMON_CORE
  360. #undef RENAME
  361. #undef FELEM
  362. #undef FELEM2
  363. #undef DELEM
  364. #undef FELEML
  365. #undef OUT
  366. #undef FELEM_MIN
  367. #undef FELEM_MAX
  368. #undef FILTER_SHIFT
  369. #if HAVE_SSSE3_INLINE
  370. #define COMMON_CORE COMMON_CORE_INT16_SSSE3
  371. #define RENAME(N) N ## _int16_ssse3
  372. #define FILTER_SHIFT 15
  373. #define DELEM int16_t
  374. #define FELEM int16_t
  375. #define FELEM2 int32_t
  376. #define FELEML int64_t
  377. #define FELEM_MAX INT16_MAX
  378. #define FELEM_MIN INT16_MIN
  379. #define OUT(d, v) v = (v + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;\
  380. d = (unsigned)(v + 32768) > 65535 ? (v>>31) ^ 32767 : v
  381. #include "resample_template.c"
  382. #endif
  383. #endif // HAVE_MMXEXT_INLINE
  384. int swri_multiple_resample(ResampleContext *c, AudioData *dst, int dst_size, AudioData *src, int src_size, int *consumed){
  385. int i, ret= -1;
  386. int av_unused mm_flags = av_get_cpu_flags();
  387. int need_emms= 0;
  388. for(i=0; i<dst->ch_count; i++){
  389. #if HAVE_MMXEXT_INLINE
  390. #if HAVE_SSSE3_INLINE
  391. if(c->format == AV_SAMPLE_FMT_S16P && (mm_flags&AV_CPU_FLAG_SSSE3)) ret= swri_resample_int16_ssse3(c, (int16_t*)dst->ch[i], (const int16_t*)src->ch[i], consumed, src_size, dst_size, i+1==dst->ch_count);
  392. else
  393. #endif
  394. if(c->format == AV_SAMPLE_FMT_S16P && (mm_flags&AV_CPU_FLAG_MMX2 )){
  395. ret= swri_resample_int16_mmx2 (c, (int16_t*)dst->ch[i], (const int16_t*)src->ch[i], consumed, src_size, dst_size, i+1==dst->ch_count);
  396. need_emms= 1;
  397. } else
  398. #endif
  399. if(c->format == AV_SAMPLE_FMT_S16P) ret= swri_resample_int16(c, (int16_t*)dst->ch[i], (const int16_t*)src->ch[i], consumed, src_size, dst_size, i+1==dst->ch_count);
  400. else if(c->format == AV_SAMPLE_FMT_S32P) ret= swri_resample_int32(c, (int32_t*)dst->ch[i], (const int32_t*)src->ch[i], consumed, src_size, dst_size, i+1==dst->ch_count);
  401. else if(c->format == AV_SAMPLE_FMT_FLTP) ret= swri_resample_float(c, (float *)dst->ch[i], (const float *)src->ch[i], consumed, src_size, dst_size, i+1==dst->ch_count);
  402. else if(c->format == AV_SAMPLE_FMT_DBLP) ret= swri_resample_double(c,(double *)dst->ch[i], (const double *)src->ch[i], consumed, src_size, dst_size, i+1==dst->ch_count);
  403. }
  404. if(need_emms)
  405. emms_c();
  406. return ret;
  407. }
  408. int64_t swr_get_delay(struct SwrContext *s, int64_t base){
  409. ResampleContext *c = s->resample;
  410. if(c){
  411. int64_t num = s->in_buffer_count - (c->filter_length-1)/2;
  412. num <<= c->phase_shift;
  413. num -= c->index;
  414. num *= c->src_incr;
  415. num -= c->frac;
  416. return av_rescale(num, base, s->in_sample_rate*(int64_t)c->src_incr << c->phase_shift);
  417. }else{
  418. return (s->in_buffer_count*base + (s->in_sample_rate>>1))/ s->in_sample_rate;
  419. }
  420. }