1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libopus/celt/celt.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,223 @@ 1.4 +/* Copyright (c) 2007-2008 CSIRO 1.5 + Copyright (c) 2007-2010 Xiph.Org Foundation 1.6 + Copyright (c) 2008 Gregory Maxwell 1.7 + Written by Jean-Marc Valin and Gregory Maxwell */ 1.8 +/* 1.9 + Redistribution and use in source and binary forms, with or without 1.10 + modification, are permitted provided that the following conditions 1.11 + are met: 1.12 + 1.13 + - Redistributions of source code must retain the above copyright 1.14 + notice, this list of conditions and the following disclaimer. 1.15 + 1.16 + - Redistributions in binary form must reproduce the above copyright 1.17 + notice, this list of conditions and the following disclaimer in the 1.18 + documentation and/or other materials provided with the distribution. 1.19 + 1.20 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.21 + ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.22 + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.23 + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 1.24 + OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.25 + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.26 + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.27 + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.28 + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.29 + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.30 + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.31 +*/ 1.32 + 1.33 +#ifdef HAVE_CONFIG_H 1.34 +#include "config.h" 1.35 +#endif 1.36 + 1.37 +#define CELT_C 1.38 + 1.39 +#include "os_support.h" 1.40 +#include "mdct.h" 1.41 +#include <math.h> 1.42 +#include "celt.h" 1.43 +#include "pitch.h" 1.44 +#include "bands.h" 1.45 +#include "modes.h" 1.46 +#include "entcode.h" 1.47 +#include "quant_bands.h" 1.48 +#include "rate.h" 1.49 +#include "stack_alloc.h" 1.50 +#include "mathops.h" 1.51 +#include "float_cast.h" 1.52 +#include <stdarg.h> 1.53 +#include "celt_lpc.h" 1.54 +#include "vq.h" 1.55 + 1.56 +#ifndef PACKAGE_VERSION 1.57 +#define PACKAGE_VERSION "unknown" 1.58 +#endif 1.59 + 1.60 + 1.61 +int resampling_factor(opus_int32 rate) 1.62 +{ 1.63 + int ret; 1.64 + switch (rate) 1.65 + { 1.66 + case 48000: 1.67 + ret = 1; 1.68 + break; 1.69 + case 24000: 1.70 + ret = 2; 1.71 + break; 1.72 + case 16000: 1.73 + ret = 3; 1.74 + break; 1.75 + case 12000: 1.76 + ret = 4; 1.77 + break; 1.78 + case 8000: 1.79 + ret = 6; 1.80 + break; 1.81 + default: 1.82 +#ifndef CUSTOM_MODES 1.83 + celt_assert(0); 1.84 +#endif 1.85 + ret = 0; 1.86 + break; 1.87 + } 1.88 + return ret; 1.89 +} 1.90 + 1.91 +#ifndef OVERRIDE_COMB_FILTER_CONST 1.92 +static void comb_filter_const(opus_val32 *y, opus_val32 *x, int T, int N, 1.93 + opus_val16 g10, opus_val16 g11, opus_val16 g12) 1.94 +{ 1.95 + opus_val32 x0, x1, x2, x3, x4; 1.96 + int i; 1.97 + x4 = x[-T-2]; 1.98 + x3 = x[-T-1]; 1.99 + x2 = x[-T]; 1.100 + x1 = x[-T+1]; 1.101 + for (i=0;i<N;i++) 1.102 + { 1.103 + x0=x[i-T+2]; 1.104 + y[i] = x[i] 1.105 + + MULT16_32_Q15(g10,x2) 1.106 + + MULT16_32_Q15(g11,ADD32(x1,x3)) 1.107 + + MULT16_32_Q15(g12,ADD32(x0,x4)); 1.108 + x4=x3; 1.109 + x3=x2; 1.110 + x2=x1; 1.111 + x1=x0; 1.112 + } 1.113 + 1.114 +} 1.115 +#endif 1.116 + 1.117 +void comb_filter(opus_val32 *y, opus_val32 *x, int T0, int T1, int N, 1.118 + opus_val16 g0, opus_val16 g1, int tapset0, int tapset1, 1.119 + const opus_val16 *window, int overlap) 1.120 +{ 1.121 + int i; 1.122 + /* printf ("%d %d %f %f\n", T0, T1, g0, g1); */ 1.123 + opus_val16 g00, g01, g02, g10, g11, g12; 1.124 + opus_val32 x0, x1, x2, x3, x4; 1.125 + static const opus_val16 gains[3][3] = { 1.126 + {QCONST16(0.3066406250f, 15), QCONST16(0.2170410156f, 15), QCONST16(0.1296386719f, 15)}, 1.127 + {QCONST16(0.4638671875f, 15), QCONST16(0.2680664062f, 15), QCONST16(0.f, 15)}, 1.128 + {QCONST16(0.7998046875f, 15), QCONST16(0.1000976562f, 15), QCONST16(0.f, 15)}}; 1.129 + 1.130 + if (g0==0 && g1==0) 1.131 + { 1.132 + /* OPT: Happens to work without the OPUS_MOVE(), but only because the current encoder already copies x to y */ 1.133 + if (x!=y) 1.134 + OPUS_MOVE(y, x, N); 1.135 + return; 1.136 + } 1.137 + g00 = MULT16_16_Q15(g0, gains[tapset0][0]); 1.138 + g01 = MULT16_16_Q15(g0, gains[tapset0][1]); 1.139 + g02 = MULT16_16_Q15(g0, gains[tapset0][2]); 1.140 + g10 = MULT16_16_Q15(g1, gains[tapset1][0]); 1.141 + g11 = MULT16_16_Q15(g1, gains[tapset1][1]); 1.142 + g12 = MULT16_16_Q15(g1, gains[tapset1][2]); 1.143 + x1 = x[-T1+1]; 1.144 + x2 = x[-T1 ]; 1.145 + x3 = x[-T1-1]; 1.146 + x4 = x[-T1-2]; 1.147 + for (i=0;i<overlap;i++) 1.148 + { 1.149 + opus_val16 f; 1.150 + x0=x[i-T1+2]; 1.151 + f = MULT16_16_Q15(window[i],window[i]); 1.152 + y[i] = x[i] 1.153 + + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g00),x[i-T0]) 1.154 + + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),ADD32(x[i-T0+1],x[i-T0-1])) 1.155 + + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),ADD32(x[i-T0+2],x[i-T0-2])) 1.156 + + MULT16_32_Q15(MULT16_16_Q15(f,g10),x2) 1.157 + + MULT16_32_Q15(MULT16_16_Q15(f,g11),ADD32(x1,x3)) 1.158 + + MULT16_32_Q15(MULT16_16_Q15(f,g12),ADD32(x0,x4)); 1.159 + x4=x3; 1.160 + x3=x2; 1.161 + x2=x1; 1.162 + x1=x0; 1.163 + 1.164 + } 1.165 + if (g1==0) 1.166 + { 1.167 + /* OPT: Happens to work without the OPUS_MOVE(), but only because the current encoder already copies x to y */ 1.168 + if (x!=y) 1.169 + OPUS_MOVE(y+overlap, x+overlap, N-overlap); 1.170 + return; 1.171 + } 1.172 + 1.173 + /* Compute the part with the constant filter. */ 1.174 + comb_filter_const(y+i, x+i, T1, N-i, g10, g11, g12); 1.175 +} 1.176 + 1.177 +const signed char tf_select_table[4][8] = { 1.178 + {0, -1, 0, -1, 0,-1, 0,-1}, 1.179 + {0, -1, 0, -2, 1, 0, 1,-1}, 1.180 + {0, -2, 0, -3, 2, 0, 1,-1}, 1.181 + {0, -2, 0, -3, 3, 0, 1,-1}, 1.182 +}; 1.183 + 1.184 + 1.185 +void init_caps(const CELTMode *m,int *cap,int LM,int C) 1.186 +{ 1.187 + int i; 1.188 + for (i=0;i<m->nbEBands;i++) 1.189 + { 1.190 + int N; 1.191 + N=(m->eBands[i+1]-m->eBands[i])<<LM; 1.192 + cap[i] = (m->cache.caps[m->nbEBands*(2*LM+C-1)+i]+64)*C*N>>2; 1.193 + } 1.194 +} 1.195 + 1.196 + 1.197 + 1.198 +const char *opus_strerror(int error) 1.199 +{ 1.200 + static const char * const error_strings[8] = { 1.201 + "success", 1.202 + "invalid argument", 1.203 + "buffer too small", 1.204 + "internal error", 1.205 + "corrupted stream", 1.206 + "request not implemented", 1.207 + "invalid state", 1.208 + "memory allocation failed" 1.209 + }; 1.210 + if (error > 0 || error < -7) 1.211 + return "unknown error"; 1.212 + else 1.213 + return error_strings[-error]; 1.214 +} 1.215 + 1.216 +const char *opus_get_version_string(void) 1.217 +{ 1.218 + return "libopus " PACKAGE_VERSION 1.219 +#ifdef FIXED_POINT 1.220 + "-fixed" 1.221 +#endif 1.222 +#ifdef FUZZING 1.223 + "-fuzzing" 1.224 +#endif 1.225 + ; 1.226 +}