1 /*
2  *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 /******************************************************************
12 
13  iLBC Speech Coder ANSI-C Source Code
14 
15  define.h
16 
17 ******************************************************************/
18 #ifndef MODULES_AUDIO_CODING_CODECS_ILBC_MAIN_SOURCE_DEFINES_H_
19 #define MODULES_AUDIO_CODING_CODECS_ILBC_MAIN_SOURCE_DEFINES_H_
20 
21 #include <stdint.h>
22 #include <string.h>
23 
24 #include "common_audio/signal_processing/include/signal_processing_library.h"
25 
26 /* general codec settings */
27 
28 #define FS 8000
29 #define BLOCKL_20MS 160
30 #define BLOCKL_30MS 240
31 #define BLOCKL_MAX 240
32 #define NSUB_20MS 4
33 #define NSUB_30MS 6
34 #define NSUB_MAX 6
35 #define NASUB_20MS 2
36 #define NASUB_30MS 4
37 #define NASUB_MAX 4
38 #define SUBL 40
39 #define STATE_LEN 80
40 #define STATE_SHORT_LEN_30MS 58
41 #define STATE_SHORT_LEN_20MS 57
42 
43 /* LPC settings */
44 
45 #define LPC_FILTERORDER 10
46 #define LPC_LOOKBACK 60
47 #define LPC_N_20MS 1
48 #define LPC_N_30MS 2
49 #define LPC_N_MAX 2
50 #define LPC_ASYMDIFF 20
51 #define LSF_NSPLIT 3
52 #define LSF_NUMBER_OF_STEPS 4
53 #define LPC_HALFORDER 5
54 #define COS_GRID_POINTS 60
55 
56 /* cb settings */
57 
58 #define CB_NSTAGES 3
59 #define CB_EXPAND 2
60 #define CB_MEML 147
61 #define CB_FILTERLEN (2 * 4)
62 #define CB_HALFFILTERLEN 4
63 #define CB_RESRANGE 34
64 #define CB_MAXGAIN_FIXQ6 83 /* error = -0.24% */
65 #define CB_MAXGAIN_FIXQ14 21299
66 
67 /* enhancer */
68 
69 #define ENH_BLOCKL 80 /* block length */
70 #define ENH_BLOCKL_HALF (ENH_BLOCKL / 2)
71 #define ENH_HL                                                         \
72   3 /* 2*ENH_HL+1 is number blocks                                     \
73                                                         in said second \
74        sequence */
75 #define ENH_SLOP                    \
76   2 /* max difference estimated and \
77                                                        correct pitch period */
78 #define ENH_PLOCSL                                                          \
79   8 /* pitch-estimates and                                                  \
80                                                      pitch-locations buffer \
81        length */
82 #define ENH_OVERHANG 2
83 #define ENH_UPS0 4 /* upsampling rate */
84 #define ENH_FL0 3  /* 2*FLO+1 is the length of each filter */
85 #define ENH_FLO_MULT2_PLUS1 7
86 #define ENH_VECTL (ENH_BLOCKL + 2 * ENH_FL0)
87 #define ENH_CORRDIM (2 * ENH_SLOP + 1)
88 #define ENH_NBLOCKS (BLOCKL / ENH_BLOCKL)
89 #define ENH_NBLOCKS_EXTRA 5
90 #define ENH_NBLOCKS_TOT 8 /* ENH_NBLOCKS+ENH_NBLOCKS_EXTRA */
91 #define ENH_BUFL (ENH_NBLOCKS_TOT) * ENH_BLOCKL
92 #define ENH_BUFL_FILTEROVERHEAD 3
93 #define ENH_A0 819                      /* Q14 */
94 #define ENH_A0_MINUS_A0A0DIV4 848256041 /* Q34 */
95 #define ENH_A0DIV2 26843546             /* Q30 */
96 
97 /* PLC */
98 
99 /* Down sampling */
100 
101 #define FILTERORDER_DS_PLUS1 7
102 #define DELAY_DS 3
103 #define FACTOR_DS 2
104 
105 /* bit stream defs */
106 
107 #define NO_OF_BYTES_20MS 38
108 #define NO_OF_BYTES_30MS 50
109 #define NO_OF_WORDS_20MS 19
110 #define NO_OF_WORDS_30MS 25
111 #define STATE_BITS 3
112 #define BYTE_LEN 8
113 #define ULP_CLASSES 3
114 
115 /* help parameters */
116 
117 #define TWO_PI_FIX 25736 /* Q12 */
118 
119 /* Constants for codebook search and creation */
120 
121 #define ST_MEM_L_TBL 85
122 #define MEM_LF_TBL 147
123 
124 /* Struct for the bits */
125 typedef struct iLBC_bits_t_ {
126   int16_t lsf[LSF_NSPLIT * LPC_N_MAX];
127   int16_t cb_index[CB_NSTAGES * (NASUB_MAX + 1)];   /* First CB_NSTAGES values
128                                                        contains extra CB index */
129   int16_t gain_index[CB_NSTAGES * (NASUB_MAX + 1)]; /* First CB_NSTAGES values
130                                                        contains extra CB gain */
131   size_t idxForMax;
132   int16_t state_first;
133   int16_t idxVec[STATE_SHORT_LEN_30MS];
134   int16_t firstbits;
135   size_t startIdx;
136 } iLBC_bits;
137 
138 /* type definition encoder instance */
139 typedef struct IlbcEncoder_ {
140   /* flag for frame size mode */
141   int16_t mode;
142 
143   /* basic parameters for different frame sizes */
144   size_t blockl;
145   size_t nsub;
146   int16_t nasub;
147   size_t no_of_bytes, no_of_words;
148   int16_t lpc_n;
149   size_t state_short_len;
150 
151   /* analysis filter state */
152   int16_t anaMem[LPC_FILTERORDER];
153 
154   /* Fix-point old lsf parameters for interpolation */
155   int16_t lsfold[LPC_FILTERORDER];
156   int16_t lsfdeqold[LPC_FILTERORDER];
157 
158   /* signal buffer for LP analysis */
159   int16_t lpc_buffer[LPC_LOOKBACK + BLOCKL_MAX];
160 
161   /* state of input HP filter */
162   int16_t hpimemx[2];
163   int16_t hpimemy[4];
164 
165 #ifdef SPLIT_10MS
166   int16_t weightdenumbuf[66];
167   int16_t past_samples[160];
168   uint16_t bytes[25];
169   int16_t section;
170   int16_t Nfor_flag;
171   int16_t Nback_flag;
172   int16_t start_pos;
173   size_t diff;
174 #endif
175 
176 } IlbcEncoder;
177 
178 /* type definition decoder instance */
179 typedef struct IlbcDecoder_ {
180   /* flag for frame size mode */
181   int16_t mode;
182 
183   /* basic parameters for different frame sizes */
184   size_t blockl;
185   size_t nsub;
186   int16_t nasub;
187   size_t no_of_bytes, no_of_words;
188   int16_t lpc_n;
189   size_t state_short_len;
190 
191   /* synthesis filter state */
192   int16_t syntMem[LPC_FILTERORDER];
193 
194   /* old LSF for interpolation */
195   int16_t lsfdeqold[LPC_FILTERORDER];
196 
197   /* pitch lag estimated in enhancer and used in PLC */
198   size_t last_lag;
199 
200   /* PLC state information */
201   int consPLICount, prev_enh_pl;
202   int16_t perSquare;
203 
204   int16_t prevScale, prevPLI;
205   size_t prevLag;
206   int16_t prevLpc[LPC_FILTERORDER + 1];
207   int16_t prevResidual[NSUB_MAX * SUBL];
208   int16_t seed;
209 
210   /* previous synthesis filter parameters */
211 
212   int16_t old_syntdenum[(LPC_FILTERORDER + 1) * NSUB_MAX];
213 
214   /* state of output HP filter */
215   int16_t hpimemx[2];
216   int16_t hpimemy[4];
217 
218   /* enhancer state information */
219   int use_enhancer;
220   int16_t enh_buf[ENH_BUFL + ENH_BUFL_FILTEROVERHEAD];
221   size_t enh_period[ENH_NBLOCKS_TOT];
222 
223 } IlbcDecoder;
224 
225 #endif
226