resample.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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_clipl_int32(llrint(tab[i] * scale / norm));
  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. static ResampleContext *resample_init(ResampleContext *c, int out_rate, int in_rate, int filter_size, int phase_shift, int linear,
  185. double cutoff0, enum AVSampleFormat format, enum SwrFilterType filter_type, int kaiser_beta,
  186. double precision, int cheby){
  187. double cutoff = cutoff0? cutoff0 : 0.97;
  188. double factor= FFMIN(out_rate * cutoff / in_rate, 1.0);
  189. int phase_count= 1<<phase_shift;
  190. if (!c || c->phase_shift != phase_shift || c->linear!=linear || c->factor != factor
  191. || c->filter_length != FFMAX((int)ceil(filter_size/factor), 1) || c->format != format
  192. || c->filter_type != filter_type || c->kaiser_beta != kaiser_beta) {
  193. c = av_mallocz(sizeof(*c));
  194. if (!c)
  195. return NULL;
  196. c->format= format;
  197. c->felem_size= av_get_bytes_per_sample(c->format);
  198. switch(c->format){
  199. case AV_SAMPLE_FMT_S16P:
  200. c->filter_shift = 15;
  201. break;
  202. case AV_SAMPLE_FMT_S32P:
  203. c->filter_shift = 30;
  204. break;
  205. case AV_SAMPLE_FMT_FLTP:
  206. case AV_SAMPLE_FMT_DBLP:
  207. c->filter_shift = 0;
  208. break;
  209. default:
  210. av_log(NULL, AV_LOG_ERROR, "Unsupported sample format\n");
  211. av_assert0(0);
  212. }
  213. if (filter_size/factor > INT32_MAX/256) {
  214. av_log(NULL, AV_LOG_ERROR, "Filter length too large\n");
  215. goto error;
  216. }
  217. c->phase_shift = phase_shift;
  218. c->phase_mask = phase_count - 1;
  219. c->linear = linear;
  220. c->factor = factor;
  221. c->filter_length = FFMAX((int)ceil(filter_size/factor), 1);
  222. c->filter_alloc = FFALIGN(c->filter_length, 8);
  223. c->filter_bank = av_calloc(c->filter_alloc, (phase_count+1)*c->felem_size);
  224. c->filter_type = filter_type;
  225. c->kaiser_beta = kaiser_beta;
  226. if (!c->filter_bank)
  227. goto error;
  228. 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))
  229. goto error;
  230. memcpy(c->filter_bank + (c->filter_alloc*phase_count+1)*c->felem_size, c->filter_bank, (c->filter_alloc-1)*c->felem_size);
  231. 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);
  232. }
  233. c->compensation_distance= 0;
  234. if(!av_reduce(&c->src_incr, &c->dst_incr, out_rate, in_rate * (int64_t)phase_count, INT32_MAX/2))
  235. goto error;
  236. c->ideal_dst_incr= c->dst_incr;
  237. c->index= -phase_count*((c->filter_length-1)/2);
  238. c->frac= 0;
  239. return c;
  240. error:
  241. av_freep(&c->filter_bank);
  242. av_free(c);
  243. return NULL;
  244. }
  245. static void resample_free(ResampleContext **c){
  246. if(!*c)
  247. return;
  248. av_freep(&(*c)->filter_bank);
  249. av_freep(c);
  250. }
  251. static int set_compensation(ResampleContext *c, int sample_delta, int compensation_distance){
  252. c->compensation_distance= compensation_distance;
  253. if (compensation_distance)
  254. c->dst_incr = c->ideal_dst_incr - c->ideal_dst_incr * (int64_t)sample_delta / compensation_distance;
  255. else
  256. c->dst_incr = c->ideal_dst_incr;
  257. return 0;
  258. }
  259. #define TEMPLATE_RESAMPLE_S16
  260. #include "resample_template.c"
  261. #undef TEMPLATE_RESAMPLE_S16
  262. #define TEMPLATE_RESAMPLE_S32
  263. #include "resample_template.c"
  264. #undef TEMPLATE_RESAMPLE_S32
  265. #define TEMPLATE_RESAMPLE_FLT
  266. #include "resample_template.c"
  267. #undef TEMPLATE_RESAMPLE_FLT
  268. #define TEMPLATE_RESAMPLE_DBL
  269. #include "resample_template.c"
  270. #undef TEMPLATE_RESAMPLE_DBL
  271. // XXX FIXME the whole C loop should be written in asm so this x86 specific code here isnt needed
  272. #if HAVE_MMXEXT_INLINE
  273. #include "x86/resample_mmx.h"
  274. #define TEMPLATE_RESAMPLE_S16_MMX2
  275. #include "resample_template.c"
  276. #undef TEMPLATE_RESAMPLE_S16_MMX2
  277. #if HAVE_SSSE3_INLINE
  278. #define TEMPLATE_RESAMPLE_S16_SSSE3
  279. #include "resample_template.c"
  280. #undef TEMPLATE_RESAMPLE_S16_SSSE3
  281. #endif
  282. #endif // HAVE_MMXEXT_INLINE
  283. static int multiple_resample(ResampleContext *c, AudioData *dst, int dst_size, AudioData *src, int src_size, int *consumed){
  284. int i, ret= -1;
  285. int av_unused mm_flags = av_get_cpu_flags();
  286. int need_emms= 0;
  287. for(i=0; i<dst->ch_count; i++){
  288. #if HAVE_MMXEXT_INLINE
  289. #if HAVE_SSSE3_INLINE
  290. 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);
  291. else
  292. #endif
  293. if(c->format == AV_SAMPLE_FMT_S16P && (mm_flags&AV_CPU_FLAG_MMX2 )){
  294. 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);
  295. need_emms= 1;
  296. } else
  297. #endif
  298. 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);
  299. 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);
  300. 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);
  301. 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);
  302. }
  303. if(need_emms)
  304. emms_c();
  305. return ret;
  306. }
  307. static int64_t get_delay(struct SwrContext *s, int64_t base){
  308. ResampleContext *c = s->resample;
  309. int64_t num = s->in_buffer_count - (c->filter_length-1)/2;
  310. num <<= c->phase_shift;
  311. num -= c->index;
  312. num *= c->src_incr;
  313. num -= c->frac;
  314. return av_rescale(num, base, s->in_sample_rate*(int64_t)c->src_incr << c->phase_shift);
  315. }
  316. static int resample_flush(struct SwrContext *s) {
  317. AudioData *a= &s->in_buffer;
  318. int i, j, ret;
  319. if((ret = swri_realloc_audio(a, s->in_buffer_index + 2*s->in_buffer_count)) < 0)
  320. return ret;
  321. av_assert0(a->planar);
  322. for(i=0; i<a->ch_count; i++){
  323. for(j=0; j<s->in_buffer_count; j++){
  324. memcpy(a->ch[i] + (s->in_buffer_index+s->in_buffer_count+j )*a->bps,
  325. a->ch[i] + (s->in_buffer_index+s->in_buffer_count-j-1)*a->bps, a->bps);
  326. }
  327. }
  328. s->in_buffer_count += (s->in_buffer_count+1)/2;
  329. return 0;
  330. }
  331. struct Resampler const swri_resampler={
  332. resample_init,
  333. resample_free,
  334. multiple_resample,
  335. resample_flush,
  336. set_compensation,
  337. get_delay,
  338. };