diff options
author | Glenn Kasten <gkasten@google.com> | 2012-11-19 09:00:47 -0800 |
---|---|---|
committer | Glenn Kasten <gkasten@google.com> | 2012-11-27 13:33:45 -0800 |
commit | 9bcb476a95a26e62f5706d1f00f4873cf44f9e04 (patch) | |
tree | ef67a0dacbf947ed0dab816f744510bca93267f2 /tools/resampler_tools | |
parent | d983364b3655a547b55bb11dbe148103198c011d (diff) | |
download | frameworks_av-9bcb476a95a26e62f5706d1f00f4873cf44f9e04.zip frameworks_av-9bcb476a95a26e62f5706d1f00f4873cf44f9e04.tar.gz frameworks_av-9bcb476a95a26e62f5706d1f00f4873cf44f9e04.tar.bz2 |
New VHQ resampler
Squashed commit of the following:
commit 12b6952da9f25e94d06dd7185bce255924e7e791
Author: Mathias Agopian <mathias@google.com>
Date: Mon Nov 19 15:27:26 2012 -0800
fix a typo in SINC resampler that prevented tracks to be mixed
we were always erasing the current mix instead of mixing into it.
Change-Id: Ib229245f9e5a0d384f1727640a59e9f0469211a2
commit 0019ce082df430278f14ab922e900ce33b64897d
Author: Dave Bort <dbort@google.com>
Date: Tue Nov 13 01:30:32 2007 -0800
Rename "TARGET" to "MODULE" in the build system.
Part one of the grand renaming.
API_CHANGE: Third parties may need to update their makefiles.
Any variables with "LOCAL" and "TARGET" in their names
should now use "MODULE" instead of "TARGET"; e.g., LOCAL_MODULE,
LOCAL_MODULE_TAGS.
PRESUBMIT=passed
OCL=39840
Change-Id: Ica9a7937d3d9552ab84db46ac6eea8a290e404fe
Signed-off-by: Glenn Kasten <gkasten@google.com>
commit f01adc0cef0e39e75c76d9195ac26a94cac0a100
Author: Glenn Kasten <gkasten@google.com>
Date: Wed Nov 14 08:32:08 2012 -0800
Fix build warnings
Change-Id: Ic43bcca166a529a6431711b05a7fa21849b6a38b
commit 9bb031a565c753a03d9c9397edea318947d80528
Author: Mathias Agopian <mathias@google.com>
Date: Sat Nov 10 04:44:30 2012 -0800
more optimizations...
calculate the offsets from the phase differently, this happens
to reduce the register pressure in the main loop, which in turns
allows the compiler to generate much better code (doesn't need
to spill a lot of stuff on the stack).
this gives another 15% performance increase
Change-Id: I2ce3479dd48b9e6941adb80e6d443d6e14d64d96
commit 5a951598f31217b8cd2babd0720c9608ee17291a
Author: Mathias Agopian <mathias@google.com>
Date: Sat Nov 10 03:26:39 2012 -0800
refactor code to improve neon code
we want to make sure we don't transfer data from the
neon unit to the arm register file, as this can be quite
slow. instead we do all the calculation on the neon side
and write the result directly to main memory.
Change-Id: Ibb56664d3ab03098ae2798b75e2b6927ac900187
commit b381ee9e83bc9fd18986e79c7809841514ed590e
Author: Mathias Agopian <mathias@google.com>
Date: Sun Nov 4 15:16:13 2012 -0800
NEON optimized SINC resampler
this currently gives us a 60% to 80% boost depending
on the quality level selected.
Change-Id: I7db385007e811ed7bffe5fd3403b44e300894f5b
commit bea077354210242ea193a50b0dbab0fedab25df3
Author: Mathias Agopian <mathias@google.com>
Date: Mon Nov 5 01:51:37 2012 -0800
minor cleanups
Change-Id: Ia12ee4fb59e90221761bec85e6450db29197591f
commit 8f4ed7decbe161a5ff38200b218f5216d80aba46
Author: Mathias Agopian <mathias@google.com>
Date: Sun Nov 4 18:49:14 2012 -0800
improve resample test
- handle stereo input
- input file can now be ommited, in this case
a linear chirp will be used automatically
- better usage information
Change-Id: I5d62a6c26a9054a1c1a517a065b4df5a2cdcda22
commit 5fcd634ea6cb4df27c495abe20f5f9b8ff55d128
Author: Mathias Agopian <mathias@google.com>
Date: Sun Nov 4 02:03:49 2012 -0800
change how we store the FIR coefficients
The coefficient table is now transposed and shows
much better its polyphase nature: we now have a FIR
per line, each line corresponding to a phase.
This doesn't change at all the results produced by
the filter, but allows us to make slightly better
use of the data cache and improves performance a bit
(although not as much as I thought it would).
The main benefit is that it is the first step
before we can make much larger optimizations
(like using NEON).
Change-Id: Iebf7695825dcbd41f25861efcaefbaa3365ecb43
commit d652231abf4c7e2ea1fc89caae730cec1f7259a1
Author: Mathias Agopian <mathias@google.com>
Date: Sat Nov 3 23:37:53 2012 -0700
improve SINC resampler performance
The improvement is about 60% by just tweaking a few
things to help the compiler generate better code.
It turns out that inlining too much stuff manually was hurting us.
Change-Id: I8068f0f75051f95ac600e50ce552572dd1e8c304
commit 9dc68ef5b94c700c4ee68790e8cbb334c90a538d
Author: Mathias Agopian <mathias@google.com>
Date: Thu Nov 1 21:03:46 2012 -0700
new coefficients for the vhq resampler
previous coefficients were provided by a 3rd party and didn't have a
way to re-generate them. we're now using the 'fir' utility.
the performance of the filter is virtually identical, except for
the down-sampling case which seems slightly better now:
It looks like both the previous and new coefficients are generating
some sort of clipping for full-scale signals in the down-sampling case
(although the new ones seem better), the reason for that is
unknown (see bug: 7453062)
Also updated the HQ coefficients for the down-samplers, previous ones
were a little bit too conservative -- the new ones push the cut-off
frequency up by about 1 KHz.
Change-Id: I54a827b5c707c7cc41268ed01283758dce1d7647
commit 38e0b8560a6fc1b7124e22e0e09a84a285182f8e
Author: Mathias Agopian <mathias@google.com>
Date: Tue Oct 30 13:51:44 2012 -0700
fix SINC resampler on non ARM architectures
make sure the C version of the code generates the same
output than the ARM assemply version.
Change-Id: Ide218785c35d02598b2d7278e646b1b178148698
commit a1878128b182696ba508569b4d211d0dfae92463
Author: Mathias Agopian <mathias@google.com>
Date: Tue Oct 30 12:49:07 2012 -0700
fix another issue with generating FIR coefficients
the impulse response of a low-pass is 2*f*sinc(2*pi*f*k), we were
missing the 2*f scale factor. This explains why we were seeing
clipping and had to manually scale the filter down.
Change-Id: I86d0bb82ecdd99681c8ba5a8112a8257bf6f0186
commit 1a0fb993430acc9f601e6c538305bc407c20ac5d
Author: Mathias Agopian <mathias@google.com>
Date: Mon Oct 29 17:13:20 2012 -0700
fir a typo that caused up-sampling coefficiens to be wrong
up-sample coefficient were generated with a cut-off frequency of 24KHz
intead of ~20KHz, which caused more aliasing in the audible band.
also increased the attenuation to 1.3 dB on both up and down
sampling coefficient to avoid clipping.
Change-Id: Ie8aeecf1429190541b656810c6716b6aae5ece2e
commit 9520ad6862bd682ad075a9d9e3e94ada9f6e58b6
Author: Mathias Agopian <mathias@google.com>
Date: Mon Oct 29 17:13:16 2012 -0700
test-resample: clip instead of overflowing
Change-Id: I550e5a59e51c11e1095ca338222b094f92b96878
commit ba36656300f250f7f1fdeb75149749344260e6cb
Author: Mathias Agopian <mathias@google.com>
Date: Sun Oct 21 01:01:38 2012 -0700
a test app for the resamplers
Change-Id: I66852d90d384f1d9e77b51ad1a1ebdbaf61d0607
commit 056a08b9bfd33cf27228c992adc8293a56b01be8
Author: Mathias Agopian <mathias@google.com>
Date: Fri Oct 26 14:11:01 2012 -0700
reenable the cubic resampler
cubic resampler was disabled because it hadn't been qualified,
however after I did some tests, it does improve significantly
the sound quality over the order-1 resampler, even if it is
still quite bad.
also HIGH_QUALITY resampler was partially disabled, it's now
fully enabled. It's a big improvement over the cubic resampler
in terms of aliasing noise (it's not as good in the pass-band).
Change-Id: I70e3658c255896588642697be9eb594ff4ec0f8b
commit 8c0241d3ff50ae85167f69b3bd369244894cfa44
Author: Mathias Agopian <mathias@google.com>
Date: Fri Oct 26 13:48:42 2012 -0700
improve SINC resampler coefficients
- we increase the interpolation precision from 4 to 7 bits
this doesn't increase CPU power required, it only increases the
size of the filter table but significantly reduces the noise
introduced by the quantization of the impulse response.
- the parameters of the filter are set such that aliasing is
rejected at 80 dB below 20 KHz. Because we don't use a lot of
coefficient (to save compute power), there are quite a bit of
attenuation in the pass-band: starting at 9KHz for the
down-sampler (48 to 44.1), and starting at 13 KHz for the
up-sampler (44.1 to 48) -- the transition band is about 15 KHz.
Change-Id: I855548d2aab8a0fb0d2a2da3a364b6842d7d3838
commit 69e7dab2192adc1f780464146810629ebd01b145
Author: Pixelflinger <mathias.agopian@gmail.com>
Date: Thu Oct 25 19:43:49 2012 -0700
improve fir tool: cleanup, better default, bug fixes
- all parameters can be changed on the command-line
- added float output
- added debug option
- added an option to generate a polyphase filter coefficients
- added an attenuation option in dBFS
- added a lot of comments and references
- fixed kaiser window parameter
also the default should generate a filter with 80 dB rejection
(of the 24 KHz aliasing) above 20 KHz and a 15 KHz transition
band around ~20 KHz (for 48 KHz sampling rate).
It's not very good but corresponds to the current code.
commit 8347499d105a50257c18e9dac652e750b06428b1
Author: Glenn Kasten <gkasten@google.com>
Date: Mon Oct 22 17:09:27 2012 -0700
Increase allowed number of VHQ resamplers to 3
Bug: 7378660
Change-Id: I69e33ca2eb4bb9bd38e2c63df62cd1130d68baf6
commit f91cf3cad7f5c4d52614271c89ab468741c5d24c
Author: Mathias Agopian <mathias@google.com>
Date: Sun Oct 21 03:04:05 2012 -0700
Fix a typo that caused the high quality resampler to produce garbage
the problem is that if libaudio_resampler is present, it is those
coefficients that will always be selected, but the correct
meta-data.
Bug: 7385994
Change-Id: Ieebeb37b4dfb62a1a051bc29fae2ce056dbc6621
commit e158a9e4262a174c59469a205658bc3ca4078234
Author: Dan Bornstein <danfuzz@google.com>
Date: Fri Oct 3 10:34:57 2008 -0700
Manually merge change #111620 from tc3 to mainline, to keep the
automerger from choking on it.
p4 sync
p4 integrate -r -b android_to_tc3 //...@111620,111620
p4 resolve -a
p4 resolve # resolve a couple merge travesties
PRESUBMIT=passed
BUG=1399648
TBR=edheyl
OCL=111902
Change-Id: I854b01553dd92bbf9c864f5a9bd51a3d665f0ac2
Signed-off-by: Glenn Kasten <gkasten@google.com>
commit b9f3c26032be7a6ea01a10d93d94826f449e68ab
Author: Dave Bort <dbort@google.com>
Date: Fri Jan 18 14:51:05 2008 -0800
Rename "Makefile" to "Android.mk" throughout the tree.
For <http://b/issue?id=960416>.
I've tested this as much as I can, but 1500 open files =
easy to mess things up. Please let me know if there's
a problem rather than rolling back this change.
PRESUBMIT=passed
BUG=960416
TBR=joeo
OCL=46537
Change-Id: I5a404caf0f398a7afa7ae7abaf2f2a1c6ab490eb
Signed-off-by: Glenn Kasten <gkasten@google.com>
commit 0c22a9a44c4103483fba1d944acf1354c5eb1617
Author: Mathias Agopian <mathias@google.com>
Date: Mon Oct 29 23:44:25 2007 -0700
Tweak the SINC resampler parameters and double the performance. It's using about 10% CPU in the worse case now.
Change-Id: I50ac7e6c6702a427fa36ab6d976c507155057507
Signed-off-by: Glenn Kasten <gkasten@google.com>
commit b85e41487983ad085b859acf8251e7e54480308a
Author: Mathias Agopian <mathias@google.com>
Date: Mon Oct 29 04:34:36 2007 -0700
A sinc resampler for Audioflinger. It's not enabled yet, but fully functional and apparently working. It need more "quality" tests. In the 48->44 KHz, it takes about 25% of the CPU time.
Change-Id: I80eb5185e13ebdb907e0b85c49ba1272c23d60ec
Signed-off-by: Glenn Kasten <gkasten@google.com>
commit ba3949ef17cac2ba71cc3096c413782a49c922e5
Author: Mathias Agopian <mathias@google.com>
Date: Thu Aug 23 21:01:28 2007 -0700
fix a few small typos in the FIR computation
Change-Id: I6e56b514fe520f30f7487f85c64ea5d2a7c19b40
Signed-off-by: Glenn Kasten <gkasten@google.com>
commit 7474bfa7de2604021963794dddfe44985648db6a
Author: Mathias Agopian <mathias@google.com>
Date: Thu Aug 23 03:16:02 2007 -0700
This is a tool to compute the the reconstruction filter coefficients for a sinc audio resampler.
Change-Id: I99be2505139b8e0e7647200e1647509d4f7e6067
Signed-off-by: Glenn Kasten <gkasten@google.com>
Bug: 7577965
Change-Id: I2c84a9283a1668723bad83e1a119c849c88c3e6b
Diffstat (limited to 'tools/resampler_tools')
-rw-r--r-- | tools/resampler_tools/Android.mk | 17 | ||||
-rw-r--r-- | tools/resampler_tools/fir.cpp | 281 |
2 files changed, 298 insertions, 0 deletions
diff --git a/tools/resampler_tools/Android.mk b/tools/resampler_tools/Android.mk new file mode 100644 index 0000000..e8cbe39 --- /dev/null +++ b/tools/resampler_tools/Android.mk @@ -0,0 +1,17 @@ +# Copyright 2005 The Android Open Source Project +# +# Android.mk for resampler_tools +# + + +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_SRC_FILES := \ + fir.cpp + +LOCAL_MODULE := fir + +include $(BUILD_HOST_EXECUTABLE) + + diff --git a/tools/resampler_tools/fir.cpp b/tools/resampler_tools/fir.cpp new file mode 100644 index 0000000..cc3d509 --- /dev/null +++ b/tools/resampler_tools/fir.cpp @@ -0,0 +1,281 @@ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <math.h> +#include <stdio.h> +#include <unistd.h> +#include <stdlib.h> +#include <string.h> + +static double sinc(double x) { + if (fabs(x) == 0.0f) return 1.0f; + return sin(x) / x; +} + +static double sqr(double x) { + return x*x; +} + +static double I0(double x) { + // from the Numerical Recipes in C p. 237 + double ax,ans,y; + ax=fabs(x); + if (ax < 3.75) { + y=x/3.75; + y*=y; + ans=1.0+y*(3.5156229+y*(3.0899424+y*(1.2067492 + +y*(0.2659732+y*(0.360768e-1+y*0.45813e-2))))); + } else { + y=3.75/ax; + ans=(exp(ax)/sqrt(ax))*(0.39894228+y*(0.1328592e-1 + +y*(0.225319e-2+y*(-0.157565e-2+y*(0.916281e-2 + +y*(-0.2057706e-1+y*(0.2635537e-1+y*(-0.1647633e-1 + +y*0.392377e-2)))))))); + } + return ans; +} + +static double kaiser(int k, int N, double beta) { + if (k < 0 || k > N) + return 0; + return I0(beta * sqrt(1.0 - sqr((2.0*k)/N - 1.0))) / I0(beta); +} + + +static void usage(char* name) { + fprintf(stderr, + "usage: %s [-h] [-d] [-s sample_rate] [-c cut-off_frequency] [-n half_zero_crossings] [-f {float|fixed}] [-b beta] [-v dBFS] [-l lerp]\n" + " %s [-h] [-d] [-s sample_rate] [-c cut-off_frequency] [-n half_zero_crossings] [-f {float|fixed}] [-b beta] [-v dBFS] -p M/N\n" + " -h this help message\n" + " -d debug, print comma-separated coefficient table\n" + " -p generate poly-phase filter coefficients, with sample increment M/N\n" + " -s sample rate (48000)\n" + " -c cut-off frequency (20478)\n" + " -n number of zero-crossings on one side (8)\n" + " -l number of lerping bits (4)\n" + " -f output format, can be fixed-point or floating-point (fixed)\n" + " -b kaiser window parameter beta (7.865 [-80dB])\n" + " -v attenuation in dBFS (0)\n", + name, name + ); + exit(0); +} + +int main(int argc, char** argv) +{ + // nc is the number of bits to store the coefficients + const int nc = 32; + + bool polyphase = false; + unsigned int polyM = 160; + unsigned int polyN = 147; + bool debug = false; + double Fs = 48000; + double Fc = 20478; + double atten = 1; + int format = 0; + + + // in order to keep the errors associated with the linear + // interpolation of the coefficients below the quantization error + // we must satisfy: + // 2^nz >= 2^(nc/2) + // + // for 16 bit coefficients that would be 256 + // + // note that increasing nz only increases memory requirements, + // but doesn't increase the amount of computation to do. + // + // + // see: + // Smith, J.O. Digital Audio Resampling Home Page + // https://ccrma.stanford.edu/~jos/resample/, 2011-03-29 + // + int nz = 4; + + // | 0.1102*(A - 8.7) A > 50 + // beta = | 0.5842*(A - 21)^0.4 + 0.07886*(A - 21) 21 <= A <= 50 + // | 0 A < 21 + // with A is the desired stop-band attenuation in dBFS + // + // for eg: + // + // 30 dB 2.210 + // 40 dB 3.384 + // 50 dB 4.538 + // 60 dB 5.658 + // 70 dB 6.764 + // 80 dB 7.865 + // 90 dB 8.960 + // 100 dB 10.056 + double beta = 7.865; + + + // 2*nzc = (A - 8) / (2.285 * dw) + // with dw the transition width = 2*pi*dF/Fs + // + int nzc = 8; + + // + // Example: + // 44.1 KHz to 48 KHz resampling + // 100 dB rejection above 28 KHz + // (the spectrum will fold around 24 KHz and we want 100 dB rejection + // at the point where the folding reaches 20 KHz) + // ...___|_____ + // | \| + // | ____/|\____ + // |/alias| \ + // ------/------+------\---------> KHz + // 20 24 28 + + // Transition band 8 KHz, or dw = 1.0472 + // + // beta = 10.056 + // nzc = 20 + // + + int ch; + while ((ch = getopt(argc, argv, ":hds:c:n:f:l:b:p:v:")) != -1) { + switch (ch) { + case 'd': + debug = true; + break; + case 'p': + if (sscanf(optarg, "%u/%u", &polyM, &polyN) != 2) { + usage(argv[0]); + } + polyphase = true; + break; + case 's': + Fs = atof(optarg); + break; + case 'c': + Fc = atof(optarg); + break; + case 'n': + nzc = atoi(optarg); + break; + case 'l': + nz = atoi(optarg); + break; + case 'f': + if (!strcmp(optarg,"fixed")) format = 0; + else if (!strcmp(optarg,"float")) format = 1; + else usage(argv[0]); + break; + case 'b': + beta = atof(optarg); + break; + case 'v': + atten = pow(10, -fabs(atof(optarg))*0.05 ); + break; + case 'h': + default: + usage(argv[0]); + break; + } + } + + // cut off frequency ratio Fc/Fs + double Fcr = Fc / Fs; + + + // total number of coefficients (one side) + const int M = (1 << nz); + const int N = M * nzc; + + // generate the right half of the filter + if (!debug) { + printf("// cmd-line: "); + for (int i=1 ; i<argc ; i++) { + printf("%s ", argv[i]); + } + printf("\n"); + if (!polyphase) { + printf("const int32_t RESAMPLE_FIR_SIZE = %d;\n", N); + printf("const int32_t RESAMPLE_FIR_LERP_INT_BITS = %d;\n", nz); + printf("const int32_t RESAMPLE_FIR_NUM_COEF = %d;\n", nzc); + } else { + printf("const int32_t RESAMPLE_FIR_SIZE = %d;\n", 2*nzc*polyN); + printf("const int32_t RESAMPLE_FIR_NUM_COEF = %d;\n", 2*nzc); + } + if (!format) { + printf("const int32_t RESAMPLE_FIR_COEF_BITS = %d;\n", nc); + } + printf("\n"); + printf("static %s resampleFIR[] = {", !format ? "int32_t" : "float"); + } + + if (!polyphase) { + for (int i=0 ; i<=M ; i++) { // an extra set of coefs for interpolation + for (int j=0 ; j<nzc ; j++) { + int ix = j*M + i; + double x = (2.0 * M_PI * ix * Fcr) / (1 << nz); + double y = kaiser(ix+N, 2*N, beta) * sinc(x) * 2.0 * Fcr; + y *= atten; + + if (!debug) { + if (j == 0) + printf("\n "); + } + + if (!format) { + int64_t yi = floor(y * ((1ULL<<(nc-1))) + 0.5); + if (yi >= (1LL<<(nc-1))) yi = (1LL<<(nc-1))-1; + printf("0x%08x, ", int32_t(yi)); + } else { + printf("%.9g%s ", y, debug ? "," : "f,"); + } + } + } + } else { + for (int j=0 ; j<polyN ; j++) { + // calculate the phase + double p = ((polyM*j) % polyN) / double(polyN); + if (!debug) printf("\n "); + else printf("\n"); + // generate a FIR per phase + for (int i=-nzc ; i<nzc ; i++) { + double x = 2.0 * M_PI * Fcr * (i + p); + double y = kaiser(i+N, 2*N, beta) * sinc(x) * 2.0 * Fcr;; + y *= atten; + if (!format) { + int64_t yi = floor(y * ((1ULL<<(nc-1))) + 0.5); + if (yi >= (1LL<<(nc-1))) yi = (1LL<<(nc-1))-1; + printf("0x%08x", int32_t(yi)); + } else { + printf("%.9g%s", y, debug ? "" : "f"); + } + + if (debug && (i==nzc-1)) { + } else { + printf(", "); + } + } + } + } + + if (!debug) { + printf("\n};"); + } + printf("\n"); + return 0; +} + +// http://www.csee.umbc.edu/help/sound/AFsp-V2R1/html/audio/ResampAudio.html + + |