[541] | 1 | /*
|
---|
| 2 | * audio resampling
|
---|
| 3 | *
|
---|
| 4 | * This file is part of uniaud.dll.
|
---|
| 5 | *
|
---|
| 6 | * Copyright (c) 2010 Mensys BV
|
---|
| 7 | * Copyright (c) 2007 Vlad Stelmahovsky aka Vladest
|
---|
| 8 | * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
|
---|
| 9 | *
|
---|
| 10 | * This library is free software: you can redistribute it and/or modify
|
---|
| 11 | * it under the terms of the GNU Lesser General Public License as
|
---|
| 12 | * published by the Free Software Foundation, either version 3 of
|
---|
| 13 | * the License, or (at your option) any later version.
|
---|
| 14 | *
|
---|
| 15 | * This library is distributed in the hope that it will be useful,
|
---|
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 18 | * GNU Lesser General Public License for more details.
|
---|
| 19 | *
|
---|
| 20 | * You should have received a copy of the GNU Lesser General Public
|
---|
| 21 | * License and the GNU General Public License along with this library.
|
---|
| 22 | * If not, see <http://www.gnu.org/licenses/>.
|
---|
| 23 | */
|
---|
| 24 | #include <math.h>
|
---|
| 25 | #include <stdint.h>
|
---|
| 26 | #include <string.h>
|
---|
| 27 | #include <malloc.h>
|
---|
| 28 | #include "resample.h"
|
---|
| 29 |
|
---|
| 30 | #ifndef M_PI
|
---|
| 31 | #define M_PI 3.14159265358979323846
|
---|
| 32 | #endif
|
---|
| 33 |
|
---|
| 34 | #define PHASE_SHIFT 10
|
---|
| 35 | #define PHASE_COUNT (1<<PHASE_SHIFT)
|
---|
| 36 | #define PHASE_MASK (PHASE_COUNT-1)
|
---|
| 37 | #define FILTER_SHIFT 15
|
---|
| 38 |
|
---|
| 39 | #if 0
|
---|
| 40 | static inline long int lrintf(float x)
|
---|
| 41 | {
|
---|
| 42 | int32_t i;
|
---|
| 43 | asm volatile(
|
---|
| 44 | "fistpl %0\n\t"
|
---|
| 45 | : "=m" (i) : "t" (x) : "st"
|
---|
| 46 | );
|
---|
| 47 | return i;
|
---|
| 48 | }
|
---|
| 49 | #endif
|
---|
| 50 | static inline long int lrintf(float x) {
|
---|
| 51 | return x;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | static inline int clip(int a, int amin, int amax)
|
---|
| 55 | {
|
---|
| 56 | if (a < amin)
|
---|
| 57 | return amin;
|
---|
| 58 | else if (a > amax)
|
---|
| 59 | return amax;
|
---|
| 60 | else
|
---|
| 61 | return a;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
|
---|
| 65 |
|
---|
| 66 | #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
|
---|
| 67 | #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
|
---|
| 68 |
|
---|
| 69 | /**
|
---|
| 70 | * 0th order modified bessel function of the first kind.
|
---|
| 71 | */
|
---|
| 72 | double bessel(double x){
|
---|
| 73 | double v=1;
|
---|
| 74 | double t=1;
|
---|
| 75 | int i;
|
---|
| 76 |
|
---|
| 77 | for(i=1; i<50; i++){
|
---|
| 78 | t *= i;
|
---|
| 79 | v += pow(x*x/4, i)/(t*t);
|
---|
| 80 | }
|
---|
| 81 | return v;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | /**
|
---|
| 85 | * builds a polyphase filterbank.
|
---|
| 86 | * @param factor resampling factor
|
---|
| 87 | * @param scale wanted sum of coefficients for each filter
|
---|
| 88 | * @param type 0->cubic, 1->blackman nuttall windowed sinc, 2->kaiser windowed sinc beta=16
|
---|
| 89 | */
|
---|
| 90 | void av_build_filter(int16_t *filter, double factor, int tap_count, int phase_count, int scale, int type){
|
---|
| 91 | int ph, i, v;
|
---|
| 92 | double x, y, w, tab[16 /*tap_count*/];
|
---|
| 93 | const int center= (tap_count-1)/2;
|
---|
| 94 |
|
---|
| 95 | /* if upsampling, only need to interpolate, no filter */
|
---|
| 96 | if (factor > 1.0)
|
---|
| 97 | factor = 1.0;
|
---|
| 98 |
|
---|
| 99 | for(ph=0;ph<phase_count;ph++) {
|
---|
| 100 | double norm = 0;
|
---|
| 101 | double e= 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 | case 2:
|
---|
| 118 | w = 2.0*x / (factor*tap_count*M_PI);
|
---|
| 119 | y *= bessel(16*sqrt(FFMAX(1-w*w, 0)));
|
---|
| 120 | break;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | tab[i] = y;
|
---|
| 124 | norm += y;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | /* normalize so that an uniform color remains the same */
|
---|
| 128 | for(i=0;i<tap_count;i++) {
|
---|
| 129 | v = clip(lrintf(tab[i] * scale / norm + e), -32768, 32767);
|
---|
| 130 | filter[ph * tap_count + i] = v;
|
---|
| 131 | e += tab[i] * scale / norm - v;
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | /**
|
---|
| 137 | * initalizes a audio resampler.
|
---|
| 138 | * note, if either rate is not a integer then simply scale both rates up so they are
|
---|
| 139 | */
|
---|
| 140 | AVResampleContext *av_resample_init(int out_rate, int in_rate, int filter_size, int phase_shift, int linear, double cutoff){
|
---|
| 141 | AVResampleContext *c= (AVResampleContext *) malloc(sizeof(AVResampleContext));
|
---|
| 142 | double factor= FFMIN(out_rate * cutoff / in_rate, 1.0);
|
---|
| 143 | int phase_count= 1<<phase_shift;
|
---|
| 144 |
|
---|
| 145 | memset(c, 0, sizeof(AVResampleContext));
|
---|
| 146 |
|
---|
| 147 | c->phase_shift= phase_shift;
|
---|
| 148 | c->phase_mask= phase_count-1;
|
---|
| 149 | c->linear= linear;
|
---|
| 150 |
|
---|
| 151 | c->filter_length= ceil(filter_size/factor);
|
---|
| 152 | c->filter_bank= malloc(c->filter_length*(phase_count+1)*sizeof(FELEM));
|
---|
| 153 | if (c->filter_bank)
|
---|
| 154 | memset(c->filter_bank,0,c->filter_length*(phase_count+1)*sizeof(FELEM));
|
---|
| 155 | av_build_filter(c->filter_bank, factor, c->filter_length, phase_count, 1<<FILTER_SHIFT, 1);
|
---|
| 156 | memcpy(&c->filter_bank[c->filter_length*phase_count+1], c->filter_bank, (c->filter_length-1)*sizeof(FELEM));
|
---|
| 157 | c->filter_bank[c->filter_length*phase_count]= c->filter_bank[c->filter_length - 1];
|
---|
| 158 |
|
---|
| 159 | c->src_incr= out_rate;
|
---|
| 160 | c->ideal_dst_incr= c->dst_incr= in_rate * phase_count;
|
---|
| 161 | c->index= -phase_count*((c->filter_length-1)/2);
|
---|
| 162 |
|
---|
| 163 | return c;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | void av_resample_close(AVResampleContext *c){
|
---|
| 167 | free(c->filter_bank);
|
---|
| 168 | free(c);
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | /**
|
---|
| 172 | * Compensates samplerate/timestamp drift. The compensation is done by changing
|
---|
| 173 | * the resampler parameters, so no audible clicks or similar distortions ocur
|
---|
| 174 | * @param compensation_distance distance in output samples over which the compensation should be performed
|
---|
| 175 | * @param sample_delta number of output samples which should be output less
|
---|
| 176 | *
|
---|
| 177 | * example: av_resample_compensate(c, 10, 500)
|
---|
| 178 | * here instead of 510 samples only 500 samples would be output
|
---|
| 179 | *
|
---|
| 180 | * note, due to rounding the actual compensation might be slightly different,
|
---|
| 181 | * especially if the compensation_distance is large and the in_rate used during init is small
|
---|
| 182 | */
|
---|
| 183 |
|
---|
| 184 | void av_resample_compensate(AVResampleContext *c, int sample_delta, int compensation_distance){
|
---|
| 185 | // sample_delta += (c->ideal_dst_incr - c->dst_incr)*(int64_t)c->compensation_distance / c->ideal_dst_incr;
|
---|
| 186 | c->compensation_distance= compensation_distance;
|
---|
| 187 | c->dst_incr = c->ideal_dst_incr - c->ideal_dst_incr * (int64_t)sample_delta / compensation_distance;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | /**
|
---|
| 191 | * resamples.
|
---|
| 192 | * @param src an array of unconsumed samples
|
---|
| 193 | * @param consumed the number of samples of src which have been consumed are returned here
|
---|
| 194 | * @param src_size the number of unconsumed samples available
|
---|
| 195 | * @param dst_size the amount of space in samples available in dst
|
---|
| 196 | * @param update_ctx if this is 0 then the context wont be modified, that way several channels can be resampled with the same context
|
---|
| 197 | * @return the number of samples written in dst or -1 if an error occured
|
---|
| 198 | */
|
---|
| 199 | int av_resample(AVResampleContext *c, short *dst, short *src, int *consumed, int src_size, int dst_size, int update_ctx){
|
---|
| 200 | int dst_index, i;
|
---|
| 201 | int index= c->index;
|
---|
| 202 | int frac= c->frac;
|
---|
| 203 | int dst_incr_frac= c->dst_incr % c->src_incr;
|
---|
| 204 | int dst_incr= c->dst_incr / c->src_incr;
|
---|
| 205 | int compensation_distance= c->compensation_distance;
|
---|
| 206 |
|
---|
| 207 | for(dst_index=0; dst_index < dst_size; dst_index++){
|
---|
| 208 | FELEM *filter= c->filter_bank + c->filter_length*(index & c->phase_mask);
|
---|
| 209 | int sample_index= index >> c->phase_shift;
|
---|
| 210 | FELEM2 val=0;
|
---|
| 211 |
|
---|
| 212 | if(sample_index < 0){
|
---|
| 213 | for(i=0; i<c->filter_length; i++)
|
---|
| 214 | val += src[ABS(sample_index + i) % src_size] * filter[i];
|
---|
| 215 | }else if(sample_index + c->filter_length > src_size){
|
---|
| 216 | break;
|
---|
| 217 | }else if(c->linear){
|
---|
| 218 | int64_t v=0;
|
---|
| 219 | int sub_phase= (frac<<8) / c->src_incr;
|
---|
| 220 | for(i=0; i<c->filter_length; i++){
|
---|
| 221 | int64_t coeff= filter[i]*(256 - sub_phase) + filter[i + c->filter_length]*sub_phase;
|
---|
| 222 | v += src[sample_index + i] * coeff;
|
---|
| 223 | }
|
---|
| 224 | val= v>>8;
|
---|
| 225 | }else{
|
---|
| 226 | for(i=0; i<c->filter_length; i++){
|
---|
| 227 | val += src[sample_index + i] * (FELEM2)filter[i];
|
---|
| 228 | }
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | val = (val + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;
|
---|
| 232 | dst[dst_index] = (unsigned)(val + 32768) > 65535 ? (val>>31) ^ 32767 : val;
|
---|
| 233 |
|
---|
| 234 | frac += dst_incr_frac;
|
---|
| 235 | index += dst_incr;
|
---|
| 236 | if(frac >= c->src_incr){
|
---|
| 237 | frac -= c->src_incr;
|
---|
| 238 | index++;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | if(dst_index + 1 == compensation_distance){
|
---|
| 242 | compensation_distance= 0;
|
---|
| 243 | dst_incr_frac= c->ideal_dst_incr % c->src_incr;
|
---|
| 244 | dst_incr= c->ideal_dst_incr / c->src_incr;
|
---|
| 245 | }
|
---|
| 246 | }
|
---|
| 247 | *consumed= FFMAX(index, 0) >> c->phase_shift;
|
---|
| 248 | if(index>=0) index &= c->phase_mask;
|
---|
| 249 |
|
---|
| 250 | if(compensation_distance){
|
---|
| 251 | compensation_distance -= dst_index;
|
---|
| 252 | //assert(compensation_distance > 0);
|
---|
| 253 | }
|
---|
| 254 | if(update_ctx){
|
---|
| 255 | c->frac= frac;
|
---|
| 256 | c->index= index;
|
---|
| 257 | c->dst_incr= dst_incr_frac + c->src_incr*dst_incr;
|
---|
| 258 | c->compensation_distance= compensation_distance;
|
---|
| 259 | }
|
---|
| 260 | #if 0
|
---|
| 261 | if(update_ctx && !c->compensation_distance){
|
---|
| 262 | #undef rand
|
---|
| 263 | av_resample_compensate(c, rand() % (8000*2) - 8000, 8000*2);
|
---|
| 264 | av_log(NULL, AV_LOG_DEBUG, "%d %d %d\n", c->dst_incr, c->ideal_dst_incr, c->compensation_distance);
|
---|
| 265 | }
|
---|
| 266 | #endif
|
---|
| 267 |
|
---|
| 268 | return dst_index;
|
---|
| 269 | }
|
---|