Libav
|
00001 /* 00002 * Various fixed-point math operations 00003 * 00004 * Copyright (c) 2008 Vladimir Voroshilov 00005 * 00006 * This file is part of FFmpeg. 00007 * 00008 * FFmpeg is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * FFmpeg is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with FFmpeg; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 #include <inttypes.h> 00024 #include <limits.h> 00025 #include <assert.h> 00026 00027 #include "avcodec.h" 00028 #include "celp_math.h" 00029 00030 #ifdef G729_BITEXACT 00031 00034 static const int16_t base_cos[64] = 00035 { 00036 32767, 32729, 32610, 32413, 32138, 31786, 31357, 30853, 00037 30274, 29622, 28899, 28106, 27246, 26320, 25330, 24279, 00038 23170, 22006, 20788, 19520, 18205, 16846, 15447, 14010, 00039 12540, 11039, 9512, 7962, 6393, 4808, 3212, 1608, 00040 0, -1608, -3212, -4808, -6393, -7962, -9512, -11039, 00041 -12540, -14010, -15447, -16846, -18205, -19520, -20788, -22006, 00042 -23170, -24279, -25330, -26320, -27246, -28106, -28899, -29622, 00043 -30274, -30853, -31357, -31786, -32138, -32413, -32610, -32729 00044 }; 00045 00052 static const int16_t slope_cos[64] = 00053 { 00054 -632, -1893, -3150, -4399, -5638, -6863, -8072, -9261, 00055 -10428, -11570, -12684, -13767, -14817, -15832, -16808, -17744, 00056 -18637, -19486, -20287, -21039, -21741, -22390, -22986, -23526, 00057 -24009, -24435, -24801, -25108, -25354, -25540, -25664, -25726, 00058 -25726, -25664, -25540, -25354, -25108, -24801, -24435, -24009, 00059 -23526, -22986, -22390, -21741, -21039, -20287, -19486, -18637, 00060 -17744, -16808, -15832, -14817, -13767, -12684, -11570, -10428, 00061 -9261, -8072, -6863, -5638, -4399, -3150, -1893, -632 00062 }; 00063 00069 static const uint16_t tab_exp2[33] = 00070 { 00071 16384, 16743, 17109, 17484, 17867, 18258, 18658, 19066, 19484, 19911, 00072 20347, 20792, 21247, 21713, 22188, 22674, 23170, 23678, 24196, 24726, 00073 25268, 25821, 26386, 26964, 27554, 28158, 28774, 29405, 30048, 30706, 00074 31379, 32066, 32767 00075 }; 00076 00077 int16_t ff_cos(uint16_t arg) 00078 { 00079 uint8_t offset= arg; 00080 uint8_t ind = arg >> 8; 00081 00082 assert(arg < 0x4000); 00083 00084 return FFMAX(base_cos[ind] + ((slope_cos[ind] * offset) >> 12), -0x8000); 00085 } 00086 00087 int ff_exp2(uint16_t power) 00088 { 00089 uint16_t frac_x0; 00090 uint16_t frac_dx; 00091 int result; 00092 00093 assert(power <= 0x7fff); 00094 00095 frac_x0 = power >> 10; 00096 frac_dx = (power & 0x03ff) << 5; 00097 00098 result = tab_exp2[frac_x0] << 15; 00099 result += frac_dx * (tab_exp2[frac_x0+1] - tab_exp2[frac_x0]); 00100 00101 return result >> 10; 00102 } 00103 00104 #else // G729_BITEXACT 00105 00109 static const int16_t tab_cos[65] = 00110 { 00111 32767, 32738, 32617, 32421, 32145, 31793, 31364, 30860, 00112 30280, 29629, 28905, 28113, 27252, 26326, 25336, 24285, 00113 23176, 22011, 20793, 19525, 18210, 16851, 15451, 14014, 00114 12543, 11043, 9515, 7965, 6395, 4810, 3214, 1609, 00115 1, -1607, -3211, -4808, -6393, -7962, -9513, -11040, 00116 -12541, -14012, -15449, -16848, -18207, -19523, -20791, -22009, 00117 -23174, -24283, -25334, -26324, -27250, -28111, -28904, -29627, 00118 -30279, -30858, -31363, -31792, -32144, -32419, -32616, -32736, -32768, 00119 }; 00120 00121 static const uint16_t exp2a[]= 00122 { 00123 0, 1435, 2901, 4400, 5931, 7496, 9096, 10730, 00124 12400, 14106, 15850, 17632, 19454, 21315, 23216, 25160, 00125 27146, 29175, 31249, 33368, 35534, 37747, 40009, 42320, 00126 44682, 47095, 49562, 52082, 54657, 57289, 59979, 62727, 00127 }; 00128 00129 static const uint16_t exp2b[]= 00130 { 00131 3, 712, 1424, 2134, 2845, 3557, 4270, 4982, 00132 5696, 6409, 7124, 7839, 8554, 9270, 9986, 10704, 00133 11421, 12138, 12857, 13576, 14295, 15014, 15734, 16455, 00134 17176, 17898, 18620, 19343, 20066, 20790, 21514, 22238, 00135 }; 00136 00137 int16_t ff_cos(uint16_t arg) 00138 { 00139 uint8_t offset= arg; 00140 uint8_t ind = arg >> 8; 00141 00142 assert(arg <= 0x3fff); 00143 00144 return tab_cos[ind] + (offset * (tab_cos[ind+1] - tab_cos[ind]) >> 8); 00145 } 00146 00147 int ff_exp2(uint16_t power) 00148 { 00149 unsigned int result= exp2a[power>>10] + 0x10000; 00150 00151 assert(power <= 0x7fff); 00152 00153 result= (result<<3) + ((result*exp2b[(power>>5)&31])>>17); 00154 return result + ((result*(power&31)*89)>>22); 00155 } 00156 00157 #endif // else G729_BITEXACT 00158 00164 static const uint16_t tab_log2[33] = 00165 { 00166 #ifdef G729_BITEXACT 00167 0, 1455, 2866, 4236, 5568, 6863, 8124, 9352, 00168 10549, 11716, 12855, 13967, 15054, 16117, 17156, 18172, 00169 19167, 20142, 21097, 22033, 22951, 23852, 24735, 25603, 00170 26455, 27291, 28113, 28922, 29716, 30497, 31266, 32023, 32767, 00171 #else 00172 4, 1459, 2870, 4240, 5572, 6867, 8127, 9355, 00173 10552, 11719, 12858, 13971, 15057, 16120, 17158, 18175, 00174 19170, 20145, 21100, 22036, 22954, 23854, 24738, 25605, 00175 26457, 27294, 28116, 28924, 29719, 30500, 31269, 32025, 32769, 00176 #endif 00177 }; 00178 00179 int ff_log2(uint32_t value) 00180 { 00181 uint8_t power_int; 00182 uint8_t frac_x0; 00183 uint16_t frac_dx; 00184 00185 // Stripping zeros from beginning 00186 power_int = av_log2(value); 00187 value <<= (31 - power_int); 00188 00189 // b31 is always non-zero now 00190 frac_x0 = (value & 0x7c000000) >> 26; // b26-b31 and [32..63] -> [0..31] 00191 frac_dx = (value & 0x03fff800) >> 11; 00192 00193 value = tab_log2[frac_x0]; 00194 value += (frac_dx * (tab_log2[frac_x0+1] - tab_log2[frac_x0])) >> 15; 00195 00196 return (power_int << 15) + value; 00197 } 00198 00199 float ff_dot_productf(const float* a, const float* b, int length) 00200 { 00201 float sum = 0; 00202 int i; 00203 00204 for(i=0; i<length; i++) 00205 sum += a[i] * b[i]; 00206 00207 return sum; 00208 }