resample.c 13 KB

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