resample.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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(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. av_assert0(0);
  210. }
  211. c->phase_shift = phase_shift;
  212. c->phase_mask = phase_count - 1;
  213. c->linear = linear;
  214. c->factor = factor;
  215. c->filter_length = FFMAX((int)ceil(filter_size/factor), 1);
  216. c->filter_alloc = FFALIGN(c->filter_length, 8);
  217. c->filter_bank = av_mallocz(c->filter_alloc*(phase_count+1)*c->felem_size);
  218. c->filter_type = filter_type;
  219. c->kaiser_beta = kaiser_beta;
  220. if (!c->filter_bank)
  221. goto error;
  222. 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))
  223. goto error;
  224. memcpy(c->filter_bank + (c->filter_alloc*phase_count+1)*c->felem_size, c->filter_bank, (c->filter_alloc-1)*c->felem_size);
  225. 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);
  226. }
  227. c->compensation_distance= 0;
  228. if(!av_reduce(&c->src_incr, &c->dst_incr, out_rate, in_rate * (int64_t)phase_count, INT32_MAX/2))
  229. goto error;
  230. c->ideal_dst_incr= c->dst_incr;
  231. c->index= -phase_count*((c->filter_length-1)/2);
  232. c->frac= 0;
  233. return c;
  234. error:
  235. av_free(c->filter_bank);
  236. av_free(c);
  237. return NULL;
  238. }
  239. void swri_resample_free(ResampleContext **c){
  240. if(!*c)
  241. return;
  242. av_freep(&(*c)->filter_bank);
  243. av_freep(c);
  244. }
  245. int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance){
  246. ResampleContext *c;
  247. int ret;
  248. if (!s || compensation_distance < 0)
  249. return AVERROR(EINVAL);
  250. if (!compensation_distance && sample_delta)
  251. return AVERROR(EINVAL);
  252. if (!s->resample) {
  253. s->flags |= SWR_FLAG_RESAMPLE;
  254. ret = swr_init(s);
  255. if (ret < 0)
  256. return ret;
  257. }
  258. c= s->resample;
  259. c->compensation_distance= compensation_distance;
  260. if (compensation_distance)
  261. c->dst_incr = c->ideal_dst_incr - c->ideal_dst_incr * (int64_t)sample_delta / compensation_distance;
  262. else
  263. c->dst_incr = c->ideal_dst_incr;
  264. return 0;
  265. }
  266. #define TEMPLATE_RESAMPLE_S16
  267. #include "resample_template.c"
  268. #undef TEMPLATE_RESAMPLE_S16
  269. #define TEMPLATE_RESAMPLE_S32
  270. #include "resample_template.c"
  271. #undef TEMPLATE_RESAMPLE_S32
  272. #define TEMPLATE_RESAMPLE_FLT
  273. #include "resample_template.c"
  274. #undef TEMPLATE_RESAMPLE_FLT
  275. #define TEMPLATE_RESAMPLE_DBL
  276. #include "resample_template.c"
  277. #undef TEMPLATE_RESAMPLE_DBL
  278. // XXX FIXME the whole C loop should be written in asm so this x86 specific code here isnt needed
  279. #if HAVE_MMXEXT_INLINE
  280. #include "x86/resample_mmx.h"
  281. #define TEMPLATE_RESAMPLE_S16_MMX2
  282. #include "resample_template.c"
  283. #undef TEMPLATE_RESAMPLE_S16_MMX2
  284. #define TEMPLATE_RESAMPLE_S16_SSSE3
  285. #include "resample_template.c"
  286. #undef TEMPLATE_RESAMPLE_S16_SSSE3
  287. #endif // HAVE_MMXEXT_INLINE
  288. int swri_multiple_resample(ResampleContext *c, AudioData *dst, int dst_size, AudioData *src, int src_size, int *consumed){
  289. int i, ret= -1;
  290. int av_unused mm_flags = av_get_cpu_flags();
  291. int need_emms= 0;
  292. for(i=0; i<dst->ch_count; i++){
  293. #if HAVE_MMXEXT_INLINE
  294. #if HAVE_SSSE3_INLINE
  295. 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);
  296. else
  297. #endif
  298. if(c->format == AV_SAMPLE_FMT_S16P && (mm_flags&AV_CPU_FLAG_MMX2 )){
  299. 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);
  300. need_emms= 1;
  301. } else
  302. #endif
  303. 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);
  304. 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);
  305. 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);
  306. 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);
  307. }
  308. if(need_emms)
  309. emms_c();
  310. return ret;
  311. }
  312. int64_t swr_get_delay(struct SwrContext *s, int64_t base){
  313. ResampleContext *c = s->resample;
  314. if(c){
  315. int64_t num = s->in_buffer_count - (c->filter_length-1)/2;
  316. num <<= c->phase_shift;
  317. num -= c->index;
  318. num *= c->src_incr;
  319. num -= c->frac;
  320. return av_rescale(num, base, s->in_sample_rate*(int64_t)c->src_incr << c->phase_shift);
  321. }else{
  322. return (s->in_buffer_count*base + (s->in_sample_rate>>1))/ s->in_sample_rate;
  323. }
  324. }