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.

452 lines
15KB

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