AudioResamplerSinc.cpp revision 3bd72cc23f74e750069a2943ad3d5c9af3be4b55
1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "AudioResamplerSinc"
18//#define LOG_NDEBUG 0
19
20#include <string.h>
21#include "AudioResamplerSinc.h"
22#include <dlfcn.h>
23#include <cutils/properties.h>
24#include <stdlib.h>
25#include <utils/Log.h>
26
27namespace android {
28// ----------------------------------------------------------------------------
29
30
31/*
32 * These coeficients are computed with the "fir" utility found in
33 * tools/resampler_tools
34 * cmd-line: fir -v 0.3 -l 7 -s 48000 -c 20478
35 */
36const int32_t AudioResamplerSinc::mFirCoefsUp[] = {
37        0x7ba78e22, 0x7ba44428, 0x7b9a668f, 0x7b89f654, 0x7b72f51b, 0x7b556530, 0x7b314988, 0x7b06a5be, 0x7ad57e15, 0x7a9dd776, 0x7a5fb76f, 0x7a1b2433, 0x79d0249b, 0x797ec022, 0x7926fee5, 0x78c8e9a4, 0x786489be, 0x77f9e933, 0x7789129f, 0x7712113b, 0x7694f0de, 0x7611bdf6, 0x7588858a, 0x74f95538, 0x74643b33, 0x73c94642, 0x732885bc, 0x72820989, 0x71d5e21d, 0x7124207a, 0x706cd62a, 0x6fb0153e, 0x6eedf04d, 0x6e267a71, 0x6d59c744, 0x6c87eade, 0x6bb0f9d4, 0x6ad50932, 0x69f42e7d, 0x690e7fac, 0x68241328, 0x6734ffca, 0x66415cd4, 0x654941f4, 0x644cc73b, 0x634c051f, 0x62471477, 0x613e0e75, 0x60310ca7, 0x5f2028f0, 0x5e0b7d8a, 0x5cf324fd, 0x5bd73a21, 0x5ab7d815, 0x59951a42, 0x586f1c54, 0x5745fa37, 0x5619d015, 0x54eaba51, 0x53b8d585, 0x52843e7f, 0x514d1239, 0x50136ddd, 0x4ed76ebc, 0x4d99324c, 0x4c58d626, 0x4b167800, 0x49d235ab, 0x488c2d10, 0x47447c2b, 0x45fb410a, 0x44b099c4, 0x4364a47d, 0x42177f5c, 0x40c9488e, 0x3f7a1e3b, 0x3e2a1e87, 0x3cd96790, 0x3b881766, 0x3a364c0b, 0x38e4236f, 0x3791bb6b, 0x363f31c0, 0x34eca410, 0x339a2fe1, 0x3247f291, 0x30f6095c, 0x2fa49150, 0x2e53a752, 0x2d036813, 0x2bb3f012, 0x2a655b9a, 0x2917c6b7, 0x27cb4d3e, 0x26800abf, 0x25361a8c, 0x23ed97ae, 0x22a69ce7, 0x216144ad, 0x201da929, 0x1edbe431, 0x1d9c0f49, 0x1c5e439e, 0x1b229a04, 0x19e92af4, 0x18b20e89, 0x177d5c7e, 0x164b2c2a, 0x151b947f, 0x13eeac09, 0x12c488ea, 0x119d40d7, 0x1078e918, 0x0f579686, 0x0e395d87, 0x0d1e520d, 0x0c068797, 0x0af21128, 0x09e10150, 0x08d36a1f, 0x07c95d2c, 0x06c2eb8e, 0x05c025df, 0x04c11c38, 0x03c5de2f, 0x02ce7ad8, 0x01db00c3, 0x00eb7df9,
38        0x00000000, 0xff1893d3, 0xfe3545e8, 0xfd56222b, 0xfc7b33fd, 0xfba48638, 0xfad22328, 0xfa04148f, 0xf93a63a3, 0xf8751910, 0xf7b43cf2, 0xf6f7d6db, 0xf63fedcf, 0xf58c8845, 0xf4ddac29, 0xf4335ed9, 0xf38da525, 0xf2ec8353, 0xf24ffd1c, 0xf1b815ad, 0xf124cfa8, 0xf0962d24, 0xf00c2fad, 0xef86d846, 0xef06276a, 0xee8a1d0b, 0xee12b892, 0xed9ff8e4, 0xed31dc5f, 0xecc860de, 0xec6383b6, 0xec0341bc, 0xeba79744, 0xeb50801f, 0xeafdf7a4, 0xeaaff8aa, 0xea667d8d, 0xea21802e, 0xe9e0f9f7, 0xe9a4e3da, 0xe96d3654, 0xe939e96d, 0xe90af4be, 0xe8e04f6d, 0xe8b9f032, 0xe897cd5b, 0xe879dcc9, 0xe86013f5, 0xe84a67f4, 0xe838cd74, 0xe82b38c1, 0xe8219dc9, 0xe81bf01a, 0xe81a22e7, 0xe81c290b, 0xe821f507, 0xe82b790b, 0xe838a6f1, 0xe8497046, 0xe85dc648, 0xe87599e9, 0xe890dbd5, 0xe8af7c6e, 0xe8d16bd7, 0xe8f699ee, 0xe91ef654, 0xe94a706f, 0xe978f769, 0xe9aa7a37, 0xe9dee79a, 0xea162e21, 0xea503c2b, 0xea8cffec, 0xeacc676e, 0xeb0e6095, 0xeb52d91e, 0xeb99bea7, 0xebe2fead, 0xec2e8693, 0xec7c439f, 0xeccc2303, 0xed1e11db, 0xed71fd31, 0xedc7d201, 0xee1f7d3a, 0xee78ebc1, 0xeed40a74, 0xef30c62b, 0xef8f0bbd, 0xefeec803, 0xf04fe7d5, 0xf0b25814, 0xf11605a8, 0xf17add82, 0xf1e0cca2, 0xf247c017, 0xf2afa4ff, 0xf3186891, 0xf381f816, 0xf3ec40f2, 0xf45730a4, 0xf4c2b4c9, 0xf52ebb1b, 0xf59b3178, 0xf60805e2, 0xf6752681, 0xf6e281a4, 0xf75005c5, 0xf7bda18b, 0xf82b43c9, 0xf898db86, 0xf90657f7, 0xf973a887, 0xf9e0bcd7, 0xfa4d84bf, 0xfab9f050, 0xfb25efd6, 0xfb9173db, 0xfbfc6d24, 0xfc66ccb9, 0xfcd083e2, 0xfd398428, 0xfda1bf5c, 0xfe092790, 0xfe6faf21, 0xfed548af, 0xff39e729, 0xff9d7dc3,
39        0x00000000, 0x006161ae, 0x00c196e9, 0x0120941c, 0x017e4e00, 0x01dab9a0, 0x0235cc56, 0x028f7bcf, 0x02e7be0b, 0x033e895c, 0x0393d469, 0x03e7962c, 0x0439c5f4, 0x048a5b66, 0x04d94e7c, 0x05269785, 0x05722f29, 0x05bc0e63, 0x06042e86, 0x064a893e, 0x068f1889, 0x06d1d6c1, 0x0712be93, 0x0751cb05, 0x078ef772, 0x07ca3f8d, 0x08039f5e, 0x083b1344, 0x087097f3, 0x08a42a74, 0x08d5c827, 0x09056ebd, 0x09331c3e, 0x095ecf04, 0x098885bc, 0x09b03f66, 0x09d5fb52, 0x09f9b923, 0x0a1b78cb, 0x0a3b3a8a, 0x0a58fef1, 0x0a74c6dd, 0x0a8e9378, 0x0aa66638, 0x0abc40dd, 0x0ad02573, 0x0ae2164c, 0x0af21603, 0x0b00277a, 0x0b0c4dd8, 0x0b168c87, 0x0b1ee736, 0x0b2561d5, 0x0b2a0093, 0x0b2cc7e1, 0x0b2dbc6d, 0x0b2ce320, 0x0b2a411f, 0x0b25dbcb, 0x0b1fb8ba, 0x0b17ddbc, 0x0b0e50d4, 0x0b03183b, 0x0af63a5b, 0x0ae7bdd1, 0x0ad7a969, 0x0ac6041a, 0x0ab2d50d, 0x0a9e2391, 0x0a87f721, 0x0a70575f, 0x0a574c14, 0x0a3cdd2c, 0x0a2112b7, 0x0a03f4e7, 0x09e58c0d, 0x09c5e097, 0x09a4fb12, 0x0982e424, 0x095fa48c, 0x093b4523, 0x0915ced5, 0x08ef4aa5, 0x08c7c1a7, 0x089f3d00, 0x0875c5e5, 0x084b659a, 0x0820256e, 0x07f40ebb, 0x07c72ae4, 0x07998354, 0x076b217a, 0x073c0ecd, 0x070c54c3, 0x06dbfcd7, 0x06ab1080, 0x06799936, 0x0647a06e, 0x06152f96, 0x05e25018, 0x05af0b56, 0x057b6aa7, 0x0547775b, 0x05133ab3, 0x04debde6, 0x04aa0a19, 0x04752865, 0x044021d0, 0x040aff4c, 0x03d5c9ba, 0x03a089e5, 0x036b4882, 0x03360e30, 0x0300e373, 0x02cbd0b8, 0x0296de51, 0x02621472, 0x022d7b36, 0x01f91a98, 0x01c4fa74, 0x01912288, 0x015d9a6f, 0x012a69a6, 0x00f79784, 0x00c52b40, 0x00932be9, 0x0061a06e, 0x00308f96,
40        0x00000000, 0xffcff828, 0xffa07e5f, 0xff7198d0, 0xff434d7c, 0xff15a23b, 0xfee89cbb, 0xfebc4281, 0xfe9098e4, 0xfe65a513, 0xfe3b6c10, 0xfe11f2b0, 0xfde93d9e, 0xfdc15155, 0xfd9a3226, 0xfd73e434, 0xfd4e6b74, 0xfd29cbad, 0xfd06087a, 0xfce32547, 0xfcc12551, 0xfca00ba9, 0xfc7fdb31, 0xfc60969d, 0xfc424073, 0xfc24db0b, 0xfc086890, 0xfbeceafd, 0xfbd26423, 0xfbb8d5a2, 0xfba040ee, 0xfb88a750, 0xfb7209e1, 0xfb5c6990, 0xfb47c71f, 0xfb342324, 0xfb217e0a, 0xfb0fd810, 0xfaff314c, 0xfaef89a9, 0xfae0e0e7, 0xfad3369e, 0xfac68a3e, 0xfabadb0d, 0xfab0282a, 0xfaa6708d, 0xfa9db307, 0xfa95ee44, 0xfa8f20c9, 0xfa8948f7, 0xfa84650c, 0xfa807321, 0xfa7d712d, 0xfa7b5d05, 0xfa7a345d, 0xfa79f4c8, 0xfa7a9bb9, 0xfa7c2684, 0xfa7e925f, 0xfa81dc63, 0xfa86018b, 0xfa8afeb7, 0xfa90d0ac, 0xfa977415, 0xfa9ee582, 0xfaa7216d, 0xfab02435, 0xfab9ea25, 0xfac46f71, 0xfacfb037, 0xfadba883, 0xfae8544b, 0xfaf5af73, 0xfb03b5d1, 0xfb126324, 0xfb21b321, 0xfb31a16a, 0xfb422996, 0xfb53472c, 0xfb64f5a9, 0xfb77307e, 0xfb89f311, 0xfb9d38bf, 0xfbb0fcdb, 0xfbc53ab3, 0xfbd9ed8a, 0xfbef10a2, 0xfc049f32, 0xfc1a9472, 0xfc30eb93, 0xfc479fc5, 0xfc5eac35, 0xfc760c11, 0xfc8dba84, 0xfca5b2bc, 0xfcbdefe8, 0xfcd66d38, 0xfcef25e2, 0xfd08151d, 0xfd213624, 0xfd3a843c, 0xfd53faab, 0xfd6d94c0, 0xfd874dd3, 0xfda12141, 0xfdbb0a73, 0xfdd504da, 0xfdef0bf1, 0xfe091b41, 0xfe232e5a, 0xfe3d40dc, 0xfe574e72, 0xfe7152d5, 0xfe8b49cc, 0xfea52f2d, 0xfebefedd, 0xfed8b4cf, 0xfef24d09, 0xff0bc3a0, 0xff2514ba, 0xff3e3c8f, 0xff57376b, 0xff7001a9, 0xff8897b9, 0xffa0f61e, 0xffb91970, 0xffd0fe58, 0xffe8a197,
41        0x00000000, 0x0017167d, 0x002de20b, 0x00445fc0, 0x005a8cc4, 0x00706659, 0x0085e9d5, 0x009b14a4, 0x00afe44b, 0x00c45665, 0x00d868a3, 0x00ec18cd, 0x00ff64c6, 0x01124a84, 0x0124c816, 0x0136dba4, 0x0148836b, 0x0159bdc2, 0x016a8918, 0x017ae3f0, 0x018acce9, 0x019a42b6, 0x01a94425, 0x01b7d01a, 0x01c5e58e, 0x01d38396, 0x01e0a95a, 0x01ed561d, 0x01f98935, 0x02054211, 0x02108037, 0x021b4343, 0x02258ae6, 0x022f56e9, 0x0238a72b, 0x02417b9e, 0x0249d44b, 0x0251b151, 0x025912e4, 0x025ff949, 0x026664de, 0x026c5612, 0x0271cd69, 0x0276cb79, 0x027b50ed, 0x027f5e80, 0x0282f503, 0x02861556, 0x0288c06b, 0x028af746, 0x028cbafc, 0x028e0cb1, 0x028eed9b, 0x028f5eff, 0x028f6231, 0x028ef893, 0x028e2396, 0x028ce4bb, 0x028b3d8e, 0x02892fa9, 0x0286bcb2, 0x0283e65d, 0x0280ae68, 0x027d169e, 0x027920d3, 0x0274cee6, 0x027022c2, 0x026b1e58, 0x0265c3a6, 0x026014b0, 0x025a1382, 0x0253c232, 0x024d22da, 0x0246379d, 0x023f02a5, 0x0237861f, 0x022fc441, 0x0227bf42, 0x021f7961, 0x0216f4de, 0x020e3400, 0x0205390e, 0x01fc0653, 0x01f29e1c, 0x01e902b9, 0x01df367a, 0x01d53bb1, 0x01cb14ae, 0x01c0c3c5, 0x01b64b47, 0x01abad84, 0x01a0ecce, 0x01960b72, 0x018b0bbd, 0x017feffa, 0x0174ba6f, 0x01696d61, 0x015e0b11, 0x015295be, 0x01470f9f, 0x013b7aea, 0x012fd9cf, 0x01242e78, 0x01187b0c, 0x010cc1a9, 0x01010469, 0x00f54560, 0x00e9869a, 0x00ddca1c, 0x00d211e5, 0x00c65fec, 0x00bab620, 0x00af1668, 0x00a382a2, 0x0097fca6, 0x008c8641, 0x00812137, 0x0075cf45, 0x006a921e, 0x005f6b69, 0x00545cc6, 0x004967cc, 0x003e8e04, 0x0033d0f1, 0x0029320a, 0x001eb2bb, 0x00145466, 0x000a1864,
42        0x00000000, 0xfff60c7d, 0xffec3f11, 0xffe298e9, 0xffd91b25, 0xffcfc6dc, 0xffc69d18, 0xffbd9ed7, 0xffb4cd10, 0xffac28a9, 0xffa3b281, 0xff9b6b6a, 0xff93542b, 0xff8b6d80, 0xff83b819, 0xff7c349b, 0xff74e3a0, 0xff6dc5b6, 0xff66db62, 0xff60251c, 0xff59a351, 0xff535664, 0xff4d3ead, 0xff475c78, 0xff41b008, 0xff3c3995, 0xff36f94b, 0xff31ef4d, 0xff2d1bb4, 0xff287e8e, 0xff2417e0, 0xff1fe7a4, 0xff1bedca, 0xff182a3b, 0xff149cd2, 0xff114566, 0xff0e23c0, 0xff0b37a4, 0xff0880cb, 0xff05fee6, 0xff03b19d, 0xff01988f, 0xfeffb356, 0xfefe0182, 0xfefc829a, 0xfefb3620, 0xfefa1b8d, 0xfef93254, 0xfef879e0, 0xfef7f196, 0xfef798d4, 0xfef76ef3, 0xfef77344, 0xfef7a513, 0xfef803a8, 0xfef88e42, 0xfef9441d, 0xfefa2471, 0xfefb2e6f, 0xfefc6144, 0xfefdbc19, 0xfeff3e14, 0xff00e655, 0xff02b3f9, 0xff04a61b, 0xff06bbd0, 0xff08f42c, 0xff0b4e40, 0xff0dc91a, 0xff1063c5, 0xff131d4a, 0xff15f4b2, 0xff18e902, 0xff1bf93e, 0xff1f2469, 0xff226984, 0xff25c791, 0xff293d8f, 0xff2cca7e, 0xff306d5d, 0xff34252b, 0xff37f0e7, 0xff3bcf91, 0xff3fc028, 0xff43c1ad, 0xff47d321, 0xff4bf386, 0xff5021df, 0xff545d32, 0xff58a483, 0xff5cf6dc, 0xff615345, 0xff65b8ca, 0xff6a267a, 0xff6e9b62, 0xff731697, 0xff77972d, 0xff7c1c3b, 0xff80a4dd, 0xff85302f, 0xff89bd52, 0xff8e4b69, 0xff92d99c, 0xff976715, 0xff9bf301, 0xffa07c94, 0xffa50301, 0xffa98582, 0xffae0354, 0xffb27bb9, 0xffb6edf5, 0xffbb5953, 0xffbfbd1e, 0xffc418ab, 0xffc86b4e, 0xffccb463, 0xffd0f349, 0xffd52763, 0xffd9501b, 0xffdd6cde, 0xffe17d1d, 0xffe5804e, 0xffe975ed, 0xffed5d7b, 0xfff1367b, 0xfff50077, 0xfff8baff, 0xfffc65a4,
43        0x00000000, 0x000389af, 0x00070254, 0x000a6994, 0x000dbf1c, 0x0011029c, 0x001433c8, 0x0017525c, 0x001a5e15, 0x001d56b7, 0x00203c0a, 0x00230ddc, 0x0025cbfd, 0x00287644, 0x002b0c8b, 0x002d8eb2, 0x002ffc9c, 0x00325631, 0x00349b5d, 0x0036cc12, 0x0038e844, 0x003aefed, 0x003ce309, 0x003ec19a, 0x00408ba5, 0x00424134, 0x0043e254, 0x00456f15, 0x0046e78c, 0x00484bd1, 0x00499c00, 0x004ad839, 0x004c009d, 0x004d1552, 0x004e1683, 0x004f045a, 0x004fdf07, 0x0050a6bc, 0x00515bae, 0x0051fe16, 0x00528e2d, 0x00530c30, 0x00537860, 0x0053d2fd, 0x00541c4d, 0x00545496, 0x00547c20, 0x00549337, 0x00549a25, 0x0054913b, 0x005478c8, 0x0054511d, 0x00541a8f, 0x0053d572, 0x0053821b, 0x005320e4, 0x0052b223, 0x00523633, 0x0051ad6f, 0x00511831, 0x005076d8, 0x004fc9bf, 0x004f1144, 0x004e4dc6, 0x004d7fa4, 0x004ca73d, 0x004bc4ef, 0x004ad91c, 0x0049e423, 0x0048e663, 0x0047e03c, 0x0046d20f, 0x0045bc3b, 0x00449f1f, 0x00437b19, 0x0042508a, 0x00411fce, 0x003fe943, 0x003ead47, 0x003d6c34, 0x003c2668, 0x003adc3d, 0x00398e0c, 0x00383c2f, 0x0036e6fd, 0x00358ece, 0x003433f7, 0x0032d6cd, 0x003177a3, 0x003016cc, 0x002eb499, 0x002d5159, 0x002bed5b, 0x002a88ec, 0x00292456, 0x0027bfe5, 0x00265be0, 0x0024f88e, 0x00239636, 0x00223519, 0x0020d57b, 0x001f779c, 0x001e1bbb, 0x001cc214, 0x001b6ae3, 0x001a1661, 0x0018c4c7, 0x0017764a, 0x00162b1e, 0x0014e376, 0x00139f83, 0x00125f73, 0x00112373, 0x000febaf, 0x000eb84f, 0x000d897c, 0x000c5f5b, 0x000b3a10, 0x000a19bc, 0x0008fe81, 0x0007e87c, 0x0006d7cb, 0x0005cc88, 0x0004c6cc, 0x0003c6ae, 0x0002cc45, 0x0001d7a4, 0x0000e8dd,
44        0x00000000, 0xffff1d1d, 0xfffe4040, 0xfffd6975, 0xfffc98c6, 0xfffbce3b, 0xfffb09db, 0xfffa4bab, 0xfff993af, 0xfff8e1e9, 0xfff83659, 0xfff790ff, 0xfff6f1d9, 0xfff658e3, 0xfff5c619, 0xfff53974, 0xfff4b2ed, 0xfff4327c, 0xfff3b816, 0xfff343b1, 0xfff2d541, 0xfff26cb7, 0xfff20a07, 0xfff1ad20, 0xfff155f3, 0xfff1046d, 0xfff0b87e, 0xfff07211, 0xfff03112, 0xffeff56d, 0xffefbf0d, 0xffef8ddb, 0xffef61c0, 0xffef3aa3, 0xffef186e, 0xffeefb07, 0xffeee254, 0xffeece3c, 0xffeebea4, 0xffeeb371, 0xffeeac88, 0xffeea9cc, 0xffeeab22, 0xffeeb06e, 0xffeeb992, 0xffeec671, 0xffeed6ef, 0xffeeeaef, 0xffef0251, 0xffef1cfa, 0xffef3acb, 0xffef5ba6, 0xffef7f6e, 0xffefa605, 0xffefcf4d, 0xffeffb28, 0xfff02979, 0xfff05a22, 0xfff08d05, 0xfff0c207, 0xfff0f909, 0xfff131ef, 0xfff16c9c, 0xfff1a8f3, 0xfff1e6da, 0xfff22634, 0xfff266e6, 0xfff2a8d5, 0xfff2ebe6, 0xfff32fff, 0xfff37506, 0xfff3bae1, 0xfff40178, 0xfff448b2, 0xfff49077, 0xfff4d8b0, 0xfff52144, 0xfff56a1e, 0xfff5b328, 0xfff5fc4b, 0xfff64574, 0xfff68e8c, 0xfff6d782, 0xfff72040, 0xfff768b5, 0xfff7b0ce, 0xfff7f879, 0xfff83fa6, 0xfff88644, 0xfff8cc43, 0xfff91195, 0xfff95629, 0xfff999f3, 0xfff9dce5, 0xfffa1ef2, 0xfffa600e, 0xfffaa02d, 0xfffadf44, 0xfffb1d49, 0xfffb5a32, 0xfffb95f6, 0xfffbd08c, 0xfffc09ec, 0xfffc420e, 0xfffc78ed, 0xfffcae80, 0xfffce2c3, 0xfffd15b1, 0xfffd4744, 0xfffd7779, 0xfffda64c, 0xfffdd3ba, 0xfffdffc0, 0xfffe2a5c, 0xfffe538d, 0xfffe7b51, 0xfffea1a9, 0xfffec693, 0xfffeea11, 0xffff0c22, 0xffff2cc8, 0xffff4c05, 0xffff69db, 0xffff864b, 0xffffa15a, 0xffffbb09, 0xffffd35c, 0xffffea58,
45        0x00000000 // this one is needed for lerping the last coefficient
46};
47
48/*
49 * These coefficients are optimized for 48KHz -> 44.1KHz
50 * cmd-line: fir -v 0.3 -l 7 -s 48000 -c 16600
51 */
52const int32_t AudioResamplerSinc::mFirCoefsDown[] = {
53        0x7ba78e22, 0x7ba5ec84, 0x7ba107c0, 0x7b98e016, 0x7b8d75f3, 0x7b7ec9ed, 0x7b6cdcc5, 0x7b57af69, 0x7b3f42f0, 0x7b23989d, 0x7b04b1dc, 0x7ae29047, 0x7abd359f, 0x7a94a3d0, 0x7a68dcf4, 0x7a39e349, 0x7a07b93d, 0x79d26164, 0x7999de7d, 0x795e3370, 0x791f6350, 0x78dd7157, 0x789860e9, 0x78503592, 0x7804f307, 0x77b69d25, 0x776537f2, 0x7710c799, 0x76b95070, 0x765ed6f1, 0x76015fbf, 0x75a0efa2, 0x753d8b88, 0x74d73888, 0x746dfbda, 0x7401dade, 0x7392db19, 0x73210234, 0x72ac55fc, 0x7234dc61, 0x71ba9b77, 0x713d9976, 0x70bddcb7, 0x703b6bb6, 0x6fb64d11, 0x6f2e8786, 0x6ea421f5, 0x6e17235e, 0x6d8792e2, 0x6cf577bf, 0x6c60d954, 0x6bc9bf1f, 0x6b3030bb, 0x6a9435e0, 0x69f5d664, 0x69551a39, 0x68b2096e, 0x680cac2d, 0x67650abb, 0x66bb2d77, 0x660f1cda, 0x6560e178, 0x64b083fb, 0x63fe0d27, 0x634985d8, 0x6292f701, 0x61da69ab, 0x611fe6f5, 0x60637814, 0x5fa52650, 0x5ee4fb09, 0x5e22ffae, 0x5d5f3dc5, 0x5c99bee4, 0x5bd28cb4, 0x5b09b0ee, 0x5a3f355e, 0x597323dc, 0x58a58654, 0x57d666bd, 0x5705cf1d, 0x5633c98a, 0x55606024, 0x548b9d17, 0x53b58a9c, 0x52de32f7, 0x5205a075, 0x512bdd6f, 0x5050f443, 0x4f74ef5c, 0x4e97d929, 0x4db9bc22, 0x4cdaa2c5, 0x4bfa9795, 0x4b19a51b, 0x4a37d5e5, 0x49553484, 0x4871cb8b, 0x478da592, 0x46a8cd31, 0x45c34d02, 0x44dd2f9f, 0x43f67fa3, 0x430f47a7, 0x42279244, 0x413f6a10, 0x4056d99f, 0x3f6deb81, 0x3e84aa43, 0x3d9b206d, 0x3cb15882, 0x3bc75d00, 0x3add385c, 0x39f2f507, 0x39089d69, 0x381e3be1, 0x3733dac8, 0x3649846b, 0x355f430d, 0x347520e7, 0x338b2828, 0x32a162f0, 0x31b7db56, 0x30ce9b63, 0x2fe5ad11, 0x2efd1a4d, 0x2e14ecf6, 0x2d2d2eda,
54        0x2c45e9b9, 0x2b5f2742, 0x2a78f112, 0x299350b7, 0x28ae4fab, 0x27c9f756, 0x26e6510d, 0x26036613, 0x25213f95, 0x243fe6ac, 0x235f645c, 0x227fc196, 0x21a10731, 0x20c33def, 0x1fe66e7e, 0x1f0aa171, 0x1e2fdf44, 0x1d56305d, 0x1c7d9d06, 0x1ba62d74, 0x1acfe9be, 0x19fad9e5, 0x192705ce, 0x18547543, 0x17832ff3, 0x16b33d74, 0x15e4a53c, 0x15176ea9, 0x144ba0f9, 0x13814350, 0x12b85cb4, 0x11f0f40c, 0x112b1024, 0x1066b7a7, 0x0fa3f123, 0x0ee2c308, 0x0e2333a7, 0x0d654930, 0x0ca909b5, 0x0bee7b28, 0x0b35a35b, 0x0a7e8800, 0x09c92ea8, 0x09159cc5, 0x0863d7a5, 0x07b3e479, 0x0705c84e, 0x06598811, 0x05af288c, 0x0506ae68, 0x04601e2e, 0x03bb7c42, 0x0318cce7, 0x02781440, 0x01d9564b, 0x013c96e3, 0x00a1d9c5, 0x00092285, 0xff72749a, 0xfeddd356, 0xfe4b41e8, 0xfdbac35c, 0xfd2c5a9c, 0xfca00a6f, 0xfc15d57a, 0xfb8dbe3c, 0xfb07c716, 0xfa83f243, 0xfa0241db, 0xf982b7d4, 0xf9055602, 0xf88a1e16, 0xf811119e, 0xf79a3206, 0xf7258096, 0xf6b2fe76, 0xf642acab, 0xf5d48c16, 0xf5689d79, 0xf4fee173, 0xf4975880, 0xf43202fb, 0xf3cee11f, 0xf36df305, 0xf30f38a2, 0xf2b2b1d0, 0xf2585e42, 0xf2003d8f, 0xf1aa4f2b, 0xf156926c, 0xf1050685, 0xf0b5aa8d, 0xf0687d78, 0xf01d7e1e, 0xefd4ab35, 0xef8e0357, 0xef4984fd, 0xef072e84, 0xeec6fe2b, 0xee88f210, 0xee4d0839, 0xee133e8a, 0xeddb92ce, 0xeda602b0, 0xed728bc3, 0xed412b7b, 0xed11df32, 0xece4a425, 0xecb97779, 0xec905638, 0xec693d4f, 0xec442995, 0xec2117c5, 0xec000482, 0xebe0ec58, 0xebc3cbb7, 0xeba89efa, 0xeb8f6264, 0xeb781221, 0xeb62aa45, 0xeb4f26ce, 0xeb3d83a7, 0xeb2dbca1, 0xeb1fcd7b, 0xeb13b1df, 0xeb096562, 0xeb00e385, 0xeafa27b6,
55        0xeaf52d50, 0xeaf1ef9d, 0xeaf069d1, 0xeaf09712, 0xeaf27274, 0xeaf5f6fa, 0xeafb1f95, 0xeb01e728, 0xeb0a4886, 0xeb143e74, 0xeb1fc3a7, 0xeb2cd2c7, 0xeb3b666c, 0xeb4b7925, 0xeb5d0571, 0xeb7005c4, 0xeb847485, 0xeb9a4c11, 0xebb186ba, 0xebca1ec8, 0xebe40e77, 0xebff4ffb, 0xec1bdd80, 0xec39b127, 0xec58c50a, 0xec79133d, 0xec9a95ca, 0xecbd46b7, 0xece12000, 0xed061ba0, 0xed2c3388, 0xed5361a8, 0xed7b9fe8, 0xeda4e830, 0xedcf3461, 0xedfa7e5a, 0xee26bff8, 0xee53f315, 0xee82118a, 0xeeb1152e, 0xeee0f7d8, 0xef11b35d, 0xef434193, 0xef759c51, 0xefa8bd6e, 0xefdc9ec2, 0xf0113a28, 0xf046897c, 0xf07c869d, 0xf0b32b6c, 0xf0ea71cf, 0xf12253af, 0xf15acaf8, 0xf193d19c, 0xf1cd6192, 0xf20774d5, 0xf2420568, 0xf27d0d52, 0xf2b886a1, 0xf2f46b6a, 0xf330b5ca, 0xf36d5fe4, 0xf3aa63e4, 0xf3e7bbfe, 0xf4256270, 0xf463517e, 0xf4a18378, 0xf4dff2b7, 0xf51e999d, 0xf55d7297, 0xf59c781d, 0xf5dba4b2, 0xf61af2e4, 0xf65a5d4c, 0xf699de8f, 0xf6d97160, 0xf719107b, 0xf758b6ab, 0xf7985ec9, 0xf7d803b9, 0xf817a06d, 0xf8572fe6, 0xf896ad32, 0xf8d6136d, 0xf9155dc3, 0xf954876c, 0xf9938bb3, 0xf9d265ef, 0xfa111187, 0xfa4f89f3, 0xfa8dcab9, 0xfacbcf70, 0xfb0993bf, 0xfb47135d, 0xfb844a13, 0xfbc133ba, 0xfbfdcc3b, 0xfc3a0f90, 0xfc75f9c7, 0xfcb186fb, 0xfcecb35d, 0xfd277b2d, 0xfd61dabc, 0xfd9bce6f, 0xfdd552bd, 0xfe0e642d, 0xfe46ff5a, 0xfe7f20f1, 0xfeb6c5b1, 0xfeedea6c, 0xff248c06, 0xff5aa776, 0xff9039c5, 0xffc54010, 0xfff9b786, 0x002d9d69, 0x0060ef0e, 0x0093a9dd, 0x00c5cb50, 0x00f750f6, 0x0128386e, 0x01587f6d, 0x018823b9, 0x01b7232d, 0x01e57bb4, 0x02132b4f, 0x02403010, 0x026c881c,
56        0x029831ad, 0x02c32b0d, 0x02ed729c, 0x031706c9, 0x033fe618, 0x03680f20, 0x038f8089, 0x03b63910, 0x03dc3782, 0x04017abf, 0x042601ba, 0x0449cb78, 0x046cd70f, 0x048f23a9, 0x04b0b080, 0x04d17ce2, 0x04f1882b, 0x0510d1cc, 0x052f5947, 0x054d1e2e, 0x056a2024, 0x05865edf, 0x05a1da25, 0x05bc91cb, 0x05d685b9, 0x05efb5e6, 0x0608225b, 0x061fcb2f, 0x0636b08a, 0x064cd2a4, 0x066231c4, 0x0676ce42, 0x068aa883, 0x069dc0fd, 0x06b01833, 0x06c1aeba, 0x06d28532, 0x06e29c4a, 0x06f1f4c2, 0x07008f64, 0x070e6d0a, 0x071b8e9c, 0x0727f50e, 0x0733a162, 0x073e94a5, 0x0748cff4, 0x07525475, 0x075b235d, 0x07633dec, 0x076aa56d, 0x07715b37, 0x077760ae, 0x077cb73f, 0x07816063, 0x07855d9c, 0x0788b07a, 0x078b5a93, 0x078d5d89, 0x078ebb09, 0x078f74c8, 0x078f8c82, 0x078f0401, 0x078ddd14, 0x078c1993, 0x0789bb60, 0x0786c464, 0x0783368e, 0x077f13d8, 0x077a5e41, 0x077517d0, 0x076f4291, 0x0768e09a, 0x0761f403, 0x075a7eef, 0x07528382, 0x074a03e9, 0x07410255, 0x073780fc, 0x072d8219, 0x072307ec, 0x071814ba, 0x070caaca, 0x0700cc69, 0x06f47be7, 0x06e7bb97, 0x06da8dcf, 0x06ccf4e9, 0x06bef340, 0x06b08b35, 0x06a1bf28, 0x0692917b, 0x06830493, 0x06731ad7, 0x0662d6af, 0x06523a82, 0x064148bc, 0x063003c6, 0x061e6e0c, 0x060c89f8, 0x05fa59f5, 0x05e7e06f, 0x05d51fd0, 0x05c21a83, 0x05aed2ef, 0x059b4b7f, 0x05878698, 0x057386a1, 0x055f4dfc, 0x054adf0e, 0x05363c35, 0x052167d0, 0x050c643b, 0x04f733cf, 0x04e1d8e2, 0x04cc55c8, 0x04b6acd2, 0x04a0e04c, 0x048af281, 0x0474e5b7, 0x045ebc2f, 0x0448782a, 0x04321be1, 0x041ba98b, 0x0405235a, 0x03ee8b7b, 0x03d7e417, 0x03c12f51, 0x03aa6f4a,
57        0x0393a61a, 0x037cd5d6, 0x0366008e, 0x034f284c, 0x03384f14, 0x032176e3, 0x030aa1b4, 0x02f3d179, 0x02dd081e, 0x02c64789, 0x02af919c, 0x0298e830, 0x02824d17, 0x026bc220, 0x02554910, 0x023ee3a6, 0x0228939b, 0x02125aa0, 0x01fc3a61, 0x01e63480, 0x01d04a9a, 0x01ba7e44, 0x01a4d10c, 0x018f4478, 0x0179da08, 0x01649334, 0x014f716a, 0x013a7615, 0x0125a295, 0x0110f844, 0x00fc7872, 0x00e8246b, 0x00d3fd70, 0x00c004bc, 0x00ac3b81, 0x0098a2eb, 0x00853c1b, 0x0072082e, 0x005f0837, 0x004c3d40, 0x0039a84d, 0x00274a5a, 0x0015245a, 0x00033739, 0xfff183db, 0xffe00b1b, 0xffcecdcd, 0xffbdccbe, 0xffad08b2, 0xff9c8265, 0xff8c3a8b, 0xff7c31d2, 0xff6c68de, 0xff5ce04c, 0xff4d98b2, 0xff3e929e, 0xff2fce96, 0xff214d18, 0xff130e9b, 0xff05138f, 0xfef75c5b, 0xfee9e960, 0xfedcbaf7, 0xfecfd172, 0xfec32d1a, 0xfeb6ce34, 0xfeaab4fb, 0xfe9ee1a5, 0xfe93545e, 0xfe880d4e, 0xfe7d0c95, 0xfe72524c, 0xfe67de84, 0xfe5db14b, 0xfe53caa3, 0xfe4a2a8d, 0xfe40d0ff, 0xfe37bdec, 0xfe2ef13e, 0xfe266ada, 0xfe1e2a9e, 0xfe163064, 0xfe0e7bfe, 0xfe070d39, 0xfdffe3db, 0xfdf8ffa6, 0xfdf26054, 0xfdec059d, 0xfde5ef30, 0xfde01cb8, 0xfdda8ddc, 0xfdd5423b, 0xfdd03971, 0xfdcb7316, 0xfdc6eeb9, 0xfdc2abe9, 0xfdbeaa2d, 0xfdbae90a, 0xfdb767fd, 0xfdb42681, 0xfdb1240e, 0xfdae6015, 0xfdabda05, 0xfda99147, 0xfda78541, 0xfda5b557, 0xfda420e6, 0xfda2c74b, 0xfda1a7dd, 0xfda0c1f0, 0xfda014d5, 0xfd9f9fdc, 0xfd9f624e, 0xfd9f5b73, 0xfd9f8a91, 0xfd9feeeb, 0xfda087c0, 0xfda1544d, 0xfda253ce, 0xfda3857b, 0xfda4e88a, 0xfda67c31, 0xfda83fa0, 0xfdaa3209, 0xfdac529a, 0xfdaea081, 0xfdb11ae7, 0xfdb3c0f9,
58        0xfdb691dc, 0xfdb98cba, 0xfdbcb0b8, 0xfdbffcfa, 0xfdc370a5, 0xfdc70adc, 0xfdcacac1, 0xfdceaf74, 0xfdd2b818, 0xfdd6e3cc, 0xfddb31b0, 0xfddfa0e4, 0xfde43087, 0xfde8dfb8, 0xfdedad97, 0xfdf29942, 0xfdf7a1d8, 0xfdfcc679, 0xfe020645, 0xfe07605b, 0xfe0cd3dc, 0xfe125fe8, 0xfe1803a3, 0xfe1dbe2d, 0xfe238ea9, 0xfe29743c, 0xfe2f6e0a, 0xfe357b39, 0xfe3b9af0, 0xfe41cc56, 0xfe480e94, 0xfe4e60d6, 0xfe54c246, 0xfe5b3212, 0xfe61af68, 0xfe683978, 0xfe6ecf74, 0xfe75708f, 0xfe7c1bff, 0xfe82d0f9, 0xfe898eb7, 0xfe905473, 0xfe972169, 0xfe9df4d8, 0xfea4ce00, 0xfeabac24, 0xfeb28e88, 0xfeb97473, 0xfec05d2d, 0xfec74803, 0xfece3442, 0xfed5213a, 0xfedc0e3c, 0xfee2fa9f, 0xfee9e5b8, 0xfef0cee2, 0xfef7b579, 0xfefe98db, 0xff05786b, 0xff0c538b, 0xff1329a3, 0xff19fa1b, 0xff20c461, 0xff2787e2, 0xff2e4410, 0xff34f85f, 0xff3ba447, 0xff424740, 0xff48e0c9, 0xff4f705f, 0xff55f586, 0xff5c6fc2, 0xff62de9c, 0xff69419f, 0xff6f9858, 0xff75e258, 0xff7c1f32, 0xff824e7e, 0xff886fd4, 0xff8e82d1, 0xff948714, 0xff9a7c40, 0xffa061f8, 0xffa637e6, 0xffabfdb4, 0xffb1b310, 0xffb757ab, 0xffbceb37, 0xffc26d6c, 0xffc7de03, 0xffcd3cb8, 0xffd28949, 0xffd7c379, 0xffdceb0d, 0xffe1ffcc, 0xffe7017f, 0xffebeff5, 0xfff0cafc, 0xfff59268, 0xfffa460d, 0xfffee5c4, 0x00037166, 0x0007e8d2, 0x000c4be7, 0x00109a87, 0x0014d499, 0x0018fa02, 0x001d0aad, 0x00210688, 0x0024ed80, 0x0028bf89, 0x002c7c95, 0x0030249a, 0x0033b793, 0x00373579, 0x003a9e4b, 0x003df207, 0x004130b0, 0x00445a4a, 0x00476eda, 0x004a6e6a, 0x004d5903, 0x00502eb3, 0x0052ef87, 0x00559b91, 0x005832e3, 0x005ab591, 0x005d23b1,
59        0x005f7d5c, 0x0061c2ac, 0x0063f3bc, 0x006610aa, 0x00681995, 0x006a0e9e, 0x006befe8, 0x006dbd95, 0x006f77cd, 0x00711eb5, 0x0072b277, 0x0074333d, 0x0075a131, 0x0076fc81, 0x0078455a, 0x00797bed, 0x007aa068, 0x007bb2fe, 0x007cb3e3, 0x007da349, 0x007e8166, 0x007f4e6f, 0x00800a9d, 0x0080b626, 0x00815144, 0x0081dc31, 0x00825727, 0x0082c261, 0x00831e1c, 0x00836a95, 0x0083a80a, 0x0083d6b7, 0x0083f6dd, 0x008408bb, 0x00840c91, 0x0084029f, 0x0083eb26, 0x0083c667, 0x008394a4, 0x00835620, 0x00830b1d, 0x0082b3dc, 0x008250a3, 0x0081e1b2, 0x0081674f, 0x0080e1bc, 0x0080513c, 0x007fb615, 0x007f1089, 0x007e60dc, 0x007da752, 0x007ce42f, 0x007c17b7, 0x007b422c, 0x007a63d3, 0x00797cef, 0x00788dc2, 0x00779690, 0x0076979c, 0x00759127, 0x00748375, 0x00736ec6, 0x0072535c, 0x00713179, 0x0070095c, 0x006edb47, 0x006da779, 0x006c6e31, 0x006b2faf, 0x0069ec30, 0x0068a3f3, 0x00675735, 0x00660633, 0x0064b129, 0x00635852, 0x0061fbea, 0x00609c2a, 0x005f394d, 0x005dd38c, 0x005c6b1e, 0x005b003c, 0x0059931c, 0x005823f5, 0x0056b2fc, 0x00554066, 0x0053cc66, 0x00525730, 0x0050e0f6, 0x004f69ea, 0x004df23c, 0x004c7a1d, 0x004b01bb, 0x00498945, 0x004810e8, 0x004698d0, 0x0045212a, 0x0043aa20, 0x004233dd, 0x0040be88, 0x003f4a4b, 0x003dd74c, 0x003c65b3, 0x003af5a4, 0x00398744, 0x00381ab7, 0x0036b020, 0x003547a0, 0x0033e15a, 0x00327d6b, 0x00311bf5, 0x002fbd15, 0x002e60e9, 0x002d078c, 0x002bb11b, 0x002a5db0, 0x00290d66, 0x0027c054, 0x00267693, 0x0025303b, 0x0023ed60, 0x0022ae19, 0x0021727a, 0x00203a97, 0x001f0682, 0x001dd64d, 0x001caa0a, 0x001b81c7, 0x001a5d96,
60        0x00193d84, 0x0018219f, 0x001709f3, 0x0015f68d, 0x0014e779, 0x0013dcc0, 0x0012d66c, 0x0011d487, 0x0010d717, 0x000fde26, 0x000ee9b8, 0x000df9d5, 0x000d0e82, 0x000c27c2, 0x000b459a, 0x000a680d, 0x00098f1d, 0x0008bacc, 0x0007eb1a, 0x00072009, 0x00065999, 0x000597c7, 0x0004da94, 0x000421fc, 0x00036dfd, 0x0002be95, 0x000213be, 0x00016d76, 0x0000cbb6, 0x00002e7a, 0xffff95bc, 0xffff0175, 0xfffe719f, 0xfffde632, 0xfffd5f26, 0xfffcdc72, 0xfffc5e10, 0xfffbe3f4, 0xfffb6e16, 0xfffafc6b, 0xfffa8eea, 0xfffa2588, 0xfff9c039, 0xfff95ef2, 0xfff901a8, 0xfff8a84e, 0xfff852d8, 0xfff8013a, 0xfff7b366, 0xfff7694f, 0xfff722e9, 0xfff6e024, 0xfff6a0f4, 0xfff66549, 0xfff62d17, 0xfff5f84d, 0xfff5c6de, 0xfff598bb, 0xfff56dd4, 0xfff5461a, 0xfff5217e, 0xfff4fff1, 0xfff4e162, 0xfff4c5c3, 0xfff4ad03, 0xfff49712, 0xfff483e1, 0xfff47360, 0xfff4657e, 0xfff45a2c, 0xfff45159, 0xfff44af5, 0xfff446f1, 0xfff4453b, 0xfff445c5, 0xfff4487d, 0xfff44d54, 0xfff4543a, 0xfff45d1e, 0xfff467f1, 0xfff474a4, 0xfff48325, 0xfff49366, 0xfff4a556, 0xfff4b8e7, 0xfff4ce09, 0xfff4e4ad, 0xfff4fcc2, 0xfff5163b, 0xfff53109, 0xfff54d1b, 0xfff56a65, 0xfff588d7, 0xfff5a863, 0xfff5c8fb, 0xfff5ea91, 0xfff60d16, 0xfff6307e, 0xfff654bb, 0xfff679bf, 0xfff69f7d, 0xfff6c5e9, 0xfff6ecf5, 0xfff71495, 0xfff73cbe, 0xfff76562, 0xfff78e75, 0xfff7b7ed, 0xfff7e1be, 0xfff80bdc, 0xfff8363c, 0xfff860d4, 0xfff88b99, 0xfff8b681, 0xfff8e182, 0xfff90c92, 0xfff937a6, 0xfff962b7, 0xfff98dba, 0xfff9b8a7, 0xfff9e376, 0xfffa0e1d, 0xfffa3895, 0xfffa62d5, 0xfffa8cd6, 0xfffab691, 0xfffadfff, 0xfffb0917,
61        0x00000000 // this one is needed for lerping the last coefficient
62};
63
64// we use 15 bits to interpolate between these samples
65// this cannot change because the mul below rely on it.
66static const int pLerpBits = 15;
67
68static pthread_once_t once_control = PTHREAD_ONCE_INIT;
69static readCoefficientsFn readResampleCoefficients = NULL;
70
71/*static*/ AudioResamplerSinc::Constants AudioResamplerSinc::highQualityConstants;
72/*static*/ AudioResamplerSinc::Constants AudioResamplerSinc::veryHighQualityConstants;
73
74void AudioResamplerSinc::init_routine()
75{
76    // for high quality resampler, the parameters for coefficients are compile-time constants
77    Constants *c = &highQualityConstants;
78    c->coefsBits = RESAMPLE_FIR_LERP_INT_BITS;
79    c->cShift = kNumPhaseBits - c->coefsBits;
80    c->cMask = ((1<< c->coefsBits)-1) << c->cShift;
81    c->pShift = kNumPhaseBits - c->coefsBits - pLerpBits;
82    c->pMask = ((1<< pLerpBits)-1) << c->pShift;
83    c->halfNumCoefs = RESAMPLE_FIR_NUM_COEF;
84
85    // for very high quality resampler, the parameters are load-time constants
86    veryHighQualityConstants = highQualityConstants;
87
88    // Open the dll to get the coefficients for VERY_HIGH_QUALITY
89    void *resampleCoeffLib = dlopen("libaudio-resampler.so", RTLD_NOW);
90    ALOGV("Open libaudio-resampler library = %p", resampleCoeffLib);
91    if (resampleCoeffLib == NULL) {
92        ALOGE("Could not open audio-resampler library: %s", dlerror());
93        return;
94    }
95
96    readResampleCoefficients = (readCoefficientsFn) dlsym(resampleCoeffLib,
97            "readResamplerCoefficients");
98    readResampleFirNumCoeffFn readResampleFirNumCoeff = (readResampleFirNumCoeffFn)
99            dlsym(resampleCoeffLib, "readResampleFirNumCoeff");
100    readResampleFirLerpIntBitsFn readResampleFirLerpIntBits = (readResampleFirLerpIntBitsFn)
101            dlsym(resampleCoeffLib, "readResampleFirLerpIntBits");
102    if (!readResampleCoefficients || !readResampleFirNumCoeff || !readResampleFirLerpIntBits) {
103        readResampleCoefficients = NULL;
104        dlclose(resampleCoeffLib);
105        resampleCoeffLib = NULL;
106        ALOGE("Could not find symbol: %s", dlerror());
107        return;
108    }
109
110    c = &veryHighQualityConstants;
111    // we have 16 coefs samples per zero-crossing
112    c->coefsBits = readResampleFirLerpIntBits();
113    ALOGV("coefsBits = %d", c->coefsBits);
114    c->cShift = kNumPhaseBits - c->coefsBits;
115    c->cMask = ((1<<c->coefsBits)-1) << c->cShift;
116    c->pShift = kNumPhaseBits - c->coefsBits - pLerpBits;
117    c->pMask = ((1<<pLerpBits)-1) << c->pShift;
118    // number of zero-crossing on each side
119    c->halfNumCoefs = readResampleFirNumCoeff();
120    ALOGV("halfNumCoefs = %d", c->halfNumCoefs);
121    // note that we "leak" resampleCoeffLib until the process exits
122}
123
124// ----------------------------------------------------------------------------
125
126static inline
127int32_t mulRL(int left, int32_t in, uint32_t vRL)
128{
129#if defined(__arm__) && !defined(__thumb__)
130    int32_t out;
131    if (left) {
132        asm( "smultb %[out], %[in], %[vRL] \n"
133             : [out]"=r"(out)
134             : [in]"%r"(in), [vRL]"r"(vRL)
135             : );
136    } else {
137        asm( "smultt %[out], %[in], %[vRL] \n"
138             : [out]"=r"(out)
139             : [in]"%r"(in), [vRL]"r"(vRL)
140             : );
141    }
142    return out;
143#else
144    if (left) {
145        return int16_t(in>>16) * int16_t(vRL&0xFFFF);
146    } else {
147        return int16_t(in>>16) * int16_t(vRL>>16);
148    }
149#endif
150}
151
152static inline
153int32_t mulAdd(int16_t in, int32_t v, int32_t a)
154{
155#if defined(__arm__) && !defined(__thumb__)
156    int32_t out;
157    asm( "smlawb %[out], %[v], %[in], %[a] \n"
158         : [out]"=r"(out)
159         : [in]"%r"(in), [v]"r"(v), [a]"r"(a)
160         : );
161    return out;
162#else
163    return a + in * (v>>16);
164    // improved precision
165    // return a + in * (v>>16) + ((in * (v & 0xffff)) >> 16);
166#endif
167}
168
169static inline
170int32_t mulAddRL(int left, uint32_t inRL, int32_t v, int32_t a)
171{
172#if defined(__arm__) && !defined(__thumb__)
173    int32_t out;
174    if (left) {
175        asm( "smlawb %[out], %[v], %[inRL], %[a] \n"
176             : [out]"=r"(out)
177             : [inRL]"%r"(inRL), [v]"r"(v), [a]"r"(a)
178             : );
179    } else {
180        asm( "smlawt %[out], %[v], %[inRL], %[a] \n"
181             : [out]"=r"(out)
182             : [inRL]"%r"(inRL), [v]"r"(v), [a]"r"(a)
183             : );
184    }
185    return out;
186#else
187    if (left) {
188        return a + (int16_t(inRL&0xFFFF) * (v>>16));
189        //improved precision
190        // return a + (int16_t(inRL&0xFFFF) * (v>>16)) + ((int16_t(inRL&0xFFFF) * (v & 0xffff)) >> 16);
191    } else {
192        return a + (int16_t(inRL>>16) * (v>>16));
193    }
194#endif
195}
196
197// ----------------------------------------------------------------------------
198
199AudioResamplerSinc::AudioResamplerSinc(int bitDepth,
200        int inChannelCount, int32_t sampleRate, src_quality quality)
201    : AudioResampler(bitDepth, inChannelCount, sampleRate, quality),
202    mState(0)
203{
204    /*
205     * Layout of the state buffer for 32 tap:
206     *
207     * "present" sample            beginning of 2nd buffer
208     *                 v                v
209     *  0              01               2              23              3
210     *  0              F0               0              F0              F
211     * [pppppppppppppppInnnnnnnnnnnnnnnnpppppppppppppppInnnnnnnnnnnnnnnn]
212     *                 ^               ^ head
213     *
214     * p = past samples, convoluted with the (p)ositive side of sinc()
215     * n = future samples, convoluted with the (n)egative side of sinc()
216     * r = extra space for implementing the ring buffer
217     *
218     */
219
220    // Load the constants for coefficients
221    int ok = pthread_once(&once_control, init_routine);
222    if (ok != 0) {
223        ALOGE("%s pthread_once failed: %d", __func__, ok);
224    }
225    mConstants = (quality == VERY_HIGH_QUALITY) ? &veryHighQualityConstants : &highQualityConstants;
226}
227
228
229AudioResamplerSinc::~AudioResamplerSinc()
230{
231    delete[] mState;
232}
233
234void AudioResamplerSinc::init() {
235    const Constants *c = mConstants;
236
237    const size_t numCoefs = 2*c->halfNumCoefs;
238    const size_t stateSize = numCoefs * mChannelCount * 2;
239    mState = new int16_t[stateSize];
240    memset(mState, 0, sizeof(int16_t)*stateSize);
241    mImpulse = mState + (c->halfNumCoefs-1)*mChannelCount;
242    mRingFull = mImpulse + (numCoefs+1)*mChannelCount;
243}
244
245void AudioResamplerSinc::resample(int32_t* out, size_t outFrameCount,
246            AudioBufferProvider* provider)
247{
248
249    // FIXME store current state (up or down sample) and only load the coefs when the state
250    // changes. Or load two pointers one for up and one for down in the init function.
251    // Not critical now since the read functions are fast, but would be important if read was slow.
252    if (mConstants == &veryHighQualityConstants && readResampleCoefficients) {
253        ALOGV("get coefficient from libmm-audio resampler library");
254        mFirCoefs = (mInSampleRate <= mSampleRate) ? readResampleCoefficients(true) :
255                readResampleCoefficients(false);
256    } else {
257        ALOGV("Use default coefficients");
258        mFirCoefs = (mInSampleRate <= mSampleRate) ? mFirCoefsUp : mFirCoefsDown;
259    }
260
261    // select the appropriate resampler
262    switch (mChannelCount) {
263    case 1:
264        resample<1>(out, outFrameCount, provider);
265        break;
266    case 2:
267        resample<2>(out, outFrameCount, provider);
268        break;
269    }
270
271}
272
273
274template<int CHANNELS>
275void AudioResamplerSinc::resample(int32_t* out, size_t outFrameCount,
276        AudioBufferProvider* provider)
277{
278    const Constants *c = mConstants;
279    int16_t* impulse = mImpulse;
280    uint32_t vRL = mVolumeRL;
281    size_t inputIndex = mInputIndex;
282    uint32_t phaseFraction = mPhaseFraction;
283    uint32_t phaseIncrement = mPhaseIncrement;
284    size_t outputIndex = 0;
285    size_t outputSampleCount = outFrameCount * 2;
286    size_t inFrameCount = (outFrameCount*mInSampleRate)/mSampleRate;
287
288    while (outputIndex < outputSampleCount) {
289        // buffer is empty, fetch a new one
290        while (mBuffer.frameCount == 0) {
291            mBuffer.frameCount = inFrameCount;
292            provider->getNextBuffer(&mBuffer,
293                                    calculateOutputPTS(outputIndex / 2));
294            if (mBuffer.raw == NULL) {
295                goto resample_exit;
296            }
297            const uint32_t phaseIndex = phaseFraction >> kNumPhaseBits;
298            if (phaseIndex == 1) {
299                // read one frame
300                read<CHANNELS>(impulse, phaseFraction, mBuffer.i16, inputIndex);
301            } else if (phaseIndex == 2) {
302                // read 2 frames
303                read<CHANNELS>(impulse, phaseFraction, mBuffer.i16, inputIndex);
304                inputIndex++;
305                if (inputIndex >= mBuffer.frameCount) {
306                    inputIndex -= mBuffer.frameCount;
307                    provider->releaseBuffer(&mBuffer);
308                } else {
309                    read<CHANNELS>(impulse, phaseFraction, mBuffer.i16, inputIndex);
310                }
311            }
312        }
313        int16_t *in = mBuffer.i16;
314        const size_t frameCount = mBuffer.frameCount;
315
316        // Always read-in the first samples from the input buffer
317        int16_t* head = impulse + c->halfNumCoefs*CHANNELS;
318        head[0] = in[inputIndex*CHANNELS + 0];
319        if (CHANNELS == 2)
320            head[1] = in[inputIndex*CHANNELS + 1];
321
322        // handle boundary case
323        int32_t l, r;
324        while (outputIndex < outputSampleCount) {
325            filterCoefficient<CHANNELS>(l, r, phaseFraction, impulse);
326            out[outputIndex++] += 2 * mulRL(1, l, vRL);
327            out[outputIndex++] += 2 * mulRL(0, r, vRL);
328
329            phaseFraction += phaseIncrement;
330            const uint32_t phaseIndex = phaseFraction >> kNumPhaseBits;
331            if (phaseIndex == 1) {
332                inputIndex++;
333                if (inputIndex >= frameCount)
334                    break;  // need a new buffer
335                read<CHANNELS>(impulse, phaseFraction, in, inputIndex);
336            } else if (phaseIndex == 2) {    // maximum value
337                inputIndex++;
338                if (inputIndex >= frameCount)
339                    break;  // 0 frame available, 2 frames needed
340                // read first frame
341                read<CHANNELS>(impulse, phaseFraction, in, inputIndex);
342                inputIndex++;
343                if (inputIndex >= frameCount)
344                    break;  // 0 frame available, 1 frame needed
345                // read second frame
346                read<CHANNELS>(impulse, phaseFraction, in, inputIndex);
347            }
348        }
349
350        // if done with buffer, save samples
351        if (inputIndex >= frameCount) {
352            inputIndex -= frameCount;
353            provider->releaseBuffer(&mBuffer);
354        }
355    }
356
357resample_exit:
358    mImpulse = impulse;
359    mInputIndex = inputIndex;
360    mPhaseFraction = phaseFraction;
361}
362
363template<int CHANNELS>
364/***
365* read()
366*
367* This function reads only one frame from input buffer and writes it in
368* state buffer
369*
370**/
371void AudioResamplerSinc::read(
372        int16_t*& impulse, uint32_t& phaseFraction,
373        const int16_t* in, size_t inputIndex)
374{
375    const Constants *c = mConstants;
376    const uint32_t phaseIndex = phaseFraction >> kNumPhaseBits;
377    impulse += CHANNELS;
378    phaseFraction -= 1LU<<kNumPhaseBits;
379    if (impulse >= mRingFull) {
380        const size_t stateSize = (c->halfNumCoefs*2)*CHANNELS;
381        memcpy(mState, mState+stateSize, sizeof(int16_t)*stateSize);
382        impulse -= stateSize;
383    }
384    int16_t* head = impulse + c->halfNumCoefs*CHANNELS;
385    head[0] = in[inputIndex*CHANNELS + 0];
386    if (CHANNELS == 2)
387        head[1] = in[inputIndex*CHANNELS + 1];
388}
389
390template<int CHANNELS>
391void AudioResamplerSinc::filterCoefficient(
392        int32_t& l, int32_t& r, uint32_t phase, const int16_t *samples)
393{
394    const Constants *c = mConstants;
395
396    // compute the index of the coefficient on the positive side and
397    // negative side
398    uint32_t indexP = (phase & c->cMask) >> c->cShift;
399    uint16_t lerpP = (phase & c->pMask) >> c->pShift;
400    uint32_t indexN = (-phase & c->cMask) >> c->cShift;
401    uint16_t lerpN = (-phase & c->pMask) >> c->pShift;
402    if ((indexP == 0) && (lerpP == 0)) {
403        indexN = c->cMask >> c->cShift;
404        lerpN = c->pMask >> c->pShift;
405    }
406
407    l = 0;
408    r = 0;
409    const int32_t* coefs = mFirCoefs;
410    const int16_t *sP = samples;
411    const int16_t *sN = samples+CHANNELS;
412    for (unsigned int i=0 ; i < c->halfNumCoefs/4 ; i++) {
413        interpolate<CHANNELS>(l, r, coefs+indexP, lerpP, sP);
414        interpolate<CHANNELS>(l, r, coefs+indexN, lerpN, sN);
415        sP -= CHANNELS; sN += CHANNELS; coefs += 1 << c->coefsBits;
416        interpolate<CHANNELS>(l, r, coefs+indexP, lerpP, sP);
417        interpolate<CHANNELS>(l, r, coefs+indexN, lerpN, sN);
418        sP -= CHANNELS; sN += CHANNELS; coefs += 1 << c->coefsBits;
419        interpolate<CHANNELS>(l, r, coefs+indexP, lerpP, sP);
420        interpolate<CHANNELS>(l, r, coefs+indexN, lerpN, sN);
421        sP -= CHANNELS; sN += CHANNELS; coefs += 1 << c->coefsBits;
422        interpolate<CHANNELS>(l, r, coefs+indexP, lerpP, sP);
423        interpolate<CHANNELS>(l, r, coefs+indexN, lerpN, sN);
424        sP -= CHANNELS; sN += CHANNELS; coefs += 1 << c->coefsBits;
425    }
426}
427
428template<int CHANNELS>
429void AudioResamplerSinc::interpolate(
430        int32_t& l, int32_t& r,
431        const int32_t* coefs, int16_t lerp, const int16_t* samples)
432{
433    int32_t c0 = coefs[0];
434    int32_t c1 = coefs[1];
435    int32_t sinc = mulAdd(lerp, (c1-c0)<<1, c0);
436    if (CHANNELS == 2) {
437        uint32_t rl = *reinterpret_cast<const uint32_t*>(samples);
438        l = mulAddRL(1, rl, sinc, l);
439        r = mulAddRL(0, rl, sinc, r);
440    } else {
441        r = l = mulAdd(samples[0], sinc, l);
442    }
443}
444// ----------------------------------------------------------------------------
445}; // namespace android
446