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.

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