annotate src/opus-1.3/celt/celt.c @ 79:91c729825bca pa_catalina

Update build for AUDIO_COMPONENT_FIX
author Chris Cannam
date Wed, 30 Oct 2019 12:40:34 +0000
parents 7aeed7906520
children
rev   line source
Chris@69 1 /* Copyright (c) 2007-2008 CSIRO
Chris@69 2 Copyright (c) 2007-2010 Xiph.Org Foundation
Chris@69 3 Copyright (c) 2008 Gregory Maxwell
Chris@69 4 Written by Jean-Marc Valin and Gregory Maxwell */
Chris@69 5 /*
Chris@69 6 Redistribution and use in source and binary forms, with or without
Chris@69 7 modification, are permitted provided that the following conditions
Chris@69 8 are met:
Chris@69 9
Chris@69 10 - Redistributions of source code must retain the above copyright
Chris@69 11 notice, this list of conditions and the following disclaimer.
Chris@69 12
Chris@69 13 - Redistributions in binary form must reproduce the above copyright
Chris@69 14 notice, this list of conditions and the following disclaimer in the
Chris@69 15 documentation and/or other materials provided with the distribution.
Chris@69 16
Chris@69 17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Chris@69 18 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Chris@69 19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Chris@69 20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
Chris@69 21 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Chris@69 22 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Chris@69 23 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
Chris@69 24 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
Chris@69 25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
Chris@69 26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Chris@69 27 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Chris@69 28 */
Chris@69 29
Chris@69 30 #ifdef HAVE_CONFIG_H
Chris@69 31 #include "config.h"
Chris@69 32 #endif
Chris@69 33
Chris@69 34 #define CELT_C
Chris@69 35
Chris@69 36 #include "os_support.h"
Chris@69 37 #include "mdct.h"
Chris@69 38 #include <math.h>
Chris@69 39 #include "celt.h"
Chris@69 40 #include "pitch.h"
Chris@69 41 #include "bands.h"
Chris@69 42 #include "modes.h"
Chris@69 43 #include "entcode.h"
Chris@69 44 #include "quant_bands.h"
Chris@69 45 #include "rate.h"
Chris@69 46 #include "stack_alloc.h"
Chris@69 47 #include "mathops.h"
Chris@69 48 #include "float_cast.h"
Chris@69 49 #include <stdarg.h>
Chris@69 50 #include "celt_lpc.h"
Chris@69 51 #include "vq.h"
Chris@69 52
Chris@69 53 #ifndef PACKAGE_VERSION
Chris@69 54 #define PACKAGE_VERSION "unknown"
Chris@69 55 #endif
Chris@69 56
Chris@69 57 #if defined(MIPSr1_ASM)
Chris@69 58 #include "mips/celt_mipsr1.h"
Chris@69 59 #endif
Chris@69 60
Chris@69 61
Chris@69 62 int resampling_factor(opus_int32 rate)
Chris@69 63 {
Chris@69 64 int ret;
Chris@69 65 switch (rate)
Chris@69 66 {
Chris@69 67 case 48000:
Chris@69 68 ret = 1;
Chris@69 69 break;
Chris@69 70 case 24000:
Chris@69 71 ret = 2;
Chris@69 72 break;
Chris@69 73 case 16000:
Chris@69 74 ret = 3;
Chris@69 75 break;
Chris@69 76 case 12000:
Chris@69 77 ret = 4;
Chris@69 78 break;
Chris@69 79 case 8000:
Chris@69 80 ret = 6;
Chris@69 81 break;
Chris@69 82 default:
Chris@69 83 #ifndef CUSTOM_MODES
Chris@69 84 celt_assert(0);
Chris@69 85 #endif
Chris@69 86 ret = 0;
Chris@69 87 break;
Chris@69 88 }
Chris@69 89 return ret;
Chris@69 90 }
Chris@69 91
Chris@69 92 #if !defined(OVERRIDE_COMB_FILTER_CONST) || defined(NON_STATIC_COMB_FILTER_CONST_C)
Chris@69 93 /* This version should be faster on ARM */
Chris@69 94 #ifdef OPUS_ARM_ASM
Chris@69 95 #ifndef NON_STATIC_COMB_FILTER_CONST_C
Chris@69 96 static
Chris@69 97 #endif
Chris@69 98 void comb_filter_const_c(opus_val32 *y, opus_val32 *x, int T, int N,
Chris@69 99 opus_val16 g10, opus_val16 g11, opus_val16 g12)
Chris@69 100 {
Chris@69 101 opus_val32 x0, x1, x2, x3, x4;
Chris@69 102 int i;
Chris@69 103 x4 = SHL32(x[-T-2], 1);
Chris@69 104 x3 = SHL32(x[-T-1], 1);
Chris@69 105 x2 = SHL32(x[-T], 1);
Chris@69 106 x1 = SHL32(x[-T+1], 1);
Chris@69 107 for (i=0;i<N-4;i+=5)
Chris@69 108 {
Chris@69 109 opus_val32 t;
Chris@69 110 x0=SHL32(x[i-T+2],1);
Chris@69 111 t = MAC16_32_Q16(x[i], g10, x2);
Chris@69 112 t = MAC16_32_Q16(t, g11, ADD32(x1,x3));
Chris@69 113 t = MAC16_32_Q16(t, g12, ADD32(x0,x4));
Chris@69 114 t = SATURATE(t, SIG_SAT);
Chris@69 115 y[i] = t;
Chris@69 116 x4=SHL32(x[i-T+3],1);
Chris@69 117 t = MAC16_32_Q16(x[i+1], g10, x1);
Chris@69 118 t = MAC16_32_Q16(t, g11, ADD32(x0,x2));
Chris@69 119 t = MAC16_32_Q16(t, g12, ADD32(x4,x3));
Chris@69 120 t = SATURATE(t, SIG_SAT);
Chris@69 121 y[i+1] = t;
Chris@69 122 x3=SHL32(x[i-T+4],1);
Chris@69 123 t = MAC16_32_Q16(x[i+2], g10, x0);
Chris@69 124 t = MAC16_32_Q16(t, g11, ADD32(x4,x1));
Chris@69 125 t = MAC16_32_Q16(t, g12, ADD32(x3,x2));
Chris@69 126 t = SATURATE(t, SIG_SAT);
Chris@69 127 y[i+2] = t;
Chris@69 128 x2=SHL32(x[i-T+5],1);
Chris@69 129 t = MAC16_32_Q16(x[i+3], g10, x4);
Chris@69 130 t = MAC16_32_Q16(t, g11, ADD32(x3,x0));
Chris@69 131 t = MAC16_32_Q16(t, g12, ADD32(x2,x1));
Chris@69 132 t = SATURATE(t, SIG_SAT);
Chris@69 133 y[i+3] = t;
Chris@69 134 x1=SHL32(x[i-T+6],1);
Chris@69 135 t = MAC16_32_Q16(x[i+4], g10, x3);
Chris@69 136 t = MAC16_32_Q16(t, g11, ADD32(x2,x4));
Chris@69 137 t = MAC16_32_Q16(t, g12, ADD32(x1,x0));
Chris@69 138 t = SATURATE(t, SIG_SAT);
Chris@69 139 y[i+4] = t;
Chris@69 140 }
Chris@69 141 #ifdef CUSTOM_MODES
Chris@69 142 for (;i<N;i++)
Chris@69 143 {
Chris@69 144 opus_val32 t;
Chris@69 145 x0=SHL32(x[i-T+2],1);
Chris@69 146 t = MAC16_32_Q16(x[i], g10, x2);
Chris@69 147 t = MAC16_32_Q16(t, g11, ADD32(x1,x3));
Chris@69 148 t = MAC16_32_Q16(t, g12, ADD32(x0,x4));
Chris@69 149 t = SATURATE(t, SIG_SAT);
Chris@69 150 y[i] = t;
Chris@69 151 x4=x3;
Chris@69 152 x3=x2;
Chris@69 153 x2=x1;
Chris@69 154 x1=x0;
Chris@69 155 }
Chris@69 156 #endif
Chris@69 157 }
Chris@69 158 #else
Chris@69 159 #ifndef NON_STATIC_COMB_FILTER_CONST_C
Chris@69 160 static
Chris@69 161 #endif
Chris@69 162 void comb_filter_const_c(opus_val32 *y, opus_val32 *x, int T, int N,
Chris@69 163 opus_val16 g10, opus_val16 g11, opus_val16 g12)
Chris@69 164 {
Chris@69 165 opus_val32 x0, x1, x2, x3, x4;
Chris@69 166 int i;
Chris@69 167 x4 = x[-T-2];
Chris@69 168 x3 = x[-T-1];
Chris@69 169 x2 = x[-T];
Chris@69 170 x1 = x[-T+1];
Chris@69 171 for (i=0;i<N;i++)
Chris@69 172 {
Chris@69 173 x0=x[i-T+2];
Chris@69 174 y[i] = x[i]
Chris@69 175 + MULT16_32_Q15(g10,x2)
Chris@69 176 + MULT16_32_Q15(g11,ADD32(x1,x3))
Chris@69 177 + MULT16_32_Q15(g12,ADD32(x0,x4));
Chris@69 178 y[i] = SATURATE(y[i], SIG_SAT);
Chris@69 179 x4=x3;
Chris@69 180 x3=x2;
Chris@69 181 x2=x1;
Chris@69 182 x1=x0;
Chris@69 183 }
Chris@69 184
Chris@69 185 }
Chris@69 186 #endif
Chris@69 187 #endif
Chris@69 188
Chris@69 189 #ifndef OVERRIDE_comb_filter
Chris@69 190 void comb_filter(opus_val32 *y, opus_val32 *x, int T0, int T1, int N,
Chris@69 191 opus_val16 g0, opus_val16 g1, int tapset0, int tapset1,
Chris@69 192 const opus_val16 *window, int overlap, int arch)
Chris@69 193 {
Chris@69 194 int i;
Chris@69 195 /* printf ("%d %d %f %f\n", T0, T1, g0, g1); */
Chris@69 196 opus_val16 g00, g01, g02, g10, g11, g12;
Chris@69 197 opus_val32 x0, x1, x2, x3, x4;
Chris@69 198 static const opus_val16 gains[3][3] = {
Chris@69 199 {QCONST16(0.3066406250f, 15), QCONST16(0.2170410156f, 15), QCONST16(0.1296386719f, 15)},
Chris@69 200 {QCONST16(0.4638671875f, 15), QCONST16(0.2680664062f, 15), QCONST16(0.f, 15)},
Chris@69 201 {QCONST16(0.7998046875f, 15), QCONST16(0.1000976562f, 15), QCONST16(0.f, 15)}};
Chris@69 202
Chris@69 203 if (g0==0 && g1==0)
Chris@69 204 {
Chris@69 205 /* OPT: Happens to work without the OPUS_MOVE(), but only because the current encoder already copies x to y */
Chris@69 206 if (x!=y)
Chris@69 207 OPUS_MOVE(y, x, N);
Chris@69 208 return;
Chris@69 209 }
Chris@69 210 /* When the gain is zero, T0 and/or T1 is set to zero. We need
Chris@69 211 to have then be at least 2 to avoid processing garbage data. */
Chris@69 212 T0 = IMAX(T0, COMBFILTER_MINPERIOD);
Chris@69 213 T1 = IMAX(T1, COMBFILTER_MINPERIOD);
Chris@69 214 g00 = MULT16_16_P15(g0, gains[tapset0][0]);
Chris@69 215 g01 = MULT16_16_P15(g0, gains[tapset0][1]);
Chris@69 216 g02 = MULT16_16_P15(g0, gains[tapset0][2]);
Chris@69 217 g10 = MULT16_16_P15(g1, gains[tapset1][0]);
Chris@69 218 g11 = MULT16_16_P15(g1, gains[tapset1][1]);
Chris@69 219 g12 = MULT16_16_P15(g1, gains[tapset1][2]);
Chris@69 220 x1 = x[-T1+1];
Chris@69 221 x2 = x[-T1 ];
Chris@69 222 x3 = x[-T1-1];
Chris@69 223 x4 = x[-T1-2];
Chris@69 224 /* If the filter didn't change, we don't need the overlap */
Chris@69 225 if (g0==g1 && T0==T1 && tapset0==tapset1)
Chris@69 226 overlap=0;
Chris@69 227 for (i=0;i<overlap;i++)
Chris@69 228 {
Chris@69 229 opus_val16 f;
Chris@69 230 x0=x[i-T1+2];
Chris@69 231 f = MULT16_16_Q15(window[i],window[i]);
Chris@69 232 y[i] = x[i]
Chris@69 233 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g00),x[i-T0])
Chris@69 234 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),ADD32(x[i-T0+1],x[i-T0-1]))
Chris@69 235 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),ADD32(x[i-T0+2],x[i-T0-2]))
Chris@69 236 + MULT16_32_Q15(MULT16_16_Q15(f,g10),x2)
Chris@69 237 + MULT16_32_Q15(MULT16_16_Q15(f,g11),ADD32(x1,x3))
Chris@69 238 + MULT16_32_Q15(MULT16_16_Q15(f,g12),ADD32(x0,x4));
Chris@69 239 y[i] = SATURATE(y[i], SIG_SAT);
Chris@69 240 x4=x3;
Chris@69 241 x3=x2;
Chris@69 242 x2=x1;
Chris@69 243 x1=x0;
Chris@69 244
Chris@69 245 }
Chris@69 246 if (g1==0)
Chris@69 247 {
Chris@69 248 /* OPT: Happens to work without the OPUS_MOVE(), but only because the current encoder already copies x to y */
Chris@69 249 if (x!=y)
Chris@69 250 OPUS_MOVE(y+overlap, x+overlap, N-overlap);
Chris@69 251 return;
Chris@69 252 }
Chris@69 253
Chris@69 254 /* Compute the part with the constant filter. */
Chris@69 255 comb_filter_const(y+i, x+i, T1, N-i, g10, g11, g12, arch);
Chris@69 256 }
Chris@69 257 #endif /* OVERRIDE_comb_filter */
Chris@69 258
Chris@69 259 /* TF change table. Positive values mean better frequency resolution (longer
Chris@69 260 effective window), whereas negative values mean better time resolution
Chris@69 261 (shorter effective window). The second index is computed as:
Chris@69 262 4*isTransient + 2*tf_select + per_band_flag */
Chris@69 263 const signed char tf_select_table[4][8] = {
Chris@69 264 /*isTransient=0 isTransient=1 */
Chris@69 265 {0, -1, 0, -1, 0,-1, 0,-1}, /* 2.5 ms */
Chris@69 266 {0, -1, 0, -2, 1, 0, 1,-1}, /* 5 ms */
Chris@69 267 {0, -2, 0, -3, 2, 0, 1,-1}, /* 10 ms */
Chris@69 268 {0, -2, 0, -3, 3, 0, 1,-1}, /* 20 ms */
Chris@69 269 };
Chris@69 270
Chris@69 271
Chris@69 272 void init_caps(const CELTMode *m,int *cap,int LM,int C)
Chris@69 273 {
Chris@69 274 int i;
Chris@69 275 for (i=0;i<m->nbEBands;i++)
Chris@69 276 {
Chris@69 277 int N;
Chris@69 278 N=(m->eBands[i+1]-m->eBands[i])<<LM;
Chris@69 279 cap[i] = (m->cache.caps[m->nbEBands*(2*LM+C-1)+i]+64)*C*N>>2;
Chris@69 280 }
Chris@69 281 }
Chris@69 282
Chris@69 283
Chris@69 284
Chris@69 285 const char *opus_strerror(int error)
Chris@69 286 {
Chris@69 287 static const char * const error_strings[8] = {
Chris@69 288 "success",
Chris@69 289 "invalid argument",
Chris@69 290 "buffer too small",
Chris@69 291 "internal error",
Chris@69 292 "corrupted stream",
Chris@69 293 "request not implemented",
Chris@69 294 "invalid state",
Chris@69 295 "memory allocation failed"
Chris@69 296 };
Chris@69 297 if (error > 0 || error < -7)
Chris@69 298 return "unknown error";
Chris@69 299 else
Chris@69 300 return error_strings[-error];
Chris@69 301 }
Chris@69 302
Chris@69 303 const char *opus_get_version_string(void)
Chris@69 304 {
Chris@69 305 return "libopus " PACKAGE_VERSION
Chris@69 306 /* Applications may rely on the presence of this substring in the version
Chris@69 307 string to determine if they have a fixed-point or floating-point build
Chris@69 308 at runtime. */
Chris@69 309 #ifdef FIXED_POINT
Chris@69 310 "-fixed"
Chris@69 311 #endif
Chris@69 312 #ifdef FUZZING
Chris@69 313 "-fuzzing"
Chris@69 314 #endif
Chris@69 315 ;
Chris@69 316 }