summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libopus/celt
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libopus/celt')
-rw-r--r--lib/rbcodec/codecs/libopus/celt/_kiss_fft_guts.h175
-rw-r--r--lib/rbcodec/codecs/libopus/celt/arch.h209
-rw-r--r--lib/rbcodec/codecs/libopus/celt/bands.c1302
-rw-r--r--lib/rbcodec/codecs/libopus/celt/bands.h95
-rw-r--r--lib/rbcodec/codecs/libopus/celt/celt.c2870
-rw-r--r--lib/rbcodec/codecs/libopus/celt/celt.h117
-rw-r--r--lib/rbcodec/codecs/libopus/celt/celt_lpc.c188
-rw-r--r--lib/rbcodec/codecs/libopus/celt/celt_lpc.h53
-rw-r--r--lib/rbcodec/codecs/libopus/celt/cwrs.c645
-rw-r--r--lib/rbcodec/codecs/libopus/celt/cwrs.h48
-rw-r--r--lib/rbcodec/codecs/libopus/celt/ecintrin.h87
-rw-r--r--lib/rbcodec/codecs/libopus/celt/entcode.c88
-rw-r--r--lib/rbcodec/codecs/libopus/celt/entcode.h116
-rw-r--r--lib/rbcodec/codecs/libopus/celt/entdec.c245
-rw-r--r--lib/rbcodec/codecs/libopus/celt/entdec.h100
-rw-r--r--lib/rbcodec/codecs/libopus/celt/entenc.c294
-rw-r--r--lib/rbcodec/codecs/libopus/celt/entenc.h110
-rw-r--r--lib/rbcodec/codecs/libopus/celt/fixed_generic.h129
-rw-r--r--lib/rbcodec/codecs/libopus/celt/float_cast.h140
-rw-r--r--lib/rbcodec/codecs/libopus/celt/kiss_fft.c722
-rw-r--r--lib/rbcodec/codecs/libopus/celt/kiss_fft.h139
-rw-r--r--lib/rbcodec/codecs/libopus/celt/laplace.c134
-rw-r--r--lib/rbcodec/codecs/libopus/celt/laplace.h48
-rw-r--r--lib/rbcodec/codecs/libopus/celt/mathops.c206
-rw-r--r--lib/rbcodec/codecs/libopus/celt/mathops.h237
-rw-r--r--lib/rbcodec/codecs/libopus/celt/mdct.c332
-rw-r--r--lib/rbcodec/codecs/libopus/celt/mdct.h70
-rw-r--r--lib/rbcodec/codecs/libopus/celt/mfrngcod.h48
-rw-r--r--lib/rbcodec/codecs/libopus/celt/modes.c430
-rw-r--r--lib/rbcodec/codecs/libopus/celt/modes.h83
-rw-r--r--lib/rbcodec/codecs/libopus/celt/os_support.h89
-rw-r--r--lib/rbcodec/codecs/libopus/celt/pitch.c410
-rw-r--r--lib/rbcodec/codecs/libopus/celt/pitch.h48
-rw-r--r--lib/rbcodec/codecs/libopus/celt/quant_bands.c567
-rw-r--r--lib/rbcodec/codecs/libopus/celt/quant_bands.h60
-rw-r--r--lib/rbcodec/codecs/libopus/celt/rate.c638
-rw-r--r--lib/rbcodec/codecs/libopus/celt/rate.h101
-rw-r--r--lib/rbcodec/codecs/libopus/celt/stack_alloc.h149
-rw-r--r--lib/rbcodec/codecs/libopus/celt/static_modes_fixed.h595
-rw-r--r--lib/rbcodec/codecs/libopus/celt/vq.c415
-rw-r--r--lib/rbcodec/codecs/libopus/celt/vq.h73
41 files changed, 12605 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libopus/celt/_kiss_fft_guts.h b/lib/rbcodec/codecs/libopus/celt/_kiss_fft_guts.h
new file mode 100644
index 0000000000..33e62c6b3d
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/_kiss_fft_guts.h
@@ -0,0 +1,175 @@
1/*Copyright (c) 2003-2004, Mark Borgerding
2
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 POSSIBILITY OF SUCH DAMAGE.*/
25
26#ifndef KISS_FFT_GUTS_H
27#define KISS_FFT_GUTS_H
28
29#define MIN(a,b) ((a)<(b) ? (a):(b))
30#define MAX(a,b) ((a)>(b) ? (a):(b))
31
32/* kiss_fft.h
33 defines kiss_fft_scalar as either short or a float type
34 and defines
35 typedef struct { kiss_fft_scalar r; kiss_fft_scalar i; }kiss_fft_cpx; */
36#include "kiss_fft.h"
37
38/*
39 Explanation of macros dealing with complex math:
40
41 C_MUL(m,a,b) : m = a*b
42 C_FIXDIV( c , div ) : if a fixed point impl., c /= div. noop otherwise
43 C_SUB( res, a,b) : res = a - b
44 C_SUBFROM( res , a) : res -= a
45 C_ADDTO( res , a) : res += a
46 * */
47#ifdef FIXED_POINT
48#include "arch.h"
49
50
51#define SAMP_MAX 2147483647
52#define TWID_MAX 32767
53#define TRIG_UPSCALE 1
54
55#define SAMP_MIN -SAMP_MAX
56
57
58# define S_MUL(a,b) MULT16_32_Q15(b, a)
59
60# define C_MUL(m,a,b) \
61 do{ (m).r = SUB32(S_MUL((a).r,(b).r) , S_MUL((a).i,(b).i)); \
62 (m).i = ADD32(S_MUL((a).r,(b).i) , S_MUL((a).i,(b).r)); }while(0)
63
64# define C_MULC(m,a,b) \
65 do{ (m).r = ADD32(S_MUL((a).r,(b).r) , S_MUL((a).i,(b).i)); \
66 (m).i = SUB32(S_MUL((a).i,(b).r) , S_MUL((a).r,(b).i)); }while(0)
67
68# define C_MUL4(m,a,b) \
69 do{ (m).r = SHR32(SUB32(S_MUL((a).r,(b).r) , S_MUL((a).i,(b).i)),2); \
70 (m).i = SHR32(ADD32(S_MUL((a).r,(b).i) , S_MUL((a).i,(b).r)),2); }while(0)
71
72# define C_MULBYSCALAR( c, s ) \
73 do{ (c).r = S_MUL( (c).r , s ) ;\
74 (c).i = S_MUL( (c).i , s ) ; }while(0)
75
76# define DIVSCALAR(x,k) \
77 (x) = S_MUL( x, (TWID_MAX-((k)>>1))/(k)+1 )
78
79# define C_FIXDIV(c,div) \
80 do { DIVSCALAR( (c).r , div); \
81 DIVSCALAR( (c).i , div); }while (0)
82
83#define C_ADD( res, a,b)\
84 do {(res).r=ADD32((a).r,(b).r); (res).i=ADD32((a).i,(b).i); \
85 }while(0)
86#define C_SUB( res, a,b)\
87 do {(res).r=SUB32((a).r,(b).r); (res).i=SUB32((a).i,(b).i); \
88 }while(0)
89#define C_ADDTO( res , a)\
90 do {(res).r = ADD32((res).r, (a).r); (res).i = ADD32((res).i,(a).i);\
91 }while(0)
92
93#define C_SUBFROM( res , a)\
94 do {(res).r = ADD32((res).r,(a).r); (res).i = SUB32((res).i,(a).i); \
95 }while(0)
96
97#else /* not FIXED_POINT*/
98
99# define S_MUL(a,b) ( (a)*(b) )
100#define C_MUL(m,a,b) \
101 do{ (m).r = (a).r*(b).r - (a).i*(b).i;\
102 (m).i = (a).r*(b).i + (a).i*(b).r; }while(0)
103#define C_MULC(m,a,b) \
104 do{ (m).r = (a).r*(b).r + (a).i*(b).i;\
105 (m).i = (a).i*(b).r - (a).r*(b).i; }while(0)
106
107#define C_MUL4(m,a,b) C_MUL(m,a,b)
108
109# define C_FIXDIV(c,div) /* NOOP */
110# define C_MULBYSCALAR( c, s ) \
111 do{ (c).r *= (s);\
112 (c).i *= (s); }while(0)
113#endif
114
115#ifndef CHECK_OVERFLOW_OP
116# define CHECK_OVERFLOW_OP(a,op,b) /* noop */
117#endif
118
119#ifndef C_ADD
120#define C_ADD( res, a,b)\
121 do { \
122 CHECK_OVERFLOW_OP((a).r,+,(b).r)\
123 CHECK_OVERFLOW_OP((a).i,+,(b).i)\
124 (res).r=(a).r+(b).r; (res).i=(a).i+(b).i; \
125 }while(0)
126#define C_SUB( res, a,b)\
127 do { \
128 CHECK_OVERFLOW_OP((a).r,-,(b).r)\
129 CHECK_OVERFLOW_OP((a).i,-,(b).i)\
130 (res).r=(a).r-(b).r; (res).i=(a).i-(b).i; \
131 }while(0)
132#define C_ADDTO( res , a)\
133 do { \
134 CHECK_OVERFLOW_OP((res).r,+,(a).r)\
135 CHECK_OVERFLOW_OP((res).i,+,(a).i)\
136 (res).r += (a).r; (res).i += (a).i;\
137 }while(0)
138
139#define C_SUBFROM( res , a)\
140 do {\
141 CHECK_OVERFLOW_OP((res).r,-,(a).r)\
142 CHECK_OVERFLOW_OP((res).i,-,(a).i)\
143 (res).r -= (a).r; (res).i -= (a).i; \
144 }while(0)
145#endif /* C_ADD defined */
146
147#ifdef FIXED_POINT
148/*# define KISS_FFT_COS(phase) TRIG_UPSCALE*floor(MIN(32767,MAX(-32767,.5+32768 * cos (phase))))
149# define KISS_FFT_SIN(phase) TRIG_UPSCALE*floor(MIN(32767,MAX(-32767,.5+32768 * sin (phase))))*/
150# define KISS_FFT_COS(phase) floor(.5+TWID_MAX*cos (phase))
151# define KISS_FFT_SIN(phase) floor(.5+TWID_MAX*sin (phase))
152# define HALF_OF(x) ((x)>>1)
153#elif defined(USE_SIMD)
154# define KISS_FFT_COS(phase) _mm_set1_ps( cos(phase) )
155# define KISS_FFT_SIN(phase) _mm_set1_ps( sin(phase) )
156# define HALF_OF(x) ((x)*_mm_set1_ps(.5f))
157#else
158# define KISS_FFT_COS(phase) (kiss_fft_scalar) cos(phase)
159# define KISS_FFT_SIN(phase) (kiss_fft_scalar) sin(phase)
160# define HALF_OF(x) ((x)*.5f)
161#endif
162
163#define kf_cexp(x,phase) \
164 do{ \
165 (x)->r = KISS_FFT_COS(phase);\
166 (x)->i = KISS_FFT_SIN(phase);\
167 }while(0)
168
169#define kf_cexp2(x,phase) \
170 do{ \
171 (x)->r = TRIG_UPSCALE*celt_cos_norm((phase));\
172 (x)->i = TRIG_UPSCALE*celt_cos_norm((phase)-32768);\
173}while(0)
174
175#endif /* KISS_FFT_GUTS_H */
diff --git a/lib/rbcodec/codecs/libopus/celt/arch.h b/lib/rbcodec/codecs/libopus/celt/arch.h
new file mode 100644
index 0000000000..03cda40f69
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/arch.h
@@ -0,0 +1,209 @@
1/* Copyright (c) 2003-2008 Jean-Marc Valin
2 Copyright (c) 2007-2008 CSIRO
3 Copyright (c) 2007-2009 Xiph.Org Foundation
4 Written by Jean-Marc Valin */
5/**
6 @file arch.h
7 @brief Various architecture definitions for CELT
8*/
9/*
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions
12 are met:
13
14 - Redistributions of source code must retain the above copyright
15 notice, this list of conditions and the following disclaimer.
16
17 - Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in the
19 documentation and/or other materials provided with the distribution.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*/
33
34#ifndef ARCH_H
35#define ARCH_H
36
37#include "opus_types.h"
38
39# if !defined(__GNUC_PREREQ)
40# if defined(__GNUC__)&&defined(__GNUC_MINOR__)
41# define __GNUC_PREREQ(_maj,_min) \
42 ((__GNUC__<<16)+__GNUC_MINOR__>=((_maj)<<16)+(_min))
43# else
44# define __GNUC_PREREQ(_maj,_min) 0
45# endif
46# endif
47
48#define CELT_SIG_SCALE 32768.f
49
50#define celt_fatal(str) _celt_fatal(str, __FILE__, __LINE__);
51#ifdef ENABLE_ASSERTIONS
52#include <stdio.h>
53#include <stdlib.h>
54#ifdef __GNUC__
55__attribute__((noreturn))
56#endif
57static inline void _celt_fatal(const char *str, const char *file, int line)
58{
59 fprintf (stderr, "Fatal (internal) error in %s, line %d: %s\n", file, line, str);
60 abort();
61}
62#define celt_assert(cond) {if (!(cond)) {celt_fatal("assertion failed: " #cond);}}
63#define celt_assert2(cond, message) {if (!(cond)) {celt_fatal("assertion failed: " #cond "\n" message);}}
64#else
65#define celt_assert(cond)
66#define celt_assert2(cond, message)
67#endif
68
69#define IMUL32(a,b) ((a)*(b))
70
71#define ABS(x) ((x) < 0 ? (-(x)) : (x)) /**< Absolute integer value. */
72#define ABS16(x) ((x) < 0 ? (-(x)) : (x)) /**< Absolute 16-bit value. */
73#define MIN16(a,b) ((a) < (b) ? (a) : (b)) /**< Minimum 16-bit value. */
74#define MAX16(a,b) ((a) > (b) ? (a) : (b)) /**< Maximum 16-bit value. */
75#define ABS32(x) ((x) < 0 ? (-(x)) : (x)) /**< Absolute 32-bit value. */
76#define MIN32(a,b) ((a) < (b) ? (a) : (b)) /**< Minimum 32-bit value. */
77#define MAX32(a,b) ((a) > (b) ? (a) : (b)) /**< Maximum 32-bit value. */
78#define IMIN(a,b) ((a) < (b) ? (a) : (b)) /**< Minimum int value. */
79#define IMAX(a,b) ((a) > (b) ? (a) : (b)) /**< Maximum int value. */
80#define UADD32(a,b) ((a)+(b))
81#define USUB32(a,b) ((a)-(b))
82
83#define PRINT_MIPS(file)
84
85#ifdef FIXED_POINT
86
87typedef opus_int16 opus_val16;
88typedef opus_int32 opus_val32;
89
90typedef opus_val32 celt_sig;
91typedef opus_val16 celt_norm;
92typedef opus_val32 celt_ener;
93
94#define Q15ONE 32767
95
96#define SIG_SHIFT 12
97
98#define NORM_SCALING 16384
99
100#define DB_SHIFT 10
101
102#define EPSILON 1
103#define VERY_LARGE16 ((opus_val16)32767)
104#define Q15_ONE ((opus_val16)32767)
105
106#define SCALEIN(a) (a)
107#define SCALEOUT(a) (a)
108
109#ifdef FIXED_DEBUG
110#include "fixed_debug.h"
111#else
112
113#include "fixed_generic.h"
114
115#ifdef ARM5E_ASM
116#include "fixed_arm5e.h"
117#elif defined (ARM4_ASM)
118#include "fixed_arm4.h"
119#elif defined (BFIN_ASM)
120#include "fixed_bfin.h"
121#elif defined (TI_C5X_ASM)
122#include "fixed_c5x.h"
123#elif defined (TI_C6X_ASM)
124#include "fixed_c6x.h"
125#endif
126
127#endif
128
129#else /* FIXED_POINT */
130
131typedef float opus_val16;
132typedef float opus_val32;
133
134typedef float celt_sig;
135typedef float celt_norm;
136typedef float celt_ener;
137
138#define Q15ONE 1.0f
139
140#define NORM_SCALING 1.f
141
142#define EPSILON 1e-15f
143#define VERY_LARGE16 1e15f
144#define Q15_ONE ((opus_val16)1.f)
145
146#define QCONST16(x,bits) (x)
147#define QCONST32(x,bits) (x)
148
149#define NEG16(x) (-(x))
150#define NEG32(x) (-(x))
151#define EXTRACT16(x) (x)
152#define EXTEND32(x) (x)
153#define SHR16(a,shift) (a)
154#define SHL16(a,shift) (a)
155#define SHR32(a,shift) (a)
156#define SHL32(a,shift) (a)
157#define PSHR32(a,shift) (a)
158#define VSHR32(a,shift) (a)
159
160#define PSHR(a,shift) (a)
161#define SHR(a,shift) (a)
162#define SHL(a,shift) (a)
163#define SATURATE(x,a) (x)
164
165#define ROUND16(a,shift) (a)
166#define HALF16(x) (.5f*(x))
167#define HALF32(x) (.5f*(x))
168
169#define ADD16(a,b) ((a)+(b))
170#define SUB16(a,b) ((a)-(b))
171#define ADD32(a,b) ((a)+(b))
172#define SUB32(a,b) ((a)-(b))
173#define MULT16_16_16(a,b) ((a)*(b))
174#define MULT16_16(a,b) ((opus_val32)(a)*(opus_val32)(b))
175#define MAC16_16(c,a,b) ((c)+(opus_val32)(a)*(opus_val32)(b))
176
177#define MULT16_32_Q15(a,b) ((a)*(b))
178#define MULT16_32_Q16(a,b) ((a)*(b))
179
180#define MULT32_32_Q31(a,b) ((a)*(b))
181
182#define MAC16_32_Q15(c,a,b) ((c)+(a)*(b))
183
184#define MULT16_16_Q11_32(a,b) ((a)*(b))
185#define MULT16_16_Q13(a,b) ((a)*(b))
186#define MULT16_16_Q14(a,b) ((a)*(b))
187#define MULT16_16_Q15(a,b) ((a)*(b))
188#define MULT16_16_P15(a,b) ((a)*(b))
189#define MULT16_16_P13(a,b) ((a)*(b))
190#define MULT16_16_P14(a,b) ((a)*(b))
191#define MULT16_32_P16(a,b) ((a)*(b))
192
193#define DIV32_16(a,b) (((opus_val32)(a))/(opus_val16)(b))
194#define DIV32(a,b) (((opus_val32)(a))/(opus_val32)(b))
195
196#define SCALEIN(a) ((a)*CELT_SIG_SCALE)
197#define SCALEOUT(a) ((a)*(1/CELT_SIG_SCALE))
198
199#endif /* !FIXED_POINT */
200
201#ifndef GLOBAL_STACK_SIZE
202#ifdef FIXED_POINT
203#define GLOBAL_STACK_SIZE 100000
204#else
205#define GLOBAL_STACK_SIZE 100000
206#endif
207#endif
208
209#endif /* ARCH_H */
diff --git a/lib/rbcodec/codecs/libopus/celt/bands.c b/lib/rbcodec/codecs/libopus/celt/bands.c
new file mode 100644
index 0000000000..6e612980b6
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/bands.c
@@ -0,0 +1,1302 @@
1/* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2009 Xiph.Org Foundation
3 Copyright (c) 2008-2009 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 "opus_config.h"
32#endif
33
34#include <math.h>
35#include "bands.h"
36#include "modes.h"
37#include "vq.h"
38#include "cwrs.h"
39#include "stack_alloc.h"
40#include "os_support.h"
41#include "mathops.h"
42#include "rate.h"
43
44opus_uint32 celt_lcg_rand(opus_uint32 seed)
45{
46 return 1664525 * seed + 1013904223;
47}
48
49/* This is a cos() approximation designed to be bit-exact on any platform. Bit exactness
50 with this approximation is important because it has an impact on the bit allocation */
51static opus_int16 bitexact_cos(opus_int16 x)
52{
53 opus_int32 tmp;
54 opus_int16 x2;
55 tmp = (4096+((opus_int32)(x)*(x)))>>13;
56 celt_assert(tmp<=32767);
57 x2 = tmp;
58 x2 = (32767-x2) + FRAC_MUL16(x2, (-7651 + FRAC_MUL16(x2, (8277 + FRAC_MUL16(-626, x2)))));
59 celt_assert(x2<=32766);
60 return 1+x2;
61}
62
63static int bitexact_log2tan(int isin,int icos)
64{
65 int lc;
66 int ls;
67 lc=EC_ILOG(icos);
68 ls=EC_ILOG(isin);
69 icos<<=15-lc;
70 isin<<=15-ls;
71 return (ls-lc)*(1<<11)
72 +FRAC_MUL16(isin, FRAC_MUL16(isin, -2597) + 7932)
73 -FRAC_MUL16(icos, FRAC_MUL16(icos, -2597) + 7932);
74}
75
76#ifdef FIXED_POINT
77/* Compute the amplitude (sqrt energy) in each of the bands */
78void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *bandE, int end, int C, int M)
79{
80 int i, c, N;
81 const opus_int16 *eBands = m->eBands;
82 N = M*m->shortMdctSize;
83 c=0; do {
84 for (i=0;i<end;i++)
85 {
86 int j;
87 opus_val32 maxval=0;
88 opus_val32 sum = 0;
89
90 j=M*eBands[i]; do {
91 maxval = MAX32(maxval, X[j+c*N]);
92 maxval = MAX32(maxval, -X[j+c*N]);
93 } while (++j<M*eBands[i+1]);
94
95 if (maxval > 0)
96 {
97 int shift = celt_ilog2(maxval)-10;
98 j=M*eBands[i]; do {
99 sum = MAC16_16(sum, EXTRACT16(VSHR32(X[j+c*N],shift)),
100 EXTRACT16(VSHR32(X[j+c*N],shift)));
101 } while (++j<M*eBands[i+1]);
102 /* We're adding one here to ensure the normalized band isn't larger than unity norm */
103 bandE[i+c*m->nbEBands] = EPSILON+VSHR32(EXTEND32(celt_sqrt(sum)),-shift);
104 } else {
105 bandE[i+c*m->nbEBands] = EPSILON;
106 }
107 /*printf ("%f ", bandE[i+c*m->nbEBands]);*/
108 }
109 } while (++c<C);
110 /*printf ("\n");*/
111}
112
113/* Normalise each band such that the energy is one. */
114void normalise_bands(const CELTMode *m, const celt_sig * OPUS_RESTRICT freq, celt_norm * OPUS_RESTRICT X, const celt_ener *bandE, int end, int C, int M)
115{
116 int i, c, N;
117 const opus_int16 *eBands = m->eBands;
118 N = M*m->shortMdctSize;
119 c=0; do {
120 i=0; do {
121 opus_val16 g;
122 int j,shift;
123 opus_val16 E;
124 shift = celt_zlog2(bandE[i+c*m->nbEBands])-13;
125 E = VSHR32(bandE[i+c*m->nbEBands], shift);
126 g = EXTRACT16(celt_rcp(SHL32(E,3)));
127 j=M*eBands[i]; do {
128 X[j+c*N] = MULT16_16_Q15(VSHR32(freq[j+c*N],shift-1),g);
129 } while (++j<M*eBands[i+1]);
130 } while (++i<end);
131 } while (++c<C);
132}
133
134#else /* FIXED_POINT */
135/* Compute the amplitude (sqrt energy) in each of the bands */
136void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *bandE, int end, int C, int M)
137{
138 int i, c, N;
139 const opus_int16 *eBands = m->eBands;
140 N = M*m->shortMdctSize;
141 c=0; do {
142 for (i=0;i<end;i++)
143 {
144 int j;
145 opus_val32 sum = 1e-27f;
146 for (j=M*eBands[i];j<M*eBands[i+1];j++)
147 sum += X[j+c*N]*X[j+c*N];
148 bandE[i+c*m->nbEBands] = celt_sqrt(sum);
149 /*printf ("%f ", bandE[i+c*m->nbEBands]);*/
150 }
151 } while (++c<C);
152 /*printf ("\n");*/
153}
154
155/* Normalise each band such that the energy is one. */
156void normalise_bands(const CELTMode *m, const celt_sig * OPUS_RESTRICT freq, celt_norm * OPUS_RESTRICT X, const celt_ener *bandE, int end, int C, int M)
157{
158 int i, c, N;
159 const opus_int16 *eBands = m->eBands;
160 N = M*m->shortMdctSize;
161 c=0; do {
162 for (i=0;i<end;i++)
163 {
164 int j;
165 opus_val16 g = 1.f/(1e-27f+bandE[i+c*m->nbEBands]);
166 for (j=M*eBands[i];j<M*eBands[i+1];j++)
167 X[j+c*N] = freq[j+c*N]*g;
168 }
169 } while (++c<C);
170}
171
172#endif /* FIXED_POINT */
173
174/* De-normalise the energy to produce the synthesis from the unit-energy bands */
175void denormalise_bands(const CELTMode *m, const celt_norm * OPUS_RESTRICT X, celt_sig * OPUS_RESTRICT freq, const celt_ener *bandE, int end, int C, int M)
176{
177 int i, c, N;
178 const opus_int16 *eBands = m->eBands;
179 N = M*m->shortMdctSize;
180 celt_assert2(C<=2, "denormalise_bands() not implemented for >2 channels");
181 c=0; do {
182 celt_sig * OPUS_RESTRICT f;
183 const celt_norm * OPUS_RESTRICT x;
184 f = freq+c*N;
185 x = X+c*N;
186 for (i=0;i<end;i++)
187 {
188 int j, band_end;
189 opus_val32 g = SHR32(bandE[i+c*m->nbEBands],1);
190 j=M*eBands[i];
191 band_end = M*eBands[i+1];
192 do {
193 *f++ = SHL32(MULT16_32_Q15(*x, g),2);
194 x++;
195 } while (++j<band_end);
196 }
197 for (i=M*eBands[end];i<N;i++)
198 *f++ = 0;
199 } while (++c<C);
200}
201
202/* This prevents energy collapse for transients with multiple short MDCTs */
203void anti_collapse(const CELTMode *m, celt_norm *X_, unsigned char *collapse_masks, int LM, int C, int size,
204 int start, int end, opus_val16 *logE, opus_val16 *prev1logE,
205 opus_val16 *prev2logE, int *pulses, opus_uint32 seed)
206{
207 int c, i, j, k;
208 for (i=start;i<end;i++)
209 {
210 int N0;
211 opus_val16 thresh, sqrt_1;
212 int depth;
213#ifdef FIXED_POINT
214 int shift;
215 opus_val32 thresh32;
216#endif
217
218 N0 = m->eBands[i+1]-m->eBands[i];
219 /* depth in 1/8 bits */
220 depth = (1+pulses[i])/((m->eBands[i+1]-m->eBands[i])<<LM);
221
222#ifdef FIXED_POINT
223 thresh32 = SHR32(celt_exp2(-SHL16(depth, 10-BITRES)),1);
224 thresh = MULT16_32_Q15(QCONST16(0.5f, 15), MIN32(32767,thresh32));
225 {
226 opus_val32 t;
227 t = N0<<LM;
228 shift = celt_ilog2(t)>>1;
229 t = SHL32(t, (7-shift)<<1);
230 sqrt_1 = celt_rsqrt_norm(t);
231 }
232#else
233 thresh = .5f*celt_exp2(-.125f*depth);
234 sqrt_1 = celt_rsqrt(N0<<LM);
235#endif
236
237 c=0; do
238 {
239 celt_norm *X;
240 opus_val16 prev1;
241 opus_val16 prev2;
242 opus_val32 Ediff;
243 opus_val16 r;
244 int renormalize=0;
245 prev1 = prev1logE[c*m->nbEBands+i];
246 prev2 = prev2logE[c*m->nbEBands+i];
247 if (C==1)
248 {
249 prev1 = MAX16(prev1,prev1logE[m->nbEBands+i]);
250 prev2 = MAX16(prev2,prev2logE[m->nbEBands+i]);
251 }
252 Ediff = EXTEND32(logE[c*m->nbEBands+i])-EXTEND32(MIN16(prev1,prev2));
253 Ediff = MAX32(0, Ediff);
254
255#ifdef FIXED_POINT
256 if (Ediff < 16384)
257 {
258 opus_val32 r32 = SHR32(celt_exp2(-EXTRACT16(Ediff)),1);
259 r = 2*MIN16(16383,r32);
260 } else {
261 r = 0;
262 }
263 if (LM==3)
264 r = MULT16_16_Q14(23170, MIN32(23169, r));
265 r = SHR16(MIN16(thresh, r),1);
266 r = SHR32(MULT16_16_Q15(sqrt_1, r),shift);
267#else
268 /* r needs to be multiplied by 2 or 2*sqrt(2) depending on LM because
269 short blocks don't have the same energy as long */
270 r = 2.f*celt_exp2(-Ediff);
271 if (LM==3)
272 r *= 1.41421356f;
273 r = MIN16(thresh, r);
274 r = r*sqrt_1;
275#endif
276 X = X_+c*size+(m->eBands[i]<<LM);
277 for (k=0;k<1<<LM;k++)
278 {
279 /* Detect collapse */
280 if (!(collapse_masks[i*C+c]&1<<k))
281 {
282 /* Fill with noise */
283 for (j=0;j<N0;j++)
284 {
285 seed = celt_lcg_rand(seed);
286 X[(j<<LM)+k] = (seed&0x8000 ? r : -r);
287 }
288 renormalize = 1;
289 }
290 }
291 /* We just added some energy, so we need to renormalise */
292 if (renormalize)
293 renormalise_vector(X, N0<<LM, Q15ONE);
294 } while (++c<C);
295 }
296}
297
298static void intensity_stereo(const CELTMode *m, celt_norm *X, celt_norm *Y, const celt_ener *bandE, int bandID, int N)
299{
300 int i = bandID;
301 int j;
302 opus_val16 a1, a2;
303 opus_val16 left, right;
304 opus_val16 norm;
305#ifdef FIXED_POINT
306 int shift = celt_zlog2(MAX32(bandE[i], bandE[i+m->nbEBands]))-13;
307#endif
308 left = VSHR32(bandE[i],shift);
309 right = VSHR32(bandE[i+m->nbEBands],shift);
310 norm = EPSILON + celt_sqrt(EPSILON+MULT16_16(left,left)+MULT16_16(right,right));
311 a1 = DIV32_16(SHL32(EXTEND32(left),14),norm);
312 a2 = DIV32_16(SHL32(EXTEND32(right),14),norm);
313 for (j=0;j<N;j++)
314 {
315 celt_norm r, l;
316 l = X[j];
317 r = Y[j];
318 X[j] = MULT16_16_Q14(a1,l) + MULT16_16_Q14(a2,r);
319 /* Side is not encoded, no need to calculate */
320 }
321}
322
323static void stereo_split(celt_norm *X, celt_norm *Y, int N)
324{
325 int j;
326 for (j=0;j<N;j++)
327 {
328 celt_norm r, l;
329 l = MULT16_16_Q15(QCONST16(.70710678f,15), X[j]);
330 r = MULT16_16_Q15(QCONST16(.70710678f,15), Y[j]);
331 X[j] = l+r;
332 Y[j] = r-l;
333 }
334}
335
336static void stereo_merge(celt_norm *X, celt_norm *Y, opus_val16 mid, int N)
337{
338 int j;
339 opus_val32 xp=0, side=0;
340 opus_val32 El, Er;
341 opus_val16 mid2;
342#ifdef FIXED_POINT
343 int kl, kr;
344#endif
345 opus_val32 t, lgain, rgain;
346
347 /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */
348 for (j=0;j<N;j++)
349 {
350 xp = MAC16_16(xp, X[j], Y[j]);
351 side = MAC16_16(side, Y[j], Y[j]);
352 }
353 /* Compensating for the mid normalization */
354 xp = MULT16_32_Q15(mid, xp);
355 /* mid and side are in Q15, not Q14 like X and Y */
356 mid2 = SHR32(mid, 1);
357 El = MULT16_16(mid2, mid2) + side - 2*xp;
358 Er = MULT16_16(mid2, mid2) + side + 2*xp;
359 if (Er < QCONST32(6e-4f, 28) || El < QCONST32(6e-4f, 28))
360 {
361 for (j=0;j<N;j++)
362 Y[j] = X[j];
363 return;
364 }
365
366#ifdef FIXED_POINT
367 kl = celt_ilog2(El)>>1;
368 kr = celt_ilog2(Er)>>1;
369#endif
370 t = VSHR32(El, (kl-7)<<1);
371 lgain = celt_rsqrt_norm(t);
372 t = VSHR32(Er, (kr-7)<<1);
373 rgain = celt_rsqrt_norm(t);
374
375#ifdef FIXED_POINT
376 if (kl < 7)
377 kl = 7;
378 if (kr < 7)
379 kr = 7;
380#endif
381
382 for (j=0;j<N;j++)
383 {
384 celt_norm r, l;
385 /* Apply mid scaling (side is already scaled) */
386 l = MULT16_16_Q15(mid, X[j]);
387 r = Y[j];
388 X[j] = EXTRACT16(PSHR32(MULT16_16(lgain, SUB16(l,r)), kl+1));
389 Y[j] = EXTRACT16(PSHR32(MULT16_16(rgain, ADD16(l,r)), kr+1));
390 }
391}
392
393/* Decide whether we should spread the pulses in the current frame */
394int spreading_decision(const CELTMode *m, celt_norm *X, int *average,
395 int last_decision, int *hf_average, int *tapset_decision, int update_hf,
396 int end, int C, int M)
397{
398 int i, c, N0;
399 int sum = 0, nbBands=0;
400 const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
401 int decision;
402 int hf_sum=0;
403
404 celt_assert(end>0);
405
406 N0 = M*m->shortMdctSize;
407
408 if (M*(eBands[end]-eBands[end-1]) <= 8)
409 return SPREAD_NONE;
410 c=0; do {
411 for (i=0;i<end;i++)
412 {
413 int j, N, tmp=0;
414 int tcount[3] = {0,0,0};
415 celt_norm * OPUS_RESTRICT x = X+M*eBands[i]+c*N0;
416 N = M*(eBands[i+1]-eBands[i]);
417 if (N<=8)
418 continue;
419 /* Compute rough CDF of |x[j]| */
420 for (j=0;j<N;j++)
421 {
422 opus_val32 x2N; /* Q13 */
423
424 x2N = MULT16_16(MULT16_16_Q15(x[j], x[j]), N);
425 if (x2N < QCONST16(0.25f,13))
426 tcount[0]++;
427 if (x2N < QCONST16(0.0625f,13))
428 tcount[1]++;
429 if (x2N < QCONST16(0.015625f,13))
430 tcount[2]++;
431 }
432
433 /* Only include four last bands (8 kHz and up) */
434 if (i>m->nbEBands-4)
435 hf_sum += 32*(tcount[1]+tcount[0])/N;
436 tmp = (2*tcount[2] >= N) + (2*tcount[1] >= N) + (2*tcount[0] >= N);
437 sum += tmp*256;
438 nbBands++;
439 }
440 } while (++c<C);
441
442 if (update_hf)
443 {
444 if (hf_sum)
445 hf_sum /= C*(4-m->nbEBands+end);
446 *hf_average = (*hf_average+hf_sum)>>1;
447 hf_sum = *hf_average;
448 if (*tapset_decision==2)
449 hf_sum += 4;
450 else if (*tapset_decision==0)
451 hf_sum -= 4;
452 if (hf_sum > 22)
453 *tapset_decision=2;
454 else if (hf_sum > 18)
455 *tapset_decision=1;
456 else
457 *tapset_decision=0;
458 }
459 /*printf("%d %d %d\n", hf_sum, *hf_average, *tapset_decision);*/
460 celt_assert(nbBands>0); /*M*(eBands[end]-eBands[end-1]) <= 8 assures this*/
461 sum /= nbBands;
462 /* Recursive averaging */
463 sum = (sum+*average)>>1;
464 *average = sum;
465 /* Hysteresis */
466 sum = (3*sum + (((3-last_decision)<<7) + 64) + 2)>>2;
467 if (sum < 80)
468 {
469 decision = SPREAD_AGGRESSIVE;
470 } else if (sum < 256)
471 {
472 decision = SPREAD_NORMAL;
473 } else if (sum < 384)
474 {
475 decision = SPREAD_LIGHT;
476 } else {
477 decision = SPREAD_NONE;
478 }
479#ifdef FUZZING
480 decision = rand()&0x3;
481 *tapset_decision=rand()%3;
482#endif
483 return decision;
484}
485
486#ifdef MEASURE_NORM_MSE
487
488float MSE[30] = {0};
489int nbMSEBands = 0;
490int MSECount[30] = {0};
491
492void dump_norm_mse(void)
493{
494 int i;
495 for (i=0;i<nbMSEBands;i++)
496 {
497 printf ("%g ", MSE[i]/MSECount[i]);
498 }
499 printf ("\n");
500}
501
502void measure_norm_mse(const CELTMode *m, float *X, float *X0, float *bandE, float *bandE0, int M, int N, int C)
503{
504 static int init = 0;
505 int i;
506 if (!init)
507 {
508 atexit(dump_norm_mse);
509 init = 1;
510 }
511 for (i=0;i<m->nbEBands;i++)
512 {
513 int j;
514 int c;
515 float g;
516 if (bandE0[i]<10 || (C==2 && bandE0[i+m->nbEBands]<1))
517 continue;
518 c=0; do {
519 g = bandE[i+c*m->nbEBands]/(1e-15+bandE0[i+c*m->nbEBands]);
520 for (j=M*m->eBands[i];j<M*m->eBands[i+1];j++)
521 MSE[i] += (g*X[j+c*N]-X0[j+c*N])*(g*X[j+c*N]-X0[j+c*N]);
522 } while (++c<C);
523 MSECount[i]+=C;
524 }
525 nbMSEBands = m->nbEBands;
526}
527
528#endif
529
530/* Indexing table for converting from natural Hadamard to ordery Hadamard
531 This is essentially a bit-reversed Gray, on top of which we've added
532 an inversion of the order because we want the DC at the end rather than
533 the beginning. The lines are for N=2, 4, 8, 16 */
534static const int ordery_table[] = {
535 1, 0,
536 3, 0, 2, 1,
537 7, 0, 4, 3, 6, 1, 5, 2,
538 15, 0, 8, 7, 12, 3, 11, 4, 14, 1, 9, 6, 13, 2, 10, 5,
539};
540
541static void deinterleave_hadamard(celt_norm *X, int N0, int stride, int hadamard)
542{
543 int i,j;
544 VARDECL(celt_norm, tmp);
545 int N;
546 SAVE_STACK;
547 N = N0*stride;
548 ALLOC(tmp, N, celt_norm);
549 celt_assert(stride>0);
550 if (hadamard)
551 {
552 const int *ordery = ordery_table+stride-2;
553 for (i=0;i<stride;i++)
554 {
555 for (j=0;j<N0;j++)
556 tmp[ordery[i]*N0+j] = X[j*stride+i];
557 }
558 } else {
559 for (i=0;i<stride;i++)
560 for (j=0;j<N0;j++)
561 tmp[i*N0+j] = X[j*stride+i];
562 }
563 for (j=0;j<N;j++)
564 X[j] = tmp[j];
565 RESTORE_STACK;
566}
567
568static void interleave_hadamard(celt_norm *X, int N0, int stride, int hadamard)
569{
570 int i,j;
571 VARDECL(celt_norm, tmp);
572 int N;
573 SAVE_STACK;
574 N = N0*stride;
575 ALLOC(tmp, N, celt_norm);
576 if (hadamard)
577 {
578 const int *ordery = ordery_table+stride-2;
579 for (i=0;i<stride;i++)
580 for (j=0;j<N0;j++)
581 tmp[j*stride+i] = X[ordery[i]*N0+j];
582 } else {
583 for (i=0;i<stride;i++)
584 for (j=0;j<N0;j++)
585 tmp[j*stride+i] = X[i*N0+j];
586 }
587 for (j=0;j<N;j++)
588 X[j] = tmp[j];
589 RESTORE_STACK;
590}
591
592void haar1(celt_norm *X, int N0, int stride)
593{
594 int i, j;
595 N0 >>= 1;
596 for (i=0;i<stride;i++)
597 for (j=0;j<N0;j++)
598 {
599 celt_norm tmp1, tmp2;
600 tmp1 = MULT16_16_Q15(QCONST16(.70710678f,15), X[stride*2*j+i]);
601 tmp2 = MULT16_16_Q15(QCONST16(.70710678f,15), X[stride*(2*j+1)+i]);
602 X[stride*2*j+i] = tmp1 + tmp2;
603 X[stride*(2*j+1)+i] = tmp1 - tmp2;
604 }
605}
606
607static int compute_qn(int N, int b, int offset, int pulse_cap, int stereo)
608{
609 static const opus_int16 exp2_table8[8] =
610 {16384, 17866, 19483, 21247, 23170, 25267, 27554, 30048};
611 int qn, qb;
612 int N2 = 2*N-1;
613 if (stereo && N==2)
614 N2--;
615 /* The upper limit ensures that in a stereo split with itheta==16384, we'll
616 always have enough bits left over to code at least one pulse in the
617 side; otherwise it would collapse, since it doesn't get folded. */
618 qb = IMIN(b-pulse_cap-(4<<BITRES), (b+N2*offset)/N2);
619
620 qb = IMIN(8<<BITRES, qb);
621
622 if (qb<(1<<BITRES>>1)) {
623 qn = 1;
624 } else {
625 qn = exp2_table8[qb&0x7]>>(14-(qb>>BITRES));
626 qn = (qn+1)>>1<<1;
627 }
628 celt_assert(qn <= 256);
629 return qn;
630}
631
632/* This function is responsible for encoding and decoding a band for both
633 the mono and stereo case. Even in the mono case, it can split the band
634 in two and transmit the energy difference with the two half-bands. It
635 can be called recursively so bands can end up being split in 8 parts. */
636static unsigned quant_band(int encode, const CELTMode *m, int i, celt_norm *X, celt_norm *Y,
637 int N, int b, int spread, int B, int intensity, int tf_change, celt_norm *lowband, ec_ctx *ec,
638 opus_int32 *remaining_bits, int LM, celt_norm *lowband_out, const celt_ener *bandE, int level,
639 opus_uint32 *seed, opus_val16 gain, celt_norm *lowband_scratch, int fill)
640{
641 const unsigned char *cache;
642 int q;
643 int curr_bits;
644 int stereo, split;
645 int imid=0, iside=0;
646 int N0=N;
647 int N_B=N;
648 int N_B0;
649 int B0=B;
650 int time_divide=0;
651 int recombine=0;
652 int inv = 0;
653 opus_val16 mid=0, side=0;
654 int longBlocks;
655 unsigned cm=0;
656#ifdef RESYNTH
657 int resynth = 1;
658#else
659 int resynth = !encode;
660#endif
661
662 longBlocks = B0==1;
663
664 N_B /= B;
665 N_B0 = N_B;
666
667 split = stereo = Y != NULL;
668
669 /* Special case for one sample */
670 if (N==1)
671 {
672 int c;
673 celt_norm *x = X;
674 c=0; do {
675 int sign=0;
676 if (*remaining_bits>=1<<BITRES)
677 {
678 if (encode)
679 {
680 sign = x[0]<0;
681 ec_enc_bits(ec, sign, 1);
682 } else {
683 sign = ec_dec_bits(ec, 1);
684 }
685 *remaining_bits -= 1<<BITRES;
686 b-=1<<BITRES;
687 }
688 if (resynth)
689 x[0] = sign ? -NORM_SCALING : NORM_SCALING;
690 x = Y;
691 } while (++c<1+stereo);
692 if (lowband_out)
693 lowband_out[0] = SHR16(X[0],4);
694 return 1;
695 }
696
697 if (!stereo && level == 0)
698 {
699 int k;
700 if (tf_change>0)
701 recombine = tf_change;
702 /* Band recombining to increase frequency resolution */
703
704 if (lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1))
705 {
706 int j;
707 for (j=0;j<N;j++)
708 lowband_scratch[j] = lowband[j];
709 lowband = lowband_scratch;
710 }
711
712 for (k=0;k<recombine;k++)
713 {
714 static const unsigned char bit_interleave_table[16]={
715 0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3
716 };
717 if (encode)
718 haar1(X, N>>k, 1<<k);
719 if (lowband)
720 haar1(lowband, N>>k, 1<<k);
721 fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2;
722 }
723 B>>=recombine;
724 N_B<<=recombine;
725
726 /* Increasing the time resolution */
727 while ((N_B&1) == 0 && tf_change<0)
728 {
729 if (encode)
730 haar1(X, N_B, B);
731 if (lowband)
732 haar1(lowband, N_B, B);
733 fill |= fill<<B;
734 B <<= 1;
735 N_B >>= 1;
736 time_divide++;
737 tf_change++;
738 }
739 B0=B;
740 N_B0 = N_B;
741
742 /* Reorganize the samples in time order instead of frequency order */
743 if (B0>1)
744 {
745 if (encode)
746 deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
747 if (lowband)
748 deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks);
749 }
750 }
751
752 /* If we need 1.5 more bit than we can produce, split the band in two. */
753 cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i];
754 if (!stereo && LM != -1 && b > cache[cache[0]]+12 && N>2)
755 {
756 N >>= 1;
757 Y = X+N;
758 split = 1;
759 LM -= 1;
760 if (B==1)
761 fill = (fill&1)|(fill<<1);
762 B = (B+1)>>1;
763 }
764
765 if (split)
766 {
767 int qn;
768 int itheta=0;
769 int mbits, sbits, delta;
770 int qalloc;
771 int pulse_cap;
772 int offset;
773 int orig_fill;
774 opus_int32 tell;
775
776 /* Decide on the resolution to give to the split parameter theta */
777 pulse_cap = m->logN[i]+LM*(1<<BITRES);
778 offset = (pulse_cap>>1) - (stereo&&N==2 ? QTHETA_OFFSET_TWOPHASE : QTHETA_OFFSET);
779 qn = compute_qn(N, b, offset, pulse_cap, stereo);
780 if (stereo && i>=intensity)
781 qn = 1;
782 if (encode)
783 {
784 /* theta is the atan() of the ratio between the (normalized)
785 side and mid. With just that parameter, we can re-scale both
786 mid and side because we know that 1) they have unit norm and
787 2) they are orthogonal. */
788 itheta = stereo_itheta(X, Y, stereo, N);
789 }
790 tell = ec_tell_frac(ec);
791 if (qn!=1)
792 {
793 if (encode)
794 itheta = (itheta*qn+8192)>>14;
795
796 /* Entropy coding of the angle. We use a uniform pdf for the
797 time split, a step for stereo, and a triangular one for the rest. */
798 if (stereo && N>2)
799 {
800 int p0 = 3;
801 int x = itheta;
802 int x0 = qn/2;
803 int ft = p0*(x0+1) + x0;
804 /* Use a probability of p0 up to itheta=8192 and then use 1 after */
805 if (encode)
806 {
807 ec_encode(ec,x<=x0?p0*x:(x-1-x0)+(x0+1)*p0,x<=x0?p0*(x+1):(x-x0)+(x0+1)*p0,ft);
808 } else {
809 int fs;
810 fs=ec_decode(ec,ft);
811 if (fs<(x0+1)*p0)
812 x=fs/p0;
813 else
814 x=x0+1+(fs-(x0+1)*p0);
815 ec_dec_update(ec,x<=x0?p0*x:(x-1-x0)+(x0+1)*p0,x<=x0?p0*(x+1):(x-x0)+(x0+1)*p0,ft);
816 itheta = x;
817 }
818 } else if (B0>1 || stereo) {
819 /* Uniform pdf */
820 if (encode)
821 ec_enc_uint(ec, itheta, qn+1);
822 else
823 itheta = ec_dec_uint(ec, qn+1);
824 } else {
825 int fs=1, ft;
826 ft = ((qn>>1)+1)*((qn>>1)+1);
827 if (encode)
828 {
829 int fl;
830
831 fs = itheta <= (qn>>1) ? itheta + 1 : qn + 1 - itheta;
832 fl = itheta <= (qn>>1) ? itheta*(itheta + 1)>>1 :
833 ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
834
835 ec_encode(ec, fl, fl+fs, ft);
836 } else {
837 /* Triangular pdf */
838 int fl=0;
839 int fm;
840 fm = ec_decode(ec, ft);
841
842 if (fm < ((qn>>1)*((qn>>1) + 1)>>1))
843 {
844 itheta = (isqrt32(8*(opus_uint32)fm + 1) - 1)>>1;
845 fs = itheta + 1;
846 fl = itheta*(itheta + 1)>>1;
847 }
848 else
849 {
850 itheta = (2*(qn + 1)
851 - isqrt32(8*(opus_uint32)(ft - fm - 1) + 1))>>1;
852 fs = qn + 1 - itheta;
853 fl = ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
854 }
855
856 ec_dec_update(ec, fl, fl+fs, ft);
857 }
858 }
859 itheta = (opus_int32)itheta*16384/qn;
860 if (encode && stereo)
861 {
862 if (itheta==0)
863 intensity_stereo(m, X, Y, bandE, i, N);
864 else
865 stereo_split(X, Y, N);
866 }
867 /* NOTE: Renormalising X and Y *may* help fixed-point a bit at very high rate.
868 Let's do that at higher complexity */
869 } else if (stereo) {
870 if (encode)
871 {
872 inv = itheta > 8192;
873 if (inv)
874 {
875 int j;
876 for (j=0;j<N;j++)
877 Y[j] = -Y[j];
878 }
879 intensity_stereo(m, X, Y, bandE, i, N);
880 }
881 if (b>2<<BITRES && *remaining_bits > 2<<BITRES)
882 {
883 if (encode)
884 ec_enc_bit_logp(ec, inv, 2);
885 else
886 inv = ec_dec_bit_logp(ec, 2);
887 } else
888 inv = 0;
889 itheta = 0;
890 }
891 qalloc = ec_tell_frac(ec) - tell;
892 b -= qalloc;
893
894 orig_fill = fill;
895 if (itheta == 0)
896 {
897 imid = 32767;
898 iside = 0;
899 fill &= (1<<B)-1;
900 delta = -16384;
901 } else if (itheta == 16384)
902 {
903 imid = 0;
904 iside = 32767;
905 fill &= ((1<<B)-1)<<B;
906 delta = 16384;
907 } else {
908 imid = bitexact_cos(itheta);
909 iside = bitexact_cos(16384-itheta);
910 /* This is the mid vs side allocation that minimizes squared error
911 in that band. */
912 delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
913 }
914
915#ifdef FIXED_POINT
916 mid = imid;
917 side = iside;
918#else
919 mid = (1.f/32768)*imid;
920 side = (1.f/32768)*iside;
921#endif
922
923 /* This is a special case for N=2 that only works for stereo and takes
924 advantage of the fact that mid and side are orthogonal to encode
925 the side with just one bit. */
926 if (N==2 && stereo)
927 {
928 int c;
929 int sign=0;
930 celt_norm *x2, *y2;
931 mbits = b;
932 sbits = 0;
933 /* Only need one bit for the side */
934 if (itheta != 0 && itheta != 16384)
935 sbits = 1<<BITRES;
936 mbits -= sbits;
937 c = itheta > 8192;
938 *remaining_bits -= qalloc+sbits;
939
940 x2 = c ? Y : X;
941 y2 = c ? X : Y;
942 if (sbits)
943 {
944 if (encode)
945 {
946 /* Here we only need to encode a sign for the side */
947 sign = x2[0]*y2[1] - x2[1]*y2[0] < 0;
948 ec_enc_bits(ec, sign, 1);
949 } else {
950 sign = ec_dec_bits(ec, 1);
951 }
952 }
953 sign = 1-2*sign;
954 /* We use orig_fill here because we want to fold the side, but if
955 itheta==16384, we'll have cleared the low bits of fill. */
956 cm = quant_band(encode, m, i, x2, NULL, N, mbits, spread, B, intensity, tf_change, lowband, ec, remaining_bits, LM, lowband_out, NULL, level, seed, gain, lowband_scratch, orig_fill);
957 /* We don't split N=2 bands, so cm is either 1 or 0 (for a fold-collapse),
958 and there's no need to worry about mixing with the other channel. */
959 y2[0] = -sign*x2[1];
960 y2[1] = sign*x2[0];
961 if (resynth)
962 {
963 celt_norm tmp;
964 X[0] = MULT16_16_Q15(mid, X[0]);
965 X[1] = MULT16_16_Q15(mid, X[1]);
966 Y[0] = MULT16_16_Q15(side, Y[0]);
967 Y[1] = MULT16_16_Q15(side, Y[1]);
968 tmp = X[0];
969 X[0] = SUB16(tmp,Y[0]);
970 Y[0] = ADD16(tmp,Y[0]);
971 tmp = X[1];
972 X[1] = SUB16(tmp,Y[1]);
973 Y[1] = ADD16(tmp,Y[1]);
974 }
975 } else {
976 /* "Normal" split code */
977 celt_norm *next_lowband2=NULL;
978 celt_norm *next_lowband_out1=NULL;
979 int next_level=0;
980 opus_int32 rebalance;
981
982 /* Give more bits to low-energy MDCTs than they would otherwise deserve */
983 if (B0>1 && !stereo && (itheta&0x3fff))
984 {
985 if (itheta > 8192)
986 /* Rough approximation for pre-echo masking */
987 delta -= delta>>(4-LM);
988 else
989 /* Corresponds to a forward-masking slope of 1.5 dB per 10 ms */
990 delta = IMIN(0, delta + (N<<BITRES>>(5-LM)));
991 }
992 mbits = IMAX(0, IMIN(b, (b-delta)/2));
993 sbits = b-mbits;
994 *remaining_bits -= qalloc;
995
996 if (lowband && !stereo)
997 next_lowband2 = lowband+N; /* >32-bit split case */
998
999 /* Only stereo needs to pass on lowband_out. Otherwise, it's
1000 handled at the end */
1001 if (stereo)
1002 next_lowband_out1 = lowband_out;
1003 else
1004 next_level = level+1;
1005
1006 rebalance = *remaining_bits;
1007 if (mbits >= sbits)
1008 {
1009 /* In stereo mode, we do not apply a scaling to the mid because we need the normalized
1010 mid for folding later */
1011 cm = quant_band(encode, m, i, X, NULL, N, mbits, spread, B, intensity, tf_change,
1012 lowband, ec, remaining_bits, LM, next_lowband_out1,
1013 NULL, next_level, seed, stereo ? Q15ONE : MULT16_16_P15(gain,mid), lowband_scratch, fill);
1014 rebalance = mbits - (rebalance-*remaining_bits);
1015 if (rebalance > 3<<BITRES && itheta!=0)
1016 sbits += rebalance - (3<<BITRES);
1017
1018 /* For a stereo split, the high bits of fill are always zero, so no
1019 folding will be done to the side. */
1020 cm |= quant_band(encode, m, i, Y, NULL, N, sbits, spread, B, intensity, tf_change,
1021 next_lowband2, ec, remaining_bits, LM, NULL,
1022 NULL, next_level, seed, MULT16_16_P15(gain,side), NULL, fill>>B)<<((B0>>1)&(stereo-1));
1023 } else {
1024 /* For a stereo split, the high bits of fill are always zero, so no
1025 folding will be done to the side. */
1026 cm = quant_band(encode, m, i, Y, NULL, N, sbits, spread, B, intensity, tf_change,
1027 next_lowband2, ec, remaining_bits, LM, NULL,
1028 NULL, next_level, seed, MULT16_16_P15(gain,side), NULL, fill>>B)<<((B0>>1)&(stereo-1));
1029 rebalance = sbits - (rebalance-*remaining_bits);
1030 if (rebalance > 3<<BITRES && itheta!=16384)
1031 mbits += rebalance - (3<<BITRES);
1032 /* In stereo mode, we do not apply a scaling to the mid because we need the normalized
1033 mid for folding later */
1034 cm |= quant_band(encode, m, i, X, NULL, N, mbits, spread, B, intensity, tf_change,
1035 lowband, ec, remaining_bits, LM, next_lowband_out1,
1036 NULL, next_level, seed, stereo ? Q15ONE : MULT16_16_P15(gain,mid), lowband_scratch, fill);
1037 }
1038 }
1039
1040 } else {
1041 /* This is the basic no-split case */
1042 q = bits2pulses(m, i, LM, b);
1043 curr_bits = pulses2bits(m, i, LM, q);
1044 *remaining_bits -= curr_bits;
1045
1046 /* Ensures we can never bust the budget */
1047 while (*remaining_bits < 0 && q > 0)
1048 {
1049 *remaining_bits += curr_bits;
1050 q--;
1051 curr_bits = pulses2bits(m, i, LM, q);
1052 *remaining_bits -= curr_bits;
1053 }
1054
1055 if (q!=0)
1056 {
1057 int K = get_pulses(q);
1058
1059 /* Finally do the actual quantization */
1060 if (encode)
1061 {
1062 cm = alg_quant(X, N, K, spread, B, ec
1063#ifdef RESYNTH
1064 , gain
1065#endif
1066 );
1067 } else {
1068 cm = alg_unquant(X, N, K, spread, B, ec, gain);
1069 }
1070 } else {
1071 /* If there's no pulse, fill the band anyway */
1072 int j;
1073 if (resynth)
1074 {
1075 unsigned cm_mask;
1076 /*B can be as large as 16, so this shift might overflow an int on a
1077 16-bit platform; use a long to get defined behavior.*/
1078 cm_mask = (unsigned)(1UL<<B)-1;
1079 fill &= cm_mask;
1080 if (!fill)
1081 {
1082 for (j=0;j<N;j++)
1083 X[j] = 0;
1084 } else {
1085 if (lowband == NULL)
1086 {
1087 /* Noise */
1088 for (j=0;j<N;j++)
1089 {
1090 *seed = celt_lcg_rand(*seed);
1091 X[j] = (celt_norm)((opus_int32)*seed>>20);
1092 }
1093 cm = cm_mask;
1094 } else {
1095 /* Folded spectrum */
1096 for (j=0;j<N;j++)
1097 {
1098 opus_val16 tmp;
1099 *seed = celt_lcg_rand(*seed);
1100 /* About 48 dB below the "normal" folding level */
1101 tmp = QCONST16(1.0f/256, 10);
1102 tmp = (*seed)&0x8000 ? tmp : -tmp;
1103 X[j] = lowband[j]+tmp;
1104 }
1105 cm = fill;
1106 }
1107 renormalise_vector(X, N, gain);
1108 }
1109 }
1110 }
1111 }
1112
1113 /* This code is used by the decoder and by the resynthesis-enabled encoder */
1114 if (resynth)
1115 {
1116 if (stereo)
1117 {
1118 if (N!=2)
1119 stereo_merge(X, Y, mid, N);
1120 if (inv)
1121 {
1122 int j;
1123 for (j=0;j<N;j++)
1124 Y[j] = -Y[j];
1125 }
1126 } else if (level == 0)
1127 {
1128 int k;
1129
1130 /* Undo the sample reorganization going from time order to frequency order */
1131 if (B0>1)
1132 interleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1133
1134 /* Undo time-freq changes that we did earlier */
1135 N_B = N_B0;
1136 B = B0;
1137 for (k=0;k<time_divide;k++)
1138 {
1139 B >>= 1;
1140 N_B <<= 1;
1141 cm |= cm>>B;
1142 haar1(X, N_B, B);
1143 }
1144
1145 for (k=0;k<recombine;k++)
1146 {
1147 static const unsigned char bit_deinterleave_table[16]={
1148 0x00,0x03,0x0C,0x0F,0x30,0x33,0x3C,0x3F,
1149 0xC0,0xC3,0xCC,0xCF,0xF0,0xF3,0xFC,0xFF
1150 };
1151 cm = bit_deinterleave_table[cm];
1152 haar1(X, N0>>k, 1<<k);
1153 }
1154 B<<=recombine;
1155
1156 /* Scale output for later folding */
1157 if (lowband_out)
1158 {
1159 int j;
1160 opus_val16 n;
1161 n = celt_sqrt(SHL32(EXTEND32(N0),22));
1162 for (j=0;j<N0;j++)
1163 lowband_out[j] = MULT16_16_Q15(n,X[j]);
1164 }
1165 cm &= (1<<B)-1;
1166 }
1167 }
1168 return cm;
1169}
1170
1171void quant_all_bands(int encode, const CELTMode *m, int start, int end,
1172 celt_norm *X_, celt_norm *Y_, unsigned char *collapse_masks, const celt_ener *bandE, int *pulses,
1173 int shortBlocks, int spread, int dual_stereo, int intensity, int *tf_res,
1174 opus_int32 total_bits, opus_int32 balance, ec_ctx *ec, int LM, int codedBands, opus_uint32 *seed)
1175{
1176 int i;
1177 opus_int32 remaining_bits;
1178 const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
1179 celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2;
1180 VARDECL(celt_norm, _norm);
1181 VARDECL(celt_norm, lowband_scratch);
1182 int B;
1183 int M;
1184 int lowband_offset;
1185 int update_lowband = 1;
1186 int C = Y_ != NULL ? 2 : 1;
1187#ifdef RESYNTH
1188 int resynth = 1;
1189#else
1190 int resynth = !encode;
1191#endif
1192 SAVE_STACK;
1193
1194 M = 1<<LM;
1195 B = shortBlocks ? M : 1;
1196 ALLOC(_norm, C*M*eBands[m->nbEBands], celt_norm);
1197 ALLOC(lowband_scratch, M*(eBands[m->nbEBands]-eBands[m->nbEBands-1]), celt_norm);
1198 norm = _norm;
1199 norm2 = norm + M*eBands[m->nbEBands];
1200
1201 lowband_offset = 0;
1202 for (i=start;i<end;i++)
1203 {
1204 opus_int32 tell;
1205 int b;
1206 int N;
1207 opus_int32 curr_balance;
1208 int effective_lowband=-1;
1209 celt_norm * OPUS_RESTRICT X, * OPUS_RESTRICT Y;
1210 int tf_change=0;
1211 unsigned x_cm;
1212 unsigned y_cm;
1213
1214 X = X_+M*eBands[i];
1215 if (Y_!=NULL)
1216 Y = Y_+M*eBands[i];
1217 else
1218 Y = NULL;
1219 N = M*eBands[i+1]-M*eBands[i];
1220 tell = ec_tell_frac(ec);
1221
1222 /* Compute how many bits we want to allocate to this band */
1223 if (i != start)
1224 balance -= tell;
1225 remaining_bits = total_bits-tell-1;
1226 if (i <= codedBands-1)
1227 {
1228 curr_balance = balance / IMIN(3, codedBands-i);
1229 b = IMAX(0, IMIN(16383, IMIN(remaining_bits+1,pulses[i]+curr_balance)));
1230 } else {
1231 b = 0;
1232 }
1233
1234 if (resynth && M*eBands[i]-N >= M*eBands[start] && (update_lowband || lowband_offset==0))
1235 lowband_offset = i;
1236
1237 tf_change = tf_res[i];
1238 if (i>=m->effEBands)
1239 {
1240 X=norm;
1241 if (Y_!=NULL)
1242 Y = norm;
1243 }
1244
1245 /* Get a conservative estimate of the collapse_mask's for the bands we're
1246 going to be folding from. */
1247 if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change<0))
1248 {
1249 int fold_start;
1250 int fold_end;
1251 int fold_i;
1252 /* This ensures we never repeat spectral content within one band */
1253 effective_lowband = IMAX(M*eBands[start], M*eBands[lowband_offset]-N);
1254 fold_start = lowband_offset;
1255 while(M*eBands[--fold_start] > effective_lowband);
1256 fold_end = lowband_offset-1;
1257 while(M*eBands[++fold_end] < effective_lowband+N);
1258 x_cm = y_cm = 0;
1259 fold_i = fold_start; do {
1260 x_cm |= collapse_masks[fold_i*C+0];
1261 y_cm |= collapse_masks[fold_i*C+C-1];
1262 } while (++fold_i<fold_end);
1263 }
1264 /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost
1265 always) be non-zero.*/
1266 else
1267 x_cm = y_cm = (1<<B)-1;
1268
1269 if (dual_stereo && i==intensity)
1270 {
1271 int j;
1272
1273 /* Switch off dual stereo to do intensity */
1274 dual_stereo = 0;
1275 if (resynth)
1276 for (j=M*eBands[start];j<M*eBands[i];j++)
1277 norm[j] = HALF32(norm[j]+norm2[j]);
1278 }
1279 if (dual_stereo)
1280 {
1281 x_cm = quant_band(encode, m, i, X, NULL, N, b/2, spread, B, intensity, tf_change,
1282 effective_lowband != -1 ? norm+effective_lowband : NULL, ec, &remaining_bits, LM,
1283 norm+M*eBands[i], bandE, 0, seed, Q15ONE, lowband_scratch, x_cm);
1284 y_cm = quant_band(encode, m, i, Y, NULL, N, b/2, spread, B, intensity, tf_change,
1285 effective_lowband != -1 ? norm2+effective_lowband : NULL, ec, &remaining_bits, LM,
1286 norm2+M*eBands[i], bandE, 0, seed, Q15ONE, lowband_scratch, y_cm);
1287 } else {
1288 x_cm = quant_band(encode, m, i, X, Y, N, b, spread, B, intensity, tf_change,
1289 effective_lowband != -1 ? norm+effective_lowband : NULL, ec, &remaining_bits, LM,
1290 norm+M*eBands[i], bandE, 0, seed, Q15ONE, lowband_scratch, x_cm|y_cm);
1291 y_cm = x_cm;
1292 }
1293 collapse_masks[i*C+0] = (unsigned char)x_cm;
1294 collapse_masks[i*C+C-1] = (unsigned char)y_cm;
1295 balance += pulses[i] + tell;
1296
1297 /* Update the folding position only as long as we have 1 bit/sample depth */
1298 update_lowband = b>(N<<BITRES);
1299 }
1300 RESTORE_STACK;
1301}
1302
diff --git a/lib/rbcodec/codecs/libopus/celt/bands.h b/lib/rbcodec/codecs/libopus/celt/bands.h
new file mode 100644
index 0000000000..9ff8ffd7ba
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/bands.h
@@ -0,0 +1,95 @@
1/* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2009 Xiph.Org Foundation
3 Copyright (c) 2008-2009 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#ifndef BANDS_H
31#define BANDS_H
32
33#include "arch.h"
34#include "modes.h"
35#include "entenc.h"
36#include "entdec.h"
37#include "rate.h"
38
39/** Compute the amplitude (sqrt energy) in each of the bands
40 * @param m Mode data
41 * @param X Spectrum
42 * @param bands Square root of the energy for each band (returned)
43 */
44void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *bandE, int end, int C, int M);
45
46/*void compute_noise_energies(const CELTMode *m, const celt_sig *X, const opus_val16 *tonality, celt_ener *bandE);*/
47
48/** Normalise each band of X such that the energy in each band is
49 equal to 1
50 * @param m Mode data
51 * @param X Spectrum (returned normalised)
52 * @param bands Square root of the energy for each band
53 */
54void normalise_bands(const CELTMode *m, const celt_sig * OPUS_RESTRICT freq, celt_norm * OPUS_RESTRICT X, const celt_ener *bandE, int end, int C, int M);
55
56/** Denormalise each band of X to restore full amplitude
57 * @param m Mode data
58 * @param X Spectrum (returned de-normalised)
59 * @param bands Square root of the energy for each band
60 */
61void denormalise_bands(const CELTMode *m, const celt_norm * OPUS_RESTRICT X, celt_sig * OPUS_RESTRICT freq, const celt_ener *bandE, int end, int C, int M);
62
63#define SPREAD_NONE (0)
64#define SPREAD_LIGHT (1)
65#define SPREAD_NORMAL (2)
66#define SPREAD_AGGRESSIVE (3)
67
68int spreading_decision(const CELTMode *m, celt_norm *X, int *average,
69 int last_decision, int *hf_average, int *tapset_decision, int update_hf,
70 int end, int C, int M);
71
72#ifdef MEASURE_NORM_MSE
73void measure_norm_mse(const CELTMode *m, float *X, float *X0, float *bandE, float *bandE0, int M, int N, int C);
74#endif
75
76void haar1(celt_norm *X, int N0, int stride);
77
78/** Quantisation/encoding of the residual spectrum
79 * @param m Mode data
80 * @param X Residual (normalised)
81 * @param total_bits Total number of bits that can be used for the frame (including the ones already spent)
82 * @param enc Entropy encoder
83 */
84void quant_all_bands(int encode, const CELTMode *m, int start, int end,
85 celt_norm * X, celt_norm * Y, unsigned char *collapse_masks, const celt_ener *bandE, int *pulses,
86 int time_domain, int fold, int dual_stereo, int intensity, int *tf_res,
87 opus_int32 total_bits, opus_int32 balance, ec_ctx *ec, int M, int codedBands, opus_uint32 *seed);
88
89void anti_collapse(const CELTMode *m, celt_norm *X_, unsigned char *collapse_masks, int LM, int C, int size,
90 int start, int end, opus_val16 *logE, opus_val16 *prev1logE,
91 opus_val16 *prev2logE, int *pulses, opus_uint32 seed);
92
93opus_uint32 celt_lcg_rand(opus_uint32 seed);
94
95#endif /* BANDS_H */
diff --git a/lib/rbcodec/codecs/libopus/celt/celt.c b/lib/rbcodec/codecs/libopus/celt/celt.c
new file mode 100644
index 0000000000..8d42cc95b3
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/celt.c
@@ -0,0 +1,2870 @@
1/* 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 "opus_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 OPUS_VERSION
54#define OPUS_VERSION "unknown"
55#endif
56
57#ifdef CUSTOM_MODES
58#define OPUS_CUSTOM_NOSTATIC
59#else
60#define OPUS_CUSTOM_NOSTATIC static inline
61#endif
62
63static const unsigned char trim_icdf[11] = {126, 124, 119, 109, 87, 41, 19, 9, 4, 2, 0};
64/* Probs: NONE: 21.875%, LIGHT: 6.25%, NORMAL: 65.625%, AGGRESSIVE: 6.25% */
65static const unsigned char spread_icdf[4] = {25, 23, 2, 0};
66
67static const unsigned char tapset_icdf[3]={2,1,0};
68
69#ifdef CUSTOM_MODES
70static const unsigned char toOpusTable[20] = {
71 0xE0, 0xE8, 0xF0, 0xF8,
72 0xC0, 0xC8, 0xD0, 0xD8,
73 0xA0, 0xA8, 0xB0, 0xB8,
74 0x00, 0x00, 0x00, 0x00,
75 0x80, 0x88, 0x90, 0x98,
76};
77
78static const unsigned char fromOpusTable[16] = {
79 0x80, 0x88, 0x90, 0x98,
80 0x40, 0x48, 0x50, 0x58,
81 0x20, 0x28, 0x30, 0x38,
82 0x00, 0x08, 0x10, 0x18
83};
84
85static inline int toOpus(unsigned char c)
86{
87 int ret=0;
88 if (c<0xA0)
89 ret = toOpusTable[c>>3];
90 if (ret == 0)
91 return -1;
92 else
93 return ret|(c&0x7);
94}
95
96static inline int fromOpus(unsigned char c)
97{
98 if (c<0x80)
99 return -1;
100 else
101 return fromOpusTable[(c>>3)-16] | (c&0x7);
102}
103#endif /* CUSTOM_MODES */
104
105#define COMBFILTER_MAXPERIOD 1024
106#define COMBFILTER_MINPERIOD 15
107
108static int resampling_factor(opus_int32 rate)
109{
110 int ret;
111 switch (rate)
112 {
113 case 48000:
114 ret = 1;
115 break;
116 case 24000:
117 ret = 2;
118 break;
119 case 16000:
120 ret = 3;
121 break;
122 case 12000:
123 ret = 4;
124 break;
125 case 8000:
126 ret = 6;
127 break;
128 default:
129#ifndef CUSTOM_MODES
130 celt_assert(0);
131#endif
132 ret = 0;
133 break;
134 }
135 return ret;
136}
137
138/** Encoder state
139 @brief Encoder state
140 */
141struct OpusCustomEncoder {
142 const OpusCustomMode *mode; /**< Mode used by the encoder */
143 int overlap;
144 int channels;
145 int stream_channels;
146
147 int force_intra;
148 int clip;
149 int disable_pf;
150 int complexity;
151 int upsample;
152 int start, end;
153
154 opus_int32 bitrate;
155 int vbr;
156 int signalling;
157 int constrained_vbr; /* If zero, VBR can do whatever it likes with the rate */
158 int loss_rate;
159 int lsb_depth;
160
161 /* Everything beyond this point gets cleared on a reset */
162#define ENCODER_RESET_START rng
163
164 opus_uint32 rng;
165 int spread_decision;
166 opus_val32 delayedIntra;
167 int tonal_average;
168 int lastCodedBands;
169 int hf_average;
170 int tapset_decision;
171
172 int prefilter_period;
173 opus_val16 prefilter_gain;
174 int prefilter_tapset;
175#ifdef RESYNTH
176 int prefilter_period_old;
177 opus_val16 prefilter_gain_old;
178 int prefilter_tapset_old;
179#endif
180 int consec_transient;
181
182 opus_val32 preemph_memE[2];
183 opus_val32 preemph_memD[2];
184
185 /* VBR-related parameters */
186 opus_int32 vbr_reservoir;
187 opus_int32 vbr_drift;
188 opus_int32 vbr_offset;
189 opus_int32 vbr_count;
190
191#ifdef RESYNTH
192 celt_sig syn_mem[2][2*MAX_PERIOD];
193#endif
194
195 celt_sig in_mem[1]; /* Size = channels*mode->overlap */
196 /* celt_sig prefilter_mem[], Size = channels*COMBFILTER_PERIOD */
197 /* celt_sig overlap_mem[], Size = channels*mode->overlap */
198 /* opus_val16 oldEBands[], Size = 2*channels*mode->nbEBands */
199};
200
201int celt_encoder_get_size(int channels)
202{
203 CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
204 return opus_custom_encoder_get_size(mode, channels);
205}
206
207OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_get_size(const CELTMode *mode, int channels)
208{
209 int size = sizeof(struct CELTEncoder)
210 + (2*channels*mode->overlap-1)*sizeof(celt_sig)
211 + channels*COMBFILTER_MAXPERIOD*sizeof(celt_sig)
212 + 3*channels*mode->nbEBands*sizeof(opus_val16);
213 return size;
214}
215
216#ifdef CUSTOM_MODES
217CELTEncoder *opus_custom_encoder_create(const CELTMode *mode, int channels, int *error)
218{
219 int ret;
220 CELTEncoder *st = (CELTEncoder *)opus_alloc(opus_custom_encoder_get_size(mode, channels));
221 /* init will handle the NULL case */
222 ret = opus_custom_encoder_init(st, mode, channels);
223 if (ret != OPUS_OK)
224 {
225 opus_custom_encoder_destroy(st);
226 st = NULL;
227 }
228 if (error)
229 *error = ret;
230 return st;
231}
232#endif /* CUSTOM_MODES */
233
234int celt_encoder_init(CELTEncoder *st, opus_int32 sampling_rate, int channels)
235{
236 int ret;
237 ret = opus_custom_encoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels);
238 if (ret != OPUS_OK)
239 return ret;
240 st->upsample = resampling_factor(sampling_rate);
241 return OPUS_OK;
242}
243
244OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_init(CELTEncoder *st, const CELTMode *mode, int channels)
245{
246 if (channels < 0 || channels > 2)
247 return OPUS_BAD_ARG;
248
249 if (st==NULL || mode==NULL)
250 return OPUS_ALLOC_FAIL;
251
252 OPUS_CLEAR((char*)st, opus_custom_encoder_get_size(mode, channels));
253
254 st->mode = mode;
255 st->overlap = mode->overlap;
256 st->stream_channels = st->channels = channels;
257
258 st->upsample = 1;
259 st->start = 0;
260 st->end = st->mode->effEBands;
261 st->signalling = 1;
262
263 st->constrained_vbr = 1;
264 st->clip = 1;
265
266 st->bitrate = OPUS_BITRATE_MAX;
267 st->vbr = 0;
268 st->force_intra = 0;
269 st->complexity = 5;
270 st->lsb_depth=24;
271
272 opus_custom_encoder_ctl(st, OPUS_RESET_STATE);
273
274 return OPUS_OK;
275}
276
277#ifdef CUSTOM_MODES
278void opus_custom_encoder_destroy(CELTEncoder *st)
279{
280 opus_free(st);
281}
282#endif /* CUSTOM_MODES */
283
284static inline opus_val16 SIG2WORD16(celt_sig x)
285{
286#ifdef FIXED_POINT
287 x = PSHR32(x, SIG_SHIFT);
288 x = MAX32(x, -32768);
289 x = MIN32(x, 32767);
290 return EXTRACT16(x);
291#else
292 return (opus_val16)x;
293#endif
294}
295
296static int transient_analysis(const opus_val32 * OPUS_RESTRICT in, int len, int C,
297 int overlap)
298{
299 int i;
300 VARDECL(opus_val16, tmp);
301 opus_val32 mem0=0,mem1=0;
302 int is_transient = 0;
303 int block;
304 int N;
305 VARDECL(opus_val16, bins);
306 SAVE_STACK;
307 ALLOC(tmp, len, opus_val16);
308
309 block = overlap/2;
310 N=len/block;
311 ALLOC(bins, N, opus_val16);
312 if (C==1)
313 {
314 for (i=0;i<len;i++)
315 tmp[i] = SHR32(in[i],SIG_SHIFT);
316 } else {
317 for (i=0;i<len;i++)
318 tmp[i] = SHR32(ADD32(in[i],in[i+len]), SIG_SHIFT+1);
319 }
320
321 /* High-pass filter: (1 - 2*z^-1 + z^-2) / (1 - z^-1 + .5*z^-2) */
322 for (i=0;i<len;i++)
323 {
324 opus_val32 x,y;
325 x = tmp[i];
326 y = ADD32(mem0, x);
327#ifdef FIXED_POINT
328 mem0 = mem1 + y - SHL32(x,1);
329 mem1 = x - SHR32(y,1);
330#else
331 mem0 = mem1 + y - 2*x;
332 mem1 = x - .5f*y;
333#endif
334 tmp[i] = EXTRACT16(SHR32(y,2));
335 }
336 /* First few samples are bad because we don't propagate the memory */
337 for (i=0;i<12;i++)
338 tmp[i] = 0;
339
340 for (i=0;i<N;i++)
341 {
342 int j;
343 opus_val16 max_abs=0;
344 for (j=0;j<block;j++)
345 max_abs = MAX16(max_abs, ABS16(tmp[i*block+j]));
346 bins[i] = max_abs;
347 }
348 for (i=0;i<N;i++)
349 {
350 int j;
351 int conseq=0;
352 opus_val16 t1, t2, t3;
353
354 t1 = MULT16_16_Q15(QCONST16(.15f, 15), bins[i]);
355 t2 = MULT16_16_Q15(QCONST16(.4f, 15), bins[i]);
356 t3 = MULT16_16_Q15(QCONST16(.15f, 15), bins[i]);
357 for (j=0;j<i;j++)
358 {
359 if (bins[j] < t1)
360 conseq++;
361 if (bins[j] < t2)
362 conseq++;
363 else
364 conseq = 0;
365 }
366 if (conseq>=3)
367 is_transient=1;
368 conseq = 0;
369 for (j=i+1;j<N;j++)
370 {
371 if (bins[j] < t3)
372 conseq++;
373 else
374 conseq = 0;
375 }
376 if (conseq>=7)
377 is_transient=1;
378 }
379 RESTORE_STACK;
380#ifdef FUZZING
381 is_transient = rand()&0x1;
382#endif
383 return is_transient;
384}
385
386/** Apply window and compute the MDCT for all sub-frames and
387 all channels in a frame */
388static void compute_mdcts(const CELTMode *mode, int shortBlocks, celt_sig * OPUS_RESTRICT in, celt_sig * OPUS_RESTRICT out, int C, int LM)
389{
390 if (C==1 && !shortBlocks)
391 {
392 const int overlap = OVERLAP(mode);
393 clt_mdct_forward(&mode->mdct, in, out, mode->window, overlap, mode->maxLM-LM, 1);
394 } else {
395 const int overlap = OVERLAP(mode);
396 int N = mode->shortMdctSize<<LM;
397 int B = 1;
398 int b, c;
399 if (shortBlocks)
400 {
401 N = mode->shortMdctSize;
402 B = shortBlocks;
403 }
404 c=0; do {
405 for (b=0;b<B;b++)
406 {
407 /* Interleaving the sub-frames while doing the MDCTs */
408 clt_mdct_forward(&mode->mdct, in+c*(B*N+overlap)+b*N, &out[b+c*N*B], mode->window, overlap, shortBlocks ? mode->maxLM : mode->maxLM-LM, B);
409 }
410 } while (++c<C);
411 }
412}
413
414/** Compute the IMDCT and apply window for all sub-frames and
415 all channels in a frame */
416static void compute_inv_mdcts(const CELTMode *mode, int shortBlocks, celt_sig *X,
417 celt_sig * OPUS_RESTRICT out_mem[],
418 celt_sig * OPUS_RESTRICT overlap_mem[], int C, int LM)
419{
420 int c;
421 const int N = mode->shortMdctSize<<LM;
422 const int overlap = OVERLAP(mode);
423 VARDECL(opus_val32, x);
424 SAVE_STACK;
425
426 ALLOC(x, N+overlap, opus_val32);
427 c=0; do {
428 int j;
429 int b;
430 int N2 = N;
431 int B = 1;
432
433 if (shortBlocks)
434 {
435 N2 = mode->shortMdctSize;
436 B = shortBlocks;
437 }
438 /* Prevents problems from the imdct doing the overlap-add */
439 OPUS_CLEAR(x, overlap);
440
441 for (b=0;b<B;b++)
442 {
443 /* IMDCT on the interleaved the sub-frames */
444 clt_mdct_backward(&mode->mdct, &X[b+c*N2*B], x+N2*b, mode->window, overlap, shortBlocks ? mode->maxLM : mode->maxLM-LM, B);
445 }
446
447 for (j=0;j<overlap;j++)
448 out_mem[c][j] = x[j] + overlap_mem[c][j];
449 for (;j<N;j++)
450 out_mem[c][j] = x[j];
451 for (j=0;j<overlap;j++)
452 overlap_mem[c][j] = x[N+j];
453 } while (++c<C);
454 RESTORE_STACK;
455}
456
457static void deemphasis(celt_sig *in[], opus_val16 *pcm, int N, int C, int downsample, const opus_val16 *coef, celt_sig *mem)
458{
459 int c;
460 int count=0;
461 c=0; do {
462 int j;
463 celt_sig * OPUS_RESTRICT x;
464 opus_val16 * OPUS_RESTRICT y;
465 celt_sig m = mem[c];
466 x =in[c];
467 y = pcm+c;
468 for (j=0;j<N;j++)
469 {
470 celt_sig tmp = *x + m;
471 m = MULT16_32_Q15(coef[0], tmp)
472 - MULT16_32_Q15(coef[1], *x);
473 tmp = SHL32(MULT16_32_Q15(coef[3], tmp), 2);
474 x++;
475 /* Technically the store could be moved outside of the if because
476 the stores we don't want will just be overwritten */
477 if (count==0)
478 *y = SCALEOUT(SIG2WORD16(tmp));
479 if (++count==downsample)
480 {
481 y+=C;
482 count=0;
483 }
484 }
485 mem[c] = m;
486 } while (++c<C);
487}
488
489static void comb_filter(opus_val32 *y, opus_val32 *x, int T0, int T1, int N,
490 opus_val16 g0, opus_val16 g1, int tapset0, int tapset1,
491 const opus_val16 *window, int overlap)
492{
493 int i;
494 /* printf ("%d %d %f %f\n", T0, T1, g0, g1); */
495 opus_val16 g00, g01, g02, g10, g11, g12;
496 static const opus_val16 gains[3][3] = {
497 {QCONST16(0.3066406250f, 15), QCONST16(0.2170410156f, 15), QCONST16(0.1296386719f, 15)},
498 {QCONST16(0.4638671875f, 15), QCONST16(0.2680664062f, 15), QCONST16(0.f, 15)},
499 {QCONST16(0.7998046875f, 15), QCONST16(0.1000976562f, 15), QCONST16(0.f, 15)}};
500 g00 = MULT16_16_Q15(g0, gains[tapset0][0]);
501 g01 = MULT16_16_Q15(g0, gains[tapset0][1]);
502 g02 = MULT16_16_Q15(g0, gains[tapset0][2]);
503 g10 = MULT16_16_Q15(g1, gains[tapset1][0]);
504 g11 = MULT16_16_Q15(g1, gains[tapset1][1]);
505 g12 = MULT16_16_Q15(g1, gains[tapset1][2]);
506 for (i=0;i<overlap;i++)
507 {
508 opus_val16 f;
509 f = MULT16_16_Q15(window[i],window[i]);
510 y[i] = x[i]
511 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g00),x[i-T0])
512 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),x[i-T0-1])
513 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),x[i-T0+1])
514 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),x[i-T0-2])
515 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),x[i-T0+2])
516 + MULT16_32_Q15(MULT16_16_Q15(f,g10),x[i-T1])
517 + MULT16_32_Q15(MULT16_16_Q15(f,g11),x[i-T1-1])
518 + MULT16_32_Q15(MULT16_16_Q15(f,g11),x[i-T1+1])
519 + MULT16_32_Q15(MULT16_16_Q15(f,g12),x[i-T1-2])
520 + MULT16_32_Q15(MULT16_16_Q15(f,g12),x[i-T1+2]);
521
522 }
523 for (i=overlap;i<N;i++)
524 y[i] = x[i]
525 + MULT16_32_Q15(g10,x[i-T1])
526 + MULT16_32_Q15(g11,x[i-T1-1])
527 + MULT16_32_Q15(g11,x[i-T1+1])
528 + MULT16_32_Q15(g12,x[i-T1-2])
529 + MULT16_32_Q15(g12,x[i-T1+2]);
530}
531
532static const signed char tf_select_table[4][8] = {
533 {0, -1, 0, -1, 0,-1, 0,-1},
534 {0, -1, 0, -2, 1, 0, 1,-1},
535 {0, -2, 0, -3, 2, 0, 1,-1},
536 {0, -2, 0, -3, 3, 0, 1,-1},
537};
538
539static opus_val32 l1_metric(const celt_norm *tmp, int N, int LM, int width)
540{
541 int i, j;
542 static const opus_val16 sqrtM_1[4] = {Q15ONE, QCONST16(.70710678f,15), QCONST16(0.5f,15), QCONST16(0.35355339f,15)};
543 opus_val32 L1;
544 opus_val16 bias;
545 L1=0;
546 for (i=0;i<1<<LM;i++)
547 {
548 opus_val32 L2 = 0;
549 for (j=0;j<N>>LM;j++)
550 L2 = MAC16_16(L2, tmp[(j<<LM)+i], tmp[(j<<LM)+i]);
551 L1 += celt_sqrt(L2);
552 }
553 L1 = MULT16_32_Q15(sqrtM_1[LM], L1);
554 if (width==1)
555 bias = QCONST16(.12f,15)*LM;
556 else if (width==2)
557 bias = QCONST16(.05f,15)*LM;
558 else
559 bias = QCONST16(.02f,15)*LM;
560 L1 = MAC16_32_Q15(L1, bias, L1);
561 return L1;
562}
563
564static int tf_analysis(const CELTMode *m, int len, int C, int isTransient,
565 int *tf_res, int nbCompressedBytes, celt_norm *X, int N0, int LM,
566 int *tf_sum)
567{
568 int i;
569 VARDECL(int, metric);
570 int cost0;
571 int cost1;
572 VARDECL(int, path0);
573 VARDECL(int, path1);
574 VARDECL(celt_norm, tmp);
575 int lambda;
576 int tf_select=0;
577 SAVE_STACK;
578
579 if (nbCompressedBytes<15*C)
580 {
581 *tf_sum = 0;
582 for (i=0;i<len;i++)
583 tf_res[i] = isTransient;
584 return 0;
585 }
586 if (nbCompressedBytes<40)
587 lambda = 12;
588 else if (nbCompressedBytes<60)
589 lambda = 6;
590 else if (nbCompressedBytes<100)
591 lambda = 4;
592 else
593 lambda = 3;
594
595 ALLOC(metric, len, int);
596 ALLOC(tmp, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm);
597 ALLOC(path0, len, int);
598 ALLOC(path1, len, int);
599
600 *tf_sum = 0;
601 for (i=0;i<len;i++)
602 {
603 int j, k, N;
604 opus_val32 L1, best_L1;
605 int best_level=0;
606 N = (m->eBands[i+1]-m->eBands[i])<<LM;
607 for (j=0;j<N;j++)
608 tmp[j] = X[j+(m->eBands[i]<<LM)];
609 /* Just add the right channel if we're in stereo */
610 if (C==2)
611 for (j=0;j<N;j++)
612 tmp[j] = ADD16(SHR16(tmp[j], 1),SHR16(X[N0+j+(m->eBands[i]<<LM)], 1));
613 L1 = l1_metric(tmp, N, isTransient ? LM : 0, N>>LM);
614 best_L1 = L1;
615 /*printf ("%f ", L1);*/
616 for (k=0;k<LM;k++)
617 {
618 int B;
619
620 if (isTransient)
621 B = (LM-k-1);
622 else
623 B = k+1;
624
625 if (isTransient)
626 haar1(tmp, N>>(LM-k), 1<<(LM-k));
627 else
628 haar1(tmp, N>>k, 1<<k);
629
630 L1 = l1_metric(tmp, N, B, N>>LM);
631
632 if (L1 < best_L1)
633 {
634 best_L1 = L1;
635 best_level = k+1;
636 }
637 }
638 /*printf ("%d ", isTransient ? LM-best_level : best_level);*/
639 if (isTransient)
640 metric[i] = best_level;
641 else
642 metric[i] = -best_level;
643 *tf_sum += metric[i];
644 }
645 /*printf("\n");*/
646 /* NOTE: Future optimized implementations could detect extreme transients and set
647 tf_select = 1 but so far we have not found a reliable way of making this useful */
648 tf_select = 0;
649
650 cost0 = 0;
651 cost1 = isTransient ? 0 : lambda;
652 /* Viterbi forward pass */
653 for (i=1;i<len;i++)
654 {
655 int curr0, curr1;
656 int from0, from1;
657
658 from0 = cost0;
659 from1 = cost1 + lambda;
660 if (from0 < from1)
661 {
662 curr0 = from0;
663 path0[i]= 0;
664 } else {
665 curr0 = from1;
666 path0[i]= 1;
667 }
668
669 from0 = cost0 + lambda;
670 from1 = cost1;
671 if (from0 < from1)
672 {
673 curr1 = from0;
674 path1[i]= 0;
675 } else {
676 curr1 = from1;
677 path1[i]= 1;
678 }
679 cost0 = curr0 + abs(metric[i]-tf_select_table[LM][4*isTransient+2*tf_select+0]);
680 cost1 = curr1 + abs(metric[i]-tf_select_table[LM][4*isTransient+2*tf_select+1]);
681 }
682 tf_res[len-1] = cost0 < cost1 ? 0 : 1;
683 /* Viterbi backward pass to check the decisions */
684 for (i=len-2;i>=0;i--)
685 {
686 if (tf_res[i+1] == 1)
687 tf_res[i] = path1[i+1];
688 else
689 tf_res[i] = path0[i+1];
690 }
691 RESTORE_STACK;
692#ifdef FUZZING
693 tf_select = rand()&0x1;
694 tf_res[0] = rand()&0x1;
695 for (i=1;i<len;i++)
696 tf_res[i] = tf_res[i-1] ^ ((rand()&0xF) == 0);
697#endif
698 return tf_select;
699}
700
701static void tf_encode(int start, int end, int isTransient, int *tf_res, int LM, int tf_select, ec_enc *enc)
702{
703 int curr, i;
704 int tf_select_rsv;
705 int tf_changed;
706 int logp;
707 opus_uint32 budget;
708 opus_uint32 tell;
709 budget = enc->storage*8;
710 tell = ec_tell(enc);
711 logp = isTransient ? 2 : 4;
712 /* Reserve space to code the tf_select decision. */
713 tf_select_rsv = LM>0 && tell+logp+1 <= budget;
714 budget -= tf_select_rsv;
715 curr = tf_changed = 0;
716 for (i=start;i<end;i++)
717 {
718 if (tell+logp<=budget)
719 {
720 ec_enc_bit_logp(enc, tf_res[i] ^ curr, logp);
721 tell = ec_tell(enc);
722 curr = tf_res[i];
723 tf_changed |= curr;
724 }
725 else
726 tf_res[i] = curr;
727 logp = isTransient ? 4 : 5;
728 }
729 /* Only code tf_select if it would actually make a difference. */
730 if (tf_select_rsv &&
731 tf_select_table[LM][4*isTransient+0+tf_changed]!=
732 tf_select_table[LM][4*isTransient+2+tf_changed])
733 ec_enc_bit_logp(enc, tf_select, 1);
734 else
735 tf_select = 0;
736 for (i=start;i<end;i++)
737 tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
738 /*printf("%d %d ", isTransient, tf_select); for(i=0;i<end;i++)printf("%d ", tf_res[i]);printf("\n");*/
739}
740
741static void tf_decode(int start, int end, int isTransient, int *tf_res, int LM, ec_dec *dec)
742{
743 int i, curr, tf_select;
744 int tf_select_rsv;
745 int tf_changed;
746 int logp;
747 opus_uint32 budget;
748 opus_uint32 tell;
749
750 budget = dec->storage*8;
751 tell = ec_tell(dec);
752 logp = isTransient ? 2 : 4;
753 tf_select_rsv = LM>0 && tell+logp+1<=budget;
754 budget -= tf_select_rsv;
755 tf_changed = curr = 0;
756 for (i=start;i<end;i++)
757 {
758 if (tell+logp<=budget)
759 {
760 curr ^= ec_dec_bit_logp(dec, logp);
761 tell = ec_tell(dec);
762 tf_changed |= curr;
763 }
764 tf_res[i] = curr;
765 logp = isTransient ? 4 : 5;
766 }
767 tf_select = 0;
768 if (tf_select_rsv &&
769 tf_select_table[LM][4*isTransient+0+tf_changed] !=
770 tf_select_table[LM][4*isTransient+2+tf_changed])
771 {
772 tf_select = ec_dec_bit_logp(dec, 1);
773 }
774 for (i=start;i<end;i++)
775 {
776 tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
777 }
778}
779
780static void init_caps(const CELTMode *m,int *cap,int LM,int C)
781{
782 int i;
783 for (i=0;i<m->nbEBands;i++)
784 {
785 int N;
786 N=(m->eBands[i+1]-m->eBands[i])<<LM;
787 cap[i] = (m->cache.caps[m->nbEBands*(2*LM+C-1)+i]+64)*C*N>>2;
788 }
789}
790
791static int alloc_trim_analysis(const CELTMode *m, const celt_norm *X,
792 const opus_val16 *bandLogE, int end, int LM, int C, int N0)
793{
794 int i;
795 opus_val32 diff=0;
796 int c;
797 int trim_index = 5;
798 if (C==2)
799 {
800 opus_val16 sum = 0; /* Q10 */
801 /* Compute inter-channel correlation for low frequencies */
802 for (i=0;i<8;i++)
803 {
804 int j;
805 opus_val32 partial = 0;
806 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
807 partial = MAC16_16(partial, X[j], X[N0+j]);
808 sum = ADD16(sum, EXTRACT16(SHR32(partial, 18)));
809 }
810 sum = MULT16_16_Q15(QCONST16(1.f/8, 15), sum);
811 /*printf ("%f\n", sum);*/
812 if (sum > QCONST16(.995f,10))
813 trim_index-=4;
814 else if (sum > QCONST16(.92f,10))
815 trim_index-=3;
816 else if (sum > QCONST16(.85f,10))
817 trim_index-=2;
818 else if (sum > QCONST16(.8f,10))
819 trim_index-=1;
820 }
821
822 /* Estimate spectral tilt */
823 c=0; do {
824 for (i=0;i<end-1;i++)
825 {
826 diff += bandLogE[i+c*m->nbEBands]*(opus_int32)(2+2*i-m->nbEBands);
827 }
828 } while (++c<C);
829 /* We divide by two here to avoid making the tilt larger for stereo as a
830 result of a bug in the loop above */
831 diff /= 2*C*(end-1);
832 /*printf("%f\n", diff);*/
833 if (diff > QCONST16(2.f, DB_SHIFT))
834 trim_index--;
835 if (diff > QCONST16(8.f, DB_SHIFT))
836 trim_index--;
837 if (diff < -QCONST16(4.f, DB_SHIFT))
838 trim_index++;
839 if (diff < -QCONST16(10.f, DB_SHIFT))
840 trim_index++;
841
842 if (trim_index<0)
843 trim_index = 0;
844 if (trim_index>10)
845 trim_index = 10;
846#ifdef FUZZING
847 trim_index = rand()%11;
848#endif
849 return trim_index;
850}
851
852static int stereo_analysis(const CELTMode *m, const celt_norm *X,
853 int LM, int N0)
854{
855 int i;
856 int thetas;
857 opus_val32 sumLR = EPSILON, sumMS = EPSILON;
858
859 /* Use the L1 norm to model the entropy of the L/R signal vs the M/S signal */
860 for (i=0;i<13;i++)
861 {
862 int j;
863 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
864 {
865 opus_val32 L, R, M, S;
866 /* We cast to 32-bit first because of the -32768 case */
867 L = EXTEND32(X[j]);
868 R = EXTEND32(X[N0+j]);
869 M = ADD32(L, R);
870 S = SUB32(L, R);
871 sumLR = ADD32(sumLR, ADD32(ABS32(L), ABS32(R)));
872 sumMS = ADD32(sumMS, ADD32(ABS32(M), ABS32(S)));
873 }
874 }
875 sumMS = MULT16_32_Q15(QCONST16(0.707107f, 15), sumMS);
876 thetas = 13;
877 /* We don't need thetas for lower bands with LM<=1 */
878 if (LM<=1)
879 thetas -= 8;
880 return MULT16_32_Q15((m->eBands[13]<<(LM+1))+thetas, sumMS)
881 > MULT16_32_Q15(m->eBands[13]<<(LM+1), sumLR);
882}
883
884int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_val16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc)
885{
886 int i, c, N;
887 opus_int32 bits;
888 ec_enc _enc;
889 VARDECL(celt_sig, in);
890 VARDECL(celt_sig, freq);
891 VARDECL(celt_norm, X);
892 VARDECL(celt_ener, bandE);
893 VARDECL(opus_val16, bandLogE);
894 VARDECL(int, fine_quant);
895 VARDECL(opus_val16, error);
896 VARDECL(int, pulses);
897 VARDECL(int, cap);
898 VARDECL(int, offsets);
899 VARDECL(int, fine_priority);
900 VARDECL(int, tf_res);
901 VARDECL(unsigned char, collapse_masks);
902 celt_sig *prefilter_mem;
903 opus_val16 *oldBandE, *oldLogE, *oldLogE2;
904 int shortBlocks=0;
905 int isTransient=0;
906 const int CC = st->channels;
907 const int C = st->stream_channels;
908 int LM, M;
909 int tf_select;
910 int nbFilledBytes, nbAvailableBytes;
911 int effEnd;
912 int codedBands;
913 int tf_sum;
914 int alloc_trim;
915 int pitch_index=COMBFILTER_MINPERIOD;
916 opus_val16 gain1 = 0;
917 int intensity=0;
918 int dual_stereo=0;
919 int effectiveBytes;
920 opus_val16 pf_threshold;
921 int dynalloc_logp;
922 opus_int32 vbr_rate;
923 opus_int32 total_bits;
924 opus_int32 total_boost;
925 opus_int32 balance;
926 opus_int32 tell;
927 int prefilter_tapset=0;
928 int pf_on;
929 int anti_collapse_rsv;
930 int anti_collapse_on=0;
931 int silence=0;
932 ALLOC_STACK;
933
934 if (nbCompressedBytes<2 || pcm==NULL)
935 return OPUS_BAD_ARG;
936
937 frame_size *= st->upsample;
938 for (LM=0;LM<=st->mode->maxLM;LM++)
939 if (st->mode->shortMdctSize<<LM==frame_size)
940 break;
941 if (LM>st->mode->maxLM)
942 return OPUS_BAD_ARG;
943 M=1<<LM;
944 N = M*st->mode->shortMdctSize;
945
946 prefilter_mem = st->in_mem+CC*(st->overlap);
947 oldBandE = (opus_val16*)(st->in_mem+CC*(2*st->overlap+COMBFILTER_MAXPERIOD));
948 oldLogE = oldBandE + CC*st->mode->nbEBands;
949 oldLogE2 = oldLogE + CC*st->mode->nbEBands;
950
951 if (enc==NULL)
952 {
953 tell=1;
954 nbFilledBytes=0;
955 } else {
956 tell=ec_tell(enc);
957 nbFilledBytes=(tell+4)>>3;
958 }
959
960#ifdef CUSTOM_MODES
961 if (st->signalling && enc==NULL)
962 {
963 int tmp = (st->mode->effEBands-st->end)>>1;
964 st->end = IMAX(1, st->mode->effEBands-tmp);
965 compressed[0] = tmp<<5;
966 compressed[0] |= LM<<3;
967 compressed[0] |= (C==2)<<2;
968 /* Convert "standard mode" to Opus header */
969 if (st->mode->Fs==48000 && st->mode->shortMdctSize==120)
970 {
971 int c0 = toOpus(compressed[0]);
972 if (c0<0)
973 return OPUS_BAD_ARG;
974 compressed[0] = c0;
975 }
976 compressed++;
977 nbCompressedBytes--;
978 }
979#else
980 celt_assert(st->signalling==0);
981#endif
982
983 /* Can't produce more than 1275 output bytes */
984 nbCompressedBytes = IMIN(nbCompressedBytes,1275);
985 nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
986
987 if (st->vbr && st->bitrate!=OPUS_BITRATE_MAX)
988 {
989 opus_int32 den=st->mode->Fs>>BITRES;
990 vbr_rate=(st->bitrate*frame_size+(den>>1))/den;
991#ifdef CUSTOM_MODES
992 if (st->signalling)
993 vbr_rate -= 8<<BITRES;
994#endif
995 effectiveBytes = vbr_rate>>(3+BITRES);
996 } else {
997 opus_int32 tmp;
998 vbr_rate = 0;
999 tmp = st->bitrate*frame_size;
1000 if (tell>1)
1001 tmp += tell;
1002 if (st->bitrate!=OPUS_BITRATE_MAX)
1003 nbCompressedBytes = IMAX(2, IMIN(nbCompressedBytes,
1004 (tmp+4*st->mode->Fs)/(8*st->mode->Fs)-!!st->signalling));
1005 effectiveBytes = nbCompressedBytes;
1006 }
1007
1008 if (enc==NULL)
1009 {
1010 ec_enc_init(&_enc, compressed, nbCompressedBytes);
1011 enc = &_enc;
1012 }
1013
1014 if (vbr_rate>0)
1015 {
1016 /* Computes the max bit-rate allowed in VBR mode to avoid violating the
1017 target rate and buffering.
1018 We must do this up front so that bust-prevention logic triggers
1019 correctly if we don't have enough bits. */
1020 if (st->constrained_vbr)
1021 {
1022 opus_int32 vbr_bound;
1023 opus_int32 max_allowed;
1024 /* We could use any multiple of vbr_rate as bound (depending on the
1025 delay).
1026 This is clamped to ensure we use at least two bytes if the encoder
1027 was entirely empty, but to allow 0 in hybrid mode. */
1028 vbr_bound = vbr_rate;
1029 max_allowed = IMIN(IMAX(tell==1?2:0,
1030 (vbr_rate+vbr_bound-st->vbr_reservoir)>>(BITRES+3)),
1031 nbAvailableBytes);
1032 if(max_allowed < nbAvailableBytes)
1033 {
1034 nbCompressedBytes = nbFilledBytes+max_allowed;
1035 nbAvailableBytes = max_allowed;
1036 ec_enc_shrink(enc, nbCompressedBytes);
1037 }
1038 }
1039 }
1040 total_bits = nbCompressedBytes*8;
1041
1042 effEnd = st->end;
1043 if (effEnd > st->mode->effEBands)
1044 effEnd = st->mode->effEBands;
1045
1046 ALLOC(in, CC*(N+st->overlap), celt_sig);
1047
1048 /* Find pitch period and gain */
1049 {
1050 VARDECL(celt_sig, _pre);
1051 celt_sig *pre[2];
1052 SAVE_STACK;
1053 ALLOC(_pre, CC*(N+COMBFILTER_MAXPERIOD), celt_sig);
1054
1055 pre[0] = _pre;
1056 pre[1] = _pre + (N+COMBFILTER_MAXPERIOD);
1057
1058 silence = 1;
1059 c=0; do {
1060 int count = 0;
1061 const opus_val16 * OPUS_RESTRICT pcmp = pcm+c;
1062 celt_sig * OPUS_RESTRICT inp = in+c*(N+st->overlap)+st->overlap;
1063
1064 for (i=0;i<N;i++)
1065 {
1066 celt_sig x, tmp;
1067
1068 x = SCALEIN(*pcmp);
1069#ifndef FIXED_POINT
1070 if (!(x==x))
1071 x = 0;
1072 if (st->clip)
1073 x = MAX32(-65536.f, MIN32(65536.f,x));
1074#endif
1075 if (++count==st->upsample)
1076 {
1077 count=0;
1078 pcmp+=CC;
1079 } else {
1080 x = 0;
1081 }
1082 /* Apply pre-emphasis */
1083 tmp = MULT16_16(st->mode->preemph[2], x);
1084 *inp = tmp + st->preemph_memE[c];
1085 st->preemph_memE[c] = MULT16_32_Q15(st->mode->preemph[1], *inp)
1086 - MULT16_32_Q15(st->mode->preemph[0], tmp);
1087 silence = silence && *inp == 0;
1088 inp++;
1089 }
1090 OPUS_COPY(pre[c], prefilter_mem+c*COMBFILTER_MAXPERIOD, COMBFILTER_MAXPERIOD);
1091 OPUS_COPY(pre[c]+COMBFILTER_MAXPERIOD, in+c*(N+st->overlap)+st->overlap, N);
1092 } while (++c<CC);
1093
1094#ifdef FUZZING
1095 if ((rand()&0x3F)==0)
1096 silence = 1;
1097#endif
1098 if (tell==1)
1099 ec_enc_bit_logp(enc, silence, 15);
1100 else
1101 silence=0;
1102 if (silence)
1103 {
1104 /*In VBR mode there is no need to send more than the minimum. */
1105 if (vbr_rate>0)
1106 {
1107 effectiveBytes=nbCompressedBytes=IMIN(nbCompressedBytes, nbFilledBytes+2);
1108 total_bits=nbCompressedBytes*8;
1109 nbAvailableBytes=2;
1110 ec_enc_shrink(enc, nbCompressedBytes);
1111 }
1112 /* Pretend we've filled all the remaining bits with zeros
1113 (that's what the initialiser did anyway) */
1114 tell = nbCompressedBytes*8;
1115 enc->nbits_total+=tell-ec_tell(enc);
1116 }
1117 if (nbAvailableBytes>12*C && st->start==0 && !silence && !st->disable_pf && st->complexity >= 5)
1118 {
1119 VARDECL(opus_val16, pitch_buf);
1120 ALLOC(pitch_buf, (COMBFILTER_MAXPERIOD+N)>>1, opus_val16);
1121
1122 pitch_downsample(pre, pitch_buf, COMBFILTER_MAXPERIOD+N, CC);
1123 pitch_search(pitch_buf+(COMBFILTER_MAXPERIOD>>1), pitch_buf, N,
1124 COMBFILTER_MAXPERIOD-COMBFILTER_MINPERIOD, &pitch_index);
1125 pitch_index = COMBFILTER_MAXPERIOD-pitch_index;
1126
1127 gain1 = remove_doubling(pitch_buf, COMBFILTER_MAXPERIOD, COMBFILTER_MINPERIOD,
1128 N, &pitch_index, st->prefilter_period, st->prefilter_gain);
1129 if (pitch_index > COMBFILTER_MAXPERIOD-2)
1130 pitch_index = COMBFILTER_MAXPERIOD-2;
1131 gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1);
1132 if (st->loss_rate>2)
1133 gain1 = HALF32(gain1);
1134 if (st->loss_rate>4)
1135 gain1 = HALF32(gain1);
1136 if (st->loss_rate>8)
1137 gain1 = 0;
1138 prefilter_tapset = st->tapset_decision;
1139 } else {
1140 gain1 = 0;
1141 }
1142
1143 /* Gain threshold for enabling the prefilter/postfilter */
1144 pf_threshold = QCONST16(.2f,15);
1145
1146 /* Adjusting the threshold based on rate and continuity */
1147 if (abs(pitch_index-st->prefilter_period)*10>pitch_index)
1148 pf_threshold += QCONST16(.2f,15);
1149 if (nbAvailableBytes<25)
1150 pf_threshold += QCONST16(.1f,15);
1151 if (nbAvailableBytes<35)
1152 pf_threshold += QCONST16(.1f,15);
1153 if (st->prefilter_gain > QCONST16(.4f,15))
1154 pf_threshold -= QCONST16(.1f,15);
1155 if (st->prefilter_gain > QCONST16(.55f,15))
1156 pf_threshold -= QCONST16(.1f,15);
1157
1158 /* Hard threshold at 0.2 */
1159 pf_threshold = MAX16(pf_threshold, QCONST16(.2f,15));
1160 if (gain1<pf_threshold)
1161 {
1162 if(st->start==0 && tell+16<=total_bits)
1163 ec_enc_bit_logp(enc, 0, 1);
1164 gain1 = 0;
1165 pf_on = 0;
1166 } else {
1167 /*This block is not gated by a total bits check only because
1168 of the nbAvailableBytes check above.*/
1169 int qg;
1170 int octave;
1171
1172 if (ABS16(gain1-st->prefilter_gain)<QCONST16(.1f,15))
1173 gain1=st->prefilter_gain;
1174
1175#ifdef FIXED_POINT
1176 qg = ((gain1+1536)>>10)/3-1;
1177#else
1178 qg = (int)floor(.5f+gain1*32/3)-1;
1179#endif
1180 qg = IMAX(0, IMIN(7, qg));
1181 ec_enc_bit_logp(enc, 1, 1);
1182 pitch_index += 1;
1183 octave = EC_ILOG(pitch_index)-5;
1184 ec_enc_uint(enc, octave, 6);
1185 ec_enc_bits(enc, pitch_index-(16<<octave), 4+octave);
1186 pitch_index -= 1;
1187 ec_enc_bits(enc, qg, 3);
1188 if (ec_tell(enc)+2<=total_bits)
1189 ec_enc_icdf(enc, prefilter_tapset, tapset_icdf, 2);
1190 else
1191 prefilter_tapset = 0;
1192 gain1 = QCONST16(0.09375f,15)*(qg+1);
1193 pf_on = 1;
1194 }
1195 /*printf("%d %f\n", pitch_index, gain1);*/
1196
1197 c=0; do {
1198 int offset = st->mode->shortMdctSize-st->mode->overlap;
1199 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
1200 OPUS_COPY(in+c*(N+st->overlap), st->in_mem+c*(st->overlap), st->overlap);
1201 if (offset)
1202 comb_filter(in+c*(N+st->overlap)+st->overlap, pre[c]+COMBFILTER_MAXPERIOD,
1203 st->prefilter_period, st->prefilter_period, offset, -st->prefilter_gain, -st->prefilter_gain,
1204 st->prefilter_tapset, st->prefilter_tapset, NULL, 0);
1205
1206 comb_filter(in+c*(N+st->overlap)+st->overlap+offset, pre[c]+COMBFILTER_MAXPERIOD+offset,
1207 st->prefilter_period, pitch_index, N-offset, -st->prefilter_gain, -gain1,
1208 st->prefilter_tapset, prefilter_tapset, st->mode->window, st->mode->overlap);
1209 OPUS_COPY(st->in_mem+c*(st->overlap), in+c*(N+st->overlap)+N, st->overlap);
1210
1211 if (N>COMBFILTER_MAXPERIOD)
1212 {
1213 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, pre[c]+N, COMBFILTER_MAXPERIOD);
1214 } else {
1215 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, prefilter_mem+c*COMBFILTER_MAXPERIOD+N, COMBFILTER_MAXPERIOD-N);
1216 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD+COMBFILTER_MAXPERIOD-N, pre[c]+COMBFILTER_MAXPERIOD, N);
1217 }
1218 } while (++c<CC);
1219
1220 RESTORE_STACK;
1221 }
1222
1223 isTransient = 0;
1224 shortBlocks = 0;
1225 if (LM>0 && ec_tell(enc)+3<=total_bits)
1226 {
1227 if (st->complexity > 1)
1228 {
1229 isTransient = transient_analysis(in, N+st->overlap, CC,
1230 st->overlap);
1231 if (isTransient)
1232 shortBlocks = M;
1233 }
1234 ec_enc_bit_logp(enc, isTransient, 3);
1235 }
1236
1237 ALLOC(freq, CC*N, celt_sig); /**< Interleaved signal MDCTs */
1238 ALLOC(bandE,st->mode->nbEBands*CC, celt_ener);
1239 ALLOC(bandLogE,st->mode->nbEBands*CC, opus_val16);
1240 /* Compute MDCTs */
1241 compute_mdcts(st->mode, shortBlocks, in, freq, CC, LM);
1242
1243 if (CC==2&&C==1)
1244 {
1245 for (i=0;i<N;i++)
1246 freq[i] = ADD32(HALF32(freq[i]), HALF32(freq[N+i]));
1247 }
1248 if (st->upsample != 1)
1249 {
1250 c=0; do
1251 {
1252 int bound = N/st->upsample;
1253 for (i=0;i<bound;i++)
1254 freq[c*N+i] *= st->upsample;
1255 for (;i<N;i++)
1256 freq[c*N+i] = 0;
1257 } while (++c<C);
1258 }
1259 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
1260
1261 compute_band_energies(st->mode, freq, bandE, effEnd, C, M);
1262
1263 amp2Log2(st->mode, effEnd, st->end, bandE, bandLogE, C);
1264
1265 /* Band normalisation */
1266 normalise_bands(st->mode, freq, X, bandE, effEnd, C, M);
1267
1268 ALLOC(tf_res, st->mode->nbEBands, int);
1269 tf_select = tf_analysis(st->mode, effEnd, C, isTransient, tf_res, effectiveBytes, X, N, LM, &tf_sum);
1270 for (i=effEnd;i<st->end;i++)
1271 tf_res[i] = tf_res[effEnd-1];
1272
1273 ALLOC(error, C*st->mode->nbEBands, opus_val16);
1274 quant_coarse_energy(st->mode, st->start, st->end, effEnd, bandLogE,
1275 oldBandE, total_bits, error, enc,
1276 C, LM, nbAvailableBytes, st->force_intra,
1277 &st->delayedIntra, st->complexity >= 4, st->loss_rate);
1278
1279 tf_encode(st->start, st->end, isTransient, tf_res, LM, tf_select, enc);
1280
1281 st->spread_decision = SPREAD_NORMAL;
1282 if (ec_tell(enc)+4<=total_bits)
1283 {
1284 if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C)
1285 {
1286 if (st->complexity == 0)
1287 st->spread_decision = SPREAD_NONE;
1288 } else {
1289 st->spread_decision = spreading_decision(st->mode, X,
1290 &st->tonal_average, st->spread_decision, &st->hf_average,
1291 &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M);
1292 }
1293 ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5);
1294 }
1295
1296 ALLOC(cap, st->mode->nbEBands, int);
1297 ALLOC(offsets, st->mode->nbEBands, int);
1298
1299 init_caps(st->mode,cap,LM,C);
1300 for (i=0;i<st->mode->nbEBands;i++)
1301 offsets[i] = 0;
1302 /* Dynamic allocation code */
1303 /* Make sure that dynamic allocation can't make us bust the budget */
1304 if (effectiveBytes > 50 && LM>=1)
1305 {
1306 int t1, t2;
1307 if (LM <= 1)
1308 {
1309 t1 = 3;
1310 t2 = 5;
1311 } else {
1312 t1 = 2;
1313 t2 = 4;
1314 }
1315 for (i=st->start+1;i<st->end-1;i++)
1316 {
1317 opus_val32 d2;
1318 d2 = 2*bandLogE[i]-bandLogE[i-1]-bandLogE[i+1];
1319 if (C==2)
1320 d2 = HALF32(d2 + 2*bandLogE[i+st->mode->nbEBands]-
1321 bandLogE[i-1+st->mode->nbEBands]-bandLogE[i+1+st->mode->nbEBands]);
1322#ifdef FUZZING
1323 if((rand()&0xF)==0)
1324 {
1325 offsets[i] += 1;
1326 if((rand()&0x3)==0)
1327 offsets[i] += 1+(rand()&0x3);
1328 }
1329#else
1330 if (d2 > SHL16(t1,DB_SHIFT))
1331 offsets[i] += 1;
1332 if (d2 > SHL16(t2,DB_SHIFT))
1333 offsets[i] += 1;
1334#endif
1335 }
1336 }
1337 dynalloc_logp = 6;
1338 total_bits<<=BITRES;
1339 total_boost = 0;
1340 tell = ec_tell_frac(enc);
1341 for (i=st->start;i<st->end;i++)
1342 {
1343 int width, quanta;
1344 int dynalloc_loop_logp;
1345 int boost;
1346 int j;
1347 width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
1348 /* quanta is 6 bits, but no more than 1 bit/sample
1349 and no less than 1/8 bit/sample */
1350 quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
1351 dynalloc_loop_logp = dynalloc_logp;
1352 boost = 0;
1353 for (j = 0; tell+(dynalloc_loop_logp<<BITRES) < total_bits-total_boost
1354 && boost < cap[i]; j++)
1355 {
1356 int flag;
1357 flag = j<offsets[i];
1358 ec_enc_bit_logp(enc, flag, dynalloc_loop_logp);
1359 tell = ec_tell_frac(enc);
1360 if (!flag)
1361 break;
1362 boost += quanta;
1363 total_boost += quanta;
1364 dynalloc_loop_logp = 1;
1365 }
1366 /* Making dynalloc more likely */
1367 if (j)
1368 dynalloc_logp = IMAX(2, dynalloc_logp-1);
1369 offsets[i] = boost;
1370 }
1371 alloc_trim = 5;
1372 if (tell+(6<<BITRES) <= total_bits - total_boost)
1373 {
1374 alloc_trim = alloc_trim_analysis(st->mode, X, bandLogE,
1375 st->end, LM, C, N);
1376 ec_enc_icdf(enc, alloc_trim, trim_icdf, 7);
1377 tell = ec_tell_frac(enc);
1378 }
1379
1380 /* Variable bitrate */
1381 if (vbr_rate>0)
1382 {
1383 opus_val16 alpha;
1384 opus_int32 delta;
1385 /* The target rate in 8th bits per frame */
1386 opus_int32 target;
1387 opus_int32 min_allowed;
1388 int lm_diff = st->mode->maxLM - LM;
1389
1390 /* Don't attempt to use more than 510 kb/s, even for frames smaller than 20 ms.
1391 The CELT allocator will just not be able to use more than that anyway. */
1392 nbCompressedBytes = IMIN(nbCompressedBytes,1275>>(3-LM));
1393 target = vbr_rate + (st->vbr_offset>>lm_diff) - ((40*C+20)<<BITRES);
1394
1395 /* Shortblocks get a large boost in bitrate, but since they
1396 are uncommon long blocks are not greatly affected */
1397 if (shortBlocks || tf_sum < -2*(st->end-st->start))
1398 target = 7*target/4;
1399 else if (tf_sum < -(st->end-st->start))
1400 target = 3*target/2;
1401 else if (M > 1)
1402 target-=(target+14)/28;
1403
1404 /* The current offset is removed from the target and the space used
1405 so far is added*/
1406 target=target+tell;
1407
1408 /* In VBR mode the frame size must not be reduced so much that it would
1409 result in the encoder running out of bits.
1410 The margin of 2 bytes ensures that none of the bust-prevention logic
1411 in the decoder will have triggered so far. */
1412 min_allowed = ((tell+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3)) + 2 - nbFilledBytes;
1413
1414 nbAvailableBytes = (target+(1<<(BITRES+2)))>>(BITRES+3);
1415 nbAvailableBytes = IMAX(min_allowed,nbAvailableBytes);
1416 nbAvailableBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes) - nbFilledBytes;
1417
1418 /* By how much did we "miss" the target on that frame */
1419 delta = target - vbr_rate;
1420
1421 target=nbAvailableBytes<<(BITRES+3);
1422
1423 /*If the frame is silent we don't adjust our drift, otherwise
1424 the encoder will shoot to very high rates after hitting a
1425 span of silence, but we do allow the bitres to refill.
1426 This means that we'll undershoot our target in CVBR/VBR modes
1427 on files with lots of silence. */
1428 if(silence)
1429 {
1430 nbAvailableBytes = 2;
1431 target = 2*8<<BITRES;
1432 delta = 0;
1433 }
1434
1435 if (st->vbr_count < 970)
1436 {
1437 st->vbr_count++;
1438 alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+20),16));
1439 } else
1440 alpha = QCONST16(.001f,15);
1441 /* How many bits have we used in excess of what we're allowed */
1442 if (st->constrained_vbr)
1443 st->vbr_reservoir += target - vbr_rate;
1444 /*printf ("%d\n", st->vbr_reservoir);*/
1445
1446 /* Compute the offset we need to apply in order to reach the target */
1447 st->vbr_drift += (opus_int32)MULT16_32_Q15(alpha,(delta*(1<<lm_diff))-st->vbr_offset-st->vbr_drift);
1448 st->vbr_offset = -st->vbr_drift;
1449 /*printf ("%d\n", st->vbr_drift);*/
1450
1451 if (st->constrained_vbr && st->vbr_reservoir < 0)
1452 {
1453 /* We're under the min value -- increase rate */
1454 int adjust = (-st->vbr_reservoir)/(8<<BITRES);
1455 /* Unless we're just coding silence */
1456 nbAvailableBytes += silence?0:adjust;
1457 st->vbr_reservoir = 0;
1458 /*printf ("+%d\n", adjust);*/
1459 }
1460 nbCompressedBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes);
1461 /* This moves the raw bits to take into account the new compressed size */
1462 ec_enc_shrink(enc, nbCompressedBytes);
1463 }
1464 if (C==2)
1465 {
1466 int effectiveRate;
1467
1468 /* Always use MS for 2.5 ms frames until we can do a better analysis */
1469 if (LM!=0)
1470 dual_stereo = stereo_analysis(st->mode, X, LM, N);
1471
1472 /* Account for coarse energy */
1473 effectiveRate = (8*effectiveBytes - 80)>>LM;
1474
1475 /* effectiveRate in kb/s */
1476 effectiveRate = 2*effectiveRate/5;
1477 if (effectiveRate<35)
1478 intensity = 8;
1479 else if (effectiveRate<50)
1480 intensity = 12;
1481 else if (effectiveRate<68)
1482 intensity = 16;
1483 else if (effectiveRate<84)
1484 intensity = 18;
1485 else if (effectiveRate<102)
1486 intensity = 19;
1487 else if (effectiveRate<130)
1488 intensity = 20;
1489 else
1490 intensity = 100;
1491 intensity = IMIN(st->end,IMAX(st->start, intensity));
1492 }
1493
1494 /* Bit allocation */
1495 ALLOC(fine_quant, st->mode->nbEBands, int);
1496 ALLOC(pulses, st->mode->nbEBands, int);
1497 ALLOC(fine_priority, st->mode->nbEBands, int);
1498
1499 /* bits = packet size - where we are - safety*/
1500 bits = (((opus_int32)nbCompressedBytes*8)<<BITRES) - ec_tell_frac(enc) - 1;
1501 anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
1502 bits -= anti_collapse_rsv;
1503 codedBands = compute_allocation(st->mode, st->start, st->end, offsets, cap,
1504 alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses,
1505 fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands);
1506 st->lastCodedBands = codedBands;
1507
1508 quant_fine_energy(st->mode, st->start, st->end, oldBandE, error, fine_quant, enc, C);
1509
1510#ifdef MEASURE_NORM_MSE
1511 float X0[3000];
1512 float bandE0[60];
1513 c=0; do
1514 for (i=0;i<N;i++)
1515 X0[i+c*N] = X[i+c*N];
1516 while (++c<C);
1517 for (i=0;i<C*st->mode->nbEBands;i++)
1518 bandE0[i] = bandE[i];
1519#endif
1520
1521 /* Residual quantisation */
1522 ALLOC(collapse_masks, C*st->mode->nbEBands, unsigned char);
1523 quant_all_bands(1, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks,
1524 bandE, pulses, shortBlocks, st->spread_decision, dual_stereo, intensity, tf_res,
1525 nbCompressedBytes*(8<<BITRES)-anti_collapse_rsv, balance, enc, LM, codedBands, &st->rng);
1526
1527 if (anti_collapse_rsv > 0)
1528 {
1529 anti_collapse_on = st->consec_transient<2;
1530#ifdef FUZZING
1531 anti_collapse_on = rand()&0x1;
1532#endif
1533 ec_enc_bits(enc, anti_collapse_on, 1);
1534 }
1535 quant_energy_finalise(st->mode, st->start, st->end, oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C);
1536
1537 if (silence)
1538 {
1539 for (i=0;i<C*st->mode->nbEBands;i++)
1540 oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
1541 }
1542
1543#ifdef RESYNTH
1544 /* Re-synthesis of the coded audio if required */
1545 {
1546 celt_sig *out_mem[2];
1547 celt_sig *overlap_mem[2];
1548
1549 log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C);
1550 if (silence)
1551 {
1552 for (i=0;i<C*st->mode->nbEBands;i++)
1553 bandE[i] = 0;
1554 }
1555
1556#ifdef MEASURE_NORM_MSE
1557 measure_norm_mse(st->mode, X, X0, bandE, bandE0, M, N, C);
1558#endif
1559 if (anti_collapse_on)
1560 {
1561 anti_collapse(st->mode, X, collapse_masks, LM, C, N,
1562 st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng);
1563 }
1564
1565 /* Synthesis */
1566 denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M);
1567
1568 OPUS_MOVE(st->syn_mem[0], st->syn_mem[0]+N, MAX_PERIOD);
1569 if (CC==2)
1570 OPUS_MOVE(st->syn_mem[1], st->syn_mem[1]+N, MAX_PERIOD);
1571
1572 c=0; do
1573 for (i=0;i<M*st->mode->eBands[st->start];i++)
1574 freq[c*N+i] = 0;
1575 while (++c<C);
1576 c=0; do
1577 for (i=M*st->mode->eBands[st->end];i<N;i++)
1578 freq[c*N+i] = 0;
1579 while (++c<C);
1580
1581 if (CC==2&&C==1)
1582 {
1583 for (i=0;i<N;i++)
1584 freq[N+i] = freq[i];
1585 }
1586
1587 out_mem[0] = st->syn_mem[0]+MAX_PERIOD;
1588 if (CC==2)
1589 out_mem[1] = st->syn_mem[1]+MAX_PERIOD;
1590
1591 overlap_mem[0] = prefilter_mem+CC*COMBFILTER_MAXPERIOD;
1592 if (CC==2)
1593 overlap_mem[1] = overlap_mem[0] + st->overlap;
1594
1595 compute_inv_mdcts(st->mode, shortBlocks, freq, out_mem, overlap_mem, CC, LM);
1596
1597 c=0; do {
1598 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
1599 st->prefilter_period_old=IMAX(st->prefilter_period_old, COMBFILTER_MINPERIOD);
1600 comb_filter(out_mem[c], out_mem[c], st->prefilter_period_old, st->prefilter_period, st->mode->shortMdctSize,
1601 st->prefilter_gain_old, st->prefilter_gain, st->prefilter_tapset_old, st->prefilter_tapset,
1602 st->mode->window, st->overlap);
1603 if (LM!=0)
1604 comb_filter(out_mem[c]+st->mode->shortMdctSize, out_mem[c]+st->mode->shortMdctSize, st->prefilter_period, pitch_index, N-st->mode->shortMdctSize,
1605 st->prefilter_gain, gain1, st->prefilter_tapset, prefilter_tapset,
1606 st->mode->window, st->mode->overlap);
1607 } while (++c<CC);
1608
1609 deemphasis(out_mem, (opus_val16*)pcm, N, CC, st->upsample, st->mode->preemph, st->preemph_memD);
1610 st->prefilter_period_old = st->prefilter_period;
1611 st->prefilter_gain_old = st->prefilter_gain;
1612 st->prefilter_tapset_old = st->prefilter_tapset;
1613 }
1614#endif
1615
1616 st->prefilter_period = pitch_index;
1617 st->prefilter_gain = gain1;
1618 st->prefilter_tapset = prefilter_tapset;
1619#ifdef RESYNTH
1620 if (LM!=0)
1621 {
1622 st->prefilter_period_old = st->prefilter_period;
1623 st->prefilter_gain_old = st->prefilter_gain;
1624 st->prefilter_tapset_old = st->prefilter_tapset;
1625 }
1626#endif
1627
1628 if (CC==2&&C==1) {
1629 for (i=0;i<st->mode->nbEBands;i++)
1630 oldBandE[st->mode->nbEBands+i]=oldBandE[i];
1631 }
1632
1633 if (!isTransient)
1634 {
1635 for (i=0;i<CC*st->mode->nbEBands;i++)
1636 oldLogE2[i] = oldLogE[i];
1637 for (i=0;i<CC*st->mode->nbEBands;i++)
1638 oldLogE[i] = oldBandE[i];
1639 } else {
1640 for (i=0;i<CC*st->mode->nbEBands;i++)
1641 oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
1642 }
1643 /* In case start or end were to change */
1644 c=0; do
1645 {
1646 for (i=0;i<st->start;i++)
1647 {
1648 oldBandE[c*st->mode->nbEBands+i]=0;
1649 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
1650 }
1651 for (i=st->end;i<st->mode->nbEBands;i++)
1652 {
1653 oldBandE[c*st->mode->nbEBands+i]=0;
1654 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
1655 }
1656 } while (++c<CC);
1657
1658 if (isTransient)
1659 st->consec_transient++;
1660 else
1661 st->consec_transient=0;
1662 st->rng = enc->rng;
1663
1664 /* If there's any room left (can only happen for very high rates),
1665 it's already filled with zeros */
1666 ec_enc_done(enc);
1667
1668#ifdef CUSTOM_MODES
1669 if (st->signalling)
1670 nbCompressedBytes++;
1671#endif
1672
1673 RESTORE_STACK;
1674 if (ec_get_error(enc))
1675 return OPUS_INTERNAL_ERROR;
1676 else
1677 return nbCompressedBytes;
1678}
1679
1680
1681#ifdef CUSTOM_MODES
1682
1683#ifdef FIXED_POINT
1684int opus_custom_encode(CELTEncoder * OPUS_RESTRICT st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1685{
1686 return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
1687}
1688
1689#ifndef DISABLE_FLOAT_API
1690int opus_custom_encode_float(CELTEncoder * OPUS_RESTRICT st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1691{
1692 int j, ret, C, N;
1693 VARDECL(opus_int16, in);
1694 ALLOC_STACK;
1695
1696 if (pcm==NULL)
1697 return OPUS_BAD_ARG;
1698
1699 C = st->channels;
1700 N = frame_size;
1701 ALLOC(in, C*N, opus_int16);
1702
1703 for (j=0;j<C*N;j++)
1704 in[j] = FLOAT2INT16(pcm[j]);
1705
1706 ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
1707#ifdef RESYNTH
1708 for (j=0;j<C*N;j++)
1709 ((float*)pcm)[j]=in[j]*(1.f/32768.f);
1710#endif
1711 RESTORE_STACK;
1712 return ret;
1713}
1714#endif /* DISABLE_FLOAT_API */
1715#else
1716
1717int opus_custom_encode(CELTEncoder * OPUS_RESTRICT st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1718{
1719 int j, ret, C, N;
1720 VARDECL(celt_sig, in);
1721 ALLOC_STACK;
1722
1723 if (pcm==NULL)
1724 return OPUS_BAD_ARG;
1725
1726 C=st->channels;
1727 N=frame_size;
1728 ALLOC(in, C*N, celt_sig);
1729 for (j=0;j<C*N;j++) {
1730 in[j] = SCALEOUT(pcm[j]);
1731 }
1732
1733 ret = celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
1734#ifdef RESYNTH
1735 for (j=0;j<C*N;j++)
1736 ((opus_int16*)pcm)[j] = FLOAT2INT16(in[j]);
1737#endif
1738 RESTORE_STACK;
1739 return ret;
1740}
1741
1742int opus_custom_encode_float(CELTEncoder * OPUS_RESTRICT st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1743{
1744 return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
1745}
1746
1747#endif
1748
1749#endif /* CUSTOM_MODES */
1750
1751int opus_custom_encoder_ctl(CELTEncoder * OPUS_RESTRICT st, int request, ...)
1752{
1753 va_list ap;
1754
1755 va_start(ap, request);
1756 switch (request)
1757 {
1758 case OPUS_SET_COMPLEXITY_REQUEST:
1759 {
1760 int value = va_arg(ap, opus_int32);
1761 if (value<0 || value>10)
1762 goto bad_arg;
1763 st->complexity = value;
1764 }
1765 break;
1766 case CELT_SET_START_BAND_REQUEST:
1767 {
1768 opus_int32 value = va_arg(ap, opus_int32);
1769 if (value<0 || value>=st->mode->nbEBands)
1770 goto bad_arg;
1771 st->start = value;
1772 }
1773 break;
1774 case CELT_SET_END_BAND_REQUEST:
1775 {
1776 opus_int32 value = va_arg(ap, opus_int32);
1777 if (value<1 || value>st->mode->nbEBands)
1778 goto bad_arg;
1779 st->end = value;
1780 }
1781 break;
1782 case CELT_SET_PREDICTION_REQUEST:
1783 {
1784 int value = va_arg(ap, opus_int32);
1785 if (value<0 || value>2)
1786 goto bad_arg;
1787 st->disable_pf = value<=1;
1788 st->force_intra = value==0;
1789 }
1790 break;
1791 case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
1792 {
1793 int value = va_arg(ap, opus_int32);
1794 if (value<0 || value>100)
1795 goto bad_arg;
1796 st->loss_rate = value;
1797 }
1798 break;
1799 case OPUS_SET_VBR_CONSTRAINT_REQUEST:
1800 {
1801 opus_int32 value = va_arg(ap, opus_int32);
1802 st->constrained_vbr = value;
1803 }
1804 break;
1805 case OPUS_SET_VBR_REQUEST:
1806 {
1807 opus_int32 value = va_arg(ap, opus_int32);
1808 st->vbr = value;
1809 }
1810 break;
1811 case OPUS_SET_BITRATE_REQUEST:
1812 {
1813 opus_int32 value = va_arg(ap, opus_int32);
1814 if (value<=500 && value!=OPUS_BITRATE_MAX)
1815 goto bad_arg;
1816 value = IMIN(value, 260000*st->channels);
1817 st->bitrate = value;
1818 }
1819 break;
1820 case CELT_SET_CHANNELS_REQUEST:
1821 {
1822 opus_int32 value = va_arg(ap, opus_int32);
1823 if (value<1 || value>2)
1824 goto bad_arg;
1825 st->stream_channels = value;
1826 }
1827 break;
1828 case OPUS_SET_LSB_DEPTH_REQUEST:
1829 {
1830 opus_int32 value = va_arg(ap, opus_int32);
1831 if (value<8 || value>24)
1832 goto bad_arg;
1833 st->lsb_depth=value;
1834 }
1835 break;
1836 case OPUS_GET_LSB_DEPTH_REQUEST:
1837 {
1838 opus_int32 *value = va_arg(ap, opus_int32*);
1839 *value=st->lsb_depth;
1840 }
1841 break;
1842 case OPUS_RESET_STATE:
1843 {
1844 int i;
1845 opus_val16 *oldBandE, *oldLogE, *oldLogE2;
1846 oldBandE = (opus_val16*)(st->in_mem+st->channels*(2*st->overlap+COMBFILTER_MAXPERIOD));
1847 oldLogE = oldBandE + st->channels*st->mode->nbEBands;
1848 oldLogE2 = oldLogE + st->channels*st->mode->nbEBands;
1849 OPUS_CLEAR((char*)&st->ENCODER_RESET_START,
1850 opus_custom_encoder_get_size(st->mode, st->channels)-
1851 ((char*)&st->ENCODER_RESET_START - (char*)st));
1852 for (i=0;i<st->channels*st->mode->nbEBands;i++)
1853 oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT);
1854 st->vbr_offset = 0;
1855 st->delayedIntra = 1;
1856 st->spread_decision = SPREAD_NORMAL;
1857 st->tonal_average = 256;
1858 st->hf_average = 0;
1859 st->tapset_decision = 0;
1860 }
1861 break;
1862#ifdef CUSTOM_MODES
1863 case CELT_SET_INPUT_CLIPPING_REQUEST:
1864 {
1865 opus_int32 value = va_arg(ap, opus_int32);
1866 st->clip = value;
1867 }
1868 break;
1869#endif
1870 case CELT_SET_SIGNALLING_REQUEST:
1871 {
1872 opus_int32 value = va_arg(ap, opus_int32);
1873 st->signalling = value;
1874 }
1875 break;
1876 case CELT_GET_MODE_REQUEST:
1877 {
1878 const CELTMode ** value = va_arg(ap, const CELTMode**);
1879 if (value==0)
1880 goto bad_arg;
1881 *value=st->mode;
1882 }
1883 break;
1884 case OPUS_GET_FINAL_RANGE_REQUEST:
1885 {
1886 opus_uint32 * value = va_arg(ap, opus_uint32 *);
1887 if (value==0)
1888 goto bad_arg;
1889 *value=st->rng;
1890 }
1891 break;
1892 default:
1893 goto bad_request;
1894 }
1895 va_end(ap);
1896 return OPUS_OK;
1897bad_arg:
1898 va_end(ap);
1899 return OPUS_BAD_ARG;
1900bad_request:
1901 va_end(ap);
1902 return OPUS_UNIMPLEMENTED;
1903}
1904
1905/**********************************************************************/
1906/* */
1907/* DECODER */
1908/* */
1909/**********************************************************************/
1910#define DECODE_BUFFER_SIZE 2048
1911
1912/** Decoder state
1913 @brief Decoder state
1914 */
1915struct OpusCustomDecoder {
1916 const OpusCustomMode *mode;
1917 int overlap;
1918 int channels;
1919 int stream_channels;
1920
1921 int downsample;
1922 int start, end;
1923 int signalling;
1924
1925 /* Everything beyond this point gets cleared on a reset */
1926#define DECODER_RESET_START rng
1927
1928 opus_uint32 rng;
1929 int error;
1930 int last_pitch_index;
1931 int loss_count;
1932 int postfilter_period;
1933 int postfilter_period_old;
1934 opus_val16 postfilter_gain;
1935 opus_val16 postfilter_gain_old;
1936 int postfilter_tapset;
1937 int postfilter_tapset_old;
1938
1939 celt_sig preemph_memD[2];
1940
1941 celt_sig _decode_mem[1]; /* Size = channels*(DECODE_BUFFER_SIZE+mode->overlap) */
1942 /* opus_val16 lpc[], Size = channels*LPC_ORDER */
1943 /* opus_val16 oldEBands[], Size = 2*mode->nbEBands */
1944 /* opus_val16 oldLogE[], Size = 2*mode->nbEBands */
1945 /* opus_val16 oldLogE2[], Size = 2*mode->nbEBands */
1946 /* opus_val16 backgroundLogE[], Size = 2*mode->nbEBands */
1947};
1948
1949int celt_decoder_get_size(int channels)
1950{
1951 const CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
1952 return opus_custom_decoder_get_size(mode, channels);
1953}
1954
1955OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_get_size(const CELTMode *mode, int channels)
1956{
1957 int size = sizeof(struct CELTDecoder)
1958 + (channels*(DECODE_BUFFER_SIZE+mode->overlap)-1)*sizeof(celt_sig)
1959 + channels*LPC_ORDER*sizeof(opus_val16)
1960 + 4*2*mode->nbEBands*sizeof(opus_val16);
1961 return size;
1962}
1963
1964#ifdef CUSTOM_MODES
1965CELTDecoder *opus_custom_decoder_create(const CELTMode *mode, int channels, int *error)
1966{
1967 int ret;
1968 CELTDecoder *st = (CELTDecoder *)opus_alloc(opus_custom_decoder_get_size(mode, channels));
1969 ret = opus_custom_decoder_init(st, mode, channels);
1970 if (ret != OPUS_OK)
1971 {
1972 opus_custom_decoder_destroy(st);
1973 st = NULL;
1974 }
1975 if (error)
1976 *error = ret;
1977 return st;
1978}
1979#endif /* CUSTOM_MODES */
1980
1981int celt_decoder_init(CELTDecoder *st, opus_int32 sampling_rate, int channels)
1982{
1983 int ret;
1984 ret = opus_custom_decoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels);
1985 if (ret != OPUS_OK)
1986 return ret;
1987 st->downsample = resampling_factor(sampling_rate);
1988 if (st->downsample==0)
1989 return OPUS_BAD_ARG;
1990 else
1991 return OPUS_OK;
1992}
1993
1994OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_init(CELTDecoder *st, const CELTMode *mode, int channels)
1995{
1996 if (channels < 0 || channels > 2)
1997 return OPUS_BAD_ARG;
1998
1999 if (st==NULL)
2000 return OPUS_ALLOC_FAIL;
2001
2002 OPUS_CLEAR((char*)st, opus_custom_decoder_get_size(mode, channels));
2003
2004 st->mode = mode;
2005 st->overlap = mode->overlap;
2006 st->stream_channels = st->channels = channels;
2007
2008 st->downsample = 1;
2009 st->start = 0;
2010 st->end = st->mode->effEBands;
2011 st->signalling = 1;
2012
2013 st->loss_count = 0;
2014
2015 opus_custom_decoder_ctl(st, OPUS_RESET_STATE);
2016
2017 return OPUS_OK;
2018}
2019
2020#ifdef CUSTOM_MODES
2021void opus_custom_decoder_destroy(CELTDecoder *st)
2022{
2023 opus_free(st);
2024}
2025#endif /* CUSTOM_MODES */
2026
2027static void celt_decode_lost(CELTDecoder * OPUS_RESTRICT st, opus_val16 * OPUS_RESTRICT pcm, int N, int LM)
2028{
2029 int c;
2030 int pitch_index;
2031 int overlap = st->mode->overlap;
2032 opus_val16 fade = Q15ONE;
2033 int i, len;
2034 const int C = st->channels;
2035 int offset;
2036 celt_sig *out_mem[2];
2037 celt_sig *decode_mem[2];
2038 celt_sig *overlap_mem[2];
2039 opus_val16 *lpc;
2040 opus_val32 *out_syn[2];
2041 opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
2042 SAVE_STACK;
2043
2044 c=0; do {
2045 decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap);
2046 out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD;
2047 overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE;
2048 } while (++c<C);
2049 lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*C);
2050 oldBandE = lpc+C*LPC_ORDER;
2051 oldLogE = oldBandE + 2*st->mode->nbEBands;
2052 oldLogE2 = oldLogE + 2*st->mode->nbEBands;
2053 backgroundLogE = oldLogE2 + 2*st->mode->nbEBands;
2054
2055 out_syn[0] = out_mem[0]+MAX_PERIOD-N;
2056 if (C==2)
2057 out_syn[1] = out_mem[1]+MAX_PERIOD-N;
2058
2059 len = N+st->mode->overlap;
2060
2061 if (st->loss_count >= 5 || st->start!=0)
2062 {
2063 /* Noise-based PLC/CNG */
2064 VARDECL(celt_sig, freq);
2065 VARDECL(celt_norm, X);
2066 VARDECL(celt_ener, bandE);
2067 opus_uint32 seed;
2068 int effEnd;
2069
2070 effEnd = st->end;
2071 if (effEnd > st->mode->effEBands)
2072 effEnd = st->mode->effEBands;
2073
2074 ALLOC(freq, C*N, celt_sig); /**< Interleaved signal MDCTs */
2075 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
2076 ALLOC(bandE, st->mode->nbEBands*C, celt_ener);
2077
2078 if (st->loss_count >= 5)
2079 log2Amp(st->mode, st->start, st->end, bandE, backgroundLogE, C);
2080 else {
2081 /* Energy decay */
2082 opus_val16 decay = st->loss_count==0 ? QCONST16(1.5f, DB_SHIFT) : QCONST16(.5f, DB_SHIFT);
2083 c=0; do
2084 {
2085 for (i=st->start;i<st->end;i++)
2086 oldBandE[c*st->mode->nbEBands+i] -= decay;
2087 } while (++c<C);
2088 log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C);
2089 }
2090 seed = st->rng;
2091 for (c=0;c<C;c++)
2092 {
2093 for (i=0;i<(st->mode->eBands[st->start]<<LM);i++)
2094 X[c*N+i] = 0;
2095 for (i=st->start;i<st->mode->effEBands;i++)
2096 {
2097 int j;
2098 int boffs;
2099 int blen;
2100 boffs = N*c+(st->mode->eBands[i]<<LM);
2101 blen = (st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
2102 for (j=0;j<blen;j++)
2103 {
2104 seed = celt_lcg_rand(seed);
2105 X[boffs+j] = (celt_norm)((opus_int32)seed>>20);
2106 }
2107 renormalise_vector(X+boffs, blen, Q15ONE);
2108 }
2109 for (i=(st->mode->eBands[st->end]<<LM);i<N;i++)
2110 X[c*N+i] = 0;
2111 }
2112 st->rng = seed;
2113
2114 denormalise_bands(st->mode, X, freq, bandE, st->mode->effEBands, C, 1<<LM);
2115
2116 c=0; do
2117 for (i=0;i<st->mode->eBands[st->start]<<LM;i++)
2118 freq[c*N+i] = 0;
2119 while (++c<C);
2120 c=0; do {
2121 int bound = st->mode->eBands[effEnd]<<LM;
2122 if (st->downsample!=1)
2123 bound = IMIN(bound, N/st->downsample);
2124 for (i=bound;i<N;i++)
2125 freq[c*N+i] = 0;
2126 } while (++c<C);
2127 compute_inv_mdcts(st->mode, 0, freq, out_syn, overlap_mem, C, LM);
2128 } else {
2129 /* Pitch-based PLC */
2130 if (st->loss_count == 0)
2131 {
2132 opus_val16 pitch_buf[DECODE_BUFFER_SIZE>>1];
2133 /* Corresponds to a min pitch of 67 Hz. It's possible to save CPU in this
2134 search by using only part of the decode buffer */
2135 int poffset = 720;
2136 pitch_downsample(decode_mem, pitch_buf, DECODE_BUFFER_SIZE, C);
2137 /* Max pitch is 100 samples (480 Hz) */
2138 pitch_search(pitch_buf+((poffset)>>1), pitch_buf, DECODE_BUFFER_SIZE-poffset,
2139 poffset-100, &pitch_index);
2140 pitch_index = poffset-pitch_index;
2141 st->last_pitch_index = pitch_index;
2142 } else {
2143 pitch_index = st->last_pitch_index;
2144 fade = QCONST16(.8f,15);
2145 }
2146
2147 c=0; do {
2148 VARDECL(opus_val32, e);
2149 opus_val16 exc[MAX_PERIOD];
2150 opus_val32 ac[LPC_ORDER+1];
2151 opus_val16 decay = 1;
2152 opus_val32 S1=0;
2153 opus_val16 mem[LPC_ORDER]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
2154
2155 ALLOC(e, MAX_PERIOD+2*st->mode->overlap, opus_val32);
2156
2157 offset = MAX_PERIOD-pitch_index;
2158 for (i=0;i<MAX_PERIOD;i++)
2159 exc[i] = ROUND16(out_mem[c][i], SIG_SHIFT);
2160
2161 if (st->loss_count == 0)
2162 {
2163 _celt_autocorr(exc, ac, st->mode->window, st->mode->overlap,
2164 LPC_ORDER, MAX_PERIOD);
2165
2166 /* Noise floor -40 dB */
2167#ifdef FIXED_POINT
2168 ac[0] += SHR32(ac[0],13);
2169#else
2170 ac[0] *= 1.0001f;
2171#endif
2172 /* Lag windowing */
2173 for (i=1;i<=LPC_ORDER;i++)
2174 {
2175 /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
2176#ifdef FIXED_POINT
2177 ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
2178#else
2179 ac[i] -= ac[i]*(.008f*i)*(.008f*i);
2180#endif
2181 }
2182
2183 _celt_lpc(lpc+c*LPC_ORDER, ac, LPC_ORDER);
2184 }
2185 for (i=0;i<LPC_ORDER;i++)
2186 mem[i] = ROUND16(out_mem[c][MAX_PERIOD-1-i], SIG_SHIFT);
2187 celt_fir(exc, lpc+c*LPC_ORDER, exc, MAX_PERIOD, LPC_ORDER, mem);
2188 /*for (i=0;i<MAX_PERIOD;i++)printf("%d ", exc[i]); printf("\n");*/
2189 /* Check if the waveform is decaying (and if so how fast) */
2190 {
2191 opus_val32 E1=1, E2=1;
2192 int period;
2193 if (pitch_index <= MAX_PERIOD/2)
2194 period = pitch_index;
2195 else
2196 period = MAX_PERIOD/2;
2197 for (i=0;i<period;i++)
2198 {
2199 E1 += SHR32(MULT16_16(exc[MAX_PERIOD-period+i],exc[MAX_PERIOD-period+i]),8);
2200 E2 += SHR32(MULT16_16(exc[MAX_PERIOD-2*period+i],exc[MAX_PERIOD-2*period+i]),8);
2201 }
2202 if (E1 > E2)
2203 E1 = E2;
2204 decay = celt_sqrt(frac_div32(SHR32(E1,1),E2));
2205 }
2206
2207 /* Copy excitation, taking decay into account */
2208 for (i=0;i<len+st->mode->overlap;i++)
2209 {
2210 opus_val16 tmp;
2211 if (offset+i >= MAX_PERIOD)
2212 {
2213 offset -= pitch_index;
2214 decay = MULT16_16_Q15(decay, decay);
2215 }
2216 e[i] = SHL32(EXTEND32(MULT16_16_Q15(decay, exc[offset+i])), SIG_SHIFT);
2217 tmp = ROUND16(out_mem[c][offset+i],SIG_SHIFT);
2218 S1 += SHR32(MULT16_16(tmp,tmp),8);
2219 }
2220 for (i=0;i<LPC_ORDER;i++)
2221 mem[i] = ROUND16(out_mem[c][MAX_PERIOD-1-i], SIG_SHIFT);
2222 for (i=0;i<len+st->mode->overlap;i++)
2223 e[i] = MULT16_32_Q15(fade, e[i]);
2224 celt_iir(e, lpc+c*LPC_ORDER, e, len+st->mode->overlap, LPC_ORDER, mem);
2225
2226 {
2227 opus_val32 S2=0;
2228 for (i=0;i<len+overlap;i++)
2229 {
2230 opus_val16 tmp = ROUND16(e[i],SIG_SHIFT);
2231 S2 += SHR32(MULT16_16(tmp,tmp),8);
2232 }
2233 /* This checks for an "explosion" in the synthesis */
2234#ifdef FIXED_POINT
2235 if (!(S1 > SHR32(S2,2)))
2236#else
2237 /* Float test is written this way to catch NaNs at the same time */
2238 if (!(S1 > 0.2f*S2))
2239#endif
2240 {
2241 for (i=0;i<len+overlap;i++)
2242 e[i] = 0;
2243 } else if (S1 < S2)
2244 {
2245 opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1));
2246 for (i=0;i<len+overlap;i++)
2247 e[i] = MULT16_32_Q15(ratio, e[i]);
2248 }
2249 }
2250
2251 /* Apply post-filter to the MDCT overlap of the previous frame */
2252 comb_filter(out_mem[c]+MAX_PERIOD, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap,
2253 st->postfilter_gain, st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset,
2254 NULL, 0);
2255
2256 for (i=0;i<MAX_PERIOD+st->mode->overlap-N;i++)
2257 out_mem[c][i] = out_mem[c][N+i];
2258
2259 /* Apply TDAC to the concealed audio so that it blends with the
2260 previous and next frames */
2261 for (i=0;i<overlap/2;i++)
2262 {
2263 opus_val32 tmp;
2264 tmp = MULT16_32_Q15(st->mode->window[i], e[N+overlap-1-i]) +
2265 MULT16_32_Q15(st->mode->window[overlap-i-1], e[N+i ]);
2266 out_mem[c][MAX_PERIOD+i] = MULT16_32_Q15(st->mode->window[overlap-i-1], tmp);
2267 out_mem[c][MAX_PERIOD+overlap-i-1] = MULT16_32_Q15(st->mode->window[i], tmp);
2268 }
2269 for (i=0;i<N;i++)
2270 out_mem[c][MAX_PERIOD-N+i] = e[i];
2271
2272 /* Apply pre-filter to the MDCT overlap for the next frame (post-filter will be applied then) */
2273 comb_filter(e, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap,
2274 -st->postfilter_gain, -st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset,
2275 NULL, 0);
2276 for (i=0;i<overlap;i++)
2277 out_mem[c][MAX_PERIOD+i] = e[i];
2278 } while (++c<C);
2279 }
2280
2281 deemphasis(out_syn, pcm, N, C, st->downsample, st->mode->preemph, st->preemph_memD);
2282
2283 st->loss_count++;
2284
2285 RESTORE_STACK;
2286}
2287
2288int celt_decode_with_ec(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_val16 * OPUS_RESTRICT pcm, int frame_size, ec_dec *dec)
2289{
2290 int c, i, N;
2291 int spread_decision;
2292 opus_int32 bits;
2293 ec_dec _dec;
2294 VARDECL(celt_sig, freq);
2295 VARDECL(celt_norm, X);
2296 VARDECL(celt_ener, bandE);
2297 VARDECL(int, fine_quant);
2298 VARDECL(int, pulses);
2299 VARDECL(int, cap);
2300 VARDECL(int, offsets);
2301 VARDECL(int, fine_priority);
2302 VARDECL(int, tf_res);
2303 VARDECL(unsigned char, collapse_masks);
2304 celt_sig *out_mem[2];
2305 celt_sig *decode_mem[2];
2306 celt_sig *overlap_mem[2];
2307 celt_sig *out_syn[2];
2308 opus_val16 *lpc;
2309 opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
2310
2311 int shortBlocks;
2312 int isTransient;
2313 int intra_ener;
2314 const int CC = st->channels;
2315 int LM, M;
2316 int effEnd;
2317 int codedBands;
2318 int alloc_trim;
2319 int postfilter_pitch;
2320 opus_val16 postfilter_gain;
2321 int intensity=0;
2322 int dual_stereo=0;
2323 opus_int32 total_bits;
2324 opus_int32 balance;
2325 opus_int32 tell;
2326 int dynalloc_logp;
2327 int postfilter_tapset;
2328 int anti_collapse_rsv;
2329 int anti_collapse_on=0;
2330 int silence;
2331 int C = st->stream_channels;
2332 ALLOC_STACK;
2333
2334 frame_size *= st->downsample;
2335
2336 c=0; do {
2337 decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap);
2338 out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD;
2339 overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE;
2340 } while (++c<CC);
2341 lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*CC);
2342 oldBandE = lpc+CC*LPC_ORDER;
2343 oldLogE = oldBandE + 2*st->mode->nbEBands;
2344 oldLogE2 = oldLogE + 2*st->mode->nbEBands;
2345 backgroundLogE = oldLogE2 + 2*st->mode->nbEBands;
2346
2347#ifdef CUSTOM_MODES
2348 if (st->signalling && data!=NULL)
2349 {
2350 int data0=data[0];
2351 /* Convert "standard mode" to Opus header */
2352 if (st->mode->Fs==48000 && st->mode->shortMdctSize==120)
2353 {
2354 data0 = fromOpus(data0);
2355 if (data0<0)
2356 return OPUS_INVALID_PACKET;
2357 }
2358 st->end = IMAX(1, st->mode->effEBands-2*(data0>>5));
2359 LM = (data0>>3)&0x3;
2360 C = 1 + ((data0>>2)&0x1);
2361 data++;
2362 len--;
2363 if (LM>st->mode->maxLM)
2364 return OPUS_INVALID_PACKET;
2365 if (frame_size < st->mode->shortMdctSize<<LM)
2366 return OPUS_BUFFER_TOO_SMALL;
2367 else
2368 frame_size = st->mode->shortMdctSize<<LM;
2369 } else {
2370#else
2371 {
2372#endif
2373 for (LM=0;LM<=st->mode->maxLM;LM++)
2374 if (st->mode->shortMdctSize<<LM==frame_size)
2375 break;
2376 if (LM>st->mode->maxLM)
2377 return OPUS_BAD_ARG;
2378 }
2379 M=1<<LM;
2380
2381 if (len<0 || len>1275 || pcm==NULL)
2382 return OPUS_BAD_ARG;
2383
2384 N = M*st->mode->shortMdctSize;
2385
2386 effEnd = st->end;
2387 if (effEnd > st->mode->effEBands)
2388 effEnd = st->mode->effEBands;
2389
2390 ALLOC(freq, IMAX(CC,C)*N, celt_sig); /**< Interleaved signal MDCTs */
2391 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
2392 ALLOC(bandE, st->mode->nbEBands*C, celt_ener);
2393 c=0; do
2394 for (i=0;i<M*st->mode->eBands[st->start];i++)
2395 X[c*N+i] = 0;
2396 while (++c<C);
2397 c=0; do
2398 for (i=M*st->mode->eBands[effEnd];i<N;i++)
2399 X[c*N+i] = 0;
2400 while (++c<C);
2401
2402 if (data == NULL || len<=1)
2403 {
2404 celt_decode_lost(st, pcm, N, LM);
2405 RESTORE_STACK;
2406 return frame_size/st->downsample;
2407 }
2408
2409 if (dec == NULL)
2410 {
2411 ec_dec_init(&_dec,(unsigned char*)data,len);
2412 dec = &_dec;
2413 }
2414
2415 if (C==1)
2416 {
2417 for (i=0;i<st->mode->nbEBands;i++)
2418 oldBandE[i]=MAX16(oldBandE[i],oldBandE[st->mode->nbEBands+i]);
2419 }
2420
2421 total_bits = len*8;
2422 tell = ec_tell(dec);
2423
2424 if (tell >= total_bits)
2425 silence = 1;
2426 else if (tell==1)
2427 silence = ec_dec_bit_logp(dec, 15);
2428 else
2429 silence = 0;
2430 if (silence)
2431 {
2432 /* Pretend we've read all the remaining bits */
2433 tell = len*8;
2434 dec->nbits_total+=tell-ec_tell(dec);
2435 }
2436
2437 postfilter_gain = 0;
2438 postfilter_pitch = 0;
2439 postfilter_tapset = 0;
2440 if (st->start==0 && tell+16 <= total_bits)
2441 {
2442 if(ec_dec_bit_logp(dec, 1))
2443 {
2444 int qg, octave;
2445 octave = ec_dec_uint(dec, 6);
2446 postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1;
2447 qg = ec_dec_bits(dec, 3);
2448 if (ec_tell(dec)+2<=total_bits)
2449 postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2);
2450 postfilter_gain = QCONST16(.09375f,15)*(qg+1);
2451 }
2452 tell = ec_tell(dec);
2453 }
2454
2455 if (LM > 0 && tell+3 <= total_bits)
2456 {
2457 isTransient = ec_dec_bit_logp(dec, 3);
2458 tell = ec_tell(dec);
2459 }
2460 else
2461 isTransient = 0;
2462
2463 if (isTransient)
2464 shortBlocks = M;
2465 else
2466 shortBlocks = 0;
2467
2468 /* Decode the global flags (first symbols in the stream) */
2469 intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0;
2470 /* Get band energies */
2471 unquant_coarse_energy(st->mode, st->start, st->end, oldBandE,
2472 intra_ener, dec, C, LM);
2473
2474 ALLOC(tf_res, st->mode->nbEBands, int);
2475 tf_decode(st->start, st->end, isTransient, tf_res, LM, dec);
2476
2477 tell = ec_tell(dec);
2478 spread_decision = SPREAD_NORMAL;
2479 if (tell+4 <= total_bits)
2480 spread_decision = ec_dec_icdf(dec, spread_icdf, 5);
2481
2482 ALLOC(pulses, st->mode->nbEBands, int);
2483 ALLOC(cap, st->mode->nbEBands, int);
2484 ALLOC(offsets, st->mode->nbEBands, int);
2485 ALLOC(fine_priority, st->mode->nbEBands, int);
2486
2487 init_caps(st->mode,cap,LM,C);
2488
2489 dynalloc_logp = 6;
2490 total_bits<<=BITRES;
2491 tell = ec_tell_frac(dec);
2492 for (i=st->start;i<st->end;i++)
2493 {
2494 int width, quanta;
2495 int dynalloc_loop_logp;
2496 int boost;
2497 width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
2498 /* quanta is 6 bits, but no more than 1 bit/sample
2499 and no less than 1/8 bit/sample */
2500 quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
2501 dynalloc_loop_logp = dynalloc_logp;
2502 boost = 0;
2503 while (tell+(dynalloc_loop_logp<<BITRES) < total_bits && boost < cap[i])
2504 {
2505 int flag;
2506 flag = ec_dec_bit_logp(dec, dynalloc_loop_logp);
2507 tell = ec_tell_frac(dec);
2508 if (!flag)
2509 break;
2510 boost += quanta;
2511 total_bits -= quanta;
2512 dynalloc_loop_logp = 1;
2513 }
2514 offsets[i] = boost;
2515 /* Making dynalloc more likely */
2516 if (boost>0)
2517 dynalloc_logp = IMAX(2, dynalloc_logp-1);
2518 }
2519
2520 ALLOC(fine_quant, st->mode->nbEBands, int);
2521 alloc_trim = tell+(6<<BITRES) <= total_bits ?
2522 ec_dec_icdf(dec, trim_icdf, 7) : 5;
2523
2524 bits = (((opus_int32)len*8)<<BITRES) - ec_tell_frac(dec) - 1;
2525 anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
2526 bits -= anti_collapse_rsv;
2527 codedBands = compute_allocation(st->mode, st->start, st->end, offsets, cap,
2528 alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses,
2529 fine_quant, fine_priority, C, LM, dec, 0, 0);
2530
2531 unquant_fine_energy(st->mode, st->start, st->end, oldBandE, fine_quant, dec, C);
2532
2533 /* Decode fixed codebook */
2534 ALLOC(collapse_masks, C*st->mode->nbEBands, unsigned char);
2535 quant_all_bands(0, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks,
2536 NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res,
2537 len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng);
2538
2539 if (anti_collapse_rsv > 0)
2540 {
2541 anti_collapse_on = ec_dec_bits(dec, 1);
2542 }
2543
2544 unquant_energy_finalise(st->mode, st->start, st->end, oldBandE,
2545 fine_quant, fine_priority, len*8-ec_tell(dec), dec, C);
2546
2547 if (anti_collapse_on)
2548 anti_collapse(st->mode, X, collapse_masks, LM, C, N,
2549 st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng);
2550
2551 log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C);
2552
2553 if (silence)
2554 {
2555 for (i=0;i<C*st->mode->nbEBands;i++)
2556 {
2557 bandE[i] = 0;
2558 oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
2559 }
2560 }
2561 /* Synthesis */
2562 denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M);
2563
2564 OPUS_MOVE(decode_mem[0], decode_mem[0]+N, DECODE_BUFFER_SIZE-N);
2565 if (CC==2)
2566 OPUS_MOVE(decode_mem[1], decode_mem[1]+N, DECODE_BUFFER_SIZE-N);
2567
2568 c=0; do
2569 for (i=0;i<M*st->mode->eBands[st->start];i++)
2570 freq[c*N+i] = 0;
2571 while (++c<C);
2572 c=0; do {
2573 int bound = M*st->mode->eBands[effEnd];
2574 if (st->downsample!=1)
2575 bound = IMIN(bound, N/st->downsample);
2576 for (i=bound;i<N;i++)
2577 freq[c*N+i] = 0;
2578 } while (++c<C);
2579
2580 out_syn[0] = out_mem[0]+MAX_PERIOD-N;
2581 if (CC==2)
2582 out_syn[1] = out_mem[1]+MAX_PERIOD-N;
2583
2584 if (CC==2&&C==1)
2585 {
2586 for (i=0;i<N;i++)
2587 freq[N+i] = freq[i];
2588 }
2589 if (CC==1&&C==2)
2590 {
2591 for (i=0;i<N;i++)
2592 freq[i] = HALF32(ADD32(freq[i],freq[N+i]));
2593 }
2594
2595 /* Compute inverse MDCTs */
2596 compute_inv_mdcts(st->mode, shortBlocks, freq, out_syn, overlap_mem, CC, LM);
2597
2598 c=0; do {
2599 st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD);
2600 st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD);
2601 comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, st->mode->shortMdctSize,
2602 st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset,
2603 st->mode->window, st->overlap);
2604 if (LM!=0)
2605 comb_filter(out_syn[c]+st->mode->shortMdctSize, out_syn[c]+st->mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-st->mode->shortMdctSize,
2606 st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset,
2607 st->mode->window, st->mode->overlap);
2608
2609 } while (++c<CC);
2610 st->postfilter_period_old = st->postfilter_period;
2611 st->postfilter_gain_old = st->postfilter_gain;
2612 st->postfilter_tapset_old = st->postfilter_tapset;
2613 st->postfilter_period = postfilter_pitch;
2614 st->postfilter_gain = postfilter_gain;
2615 st->postfilter_tapset = postfilter_tapset;
2616 if (LM!=0)
2617 {
2618 st->postfilter_period_old = st->postfilter_period;
2619 st->postfilter_gain_old = st->postfilter_gain;
2620 st->postfilter_tapset_old = st->postfilter_tapset;
2621 }
2622
2623 if (C==1) {
2624 for (i=0;i<st->mode->nbEBands;i++)
2625 oldBandE[st->mode->nbEBands+i]=oldBandE[i];
2626 }
2627
2628 /* In case start or end were to change */
2629 if (!isTransient)
2630 {
2631 for (i=0;i<2*st->mode->nbEBands;i++)
2632 oldLogE2[i] = oldLogE[i];
2633 for (i=0;i<2*st->mode->nbEBands;i++)
2634 oldLogE[i] = oldBandE[i];
2635 for (i=0;i<2*st->mode->nbEBands;i++)
2636 backgroundLogE[i] = MIN16(backgroundLogE[i] + M*QCONST16(0.001f,DB_SHIFT), oldBandE[i]);
2637 } else {
2638 for (i=0;i<2*st->mode->nbEBands;i++)
2639 oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
2640 }
2641 c=0; do
2642 {
2643 for (i=0;i<st->start;i++)
2644 {
2645 oldBandE[c*st->mode->nbEBands+i]=0;
2646 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
2647 }
2648 for (i=st->end;i<st->mode->nbEBands;i++)
2649 {
2650 oldBandE[c*st->mode->nbEBands+i]=0;
2651 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
2652 }
2653 } while (++c<2);
2654 st->rng = dec->rng;
2655
2656 deemphasis(out_syn, pcm, N, CC, st->downsample, st->mode->preemph, st->preemph_memD);
2657 st->loss_count = 0;
2658 RESTORE_STACK;
2659 if (ec_tell(dec) > 8*len)
2660 return OPUS_INTERNAL_ERROR;
2661 if(ec_get_error(dec))
2662 st->error = 1;
2663 return frame_size/st->downsample;
2664}
2665
2666
2667#ifdef CUSTOM_MODES
2668
2669#ifdef FIXED_POINT
2670int opus_custom_decode(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int16 * OPUS_RESTRICT pcm, int frame_size)
2671{
2672 return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL);
2673}
2674
2675#ifndef DISABLE_FLOAT_API
2676int opus_custom_decode_float(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, float * OPUS_RESTRICT pcm, int frame_size)
2677{
2678 int j, ret, C, N;
2679 VARDECL(opus_int16, out);
2680 ALLOC_STACK;
2681
2682 if (pcm==NULL)
2683 return OPUS_BAD_ARG;
2684
2685 C = st->channels;
2686 N = frame_size;
2687
2688 ALLOC(out, C*N, opus_int16);
2689 ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL);
2690 if (ret>0)
2691 for (j=0;j<C*ret;j++)
2692 pcm[j]=out[j]*(1.f/32768.f);
2693
2694 RESTORE_STACK;
2695 return ret;
2696}
2697#endif /* DISABLE_FLOAT_API */
2698
2699#else
2700
2701int opus_custom_decode_float(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, float * OPUS_RESTRICT pcm, int frame_size)
2702{
2703 return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL);
2704}
2705
2706int opus_custom_decode(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int16 * OPUS_RESTRICT pcm, int frame_size)
2707{
2708 int j, ret, C, N;
2709 VARDECL(celt_sig, out);
2710 ALLOC_STACK;
2711
2712 if (pcm==NULL)
2713 return OPUS_BAD_ARG;
2714
2715 C = st->channels;
2716 N = frame_size;
2717 ALLOC(out, C*N, celt_sig);
2718
2719 ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL);
2720
2721 if (ret>0)
2722 for (j=0;j<C*ret;j++)
2723 pcm[j] = FLOAT2INT16 (out[j]);
2724
2725 RESTORE_STACK;
2726 return ret;
2727}
2728
2729#endif
2730#endif /* CUSTOM_MODES */
2731
2732int opus_custom_decoder_ctl(CELTDecoder * OPUS_RESTRICT st, int request, ...)
2733{
2734 va_list ap;
2735
2736 va_start(ap, request);
2737 switch (request)
2738 {
2739 case CELT_SET_START_BAND_REQUEST:
2740 {
2741 opus_int32 value = va_arg(ap, opus_int32);
2742 if (value<0 || value>=st->mode->nbEBands)
2743 goto bad_arg;
2744 st->start = value;
2745 }
2746 break;
2747 case CELT_SET_END_BAND_REQUEST:
2748 {
2749 opus_int32 value = va_arg(ap, opus_int32);
2750 if (value<1 || value>st->mode->nbEBands)
2751 goto bad_arg;
2752 st->end = value;
2753 }
2754 break;
2755 case CELT_SET_CHANNELS_REQUEST:
2756 {
2757 opus_int32 value = va_arg(ap, opus_int32);
2758 if (value<1 || value>2)
2759 goto bad_arg;
2760 st->stream_channels = value;
2761 }
2762 break;
2763 case CELT_GET_AND_CLEAR_ERROR_REQUEST:
2764 {
2765 opus_int32 *value = va_arg(ap, opus_int32*);
2766 if (value==NULL)
2767 goto bad_arg;
2768 *value=st->error;
2769 st->error = 0;
2770 }
2771 break;
2772 case OPUS_GET_LOOKAHEAD_REQUEST:
2773 {
2774 opus_int32 *value = va_arg(ap, opus_int32*);
2775 if (value==NULL)
2776 goto bad_arg;
2777 *value = st->overlap/st->downsample;
2778 }
2779 break;
2780 case OPUS_RESET_STATE:
2781 {
2782 int i;
2783 opus_val16 *lpc, *oldBandE, *oldLogE, *oldLogE2;
2784 lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*st->channels);
2785 oldBandE = lpc+st->channels*LPC_ORDER;
2786 oldLogE = oldBandE + 2*st->mode->nbEBands;
2787 oldLogE2 = oldLogE + 2*st->mode->nbEBands;
2788 OPUS_CLEAR((char*)&st->DECODER_RESET_START,
2789 opus_custom_decoder_get_size(st->mode, st->channels)-
2790 ((char*)&st->DECODER_RESET_START - (char*)st));
2791 for (i=0;i<2*st->mode->nbEBands;i++)
2792 oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT);
2793 }
2794 break;
2795 case OPUS_GET_PITCH_REQUEST:
2796 {
2797 opus_int32 *value = va_arg(ap, opus_int32*);
2798 if (value==NULL)
2799 goto bad_arg;
2800 *value = st->postfilter_period;
2801 }
2802 break;
2803#ifdef OPUS_BUILD
2804 case CELT_GET_MODE_REQUEST:
2805 {
2806 const CELTMode ** value = va_arg(ap, const CELTMode**);
2807 if (value==0)
2808 goto bad_arg;
2809 *value=st->mode;
2810 }
2811 break;
2812 case CELT_SET_SIGNALLING_REQUEST:
2813 {
2814 opus_int32 value = va_arg(ap, opus_int32);
2815 st->signalling = value;
2816 }
2817 break;
2818 case OPUS_GET_FINAL_RANGE_REQUEST:
2819 {
2820 opus_uint32 * value = va_arg(ap, opus_uint32 *);
2821 if (value==0)
2822 goto bad_arg;
2823 *value=st->rng;
2824 }
2825 break;
2826#endif
2827 default:
2828 goto bad_request;
2829 }
2830 va_end(ap);
2831 return OPUS_OK;
2832bad_arg:
2833 va_end(ap);
2834 return OPUS_BAD_ARG;
2835bad_request:
2836 va_end(ap);
2837 return OPUS_UNIMPLEMENTED;
2838}
2839
2840
2841
2842const char *opus_strerror(int error)
2843{
2844 static const char * const error_strings[8] = {
2845 "success",
2846 "invalid argument",
2847 "buffer too small",
2848 "internal error",
2849 "corrupted stream",
2850 "request not implemented",
2851 "invalid state",
2852 "memory allocation failed"
2853 };
2854 if (error > 0 || error < -7)
2855 return "unknown error";
2856 else
2857 return error_strings[-error];
2858}
2859
2860const char *opus_get_version_string(void)
2861{
2862 return "libopus " OPUS_VERSION
2863#ifdef FIXED_POINT
2864 "-fixed"
2865#endif
2866#ifdef FUZZING
2867 "-fuzzing"
2868#endif
2869 ;
2870}
diff --git a/lib/rbcodec/codecs/libopus/celt/celt.h b/lib/rbcodec/codecs/libopus/celt/celt.h
new file mode 100644
index 0000000000..218cd883df
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/celt.h
@@ -0,0 +1,117 @@
1/* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2009 Xiph.Org Foundation
3 Copyright (c) 2008 Gregory Maxwell
4 Written by Jean-Marc Valin and Gregory Maxwell */
5/**
6 @file celt.h
7 @brief Contains all the functions for encoding and decoding audio
8 */
9
10/*
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions
13 are met:
14
15 - Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
17
18 - Redistributions in binary form must reproduce the above copyright
19 notice, this list of conditions and the following disclaimer in the
20 documentation and/or other materials provided with the distribution.
21
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
26 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33*/
34
35#ifndef CELT_H
36#define CELT_H
37
38#include "opus_types.h"
39#include "opus_defines.h"
40#include "opus_custom.h"
41#include "entenc.h"
42#include "entdec.h"
43#include "arch.h"
44
45#ifdef __cplusplus
46extern "C" {
47#endif
48
49#define CELTEncoder OpusCustomEncoder
50#define CELTDecoder OpusCustomDecoder
51#define CELTMode OpusCustomMode
52
53#define _celt_check_mode_ptr_ptr(ptr) ((ptr) + ((ptr) - (const CELTMode**)(ptr)))
54
55/* Encoder/decoder Requests */
56
57#define CELT_SET_PREDICTION_REQUEST 10002
58/** Controls the use of interframe prediction.
59 0=Independent frames
60 1=Short term interframe prediction allowed
61 2=Long term prediction allowed
62 */
63#define CELT_SET_PREDICTION(x) CELT_SET_PREDICTION_REQUEST, __opus_check_int(x)
64
65#define CELT_SET_INPUT_CLIPPING_REQUEST 10004
66#define CELT_SET_INPUT_CLIPPING(x) CELT_SET_INPUT_CLIPPING_REQUEST, __opus_check_int(x)
67
68#define CELT_GET_AND_CLEAR_ERROR_REQUEST 10007
69#define CELT_GET_AND_CLEAR_ERROR(x) CELT_GET_AND_CLEAR_ERROR_REQUEST, __opus_check_int_ptr(x)
70
71#define CELT_SET_CHANNELS_REQUEST 10008
72#define CELT_SET_CHANNELS(x) CELT_SET_CHANNELS_REQUEST, __opus_check_int(x)
73
74
75/* Internal */
76#define CELT_SET_START_BAND_REQUEST 10010
77#define CELT_SET_START_BAND(x) CELT_SET_START_BAND_REQUEST, __opus_check_int(x)
78
79#define CELT_SET_END_BAND_REQUEST 10012
80#define CELT_SET_END_BAND(x) CELT_SET_END_BAND_REQUEST, __opus_check_int(x)
81
82#define CELT_GET_MODE_REQUEST 10015
83/** Get the CELTMode used by an encoder or decoder */
84#define CELT_GET_MODE(x) CELT_GET_MODE_REQUEST, _celt_check_mode_ptr_ptr(x)
85
86#define CELT_SET_SIGNALLING_REQUEST 10016
87#define CELT_SET_SIGNALLING(x) CELT_SET_SIGNALLING_REQUEST, __opus_check_int(x)
88
89
90
91/* Encoder stuff */
92
93int celt_encoder_get_size(int channels);
94
95int celt_encode_with_ec(OpusCustomEncoder * OPUS_RESTRICT st, const opus_val16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc);
96
97int celt_encoder_init(CELTEncoder *st, opus_int32 sampling_rate, int channels);
98
99
100
101/* Decoder stuff */
102
103int celt_decoder_get_size(int channels);
104
105
106int celt_decoder_init(CELTDecoder *st, opus_int32 sampling_rate, int channels);
107
108int celt_decode_with_ec(OpusCustomDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_val16 * OPUS_RESTRICT pcm, int frame_size, ec_dec *dec);
109
110#define celt_encoder_ctl opus_custom_encoder_ctl
111#define celt_decoder_ctl opus_custom_decoder_ctl
112
113#ifdef __cplusplus
114}
115#endif
116
117#endif /* CELT_H */
diff --git a/lib/rbcodec/codecs/libopus/celt/celt_lpc.c b/lib/rbcodec/codecs/libopus/celt/celt_lpc.c
new file mode 100644
index 0000000000..66aed1de09
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/celt_lpc.c
@@ -0,0 +1,188 @@
1/* Copyright (c) 2009-2010 Xiph.Org Foundation
2 Written by Jean-Marc Valin */
3/*
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#ifdef HAVE_CONFIG_H
29#include "opus_config.h"
30#endif
31
32#include "celt_lpc.h"
33#include "stack_alloc.h"
34#include "mathops.h"
35
36void _celt_lpc(
37 opus_val16 *_lpc, /* out: [0...p-1] LPC coefficients */
38const opus_val32 *ac, /* in: [0...p] autocorrelation values */
39int p
40)
41{
42 int i, j;
43 opus_val32 r;
44 opus_val32 error = ac[0];
45#ifdef FIXED_POINT
46 opus_val32 lpc[LPC_ORDER];
47#else
48 float *lpc = _lpc;
49#endif
50
51 for (i = 0; i < p; i++)
52 lpc[i] = 0;
53 if (ac[0] != 0)
54 {
55 for (i = 0; i < p; i++) {
56 /* Sum up this iteration's reflection coefficient */
57 opus_val32 rr = 0;
58 for (j = 0; j < i; j++)
59 rr += MULT32_32_Q31(lpc[j],ac[i - j]);
60 rr += SHR32(ac[i + 1],3);
61 r = -frac_div32(SHL32(rr,3), error);
62 /* Update LPC coefficients and total error */
63 lpc[i] = SHR32(r,3);
64 for (j = 0; j < (i+1)>>1; j++)
65 {
66 opus_val32 tmp1, tmp2;
67 tmp1 = lpc[j];
68 tmp2 = lpc[i-1-j];
69 lpc[j] = tmp1 + MULT32_32_Q31(r,tmp2);
70 lpc[i-1-j] = tmp2 + MULT32_32_Q31(r,tmp1);
71 }
72
73 error = error - MULT32_32_Q31(MULT32_32_Q31(r,r),error);
74 /* Bail out once we get 30 dB gain */
75#ifdef FIXED_POINT
76 if (error<SHR32(ac[0],10))
77 break;
78#else
79 if (error<.001f*ac[0])
80 break;
81#endif
82 }
83 }
84#ifdef FIXED_POINT
85 for (i=0;i<p;i++)
86 _lpc[i] = ROUND16(lpc[i],16);
87#endif
88}
89
90void celt_fir(const opus_val16 *x,
91 const opus_val16 *num,
92 opus_val16 *y,
93 int N,
94 int ord,
95 opus_val16 *mem)
96{
97 int i,j;
98
99 for (i=0;i<N;i++)
100 {
101 opus_val32 sum = SHL32(EXTEND32(x[i]), SIG_SHIFT);
102 for (j=0;j<ord;j++)
103 {
104 sum += MULT16_16(num[j],mem[j]);
105 }
106 for (j=ord-1;j>=1;j--)
107 {
108 mem[j]=mem[j-1];
109 }
110 mem[0] = x[i];
111 y[i] = ROUND16(sum, SIG_SHIFT);
112 }
113}
114
115void celt_iir(const opus_val32 *x,
116 const opus_val16 *den,
117 opus_val32 *y,
118 int N,
119 int ord,
120 opus_val16 *mem)
121{
122 int i,j;
123 for (i=0;i<N;i++)
124 {
125 opus_val32 sum = x[i];
126 for (j=0;j<ord;j++)
127 {
128 sum -= MULT16_16(den[j],mem[j]);
129 }
130 for (j=ord-1;j>=1;j--)
131 {
132 mem[j]=mem[j-1];
133 }
134 mem[0] = ROUND16(sum,SIG_SHIFT);
135 y[i] = sum;
136 }
137}
138
139void _celt_autocorr(
140 const opus_val16 *x, /* in: [0...n-1] samples x */
141 opus_val32 *ac, /* out: [0...lag-1] ac values */
142 const opus_val16 *window,
143 int overlap,
144 int lag,
145 int n
146 )
147{
148 opus_val32 d;
149 int i;
150 VARDECL(opus_val16, xx);
151 SAVE_STACK;
152 ALLOC(xx, n, opus_val16);
153 celt_assert(n>0);
154 celt_assert(overlap>=0);
155 for (i=0;i<n;i++)
156 xx[i] = x[i];
157 for (i=0;i<overlap;i++)
158 {
159 xx[i] = MULT16_16_Q15(x[i],window[i]);
160 xx[n-i-1] = MULT16_16_Q15(x[n-i-1],window[i]);
161 }
162#ifdef FIXED_POINT
163 {
164 opus_val32 ac0=0;
165 int shift;
166 for(i=0;i<n;i++)
167 ac0 += SHR32(MULT16_16(xx[i],xx[i]),9);
168 ac0 += 1+n;
169
170 shift = celt_ilog2(ac0)-30+10;
171 shift = (shift+1)/2;
172 for(i=0;i<n;i++)
173 xx[i] = VSHR32(xx[i], shift);
174 }
175#endif
176 while (lag>=0)
177 {
178 for (i = lag, d = 0; i < n; i++)
179 d += xx[i] * xx[i-lag];
180 ac[lag] = d;
181 /*printf ("%f ", ac[lag]);*/
182 lag--;
183 }
184 /*printf ("\n");*/
185 ac[0] += 10;
186
187 RESTORE_STACK;
188}
diff --git a/lib/rbcodec/codecs/libopus/celt/celt_lpc.h b/lib/rbcodec/codecs/libopus/celt/celt_lpc.h
new file mode 100644
index 0000000000..2baa77edf8
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/celt_lpc.h
@@ -0,0 +1,53 @@
1/* Copyright (c) 2009-2010 Xiph.Org Foundation
2 Written by Jean-Marc Valin */
3/*
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#ifndef PLC_H
29#define PLC_H
30
31#include "arch.h"
32
33#define LPC_ORDER 24
34
35void _celt_lpc(opus_val16 *_lpc, const opus_val32 *ac, int p);
36
37void celt_fir(const opus_val16 *x,
38 const opus_val16 *num,
39 opus_val16 *y,
40 int N,
41 int ord,
42 opus_val16 *mem);
43
44void celt_iir(const opus_val32 *x,
45 const opus_val16 *den,
46 opus_val32 *y,
47 int N,
48 int ord,
49 opus_val16 *mem);
50
51void _celt_autocorr(const opus_val16 *x, opus_val32 *ac, const opus_val16 *window, int overlap, int lag, int n);
52
53#endif /* PLC_H */
diff --git a/lib/rbcodec/codecs/libopus/celt/cwrs.c b/lib/rbcodec/codecs/libopus/celt/cwrs.c
new file mode 100644
index 0000000000..3d5dd790d9
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/cwrs.c
@@ -0,0 +1,645 @@
1/* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2009 Xiph.Org Foundation
3 Copyright (c) 2007-2009 Timothy B. Terriberry
4 Written by Timothy B. Terriberry and Jean-Marc Valin */
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 "opus_config.h"
32#endif
33
34#include "os_support.h"
35#include "cwrs.h"
36#include "mathops.h"
37#include "arch.h"
38
39#ifdef CUSTOM_MODES
40
41/*Guaranteed to return a conservatively large estimate of the binary logarithm
42 with frac bits of fractional precision.
43 Tested for all possible 32-bit inputs with frac=4, where the maximum
44 overestimation is 0.06254243 bits.*/
45int log2_frac(opus_uint32 val, int frac)
46{
47 int l;
48 l=EC_ILOG(val);
49 if(val&(val-1)){
50 /*This is (val>>l-16), but guaranteed to round up, even if adding a bias
51 before the shift would cause overflow (e.g., for 0xFFFFxxxx).
52 Doesn't work for val=0, but that case fails the test above.*/
53 if(l>16)val=((val-1)>>(l-16))+1;
54 else val<<=16-l;
55 l=(l-1)<<frac;
56 /*Note that we always need one iteration, since the rounding up above means
57 that we might need to adjust the integer part of the logarithm.*/
58 do{
59 int b;
60 b=(int)(val>>16);
61 l+=b<<frac;
62 val=(val+b)>>b;
63 val=(val*val+0x7FFF)>>15;
64 }
65 while(frac-->0);
66 /*If val is not exactly 0x8000, then we have to round up the remainder.*/
67 return l+(val>0x8000);
68 }
69 /*Exact powers of two require no rounding.*/
70 else return (l-1)<<frac;
71}
72#endif
73
74#ifndef SMALL_FOOTPRINT
75
76#define MASK32 (0xFFFFFFFF)
77
78/*INV_TABLE[i] holds the multiplicative inverse of (2*i+1) mod 2**32.*/
79static const opus_uint32 INV_TABLE[53]={
80 0x00000001,0xAAAAAAAB,0xCCCCCCCD,0xB6DB6DB7,
81 0x38E38E39,0xBA2E8BA3,0xC4EC4EC5,0xEEEEEEEF,
82 0xF0F0F0F1,0x286BCA1B,0x3CF3CF3D,0xE9BD37A7,
83 0xC28F5C29,0x684BDA13,0x4F72C235,0xBDEF7BDF,
84 0x3E0F83E1,0x8AF8AF8B,0x914C1BAD,0x96F96F97,
85 0xC18F9C19,0x2FA0BE83,0xA4FA4FA5,0x677D46CF,
86 0x1A1F58D1,0xFAFAFAFB,0x8C13521D,0x586FB587,
87 0xB823EE09,0xA08AD8F3,0xC10C9715,0xBEFBEFBF,
88 0xC0FC0FC1,0x07A44C6B,0xA33F128D,0xE327A977,
89 0xC7E3F1F9,0x962FC963,0x3F2B3885,0x613716AF,
90 0x781948B1,0x2B2E43DB,0xFCFCFCFD,0x6FD0EB67,
91 0xFA3F47E9,0xD2FD2FD3,0x3F4FD3F5,0xD4E25B9F,
92 0x5F02A3A1,0xBF5A814B,0x7C32B16D,0xD3431B57,
93 0xD8FD8FD9,
94};
95
96/*Computes (_a*_b-_c)/(2*_d+1) when the quotient is known to be exact.
97 _a, _b, _c, and _d may be arbitrary so long as the arbitrary precision result
98 fits in 32 bits, but currently the table for multiplicative inverses is only
99 valid for _d<=52.*/
100static inline opus_uint32 imusdiv32odd(opus_uint32 _a,opus_uint32 _b,
101 opus_uint32 _c,int _d){
102 celt_assert(_d<=52);
103 return (_a*_b-_c)*INV_TABLE[_d]&MASK32;
104}
105
106/*Computes (_a*_b-_c)/_d when the quotient is known to be exact.
107 _d does not actually have to be even, but imusdiv32odd will be faster when
108 it's odd, so you should use that instead.
109 _a and _d are assumed to be small (e.g., _a*_d fits in 32 bits; currently the
110 table for multiplicative inverses is only valid for _d<=54).
111 _b and _c may be arbitrary so long as the arbitrary precision reuslt fits in
112 32 bits.*/
113static inline opus_uint32 imusdiv32even(opus_uint32 _a,opus_uint32 _b,
114 opus_uint32 _c,int _d){
115 opus_uint32 inv;
116 int mask;
117 int shift;
118 int one;
119 celt_assert(_d>0);
120 celt_assert(_d<=54);
121 shift=EC_ILOG(_d^(_d-1));
122 inv=INV_TABLE[(_d-1)>>shift];
123 shift--;
124 one=1<<shift;
125 mask=one-1;
126 return (_a*(_b>>shift)-(_c>>shift)+
127 ((_a*(_b&mask)+one-(_c&mask))>>shift)-1)*inv&MASK32;
128}
129
130#endif /* SMALL_FOOTPRINT */
131
132/*Although derived separately, the pulse vector coding scheme is equivalent to
133 a Pyramid Vector Quantizer \cite{Fis86}.
134 Some additional notes about an early version appear at
135 http://people.xiph.org/~tterribe/notes/cwrs.html, but the codebook ordering
136 and the definitions of some terms have evolved since that was written.
137
138 The conversion from a pulse vector to an integer index (encoding) and back
139 (decoding) is governed by two related functions, V(N,K) and U(N,K).
140
141 V(N,K) = the number of combinations, with replacement, of N items, taken K
142 at a time, when a sign bit is added to each item taken at least once (i.e.,
143 the number of N-dimensional unit pulse vectors with K pulses).
144 One way to compute this is via
145 V(N,K) = K>0 ? sum(k=1...K,2**k*choose(N,k)*choose(K-1,k-1)) : 1,
146 where choose() is the binomial function.
147 A table of values for N<10 and K<10 looks like:
148 V[10][10] = {
149 {1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
150 {1, 2, 2, 2, 2, 2, 2, 2, 2, 2},
151 {1, 4, 8, 12, 16, 20, 24, 28, 32, 36},
152 {1, 6, 18, 38, 66, 102, 146, 198, 258, 326},
153 {1, 8, 32, 88, 192, 360, 608, 952, 1408, 1992},
154 {1, 10, 50, 170, 450, 1002, 1970, 3530, 5890, 9290},
155 {1, 12, 72, 292, 912, 2364, 5336, 10836, 20256, 35436},
156 {1, 14, 98, 462, 1666, 4942, 12642, 28814, 59906, 115598},
157 {1, 16, 128, 688, 2816, 9424, 27008, 68464, 157184, 332688},
158 {1, 18, 162, 978, 4482, 16722, 53154, 148626, 374274, 864146}
159 };
160
161 U(N,K) = the number of such combinations wherein N-1 objects are taken at
162 most K-1 at a time.
163 This is given by
164 U(N,K) = sum(k=0...K-1,V(N-1,k))
165 = K>0 ? (V(N-1,K-1) + V(N,K-1))/2 : 0.
166 The latter expression also makes clear that U(N,K) is half the number of such
167 combinations wherein the first object is taken at least once.
168 Although it may not be clear from either of these definitions, U(N,K) is the
169 natural function to work with when enumerating the pulse vector codebooks,
170 not V(N,K).
171 U(N,K) is not well-defined for N=0, but with the extension
172 U(0,K) = K>0 ? 0 : 1,
173 the function becomes symmetric: U(N,K) = U(K,N), with a similar table:
174 U[10][10] = {
175 {1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
176 {0, 1, 1, 1, 1, 1, 1, 1, 1, 1},
177 {0, 1, 3, 5, 7, 9, 11, 13, 15, 17},
178 {0, 1, 5, 13, 25, 41, 61, 85, 113, 145},
179 {0, 1, 7, 25, 63, 129, 231, 377, 575, 833},
180 {0, 1, 9, 41, 129, 321, 681, 1289, 2241, 3649},
181 {0, 1, 11, 61, 231, 681, 1683, 3653, 7183, 13073},
182 {0, 1, 13, 85, 377, 1289, 3653, 8989, 19825, 40081},
183 {0, 1, 15, 113, 575, 2241, 7183, 19825, 48639, 108545},
184 {0, 1, 17, 145, 833, 3649, 13073, 40081, 108545, 265729}
185 };
186
187 With this extension, V(N,K) may be written in terms of U(N,K):
188 V(N,K) = U(N,K) + U(N,K+1)
189 for all N>=0, K>=0.
190 Thus U(N,K+1) represents the number of combinations where the first element
191 is positive or zero, and U(N,K) represents the number of combinations where
192 it is negative.
193 With a large enough table of U(N,K) values, we could write O(N) encoding
194 and O(min(N*log(K),N+K)) decoding routines, but such a table would be
195 prohibitively large for small embedded devices (K may be as large as 32767
196 for small N, and N may be as large as 200).
197
198 Both functions obey the same recurrence relation:
199 V(N,K) = V(N-1,K) + V(N,K-1) + V(N-1,K-1),
200 U(N,K) = U(N-1,K) + U(N,K-1) + U(N-1,K-1),
201 for all N>0, K>0, with different initial conditions at N=0 or K=0.
202 This allows us to construct a row of one of the tables above given the
203 previous row or the next row.
204 Thus we can derive O(NK) encoding and decoding routines with O(K) memory
205 using only addition and subtraction.
206
207 When encoding, we build up from the U(2,K) row and work our way forwards.
208 When decoding, we need to start at the U(N,K) row and work our way backwards,
209 which requires a means of computing U(N,K).
210 U(N,K) may be computed from two previous values with the same N:
211 U(N,K) = ((2*N-1)*U(N,K-1) - U(N,K-2))/(K-1) + U(N,K-2)
212 for all N>1, and since U(N,K) is symmetric, a similar relation holds for two
213 previous values with the same K:
214 U(N,K>1) = ((2*K-1)*U(N-1,K) - U(N-2,K))/(N-1) + U(N-2,K)
215 for all K>1.
216 This allows us to construct an arbitrary row of the U(N,K) table by starting
217 with the first two values, which are constants.
218 This saves roughly 2/3 the work in our O(NK) decoding routine, but costs O(K)
219 multiplications.
220 Similar relations can be derived for V(N,K), but are not used here.
221
222 For N>0 and K>0, U(N,K) and V(N,K) take on the form of an (N-1)-degree
223 polynomial for fixed N.
224 The first few are
225 U(1,K) = 1,
226 U(2,K) = 2*K-1,
227 U(3,K) = (2*K-2)*K+1,
228 U(4,K) = (((4*K-6)*K+8)*K-3)/3,
229 U(5,K) = ((((2*K-4)*K+10)*K-8)*K+3)/3,
230 and
231 V(1,K) = 2,
232 V(2,K) = 4*K,
233 V(3,K) = 4*K*K+2,
234 V(4,K) = 8*(K*K+2)*K/3,
235 V(5,K) = ((4*K*K+20)*K*K+6)/3,
236 for all K>0.
237 This allows us to derive O(N) encoding and O(N*log(K)) decoding routines for
238 small N (and indeed decoding is also O(N) for N<3).
239
240 @ARTICLE{Fis86,
241 author="Thomas R. Fischer",
242 title="A Pyramid Vector Quantizer",
243 journal="IEEE Transactions on Information Theory",
244 volume="IT-32",
245 number=4,
246 pages="568--583",
247 month=Jul,
248 year=1986
249 }*/
250
251#ifndef SMALL_FOOTPRINT
252/*Compute U(2,_k).
253 Note that this may be called with _k=32768 (maxK[2]+1).*/
254static inline unsigned ucwrs2(unsigned _k){
255 celt_assert(_k>0);
256 return _k+(_k-1);
257}
258
259/*Compute V(2,_k).*/
260static inline opus_uint32 ncwrs2(int _k){
261 celt_assert(_k>0);
262 return 4*(opus_uint32)_k;
263}
264
265/*Compute U(3,_k).
266 Note that this may be called with _k=32768 (maxK[3]+1).*/
267static inline opus_uint32 ucwrs3(unsigned _k){
268 celt_assert(_k>0);
269 return (2*(opus_uint32)_k-2)*_k+1;
270}
271
272/*Compute V(3,_k).*/
273static inline opus_uint32 ncwrs3(int _k){
274 celt_assert(_k>0);
275 return 2*(2*(unsigned)_k*(opus_uint32)_k+1);
276}
277
278/*Compute U(4,_k).*/
279static inline opus_uint32 ucwrs4(int _k){
280 celt_assert(_k>0);
281 return imusdiv32odd(2*_k,(2*_k-3)*(opus_uint32)_k+4,3,1);
282}
283
284/*Compute V(4,_k).*/
285static inline opus_uint32 ncwrs4(int _k){
286 celt_assert(_k>0);
287 return ((_k*(opus_uint32)_k+2)*_k)/3<<3;
288}
289
290#endif /* SMALL_FOOTPRINT */
291
292/*Computes the next row/column of any recurrence that obeys the relation
293 u[i][j]=u[i-1][j]+u[i][j-1]+u[i-1][j-1].
294 _ui0 is the base case for the new row/column.*/
295static inline void unext(opus_uint32 *_ui,unsigned _len,opus_uint32 _ui0){
296 opus_uint32 ui1;
297 unsigned j;
298 /*This do-while will overrun the array if we don't have storage for at least
299 2 values.*/
300 j=1; do {
301 ui1=UADD32(UADD32(_ui[j],_ui[j-1]),_ui0);
302 _ui[j-1]=_ui0;
303 _ui0=ui1;
304 } while (++j<_len);
305 _ui[j-1]=_ui0;
306}
307
308/*Computes the previous row/column of any recurrence that obeys the relation
309 u[i-1][j]=u[i][j]-u[i][j-1]-u[i-1][j-1].
310 _ui0 is the base case for the new row/column.*/
311static inline void uprev(opus_uint32 *_ui,unsigned _n,opus_uint32 _ui0){
312 opus_uint32 ui1;
313 unsigned j;
314 /*This do-while will overrun the array if we don't have storage for at least
315 2 values.*/
316 j=1; do {
317 ui1=USUB32(USUB32(_ui[j],_ui[j-1]),_ui0);
318 _ui[j-1]=_ui0;
319 _ui0=ui1;
320 } while (++j<_n);
321 _ui[j-1]=_ui0;
322}
323
324/*Compute V(_n,_k), as well as U(_n,0..._k+1).
325 _u: On exit, _u[i] contains U(_n,i) for i in [0..._k+1].*/
326static opus_uint32 ncwrs_urow(unsigned _n,unsigned _k,opus_uint32 *_u){
327 opus_uint32 um2;
328 unsigned len;
329 unsigned k;
330 len=_k+2;
331 /*We require storage at least 3 values (e.g., _k>0).*/
332 celt_assert(len>=3);
333 _u[0]=0;
334 _u[1]=um2=1;
335#ifndef SMALL_FOOTPRINT
336 /*_k>52 doesn't work in the false branch due to the limits of INV_TABLE,
337 but _k isn't tested here because k<=52 for n=7*/
338 if(_n<=6)
339#endif
340 {
341 /*If _n==0, _u[0] should be 1 and the rest should be 0.*/
342 /*If _n==1, _u[i] should be 1 for i>1.*/
343 celt_assert(_n>=2);
344 /*If _k==0, the following do-while loop will overflow the buffer.*/
345 celt_assert(_k>0);
346 k=2;
347 do _u[k]=(k<<1)-1;
348 while(++k<len);
349 for(k=2;k<_n;k++)unext(_u+1,_k+1,1);
350 }
351#ifndef SMALL_FOOTPRINT
352 else{
353 opus_uint32 um1;
354 opus_uint32 n2m1;
355 _u[2]=n2m1=um1=(_n<<1)-1;
356 for(k=3;k<len;k++){
357 /*U(N,K) = ((2*N-1)*U(N,K-1)-U(N,K-2))/(K-1) + U(N,K-2)*/
358 _u[k]=um2=imusdiv32even(n2m1,um1,um2,k-1)+um2;
359 if(++k>=len)break;
360 _u[k]=um1=imusdiv32odd(n2m1,um2,um1,(k-1)>>1)+um1;
361 }
362 }
363#endif /* SMALL_FOOTPRINT */
364 return _u[_k]+_u[_k+1];
365}
366
367#ifndef SMALL_FOOTPRINT
368
369/*Returns the _i'th combination of _k elements (at most 32767) chosen from a
370 set of size 1 with associated sign bits.
371 _y: Returns the vector of pulses.*/
372static inline void cwrsi1(int _k,opus_uint32 _i,int *_y){
373 int s;
374 s=-(int)_i;
375 _y[0]=(_k+s)^s;
376}
377
378/*Returns the _i'th combination of _k elements (at most 32767) chosen from a
379 set of size 2 with associated sign bits.
380 _y: Returns the vector of pulses.*/
381static inline void cwrsi2(int _k,opus_uint32 _i,int *_y){
382 opus_uint32 p;
383 int s;
384 int yj;
385 p=ucwrs2(_k+1U);
386 s=-(_i>=p);
387 _i-=p&s;
388 yj=_k;
389 _k=(_i+1)>>1;
390 p=_k?ucwrs2(_k):0;
391 _i-=p;
392 yj-=_k;
393 _y[0]=(yj+s)^s;
394 cwrsi1(_k,_i,_y+1);
395}
396
397/*Returns the _i'th combination of _k elements (at most 32767) chosen from a
398 set of size 3 with associated sign bits.
399 _y: Returns the vector of pulses.*/
400static void cwrsi3(int _k,opus_uint32 _i,int *_y){
401 opus_uint32 p;
402 int s;
403 int yj;
404 p=ucwrs3(_k+1U);
405 s=-(_i>=p);
406 _i-=p&s;
407 yj=_k;
408 /*Finds the maximum _k such that ucwrs3(_k)<=_i (tested for all
409 _i<2147418113=U(3,32768)).*/
410 _k=_i>0?(isqrt32(2*_i-1)+1)>>1:0;
411 p=_k?ucwrs3(_k):0;
412 _i-=p;
413 yj-=_k;
414 _y[0]=(yj+s)^s;
415 cwrsi2(_k,_i,_y+1);
416}
417
418/*Returns the _i'th combination of _k elements (at most 1172) chosen from a set
419 of size 4 with associated sign bits.
420 _y: Returns the vector of pulses.*/
421static void cwrsi4(int _k,opus_uint32 _i,int *_y){
422 opus_uint32 p;
423 int s;
424 int yj;
425 int kl;
426 int kr;
427 p=ucwrs4(_k+1);
428 s=-(_i>=p);
429 _i-=p&s;
430 yj=_k;
431 /*We could solve a cubic for k here, but the form of the direct solution does
432 not lend itself well to exact integer arithmetic.
433 Instead we do a binary search on U(4,K).*/
434 kl=0;
435 kr=_k;
436 for(;;){
437 _k=(kl+kr)>>1;
438 p=_k?ucwrs4(_k):0;
439 if(p<_i){
440 if(_k>=kr)break;
441 kl=_k+1;
442 }
443 else if(p>_i)kr=_k-1;
444 else break;
445 }
446 _i-=p;
447 yj-=_k;
448 _y[0]=(yj+s)^s;
449 cwrsi3(_k,_i,_y+1);
450}
451
452#endif /* SMALL_FOOTPRINT */
453
454/*Returns the _i'th combination of _k elements chosen from a set of size _n
455 with associated sign bits.
456 _y: Returns the vector of pulses.
457 _u: Must contain entries [0..._k+1] of row _n of U() on input.
458 Its contents will be destructively modified.*/
459static void cwrsi(int _n,int _k,opus_uint32 _i,int *_y,opus_uint32 *_u){
460 int j;
461 celt_assert(_n>0);
462 j=0;
463 do{
464 opus_uint32 p;
465 int s;
466 int yj;
467 p=_u[_k+1];
468 s=-(_i>=p);
469 _i-=p&s;
470 yj=_k;
471 p=_u[_k];
472 while(p>_i)p=_u[--_k];
473 _i-=p;
474 yj-=_k;
475 _y[j]=(yj+s)^s;
476 uprev(_u,_k+2,0);
477 }
478 while(++j<_n);
479}
480
481/*Returns the index of the given combination of K elements chosen from a set
482 of size 1 with associated sign bits.
483 _y: The vector of pulses, whose sum of absolute values is K.
484 _k: Returns K.*/
485static inline opus_uint32 icwrs1(const int *_y,int *_k){
486 *_k=abs(_y[0]);
487 return _y[0]<0;
488}
489
490#ifndef SMALL_FOOTPRINT
491
492/*Returns the index of the given combination of K elements chosen from a set
493 of size 2 with associated sign bits.
494 _y: The vector of pulses, whose sum of absolute values is K.
495 _k: Returns K.*/
496static inline opus_uint32 icwrs2(const int *_y,int *_k){
497 opus_uint32 i;
498 int k;
499 i=icwrs1(_y+1,&k);
500 i+=k?ucwrs2(k):0;
501 k+=abs(_y[0]);
502 if(_y[0]<0)i+=ucwrs2(k+1U);
503 *_k=k;
504 return i;
505}
506
507/*Returns the index of the given combination of K elements chosen from a set
508 of size 3 with associated sign bits.
509 _y: The vector of pulses, whose sum of absolute values is K.
510 _k: Returns K.*/
511static inline opus_uint32 icwrs3(const int *_y,int *_k){
512 opus_uint32 i;
513 int k;
514 i=icwrs2(_y+1,&k);
515 i+=k?ucwrs3(k):0;
516 k+=abs(_y[0]);
517 if(_y[0]<0)i+=ucwrs3(k+1U);
518 *_k=k;
519 return i;
520}
521
522/*Returns the index of the given combination of K elements chosen from a set
523 of size 4 with associated sign bits.
524 _y: The vector of pulses, whose sum of absolute values is K.
525 _k: Returns K.*/
526static inline opus_uint32 icwrs4(const int *_y,int *_k){
527 opus_uint32 i;
528 int k;
529 i=icwrs3(_y+1,&k);
530 i+=k?ucwrs4(k):0;
531 k+=abs(_y[0]);
532 if(_y[0]<0)i+=ucwrs4(k+1);
533 *_k=k;
534 return i;
535}
536
537#endif /* SMALL_FOOTPRINT */
538
539/*Returns the index of the given combination of K elements chosen from a set
540 of size _n with associated sign bits.
541 _y: The vector of pulses, whose sum of absolute values must be _k.
542 _nc: Returns V(_n,_k).*/
543static inline opus_uint32 icwrs(int _n,int _k,opus_uint32 *_nc,const int *_y,
544 opus_uint32 *_u){
545 opus_uint32 i;
546 int j;
547 int k;
548 /*We can't unroll the first two iterations of the loop unless _n>=2.*/
549 celt_assert(_n>=2);
550 _u[0]=0;
551 for(k=1;k<=_k+1;k++)_u[k]=(k<<1)-1;
552 i=icwrs1(_y+_n-1,&k);
553 j=_n-2;
554 i+=_u[k];
555 k+=abs(_y[j]);
556 if(_y[j]<0)i+=_u[k+1];
557 while(j-->0){
558 unext(_u,_k+2,0);
559 i+=_u[k];
560 k+=abs(_y[j]);
561 if(_y[j]<0)i+=_u[k+1];
562 }
563 *_nc=_u[k]+_u[k+1];
564 return i;
565}
566
567#ifdef CUSTOM_MODES
568void get_required_bits(opus_int16 *_bits,int _n,int _maxk,int _frac){
569 int k;
570 /*_maxk==0 => there's nothing to do.*/
571 celt_assert(_maxk>0);
572 _bits[0]=0;
573 if (_n==1)
574 {
575 for (k=1;k<=_maxk;k++)
576 _bits[k] = 1<<_frac;
577 }
578 else {
579 VARDECL(opus_uint32,u);
580 SAVE_STACK;
581 ALLOC(u,_maxk+2U,opus_uint32);
582 ncwrs_urow(_n,_maxk,u);
583 for(k=1;k<=_maxk;k++)
584 _bits[k]=log2_frac(u[k]+u[k+1],_frac);
585 RESTORE_STACK;
586 }
587}
588#endif /* CUSTOM_MODES */
589
590void encode_pulses(const int *_y,int _n,int _k,ec_enc *_enc){
591 opus_uint32 i;
592 celt_assert(_k>0);
593#ifndef SMALL_FOOTPRINT
594 switch(_n){
595 case 2:{
596 i=icwrs2(_y,&_k);
597 ec_enc_uint(_enc,i,ncwrs2(_k));
598 }break;
599 case 3:{
600 i=icwrs3(_y,&_k);
601 ec_enc_uint(_enc,i,ncwrs3(_k));
602 }break;
603 case 4:{
604 i=icwrs4(_y,&_k);
605 ec_enc_uint(_enc,i,ncwrs4(_k));
606 }break;
607 default:
608 {
609#endif
610 VARDECL(opus_uint32,u);
611 opus_uint32 nc;
612 SAVE_STACK;
613 ALLOC(u,_k+2U,opus_uint32);
614 i=icwrs(_n,_k,&nc,_y,u);
615 ec_enc_uint(_enc,i,nc);
616 RESTORE_STACK;
617#ifndef SMALL_FOOTPRINT
618 }
619 break;
620 }
621#endif
622}
623
624void decode_pulses(int *_y,int _n,int _k,ec_dec *_dec)
625{
626 celt_assert(_k>0);
627#ifndef SMALL_FOOTPRINT
628 switch(_n){
629 case 2:cwrsi2(_k,ec_dec_uint(_dec,ncwrs2(_k)),_y);break;
630 case 3:cwrsi3(_k,ec_dec_uint(_dec,ncwrs3(_k)),_y);break;
631 case 4:cwrsi4(_k,ec_dec_uint(_dec,ncwrs4(_k)),_y);break;
632 default:
633 {
634#endif
635 VARDECL(opus_uint32,u);
636 SAVE_STACK;
637 ALLOC(u,_k+2U,opus_uint32);
638 cwrsi(_n,_k,ec_dec_uint(_dec,ncwrs_urow(_n,_k,u)),_y,u);
639 RESTORE_STACK;
640#ifndef SMALL_FOOTPRINT
641 }
642 break;
643 }
644#endif
645}
diff --git a/lib/rbcodec/codecs/libopus/celt/cwrs.h b/lib/rbcodec/codecs/libopus/celt/cwrs.h
new file mode 100644
index 0000000000..7dfbd076d1
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/cwrs.h
@@ -0,0 +1,48 @@
1/* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2009 Xiph.Org Foundation
3 Copyright (c) 2007-2009 Timothy B. Terriberry
4 Written by Timothy B. Terriberry and Jean-Marc Valin */
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#ifndef CWRS_H
31#define CWRS_H
32
33#include "arch.h"
34#include "stack_alloc.h"
35#include "entenc.h"
36#include "entdec.h"
37
38#ifdef CUSTOM_MODES
39int log2_frac(opus_uint32 val, int frac);
40#endif
41
42void get_required_bits(opus_int16 *bits, int N, int K, int frac);
43
44void encode_pulses(const int *_y, int N, int K, ec_enc *enc);
45
46void decode_pulses(int *_y, int N, int K, ec_dec *dec);
47
48#endif /* CWRS_H */
diff --git a/lib/rbcodec/codecs/libopus/celt/ecintrin.h b/lib/rbcodec/codecs/libopus/celt/ecintrin.h
new file mode 100644
index 0000000000..3dffa5f95c
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/ecintrin.h
@@ -0,0 +1,87 @@
1/* Copyright (c) 2003-2008 Timothy B. Terriberry
2 Copyright (c) 2008 Xiph.Org Foundation */
3/*
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28/*Some common macros for potential platform-specific optimization.*/
29#include "opus_types.h"
30#include <math.h>
31#include <limits.h>
32#include "arch.h"
33#if !defined(_ecintrin_H)
34# define _ecintrin_H (1)
35
36/*Some specific platforms may have optimized intrinsic or inline assembly
37 versions of these functions which can substantially improve performance.
38 We define macros for them to allow easy incorporation of these non-ANSI
39 features.*/
40
41/*Modern gcc (4.x) can compile the naive versions of min and max with cmov if
42 given an appropriate architecture, but the branchless bit-twiddling versions
43 are just as fast, and do not require any special target architecture.
44 Earlier gcc versions (3.x) compiled both code to the same assembly
45 instructions, because of the way they represented ((_b)>(_a)) internally.*/
46# define EC_MINI(_a,_b) ((_a)+(((_b)-(_a))&-((_b)<(_a))))
47
48/*Count leading zeros.
49 This macro should only be used for implementing ec_ilog(), if it is defined.
50 All other code should use EC_ILOG() instead.*/
51#if defined(_MSC_VER)
52# include <intrin.h>
53/*In _DEBUG mode this is not an intrinsic by default.*/
54# pragma intrinsic(_BitScanReverse)
55
56static __inline int ec_bsr(unsigned long _x){
57 unsigned long ret;
58 _BitScanReverse(&ret,_x);
59 return (int)ret;
60}
61# define EC_CLZ0 (1)
62# define EC_CLZ(_x) (-ec_bsr(_x))
63#elif defined(ENABLE_TI_DSPLIB)
64# include "dsplib.h"
65# define EC_CLZ0 (31)
66# define EC_CLZ(_x) (_lnorm(_x))
67#elif __GNUC_PREREQ(3,4)
68# if INT_MAX>=2147483647
69# define EC_CLZ0 ((int)sizeof(unsigned)*CHAR_BIT)
70# define EC_CLZ(_x) (__builtin_clz(_x))
71# elif LONG_MAX>=2147483647L
72# define EC_CLZ0 ((int)sizeof(unsigned long)*CHAR_BIT)
73# define EC_CLZ(_x) (__builtin_clzl(_x))
74# endif
75#endif
76
77#if defined(EC_CLZ)
78/*Note that __builtin_clz is not defined when _x==0, according to the gcc
79 documentation (and that of the BSR instruction that implements it on x86).
80 The majority of the time we can never pass it zero.
81 When we need to, it can be special cased.*/
82# define EC_ILOG(_x) (EC_CLZ0-EC_CLZ(_x))
83#else
84int ec_ilog(opus_uint32 _v);
85# define EC_ILOG(_x) (ec_ilog(_x))
86#endif
87#endif
diff --git a/lib/rbcodec/codecs/libopus/celt/entcode.c b/lib/rbcodec/codecs/libopus/celt/entcode.c
new file mode 100644
index 0000000000..80e64fefaa
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/entcode.c
@@ -0,0 +1,88 @@
1/* Copyright (c) 2001-2011 Timothy B. Terriberry
2*/
3/*
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#ifdef HAVE_CONFIG_H
29#include "opus_config.h"
30#endif
31
32#include "entcode.h"
33#include "arch.h"
34
35#if !defined(EC_CLZ)
36int ec_ilog(opus_uint32 _v){
37 /*On a Pentium M, this branchless version tested as the fastest on
38 1,000,000,000 random 32-bit integers, edging out a similar version with
39 branches, and a 256-entry LUT version.*/
40 int ret;
41 int m;
42 ret=!!_v;
43 m=!!(_v&0xFFFF0000)<<4;
44 _v>>=m;
45 ret|=m;
46 m=!!(_v&0xFF00)<<3;
47 _v>>=m;
48 ret|=m;
49 m=!!(_v&0xF0)<<2;
50 _v>>=m;
51 ret|=m;
52 m=!!(_v&0xC)<<1;
53 _v>>=m;
54 ret|=m;
55 ret+=!!(_v&0x2);
56 return ret;
57}
58#endif
59
60opus_uint32 ec_tell_frac(ec_ctx *_this){
61 opus_uint32 nbits;
62 opus_uint32 r;
63 int l;
64 int i;
65 /*To handle the non-integral number of bits still left in the encoder/decoder
66 state, we compute the worst-case number of bits of val that must be
67 encoded to ensure that the value is inside the range for any possible
68 subsequent bits.
69 The computation here is independent of val itself (the decoder does not
70 even track that value), even though the real number of bits used after
71 ec_enc_done() may be 1 smaller if rng is a power of two and the
72 corresponding trailing bits of val are all zeros.
73 If we did try to track that special case, then coding a value with a
74 probability of 1/(1<<n) might sometimes appear to use more than n bits.
75 This may help explain the surprising result that a newly initialized
76 encoder or decoder claims to have used 1 bit.*/
77 nbits=_this->nbits_total<<BITRES;
78 l=EC_ILOG(_this->rng);
79 r=_this->rng>>(l-16);
80 for(i=BITRES;i-->0;){
81 int b;
82 r=r*r>>15;
83 b=(int)(r>>16);
84 l=l<<1|b;
85 r>>=b;
86 }
87 return nbits-l;
88}
diff --git a/lib/rbcodec/codecs/libopus/celt/entcode.h b/lib/rbcodec/codecs/libopus/celt/entcode.h
new file mode 100644
index 0000000000..aebecc0647
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/entcode.h
@@ -0,0 +1,116 @@
1/* Copyright (c) 2001-2011 Timothy B. Terriberry
2 Copyright (c) 2008-2009 Xiph.Org Foundation */
3/*
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#include "opus_types.h"
29
30#if !defined(_entcode_H)
31# define _entcode_H (1)
32# include <limits.h>
33# include <stddef.h>
34# include "ecintrin.h"
35
36/*OPT: ec_window must be at least 32 bits, but if you have fast arithmetic on a
37 larger type, you can speed up the decoder by using it here.*/
38typedef opus_uint32 ec_window;
39typedef struct ec_ctx ec_ctx;
40typedef struct ec_ctx ec_enc;
41typedef struct ec_ctx ec_dec;
42
43# define EC_WINDOW_SIZE ((int)sizeof(ec_window)*CHAR_BIT)
44
45/*The number of bits to use for the range-coded part of unsigned integers.*/
46# define EC_UINT_BITS (8)
47
48/*The resolution of fractional-precision bit usage measurements, i.e.,
49 3 => 1/8th bits.*/
50# define BITRES 3
51
52/*The entropy encoder/decoder context.
53 We use the same structure for both, so that common functions like ec_tell()
54 can be used on either one.*/
55struct ec_ctx{
56 /*Buffered input/output.*/
57 unsigned char *buf;
58 /*The size of the buffer.*/
59 opus_uint32 storage;
60 /*The offset at which the last byte containing raw bits was read/written.*/
61 opus_uint32 end_offs;
62 /*Bits that will be read from/written at the end.*/
63 ec_window end_window;
64 /*Number of valid bits in end_window.*/
65 int nend_bits;
66 /*The total number of whole bits read/written.
67 This does not include partial bits currently in the range coder.*/
68 int nbits_total;
69 /*The offset at which the next range coder byte will be read/written.*/
70 opus_uint32 offs;
71 /*The number of values in the current range.*/
72 opus_uint32 rng;
73 /*In the decoder: the difference between the top of the current range and
74 the input value, minus one.
75 In the encoder: the low end of the current range.*/
76 opus_uint32 val;
77 /*In the decoder: the saved normalization factor from ec_decode().
78 In the encoder: the number of oustanding carry propagating symbols.*/
79 opus_uint32 ext;
80 /*A buffered input/output symbol, awaiting carry propagation.*/
81 int rem;
82 /*Nonzero if an error occurred.*/
83 int error;
84};
85
86static inline opus_uint32 ec_range_bytes(ec_ctx *_this){
87 return _this->offs;
88}
89
90static inline unsigned char *ec_get_buffer(ec_ctx *_this){
91 return _this->buf;
92}
93
94static inline int ec_get_error(ec_ctx *_this){
95 return _this->error;
96}
97
98/*Returns the number of bits "used" by the encoded or decoded symbols so far.
99 This same number can be computed in either the encoder or the decoder, and is
100 suitable for making coding decisions.
101 Return: The number of bits.
102 This will always be slightly larger than the exact value (e.g., all
103 rounding error is in the positive direction).*/
104static inline int ec_tell(ec_ctx *_this){
105 return _this->nbits_total-EC_ILOG(_this->rng);
106}
107
108/*Returns the number of bits "used" by the encoded or decoded symbols so far.
109 This same number can be computed in either the encoder or the decoder, and is
110 suitable for making coding decisions.
111 Return: The number of bits scaled by 2**BITRES.
112 This will always be slightly larger than the exact value (e.g., all
113 rounding error is in the positive direction).*/
114opus_uint32 ec_tell_frac(ec_ctx *_this);
115
116#endif
diff --git a/lib/rbcodec/codecs/libopus/celt/entdec.c b/lib/rbcodec/codecs/libopus/celt/entdec.c
new file mode 100644
index 0000000000..ff8442d534
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/entdec.c
@@ -0,0 +1,245 @@
1/* Copyright (c) 2001-2011 Timothy B. Terriberry
2 Copyright (c) 2008-2009 Xiph.Org Foundation */
3/*
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#ifdef HAVE_CONFIG_H
29#include "opus_config.h"
30#endif
31
32#include <stddef.h>
33#include "os_support.h"
34#include "arch.h"
35#include "entdec.h"
36#include "mfrngcod.h"
37
38/*A range decoder.
39 This is an entropy decoder based upon \cite{Mar79}, which is itself a
40 rediscovery of the FIFO arithmetic code introduced by \cite{Pas76}.
41 It is very similar to arithmetic encoding, except that encoding is done with
42 digits in any base, instead of with bits, and so it is faster when using
43 larger bases (i.e.: a byte).
44 The author claims an average waste of $\frac{1}{2}\log_b(2b)$ bits, where $b$
45 is the base, longer than the theoretical optimum, but to my knowledge there
46 is no published justification for this claim.
47 This only seems true when using near-infinite precision arithmetic so that
48 the process is carried out with no rounding errors.
49
50 An excellent description of implementation details is available at
51 http://www.arturocampos.com/ac_range.html
52 A recent work \cite{MNW98} which proposes several changes to arithmetic
53 encoding for efficiency actually re-discovers many of the principles
54 behind range encoding, and presents a good theoretical analysis of them.
55
56 End of stream is handled by writing out the smallest number of bits that
57 ensures that the stream will be correctly decoded regardless of the value of
58 any subsequent bits.
59 ec_tell() can be used to determine how many bits were needed to decode
60 all the symbols thus far; other data can be packed in the remaining bits of
61 the input buffer.
62 @PHDTHESIS{Pas76,
63 author="Richard Clark Pasco",
64 title="Source coding algorithms for fast data compression",
65 school="Dept. of Electrical Engineering, Stanford University",
66 address="Stanford, CA",
67 month=May,
68 year=1976
69 }
70 @INPROCEEDINGS{Mar79,
71 author="Martin, G.N.N.",
72 title="Range encoding: an algorithm for removing redundancy from a digitised
73 message",
74 booktitle="Video & Data Recording Conference",
75 year=1979,
76 address="Southampton",
77 month=Jul
78 }
79 @ARTICLE{MNW98,
80 author="Alistair Moffat and Radford Neal and Ian H. Witten",
81 title="Arithmetic Coding Revisited",
82 journal="{ACM} Transactions on Information Systems",
83 year=1998,
84 volume=16,
85 number=3,
86 pages="256--294",
87 month=Jul,
88 URL="http://www.stanford.edu/class/ee398/handouts/papers/Moffat98ArithmCoding.pdf"
89 }*/
90
91static int ec_read_byte(ec_dec *_this){
92 return _this->offs<_this->storage?_this->buf[_this->offs++]:0;
93}
94
95static int ec_read_byte_from_end(ec_dec *_this){
96 return _this->end_offs<_this->storage?
97 _this->buf[_this->storage-++(_this->end_offs)]:0;
98}
99
100/*Normalizes the contents of val and rng so that rng lies entirely in the
101 high-order symbol.*/
102static void ec_dec_normalize(ec_dec *_this){
103 /*If the range is too small, rescale it and input some bits.*/
104 while(_this->rng<=EC_CODE_BOT){
105 int sym;
106 _this->nbits_total+=EC_SYM_BITS;
107 _this->rng<<=EC_SYM_BITS;
108 /*Use up the remaining bits from our last symbol.*/
109 sym=_this->rem;
110 /*Read the next value from the input.*/
111 _this->rem=ec_read_byte(_this);
112 /*Take the rest of the bits we need from this new symbol.*/
113 sym=(sym<<EC_SYM_BITS|_this->rem)>>(EC_SYM_BITS-EC_CODE_EXTRA);
114 /*And subtract them from val, capped to be less than EC_CODE_TOP.*/
115 _this->val=((_this->val<<EC_SYM_BITS)+(EC_SYM_MAX&~sym))&(EC_CODE_TOP-1);
116 }
117}
118
119void ec_dec_init(ec_dec *_this,unsigned char *_buf,opus_uint32 _storage){
120 _this->buf=_buf;
121 _this->storage=_storage;
122 _this->end_offs=0;
123 _this->end_window=0;
124 _this->nend_bits=0;
125 /*This is the offset from which ec_tell() will subtract partial bits.
126 The final value after the ec_dec_normalize() call will be the same as in
127 the encoder, but we have to compensate for the bits that are added there.*/
128 _this->nbits_total=EC_CODE_BITS+1
129 -((EC_CODE_BITS-EC_CODE_EXTRA)/EC_SYM_BITS)*EC_SYM_BITS;
130 _this->offs=0;
131 _this->rng=1U<<EC_CODE_EXTRA;
132 _this->rem=ec_read_byte(_this);
133 _this->val=_this->rng-1-(_this->rem>>(EC_SYM_BITS-EC_CODE_EXTRA));
134 _this->error=0;
135 /*Normalize the interval.*/
136 ec_dec_normalize(_this);
137}
138
139unsigned ec_decode(ec_dec *_this,unsigned _ft){
140 unsigned s;
141 _this->ext=_this->rng/_ft;
142 s=(unsigned)(_this->val/_this->ext);
143 return _ft-EC_MINI(s+1,_ft);
144}
145
146unsigned ec_decode_bin(ec_dec *_this,unsigned _bits){
147 unsigned s;
148 _this->ext=_this->rng>>_bits;
149 s=(unsigned)(_this->val/_this->ext);
150 return (1U<<_bits)-EC_MINI(s+1U,1U<<_bits);
151}
152
153void ec_dec_update(ec_dec *_this,unsigned _fl,unsigned _fh,unsigned _ft){
154 opus_uint32 s;
155 s=IMUL32(_this->ext,_ft-_fh);
156 _this->val-=s;
157 _this->rng=_fl>0?IMUL32(_this->ext,_fh-_fl):_this->rng-s;
158 ec_dec_normalize(_this);
159}
160
161/*The probability of having a "one" is 1/(1<<_logp).*/
162int ec_dec_bit_logp(ec_dec *_this,unsigned _logp){
163 opus_uint32 r;
164 opus_uint32 d;
165 opus_uint32 s;
166 int ret;
167 r=_this->rng;
168 d=_this->val;
169 s=r>>_logp;
170 ret=d<s;
171 if(!ret)_this->val=d-s;
172 _this->rng=ret?s:r-s;
173 ec_dec_normalize(_this);
174 return ret;
175}
176
177int ec_dec_icdf(ec_dec *_this,const unsigned char *_icdf,unsigned _ftb){
178 opus_uint32 r;
179 opus_uint32 d;
180 opus_uint32 s;
181 opus_uint32 t;
182 int ret;
183 s=_this->rng;
184 d=_this->val;
185 r=s>>_ftb;
186 ret=-1;
187 do{
188 t=s;
189 s=IMUL32(r,_icdf[++ret]);
190 }
191 while(d<s);
192 _this->val=d-s;
193 _this->rng=t-s;
194 ec_dec_normalize(_this);
195 return ret;
196}
197
198opus_uint32 ec_dec_uint(ec_dec *_this,opus_uint32 _ft){
199 unsigned ft;
200 unsigned s;
201 int ftb;
202 /*In order to optimize EC_ILOG(), it is undefined for the value 0.*/
203 celt_assert(_ft>1);
204 _ft--;
205 ftb=EC_ILOG(_ft);
206 if(ftb>EC_UINT_BITS){
207 opus_uint32 t;
208 ftb-=EC_UINT_BITS;
209 ft=(unsigned)(_ft>>ftb)+1;
210 s=ec_decode(_this,ft);
211 ec_dec_update(_this,s,s+1,ft);
212 t=(opus_uint32)s<<ftb|ec_dec_bits(_this,ftb);
213 if(t<=_ft)return t;
214 _this->error=1;
215 return _ft;
216 }
217 else{
218 _ft++;
219 s=ec_decode(_this,(unsigned)_ft);
220 ec_dec_update(_this,s,s+1,(unsigned)_ft);
221 return s;
222 }
223}
224
225opus_uint32 ec_dec_bits(ec_dec *_this,unsigned _bits){
226 ec_window window;
227 int available;
228 opus_uint32 ret;
229 window=_this->end_window;
230 available=_this->nend_bits;
231 if((unsigned)available<_bits){
232 do{
233 window|=(ec_window)ec_read_byte_from_end(_this)<<available;
234 available+=EC_SYM_BITS;
235 }
236 while(available<=EC_WINDOW_SIZE-EC_SYM_BITS);
237 }
238 ret=(opus_uint32)window&(((opus_uint32)1<<_bits)-1U);
239 window>>=_bits;
240 available-=_bits;
241 _this->end_window=window;
242 _this->nend_bits=available;
243 _this->nbits_total+=_bits;
244 return ret;
245}
diff --git a/lib/rbcodec/codecs/libopus/celt/entdec.h b/lib/rbcodec/codecs/libopus/celt/entdec.h
new file mode 100644
index 0000000000..d8ab318730
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/entdec.h
@@ -0,0 +1,100 @@
1/* Copyright (c) 2001-2011 Timothy B. Terriberry
2 Copyright (c) 2008-2009 Xiph.Org Foundation */
3/*
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#if !defined(_entdec_H)
29# define _entdec_H (1)
30# include <limits.h>
31# include "entcode.h"
32
33/*Initializes the decoder.
34 _buf: The input buffer to use.
35 Return: 0 on success, or a negative value on error.*/
36void ec_dec_init(ec_dec *_this,unsigned char *_buf,opus_uint32 _storage);
37
38/*Calculates the cumulative frequency for the next symbol.
39 This can then be fed into the probability model to determine what that
40 symbol is, and the additional frequency information required to advance to
41 the next symbol.
42 This function cannot be called more than once without a corresponding call to
43 ec_dec_update(), or decoding will not proceed correctly.
44 _ft: The total frequency of the symbols in the alphabet the next symbol was
45 encoded with.
46 Return: A cumulative frequency representing the encoded symbol.
47 If the cumulative frequency of all the symbols before the one that
48 was encoded was fl, and the cumulative frequency of all the symbols
49 up to and including the one encoded is fh, then the returned value
50 will fall in the range [fl,fh).*/
51unsigned ec_decode(ec_dec *_this,unsigned _ft);
52
53/*Equivalent to ec_decode() with _ft==1<<_bits.*/
54unsigned ec_decode_bin(ec_dec *_this,unsigned _bits);
55
56/*Advance the decoder past the next symbol using the frequency information the
57 symbol was encoded with.
58 Exactly one call to ec_decode() must have been made so that all necessary
59 intermediate calculations are performed.
60 _fl: The cumulative frequency of all symbols that come before the symbol
61 decoded.
62 _fh: The cumulative frequency of all symbols up to and including the symbol
63 decoded.
64 Together with _fl, this defines the range [_fl,_fh) in which the value
65 returned above must fall.
66 _ft: The total frequency of the symbols in the alphabet the symbol decoded
67 was encoded in.
68 This must be the same as passed to the preceding call to ec_decode().*/
69void ec_dec_update(ec_dec *_this,unsigned _fl,unsigned _fh,unsigned _ft);
70
71/* Decode a bit that has a 1/(1<<_logp) probability of being a one */
72int ec_dec_bit_logp(ec_dec *_this,unsigned _logp);
73
74/*Decodes a symbol given an "inverse" CDF table.
75 No call to ec_dec_update() is necessary after this call.
76 _icdf: The "inverse" CDF, such that symbol s falls in the range
77 [s>0?ft-_icdf[s-1]:0,ft-_icdf[s]), where ft=1<<_ftb.
78 The values must be monotonically non-increasing, and the last value
79 must be 0.
80 _ftb: The number of bits of precision in the cumulative distribution.
81 Return: The decoded symbol s.*/
82int ec_dec_icdf(ec_dec *_this,const unsigned char *_icdf,unsigned _ftb);
83
84/*Extracts a raw unsigned integer with a non-power-of-2 range from the stream.
85 The bits must have been encoded with ec_enc_uint().
86 No call to ec_dec_update() is necessary after this call.
87 _ft: The number of integers that can be decoded (one more than the max).
88 This must be at least one, and no more than 2**32-1.
89 Return: The decoded bits.*/
90opus_uint32 ec_dec_uint(ec_dec *_this,opus_uint32 _ft);
91
92/*Extracts a sequence of raw bits from the stream.
93 The bits must have been encoded with ec_enc_bits().
94 No call to ec_dec_update() is necessary after this call.
95 _ftb: The number of bits to extract.
96 This must be between 0 and 25, inclusive.
97 Return: The decoded bits.*/
98opus_uint32 ec_dec_bits(ec_dec *_this,unsigned _ftb);
99
100#endif
diff --git a/lib/rbcodec/codecs/libopus/celt/entenc.c b/lib/rbcodec/codecs/libopus/celt/entenc.c
new file mode 100644
index 0000000000..0ec6e91fd7
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/entenc.c
@@ -0,0 +1,294 @@
1/* Copyright (c) 2001-2011 Timothy B. Terriberry
2 Copyright (c) 2008-2009 Xiph.Org Foundation */
3/*
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#if defined(HAVE_CONFIG_H)
29# include "opus_config.h"
30#endif
31#include "os_support.h"
32#include "arch.h"
33#include "entenc.h"
34#include "mfrngcod.h"
35
36/*A range encoder.
37 See entdec.c and the references for implementation details \cite{Mar79,MNW98}.
38
39 @INPROCEEDINGS{Mar79,
40 author="Martin, G.N.N.",
41 title="Range encoding: an algorithm for removing redundancy from a digitised
42 message",
43 booktitle="Video \& Data Recording Conference",
44 year=1979,
45 address="Southampton",
46 month=Jul
47 }
48 @ARTICLE{MNW98,
49 author="Alistair Moffat and Radford Neal and Ian H. Witten",
50 title="Arithmetic Coding Revisited",
51 journal="{ACM} Transactions on Information Systems",
52 year=1998,
53 volume=16,
54 number=3,
55 pages="256--294",
56 month=Jul,
57 URL="http://www.stanford.edu/class/ee398/handouts/papers/Moffat98ArithmCoding.pdf"
58 }*/
59
60static int ec_write_byte(ec_enc *_this,unsigned _value){
61 if(_this->offs+_this->end_offs>=_this->storage)return -1;
62 _this->buf[_this->offs++]=(unsigned char)_value;
63 return 0;
64}
65
66static int ec_write_byte_at_end(ec_enc *_this,unsigned _value){
67 if(_this->offs+_this->end_offs>=_this->storage)return -1;
68 _this->buf[_this->storage-++(_this->end_offs)]=(unsigned char)_value;
69 return 0;
70}
71
72/*Outputs a symbol, with a carry bit.
73 If there is a potential to propagate a carry over several symbols, they are
74 buffered until it can be determined whether or not an actual carry will
75 occur.
76 If the counter for the buffered symbols overflows, then the stream becomes
77 undecodable.
78 This gives a theoretical limit of a few billion symbols in a single packet on
79 32-bit systems.
80 The alternative is to truncate the range in order to force a carry, but
81 requires similar carry tracking in the decoder, needlessly slowing it down.*/
82static void ec_enc_carry_out(ec_enc *_this,int _c){
83 if(_c!=EC_SYM_MAX){
84 /*No further carry propagation possible, flush buffer.*/
85 int carry;
86 carry=_c>>EC_SYM_BITS;
87 /*Don't output a byte on the first write.
88 This compare should be taken care of by branch-prediction thereafter.*/
89 if(_this->rem>=0)_this->error|=ec_write_byte(_this,_this->rem+carry);
90 if(_this->ext>0){
91 unsigned sym;
92 sym=(EC_SYM_MAX+carry)&EC_SYM_MAX;
93 do _this->error|=ec_write_byte(_this,sym);
94 while(--(_this->ext)>0);
95 }
96 _this->rem=_c&EC_SYM_MAX;
97 }
98 else _this->ext++;
99}
100
101static void ec_enc_normalize(ec_enc *_this){
102 /*If the range is too small, output some bits and rescale it.*/
103 while(_this->rng<=EC_CODE_BOT){
104 ec_enc_carry_out(_this,(int)(_this->val>>EC_CODE_SHIFT));
105 /*Move the next-to-high-order symbol into the high-order position.*/
106 _this->val=(_this->val<<EC_SYM_BITS)&(EC_CODE_TOP-1);
107 _this->rng<<=EC_SYM_BITS;
108 _this->nbits_total+=EC_SYM_BITS;
109 }
110}
111
112void ec_enc_init(ec_enc *_this,unsigned char *_buf,opus_uint32 _size){
113 _this->buf=_buf;
114 _this->end_offs=0;
115 _this->end_window=0;
116 _this->nend_bits=0;
117 /*This is the offset from which ec_tell() will subtract partial bits.*/
118 _this->nbits_total=EC_CODE_BITS+1;
119 _this->offs=0;
120 _this->rng=EC_CODE_TOP;
121 _this->rem=-1;
122 _this->val=0;
123 _this->ext=0;
124 _this->storage=_size;
125 _this->error=0;
126}
127
128void ec_encode(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned _ft){
129 opus_uint32 r;
130 r=_this->rng/_ft;
131 if(_fl>0){
132 _this->val+=_this->rng-IMUL32(r,(_ft-_fl));
133 _this->rng=IMUL32(r,(_fh-_fl));
134 }
135 else _this->rng-=IMUL32(r,(_ft-_fh));
136 ec_enc_normalize(_this);
137}
138
139void ec_encode_bin(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned _bits){
140 opus_uint32 r;
141 r=_this->rng>>_bits;
142 if(_fl>0){
143 _this->val+=_this->rng-IMUL32(r,((1U<<_bits)-_fl));
144 _this->rng=IMUL32(r,(_fh-_fl));
145 }
146 else _this->rng-=IMUL32(r,((1U<<_bits)-_fh));
147 ec_enc_normalize(_this);
148}
149
150/*The probability of having a "one" is 1/(1<<_logp).*/
151void ec_enc_bit_logp(ec_enc *_this,int _val,unsigned _logp){
152 opus_uint32 r;
153 opus_uint32 s;
154 opus_uint32 l;
155 r=_this->rng;
156 l=_this->val;
157 s=r>>_logp;
158 r-=s;
159 if(_val)_this->val=l+r;
160 _this->rng=_val?s:r;
161 ec_enc_normalize(_this);
162}
163
164void ec_enc_icdf(ec_enc *_this,int _s,const unsigned char *_icdf,unsigned _ftb){
165 opus_uint32 r;
166 r=_this->rng>>_ftb;
167 if(_s>0){
168 _this->val+=_this->rng-IMUL32(r,_icdf[_s-1]);
169 _this->rng=IMUL32(r,_icdf[_s-1]-_icdf[_s]);
170 }
171 else _this->rng-=IMUL32(r,_icdf[_s]);
172 ec_enc_normalize(_this);
173}
174
175void ec_enc_uint(ec_enc *_this,opus_uint32 _fl,opus_uint32 _ft){
176 unsigned ft;
177 unsigned fl;
178 int ftb;
179 /*In order to optimize EC_ILOG(), it is undefined for the value 0.*/
180 celt_assert(_ft>1);
181 _ft--;
182 ftb=EC_ILOG(_ft);
183 if(ftb>EC_UINT_BITS){
184 ftb-=EC_UINT_BITS;
185 ft=(_ft>>ftb)+1;
186 fl=(unsigned)(_fl>>ftb);
187 ec_encode(_this,fl,fl+1,ft);
188 ec_enc_bits(_this,_fl&(((opus_uint32)1<<ftb)-1U),ftb);
189 }
190 else ec_encode(_this,_fl,_fl+1,_ft+1);
191}
192
193void ec_enc_bits(ec_enc *_this,opus_uint32 _fl,unsigned _bits){
194 ec_window window;
195 int used;
196 window=_this->end_window;
197 used=_this->nend_bits;
198 celt_assert(_bits>0);
199 if(used+_bits>EC_WINDOW_SIZE){
200 do{
201 _this->error|=ec_write_byte_at_end(_this,(unsigned)window&EC_SYM_MAX);
202 window>>=EC_SYM_BITS;
203 used-=EC_SYM_BITS;
204 }
205 while(used>=EC_SYM_BITS);
206 }
207 window|=(ec_window)_fl<<used;
208 used+=_bits;
209 _this->end_window=window;
210 _this->nend_bits=used;
211 _this->nbits_total+=_bits;
212}
213
214void ec_enc_patch_initial_bits(ec_enc *_this,unsigned _val,unsigned _nbits){
215 int shift;
216 unsigned mask;
217 celt_assert(_nbits<=EC_SYM_BITS);
218 shift=EC_SYM_BITS-_nbits;
219 mask=((1<<_nbits)-1)<<shift;
220 if(_this->offs>0){
221 /*The first byte has been finalized.*/
222 _this->buf[0]=(unsigned char)((_this->buf[0]&~mask)|_val<<shift);
223 }
224 else if(_this->rem>=0){
225 /*The first byte is still awaiting carry propagation.*/
226 _this->rem=(_this->rem&~mask)|_val<<shift;
227 }
228 else if(_this->rng<=(EC_CODE_TOP>>_nbits)){
229 /*The renormalization loop has never been run.*/
230 _this->val=(_this->val&~((opus_uint32)mask<<EC_CODE_SHIFT))|
231 (opus_uint32)_val<<(EC_CODE_SHIFT+shift);
232 }
233 /*The encoder hasn't even encoded _nbits of data yet.*/
234 else _this->error=-1;
235}
236
237void ec_enc_shrink(ec_enc *_this,opus_uint32 _size){
238 celt_assert(_this->offs+_this->end_offs<=_size);
239 OPUS_MOVE(_this->buf+_size-_this->end_offs,
240 _this->buf+_this->storage-_this->end_offs,_this->end_offs);
241 _this->storage=_size;
242}
243
244void ec_enc_done(ec_enc *_this){
245 ec_window window;
246 int used;
247 opus_uint32 msk;
248 opus_uint32 end;
249 int l;
250 /*We output the minimum number of bits that ensures that the symbols encoded
251 thus far will be decoded correctly regardless of the bits that follow.*/
252 l=EC_CODE_BITS-EC_ILOG(_this->rng);
253 msk=(EC_CODE_TOP-1)>>l;
254 end=(_this->val+msk)&~msk;
255 if((end|msk)>=_this->val+_this->rng){
256 l++;
257 msk>>=1;
258 end=(_this->val+msk)&~msk;
259 }
260 while(l>0){
261 ec_enc_carry_out(_this,(int)(end>>EC_CODE_SHIFT));
262 end=(end<<EC_SYM_BITS)&(EC_CODE_TOP-1);
263 l-=EC_SYM_BITS;
264 }
265 /*If we have a buffered byte flush it into the output buffer.*/
266 if(_this->rem>=0||_this->ext>0)ec_enc_carry_out(_this,0);
267 /*If we have buffered extra bits, flush them as well.*/
268 window=_this->end_window;
269 used=_this->nend_bits;
270 while(used>=EC_SYM_BITS){
271 _this->error|=ec_write_byte_at_end(_this,(unsigned)window&EC_SYM_MAX);
272 window>>=EC_SYM_BITS;
273 used-=EC_SYM_BITS;
274 }
275 /*Clear any excess space and add any remaining extra bits to the last byte.*/
276 if(!_this->error){
277 OPUS_CLEAR(_this->buf+_this->offs,
278 _this->storage-_this->offs-_this->end_offs);
279 if(used>0){
280 /*If there's no range coder data at all, give up.*/
281 if(_this->end_offs>=_this->storage)_this->error=-1;
282 else{
283 l=-l;
284 /*If we've busted, don't add too many extra bits to the last byte; it
285 would corrupt the range coder data, and that's more important.*/
286 if(_this->offs+_this->end_offs>=_this->storage&&l<used){
287 window&=(1<<l)-1;
288 _this->error=-1;
289 }
290 _this->buf[_this->storage-_this->end_offs-1]|=(unsigned char)window;
291 }
292 }
293 }
294}
diff --git a/lib/rbcodec/codecs/libopus/celt/entenc.h b/lib/rbcodec/codecs/libopus/celt/entenc.h
new file mode 100644
index 0000000000..796bc4d572
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/entenc.h
@@ -0,0 +1,110 @@
1/* Copyright (c) 2001-2011 Timothy B. Terriberry
2 Copyright (c) 2008-2009 Xiph.Org Foundation */
3/*
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#if !defined(_entenc_H)
29# define _entenc_H (1)
30# include <stddef.h>
31# include "entcode.h"
32
33/*Initializes the encoder.
34 _buf: The buffer to store output bytes in.
35 _size: The size of the buffer, in chars.*/
36void ec_enc_init(ec_enc *_this,unsigned char *_buf,opus_uint32 _size);
37/*Encodes a symbol given its frequency information.
38 The frequency information must be discernable by the decoder, assuming it
39 has read only the previous symbols from the stream.
40 It is allowable to change the frequency information, or even the entire
41 source alphabet, so long as the decoder can tell from the context of the
42 previously encoded information that it is supposed to do so as well.
43 _fl: The cumulative frequency of all symbols that come before the one to be
44 encoded.
45 _fh: The cumulative frequency of all symbols up to and including the one to
46 be encoded.
47 Together with _fl, this defines the range [_fl,_fh) in which the
48 decoded value will fall.
49 _ft: The sum of the frequencies of all the symbols*/
50void ec_encode(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned _ft);
51
52/*Equivalent to ec_encode() with _ft==1<<_bits.*/
53void ec_encode_bin(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned _bits);
54
55/* Encode a bit that has a 1/(1<<_logp) probability of being a one */
56void ec_enc_bit_logp(ec_enc *_this,int _val,unsigned _logp);
57
58/*Encodes a symbol given an "inverse" CDF table.
59 _s: The index of the symbol to encode.
60 _icdf: The "inverse" CDF, such that symbol _s falls in the range
61 [_s>0?ft-_icdf[_s-1]:0,ft-_icdf[_s]), where ft=1<<_ftb.
62 The values must be monotonically non-increasing, and the last value
63 must be 0.
64 _ftb: The number of bits of precision in the cumulative distribution.*/
65void ec_enc_icdf(ec_enc *_this,int _s,const unsigned char *_icdf,unsigned _ftb);
66
67/*Encodes a raw unsigned integer in the stream.
68 _fl: The integer to encode.
69 _ft: The number of integers that can be encoded (one more than the max).
70 This must be at least one, and no more than 2**32-1.*/
71void ec_enc_uint(ec_enc *_this,opus_uint32 _fl,opus_uint32 _ft);
72
73/*Encodes a sequence of raw bits in the stream.
74 _fl: The bits to encode.
75 _ftb: The number of bits to encode.
76 This must be between 1 and 25, inclusive.*/
77void ec_enc_bits(ec_enc *_this,opus_uint32 _fl,unsigned _ftb);
78
79/*Overwrites a few bits at the very start of an existing stream, after they
80 have already been encoded.
81 This makes it possible to have a few flags up front, where it is easy for
82 decoders to access them without parsing the whole stream, even if their
83 values are not determined until late in the encoding process, without having
84 to buffer all the intermediate symbols in the encoder.
85 In order for this to work, at least _nbits bits must have already been
86 encoded using probabilities that are an exact power of two.
87 The encoder can verify the number of encoded bits is sufficient, but cannot
88 check this latter condition.
89 _val: The bits to encode (in the least _nbits significant bits).
90 They will be decoded in order from most-significant to least.
91 _nbits: The number of bits to overwrite.
92 This must be no more than 8.*/
93void ec_enc_patch_initial_bits(ec_enc *_this,unsigned _val,unsigned _nbits);
94
95/*Compacts the data to fit in the target size.
96 This moves up the raw bits at the end of the current buffer so they are at
97 the end of the new buffer size.
98 The caller must ensure that the amount of data that's already been written
99 will fit in the new size.
100 _size: The number of bytes in the new buffer.
101 This must be large enough to contain the bits already written, and
102 must be no larger than the existing size.*/
103void ec_enc_shrink(ec_enc *_this,opus_uint32 _size);
104
105/*Indicates that there are no more symbols to encode.
106 All reamining output bytes are flushed to the output buffer.
107 ec_enc_init() must be called before the encoder can be used again.*/
108void ec_enc_done(ec_enc *_this);
109
110#endif
diff --git a/lib/rbcodec/codecs/libopus/celt/fixed_generic.h b/lib/rbcodec/codecs/libopus/celt/fixed_generic.h
new file mode 100644
index 0000000000..71e28d62a8
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/fixed_generic.h
@@ -0,0 +1,129 @@
1/* Copyright (C) 2007-2009 Xiph.Org Foundation
2 Copyright (C) 2003-2008 Jean-Marc Valin
3 Copyright (C) 2007-2008 CSIRO */
4/**
5 @file fixed_generic.h
6 @brief Generic fixed-point operations
7*/
8/*
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions
11 are met:
12
13 - Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
15
16 - Redistributions in binary form must reproduce the above copyright
17 notice, this list of conditions and the following disclaimer in the
18 documentation and/or other materials provided with the distribution.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
24 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31*/
32
33#ifndef FIXED_GENERIC_H
34#define FIXED_GENERIC_H
35
36/** Multiply a 16-bit signed value by a 16-bit unsigned value. The result is a 32-bit signed value */
37#define MULT16_16SU(a,b) ((opus_val32)(opus_val16)(a)*(opus_val32)(opus_uint16)(b))
38
39/** 16x32 multiplication, followed by a 16-bit shift right. Results fits in 32 bits */
40#define MULT16_32_Q16(a,b) ADD32(MULT16_16((a),SHR((b),16)), SHR(MULT16_16SU((a),((b)&0x0000ffff)),16))
41
42/** 16x32 multiplication, followed by a 16-bit shift right (round-to-nearest). Results fits in 32 bits */
43#define MULT16_32_P16(a,b) ADD32(MULT16_16((a),SHR((b),16)), PSHR(MULT16_16((a),((b)&0x0000ffff)),16))
44
45/** 16x32 multiplication, followed by a 15-bit shift right. Results fits in 32 bits */
46#define MULT16_32_Q15(a,b) ADD32(SHL(MULT16_16((a),SHR((b),16)),1), SHR(MULT16_16SU((a),((b)&0x0000ffff)),15))
47
48/** 32x32 multiplication, followed by a 31-bit shift right. Results fits in 32 bits */
49#define MULT32_32_Q31(a,b) ADD32(ADD32(SHL(MULT16_16(SHR((a),16),SHR((b),16)),1), SHR(MULT16_16SU(SHR((a),16),((b)&0x0000ffff)),15)), SHR(MULT16_16SU(SHR((b),16),((a)&0x0000ffff)),15))
50
51/** Compile-time conversion of float constant to 16-bit value */
52#define QCONST16(x,bits) ((opus_val16)(.5+(x)*(((opus_val32)1)<<(bits))))
53
54/** Compile-time conversion of float constant to 32-bit value */
55#define QCONST32(x,bits) ((opus_val32)(.5+(x)*(((opus_val32)1)<<(bits))))
56
57/** Negate a 16-bit value */
58#define NEG16(x) (-(x))
59/** Negate a 32-bit value */
60#define NEG32(x) (-(x))
61
62/** Change a 32-bit value into a 16-bit value. The value is assumed to fit in 16-bit, otherwise the result is undefined */
63#define EXTRACT16(x) ((opus_val16)(x))
64/** Change a 16-bit value into a 32-bit value */
65#define EXTEND32(x) ((opus_val32)(x))
66
67/** Arithmetic shift-right of a 16-bit value */
68#define SHR16(a,shift) ((a) >> (shift))
69/** Arithmetic shift-left of a 16-bit value */
70#define SHL16(a,shift) ((opus_int16)((opus_uint16)(a)<<(shift)))
71/** Arithmetic shift-right of a 32-bit value */
72#define SHR32(a,shift) ((a) >> (shift))
73/** Arithmetic shift-left of a 32-bit value */
74#define SHL32(a,shift) ((opus_int32)((opus_uint32)(a)<<(shift)))
75
76/** 32-bit arithmetic shift right with rounding-to-nearest instead of rounding down */
77#define PSHR32(a,shift) (SHR32((a)+((EXTEND32(1)<<((shift))>>1)),shift))
78/** 32-bit arithmetic shift right where the argument can be negative */
79#define VSHR32(a, shift) (((shift)>0) ? SHR32(a, shift) : SHL32(a, -(shift)))
80
81/** "RAW" macros, should not be used outside of this header file */
82#define SHR(a,shift) ((a) >> (shift))
83#define SHL(a,shift) SHL32(a,shift)
84#define PSHR(a,shift) (SHR((a)+((EXTEND32(1)<<((shift))>>1)),shift))
85#define SATURATE(x,a) (((x)>(a) ? (a) : (x)<-(a) ? -(a) : (x)))
86
87/** Shift by a and round-to-neareast 32-bit value. Result is a 16-bit value */
88#define ROUND16(x,a) (EXTRACT16(PSHR32((x),(a))))
89/** Divide by two */
90#define HALF16(x) (SHR16(x,1))
91#define HALF32(x) (SHR32(x,1))
92
93/** Add two 16-bit values */
94#define ADD16(a,b) ((opus_val16)((opus_val16)(a)+(opus_val16)(b)))
95/** Subtract two 16-bit values */
96#define SUB16(a,b) ((opus_val16)(a)-(opus_val16)(b))
97/** Add two 32-bit values */
98#define ADD32(a,b) ((opus_val32)(a)+(opus_val32)(b))
99/** Subtract two 32-bit values */
100#define SUB32(a,b) ((opus_val32)(a)-(opus_val32)(b))
101
102/** 16x16 multiplication where the result fits in 16 bits */
103#define MULT16_16_16(a,b) ((((opus_val16)(a))*((opus_val16)(b))))
104
105/* (opus_val32)(opus_val16) gives TI compiler a hint that it's 16x16->32 multiply */
106/** 16x16 multiplication where the result fits in 32 bits */
107#define MULT16_16(a,b) (((opus_val32)(opus_val16)(a))*((opus_val32)(opus_val16)(b)))
108
109/** 16x16 multiply-add where the result fits in 32 bits */
110#define MAC16_16(c,a,b) (ADD32((c),MULT16_16((a),(b))))
111/** 16x32 multiply-add, followed by a 15-bit shift right. Results fits in 32 bits */
112#define MAC16_32_Q15(c,a,b) ADD32(c,ADD32(MULT16_16((a),SHR((b),15)), SHR(MULT16_16((a),((b)&0x00007fff)),15)))
113
114#define MULT16_16_Q11_32(a,b) (SHR(MULT16_16((a),(b)),11))
115#define MULT16_16_Q13(a,b) (SHR(MULT16_16((a),(b)),13))
116#define MULT16_16_Q14(a,b) (SHR(MULT16_16((a),(b)),14))
117#define MULT16_16_Q15(a,b) (SHR(MULT16_16((a),(b)),15))
118
119#define MULT16_16_P13(a,b) (SHR(ADD32(4096,MULT16_16((a),(b))),13))
120#define MULT16_16_P14(a,b) (SHR(ADD32(8192,MULT16_16((a),(b))),14))
121#define MULT16_16_P15(a,b) (SHR(ADD32(16384,MULT16_16((a),(b))),15))
122
123/** Divide a 32-bit value by a 16-bit value. Result fits in 16 bits */
124#define DIV32_16(a,b) ((opus_val16)(((opus_val32)(a))/((opus_val16)(b))))
125
126/** Divide a 32-bit value by a 32-bit value. Result fits in 32 bits */
127#define DIV32(a,b) (((opus_val32)(a))/((opus_val32)(b)))
128
129#endif
diff --git a/lib/rbcodec/codecs/libopus/celt/float_cast.h b/lib/rbcodec/codecs/libopus/celt/float_cast.h
new file mode 100644
index 0000000000..5ded291599
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/float_cast.h
@@ -0,0 +1,140 @@
1/* Copyright (C) 2001 Erik de Castro Lopo <erikd AT mega-nerd DOT com> */
2/*
3 Redistribution and use in source and binary forms, with or without
4 modification, are permitted provided that the following conditions
5 are met:
6
7 - Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9
10 - Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
18 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*/
26
27/* Version 1.1 */
28
29#ifndef FLOAT_CAST_H
30#define FLOAT_CAST_H
31
32
33#include "arch.h"
34
35/*============================================================================
36** On Intel Pentium processors (especially PIII and probably P4), converting
37** from float to int is very slow. To meet the C specs, the code produced by
38** most C compilers targeting Pentium needs to change the FPU rounding mode
39** before the float to int conversion is performed.
40**
41** Changing the FPU rounding mode causes the FPU pipeline to be flushed. It
42** is this flushing of the pipeline which is so slow.
43**
44** Fortunately the ISO C99 specifications define the functions lrint, lrintf,
45** llrint and llrintf which fix this problem as a side effect.
46**
47** On Unix-like systems, the configure process should have detected the
48** presence of these functions. If they weren't found we have to replace them
49** here with a standard C cast.
50*/
51
52/*
53** The C99 prototypes for lrint and lrintf are as follows:
54**
55** long int lrintf (float x) ;
56** long int lrint (double x) ;
57*/
58
59/* The presence of the required functions are detected during the configure
60** process and the values HAVE_LRINT and HAVE_LRINTF are set accordingly in
61** the config.h file.
62*/
63
64#if (HAVE_LRINTF)
65
66/* These defines enable functionality introduced with the 1999 ISO C
67** standard. They must be defined before the inclusion of math.h to
68** engage them. If optimisation is enabled, these functions will be
69** inlined. With optimisation switched off, you have to link in the
70** maths library using -lm.
71*/
72
73#define _ISOC9X_SOURCE 1
74#define _ISOC99_SOURCE 1
75
76#define __USE_ISOC9X 1
77#define __USE_ISOC99 1
78
79#include <math.h>
80#define float2int(x) lrintf(x)
81
82#elif (defined(HAVE_LRINT))
83
84#define _ISOC9X_SOURCE 1
85#define _ISOC99_SOURCE 1
86
87#define __USE_ISOC9X 1
88#define __USE_ISOC99 1
89
90#include <math.h>
91#define float2int(x) lrint(x)
92
93#elif (defined(_MSC_VER) && _MSC_VER >= 1400) && (defined (WIN64) || defined (_WIN64))
94 #include <xmmintrin.h>
95
96 __inline long int float2int(float value)
97 {
98 return _mm_cvtss_si32(_mm_load_ss(&value));
99 }
100#elif (defined(_MSC_VER) && _MSC_VER >= 1400) && (defined (WIN32) || defined (_WIN32))
101 #include <math.h>
102
103 /* Win32 doesn't seem to have these functions.
104 ** Therefore implement inline versions of these functions here.
105 */
106
107 __inline long int
108 float2int (float flt)
109 { int intgr;
110
111 _asm
112 { fld flt
113 fistp intgr
114 } ;
115
116 return intgr ;
117 }
118
119#else
120
121#if (defined(__GNUC__) && defined(__STDC__) && __STDC__ && __STDC_VERSION__ >= 199901L)
122 /* supported by gcc in C99 mode, but not by all other compilers */
123 #warning "Don't have the functions lrint() and lrintf ()."
124 #warning "Replacing these functions with a standard C cast."
125#endif /* __STDC_VERSION__ >= 199901L */
126 #include <math.h>
127 #define float2int(flt) ((int)(floor(.5+flt)))
128#endif
129
130#ifndef DISABLE_FLOAT_API
131static inline opus_int16 FLOAT2INT16(float x)
132{
133 x = x*CELT_SIG_SCALE;
134 x = MAX32(x, -32768);
135 x = MIN32(x, 32767);
136 return (opus_int16)float2int(x);
137}
138#endif /* DISABLE_FLOAT_API */
139
140#endif /* FLOAT_CAST_H */
diff --git a/lib/rbcodec/codecs/libopus/celt/kiss_fft.c b/lib/rbcodec/codecs/libopus/celt/kiss_fft.c
new file mode 100644
index 0000000000..3ba075ab0c
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/kiss_fft.c
@@ -0,0 +1,722 @@
1/*Copyright (c) 2003-2004, Mark Borgerding
2 Lots of modifications by Jean-Marc Valin
3 Copyright (c) 2005-2007, Xiph.Org Foundation
4 Copyright (c) 2008, Xiph.Org Foundation, CSIRO
5
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11 * Redistributions of source code must retain the above copyright notice,
12 this list of conditions and the following disclaimer.
13 * Redistributions in binary form must reproduce the above copyright notice,
14 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 "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.*/
28
29/* This code is originally from Mark Borgerding's KISS-FFT but has been
30 heavily modified to better suit Opus */
31
32#ifndef SKIP_CONFIG_H
33# ifdef HAVE_CONFIG_H
34# include "opus_config.h"
35# endif
36#endif
37
38#include "_kiss_fft_guts.h"
39#include "arch.h"
40#include "os_support.h"
41#include "mathops.h"
42#include "stack_alloc.h"
43#include "os_support.h"
44
45/* The guts header contains all the multiplication and addition macros that are defined for
46 complex numbers. It also delares the kf_ internal functions.
47*/
48
49static void kf_bfly2(
50 kiss_fft_cpx * Fout,
51 const size_t fstride,
52 const kiss_fft_state *st,
53 int m,
54 int N,
55 int mm
56 )
57{
58 kiss_fft_cpx * Fout2;
59 const kiss_twiddle_cpx * tw1;
60 int i,j;
61 kiss_fft_cpx * Fout_beg = Fout;
62 for (i=0;i<N;i++)
63 {
64 Fout = Fout_beg + i*mm;
65 Fout2 = Fout + m;
66 tw1 = st->twiddles;
67 for(j=0;j<m;j++)
68 {
69 kiss_fft_cpx t;
70 Fout->r = SHR32(Fout->r, 1);Fout->i = SHR32(Fout->i, 1);
71 Fout2->r = SHR32(Fout2->r, 1);Fout2->i = SHR32(Fout2->i, 1);
72 C_MUL (t, *Fout2 , *tw1);
73 tw1 += fstride;
74 C_SUB( *Fout2 , *Fout , t );
75 C_ADDTO( *Fout , t );
76 ++Fout2;
77 ++Fout;
78 }
79 }
80}
81
82static void ki_bfly2(
83 kiss_fft_cpx * Fout,
84 const size_t fstride,
85 const kiss_fft_state *st,
86 int m,
87 int N,
88 int mm
89 )
90{
91 kiss_fft_cpx * Fout2;
92 const kiss_twiddle_cpx * tw1;
93 kiss_fft_cpx t;
94 int i,j;
95 kiss_fft_cpx * Fout_beg = Fout;
96 for (i=0;i<N;i++)
97 {
98 Fout = Fout_beg + i*mm;
99 Fout2 = Fout + m;
100 tw1 = st->twiddles;
101 for(j=0;j<m;j++)
102 {
103 C_MULC (t, *Fout2 , *tw1);
104 tw1 += fstride;
105 C_SUB( *Fout2 , *Fout , t );
106 C_ADDTO( *Fout , t );
107 ++Fout2;
108 ++Fout;
109 }
110 }
111}
112
113static void kf_bfly4(
114 kiss_fft_cpx * Fout,
115 const size_t fstride,
116 const kiss_fft_state *st,
117 int m,
118 int N,
119 int mm
120 )
121{
122 const kiss_twiddle_cpx *tw1,*tw2,*tw3;
123 kiss_fft_cpx scratch[6];
124 const size_t m2=2*m;
125 const size_t m3=3*m;
126 int i, j;
127
128 kiss_fft_cpx * Fout_beg = Fout;
129 for (i=0;i<N;i++)
130 {
131 Fout = Fout_beg + i*mm;
132 tw3 = tw2 = tw1 = st->twiddles;
133 for (j=0;j<m;j++)
134 {
135 C_MUL4(scratch[0],Fout[m] , *tw1 );
136 C_MUL4(scratch[1],Fout[m2] , *tw2 );
137 C_MUL4(scratch[2],Fout[m3] , *tw3 );
138
139 Fout->r = PSHR32(Fout->r, 2);
140 Fout->i = PSHR32(Fout->i, 2);
141 C_SUB( scratch[5] , *Fout, scratch[1] );
142 C_ADDTO(*Fout, scratch[1]);
143 C_ADD( scratch[3] , scratch[0] , scratch[2] );
144 C_SUB( scratch[4] , scratch[0] , scratch[2] );
145 Fout[m2].r = PSHR32(Fout[m2].r, 2);
146 Fout[m2].i = PSHR32(Fout[m2].i, 2);
147 C_SUB( Fout[m2], *Fout, scratch[3] );
148 tw1 += fstride;
149 tw2 += fstride*2;
150 tw3 += fstride*3;
151 C_ADDTO( *Fout , scratch[3] );
152
153 Fout[m].r = scratch[5].r + scratch[4].i;
154 Fout[m].i = scratch[5].i - scratch[4].r;
155 Fout[m3].r = scratch[5].r - scratch[4].i;
156 Fout[m3].i = scratch[5].i + scratch[4].r;
157 ++Fout;
158 }
159 }
160}
161
162static void ki_bfly4(
163 kiss_fft_cpx * Fout,
164 const size_t fstride,
165 const kiss_fft_state *st,
166 int m,
167 int N,
168 int mm
169 )
170{
171 const kiss_twiddle_cpx *tw1,*tw2,*tw3;
172 kiss_fft_cpx scratch[6];
173 const size_t m2=2*m;
174 const size_t m3=3*m;
175 int i, j;
176
177 kiss_fft_cpx * Fout_beg = Fout;
178 for (i=0;i<N;i++)
179 {
180 Fout = Fout_beg + i*mm;
181 tw3 = tw2 = tw1 = st->twiddles;
182 for (j=0;j<m;j++)
183 {
184 C_MULC(scratch[0],Fout[m] , *tw1 );
185 C_MULC(scratch[1],Fout[m2] , *tw2 );
186 C_MULC(scratch[2],Fout[m3] , *tw3 );
187
188 C_SUB( scratch[5] , *Fout, scratch[1] );
189 C_ADDTO(*Fout, scratch[1]);
190 C_ADD( scratch[3] , scratch[0] , scratch[2] );
191 C_SUB( scratch[4] , scratch[0] , scratch[2] );
192 C_SUB( Fout[m2], *Fout, scratch[3] );
193 tw1 += fstride;
194 tw2 += fstride*2;
195 tw3 += fstride*3;
196 C_ADDTO( *Fout , scratch[3] );
197
198 Fout[m].r = scratch[5].r - scratch[4].i;
199 Fout[m].i = scratch[5].i + scratch[4].r;
200 Fout[m3].r = scratch[5].r + scratch[4].i;
201 Fout[m3].i = scratch[5].i - scratch[4].r;
202 ++Fout;
203 }
204 }
205}
206
207#ifndef RADIX_TWO_ONLY
208
209static void kf_bfly3(
210 kiss_fft_cpx * Fout,
211 const size_t fstride,
212 const kiss_fft_state *st,
213 int m,
214 int N,
215 int mm
216 )
217{
218 int i;
219 size_t k;
220 const size_t m2 = 2*m;
221 const kiss_twiddle_cpx *tw1,*tw2;
222 kiss_fft_cpx scratch[5];
223 kiss_twiddle_cpx epi3;
224
225 kiss_fft_cpx * Fout_beg = Fout;
226 epi3 = st->twiddles[fstride*m];
227 for (i=0;i<N;i++)
228 {
229 Fout = Fout_beg + i*mm;
230 tw1=tw2=st->twiddles;
231 k=m;
232 do {
233 C_FIXDIV(*Fout,3); C_FIXDIV(Fout[m],3); C_FIXDIV(Fout[m2],3);
234
235 C_MUL(scratch[1],Fout[m] , *tw1);
236 C_MUL(scratch[2],Fout[m2] , *tw2);
237
238 C_ADD(scratch[3],scratch[1],scratch[2]);
239 C_SUB(scratch[0],scratch[1],scratch[2]);
240 tw1 += fstride;
241 tw2 += fstride*2;
242
243 Fout[m].r = Fout->r - HALF_OF(scratch[3].r);
244 Fout[m].i = Fout->i - HALF_OF(scratch[3].i);
245
246 C_MULBYSCALAR( scratch[0] , epi3.i );
247
248 C_ADDTO(*Fout,scratch[3]);
249
250 Fout[m2].r = Fout[m].r + scratch[0].i;
251 Fout[m2].i = Fout[m].i - scratch[0].r;
252
253 Fout[m].r -= scratch[0].i;
254 Fout[m].i += scratch[0].r;
255
256 ++Fout;
257 } while(--k);
258 }
259}
260
261static void ki_bfly3(
262 kiss_fft_cpx * Fout,
263 const size_t fstride,
264 const kiss_fft_state *st,
265 int m,
266 int N,
267 int mm
268 )
269{
270 int i, k;
271 const size_t m2 = 2*m;
272 const kiss_twiddle_cpx *tw1,*tw2;
273 kiss_fft_cpx scratch[5];
274 kiss_twiddle_cpx epi3;
275
276 kiss_fft_cpx * Fout_beg = Fout;
277 epi3 = st->twiddles[fstride*m];
278 for (i=0;i<N;i++)
279 {
280 Fout = Fout_beg + i*mm;
281 tw1=tw2=st->twiddles;
282 k=m;
283 do{
284
285 C_MULC(scratch[1],Fout[m] , *tw1);
286 C_MULC(scratch[2],Fout[m2] , *tw2);
287
288 C_ADD(scratch[3],scratch[1],scratch[2]);
289 C_SUB(scratch[0],scratch[1],scratch[2]);
290 tw1 += fstride;
291 tw2 += fstride*2;
292
293 Fout[m].r = Fout->r - HALF_OF(scratch[3].r);
294 Fout[m].i = Fout->i - HALF_OF(scratch[3].i);
295
296 C_MULBYSCALAR( scratch[0] , -epi3.i );
297
298 C_ADDTO(*Fout,scratch[3]);
299
300 Fout[m2].r = Fout[m].r + scratch[0].i;
301 Fout[m2].i = Fout[m].i - scratch[0].r;
302
303 Fout[m].r -= scratch[0].i;
304 Fout[m].i += scratch[0].r;
305
306 ++Fout;
307 }while(--k);
308 }
309}
310
311static void kf_bfly5(
312 kiss_fft_cpx * Fout,
313 const size_t fstride,
314 const kiss_fft_state *st,
315 int m,
316 int N,
317 int mm
318 )
319{
320 kiss_fft_cpx *Fout0,*Fout1,*Fout2,*Fout3,*Fout4;
321 int i, u;
322 kiss_fft_cpx scratch[13];
323 const kiss_twiddle_cpx * twiddles = st->twiddles;
324 const kiss_twiddle_cpx *tw;
325 kiss_twiddle_cpx ya,yb;
326 kiss_fft_cpx * Fout_beg = Fout;
327
328 ya = twiddles[fstride*m];
329 yb = twiddles[fstride*2*m];
330 tw=st->twiddles;
331
332 for (i=0;i<N;i++)
333 {
334 Fout = Fout_beg + i*mm;
335 Fout0=Fout;
336 Fout1=Fout0+m;
337 Fout2=Fout0+2*m;
338 Fout3=Fout0+3*m;
339 Fout4=Fout0+4*m;
340
341 for ( u=0; u<m; ++u ) {
342 C_FIXDIV( *Fout0,5); C_FIXDIV( *Fout1,5); C_FIXDIV( *Fout2,5); C_FIXDIV( *Fout3,5); C_FIXDIV( *Fout4,5);
343 scratch[0] = *Fout0;
344
345 C_MUL(scratch[1] ,*Fout1, tw[u*fstride]);
346 C_MUL(scratch[2] ,*Fout2, tw[2*u*fstride]);
347 C_MUL(scratch[3] ,*Fout3, tw[3*u*fstride]);
348 C_MUL(scratch[4] ,*Fout4, tw[4*u*fstride]);
349
350 C_ADD( scratch[7],scratch[1],scratch[4]);
351 C_SUB( scratch[10],scratch[1],scratch[4]);
352 C_ADD( scratch[8],scratch[2],scratch[3]);
353 C_SUB( scratch[9],scratch[2],scratch[3]);
354
355 Fout0->r += scratch[7].r + scratch[8].r;
356 Fout0->i += scratch[7].i + scratch[8].i;
357
358 scratch[5].r = scratch[0].r + S_MUL(scratch[7].r,ya.r) + S_MUL(scratch[8].r,yb.r);
359 scratch[5].i = scratch[0].i + S_MUL(scratch[7].i,ya.r) + S_MUL(scratch[8].i,yb.r);
360
361 scratch[6].r = S_MUL(scratch[10].i,ya.i) + S_MUL(scratch[9].i,yb.i);
362 scratch[6].i = -S_MUL(scratch[10].r,ya.i) - S_MUL(scratch[9].r,yb.i);
363
364 C_SUB(*Fout1,scratch[5],scratch[6]);
365 C_ADD(*Fout4,scratch[5],scratch[6]);
366
367 scratch[11].r = scratch[0].r + S_MUL(scratch[7].r,yb.r) + S_MUL(scratch[8].r,ya.r);
368 scratch[11].i = scratch[0].i + S_MUL(scratch[7].i,yb.r) + S_MUL(scratch[8].i,ya.r);
369 scratch[12].r = - S_MUL(scratch[10].i,yb.i) + S_MUL(scratch[9].i,ya.i);
370 scratch[12].i = S_MUL(scratch[10].r,yb.i) - S_MUL(scratch[9].r,ya.i);
371
372 C_ADD(*Fout2,scratch[11],scratch[12]);
373 C_SUB(*Fout3,scratch[11],scratch[12]);
374
375 ++Fout0;++Fout1;++Fout2;++Fout3;++Fout4;
376 }
377 }
378}
379
380static void ki_bfly5(
381 kiss_fft_cpx * Fout,
382 const size_t fstride,
383 const kiss_fft_state *st,
384 int m,
385 int N,
386 int mm
387 )
388{
389 kiss_fft_cpx *Fout0,*Fout1,*Fout2,*Fout3,*Fout4;
390 int i, u;
391 kiss_fft_cpx scratch[13];
392 const kiss_twiddle_cpx * twiddles = st->twiddles;
393 const kiss_twiddle_cpx *tw;
394 kiss_twiddle_cpx ya,yb;
395 kiss_fft_cpx * Fout_beg = Fout;
396
397 ya = twiddles[fstride*m];
398 yb = twiddles[fstride*2*m];
399 tw=st->twiddles;
400
401 for (i=0;i<N;i++)
402 {
403 Fout = Fout_beg + i*mm;
404 Fout0=Fout;
405 Fout1=Fout0+m;
406 Fout2=Fout0+2*m;
407 Fout3=Fout0+3*m;
408 Fout4=Fout0+4*m;
409
410 for ( u=0; u<m; ++u ) {
411 scratch[0] = *Fout0;
412
413 C_MULC(scratch[1] ,*Fout1, tw[u*fstride]);
414 C_MULC(scratch[2] ,*Fout2, tw[2*u*fstride]);
415 C_MULC(scratch[3] ,*Fout3, tw[3*u*fstride]);
416 C_MULC(scratch[4] ,*Fout4, tw[4*u*fstride]);
417
418 C_ADD( scratch[7],scratch[1],scratch[4]);
419 C_SUB( scratch[10],scratch[1],scratch[4]);
420 C_ADD( scratch[8],scratch[2],scratch[3]);
421 C_SUB( scratch[9],scratch[2],scratch[3]);
422
423 Fout0->r += scratch[7].r + scratch[8].r;
424 Fout0->i += scratch[7].i + scratch[8].i;
425
426 scratch[5].r = scratch[0].r + S_MUL(scratch[7].r,ya.r) + S_MUL(scratch[8].r,yb.r);
427 scratch[5].i = scratch[0].i + S_MUL(scratch[7].i,ya.r) + S_MUL(scratch[8].i,yb.r);
428
429 scratch[6].r = -S_MUL(scratch[10].i,ya.i) - S_MUL(scratch[9].i,yb.i);
430 scratch[6].i = S_MUL(scratch[10].r,ya.i) + S_MUL(scratch[9].r,yb.i);
431
432 C_SUB(*Fout1,scratch[5],scratch[6]);
433 C_ADD(*Fout4,scratch[5],scratch[6]);
434
435 scratch[11].r = scratch[0].r + S_MUL(scratch[7].r,yb.r) + S_MUL(scratch[8].r,ya.r);
436 scratch[11].i = scratch[0].i + S_MUL(scratch[7].i,yb.r) + S_MUL(scratch[8].i,ya.r);
437 scratch[12].r = S_MUL(scratch[10].i,yb.i) - S_MUL(scratch[9].i,ya.i);
438 scratch[12].i = -S_MUL(scratch[10].r,yb.i) + S_MUL(scratch[9].r,ya.i);
439
440 C_ADD(*Fout2,scratch[11],scratch[12]);
441 C_SUB(*Fout3,scratch[11],scratch[12]);
442
443 ++Fout0;++Fout1;++Fout2;++Fout3;++Fout4;
444 }
445 }
446}
447
448#endif
449
450
451#ifdef CUSTOM_MODES
452
453static
454void compute_bitrev_table(
455 int Fout,
456 opus_int16 *f,
457 const size_t fstride,
458 int in_stride,
459 opus_int16 * factors,
460 const kiss_fft_state *st
461 )
462{
463 const int p=*factors++; /* the radix */
464 const int m=*factors++; /* stage's fft length/p */
465
466 /*printf ("fft %d %d %d %d %d %d\n", p*m, m, p, s2, fstride*in_stride, N);*/
467 if (m==1)
468 {
469 int j;
470 for (j=0;j<p;j++)
471 {
472 *f = Fout+j;
473 f += fstride*in_stride;
474 }
475 } else {
476 int j;
477 for (j=0;j<p;j++)
478 {
479 compute_bitrev_table( Fout , f, fstride*p, in_stride, factors,st);
480 f += fstride*in_stride;
481 Fout += m;
482 }
483 }
484}
485
486/* facbuf is populated by p1,m1,p2,m2, ...
487 where
488 p[i] * m[i] = m[i-1]
489 m0 = n */
490static
491int kf_factor(int n,opus_int16 * facbuf)
492{
493 int p=4;
494
495 /*factor out powers of 4, powers of 2, then any remaining primes */
496 do {
497 while (n % p) {
498 switch (p) {
499 case 4: p = 2; break;
500 case 2: p = 3; break;
501 default: p += 2; break;
502 }
503 if (p>32000 || (opus_int32)p*(opus_int32)p > n)
504 p = n; /* no more factors, skip to end */
505 }
506 n /= p;
507#ifdef RADIX_TWO_ONLY
508 if (p!=2 && p != 4)
509#else
510 if (p>5)
511#endif
512 {
513 return 0;
514 }
515 *facbuf++ = p;
516 *facbuf++ = n;
517 } while (n > 1);
518 return 1;
519}
520
521static void compute_twiddles(kiss_twiddle_cpx *twiddles, int nfft)
522{
523 int i;
524#ifdef FIXED_POINT
525 for (i=0;i<nfft;++i) {
526 opus_val32 phase = -i;
527 kf_cexp2(twiddles+i, DIV32(SHL32(phase,17),nfft));
528 }
529#else
530 for (i=0;i<nfft;++i) {
531 const double pi=3.14159265358979323846264338327;
532 double phase = ( -2*pi /nfft ) * i;
533 kf_cexp(twiddles+i, phase );
534 }
535#endif
536}
537
538/*
539 *
540 * Allocates all necessary storage space for the fft and ifft.
541 * The return value is a contiguous block of memory. As such,
542 * It can be freed with free().
543 * */
544kiss_fft_state *opus_fft_alloc_twiddles(int nfft,void * mem,size_t * lenmem, const kiss_fft_state *base)
545{
546 kiss_fft_state *st=NULL;
547 size_t memneeded = sizeof(struct kiss_fft_state); /* twiddle factors*/
548
549 if ( lenmem==NULL ) {
550 st = ( kiss_fft_state*)KISS_FFT_MALLOC( memneeded );
551 }else{
552 if (mem != NULL && *lenmem >= memneeded)
553 st = (kiss_fft_state*)mem;
554 *lenmem = memneeded;
555 }
556 if (st) {
557 opus_int16 *bitrev;
558 kiss_twiddle_cpx *twiddles;
559
560 st->nfft=nfft;
561#ifndef FIXED_POINT
562 st->scale = 1.f/nfft;
563#endif
564 if (base != NULL)
565 {
566 st->twiddles = base->twiddles;
567 st->shift = 0;
568 while (nfft<<st->shift != base->nfft && st->shift < 32)
569 st->shift++;
570 if (st->shift>=32)
571 goto fail;
572 } else {
573 st->twiddles = twiddles = (kiss_twiddle_cpx*)KISS_FFT_MALLOC(sizeof(kiss_twiddle_cpx)*nfft);
574 compute_twiddles(twiddles, nfft);
575 st->shift = -1;
576 }
577 if (!kf_factor(nfft,st->factors))
578 {
579 goto fail;
580 }
581
582 /* bitrev */
583 st->bitrev = bitrev = (opus_int16*)KISS_FFT_MALLOC(sizeof(opus_int16)*nfft);
584 if (st->bitrev==NULL)
585 goto fail;
586 compute_bitrev_table(0, bitrev, 1,1, st->factors,st);
587 }
588 return st;
589fail:
590 opus_fft_free(st);
591 return NULL;
592}
593
594kiss_fft_state *opus_fft_alloc(int nfft,void * mem,size_t * lenmem )
595{
596 return opus_fft_alloc_twiddles(nfft, mem, lenmem, NULL);
597}
598
599void opus_fft_free(const kiss_fft_state *cfg)
600{
601 if (cfg)
602 {
603 opus_free((opus_int16*)cfg->bitrev);
604 if (cfg->shift < 0)
605 opus_free((kiss_twiddle_cpx*)cfg->twiddles);
606 opus_free((kiss_fft_state*)cfg);
607 }
608}
609
610#endif /* CUSTOM_MODES */
611
612void opus_fft(const kiss_fft_state *st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout)
613{
614 int m2, m;
615 int p;
616 int L;
617 int fstride[MAXFACTORS];
618 int i;
619 int shift;
620
621 /* st->shift can be -1 */
622 shift = st->shift>0 ? st->shift : 0;
623
624 celt_assert2 (fin != fout, "In-place FFT not supported");
625 /* Bit-reverse the input */
626 for (i=0;i<st->nfft;i++)
627 {
628 fout[st->bitrev[i]] = fin[i];
629#ifndef FIXED_POINT
630 fout[st->bitrev[i]].r *= st->scale;
631 fout[st->bitrev[i]].i *= st->scale;
632#endif
633 }
634
635 fstride[0] = 1;
636 L=0;
637 do {
638 p = st->factors[2*L];
639 m = st->factors[2*L+1];
640 fstride[L+1] = fstride[L]*p;
641 L++;
642 } while(m!=1);
643 m = st->factors[2*L-1];
644 for (i=L-1;i>=0;i--)
645 {
646 if (i!=0)
647 m2 = st->factors[2*i-1];
648 else
649 m2 = 1;
650 switch (st->factors[2*i])
651 {
652 case 2:
653 kf_bfly2(fout,fstride[i]<<shift,st,m, fstride[i], m2);
654 break;
655 case 4:
656 kf_bfly4(fout,fstride[i]<<shift,st,m, fstride[i], m2);
657 break;
658 #ifndef RADIX_TWO_ONLY
659 case 3:
660 kf_bfly3(fout,fstride[i]<<shift,st,m, fstride[i], m2);
661 break;
662 case 5:
663 kf_bfly5(fout,fstride[i]<<shift,st,m, fstride[i], m2);
664 break;
665 #endif
666 }
667 m = m2;
668 }
669}
670
671void opus_ifft(const kiss_fft_state *st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout)
672{
673 int m2, m;
674 int p;
675 int L;
676 int fstride[MAXFACTORS];
677 int i;
678 int shift;
679
680 /* st->shift can be -1 */
681 shift = st->shift>0 ? st->shift : 0;
682 celt_assert2 (fin != fout, "In-place FFT not supported");
683 /* Bit-reverse the input */
684 for (i=0;i<st->nfft;i++)
685 fout[st->bitrev[i]] = fin[i];
686
687 fstride[0] = 1;
688 L=0;
689 do {
690 p = st->factors[2*L];
691 m = st->factors[2*L+1];
692 fstride[L+1] = fstride[L]*p;
693 L++;
694 } while(m!=1);
695 m = st->factors[2*L-1];
696 for (i=L-1;i>=0;i--)
697 {
698 if (i!=0)
699 m2 = st->factors[2*i-1];
700 else
701 m2 = 1;
702 switch (st->factors[2*i])
703 {
704 case 2:
705 ki_bfly2(fout,fstride[i]<<shift,st,m, fstride[i], m2);
706 break;
707 case 4:
708 ki_bfly4(fout,fstride[i]<<shift,st,m, fstride[i], m2);
709 break;
710#ifndef RADIX_TWO_ONLY
711 case 3:
712 ki_bfly3(fout,fstride[i]<<shift,st,m, fstride[i], m2);
713 break;
714 case 5:
715 ki_bfly5(fout,fstride[i]<<shift,st,m, fstride[i], m2);
716 break;
717#endif
718 }
719 m = m2;
720 }
721}
722
diff --git a/lib/rbcodec/codecs/libopus/celt/kiss_fft.h b/lib/rbcodec/codecs/libopus/celt/kiss_fft.h
new file mode 100644
index 0000000000..66332e3bb9
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/kiss_fft.h
@@ -0,0 +1,139 @@
1/*Copyright (c) 2003-2004, Mark Borgerding
2 Lots of modifications by Jean-Marc Valin
3 Copyright (c) 2005-2007, Xiph.Org Foundation
4 Copyright (c) 2008, Xiph.Org Foundation, CSIRO
5
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11 * Redistributions of source code must retain the above copyright notice,
12 this list of conditions and the following disclaimer.
13 * Redistributions in binary form must reproduce the above copyright notice,
14 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 "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.*/
28
29#ifndef KISS_FFT_H
30#define KISS_FFT_H
31
32#include <stdlib.h>
33#include <math.h>
34#include "arch.h"
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40#ifdef USE_SIMD
41# include <xmmintrin.h>
42# define kiss_fft_scalar __m128
43#define KISS_FFT_MALLOC(nbytes) memalign(16,nbytes)
44#else
45#define KISS_FFT_MALLOC opus_alloc
46#endif
47
48#ifdef FIXED_POINT
49#include "arch.h"
50
51# define kiss_fft_scalar opus_int32
52# define kiss_twiddle_scalar opus_int16
53
54
55#else
56# ifndef kiss_fft_scalar
57/* default is float */
58# define kiss_fft_scalar float
59# define kiss_twiddle_scalar float
60# define KF_SUFFIX _celt_single
61# endif
62#endif
63
64typedef struct {
65 kiss_fft_scalar r;
66 kiss_fft_scalar i;
67}kiss_fft_cpx;
68
69typedef struct {
70 kiss_twiddle_scalar r;
71 kiss_twiddle_scalar i;
72}kiss_twiddle_cpx;
73
74#define MAXFACTORS 8
75/* e.g. an fft of length 128 has 4 factors
76 as far as kissfft is concerned
77 4*4*4*2
78 */
79
80typedef struct kiss_fft_state{
81 int nfft;
82#ifndef FIXED_POINT
83 kiss_fft_scalar scale;
84#endif
85 int shift;
86 opus_int16 factors[2*MAXFACTORS];
87 const opus_int16 *bitrev;
88 const kiss_twiddle_cpx *twiddles;
89} kiss_fft_state;
90
91/*typedef struct kiss_fft_state* kiss_fft_cfg;*/
92
93/**
94 * opus_fft_alloc
95 *
96 * Initialize a FFT (or IFFT) algorithm's cfg/state buffer.
97 *
98 * typical usage: kiss_fft_cfg mycfg=opus_fft_alloc(1024,0,NULL,NULL);
99 *
100 * The return value from fft_alloc is a cfg buffer used internally
101 * by the fft routine or NULL.
102 *
103 * If lenmem is NULL, then opus_fft_alloc will allocate a cfg buffer using malloc.
104 * The returned value should be free()d when done to avoid memory leaks.
105 *
106 * The state can be placed in a user supplied buffer 'mem':
107 * If lenmem is not NULL and mem is not NULL and *lenmem is large enough,
108 * then the function places the cfg in mem and the size used in *lenmem
109 * and returns mem.
110 *
111 * If lenmem is not NULL and ( mem is NULL or *lenmem is not large enough),
112 * then the function returns NULL and places the minimum cfg
113 * buffer size in *lenmem.
114 * */
115
116kiss_fft_state *opus_fft_alloc_twiddles(int nfft,void * mem,size_t * lenmem, const kiss_fft_state *base);
117
118kiss_fft_state *opus_fft_alloc(int nfft,void * mem,size_t * lenmem);
119
120/**
121 * opus_fft(cfg,in_out_buf)
122 *
123 * Perform an FFT on a complex input buffer.
124 * for a forward FFT,
125 * fin should be f[0] , f[1] , ... ,f[nfft-1]
126 * fout will be F[0] , F[1] , ... ,F[nfft-1]
127 * Note that each element is complex and can be accessed like
128 f[k].r and f[k].i
129 * */
130void opus_fft(const kiss_fft_state *cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout);
131void opus_ifft(const kiss_fft_state *cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout);
132
133void opus_fft_free(const kiss_fft_state *cfg);
134
135#ifdef __cplusplus
136}
137#endif
138
139#endif
diff --git a/lib/rbcodec/codecs/libopus/celt/laplace.c b/lib/rbcodec/codecs/libopus/celt/laplace.c
new file mode 100644
index 0000000000..6fa4009d57
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/laplace.c
@@ -0,0 +1,134 @@
1/* Copyright (c) 2007 CSIRO
2 Copyright (c) 2007-2009 Xiph.Org Foundation
3 Written by Jean-Marc Valin */
4/*
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
12 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
20 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28
29#ifdef HAVE_CONFIG_H
30#include "opus_config.h"
31#endif
32
33#include "laplace.h"
34#include "mathops.h"
35
36/* The minimum probability of an energy delta (out of 32768). */
37#define LAPLACE_LOG_MINP (0)
38#define LAPLACE_MINP (1<<LAPLACE_LOG_MINP)
39/* The minimum number of guaranteed representable energy deltas (in one
40 direction). */
41#define LAPLACE_NMIN (16)
42
43/* When called, decay is positive and at most 11456. */
44static unsigned ec_laplace_get_freq1(unsigned fs0, int decay)
45{
46 unsigned ft;
47 ft = 32768 - LAPLACE_MINP*(2*LAPLACE_NMIN) - fs0;
48 return ft*(opus_int32)(16384-decay)>>15;
49}
50
51void ec_laplace_encode(ec_enc *enc, int *value, unsigned fs, int decay)
52{
53 unsigned fl;
54 int val = *value;
55 fl = 0;
56 if (val)
57 {
58 int s;
59 int i;
60 s = -(val<0);
61 val = (val+s)^s;
62 fl = fs;
63 fs = ec_laplace_get_freq1(fs, decay);
64 /* Search the decaying part of the PDF.*/
65 for (i=1; fs > 0 && i < val; i++)
66 {
67 fs *= 2;
68 fl += fs+2*LAPLACE_MINP;
69 fs = (fs*(opus_int32)decay)>>15;
70 }
71 /* Everything beyond that has probability LAPLACE_MINP. */
72 if (!fs)
73 {
74 int di;
75 int ndi_max;
76 ndi_max = (32768-fl+LAPLACE_MINP-1)>>LAPLACE_LOG_MINP;
77 ndi_max = (ndi_max-s)>>1;
78 di = IMIN(val - i, ndi_max - 1);
79 fl += (2*di+1+s)*LAPLACE_MINP;
80 fs = IMIN(LAPLACE_MINP, 32768-fl);
81 *value = (i+di+s)^s;
82 }
83 else
84 {
85 fs += LAPLACE_MINP;
86 fl += fs&~s;
87 }
88 celt_assert(fl+fs<=32768);
89 celt_assert(fs>0);
90 }
91 ec_encode_bin(enc, fl, fl+fs, 15);
92}
93
94int ec_laplace_decode(ec_dec *dec, unsigned fs, int decay)
95{
96 int val=0;
97 unsigned fl;
98 unsigned fm;
99 fm = ec_decode_bin(dec, 15);
100 fl = 0;
101 if (fm >= fs)
102 {
103 val++;
104 fl = fs;
105 fs = ec_laplace_get_freq1(fs, decay)+LAPLACE_MINP;
106 /* Search the decaying part of the PDF.*/
107 while(fs > LAPLACE_MINP && fm >= fl+2*fs)
108 {
109 fs *= 2;
110 fl += fs;
111 fs = ((fs-2*LAPLACE_MINP)*(opus_int32)decay)>>15;
112 fs += LAPLACE_MINP;
113 val++;
114 }
115 /* Everything beyond that has probability LAPLACE_MINP. */
116 if (fs <= LAPLACE_MINP)
117 {
118 int di;
119 di = (fm-fl)>>(LAPLACE_LOG_MINP+1);
120 val += di;
121 fl += 2*di*LAPLACE_MINP;
122 }
123 if (fm < fl+fs)
124 val = -val;
125 else
126 fl += fs;
127 }
128 celt_assert(fl<32768);
129 celt_assert(fs>0);
130 celt_assert(fl<=fm);
131 celt_assert(fm<IMIN(fl+fs,32768));
132 ec_dec_update(dec, fl, IMIN(fl+fs,32768), 32768);
133 return val;
134}
diff --git a/lib/rbcodec/codecs/libopus/celt/laplace.h b/lib/rbcodec/codecs/libopus/celt/laplace.h
new file mode 100644
index 0000000000..46c14b5da5
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/laplace.h
@@ -0,0 +1,48 @@
1/* Copyright (c) 2007 CSIRO
2 Copyright (c) 2007-2009 Xiph.Org Foundation
3 Written by Jean-Marc Valin */
4/*
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
12 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
20 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28
29#include "entenc.h"
30#include "entdec.h"
31
32/** Encode a value that is assumed to be the realisation of a
33 Laplace-distributed random process
34 @param enc Entropy encoder state
35 @param value Value to encode
36 @param fs Probability of 0, multiplied by 32768
37 @param decay Probability of the value +/- 1, multiplied by 16384
38*/
39void ec_laplace_encode(ec_enc *enc, int *value, unsigned fs, int decay);
40
41/** Decode a value that is assumed to be the realisation of a
42 Laplace-distributed random process
43 @param dec Entropy decoder state
44 @param fs Probability of 0, multiplied by 32768
45 @param decay Probability of the value +/- 1, multiplied by 16384
46 @return Value decoded
47 */
48int ec_laplace_decode(ec_dec *dec, unsigned fs, int decay);
diff --git a/lib/rbcodec/codecs/libopus/celt/mathops.c b/lib/rbcodec/codecs/libopus/celt/mathops.c
new file mode 100644
index 0000000000..1af6672592
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/mathops.c
@@ -0,0 +1,206 @@
1/* Copyright (c) 2002-2008 Jean-Marc Valin
2 Copyright (c) 2007-2008 CSIRO
3 Copyright (c) 2007-2009 Xiph.Org Foundation
4 Written by Jean-Marc Valin */
5/**
6 @file mathops.h
7 @brief Various math functions
8*/
9/*
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions
12 are met:
13
14 - Redistributions of source code must retain the above copyright
15 notice, this list of conditions and the following disclaimer.
16
17 - Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in the
19 documentation and/or other materials provided with the distribution.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*/
33
34#ifdef HAVE_CONFIG_H
35#include "opus_config.h"
36#endif
37
38#include "mathops.h"
39
40/*Compute floor(sqrt(_val)) with exact arithmetic.
41 This has been tested on all possible 32-bit inputs.*/
42unsigned isqrt32(opus_uint32 _val){
43 unsigned b;
44 unsigned g;
45 int bshift;
46 /*Uses the second method from
47 http://www.azillionmonkeys.com/qed/sqroot.html
48 The main idea is to search for the largest binary digit b such that
49 (g+b)*(g+b) <= _val, and add it to the solution g.*/
50 g=0;
51 bshift=(EC_ILOG(_val)-1)>>1;
52 b=1U<<bshift;
53 do{
54 opus_uint32 t;
55 t=(((opus_uint32)g<<1)+b)<<bshift;
56 if(t<=_val){
57 g+=b;
58 _val-=t;
59 }
60 b>>=1;
61 bshift--;
62 }
63 while(bshift>=0);
64 return g;
65}
66
67#ifdef FIXED_POINT
68
69opus_val32 frac_div32(opus_val32 a, opus_val32 b)
70{
71 opus_val16 rcp;
72 opus_val32 result, rem;
73 int shift = celt_ilog2(b)-29;
74 a = VSHR32(a,shift);
75 b = VSHR32(b,shift);
76 /* 16-bit reciprocal */
77 rcp = ROUND16(celt_rcp(ROUND16(b,16)),3);
78 result = MULT16_32_Q15(rcp, a);
79 rem = PSHR32(a,2)-MULT32_32_Q31(result, b);
80 result = ADD32(result, SHL32(MULT16_32_Q15(rcp, rem),2));
81 if (result >= 536870912) /* 2^29 */
82 return 2147483647; /* 2^31 - 1 */
83 else if (result <= -536870912) /* -2^29 */
84 return -2147483647; /* -2^31 */
85 else
86 return SHL32(result, 2);
87}
88
89/** Reciprocal sqrt approximation in the range [0.25,1) (Q16 in, Q14 out) */
90opus_val16 celt_rsqrt_norm(opus_val32 x)
91{
92 opus_val16 n;
93 opus_val16 r;
94 opus_val16 r2;
95 opus_val16 y;
96 /* Range of n is [-16384,32767] ([-0.5,1) in Q15). */
97 n = x-32768;
98 /* Get a rough initial guess for the root.
99 The optimal minimax quadratic approximation (using relative error) is
100 r = 1.437799046117536+n*(-0.823394375837328+n*0.4096419668459485).
101 Coefficients here, and the final result r, are Q14.*/
102 r = ADD16(23557, MULT16_16_Q15(n, ADD16(-13490, MULT16_16_Q15(n, 6713))));
103 /* We want y = x*r*r-1 in Q15, but x is 32-bit Q16 and r is Q14.
104 We can compute the result from n and r using Q15 multiplies with some
105 adjustment, carefully done to avoid overflow.
106 Range of y is [-1564,1594]. */
107 r2 = MULT16_16_Q15(r, r);
108 y = SHL16(SUB16(ADD16(MULT16_16_Q15(r2, n), r2), 16384), 1);
109 /* Apply a 2nd-order Householder iteration: r += r*y*(y*0.375-0.5).
110 This yields the Q14 reciprocal square root of the Q16 x, with a maximum
111 relative error of 1.04956E-4, a (relative) RMSE of 2.80979E-5, and a
112 peak absolute error of 2.26591/16384. */
113 return ADD16(r, MULT16_16_Q15(r, MULT16_16_Q15(y,
114 SUB16(MULT16_16_Q15(y, 12288), 16384))));
115}
116
117/** Sqrt approximation (QX input, QX/2 output) */
118opus_val32 celt_sqrt(opus_val32 x)
119{
120 int k;
121 opus_val16 n;
122 opus_val32 rt;
123 static const opus_val16 C[5] = {23175, 11561, -3011, 1699, -664};
124 if (x==0)
125 return 0;
126 k = (celt_ilog2(x)>>1)-7;
127 x = VSHR32(x, 2*k);
128 n = x-32768;
129 rt = ADD16(C[0], MULT16_16_Q15(n, ADD16(C[1], MULT16_16_Q15(n, ADD16(C[2],
130 MULT16_16_Q15(n, ADD16(C[3], MULT16_16_Q15(n, (C[4])))))))));
131 rt = VSHR32(rt,7-k);
132 return rt;
133}
134
135#define L1 32767
136#define L2 -7651
137#define L3 8277
138#define L4 -626
139
140static inline opus_val16 _celt_cos_pi_2(opus_val16 x)
141{
142 opus_val16 x2;
143
144 x2 = MULT16_16_P15(x,x);
145 return ADD16(1,MIN16(32766,ADD32(SUB16(L1,x2), MULT16_16_P15(x2, ADD32(L2, MULT16_16_P15(x2, ADD32(L3, MULT16_16_P15(L4, x2
146 ))))))));
147}
148
149#undef L1
150#undef L2
151#undef L3
152#undef L4
153
154opus_val16 celt_cos_norm(opus_val32 x)
155{
156 x = x&0x0001ffff;
157 if (x>SHL32(EXTEND32(1), 16))
158 x = SUB32(SHL32(EXTEND32(1), 17),x);
159 if (x&0x00007fff)
160 {
161 if (x<SHL32(EXTEND32(1), 15))
162 {
163 return _celt_cos_pi_2(EXTRACT16(x));
164 } else {
165 return NEG32(_celt_cos_pi_2(EXTRACT16(65536-x)));
166 }
167 } else {
168 if (x&0x0000ffff)
169 return 0;
170 else if (x&0x0001ffff)
171 return -32767;
172 else
173 return 32767;
174 }
175}
176
177/** Reciprocal approximation (Q15 input, Q16 output) */
178opus_val32 celt_rcp(opus_val32 x)
179{
180 int i;
181 opus_val16 n;
182 opus_val16 r;
183 celt_assert2(x>0, "celt_rcp() only defined for positive values");
184 i = celt_ilog2(x);
185 /* n is Q15 with range [0,1). */
186 n = VSHR32(x,i-15)-32768;
187 /* Start with a linear approximation:
188 r = 1.8823529411764706-0.9411764705882353*n.
189 The coefficients and the result are Q14 in the range [15420,30840].*/
190 r = ADD16(30840, MULT16_16_Q15(-15420, n));
191 /* Perform two Newton iterations:
192 r -= r*((r*n)-1.Q15)
193 = r*((r*n)+(r-1.Q15)). */
194 r = SUB16(r, MULT16_16_Q15(r,
195 ADD16(MULT16_16_Q15(r, n), ADD16(r, -32768))));
196 /* We subtract an extra 1 in the second iteration to avoid overflow; it also
197 neatly compensates for truncation error in the rest of the process. */
198 r = SUB16(r, ADD16(1, MULT16_16_Q15(r,
199 ADD16(MULT16_16_Q15(r, n), ADD16(r, -32768)))));
200 /* r is now the Q15 solution to 2/(n+1), with a maximum relative error
201 of 7.05346E-5, a (relative) RMSE of 2.14418E-5, and a peak absolute
202 error of 1.24665/32768. */
203 return VSHR32(EXTEND32(r),i-16);
204}
205
206#endif
diff --git a/lib/rbcodec/codecs/libopus/celt/mathops.h b/lib/rbcodec/codecs/libopus/celt/mathops.h
new file mode 100644
index 0000000000..4e97795606
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/mathops.h
@@ -0,0 +1,237 @@
1/* Copyright (c) 2002-2008 Jean-Marc Valin
2 Copyright (c) 2007-2008 CSIRO
3 Copyright (c) 2007-2009 Xiph.Org Foundation
4 Written by Jean-Marc Valin */
5/**
6 @file mathops.h
7 @brief Various math functions
8*/
9/*
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions
12 are met:
13
14 - Redistributions of source code must retain the above copyright
15 notice, this list of conditions and the following disclaimer.
16
17 - Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in the
19 documentation and/or other materials provided with the distribution.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*/
33
34#ifndef MATHOPS_H
35#define MATHOPS_H
36
37#include "arch.h"
38#include "entcode.h"
39#include "os_support.h"
40
41/* Multiplies two 16-bit fractional values. Bit-exactness of this macro is important */
42#define FRAC_MUL16(a,b) ((16384+((opus_int32)(opus_int16)(a)*(opus_int16)(b)))>>15)
43
44unsigned isqrt32(opus_uint32 _val);
45
46#ifndef FIXED_POINT
47
48#define PI 3.141592653f
49#define celt_sqrt(x) ((float)sqrt(x))
50#define celt_rsqrt(x) (1.f/celt_sqrt(x))
51#define celt_rsqrt_norm(x) (celt_rsqrt(x))
52#define celt_cos_norm(x) ((float)cos((.5f*PI)*(x)))
53#define celt_rcp(x) (1.f/(x))
54#define celt_div(a,b) ((a)/(b))
55#define frac_div32(a,b) ((float)(a)/(b))
56
57#ifdef FLOAT_APPROX
58
59/* Note: This assumes radix-2 floating point with the exponent at bits 23..30 and an offset of 127
60 denorm, +/- inf and NaN are *not* handled */
61
62/** Base-2 log approximation (log2(x)). */
63static inline float celt_log2(float x)
64{
65 int integer;
66 float frac;
67 union {
68 float f;
69 opus_uint32 i;
70 } in;
71 in.f = x;
72 integer = (in.i>>23)-127;
73 in.i -= integer<<23;
74 frac = in.f - 1.5f;
75 frac = -0.41445418f + frac*(0.95909232f
76 + frac*(-0.33951290f + frac*0.16541097f));
77 return 1+integer+frac;
78}
79
80/** Base-2 exponential approximation (2^x). */
81static inline float celt_exp2(float x)
82{
83 int integer;
84 float frac;
85 union {
86 float f;
87 opus_uint32 i;
88 } res;
89 integer = floor(x);
90 if (integer < -50)
91 return 0;
92 frac = x-integer;
93 /* K0 = 1, K1 = log(2), K2 = 3-4*log(2), K3 = 3*log(2) - 2 */
94 res.f = 0.99992522f + frac * (0.69583354f
95 + frac * (0.22606716f + 0.078024523f*frac));
96 res.i = (res.i + (integer<<23)) & 0x7fffffff;
97 return res.f;
98}
99
100#else
101#define celt_log2(x) ((float)(1.442695040888963387*log(x)))
102#define celt_exp2(x) ((float)exp(0.6931471805599453094*(x)))
103#endif
104
105#endif
106
107#ifdef FIXED_POINT
108
109#include "os_support.h"
110
111#ifndef OVERRIDE_CELT_ILOG2
112/** Integer log in base2. Undefined for zero and negative numbers */
113static inline opus_int16 celt_ilog2(opus_int32 x)
114{
115 celt_assert2(x>0, "celt_ilog2() only defined for strictly positive numbers");
116 return EC_ILOG(x)-1;
117}
118#endif
119
120#ifndef OVERRIDE_CELT_MAXABS16
121static inline opus_val16 celt_maxabs16(opus_val16 *x, int len)
122{
123 int i;
124 opus_val16 maxval = 0;
125 for (i=0;i<len;i++)
126 maxval = MAX16(maxval, ABS16(x[i]));
127 return maxval;
128}
129#endif
130
131#ifndef OVERRIDE_CELT_MAXABS32
132static inline opus_val32 celt_maxabs32(opus_val32 *x, int len)
133{
134 int i;
135 opus_val32 maxval = 0;
136 for (i=0;i<len;i++)
137 maxval = MAX32(maxval, ABS32(x[i]));
138 return maxval;
139}
140#endif
141
142/** Integer log in base2. Defined for zero, but not for negative numbers */
143static inline opus_int16 celt_zlog2(opus_val32 x)
144{
145 return x <= 0 ? 0 : celt_ilog2(x);
146}
147
148opus_val16 celt_rsqrt_norm(opus_val32 x);
149
150opus_val32 celt_sqrt(opus_val32 x);
151
152opus_val16 celt_cos_norm(opus_val32 x);
153
154static inline opus_val16 celt_log2(opus_val32 x)
155{
156 int i;
157 opus_val16 n, frac;
158 /* -0.41509302963303146, 0.9609890551383969, -0.31836011537636605,
159 0.15530808010959576, -0.08556153059057618 */
160 static const opus_val16 C[5] = {-6801+(1<<(13-DB_SHIFT)), 15746, -5217, 2545, -1401};
161 if (x==0)
162 return -32767;
163 i = celt_ilog2(x);
164 n = VSHR32(x,i-15)-32768-16384;
165 frac = ADD16(C[0], MULT16_16_Q15(n, ADD16(C[1], MULT16_16_Q15(n, ADD16(C[2], MULT16_16_Q15(n, ADD16(C[3], MULT16_16_Q15(n, C[4]))))))));
166 return SHL16(i-13,DB_SHIFT)+SHR16(frac,14-DB_SHIFT);
167}
168
169/*
170 K0 = 1
171 K1 = log(2)
172 K2 = 3-4*log(2)
173 K3 = 3*log(2) - 2
174*/
175#define D0 16383
176#define D1 22804
177#define D2 14819
178#define D3 10204
179/** Base-2 exponential approximation (2^x). (Q10 input, Q16 output) */
180static inline opus_val32 celt_exp2(opus_val16 x)
181{
182 int integer;
183 opus_val16 frac;
184 integer = SHR16(x,10);
185 if (integer>14)
186 return 0x7f000000;
187 else if (integer < -15)
188 return 0;
189 frac = SHL16(x-SHL16(integer,10),4);
190 frac = ADD16(D0, MULT16_16_Q15(frac, ADD16(D1, MULT16_16_Q15(frac, ADD16(D2 , MULT16_16_Q15(D3,frac))))));
191 return VSHR32(EXTEND32(frac), -integer-2);
192}
193
194opus_val32 celt_rcp(opus_val32 x);
195
196#define celt_div(a,b) MULT32_32_Q31((opus_val32)(a),celt_rcp(b))
197
198opus_val32 frac_div32(opus_val32 a, opus_val32 b);
199
200#define M1 32767
201#define M2 -21
202#define M3 -11943
203#define M4 4936
204
205/* Atan approximation using a 4th order polynomial. Input is in Q15 format
206 and normalized by pi/4. Output is in Q15 format */
207static inline opus_val16 celt_atan01(opus_val16 x)
208{
209 return MULT16_16_P15(x, ADD32(M1, MULT16_16_P15(x, ADD32(M2, MULT16_16_P15(x, ADD32(M3, MULT16_16_P15(M4, x)))))));
210}
211
212#undef M1
213#undef M2
214#undef M3
215#undef M4
216
217/* atan2() approximation valid for positive input values */
218static inline opus_val16 celt_atan2p(opus_val16 y, opus_val16 x)
219{
220 if (y < x)
221 {
222 opus_val32 arg;
223 arg = celt_div(SHL32(EXTEND32(y),15),x);
224 if (arg >= 32767)
225 arg = 32767;
226 return SHR16(celt_atan01(EXTRACT16(arg)),1);
227 } else {
228 opus_val32 arg;
229 arg = celt_div(SHL32(EXTEND32(x),15),y);
230 if (arg >= 32767)
231 arg = 32767;
232 return 25736-SHR16(celt_atan01(EXTRACT16(arg)),1);
233 }
234}
235
236#endif /* FIXED_POINT */
237#endif /* MATHOPS_H */
diff --git a/lib/rbcodec/codecs/libopus/celt/mdct.c b/lib/rbcodec/codecs/libopus/celt/mdct.c
new file mode 100644
index 0000000000..abf4e79d8d
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/mdct.c
@@ -0,0 +1,332 @@
1/* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2008 Xiph.Org Foundation
3 Written by Jean-Marc Valin */
4/*
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
12 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
20 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28
29/* This is a simple MDCT implementation that uses a N/4 complex FFT
30 to do most of the work. It should be relatively straightforward to
31 plug in pretty much and FFT here.
32
33 This replaces the Vorbis FFT (and uses the exact same API), which
34 was a bit too messy and that was ending up duplicating code
35 (might as well use the same FFT everywhere).
36
37 The algorithm is similar to (and inspired from) Fabrice Bellard's
38 MDCT implementation in FFMPEG, but has differences in signs, ordering
39 and scaling in many places.
40*/
41
42#ifndef SKIP_CONFIG_H
43#ifdef HAVE_CONFIG_H
44#include "opus_config.h"
45#endif
46#endif
47
48#include "mdct.h"
49#include "kiss_fft.h"
50#include "_kiss_fft_guts.h"
51#include <math.h>
52#include "os_support.h"
53#include "mathops.h"
54#include "stack_alloc.h"
55
56#ifdef CUSTOM_MODES
57
58int clt_mdct_init(mdct_lookup *l,int N, int maxshift)
59{
60 int i;
61 int N4;
62 kiss_twiddle_scalar *trig;
63#if defined(FIXED_POINT)
64 int N2=N>>1;
65#endif
66 l->n = N;
67 N4 = N>>2;
68 l->maxshift = maxshift;
69 for (i=0;i<=maxshift;i++)
70 {
71 if (i==0)
72 l->kfft[i] = opus_fft_alloc(N>>2>>i, 0, 0);
73 else
74 l->kfft[i] = opus_fft_alloc_twiddles(N>>2>>i, 0, 0, l->kfft[0]);
75#ifndef ENABLE_TI_DSPLIB55
76 if (l->kfft[i]==NULL)
77 return 0;
78#endif
79 }
80 l->trig = trig = (kiss_twiddle_scalar*)opus_alloc((N4+1)*sizeof(kiss_twiddle_scalar));
81 if (l->trig==NULL)
82 return 0;
83 /* We have enough points that sine isn't necessary */
84#if defined(FIXED_POINT)
85 for (i=0;i<=N4;i++)
86 trig[i] = TRIG_UPSCALE*celt_cos_norm(DIV32(ADD32(SHL32(EXTEND32(i),17),N2),N));
87#else
88 for (i=0;i<=N4;i++)
89 trig[i] = (kiss_twiddle_scalar)cos(2*PI*i/N);
90#endif
91 return 1;
92}
93
94void clt_mdct_clear(mdct_lookup *l)
95{
96 int i;
97 for (i=0;i<=l->maxshift;i++)
98 opus_fft_free(l->kfft[i]);
99 opus_free((kiss_twiddle_scalar*)l->trig);
100}
101
102#endif /* CUSTOM_MODES */
103
104/* Forward MDCT trashes the input array */
105void clt_mdct_forward(const mdct_lookup *l, kiss_fft_scalar *in, kiss_fft_scalar * OPUS_RESTRICT out,
106 const opus_val16 *window, int overlap, int shift, int stride)
107{
108 int i;
109 int N, N2, N4;
110 kiss_twiddle_scalar sine;
111 VARDECL(kiss_fft_scalar, f);
112 SAVE_STACK;
113 N = l->n;
114 N >>= shift;
115 N2 = N>>1;
116 N4 = N>>2;
117 ALLOC(f, N2, kiss_fft_scalar);
118 /* sin(x) ~= x here */
119#ifdef FIXED_POINT
120 sine = TRIG_UPSCALE*(QCONST16(0.7853981f, 15)+N2)/N;
121#else
122 sine = (kiss_twiddle_scalar)2*PI*(.125f)/N;
123#endif
124
125 /* Consider the input to be composed of four blocks: [a, b, c, d] */
126 /* Window, shuffle, fold */
127 {
128 /* Temp pointers to make it really clear to the compiler what we're doing */
129 const kiss_fft_scalar * OPUS_RESTRICT xp1 = in+(overlap>>1);
130 const kiss_fft_scalar * OPUS_RESTRICT xp2 = in+N2-1+(overlap>>1);
131 kiss_fft_scalar * OPUS_RESTRICT yp = f;
132 const opus_val16 * OPUS_RESTRICT wp1 = window+(overlap>>1);
133 const opus_val16 * OPUS_RESTRICT wp2 = window+(overlap>>1)-1;
134 for(i=0;i<(overlap>>2);i++)
135 {
136 /* Real part arranged as -d-cR, Imag part arranged as -b+aR*/
137 *yp++ = MULT16_32_Q15(*wp2, xp1[N2]) + MULT16_32_Q15(*wp1,*xp2);
138 *yp++ = MULT16_32_Q15(*wp1, *xp1) - MULT16_32_Q15(*wp2, xp2[-N2]);
139 xp1+=2;
140 xp2-=2;
141 wp1+=2;
142 wp2-=2;
143 }
144 wp1 = window;
145 wp2 = window+overlap-1;
146 for(;i<N4-(overlap>>2);i++)
147 {
148 /* Real part arranged as a-bR, Imag part arranged as -c-dR */
149 *yp++ = *xp2;
150 *yp++ = *xp1;
151 xp1+=2;
152 xp2-=2;
153 }
154 for(;i<N4;i++)
155 {
156 /* Real part arranged as a-bR, Imag part arranged as -c-dR */
157 *yp++ = -MULT16_32_Q15(*wp1, xp1[-N2]) + MULT16_32_Q15(*wp2, *xp2);
158 *yp++ = MULT16_32_Q15(*wp2, *xp1) + MULT16_32_Q15(*wp1, xp2[N2]);
159 xp1+=2;
160 xp2-=2;
161 wp1+=2;
162 wp2-=2;
163 }
164 }
165 /* Pre-rotation */
166 {
167 kiss_fft_scalar * OPUS_RESTRICT yp = f;
168 const kiss_twiddle_scalar *t = &l->trig[0];
169 for(i=0;i<N4;i++)
170 {
171 kiss_fft_scalar re, im, yr, yi;
172 re = yp[0];
173 im = yp[1];
174 yr = -S_MUL(re,t[i<<shift]) - S_MUL(im,t[(N4-i)<<shift]);
175 yi = -S_MUL(im,t[i<<shift]) + S_MUL(re,t[(N4-i)<<shift]);
176 /* works because the cos is nearly one */
177 *yp++ = yr + S_MUL(yi,sine);
178 *yp++ = yi - S_MUL(yr,sine);
179 }
180 }
181
182 /* N/4 complex FFT, down-scales by 4/N */
183 opus_fft(l->kfft[shift], (kiss_fft_cpx *)f, (kiss_fft_cpx *)in);
184
185 /* Post-rotate */
186 {
187 /* Temp pointers to make it really clear to the compiler what we're doing */
188 const kiss_fft_scalar * OPUS_RESTRICT fp = in;
189 kiss_fft_scalar * OPUS_RESTRICT yp1 = out;
190 kiss_fft_scalar * OPUS_RESTRICT yp2 = out+stride*(N2-1);
191 const kiss_twiddle_scalar *t = &l->trig[0];
192 /* Temp pointers to make it really clear to the compiler what we're doing */
193 for(i=0;i<N4;i++)
194 {
195 kiss_fft_scalar yr, yi;
196 yr = S_MUL(fp[1],t[(N4-i)<<shift]) + S_MUL(fp[0],t[i<<shift]);
197 yi = S_MUL(fp[0],t[(N4-i)<<shift]) - S_MUL(fp[1],t[i<<shift]);
198 /* works because the cos is nearly one */
199 *yp1 = yr - S_MUL(yi,sine);
200 *yp2 = yi + S_MUL(yr,sine);;
201 fp += 2;
202 yp1 += 2*stride;
203 yp2 -= 2*stride;
204 }
205 }
206 RESTORE_STACK;
207}
208
209void clt_mdct_backward(const mdct_lookup *l, kiss_fft_scalar *in, kiss_fft_scalar * OPUS_RESTRICT out,
210 const opus_val16 * OPUS_RESTRICT window, int overlap, int shift, int stride)
211{
212 int i;
213 int N, N2, N4;
214 kiss_twiddle_scalar sine;
215 VARDECL(kiss_fft_scalar, f);
216 VARDECL(kiss_fft_scalar, f2);
217 SAVE_STACK;
218 N = l->n;
219 N >>= shift;
220 N2 = N>>1;
221 N4 = N>>2;
222 ALLOC(f, N2, kiss_fft_scalar);
223 ALLOC(f2, N2, kiss_fft_scalar);
224 /* sin(x) ~= x here */
225#ifdef FIXED_POINT
226 sine = TRIG_UPSCALE*(QCONST16(0.7853981f, 15)+N2)/N;
227#else
228 sine = (kiss_twiddle_scalar)2*PI*(.125f)/N;
229#endif
230
231 /* Pre-rotate */
232 {
233 /* Temp pointers to make it really clear to the compiler what we're doing */
234 const kiss_fft_scalar * OPUS_RESTRICT xp1 = in;
235 const kiss_fft_scalar * OPUS_RESTRICT xp2 = in+stride*(N2-1);
236 kiss_fft_scalar * OPUS_RESTRICT yp = f2;
237 const kiss_twiddle_scalar *t = &l->trig[0];
238 for(i=0;i<N4;i++)
239 {
240 kiss_fft_scalar yr, yi;
241 yr = -S_MUL(*xp2, t[i<<shift]) + S_MUL(*xp1,t[(N4-i)<<shift]);
242 yi = -S_MUL(*xp2, t[(N4-i)<<shift]) - S_MUL(*xp1,t[i<<shift]);
243 /* works because the cos is nearly one */
244 *yp++ = yr - S_MUL(yi,sine);
245 *yp++ = yi + S_MUL(yr,sine);
246 xp1+=2*stride;
247 xp2-=2*stride;
248 }
249 }
250
251 /* Inverse N/4 complex FFT. This one should *not* downscale even in fixed-point */
252 opus_ifft(l->kfft[shift], (kiss_fft_cpx *)f2, (kiss_fft_cpx *)f);
253
254 /* Post-rotate */
255 {
256 kiss_fft_scalar * OPUS_RESTRICT fp = f;
257 const kiss_twiddle_scalar *t = &l->trig[0];
258
259 for(i=0;i<N4;i++)
260 {
261 kiss_fft_scalar re, im, yr, yi;
262 re = fp[0];
263 im = fp[1];
264 /* We'd scale up by 2 here, but instead it's done when mixing the windows */
265 yr = S_MUL(re,t[i<<shift]) - S_MUL(im,t[(N4-i)<<shift]);
266 yi = S_MUL(im,t[i<<shift]) + S_MUL(re,t[(N4-i)<<shift]);
267 /* works because the cos is nearly one */
268 *fp++ = yr - S_MUL(yi,sine);
269 *fp++ = yi + S_MUL(yr,sine);
270 }
271 }
272 /* De-shuffle the components for the middle of the window only */
273 {
274 const kiss_fft_scalar * OPUS_RESTRICT fp1 = f;
275 const kiss_fft_scalar * OPUS_RESTRICT fp2 = f+N2-1;
276 kiss_fft_scalar * OPUS_RESTRICT yp = f2;
277 for(i = 0; i < N4; i++)
278 {
279 *yp++ =-*fp1;
280 *yp++ = *fp2;
281 fp1 += 2;
282 fp2 -= 2;
283 }
284 }
285 out -= (N2-overlap)>>1;
286 /* Mirror on both sides for TDAC */
287 {
288 kiss_fft_scalar * OPUS_RESTRICT fp1 = f2+N4-1;
289 kiss_fft_scalar * OPUS_RESTRICT xp1 = out+N2-1;
290 kiss_fft_scalar * OPUS_RESTRICT yp1 = out+N4-overlap/2;
291 const opus_val16 * OPUS_RESTRICT wp1 = window;
292 const opus_val16 * OPUS_RESTRICT wp2 = window+overlap-1;
293 for(i = 0; i< N4-overlap/2; i++)
294 {
295 *xp1 = *fp1;
296 xp1--;
297 fp1--;
298 }
299 for(; i < N4; i++)
300 {
301 kiss_fft_scalar x1;
302 x1 = *fp1--;
303 *yp1++ +=-MULT16_32_Q15(*wp1, x1);
304 *xp1-- += MULT16_32_Q15(*wp2, x1);
305 wp1++;
306 wp2--;
307 }
308 }
309 {
310 kiss_fft_scalar * OPUS_RESTRICT fp2 = f2+N4;
311 kiss_fft_scalar * OPUS_RESTRICT xp2 = out+N2;
312 kiss_fft_scalar * OPUS_RESTRICT yp2 = out+N-1-(N4-overlap/2);
313 const opus_val16 * OPUS_RESTRICT wp1 = window;
314 const opus_val16 * OPUS_RESTRICT wp2 = window+overlap-1;
315 for(i = 0; i< N4-overlap/2; i++)
316 {
317 *xp2 = *fp2;
318 xp2++;
319 fp2++;
320 }
321 for(; i < N4; i++)
322 {
323 kiss_fft_scalar x2;
324 x2 = *fp2++;
325 *yp2-- = MULT16_32_Q15(*wp1, x2);
326 *xp2++ = MULT16_32_Q15(*wp2, x2);
327 wp1++;
328 wp2--;
329 }
330 }
331 RESTORE_STACK;
332}
diff --git a/lib/rbcodec/codecs/libopus/celt/mdct.h b/lib/rbcodec/codecs/libopus/celt/mdct.h
new file mode 100644
index 0000000000..d72182138a
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/mdct.h
@@ -0,0 +1,70 @@
1/* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2008 Xiph.Org Foundation
3 Written by Jean-Marc Valin */
4/*
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
12 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
20 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28
29/* This is a simple MDCT implementation that uses a N/4 complex FFT
30 to do most of the work. It should be relatively straightforward to
31 plug in pretty much and FFT here.
32
33 This replaces the Vorbis FFT (and uses the exact same API), which
34 was a bit too messy and that was ending up duplicating code
35 (might as well use the same FFT everywhere).
36
37 The algorithm is similar to (and inspired from) Fabrice Bellard's
38 MDCT implementation in FFMPEG, but has differences in signs, ordering
39 and scaling in many places.
40*/
41
42#ifndef MDCT_H
43#define MDCT_H
44
45#include "opus_defines.h"
46#include "kiss_fft.h"
47#include "arch.h"
48
49typedef struct {
50 int n;
51 int maxshift;
52 const kiss_fft_state *kfft[4];
53 const kiss_twiddle_scalar * OPUS_RESTRICT trig;
54} mdct_lookup;
55
56int clt_mdct_init(mdct_lookup *l,int N, int maxshift);
57void clt_mdct_clear(mdct_lookup *l);
58
59/** Compute a forward MDCT and scale by 4/N, trashes the input array */
60void clt_mdct_forward(const mdct_lookup *l, kiss_fft_scalar *in,
61 kiss_fft_scalar * OPUS_RESTRICT out,
62 const opus_val16 *window, int overlap, int shift, int stride);
63
64/** Compute a backward MDCT (no scaling) and performs weighted overlap-add
65 (scales implicitly by 1/2) */
66void clt_mdct_backward(const mdct_lookup *l, kiss_fft_scalar *in,
67 kiss_fft_scalar * OPUS_RESTRICT out,
68 const opus_val16 * OPUS_RESTRICT window, int overlap, int shift, int stride);
69
70#endif
diff --git a/lib/rbcodec/codecs/libopus/celt/mfrngcod.h b/lib/rbcodec/codecs/libopus/celt/mfrngcod.h
new file mode 100644
index 0000000000..809152a59a
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/mfrngcod.h
@@ -0,0 +1,48 @@
1/* Copyright (c) 2001-2008 Timothy B. Terriberry
2 Copyright (c) 2008-2009 Xiph.Org Foundation */
3/*
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#if !defined(_mfrngcode_H)
29# define _mfrngcode_H (1)
30# include "entcode.h"
31
32/*Constants used by the entropy encoder/decoder.*/
33
34/*The number of bits to output at a time.*/
35# define EC_SYM_BITS (8)
36/*The total number of bits in each of the state registers.*/
37# define EC_CODE_BITS (32)
38/*The maximum symbol value.*/
39# define EC_SYM_MAX ((1U<<EC_SYM_BITS)-1)
40/*Bits to shift by to move a symbol into the high-order position.*/
41# define EC_CODE_SHIFT (EC_CODE_BITS-EC_SYM_BITS-1)
42/*Carry bit of the high-order range symbol.*/
43# define EC_CODE_TOP (((opus_uint32)1U)<<(EC_CODE_BITS-1))
44/*Low-order bit of the high-order range symbol.*/
45# define EC_CODE_BOT (EC_CODE_TOP>>EC_SYM_BITS)
46/*The number of bits available for the last, partial symbol in the code field.*/
47# define EC_CODE_EXTRA ((EC_CODE_BITS-2)%EC_SYM_BITS+1)
48#endif
diff --git a/lib/rbcodec/codecs/libopus/celt/modes.c b/lib/rbcodec/codecs/libopus/celt/modes.c
new file mode 100644
index 0000000000..d44cb3b9de
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/modes.c
@@ -0,0 +1,430 @@
1/* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2009 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 "opus_config.h"
32#endif
33
34#include "celt.h"
35#include "modes.h"
36#include "rate.h"
37#include "os_support.h"
38#include "stack_alloc.h"
39#include "quant_bands.h"
40
41static const opus_int16 eband5ms[] = {
42/*0 200 400 600 800 1k 1.2 1.4 1.6 2k 2.4 2.8 3.2 4k 4.8 5.6 6.8 8k 9.6 12k 15.6 */
43 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 34, 40, 48, 60, 78, 100
44};
45
46/* Alternate tuning (partially derived from Vorbis) */
47#define BITALLOC_SIZE 11
48/* Bit allocation table in units of 1/32 bit/sample (0.1875 dB SNR) */
49static const unsigned char band_allocation[] = {
50/*0 200 400 600 800 1k 1.2 1.4 1.6 2k 2.4 2.8 3.2 4k 4.8 5.6 6.8 8k 9.6 12k 15.6 */
51 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
52 90, 80, 75, 69, 63, 56, 49, 40, 34, 29, 20, 18, 10, 0, 0, 0, 0, 0, 0, 0, 0,
53110,100, 90, 84, 78, 71, 65, 58, 51, 45, 39, 32, 26, 20, 12, 0, 0, 0, 0, 0, 0,
54118,110,103, 93, 86, 80, 75, 70, 65, 59, 53, 47, 40, 31, 23, 15, 4, 0, 0, 0, 0,
55126,119,112,104, 95, 89, 83, 78, 72, 66, 60, 54, 47, 39, 32, 25, 17, 12, 1, 0, 0,
56134,127,120,114,103, 97, 91, 85, 78, 72, 66, 60, 54, 47, 41, 35, 29, 23, 16, 10, 1,
57144,137,130,124,113,107,101, 95, 88, 82, 76, 70, 64, 57, 51, 45, 39, 33, 26, 15, 1,
58152,145,138,132,123,117,111,105, 98, 92, 86, 80, 74, 67, 61, 55, 49, 43, 36, 20, 1,
59162,155,148,142,133,127,121,115,108,102, 96, 90, 84, 77, 71, 65, 59, 53, 46, 30, 1,
60172,165,158,152,143,137,131,125,118,112,106,100, 94, 87, 81, 75, 69, 63, 56, 45, 20,
61200,200,200,200,200,200,200,200,198,193,188,183,178,173,168,163,158,153,148,129,104,
62};
63
64#ifndef CUSTOM_MODES_ONLY
65 #ifdef FIXED_POINT
66 #include "static_modes_fixed.h"
67 #else
68 #include "static_modes_float.h"
69 #endif
70#endif /* CUSTOM_MODES_ONLY */
71
72#ifndef M_PI
73#define M_PI 3.141592653
74#endif
75
76#ifdef CUSTOM_MODES
77
78/* Defining 25 critical bands for the full 0-20 kHz audio bandwidth
79 Taken from http://ccrma.stanford.edu/~jos/bbt/Bark_Frequency_Scale.html */
80#define BARK_BANDS 25
81static const opus_int16 bark_freq[BARK_BANDS+1] = {
82 0, 100, 200, 300, 400,
83 510, 630, 770, 920, 1080,
84 1270, 1480, 1720, 2000, 2320,
85 2700, 3150, 3700, 4400, 5300,
86 6400, 7700, 9500, 12000, 15500,
87 20000};
88
89static opus_int16 *compute_ebands(opus_int32 Fs, int frame_size, int res, int *nbEBands)
90{
91 opus_int16 *eBands;
92 int i, j, lin, low, high, nBark, offset=0;
93
94 /* All modes that have 2.5 ms short blocks use the same definition */
95 if (Fs == 400*(opus_int32)frame_size)
96 {
97 *nbEBands = sizeof(eband5ms)/sizeof(eband5ms[0])-1;
98 eBands = opus_alloc(sizeof(opus_int16)*(*nbEBands+1));
99 for (i=0;i<*nbEBands+1;i++)
100 eBands[i] = eband5ms[i];
101 return eBands;
102 }
103 /* Find the number of critical bands supported by our sampling rate */
104 for (nBark=1;nBark<BARK_BANDS;nBark++)
105 if (bark_freq[nBark+1]*2 >= Fs)
106 break;
107
108 /* Find where the linear part ends (i.e. where the spacing is more than min_width */
109 for (lin=0;lin<nBark;lin++)
110 if (bark_freq[lin+1]-bark_freq[lin] >= res)
111 break;
112
113 low = (bark_freq[lin]+res/2)/res;
114 high = nBark-lin;
115 *nbEBands = low+high;
116 eBands = opus_alloc(sizeof(opus_int16)*(*nbEBands+2));
117
118 if (eBands==NULL)
119 return NULL;
120
121 /* Linear spacing (min_width) */
122 for (i=0;i<low;i++)
123 eBands[i] = i;
124 if (low>0)
125 offset = eBands[low-1]*res - bark_freq[lin-1];
126 /* Spacing follows critical bands */
127 for (i=0;i<high;i++)
128 {
129 int target = bark_freq[lin+i];
130 /* Round to an even value */
131 eBands[i+low] = (target+offset/2+res)/(2*res)*2;
132 offset = eBands[i+low]*res - target;
133 }
134 /* Enforce the minimum spacing at the boundary */
135 for (i=0;i<*nbEBands;i++)
136 if (eBands[i] < i)
137 eBands[i] = i;
138 /* Round to an even value */
139 eBands[*nbEBands] = (bark_freq[nBark]+res)/(2*res)*2;
140 if (eBands[*nbEBands] > frame_size)
141 eBands[*nbEBands] = frame_size;
142 for (i=1;i<*nbEBands-1;i++)
143 {
144 if (eBands[i+1]-eBands[i] < eBands[i]-eBands[i-1])
145 {
146 eBands[i] -= (2*eBands[i]-eBands[i-1]-eBands[i+1])/2;
147 }
148 }
149 /* Remove any empty bands. */
150 for (i=j=0;i<*nbEBands;i++)
151 if(eBands[i+1]>eBands[j])
152 eBands[++j]=eBands[i+1];
153 *nbEBands=j;
154
155 for (i=1;i<*nbEBands;i++)
156 {
157 /* Every band must be smaller than the last band. */
158 celt_assert(eBands[i]-eBands[i-1]<=eBands[*nbEBands]-eBands[*nbEBands-1]);
159 /* Each band must be no larger than twice the size of the previous one. */
160 celt_assert(eBands[i+1]-eBands[i]<=2*(eBands[i]-eBands[i-1]));
161 }
162
163 return eBands;
164}
165
166static void compute_allocation_table(CELTMode *mode)
167{
168 int i, j;
169 unsigned char *allocVectors;
170 int maxBands = sizeof(eband5ms)/sizeof(eband5ms[0])-1;
171
172 mode->nbAllocVectors = BITALLOC_SIZE;
173 allocVectors = opus_alloc(sizeof(unsigned char)*(BITALLOC_SIZE*mode->nbEBands));
174 if (allocVectors==NULL)
175 return;
176
177 /* Check for standard mode */
178 if (mode->Fs == 400*(opus_int32)mode->shortMdctSize)
179 {
180 for (i=0;i<BITALLOC_SIZE*mode->nbEBands;i++)
181 allocVectors[i] = band_allocation[i];
182 mode->allocVectors = allocVectors;
183 return;
184 }
185 /* If not the standard mode, interpolate */
186 /* Compute per-codec-band allocation from per-critical-band matrix */
187 for (i=0;i<BITALLOC_SIZE;i++)
188 {
189 for (j=0;j<mode->nbEBands;j++)
190 {
191 int k;
192 for (k=0;k<maxBands;k++)
193 {
194 if (400*(opus_int32)eband5ms[k] > mode->eBands[j]*(opus_int32)mode->Fs/mode->shortMdctSize)
195 break;
196 }
197 if (k>maxBands-1)
198 allocVectors[i*mode->nbEBands+j] = band_allocation[i*maxBands + maxBands-1];
199 else {
200 opus_int32 a0, a1;
201 a1 = mode->eBands[j]*(opus_int32)mode->Fs/mode->shortMdctSize - 400*(opus_int32)eband5ms[k-1];
202 a0 = 400*(opus_int32)eband5ms[k] - mode->eBands[j]*(opus_int32)mode->Fs/mode->shortMdctSize;
203 allocVectors[i*mode->nbEBands+j] = (a0*band_allocation[i*maxBands+k-1]
204 + a1*band_allocation[i*maxBands+k])/(a0+a1);
205 }
206 }
207 }
208
209 /*printf ("\n");
210 for (i=0;i<BITALLOC_SIZE;i++)
211 {
212 for (j=0;j<mode->nbEBands;j++)
213 printf ("%d ", allocVectors[i*mode->nbEBands+j]);
214 printf ("\n");
215 }
216 exit(0);*/
217
218 mode->allocVectors = allocVectors;
219}
220
221#endif /* CUSTOM_MODES */
222
223CELTMode *opus_custom_mode_create(opus_int32 Fs, int frame_size, int *error)
224{
225 int i;
226#ifdef CUSTOM_MODES
227 CELTMode *mode=NULL;
228 int res;
229 opus_val16 *window;
230 opus_int16 *logN;
231 int LM;
232 ALLOC_STACK;
233#if !defined(VAR_ARRAYS) && !defined(USE_ALLOCA)
234 if (global_stack==NULL)
235 goto failure;
236#endif
237#endif
238
239#ifndef CUSTOM_MODES_ONLY
240 for (i=0;i<TOTAL_MODES;i++)
241 {
242 int j;
243 for (j=0;j<4;j++)
244 {
245 if (Fs == static_mode_list[i]->Fs &&
246 (frame_size<<j) == static_mode_list[i]->shortMdctSize*static_mode_list[i]->nbShortMdcts)
247 {
248 if (error)
249 *error = OPUS_OK;
250 return (CELTMode*)static_mode_list[i];
251 }
252 }
253 }
254#endif /* CUSTOM_MODES_ONLY */
255
256#ifndef CUSTOM_MODES
257 if (error)
258 *error = OPUS_BAD_ARG;
259 return NULL;
260#else
261
262 /* The good thing here is that permutation of the arguments will automatically be invalid */
263
264 if (Fs < 8000 || Fs > 96000)
265 {
266 if (error)
267 *error = OPUS_BAD_ARG;
268 return NULL;
269 }
270 if (frame_size < 40 || frame_size > 1024 || frame_size%2!=0)
271 {
272 if (error)
273 *error = OPUS_BAD_ARG;
274 return NULL;
275 }
276 /* Frames of less than 1ms are not supported. */
277 if ((opus_int32)frame_size*1000 < Fs)
278 {
279 if (error)
280 *error = OPUS_BAD_ARG;
281 return NULL;
282 }
283
284 if ((opus_int32)frame_size*75 >= Fs && (frame_size%16)==0)
285 {
286 LM = 3;
287 } else if ((opus_int32)frame_size*150 >= Fs && (frame_size%8)==0)
288 {
289 LM = 2;
290 } else if ((opus_int32)frame_size*300 >= Fs && (frame_size%4)==0)
291 {
292 LM = 1;
293 } else
294 {
295 LM = 0;
296 }
297
298 /* Shorts longer than 3.3ms are not supported. */
299 if ((opus_int32)(frame_size>>LM)*300 > Fs)
300 {
301 if (error)
302 *error = OPUS_BAD_ARG;
303 return NULL;
304 }
305
306 mode = opus_alloc(sizeof(CELTMode));
307 if (mode==NULL)
308 goto failure;
309 mode->Fs = Fs;
310
311 /* Pre/de-emphasis depends on sampling rate. The "standard" pre-emphasis
312 is defined as A(z) = 1 - 0.85*z^-1 at 48 kHz. Other rates should
313 approximate that. */
314 if(Fs < 12000) /* 8 kHz */
315 {
316 mode->preemph[0] = QCONST16(0.3500061035f, 15);
317 mode->preemph[1] = -QCONST16(0.1799926758f, 15);
318 mode->preemph[2] = QCONST16(0.2719968125f, SIG_SHIFT); /* exact 1/preemph[3] */
319 mode->preemph[3] = QCONST16(3.6765136719f, 13);
320 } else if(Fs < 24000) /* 16 kHz */
321 {
322 mode->preemph[0] = QCONST16(0.6000061035f, 15);
323 mode->preemph[1] = -QCONST16(0.1799926758f, 15);
324 mode->preemph[2] = QCONST16(0.4424998650f, SIG_SHIFT); /* exact 1/preemph[3] */
325 mode->preemph[3] = QCONST16(2.2598876953f, 13);
326 } else if(Fs < 40000) /* 32 kHz */
327 {
328 mode->preemph[0] = QCONST16(0.7799987793f, 15);
329 mode->preemph[1] = -QCONST16(0.1000061035f, 15);
330 mode->preemph[2] = QCONST16(0.7499771125f, SIG_SHIFT); /* exact 1/preemph[3] */
331 mode->preemph[3] = QCONST16(1.3333740234f, 13);
332 } else /* 48 kHz */
333 {
334 mode->preemph[0] = QCONST16(0.8500061035f, 15);
335 mode->preemph[1] = QCONST16(0.0f, 15);
336 mode->preemph[2] = QCONST16(1.f, SIG_SHIFT);
337 mode->preemph[3] = QCONST16(1.f, 13);
338 }
339
340 mode->maxLM = LM;
341 mode->nbShortMdcts = 1<<LM;
342 mode->shortMdctSize = frame_size/mode->nbShortMdcts;
343 res = (mode->Fs+mode->shortMdctSize)/(2*mode->shortMdctSize);
344
345 mode->eBands = compute_ebands(Fs, mode->shortMdctSize, res, &mode->nbEBands);
346 if (mode->eBands==NULL)
347 goto failure;
348
349 mode->effEBands = mode->nbEBands;
350 while (mode->eBands[mode->effEBands] > mode->shortMdctSize)
351 mode->effEBands--;
352
353 /* Overlap must be divisible by 4 */
354 mode->overlap = ((mode->shortMdctSize>>2)<<2);
355
356 compute_allocation_table(mode);
357 if (mode->allocVectors==NULL)
358 goto failure;
359
360 window = (opus_val16*)opus_alloc(mode->overlap*sizeof(opus_val16));
361 if (window==NULL)
362 goto failure;
363
364#ifndef FIXED_POINT
365 for (i=0;i<mode->overlap;i++)
366 window[i] = Q15ONE*sin(.5*M_PI* sin(.5*M_PI*(i+.5)/mode->overlap) * sin(.5*M_PI*(i+.5)/mode->overlap));
367#else
368 for (i=0;i<mode->overlap;i++)
369 window[i] = MIN32(32767,floor(.5+32768.*sin(.5*M_PI* sin(.5*M_PI*(i+.5)/mode->overlap) * sin(.5*M_PI*(i+.5)/mode->overlap))));
370#endif
371 mode->window = window;
372
373 logN = (opus_int16*)opus_alloc(mode->nbEBands*sizeof(opus_int16));
374 if (logN==NULL)
375 goto failure;
376
377 for (i=0;i<mode->nbEBands;i++)
378 logN[i] = log2_frac(mode->eBands[i+1]-mode->eBands[i], BITRES);
379 mode->logN = logN;
380
381 compute_pulse_cache(mode, mode->maxLM);
382
383 if (clt_mdct_init(&mode->mdct, 2*mode->shortMdctSize*mode->nbShortMdcts,
384 mode->maxLM) == 0)
385 goto failure;
386
387 if (error)
388 *error = OPUS_OK;
389
390 return mode;
391failure:
392 if (error)
393 *error = OPUS_ALLOC_FAIL;
394 if (mode!=NULL)
395 opus_custom_mode_destroy(mode);
396 return NULL;
397#endif /* !CUSTOM_MODES */
398}
399
400#ifdef CUSTOM_MODES
401void opus_custom_mode_destroy(CELTMode *mode)
402{
403 if (mode == NULL)
404 return;
405#ifndef CUSTOM_MODES_ONLY
406 {
407 int i;
408 for (i=0;i<TOTAL_MODES;i++)
409 {
410 if (mode == static_mode_list[i])
411 {
412 return;
413 }
414 }
415 }
416#endif /* CUSTOM_MODES_ONLY */
417 opus_free((opus_int16*)mode->eBands);
418 opus_free((opus_int16*)mode->allocVectors);
419
420 opus_free((opus_val16*)mode->window);
421 opus_free((opus_int16*)mode->logN);
422
423 opus_free((opus_int16*)mode->cache.index);
424 opus_free((unsigned char*)mode->cache.bits);
425 opus_free((unsigned char*)mode->cache.caps);
426 clt_mdct_clear(&mode->mdct);
427
428 opus_free((CELTMode *)mode);
429}
430#endif
diff --git a/lib/rbcodec/codecs/libopus/celt/modes.h b/lib/rbcodec/codecs/libopus/celt/modes.h
new file mode 100644
index 0000000000..c8340f9875
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/modes.h
@@ -0,0 +1,83 @@
1/* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2009 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#ifndef MODES_H
31#define MODES_H
32
33#include "opus_types.h"
34#include "celt.h"
35#include "arch.h"
36#include "mdct.h"
37#include "entenc.h"
38#include "entdec.h"
39
40#define MAX_PERIOD 1024
41
42#ifndef OVERLAP
43#define OVERLAP(mode) ((mode)->overlap)
44#endif
45
46#ifndef FRAMESIZE
47#define FRAMESIZE(mode) ((mode)->mdctSize)
48#endif
49
50typedef struct {
51 int size;
52 const opus_int16 *index;
53 const unsigned char *bits;
54 const unsigned char *caps;
55} PulseCache;
56
57/** Mode definition (opaque)
58 @brief Mode definition
59 */
60struct OpusCustomMode {
61 opus_int32 Fs;
62 int overlap;
63
64 int nbEBands;
65 int effEBands;
66 opus_val16 preemph[4];
67 const opus_int16 *eBands; /**< Definition for each "pseudo-critical band" */
68
69 int maxLM;
70 int nbShortMdcts;
71 int shortMdctSize;
72
73 int nbAllocVectors; /**< Number of lines in the matrix below */
74 const unsigned char *allocVectors; /**< Number of bits in each band for several rates */
75 const opus_int16 *logN;
76
77 const opus_val16 *window;
78 mdct_lookup mdct;
79 PulseCache cache;
80};
81
82
83#endif
diff --git a/lib/rbcodec/codecs/libopus/celt/os_support.h b/lib/rbcodec/codecs/libopus/celt/os_support.h
new file mode 100644
index 0000000000..2484f0b2f7
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/os_support.h
@@ -0,0 +1,89 @@
1/* Copyright (C) 2007 Jean-Marc Valin
2
3 File: os_support.h
4 This is the (tiny) OS abstraction layer. Aside from math.h, this is the
5 only place where system headers are allowed.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are
9 met:
10
11 1. Redistributions of source code must retain the above copyright notice,
12 this list of conditions and the following disclaimer.
13
14 2. Redistributions in binary form must reproduce the above copyright
15 notice, this list of conditions and the following disclaimer in the
16 documentation and/or other materials provided with the distribution.
17
18 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 POSSIBILITY OF SUCH DAMAGE.
29*/
30
31#ifndef OS_SUPPORT_H
32#define OS_SUPPORT_H
33
34#ifdef CUSTOM_SUPPORT
35# include "custom_support.h"
36#endif
37
38#include <string.h>
39#include <stdio.h>
40#include <stdlib.h>
41
42/** Opus wrapper for malloc(). To do your own dynamic allocation, all you need to do is replace this function and opus_free */
43#ifndef OVERRIDE_OPUS_ALLOC
44static inline void *opus_alloc (size_t size)
45{
46 return malloc(size);
47}
48#endif
49
50/** Same as celt_alloc(), except that the area is only needed inside a CELT call (might cause problem with wideband though) */
51#ifndef OVERRIDE_OPUS_ALLOC_SCRATCH
52static inline void *opus_alloc_scratch (size_t size)
53{
54 /* Scratch space doesn't need to be cleared */
55 return opus_alloc(size);
56}
57#endif
58
59/** Opus wrapper for free(). To do your own dynamic allocation, all you need to do is replace this function and opus_alloc */
60#ifndef OVERRIDE_OPUS_FREE
61static inline void opus_free (void *ptr)
62{
63 free(ptr);
64}
65#endif
66
67/** Copy n bytes of memory from src to dst. The 0* term provides compile-time type checking */
68#ifndef OVERRIDE_OPUS_COPY
69#define OPUS_COPY(dst, src, n) (memcpy((dst), (src), (n)*sizeof(*(dst)) + 0*((dst)-(src)) ))
70#endif
71
72/** Copy n bytes of memory from src to dst, allowing overlapping regions. The 0* term
73 provides compile-time type checking */
74#ifndef OVERRIDE_OPUS_MOVE
75#define OPUS_MOVE(dst, src, n) (memmove((dst), (src), (n)*sizeof(*(dst)) + 0*((dst)-(src)) ))
76#endif
77
78/** Set n elements of dst to zero, starting at address s */
79#ifndef OVERRIDE_OPUS_CLEAR
80#define OPUS_CLEAR(dst, n) (memset((dst), 0, (n)*sizeof(*(dst))))
81#endif
82
83/*#ifdef __GNUC__
84#pragma GCC poison printf sprintf
85#pragma GCC poison malloc free realloc calloc
86#endif*/
87
88#endif /* OS_SUPPORT_H */
89
diff --git a/lib/rbcodec/codecs/libopus/celt/pitch.c b/lib/rbcodec/codecs/libopus/celt/pitch.c
new file mode 100644
index 0000000000..3bad8e46a7
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/pitch.c
@@ -0,0 +1,410 @@
1/* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2009 Xiph.Org Foundation
3 Written by Jean-Marc Valin */
4/**
5 @file pitch.c
6 @brief Pitch analysis
7 */
8
9/*
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions
12 are met:
13
14 - Redistributions of source code must retain the above copyright
15 notice, this list of conditions and the following disclaimer.
16
17 - Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in the
19 documentation and/or other materials provided with the distribution.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*/
33
34#ifdef HAVE_CONFIG_H
35#include "opus_config.h"
36#endif
37
38#include "pitch.h"
39#include "os_support.h"
40#include "modes.h"
41#include "stack_alloc.h"
42#include "mathops.h"
43#include "celt_lpc.h"
44
45static void find_best_pitch(opus_val32 *xcorr, opus_val16 *y, int len,
46 int max_pitch, int *best_pitch
47#ifdef FIXED_POINT
48 , int yshift, opus_val32 maxcorr
49#endif
50 )
51{
52 int i, j;
53 opus_val32 Syy=1;
54 opus_val16 best_num[2];
55 opus_val32 best_den[2];
56#ifdef FIXED_POINT
57 int xshift;
58
59 xshift = celt_ilog2(maxcorr)-14;
60#endif
61
62 best_num[0] = -1;
63 best_num[1] = -1;
64 best_den[0] = 0;
65 best_den[1] = 0;
66 best_pitch[0] = 0;
67 best_pitch[1] = 1;
68 for (j=0;j<len;j++)
69 Syy = ADD32(Syy, SHR32(MULT16_16(y[j],y[j]), yshift));
70 for (i=0;i<max_pitch;i++)
71 {
72 if (xcorr[i]>0)
73 {
74 opus_val16 num;
75 opus_val32 xcorr16;
76 xcorr16 = EXTRACT16(VSHR32(xcorr[i], xshift));
77#ifndef FIXED_POINT
78 /* Considering the range of xcorr16, this should avoid both underflows
79 and overflows (inf) when squaring xcorr16 */
80 xcorr16 *= 1e-12;
81#endif
82 num = MULT16_16_Q15(xcorr16,xcorr16);
83 if (MULT16_32_Q15(num,best_den[1]) > MULT16_32_Q15(best_num[1],Syy))
84 {
85 if (MULT16_32_Q15(num,best_den[0]) > MULT16_32_Q15(best_num[0],Syy))
86 {
87 best_num[1] = best_num[0];
88 best_den[1] = best_den[0];
89 best_pitch[1] = best_pitch[0];
90 best_num[0] = num;
91 best_den[0] = Syy;
92 best_pitch[0] = i;
93 } else {
94 best_num[1] = num;
95 best_den[1] = Syy;
96 best_pitch[1] = i;
97 }
98 }
99 }
100 Syy += SHR32(MULT16_16(y[i+len],y[i+len]),yshift) - SHR32(MULT16_16(y[i],y[i]),yshift);
101 Syy = MAX32(1, Syy);
102 }
103}
104
105void pitch_downsample(celt_sig * OPUS_RESTRICT x[], opus_val16 * OPUS_RESTRICT x_lp,
106 int len, int C)
107{
108 int i;
109 opus_val32 ac[5];
110 opus_val16 tmp=Q15ONE;
111 opus_val16 lpc[4], mem[4]={0,0,0,0};
112#ifdef FIXED_POINT
113 int shift;
114 opus_val32 maxabs = celt_maxabs32(x[0], len);
115 if (C==2)
116 {
117 opus_val32 maxabs_1 = celt_maxabs32(x[1], len);
118 maxabs = MAX32(maxabs, maxabs_1);
119 }
120 if (maxabs<1)
121 maxabs=1;
122 shift = celt_ilog2(maxabs)-10;
123 if (shift<0)
124 shift=0;
125 if (C==2)
126 shift++;
127#endif
128 for (i=1;i<len>>1;i++)
129 x_lp[i] = SHR32(HALF32(HALF32(x[0][(2*i-1)]+x[0][(2*i+1)])+x[0][2*i]), shift);
130 x_lp[0] = SHR32(HALF32(HALF32(x[0][1])+x[0][0]), shift);
131 if (C==2)
132 {
133 for (i=1;i<len>>1;i++)
134 x_lp[i] += SHR32(HALF32(HALF32(x[1][(2*i-1)]+x[1][(2*i+1)])+x[1][2*i]), shift);
135 x_lp[0] += SHR32(HALF32(HALF32(x[1][1])+x[1][0]), shift);
136 }
137
138 _celt_autocorr(x_lp, ac, NULL, 0,
139 4, len>>1);
140
141 /* Noise floor -40 dB */
142#ifdef FIXED_POINT
143 ac[0] += SHR32(ac[0],13);
144#else
145 ac[0] *= 1.0001f;
146#endif
147 /* Lag windowing */
148 for (i=1;i<=4;i++)
149 {
150 /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
151#ifdef FIXED_POINT
152 ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
153#else
154 ac[i] -= ac[i]*(.008f*i)*(.008f*i);
155#endif
156 }
157
158 _celt_lpc(lpc, ac, 4);
159 for (i=0;i<4;i++)
160 {
161 tmp = MULT16_16_Q15(QCONST16(.9f,15), tmp);
162 lpc[i] = MULT16_16_Q15(lpc[i], tmp);
163 }
164 celt_fir(x_lp, lpc, x_lp, len>>1, 4, mem);
165
166 mem[0]=0;
167 lpc[0]=QCONST16(.8f,12);
168 celt_fir(x_lp, lpc, x_lp, len>>1, 1, mem);
169
170}
171
172void pitch_search(const opus_val16 * OPUS_RESTRICT x_lp, opus_val16 * OPUS_RESTRICT y,
173 int len, int max_pitch, int *pitch)
174{
175 int i, j;
176 int lag;
177 int best_pitch[2]={0,0};
178 VARDECL(opus_val16, x_lp4);
179 VARDECL(opus_val16, y_lp4);
180 VARDECL(opus_val32, xcorr);
181#ifdef FIXED_POINT
182 opus_val32 maxcorr=1;
183 opus_val16 xmax, ymax;
184 int shift=0;
185#endif
186 int offset;
187
188 SAVE_STACK;
189
190 celt_assert(len>0);
191 celt_assert(max_pitch>0);
192 lag = len+max_pitch;
193
194 ALLOC(x_lp4, len>>2, opus_val16);
195 ALLOC(y_lp4, lag>>2, opus_val16);
196 ALLOC(xcorr, max_pitch>>1, opus_val32);
197
198 /* Downsample by 2 again */
199 for (j=0;j<len>>2;j++)
200 x_lp4[j] = x_lp[2*j];
201 for (j=0;j<lag>>2;j++)
202 y_lp4[j] = y[2*j];
203
204#ifdef FIXED_POINT
205 xmax = celt_maxabs16(x_lp4, len>>2);
206 ymax = celt_maxabs16(y_lp4, lag>>2);
207 shift = celt_ilog2(MAX16(1, MAX16(xmax, ymax)))-11;
208 if (shift>0)
209 {
210 for (j=0;j<len>>2;j++)
211 x_lp4[j] = SHR16(x_lp4[j], shift);
212 for (j=0;j<lag>>2;j++)
213 y_lp4[j] = SHR16(y_lp4[j], shift);
214 /* Use double the shift for a MAC */
215 shift *= 2;
216 } else {
217 shift = 0;
218 }
219#endif
220
221 /* Coarse search with 4x decimation */
222
223 for (i=0;i<max_pitch>>2;i++)
224 {
225 opus_val32 sum = 0;
226 for (j=0;j<len>>2;j++)
227 sum = MAC16_16(sum, x_lp4[j],y_lp4[i+j]);
228 xcorr[i] = MAX32(-1, sum);
229#ifdef FIXED_POINT
230 maxcorr = MAX32(maxcorr, sum);
231#endif
232 }
233 find_best_pitch(xcorr, y_lp4, len>>2, max_pitch>>2, best_pitch
234#ifdef FIXED_POINT
235 , 0, maxcorr
236#endif
237 );
238
239 /* Finer search with 2x decimation */
240#ifdef FIXED_POINT
241 maxcorr=1;
242#endif
243 for (i=0;i<max_pitch>>1;i++)
244 {
245 opus_val32 sum=0;
246 xcorr[i] = 0;
247 if (abs(i-2*best_pitch[0])>2 && abs(i-2*best_pitch[1])>2)
248 continue;
249 for (j=0;j<len>>1;j++)
250 sum += SHR32(MULT16_16(x_lp[j],y[i+j]), shift);
251 xcorr[i] = MAX32(-1, sum);
252#ifdef FIXED_POINT
253 maxcorr = MAX32(maxcorr, sum);
254#endif
255 }
256 find_best_pitch(xcorr, y, len>>1, max_pitch>>1, best_pitch
257#ifdef FIXED_POINT
258 , shift+1, maxcorr
259#endif
260 );
261
262 /* Refine by pseudo-interpolation */
263 if (best_pitch[0]>0 && best_pitch[0]<(max_pitch>>1)-1)
264 {
265 opus_val32 a, b, c;
266 a = xcorr[best_pitch[0]-1];
267 b = xcorr[best_pitch[0]];
268 c = xcorr[best_pitch[0]+1];
269 if ((c-a) > MULT16_32_Q15(QCONST16(.7f,15),b-a))
270 offset = 1;
271 else if ((a-c) > MULT16_32_Q15(QCONST16(.7f,15),b-c))
272 offset = -1;
273 else
274 offset = 0;
275 } else {
276 offset = 0;
277 }
278 *pitch = 2*best_pitch[0]-offset;
279
280 RESTORE_STACK;
281}
282
283static const int second_check[16] = {0, 0, 3, 2, 3, 2, 5, 2, 3, 2, 3, 2, 5, 2, 3, 2};
284opus_val16 remove_doubling(opus_val16 *x, int maxperiod, int minperiod,
285 int N, int *T0_, int prev_period, opus_val16 prev_gain)
286{
287 int k, i, T, T0;
288 opus_val16 g, g0;
289 opus_val16 pg;
290 opus_val32 xy,xx,yy;
291 opus_val32 xcorr[3];
292 opus_val32 best_xy, best_yy;
293 int offset;
294 int minperiod0;
295
296 minperiod0 = minperiod;
297 maxperiod /= 2;
298 minperiod /= 2;
299 *T0_ /= 2;
300 prev_period /= 2;
301 N /= 2;
302 x += maxperiod;
303 if (*T0_>=maxperiod)
304 *T0_=maxperiod-1;
305
306 T = T0 = *T0_;
307 xx=xy=yy=0;
308 for (i=0;i<N;i++)
309 {
310 xy = MAC16_16(xy, x[i], x[i-T0]);
311 xx = MAC16_16(xx, x[i], x[i]);
312 yy = MAC16_16(yy, x[i-T0],x[i-T0]);
313 }
314 best_xy = xy;
315 best_yy = yy;
316#ifdef FIXED_POINT
317 {
318 opus_val32 x2y2;
319 int sh, t;
320 x2y2 = 1+HALF32(MULT32_32_Q31(xx,yy));
321 sh = celt_ilog2(x2y2)>>1;
322 t = VSHR32(x2y2, 2*(sh-7));
323 g = g0 = VSHR32(MULT16_32_Q15(celt_rsqrt_norm(t), xy),sh+1);
324 }
325#else
326 g = g0 = xy/celt_sqrt(1+xx*yy);
327#endif
328 /* Look for any pitch at T/k */
329 for (k=2;k<=15;k++)
330 {
331 int T1, T1b;
332 opus_val16 g1;
333 opus_val16 cont=0;
334 T1 = (2*T0+k)/(2*k);
335 if (T1 < minperiod)
336 break;
337 /* Look for another strong correlation at T1b */
338 if (k==2)
339 {
340 if (T1+T0>maxperiod)
341 T1b = T0;
342 else
343 T1b = T0+T1;
344 } else
345 {
346 T1b = (2*second_check[k]*T0+k)/(2*k);
347 }
348 xy=yy=0;
349 for (i=0;i<N;i++)
350 {
351 xy = MAC16_16(xy, x[i], x[i-T1]);
352 yy = MAC16_16(yy, x[i-T1], x[i-T1]);
353
354 xy = MAC16_16(xy, x[i], x[i-T1b]);
355 yy = MAC16_16(yy, x[i-T1b], x[i-T1b]);
356 }
357#ifdef FIXED_POINT
358 {
359 opus_val32 x2y2;
360 int sh, t;
361 x2y2 = 1+MULT32_32_Q31(xx,yy);
362 sh = celt_ilog2(x2y2)>>1;
363 t = VSHR32(x2y2, 2*(sh-7));
364 g1 = VSHR32(MULT16_32_Q15(celt_rsqrt_norm(t), xy),sh+1);
365 }
366#else
367 g1 = xy/celt_sqrt(1+2.f*xx*1.f*yy);
368#endif
369 if (abs(T1-prev_period)<=1)
370 cont = prev_gain;
371 else if (abs(T1-prev_period)<=2 && 5*k*k < T0)
372 cont = HALF32(prev_gain);
373 else
374 cont = 0;
375 if (g1 > QCONST16(.3f,15) + MULT16_16_Q15(QCONST16(.4f,15),g0)-cont)
376 {
377 best_xy = xy;
378 best_yy = yy;
379 T = T1;
380 g = g1;
381 }
382 }
383 best_xy = MAX32(0, best_xy);
384 if (best_yy <= best_xy)
385 pg = Q15ONE;
386 else
387 pg = SHR32(frac_div32(best_xy,best_yy+1),16);
388
389 for (k=0;k<3;k++)
390 {
391 int T1 = T+k-1;
392 xy = 0;
393 for (i=0;i<N;i++)
394 xy = MAC16_16(xy, x[i], x[i-T1]);
395 xcorr[k] = xy;
396 }
397 if ((xcorr[2]-xcorr[0]) > MULT16_32_Q15(QCONST16(.7f,15),xcorr[1]-xcorr[0]))
398 offset = 1;
399 else if ((xcorr[0]-xcorr[2]) > MULT16_32_Q15(QCONST16(.7f,15),xcorr[1]-xcorr[2]))
400 offset = -1;
401 else
402 offset = 0;
403 if (pg > g)
404 pg = g;
405 *T0_ = 2*T+offset;
406
407 if (*T0_<minperiod0)
408 *T0_=minperiod0;
409 return pg;
410}
diff --git a/lib/rbcodec/codecs/libopus/celt/pitch.h b/lib/rbcodec/codecs/libopus/celt/pitch.h
new file mode 100644
index 0000000000..2757071a6f
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/pitch.h
@@ -0,0 +1,48 @@
1/* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2009 Xiph.Org Foundation
3 Written by Jean-Marc Valin */
4/**
5 @file pitch.h
6 @brief Pitch analysis
7 */
8
9/*
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions
12 are met:
13
14 - Redistributions of source code must retain the above copyright
15 notice, this list of conditions and the following disclaimer.
16
17 - Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in the
19 documentation and/or other materials provided with the distribution.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*/
33
34#ifndef PITCH_H
35#define PITCH_H
36
37#include "modes.h"
38
39void pitch_downsample(celt_sig * OPUS_RESTRICT x[], opus_val16 * OPUS_RESTRICT x_lp,
40 int len, int C);
41
42void pitch_search(const opus_val16 * OPUS_RESTRICT x_lp, opus_val16 * OPUS_RESTRICT y,
43 int len, int max_pitch, int *pitch);
44
45opus_val16 remove_doubling(opus_val16 *x, int maxperiod, int minperiod,
46 int N, int *T0, int prev_period, opus_val16 prev_gain);
47
48#endif
diff --git a/lib/rbcodec/codecs/libopus/celt/quant_bands.c b/lib/rbcodec/codecs/libopus/celt/quant_bands.c
new file mode 100644
index 0000000000..5ad5311f84
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/quant_bands.c
@@ -0,0 +1,567 @@
1/* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2009 Xiph.Org Foundation
3 Written by Jean-Marc Valin */
4/*
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
12 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
20 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28
29#ifdef HAVE_CONFIG_H
30#include "opus_config.h"
31#endif
32
33#include "quant_bands.h"
34#include "laplace.h"
35#include <math.h>
36#include "os_support.h"
37#include "arch.h"
38#include "mathops.h"
39#include "stack_alloc.h"
40#include "rate.h"
41
42#ifdef FIXED_POINT
43/* Mean energy in each band quantized in Q6 */
44static const signed char eMeans[25] = {
45 103,100, 92, 85, 81,
46 77, 72, 70, 78, 75,
47 73, 71, 78, 74, 69,
48 72, 70, 74, 76, 71,
49 60, 60, 60, 60, 60
50};
51#else
52/* Mean energy in each band quantized in Q6 and converted back to float */
53static const opus_val16 eMeans[25] = {
54 6.437500f, 6.250000f, 5.750000f, 5.312500f, 5.062500f,
55 4.812500f, 4.500000f, 4.375000f, 4.875000f, 4.687500f,
56 4.562500f, 4.437500f, 4.875000f, 4.625000f, 4.312500f,
57 4.500000f, 4.375000f, 4.625000f, 4.750000f, 4.437500f,
58 3.750000f, 3.750000f, 3.750000f, 3.750000f, 3.750000f
59};
60#endif
61/* prediction coefficients: 0.9, 0.8, 0.65, 0.5 */
62#ifdef FIXED_POINT
63static const opus_val16 pred_coef[4] = {29440, 26112, 21248, 16384};
64static const opus_val16 beta_coef[4] = {30147, 22282, 12124, 6554};
65static const opus_val16 beta_intra = 4915;
66#else
67static const opus_val16 pred_coef[4] = {29440/32768., 26112/32768., 21248/32768., 16384/32768.};
68static const opus_val16 beta_coef[4] = {30147/32768., 22282/32768., 12124/32768., 6554/32768.};
69static const opus_val16 beta_intra = 4915/32768.;
70#endif
71
72/*Parameters of the Laplace-like probability models used for the coarse energy.
73 There is one pair of parameters for each frame size, prediction type
74 (inter/intra), and band number.
75 The first number of each pair is the probability of 0, and the second is the
76 decay rate, both in Q8 precision.*/
77static const unsigned char e_prob_model[4][2][42] = {
78 /*120 sample frames.*/
79 {
80 /*Inter*/
81 {
82 72, 127, 65, 129, 66, 128, 65, 128, 64, 128, 62, 128, 64, 128,
83 64, 128, 92, 78, 92, 79, 92, 78, 90, 79, 116, 41, 115, 40,
84 114, 40, 132, 26, 132, 26, 145, 17, 161, 12, 176, 10, 177, 11
85 },
86 /*Intra*/
87 {
88 24, 179, 48, 138, 54, 135, 54, 132, 53, 134, 56, 133, 55, 132,
89 55, 132, 61, 114, 70, 96, 74, 88, 75, 88, 87, 74, 89, 66,
90 91, 67, 100, 59, 108, 50, 120, 40, 122, 37, 97, 43, 78, 50
91 }
92 },
93 /*240 sample frames.*/
94 {
95 /*Inter*/
96 {
97 83, 78, 84, 81, 88, 75, 86, 74, 87, 71, 90, 73, 93, 74,
98 93, 74, 109, 40, 114, 36, 117, 34, 117, 34, 143, 17, 145, 18,
99 146, 19, 162, 12, 165, 10, 178, 7, 189, 6, 190, 8, 177, 9
100 },
101 /*Intra*/
102 {
103 23, 178, 54, 115, 63, 102, 66, 98, 69, 99, 74, 89, 71, 91,
104 73, 91, 78, 89, 86, 80, 92, 66, 93, 64, 102, 59, 103, 60,
105 104, 60, 117, 52, 123, 44, 138, 35, 133, 31, 97, 38, 77, 45
106 }
107 },
108 /*480 sample frames.*/
109 {
110 /*Inter*/
111 {
112 61, 90, 93, 60, 105, 42, 107, 41, 110, 45, 116, 38, 113, 38,
113 112, 38, 124, 26, 132, 27, 136, 19, 140, 20, 155, 14, 159, 16,
114 158, 18, 170, 13, 177, 10, 187, 8, 192, 6, 175, 9, 159, 10
115 },
116 /*Intra*/
117 {
118 21, 178, 59, 110, 71, 86, 75, 85, 84, 83, 91, 66, 88, 73,
119 87, 72, 92, 75, 98, 72, 105, 58, 107, 54, 115, 52, 114, 55,
120 112, 56, 129, 51, 132, 40, 150, 33, 140, 29, 98, 35, 77, 42
121 }
122 },
123 /*960 sample frames.*/
124 {
125 /*Inter*/
126 {
127 42, 121, 96, 66, 108, 43, 111, 40, 117, 44, 123, 32, 120, 36,
128 119, 33, 127, 33, 134, 34, 139, 21, 147, 23, 152, 20, 158, 25,
129 154, 26, 166, 21, 173, 16, 184, 13, 184, 10, 150, 13, 139, 15
130 },
131 /*Intra*/
132 {
133 22, 178, 63, 114, 74, 82, 84, 83, 92, 82, 103, 62, 96, 72,
134 96, 67, 101, 73, 107, 72, 113, 55, 118, 52, 125, 52, 118, 52,
135 117, 55, 135, 49, 137, 39, 157, 32, 145, 29, 97, 33, 77, 40
136 }
137 }
138};
139
140static const unsigned char small_energy_icdf[3]={2,1,0};
141
142static opus_val32 loss_distortion(const opus_val16 *eBands, opus_val16 *oldEBands, int start, int end, int len, int C)
143{
144 int c, i;
145 opus_val32 dist = 0;
146 c=0; do {
147 for (i=start;i<end;i++)
148 {
149 opus_val16 d = SUB16(SHR16(eBands[i+c*len], 3), SHR16(oldEBands[i+c*len], 3));
150 dist = MAC16_16(dist, d,d);
151 }
152 } while (++c<C);
153 return MIN32(200,SHR32(dist,2*DB_SHIFT-6));
154}
155
156static int quant_coarse_energy_impl(const CELTMode *m, int start, int end,
157 const opus_val16 *eBands, opus_val16 *oldEBands,
158 opus_int32 budget, opus_int32 tell,
159 const unsigned char *prob_model, opus_val16 *error, ec_enc *enc,
160 int C, int LM, int intra, opus_val16 max_decay)
161{
162 int i, c;
163 int badness = 0;
164 opus_val32 prev[2] = {0,0};
165 opus_val16 coef;
166 opus_val16 beta;
167
168 if (tell+3 <= budget)
169 ec_enc_bit_logp(enc, intra, 3);
170 if (intra)
171 {
172 coef = 0;
173 beta = beta_intra;
174 } else {
175 beta = beta_coef[LM];
176 coef = pred_coef[LM];
177 }
178
179 /* Encode at a fixed coarse resolution */
180 for (i=start;i<end;i++)
181 {
182 c=0;
183 do {
184 int bits_left;
185 int qi, qi0;
186 opus_val32 q;
187 opus_val16 x;
188 opus_val32 f, tmp;
189 opus_val16 oldE;
190 opus_val16 decay_bound;
191 x = eBands[i+c*m->nbEBands];
192 oldE = MAX16(-QCONST16(9.f,DB_SHIFT), oldEBands[i+c*m->nbEBands]);
193#ifdef FIXED_POINT
194 f = SHL32(EXTEND32(x),7) - PSHR32(MULT16_16(coef,oldE), 8) - prev[c];
195 /* Rounding to nearest integer here is really important! */
196 qi = (f+QCONST32(.5f,DB_SHIFT+7))>>(DB_SHIFT+7);
197 decay_bound = EXTRACT16(MAX32(-QCONST16(28.f,DB_SHIFT),
198 SUB32((opus_val32)oldEBands[i+c*m->nbEBands],max_decay)));
199#else
200 f = x-coef*oldE-prev[c];
201 /* Rounding to nearest integer here is really important! */
202 qi = (int)floor(.5f+f);
203 decay_bound = MAX16(-QCONST16(28.f,DB_SHIFT), oldEBands[i+c*m->nbEBands]) - max_decay;
204#endif
205 /* Prevent the energy from going down too quickly (e.g. for bands
206 that have just one bin) */
207 if (qi < 0 && x < decay_bound)
208 {
209 qi += (int)SHR16(SUB16(decay_bound,x), DB_SHIFT);
210 if (qi > 0)
211 qi = 0;
212 }
213 qi0 = qi;
214 /* If we don't have enough bits to encode all the energy, just assume
215 something safe. */
216 tell = ec_tell(enc);
217 bits_left = budget-tell-3*C*(end-i);
218 if (i!=start && bits_left < 30)
219 {
220 if (bits_left < 24)
221 qi = IMIN(1, qi);
222 if (bits_left < 16)
223 qi = IMAX(-1, qi);
224 }
225 if (budget-tell >= 15)
226 {
227 int pi;
228 pi = 2*IMIN(i,20);
229 ec_laplace_encode(enc, &qi,
230 prob_model[pi]<<7, prob_model[pi+1]<<6);
231 }
232 else if(budget-tell >= 2)
233 {
234 qi = IMAX(-1, IMIN(qi, 1));
235 ec_enc_icdf(enc, 2*qi^-(qi<0), small_energy_icdf, 2);
236 }
237 else if(budget-tell >= 1)
238 {
239 qi = IMIN(0, qi);
240 ec_enc_bit_logp(enc, -qi, 1);
241 }
242 else
243 qi = -1;
244 error[i+c*m->nbEBands] = PSHR32(f,7) - SHL16(qi,DB_SHIFT);
245 badness += abs(qi0-qi);
246 q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT);
247
248 tmp = PSHR32(MULT16_16(coef,oldE),8) + prev[c] + SHL32(q,7);
249#ifdef FIXED_POINT
250 tmp = MAX32(-QCONST32(28.f, DB_SHIFT+7), tmp);
251#endif
252 oldEBands[i+c*m->nbEBands] = PSHR32(tmp, 7);
253 prev[c] = prev[c] + SHL32(q,7) - MULT16_16(beta,PSHR32(q,8));
254 } while (++c < C);
255 }
256 return badness;
257}
258
259void quant_coarse_energy(const CELTMode *m, int start, int end, int effEnd,
260 const opus_val16 *eBands, opus_val16 *oldEBands, opus_uint32 budget,
261 opus_val16 *error, ec_enc *enc, int C, int LM, int nbAvailableBytes,
262 int force_intra, opus_val32 *delayedIntra, int two_pass, int loss_rate)
263{
264 int intra;
265 opus_val16 max_decay;
266 VARDECL(opus_val16, oldEBands_intra);
267 VARDECL(opus_val16, error_intra);
268 ec_enc enc_start_state;
269 opus_uint32 tell;
270 int badness1=0;
271 opus_int32 intra_bias;
272 opus_val32 new_distortion;
273 SAVE_STACK;
274
275 intra = force_intra || (!two_pass && *delayedIntra>2*C*(end-start) && nbAvailableBytes > (end-start)*C);
276 intra_bias = (opus_int32)((budget**delayedIntra*loss_rate)/(C*512));
277 new_distortion = loss_distortion(eBands, oldEBands, start, effEnd, m->nbEBands, C);
278
279 tell = ec_tell(enc);
280 if (tell+3 > budget)
281 two_pass = intra = 0;
282
283 /* Encode the global flags using a simple probability model
284 (first symbols in the stream) */
285
286#ifdef FIXED_POINT
287 max_decay = MIN32(QCONST16(16.f,DB_SHIFT), SHL32(EXTEND32(nbAvailableBytes),DB_SHIFT-3));
288#else
289 max_decay = MIN32(16.f, .125f*nbAvailableBytes);
290#endif
291
292 enc_start_state = *enc;
293
294 ALLOC(oldEBands_intra, C*m->nbEBands, opus_val16);
295 ALLOC(error_intra, C*m->nbEBands, opus_val16);
296 OPUS_COPY(oldEBands_intra, oldEBands, C*m->nbEBands);
297
298 if (two_pass || intra)
299 {
300 badness1 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands_intra, budget,
301 tell, e_prob_model[LM][1], error_intra, enc, C, LM, 1, max_decay);
302 }
303
304 if (!intra)
305 {
306 unsigned char *intra_buf;
307 ec_enc enc_intra_state;
308 opus_int32 tell_intra;
309 opus_uint32 nstart_bytes;
310 opus_uint32 nintra_bytes;
311 int badness2;
312 VARDECL(unsigned char, intra_bits);
313
314 tell_intra = ec_tell_frac(enc);
315
316 enc_intra_state = *enc;
317
318 nstart_bytes = ec_range_bytes(&enc_start_state);
319 nintra_bytes = ec_range_bytes(&enc_intra_state);
320 intra_buf = ec_get_buffer(&enc_intra_state) + nstart_bytes;
321 ALLOC(intra_bits, nintra_bytes-nstart_bytes, unsigned char);
322 /* Copy bits from intra bit-stream */
323 OPUS_COPY(intra_bits, intra_buf, nintra_bytes - nstart_bytes);
324
325 *enc = enc_start_state;
326
327 badness2 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands, budget,
328 tell, e_prob_model[LM][intra], error, enc, C, LM, 0, max_decay);
329
330 if (two_pass && (badness1 < badness2 || (badness1 == badness2 && ((opus_int32)ec_tell_frac(enc))+intra_bias > tell_intra)))
331 {
332 *enc = enc_intra_state;
333 /* Copy intra bits to bit-stream */
334 OPUS_COPY(intra_buf, intra_bits, nintra_bytes - nstart_bytes);
335 OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
336 OPUS_COPY(error, error_intra, C*m->nbEBands);
337 intra = 1;
338 }
339 } else {
340 OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
341 OPUS_COPY(error, error_intra, C*m->nbEBands);
342 }
343
344 if (intra)
345 *delayedIntra = new_distortion;
346 else
347 *delayedIntra = ADD32(MULT16_32_Q15(MULT16_16_Q15(pred_coef[LM], pred_coef[LM]),*delayedIntra),
348 new_distortion);
349
350 RESTORE_STACK;
351}
352
353void quant_fine_energy(const CELTMode *m, int start, int end, opus_val16 *oldEBands, opus_val16 *error, int *fine_quant, ec_enc *enc, int C)
354{
355 int i, c;
356
357 /* Encode finer resolution */
358 for (i=start;i<end;i++)
359 {
360 opus_int16 frac = 1<<fine_quant[i];
361 if (fine_quant[i] <= 0)
362 continue;
363 c=0;
364 do {
365 int q2;
366 opus_val16 offset;
367#ifdef FIXED_POINT
368 /* Has to be without rounding */
369 q2 = (error[i+c*m->nbEBands]+QCONST16(.5f,DB_SHIFT))>>(DB_SHIFT-fine_quant[i]);
370#else
371 q2 = (int)floor((error[i+c*m->nbEBands]+.5f)*frac);
372#endif
373 if (q2 > frac-1)
374 q2 = frac-1;
375 if (q2<0)
376 q2 = 0;
377 ec_enc_bits(enc, q2, fine_quant[i]);
378#ifdef FIXED_POINT
379 offset = SUB16(SHR32(SHL32(EXTEND32(q2),DB_SHIFT)+QCONST16(.5f,DB_SHIFT),fine_quant[i]),QCONST16(.5f,DB_SHIFT));
380#else
381 offset = (q2+.5f)*(1<<(14-fine_quant[i]))*(1.f/16384) - .5f;
382#endif
383 oldEBands[i+c*m->nbEBands] += offset;
384 error[i+c*m->nbEBands] -= offset;
385 /*printf ("%f ", error[i] - offset);*/
386 } while (++c < C);
387 }
388}
389
390void quant_energy_finalise(const CELTMode *m, int start, int end, opus_val16 *oldEBands, opus_val16 *error, int *fine_quant, int *fine_priority, int bits_left, ec_enc *enc, int C)
391{
392 int i, prio, c;
393
394 /* Use up the remaining bits */
395 for (prio=0;prio<2;prio++)
396 {
397 for (i=start;i<end && bits_left>=C ;i++)
398 {
399 if (fine_quant[i] >= MAX_FINE_BITS || fine_priority[i]!=prio)
400 continue;
401 c=0;
402 do {
403 int q2;
404 opus_val16 offset;
405 q2 = error[i+c*m->nbEBands]<0 ? 0 : 1;
406 ec_enc_bits(enc, q2, 1);
407#ifdef FIXED_POINT
408 offset = SHR16(SHL16(q2,DB_SHIFT)-QCONST16(.5f,DB_SHIFT),fine_quant[i]+1);
409#else
410 offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384);
411#endif
412 oldEBands[i+c*m->nbEBands] += offset;
413 bits_left--;
414 } while (++c < C);
415 }
416 }
417}
418
419void unquant_coarse_energy(const CELTMode *m, int start, int end, opus_val16 *oldEBands, int intra, ec_dec *dec, int C, int LM)
420{
421 const unsigned char *prob_model = e_prob_model[LM][intra];
422 int i, c;
423 opus_val32 prev[2] = {0, 0};
424 opus_val16 coef;
425 opus_val16 beta;
426 opus_int32 budget;
427 opus_int32 tell;
428
429 if (intra)
430 {
431 coef = 0;
432 beta = beta_intra;
433 } else {
434 beta = beta_coef[LM];
435 coef = pred_coef[LM];
436 }
437
438 budget = dec->storage*8;
439
440 /* Decode at a fixed coarse resolution */
441 for (i=start;i<end;i++)
442 {
443 c=0;
444 do {
445 int qi;
446 opus_val32 q;
447 opus_val32 tmp;
448 /* It would be better to express this invariant as a
449 test on C at function entry, but that isn't enough
450 to make the static analyzer happy. */
451 celt_assert(c<2);
452 tell = ec_tell(dec);
453 if(budget-tell>=15)
454 {
455 int pi;
456 pi = 2*IMIN(i,20);
457 qi = ec_laplace_decode(dec,
458 prob_model[pi]<<7, prob_model[pi+1]<<6);
459 }
460 else if(budget-tell>=2)
461 {
462 qi = ec_dec_icdf(dec, small_energy_icdf, 2);
463 qi = (qi>>1)^-(qi&1);
464 }
465 else if(budget-tell>=1)
466 {
467 qi = -ec_dec_bit_logp(dec, 1);
468 }
469 else
470 qi = -1;
471 q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT);
472
473 oldEBands[i+c*m->nbEBands] = MAX16(-QCONST16(9.f,DB_SHIFT), oldEBands[i+c*m->nbEBands]);
474 tmp = PSHR32(MULT16_16(coef,oldEBands[i+c*m->nbEBands]),8) + prev[c] + SHL32(q,7);
475#ifdef FIXED_POINT
476 tmp = MAX32(-QCONST32(28.f, DB_SHIFT+7), tmp);
477#endif
478 oldEBands[i+c*m->nbEBands] = PSHR32(tmp, 7);
479 prev[c] = prev[c] + SHL32(q,7) - MULT16_16(beta,PSHR32(q,8));
480 } while (++c < C);
481 }
482}
483
484void unquant_fine_energy(const CELTMode *m, int start, int end, opus_val16 *oldEBands, int *fine_quant, ec_dec *dec, int C)
485{
486 int i, c;
487 /* Decode finer resolution */
488 for (i=start;i<end;i++)
489 {
490 if (fine_quant[i] <= 0)
491 continue;
492 c=0;
493 do {
494 int q2;
495 opus_val16 offset;
496 q2 = ec_dec_bits(dec, fine_quant[i]);
497#ifdef FIXED_POINT
498 offset = SUB16(SHR32(SHL32(EXTEND32(q2),DB_SHIFT)+QCONST16(.5f,DB_SHIFT),fine_quant[i]),QCONST16(.5f,DB_SHIFT));
499#else
500 offset = (q2+.5f)*(1<<(14-fine_quant[i]))*(1.f/16384) - .5f;
501#endif
502 oldEBands[i+c*m->nbEBands] += offset;
503 } while (++c < C);
504 }
505}
506
507void unquant_energy_finalise(const CELTMode *m, int start, int end, opus_val16 *oldEBands, int *fine_quant, int *fine_priority, int bits_left, ec_dec *dec, int C)
508{
509 int i, prio, c;
510
511 /* Use up the remaining bits */
512 for (prio=0;prio<2;prio++)
513 {
514 for (i=start;i<end && bits_left>=C ;i++)
515 {
516 if (fine_quant[i] >= MAX_FINE_BITS || fine_priority[i]!=prio)
517 continue;
518 c=0;
519 do {
520 int q2;
521 opus_val16 offset;
522 q2 = ec_dec_bits(dec, 1);
523#ifdef FIXED_POINT
524 offset = SHR16(SHL16(q2,DB_SHIFT)-QCONST16(.5f,DB_SHIFT),fine_quant[i]+1);
525#else
526 offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384);
527#endif
528 oldEBands[i+c*m->nbEBands] += offset;
529 bits_left--;
530 } while (++c < C);
531 }
532 }
533}
534
535void log2Amp(const CELTMode *m, int start, int end,
536 celt_ener *eBands, const opus_val16 *oldEBands, int C)
537{
538 int c, i;
539 c=0;
540 do {
541 for (i=0;i<start;i++)
542 eBands[i+c*m->nbEBands] = 0;
543 for (;i<end;i++)
544 {
545 opus_val16 lg = ADD16(oldEBands[i+c*m->nbEBands],
546 SHL16((opus_val16)eMeans[i],6));
547 eBands[i+c*m->nbEBands] = PSHR32(celt_exp2(lg),4);
548 }
549 for (;i<m->nbEBands;i++)
550 eBands[i+c*m->nbEBands] = 0;
551 } while (++c < C);
552}
553
554void amp2Log2(const CELTMode *m, int effEnd, int end,
555 celt_ener *bandE, opus_val16 *bandLogE, int C)
556{
557 int c, i;
558 c=0;
559 do {
560 for (i=0;i<effEnd;i++)
561 bandLogE[i+c*m->nbEBands] =
562 celt_log2(SHL32(bandE[i+c*m->nbEBands],2))
563 - SHL16((opus_val16)eMeans[i],6);
564 for (i=effEnd;i<end;i++)
565 bandLogE[c*m->nbEBands+i] = -QCONST16(14.f,DB_SHIFT);
566 } while (++c < C);
567}
diff --git a/lib/rbcodec/codecs/libopus/celt/quant_bands.h b/lib/rbcodec/codecs/libopus/celt/quant_bands.h
new file mode 100644
index 0000000000..bec2855cf0
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/quant_bands.h
@@ -0,0 +1,60 @@
1/* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2009 Xiph.Org Foundation
3 Written by Jean-Marc Valin */
4/*
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
12 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
20 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28
29#ifndef QUANT_BANDS
30#define QUANT_BANDS
31
32#include "arch.h"
33#include "modes.h"
34#include "entenc.h"
35#include "entdec.h"
36#include "mathops.h"
37
38void amp2Log2(const CELTMode *m, int effEnd, int end,
39 celt_ener *bandE, opus_val16 *bandLogE, int C);
40
41void log2Amp(const CELTMode *m, int start, int end,
42 celt_ener *eBands, const opus_val16 *oldEBands, int C);
43
44void quant_coarse_energy(const CELTMode *m, int start, int end, int effEnd,
45 const opus_val16 *eBands, opus_val16 *oldEBands, opus_uint32 budget,
46 opus_val16 *error, ec_enc *enc, int C, int LM,
47 int nbAvailableBytes, int force_intra, opus_val32 *delayedIntra,
48 int two_pass, int loss_rate);
49
50void quant_fine_energy(const CELTMode *m, int start, int end, opus_val16 *oldEBands, opus_val16 *error, int *fine_quant, ec_enc *enc, int C);
51
52void quant_energy_finalise(const CELTMode *m, int start, int end, opus_val16 *oldEBands, opus_val16 *error, int *fine_quant, int *fine_priority, int bits_left, ec_enc *enc, int C);
53
54void unquant_coarse_energy(const CELTMode *m, int start, int end, opus_val16 *oldEBands, int intra, ec_dec *dec, int C, int LM);
55
56void unquant_fine_energy(const CELTMode *m, int start, int end, opus_val16 *oldEBands, int *fine_quant, ec_dec *dec, int C);
57
58void unquant_energy_finalise(const CELTMode *m, int start, int end, opus_val16 *oldEBands, int *fine_quant, int *fine_priority, int bits_left, ec_dec *dec, int C);
59
60#endif /* QUANT_BANDS */
diff --git a/lib/rbcodec/codecs/libopus/celt/rate.c b/lib/rbcodec/codecs/libopus/celt/rate.c
new file mode 100644
index 0000000000..3b056d8dc7
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/rate.c
@@ -0,0 +1,638 @@
1/* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2009 Xiph.Org Foundation
3 Written by Jean-Marc Valin */
4/*
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
12 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
20 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28
29#ifdef HAVE_CONFIG_H
30#include "opus_config.h"
31#endif
32
33#include <math.h>
34#include "modes.h"
35#include "cwrs.h"
36#include "arch.h"
37#include "os_support.h"
38
39#include "entcode.h"
40#include "rate.h"
41
42static const unsigned char LOG2_FRAC_TABLE[24]={
43 0,
44 8,13,
45 16,19,21,23,
46 24,26,27,28,29,30,31,32,
47 32,33,34,34,35,36,36,37,37
48};
49
50#ifdef CUSTOM_MODES
51
52/*Determines if V(N,K) fits in a 32-bit unsigned integer.
53 N and K are themselves limited to 15 bits.*/
54static int fits_in32(int _n, int _k)
55{
56 static const opus_int16 maxN[15] = {
57 32767, 32767, 32767, 1476, 283, 109, 60, 40,
58 29, 24, 20, 18, 16, 14, 13};
59 static const opus_int16 maxK[15] = {
60 32767, 32767, 32767, 32767, 1172, 238, 95, 53,
61 36, 27, 22, 18, 16, 15, 13};
62 if (_n>=14)
63 {
64 if (_k>=14)
65 return 0;
66 else
67 return _n <= maxN[_k];
68 } else {
69 return _k <= maxK[_n];
70 }
71}
72
73void compute_pulse_cache(CELTMode *m, int LM)
74{
75 int C;
76 int i;
77 int j;
78 int curr=0;
79 int nbEntries=0;
80 int entryN[100], entryK[100], entryI[100];
81 const opus_int16 *eBands = m->eBands;
82 PulseCache *cache = &m->cache;
83 opus_int16 *cindex;
84 unsigned char *bits;
85 unsigned char *cap;
86
87 cindex = (opus_int16 *)opus_alloc(sizeof(cache->index[0])*m->nbEBands*(LM+2));
88 cache->index = cindex;
89
90 /* Scan for all unique band sizes */
91 for (i=0;i<=LM+1;i++)
92 {
93 for (j=0;j<m->nbEBands;j++)
94 {
95 int k;
96 int N = (eBands[j+1]-eBands[j])<<i>>1;
97 cindex[i*m->nbEBands+j] = -1;
98 /* Find other bands that have the same size */
99 for (k=0;k<=i;k++)
100 {
101 int n;
102 for (n=0;n<m->nbEBands && (k!=i || n<j);n++)
103 {
104 if (N == (eBands[n+1]-eBands[n])<<k>>1)
105 {
106 cindex[i*m->nbEBands+j] = cindex[k*m->nbEBands+n];
107 break;
108 }
109 }
110 }
111 if (cache->index[i*m->nbEBands+j] == -1 && N!=0)
112 {
113 int K;
114 entryN[nbEntries] = N;
115 K = 0;
116 while (fits_in32(N,get_pulses(K+1)) && K<MAX_PSEUDO)
117 K++;
118 entryK[nbEntries] = K;
119 cindex[i*m->nbEBands+j] = curr;
120 entryI[nbEntries] = curr;
121
122 curr += K+1;
123 nbEntries++;
124 }
125 }
126 }
127 bits = (unsigned char *)opus_alloc(sizeof(unsigned char)*curr);
128 cache->bits = bits;
129 cache->size = curr;
130 /* Compute the cache for all unique sizes */
131 for (i=0;i<nbEntries;i++)
132 {
133 unsigned char *ptr = bits+entryI[i];
134 opus_int16 tmp[MAX_PULSES+1];
135 get_required_bits(tmp, entryN[i], get_pulses(entryK[i]), BITRES);
136 for (j=1;j<=entryK[i];j++)
137 ptr[j] = tmp[get_pulses(j)]-1;
138 ptr[0] = entryK[i];
139 }
140
141 /* Compute the maximum rate for each band at which we'll reliably use as
142 many bits as we ask for. */
143 cache->caps = cap = (unsigned char *)opus_alloc(sizeof(cache->caps[0])*(LM+1)*2*m->nbEBands);
144 for (i=0;i<=LM;i++)
145 {
146 for (C=1;C<=2;C++)
147 {
148 for (j=0;j<m->nbEBands;j++)
149 {
150 int N0;
151 int max_bits;
152 N0 = m->eBands[j+1]-m->eBands[j];
153 /* N=1 bands only have a sign bit and fine bits. */
154 if (N0<<i == 1)
155 max_bits = C*(1+MAX_FINE_BITS)<<BITRES;
156 else
157 {
158 const unsigned char *pcache;
159 opus_int32 num;
160 opus_int32 den;
161 int LM0;
162 int N;
163 int offset;
164 int ndof;
165 int qb;
166 int k;
167 LM0 = 0;
168 /* Even-sized bands bigger than N=2 can be split one more time.
169 As of commit 44203907 all bands >1 are even, including custom modes.*/
170 if (N0 > 2)
171 {
172 N0>>=1;
173 LM0--;
174 }
175 /* N0=1 bands can't be split down to N<2. */
176 else if (N0 <= 1)
177 {
178 LM0=IMIN(i,1);
179 N0<<=LM0;
180 }
181 /* Compute the cost for the lowest-level PVQ of a fully split
182 band. */
183 pcache = bits + cindex[(LM0+1)*m->nbEBands+j];
184 max_bits = pcache[pcache[0]]+1;
185 /* Add in the cost of coding regular splits. */
186 N = N0;
187 for(k=0;k<i-LM0;k++){
188 max_bits <<= 1;
189 /* Offset the number of qtheta bits by log2(N)/2
190 + QTHETA_OFFSET compared to their "fair share" of
191 total/N */
192 offset = ((m->logN[j]+((LM0+k)<<BITRES))>>1)-QTHETA_OFFSET;
193 /* The number of qtheta bits we'll allocate if the remainder
194 is to be max_bits.
195 The average measured cost for theta is 0.89701 times qb,
196 approximated here as 459/512. */
197 num=459*(opus_int32)((2*N-1)*offset+max_bits);
198 den=((opus_int32)(2*N-1)<<9)-459;
199 qb = IMIN((num+(den>>1))/den, 57);
200 celt_assert(qb >= 0);
201 max_bits += qb;
202 N <<= 1;
203 }
204 /* Add in the cost of a stereo split, if necessary. */
205 if (C==2)
206 {
207 max_bits <<= 1;
208 offset = ((m->logN[j]+(i<<BITRES))>>1)-(N==2?QTHETA_OFFSET_TWOPHASE:QTHETA_OFFSET);
209 ndof = 2*N-1-(N==2);
210 /* The average measured cost for theta with the step PDF is
211 0.95164 times qb, approximated here as 487/512. */
212 num = (N==2?512:487)*(opus_int32)(max_bits+ndof*offset);
213 den = ((opus_int32)ndof<<9)-(N==2?512:487);
214 qb = IMIN((num+(den>>1))/den, (N==2?64:61));
215 celt_assert(qb >= 0);
216 max_bits += qb;
217 }
218 /* Add the fine bits we'll use. */
219 /* Compensate for the extra DoF in stereo */
220 ndof = C*N + ((C==2 && N>2) ? 1 : 0);
221 /* Offset the number of fine bits by log2(N)/2 + FINE_OFFSET
222 compared to their "fair share" of total/N */
223 offset = ((m->logN[j] + (i<<BITRES))>>1)-FINE_OFFSET;
224 /* N=2 is the only point that doesn't match the curve */
225 if (N==2)
226 offset += 1<<BITRES>>2;
227 /* The number of fine bits we'll allocate if the remainder is
228 to be max_bits. */
229 num = max_bits+ndof*offset;
230 den = (ndof-1)<<BITRES;
231 qb = IMIN((num+(den>>1))/den, MAX_FINE_BITS);
232 celt_assert(qb >= 0);
233 max_bits += C*qb<<BITRES;
234 }
235 max_bits = (4*max_bits/(C*((m->eBands[j+1]-m->eBands[j])<<i)))-64;
236 celt_assert(max_bits >= 0);
237 celt_assert(max_bits < 256);
238 *cap++ = (unsigned char)max_bits;
239 }
240 }
241 }
242}
243
244#endif /* CUSTOM_MODES */
245
246#define ALLOC_STEPS 6
247
248static inline int interp_bits2pulses(const CELTMode *m, int start, int end, int skip_start,
249 const int *bits1, const int *bits2, const int *thresh, const int *cap, opus_int32 total, opus_int32 *_balance,
250 int skip_rsv, int *intensity, int intensity_rsv, int *dual_stereo, int dual_stereo_rsv, int *bits,
251 int *ebits, int *fine_priority, int C, int LM, ec_ctx *ec, int encode, int prev)
252{
253 opus_int32 psum;
254 int lo, hi;
255 int i, j;
256 int logM;
257 int stereo;
258 int codedBands=-1;
259 int alloc_floor;
260 opus_int32 left, percoeff;
261 int done;
262 opus_int32 balance;
263 SAVE_STACK;
264
265 alloc_floor = C<<BITRES;
266 stereo = C>1;
267
268 logM = LM<<BITRES;
269 lo = 0;
270 hi = 1<<ALLOC_STEPS;
271 for (i=0;i<ALLOC_STEPS;i++)
272 {
273 int mid = (lo+hi)>>1;
274 psum = 0;
275 done = 0;
276 for (j=end;j-->start;)
277 {
278 int tmp = bits1[j] + (mid*(opus_int32)bits2[j]>>ALLOC_STEPS);
279 if (tmp >= thresh[j] || done)
280 {
281 done = 1;
282 /* Don't allocate more than we can actually use */
283 psum += IMIN(tmp, cap[j]);
284 } else {
285 if (tmp >= alloc_floor)
286 psum += alloc_floor;
287 }
288 }
289 if (psum > total)
290 hi = mid;
291 else
292 lo = mid;
293 }
294 psum = 0;
295 /*printf ("interp bisection gave %d\n", lo);*/
296 done = 0;
297 for (j=end;j-->start;)
298 {
299 int tmp = bits1[j] + (lo*bits2[j]>>ALLOC_STEPS);
300 if (tmp < thresh[j] && !done)
301 {
302 if (tmp >= alloc_floor)
303 tmp = alloc_floor;
304 else
305 tmp = 0;
306 } else
307 done = 1;
308 /* Don't allocate more than we can actually use */
309 tmp = IMIN(tmp, cap[j]);
310 bits[j] = tmp;
311 psum += tmp;
312 }
313
314 /* Decide which bands to skip, working backwards from the end. */
315 for (codedBands=end;;codedBands--)
316 {
317 int band_width;
318 int band_bits;
319 int rem;
320 j = codedBands-1;
321 /* Never skip the first band, nor a band that has been boosted by
322 dynalloc.
323 In the first case, we'd be coding a bit to signal we're going to waste
324 all the other bits.
325 In the second case, we'd be coding a bit to redistribute all the bits
326 we just signaled should be cocentrated in this band. */
327 if (j<=skip_start)
328 {
329 /* Give the bit we reserved to end skipping back. */
330 total += skip_rsv;
331 break;
332 }
333 /*Figure out how many left-over bits we would be adding to this band.
334 This can include bits we've stolen back from higher, skipped bands.*/
335 left = total-psum;
336 percoeff = left/(m->eBands[codedBands]-m->eBands[start]);
337 left -= (m->eBands[codedBands]-m->eBands[start])*percoeff;
338 rem = IMAX(left-(m->eBands[j]-m->eBands[start]),0);
339 band_width = m->eBands[codedBands]-m->eBands[j];
340 band_bits = (int)(bits[j] + percoeff*band_width + rem);
341 /*Only code a skip decision if we're above the threshold for this band.
342 Otherwise it is force-skipped.
343 This ensures that we have enough bits to code the skip flag.*/
344 if (band_bits >= IMAX(thresh[j], alloc_floor+(1<<BITRES)))
345 {
346 if (encode)
347 {
348 /*This if() block is the only part of the allocation function that
349 is not a mandatory part of the bitstream: any bands we choose to
350 skip here must be explicitly signaled.*/
351 /*Choose a threshold with some hysteresis to keep bands from
352 fluctuating in and out.*/
353#ifdef FUZZING
354 if ((rand()&0x1) == 0)
355#else
356 if (band_bits > ((j<prev?7:9)*band_width<<LM<<BITRES)>>4)
357#endif
358 {
359 ec_enc_bit_logp(ec, 1, 1);
360 break;
361 }
362 ec_enc_bit_logp(ec, 0, 1);
363 } else if (ec_dec_bit_logp(ec, 1)) {
364 break;
365 }
366 /*We used a bit to skip this band.*/
367 psum += 1<<BITRES;
368 band_bits -= 1<<BITRES;
369 }
370 /*Reclaim the bits originally allocated to this band.*/
371 psum -= bits[j]+intensity_rsv;
372 if (intensity_rsv > 0)
373 intensity_rsv = LOG2_FRAC_TABLE[j-start];
374 psum += intensity_rsv;
375 if (band_bits >= alloc_floor)
376 {
377 /*If we have enough for a fine energy bit per channel, use it.*/
378 psum += alloc_floor;
379 bits[j] = alloc_floor;
380 } else {
381 /*Otherwise this band gets nothing at all.*/
382 bits[j] = 0;
383 }
384 }
385
386 celt_assert(codedBands > start);
387 /* Code the intensity and dual stereo parameters. */
388 if (intensity_rsv > 0)
389 {
390 if (encode)
391 {
392 *intensity = IMIN(*intensity, codedBands);
393 ec_enc_uint(ec, *intensity-start, codedBands+1-start);
394 }
395 else
396 *intensity = start+ec_dec_uint(ec, codedBands+1-start);
397 }
398 else
399 *intensity = 0;
400 if (*intensity <= start)
401 {
402 total += dual_stereo_rsv;
403 dual_stereo_rsv = 0;
404 }
405 if (dual_stereo_rsv > 0)
406 {
407 if (encode)
408 ec_enc_bit_logp(ec, *dual_stereo, 1);
409 else
410 *dual_stereo = ec_dec_bit_logp(ec, 1);
411 }
412 else
413 *dual_stereo = 0;
414
415 /* Allocate the remaining bits */
416 left = total-psum;
417 percoeff = left/(m->eBands[codedBands]-m->eBands[start]);
418 left -= (m->eBands[codedBands]-m->eBands[start])*percoeff;
419 for (j=start;j<codedBands;j++)
420 bits[j] += ((int)percoeff*(m->eBands[j+1]-m->eBands[j]));
421 for (j=start;j<codedBands;j++)
422 {
423 int tmp = (int)IMIN(left, m->eBands[j+1]-m->eBands[j]);
424 bits[j] += tmp;
425 left -= tmp;
426 }
427 /*for (j=0;j<end;j++)printf("%d ", bits[j]);printf("\n");*/
428
429 balance = 0;
430 for (j=start;j<codedBands;j++)
431 {
432 int N0, N, den;
433 int offset;
434 int NClogN;
435 opus_int32 excess, bit;
436
437 celt_assert(bits[j] >= 0);
438 N0 = m->eBands[j+1]-m->eBands[j];
439 N=N0<<LM;
440 bit = (opus_int32)bits[j]+balance;
441
442 if (N>1)
443 {
444 excess = MAX32(bit-cap[j],0);
445 bits[j] = bit-excess;
446
447 /* Compensate for the extra DoF in stereo */
448 den=(C*N+ ((C==2 && N>2 && !*dual_stereo && j<*intensity) ? 1 : 0));
449
450 NClogN = den*(m->logN[j] + logM);
451
452 /* Offset for the number of fine bits by log2(N)/2 + FINE_OFFSET
453 compared to their "fair share" of total/N */
454 offset = (NClogN>>1)-den*FINE_OFFSET;
455
456 /* N=2 is the only point that doesn't match the curve */
457 if (N==2)
458 offset += den<<BITRES>>2;
459
460 /* Changing the offset for allocating the second and third
461 fine energy bit */
462 if (bits[j] + offset < den*2<<BITRES)
463 offset += NClogN>>2;
464 else if (bits[j] + offset < den*3<<BITRES)
465 offset += NClogN>>3;
466
467 /* Divide with rounding */
468 ebits[j] = IMAX(0, (bits[j] + offset + (den<<(BITRES-1))) / (den<<BITRES));
469
470 /* Make sure not to bust */
471 if (C*ebits[j] > (bits[j]>>BITRES))
472 ebits[j] = bits[j] >> stereo >> BITRES;
473
474 /* More than that is useless because that's about as far as PVQ can go */
475 ebits[j] = IMIN(ebits[j], MAX_FINE_BITS);
476
477 /* If we rounded down or capped this band, make it a candidate for the
478 final fine energy pass */
479 fine_priority[j] = ebits[j]*(den<<BITRES) >= bits[j]+offset;
480
481 /* Remove the allocated fine bits; the rest are assigned to PVQ */
482 bits[j] -= C*ebits[j]<<BITRES;
483
484 } else {
485 /* For N=1, all bits go to fine energy except for a single sign bit */
486 excess = MAX32(0,bit-(C<<BITRES));
487 bits[j] = bit-excess;
488 ebits[j] = 0;
489 fine_priority[j] = 1;
490 }
491
492 /* Fine energy can't take advantage of the re-balancing in
493 quant_all_bands().
494 Instead, do the re-balancing here.*/
495 if(excess > 0)
496 {
497 int extra_fine;
498 int extra_bits;
499 extra_fine = IMIN(excess>>(stereo+BITRES),MAX_FINE_BITS-ebits[j]);
500 ebits[j] += extra_fine;
501 extra_bits = extra_fine*C<<BITRES;
502 fine_priority[j] = extra_bits >= excess-balance;
503 excess -= extra_bits;
504 }
505 balance = excess;
506
507 celt_assert(bits[j] >= 0);
508 celt_assert(ebits[j] >= 0);
509 }
510 /* Save any remaining bits over the cap for the rebalancing in
511 quant_all_bands(). */
512 *_balance = balance;
513
514 /* The skipped bands use all their bits for fine energy. */
515 for (;j<end;j++)
516 {
517 ebits[j] = bits[j] >> stereo >> BITRES;
518 celt_assert(C*ebits[j]<<BITRES == bits[j]);
519 bits[j] = 0;
520 fine_priority[j] = ebits[j]<1;
521 }
522 RESTORE_STACK;
523 return codedBands;
524}
525
526int compute_allocation(const CELTMode *m, int start, int end, const int *offsets, const int *cap, int alloc_trim, int *intensity, int *dual_stereo,
527 opus_int32 total, opus_int32 *balance, int *pulses, int *ebits, int *fine_priority, int C, int LM, ec_ctx *ec, int encode, int prev)
528{
529 int lo, hi, len, j;
530 int codedBands;
531 int skip_start;
532 int skip_rsv;
533 int intensity_rsv;
534 int dual_stereo_rsv;
535 VARDECL(int, bits1);
536 VARDECL(int, bits2);
537 VARDECL(int, thresh);
538 VARDECL(int, trim_offset);
539 SAVE_STACK;
540
541 total = IMAX(total, 0);
542 len = m->nbEBands;
543 skip_start = start;
544 /* Reserve a bit to signal the end of manually skipped bands. */
545 skip_rsv = total >= 1<<BITRES ? 1<<BITRES : 0;
546 total -= skip_rsv;
547 /* Reserve bits for the intensity and dual stereo parameters. */
548 intensity_rsv = dual_stereo_rsv = 0;
549 if (C==2)
550 {
551 intensity_rsv = LOG2_FRAC_TABLE[end-start];
552 if (intensity_rsv>total)
553 intensity_rsv = 0;
554 else
555 {
556 total -= intensity_rsv;
557 dual_stereo_rsv = total>=1<<BITRES ? 1<<BITRES : 0;
558 total -= dual_stereo_rsv;
559 }
560 }
561 ALLOC(bits1, len, int);
562 ALLOC(bits2, len, int);
563 ALLOC(thresh, len, int);
564 ALLOC(trim_offset, len, int);
565
566 for (j=start;j<end;j++)
567 {
568 /* Below this threshold, we're sure not to allocate any PVQ bits */
569 thresh[j] = IMAX((C)<<BITRES, (3*(m->eBands[j+1]-m->eBands[j])<<LM<<BITRES)>>4);
570 /* Tilt of the allocation curve */
571 trim_offset[j] = C*(m->eBands[j+1]-m->eBands[j])*(alloc_trim-5-LM)*(end-j-1)
572 *(1<<(LM+BITRES))>>6;
573 /* Giving less resolution to single-coefficient bands because they get
574 more benefit from having one coarse value per coefficient*/
575 if ((m->eBands[j+1]-m->eBands[j])<<LM==1)
576 trim_offset[j] -= C<<BITRES;
577 }
578 lo = 1;
579 hi = m->nbAllocVectors - 1;
580 do
581 {
582 int done = 0;
583 int psum = 0;
584 int mid = (lo+hi) >> 1;
585 for (j=end;j-->start;)
586 {
587 int bitsj;
588 int N = m->eBands[j+1]-m->eBands[j];
589 bitsj = C*N*m->allocVectors[mid*len+j]<<LM>>2;
590 if (bitsj > 0)
591 bitsj = IMAX(0, bitsj + trim_offset[j]);
592 bitsj += offsets[j];
593 if (bitsj >= thresh[j] || done)
594 {
595 done = 1;
596 /* Don't allocate more than we can actually use */
597 psum += IMIN(bitsj, cap[j]);
598 } else {
599 if (bitsj >= C<<BITRES)
600 psum += C<<BITRES;
601 }
602 }
603 if (psum > total)
604 hi = mid - 1;
605 else
606 lo = mid + 1;
607 /*printf ("lo = %d, hi = %d\n", lo, hi);*/
608 }
609 while (lo <= hi);
610 hi = lo--;
611 /*printf ("interp between %d and %d\n", lo, hi);*/
612 for (j=start;j<end;j++)
613 {
614 int bits1j, bits2j;
615 int N = m->eBands[j+1]-m->eBands[j];
616 bits1j = C*N*m->allocVectors[lo*len+j]<<LM>>2;
617 bits2j = hi>=m->nbAllocVectors ?
618 cap[j] : C*N*m->allocVectors[hi*len+j]<<LM>>2;
619 if (bits1j > 0)
620 bits1j = IMAX(0, bits1j + trim_offset[j]);
621 if (bits2j > 0)
622 bits2j = IMAX(0, bits2j + trim_offset[j]);
623 if (lo > 0)
624 bits1j += offsets[j];
625 bits2j += offsets[j];
626 if (offsets[j]>0)
627 skip_start = j;
628 bits2j = IMAX(0,bits2j-bits1j);
629 bits1[j] = bits1j;
630 bits2[j] = bits2j;
631 }
632 codedBands = interp_bits2pulses(m, start, end, skip_start, bits1, bits2, thresh, cap,
633 total, balance, skip_rsv, intensity, intensity_rsv, dual_stereo, dual_stereo_rsv,
634 pulses, ebits, fine_priority, C, LM, ec, encode, prev);
635 RESTORE_STACK;
636 return codedBands;
637}
638
diff --git a/lib/rbcodec/codecs/libopus/celt/rate.h b/lib/rbcodec/codecs/libopus/celt/rate.h
new file mode 100644
index 0000000000..e0d5022326
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/rate.h
@@ -0,0 +1,101 @@
1/* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2009 Xiph.Org Foundation
3 Written by Jean-Marc Valin */
4/*
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
12 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
20 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28
29#ifndef RATE_H
30#define RATE_H
31
32#define MAX_PSEUDO 40
33#define LOG_MAX_PSEUDO 6
34
35#define MAX_PULSES 128
36
37#define MAX_FINE_BITS 8
38
39#define FINE_OFFSET 21
40#define QTHETA_OFFSET 4
41#define QTHETA_OFFSET_TWOPHASE 16
42
43#include "cwrs.h"
44#include "modes.h"
45
46void compute_pulse_cache(CELTMode *m, int LM);
47
48static inline int get_pulses(int i)
49{
50 return i<8 ? i : (8 + (i&7)) << ((i>>3)-1);
51}
52
53static inline int bits2pulses(const CELTMode *m, int band, int LM, int bits)
54{
55 int i;
56 int lo, hi;
57 const unsigned char *cache;
58
59 LM++;
60 cache = m->cache.bits + m->cache.index[LM*m->nbEBands+band];
61
62 lo = 0;
63 hi = cache[0];
64 bits--;
65 for (i=0;i<LOG_MAX_PSEUDO;i++)
66 {
67 int mid = (lo+hi+1)>>1;
68 /* OPT: Make sure this is implemented with a conditional move */
69 if ((int)cache[mid] >= bits)
70 hi = mid;
71 else
72 lo = mid;
73 }
74 if (bits- (lo == 0 ? -1 : (int)cache[lo]) <= (int)cache[hi]-bits)
75 return lo;
76 else
77 return hi;
78}
79
80static inline int pulses2bits(const CELTMode *m, int band, int LM, int pulses)
81{
82 const unsigned char *cache;
83
84 LM++;
85 cache = m->cache.bits + m->cache.index[LM*m->nbEBands+band];
86 return pulses == 0 ? 0 : cache[pulses]+1;
87}
88
89/** Compute the pulse allocation, i.e. how many pulses will go in each
90 * band.
91 @param m mode
92 @param offsets Requested increase or decrease in the number of bits for
93 each band
94 @param total Number of bands
95 @param pulses Number of pulses per band (returned)
96 @return Total number of bits allocated
97*/
98int compute_allocation(const CELTMode *m, int start, int end, const int *offsets, const int *cap, int alloc_trim, int *intensity, int *dual_stero,
99 opus_int32 total, opus_int32 *balance, int *pulses, int *ebits, int *fine_priority, int C, int LM, ec_ctx *ec, int encode, int prev);
100
101#endif
diff --git a/lib/rbcodec/codecs/libopus/celt/stack_alloc.h b/lib/rbcodec/codecs/libopus/celt/stack_alloc.h
new file mode 100644
index 0000000000..a6f06d2263
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/stack_alloc.h
@@ -0,0 +1,149 @@
1/* Copyright (C) 2002-2003 Jean-Marc Valin
2 Copyright (C) 2007-2009 Xiph.Org Foundation */
3/**
4 @file stack_alloc.h
5 @brief Temporary memory allocation on stack
6*/
7/*
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions
10 are met:
11
12 - Redistributions of source code must retain the above copyright
13 notice, this list of conditions and the following disclaimer.
14
15 - Redistributions in binary form must reproduce the above copyright
16 notice, this list of conditions and the following disclaimer in the
17 documentation and/or other materials provided with the distribution.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
23 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30*/
31
32#ifndef STACK_ALLOC_H
33#define STACK_ALLOC_H
34
35#if (!defined (VAR_ARRAYS) && !defined (USE_ALLOCA) && !defined (NONTHREADSAFE_PSEUDOSTACK))
36#error "Opus requires one of VAR_ARRAYS, USE_ALLOCA, or NONTHREADSAFE_PSEUDOSTACK be defined to select the temporary allocation mode."
37#endif
38
39#ifdef USE_ALLOCA
40# ifdef WIN32
41# include <malloc.h>
42# else
43# ifdef HAVE_ALLOCA_H
44# include <alloca.h>
45# else
46# include <stdlib.h>
47# endif
48# endif
49#endif
50
51/**
52 * @def ALIGN(stack, size)
53 *
54 * Aligns the stack to a 'size' boundary
55 *
56 * @param stack Stack
57 * @param size New size boundary
58 */
59
60/**
61 * @def PUSH(stack, size, type)
62 *
63 * Allocates 'size' elements of type 'type' on the stack
64 *
65 * @param stack Stack
66 * @param size Number of elements
67 * @param type Type of element
68 */
69
70/**
71 * @def VARDECL(var)
72 *
73 * Declare variable on stack
74 *
75 * @param var Variable to declare
76 */
77
78/**
79 * @def ALLOC(var, size, type)
80 *
81 * Allocate 'size' elements of 'type' on stack
82 *
83 * @param var Name of variable to allocate
84 * @param size Number of elements
85 * @param type Type of element
86 */
87
88#if defined(VAR_ARRAYS)
89
90#define VARDECL(type, var)
91#define ALLOC(var, size, type) type var[size]
92#define SAVE_STACK
93#define RESTORE_STACK
94#define ALLOC_STACK
95
96#elif defined(USE_ALLOCA)
97
98#define VARDECL(type, var) type *var
99
100# ifdef WIN32
101# define ALLOC(var, size, type) var = ((type*)_alloca(sizeof(type)*(size)))
102# else
103# define ALLOC(var, size, type) var = ((type*)alloca(sizeof(type)*(size)))
104# endif
105
106#define SAVE_STACK
107#define RESTORE_STACK
108#define ALLOC_STACK
109
110#else
111
112#ifdef CELT_C
113char *global_stack=0;
114#else
115extern char *global_stack;
116#endif /* CELT_C */
117
118#ifdef ENABLE_VALGRIND
119
120#include <valgrind/memcheck.h>
121
122#ifdef CELT_C
123char *global_stack_top=0;
124#else
125extern char *global_stack_top;
126#endif /* CELT_C */
127
128#define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1))
129#define PUSH(stack, size, type) (VALGRIND_MAKE_MEM_NOACCESS(stack, global_stack_top-stack),ALIGN((stack),sizeof(type)/sizeof(char)),VALGRIND_MAKE_MEM_UNDEFINED(stack, ((size)*sizeof(type)/sizeof(char))),(stack)+=(2*(size)*sizeof(type)/sizeof(char)),(type*)((stack)-(2*(size)*sizeof(type)/sizeof(char))))
130#define RESTORE_STACK ((global_stack = _saved_stack),VALGRIND_MAKE_MEM_NOACCESS(global_stack, global_stack_top-global_stack))
131#define ALLOC_STACK char *_saved_stack; ((global_stack = (global_stack==0) ? ((global_stack_top=opus_alloc_scratch(GLOBAL_STACK_SIZE*2)+(GLOBAL_STACK_SIZE*2))-(GLOBAL_STACK_SIZE*2)) : global_stack),VALGRIND_MAKE_MEM_NOACCESS(global_stack, global_stack_top-global_stack)); _saved_stack = global_stack;
132
133#else
134
135#define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1))
136#define PUSH(stack, size, type) (ALIGN((stack),sizeof(type)/sizeof(char)),(stack)+=(size)*(sizeof(type)/sizeof(char)),(type*)((stack)-(size)*(sizeof(type)/sizeof(char))))
137#define RESTORE_STACK (global_stack = _saved_stack)
138#define ALLOC_STACK char *_saved_stack; (global_stack = (global_stack==0) ? opus_alloc_scratch(GLOBAL_STACK_SIZE) : global_stack); _saved_stack = global_stack;
139
140#endif /* ENABLE_VALGRIND */
141
142#include "os_support.h"
143#define VARDECL(type, var) type *var
144#define ALLOC(var, size, type) var = PUSH(global_stack, size, type)
145#define SAVE_STACK char *_saved_stack = global_stack;
146
147#endif /* VAR_ARRAYS */
148
149#endif /* STACK_ALLOC_H */
diff --git a/lib/rbcodec/codecs/libopus/celt/static_modes_fixed.h b/lib/rbcodec/codecs/libopus/celt/static_modes_fixed.h
new file mode 100644
index 0000000000..216df9e605
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/static_modes_fixed.h
@@ -0,0 +1,595 @@
1/* The contents of this file was automatically generated by dump_modes.c
2 with arguments: 48000 960
3 It contains static definitions for some pre-defined modes. */
4#include "modes.h"
5#include "rate.h"
6
7#ifndef DEF_WINDOW120
8#define DEF_WINDOW120
9static const opus_val16 window120[120] = {
102, 20, 55, 108, 178,
11266, 372, 494, 635, 792,
12966, 1157, 1365, 1590, 1831,
132089, 2362, 2651, 2956, 3276,
143611, 3961, 4325, 4703, 5094,
155499, 5916, 6346, 6788, 7241,
167705, 8179, 8663, 9156, 9657,
1710167, 10684, 11207, 11736, 12271,
1812810, 13353, 13899, 14447, 14997,
1915547, 16098, 16648, 17197, 17744,
2018287, 18827, 19363, 19893, 20418,
2120936, 21447, 21950, 22445, 22931,
2223407, 23874, 24330, 24774, 25208,
2325629, 26039, 26435, 26819, 27190,
2427548, 27893, 28224, 28541, 28845,
2529135, 29411, 29674, 29924, 30160,
2630384, 30594, 30792, 30977, 31151,
2731313, 31463, 31602, 31731, 31849,
2831958, 32057, 32148, 32229, 32303,
2932370, 32429, 32481, 32528, 32568,
3032604, 32634, 32661, 32683, 32701,
3132717, 32729, 32740, 32748, 32754,
3232758, 32762, 32764, 32766, 32767,
3332767, 32767, 32767, 32767, 32767,
34};
35#endif
36
37#ifndef DEF_LOGN400
38#define DEF_LOGN400
39static const opus_int16 logN400[21] = {
400, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8, 16, 16, 16, 21, 21, 24, 29, 34, 36, };
41#endif
42
43#ifndef DEF_PULSE_CACHE50
44#define DEF_PULSE_CACHE50
45static const opus_int16 cache_index50[105] = {
46-1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 41, 41, 41,
4782, 82, 123, 164, 200, 222, 0, 0, 0, 0, 0, 0, 0, 0, 41,
4841, 41, 41, 123, 123, 123, 164, 164, 240, 266, 283, 295, 41, 41, 41,
4941, 41, 41, 41, 41, 123, 123, 123, 123, 240, 240, 240, 266, 266, 305,
50318, 328, 336, 123, 123, 123, 123, 123, 123, 123, 123, 240, 240, 240, 240,
51305, 305, 305, 318, 318, 343, 351, 358, 364, 240, 240, 240, 240, 240, 240,
52240, 240, 305, 305, 305, 305, 343, 343, 343, 351, 351, 370, 376, 382, 387,
53};
54static const unsigned char cache_bits50[392] = {
5540, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
567, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
577, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 40, 15, 23, 28,
5831, 34, 36, 38, 39, 41, 42, 43, 44, 45, 46, 47, 47, 49, 50,
5951, 52, 53, 54, 55, 55, 57, 58, 59, 60, 61, 62, 63, 63, 65,
6066, 67, 68, 69, 70, 71, 71, 40, 20, 33, 41, 48, 53, 57, 61,
6164, 66, 69, 71, 73, 75, 76, 78, 80, 82, 85, 87, 89, 91, 92,
6294, 96, 98, 101, 103, 105, 107, 108, 110, 112, 114, 117, 119, 121, 123,
63124, 126, 128, 40, 23, 39, 51, 60, 67, 73, 79, 83, 87, 91, 94,
6497, 100, 102, 105, 107, 111, 115, 118, 121, 124, 126, 129, 131, 135, 139,
65142, 145, 148, 150, 153, 155, 159, 163, 166, 169, 172, 174, 177, 179, 35,
6628, 49, 65, 78, 89, 99, 107, 114, 120, 126, 132, 136, 141, 145, 149,
67153, 159, 165, 171, 176, 180, 185, 189, 192, 199, 205, 211, 216, 220, 225,
68229, 232, 239, 245, 251, 21, 33, 58, 79, 97, 112, 125, 137, 148, 157,
69166, 174, 182, 189, 195, 201, 207, 217, 227, 235, 243, 251, 17, 35, 63,
7086, 106, 123, 139, 152, 165, 177, 187, 197, 206, 214, 222, 230, 237, 250,
7125, 31, 55, 75, 91, 105, 117, 128, 138, 146, 154, 161, 168, 174, 180,
72185, 190, 200, 208, 215, 222, 229, 235, 240, 245, 255, 16, 36, 65, 89,
73110, 128, 144, 159, 173, 185, 196, 207, 217, 226, 234, 242, 250, 11, 41,
7474, 103, 128, 151, 172, 191, 209, 225, 241, 255, 9, 43, 79, 110, 138,
75163, 186, 207, 227, 246, 12, 39, 71, 99, 123, 144, 164, 182, 198, 214,
76228, 241, 253, 9, 44, 81, 113, 142, 168, 192, 214, 235, 255, 7, 49,
7790, 127, 160, 191, 220, 247, 6, 51, 95, 134, 170, 203, 234, 7, 47,
7887, 123, 155, 184, 212, 237, 6, 52, 97, 137, 174, 208, 240, 5, 57,
79106, 151, 192, 231, 5, 59, 111, 158, 202, 243, 5, 55, 103, 147, 187,
80224, 5, 60, 113, 161, 206, 248, 4, 65, 122, 175, 224, 4, 67, 127,
81182, 234, };
82static const unsigned char cache_caps50[168] = {
83224, 224, 224, 224, 224, 224, 224, 224, 160, 160, 160, 160, 185, 185, 185,
84178, 178, 168, 134, 61, 37, 224, 224, 224, 224, 224, 224, 224, 224, 240,
85240, 240, 240, 207, 207, 207, 198, 198, 183, 144, 66, 40, 160, 160, 160,
86160, 160, 160, 160, 160, 185, 185, 185, 185, 193, 193, 193, 183, 183, 172,
87138, 64, 38, 240, 240, 240, 240, 240, 240, 240, 240, 207, 207, 207, 207,
88204, 204, 204, 193, 193, 180, 143, 66, 40, 185, 185, 185, 185, 185, 185,
89185, 185, 193, 193, 193, 193, 193, 193, 193, 183, 183, 172, 138, 65, 39,
90207, 207, 207, 207, 207, 207, 207, 207, 204, 204, 204, 204, 201, 201, 201,
91188, 188, 176, 141, 66, 40, 193, 193, 193, 193, 193, 193, 193, 193, 193,
92193, 193, 193, 194, 194, 194, 184, 184, 173, 139, 65, 39, 204, 204, 204,
93204, 204, 204, 204, 204, 201, 201, 201, 201, 198, 198, 198, 187, 187, 175,
94140, 66, 40, };
95#endif
96
97#ifndef FFT_TWIDDLES48000_960
98#define FFT_TWIDDLES48000_960
99static const kiss_twiddle_cpx fft_twiddles48000_960[480] = {
100{32767, 0}, {32766, -429},
101{32757, -858}, {32743, -1287},
102{32724, -1715}, {32698, -2143},
103{32667, -2570}, {32631, -2998},
104{32588, -3425}, {32541, -3851},
105{32488, -4277}, {32429, -4701},
106{32364, -5125}, {32295, -5548},
107{32219, -5971}, {32138, -6393},
108{32051, -6813}, {31960, -7231},
109{31863, -7650}, {31760, -8067},
110{31652, -8481}, {31539, -8895},
111{31419, -9306}, {31294, -9716},
112{31165, -10126}, {31030, -10532},
113{30889, -10937}, {30743, -11340},
114{30592, -11741}, {30436, -12141},
115{30274, -12540}, {30107, -12935},
116{29936, -13328}, {29758, -13718},
117{29577, -14107}, {29390, -14493},
118{29197, -14875}, {29000, -15257},
119{28797, -15635}, {28590, -16010},
120{28379, -16384}, {28162, -16753},
121{27940, -17119}, {27714, -17484},
122{27482, -17845}, {27246, -18205},
123{27006, -18560}, {26760, -18911},
124{26510, -19260}, {26257, -19606},
125{25997, -19947}, {25734, -20286},
126{25466, -20621}, {25194, -20952},
127{24918, -21281}, {24637, -21605},
128{24353, -21926}, {24063, -22242},
129{23770, -22555}, {23473, -22865},
130{23171, -23171}, {22866, -23472},
131{22557, -23769}, {22244, -24063},
132{21927, -24352}, {21606, -24636},
133{21282, -24917}, {20954, -25194},
134{20622, -25465}, {20288, -25733},
135{19949, -25997}, {19607, -26255},
136{19261, -26509}, {18914, -26760},
137{18561, -27004}, {18205, -27246},
138{17846, -27481}, {17485, -27713},
139{17122, -27940}, {16755, -28162},
140{16385, -28378}, {16012, -28590},
141{15636, -28797}, {15258, -28999},
142{14878, -29197}, {14494, -29389},
143{14108, -29576}, {13720, -29757},
144{13329, -29934}, {12937, -30107},
145{12540, -30274}, {12142, -30435},
146{11744, -30592}, {11342, -30743},
147{10939, -30889}, {10534, -31030},
148{10127, -31164}, {9718, -31294},
149{9307, -31418}, {8895, -31537},
150{8482, -31652}, {8067, -31759},
151{7650, -31862}, {7233, -31960},
152{6815, -32051}, {6393, -32138},
153{5973, -32219}, {5549, -32294},
154{5127, -32364}, {4703, -32429},
155{4278, -32487}, {3852, -32541},
156{3426, -32588}, {2999, -32630},
157{2572, -32667}, {2144, -32698},
158{1716, -32724}, {1287, -32742},
159{860, -32757}, {430, -32766},
160{0, -32767}, {-429, -32766},
161{-858, -32757}, {-1287, -32743},
162{-1715, -32724}, {-2143, -32698},
163{-2570, -32667}, {-2998, -32631},
164{-3425, -32588}, {-3851, -32541},
165{-4277, -32488}, {-4701, -32429},
166{-5125, -32364}, {-5548, -32295},
167{-5971, -32219}, {-6393, -32138},
168{-6813, -32051}, {-7231, -31960},
169{-7650, -31863}, {-8067, -31760},
170{-8481, -31652}, {-8895, -31539},
171{-9306, -31419}, {-9716, -31294},
172{-10126, -31165}, {-10532, -31030},
173{-10937, -30889}, {-11340, -30743},
174{-11741, -30592}, {-12141, -30436},
175{-12540, -30274}, {-12935, -30107},
176{-13328, -29936}, {-13718, -29758},
177{-14107, -29577}, {-14493, -29390},
178{-14875, -29197}, {-15257, -29000},
179{-15635, -28797}, {-16010, -28590},
180{-16384, -28379}, {-16753, -28162},
181{-17119, -27940}, {-17484, -27714},
182{-17845, -27482}, {-18205, -27246},
183{-18560, -27006}, {-18911, -26760},
184{-19260, -26510}, {-19606, -26257},
185{-19947, -25997}, {-20286, -25734},
186{-20621, -25466}, {-20952, -25194},
187{-21281, -24918}, {-21605, -24637},
188{-21926, -24353}, {-22242, -24063},
189{-22555, -23770}, {-22865, -23473},
190{-23171, -23171}, {-23472, -22866},
191{-23769, -22557}, {-24063, -22244},
192{-24352, -21927}, {-24636, -21606},
193{-24917, -21282}, {-25194, -20954},
194{-25465, -20622}, {-25733, -20288},
195{-25997, -19949}, {-26255, -19607},
196{-26509, -19261}, {-26760, -18914},
197{-27004, -18561}, {-27246, -18205},
198{-27481, -17846}, {-27713, -17485},
199{-27940, -17122}, {-28162, -16755},
200{-28378, -16385}, {-28590, -16012},
201{-28797, -15636}, {-28999, -15258},
202{-29197, -14878}, {-29389, -14494},
203{-29576, -14108}, {-29757, -13720},
204{-29934, -13329}, {-30107, -12937},
205{-30274, -12540}, {-30435, -12142},
206{-30592, -11744}, {-30743, -11342},
207{-30889, -10939}, {-31030, -10534},
208{-31164, -10127}, {-31294, -9718},
209{-31418, -9307}, {-31537, -8895},
210{-31652, -8482}, {-31759, -8067},
211{-31862, -7650}, {-31960, -7233},
212{-32051, -6815}, {-32138, -6393},
213{-32219, -5973}, {-32294, -5549},
214{-32364, -5127}, {-32429, -4703},
215{-32487, -4278}, {-32541, -3852},
216{-32588, -3426}, {-32630, -2999},
217{-32667, -2572}, {-32698, -2144},
218{-32724, -1716}, {-32742, -1287},
219{-32757, -860}, {-32766, -430},
220{-32767, 0}, {-32766, 429},
221{-32757, 858}, {-32743, 1287},
222{-32724, 1715}, {-32698, 2143},
223{-32667, 2570}, {-32631, 2998},
224{-32588, 3425}, {-32541, 3851},
225{-32488, 4277}, {-32429, 4701},
226{-32364, 5125}, {-32295, 5548},
227{-32219, 5971}, {-32138, 6393},
228{-32051, 6813}, {-31960, 7231},
229{-31863, 7650}, {-31760, 8067},
230{-31652, 8481}, {-31539, 8895},
231{-31419, 9306}, {-31294, 9716},
232{-31165, 10126}, {-31030, 10532},
233{-30889, 10937}, {-30743, 11340},
234{-30592, 11741}, {-30436, 12141},
235{-30274, 12540}, {-30107, 12935},
236{-29936, 13328}, {-29758, 13718},
237{-29577, 14107}, {-29390, 14493},
238{-29197, 14875}, {-29000, 15257},
239{-28797, 15635}, {-28590, 16010},
240{-28379, 16384}, {-28162, 16753},
241{-27940, 17119}, {-27714, 17484},
242{-27482, 17845}, {-27246, 18205},
243{-27006, 18560}, {-26760, 18911},
244{-26510, 19260}, {-26257, 19606},
245{-25997, 19947}, {-25734, 20286},
246{-25466, 20621}, {-25194, 20952},
247{-24918, 21281}, {-24637, 21605},
248{-24353, 21926}, {-24063, 22242},
249{-23770, 22555}, {-23473, 22865},
250{-23171, 23171}, {-22866, 23472},
251{-22557, 23769}, {-22244, 24063},
252{-21927, 24352}, {-21606, 24636},
253{-21282, 24917}, {-20954, 25194},
254{-20622, 25465}, {-20288, 25733},
255{-19949, 25997}, {-19607, 26255},
256{-19261, 26509}, {-18914, 26760},
257{-18561, 27004}, {-18205, 27246},
258{-17846, 27481}, {-17485, 27713},
259{-17122, 27940}, {-16755, 28162},
260{-16385, 28378}, {-16012, 28590},
261{-15636, 28797}, {-15258, 28999},
262{-14878, 29197}, {-14494, 29389},
263{-14108, 29576}, {-13720, 29757},
264{-13329, 29934}, {-12937, 30107},
265{-12540, 30274}, {-12142, 30435},
266{-11744, 30592}, {-11342, 30743},
267{-10939, 30889}, {-10534, 31030},
268{-10127, 31164}, {-9718, 31294},
269{-9307, 31418}, {-8895, 31537},
270{-8482, 31652}, {-8067, 31759},
271{-7650, 31862}, {-7233, 31960},
272{-6815, 32051}, {-6393, 32138},
273{-5973, 32219}, {-5549, 32294},
274{-5127, 32364}, {-4703, 32429},
275{-4278, 32487}, {-3852, 32541},
276{-3426, 32588}, {-2999, 32630},
277{-2572, 32667}, {-2144, 32698},
278{-1716, 32724}, {-1287, 32742},
279{-860, 32757}, {-430, 32766},
280{0, 32767}, {429, 32766},
281{858, 32757}, {1287, 32743},
282{1715, 32724}, {2143, 32698},
283{2570, 32667}, {2998, 32631},
284{3425, 32588}, {3851, 32541},
285{4277, 32488}, {4701, 32429},
286{5125, 32364}, {5548, 32295},
287{5971, 32219}, {6393, 32138},
288{6813, 32051}, {7231, 31960},
289{7650, 31863}, {8067, 31760},
290{8481, 31652}, {8895, 31539},
291{9306, 31419}, {9716, 31294},
292{10126, 31165}, {10532, 31030},
293{10937, 30889}, {11340, 30743},
294{11741, 30592}, {12141, 30436},
295{12540, 30274}, {12935, 30107},
296{13328, 29936}, {13718, 29758},
297{14107, 29577}, {14493, 29390},
298{14875, 29197}, {15257, 29000},
299{15635, 28797}, {16010, 28590},
300{16384, 28379}, {16753, 28162},
301{17119, 27940}, {17484, 27714},
302{17845, 27482}, {18205, 27246},
303{18560, 27006}, {18911, 26760},
304{19260, 26510}, {19606, 26257},
305{19947, 25997}, {20286, 25734},
306{20621, 25466}, {20952, 25194},
307{21281, 24918}, {21605, 24637},
308{21926, 24353}, {22242, 24063},
309{22555, 23770}, {22865, 23473},
310{23171, 23171}, {23472, 22866},
311{23769, 22557}, {24063, 22244},
312{24352, 21927}, {24636, 21606},
313{24917, 21282}, {25194, 20954},
314{25465, 20622}, {25733, 20288},
315{25997, 19949}, {26255, 19607},
316{26509, 19261}, {26760, 18914},
317{27004, 18561}, {27246, 18205},
318{27481, 17846}, {27713, 17485},
319{27940, 17122}, {28162, 16755},
320{28378, 16385}, {28590, 16012},
321{28797, 15636}, {28999, 15258},
322{29197, 14878}, {29389, 14494},
323{29576, 14108}, {29757, 13720},
324{29934, 13329}, {30107, 12937},
325{30274, 12540}, {30435, 12142},
326{30592, 11744}, {30743, 11342},
327{30889, 10939}, {31030, 10534},
328{31164, 10127}, {31294, 9718},
329{31418, 9307}, {31537, 8895},
330{31652, 8482}, {31759, 8067},
331{31862, 7650}, {31960, 7233},
332{32051, 6815}, {32138, 6393},
333{32219, 5973}, {32294, 5549},
334{32364, 5127}, {32429, 4703},
335{32487, 4278}, {32541, 3852},
336{32588, 3426}, {32630, 2999},
337{32667, 2572}, {32698, 2144},
338{32724, 1716}, {32742, 1287},
339{32757, 860}, {32766, 430},
340};
341#ifndef FFT_BITREV480
342#define FFT_BITREV480
343static const opus_int16 fft_bitrev480[480] = {
3440, 120, 240, 360, 30, 150, 270, 390, 60, 180, 300, 420, 90, 210, 330,
345450, 15, 135, 255, 375, 45, 165, 285, 405, 75, 195, 315, 435, 105, 225,
346345, 465, 5, 125, 245, 365, 35, 155, 275, 395, 65, 185, 305, 425, 95,
347215, 335, 455, 20, 140, 260, 380, 50, 170, 290, 410, 80, 200, 320, 440,
348110, 230, 350, 470, 10, 130, 250, 370, 40, 160, 280, 400, 70, 190, 310,
349430, 100, 220, 340, 460, 25, 145, 265, 385, 55, 175, 295, 415, 85, 205,
350325, 445, 115, 235, 355, 475, 1, 121, 241, 361, 31, 151, 271, 391, 61,
351181, 301, 421, 91, 211, 331, 451, 16, 136, 256, 376, 46, 166, 286, 406,
35276, 196, 316, 436, 106, 226, 346, 466, 6, 126, 246, 366, 36, 156, 276,
353396, 66, 186, 306, 426, 96, 216, 336, 456, 21, 141, 261, 381, 51, 171,
354291, 411, 81, 201, 321, 441, 111, 231, 351, 471, 11, 131, 251, 371, 41,
355161, 281, 401, 71, 191, 311, 431, 101, 221, 341, 461, 26, 146, 266, 386,
35656, 176, 296, 416, 86, 206, 326, 446, 116, 236, 356, 476, 2, 122, 242,
357362, 32, 152, 272, 392, 62, 182, 302, 422, 92, 212, 332, 452, 17, 137,
358257, 377, 47, 167, 287, 407, 77, 197, 317, 437, 107, 227, 347, 467, 7,
359127, 247, 367, 37, 157, 277, 397, 67, 187, 307, 427, 97, 217, 337, 457,
36022, 142, 262, 382, 52, 172, 292, 412, 82, 202, 322, 442, 112, 232, 352,
361472, 12, 132, 252, 372, 42, 162, 282, 402, 72, 192, 312, 432, 102, 222,
362342, 462, 27, 147, 267, 387, 57, 177, 297, 417, 87, 207, 327, 447, 117,
363237, 357, 477, 3, 123, 243, 363, 33, 153, 273, 393, 63, 183, 303, 423,
36493, 213, 333, 453, 18, 138, 258, 378, 48, 168, 288, 408, 78, 198, 318,
365438, 108, 228, 348, 468, 8, 128, 248, 368, 38, 158, 278, 398, 68, 188,
366308, 428, 98, 218, 338, 458, 23, 143, 263, 383, 53, 173, 293, 413, 83,
367203, 323, 443, 113, 233, 353, 473, 13, 133, 253, 373, 43, 163, 283, 403,
36873, 193, 313, 433, 103, 223, 343, 463, 28, 148, 268, 388, 58, 178, 298,
369418, 88, 208, 328, 448, 118, 238, 358, 478, 4, 124, 244, 364, 34, 154,
370274, 394, 64, 184, 304, 424, 94, 214, 334, 454, 19, 139, 259, 379, 49,
371169, 289, 409, 79, 199, 319, 439, 109, 229, 349, 469, 9, 129, 249, 369,
37239, 159, 279, 399, 69, 189, 309, 429, 99, 219, 339, 459, 24, 144, 264,
373384, 54, 174, 294, 414, 84, 204, 324, 444, 114, 234, 354, 474, 14, 134,
374254, 374, 44, 164, 284, 404, 74, 194, 314, 434, 104, 224, 344, 464, 29,
375149, 269, 389, 59, 179, 299, 419, 89, 209, 329, 449, 119, 239, 359, 479,
376};
377#endif
378
379#ifndef FFT_BITREV240
380#define FFT_BITREV240
381static const opus_int16 fft_bitrev240[240] = {
3820, 60, 120, 180, 15, 75, 135, 195, 30, 90, 150, 210, 45, 105, 165,
383225, 5, 65, 125, 185, 20, 80, 140, 200, 35, 95, 155, 215, 50, 110,
384170, 230, 10, 70, 130, 190, 25, 85, 145, 205, 40, 100, 160, 220, 55,
385115, 175, 235, 1, 61, 121, 181, 16, 76, 136, 196, 31, 91, 151, 211,
38646, 106, 166, 226, 6, 66, 126, 186, 21, 81, 141, 201, 36, 96, 156,
387216, 51, 111, 171, 231, 11, 71, 131, 191, 26, 86, 146, 206, 41, 101,
388161, 221, 56, 116, 176, 236, 2, 62, 122, 182, 17, 77, 137, 197, 32,
38992, 152, 212, 47, 107, 167, 227, 7, 67, 127, 187, 22, 82, 142, 202,
39037, 97, 157, 217, 52, 112, 172, 232, 12, 72, 132, 192, 27, 87, 147,
391207, 42, 102, 162, 222, 57, 117, 177, 237, 3, 63, 123, 183, 18, 78,
392138, 198, 33, 93, 153, 213, 48, 108, 168, 228, 8, 68, 128, 188, 23,
39383, 143, 203, 38, 98, 158, 218, 53, 113, 173, 233, 13, 73, 133, 193,
39428, 88, 148, 208, 43, 103, 163, 223, 58, 118, 178, 238, 4, 64, 124,
395184, 19, 79, 139, 199, 34, 94, 154, 214, 49, 109, 169, 229, 9, 69,
396129, 189, 24, 84, 144, 204, 39, 99, 159, 219, 54, 114, 174, 234, 14,
39774, 134, 194, 29, 89, 149, 209, 44, 104, 164, 224, 59, 119, 179, 239,
398};
399#endif
400
401#ifndef FFT_BITREV120
402#define FFT_BITREV120
403static const opus_int16 fft_bitrev120[120] = {
4040, 30, 60, 90, 15, 45, 75, 105, 5, 35, 65, 95, 20, 50, 80,
405110, 10, 40, 70, 100, 25, 55, 85, 115, 1, 31, 61, 91, 16, 46,
40676, 106, 6, 36, 66, 96, 21, 51, 81, 111, 11, 41, 71, 101, 26,
40756, 86, 116, 2, 32, 62, 92, 17, 47, 77, 107, 7, 37, 67, 97,
40822, 52, 82, 112, 12, 42, 72, 102, 27, 57, 87, 117, 3, 33, 63,
40993, 18, 48, 78, 108, 8, 38, 68, 98, 23, 53, 83, 113, 13, 43,
41073, 103, 28, 58, 88, 118, 4, 34, 64, 94, 19, 49, 79, 109, 9,
41139, 69, 99, 24, 54, 84, 114, 14, 44, 74, 104, 29, 59, 89, 119,
412};
413#endif
414
415#ifndef FFT_BITREV60
416#define FFT_BITREV60
417static const opus_int16 fft_bitrev60[60] = {
4180, 15, 30, 45, 5, 20, 35, 50, 10, 25, 40, 55, 1, 16, 31,
41946, 6, 21, 36, 51, 11, 26, 41, 56, 2, 17, 32, 47, 7, 22,
42037, 52, 12, 27, 42, 57, 3, 18, 33, 48, 8, 23, 38, 53, 13,
42128, 43, 58, 4, 19, 34, 49, 9, 24, 39, 54, 14, 29, 44, 59,
422};
423#endif
424
425#ifndef FFT_STATE48000_960_0
426#define FFT_STATE48000_960_0
427static const kiss_fft_state fft_state48000_960_0 = {
428480, /* nfft */
429-1, /* shift */
430{4, 120, 4, 30, 2, 15, 3, 5, 5, 1, 0, 0, 0, 0, 0, 0, }, /* factors */
431fft_bitrev480, /* bitrev */
432fft_twiddles48000_960, /* bitrev */
433};
434#endif
435
436#ifndef FFT_STATE48000_960_1
437#define FFT_STATE48000_960_1
438static const kiss_fft_state fft_state48000_960_1 = {
439240, /* nfft */
4401, /* shift */
441{4, 60, 4, 15, 3, 5, 5, 1, 0, 0, 0, 0, 0, 0, 0, 0, }, /* factors */
442fft_bitrev240, /* bitrev */
443fft_twiddles48000_960, /* bitrev */
444};
445#endif
446
447#ifndef FFT_STATE48000_960_2
448#define FFT_STATE48000_960_2
449static const kiss_fft_state fft_state48000_960_2 = {
450120, /* nfft */
4512, /* shift */
452{4, 30, 2, 15, 3, 5, 5, 1, 0, 0, 0, 0, 0, 0, 0, 0, }, /* factors */
453fft_bitrev120, /* bitrev */
454fft_twiddles48000_960, /* bitrev */
455};
456#endif
457
458#ifndef FFT_STATE48000_960_3
459#define FFT_STATE48000_960_3
460static const kiss_fft_state fft_state48000_960_3 = {
46160, /* nfft */
4623, /* shift */
463{4, 15, 3, 5, 5, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, /* factors */
464fft_bitrev60, /* bitrev */
465fft_twiddles48000_960, /* bitrev */
466};
467#endif
468
469#endif
470
471#ifndef MDCT_TWIDDLES960
472#define MDCT_TWIDDLES960
473static const opus_val16 mdct_twiddles960[481] = {
47432767, 32767, 32767, 32767, 32766,
47532763, 32762, 32759, 32757, 32753,
47632751, 32747, 32743, 32738, 32733,
47732729, 32724, 32717, 32711, 32705,
47832698, 32690, 32683, 32676, 32667,
47932658, 32650, 32640, 32631, 32620,
48032610, 32599, 32588, 32577, 32566,
48132554, 32541, 32528, 32515, 32502,
48232487, 32474, 32459, 32444, 32429,
48332413, 32397, 32381, 32364, 32348,
48432331, 32313, 32294, 32277, 32257,
48532239, 32219, 32200, 32180, 32159,
48632138, 32118, 32096, 32074, 32051,
48732029, 32006, 31984, 31960, 31936,
48831912, 31888, 31863, 31837, 31812,
48931786, 31760, 31734, 31707, 31679,
49031652, 31624, 31596, 31567, 31539,
49131508, 31479, 31450, 31419, 31388,
49231357, 31326, 31294, 31262, 31230,
49331198, 31164, 31131, 31097, 31063,
49431030, 30994, 30959, 30924, 30889,
49530853, 30816, 30779, 30743, 30705,
49630668, 30629, 30592, 30553, 30515,
49730475, 30435, 30396, 30356, 30315,
49830274, 30233, 30191, 30149, 30107,
49930065, 30022, 29979, 29936, 29891,
50029847, 29803, 29758, 29713, 29668,
50129622, 29577, 29529, 29483, 29436,
50229390, 29341, 29293, 29246, 29197,
50329148, 29098, 29050, 29000, 28949,
50428899, 28848, 28797, 28746, 28694,
50528642, 28590, 28537, 28485, 28432,
50628378, 28324, 28271, 28217, 28162,
50728106, 28051, 27995, 27940, 27884,
50827827, 27770, 27713, 27657, 27598,
50927540, 27481, 27423, 27365, 27305,
51027246, 27187, 27126, 27066, 27006,
51126945, 26883, 26822, 26760, 26698,
51226636, 26574, 26510, 26448, 26383,
51326320, 26257, 26191, 26127, 26062,
51425997, 25931, 25866, 25800, 25734,
51525667, 25601, 25533, 25466, 25398,
51625330, 25262, 25194, 25125, 25056,
51724987, 24917, 24848, 24778, 24707,
51824636, 24566, 24495, 24424, 24352,
51924280, 24208, 24135, 24063, 23990,
52023917, 23842, 23769, 23695, 23622,
52123546, 23472, 23398, 23322, 23246,
52223171, 23095, 23018, 22942, 22866,
52322788, 22711, 22634, 22557, 22478,
52422400, 22322, 22244, 22165, 22085,
52522006, 21927, 21846, 21766, 21687,
52621606, 21524, 21443, 21363, 21282,
52721199, 21118, 21035, 20954, 20870,
52820788, 20705, 20621, 20538, 20455,
52920371, 20286, 20202, 20118, 20034,
53019947, 19863, 19777, 19692, 19606,
53119520, 19434, 19347, 19260, 19174,
53219088, 18999, 18911, 18825, 18737,
53318648, 18560, 18472, 18384, 18294,
53418205, 18116, 18025, 17936, 17846,
53517757, 17666, 17576, 17485, 17395,
53617303, 17212, 17122, 17030, 16937,
53716846, 16755, 16662, 16569, 16477,
53816385, 16291, 16198, 16105, 16012,
53915917, 15824, 15730, 15636, 15541,
54015447, 15352, 15257, 15162, 15067,
54114973, 14875, 14781, 14685, 14589,
54214493, 14396, 14300, 14204, 14107,
54314010, 13914, 13815, 13718, 13621,
54413524, 13425, 13328, 13230, 13133,
54513033, 12935, 12836, 12738, 12638,
54612540, 12441, 12341, 12241, 12142,
54712044, 11943, 11843, 11744, 11643,
54811542, 11442, 11342, 11241, 11139,
54911039, 10939, 10836, 10736, 10635,
55010534, 10431, 10330, 10228, 10127,
55110024, 9921, 9820, 9718, 9614,
5529512, 9410, 9306, 9204, 9101,
5538998, 8895, 8791, 8689, 8585,
5548481, 8377, 8274, 8171, 8067,
5557962, 7858, 7753, 7650, 7545,
5567441, 7336, 7231, 7129, 7023,
5576917, 6813, 6709, 6604, 6498,
5586393, 6288, 6182, 6077, 5973,
5595867, 5760, 5656, 5549, 5445,
5605339, 5232, 5127, 5022, 4914,
5614809, 4703, 4596, 4490, 4384,
5624278, 4171, 4065, 3958, 3852,
5633745, 3640, 3532, 3426, 3318,
5643212, 3106, 2998, 2891, 2786,
5652679, 2570, 2465, 2358, 2251,
5662143, 2037, 1929, 1823, 1715,
5671609, 1501, 1393, 1287, 1180,
5681073, 964, 858, 751, 644,
569535, 429, 322, 214, 107,
5700, };
571#endif
572
573static const CELTMode mode48000_960_120 = {
57448000, /* Fs */
575120, /* overlap */
57621, /* nbEBands */
57721, /* effEBands */
578{27853, 0, 4096, 8192, }, /* preemph */
579eband5ms, /* eBands */
5803, /* maxLM */
5818, /* nbShortMdcts */
582120, /* shortMdctSize */
58311, /* nbAllocVectors */
584band_allocation, /* allocVectors */
585logN400, /* logN */
586window120, /* window */
587{1920, 3, {&fft_state48000_960_0, &fft_state48000_960_1, &fft_state48000_960_2, &fft_state48000_960_3, }, mdct_twiddles960}, /* mdct */
588{392, cache_index50, cache_bits50, cache_caps50}, /* cache */
589};
590
591/* List of all the available modes */
592#define TOTAL_MODES 1
593static const CELTMode * const static_mode_list[TOTAL_MODES] = {
594&mode48000_960_120,
595};
diff --git a/lib/rbcodec/codecs/libopus/celt/vq.c b/lib/rbcodec/codecs/libopus/celt/vq.c
new file mode 100644
index 0000000000..6a00edf9cd
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/vq.c
@@ -0,0 +1,415 @@
1/* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2009 Xiph.Org Foundation
3 Written by Jean-Marc Valin */
4/*
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
12 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
20 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28
29#ifdef HAVE_CONFIG_H
30#include "opus_config.h"
31#endif
32
33#include "mathops.h"
34#include "cwrs.h"
35#include "vq.h"
36#include "arch.h"
37#include "os_support.h"
38#include "bands.h"
39#include "rate.h"
40
41static void exp_rotation1(celt_norm *X, int len, int stride, opus_val16 c, opus_val16 s)
42{
43 int i;
44 celt_norm *Xptr;
45 Xptr = X;
46 for (i=0;i<len-stride;i++)
47 {
48 celt_norm x1, x2;
49 x1 = Xptr[0];
50 x2 = Xptr[stride];
51 Xptr[stride] = EXTRACT16(SHR32(MULT16_16(c,x2) + MULT16_16(s,x1), 15));
52 *Xptr++ = EXTRACT16(SHR32(MULT16_16(c,x1) - MULT16_16(s,x2), 15));
53 }
54 Xptr = &X[len-2*stride-1];
55 for (i=len-2*stride-1;i>=0;i--)
56 {
57 celt_norm x1, x2;
58 x1 = Xptr[0];
59 x2 = Xptr[stride];
60 Xptr[stride] = EXTRACT16(SHR32(MULT16_16(c,x2) + MULT16_16(s,x1), 15));
61 *Xptr-- = EXTRACT16(SHR32(MULT16_16(c,x1) - MULT16_16(s,x2), 15));
62 }
63}
64
65static void exp_rotation(celt_norm *X, int len, int dir, int stride, int K, int spread)
66{
67 static const int SPREAD_FACTOR[3]={15,10,5};
68 int i;
69 opus_val16 c, s;
70 opus_val16 gain, theta;
71 int stride2=0;
72 int factor;
73
74 if (2*K>=len || spread==SPREAD_NONE)
75 return;
76 factor = SPREAD_FACTOR[spread-1];
77
78 gain = celt_div((opus_val32)MULT16_16(Q15_ONE,len),(opus_val32)(len+factor*K));
79 theta = HALF16(MULT16_16_Q15(gain,gain));
80
81 c = celt_cos_norm(EXTEND32(theta));
82 s = celt_cos_norm(EXTEND32(SUB16(Q15ONE,theta))); /* sin(theta) */
83
84 if (len>=8*stride)
85 {
86 stride2 = 1;
87 /* This is just a simple (equivalent) way of computing sqrt(len/stride) with rounding.
88 It's basically incrementing long as (stride2+0.5)^2 < len/stride. */
89 while ((stride2*stride2+stride2)*stride + (stride>>2) < len)
90 stride2++;
91 }
92 /*NOTE: As a minor optimization, we could be passing around log2(B), not B, for both this and for
93 extract_collapse_mask().*/
94 len /= stride;
95 for (i=0;i<stride;i++)
96 {
97 if (dir < 0)
98 {
99 if (stride2)
100 exp_rotation1(X+i*len, len, stride2, s, c);
101 exp_rotation1(X+i*len, len, 1, c, s);
102 } else {
103 exp_rotation1(X+i*len, len, 1, c, -s);
104 if (stride2)
105 exp_rotation1(X+i*len, len, stride2, s, -c);
106 }
107 }
108}
109
110/** Takes the pitch vector and the decoded residual vector, computes the gain
111 that will give ||p+g*y||=1 and mixes the residual with the pitch. */
112static void normalise_residual(int * OPUS_RESTRICT iy, celt_norm * OPUS_RESTRICT X,
113 int N, opus_val32 Ryy, opus_val16 gain)
114{
115 int i;
116#ifdef FIXED_POINT
117 int k;
118#endif
119 opus_val32 t;
120 opus_val16 g;
121
122#ifdef FIXED_POINT
123 k = celt_ilog2(Ryy)>>1;
124#endif
125 t = VSHR32(Ryy, 2*(k-7));
126 g = MULT16_16_P15(celt_rsqrt_norm(t),gain);
127
128 i=0;
129 do
130 X[i] = EXTRACT16(PSHR32(MULT16_16(g, iy[i]), k+1));
131 while (++i < N);
132}
133
134static unsigned extract_collapse_mask(int *iy, int N, int B)
135{
136 unsigned collapse_mask;
137 int N0;
138 int i;
139 if (B<=1)
140 return 1;
141 /*NOTE: As a minor optimization, we could be passing around log2(B), not B, for both this and for
142 exp_rotation().*/
143 N0 = N/B;
144 collapse_mask = 0;
145 i=0; do {
146 int j;
147 j=0; do {
148 collapse_mask |= (iy[i*N0+j]!=0)<<i;
149 } while (++j<N0);
150 } while (++i<B);
151 return collapse_mask;
152}
153
154unsigned alg_quant(celt_norm *X, int N, int K, int spread, int B, ec_enc *enc
155#ifdef RESYNTH
156 , opus_val16 gain
157#endif
158 )
159{
160 VARDECL(celt_norm, y);
161 VARDECL(int, iy);
162 VARDECL(opus_val16, signx);
163 int i, j;
164 opus_val16 s;
165 int pulsesLeft;
166 opus_val32 sum;
167 opus_val32 xy;
168 opus_val16 yy;
169 unsigned collapse_mask;
170 SAVE_STACK;
171
172 celt_assert2(K>0, "alg_quant() needs at least one pulse");
173 celt_assert2(N>1, "alg_quant() needs at least two dimensions");
174
175 ALLOC(y, N, celt_norm);
176 ALLOC(iy, N, int);
177 ALLOC(signx, N, opus_val16);
178
179 exp_rotation(X, N, 1, B, K, spread);
180
181 /* Get rid of the sign */
182 sum = 0;
183 j=0; do {
184 if (X[j]>0)
185 signx[j]=1;
186 else {
187 signx[j]=-1;
188 X[j]=-X[j];
189 }
190 iy[j] = 0;
191 y[j] = 0;
192 } while (++j<N);
193
194 xy = yy = 0;
195
196 pulsesLeft = K;
197
198 /* Do a pre-search by projecting on the pyramid */
199 if (K > (N>>1))
200 {
201 opus_val16 rcp;
202 j=0; do {
203 sum += X[j];
204 } while (++j<N);
205
206 /* If X is too small, just replace it with a pulse at 0 */
207#ifdef FIXED_POINT
208 if (sum <= K)
209#else
210 /* Prevents infinities and NaNs from causing too many pulses
211 to be allocated. 64 is an approximation of infinity here. */
212 if (!(sum > EPSILON && sum < 64))
213#endif
214 {
215 X[0] = QCONST16(1.f,14);
216 j=1; do
217 X[j]=0;
218 while (++j<N);
219 sum = QCONST16(1.f,14);
220 }
221 rcp = EXTRACT16(MULT16_32_Q16(K-1, celt_rcp(sum)));
222 j=0; do {
223#ifdef FIXED_POINT
224 /* It's really important to round *towards zero* here */
225 iy[j] = MULT16_16_Q15(X[j],rcp);
226#else
227 iy[j] = (int)floor(rcp*X[j]);
228#endif
229 y[j] = (celt_norm)iy[j];
230 yy = MAC16_16(yy, y[j],y[j]);
231 xy = MAC16_16(xy, X[j],y[j]);
232 y[j] *= 2;
233 pulsesLeft -= iy[j];
234 } while (++j<N);
235 }
236 celt_assert2(pulsesLeft>=1, "Allocated too many pulses in the quick pass");
237
238 /* This should never happen, but just in case it does (e.g. on silence)
239 we fill the first bin with pulses. */
240#ifdef FIXED_POINT_DEBUG
241 celt_assert2(pulsesLeft<=N+3, "Not enough pulses in the quick pass");
242#endif
243 if (pulsesLeft > N+3)
244 {
245 opus_val16 tmp = (opus_val16)pulsesLeft;
246 yy = MAC16_16(yy, tmp, tmp);
247 yy = MAC16_16(yy, tmp, y[0]);
248 iy[0] += pulsesLeft;
249 pulsesLeft=0;
250 }
251
252 s = 1;
253 for (i=0;i<pulsesLeft;i++)
254 {
255 int best_id;
256 opus_val32 best_num = -VERY_LARGE16;
257 opus_val16 best_den = 0;
258#ifdef FIXED_POINT
259 int rshift;
260#endif
261#ifdef FIXED_POINT
262 rshift = 1+celt_ilog2(K-pulsesLeft+i+1);
263#endif
264 best_id = 0;
265 /* The squared magnitude term gets added anyway, so we might as well
266 add it outside the loop */
267 yy = ADD32(yy, 1);
268 j=0;
269 do {
270 opus_val16 Rxy, Ryy;
271 /* Temporary sums of the new pulse(s) */
272 Rxy = EXTRACT16(SHR32(ADD32(xy, EXTEND32(X[j])),rshift));
273 /* We're multiplying y[j] by two so we don't have to do it here */
274 Ryy = ADD16(yy, y[j]);
275
276 /* Approximate score: we maximise Rxy/sqrt(Ryy) (we're guaranteed that
277 Rxy is positive because the sign is pre-computed) */
278 Rxy = MULT16_16_Q15(Rxy,Rxy);
279 /* The idea is to check for num/den >= best_num/best_den, but that way
280 we can do it without any division */
281 /* OPT: Make sure to use conditional moves here */
282 if (MULT16_16(best_den, Rxy) > MULT16_16(Ryy, best_num))
283 {
284 best_den = Ryy;
285 best_num = Rxy;
286 best_id = j;
287 }
288 } while (++j<N);
289
290 /* Updating the sums of the new pulse(s) */
291 xy = ADD32(xy, EXTEND32(X[best_id]));
292 /* We're multiplying y[j] by two so we don't have to do it here */
293 yy = ADD16(yy, y[best_id]);
294
295 /* Only now that we've made the final choice, update y/iy */
296 /* Multiplying y[j] by 2 so we don't have to do it everywhere else */
297 y[best_id] += 2*s;
298 iy[best_id]++;
299 }
300
301 /* Put the original sign back */
302 j=0;
303 do {
304 X[j] = MULT16_16(signx[j],X[j]);
305 if (signx[j] < 0)
306 iy[j] = -iy[j];
307 } while (++j<N);
308 encode_pulses(iy, N, K, enc);
309
310#ifdef RESYNTH
311 normalise_residual(iy, X, N, yy, gain);
312 exp_rotation(X, N, -1, B, K, spread);
313#endif
314
315 collapse_mask = extract_collapse_mask(iy, N, B);
316 RESTORE_STACK;
317 return collapse_mask;
318}
319
320/** Decode pulse vector and combine the result with the pitch vector to produce
321 the final normalised signal in the current band. */
322unsigned alg_unquant(celt_norm *X, int N, int K, int spread, int B,
323 ec_dec *dec, opus_val16 gain)
324{
325 int i;
326 opus_val32 Ryy;
327 unsigned collapse_mask;
328 VARDECL(int, iy);
329 SAVE_STACK;
330
331 celt_assert2(K>0, "alg_unquant() needs at least one pulse");
332 celt_assert2(N>1, "alg_unquant() needs at least two dimensions");
333 ALLOC(iy, N, int);
334 decode_pulses(iy, N, K, dec);
335 Ryy = 0;
336 i=0;
337 do {
338 Ryy = MAC16_16(Ryy, iy[i], iy[i]);
339 } while (++i < N);
340 normalise_residual(iy, X, N, Ryy, gain);
341 exp_rotation(X, N, -1, B, K, spread);
342 collapse_mask = extract_collapse_mask(iy, N, B);
343 RESTORE_STACK;
344 return collapse_mask;
345}
346
347void renormalise_vector(celt_norm *X, int N, opus_val16 gain)
348{
349 int i;
350#ifdef FIXED_POINT
351 int k;
352#endif
353 opus_val32 E = EPSILON;
354 opus_val16 g;
355 opus_val32 t;
356 celt_norm *xptr = X;
357 for (i=0;i<N;i++)
358 {
359 E = MAC16_16(E, *xptr, *xptr);
360 xptr++;
361 }
362#ifdef FIXED_POINT
363 k = celt_ilog2(E)>>1;
364#endif
365 t = VSHR32(E, 2*(k-7));
366 g = MULT16_16_P15(celt_rsqrt_norm(t),gain);
367
368 xptr = X;
369 for (i=0;i<N;i++)
370 {
371 *xptr = EXTRACT16(PSHR32(MULT16_16(g, *xptr), k+1));
372 xptr++;
373 }
374 /*return celt_sqrt(E);*/
375}
376
377int stereo_itheta(celt_norm *X, celt_norm *Y, int stereo, int N)
378{
379 int i;
380 int itheta;
381 opus_val16 mid, side;
382 opus_val32 Emid, Eside;
383
384 Emid = Eside = EPSILON;
385 if (stereo)
386 {
387 for (i=0;i<N;i++)
388 {
389 celt_norm m, s;
390 m = ADD16(SHR16(X[i],1),SHR16(Y[i],1));
391 s = SUB16(SHR16(X[i],1),SHR16(Y[i],1));
392 Emid = MAC16_16(Emid, m, m);
393 Eside = MAC16_16(Eside, s, s);
394 }
395 } else {
396 for (i=0;i<N;i++)
397 {
398 celt_norm m, s;
399 m = X[i];
400 s = Y[i];
401 Emid = MAC16_16(Emid, m, m);
402 Eside = MAC16_16(Eside, s, s);
403 }
404 }
405 mid = celt_sqrt(Emid);
406 side = celt_sqrt(Eside);
407#ifdef FIXED_POINT
408 /* 0.63662 = 2/pi */
409 itheta = MULT16_16_Q15(QCONST16(0.63662f,15),celt_atan2p(side, mid));
410#else
411 itheta = (int)floor(.5f+16384*0.63662f*atan2(side,mid));
412#endif
413
414 return itheta;
415}
diff --git a/lib/rbcodec/codecs/libopus/celt/vq.h b/lib/rbcodec/codecs/libopus/celt/vq.h
new file mode 100644
index 0000000000..1ceeeeb268
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/vq.h
@@ -0,0 +1,73 @@
1/* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2009 Xiph.Org Foundation
3 Written by Jean-Marc Valin */
4/**
5 @file vq.h
6 @brief Vector quantisation of the residual
7 */
8/*
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions
11 are met:
12
13 - Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
15
16 - Redistributions in binary form must reproduce the above copyright
17 notice, this list of conditions and the following disclaimer in the
18 documentation and/or other materials provided with the distribution.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
24 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31*/
32
33#ifndef VQ_H
34#define VQ_H
35
36#include "entenc.h"
37#include "entdec.h"
38#include "modes.h"
39
40/** Algebraic pulse-vector quantiser. The signal x is replaced by the sum of
41 * the pitch and a combination of pulses such that its norm is still equal
42 * to 1. This is the function that will typically require the most CPU.
43 * @param x Residual signal to quantise/encode (returns quantised version)
44 * @param W Perceptual weight to use when optimising (currently unused)
45 * @param N Number of samples to encode
46 * @param K Number of pulses to use
47 * @param p Pitch vector (it is assumed that p+x is a unit vector)
48 * @param enc Entropy encoder state
49 * @ret A mask indicating which blocks in the band received pulses
50*/
51unsigned alg_quant(celt_norm *X, int N, int K, int spread, int B,
52 ec_enc *enc
53#ifdef RESYNTH
54 , opus_val16 gain
55#endif
56 );
57
58/** Algebraic pulse decoder
59 * @param x Decoded normalised spectrum (returned)
60 * @param N Number of samples to decode
61 * @param K Number of pulses to use
62 * @param p Pitch vector (automatically added to x)
63 * @param dec Entropy decoder state
64 * @ret A mask indicating which blocks in the band received pulses
65 */
66unsigned alg_unquant(celt_norm *X, int N, int K, int spread, int B,
67 ec_dec *dec, opus_val16 gain);
68
69void renormalise_vector(celt_norm *X, int N, opus_val16 gain);
70
71int stereo_itheta(celt_norm *X, celt_norm *Y, int stereo, int N);
72
73#endif /* VQ_H */