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.

324 lines
10KB

  1. /*
  2. * audio resampling
  3. * Copyright (c) 2004 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/avassert.h"
  27. #include "avcodec.h"
  28. #include "dsputil.h"
  29. #include "libavutil/common.h"
  30. #ifndef CONFIG_RESAMPLE_HP
  31. #define FILTER_SHIFT 15
  32. #define FELEM int16_t
  33. #define FELEM2 int32_t
  34. #define FELEML int64_t
  35. #define FELEM_MAX INT16_MAX
  36. #define FELEM_MIN INT16_MIN
  37. #define WINDOW_TYPE 9
  38. #elif !defined(CONFIG_RESAMPLE_AUDIOPHILE_KIDDY_MODE)
  39. #define FILTER_SHIFT 30
  40. #define FELEM int32_t
  41. #define FELEM2 int64_t
  42. #define FELEML int64_t
  43. #define FELEM_MAX INT32_MAX
  44. #define FELEM_MIN INT32_MIN
  45. #define WINDOW_TYPE 12
  46. #else
  47. #define FILTER_SHIFT 0
  48. #define FELEM double
  49. #define FELEM2 double
  50. #define FELEML double
  51. #define WINDOW_TYPE 24
  52. #endif
  53. typedef struct AVResampleContext{
  54. const AVClass *av_class;
  55. FELEM *filter_bank;
  56. int filter_length;
  57. int ideal_dst_incr;
  58. int dst_incr;
  59. int index;
  60. int frac;
  61. int src_incr;
  62. int compensation_distance;
  63. int phase_shift;
  64. int phase_mask;
  65. int linear;
  66. }AVResampleContext;
  67. /**
  68. * 0th order modified bessel function of the first kind.
  69. */
  70. static double bessel(double x){
  71. double v=1;
  72. double lastv=0;
  73. double t=1;
  74. int i;
  75. x= x*x/4;
  76. for(i=1; v != lastv; i++){
  77. lastv=v;
  78. t *= x/(i*i);
  79. v += t;
  80. }
  81. return v;
  82. }
  83. /**
  84. * Build a polyphase filterbank.
  85. * @param factor resampling factor
  86. * @param scale wanted sum of coefficients for each filter
  87. * @param type 0->cubic, 1->blackman nuttall windowed sinc, 2..16->kaiser windowed sinc beta=2..16
  88. * @return 0 on success, negative on error
  89. */
  90. static int build_filter(FELEM *filter, double factor, int tap_count, int phase_count, int scale, int type){
  91. int ph, i;
  92. double x, y, w;
  93. double *tab = av_malloc(tap_count * sizeof(*tab));
  94. const int center= (tap_count-1)/2;
  95. if (!tab)
  96. return AVERROR(ENOMEM);
  97. /* if upsampling, only need to interpolate, no filter */
  98. if (factor > 1.0)
  99. factor = 1.0;
  100. for(ph=0;ph<phase_count;ph++) {
  101. double norm = 0;
  102. for(i=0;i<tap_count;i++) {
  103. x = M_PI * ((double)(i - center) - (double)ph / phase_count) * factor;
  104. if (x == 0) y = 1.0;
  105. else y = sin(x) / x;
  106. switch(type){
  107. case 0:{
  108. const float d= -0.5; //first order derivative = -0.5
  109. x = fabs(((double)(i - center) - (double)ph / phase_count) * factor);
  110. if(x<1.0) y= 1 - 3*x*x + 2*x*x*x + d*( -x*x + x*x*x);
  111. else y= d*(-4 + 8*x - 5*x*x + x*x*x);
  112. break;}
  113. case 1:
  114. w = 2.0*x / (factor*tap_count) + M_PI;
  115. y *= 0.3635819 - 0.4891775 * cos(w) + 0.1365995 * cos(2*w) - 0.0106411 * cos(3*w);
  116. break;
  117. default:
  118. w = 2.0*x / (factor*tap_count*M_PI);
  119. y *= bessel(type*sqrt(FFMAX(1-w*w, 0)));
  120. break;
  121. }
  122. tab[i] = y;
  123. norm += y;
  124. }
  125. /* normalize so that an uniform color remains the same */
  126. for(i=0;i<tap_count;i++) {
  127. #ifdef CONFIG_RESAMPLE_AUDIOPHILE_KIDDY_MODE
  128. filter[ph * tap_count + i] = tab[i] / norm;
  129. #else
  130. filter[ph * tap_count + i] = av_clip(lrintf(tab[i] * scale / norm), FELEM_MIN, FELEM_MAX);
  131. #endif
  132. }
  133. }
  134. #if 0
  135. {
  136. #define LEN 1024
  137. int j,k;
  138. double sine[LEN + tap_count];
  139. double filtered[LEN];
  140. double maxff=-2, minff=2, maxsf=-2, minsf=2;
  141. for(i=0; i<LEN; i++){
  142. double ss=0, sf=0, ff=0;
  143. for(j=0; j<LEN+tap_count; j++)
  144. sine[j]= cos(i*j*M_PI/LEN);
  145. for(j=0; j<LEN; j++){
  146. double sum=0;
  147. ph=0;
  148. for(k=0; k<tap_count; k++)
  149. sum += filter[ph * tap_count + k] * sine[k+j];
  150. filtered[j]= sum / (1<<FILTER_SHIFT);
  151. ss+= sine[j + center] * sine[j + center];
  152. ff+= filtered[j] * filtered[j];
  153. sf+= sine[j + center] * filtered[j];
  154. }
  155. ss= sqrt(2*ss/LEN);
  156. ff= sqrt(2*ff/LEN);
  157. sf= 2*sf/LEN;
  158. maxff= FFMAX(maxff, ff);
  159. minff= FFMIN(minff, ff);
  160. maxsf= FFMAX(maxsf, sf);
  161. minsf= FFMIN(minsf, sf);
  162. if(i%11==0){
  163. 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);
  164. minff=minsf= 2;
  165. maxff=maxsf= -2;
  166. }
  167. }
  168. }
  169. #endif
  170. av_free(tab);
  171. return 0;
  172. }
  173. AVResampleContext *av_resample_init(int out_rate, int in_rate, int filter_size, int phase_shift, int linear, double cutoff){
  174. AVResampleContext *c= av_mallocz(sizeof(AVResampleContext));
  175. double factor= FFMIN(out_rate * cutoff / in_rate, 1.0);
  176. int phase_count= 1<<phase_shift;
  177. if (!c)
  178. return NULL;
  179. c->phase_shift= phase_shift;
  180. c->phase_mask= phase_count-1;
  181. c->linear= linear;
  182. c->filter_length= FFMAX((int)ceil(filter_size/factor), 1);
  183. c->filter_bank= av_mallocz(c->filter_length*(phase_count+1)*sizeof(FELEM));
  184. if (!c->filter_bank)
  185. goto error;
  186. if (build_filter(c->filter_bank, factor, c->filter_length, phase_count, 1<<FILTER_SHIFT, WINDOW_TYPE))
  187. goto error;
  188. memcpy(&c->filter_bank[c->filter_length*phase_count+1], c->filter_bank, (c->filter_length-1)*sizeof(FELEM));
  189. c->filter_bank[c->filter_length*phase_count]= c->filter_bank[c->filter_length - 1];
  190. if(!av_reduce(&c->src_incr, &c->dst_incr, out_rate, in_rate * (int64_t)phase_count, INT32_MAX/2))
  191. goto error;
  192. c->ideal_dst_incr= c->dst_incr;
  193. c->index= -phase_count*((c->filter_length-1)/2);
  194. return c;
  195. error:
  196. av_free(c->filter_bank);
  197. av_free(c);
  198. return NULL;
  199. }
  200. void av_resample_close(AVResampleContext *c){
  201. av_freep(&c->filter_bank);
  202. av_freep(&c);
  203. }
  204. void av_resample_compensate(AVResampleContext *c, int sample_delta, int compensation_distance){
  205. // sample_delta += (c->ideal_dst_incr - c->dst_incr)*(int64_t)c->compensation_distance / c->ideal_dst_incr;
  206. c->compensation_distance= compensation_distance;
  207. c->dst_incr = c->ideal_dst_incr - c->ideal_dst_incr * (int64_t)sample_delta / compensation_distance;
  208. }
  209. int av_resample(AVResampleContext *c, short *dst, short *src, int *consumed, int src_size, int dst_size, int update_ctx){
  210. int dst_index, i;
  211. int index= c->index;
  212. int frac= c->frac;
  213. int dst_incr_frac= c->dst_incr % c->src_incr;
  214. int dst_incr= c->dst_incr / c->src_incr;
  215. int compensation_distance= c->compensation_distance;
  216. if(compensation_distance == 0 && c->filter_length == 1 && c->phase_shift==0){
  217. int64_t index2= ((int64_t)index)<<32;
  218. int64_t incr= (1LL<<32) * c->dst_incr / c->src_incr;
  219. dst_size= FFMIN(dst_size, (src_size-1-index) * (int64_t)c->src_incr / c->dst_incr);
  220. for(dst_index=0; dst_index < dst_size; dst_index++){
  221. dst[dst_index] = src[index2>>32];
  222. index2 += incr;
  223. }
  224. index += dst_index * dst_incr;
  225. index += (frac + dst_index * (int64_t)dst_incr_frac) / c->src_incr;
  226. frac = (frac + dst_index * (int64_t)dst_incr_frac) % c->src_incr;
  227. }else{
  228. for(dst_index=0; dst_index < dst_size; dst_index++){
  229. FELEM *filter= c->filter_bank + c->filter_length*(index & c->phase_mask);
  230. int sample_index= index >> c->phase_shift;
  231. FELEM2 val=0;
  232. if(sample_index < 0){
  233. for(i=0; i<c->filter_length; i++)
  234. val += src[FFABS(sample_index + i) % src_size] * filter[i];
  235. }else if(sample_index + c->filter_length > src_size){
  236. break;
  237. }else if(c->linear){
  238. FELEM2 v2=0;
  239. for(i=0; i<c->filter_length; i++){
  240. val += src[sample_index + i] * (FELEM2)filter[i];
  241. v2 += src[sample_index + i] * (FELEM2)filter[i + c->filter_length];
  242. }
  243. val+=(v2-val)*(FELEML)frac / c->src_incr;
  244. }else{
  245. for(i=0; i<c->filter_length; i++){
  246. val += src[sample_index + i] * (FELEM2)filter[i];
  247. }
  248. }
  249. #ifdef CONFIG_RESAMPLE_AUDIOPHILE_KIDDY_MODE
  250. dst[dst_index] = av_clip_int16(lrintf(val));
  251. #else
  252. val = (val + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;
  253. dst[dst_index] = (unsigned)(val + 32768) > 65535 ? (val>>31) ^ 32767 : val;
  254. #endif
  255. frac += dst_incr_frac;
  256. index += dst_incr;
  257. if(frac >= c->src_incr){
  258. frac -= c->src_incr;
  259. index++;
  260. }
  261. if(dst_index + 1 == compensation_distance){
  262. compensation_distance= 0;
  263. dst_incr_frac= c->ideal_dst_incr % c->src_incr;
  264. dst_incr= c->ideal_dst_incr / c->src_incr;
  265. }
  266. }
  267. }
  268. *consumed= FFMAX(index, 0) >> c->phase_shift;
  269. if(index>=0) index &= c->phase_mask;
  270. if(compensation_distance){
  271. compensation_distance -= dst_index;
  272. av_assert2(compensation_distance > 0);
  273. }
  274. if(update_ctx){
  275. c->frac= frac;
  276. c->index= index;
  277. c->dst_incr= dst_incr_frac + c->src_incr*dst_incr;
  278. c->compensation_distance= compensation_distance;
  279. }
  280. #if 0
  281. if(update_ctx && !c->compensation_distance){
  282. #undef rand
  283. av_resample_compensate(c, rand() % (8000*2) - 8000, 8000*2);
  284. av_log(NULL, AV_LOG_DEBUG, "%d %d %d\n", c->dst_incr, c->ideal_dst_incr, c->compensation_distance);
  285. }
  286. #endif
  287. return dst_index;
  288. }