blob: 3e0ce6e6a550768ea603e05b6ac740b029fd55c6 [file] [log] [blame]
Alexandre Lision744f7422013-09-25 11:39:37 -04001/* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2010 Xiph.Org Foundation
3 Copyright (c) 2008 Gregory Maxwell
4 Written by Jean-Marc Valin and Gregory Maxwell */
5/*
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9
10 - Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12
13 - Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*/
29
30#ifdef HAVE_CONFIG_H
31#include "config.h"
32#endif
33
34#define CELT_C
35
36#include "os_support.h"
37#include "mdct.h"
38#include <math.h>
39#include "celt.h"
40#include "pitch.h"
41#include "bands.h"
42#include "modes.h"
43#include "entcode.h"
44#include "quant_bands.h"
45#include "rate.h"
46#include "stack_alloc.h"
47#include "mathops.h"
48#include "float_cast.h"
49#include <stdarg.h>
50#include "celt_lpc.h"
51#include "vq.h"
52
53#ifndef PACKAGE_VERSION
54#define PACKAGE_VERSION "unknown"
55#endif
56
Alexandre Lision744f7422013-09-25 11:39:37 -040057
Alexandre Lision85382382014-01-27 15:54:16 -050058int resampling_factor(opus_int32 rate)
Alexandre Lision744f7422013-09-25 11:39:37 -040059{
60 int ret;
61 switch (rate)
62 {
63 case 48000:
64 ret = 1;
65 break;
66 case 24000:
67 ret = 2;
68 break;
69 case 16000:
70 ret = 3;
71 break;
72 case 12000:
73 ret = 4;
74 break;
75 case 8000:
76 ret = 6;
77 break;
78 default:
79#ifndef CUSTOM_MODES
80 celt_assert(0);
81#endif
82 ret = 0;
83 break;
84 }
85 return ret;
86}
87
Alexandre Lision85382382014-01-27 15:54:16 -050088#ifndef OVERRIDE_COMB_FILTER_CONST
89static void comb_filter_const(opus_val32 *y, opus_val32 *x, int T, int N,
90 opus_val16 g10, opus_val16 g11, opus_val16 g12)
Alexandre Lision744f7422013-09-25 11:39:37 -040091{
Alexandre Lision85382382014-01-27 15:54:16 -050092 opus_val32 x0, x1, x2, x3, x4;
Alexandre Lision744f7422013-09-25 11:39:37 -040093 int i;
Alexandre Lision85382382014-01-27 15:54:16 -050094 x4 = x[-T-2];
95 x3 = x[-T-1];
96 x2 = x[-T];
97 x1 = x[-T+1];
Alexandre Lision744f7422013-09-25 11:39:37 -040098 for (i=0;i<N;i++)
99 {
Alexandre Lision85382382014-01-27 15:54:16 -0500100 x0=x[i-T+2];
101 y[i] = x[i]
102 + MULT16_32_Q15(g10,x2)
103 + MULT16_32_Q15(g11,ADD32(x1,x3))
104 + MULT16_32_Q15(g12,ADD32(x0,x4));
105 x4=x3;
106 x3=x2;
107 x2=x1;
108 x1=x0;
Alexandre Lision744f7422013-09-25 11:39:37 -0400109 }
Alexandre Lision744f7422013-09-25 11:39:37 -0400110
Alexandre Lision85382382014-01-27 15:54:16 -0500111}
Alexandre Lision744f7422013-09-25 11:39:37 -0400112#endif
Alexandre Lision744f7422013-09-25 11:39:37 -0400113
Alexandre Lision85382382014-01-27 15:54:16 -0500114void comb_filter(opus_val32 *y, opus_val32 *x, int T0, int T1, int N,
Alexandre Lision744f7422013-09-25 11:39:37 -0400115 opus_val16 g0, opus_val16 g1, int tapset0, int tapset1,
116 const opus_val16 *window, int overlap)
117{
118 int i;
119 /* printf ("%d %d %f %f\n", T0, T1, g0, g1); */
120 opus_val16 g00, g01, g02, g10, g11, g12;
Alexandre Lision85382382014-01-27 15:54:16 -0500121 opus_val32 x0, x1, x2, x3, x4;
Alexandre Lision744f7422013-09-25 11:39:37 -0400122 static const opus_val16 gains[3][3] = {
123 {QCONST16(0.3066406250f, 15), QCONST16(0.2170410156f, 15), QCONST16(0.1296386719f, 15)},
124 {QCONST16(0.4638671875f, 15), QCONST16(0.2680664062f, 15), QCONST16(0.f, 15)},
125 {QCONST16(0.7998046875f, 15), QCONST16(0.1000976562f, 15), QCONST16(0.f, 15)}};
Alexandre Lision85382382014-01-27 15:54:16 -0500126
127 if (g0==0 && g1==0)
128 {
129 /* OPT: Happens to work without the OPUS_MOVE(), but only because the current encoder already copies x to y */
130 if (x!=y)
131 OPUS_MOVE(y, x, N);
132 return;
133 }
Alexandre Lision744f7422013-09-25 11:39:37 -0400134 g00 = MULT16_16_Q15(g0, gains[tapset0][0]);
135 g01 = MULT16_16_Q15(g0, gains[tapset0][1]);
136 g02 = MULT16_16_Q15(g0, gains[tapset0][2]);
137 g10 = MULT16_16_Q15(g1, gains[tapset1][0]);
138 g11 = MULT16_16_Q15(g1, gains[tapset1][1]);
139 g12 = MULT16_16_Q15(g1, gains[tapset1][2]);
Alexandre Lision85382382014-01-27 15:54:16 -0500140 x1 = x[-T1+1];
141 x2 = x[-T1 ];
142 x3 = x[-T1-1];
143 x4 = x[-T1-2];
Alexandre Lision744f7422013-09-25 11:39:37 -0400144 for (i=0;i<overlap;i++)
145 {
146 opus_val16 f;
Alexandre Lision85382382014-01-27 15:54:16 -0500147 x0=x[i-T1+2];
Alexandre Lision744f7422013-09-25 11:39:37 -0400148 f = MULT16_16_Q15(window[i],window[i]);
149 y[i] = x[i]
150 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g00),x[i-T0])
Alexandre Lision85382382014-01-27 15:54:16 -0500151 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),ADD32(x[i-T0+1],x[i-T0-1]))
152 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),ADD32(x[i-T0+2],x[i-T0-2]))
153 + MULT16_32_Q15(MULT16_16_Q15(f,g10),x2)
154 + MULT16_32_Q15(MULT16_16_Q15(f,g11),ADD32(x1,x3))
155 + MULT16_32_Q15(MULT16_16_Q15(f,g12),ADD32(x0,x4));
156 x4=x3;
157 x3=x2;
158 x2=x1;
159 x1=x0;
Alexandre Lision744f7422013-09-25 11:39:37 -0400160
161 }
Alexandre Lision85382382014-01-27 15:54:16 -0500162 if (g1==0)
163 {
164 /* OPT: Happens to work without the OPUS_MOVE(), but only because the current encoder already copies x to y */
165 if (x!=y)
166 OPUS_MOVE(y+overlap, x+overlap, N-overlap);
167 return;
168 }
169
170 /* Compute the part with the constant filter. */
171 comb_filter_const(y+i, x+i, T1, N-i, g10, g11, g12);
Alexandre Lision744f7422013-09-25 11:39:37 -0400172}
173
Alexandre Lision85382382014-01-27 15:54:16 -0500174const signed char tf_select_table[4][8] = {
Alexandre Lision744f7422013-09-25 11:39:37 -0400175 {0, -1, 0, -1, 0,-1, 0,-1},
176 {0, -1, 0, -2, 1, 0, 1,-1},
177 {0, -2, 0, -3, 2, 0, 1,-1},
178 {0, -2, 0, -3, 3, 0, 1,-1},
179};
180
Alexandre Lision744f7422013-09-25 11:39:37 -0400181
Alexandre Lision85382382014-01-27 15:54:16 -0500182void init_caps(const CELTMode *m,int *cap,int LM,int C)
Alexandre Lision744f7422013-09-25 11:39:37 -0400183{
184 int i;
185 for (i=0;i<m->nbEBands;i++)
186 {
187 int N;
188 N=(m->eBands[i+1]-m->eBands[i])<<LM;
189 cap[i] = (m->cache.caps[m->nbEBands*(2*LM+C-1)+i]+64)*C*N>>2;
190 }
191}
192
Alexandre Lision744f7422013-09-25 11:39:37 -0400193
194
195const char *opus_strerror(int error)
196{
197 static const char * const error_strings[8] = {
198 "success",
199 "invalid argument",
200 "buffer too small",
201 "internal error",
202 "corrupted stream",
203 "request not implemented",
204 "invalid state",
205 "memory allocation failed"
206 };
207 if (error > 0 || error < -7)
208 return "unknown error";
209 else
210 return error_strings[-error];
211}
212
213const char *opus_get_version_string(void)
214{
215 return "libopus " PACKAGE_VERSION
216#ifdef FIXED_POINT
217 "-fixed"
218#endif
219#ifdef FUZZING
220 "-fuzzing"
221#endif
222 ;
223}