wok view asterisk/stuff/rfc3951.txt @ rev 17457
Add icmpshell
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Thu Dec 18 15:59:17 2014 +0100 (2014-12-18) |
parents | |
children |
line source
7 Network Working Group S. Andersen
8 Request for Comments: 3951 Aalborg University
9 Category: Experimental A. Duric
10 Telio
11 H. Astrom
12 R. Hagen
13 W. Kleijn
14 J. Linden
15 Global IP Sound
16 December 2004
19 Internet Low Bit Rate Codec (iLBC)
21 Status of this Memo
23 This memo defines an Experimental Protocol for the Internet
24 community. It does not specify an Internet standard of any kind.
25 Discussion and suggestions for improvement are requested.
26 Distribution of this memo is unlimited.
28 Copyright Notice
30 Copyright (C) The Internet Society (2004).
32 Abstract
34 This document specifies a speech codec suitable for robust voice
35 communication over IP. The codec is developed by Global IP Sound
36 (GIPS). It is designed for narrow band speech and results in a
37 payload bit rate of 13.33 kbit/s for 30 ms frames and 15.20 kbit/s
38 for 20 ms frames. The codec enables graceful speech quality
39 degradation in the case of lost frames, which occurs in connection
40 with lost or delayed IP packets.
58 Andersen, et al. Experimental [Page 1]
60 RFC 3951 Internet Low Bit Rate Codec December 2004
63 Table of Contents
65 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . 4
66 2. Outline of the Codec . . . . . . . . . . . . . . . . . . . . . 5
67 2.1. Encoder. . . . . . . . . . . . . . . . . . . . . . . . . 5
68 2.2. Decoder. . . . . . . . . . . . . . . . . . . . . . . . . 7
69 3. Encoder Principles . . . . . . . . . . . . . . . . . . . . . . 7
70 3.1. Pre-processing . . . . . . . . . . . . . . . . . . . . . 9
71 3.2. LPC Analysis and Quantization. . . . . . . . . . . . . . 9
72 3.2.1. Computation of Autocorrelation Coefficients. . . 10
73 3.2.2. Computation of LPC Coefficients. . . . . . . . . 11
74 3.2.3. Computation of LSF Coefficients from LPC
75 Coefficients . . . . . . . . . . . . . . . . . . 11
76 3.2.4. Quantization of LSF Coefficients . . . . . . . . 12
77 3.2.5. Stability Check of LSF Coefficients. . . . . . . 13
78 3.2.6. Interpolation of LSF Coefficients. . . . . . . . 13
79 3.2.7. LPC Analysis and Quantization for 20 ms Frames . 14
80 3.3. Calculation of the Residual. . . . . . . . . . . . . . . 15
81 3.4. Perceptual Weighting Filter. . . . . . . . . . . . . . . 15
82 3.5. Start State Encoder. . . . . . . . . . . . . . . . . . . 15
83 3.5.1. Start State Estimation . . . . . . . . . . . . . 16
84 3.5.2. All-Pass Filtering and Scale Quantization. . . . 17
85 3.5.3. Scalar Quantization. . . . . . . . . . . . . . . 18
86 3.6. Encoding the Remaining Samples . . . . . . . . . . . . . 19
87 3.6.1. Codebook Memory. . . . . . . . . . . . . . . . . 20
88 3.6.2. Perceptual Weighting of Codebook Memory
89 and Target . . . . . . . . . . . . . . . . . . . 22
90 3.6.3. Codebook Creation. . . . . . . . . . . . . . . . 23
91 3.6.3.1. Creation of a Base Codebook . . . . . . 23
92 3.6.3.2. Codebook Expansion. . . . . . . . . . . 24
93 3.6.3.3. Codebook Augmentation . . . . . . . . . 24
94 3.6.4. Codebook Search. . . . . . . . . . . . . . . . . 26
95 3.6.4.1. Codebook Search at Each Stage . . . . . 26
96 3.6.4.2. Gain Quantization at Each Stage . . . . 27
97 3.6.4.3. Preparation of Target for Next Stage. . 28
98 3.7. Gain Correction Encoding . . . . . . . . . . . . . . . . 28
99 3.8. Bitstream Definition . . . . . . . . . . . . . . . . . . 29
100 4. Decoder Principles . . . . . . . . . . . . . . . . . . . . . . 32
101 4.1. LPC Filter Reconstruction. . . . . . . . . . . . . . . . 33
102 4.2. Start State Reconstruction . . . . . . . . . . . . . . . 33
103 4.3. Excitation Decoding Loop . . . . . . . . . . . . . . . . 34
104 4.4. Multistage Adaptive Codebook Decoding. . . . . . . . . . 35
105 4.4.1. Construction of the Decoded Excitation Signal. . 35
106 4.5. Packet Loss Concealment. . . . . . . . . . . . . . . . . 35
107 4.5.1. Block Received Correctly and Previous Block
108 Also Received. . . . . . . . . . . . . . . . . . 35
109 4.5.2. Block Not Received . . . . . . . . . . . . . . . 36
114 Andersen, et al. Experimental [Page 2]
116 RFC 3951 Internet Low Bit Rate Codec December 2004
119 4.5.3. Block Received Correctly When Previous Block
120 Not Received . . . . . . . . . . . . . . . . . . 36
121 4.6. Enhancement. . . . . . . . . . . . . . . . . . . . . . . 37
122 4.6.1. Estimating the Pitch . . . . . . . . . . . . . . 39
123 4.6.2. Determination of the Pitch-Synchronous
124 Sequences. . . . . . . . . . . . . . . . . . . . 39
125 4.6.3. Calculation of the Smoothed Excitation . . . . . 41
126 4.6.4. Enhancer Criterion . . . . . . . . . . . . . . . 41
127 4.6.5. Enhancing the Excitation . . . . . . . . . . . . 42
128 4.7. Synthesis Filtering. . . . . . . . . . . . . . . . . . . 43
129 4.8. Post Filtering . . . . . . . . . . . . . . . . . . . . . 43
130 5. Security Considerations. . . . . . . . . . . . . . . . . . . . 43
131 6. Evaluation of the iLBC Implementations . . . . . . . . . . . . 43
132 7. References . . . . . . . . . . . . . . . . . . . . . . . . . . 43
133 7.1. Normative References . . . . . . . . . . . . . . . . . . 43
134 7.2. Informative References . . . . . . . . . . . . . . . . . 44
135 8. ACKNOWLEDGEMENTS . . . . . . . . . . . . . . . . . . . . . . . 44
136 APPENDIX A: Reference Implementation . . . . . . . . . . . . . . . 45
137 A.1. iLBC_test.c. . . . . . . . . . . . . . . . . . . . . . . 46
138 A.2 iLBC_encode.h. . . . . . . . . . . . . . . . . . . . . . 52
139 A.3. iLBC_encode.c. . . . . . . . . . . . . . . . . . . . . . 53
140 A.4. iLBC_decode.h. . . . . . . . . . . . . . . . . . . . . . 63
141 A.5. iLBC_decode.c. . . . . . . . . . . . . . . . . . . . . . 64
142 A.6. iLBC_define.h. . . . . . . . . . . . . . . . . . . . . . 76
143 A.7. constants.h. . . . . . . . . . . . . . . . . . . . . . . 80
144 A.8. constants.c. . . . . . . . . . . . . . . . . . . . . . . 82
145 A.9. anaFilter.h. . . . . . . . . . . . . . . . . . . . . . . 96
146 A.10. anaFilter.c. . . . . . . . . . . . . . . . . . . . . . . 97
147 A.11. createCB.h . . . . . . . . . . . . . . . . . . . . . . . 98
148 A.12. createCB.c . . . . . . . . . . . . . . . . . . . . . . . 99
149 A.13. doCPLC.h . . . . . . . . . . . . . . . . . . . . . . . .104
150 A.14. doCPLC.c . . . . . . . . . . . . . . . . . . . . . . . .104
151 A.15. enhancer.h . . . . . . . . . . . . . . . . . . . . . . .109
152 A.16. enhancer.c . . . . . . . . . . . . . . . . . . . . . . .110
153 A.17. filter.h . . . . . . . . . . . . . . . . . . . . . . . .123
154 A.18. filter.c . . . . . . . . . . . . . . . . . . . . . . . .125
155 A.19. FrameClassify.h. . . . . . . . . . . . . . . . . . . . .128
156 A.20. FrameClassify.c. . . . . . . . . . . . . . . . . . . . .129
157 A.21. gainquant.h. . . . . . . . . . . . . . . . . . . . . . .131
158 A.22. gainquant.c. . . . . . . . . . . . . . . . . . . . . . .131
159 A.23. getCBvec.h . . . . . . . . . . . . . . . . . . . . . . .134
160 A.24. getCBvec.c . . . . . . . . . . . . . . . . . . . . . . .134
161 A.25. helpfun.h. . . . . . . . . . . . . . . . . . . . . . . .138
162 A.26. helpfun.c. . . . . . . . . . . . . . . . . . . . . . . .140
163 A.27. hpInput.h. . . . . . . . . . . . . . . . . . . . . . . .146
164 A.28. hpInput.c. . . . . . . . . . . . . . . . . . . . . . . .146
165 A.29. hpOutput.h . . . . . . . . . . . . . . . . . . . . . . .148
166 A.30. hpOutput.c . . . . . . . . . . . . . . . . . . . . . . .148
170 Andersen, et al. Experimental [Page 3]
172 RFC 3951 Internet Low Bit Rate Codec December 2004
175 A.31. iCBConstruct.h . . . . . . . . . . . . . . . . . . . . .149
176 A.32. iCBConstruct.c . . . . . . . . . . . . . . . . . . . . .150
177 A.33. iCBSearch.h. . . . . . . . . . . . . . . . . . . . . . .152
178 A.34. iCBSearch.c. . . . . . . . . . . . . . . . . . . . . . .153
179 A.35. LPCdecode.h. . . . . . . . . . . . . . . . . . . . . . .163
180 A.36. LPCdecode.c. . . . . . . . . . . . . . . . . . . . . . .164
181 A.37. LPCencode.h. . . . . . . . . . . . . . . . . . . . . . .167
182 A.38. LPCencode.c. . . . . . . . . . . . . . . . . . . . . . .167
183 A.39. lsf.h. . . . . . . . . . . . . . . . . . . . . . . . . .172
184 A.40. lsf.c. . . . . . . . . . . . . . . . . . . . . . . . . .172
185 A.41. packing.h. . . . . . . . . . . . . . . . . . . . . . . .178
186 A.42. packing.c. . . . . . . . . . . . . . . . . . . . . . . .179
187 A.43. StateConstructW.h. . . . . . . . . . . . . . . . . . . .182
188 A.44. StateConstructW.c. . . . . . . . . . . . . . . . . . . .183
189 A.45. StateSearchW.h . . . . . . . . . . . . . . . . . . . . .185
190 A.46. StateSearchW.c . . . . . . . . . . . . . . . . . . . . .186
191 A.47. syntFilter.h . . . . . . . . . . . . . . . . . . . . . .190
192 A.48. syntFilter.c . . . . . . . . . . . . . . . . . . . . . .190
193 Authors' Addresses . . . . . . . . . . . . . . . . . . . . . . . .192
194 Full Copyright Statement . . . . . . . . . . . . . . . . . . . . .194
196 1. Introduction
198 This document contains the description of an algorithm for the coding
199 of speech signals sampled at 8 kHz. The algorithm, called iLBC, uses
200 a block-independent linear-predictive coding (LPC) algorithm and has
201 support for two basic frame lengths: 20 ms at 15.2 kbit/s and 30 ms
202 at 13.33 kbit/s. When the codec operates at block lengths of 20 ms,
203 it produces 304 bits per block, which SHOULD be packetized as in [1].
204 Similarly, for block lengths of 30 ms it produces 400 bits per block,
205 which SHOULD be packetized as in [1]. The two modes for the
206 different frame sizes operate in a very similar way. When they
207 differ it is explicitly stated in the text, usually with the notation
208 x/y, where x refers to the 20 ms mode and y refers to the 30 ms mode.
210 The described algorithm results in a speech coding system with a
211 controlled response to packet losses similar to what is known from
212 pulse code modulation (PCM) with packet loss concealment (PLC), such
213 as the ITU-T G.711 standard [4], which operates at a fixed bit rate
214 of 64 kbit/s. At the same time, the described algorithm enables
215 fixed bit rate coding with a quality-versus-bit rate tradeoff close
216 to state-of-the-art. A suitable RTP payload format for the iLBC
217 codec is specified in [1].
219 Some of the applications for which this coder is suitable are real
220 time communications such as telephony and videoconferencing,
221 streaming audio, archival, and messaging.
226 Andersen, et al. Experimental [Page 4]
228 RFC 3951 Internet Low Bit Rate Codec December 2004
231 Cable Television Laboratories (CableLabs(R)) has adopted iLBC as a
232 mandatory PacketCable(TM) audio codec standard for VoIP over Cable
233 applications [3].
235 This document is organized as follows. Section 2 gives a brief
236 outline of the codec. The specific encoder and decoder algorithms
237 are explained in sections 3 and 4, respectively. Appendix A provides
238 a c-code reference implementation.
240 The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
241 "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this
242 document are to be interpreted as described in BCP 14, RFC 2119 [2].
244 2. Outline of the Codec
246 The codec consists of an encoder and a decoder as described in
247 sections 2.1 and 2.2, respectively.
249 The essence of the codec is LPC and block-based coding of the LPC
250 residual signal. For each 160/240 (20 ms/30 ms) sample block, the
251 following major steps are performed: A set of LPC filters are
252 computed, and the speech signal is filtered through them to produce
253 the residual signal. The codec uses scalar quantization of the
254 dominant part, in terms of energy, of the residual signal for the
255 block. The dominant state is of length 57/58 (20 ms/30 ms) samples
256 and forms a start state for dynamic codebooks constructed from the
257 already coded parts of the residual signal. These dynamic codebooks
258 are used to code the remaining parts of the residual signal. By this
259 method, coding independence between blocks is achieved, resulting in
260 elimination of propagation of perceptual degradations due to packet
261 loss. The method facilitates high-quality packet loss concealment
262 (PLC).
264 2.1. Encoder
266 The input to the encoder SHOULD be 16 bit uniform PCM sampled at 8
267 kHz. It SHOULD be partitioned into blocks of BLOCKL=160/240 samples
268 for the 20/30 ms frame size. Each block is divided into NSUB=4/6
269 consecutive sub-blocks of SUBL=40 samples each. For 30 ms frame
270 size, the encoder performs two LPC_FILTERORDER=10 linear-predictive
271 coding (LPC) analyses. The first analysis applies a smooth window
272 centered over the second sub-block and extending to the middle of the
273 fifth sub-block. The second LPC analysis applies a smooth asymmetric
274 window centered over the fifth sub-block and extending to the end of
275 the sixth sub-block. For 20 ms frame size, one LPC_FILTERORDER=10
276 linear-predictive coding (LPC) analysis is performed with a smooth
277 window centered over the third sub-frame.
282 Andersen, et al. Experimental [Page 5]
284 RFC 3951 Internet Low Bit Rate Codec December 2004
287 For each of the LPC analyses, a set of line-spectral frequencies
288 (LSFs) are obtained, quantized, and interpolated to obtain LSF
289 coefficients for each sub-block. Subsequently, the LPC residual is
290 computed by using the quantized and interpolated LPC analysis
291 filters.
293 The two consecutive sub-blocks of the residual exhibiting the maximal
294 weighted energy are identified. Within these two sub-blocks, the
295 start state (segment) is selected from two choices: the first 57/58
296 samples or the last 57/58 samples of the two consecutive sub-blocks.
297 The selected segment is the one of higher energy. The start state is
298 encoded with scalar quantization.
300 A dynamic codebook encoding procedure is used to encode 1) the 23/22
301 (20 ms/30 ms) remaining samples in the two sub-blocks containing the
302 start state; 2) the sub-blocks after the start state in time; and 3)
303 the sub-blocks before the start state in time. Thus, the encoding
304 target can be either the 23/22 samples remaining of the two sub-
305 blocks containing the start state or a 40-sample sub-block. This
306 target can consist of samples indexed forward in time or backward in
307 time, depending on the location of the start state.
309 The codebook coding is based on an adaptive codebook built from a
310 codebook memory that contains decoded LPC excitation samples from the
311 already encoded part of the block. These samples are indexed in the
312 same time direction as the target vector, ending at the sample
313 instant prior to the first sample instant represented in the target
314 vector. The codebook is used in CB_NSTAGES=3 stages in a successive
315 refinement approach, and the resulting three code vector gains are
316 encoded with 5-, 4-, and 3-bit scalar quantization, respectively.
318 The codebook search method employs noise shaping derived from the LPC
319 filters, and the main decision criterion is to minimize the squared
320 error between the target vector and the code vectors. Each code
321 vector in this codebook comes from one of CB_EXPAND=2 codebook
322 sections. The first section is filled with delayed, already encoded
323 residual vectors. The code vectors of the second codebook section
324 are constructed by predefined linear combinations of vectors in the
325 first section of the codebook.
327 As codebook encoding with squared-error matching is known to produce
328 a coded signal of less power than does the scalar quantized start
329 state signal, a gain re-scaling method is implemented by a refined
330 search for a better set of codebook gains in terms of power matching
331 after encoding. This is done by searching for a higher value of the
332 gain factor for the first stage codebook, as the subsequent stage
333 codebook gains are scaled by the first stage gain.
338 Andersen, et al. Experimental [Page 6]
340 RFC 3951 Internet Low Bit Rate Codec December 2004
343 2.2. Decoder
345 Typically for packet communications, a jitter buffer placed at the
346 receiving end decides whether the packet containing an encoded signal
347 block has been received or lost. This logic is not part of the codec
348 described here. For each encoded signal block received the decoder
349 performs a decoding. For each lost signal block, the decoder
350 performs a PLC operation.
352 The decoding for each block starts by decoding and interpolating the
353 LPC coefficients. Subsequently the start state is decoded.
355 For codebook-encoded segments, each segment is decoded by
356 constructing the three code vectors given by the received codebook
357 indices in the same way that the code vectors were constructed in the
358 encoder. The three gain factors are also decoded and the resulting
359 decoded signal is given by the sum of the three codebook vectors
360 scaled with respective gain.
362 An enhancement algorithm is applied to the reconstructed excitation
363 signal. This enhancement augments the periodicity of voiced speech
364 regions. The enhancement is optimized under the constraint that the
365 modification signal (defined as the difference between the enhanced
366 excitation and the excitation signal prior to enhancement) has a
367 short-time energy that does not exceed a preset fraction of the
368 short-time energy of the excitation signal prior to enhancement.
370 A packet loss concealment (PLC) operation is easily embedded in the
371 decoder. The PLC operation can, e.g., be based on repeating LPC
372 filters and obtaining the LPC residual signal by using a long-term
373 prediction estimate from previous residual blocks.
375 3. Encoder Principles
377 The following block diagram is an overview of all the components of
378 the iLBC encoding procedure. The description of the blocks contains
379 references to the section where that particular procedure is further
380 described.
394 Andersen, et al. Experimental [Page 7]
396 RFC 3951 Internet Low Bit Rate Codec December 2004
399 +-----------+ +---------+ +---------+
400 speech -> | 1. Pre P | -> | 2. LPC | -> | 3. Ana | ->
401 +-----------+ +---------+ +---------+
403 +---------------+ +--------------+
404 -> | 4. Start Sel | ->| 5. Scalar Qu | ->
405 +---------------+ +--------------+
407 +--------------+ +---------------+
408 -> |6. CB Search | -> | 7. Packetize | -> payload
409 | +--------------+ | +---------------+
410 ----<---------<------
411 sub-frame 0..2/4 (20 ms/30 ms)
413 Figure 3.1. Flow chart of the iLBC encoder
415 1. Pre-process speech with a HP filter, if needed (section 3.1).
417 2. Compute LPC parameters, quantize, and interpolate (section 3.2).
419 3. Use analysis filters on speech to compute residual (section 3.3).
421 4. Select position of 57/58-sample start state (section 3.5).
423 5. Quantize the 57/58-sample start state with scalar quantization
424 (section 3.5).
426 6. Search the codebook for each sub-frame. Start with 23/22 sample
427 block, then encode sub-blocks forward in time, and then encode
428 sub-blocks backward in time. For each block, the steps in Figure
429 3.4 are performed (section 3.6).
431 7. Packetize the bits into the payload specified in Table 3.2.
433 The input to the encoder SHOULD be 16-bit uniform PCM sampled at 8
434 kHz. Also it SHOULD be partitioned into blocks of BLOCKL=160/240
435 samples. Each block input to the encoder is divided into NSUB=4/6
436 consecutive sub-blocks of SUBL=40 samples each.
450 Andersen, et al. Experimental [Page 8]
452 RFC 3951 Internet Low Bit Rate Codec December 2004
455 0 39 79 119 159
456 +---------------------------------------+
457 | 1 | 2 | 3 | 4 |
458 +---------------------------------------+
459 20 ms frame
461 0 39 79 119 159 199 239
462 +-----------------------------------------------------------+
463 | 1 | 2 | 3 | 4 | 5 | 6 |
464 +-----------------------------------------------------------+
465 30 ms frame
466 Figure 3.2. One input block to the encoder for 20 ms (with four sub-
467 frames) and 30 ms (with six sub-frames).
469 3.1. Pre-processing
471 In some applications, the recorded speech signal contains DC level
472 and/or 50/60 Hz noise. If these components have not been removed
473 prior to the encoder call, they should be removed by a high-pass
474 filter. A reference implementation of this, using a filter with a
475 cutoff frequency of 90 Hz, can be found in Appendix A.28.
477 3.2. LPC Analysis and Quantization
479 The input to the LPC analysis module is a possibly high-pass filtered
480 speech buffer, speech_hp, that contains 240/300 (LPC_LOOKBACK +
481 BLOCKL = 80/60 + 160/240 = 240/300) speech samples, where samples 0
482 through 79/59 are from the previous block and samples 80/60 through
483 239/299 are from the current block. No look-ahead into the next
484 block is used. For the very first block processed, the look-back
485 samples are assumed to be zeros.
487 For each input block, the LPC analysis calculates one/two set(s) of
488 LPC_FILTERORDER=10 LPC filter coefficients using the autocorrelation
489 method and the Levinson-Durbin recursion. These coefficients are
490 converted to the Line Spectrum Frequency representation. In the 20
491 ms case, the single lsf set represents the spectral characteristics
492 as measured at the center of the third sub-block. For 30 ms frames,
493 the first set, lsf1, represents the spectral properties of the input
494 signal at the center of the second sub-block, and the other set,
495 lsf2, represents the spectral characteristics as measured at the
496 center of the fifth sub-block. The details of the computation for 30
497 ms frames are described in sections 3.2.1 through 3.2.6. Section
498 3.2.7 explains how the LPC Analysis and Quantization differs for 20
499 ms frames.
506 Andersen, et al. Experimental [Page 9]
508 RFC 3951 Internet Low Bit Rate Codec December 2004
511 3.2.1. Computation of Autocorrelation Coefficients
513 The first step in the LPC analysis procedure is to calculate
514 autocorrelation coefficients by using windowed speech samples. This
515 windowing is the only difference in the LPC analysis procedure for
516 the two sets of coefficients. For the first set, a 240-sample-long
517 standard symmetric Hanning window is applied to samples 0 through 239
518 of the input data. The first window, lpc_winTbl, is defined as
520 lpc_winTbl[i]= 0.5 * (1.0 - cos((2*PI*(i+1))/(BLOCKL+1)));
521 i=0,...,119
522 lpc_winTbl[i] = winTbl[BLOCKL - i - 1]; i=120,...,239
524 The windowed speech speech_hp_win1 is then obtained by multiplying
525 the first 240 samples of the input speech buffer with the window
526 coefficients:
528 speech_hp_win1[i] = speech_hp[i] * lpc_winTbl[i];
529 i=0,...,BLOCKL-1
531 From these 240 windowed speech samples, 11 (LPC_FILTERORDER + 1)
532 autocorrelation coefficients, acf1, are calculated:
534 acf1[lag] += speech_hp_win1[n] * speech_hp_win1[n + lag];
535 lag=0,...,LPC_FILTERORDER; n=0,...,BLOCKL-lag-1
537 In order to make the analysis more robust against numerical precision
538 problems, a spectral smoothing procedure is applied by windowing the
539 autocorrelation coefficients before the LPC coefficients are
540 computed. Also, a white noise floor is added to the autocorrelation
541 function by multiplying coefficient zero by 1.0001 (40dB below the
542 energy of the windowed speech signal). These two steps are
543 implemented by multiplying the autocorrelation coefficients with the
544 following window:
546 lpc_lagwinTbl[0] = 1.0001;
547 lpc_lagwinTbl[i] = exp(-0.5 * ((2 * PI * 60.0 * i) /FS)^2);
548 i=1,...,LPC_FILTERORDER
549 where FS=8000 is the sampling frequency
551 Then, the windowed acf function acf1_win is obtained by
553 acf1_win[i] = acf1[i] * lpc_lagwinTbl[i];
554 i=0,...,LPC_FILTERORDER
556 The second set of autocorrelation coefficients, acf2_win, are
557 obtained in a similar manner. The window, lpc_asymwinTbl, is applied
558 to samples 60 through 299, i.e., the entire current block. The
562 Andersen, et al. Experimental [Page 10]
564 RFC 3951 Internet Low Bit Rate Codec December 2004
567 window consists of two segments, the first (samples 0 to 219) being
568 half a Hanning window with length 440 and the second a quarter of a
569 cycle of a cosine wave. By using this asymmetric window, an LPC
570 analysis centered in the fifth sub-block is obtained without the need
571 for any look-ahead, which would add delay. The asymmetric window is
572 defined as
574 lpc_asymwinTbl[i] = (sin(PI * (i + 1) / 441))^2; i=0,...,219
576 lpc_asymwinTbl[i] = cos((i - 220) * PI / 40); i=220,...,239
578 and the windowed speech is computed by
580 speech_hp_win2[i] = speech_hp[i + LPC_LOOKBACK] *
581 lpc_asymwinTbl[i]; i=0,....BLOCKL-1
583 The windowed autocorrelation coefficients are then obtained in
584 exactly the same way as for the first analysis instance.
586 The generation of the windows lpc_winTbl, lpc_asymwinTbl, and
587 lpc_lagwinTbl are typically done in advance, and the arrays are
588 stored in ROM rather than repeating the calculation for every block.
590 3.2.2. Computation of LPC Coefficients
592 From the 2 x 11 smoothed autocorrelation coefficients, acf1_win and
593 acf2_win, the 2 x 11 LPC coefficients, lp1 and lp2, are calculated
594 in the same way for both analysis locations by using the well known
595 Levinson-Durbin recursion. The first LPC coefficient is always 1.0,
596 resulting in ten unique coefficients.
598 After determining the LPC coefficients, a bandwidth expansion
599 procedure is applied to smooth the spectral peaks in the
600 short-term spectrum. The bandwidth addition is obtained by the
601 following modification of the LPC coefficients:
603 lp1_bw[i] = lp1[i] * chirp^i; i=0,...,LPC_FILTERORDER
604 lp2_bw[i] = lp2[i] * chirp^i; i=0,...,LPC_FILTERORDER
606 where "chirp" is a real number between 0 and 1. It is RECOMMENDED to
607 use a value of 0.9.
609 3.2.3. Computation of LSF Coefficients from LPC Coefficients
611 Thus far, two sets of LPC coefficients that represent the short-term
612 spectral characteristics of the speech signal for two different time
613 locations within the current block have been determined. These
614 coefficients SHOULD be quantized and interpolated. Before this is
618 Andersen, et al. Experimental [Page 11]
620 RFC 3951 Internet Low Bit Rate Codec December 2004
623 done, it is advantageous to convert the LPC parameters into another
624 type of representation called Line Spectral Frequencies (LSF). The
625 LSF parameters are used because they are better suited for
626 quantization and interpolation than the regular LPC coefficients.
627 Many computationally efficient methods for calculating the LSFs from
628 the LPC coefficients have been proposed in the literature. The
629 detailed implementation of one applicable method can be found in
630 Appendix A.26. The two arrays of LSF coefficients obtained, lsf1 and
631 lsf2, are of dimension 10 (LPC_FILTERORDER).
633 3.2.4. Quantization of LSF Coefficients
635 Because the LPC filters defined by the two sets of LSFs are also
636 needed in the decoder, the LSF parameters need to be quantized and
637 transmitted as side information. The total number of bits required
638 to represent the quantization of the two LSF representations for one
639 block of speech is 40, with 20 bits used for each of lsf1 and lsf2.
641 For computational and storage reasons, the LSF vectors are quantized
642 using three-split vector quantization (VQ). That is, the LSF vectors
643 are split into three sub-vectors that are each quantized with a
644 regular VQ. The quantized versions of lsf1 and lsf2, qlsf1 and
645 qlsf2, are obtained by using the same memoryless split VQ. The
646 length of each of these two LSF vectors is 10, and they are split
647 into three sub-vectors containing 3, 3, and 4 values, respectively.
649 For each of the sub-vectors, a separate codebook of quantized values
650 has been designed with a standard VQ training method for a large
651 database containing speech from a large number of speakers recorded
652 under various conditions. The size of each of the three codebooks
653 associated with the split definitions above is
655 int size_lsfCbTbl[LSF_NSPLIT] = {64,128,128};
657 The actual values of the vector quantization codebook that must be
658 used can be found in the reference code of Appendix A. Both sets of
659 LSF coefficients, lsf1 and lsf2, are quantized with a standard
660 memoryless split vector quantization (VQ) structure using the squared
661 error criterion in the LSF domain. The split VQ quantization
662 consists of the following steps:
664 1) Quantize the first three LSF coefficients (1 - 3) with a VQ
665 codebook of size 64.
666 2) Quantize the next three LSF coefficients 4 - 6 with VQ a codebook
667 of size 128.
668 3) Quantize the last four LSF coefficients (7 - 10) with a VQ
669 codebook of size 128.
674 Andersen, et al. Experimental [Page 12]
676 RFC 3951 Internet Low Bit Rate Codec December 2004
679 This procedure, repeated for lsf1 and lsf2, gives six quantization
680 indices and the quantized sets of LSF coefficients qlsf1 and qlsf2.
681 Each set of three indices is encoded with 6 + 7 + 7 = 20 bits. The
682 total number of bits used for LSF quantization in a block is thus 40
683 bits.
685 3.2.5. Stability Check of LSF Coefficients
687 The LSF representation of the LPC filter has the convenient property
688 that the coefficients are ordered by increasing value, i.e., lsf(n-1)
689 < lsf(n), 0 < n < 10, if the corresponding synthesis filter is
690 stable. As we are employing a split VQ scheme, it is possible that
691 at the split boundaries the LSF coefficients are not ordered
692 correctly and hence that the corresponding LP filter is unstable. To
693 ensure that the filter used is stable, a stability check is performed
694 for the quantized LSF vectors. If it turns out that the coefficients
695 are not ordered appropriately (with a safety margin of 50 Hz to
696 ensure that formant peaks are not too narrow), they will be moved
697 apart. The detailed method for this can be found in Appendix A.40.
698 The same procedure is performed in the decoder. This ensures that
699 exactly the same LSF representations are used in both encoder and
700 decoder.
702 3.2.6. Interpolation of LSF Coefficients
704 From the two sets of LSF coefficients that are computed for each
705 block of speech, different LSFs are obtained for each sub-block by
706 means of interpolation. This procedure is performed for the original
707 LSFs (lsf1 and lsf2), as well as the quantized versions qlsf1 and
708 qlsf2, as both versions are used in the encoder. Here follows a
709 brief summary of the interpolation scheme; the details are found in
710 the c-code of Appendix A. In the first sub-block, the average of the
711 second LSF vector from the previous block and the first LSF vector in
712 the current block is used. For sub-blocks two through five, the LSFs
713 used are obtained by linear interpolation from lsf1 (and qlsf1) to
714 lsf2 (and qlsf2), with lsf1 used in sub-block two and lsf2 in sub-
715 block five. In the last sub-block, lsf2 is used. For the very first
716 block it is assumed that the last LSF vector of the previous block is
717 equal to a predefined vector, lsfmeanTbl, obtained by calculating the
718 mean LSF vector of the LSF design database.
720 lsfmeanTbl[LPC_FILTERORDER] = {0.281738, 0.445801, 0.663330,
721 0.962524, 1.251831, 1.533081, 1.850586, 2.137817,
722 2.481445, 2.777344}
730 Andersen, et al. Experimental [Page 13]
732 RFC 3951 Internet Low Bit Rate Codec December 2004
735 The interpolation method is standard linear interpolation in the LSF
736 domain. The interpolated LSF values are converted to LPC
737 coefficients for each sub-block. The unquantized and quantized LPC
738 coefficients form two sets of filters respectively. The unquantized
739 analysis filter for sub-block k is defined as follows
741 ___
742 \
743 Ak(z)= 1 + > ak(i)*z^(-i)
744 /__
745 i=1...LPC_FILTERORDER
747 The quantized analysis filter for sub-block k is defined as follows
748 ___
749 \
750 A~k(z)= 1 + > a~k(i)*z^(-i)
751 /__
752 i=1...LPC_FILTERORDER
754 A reference implementation of the lsf encoding is given in Appendix
755 A.38. A reference implementation of the corresponding decoding can
756 be found in Appendix A.36.
758 3.2.7. LPC Analysis and Quantization for 20 ms Frames
760 As previously stated, the codec only calculates one set of LPC
761 parameters for the 20 ms frame size as opposed to two sets for 30 ms
762 frames. A single set of autocorrelation coefficients is calculated
763 on the LPC_LOOKBACK + BLOCKL = 80 + 160 = 240 samples. These samples
764 are windowed with the asymmetric window lpc_asymwinTbl, centered over
765 the third sub-frame, to form speech_hp_win. Autocorrelation
766 coefficients, acf, are calculated on the 240 samples in speech_hp_win
767 and then windowed exactly as in section 3.2.1 (resulting in
768 acf_win).
770 This single set of windowed autocorrelation coefficients is used to
771 calculate LPC coefficients, LSF coefficients, and quantized LSF
772 coefficients in exactly the same manner as in sections 3.2.3 through
773 3.2.4. As for the 30 ms frame size, the ten LSF coefficients are
774 divided into three sub-vectors of size 3, 3, and 4 and quantized by
775 using the same scheme and codebook as in section 3.2.4 to finally get
776 3 quantization indices. The quantized LSF coefficients are
777 stabilized with the algorithm described in section 3.2.5.
779 From the set of LSF coefficients computed for this block and those
780 from the previous block, different LSFs are obtained for each sub-
781 block by means of interpolation. The interpolation is done linearly
782 in the LSF domain over the four sub-blocks, so that the n-th sub-
786 Andersen, et al. Experimental [Page 14]
788 RFC 3951 Internet Low Bit Rate Codec December 2004
791 frame uses the weight (4-n)/4 for the LSF from old frame and the
792 weight n/4 of the LSF from the current frame. For the very first
793 block the mean LSF, lsfmeanTbl, is used as the LSF from the previous
794 block. Similarly as seen in section 3.2.6, both unquantized, A(z),
795 and quantized, A~(z), analysis filters are calculated for each of the
796 four sub-blocks.
798 3.3. Calculation of the Residual
800 The block of speech samples is filtered by the quantized and
801 interpolated LPC analysis filters to yield the residual signal. In
802 particular, the corresponding LPC analysis filter for each 40 sample
803 sub-block is used to filter the speech samples for the same sub-
804 block. The filter memory at the end of each sub-block is carried
805 over to the LPC filter of the next sub-block. The signal at the
806 output of each LP analysis filter constitutes the residual signal for
807 the corresponding sub-block.
809 A reference implementation of the LPC analysis filters is given in
810 Appendix A.10.
812 3.4. Perceptual Weighting Filter
814 In principle any good design of a perceptual weighting filter can be
815 applied in the encoder without compromising this codec definition.
816 However, it is RECOMMENDED to use the perceptual weighting filter Wk
817 for sub-block k specified below:
819 Wk(z)=1/Ak(z/LPC_CHIRP_WEIGHTDENUM), where
820 LPC_CHIRP_WEIGHTDENUM = 0.4222
822 This is a simple design with low complexity that is applied in the
823 LPC residual domain. Here Ak(z) is the filter obtained for sub-block
824 k from unquantized but interpolated LSF coefficients.
826 3.5. Start State Encoder
828 The start state is quantized by using a common 6-bit scalar quantizer
829 for the block and a 3-bit scalar quantizer operating on scaled
830 samples in the weighted speech domain. In the following we describe
831 the state encoding in greater detail.
842 Andersen, et al. Experimental [Page 15]
844 RFC 3951 Internet Low Bit Rate Codec December 2004
847 3.5.1. Start State Estimation
849 The two sub-blocks containing the start state are determined by
850 finding the two consecutive sub-blocks in the block having the
851 highest power. Advantageously, down-weighting is used in the
852 beginning and end of the sub-frames, i.e., the following measure is
853 computed (NSUB=4/6 for 20/30 ms frame size):
855 nsub=1,...,NSUB-1
856 ssqn[nsub] = 0.0;
857 for (i=(nsub-1)*SUBL; i<(nsub-1)*SUBL+5; i++)
858 ssqn[nsub] += sampEn_win[i-(nsub-1)*SUBL]*
859 residual[i]*residual[i];
860 for (i=(nsub-1)*SUBL+5; i<(nsub+1)*SUBL-5; i++)
861 ssqn[nsub] += residual[i]*residual[i];
862 for (i=(nsub+1)*SUBL-5; i<(nsub+1)*SUBL; i++)
863 ssqn[nsub] += sampEn_win[(nsub+1)*SUBL-i-1]*
864 residual[i]*residual[i];
866 where sampEn_win[5]={1/6, 2/6, 3/6, 4/6, 5/6}; MAY be used. The
867 sub-frame number corresponding to the maximum value of
868 ssqEn_win[nsub-1]*ssqn[nsub] is selected as the start state
869 indicator. A weighting of ssqEn_win[]={0.8,0.9,1.0,0.9,0.8} for 30
870 ms frames and ssqEn_win[]={0.9,1.0,0.9} for 20 ms frames; MAY
871 advantageously be used to bias the start state towards the middle of
872 the frame.
874 For 20 ms frames there are three possible positions for the two-sub-
875 block length maximum power segment; the start state position is
876 encoded with 2 bits. The start state position, start, MUST be
877 encoded as
879 start=1: start state in sub-frame 0 and 1
880 start=2: start state in sub-frame 1 and 2
881 start=3: start state in sub-frame 2 and 3
883 For 30 ms frames there are five possible positions of the two-sub-
884 block length maximum power segment, the start state position is
885 encoded with 3 bits. The start state position, start, MUST be
886 encoded as
888 start=1: start state in sub-frame 0 and 1
889 start=2: start state in sub-frame 1 and 2
890 start=3: start state in sub-frame 2 and 3
891 start=4: start state in sub-frame 3 and 4
892 start=5: start state in sub-frame 4 and 5
898 Andersen, et al. Experimental [Page 16]
900 RFC 3951 Internet Low Bit Rate Codec December 2004
903 Hence, in both cases, index 0 is not used. In order to shorten the
904 start state for bit rate efficiency, the start state is brought down
905 to STATE_SHORT_LEN=57 samples for 20 ms frames and STATE_SHORT_LEN=58
906 samples for 30 ms frames. The power of the first 23/22 and last
907 23/22 samples of the two sub-frame blocks identified above is
908 computed as the sum of the squared signal sample values, and the
909 23/22-sample segment with the lowest power is excluded from the start
910 state. One bit is transmitted to indicate which of the two possible
911 57/58 sample segments is used. The start state position within the
912 two sub-frames determined above, state_first, MUST be encoded as
914 state_first=1: start state is first STATE_SHORT_LEN samples
915 state_first=0: start state is last STATE_SHORT_LEN samples
917 3.5.2. All-Pass Filtering and Scale Quantization
919 The block of residual samples in the start state is first filtered by
920 an all-pass filter with the quantized LPC coefficients as denominator
921 and reversed quantized LPC coefficients as numerator. The purpose of
922 this phase-dispersion filter is to get a more even distribution of
923 the sample values in the residual signal. The filtering is performed
924 by circular convolution, where the initial filter memory is set to
925 zero.
927 res(0..(STATE_SHORT_LEN-1)) = uncoded start state residual
928 res((STATE_SHORT_LEN)..(2*STATE_SHORT_LEN-1)) = 0
930 Pk(z) = A~rk(z)/A~k(z), where
931 ___
932 \
933 A~rk(z)= z^(-LPC_FILTERORDER)+>a~k(i+1)*z^(i-(LPC_FILTERORDER-1))
934 /__
935 i=0...(LPC_FILTERORDER-1)
937 and A~k(z) is taken from the block where the start state begins
939 res -> Pk(z) -> filtered
941 ccres(k) = filtered(k) + filtered(k+STATE_SHORT_LEN),
942 k=0..(STATE_SHORT_LEN-1)
944 The all-pass filtered block is searched for its largest magnitude
945 sample. The 10-logarithm of this magnitude is quantized with a 6-bit
946 quantizer, state_frgqTbl, by finding the nearest representation.
954 Andersen, et al. Experimental [Page 17]
956 RFC 3951 Internet Low Bit Rate Codec December 2004
959 This results in an index, idxForMax, corresponding to a quantized
960 value, qmax. The all-pass filtered residual samples in the block are
961 then multiplied with a scaling factor scal=4.5/(10^qmax) to yield
962 normalized samples.
964 state_frgqTbl[64] = {1.000085, 1.071695, 1.140395, 1.206868,
965 1.277188, 1.351503, 1.429380, 1.500727, 1.569049,
966 1.639599, 1.707071, 1.781531, 1.840799, 1.901550,
967 1.956695, 2.006750, 2.055474, 2.102787, 2.142819,
968 2.183592, 2.217962, 2.257177, 2.295739, 2.332967,
969 2.369248, 2.402792, 2.435080, 2.468598, 2.503394,
970 2.539284, 2.572944, 2.605036, 2.636331, 2.668939,
971 2.698780, 2.729101, 2.759786, 2.789834, 2.818679,
972 2.848074, 2.877470, 2.906899, 2.936655, 2.967804,
973 3.000115, 3.033367, 3.066355, 3.104231, 3.141499,
974 3.183012, 3.222952, 3.265433, 3.308441, 3.350823,
975 3.395275, 3.442793, 3.490801, 3.542514, 3.604064,
976 3.666050, 3.740994, 3.830749, 3.938770, 4.101764}
978 3.5.3. Scalar Quantization
980 The normalized samples are quantized in the perceptually weighted
981 speech domain by a sample-by-sample scalar DPCM quantization as
982 depicted in Figure 3.3. Each sample in the block is filtered by a
983 weighting filter Wk(z), specified in section 3.4, to form a weighted
984 speech sample x[n]. The target sample d[n] is formed by subtracting
985 a predicted sample y[n], where the prediction filter is given by
987 Pk(z) = 1 - 1 / Wk(z).
989 +-------+ x[n] + d[n] +-----------+ u[n]
990 residual -->| Wk(z) |-------->(+)---->| Quantizer |------> quantized
991 +-------+ - /|\ +-----------+ | residual
992 | \|/
993 y[n] +--------------------->(+)
994 | |
995 | +------+ |
996 +--------| Pk(z)|<------+
997 +------+
999 Figure 3.3. Quantization of start state samples by DPCM in weighted
1000 speech domain.
1002 The coded state sample u[n] is obtained by quantizing d[n] with a 3-
1003 bit quantizer with quantization table state_sq3Tbl.
1005 state_sq3Tbl[8] = {-3.719849, -2.177490, -1.130005, -0.309692,
1006 0.444214, 1.329712, 2.436279, 3.983887}
1010 Andersen, et al. Experimental [Page 18]
1012 RFC 3951 Internet Low Bit Rate Codec December 2004
1015 The quantized samples are transformed back to the residual domain by
1016 1) scaling with 1/scal; 2) time-reversing the scaled samples; 3)
1017 filtering the time-reversed samples by the same all-pass filter, as
1018 in section 3.5.2, by using circular convolution; and 4) time-
1019 reversing the filtered samples. (More detail is in section 4.2.)
1021 A reference implementation of the start-state encoding can be found
1022 in Appendix A.46.
1024 3.6. Encoding the Remaining Samples
1026 A dynamic codebook is used to encode 1) the 23/22 remaining samples
1027 in the two sub-blocks containing the start state; 2) the sub-blocks
1028 after the start state in time; and 3) the sub-blocks before the start
1029 state in time. Thus, the encoding target can be either the 23/22
1030 samples remaining of the 2 sub-blocks containing the start state, or
1031 a 40-sample sub-block. This target can consist of samples that are
1032 indexed forward in time or backward in time, depending on the
1033 location of the start state. The length of the target is denoted by
1034 lTarget.
1036 The coding is based on an adaptive codebook that is built from a
1037 codebook memory that contains decoded LPC excitation samples from the
1038 already encoded part of the block. These samples are indexed in the
1039 same time direction as is the target vector and end at the sample
1040 instant prior to the first sample instant represented in the target
1041 vector. The codebook memory has length lMem, which is equal to
1042 CB_MEML=147 for the two/four 40-sample sub-blocks and 85 for the
1043 23/22-sample sub-block.
1045 The following figure shows an overview of the encoding procedure.
1047 +------------+ +---------------+ +-------------+
1048 -> | 1. Decode | -> | 2. Mem setup | -> | 3. Perc. W. | ->
1049 +------------+ +---------------+ +-------------+
1051 +------------+ +-----------------+
1052 -> | 4. Search | -> | 5. Upd. Target | ------------------>
1053 | +------------+ +------------------ |
1054 ----<-------------<-----------<----------
1055 stage=0..2
1057 +----------------+
1058 -> | 6. Recalc G[0] | ---------------> gains and CB indices
1059 +----------------+
1061 Figure 3.4. Flow chart of the codebook search in the iLBC encoder.
1066 Andersen, et al. Experimental [Page 19]
1068 RFC 3951 Internet Low Bit Rate Codec December 2004
1071 1. Decode the part of the residual that has been encoded so far,
1072 using the codebook without perceptual weighting.
1074 2. Set up the memory by taking data from the decoded residual. This
1075 memory is used to construct codebooks. For blocks preceding the
1076 start state, both the decoded residual and the target are time
1077 reversed (section 3.6.1).
1078 3. Filter the memory + target with the perceptual weighting filter
1079 (section 3.6.2).
1081 4. Search for the best match between the target and the codebook
1082 vector. Compute the optimal gain for this match and quantize that
1083 gain (section 3.6.4).
1085 5. Update the perceptually weighted target by subtracting the
1086 contribution from the selected codebook vector from the
1087 perceptually weighted memory (quantized gain times selected
1088 vector). Repeat 4 and 5 for the two additional stages.
1090 6. Calculate the energy loss due to encoding of the residual. If
1091 needed, compensate for this loss by an upscaling and
1092 requantization of the gain for the first stage (section 3.7).
1094 The following sections provide an in-depth description of the
1095 different blocks of Figure 3.4.
1097 3.6.1. Codebook Memory
1099 The codebook memory is based on the already encoded sub-blocks, so
1100 the available data for encoding increases for each new sub-block that
1101 has been encoded. Until enough sub-blocks have been encoded to fill
1102 the codebook memory with data, it is padded with zeros. The
1103 following figure shows an example of the order in which the sub-
1104 blocks are encoded for the 30 ms frame size if the start state is
1105 located in the last 58 samples of sub-block 2 and 3.
1107 +-----------------------------------------------------+
1108 | 5 | 1 |///|////////| 2 | 3 | 4 |
1109 +-----------------------------------------------------+
1111 Figure 3.5. The order from 1 to 5 in which the sub-blocks are
1112 encoded. The slashed area is the start state.
1122 Andersen, et al. Experimental [Page 20]
1124 RFC 3951 Internet Low Bit Rate Codec December 2004
1127 The first target sub-block to be encoded is number 1, and the
1128 corresponding codebook memory is shown in the following figure. As
1129 the target vector comes before the start state in time, the codebook
1130 memory and target vector are time reversed; thus, after the block has
1131 been time reversed the search algorithm can be reused. As only the
1132 start state has been encoded so far, the last samples of the codebook
1133 memory are padded with zeros.
1135 +-------------------------
1136 |zeros|\\\\\\\\|\\\\| 1 |
1137 +-------------------------
1139 Figure 3.6. The codebook memory, length lMem=85 samples, and the
1140 target vector 1, length 22 samples.
1142 The next step is to encode sub-block 2 by using the memory that now
1143 has increased since sub-block 1 has been encoded. The following
1144 figure shows the codebook memory for encoding of sub-block 2.
1146 +-----------------------------------
1147 | zeros | 1 |///|////////| 2 |
1148 +-----------------------------------
1150 Figure 3.7. The codebook memory, length lMem=147 samples, and the
1151 target vector 2, length 40 samples.
1153 The next step is to encode sub-block 3 by using the memory which has
1154 been increased yet again since sub-blocks 1 and 2 have been encoded,
1155 but the sub-block still has to be padded with a few zeros. The
1156 following figure shows the codebook memory for encoding of sub-block
1157 3.
1159 +------------------------------------------
1160 |zeros| 1 |///|////////| 2 | 3 |
1161 +------------------------------------------
1163 Figure 3.8. The codebook memory, length lMem=147 samples, and the
1164 target vector 3, length 40 samples.
1166 The next step is to encode sub-block 4 by using the memory which now
1167 has increased yet again since sub-blocks 1, 2, and 3 have been
1168 encoded. This time, the memory does not have to be padded with
1169 zeros. The following figure shows the codebook memory for encoding
1170 of sub-block 4.
1178 Andersen, et al. Experimental [Page 21]
1180 RFC 3951 Internet Low Bit Rate Codec December 2004
1183 +------------------------------------------
1184 |1|///|////////| 2 | 3 | 4 |
1185 +------------------------------------------
1187 Figure 3.9. The codebook memory, length lMem=147 samples, and the
1188 target vector 4, length 40 samples.
1190 The final target sub-block to be encoded is number 5, and the
1191 following figure shows the corresponding codebook memory. As the
1192 target vector comes before the start state in time, the codebook
1193 memory and target vector are time reversed.
1195 +-------------------------------------------
1196 | 3 | 2 |\\\\\\\\|\\\\| 1 | 5 |
1197 +-------------------------------------------
1199 Figure 3.10. The codebook memory, length lMem=147 samples, and the
1200 target vector 5, length 40 samples.
1202 For the case of 20 ms frames, the encoding procedure looks almost
1203 exactly the same. The only difference is that the size of the start
1204 state is 57 samples and that there are only three sub-blocks to be
1205 encoded. The encoding order is the same as above, starting with the
1206 23-sample target and then encoding the two remaining 40-sample sub-
1207 blocks, first going forward in time and then going backward in time
1208 relative to the start state.
1210 3.6.2. Perceptual Weighting of Codebook Memory and Target
1212 To provide a perceptual weighting of the coding error, a
1213 concatenation of the codebook memory and the target to be coded is
1214 all-pole filtered with the perceptual weighting filter specified in
1215 section 3.4. The filter state of the weighting filter is set to
1216 zero.
1218 in(0..(lMem-1)) = unweighted codebook memory
1219 in(lMem..(lMem+lTarget-1)) = unweighted target signal
1222 in -> Wk(z) -> filtered,
1223 where Wk(z) is taken from the sub-block of the target
1225 weighted codebook memory = filtered(0..(lMem-1))
1226 weighted target signal = filtered(lMem..(lMem+lTarget-1))
1228 The codebook search is done with the weighted codebook memory and the
1229 weighted target, whereas the decoding and the codebook memory update
1230 uses the unweighted codebook memory.
1234 Andersen, et al. Experimental [Page 22]
1236 RFC 3951 Internet Low Bit Rate Codec December 2004
1239 3.6.3. Codebook Creation
1241 The codebook for the search is created from the perceptually weighted
1242 codebook memory. It consists of two sections, where the first is
1243 referred to as the base codebook and the second as the expanded
1244 codebook, as it is created by linear combinations of the first. Each
1245 of these two sections also has a subsection referred to as the
1246 augmented codebook. The augmented codebook is only created and used
1247 for the coding of the 40-sample sub-blocks and not for the 23/22-
1248 sample sub-block case. The codebook size used for the different
1249 sub-blocks and different stages are summarized in the table below.
1251 Stage
1252 1 2 & 3
1253 --------------------------------------------
1254 22 128 (64+0)*2 128 (64+0)*2
1255 Sub- 1:st 40 256 (108+20)*2 128 (44+20)*2
1256 Blocks 2:nd 40 256 (108+20)*2 256 (108+20)*2
1257 3:rd 40 256 (108+20)*2 256 (108+20)*2
1258 4:th 40 256 (108+20)*2 256 (108+20)*2
1260 Table 3.1. Codebook sizes for the 30 ms mode.
1262 Table 3.1 shows the codebook size for the different sub-blocks and
1263 stages for 30 ms frames. Inside the parentheses it shows how the
1264 number of codebook vectors is distributed, within the two sections,
1265 between the base/expanded codebook and the augmented base/expanded
1266 codebook. It should be interpreted in the following way:
1267 (base/expanded cb + augmented base/expanded cb). The total number of
1268 codebook vectors for a specific sub-block and stage is given by the
1269 following formula:
1271 Tot. cb vectors = base cb + aug. base cb + exp. cb + aug. exp. cb
1273 The corresponding values to Figure 3.1 for 20 ms frames are only
1274 slightly modified. The short sub-block is 23 instead of 22 samples,
1275 and the 3:rd and 4:th sub-frame are not present.
1277 3.6.3.1. Creation of a Base Codebook
1279 The base codebook is given by the perceptually weighted codebook
1280 memory that is mentioned in section 3.5.3. The different codebook
1281 vectors are given by sliding a window of length 23/22 or 40, given by
1282 variable lTarget, over the lMem-long perceptually weighted codebook
1283 memory. The indices are ordered so that the codebook vector
1284 containing sample (lMem-lTarget-n) to (lMem-n-1) of the codebook
1290 Andersen, et al. Experimental [Page 23]
1292 RFC 3951 Internet Low Bit Rate Codec December 2004
1295 memory vector has index n, where n=0..lMem-lTarget. Thus the total
1296 number of base codebook vectors is lMem-lTarget+1, and the indices
1297 are ordered from sample delay lTarget (23/22 or 40) to lMem+1 (86 or
1298 148).
1300 3.6.3.2. Codebook Expansion
1302 The base codebook is expanded by a factor of 2, creating an
1303 additional section in the codebook. This new section is obtained by
1304 filtering the base codebook, base_cb, with a FIR filter with filter
1305 length CB_FILTERLEN=8. The construction of the expanded codebook
1306 compensates for the delay of four samples introduced by the FIR
1307 filter.
1309 cbfiltersTbl[CB_FILTERLEN]={-0.033691, 0.083740, -0.144043,
1310 0.713379, 0.806152, -0.184326,
1311 0.108887, -0.034180};
1313 ___
1314 \
1315 exp_cb(k)= + > cbfiltersTbl(i)*x(k-i+4)
1316 /__
1317 i=0...(LPC_FILTERORDER-1)
1319 where x(j) = base_cb(j) for j=0..lMem-1 and 0 otherwise
1321 The individual codebook vectors of the new filtered codebook, exp_cb,
1322 and their indices are obtained in the same fashion as described above
1323 for the base codebook.
1325 3.6.3.3. Codebook Augmentation
1327 For cases where encoding entire sub-blocks, i.e., cbveclen=40, the
1328 base and expanded codebooks are augmented to increase codebook
1329 richness. The codebooks are augmented by vectors produced by
1330 interpolation of segments. The base and expanded codebook,
1331 constructed above, consists of vectors corresponding to sample delays
1332 in the range from cbveclen to lMem. The codebook augmentation
1333 attempts to augment these codebooks with vectors corresponding to
1334 sample delays from 20 to 39. However, not all of these samples are
1335 present in the base codebook and expanded codebook, respectively.
1336 Therefore, the augmentation vectors are constructed as linear
1337 combinations between samples corresponding to sample delays in the
1338 range 20 to 39. The general idea of this procedure is presented in
1339 the following figures and text. The procedure is performed for both
1340 the base codebook and the expanded codebook.
1346 Andersen, et al. Experimental [Page 24]
1348 RFC 3951 Internet Low Bit Rate Codec December 2004
1351 - - ------------------------|
1352 codebook memory |
1353 - - ------------------------|
1354 |-5-|---15---|-5-|
1355 pi pp po
1357 | | Codebook vector
1358 |---15---|-5-|-----20-----| <- corresponding to
1359 i ii iii sample delay 20
1361 Figure 3.11. Generation of the first augmented codebook.
1363 Figure 3.11 shows the codebook memory with pointers pi, pp, and po,
1364 where pi points to sample 25, pp to sample 20, and po to sample 5.
1365 Below the codebook memory, the augmented codebook vector
1366 corresponding to sample delay 20 is drawn. Segment i consists of
1367 fifteen samples from pointer pp and forward in time. Segment ii
1368 consists of five interpolated samples from pi and forward and from po
1369 and forward. The samples are linearly interpolated with weights
1370 [0.0, 0.2, 0.4, 0.6, 0.8] for pi and weights [1.0, 0.8, 0.6, 0.4,
1371 0.2] for po. Segment iii consists of twenty samples from pp and
1372 forward. The augmented codebook vector corresponding to sample delay
1373 21 is produced by moving pointers pp and pi one sample backward in
1374 time. This gives us the following figure.
1376 - - ------------------------|
1377 codebook memory |
1378 - - ------------------------|
1379 |-5-|---16---|-5-|
1380 pi pp po
1382 | | Codebook vector
1383 |---16---|-5-|-----19-----| <- corresponding to
1384 i ii iii sample delay 21
1386 Figure 3.12. Generation of the second augmented codebook.
1388 Figure 3.12 shows the codebook memory with pointers pi, pp and po
1389 where pi points to sample 26, pp to sample 21, and po to sample 5.
1390 Below the codebook memory, the augmented codebook vector
1391 corresponding to sample delay 21 is drawn. Segment i now consists of
1392 sixteen samples from pp and forward. Segment ii consists of five
1393 interpolated samples from pi and forward and from po and forward, and
1394 the interpolation weights are the same throughout the procedure.
1395 Segment iii consists of nineteen samples from pp and forward. The
1396 same procedure of moving the two pointers is continued until the last
1397 augmented vector corresponding to sample delay 39 has been created.
1398 This gives a total of twenty new codebook vectors to each of the two
1402 Andersen, et al. Experimental [Page 25]
1404 RFC 3951 Internet Low Bit Rate Codec December 2004
1407 sections. Thus the total number of codebook vectors for each of the
1408 two sections, when including the augmented codebook, becomes lMem-
1409 SUBL+1+SUBL/2. This is provided that augmentation is evoked, i.e.,
1410 that lTarget=SUBL.
1412 3.6.4. Codebook Search
1414 The codebook search uses the codebooks described in the sections
1415 above to find the best match of the perceptually weighted target, see
1416 section 3.6.2. The search method is a multi-stage gain-shape
1417 matching performed as follows. At each stage the best shape vector
1418 is identified, then the gain is calculated and quantized, and finally
1419 the target is updated in preparation for the next codebook search
1420 stage. The number of stages is CB_NSTAGES=3.
1422 If the target is the 23/22-sample vector the codebooks are indexed so
1423 that the base codebook is followed by the expanded codebook. If the
1424 target is 40 samples the order is as follows: base codebook,
1425 augmented base codebook, expanded codebook, and augmented expanded
1426 codebook. The size of each codebook section and its corresponding
1427 augmented section is given by Table 3.1 in section 3.6.3.
1429 For example, when the second 40-sample sub-block is coded, indices 0
1430 - 107 correspond to the base codebook, 108 - 127 correspond to the
1431 augmented base codebook, 128 - 235 correspond to the expanded
1432 codebook, and indices 236 - 255 correspond to the augmented expanded
1433 codebook. The indices are divided in the same fashion for all stages
1434 in the example. Only in the case of coding the first 40-sample sub-
1435 block is there a difference between stages (see Table 3.1).
1437 3.6.4.1. Codebook Search at Each Stage
1439 The codebooks are searched to find the best match to the target at
1440 each stage. When the best match is found, the target is updated and
1441 the next-stage search is started. The three chosen codebook vectors
1442 and their corresponding gains constitute the encoded sub-block. The
1443 best match is decided by the following three criteria:
1445 1. Compute the measure
1447 (target*cbvec)^2 / ||cbvec||^2
1449 for all codebook vectors, cbvec, and choose the codebook vector
1450 maximizing the measure. The expression (target*cbvec) is the dot
1451 product between the target vector to be coded and the codebook vector
1452 for which we compute the measure. The norm, ||x||, is defined as the
1453 square root of (x*x).
1458 Andersen, et al. Experimental [Page 26]
1460 RFC 3951 Internet Low Bit Rate Codec December 2004
1463 2. The absolute value of the gain, corresponding to the chosen
1464 codebook vector, cbvec, must be smaller than a fixed limit,
1465 CB_MAXGAIN=1.3:
1467 |gain| < CB_MAXGAIN
1469 where the gain is computed in the following way:
1471 gain = (target*cbvec) / ||cbvec||^2
1473 3. For the first stage, the dot product of the chosen codebook vector
1474 and target must be positive:
1476 target*cbvec > 0
1478 In practice the above criteria are used in a sequential search
1479 through all codebook vectors. The best match is found by registering
1480 a new max measure and index whenever the previously registered max
1481 measure is surpassed and all other criteria are fulfilled. If none
1482 of the codebook vectors fulfill (2) and (3), the first codebook
1483 vector is selected.
1485 3.6.4.2. Gain Quantization at Each Stage
1487 The gain follows as a result of the computation
1489 gain = (target*cbvec) / ||cbvec||^2
1491 for the optimal codebook vector found by the procedure in section
1492 3.6.4.1.
1494 The three stages quantize the gain, using 5, 4, and 3 bits,
1495 respectively. In the first stage, the gain is limited to positive
1496 values. This gain is quantized by finding the nearest value in the
1497 quantization table gain_sq5Tbl.
1499 gain_sq5Tbl[32]={0.037476, 0.075012, 0.112488, 0.150024, 0.187500,
1500 0.224976, 0.262512, 0.299988, 0.337524, 0.375000,
1501 0.412476, 0.450012, 0.487488, 0.525024, 0.562500,
1502 0.599976, 0.637512, 0.674988, 0.712524, 0.750000,
1503 0.787476, 0.825012, 0.862488, 0.900024, 0.937500,
1504 0.974976, 1.012512, 1.049988, 1.087524, 1.125000,
1505 1.162476, 1.200012}
1507 The gains of the subsequent two stages can be either positive or
1508 negative. The gains are quantized by using a quantization table
1509 times a scale factor. The second stage uses the table gain_sq4Tbl,
1510 and the third stage uses gain_sq3Tbl. The scale factor equates 0.1
1514 Andersen, et al. Experimental [Page 27]
1516 RFC 3951 Internet Low Bit Rate Codec December 2004
1519 or the absolute value of the quantized gain representation value
1520 obtained in the previous stage, whichever is larger. Again, the
1521 resulting gain index is the index to the nearest value of the
1522 quantization table times the scale factor.
1524 gainQ = scaleFact * gain_sqXTbl[index]
1526 gain_sq4Tbl[16]={-1.049988, -0.900024, -0.750000, -0.599976,
1527 -0.450012, -0.299988, -0.150024, 0.000000, 0.150024,
1528 0.299988, 0.450012, 0.599976, 0.750000, 0.900024,
1529 1.049988, 1.200012}
1531 gain_sq3Tbl[8]={-1.000000, -0.659973, -0.330017,0.000000,
1532 0.250000, 0.500000, 0.750000, 1.00000}
1534 3.6.4.3. Preparation of Target for Next Stage
1536 Before performing the search for the next stage, the perceptually
1537 weighted target vector is updated by subtracting from it the selected
1538 codebook vector (from the perceptually weighted codebook) times the
1539 corresponding quantized gain.
1541 target[i] = target[i] - gainQ * selected_vec[i];
1543 A reference implementation of the codebook encoding is found in
1544 Appendix A.34.
1546 3.7. Gain Correction Encoding
1548 The start state is quantized in a relatively model independent manner
1549 using 3 bits per sample. In contrast, the remaining parts of the
1550 block are encoded by using an adaptive codebook. This codebook will
1551 produce high matching accuracy whenever there is a high correlation
1552 between the target and the best codebook vector. For unvoiced speech
1553 segments and background noises, this is not necessarily so, which,
1554 due to the nature of the squared error criterion, results in a coded
1555 signal with less power than the target signal. As the coded start
1556 state has good power matching to the target, the result is a power
1557 fluctuation within the encoded frame. Perceptually, the main problem
1558 with this is that the time envelope of the signal energy becomes
1559 unsteady. To overcome this problem, the gains for the codebooks are
1560 re-scaled after the codebook encoding by searching for a new gain
1561 factor for the first stage codebook that provides better power
1562 matching.
1564 First, the energy for the target signal, tene, is computed along with
1565 the energy for the coded signal, cene, given by the addition of the
1566 three gain scaled codebook vectors. Because the gains of the second
1570 Andersen, et al. Experimental [Page 28]
1572 RFC 3951 Internet Low Bit Rate Codec December 2004
1575 and third stage scale with the gain of the first stage, when the
1576 first stage gain is changed from gain[0] to gain_sq5Tbl[i] the energy
1577 of the coded signal changes from cene to
1579 cene*(gain_sq5Tbl[i]*gain_sq5Tbl[i])/(gain[0]*gain[0])
1581 where gain[0] is the gain for the first stage found in the original
1582 codebook search. A refined search is performed by testing the gain
1583 indices i=0 to 31, and as long as the new codebook energy as given
1584 above is less than tene, the gain index for stage 1 is increased. A
1585 restriction is applied so that the new gain value for stage 1 cannot
1586 be more than two times higher than the original value found in the
1587 codebook search. Note that by using this method we do not change the
1588 shape of the encoded vector, only the gain or amplitude.
1590 3.8. Bitstream Definition
1592 The total number of bits used to describe one frame of 20 ms speech
1593 is 304, which fits in 38 bytes and results in a bit rate of 15.20
1594 kbit/s. For the case of a frame length of 30 ms speech, the total
1595 number of bits used is 400, which fits in 50 bytes and results in a
1596 bit rate of 13.33 kbit/s. In the bitstream definition, the bits are
1597 distributed into three classes according to their bit error or loss
1598 sensitivity. The most sensitive bits (class 1) are placed first in
1599 the bitstream for each frame. The less sensitive bits (class 2) are
1600 placed after the class 1 bits. The least sensitive bits (class 3)
1601 are placed at the end of the bitstream for each frame.
1603 In the 20/30 ms frame length cases for each class, the following hold
1604 true: The class 1 bits occupy a total of 6/8 bytes (48/64 bits), the
1605 class 2 bits occupy 8/12 bytes (64/96 bits), and the class 3 bits
1606 occupy 24/30 bytes (191/239 bits). This distribution of the bits
1607 enables the use of uneven level protection (ULP) as is exploited in
1608 the payload format definition for iLBC [1]. The detailed bit
1609 allocation is shown in the table below. When a quantization index is
1610 distributed between more classes, the more significant bits belong to
1611 the lowest class.
1626 Andersen, et al. Experimental [Page 29]
1628 RFC 3951 Internet Low Bit Rate Codec December 2004
1631 Bitstream structure:
1633 ------------------------------------------------------------------+
1634 Parameter | Bits Class <1,2,3> |
1635 | 20 ms frame | 30 ms frame |
1636 ----------------------------------+---------------+---------------+
1637 Split 1 | 6 <6,0,0> | 6 <6,0,0> |
1638 LSF 1 Split 2 | 7 <7,0,0> | 7 <7,0,0> |
1639 LSF Split 3 | 7 <7,0,0> | 7 <7,0,0> |
1640 ------------------+---------------+---------------+
1641 Split 1 | NA (Not Appl.)| 6 <6,0,0> |
1642 LSF 2 Split 2 | NA | 7 <7,0,0> |
1643 Split 3 | NA | 7 <7,0,0> |
1644 ------------------+---------------+---------------+
1645 Sum | 20 <20,0,0> | 40 <40,0,0> |
1646 ----------------------------------+---------------+---------------+
1647 Block Class | 2 <2,0,0> | 3 <3,0,0> |
1648 ----------------------------------+---------------+---------------+
1649 Position 22 sample segment | 1 <1,0,0> | 1 <1,0,0> |
1650 ----------------------------------+---------------+---------------+
1651 Scale Factor State Coder | 6 <6,0,0> | 6 <6,0,0> |
1652 ----------------------------------+---------------+---------------+
1653 Sample 0 | 3 <0,1,2> | 3 <0,1,2> |
1654 Quantized Sample 1 | 3 <0,1,2> | 3 <0,1,2> |
1655 Residual : | : : | : : |
1656 State : | : : | : : |
1657 Samples : | : : | : : |
1658 Sample 56 | 3 <0,1,2> | 3 <0,1,2> |
1659 Sample 57 | NA | 3 <0,1,2> |
1660 ------------------+---------------+---------------+
1661 Sum | 171 <0,57,114>| 174 <0,58,116>|
1662 ----------------------------------+---------------+---------------+
1663 Stage 1 | 7 <6,0,1> | 7 <4,2,1> |
1664 CB for 22/23 Stage 2 | 7 <0,0,7> | 7 <0,0,7> |
1665 sample block Stage 3 | 7 <0,0,7> | 7 <0,0,7> |
1666 ------------------+---------------+---------------+
1667 Sum | 21 <6,0,15> | 21 <4,2,15> |
1668 ----------------------------------+---------------+---------------+
1669 Stage 1 | 5 <2,0,3> | 5 <1,1,3> |
1670 Gain for 22/23 Stage 2 | 4 <1,1,2> | 4 <1,1,2> |
1671 sample block Stage 3 | 3 <0,0,3> | 3 <0,0,3> |
1672 ------------------+---------------+---------------+
1673 Sum | 12 <3,1,8> | 12 <2,2,8> |
1674 ----------------------------------+---------------+---------------+
1675 Stage 1 | 8 <7,0,1> | 8 <6,1,1> |
1676 sub-block 1 Stage 2 | 7 <0,0,7> | 7 <0,0,7> |
1677 Stage 3 | 7 <0,0,7> | 7 <0,0,7> |
1678 ------------------+---------------+---------------+
1682 Andersen, et al. Experimental [Page 30]
1684 RFC 3951 Internet Low Bit Rate Codec December 2004
1687 Stage 1 | 8 <0,0,8> | 8 <0,7,1> |
1688 sub-block 2 Stage 2 | 8 <0,0,8> | 8 <0,0,8> |
1689 Indices Stage 3 | 8 <0,0,8> | 8 <0,0,8> |
1690 for CB ------------------+---------------+---------------+
1691 sub-blocks Stage 1 | NA | 8 <0,7,1> |
1692 sub-block 3 Stage 2 | NA | 8 <0,0,8> |
1693 Stage 3 | NA | 8 <0,0,8> |
1694 ------------------+---------------+---------------+
1695 Stage 1 | NA | 8 <0,7,1> |
1696 sub-block 4 Stage 2 | NA | 8 <0,0,8> |
1697 Stage 3 | NA | 8 <0,0,8> |
1698 ------------------+---------------+---------------+
1699 Sum | 46 <7,0,39> | 94 <6,22,66> |
1700 ----------------------------------+---------------+---------------+
1701 Stage 1 | 5 <1,2,2> | 5 <1,2,2> |
1702 sub-block 1 Stage 2 | 4 <1,1,2> | 4 <1,2,1> |
1703 Stage 3 | 3 <0,0,3> | 3 <0,0,3> |
1704 ------------------+---------------+---------------+
1705 Stage 1 | 5 <1,1,3> | 5 <0,2,3> |
1706 sub-block 2 Stage 2 | 4 <0,2,2> | 4 <0,2,2> |
1707 Stage 3 | 3 <0,0,3> | 3 <0,0,3> |
1708 Gains for ------------------+---------------+---------------+
1709 sub-blocks Stage 1 | NA | 5 <0,1,4> |
1710 sub-block 3 Stage 2 | NA | 4 <0,1,3> |
1711 Stage 3 | NA | 3 <0,0,3> |
1712 ------------------+---------------+---------------+
1713 Stage 1 | NA | 5 <0,1,4> |
1714 sub-block 4 Stage 2 | NA | 4 <0,1,3> |
1715 Stage 3 | NA | 3 <0,0,3> |
1716 ------------------+---------------+---------------+
1717 Sum | 24 <3,6,15> | 48 <2,12,34> |
1718 ----------------------------------+---------------+---------------+
1719 Empty frame indicator | 1 <0,0,1> | 1 <0,0,1> |
1720 -------------------------------------------------------------------
1721 SUM 304 <48,64,192> 400 <64,96,240>
1723 Table 3.2. The bitstream definition for iLBC for both the 20 ms
1724 frame size mode and the 30 ms frame size mode.
1726 When packetized into the payload, the bits MUST be sorted as follows:
1727 All the class 1 bits in the order (from top to bottom) as specified
1728 in the table, all the class 2 bits (from top to bottom), and all the
1729 class 3 bits in the same sequential order. The last bit, the empty
1730 frame indicator, SHOULD be set to zero by the encoder. If this bit
1731 is set to 1 the decoder SHOULD treat the data as a lost frame. For
1732 example, this bit can be set to 1 to indicate lost frame for file
1733 storage format, as in [1].
1738 Andersen, et al. Experimental [Page 31]
1740 RFC 3951 Internet Low Bit Rate Codec December 2004
1743 4. Decoder Principles
1745 This section describes the principles of each component of the
1746 decoder algorithm.
1748 +-------------+ +--------+ +---------------+
1749 payload -> | 1. Get para | -> | 2. LPC | -> | 3. Sc Dequant | ->
1750 +-------------+ +--------+ +---------------+
1752 +-------------+ +------------------+
1753 -> | 4. Mem setup| -> | 5. Construct res |------->
1754 | +-------------+ +------------------- |
1755 ---------<-----------<-----------<------------
1756 Sub-frame 0...2/4 (20 ms/30 ms)
1758 +----------------+ +----------+
1759 -> | 6. Enhance res | -> | 7. Synth | ------------>
1760 +----------------+ +----------+
1762 +-----------------+
1763 -> | 8. Post Process | ----------------> decoded speech
1764 +-----------------+
1766 Figure 4.1. Flow chart of the iLBC decoder. If a frame was lost,
1767 steps 1 to 5 SHOULD be replaced by a PLC algorithm.
1769 1. Extract the parameters from the bitstream.
1771 2. Decode the LPC and interpolate (section 4.1).
1773 3. Construct the 57/58-sample start state (section 4.2).
1775 4. Set up the memory by using data from the decoded residual. This
1776 memory is used for codebook construction. For blocks preceding
1777 the start state, both the decoded residual and the target are time
1778 reversed. Sub-frames are decoded in the same order as they were
1779 encoded.
1781 5. Construct the residuals of this sub-frame (gain[0]*cbvec[0] +
1782 gain[1]*cbvec[1] + gain[2]*cbvec[2]). Repeat 4 and 5 until the
1783 residual of all sub-blocks has been constructed.
1785 6. Enhance the residual with the post filter (section 4.6).
1787 7. Synthesis of the residual (section 4.7).
1789 8. Post process with HP filter, if desired (section 4.8).
1794 Andersen, et al. Experimental [Page 32]
1796 RFC 3951 Internet Low Bit Rate Codec December 2004
1799 4.1. LPC Filter Reconstruction
1801 The decoding of the LP filter parameters is very straightforward.
1802 For a set of three/six indices, the corresponding LSF vector(s) are
1803 found by simple table lookup. For each of the LSF vectors, the three
1804 split vectors are concatenated to obtain qlsf1 and qlsf2,
1805 respectively (in the 20 ms mode only one LSF vector, qlsf, is
1806 constructed). The next step is the stability check described in
1807 section 3.2.5 followed by the interpolation scheme described in
1808 section 3.2.6 (3.2.7 for 20 ms frames). The only difference is that
1809 only the quantized LSFs are known at the decoder, and hence the
1810 unquantized LSFs are not processed.
1812 A reference implementation of the LPC filter reconstruction is given
1813 in Appendix A.36.
1815 4.2. Start State Reconstruction
1817 The scalar encoded STATE_SHORT_LEN=58 (STATE_SHORT_LEN=57 in the 20
1818 ms mode) state samples are reconstructed by 1) forming a set of
1819 samples (by table lookup) from the index stream idxVec[n], 2)
1820 multiplying the set with 1/scal=(10^qmax)/4.5, 3) time reversing the
1821 57/58 samples, 4) filtering the time reversed block with the
1822 dispersion (all-pass) filter used in the encoder (as described in
1823 section 3.5.2); this compensates for the phase distortion of the
1824 earlier filter operation, and 5 reversing the 57/58 samples from the
1825 previous step.
1827 in(0..(STATE_SHORT_LEN-1)) = time reversed samples from table
1828 look-up,
1829 idxVecDec((STATE_SHORT_LEN-1)..0)
1831 in(STATE_SHORT_LEN..(2*STATE_SHORT_LEN-1)) = 0
1833 Pk(z) = A~rk(z)/A~k(z), where
1834 ___
1835 \
1836 A~rk(z)= z^(-LPC_FILTERORDER) + > a~ki*z^(i-(LPC_FILTERORDER-1))
1837 /__
1838 i=0...(LPC_FILTERORDER-1)
1840 and A~k(z) is taken from the block where the start state begins
1842 in -> Pk(z) -> filtered
1844 out(k) = filtered(STATE_SHORT_LEN-1-k) +
1845 filtered(2*STATE_SHORT_LEN-1-k),
1846 k=0..(STATE_SHORT_LEN-1)
1850 Andersen, et al. Experimental [Page 33]
1852 RFC 3951 Internet Low Bit Rate Codec December 2004
1855 The remaining 23/22 samples in the state are reconstructed by the
1856 same adaptive codebook technique described in section 4.3. The
1857 location bit determines whether these are the first or the last 23/22
1858 samples of the 80-sample state vector. If the remaining 23/22
1859 samples are the first samples, then the scalar encoded
1860 STATE_SHORT_LEN state samples are time-reversed before initialization
1861 of the adaptive codebook memory vector.
1863 A reference implementation of the start state reconstruction is given
1864 in Appendix A.44.
1866 4.3. Excitation Decoding Loop
1868 The decoding of the LPC excitation vector proceeds in the same order
1869 in which the residual was encoded at the encoder. That is, after the
1870 decoding of the entire 80-sample state vector, the forward sub-blocks
1871 (corresponding to samples occurring after the state vector samples)
1872 are decoded, and then the backward sub-blocks (corresponding to
1873 samples occurring before the state vector) are decoded, resulting in
1874 a fully decoded block of excitation signal samples.
1876 In particular, each sub-block is decoded by using the multistage
1877 adaptive codebook decoding module described in section 4.4. This
1878 module relies upon an adaptive codebook memory constructed before
1879 each run of the adaptive codebook decoding. The construction of the
1880 adaptive codebook memory in the decoder is identical to the method
1881 outlined in section 3.6.3, except that it is done on the codebook
1882 memory without perceptual weighting.
1884 For the initial forward sub-block, the last STATE_LEN=80 samples of
1885 the length CB_LMEM=147 adaptive codebook memory are filled with the
1886 samples of the state vector. For subsequent forward sub-blocks, the
1887 first SUBL=40 samples of the adaptive codebook memory are discarded,
1888 the remaining samples are shifted by SUBL samples toward the
1889 beginning of the vector, and the newly decoded SUBL=40 samples are
1890 placed at the end of the adaptive codebook memory. For backward
1891 sub-blocks, the construction is similar, except that every vector of
1892 samples involved is first time reversed.
1894 A reference implementation of the excitation decoding loop is found
1895 in Appendix A.5.
1906 Andersen, et al. Experimental [Page 34]
1908 RFC 3951 Internet Low Bit Rate Codec December 2004
1911 4.4. Multistage Adaptive Codebook Decoding
1913 The Multistage Adaptive Codebook Decoding module is used at both the
1914 sender (encoder) and the receiver (decoder) ends to produce a
1915 synthetic signal in the residual domain that is eventually used to
1916 produce synthetic speech. The module takes the index values used to
1917 construct vectors that are scaled and summed together to produce a
1918 synthetic signal that is the output of the module.
1920 4.4.1. Construction of the Decoded Excitation Signal
1922 The unpacked index values provided at the input to the module are
1923 references to extended codebooks, which are constructed as described
1924 in section 3.6.3, except that they are based on the codebook memory
1925 without the perceptual weighting. The unpacked three indices are
1926 used to look up three codebook vectors. The unpacked three gain
1927 indices are used to decode the corresponding 3 gains. In this
1928 decoding, the successive rescaling, as described in section 3.6.4.2,
1929 is applied.
1931 A reference implementation of the adaptive codebook decoding is
1932 listed in Appendix A.32.
1934 4.5. Packet Loss Concealment
1936 If packet loss occurs, the decoder receives a signal saying that
1937 information regarding a block is lost. For such blocks it is
1938 RECOMMENDED to use a Packet Loss Concealment (PLC) unit to create a
1939 decoded signal that masks the effect of that packet loss. In the
1940 following we will describe an example of a PLC unit that can be used
1941 with the iLBC codec. As the PLC unit is used only at the decoder,
1942 the PLC unit does not affect interoperability between
1943 implementations. Other PLC implementations MAY therefore be used.
1945 The PLC described operates on the LP filters and the excitation
1946 signals and is based on the following principles:
1948 4.5.1. Block Received Correctly and Previous Block Also Received
1950 If the block is received correctly, the PLC only records state
1951 information of the current block that can be used in case the next
1952 block is lost. The LP filter coefficients for each sub-block and the
1953 entire decoded excitation signal are all saved in the decoder state
1954 structure. All of this information will be needed if the following
1955 block is lost.
1962 Andersen, et al. Experimental [Page 35]
1964 RFC 3951 Internet Low Bit Rate Codec December 2004
1967 4.5.2. Block Not Received
1969 If the block is not received, the block substitution is based on a
1970 pitch-synchronous repetition of the excitation signal, which is
1971 filtered by the last LP filter of the previous block. The previous
1972 block's information is stored in the decoder state structure.
1974 A correlation analysis is performed on the previous block's
1975 excitation signal in order to detect the amount of pitch periodicity
1976 and a pitch value. The correlation measure is also used to decide on
1977 the voicing level (the degree to which the previous block's
1978 excitation was a voiced or roughly periodic signal). The excitation
1979 in the previous block is used to create an excitation for the block
1980 to be substituted, such that the pitch of the previous block is
1981 maintained. Therefore, the new excitation is constructed in a
1982 pitch-synchronous manner. In order to avoid a buzzy-sounding
1983 substituted block, a random excitation is mixed with the new pitch
1984 periodic excitation, and the relative use of the two components is
1985 computed from the correlation measure (voicing level).
1987 For the block to be substituted, the newly constructed excitation
1988 signal is then passed through the LP filter to produce the speech
1989 that will be substituted for the lost block.
1991 For several consecutive lost blocks, the packet loss concealment
1992 continues in a similar manner. The correlation measure of the last
1993 block received is still used along with the same pitch value. The LP
1994 filters of the last block received are also used again. The energy
1995 of the substituted excitation for consecutive lost blocks is
1996 decreased, leading to a dampened excitation, and therefore to
1997 dampened speech.
1999 4.5.3. Block Received Correctly When Previous Block Not Received
2001 For the case in which a block is received correctly when the previous
2002 block was not, the correctly received block's directly decoded speech
2003 (based solely on the received block) is not used as the actual
2004 output. The reason for this is that the directly decoded speech does
2005 not necessarily smoothly merge into the synthetic speech generated
2006 for the previous lost block. If the two signals are not smoothly
2007 merged, an audible discontinuity is accidentally produced.
2008 Therefore, a correlation analysis between the two blocks of
2009 excitation signal (the excitation of the previous concealed block and
2010 that of the current received block) is performed to find the best
2011 phase match. Then a simple overlap-add procedure is performed to
2012 merge the previous excitation smoothly into the current block's
2013 excitation.
2018 Andersen, et al. Experimental [Page 36]
2020 RFC 3951 Internet Low Bit Rate Codec December 2004
2023 The exact implementation of the packet loss concealment does not
2024 influence interoperability of the codec.
2026 A reference implementation of the packet loss concealment is
2027 suggested in Appendix A.14. Exact compliance with this suggested
2028 algorithm is not needed for a reference implementation to be fully
2029 compatible with the overall codec specification.
2031 4.6. Enhancement
2033 The decoder contains an enhancement unit that operates on the
2034 reconstructed excitation signal. The enhancement unit increases the
2035 perceptual quality of the reconstructed signal by reducing the
2036 speech-correlated noise in the voiced speech segments. Compared to
2037 traditional postfilters, the enhancer has an advantage in that it can
2038 only modify the excitation signal slightly. This means that there is
2039 no risk of over enhancement. The enhancer works very similarly for
2040 both the 20 ms frame size mode and the 30 ms frame size mode.
2042 For the mode with 20 ms frame size, the enhancer uses a memory of six
2043 80-sample excitation blocks prior in time plus the two new 80-sample
2044 excitation blocks. For each block of 160 new unenhanced excitation
2045 samples, 160 enhanced excitation samples are produced. The enhanced
2046 excitation is 40-sample delayed compared to the unenhanced
2047 excitation, as the enhancer algorithm uses lookahead.
2049 For the mode with 30 ms frame size, the enhancer uses a memory of
2050 five 80-sample excitation blocks prior in time plus the three new
2051 80-sample excitation blocks. For each block of 240 new unenhanced
2052 excitation samples, 240 enhanced excitation samples are produced.
2053 The enhanced excitation is 80-sample delayed compared to the
2054 unenhanced excitation, as the enhancer algorithm uses lookahead.
2056 Outline of Enhancer
2058 The speech enhancement unit operates on sub-blocks of 80 samples,
2059 which means that there are two/three 80 sample sub-blocks per frame.
2060 Each of these two/three sub-blocks is enhanced separately, but in an
2061 analogous manner.
2074 Andersen, et al. Experimental [Page 37]
2076 RFC 3951 Internet Low Bit Rate Codec December 2004
2079 unenhanced residual
2080 |
2081 | +---------------+ +--------------+
2082 +-> | 1. Pitch Est | -> | 2. Find PSSQ | -------->
2083 +---------------+ | +--------------+
2084 +-----<-------<------<--+
2085 +------------+ enh block 0..1/2 |
2086 -> | 3. Smooth | |
2087 +------------+ |
2088 \ |
2089 /\ |
2090 / \ Already |
2091 / 4. \----------->----------->-----------+ |
2092 \Crit/ Fulfilled | |
2093 \? / v |
2094 \/ | |
2095 \ +-----------------+ +---------+ | |
2096 Not +->| 5. Use Constr. | -> | 6. Mix | ----->
2097 Fulfilled +-----------------+ +---------+
2099 ---------------> enhanced residual
2101 Figure 4.2. Flow chart of the enhancer.
2103 1. Pitch estimation of each of the two/three new 80-sample blocks.
2105 2. Find the pitch-period-synchronous sequence n (for block k) by a
2106 search around the estimated pitch value. Do this for n=1,2,3,
2107 -1,-2,-3.
2109 3. Calculate the smoothed residual generated by the six pitch-
2110 period-synchronous sequences from prior step.
2112 4. Check if the smoothed residual satisfies the criterion (section
2113 4.6.4).
2115 5. Use constraint to calculate mixing factor (section 4.6.5).
2117 6. Mix smoothed signal with unenhanced residual (pssq(n) n=0).
2119 The main idea of the enhancer is to find three 80 sample blocks
2120 before and three 80-sample blocks after the analyzed unenhanced sub-
2121 block and to use these to improve the quality of the excitation in
2122 that sub-block. The six blocks are chosen so that they have the
2123 highest possible correlation with the unenhanced sub-block that is
2124 being enhanced. In other words, the six blocks are pitch-period-
2125 synchronous sequences to the unenhanced sub-block.
2130 Andersen, et al. Experimental [Page 38]
2132 RFC 3951 Internet Low Bit Rate Codec December 2004
2135 A linear combination of the six pitch-period-synchronous sequences is
2136 calculated that approximates the sub-block. If the squared error
2137 between the approximation and the unenhanced sub-block is small
2138 enough, the enhanced residual is set equal to this approximation.
2139 For the cases when the squared error criterion is not fulfilled, a
2140 linear combination of the approximation and the unenhanced residual
2141 forms the enhanced residual.
2143 4.6.1. Estimating the Pitch
2145 Pitch estimates are needed to determine the locations of the pitch-
2146 period-synchronous sequences in a complexity-efficient way. For each
2147 of the new two/three sub-blocks, a pitch estimate is calculated by
2148 finding the maximum correlation in the range from lag 20 to lag 120.
2149 These pitch estimates are used to narrow down the search for the best
2150 possible pitch-period-synchronous sequences.
2152 4.6.2. Determination of the Pitch-Synchronous Sequences
2154 Upon receiving the pitch estimates from the prior step, the enhancer
2155 analyzes and enhances one 80-sample sub-block at a time. The pitch-
2156 period-synchronous-sequences pssq(n) can be viewed as vectors of
2157 length 80 samples each shifted n*lag samples from the current sub-
2158 block. The six pitch-period-synchronous-sequences, pssq(-3) to
2159 pssq(-1) and pssq(1) to pssq(3), are found one at a time by the steps
2160 below:
2162 1) Calculate the estimate of the position of the pssq(n). For
2163 pssq(n) in front of pssq(0) (n > 0), the location of the pssq(n)
2164 is estimated by moving one pitch estimate forward in time from the
2165 exact location of pssq(n-1). Similarly, pssq(n) behind pssq(0) (n
2166 < 0) is estimated by moving one pitch estimate backward in time
2167 from the exact location of pssq(n+1). If the estimated pssq(n)
2168 vector location is totally within the enhancer memory (Figure
2169 4.3), steps 2, 3, and 4 are performed, otherwise the pssq(n) is
2170 set to zeros.
2172 2) Compute the correlation between the unenhanced excitation and
2173 vectors around the estimated location interval of pssq(n). The
2174 correlation is calculated in the interval estimated location +/- 2
2175 samples. This results in five correlation values.
2177 3) The five correlation values are upsampled by a factor of 4, by
2178 using four simple upsampling filters (MA filters with coefficients
2179 upsFilter1.. upsFilter4). Within these the maximum value is
2180 found, which specifies the best pitch-period with a resolution of
2181 a quarter of a sample.
2186 Andersen, et al. Experimental [Page 39]
2188 RFC 3951 Internet Low Bit Rate Codec December 2004
2191 upsFilter1[7]={0.000000 0.000000 0.000000 1.000000
2192 0.000000 0.000000 0.000000}
2193 upsFilter2[7]={0.015625 -0.076904 0.288330 0.862061
2194 -0.106445 0.018799 -0.015625}
2195 upsFilter3[7]={0.023682 -0.124268 0.601563 0.601563
2196 -0.124268 0.023682 -0.023682}
2197 upsFilter4[7]={0.018799 -0.106445 0.862061 0.288330
2198 -0.076904 0.015625 -0.018799}
2200 4) Generate the pssq(n) vector by upsampling of the excitation memory
2201 and extracting the sequence that corresponds to the lag delay that
2202 was calculated in prior step.
2204 With the steps above, all the pssq(n) can be found in an iterative
2205 manner, first moving backward in time from pssq(0) and then forward
2206 in time from pssq(0).
2209 0 159 319 479 639
2210 +---------------------------------------------------------------+
2211 | -5 | -4 | -3 | -2 | -1 | 0 | 1 | 2 |
2212 +---------------------------------------------------------------+
2213 |pssq 0 |
2214 |pssq -1| |pssq 1 |
2215 |pssq -2| |pssq 2 |
2216 |pssq -3| |pssq 3 |
2218 Figure 4.3. Enhancement for 20 ms frame size.
2220 Figure 4.3 depicts pitch-period-synchronous sequences in the
2221 enhancement of the first 80 sample block in the 20 ms frame size
2222 mode. The unenhanced signal input is stored in the last two sub-
2223 blocks (1 - 2), and the six other sub-blocks contain unenhanced
2224 residual prior-in-time. We perform the enhancement algorithm on two
2225 blocks of 80 samples, where the first of the two blocks consists of
2226 the last 40 samples of sub-block 0 and the first 40 samples of sub-
2227 block 1. The second 80-sample block consists of the last 40 samples
2228 of sub-block 1 and the first 40 samples of sub-block 2.
2242 Andersen, et al. Experimental [Page 40]
2244 RFC 3951 Internet Low Bit Rate Codec December 2004
2247 0 159 319 479 639
2248 +---------------------------------------------------------------+
2249 | -4 | -3 | -2 | -1 | 0 | 1 | 2 | 3 |
2250 +---------------------------------------------------------------+
2251 |pssq 0 |
2252 |pssq -1| |pssq 1 |
2253 |pssq -2| |pssq 2 |
2254 |pssq -3| |pssq 3 |
2256 Figure 4.4. Enhancement for 30 ms frame size.
2258 Figure 4.4 depicts pitch-period-synchronous sequences in the
2259 enhancement of the first 80-sample block in the 30 ms frame size
2260 mode. The unenhanced signal input is stored in the last three sub-
2261 blocks (1 - 3). The five other sub-blocks contain unenhanced
2262 residual prior-in-time. The enhancement algorithm is performed on
2263 the three 80 sample sub-blocks 0, 1, and 2.
2265 4.6.3. Calculation of the Smoothed Excitation
2267 A linear combination of the six pssq(n) (n!=0) form a smoothed
2268 approximation, z, of pssq(0). Most of the weight is put on the
2269 sequences that are close to pssq(0), as these are likely to be most
2270 similar to pssq(0). The smoothed vector is also rescaled so that the
2271 energy of z is the same as the energy of pssq(0).
2273 ___
2274 \
2275 y = > pssq(i) * pssq_weight(i)
2276 /__
2277 i=-3,-2,-1,1,2,3
2279 pssq_weight(i) = 0.5*(1-cos(2*pi*(i+4)/(2*3+2)))
2281 z = C * y, where C = ||pssq(0)||/||y||
2283 4.6.4. Enhancer Criterion
2285 The criterion of the enhancer is that the enhanced excitation is not
2286 allowed to differ much from the unenhanced excitation. This
2287 criterion is checked for each 80-sample sub-block.
2289 e < (b * ||pssq(0)||^2), where b=0.05 and (Constraint 1)
2291 e = (pssq(0)-z)*(pssq(0)-z), and "*" means the dot product
2298 Andersen, et al. Experimental [Page 41]
2300 RFC 3951 Internet Low Bit Rate Codec December 2004
2303 4.6.5. Enhancing the excitation
2305 From the criterion in the previous section, it is clear that the
2306 excitation is not allowed to change much. The purpose of this
2307 constraint is to prevent the creation of an enhanced signal
2308 significantly different from the original signal. This also means
2309 that the constraint limits the numerical size of the errors that the
2310 enhancement procedure can make. That is especially important in
2311 unvoiced segments and background noise segments for which increased
2312 periodicity could lead to lower perceived quality.
2314 When the constraint in the prior section is not met, the enhanced
2315 residual is instead calculated through a constrained optimization by
2316 using the Lagrange multiplier technique. The new constraint is that
2318 e = (b * ||pssq(0)||^2) (Constraint 2)
2320 We distinguish two solution regions for the optimization: 1) the
2321 region where the first constraint is fulfilled and 2) the region
2322 where the first constraint is not fulfilled and the second constraint
2323 must be used.
2325 In the first case, where the second constraint is not needed, the
2326 optimized re-estimated vector is simply z, the energy-scaled version
2327 of y.
2329 In the second case, where the second constraint is activated and
2330 becomes an equality constraint, we have
2332 z= A*y + B*pssq(0)
2334 where
2336 A = sqrt((b-b^2/4)*(w00*w00)/ (w11*w00 + w10*w10)) and
2338 w11 = pssq(0)*pssq(0)
2339 w00 = y*y
2340 w10 = y*pssq(0) (* symbolizes the dot product)
2342 and
2344 B = 1 - b/2 - A * w10/w00
2346 Appendix A.16 contains a listing of a reference implementation for
2347 the enhancement method.
2354 Andersen, et al. Experimental [Page 42]
2356 RFC 3951 Internet Low Bit Rate Codec December 2004
2359 4.7. Synthesis Filtering
2361 Upon decoding or PLC of the LP excitation block, the decoded speech
2362 block is obtained by running the decoded LP synthesis filter,
2363 1/A~k(z), over the block. The synthesis filters have to be shifted
2364 to compensate for the delay in the enhancer. For 20 ms frame size
2365 mode, they SHOULD be shifted one 40-sample sub-block, and for 30 ms
2366 frame size mode, they SHOULD be shifted two 40-sample sub-blocks.
2367 The LP coefficients SHOULD be changed at the first sample of every
2368 sub-block while keeping the filter state. For PLC blocks, one
2369 solution is to apply the last LP coefficients of the last decoded
2370 speech block for all sub-blocks.
2372 The reference implementation for the synthesis filtering can be found
2373 in Appendix A.48.
2375 4.8. Post Filtering
2377 If desired, the decoded block can be filtered by a high-pass filter.
2378 This removes the low frequencies of the decoded signal. A reference
2379 implementation of this, with cutoff at 65 Hz, is shown in Appendix
2380 A.30.
2382 5. Security Considerations
2384 This algorithm for the coding of speech signals is not subject to any
2385 known security consideration; however, its RTP payload format [1] is
2386 subject to several considerations, which are addressed there.
2387 Confidentiality of the media streams is achieved by encryption;
2388 therefore external mechanisms, such as SRTP [5], MAY be used for that
2389 purpose.
2391 6. Evaluation of the iLBC Implementations
2393 It is possible and suggested to evaluate certain iLBC implementation
2394 by utilizing methodology and tools available at
2395 http://www.ilbcfreeware.org/evaluation.html
2397 7. References
2399 7.1. Normative References
2401 [1] Duric, A. and S. Andersen, "Real-time Transport Protocol (RTP)
2402 Payload Format for internet Low Bit Rate Codec (iLBC) Speech",
2403 RFC 3952, December 2004.
2405 [2] Bradner, S., "Key words for use in RFCs to Indicate Requirement
2406 Levels", BCP 14, RFC 2119, March 1997.
2410 Andersen, et al. Experimental [Page 43]
2412 RFC 3951 Internet Low Bit Rate Codec December 2004
2415 [3] PacketCable(TM) Audio/Video Codecs Specification, Cable
2416 Television Laboratories, Inc.
2418 7.2. Informative References
2420 [4] ITU-T Recommendation G.711, available online from the ITU
2421 bookstore at http://www.itu.int.
2423 [5] Baugher, M., McGrew, D., Naslund, M., Carrara, E., and K. Norman,
2424 "The Secure Real Time Transport Protocol (SRTP)", RFC 3711, March
2425 2004.
2427 8. Acknowledgements
2429 This extensive work, besides listed authors, has the following
2430 authors, who could not have been listed among "official" authors (due
2431 to IESG restrictions in the number of authors who can be listed):
2433 Manohar N. Murthi (Department of Electrical and Computer
2434 Engineering, University of Miami), Fredrik Galschiodt, Julian
2435 Spittka, and Jan Skoglund (Global IP Sound).
2437 The authors are deeply indebted to the following people and thank
2438 them sincerely:
2440 Henry Sinnreich, Patrik Faltstrom, Alan Johnston, and Jean-
2441 Francois Mule for great support of the iLBC initiative and for
2442 valuable feedback and comments.
2444 Peter Vary, Frank Mertz, and Christoph Erdmann (RWTH Aachen);
2445 Vladimir Cuperman (Niftybox LLC); Thomas Eriksson (Chalmers Univ
2446 of Tech), and Gernot Kubin (TU Graz), for thorough review of the
2447 iLBC document and their valuable feedback and remarks.
2466 Andersen, et al. Experimental [Page 44]
2468 RFC 3951 Internet Low Bit Rate Codec December 2004
2471 APPENDIX A. Reference Implementation
2473 This appendix contains the complete c-code for a reference
2474 implementation of encoder and decoder for the specified codec.
2476 The c-code consists of the following files with highest-level
2477 functions:
2479 iLBC_test.c: main function for evaluation purpose
2480 iLBC_encode.h: encoder header
2481 iLBC_encode.c: encoder function
2482 iLBC_decode.h: decoder header
2483 iLBC_decode.c: decoder function
2485 The following files contain global defines and constants:
2487 iLBC_define.h: global defines
2488 constants.h: global constants header
2489 constants.c: global constants memory allocations
2491 The following files contain subroutines:
2493 anaFilter.h: lpc analysis filter header
2494 anaFilter.c: lpc analysis filter function
2495 createCB.h: codebook construction header
2496 createCB.c: codebook construction function
2497 doCPLC.h: packet loss concealment header
2498 doCPLC.c: packet loss concealment function
2499 enhancer.h: signal enhancement header
2500 enhancer.c: signal enhancement function
2501 filter.h: general filter header
2502 filter.c: general filter functions
2503 FrameClassify.h: start state classification header
2504 FrameClassify.c: start state classification function
2505 gainquant.h: gain quantization header
2506 gainquant.c: gain quantization function
2507 getCBvec.h: codebook vector construction header
2508 getCBvec.c: codebook vector construction function
2509 helpfun.h: general purpose header
2510 helpfun.c: general purpose functions
2511 hpInput.h: input high pass filter header
2512 hpInput.c: input high pass filter function
2513 hpOutput.h: output high pass filter header
2514 hpOutput.c: output high pass filter function
2515 iCBConstruct.h: excitation decoding header
2516 iCBConstruct.c: excitation decoding function
2517 iCBSearch.h: excitation encoding header
2518 iCBSearch.c: excitation encoding function
2522 Andersen, et al. Experimental [Page 45]
2524 RFC 3951 Internet Low Bit Rate Codec December 2004
2527 LPCdecode.h: lpc decoding header
2528 LPCdecode.c: lpc decoding function
2529 LPCencode.h: lpc encoding header
2530 LPCencode.c: lpc encoding function
2531 lsf.h: line spectral frequencies header
2532 lsf.c: line spectral frequencies functions
2533 packing.h: bitstream packetization header
2534 packing.c: bitstream packetization functions
2535 StateConstructW.h: state decoding header
2536 StateConstructW.c: state decoding functions
2537 StateSearchW.h: state encoding header
2538 StateSearchW.c: state encoding function
2539 syntFilter.h: lpc synthesis filter header
2540 syntFilter.c: lpc synthesis filter function
2542 The implementation is portable and should work on many different
2543 platforms. However, it is not difficult to optimize the
2544 implementation on particular platforms, an exercise left to the
2545 reader.
2547 A.1. iLBC_test.c
2549 /******************************************************************
2551 iLBC Speech Coder ANSI-C Source Code
2553 iLBC_test.c
2555 Copyright (C) The Internet Society (2004).
2556 All Rights Reserved.
2558 ******************************************************************/
2560 #include <math.h>
2561 #include <stdlib.h>
2562 #include <stdio.h>
2563 #include <string.h>
2564 #include "iLBC_define.h"
2565 #include "iLBC_encode.h"
2566 #include "iLBC_decode.h"
2568 /* Runtime statistics */
2569 #include <time.h>
2571 #define ILBCNOOFWORDS_MAX (NO_OF_BYTES_30MS/2)
2573 /*----------------------------------------------------------------*
2574 * Encoder interface function
2578 Andersen, et al. Experimental [Page 46]
2580 RFC 3951 Internet Low Bit Rate Codec December 2004
2583 *---------------------------------------------------------------*/
2585 short encode( /* (o) Number of bytes encoded */
2586 iLBC_Enc_Inst_t *iLBCenc_inst,
2587 /* (i/o) Encoder instance */
2588 short *encoded_data, /* (o) The encoded bytes */
2589 short *data /* (i) The signal block to encode*/
2590 ){
2591 float block[BLOCKL_MAX];
2592 int k;
2594 /* convert signal to float */
2596 for (k=0; k<iLBCenc_inst->blockl; k++)
2597 block[k] = (float)data[k];
2599 /* do the actual encoding */
2601 iLBC_encode((unsigned char *)encoded_data, block, iLBCenc_inst);
2604 return (iLBCenc_inst->no_of_bytes);
2605 }
2607 /*----------------------------------------------------------------*
2608 * Decoder interface function
2609 *---------------------------------------------------------------*/
2611 short decode( /* (o) Number of decoded samples */
2612 iLBC_Dec_Inst_t *iLBCdec_inst, /* (i/o) Decoder instance */
2613 short *decoded_data, /* (o) Decoded signal block*/
2614 short *encoded_data, /* (i) Encoded bytes */
2615 short mode /* (i) 0=PL, 1=Normal */
2616 ){
2617 int k;
2618 float decblock[BLOCKL_MAX], dtmp;
2620 /* check if mode is valid */
2622 if (mode<0 || mode>1) {
2623 printf("\nERROR - Wrong mode - 0, 1 allowed\n"); exit(3);}
2625 /* do actual decoding of block */
2627 iLBC_decode(decblock, (unsigned char *)encoded_data,
2628 iLBCdec_inst, mode);
2630 /* convert to short */
2634 Andersen, et al. Experimental [Page 47]
2636 RFC 3951 Internet Low Bit Rate Codec December 2004
2639 for (k=0; k<iLBCdec_inst->blockl; k++){
2640 dtmp=decblock[k];
2642 if (dtmp<MIN_SAMPLE)
2643 dtmp=MIN_SAMPLE;
2644 else if (dtmp>MAX_SAMPLE)
2645 dtmp=MAX_SAMPLE;
2646 decoded_data[k] = (short) dtmp;
2647 }
2649 return (iLBCdec_inst->blockl);
2650 }
2652 /*---------------------------------------------------------------*
2653 * Main program to test iLBC encoding and decoding
2654 *
2655 * Usage:
2656 * exefile_name.exe <infile> <bytefile> <outfile> <channel>
2657 *
2658 * <infile> : Input file, speech for encoder (16-bit pcm file)
2659 * <bytefile> : Bit stream output from the encoder
2660 * <outfile> : Output file, decoded speech (16-bit pcm file)
2661 * <channel> : Bit error file, optional (16-bit)
2662 * 1 - Packet received correctly
2663 * 0 - Packet Lost
2664 *
2665 *--------------------------------------------------------------*/
2667 int main(int argc, char* argv[])
2668 {
2670 /* Runtime statistics */
2672 float starttime;
2673 float runtime;
2674 float outtime;
2676 FILE *ifileid,*efileid,*ofileid, *cfileid;
2677 short data[BLOCKL_MAX];
2678 short encoded_data[ILBCNOOFWORDS_MAX], decoded_data[BLOCKL_MAX];
2679 int len;
2680 short pli, mode;
2681 int blockcount = 0;
2682 int packetlosscount = 0;
2684 /* Create structs */
2685 iLBC_Enc_Inst_t Enc_Inst;
2686 iLBC_Dec_Inst_t Dec_Inst;
2690 Andersen, et al. Experimental [Page 48]
2692 RFC 3951 Internet Low Bit Rate Codec December 2004
2695 /* get arguments and open files */
2697 if ((argc!=5) && (argc!=6)) {
2698 fprintf(stderr,
2699 "\n*-----------------------------------------------*\n");
2700 fprintf(stderr,
2701 " %s <20,30> input encoded decoded (channel)\n\n",
2702 argv[0]);
2703 fprintf(stderr,
2704 " mode : Frame size for the encoding/decoding\n");
2705 fprintf(stderr,
2706 " 20 - 20 ms\n");
2707 fprintf(stderr,
2708 " 30 - 30 ms\n");
2709 fprintf(stderr,
2710 " input : Speech for encoder (16-bit pcm file)\n");
2711 fprintf(stderr,
2712 " encoded : Encoded bit stream\n");
2713 fprintf(stderr,
2714 " decoded : Decoded speech (16-bit pcm file)\n");
2715 fprintf(stderr,
2716 " channel : Packet loss pattern, optional (16-bit)\n");
2717 fprintf(stderr,
2718 " 1 - Packet received correctly\n");
2719 fprintf(stderr,
2720 " 0 - Packet Lost\n");
2721 fprintf(stderr,
2722 "*-----------------------------------------------*\n\n");
2723 exit(1);
2724 }
2725 mode=atoi(argv[1]);
2726 if (mode != 20 && mode != 30) {
2727 fprintf(stderr,"Wrong mode %s, must be 20, or 30\n",
2728 argv[1]);
2729 exit(2);
2730 }
2731 if ( (ifileid=fopen(argv[2],"rb")) == NULL) {
2732 fprintf(stderr,"Cannot open input file %s\n", argv[2]);
2733 exit(2);}
2734 if ( (efileid=fopen(argv[3],"wb")) == NULL) {
2735 fprintf(stderr, "Cannot open encoded file %s\n",
2736 argv[3]); exit(1);}
2737 if ( (ofileid=fopen(argv[4],"wb")) == NULL) {
2738 fprintf(stderr, "Cannot open decoded file %s\n",
2739 argv[4]); exit(1);}
2740 if (argc==6) {
2741 if( (cfileid=fopen(argv[5],"rb")) == NULL) {
2742 fprintf(stderr, "Cannot open channel file %s\n",
2746 Andersen, et al. Experimental [Page 49]
2748 RFC 3951 Internet Low Bit Rate Codec December 2004
2751 argv[5]);
2752 exit(1);
2753 }
2754 } else {
2755 cfileid=NULL;
2756 }
2758 /* print info */
2760 fprintf(stderr, "\n");
2761 fprintf(stderr,
2762 "*---------------------------------------------------*\n");
2763 fprintf(stderr,
2764 "* *\n");
2765 fprintf(stderr,
2766 "* iLBC test program *\n");
2767 fprintf(stderr,
2768 "* *\n");
2769 fprintf(stderr,
2770 "* *\n");
2771 fprintf(stderr,
2772 "*---------------------------------------------------*\n");
2773 fprintf(stderr,"\nMode : %2d ms\n", mode);
2774 fprintf(stderr,"Input file : %s\n", argv[2]);
2775 fprintf(stderr,"Encoded file : %s\n", argv[3]);
2776 fprintf(stderr,"Output file : %s\n", argv[4]);
2777 if (argc==6) {
2778 fprintf(stderr,"Channel file : %s\n", argv[5]);
2779 }
2780 fprintf(stderr,"\n");
2782 /* Initialization */
2784 initEncode(&Enc_Inst, mode);
2785 initDecode(&Dec_Inst, mode, 1);
2787 /* Runtime statistics */
2789 starttime=clock()/(float)CLOCKS_PER_SEC;
2791 /* loop over input blocks */
2793 while (fread(data,sizeof(short),Enc_Inst.blockl,ifileid)==
2794 Enc_Inst.blockl) {
2796 blockcount++;
2798 /* encoding */
2802 Andersen, et al. Experimental [Page 50]
2804 RFC 3951 Internet Low Bit Rate Codec December 2004
2807 fprintf(stderr, "--- Encoding block %i --- ",blockcount);
2808 len=encode(&Enc_Inst, encoded_data, data);
2809 fprintf(stderr, "\r");
2811 /* write byte file */
2813 fwrite(encoded_data, sizeof(unsigned char), len, efileid);
2815 /* get channel data if provided */
2816 if (argc==6) {
2817 if (fread(&pli, sizeof(short), 1, cfileid)) {
2818 if ((pli!=0)&&(pli!=1)) {
2819 fprintf(stderr, "Error in channel file\n");
2820 exit(0);
2821 }
2822 if (pli==0) {
2823 /* Packet loss -> remove info from frame */
2824 memset(encoded_data, 0,
2825 sizeof(short)*ILBCNOOFWORDS_MAX);
2826 packetlosscount++;
2827 }
2828 } else {
2829 fprintf(stderr, "Error. Channel file too short\n");
2830 exit(0);
2831 }
2832 } else {
2833 pli=1;
2834 }
2836 /* decoding */
2838 fprintf(stderr, "--- Decoding block %i --- ",blockcount);
2840 len=decode(&Dec_Inst, decoded_data, encoded_data, pli);
2841 fprintf(stderr, "\r");
2843 /* write output file */
2845 fwrite(decoded_data,sizeof(short),len,ofileid);
2846 }
2848 /* Runtime statistics */
2850 runtime = (float)(clock()/(float)CLOCKS_PER_SEC-starttime);
2851 outtime = (float)((float)blockcount*(float)mode/1000.0);
2852 printf("\n\nLength of speech file: %.1f s\n", outtime);
2853 printf("Packet loss : %.1f%%\n",
2854 100.0*(float)packetlosscount/(float)blockcount);
2858 Andersen, et al. Experimental [Page 51]
2860 RFC 3951 Internet Low Bit Rate Codec December 2004
2863 printf("Time to run iLBC :");
2864 printf(" %.1f s (%.1f %% of realtime)\n\n", runtime,
2865 (100*runtime/outtime));
2867 /* close files */
2869 fclose(ifileid); fclose(efileid); fclose(ofileid);
2870 if (argc==6) {
2871 fclose(cfileid);
2872 }
2873 return(0);
2874 }
2876 A.2. iLBC_encode.h
2878 /******************************************************************
2880 iLBC Speech Coder ANSI-C Source Code
2882 iLBC_encode.h
2884 Copyright (C) The Internet Society (2004).
2885 All Rights Reserved.
2887 ******************************************************************/
2889 #ifndef __iLBC_ILBCENCODE_H
2890 #define __iLBC_ILBCENCODE_H
2892 #include "iLBC_define.h"
2894 short initEncode( /* (o) Number of bytes
2895 encoded */
2896 iLBC_Enc_Inst_t *iLBCenc_inst, /* (i/o) Encoder instance */
2897 int mode /* (i) frame size mode */
2898 );
2900 void iLBC_encode(
2902 unsigned char *bytes, /* (o) encoded data bits iLBC */
2903 float *block, /* (o) speech vector to
2904 encode */
2905 iLBC_Enc_Inst_t *iLBCenc_inst /* (i/o) the general encoder
2906 state */
2907 );
2909 #endif
2914 Andersen, et al. Experimental [Page 52]
2916 RFC 3951 Internet Low Bit Rate Codec December 2004
2919 A.3. iLBC_encode.c
2921 /******************************************************************
2923 iLBC Speech Coder ANSI-C Source Code
2925 iLBC_encode.c
2927 Copyright (C) The Internet Society (2004).
2928 All Rights Reserved.
2930 ******************************************************************/
2932 #include <math.h>
2933 #include <stdlib.h>
2934 #include <string.h>
2936 #include "iLBC_define.h"
2937 #include "LPCencode.h"
2938 #include "FrameClassify.h"
2939 #include "StateSearchW.h"
2940 #include "StateConstructW.h"
2941 #include "helpfun.h"
2942 #include "constants.h"
2943 #include "packing.h"
2944 #include "iCBSearch.h"
2945 #include "iCBConstruct.h"
2946 #include "hpInput.h"
2947 #include "anaFilter.h"
2948 #include "syntFilter.h"
2950 /*----------------------------------------------------------------*
2951 * Initiation of encoder instance.
2952 *---------------------------------------------------------------*/
2954 short initEncode( /* (o) Number of bytes
2955 encoded */
2956 iLBC_Enc_Inst_t *iLBCenc_inst, /* (i/o) Encoder instance */
2957 int mode /* (i) frame size mode */
2958 ){
2959 iLBCenc_inst->mode = mode;
2960 if (mode==30) {
2961 iLBCenc_inst->blockl = BLOCKL_30MS;
2962 iLBCenc_inst->nsub = NSUB_30MS;
2963 iLBCenc_inst->nasub = NASUB_30MS;
2964 iLBCenc_inst->lpc_n = LPC_N_30MS;
2965 iLBCenc_inst->no_of_bytes = NO_OF_BYTES_30MS;
2966 iLBCenc_inst->no_of_words = NO_OF_WORDS_30MS;
2970 Andersen, et al. Experimental [Page 53]
2972 RFC 3951 Internet Low Bit Rate Codec December 2004
2975 iLBCenc_inst->state_short_len=STATE_SHORT_LEN_30MS;
2976 /* ULP init */
2977 iLBCenc_inst->ULP_inst=&ULP_30msTbl;
2978 }
2979 else if (mode==20) {
2980 iLBCenc_inst->blockl = BLOCKL_20MS;
2981 iLBCenc_inst->nsub = NSUB_20MS;
2982 iLBCenc_inst->nasub = NASUB_20MS;
2983 iLBCenc_inst->lpc_n = LPC_N_20MS;
2984 iLBCenc_inst->no_of_bytes = NO_OF_BYTES_20MS;
2985 iLBCenc_inst->no_of_words = NO_OF_WORDS_20MS;
2986 iLBCenc_inst->state_short_len=STATE_SHORT_LEN_20MS;
2987 /* ULP init */
2988 iLBCenc_inst->ULP_inst=&ULP_20msTbl;
2989 }
2990 else {
2991 exit(2);
2992 }
2994 memset((*iLBCenc_inst).anaMem, 0,
2995 LPC_FILTERORDER*sizeof(float));
2996 memcpy((*iLBCenc_inst).lsfold, lsfmeanTbl,
2997 LPC_FILTERORDER*sizeof(float));
2998 memcpy((*iLBCenc_inst).lsfdeqold, lsfmeanTbl,
2999 LPC_FILTERORDER*sizeof(float));
3000 memset((*iLBCenc_inst).lpc_buffer, 0,
3001 (LPC_LOOKBACK+BLOCKL_MAX)*sizeof(float));
3002 memset((*iLBCenc_inst).hpimem, 0, 4*sizeof(float));
3004 return (iLBCenc_inst->no_of_bytes);
3005 }
3007 /*----------------------------------------------------------------*
3008 * main encoder function
3009 *---------------------------------------------------------------*/
3011 void iLBC_encode(
3012 unsigned char *bytes, /* (o) encoded data bits iLBC */
3013 float *block, /* (o) speech vector to
3014 encode */
3015 iLBC_Enc_Inst_t *iLBCenc_inst /* (i/o) the general encoder
3016 state */
3017 ){
3019 float data[BLOCKL_MAX];
3020 float residual[BLOCKL_MAX], reverseResidual[BLOCKL_MAX];
3022 int start, idxForMax, idxVec[STATE_LEN];
3026 Andersen, et al. Experimental [Page 54]
3028 RFC 3951 Internet Low Bit Rate Codec December 2004
3031 float reverseDecresidual[BLOCKL_MAX], mem[CB_MEML];
3032 int n, k, meml_gotten, Nfor, Nback, i, pos;
3033 int gain_index[CB_NSTAGES*NASUB_MAX],
3034 extra_gain_index[CB_NSTAGES];
3035 int cb_index[CB_NSTAGES*NASUB_MAX],extra_cb_index[CB_NSTAGES];
3036 int lsf_i[LSF_NSPLIT*LPC_N_MAX];
3037 unsigned char *pbytes;
3038 int diff, start_pos, state_first;
3039 float en1, en2;
3040 int index, ulp, firstpart;
3041 int subcount, subframe;
3042 float weightState[LPC_FILTERORDER];
3043 float syntdenum[NSUB_MAX*(LPC_FILTERORDER+1)];
3044 float weightdenum[NSUB_MAX*(LPC_FILTERORDER+1)];
3045 float decresidual[BLOCKL_MAX];
3047 /* high pass filtering of input signal if such is not done
3048 prior to calling this function */
3050 hpInput(block, iLBCenc_inst->blockl,
3051 data, (*iLBCenc_inst).hpimem);
3053 /* otherwise simply copy */
3055 /*memcpy(data,block,iLBCenc_inst->blockl*sizeof(float));*/
3057 /* LPC of hp filtered input data */
3059 LPCencode(syntdenum, weightdenum, lsf_i, data, iLBCenc_inst);
3062 /* inverse filter to get residual */
3064 for (n=0; n<iLBCenc_inst->nsub; n++) {
3065 anaFilter(&data[n*SUBL], &syntdenum[n*(LPC_FILTERORDER+1)],
3066 SUBL, &residual[n*SUBL], iLBCenc_inst->anaMem);
3067 }
3069 /* find state location */
3071 start = FrameClassify(iLBCenc_inst, residual);
3073 /* check if state should be in first or last part of the
3074 two subframes */
3076 diff = STATE_LEN - iLBCenc_inst->state_short_len;
3077 en1 = 0;
3078 index = (start-1)*SUBL;
3082 Andersen, et al. Experimental [Page 55]
3084 RFC 3951 Internet Low Bit Rate Codec December 2004
3087 for (i = 0; i < iLBCenc_inst->state_short_len; i++) {
3088 en1 += residual[index+i]*residual[index+i];
3089 }
3090 en2 = 0;
3091 index = (start-1)*SUBL+diff;
3092 for (i = 0; i < iLBCenc_inst->state_short_len; i++) {
3093 en2 += residual[index+i]*residual[index+i];
3094 }
3097 if (en1 > en2) {
3098 state_first = 1;
3099 start_pos = (start-1)*SUBL;
3100 } else {
3101 state_first = 0;
3102 start_pos = (start-1)*SUBL + diff;
3103 }
3105 /* scalar quantization of state */
3107 StateSearchW(iLBCenc_inst, &residual[start_pos],
3108 &syntdenum[(start-1)*(LPC_FILTERORDER+1)],
3109 &weightdenum[(start-1)*(LPC_FILTERORDER+1)], &idxForMax,
3110 idxVec, iLBCenc_inst->state_short_len, state_first);
3112 StateConstructW(idxForMax, idxVec,
3113 &syntdenum[(start-1)*(LPC_FILTERORDER+1)],
3114 &decresidual[start_pos], iLBCenc_inst->state_short_len);
3116 /* predictive quantization in state */
3118 if (state_first) { /* put adaptive part in the end */
3120 /* setup memory */
3122 memset(mem, 0,
3123 (CB_MEML-iLBCenc_inst->state_short_len)*sizeof(float));
3124 memcpy(mem+CB_MEML-iLBCenc_inst->state_short_len,
3125 decresidual+start_pos,
3126 iLBCenc_inst->state_short_len*sizeof(float));
3127 memset(weightState, 0, LPC_FILTERORDER*sizeof(float));
3129 /* encode sub-frames */
3131 iCBSearch(iLBCenc_inst, extra_cb_index, extra_gain_index,
3132 &residual[start_pos+iLBCenc_inst->state_short_len],
3133 mem+CB_MEML-stMemLTbl,
3134 stMemLTbl, diff, CB_NSTAGES,
3138 Andersen, et al. Experimental [Page 56]
3140 RFC 3951 Internet Low Bit Rate Codec December 2004
3143 &weightdenum[start*(LPC_FILTERORDER+1)],
3144 weightState, 0);
3146 /* construct decoded vector */
3148 iCBConstruct(
3149 &decresidual[start_pos+iLBCenc_inst->state_short_len],
3150 extra_cb_index, extra_gain_index,
3151 mem+CB_MEML-stMemLTbl,
3152 stMemLTbl, diff, CB_NSTAGES);
3154 }
3155 else { /* put adaptive part in the beginning */
3157 /* create reversed vectors for prediction */
3159 for (k=0; k<diff; k++) {
3160 reverseResidual[k] = residual[(start+1)*SUBL-1
3161 -(k+iLBCenc_inst->state_short_len)];
3162 }
3164 /* setup memory */
3166 meml_gotten = iLBCenc_inst->state_short_len;
3167 for (k=0; k<meml_gotten; k++) {
3168 mem[CB_MEML-1-k] = decresidual[start_pos + k];
3169 }
3170 memset(mem, 0, (CB_MEML-k)*sizeof(float));
3171 memset(weightState, 0, LPC_FILTERORDER*sizeof(float));
3173 /* encode sub-frames */
3175 iCBSearch(iLBCenc_inst, extra_cb_index, extra_gain_index,
3176 reverseResidual, mem+CB_MEML-stMemLTbl, stMemLTbl,
3177 diff, CB_NSTAGES,
3178 &weightdenum[(start-1)*(LPC_FILTERORDER+1)],
3179 weightState, 0);
3181 /* construct decoded vector */
3183 iCBConstruct(reverseDecresidual, extra_cb_index,
3184 extra_gain_index, mem+CB_MEML-stMemLTbl, stMemLTbl,
3185 diff, CB_NSTAGES);
3187 /* get decoded residual from reversed vector */
3189 for (k=0; k<diff; k++) {
3190 decresidual[start_pos-1-k] = reverseDecresidual[k];
3194 Andersen, et al. Experimental [Page 57]
3196 RFC 3951 Internet Low Bit Rate Codec December 2004
3199 }
3200 }
3202 /* counter for predicted sub-frames */
3204 subcount=0;
3206 /* forward prediction of sub-frames */
3208 Nfor = iLBCenc_inst->nsub-start-1;
3211 if ( Nfor > 0 ) {
3213 /* setup memory */
3215 memset(mem, 0, (CB_MEML-STATE_LEN)*sizeof(float));
3216 memcpy(mem+CB_MEML-STATE_LEN, decresidual+(start-1)*SUBL,
3217 STATE_LEN*sizeof(float));
3218 memset(weightState, 0, LPC_FILTERORDER*sizeof(float));
3220 /* loop over sub-frames to encode */
3222 for (subframe=0; subframe<Nfor; subframe++) {
3224 /* encode sub-frame */
3226 iCBSearch(iLBCenc_inst, cb_index+subcount*CB_NSTAGES,
3227 gain_index+subcount*CB_NSTAGES,
3228 &residual[(start+1+subframe)*SUBL],
3229 mem+CB_MEML-memLfTbl[subcount],
3230 memLfTbl[subcount], SUBL, CB_NSTAGES,
3231 &weightdenum[(start+1+subframe)*
3232 (LPC_FILTERORDER+1)],
3233 weightState, subcount+1);
3235 /* construct decoded vector */
3237 iCBConstruct(&decresidual[(start+1+subframe)*SUBL],
3238 cb_index+subcount*CB_NSTAGES,
3239 gain_index+subcount*CB_NSTAGES,
3240 mem+CB_MEML-memLfTbl[subcount],
3241 memLfTbl[subcount], SUBL, CB_NSTAGES);
3243 /* update memory */
3245 memcpy(mem, mem+SUBL, (CB_MEML-SUBL)*sizeof(float));
3246 memcpy(mem+CB_MEML-SUBL,
3250 Andersen, et al. Experimental [Page 58]
3252 RFC 3951 Internet Low Bit Rate Codec December 2004
3255 &decresidual[(start+1+subframe)*SUBL],
3256 SUBL*sizeof(float));
3257 memset(weightState, 0, LPC_FILTERORDER*sizeof(float));
3259 subcount++;
3260 }
3261 }
3264 /* backward prediction of sub-frames */
3266 Nback = start-1;
3269 if ( Nback > 0 ) {
3271 /* create reverse order vectors */
3273 for (n=0; n<Nback; n++) {
3274 for (k=0; k<SUBL; k++) {
3275 reverseResidual[n*SUBL+k] =
3276 residual[(start-1)*SUBL-1-n*SUBL-k];
3277 reverseDecresidual[n*SUBL+k] =
3278 decresidual[(start-1)*SUBL-1-n*SUBL-k];
3279 }
3280 }
3282 /* setup memory */
3284 meml_gotten = SUBL*(iLBCenc_inst->nsub+1-start);
3287 if ( meml_gotten > CB_MEML ) {
3288 meml_gotten=CB_MEML;
3289 }
3290 for (k=0; k<meml_gotten; k++) {
3291 mem[CB_MEML-1-k] = decresidual[(start-1)*SUBL + k];
3292 }
3293 memset(mem, 0, (CB_MEML-k)*sizeof(float));
3294 memset(weightState, 0, LPC_FILTERORDER*sizeof(float));
3296 /* loop over sub-frames to encode */
3298 for (subframe=0; subframe<Nback; subframe++) {
3300 /* encode sub-frame */
3302 iCBSearch(iLBCenc_inst, cb_index+subcount*CB_NSTAGES,
3306 Andersen, et al. Experimental [Page 59]
3308 RFC 3951 Internet Low Bit Rate Codec December 2004
3311 gain_index+subcount*CB_NSTAGES,
3312 &reverseResidual[subframe*SUBL],
3313 mem+CB_MEML-memLfTbl[subcount],
3314 memLfTbl[subcount], SUBL, CB_NSTAGES,
3315 &weightdenum[(start-2-subframe)*
3316 (LPC_FILTERORDER+1)],
3317 weightState, subcount+1);
3319 /* construct decoded vector */
3321 iCBConstruct(&reverseDecresidual[subframe*SUBL],
3322 cb_index+subcount*CB_NSTAGES,
3323 gain_index+subcount*CB_NSTAGES,
3324 mem+CB_MEML-memLfTbl[subcount],
3325 memLfTbl[subcount], SUBL, CB_NSTAGES);
3327 /* update memory */
3329 memcpy(mem, mem+SUBL, (CB_MEML-SUBL)*sizeof(float));
3330 memcpy(mem+CB_MEML-SUBL,
3331 &reverseDecresidual[subframe*SUBL],
3332 SUBL*sizeof(float));
3333 memset(weightState, 0, LPC_FILTERORDER*sizeof(float));
3335 subcount++;
3337 }
3339 /* get decoded residual from reversed vector */
3341 for (i=0; i<SUBL*Nback; i++) {
3342 decresidual[SUBL*Nback - i - 1] =
3343 reverseDecresidual[i];
3344 }
3345 }
3346 /* end encoding part */
3348 /* adjust index */
3349 index_conv_enc(cb_index);
3351 /* pack bytes */
3353 pbytes=bytes;
3354 pos=0;
3356 /* loop over the 3 ULP classes */
3358 for (ulp=0; ulp<3; ulp++) {
3362 Andersen, et al. Experimental [Page 60]
3364 RFC 3951 Internet Low Bit Rate Codec December 2004
3368 /* LSF */
3369 for (k=0; k<LSF_NSPLIT*iLBCenc_inst->lpc_n; k++) {
3370 packsplit(&lsf_i[k], &firstpart, &lsf_i[k],
3371 iLBCenc_inst->ULP_inst->lsf_bits[k][ulp],
3372 iLBCenc_inst->ULP_inst->lsf_bits[k][ulp]+
3373 iLBCenc_inst->ULP_inst->lsf_bits[k][ulp+1]+
3374 iLBCenc_inst->ULP_inst->lsf_bits[k][ulp+2]);
3375 dopack( &pbytes, firstpart,
3376 iLBCenc_inst->ULP_inst->lsf_bits[k][ulp], &pos);
3377 }
3379 /* Start block info */
3381 packsplit(&start, &firstpart, &start,
3382 iLBCenc_inst->ULP_inst->start_bits[ulp],
3383 iLBCenc_inst->ULP_inst->start_bits[ulp]+
3384 iLBCenc_inst->ULP_inst->start_bits[ulp+1]+
3385 iLBCenc_inst->ULP_inst->start_bits[ulp+2]);
3386 dopack( &pbytes, firstpart,
3387 iLBCenc_inst->ULP_inst->start_bits[ulp], &pos);
3389 packsplit(&state_first, &firstpart, &state_first,
3390 iLBCenc_inst->ULP_inst->startfirst_bits[ulp],
3391 iLBCenc_inst->ULP_inst->startfirst_bits[ulp]+
3392 iLBCenc_inst->ULP_inst->startfirst_bits[ulp+1]+
3393 iLBCenc_inst->ULP_inst->startfirst_bits[ulp+2]);
3394 dopack( &pbytes, firstpart,
3395 iLBCenc_inst->ULP_inst->startfirst_bits[ulp], &pos);
3397 packsplit(&idxForMax, &firstpart, &idxForMax,
3398 iLBCenc_inst->ULP_inst->scale_bits[ulp],
3399 iLBCenc_inst->ULP_inst->scale_bits[ulp]+
3400 iLBCenc_inst->ULP_inst->scale_bits[ulp+1]+
3401 iLBCenc_inst->ULP_inst->scale_bits[ulp+2]);
3402 dopack( &pbytes, firstpart,
3403 iLBCenc_inst->ULP_inst->scale_bits[ulp], &pos);
3405 for (k=0; k<iLBCenc_inst->state_short_len; k++) {
3406 packsplit(idxVec+k, &firstpart, idxVec+k,
3407 iLBCenc_inst->ULP_inst->state_bits[ulp],
3408 iLBCenc_inst->ULP_inst->state_bits[ulp]+
3409 iLBCenc_inst->ULP_inst->state_bits[ulp+1]+
3410 iLBCenc_inst->ULP_inst->state_bits[ulp+2]);
3411 dopack( &pbytes, firstpart,
3412 iLBCenc_inst->ULP_inst->state_bits[ulp], &pos);
3413 }
3418 Andersen, et al. Experimental [Page 61]
3420 RFC 3951 Internet Low Bit Rate Codec December 2004
3423 /* 23/22 (20ms/30ms) sample block */
3425 for (k=0;k<CB_NSTAGES;k++) {
3426 packsplit(extra_cb_index+k, &firstpart,
3427 extra_cb_index+k,
3428 iLBCenc_inst->ULP_inst->extra_cb_index[k][ulp],
3429 iLBCenc_inst->ULP_inst->extra_cb_index[k][ulp]+
3430 iLBCenc_inst->ULP_inst->extra_cb_index[k][ulp+1]+
3431 iLBCenc_inst->ULP_inst->extra_cb_index[k][ulp+2]);
3432 dopack( &pbytes, firstpart,
3433 iLBCenc_inst->ULP_inst->extra_cb_index[k][ulp],
3434 &pos);
3435 }
3437 for (k=0;k<CB_NSTAGES;k++) {
3438 packsplit(extra_gain_index+k, &firstpart,
3439 extra_gain_index+k,
3440 iLBCenc_inst->ULP_inst->extra_cb_gain[k][ulp],
3441 iLBCenc_inst->ULP_inst->extra_cb_gain[k][ulp]+
3442 iLBCenc_inst->ULP_inst->extra_cb_gain[k][ulp+1]+
3443 iLBCenc_inst->ULP_inst->extra_cb_gain[k][ulp+2]);
3444 dopack( &pbytes, firstpart,
3445 iLBCenc_inst->ULP_inst->extra_cb_gain[k][ulp],
3446 &pos);
3447 }
3449 /* The two/four (20ms/30ms) 40 sample sub-blocks */
3451 for (i=0; i<iLBCenc_inst->nasub; i++) {
3452 for (k=0; k<CB_NSTAGES; k++) {
3453 packsplit(cb_index+i*CB_NSTAGES+k, &firstpart,
3454 cb_index+i*CB_NSTAGES+k,
3455 iLBCenc_inst->ULP_inst->cb_index[i][k][ulp],
3456 iLBCenc_inst->ULP_inst->cb_index[i][k][ulp]+
3457 iLBCenc_inst->ULP_inst->cb_index[i][k][ulp+1]+
3458 iLBCenc_inst->ULP_inst->cb_index[i][k][ulp+2]);
3459 dopack( &pbytes, firstpart,
3460 iLBCenc_inst->ULP_inst->cb_index[i][k][ulp],
3461 &pos);
3462 }
3463 }
3465 for (i=0; i<iLBCenc_inst->nasub; i++) {
3466 for (k=0; k<CB_NSTAGES; k++) {
3467 packsplit(gain_index+i*CB_NSTAGES+k, &firstpart,
3468 gain_index+i*CB_NSTAGES+k,
3469 iLBCenc_inst->ULP_inst->cb_gain[i][k][ulp],
3470 iLBCenc_inst->ULP_inst->cb_gain[i][k][ulp]+
3474 Andersen, et al. Experimental [Page 62]
3476 RFC 3951 Internet Low Bit Rate Codec December 2004
3479 iLBCenc_inst->ULP_inst->cb_gain[i][k][ulp+1]+
3480 iLBCenc_inst->ULP_inst->cb_gain[i][k][ulp+2]);
3481 dopack( &pbytes, firstpart,
3482 iLBCenc_inst->ULP_inst->cb_gain[i][k][ulp],
3483 &pos);
3484 }
3485 }
3486 }
3488 /* set the last bit to zero (otherwise the decoder
3489 will treat it as a lost frame) */
3490 dopack( &pbytes, 0, 1, &pos);
3491 }
3493 A.4. iLBC_decode.h
3495 /******************************************************************
3497 iLBC Speech Coder ANSI-C Source Code
3499 iLBC_decode.h
3501 Copyright (C) The Internet Society (2004).
3502 All Rights Reserved.
3504 ******************************************************************/
3506 #ifndef __iLBC_ILBCDECODE_H
3507 #define __iLBC_ILBCDECODE_H
3509 #include "iLBC_define.h"
3511 short initDecode( /* (o) Number of decoded
3512 samples */
3513 iLBC_Dec_Inst_t *iLBCdec_inst, /* (i/o) Decoder instance */
3514 int mode, /* (i) frame size mode */
3515 int use_enhancer /* (i) 1 to use enhancer
3516 0 to run without
3517 enhancer */
3518 );
3520 void iLBC_decode(
3521 float *decblock, /* (o) decoded signal block */
3522 unsigned char *bytes, /* (i) encoded signal bits */
3523 iLBC_Dec_Inst_t *iLBCdec_inst, /* (i/o) the decoder state
3524 structure */
3525 int mode /* (i) 0: bad packet, PLC,
3526 1: normal */
3530 Andersen, et al. Experimental [Page 63]
3532 RFC 3951 Internet Low Bit Rate Codec December 2004
3535 );
3537 #endif
3539 A.5. iLBC_decode.c
3541 /******************************************************************
3543 iLBC Speech Coder ANSI-C Source Code
3545 iLBC_decode.c
3547 Copyright (C) The Internet Society (2004).
3548 All Rights Reserved.
3550 ******************************************************************/
3552 #include <math.h>
3553 #include <stdlib.h>
3555 #include "iLBC_define.h"
3556 #include "StateConstructW.h"
3557 #include "LPCdecode.h"
3558 #include "iCBConstruct.h"
3559 #include "doCPLC.h"
3560 #include "helpfun.h"
3561 #include "constants.h"
3562 #include "packing.h"
3563 #include "string.h"
3564 #include "enhancer.h"
3565 #include "hpOutput.h"
3566 #include "syntFilter.h"
3568 /*----------------------------------------------------------------*
3569 * Initiation of decoder instance.
3570 *---------------------------------------------------------------*/
3572 short initDecode( /* (o) Number of decoded
3573 samples */
3574 iLBC_Dec_Inst_t *iLBCdec_inst, /* (i/o) Decoder instance */
3575 int mode, /* (i) frame size mode */
3576 int use_enhancer /* (i) 1 to use enhancer
3577 0 to run without
3578 enhancer */
3579 ){
3580 int i;
3582 iLBCdec_inst->mode = mode;
3586 Andersen, et al. Experimental [Page 64]
3588 RFC 3951 Internet Low Bit Rate Codec December 2004
3591 if (mode==30) {
3592 iLBCdec_inst->blockl = BLOCKL_30MS;
3593 iLBCdec_inst->nsub = NSUB_30MS;
3594 iLBCdec_inst->nasub = NASUB_30MS;
3595 iLBCdec_inst->lpc_n = LPC_N_30MS;
3596 iLBCdec_inst->no_of_bytes = NO_OF_BYTES_30MS;
3597 iLBCdec_inst->no_of_words = NO_OF_WORDS_30MS;
3598 iLBCdec_inst->state_short_len=STATE_SHORT_LEN_30MS;
3599 /* ULP init */
3600 iLBCdec_inst->ULP_inst=&ULP_30msTbl;
3601 }
3602 else if (mode==20) {
3603 iLBCdec_inst->blockl = BLOCKL_20MS;
3604 iLBCdec_inst->nsub = NSUB_20MS;
3605 iLBCdec_inst->nasub = NASUB_20MS;
3606 iLBCdec_inst->lpc_n = LPC_N_20MS;
3607 iLBCdec_inst->no_of_bytes = NO_OF_BYTES_20MS;
3608 iLBCdec_inst->no_of_words = NO_OF_WORDS_20MS;
3609 iLBCdec_inst->state_short_len=STATE_SHORT_LEN_20MS;
3610 /* ULP init */
3611 iLBCdec_inst->ULP_inst=&ULP_20msTbl;
3612 }
3613 else {
3614 exit(2);
3615 }
3617 memset(iLBCdec_inst->syntMem, 0,
3618 LPC_FILTERORDER*sizeof(float));
3619 memcpy((*iLBCdec_inst).lsfdeqold, lsfmeanTbl,
3620 LPC_FILTERORDER*sizeof(float));
3622 memset(iLBCdec_inst->old_syntdenum, 0,
3623 ((LPC_FILTERORDER + 1)*NSUB_MAX)*sizeof(float));
3624 for (i=0; i<NSUB_MAX; i++)
3625 iLBCdec_inst->old_syntdenum[i*(LPC_FILTERORDER+1)]=1.0;
3627 iLBCdec_inst->last_lag = 20;
3629 iLBCdec_inst->prevLag = 120;
3630 iLBCdec_inst->per = 0.0;
3631 iLBCdec_inst->consPLICount = 0;
3632 iLBCdec_inst->prevPLI = 0;
3633 iLBCdec_inst->prevLpc[0] = 1.0;
3634 memset(iLBCdec_inst->prevLpc+1,0,
3635 LPC_FILTERORDER*sizeof(float));
3636 memset(iLBCdec_inst->prevResidual, 0, BLOCKL_MAX*sizeof(float));
3637 iLBCdec_inst->seed=777;
3642 Andersen, et al. Experimental [Page 65]
3644 RFC 3951 Internet Low Bit Rate Codec December 2004
3647 memset(iLBCdec_inst->hpomem, 0, 4*sizeof(float));
3649 iLBCdec_inst->use_enhancer = use_enhancer;
3650 memset(iLBCdec_inst->enh_buf, 0, ENH_BUFL*sizeof(float));
3651 for (i=0;i<ENH_NBLOCKS_TOT;i++)
3652 iLBCdec_inst->enh_period[i]=(float)40.0;
3654 iLBCdec_inst->prev_enh_pl = 0;
3656 return (iLBCdec_inst->blockl);
3657 }
3659 /*----------------------------------------------------------------*
3660 * frame residual decoder function (subrutine to iLBC_decode)
3661 *---------------------------------------------------------------*/
3663 void Decode(
3664 iLBC_Dec_Inst_t *iLBCdec_inst, /* (i/o) the decoder state
3665 structure */
3666 float *decresidual, /* (o) decoded residual frame */
3667 int start, /* (i) location of start
3668 state */
3669 int idxForMax, /* (i) codebook index for the
3670 maximum value */
3671 int *idxVec, /* (i) codebook indexes for the
3672 samples in the start
3673 state */
3674 float *syntdenum, /* (i) the decoded synthesis
3675 filter coefficients */
3676 int *cb_index, /* (i) the indexes for the
3677 adaptive codebook */
3678 int *gain_index, /* (i) the indexes for the
3679 corresponding gains */
3680 int *extra_cb_index, /* (i) the indexes for the
3681 adaptive codebook part
3682 of start state */
3683 int *extra_gain_index, /* (i) the indexes for the
3684 corresponding gains */
3685 int state_first /* (i) 1 if non adaptive part
3686 of start state comes
3687 first 0 if that part
3688 comes last */
3689 ){
3690 float reverseDecresidual[BLOCKL_MAX], mem[CB_MEML];
3691 int k, meml_gotten, Nfor, Nback, i;
3692 int diff, start_pos;
3693 int subcount, subframe;
3698 Andersen, et al. Experimental [Page 66]
3700 RFC 3951 Internet Low Bit Rate Codec December 2004
3703 diff = STATE_LEN - iLBCdec_inst->state_short_len;
3705 if (state_first == 1) {
3706 start_pos = (start-1)*SUBL;
3707 } else {
3708 start_pos = (start-1)*SUBL + diff;
3709 }
3711 /* decode scalar part of start state */
3713 StateConstructW(idxForMax, idxVec,
3714 &syntdenum[(start-1)*(LPC_FILTERORDER+1)],
3715 &decresidual[start_pos], iLBCdec_inst->state_short_len);
3718 if (state_first) { /* put adaptive part in the end */
3720 /* setup memory */
3722 memset(mem, 0,
3723 (CB_MEML-iLBCdec_inst->state_short_len)*sizeof(float));
3724 memcpy(mem+CB_MEML-iLBCdec_inst->state_short_len,
3725 decresidual+start_pos,
3726 iLBCdec_inst->state_short_len*sizeof(float));
3728 /* construct decoded vector */
3730 iCBConstruct(
3731 &decresidual[start_pos+iLBCdec_inst->state_short_len],
3732 extra_cb_index, extra_gain_index, mem+CB_MEML-stMemLTbl,
3733 stMemLTbl, diff, CB_NSTAGES);
3735 }
3736 else {/* put adaptive part in the beginning */
3738 /* create reversed vectors for prediction */
3740 for (k=0; k<diff; k++) {
3741 reverseDecresidual[k] =
3742 decresidual[(start+1)*SUBL-1-
3743 (k+iLBCdec_inst->state_short_len)];
3744 }
3746 /* setup memory */
3748 meml_gotten = iLBCdec_inst->state_short_len;
3749 for (k=0; k<meml_gotten; k++){
3750 mem[CB_MEML-1-k] = decresidual[start_pos + k];
3754 Andersen, et al. Experimental [Page 67]
3756 RFC 3951 Internet Low Bit Rate Codec December 2004
3759 }
3760 memset(mem, 0, (CB_MEML-k)*sizeof(float));
3762 /* construct decoded vector */
3764 iCBConstruct(reverseDecresidual, extra_cb_index,
3765 extra_gain_index, mem+CB_MEML-stMemLTbl, stMemLTbl,
3766 diff, CB_NSTAGES);
3768 /* get decoded residual from reversed vector */
3770 for (k=0; k<diff; k++) {
3771 decresidual[start_pos-1-k] = reverseDecresidual[k];
3772 }
3773 }
3775 /* counter for predicted sub-frames */
3777 subcount=0;
3779 /* forward prediction of sub-frames */
3781 Nfor = iLBCdec_inst->nsub-start-1;
3783 if ( Nfor > 0 ){
3785 /* setup memory */
3787 memset(mem, 0, (CB_MEML-STATE_LEN)*sizeof(float));
3788 memcpy(mem+CB_MEML-STATE_LEN, decresidual+(start-1)*SUBL,
3789 STATE_LEN*sizeof(float));
3791 /* loop over sub-frames to encode */
3793 for (subframe=0; subframe<Nfor; subframe++) {
3795 /* construct decoded vector */
3797 iCBConstruct(&decresidual[(start+1+subframe)*SUBL],
3798 cb_index+subcount*CB_NSTAGES,
3799 gain_index+subcount*CB_NSTAGES,
3800 mem+CB_MEML-memLfTbl[subcount],
3801 memLfTbl[subcount], SUBL, CB_NSTAGES);
3803 /* update memory */
3805 memcpy(mem, mem+SUBL, (CB_MEML-SUBL)*sizeof(float));
3806 memcpy(mem+CB_MEML-SUBL,
3810 Andersen, et al. Experimental [Page 68]
3812 RFC 3951 Internet Low Bit Rate Codec December 2004
3815 &decresidual[(start+1+subframe)*SUBL],
3816 SUBL*sizeof(float));
3818 subcount++;
3820 }
3822 }
3824 /* backward prediction of sub-frames */
3826 Nback = start-1;
3828 if ( Nback > 0 ) {
3830 /* setup memory */
3832 meml_gotten = SUBL*(iLBCdec_inst->nsub+1-start);
3834 if ( meml_gotten > CB_MEML ) {
3835 meml_gotten=CB_MEML;
3836 }
3837 for (k=0; k<meml_gotten; k++) {
3838 mem[CB_MEML-1-k] = decresidual[(start-1)*SUBL + k];
3839 }
3840 memset(mem, 0, (CB_MEML-k)*sizeof(float));
3842 /* loop over subframes to decode */
3844 for (subframe=0; subframe<Nback; subframe++) {
3846 /* construct decoded vector */
3848 iCBConstruct(&reverseDecresidual[subframe*SUBL],
3849 cb_index+subcount*CB_NSTAGES,
3850 gain_index+subcount*CB_NSTAGES,
3851 mem+CB_MEML-memLfTbl[subcount], memLfTbl[subcount],
3852 SUBL, CB_NSTAGES);
3854 /* update memory */
3856 memcpy(mem, mem+SUBL, (CB_MEML-SUBL)*sizeof(float));
3857 memcpy(mem+CB_MEML-SUBL,
3858 &reverseDecresidual[subframe*SUBL],
3859 SUBL*sizeof(float));
3861 subcount++;
3862 }
3866 Andersen, et al. Experimental [Page 69]
3868 RFC 3951 Internet Low Bit Rate Codec December 2004
3871 /* get decoded residual from reversed vector */
3873 for (i=0; i<SUBL*Nback; i++)
3874 decresidual[SUBL*Nback - i - 1] =
3875 reverseDecresidual[i];
3876 }
3877 }
3879 /*----------------------------------------------------------------*
3880 * main decoder function
3881 *---------------------------------------------------------------*/
3883 void iLBC_decode(
3884 float *decblock, /* (o) decoded signal block */
3885 unsigned char *bytes, /* (i) encoded signal bits */
3886 iLBC_Dec_Inst_t *iLBCdec_inst, /* (i/o) the decoder state
3887 structure */
3888 int mode /* (i) 0: bad packet, PLC,
3889 1: normal */
3890 ){
3891 float data[BLOCKL_MAX];
3892 float lsfdeq[LPC_FILTERORDER*LPC_N_MAX];
3893 float PLCresidual[BLOCKL_MAX], PLClpc[LPC_FILTERORDER + 1];
3894 float zeros[BLOCKL_MAX], one[LPC_FILTERORDER + 1];
3895 int k, i, start, idxForMax, pos, lastpart, ulp;
3896 int lag, ilag;
3897 float cc, maxcc;
3898 int idxVec[STATE_LEN];
3899 int check;
3900 int gain_index[NASUB_MAX*CB_NSTAGES],
3901 extra_gain_index[CB_NSTAGES];
3902 int cb_index[CB_NSTAGES*NASUB_MAX], extra_cb_index[CB_NSTAGES];
3903 int lsf_i[LSF_NSPLIT*LPC_N_MAX];
3904 int state_first;
3905 int last_bit;
3906 unsigned char *pbytes;
3907 float weightdenum[(LPC_FILTERORDER + 1)*NSUB_MAX];
3908 int order_plus_one;
3909 float syntdenum[NSUB_MAX*(LPC_FILTERORDER+1)];
3910 float decresidual[BLOCKL_MAX];
3912 if (mode>0) { /* the data are good */
3914 /* decode data */
3916 pbytes=bytes;
3917 pos=0;
3922 Andersen, et al. Experimental [Page 70]
3924 RFC 3951 Internet Low Bit Rate Codec December 2004
3927 /* Set everything to zero before decoding */
3929 for (k=0; k<LSF_NSPLIT*LPC_N_MAX; k++) {
3930 lsf_i[k]=0;
3931 }
3932 start=0;
3933 state_first=0;
3934 idxForMax=0;
3935 for (k=0; k<iLBCdec_inst->state_short_len; k++) {
3936 idxVec[k]=0;
3937 }
3938 for (k=0; k<CB_NSTAGES; k++) {
3939 extra_cb_index[k]=0;
3940 }
3941 for (k=0; k<CB_NSTAGES; k++) {
3942 extra_gain_index[k]=0;
3943 }
3944 for (i=0; i<iLBCdec_inst->nasub; i++) {
3945 for (k=0; k<CB_NSTAGES; k++) {
3946 cb_index[i*CB_NSTAGES+k]=0;
3947 }
3948 }
3949 for (i=0; i<iLBCdec_inst->nasub; i++) {
3950 for (k=0; k<CB_NSTAGES; k++) {
3951 gain_index[i*CB_NSTAGES+k]=0;
3952 }
3953 }
3955 /* loop over ULP classes */
3957 for (ulp=0; ulp<3; ulp++) {
3959 /* LSF */
3960 for (k=0; k<LSF_NSPLIT*iLBCdec_inst->lpc_n; k++){
3961 unpack( &pbytes, &lastpart,
3962 iLBCdec_inst->ULP_inst->lsf_bits[k][ulp], &pos);
3963 packcombine(&lsf_i[k], lastpart,
3964 iLBCdec_inst->ULP_inst->lsf_bits[k][ulp]);
3965 }
3967 /* Start block info */
3969 unpack( &pbytes, &lastpart,
3970 iLBCdec_inst->ULP_inst->start_bits[ulp], &pos);
3971 packcombine(&start, lastpart,
3972 iLBCdec_inst->ULP_inst->start_bits[ulp]);
3974 unpack( &pbytes, &lastpart,
3978 Andersen, et al. Experimental [Page 71]
3980 RFC 3951 Internet Low Bit Rate Codec December 2004
3983 iLBCdec_inst->ULP_inst->startfirst_bits[ulp], &pos);
3984 packcombine(&state_first, lastpart,
3985 iLBCdec_inst->ULP_inst->startfirst_bits[ulp]);
3987 unpack( &pbytes, &lastpart,
3988 iLBCdec_inst->ULP_inst->scale_bits[ulp], &pos);
3989 packcombine(&idxForMax, lastpart,
3990 iLBCdec_inst->ULP_inst->scale_bits[ulp]);
3992 for (k=0; k<iLBCdec_inst->state_short_len; k++) {
3993 unpack( &pbytes, &lastpart,
3994 iLBCdec_inst->ULP_inst->state_bits[ulp], &pos);
3995 packcombine(idxVec+k, lastpart,
3996 iLBCdec_inst->ULP_inst->state_bits[ulp]);
3997 }
3999 /* 23/22 (20ms/30ms) sample block */
4001 for (k=0; k<CB_NSTAGES; k++) {
4002 unpack( &pbytes, &lastpart,
4003 iLBCdec_inst->ULP_inst->extra_cb_index[k][ulp],
4004 &pos);
4005 packcombine(extra_cb_index+k, lastpart,
4006 iLBCdec_inst->ULP_inst->extra_cb_index[k][ulp]);
4007 }
4008 for (k=0; k<CB_NSTAGES; k++) {
4009 unpack( &pbytes, &lastpart,
4010 iLBCdec_inst->ULP_inst->extra_cb_gain[k][ulp],
4011 &pos);
4012 packcombine(extra_gain_index+k, lastpart,
4013 iLBCdec_inst->ULP_inst->extra_cb_gain[k][ulp]);
4014 }
4016 /* The two/four (20ms/30ms) 40 sample sub-blocks */
4018 for (i=0; i<iLBCdec_inst->nasub; i++) {
4019 for (k=0; k<CB_NSTAGES; k++) {
4020 unpack( &pbytes, &lastpart,
4021 iLBCdec_inst->ULP_inst->cb_index[i][k][ulp],
4022 &pos);
4023 packcombine(cb_index+i*CB_NSTAGES+k, lastpart,
4024 iLBCdec_inst->ULP_inst->cb_index[i][k][ulp]);
4025 }
4026 }
4028 for (i=0; i<iLBCdec_inst->nasub; i++) {
4029 for (k=0; k<CB_NSTAGES; k++) {
4030 unpack( &pbytes, &lastpart,
4034 Andersen, et al. Experimental [Page 72]
4036 RFC 3951 Internet Low Bit Rate Codec December 2004
4039 iLBCdec_inst->ULP_inst->cb_gain[i][k][ulp],
4040 &pos);
4041 packcombine(gain_index+i*CB_NSTAGES+k, lastpart,
4042 iLBCdec_inst->ULP_inst->cb_gain[i][k][ulp]);
4043 }
4044 }
4045 }
4046 /* Extract last bit. If it is 1 this indicates an
4047 empty/lost frame */
4048 unpack( &pbytes, &last_bit, 1, &pos);
4050 /* Check for bit errors or empty/lost frames */
4051 if (start<1)
4052 mode = 0;
4053 if (iLBCdec_inst->mode==20 && start>3)
4054 mode = 0;
4055 if (iLBCdec_inst->mode==30 && start>5)
4056 mode = 0;
4057 if (last_bit==1)
4058 mode = 0;
4060 if (mode==1) { /* No bit errors was detected,
4061 continue decoding */
4063 /* adjust index */
4064 index_conv_dec(cb_index);
4066 /* decode the lsf */
4068 SimplelsfDEQ(lsfdeq, lsf_i, iLBCdec_inst->lpc_n);
4069 check=LSF_check(lsfdeq, LPC_FILTERORDER,
4070 iLBCdec_inst->lpc_n);
4071 DecoderInterpolateLSF(syntdenum, weightdenum,
4072 lsfdeq, LPC_FILTERORDER, iLBCdec_inst);
4074 Decode(iLBCdec_inst, decresidual, start, idxForMax,
4075 idxVec, syntdenum, cb_index, gain_index,
4076 extra_cb_index, extra_gain_index,
4077 state_first);
4079 /* preparing the plc for a future loss! */
4081 doThePLC(PLCresidual, PLClpc, 0, decresidual,
4082 syntdenum +
4083 (LPC_FILTERORDER + 1)*(iLBCdec_inst->nsub - 1),
4084 (*iLBCdec_inst).last_lag, iLBCdec_inst);
4090 Andersen, et al. Experimental [Page 73]
4092 RFC 3951 Internet Low Bit Rate Codec December 2004
4095 memcpy(decresidual, PLCresidual,
4096 iLBCdec_inst->blockl*sizeof(float));
4097 }
4099 }
4101 if (mode == 0) {
4102 /* the data is bad (either a PLC call
4103 * was made or a severe bit error was detected)
4104 */
4106 /* packet loss conceal */
4108 memset(zeros, 0, BLOCKL_MAX*sizeof(float));
4110 one[0] = 1;
4111 memset(one+1, 0, LPC_FILTERORDER*sizeof(float));
4113 start=0;
4115 doThePLC(PLCresidual, PLClpc, 1, zeros, one,
4116 (*iLBCdec_inst).last_lag, iLBCdec_inst);
4117 memcpy(decresidual, PLCresidual,
4118 iLBCdec_inst->blockl*sizeof(float));
4120 order_plus_one = LPC_FILTERORDER + 1;
4121 for (i = 0; i < iLBCdec_inst->nsub; i++) {
4122 memcpy(syntdenum+(i*order_plus_one), PLClpc,
4123 order_plus_one*sizeof(float));
4124 }
4125 }
4127 if (iLBCdec_inst->use_enhancer == 1) {
4129 /* post filtering */
4131 iLBCdec_inst->last_lag =
4132 enhancerInterface(data, decresidual, iLBCdec_inst);
4134 /* synthesis filtering */
4136 if (iLBCdec_inst->mode==20) {
4137 /* Enhancer has 40 samples delay */
4138 i=0;
4139 syntFilter(data + i*SUBL,
4140 iLBCdec_inst->old_syntdenum +
4141 (i+iLBCdec_inst->nsub-1)*(LPC_FILTERORDER+1),
4142 SUBL, iLBCdec_inst->syntMem);
4146 Andersen, et al. Experimental [Page 74]
4148 RFC 3951 Internet Low Bit Rate Codec December 2004
4151 for (i=1; i < iLBCdec_inst->nsub; i++) {
4152 syntFilter(data + i*SUBL,
4153 syntdenum + (i-1)*(LPC_FILTERORDER+1),
4154 SUBL, iLBCdec_inst->syntMem);
4155 }
4156 } else if (iLBCdec_inst->mode==30) {
4157 /* Enhancer has 80 samples delay */
4158 for (i=0; i < 2; i++) {
4159 syntFilter(data + i*SUBL,
4160 iLBCdec_inst->old_syntdenum +
4161 (i+iLBCdec_inst->nsub-2)*(LPC_FILTERORDER+1),
4162 SUBL, iLBCdec_inst->syntMem);
4163 }
4164 for (i=2; i < iLBCdec_inst->nsub; i++) {
4165 syntFilter(data + i*SUBL,
4166 syntdenum + (i-2)*(LPC_FILTERORDER+1), SUBL,
4167 iLBCdec_inst->syntMem);
4168 }
4169 }
4171 } else {
4173 /* Find last lag */
4174 lag = 20;
4175 maxcc = xCorrCoef(&decresidual[BLOCKL_MAX-ENH_BLOCKL],
4176 &decresidual[BLOCKL_MAX-ENH_BLOCKL-lag], ENH_BLOCKL);
4178 for (ilag=21; ilag<120; ilag++) {
4179 cc = xCorrCoef(&decresidual[BLOCKL_MAX-ENH_BLOCKL],
4180 &decresidual[BLOCKL_MAX-ENH_BLOCKL-ilag],
4181 ENH_BLOCKL);
4183 if (cc > maxcc) {
4184 maxcc = cc;
4185 lag = ilag;
4186 }
4187 }
4188 iLBCdec_inst->last_lag = lag;
4190 /* copy data and run synthesis filter */
4192 memcpy(data, decresidual,
4193 iLBCdec_inst->blockl*sizeof(float));
4194 for (i=0; i < iLBCdec_inst->nsub; i++) {
4195 syntFilter(data + i*SUBL,
4196 syntdenum + i*(LPC_FILTERORDER+1), SUBL,
4197 iLBCdec_inst->syntMem);
4198 }
4202 Andersen, et al. Experimental [Page 75]
4204 RFC 3951 Internet Low Bit Rate Codec December 2004
4207 }
4209 /* high pass filtering on output if desired, otherwise
4210 copy to out */
4212 hpOutput(data, iLBCdec_inst->blockl,
4213 decblock,iLBCdec_inst->hpomem);
4215 /* memcpy(decblock,data,iLBCdec_inst->blockl*sizeof(float));*/
4217 memcpy(iLBCdec_inst->old_syntdenum, syntdenum,
4219 iLBCdec_inst->nsub*(LPC_FILTERORDER+1)*sizeof(float));
4221 iLBCdec_inst->prev_enh_pl=0;
4223 if (mode==0) { /* PLC was used */
4224 iLBCdec_inst->prev_enh_pl=1;
4225 }
4226 }
4228 A.6. iLBC_define.h
4230 /******************************************************************
4232 iLBC Speech Coder ANSI-C Source Code
4234 iLBC_define.h
4236 Copyright (C) The Internet Society (2004).
4237 All Rights Reserved.
4239 ******************************************************************/
4240 #include <string.h>
4242 #ifndef __iLBC_ILBCDEFINE_H
4243 #define __iLBC_ILBCDEFINE_H
4245 /* general codec settings */
4247 #define FS (float)8000.0
4248 #define BLOCKL_20MS 160
4249 #define BLOCKL_30MS 240
4250 #define BLOCKL_MAX 240
4251 #define NSUB_20MS 4
4252 #define NSUB_30MS 6
4253 #define NSUB_MAX 6
4254 #define NASUB_20MS 2
4258 Andersen, et al. Experimental [Page 76]
4260 RFC 3951 Internet Low Bit Rate Codec December 2004
4263 #define NASUB_30MS 4
4264 #define NASUB_MAX 4
4265 #define SUBL 40
4266 #define STATE_LEN 80
4267 #define STATE_SHORT_LEN_30MS 58
4268 #define STATE_SHORT_LEN_20MS 57
4270 /* LPC settings */
4272 #define LPC_FILTERORDER 10
4273 #define LPC_CHIRP_SYNTDENUM (float)0.9025
4274 #define LPC_CHIRP_WEIGHTDENUM (float)0.4222
4275 #define LPC_LOOKBACK 60
4276 #define LPC_N_20MS 1
4277 #define LPC_N_30MS 2
4278 #define LPC_N_MAX 2
4279 #define LPC_ASYMDIFF 20
4280 #define LPC_BW (float)60.0
4281 #define LPC_WN (float)1.0001
4282 #define LSF_NSPLIT 3
4283 #define LSF_NUMBER_OF_STEPS 4
4284 #define LPC_HALFORDER (LPC_FILTERORDER/2)
4286 /* cb settings */
4288 #define CB_NSTAGES 3
4289 #define CB_EXPAND 2
4290 #define CB_MEML 147
4291 #define CB_FILTERLEN 2*4
4292 #define CB_HALFFILTERLEN 4
4293 #define CB_RESRANGE 34
4294 #define CB_MAXGAIN (float)1.3
4296 /* enhancer */
4298 #define ENH_BLOCKL 80 /* block length */
4299 #define ENH_BLOCKL_HALF (ENH_BLOCKL/2)
4300 #define ENH_HL 3 /* 2*ENH_HL+1 is number blocks
4301 in said second sequence */
4302 #define ENH_SLOP 2 /* max difference estimated and
4303 correct pitch period */
4304 #define ENH_PLOCSL 20 /* pitch-estimates and pitch-
4305 locations buffer length */
4306 #define ENH_OVERHANG 2
4307 #define ENH_UPS0 4 /* upsampling rate */
4308 #define ENH_FL0 3 /* 2*FLO+1 is the length of
4309 each filter */
4310 #define ENH_VECTL (ENH_BLOCKL+2*ENH_FL0)
4314 Andersen, et al. Experimental [Page 77]
4316 RFC 3951 Internet Low Bit Rate Codec December 2004
4319 #define ENH_CORRDIM (2*ENH_SLOP+1)
4320 #define ENH_NBLOCKS (BLOCKL_MAX/ENH_BLOCKL)
4321 #define ENH_NBLOCKS_EXTRA 5
4322 #define ENH_NBLOCKS_TOT 8 /* ENH_NBLOCKS +
4323 ENH_NBLOCKS_EXTRA */
4324 #define ENH_BUFL (ENH_NBLOCKS_TOT)*ENH_BLOCKL
4325 #define ENH_ALPHA0 (float)0.05
4327 /* Down sampling */
4329 #define FILTERORDER_DS 7
4330 #define DELAY_DS 3
4331 #define FACTOR_DS 2
4333 /* bit stream defs */
4335 #define NO_OF_BYTES_20MS 38
4336 #define NO_OF_BYTES_30MS 50
4337 #define NO_OF_WORDS_20MS 19
4338 #define NO_OF_WORDS_30MS 25
4339 #define STATE_BITS 3
4340 #define BYTE_LEN 8
4341 #define ULP_CLASSES 3
4343 /* help parameters */
4345 #define FLOAT_MAX (float)1.0e37
4346 #define EPS (float)2.220446049250313e-016
4347 #define PI (float)3.14159265358979323846
4348 #define MIN_SAMPLE -32768
4349 #define MAX_SAMPLE 32767
4350 #define TWO_PI (float)6.283185307
4351 #define PI2 (float)0.159154943
4353 /* type definition encoder instance */
4354 typedef struct iLBC_ULP_Inst_t_ {
4355 int lsf_bits[6][ULP_CLASSES+2];
4356 int start_bits[ULP_CLASSES+2];
4357 int startfirst_bits[ULP_CLASSES+2];
4358 int scale_bits[ULP_CLASSES+2];
4359 int state_bits[ULP_CLASSES+2];
4360 int extra_cb_index[CB_NSTAGES][ULP_CLASSES+2];
4361 int extra_cb_gain[CB_NSTAGES][ULP_CLASSES+2];
4362 int cb_index[NSUB_MAX][CB_NSTAGES][ULP_CLASSES+2];
4363 int cb_gain[NSUB_MAX][CB_NSTAGES][ULP_CLASSES+2];
4364 } iLBC_ULP_Inst_t;
4366 /* type definition encoder instance */
4370 Andersen, et al. Experimental [Page 78]
4372 RFC 3951 Internet Low Bit Rate Codec December 2004
4375 typedef struct iLBC_Enc_Inst_t_ {
4377 /* flag for frame size mode */
4378 int mode;
4380 /* basic parameters for different frame sizes */
4381 int blockl;
4382 int nsub;
4383 int nasub;
4384 int no_of_bytes, no_of_words;
4385 int lpc_n;
4386 int state_short_len;
4387 const iLBC_ULP_Inst_t *ULP_inst;
4389 /* analysis filter state */
4390 float anaMem[LPC_FILTERORDER];
4392 /* old lsf parameters for interpolation */
4393 float lsfold[LPC_FILTERORDER];
4394 float lsfdeqold[LPC_FILTERORDER];
4396 /* signal buffer for LP analysis */
4397 float lpc_buffer[LPC_LOOKBACK + BLOCKL_MAX];
4399 /* state of input HP filter */
4400 float hpimem[4];
4402 } iLBC_Enc_Inst_t;
4404 /* type definition decoder instance */
4405 typedef struct iLBC_Dec_Inst_t_ {
4407 /* flag for frame size mode */
4408 int mode;
4410 /* basic parameters for different frame sizes */
4411 int blockl;
4412 int nsub;
4413 int nasub;
4414 int no_of_bytes, no_of_words;
4415 int lpc_n;
4416 int state_short_len;
4417 const iLBC_ULP_Inst_t *ULP_inst;
4419 /* synthesis filter state */
4420 float syntMem[LPC_FILTERORDER];
4422 /* old LSF for interpolation */
4426 Andersen, et al. Experimental [Page 79]
4428 RFC 3951 Internet Low Bit Rate Codec December 2004
4431 float lsfdeqold[LPC_FILTERORDER];
4433 /* pitch lag estimated in enhancer and used in PLC */
4434 int last_lag;
4436 /* PLC state information */
4437 int prevLag, consPLICount, prevPLI, prev_enh_pl;
4438 float prevLpc[LPC_FILTERORDER+1];
4439 float prevResidual[NSUB_MAX*SUBL];
4440 float per;
4441 unsigned long seed;
4443 /* previous synthesis filter parameters */
4444 float old_syntdenum[(LPC_FILTERORDER + 1)*NSUB_MAX];
4446 /* state of output HP filter */
4447 float hpomem[4];
4449 /* enhancer state information */
4450 int use_enhancer;
4451 float enh_buf[ENH_BUFL];
4452 float enh_period[ENH_NBLOCKS_TOT];
4454 } iLBC_Dec_Inst_t;
4456 #endif
4458 A.7. constants.h
4460 /******************************************************************
4462 iLBC Speech Coder ANSI-C Source Code
4464 constants.h
4466 Copyright (C) The Internet Society (2004).
4467 All Rights Reserved.
4469 ******************************************************************/
4471 #ifndef __iLBC_CONSTANTS_H
4472 #define __iLBC_CONSTANTS_H
4474 #include "iLBC_define.h"
4477 /* ULP bit allocation */
4482 Andersen, et al. Experimental [Page 80]
4484 RFC 3951 Internet Low Bit Rate Codec December 2004
4487 extern const iLBC_ULP_Inst_t ULP_20msTbl;
4488 extern const iLBC_ULP_Inst_t ULP_30msTbl;
4490 /* high pass filters */
4492 extern float hpi_zero_coefsTbl[];
4493 extern float hpi_pole_coefsTbl[];
4494 extern float hpo_zero_coefsTbl[];
4495 extern float hpo_pole_coefsTbl[];
4497 /* low pass filters */
4498 extern float lpFilt_coefsTbl[];
4500 /* LPC analysis and quantization */
4502 extern float lpc_winTbl[];
4503 extern float lpc_asymwinTbl[];
4504 extern float lpc_lagwinTbl[];
4505 extern float lsfCbTbl[];
4506 extern float lsfmeanTbl[];
4507 extern int dim_lsfCbTbl[];
4508 extern int size_lsfCbTbl[];
4509 extern float lsf_weightTbl_30ms[];
4510 extern float lsf_weightTbl_20ms[];
4512 /* state quantization tables */
4514 extern float state_sq3Tbl[];
4515 extern float state_frgqTbl[];
4517 /* gain quantization tables */
4519 extern float gain_sq3Tbl[];
4520 extern float gain_sq4Tbl[];
4521 extern float gain_sq5Tbl[];
4523 /* adaptive codebook definitions */
4525 extern int search_rangeTbl[5][CB_NSTAGES];
4526 extern int memLfTbl[];
4527 extern int stMemLTbl;
4528 extern float cbfiltersTbl[CB_FILTERLEN];
4530 /* enhancer definitions */
4532 extern float polyphaserTbl[];
4533 extern float enh_plocsTbl[];
4538 Andersen, et al. Experimental [Page 81]
4540 RFC 3951 Internet Low Bit Rate Codec December 2004
4543 #endif
4545 A.8. constants.c
4547 /******************************************************************
4549 iLBC Speech Coder ANSI-C Source Code
4551 constants.c
4553 Copyright (C) The Internet Society (2004).
4554 All Rights Reserved.
4556 ******************************************************************/
4558 #include "iLBC_define.h"
4560 /* ULP bit allocation */
4562 /* 20 ms frame */
4564 const iLBC_ULP_Inst_t ULP_20msTbl = {
4565 /* LSF */
4566 { {6,0,0,0,0}, {7,0,0,0,0}, {7,0,0,0,0},
4567 {0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0}},
4568 /* Start state location, gain and samples */
4569 {2,0,0,0,0},
4570 {1,0,0,0,0},
4571 {6,0,0,0,0},
4572 {0,1,2,0,0},
4573 /* extra CB index and extra CB gain */
4574 {{6,0,1,0,0}, {0,0,7,0,0}, {0,0,7,0,0}},
4575 {{2,0,3,0,0}, {1,1,2,0,0}, {0,0,3,0,0}},
4576 /* CB index and CB gain */
4577 { {{7,0,1,0,0}, {0,0,7,0,0}, {0,0,7,0,0}},
4578 {{0,0,8,0,0}, {0,0,8,0,0}, {0,0,8,0,0}},
4579 {{0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0}},
4580 {{0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0}}},
4581 { {{1,2,2,0,0}, {1,1,2,0,0}, {0,0,3,0,0}},
4582 {{1,1,3,0,0}, {0,2,2,0,0}, {0,0,3,0,0}},
4583 {{0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0}},
4584 {{0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0}}}
4585 };
4587 /* 30 ms frame */
4589 const iLBC_ULP_Inst_t ULP_30msTbl = {
4590 /* LSF */
4594 Andersen, et al. Experimental [Page 82]
4596 RFC 3951 Internet Low Bit Rate Codec December 2004
4599 { {6,0,0,0,0}, {7,0,0,0,0}, {7,0,0,0,0},
4600 {6,0,0,0,0}, {7,0,0,0,0}, {7,0,0,0,0}},
4601 /* Start state location, gain and samples */
4602 {3,0,0,0,0},
4603 {1,0,0,0,0},
4604 {6,0,0,0,0},
4605 {0,1,2,0,0},
4606 /* extra CB index and extra CB gain */
4607 {{4,2,1,0,0}, {0,0,7,0,0}, {0,0,7,0,0}},
4608 {{1,1,3,0,0}, {1,1,2,0,0}, {0,0,3,0,0}},
4609 /* CB index and CB gain */
4610 { {{6,1,1,0,0}, {0,0,7,0,0}, {0,0,7,0,0}},
4611 {{0,7,1,0,0}, {0,0,8,0,0}, {0,0,8,0,0}},
4612 {{0,7,1,0,0}, {0,0,8,0,0}, {0,0,8,0,0}},
4613 {{0,7,1,0,0}, {0,0,8,0,0}, {0,0,8,0,0}}},
4614 { {{1,2,2,0,0}, {1,2,1,0,0}, {0,0,3,0,0}},
4615 {{0,2,3,0,0}, {0,2,2,0,0}, {0,0,3,0,0}},
4616 {{0,1,4,0,0}, {0,1,3,0,0}, {0,0,3,0,0}},
4617 {{0,1,4,0,0}, {0,1,3,0,0}, {0,0,3,0,0}}}
4618 };
4620 /* HP Filters */
4622 float hpi_zero_coefsTbl[3] = {
4623 (float)0.92727436, (float)-1.8544941, (float)0.92727436
4624 };
4625 float hpi_pole_coefsTbl[3] = {
4626 (float)1.0, (float)-1.9059465, (float)0.9114024
4627 };
4628 float hpo_zero_coefsTbl[3] = {
4629 (float)0.93980581, (float)-1.8795834, (float)0.93980581
4630 };
4631 float hpo_pole_coefsTbl[3] = {
4632 (float)1.0, (float)-1.9330735, (float)0.93589199
4633 };
4635 /* LP Filter */
4637 float lpFilt_coefsTbl[FILTERORDER_DS]={
4638 (float)-0.066650, (float)0.125000, (float)0.316650,
4639 (float)0.414063, (float)0.316650,
4640 (float)0.125000, (float)-0.066650
4641 };
4643 /* State quantization tables */
4645 float state_sq3Tbl[8] = {
4646 (float)-3.719849, (float)-2.177490, (float)-1.130005,
4650 Andersen, et al. Experimental [Page 83]
4652 RFC 3951 Internet Low Bit Rate Codec December 2004
4655 (float)-0.309692, (float)0.444214, (float)1.329712,
4656 (float)2.436279, (float)3.983887
4657 };
4659 float state_frgqTbl[64] = {
4660 (float)1.000085, (float)1.071695, (float)1.140395,
4661 (float)1.206868, (float)1.277188, (float)1.351503,
4662 (float)1.429380, (float)1.500727, (float)1.569049,
4663 (float)1.639599, (float)1.707071, (float)1.781531,
4664 (float)1.840799, (float)1.901550, (float)1.956695,
4665 (float)2.006750, (float)2.055474, (float)2.102787,
4666 (float)2.142819, (float)2.183592, (float)2.217962,
4667 (float)2.257177, (float)2.295739, (float)2.332967,
4668 (float)2.369248, (float)2.402792, (float)2.435080,
4669 (float)2.468598, (float)2.503394, (float)2.539284,
4670 (float)2.572944, (float)2.605036, (float)2.636331,
4671 (float)2.668939, (float)2.698780, (float)2.729101,
4672 (float)2.759786, (float)2.789834, (float)2.818679,
4673 (float)2.848074, (float)2.877470, (float)2.906899,
4674 (float)2.936655, (float)2.967804, (float)3.000115,
4675 (float)3.033367, (float)3.066355, (float)3.104231,
4676 (float)3.141499, (float)3.183012, (float)3.222952,
4677 (float)3.265433, (float)3.308441, (float)3.350823,
4678 (float)3.395275, (float)3.442793, (float)3.490801,
4679 (float)3.542514, (float)3.604064, (float)3.666050,
4680 (float)3.740994, (float)3.830749, (float)3.938770,
4681 (float)4.101764
4682 };
4684 /* CB tables */
4686 int search_rangeTbl[5][CB_NSTAGES]={{58,58,58}, {108,44,44},
4687 {108,108,108}, {108,108,108}, {108,108,108}};
4688 int stMemLTbl=85;
4689 int memLfTbl[NASUB_MAX]={147,147,147,147};
4691 /* expansion filter(s) */
4693 float cbfiltersTbl[CB_FILTERLEN]={
4694 (float)-0.034180, (float)0.108887, (float)-0.184326,
4695 (float)0.806152, (float)0.713379, (float)-0.144043,
4696 (float)0.083740, (float)-0.033691
4697 };
4699 /* Gain Quantization */
4701 float gain_sq3Tbl[8]={
4702 (float)-1.000000, (float)-0.659973, (float)-0.330017,
4706 Andersen, et al. Experimental [Page 84]
4708 RFC 3951 Internet Low Bit Rate Codec December 2004
4711 (float)0.000000, (float)0.250000, (float)0.500000,
4712 (float)0.750000, (float)1.00000};
4714 float gain_sq4Tbl[16]={
4715 (float)-1.049988, (float)-0.900024, (float)-0.750000,
4716 (float)-0.599976, (float)-0.450012, (float)-0.299988,
4717 (float)-0.150024, (float)0.000000, (float)0.150024,
4718 (float)0.299988, (float)0.450012, (float)0.599976,
4719 (float)0.750000, (float)0.900024, (float)1.049988,
4720 (float)1.200012};
4722 float gain_sq5Tbl[32]={
4723 (float)0.037476, (float)0.075012, (float)0.112488,
4724 (float)0.150024, (float)0.187500, (float)0.224976,
4725 (float)0.262512, (float)0.299988, (float)0.337524,
4726 (float)0.375000, (float)0.412476, (float)0.450012,
4727 (float)0.487488, (float)0.525024, (float)0.562500,
4728 (float)0.599976, (float)0.637512, (float)0.674988,
4729 (float)0.712524, (float)0.750000, (float)0.787476,
4730 (float)0.825012, (float)0.862488, (float)0.900024,
4731 (float)0.937500, (float)0.974976, (float)1.012512,
4732 (float)1.049988, (float)1.087524, (float)1.125000,
4733 (float)1.162476, (float)1.200012};
4735 /* Enhancer - Upsamling a factor 4 (ENH_UPS0 = 4) */
4736 float polyphaserTbl[ENH_UPS0*(2*ENH_FL0+1)]={
4737 (float)0.000000, (float)0.000000, (float)0.000000,
4738 (float)1.000000,
4739 (float)0.000000, (float)0.000000, (float)0.000000,
4740 (float)0.015625, (float)-0.076904, (float)0.288330,
4741 (float)0.862061,
4742 (float)-0.106445, (float)0.018799, (float)-0.015625,
4743 (float)0.023682, (float)-0.124268, (float)0.601563,
4744 (float)0.601563,
4745 (float)-0.124268, (float)0.023682, (float)-0.023682,
4746 (float)0.018799, (float)-0.106445, (float)0.862061,
4747 (float)0.288330,
4748 (float)-0.076904, (float)0.015625, (float)-0.018799};
4750 float enh_plocsTbl[ENH_NBLOCKS_TOT] = {(float)40.0, (float)120.0,
4751 (float)200.0, (float)280.0, (float)360.0,
4752 (float)440.0, (float)520.0, (float)600.0};
4754 /* LPC analysis and quantization */
4756 int dim_lsfCbTbl[LSF_NSPLIT] = {3, 3, 4};
4757 int size_lsfCbTbl[LSF_NSPLIT] = {64,128,128};
4762 Andersen, et al. Experimental [Page 85]
4764 RFC 3951 Internet Low Bit Rate Codec December 2004
4767 float lsfmeanTbl[LPC_FILTERORDER] = {
4768 (float)0.281738, (float)0.445801, (float)0.663330,
4769 (float)0.962524, (float)1.251831, (float)1.533081,
4770 (float)1.850586, (float)2.137817, (float)2.481445,
4771 (float)2.777344};
4773 float lsf_weightTbl_30ms[6] = {(float)(1.0/2.0), (float)1.0,
4774 (float)(2.0/3.0),
4775 (float)(1.0/3.0), (float)0.0, (float)0.0};
4777 float lsf_weightTbl_20ms[4] = {(float)(3.0/4.0), (float)(2.0/4.0),
4778 (float)(1.0/4.0), (float)(0.0)};
4780 /* Hanning LPC window */
4781 float lpc_winTbl[BLOCKL_MAX]={
4782 (float)0.000183, (float)0.000671, (float)0.001526,
4783 (float)0.002716, (float)0.004242, (float)0.006104,
4784 (float)0.008301, (float)0.010834, (float)0.013702,
4785 (float)0.016907, (float)0.020416, (float)0.024261,
4786 (float)0.028442, (float)0.032928, (float)0.037750,
4787 (float)0.042877, (float)0.048309, (float)0.054047,
4788 (float)0.060089, (float)0.066437, (float)0.073090,
4789 (float)0.080017, (float)0.087219, (float)0.094727,
4790 (float)0.102509, (float)0.110535, (float)0.118835,
4791 (float)0.127411, (float)0.136230, (float)0.145294,
4792 (float)0.154602, (float)0.164154, (float)0.173920,
4793 (float)0.183899, (float)0.194122, (float)0.204529,
4794 (float)0.215149, (float)0.225952, (float)0.236938,
4795 (float)0.248108, (float)0.259460, (float)0.270966,
4796 (float)0.282654, (float)0.294464, (float)0.306396,
4797 (float)0.318481, (float)0.330688, (float)0.343018,
4798 (float)0.355438, (float)0.367981, (float)0.380585,
4799 (float)0.393280, (float)0.406067, (float)0.418884,
4800 (float)0.431763, (float)0.444702, (float)0.457672,
4801 (float)0.470673, (float)0.483704, (float)0.496735,
4802 (float)0.509766, (float)0.522797, (float)0.535828,
4803 (float)0.548798, (float)0.561768, (float)0.574677,
4804 (float)0.587524, (float)0.600342, (float)0.613068,
4805 (float)0.625732, (float)0.638306, (float)0.650787,
4806 (float)0.663147, (float)0.675415, (float)0.687561,
4807 (float)0.699585, (float)0.711487, (float)0.723206,
4808 (float)0.734802, (float)0.746216, (float)0.757477,
4809 (float)0.768585, (float)0.779480, (float)0.790192,
4810 (float)0.800720, (float)0.811005, (float)0.821106,
4811 (float)0.830994, (float)0.840668, (float)0.850067,
4812 (float)0.859253, (float)0.868225, (float)0.876892,
4813 (float)0.885345, (float)0.893524, (float)0.901428,
4814 (float)0.909058, (float)0.916412, (float)0.923492,
4818 Andersen, et al. Experimental [Page 86]
4820 RFC 3951 Internet Low Bit Rate Codec December 2004
4823 (float)0.930267, (float)0.936768, (float)0.942963,
4824 (float)0.948853, (float)0.954437, (float)0.959717,
4825 (float)0.964691, (float)0.969360, (float)0.973694,
4826 (float)0.977692, (float)0.981384, (float)0.984741,
4827 (float)0.987762, (float)0.990479, (float)0.992828,
4828 (float)0.994873, (float)0.996552, (float)0.997925,
4829 (float)0.998932, (float)0.999603, (float)0.999969,
4830 (float)0.999969, (float)0.999603, (float)0.998932,
4831 (float)0.997925, (float)0.996552, (float)0.994873,
4832 (float)0.992828, (float)0.990479, (float)0.987762,
4833 (float)0.984741, (float)0.981384, (float)0.977692,
4834 (float)0.973694, (float)0.969360, (float)0.964691,
4835 (float)0.959717, (float)0.954437, (float)0.948853,
4836 (float)0.942963, (float)0.936768, (float)0.930267,
4837 (float)0.923492, (float)0.916412, (float)0.909058,
4838 (float)0.901428, (float)0.893524, (float)0.885345,
4839 (float)0.876892, (float)0.868225, (float)0.859253,
4840 (float)0.850067, (float)0.840668, (float)0.830994,
4841 (float)0.821106, (float)0.811005, (float)0.800720,
4842 (float)0.790192, (float)0.779480, (float)0.768585,
4843 (float)0.757477, (float)0.746216, (float)0.734802,
4844 (float)0.723206, (float)0.711487, (float)0.699585,
4845 (float)0.687561, (float)0.675415, (float)0.663147,
4846 (float)0.650787, (float)0.638306, (float)0.625732,
4847 (float)0.613068, (float)0.600342, (float)0.587524,
4848 (float)0.574677, (float)0.561768, (float)0.548798,
4849 (float)0.535828, (float)0.522797, (float)0.509766,
4850 (float)0.496735, (float)0.483704, (float)0.470673,
4851 (float)0.457672, (float)0.444702, (float)0.431763,
4852 (float)0.418884, (float)0.406067, (float)0.393280,
4853 (float)0.380585, (float)0.367981, (float)0.355438,
4854 (float)0.343018, (float)0.330688, (float)0.318481,
4855 (float)0.306396, (float)0.294464, (float)0.282654,
4856 (float)0.270966, (float)0.259460, (float)0.248108,
4857 (float)0.236938, (float)0.225952, (float)0.215149,
4858 (float)0.204529, (float)0.194122, (float)0.183899,
4859 (float)0.173920, (float)0.164154, (float)0.154602,
4860 (float)0.145294, (float)0.136230, (float)0.127411,
4861 (float)0.118835, (float)0.110535, (float)0.102509,
4862 (float)0.094727, (float)0.087219, (float)0.080017,
4863 (float)0.073090, (float)0.066437, (float)0.060089,
4864 (float)0.054047, (float)0.048309, (float)0.042877,
4865 (float)0.037750, (float)0.032928, (float)0.028442,
4866 (float)0.024261, (float)0.020416, (float)0.016907,
4867 (float)0.013702, (float)0.010834, (float)0.008301,
4868 (float)0.006104, (float)0.004242, (float)0.002716,
4869 (float)0.001526, (float)0.000671, (float)0.000183
4870 };
4874 Andersen, et al. Experimental [Page 87]
4876 RFC 3951 Internet Low Bit Rate Codec December 2004
4879 /* Asymmetric LPC window */
4880 float lpc_asymwinTbl[BLOCKL_MAX]={
4881 (float)0.000061, (float)0.000214, (float)0.000458,
4882 (float)0.000824, (float)0.001282, (float)0.001831,
4883 (float)0.002472, (float)0.003235, (float)0.004120,
4884 (float)0.005066, (float)0.006134, (float)0.007294,
4885 (float)0.008545, (float)0.009918, (float)0.011383,
4886 (float)0.012939, (float)0.014587, (float)0.016357,
4887 (float)0.018219, (float)0.020172, (float)0.022217,
4888 (float)0.024353, (float)0.026611, (float)0.028961,
4889 (float)0.031372, (float)0.033905, (float)0.036530,
4890 (float)0.039276, (float)0.042084, (float)0.044983,
4891 (float)0.047974, (float)0.051086, (float)0.054260,
4892 (float)0.057526, (float)0.060883, (float)0.064331,
4893 (float)0.067871, (float)0.071503, (float)0.075226,
4894 (float)0.079010, (float)0.082916, (float)0.086884,
4895 (float)0.090942, (float)0.095062, (float)0.099304,
4896 (float)0.103607, (float)0.107971, (float)0.112427,
4897 (float)0.116974, (float)0.121582, (float)0.126282,
4898 (float)0.131073, (float)0.135895, (float)0.140839,
4899 (float)0.145813, (float)0.150879, (float)0.156006,
4900 (float)0.161224, (float)0.166504, (float)0.171844,
4901 (float)0.177246, (float)0.182709, (float)0.188263,
4902 (float)0.193848, (float)0.199524, (float)0.205231,
4903 (float)0.211029, (float)0.216858, (float)0.222778,
4904 (float)0.228729, (float)0.234741, (float)0.240814,
4905 (float)0.246918, (float)0.253082, (float)0.259308,
4906 (float)0.265564, (float)0.271881, (float)0.278259,
4907 (float)0.284668, (float)0.291107, (float)0.297607,
4908 (float)0.304138, (float)0.310730, (float)0.317322,
4909 (float)0.323975, (float)0.330658, (float)0.337372,
4910 (float)0.344147, (float)0.350922, (float)0.357727,
4911 (float)0.364594, (float)0.371460, (float)0.378357,
4912 (float)0.385284, (float)0.392212, (float)0.399170,
4913 (float)0.406158, (float)0.413177, (float)0.420197,
4914 (float)0.427246, (float)0.434296, (float)0.441376,
4915 (float)0.448456, (float)0.455536, (float)0.462646,
4916 (float)0.469757, (float)0.476868, (float)0.483978,
4917 (float)0.491089, (float)0.498230, (float)0.505341,
4918 (float)0.512451, (float)0.519592, (float)0.526703,
4919 (float)0.533813, (float)0.540924, (float)0.548004,
4920 (float)0.555084, (float)0.562164, (float)0.569244,
4921 (float)0.576294, (float)0.583313, (float)0.590332,
4922 (float)0.597321, (float)0.604309, (float)0.611267,
4923 (float)0.618195, (float)0.625092, (float)0.631989,
4924 (float)0.638855, (float)0.645660, (float)0.652466,
4925 (float)0.659241, (float)0.665985, (float)0.672668,
4926 (float)0.679352, (float)0.685974, (float)0.692566,
4930 Andersen, et al. Experimental [Page 88]
4932 RFC 3951 Internet Low Bit Rate Codec December 2004
4935 (float)0.699127, (float)0.705658, (float)0.712128,
4936 (float)0.718536, (float)0.724945, (float)0.731262,
4937 (float)0.737549, (float)0.743805, (float)0.750000,
4938 (float)0.756134, (float)0.762238, (float)0.768280,
4939 (float)0.774261, (float)0.780182, (float)0.786072,
4940 (float)0.791870, (float)0.797638, (float)0.803314,
4941 (float)0.808960, (float)0.814514, (float)0.820038,
4942 (float)0.825470, (float)0.830841, (float)0.836151,
4943 (float)0.841400, (float)0.846558, (float)0.851654,
4944 (float)0.856689, (float)0.861633, (float)0.866516,
4945 (float)0.871338, (float)0.876068, (float)0.880737,
4946 (float)0.885315, (float)0.889801, (float)0.894226,
4947 (float)0.898560, (float)0.902832, (float)0.907013,
4948 (float)0.911102, (float)0.915100, (float)0.919037,
4949 (float)0.922882, (float)0.926636, (float)0.930328,
4950 (float)0.933899, (float)0.937408, (float)0.940796,
4951 (float)0.944122, (float)0.947357, (float)0.950470,
4952 (float)0.953522, (float)0.956482, (float)0.959351,
4953 (float)0.962097, (float)0.964783, (float)0.967377,
4954 (float)0.969849, (float)0.972229, (float)0.974518,
4955 (float)0.976715, (float)0.978821, (float)0.980835,
4956 (float)0.982727, (float)0.984528, (float)0.986237,
4957 (float)0.987854, (float)0.989380, (float)0.990784,
4958 (float)0.992096, (float)0.993317, (float)0.994415,
4959 (float)0.995422, (float)0.996338, (float)0.997162,
4960 (float)0.997864, (float)0.998474, (float)0.998962,
4961 (float)0.999390, (float)0.999695, (float)0.999878,
4962 (float)0.999969, (float)0.999969, (float)0.996918,
4963 (float)0.987701, (float)0.972382, (float)0.951050,
4964 (float)0.923889, (float)0.891022, (float)0.852631,
4965 (float)0.809021, (float)0.760406, (float)0.707092,
4966 (float)0.649445, (float)0.587799, (float)0.522491,
4967 (float)0.453979, (float)0.382690, (float)0.309021,
4968 (float)0.233459, (float)0.156433, (float)0.078461
4969 };
4971 /* Lag window for LPC */
4972 float lpc_lagwinTbl[LPC_FILTERORDER + 1]={
4973 (float)1.000100, (float)0.998890, (float)0.995569,
4974 (float)0.990057, (float)0.982392,
4975 (float)0.972623, (float)0.960816, (float)0.947047,
4976 (float)0.931405, (float)0.913989, (float)0.894909};
4978 /* LSF quantization*/
4979 float lsfCbTbl[64 * 3 + 128 * 3 + 128 * 4] = {
4980 (float)0.155396, (float)0.273193, (float)0.451172,
4981 (float)0.390503, (float)0.648071, (float)1.002075,
4982 (float)0.440186, (float)0.692261, (float)0.955688,
4986 Andersen, et al. Experimental [Page 89]
4988 RFC 3951 Internet Low Bit Rate Codec December 2004
4991 (float)0.343628, (float)0.642334, (float)1.071533,
4992 (float)0.318359, (float)0.491577, (float)0.670532,
4993 (float)0.193115, (float)0.375488, (float)0.725708,
4994 (float)0.364136, (float)0.510376, (float)0.658691,
4995 (float)0.297485, (float)0.527588, (float)0.842529,
4996 (float)0.227173, (float)0.365967, (float)0.563110,
4997 (float)0.244995, (float)0.396729, (float)0.636475,
4998 (float)0.169434, (float)0.300171, (float)0.520264,
4999 (float)0.312866, (float)0.464478, (float)0.643188,
5000 (float)0.248535, (float)0.429932, (float)0.626099,
5001 (float)0.236206, (float)0.491333, (float)0.817139,
5002 (float)0.334961, (float)0.625122, (float)0.895752,
5003 (float)0.343018, (float)0.518555, (float)0.698608,
5004 (float)0.372803, (float)0.659790, (float)0.945435,
5005 (float)0.176880, (float)0.316528, (float)0.581421,
5006 (float)0.416382, (float)0.625977, (float)0.805176,
5007 (float)0.303223, (float)0.568726, (float)0.915039,
5008 (float)0.203613, (float)0.351440, (float)0.588135,
5009 (float)0.221191, (float)0.375000, (float)0.614746,
5010 (float)0.199951, (float)0.323364, (float)0.476074,
5011 (float)0.300781, (float)0.433350, (float)0.566895,
5012 (float)0.226196, (float)0.354004, (float)0.507568,
5013 (float)0.300049, (float)0.508179, (float)0.711670,
5014 (float)0.312012, (float)0.492676, (float)0.763428,
5015 (float)0.329956, (float)0.541016, (float)0.795776,
5016 (float)0.373779, (float)0.604614, (float)0.928833,
5017 (float)0.210571, (float)0.452026, (float)0.755249,
5018 (float)0.271118, (float)0.473267, (float)0.662476,
5019 (float)0.285522, (float)0.436890, (float)0.634399,
5020 (float)0.246704, (float)0.565552, (float)0.859009,
5021 (float)0.270508, (float)0.406250, (float)0.553589,
5022 (float)0.361450, (float)0.578491, (float)0.813843,
5023 (float)0.342651, (float)0.482788, (float)0.622437,
5024 (float)0.340332, (float)0.549438, (float)0.743164,
5025 (float)0.200439, (float)0.336304, (float)0.540894,
5026 (float)0.407837, (float)0.644775, (float)0.895142,
5027 (float)0.294678, (float)0.454834, (float)0.699097,
5028 (float)0.193115, (float)0.344482, (float)0.643188,
5029 (float)0.275757, (float)0.420776, (float)0.598755,
5030 (float)0.380493, (float)0.608643, (float)0.861084,
5031 (float)0.222778, (float)0.426147, (float)0.676514,
5032 (float)0.407471, (float)0.700195, (float)1.053101,
5033 (float)0.218384, (float)0.377197, (float)0.669922,
5034 (float)0.313232, (float)0.454102, (float)0.600952,
5035 (float)0.347412, (float)0.571533, (float)0.874146,
5036 (float)0.238037, (float)0.405396, (float)0.729492,
5037 (float)0.223877, (float)0.412964, (float)0.822021,
5038 (float)0.395264, (float)0.582153, (float)0.743896,
5042 Andersen, et al. Experimental [Page 90]
5044 RFC 3951 Internet Low Bit Rate Codec December 2004
5047 (float)0.247925, (float)0.485596, (float)0.720581,
5048 (float)0.229126, (float)0.496582, (float)0.907715,
5049 (float)0.260132, (float)0.566895, (float)1.012695,
5050 (float)0.337402, (float)0.611572, (float)0.978149,
5051 (float)0.267822, (float)0.447632, (float)0.769287,
5052 (float)0.250610, (float)0.381714, (float)0.530029,
5053 (float)0.430054, (float)0.805054, (float)1.221924,
5054 (float)0.382568, (float)0.544067, (float)0.701660,
5055 (float)0.383545, (float)0.710327, (float)1.149170,
5056 (float)0.271362, (float)0.529053, (float)0.775513,
5057 (float)0.246826, (float)0.393555, (float)0.588623,
5058 (float)0.266846, (float)0.422119, (float)0.676758,
5059 (float)0.311523, (float)0.580688, (float)0.838623,
5060 (float)1.331177, (float)1.576782, (float)1.779541,
5061 (float)1.160034, (float)1.401978, (float)1.768188,
5062 (float)1.161865, (float)1.525146, (float)1.715332,
5063 (float)0.759521, (float)0.913940, (float)1.119873,
5064 (float)0.947144, (float)1.121338, (float)1.282471,
5065 (float)1.015015, (float)1.557007, (float)1.804932,
5066 (float)1.172974, (float)1.402100, (float)1.692627,
5067 (float)1.087524, (float)1.474243, (float)1.665405,
5068 (float)0.899536, (float)1.105225, (float)1.406250,
5069 (float)1.148438, (float)1.484741, (float)1.796265,
5070 (float)0.785645, (float)1.209839, (float)1.567749,
5071 (float)0.867798, (float)1.166504, (float)1.450684,
5072 (float)0.922485, (float)1.229858, (float)1.420898,
5073 (float)0.791260, (float)1.123291, (float)1.409546,
5074 (float)0.788940, (float)0.966064, (float)1.340332,
5075 (float)1.051147, (float)1.272827, (float)1.556641,
5076 (float)0.866821, (float)1.181152, (float)1.538818,
5077 (float)0.906738, (float)1.373535, (float)1.607910,
5078 (float)1.244751, (float)1.581421, (float)1.933838,
5079 (float)0.913940, (float)1.337280, (float)1.539673,
5080 (float)0.680542, (float)0.959229, (float)1.662720,
5081 (float)0.887207, (float)1.430542, (float)1.800781,
5082 (float)0.912598, (float)1.433594, (float)1.683960,
5083 (float)0.860474, (float)1.060303, (float)1.455322,
5084 (float)1.005127, (float)1.381104, (float)1.706909,
5085 (float)0.800781, (float)1.363892, (float)1.829102,
5086 (float)0.781860, (float)1.124390, (float)1.505981,
5087 (float)1.003662, (float)1.471436, (float)1.684692,
5088 (float)0.981323, (float)1.309570, (float)1.618042,
5089 (float)1.228760, (float)1.554321, (float)1.756470,
5090 (float)0.734375, (float)0.895752, (float)1.225586,
5091 (float)0.841797, (float)1.055664, (float)1.249268,
5092 (float)0.920166, (float)1.119385, (float)1.486206,
5093 (float)0.894409, (float)1.539063, (float)1.828979,
5094 (float)1.283691, (float)1.543335, (float)1.858276,
5098 Andersen, et al. Experimental [Page 91]
5100 RFC 3951 Internet Low Bit Rate Codec December 2004
5103 (float)0.676025, (float)0.933105, (float)1.490845,
5104 (float)0.821289, (float)1.491821, (float)1.739868,
5105 (float)0.923218, (float)1.144653, (float)1.580566,
5106 (float)1.057251, (float)1.345581, (float)1.635864,
5107 (float)0.888672, (float)1.074951, (float)1.353149,
5108 (float)0.942749, (float)1.195435, (float)1.505493,
5109 (float)1.492310, (float)1.788086, (float)2.039673,
5110 (float)1.070313, (float)1.634399, (float)1.860962,
5111 (float)1.253296, (float)1.488892, (float)1.686035,
5112 (float)0.647095, (float)0.864014, (float)1.401855,
5113 (float)0.866699, (float)1.254883, (float)1.453369,
5114 (float)1.063965, (float)1.532593, (float)1.731323,
5115 (float)1.167847, (float)1.521484, (float)1.884033,
5116 (float)0.956055, (float)1.502075, (float)1.745605,
5117 (float)0.928711, (float)1.288574, (float)1.479614,
5118 (float)1.088013, (float)1.380737, (float)1.570801,
5119 (float)0.905029, (float)1.186768, (float)1.371948,
5120 (float)1.057861, (float)1.421021, (float)1.617432,
5121 (float)1.108276, (float)1.312500, (float)1.501465,
5122 (float)0.979492, (float)1.416992, (float)1.624268,
5123 (float)1.276001, (float)1.661011, (float)2.007935,
5124 (float)0.993042, (float)1.168579, (float)1.331665,
5125 (float)0.778198, (float)0.944946, (float)1.235962,
5126 (float)1.223755, (float)1.491333, (float)1.815674,
5127 (float)0.852661, (float)1.350464, (float)1.722290,
5128 (float)1.134766, (float)1.593140, (float)1.787354,
5129 (float)1.051392, (float)1.339722, (float)1.531006,
5130 (float)0.803589, (float)1.271240, (float)1.652100,
5131 (float)0.755737, (float)1.143555, (float)1.639404,
5132 (float)0.700928, (float)0.837280, (float)1.130371,
5133 (float)0.942749, (float)1.197876, (float)1.669800,
5134 (float)0.993286, (float)1.378296, (float)1.566528,
5135 (float)0.801025, (float)1.095337, (float)1.298950,
5136 (float)0.739990, (float)1.032959, (float)1.383667,
5137 (float)0.845703, (float)1.072266, (float)1.543823,
5138 (float)0.915649, (float)1.072266, (float)1.224487,
5139 (float)1.021973, (float)1.226196, (float)1.481323,
5140 (float)0.999878, (float)1.204102, (float)1.555908,
5141 (float)0.722290, (float)0.913940, (float)1.340210,
5142 (float)0.673340, (float)0.835938, (float)1.259521,
5143 (float)0.832397, (float)1.208374, (float)1.394165,
5144 (float)0.962158, (float)1.576172, (float)1.912842,
5145 (float)1.166748, (float)1.370850, (float)1.556763,
5146 (float)0.946289, (float)1.138550, (float)1.400391,
5147 (float)1.035034, (float)1.218262, (float)1.386475,
5148 (float)1.393799, (float)1.717773, (float)2.000244,
5149 (float)0.972656, (float)1.260986, (float)1.760620,
5150 (float)1.028198, (float)1.288452, (float)1.484619,
5154 Andersen, et al. Experimental [Page 92]
5156 RFC 3951 Internet Low Bit Rate Codec December 2004
5159 (float)0.773560, (float)1.258057, (float)1.756714,
5160 (float)1.080322, (float)1.328003, (float)1.742676,
5161 (float)0.823975, (float)1.450806, (float)1.917725,
5162 (float)0.859009, (float)1.016602, (float)1.191895,
5163 (float)0.843994, (float)1.131104, (float)1.645020,
5164 (float)1.189697, (float)1.702759, (float)1.894409,
5165 (float)1.346680, (float)1.763184, (float)2.066040,
5166 (float)0.980469, (float)1.253784, (float)1.441650,
5167 (float)1.338135, (float)1.641968, (float)1.932739,
5168 (float)1.223267, (float)1.424194, (float)1.626465,
5169 (float)0.765747, (float)1.004150, (float)1.579102,
5170 (float)1.042847, (float)1.269165, (float)1.647461,
5171 (float)0.968750, (float)1.257568, (float)1.555786,
5172 (float)0.826294, (float)0.993408, (float)1.275146,
5173 (float)0.742310, (float)0.950439, (float)1.430542,
5174 (float)1.054321, (float)1.439819, (float)1.828003,
5175 (float)1.072998, (float)1.261719, (float)1.441895,
5176 (float)0.859375, (float)1.036377, (float)1.314819,
5177 (float)0.895752, (float)1.267212, (float)1.605591,
5178 (float)0.805420, (float)0.962891, (float)1.142334,
5179 (float)0.795654, (float)1.005493, (float)1.468506,
5180 (float)1.105347, (float)1.313843, (float)1.584839,
5181 (float)0.792236, (float)1.221802, (float)1.465698,
5182 (float)1.170532, (float)1.467651, (float)1.664063,
5183 (float)0.838257, (float)1.153198, (float)1.342163,
5184 (float)0.968018, (float)1.198242, (float)1.391235,
5185 (float)1.250122, (float)1.623535, (float)1.823608,
5186 (float)0.711670, (float)1.058350, (float)1.512085,
5187 (float)1.204834, (float)1.454468, (float)1.739136,
5188 (float)1.137451, (float)1.421753, (float)1.620117,
5189 (float)0.820435, (float)1.322754, (float)1.578247,
5190 (float)0.798706, (float)1.005005, (float)1.213867,
5191 (float)0.980713, (float)1.324951, (float)1.512939,
5192 (float)1.112305, (float)1.438843, (float)1.735596,
5193 (float)1.135498, (float)1.356689, (float)1.635742,
5194 (float)1.101318, (float)1.387451, (float)1.686523,
5195 (float)0.849854, (float)1.276978, (float)1.523438,
5196 (float)1.377930, (float)1.627563, (float)1.858154,
5197 (float)0.884888, (float)1.095459, (float)1.287476,
5198 (float)1.289795, (float)1.505859, (float)1.756592,
5199 (float)0.817505, (float)1.384155, (float)1.650513,
5200 (float)1.446655, (float)1.702148, (float)1.931885,
5201 (float)0.835815, (float)1.023071, (float)1.385376,
5202 (float)0.916626, (float)1.139038, (float)1.335327,
5203 (float)0.980103, (float)1.174072, (float)1.453735,
5204 (float)1.705688, (float)2.153809, (float)2.398315, (float)2.743408,
5205 (float)1.797119, (float)2.016846, (float)2.445679, (float)2.701904,
5206 (float)1.990356, (float)2.219116, (float)2.576416, (float)2.813477,
5210 Andersen, et al. Experimental [Page 93]
5212 RFC 3951 Internet Low Bit Rate Codec December 2004
5215 (float)1.849365, (float)2.190918, (float)2.611572, (float)2.835083,
5216 (float)1.657959, (float)1.854370, (float)2.159058, (float)2.726196,
5217 (float)1.437744, (float)1.897705, (float)2.253174, (float)2.655396,
5218 (float)2.028687, (float)2.247314, (float)2.542358, (float)2.875854,
5219 (float)1.736938, (float)1.922119, (float)2.185913, (float)2.743408,
5220 (float)1.521606, (float)1.870972, (float)2.526855, (float)2.786987,
5221 (float)1.841431, (float)2.050659, (float)2.463623, (float)2.857666,
5222 (float)1.590088, (float)2.067261, (float)2.427979, (float)2.794434,
5223 (float)1.746826, (float)2.057373, (float)2.320190, (float)2.800781,
5224 (float)1.734619, (float)1.940552, (float)2.306030, (float)2.826416,
5225 (float)1.786255, (float)2.204468, (float)2.457520, (float)2.795288,
5226 (float)1.861084, (float)2.170532, (float)2.414551, (float)2.763672,
5227 (float)2.001465, (float)2.307617, (float)2.552734, (float)2.811890,
5228 (float)1.784424, (float)2.124146, (float)2.381592, (float)2.645508,
5229 (float)1.888794, (float)2.135864, (float)2.418579, (float)2.861206,
5230 (float)2.301147, (float)2.531250, (float)2.724976, (float)2.913086,
5231 (float)1.837769, (float)2.051270, (float)2.261963, (float)2.553223,
5232 (float)2.012939, (float)2.221191, (float)2.440186, (float)2.678101,
5233 (float)1.429565, (float)1.858276, (float)2.582275, (float)2.845703,
5234 (float)1.622803, (float)1.897705, (float)2.367310, (float)2.621094,
5235 (float)1.581543, (float)1.960449, (float)2.515869, (float)2.736450,
5236 (float)1.419434, (float)1.933960, (float)2.394653, (float)2.746704,
5237 (float)1.721924, (float)2.059570, (float)2.421753, (float)2.769653,
5238 (float)1.911011, (float)2.220703, (float)2.461060, (float)2.740723,
5239 (float)1.581177, (float)1.860840, (float)2.516968, (float)2.874634,
5240 (float)1.870361, (float)2.098755, (float)2.432373, (float)2.656494,
5241 (float)2.059692, (float)2.279785, (float)2.495605, (float)2.729370,
5242 (float)1.815674, (float)2.181519, (float)2.451538, (float)2.680542,
5243 (float)1.407959, (float)1.768311, (float)2.343018, (float)2.668091,
5244 (float)2.168701, (float)2.394653, (float)2.604736, (float)2.829346,
5245 (float)1.636230, (float)1.865723, (float)2.329102, (float)2.824219,
5246 (float)1.878906, (float)2.139526, (float)2.376709, (float)2.679810,
5247 (float)1.765381, (float)1.971802, (float)2.195435, (float)2.586914,
5248 (float)2.164795, (float)2.410889, (float)2.673706, (float)2.903198,
5249 (float)2.071899, (float)2.331055, (float)2.645874, (float)2.907104,
5250 (float)2.026001, (float)2.311523, (float)2.594849, (float)2.863892,
5251 (float)1.948975, (float)2.180786, (float)2.514893, (float)2.797852,
5252 (float)1.881836, (float)2.130859, (float)2.478149, (float)2.804199,
5253 (float)2.238159, (float)2.452759, (float)2.652832, (float)2.868286,
5254 (float)1.897949, (float)2.101685, (float)2.524292, (float)2.880127,
5255 (float)1.856445, (float)2.074585, (float)2.541016, (float)2.791748,
5256 (float)1.695557, (float)2.199097, (float)2.506226, (float)2.742676,
5257 (float)1.612671, (float)1.877075, (float)2.435425, (float)2.732910,
5258 (float)1.568848, (float)1.786499, (float)2.194580, (float)2.768555,
5259 (float)1.953369, (float)2.164551, (float)2.486938, (float)2.874023,
5260 (float)1.388306, (float)1.725342, (float)2.384521, (float)2.771851,
5261 (float)2.115356, (float)2.337769, (float)2.592896, (float)2.864014,
5262 (float)1.905762, (float)2.111328, (float)2.363525, (float)2.789307,
5266 Andersen, et al. Experimental [Page 94]
5268 RFC 3951 Internet Low Bit Rate Codec December 2004
5271 (float)1.882568, (float)2.332031, (float)2.598267, (float)2.827637,
5272 (float)1.683594, (float)2.088745, (float)2.361938, (float)2.608643,
5273 (float)1.874023, (float)2.182129, (float)2.536133, (float)2.766968,
5274 (float)1.861938, (float)2.070435, (float)2.309692, (float)2.700562,
5275 (float)1.722168, (float)2.107422, (float)2.477295, (float)2.837646,
5276 (float)1.926880, (float)2.184692, (float)2.442627, (float)2.663818,
5277 (float)2.123901, (float)2.337280, (float)2.553101, (float)2.777466,
5278 (float)1.588135, (float)1.911499, (float)2.212769, (float)2.543945,
5279 (float)2.053955, (float)2.370850, (float)2.712158, (float)2.939941,
5280 (float)2.210449, (float)2.519653, (float)2.770386, (float)2.958618,
5281 (float)2.199463, (float)2.474731, (float)2.718262, (float)2.919922,
5282 (float)1.960083, (float)2.175415, (float)2.608032, (float)2.888794,
5283 (float)1.953735, (float)2.185181, (float)2.428223, (float)2.809570,
5284 (float)1.615234, (float)2.036499, (float)2.576538, (float)2.834595,
5285 (float)1.621094, (float)2.028198, (float)2.431030, (float)2.664673,
5286 (float)1.824951, (float)2.267456, (float)2.514526, (float)2.747925,
5287 (float)1.994263, (float)2.229126, (float)2.475220, (float)2.833984,
5288 (float)1.746338, (float)2.011353, (float)2.588257, (float)2.826904,
5289 (float)1.562866, (float)2.135986, (float)2.471680, (float)2.687256,
5290 (float)1.748901, (float)2.083496, (float)2.460938, (float)2.686279,
5291 (float)1.758057, (float)2.131470, (float)2.636597, (float)2.891602,
5292 (float)2.071289, (float)2.299072, (float)2.550781, (float)2.814331,
5293 (float)1.839600, (float)2.094360, (float)2.496460, (float)2.723999,
5294 (float)1.882202, (float)2.088257, (float)2.636841, (float)2.923096,
5295 (float)1.957886, (float)2.153198, (float)2.384399, (float)2.615234,
5296 (float)1.992920, (float)2.351196, (float)2.654419, (float)2.889771,
5297 (float)2.012817, (float)2.262451, (float)2.643799, (float)2.903076,
5298 (float)2.025635, (float)2.254761, (float)2.508423, (float)2.784058,
5299 (float)2.316040, (float)2.589355, (float)2.794189, (float)2.963623,
5300 (float)1.741211, (float)2.279541, (float)2.578491, (float)2.816284,
5301 (float)1.845337, (float)2.055786, (float)2.348511, (float)2.822021,
5302 (float)1.679932, (float)1.926514, (float)2.499756, (float)2.835693,
5303 (float)1.722534, (float)1.946899, (float)2.448486, (float)2.728760,
5304 (float)1.829834, (float)2.043213, (float)2.580444, (float)2.867676,
5305 (float)1.676636, (float)2.071655, (float)2.322510, (float)2.704834,
5306 (float)1.791504, (float)2.113525, (float)2.469727, (float)2.784058,
5307 (float)1.977051, (float)2.215088, (float)2.497437, (float)2.726929,
5308 (float)1.800171, (float)2.106689, (float)2.357788, (float)2.738892,
5309 (float)1.827759, (float)2.170166, (float)2.525879, (float)2.852417,
5310 (float)1.918335, (float)2.132813, (float)2.488403, (float)2.728149,
5311 (float)1.916748, (float)2.225098, (float)2.542603, (float)2.857666,
5312 (float)1.761230, (float)1.976074, (float)2.507446, (float)2.884521,
5313 (float)2.053711, (float)2.367432, (float)2.608032, (float)2.837646,
5314 (float)1.595337, (float)2.000977, (float)2.307129, (float)2.578247,
5315 (float)1.470581, (float)2.031250, (float)2.375854, (float)2.647583,
5316 (float)1.801392, (float)2.128052, (float)2.399780, (float)2.822876,
5317 (float)1.853638, (float)2.066650, (float)2.429199, (float)2.751465,
5318 (float)1.956299, (float)2.163696, (float)2.394775, (float)2.734253,
5322 Andersen, et al. Experimental [Page 95]
5324 RFC 3951 Internet Low Bit Rate Codec December 2004
5327 (float)1.963623, (float)2.275757, (float)2.585327, (float)2.865234,
5328 (float)1.887451, (float)2.105469, (float)2.331787, (float)2.587402,
5329 (float)2.120117, (float)2.443359, (float)2.733887, (float)2.941406,
5330 (float)1.506348, (float)1.766968, (float)2.400513, (float)2.851807,
5331 (float)1.664551, (float)1.981079, (float)2.375732, (float)2.774414,
5332 (float)1.720703, (float)1.978882, (float)2.391479, (float)2.640991,
5333 (float)1.483398, (float)1.814819, (float)2.434448, (float)2.722290,
5334 (float)1.769043, (float)2.136597, (float)2.563721, (float)2.774414,
5335 (float)1.810791, (float)2.049316, (float)2.373901, (float)2.613647,
5336 (float)1.788330, (float)2.005981, (float)2.359131, (float)2.723145,
5337 (float)1.785156, (float)1.993164, (float)2.399780, (float)2.832520,
5338 (float)1.695313, (float)2.022949, (float)2.522583, (float)2.745117,
5339 (float)1.584106, (float)1.965576, (float)2.299927, (float)2.715576,
5340 (float)1.894897, (float)2.249878, (float)2.655884, (float)2.897705,
5341 (float)1.720581, (float)1.995728, (float)2.299438, (float)2.557007,
5342 (float)1.619385, (float)2.173950, (float)2.574219, (float)2.787964,
5343 (float)1.883179, (float)2.220459, (float)2.474365, (float)2.825073,
5344 (float)1.447632, (float)2.045044, (float)2.555542, (float)2.744873,
5345 (float)1.502686, (float)2.156616, (float)2.653320, (float)2.846558,
5346 (float)1.711548, (float)1.944092, (float)2.282959, (float)2.685791,
5347 (float)1.499756, (float)1.867554, (float)2.341064, (float)2.578857,
5348 (float)1.916870, (float)2.135132, (float)2.568237, (float)2.826050,
5349 (float)1.498047, (float)1.711182, (float)2.223267, (float)2.755127,
5350 (float)1.808716, (float)1.997559, (float)2.256470, (float)2.758545,
5351 (float)2.088501, (float)2.402710, (float)2.667358, (float)2.890259,
5352 (float)1.545044, (float)1.819214, (float)2.324097, (float)2.692993,
5353 (float)1.796021, (float)2.012573, (float)2.505737, (float)2.784912,
5354 (float)1.786499, (float)2.041748, (float)2.290405, (float)2.650757,
5355 (float)1.938232, (float)2.264404, (float)2.529053, (float)2.796143
5356 };
5358 A.9. anaFilter.h
5360 /******************************************************************
5362 iLBC Speech Coder ANSI-C Source Code
5364 anaFilter.h
5366 Copyright (C) The Internet Society (2004).
5367 All Rights Reserved.
5369 ******************************************************************/
5371 #ifndef __iLBC_ANAFILTER_H
5372 #define __iLBC_ANAFILTER_H
5374 void anaFilter(
5378 Andersen, et al. Experimental [Page 96]
5380 RFC 3951 Internet Low Bit Rate Codec December 2004
5383 float *In, /* (i) Signal to be filtered */
5384 float *a, /* (i) LP parameters */
5385 int len,/* (i) Length of signal */
5386 float *Out, /* (o) Filtered signal */
5387 float *mem /* (i/o) Filter state */
5388 );
5390 #endif
5392 A.10. anaFilter.c
5394 /******************************************************************
5396 iLBC Speech Coder ANSI-C Source Code
5398 anaFilter.c
5400 Copyright (C) The Internet Society (2004).
5401 All Rights Reserved.
5403 ******************************************************************/
5405 #include <string.h>
5406 #include "iLBC_define.h"
5408 /*----------------------------------------------------------------*
5409 * LP analysis filter.
5410 *---------------------------------------------------------------*/
5412 void anaFilter(
5413 float *In, /* (i) Signal to be filtered */
5414 float *a, /* (i) LP parameters */
5415 int len,/* (i) Length of signal */
5416 float *Out, /* (o) Filtered signal */
5417 float *mem /* (i/o) Filter state */
5418 ){
5419 int i, j;
5420 float *po, *pi, *pm, *pa;
5422 po = Out;
5424 /* Filter first part using memory from past */
5426 for (i=0; i<LPC_FILTERORDER; i++) {
5427 pi = &In[i];
5428 pm = &mem[LPC_FILTERORDER-1];
5429 pa = a;
5430 *po=0.0;
5434 Andersen, et al. Experimental [Page 97]
5436 RFC 3951 Internet Low Bit Rate Codec December 2004
5439 for (j=0; j<=i; j++) {
5440 *po+=(*pa++)*(*pi--);
5441 }
5442 for (j=i+1; j<LPC_FILTERORDER+1; j++) {
5444 *po+=(*pa++)*(*pm--);
5445 }
5446 po++;
5447 }
5449 /* Filter last part where the state is entirely
5450 in the input vector */
5452 for (i=LPC_FILTERORDER; i<len; i++) {
5453 pi = &In[i];
5454 pa = a;
5455 *po=0.0;
5456 for (j=0; j<LPC_FILTERORDER+1; j++) {
5457 *po+=(*pa++)*(*pi--);
5458 }
5459 po++;
5460 }
5462 /* Update state vector */
5464 memcpy(mem, &In[len-LPC_FILTERORDER],
5465 LPC_FILTERORDER*sizeof(float));
5466 }
5468 A.11. createCB.h
5470 /******************************************************************
5472 iLBC Speech Coder ANSI-C Source Code
5474 createCB.h
5476 Copyright (C) The Internet Society (2004).
5477 All Rights Reserved.
5479 ******************************************************************/
5481 #ifndef __iLBC_CREATECB_H
5482 #define __iLBC_CREATECB_H
5484 void filteredCBvecs(
5485 float *cbvectors, /* (o) Codebook vector for the
5486 higher section */
5490 Andersen, et al. Experimental [Page 98]
5492 RFC 3951 Internet Low Bit Rate Codec December 2004
5495 float *mem, /* (i) Buffer to create codebook
5496 vectors from */
5497 int lMem /* (i) Length of buffer */
5498 );
5500 void searchAugmentedCB(
5501 int low, /* (i) Start index for the search */
5502 int high, /* (i) End index for the search */
5503 int stage, /* (i) Current stage */
5504 int startIndex, /* (i) CB index for the first
5505 augmented vector */
5506 float *target, /* (i) Target vector for encoding */
5507 float *buffer, /* (i) Pointer to the end of the
5508 buffer for augmented codebook
5509 construction */
5510 float *max_measure, /* (i/o) Currently maximum measure */
5511 int *best_index,/* (o) Currently the best index */
5512 float *gain, /* (o) Currently the best gain */
5513 float *energy, /* (o) Energy of augmented
5514 codebook vectors */
5515 float *invenergy/* (o) Inv energy of aug codebook
5516 vectors */
5517 );
5519 void createAugmentedVec(
5520 int index, /* (i) Index for the aug vector
5521 to be created */
5522 float *buffer, /* (i) Pointer to the end of the
5523 buffer for augmented codebook
5524 construction */
5525 float *cbVec /* (o) The construced codebook vector */
5526 );
5528 #endif
5530 A.12. createCB.c
5533 /******************************************************************
5535 iLBC Speech Coder ANSI-C Source Code
5537 createCB.c
5539 Copyright (C) The Internet Society (2004).
5540 All Rights Reserved.
5542 ******************************************************************/
5546 Andersen, et al. Experimental [Page 99]
5548 RFC 3951 Internet Low Bit Rate Codec December 2004
5551 #include "iLBC_define.h"
5552 #include "constants.h"
5553 #include <string.h>
5554 #include <math.h>
5556 /*----------------------------------------------------------------*
5557 * Construct an additional codebook vector by filtering the
5558 * initial codebook buffer. This vector is then used to expand
5559 * the codebook with an additional section.
5560 *---------------------------------------------------------------*/
5562 void filteredCBvecs(
5563 float *cbvectors, /* (o) Codebook vectors for the
5564 higher section */
5565 float *mem, /* (i) Buffer to create codebook
5566 vector from */
5567 int lMem /* (i) Length of buffer */
5568 ){
5569 int j, k;
5570 float *pp, *pp1;
5571 float tempbuff2[CB_MEML+CB_FILTERLEN];
5572 float *pos;
5574 memset(tempbuff2, 0, (CB_HALFFILTERLEN-1)*sizeof(float));
5575 memcpy(&tempbuff2[CB_HALFFILTERLEN-1], mem, lMem*sizeof(float));
5576 memset(&tempbuff2[lMem+CB_HALFFILTERLEN-1], 0,
5577 (CB_HALFFILTERLEN+1)*sizeof(float));
5579 /* Create codebook vector for higher section by filtering */
5581 /* do filtering */
5582 pos=cbvectors;
5583 memset(pos, 0, lMem*sizeof(float));
5584 for (k=0; k<lMem; k++) {
5585 pp=&tempbuff2[k];
5586 pp1=&cbfiltersTbl[CB_FILTERLEN-1];
5587 for (j=0;j<CB_FILTERLEN;j++) {
5588 (*pos)+=(*pp++)*(*pp1--);
5589 }
5590 pos++;
5591 }
5592 }
5594 /*----------------------------------------------------------------*
5595 * Search the augmented part of the codebook to find the best
5596 * measure.
5597 *----------------------------------------------------------------*/
5602 Andersen, et al. Experimental [Page 100]
5604 RFC 3951 Internet Low Bit Rate Codec December 2004
5607 void searchAugmentedCB(
5608 int low, /* (i) Start index for the search */
5609 int high, /* (i) End index for the search */
5610 int stage, /* (i) Current stage */
5611 int startIndex, /* (i) Codebook index for the first
5612 aug vector */
5613 float *target, /* (i) Target vector for encoding */
5614 float *buffer, /* (i) Pointer to the end of the buffer for
5615 augmented codebook construction */
5616 float *max_measure, /* (i/o) Currently maximum measure */
5617 int *best_index,/* (o) Currently the best index */
5618 float *gain, /* (o) Currently the best gain */
5619 float *energy, /* (o) Energy of augmented codebook
5620 vectors */
5621 float *invenergy/* (o) Inv energy of augmented codebook
5622 vectors */
5623 ) {
5624 int icount, ilow, j, tmpIndex;
5625 float *pp, *ppo, *ppi, *ppe, crossDot, alfa;
5626 float weighted, measure, nrjRecursive;
5627 float ftmp;
5629 /* Compute the energy for the first (low-5)
5630 noninterpolated samples */
5631 nrjRecursive = (float) 0.0;
5632 pp = buffer - low + 1;
5633 for (j=0; j<(low-5); j++) {
5634 nrjRecursive += ( (*pp)*(*pp) );
5635 pp++;
5636 }
5637 ppe = buffer - low;
5640 for (icount=low; icount<=high; icount++) {
5642 /* Index of the codebook vector used for retrieving
5643 energy values */
5644 tmpIndex = startIndex+icount-20;
5646 ilow = icount-4;
5648 /* Update the energy recursively to save complexity */
5649 nrjRecursive = nrjRecursive + (*ppe)*(*ppe);
5650 ppe--;
5651 energy[tmpIndex] = nrjRecursive;
5653 /* Compute cross dot product for the first (low-5)
5654 samples */
5658 Andersen, et al. Experimental [Page 101]
5660 RFC 3951 Internet Low Bit Rate Codec December 2004
5663 crossDot = (float) 0.0;
5664 pp = buffer-icount;
5665 for (j=0; j<ilow; j++) {
5666 crossDot += target[j]*(*pp++);
5667 }
5669 /* interpolation */
5670 alfa = (float) 0.2;
5671 ppo = buffer-4;
5672 ppi = buffer-icount-4;
5673 for (j=ilow; j<icount; j++) {
5674 weighted = ((float)1.0-alfa)*(*ppo)+alfa*(*ppi);
5675 ppo++;
5676 ppi++;
5677 energy[tmpIndex] += weighted*weighted;
5678 crossDot += target[j]*weighted;
5679 alfa += (float)0.2;
5680 }
5682 /* Compute energy and cross dot product for the
5683 remaining samples */
5684 pp = buffer - icount;
5685 for (j=icount; j<SUBL; j++) {
5686 energy[tmpIndex] += (*pp)*(*pp);
5687 crossDot += target[j]*(*pp++);
5688 }
5690 if (energy[tmpIndex]>0.0) {
5691 invenergy[tmpIndex]=(float)1.0/(energy[tmpIndex]+EPS);
5692 } else {
5693 invenergy[tmpIndex] = (float) 0.0;
5694 }
5696 if (stage==0) {
5697 measure = (float)-10000000.0;
5699 if (crossDot > 0.0) {
5700 measure = crossDot*crossDot*invenergy[tmpIndex];
5701 }
5702 }
5703 else {
5704 measure = crossDot*crossDot*invenergy[tmpIndex];
5705 }
5707 /* check if measure is better */
5708 ftmp = crossDot*invenergy[tmpIndex];
5710 if ((measure>*max_measure) && (fabs(ftmp)<CB_MAXGAIN)) {
5714 Andersen, et al. Experimental [Page 102]
5716 RFC 3951 Internet Low Bit Rate Codec December 2004
5719 *best_index = tmpIndex;
5720 *max_measure = measure;
5721 *gain = ftmp;
5722 }
5723 }
5724 }
5727 /*----------------------------------------------------------------*
5728 * Recreate a specific codebook vector from the augmented part.
5729 *
5730 *----------------------------------------------------------------*/
5732 void createAugmentedVec(
5733 int index, /* (i) Index for the augmented vector
5734 to be created */
5735 float *buffer, /* (i) Pointer to the end of the buffer for
5736 augmented codebook construction */
5737 float *cbVec/* (o) The construced codebook vector */
5738 ) {
5739 int ilow, j;
5740 float *pp, *ppo, *ppi, alfa, alfa1, weighted;
5742 ilow = index-5;
5744 /* copy the first noninterpolated part */
5746 pp = buffer-index;
5747 memcpy(cbVec,pp,sizeof(float)*index);
5749 /* interpolation */
5751 alfa1 = (float)0.2;
5752 alfa = 0.0;
5753 ppo = buffer-5;
5754 ppi = buffer-index-5;
5755 for (j=ilow; j<index; j++) {
5756 weighted = ((float)1.0-alfa)*(*ppo)+alfa*(*ppi);
5757 ppo++;
5758 ppi++;
5759 cbVec[j] = weighted;
5760 alfa += alfa1;
5761 }
5763 /* copy the second noninterpolated part */
5765 pp = buffer - index;
5766 memcpy(cbVec+index,pp,sizeof(float)*(SUBL-index));
5770 Andersen, et al. Experimental [Page 103]
5772 RFC 3951 Internet Low Bit Rate Codec December 2004
5775 }
5777 A.13. doCPLC.h
5779 /******************************************************************
5781 iLBC Speech Coder ANSI-C Source Code
5783 doCPLC.h
5785 Copyright (C) The Internet Society (2004).
5786 All Rights Reserved.
5788 ******************************************************************/
5790 #ifndef __iLBC_DOLPC_H
5791 #define __iLBC_DOLPC_H
5793 void doThePLC(
5794 float *PLCresidual, /* (o) concealed residual */
5795 float *PLClpc, /* (o) concealed LP parameters */
5796 int PLI, /* (i) packet loss indicator
5797 0 - no PL, 1 = PL */
5798 float *decresidual, /* (i) decoded residual */
5799 float *lpc, /* (i) decoded LPC (only used for no PL) */
5800 int inlag, /* (i) pitch lag */
5801 iLBC_Dec_Inst_t *iLBCdec_inst
5802 /* (i/o) decoder instance */
5803 );
5805 #endif
5807 A.14. doCPLC.c
5809 /******************************************************************
5811 iLBC Speech Coder ANSI-C Source Code
5813 doCPLC.c
5815 Copyright (C) The Internet Society (2004).
5816 All Rights Reserved.
5818 ******************************************************************/
5820 #include <math.h>
5821 #include <string.h>
5822 #include <stdio.h>
5826 Andersen, et al. Experimental [Page 104]
5828 RFC 3951 Internet Low Bit Rate Codec December 2004
5831 #include "iLBC_define.h"
5833 /*----------------------------------------------------------------*
5834 * Compute cross correlation and pitch gain for pitch prediction
5835 * of last subframe at given lag.
5836 *---------------------------------------------------------------*/
5838 void compCorr(
5839 float *cc, /* (o) cross correlation coefficient */
5840 float *gc, /* (o) gain */
5841 float *pm,
5842 float *buffer, /* (i) signal buffer */
5843 int lag, /* (i) pitch lag */
5844 int bLen, /* (i) length of buffer */
5845 int sRange /* (i) correlation search length */
5846 ){
5847 int i;
5848 float ftmp1, ftmp2, ftmp3;
5850 /* Guard against getting outside buffer */
5851 if ((bLen-sRange-lag)<0) {
5852 sRange=bLen-lag;
5853 }
5855 ftmp1 = 0.0;
5856 ftmp2 = 0.0;
5857 ftmp3 = 0.0;
5858 for (i=0; i<sRange; i++) {
5859 ftmp1 += buffer[bLen-sRange+i] *
5860 buffer[bLen-sRange+i-lag];
5861 ftmp2 += buffer[bLen-sRange+i-lag] *
5862 buffer[bLen-sRange+i-lag];
5863 ftmp3 += buffer[bLen-sRange+i] *
5864 buffer[bLen-sRange+i];
5865 }
5867 if (ftmp2 > 0.0) {
5868 *cc = ftmp1*ftmp1/ftmp2;
5869 *gc = (float)fabs(ftmp1/ftmp2);
5870 *pm=(float)fabs(ftmp1)/
5871 ((float)sqrt(ftmp2)*(float)sqrt(ftmp3));
5872 }
5873 else {
5874 *cc = 0.0;
5875 *gc = 0.0;
5876 *pm=0.0;
5877 }
5878 }
5882 Andersen, et al. Experimental [Page 105]
5884 RFC 3951 Internet Low Bit Rate Codec December 2004
5887 /*----------------------------------------------------------------*
5888 * Packet loss concealment routine. Conceals a residual signal
5889 * and LP parameters. If no packet loss, update state.
5890 *---------------------------------------------------------------*/
5892 void doThePLC(
5893 float *PLCresidual, /* (o) concealed residual */
5894 float *PLClpc, /* (o) concealed LP parameters */
5895 int PLI, /* (i) packet loss indicator
5896 0 - no PL, 1 = PL */
5897 float *decresidual, /* (i) decoded residual */
5898 float *lpc, /* (i) decoded LPC (only used for no PL) */
5899 int inlag, /* (i) pitch lag */
5900 iLBC_Dec_Inst_t *iLBCdec_inst
5901 /* (i/o) decoder instance */
5902 ){
5903 int lag=20, randlag;
5904 float gain, maxcc;
5905 float use_gain;
5906 float gain_comp, maxcc_comp, per, max_per;
5907 int i, pick, use_lag;
5908 float ftmp, randvec[BLOCKL_MAX], pitchfact, energy;
5910 /* Packet Loss */
5912 if (PLI == 1) {
5914 iLBCdec_inst->consPLICount += 1;
5916 /* if previous frame not lost,
5917 determine pitch pred. gain */
5919 if (iLBCdec_inst->prevPLI != 1) {
5921 /* Search around the previous lag to find the
5922 best pitch period */
5924 lag=inlag-3;
5925 compCorr(&maxcc, &gain, &max_per,
5926 iLBCdec_inst->prevResidual,
5927 lag, iLBCdec_inst->blockl, 60);
5928 for (i=inlag-2;i<=inlag+3;i++) {
5929 compCorr(&maxcc_comp, &gain_comp, &per,
5930 iLBCdec_inst->prevResidual,
5931 i, iLBCdec_inst->blockl, 60);
5933 if (maxcc_comp>maxcc) {
5934 maxcc=maxcc_comp;
5938 Andersen, et al. Experimental [Page 106]
5940 RFC 3951 Internet Low Bit Rate Codec December 2004
5943 gain=gain_comp;
5944 lag=i;
5945 max_per=per;
5946 }
5947 }
5949 }
5951 /* previous frame lost, use recorded lag and periodicity */
5953 else {
5954 lag=iLBCdec_inst->prevLag;
5955 max_per=iLBCdec_inst->per;
5956 }
5958 /* downscaling */
5960 use_gain=1.0;
5961 if (iLBCdec_inst->consPLICount*iLBCdec_inst->blockl>320)
5962 use_gain=(float)0.9;
5963 else if (iLBCdec_inst->consPLICount*
5964 iLBCdec_inst->blockl>2*320)
5965 use_gain=(float)0.7;
5966 else if (iLBCdec_inst->consPLICount*
5967 iLBCdec_inst->blockl>3*320)
5968 use_gain=(float)0.5;
5969 else if (iLBCdec_inst->consPLICount*
5970 iLBCdec_inst->blockl>4*320)
5971 use_gain=(float)0.0;
5973 /* mix noise and pitch repeatition */
5974 ftmp=(float)sqrt(max_per);
5975 if (ftmp>(float)0.7)
5976 pitchfact=(float)1.0;
5977 else if (ftmp>(float)0.4)
5978 pitchfact=(ftmp-(float)0.4)/((float)0.7-(float)0.4);
5979 else
5980 pitchfact=0.0;
5983 /* avoid repetition of same pitch cycle */
5984 use_lag=lag;
5985 if (lag<80) {
5986 use_lag=2*lag;
5987 }
5989 /* compute concealed residual */
5994 Andersen, et al. Experimental [Page 107]
5996 RFC 3951 Internet Low Bit Rate Codec December 2004
5999 energy = 0.0;
6000 for (i=0; i<iLBCdec_inst->blockl; i++) {
6002 /* noise component */
6004 iLBCdec_inst->seed=(iLBCdec_inst->seed*69069L+1) &
6005 (0x80000000L-1);
6006 randlag = 50 + ((signed long) iLBCdec_inst->seed)%70;
6007 pick = i - randlag;
6009 if (pick < 0) {
6010 randvec[i] =
6011 iLBCdec_inst->prevResidual[
6012 iLBCdec_inst->blockl+pick];
6013 } else {
6014 randvec[i] = randvec[pick];
6015 }
6017 /* pitch repeatition component */
6018 pick = i - use_lag;
6020 if (pick < 0) {
6021 PLCresidual[i] =
6022 iLBCdec_inst->prevResidual[
6023 iLBCdec_inst->blockl+pick];
6024 } else {
6025 PLCresidual[i] = PLCresidual[pick];
6026 }
6028 /* mix random and periodicity component */
6030 if (i<80)
6031 PLCresidual[i] = use_gain*(pitchfact *
6032 PLCresidual[i] +
6033 ((float)1.0 - pitchfact) * randvec[i]);
6034 else if (i<160)
6035 PLCresidual[i] = (float)0.95*use_gain*(pitchfact *
6036 PLCresidual[i] +
6037 ((float)1.0 - pitchfact) * randvec[i]);
6038 else
6039 PLCresidual[i] = (float)0.9*use_gain*(pitchfact *
6040 PLCresidual[i] +
6041 ((float)1.0 - pitchfact) * randvec[i]);
6043 energy += PLCresidual[i] * PLCresidual[i];
6044 }
6046 /* less than 30 dB, use only noise */
6050 Andersen, et al. Experimental [Page 108]
6052 RFC 3951 Internet Low Bit Rate Codec December 2004
6056 if (sqrt(energy/(float)iLBCdec_inst->blockl) < 30.0) {
6057 gain=0.0;
6058 for (i=0; i<iLBCdec_inst->blockl; i++) {
6059 PLCresidual[i] = randvec[i];
6060 }
6061 }
6063 /* use old LPC */
6065 memcpy(PLClpc,iLBCdec_inst->prevLpc,
6066 (LPC_FILTERORDER+1)*sizeof(float));
6068 }
6070 /* no packet loss, copy input */
6072 else {
6073 memcpy(PLCresidual, decresidual,
6074 iLBCdec_inst->blockl*sizeof(float));
6075 memcpy(PLClpc, lpc, (LPC_FILTERORDER+1)*sizeof(float));
6076 iLBCdec_inst->consPLICount = 0;
6077 }
6079 /* update state */
6081 if (PLI) {
6082 iLBCdec_inst->prevLag = lag;
6083 iLBCdec_inst->per=max_per;
6084 }
6086 iLBCdec_inst->prevPLI = PLI;
6087 memcpy(iLBCdec_inst->prevLpc, PLClpc,
6088 (LPC_FILTERORDER+1)*sizeof(float));
6089 memcpy(iLBCdec_inst->prevResidual, PLCresidual,
6090 iLBCdec_inst->blockl*sizeof(float));
6091 }
6093 A.15. enhancer.h
6095 /******************************************************************
6097 iLBC Speech Coder ANSI-C Source Code
6099 enhancer.h
6101 Copyright (C) The Internet Society (2004).
6102 All Rights Reserved.
6106 Andersen, et al. Experimental [Page 109]
6108 RFC 3951 Internet Low Bit Rate Codec December 2004
6111 ******************************************************************/
6113 #ifndef __ENHANCER_H
6114 #define __ENHANCER_H
6116 #include "iLBC_define.h"
6118 float xCorrCoef(
6119 float *target, /* (i) first array */
6120 float *regressor, /* (i) second array */
6121 int subl /* (i) dimension arrays */
6122 );
6124 int enhancerInterface(
6125 float *out, /* (o) the enhanced recidual signal */
6126 float *in, /* (i) the recidual signal to enhance */
6127 iLBC_Dec_Inst_t *iLBCdec_inst
6128 /* (i/o) the decoder state structure */
6129 );
6131 #endif
6133 A.16. enhancer.c
6135 /******************************************************************
6137 iLBC Speech Coder ANSI-C Source Code
6139 enhancer.c
6141 Copyright (C) The Internet Society (2004).
6142 All Rights Reserved.
6144 ******************************************************************/
6146 #include <math.h>
6147 #include <string.h>
6148 #include "iLBC_define.h"
6149 #include "constants.h"
6150 #include "filter.h"
6152 /*----------------------------------------------------------------*
6153 * Find index in array such that the array element with said
6154 * index is the element of said array closest to "value"
6155 * according to the squared-error criterion
6156 *---------------------------------------------------------------*/
6158 void NearestNeighbor(
6162 Andersen, et al. Experimental [Page 110]
6164 RFC 3951 Internet Low Bit Rate Codec December 2004
6167 int *index, /* (o) index of array element closest
6168 to value */
6169 float *array, /* (i) data array */
6170 float value,/* (i) value */
6171 int arlength/* (i) dimension of data array */
6172 ){
6173 int i;
6174 float bestcrit,crit;
6176 crit=array[0]-value;
6177 bestcrit=crit*crit;
6178 *index=0;
6179 for (i=1; i<arlength; i++) {
6180 crit=array[i]-value;
6181 crit=crit*crit;
6183 if (crit<bestcrit) {
6184 bestcrit=crit;
6185 *index=i;
6186 }
6187 }
6188 }
6190 /*----------------------------------------------------------------*
6191 * compute cross correlation between sequences
6192 *---------------------------------------------------------------*/
6194 void mycorr1(
6195 float* corr, /* (o) correlation of seq1 and seq2 */
6196 float* seq1, /* (i) first sequence */
6197 int dim1, /* (i) dimension first seq1 */
6198 const float *seq2, /* (i) second sequence */
6199 int dim2 /* (i) dimension seq2 */
6200 ){
6201 int i,j;
6203 for (i=0; i<=dim1-dim2; i++) {
6204 corr[i]=0.0;
6205 for (j=0; j<dim2; j++) {
6206 corr[i] += seq1[i+j] * seq2[j];
6207 }
6208 }
6209 }
6211 /*----------------------------------------------------------------*
6212 * upsample finite array assuming zeros outside bounds
6213 *---------------------------------------------------------------*/
6218 Andersen, et al. Experimental [Page 111]
6220 RFC 3951 Internet Low Bit Rate Codec December 2004
6223 void enh_upsample(
6224 float* useq1, /* (o) upsampled output sequence */
6225 float* seq1,/* (i) unupsampled sequence */
6226 int dim1, /* (i) dimension seq1 */
6227 int hfl /* (i) polyphase filter length=2*hfl+1 */
6228 ){
6229 float *pu,*ps;
6230 int i,j,k,q,filterlength,hfl2;
6231 const float *polyp[ENH_UPS0]; /* pointers to
6232 polyphase columns */
6233 const float *pp;
6235 /* define pointers for filter */
6237 filterlength=2*hfl+1;
6239 if ( filterlength > dim1 ) {
6240 hfl2=(int) (dim1/2);
6241 for (j=0; j<ENH_UPS0; j++) {
6242 polyp[j]=polyphaserTbl+j*filterlength+hfl-hfl2;
6243 }
6244 hfl=hfl2;
6245 filterlength=2*hfl+1;
6246 }
6247 else {
6248 for (j=0; j<ENH_UPS0; j++) {
6249 polyp[j]=polyphaserTbl+j*filterlength;
6250 }
6251 }
6253 /* filtering: filter overhangs left side of sequence */
6255 pu=useq1;
6256 for (i=hfl; i<filterlength; i++) {
6257 for (j=0; j<ENH_UPS0; j++) {
6258 *pu=0.0;
6259 pp = polyp[j];
6260 ps = seq1+i;
6261 for (k=0; k<=i; k++) {
6262 *pu += *ps-- * *pp++;
6263 }
6264 pu++;
6265 }
6266 }
6268 /* filtering: simple convolution=inner products */
6270 for (i=filterlength; i<dim1; i++) {
6274 Andersen, et al. Experimental [Page 112]
6276 RFC 3951 Internet Low Bit Rate Codec December 2004
6279 for (j=0;j<ENH_UPS0; j++){
6280 *pu=0.0;
6281 pp = polyp[j];
6282 ps = seq1+i;
6283 for (k=0; k<filterlength; k++) {
6284 *pu += *ps-- * *pp++;
6285 }
6286 pu++;
6287 }
6288 }
6290 /* filtering: filter overhangs right side of sequence */
6292 for (q=1; q<=hfl; q++) {
6293 for (j=0; j<ENH_UPS0; j++) {
6294 *pu=0.0;
6295 pp = polyp[j]+q;
6296 ps = seq1+dim1-1;
6297 for (k=0; k<filterlength-q; k++) {
6298 *pu += *ps-- * *pp++;
6299 }
6300 pu++;
6301 }
6302 }
6303 }
6306 /*----------------------------------------------------------------*
6307 * find segment starting near idata+estSegPos that has highest
6308 * correlation with idata+centerStartPos through
6309 * idata+centerStartPos+ENH_BLOCKL-1 segment is found at a
6310 * resolution of ENH_UPSO times the original of the original
6311 * sampling rate
6312 *---------------------------------------------------------------*/
6314 void refiner(
6315 float *seg, /* (o) segment array */
6316 float *updStartPos, /* (o) updated start point */
6317 float* idata, /* (i) original data buffer */
6318 int idatal, /* (i) dimension of idata */
6319 int centerStartPos, /* (i) beginning center segment */
6320 float estSegPos,/* (i) estimated beginning other segment */
6321 float period /* (i) estimated pitch period */
6322 ){
6323 int estSegPosRounded,searchSegStartPos,searchSegEndPos,corrdim;
6324 int tloc,tloc2,i,st,en,fraction;
6325 float vect[ENH_VECTL],corrVec[ENH_CORRDIM],maxv;
6326 float corrVecUps[ENH_CORRDIM*ENH_UPS0];
6330 Andersen, et al. Experimental [Page 113]
6332 RFC 3951 Internet Low Bit Rate Codec December 2004
6335 /* defining array bounds */
6337 estSegPosRounded=(int)(estSegPos - 0.5);
6339 searchSegStartPos=estSegPosRounded-ENH_SLOP;
6341 if (searchSegStartPos<0) {
6342 searchSegStartPos=0;
6343 }
6344 searchSegEndPos=estSegPosRounded+ENH_SLOP;
6346 if (searchSegEndPos+ENH_BLOCKL >= idatal) {
6347 searchSegEndPos=idatal-ENH_BLOCKL-1;
6348 }
6349 corrdim=searchSegEndPos-searchSegStartPos+1;
6351 /* compute upsampled correlation (corr33) and find
6352 location of max */
6354 mycorr1(corrVec,idata+searchSegStartPos,
6355 corrdim+ENH_BLOCKL-1,idata+centerStartPos,ENH_BLOCKL);
6356 enh_upsample(corrVecUps,corrVec,corrdim,ENH_FL0);
6357 tloc=0; maxv=corrVecUps[0];
6358 for (i=1; i<ENH_UPS0*corrdim; i++) {
6360 if (corrVecUps[i]>maxv) {
6361 tloc=i;
6362 maxv=corrVecUps[i];
6363 }
6364 }
6366 /* make vector can be upsampled without ever running outside
6367 bounds */
6369 *updStartPos= (float)searchSegStartPos +
6370 (float)tloc/(float)ENH_UPS0+(float)1.0;
6371 tloc2=(int)(tloc/ENH_UPS0);
6373 if (tloc>tloc2*ENH_UPS0) {
6374 tloc2++;
6375 }
6376 st=searchSegStartPos+tloc2-ENH_FL0;
6378 if (st<0) {
6379 memset(vect,0,-st*sizeof(float));
6380 memcpy(&vect[-st],idata, (ENH_VECTL+st)*sizeof(float));
6381 }
6382 else {
6386 Andersen, et al. Experimental [Page 114]
6388 RFC 3951 Internet Low Bit Rate Codec December 2004
6391 en=st+ENH_VECTL;
6393 if (en>idatal) {
6394 memcpy(vect, &idata[st],
6395 (ENH_VECTL-(en-idatal))*sizeof(float));
6396 memset(&vect[ENH_VECTL-(en-idatal)], 0,
6397 (en-idatal)*sizeof(float));
6398 }
6399 else {
6400 memcpy(vect, &idata[st], ENH_VECTL*sizeof(float));
6401 }
6402 }
6403 fraction=tloc2*ENH_UPS0-tloc;
6405 /* compute the segment (this is actually a convolution) */
6407 mycorr1(seg,vect,ENH_VECTL,polyphaserTbl+(2*ENH_FL0+1)*fraction,
6408 2*ENH_FL0+1);
6409 }
6411 /*----------------------------------------------------------------*
6412 * find the smoothed output data
6413 *---------------------------------------------------------------*/
6415 void smath(
6416 float *odata, /* (o) smoothed output */
6417 float *sseq,/* (i) said second sequence of waveforms */
6418 int hl, /* (i) 2*hl+1 is sseq dimension */
6419 float alpha0/* (i) max smoothing energy fraction */
6420 ){
6421 int i,k;
6422 float w00,w10,w11,A,B,C,*psseq,err,errs;
6423 float surround[BLOCKL_MAX]; /* shape contributed by other than
6424 current */
6425 float wt[2*ENH_HL+1]; /* waveform weighting to get
6426 surround shape */
6427 float denom;
6429 /* create shape of contribution from all waveforms except the
6430 current one */
6432 for (i=1; i<=2*hl+1; i++) {
6433 wt[i-1] = (float)0.5*(1 - (float)cos(2*PI*i/(2*hl+2)));
6434 }
6435 wt[hl]=0.0; /* for clarity, not used */
6436 for (i=0; i<ENH_BLOCKL; i++) {
6437 surround[i]=sseq[i]*wt[0];
6438 }
6442 Andersen, et al. Experimental [Page 115]
6444 RFC 3951 Internet Low Bit Rate Codec December 2004
6447 for (k=1; k<hl; k++) {
6448 psseq=sseq+k*ENH_BLOCKL;
6449 for(i=0;i<ENH_BLOCKL; i++) {
6450 surround[i]+=psseq[i]*wt[k];
6451 }
6452 }
6453 for (k=hl+1; k<=2*hl; k++) {
6454 psseq=sseq+k*ENH_BLOCKL;
6455 for(i=0;i<ENH_BLOCKL; i++) {
6456 surround[i]+=psseq[i]*wt[k];
6457 }
6458 }
6460 /* compute some inner products */
6462 w00 = w10 = w11 = 0.0;
6463 psseq=sseq+hl*ENH_BLOCKL; /* current block */
6464 for (i=0; i<ENH_BLOCKL;i++) {
6465 w00+=psseq[i]*psseq[i];
6466 w11+=surround[i]*surround[i];
6467 w10+=surround[i]*psseq[i];
6468 }
6470 if (fabs(w11) < 1.0) {
6471 w11=1.0;
6472 }
6473 C = (float)sqrt( w00/w11);
6475 /* first try enhancement without power-constraint */
6477 errs=0.0;
6478 psseq=sseq+hl*ENH_BLOCKL;
6479 for (i=0; i<ENH_BLOCKL; i++) {
6480 odata[i]=C*surround[i];
6481 err=psseq[i]-odata[i];
6482 errs+=err*err;
6483 }
6485 /* if constraint violated by first try, add constraint */
6487 if (errs > alpha0 * w00) {
6488 if ( w00 < 1) {
6489 w00=1;
6490 }
6491 denom = (w11*w00-w10*w10)/(w00*w00);
6493 if (denom > 0.0001) { /* eliminates numerical problems
6494 for if smooth */
6498 Andersen, et al. Experimental [Page 116]
6500 RFC 3951 Internet Low Bit Rate Codec December 2004
6503 A = (float)sqrt( (alpha0- alpha0*alpha0/4)/denom);
6504 B = -alpha0/2 - A * w10/w00;
6505 B = B+1;
6506 }
6507 else { /* essentially no difference between cycles;
6508 smoothing not needed */
6509 A= 0.0;
6510 B= 1.0;
6511 }
6513 /* create smoothed sequence */
6515 psseq=sseq+hl*ENH_BLOCKL;
6516 for (i=0; i<ENH_BLOCKL; i++) {
6517 odata[i]=A*surround[i]+B*psseq[i];
6518 }
6519 }
6520 }
6522 /*----------------------------------------------------------------*
6523 * get the pitch-synchronous sample sequence
6524 *---------------------------------------------------------------*/
6526 void getsseq(
6527 float *sseq, /* (o) the pitch-synchronous sequence */
6528 float *idata, /* (i) original data */
6529 int idatal, /* (i) dimension of data */
6530 int centerStartPos, /* (i) where current block starts */
6531 float *period, /* (i) rough-pitch-period array */
6532 float *plocs, /* (i) where periods of period array
6533 are taken */
6534 int periodl, /* (i) dimension period array */
6535 int hl /* (i) 2*hl+1 is the number of sequences */
6536 ){
6537 int i,centerEndPos,q;
6538 float blockStartPos[2*ENH_HL+1];
6539 int lagBlock[2*ENH_HL+1];
6540 float plocs2[ENH_PLOCSL];
6541 float *psseq;
6543 centerEndPos=centerStartPos+ENH_BLOCKL-1;
6545 /* present */
6547 NearestNeighbor(lagBlock+hl,plocs,
6548 (float)0.5*(centerStartPos+centerEndPos),periodl);
6550 blockStartPos[hl]=(float)centerStartPos;
6554 Andersen, et al. Experimental [Page 117]
6556 RFC 3951 Internet Low Bit Rate Codec December 2004
6559 psseq=sseq+ENH_BLOCKL*hl;
6560 memcpy(psseq, idata+centerStartPos, ENH_BLOCKL*sizeof(float));
6562 /* past */
6564 for (q=hl-1; q>=0; q--) {
6565 blockStartPos[q]=blockStartPos[q+1]-period[lagBlock[q+1]];
6566 NearestNeighbor(lagBlock+q,plocs,
6567 blockStartPos[q]+
6568 ENH_BLOCKL_HALF-period[lagBlock[q+1]], periodl);
6571 if (blockStartPos[q]-ENH_OVERHANG>=0) {
6572 refiner(sseq+q*ENH_BLOCKL, blockStartPos+q, idata,
6573 idatal, centerStartPos, blockStartPos[q],
6574 period[lagBlock[q+1]]);
6575 } else {
6576 psseq=sseq+q*ENH_BLOCKL;
6577 memset(psseq, 0, ENH_BLOCKL*sizeof(float));
6578 }
6579 }
6581 /* future */
6583 for (i=0; i<periodl; i++) {
6584 plocs2[i]=plocs[i]-period[i];
6585 }
6586 for (q=hl+1; q<=2*hl; q++) {
6587 NearestNeighbor(lagBlock+q,plocs2,
6588 blockStartPos[q-1]+ENH_BLOCKL_HALF,periodl);
6590 blockStartPos[q]=blockStartPos[q-1]+period[lagBlock[q]];
6591 if (blockStartPos[q]+ENH_BLOCKL+ENH_OVERHANG<idatal) {
6592 refiner(sseq+ENH_BLOCKL*q, blockStartPos+q, idata,
6593 idatal, centerStartPos, blockStartPos[q],
6594 period[lagBlock[q]]);
6595 }
6596 else {
6597 psseq=sseq+q*ENH_BLOCKL;
6598 memset(psseq, 0, ENH_BLOCKL*sizeof(float));
6599 }
6600 }
6601 }
6603 /*----------------------------------------------------------------*
6604 * perform enhancement on idata+centerStartPos through
6605 * idata+centerStartPos+ENH_BLOCKL-1
6606 *---------------------------------------------------------------*/
6610 Andersen, et al. Experimental [Page 118]
6612 RFC 3951 Internet Low Bit Rate Codec December 2004
6615 void enhancer(
6616 float *odata, /* (o) smoothed block, dimension blockl */
6617 float *idata, /* (i) data buffer used for enhancing */
6618 int idatal, /* (i) dimension idata */
6619 int centerStartPos, /* (i) first sample current block
6620 within idata */
6621 float alpha0, /* (i) max correction-energy-fraction
6622 (in [0,1]) */
6623 float *period, /* (i) pitch period array */
6624 float *plocs, /* (i) locations where period array
6625 values valid */
6626 int periodl /* (i) dimension of period and plocs */
6627 ){
6628 float sseq[(2*ENH_HL+1)*ENH_BLOCKL];
6630 /* get said second sequence of segments */
6632 getsseq(sseq,idata,idatal,centerStartPos,period,
6633 plocs,periodl,ENH_HL);
6635 /* compute the smoothed output from said second sequence */
6637 smath(odata,sseq,ENH_HL,alpha0);
6639 }
6641 /*----------------------------------------------------------------*
6642 * cross correlation
6643 *---------------------------------------------------------------*/
6645 float xCorrCoef(
6646 float *target, /* (i) first array */
6647 float *regressor, /* (i) second array */
6648 int subl /* (i) dimension arrays */
6649 ){
6650 int i;
6651 float ftmp1, ftmp2;
6653 ftmp1 = 0.0;
6654 ftmp2 = 0.0;
6655 for (i=0; i<subl; i++) {
6656 ftmp1 += target[i]*regressor[i];
6657 ftmp2 += regressor[i]*regressor[i];
6658 }
6660 if (ftmp1 > 0.0) {
6661 return (float)(ftmp1*ftmp1/ftmp2);
6662 }
6666 Andersen, et al. Experimental [Page 119]
6668 RFC 3951 Internet Low Bit Rate Codec December 2004
6671 else {
6672 return (float)0.0;
6673 }
6674 }
6676 /*----------------------------------------------------------------*
6677 * interface for enhancer
6678 *---------------------------------------------------------------*/
6680 int enhancerInterface(
6681 float *out, /* (o) enhanced signal */
6682 float *in, /* (i) unenhanced signal */
6683 iLBC_Dec_Inst_t *iLBCdec_inst /* (i) buffers etc */
6684 ){
6685 float *enh_buf, *enh_period;
6686 int iblock, isample;
6687 int lag=0, ilag, i, ioffset;
6688 float cc, maxcc;
6689 float ftmp1, ftmp2;
6690 float *inPtr, *enh_bufPtr1, *enh_bufPtr2;
6691 float plc_pred[ENH_BLOCKL];
6693 float lpState[6], downsampled[(ENH_NBLOCKS*ENH_BLOCKL+120)/2];
6694 int inLen=ENH_NBLOCKS*ENH_BLOCKL+120;
6695 int start, plc_blockl, inlag;
6697 enh_buf=iLBCdec_inst->enh_buf;
6698 enh_period=iLBCdec_inst->enh_period;
6700 memmove(enh_buf, &enh_buf[iLBCdec_inst->blockl],
6701 (ENH_BUFL-iLBCdec_inst->blockl)*sizeof(float));
6703 memcpy(&enh_buf[ENH_BUFL-iLBCdec_inst->blockl], in,
6704 iLBCdec_inst->blockl*sizeof(float));
6706 if (iLBCdec_inst->mode==30)
6707 plc_blockl=ENH_BLOCKL;
6708 else
6709 plc_blockl=40;
6711 /* when 20 ms frame, move processing one block */
6712 ioffset=0;
6713 if (iLBCdec_inst->mode==20) ioffset=1;
6715 i=3-ioffset;
6716 memmove(enh_period, &enh_period[i],
6717 (ENH_NBLOCKS_TOT-i)*sizeof(float));
6722 Andersen, et al. Experimental [Page 120]
6724 RFC 3951 Internet Low Bit Rate Codec December 2004
6727 /* Set state information to the 6 samples right before
6728 the samples to be downsampled. */
6730 memcpy(lpState,
6731 enh_buf+(ENH_NBLOCKS_EXTRA+ioffset)*ENH_BLOCKL-126,
6732 6*sizeof(float));
6734 /* Down sample a factor 2 to save computations */
6736 DownSample(enh_buf+(ENH_NBLOCKS_EXTRA+ioffset)*ENH_BLOCKL-120,
6737 lpFilt_coefsTbl, inLen-ioffset*ENH_BLOCKL,
6738 lpState, downsampled);
6740 /* Estimate the pitch in the down sampled domain. */
6741 for (iblock = 0; iblock<ENH_NBLOCKS-ioffset; iblock++) {
6743 lag = 10;
6744 maxcc = xCorrCoef(downsampled+60+iblock*
6745 ENH_BLOCKL_HALF, downsampled+60+iblock*
6746 ENH_BLOCKL_HALF-lag, ENH_BLOCKL_HALF);
6747 for (ilag=11; ilag<60; ilag++) {
6748 cc = xCorrCoef(downsampled+60+iblock*
6749 ENH_BLOCKL_HALF, downsampled+60+iblock*
6750 ENH_BLOCKL_HALF-ilag, ENH_BLOCKL_HALF);
6752 if (cc > maxcc) {
6753 maxcc = cc;
6754 lag = ilag;
6755 }
6756 }
6758 /* Store the estimated lag in the non-downsampled domain */
6759 enh_period[iblock+ENH_NBLOCKS_EXTRA+ioffset] = (float)lag*2;
6762 }
6765 /* PLC was performed on the previous packet */
6766 if (iLBCdec_inst->prev_enh_pl==1) {
6768 inlag=(int)enh_period[ENH_NBLOCKS_EXTRA+ioffset];
6770 lag = inlag-1;
6771 maxcc = xCorrCoef(in, in+lag, plc_blockl);
6772 for (ilag=inlag; ilag<=inlag+1; ilag++) {
6773 cc = xCorrCoef(in, in+ilag, plc_blockl);
6778 Andersen, et al. Experimental [Page 121]
6780 RFC 3951 Internet Low Bit Rate Codec December 2004
6783 if (cc > maxcc) {
6784 maxcc = cc;
6785 lag = ilag;
6786 }
6787 }
6789 enh_period[ENH_NBLOCKS_EXTRA+ioffset-1]=(float)lag;
6791 /* compute new concealed residual for the old lookahead,
6792 mix the forward PLC with a backward PLC from
6793 the new frame */
6795 inPtr=&in[lag-1];
6797 enh_bufPtr1=&plc_pred[plc_blockl-1];
6799 if (lag>plc_blockl) {
6800 start=plc_blockl;
6801 } else {
6802 start=lag;
6803 }
6805 for (isample = start; isample>0; isample--) {
6806 *enh_bufPtr1-- = *inPtr--;
6807 }
6809 enh_bufPtr2=&enh_buf[ENH_BUFL-1-iLBCdec_inst->blockl];
6810 for (isample = (plc_blockl-1-lag); isample>=0; isample--) {
6811 *enh_bufPtr1-- = *enh_bufPtr2--;
6812 }
6814 /* limit energy change */
6815 ftmp2=0.0;
6816 ftmp1=0.0;
6817 for (i=0;i<plc_blockl;i++) {
6818 ftmp2+=enh_buf[ENH_BUFL-1-iLBCdec_inst->blockl-i]*
6819 enh_buf[ENH_BUFL-1-iLBCdec_inst->blockl-i];
6820 ftmp1+=plc_pred[i]*plc_pred[i];
6821 }
6822 ftmp1=(float)sqrt(ftmp1/(float)plc_blockl);
6823 ftmp2=(float)sqrt(ftmp2/(float)plc_blockl);
6824 if (ftmp1>(float)2.0*ftmp2 && ftmp1>0.0) {
6825 for (i=0;i<plc_blockl-10;i++) {
6826 plc_pred[i]*=(float)2.0*ftmp2/ftmp1;
6827 }
6828 for (i=plc_blockl-10;i<plc_blockl;i++) {
6829 plc_pred[i]*=(float)(i-plc_blockl+10)*
6830 ((float)1.0-(float)2.0*ftmp2/ftmp1)/(float)(10)+
6834 Andersen, et al. Experimental [Page 122]
6836 RFC 3951 Internet Low Bit Rate Codec December 2004
6839 (float)2.0*ftmp2/ftmp1;
6840 }
6841 }
6843 enh_bufPtr1=&enh_buf[ENH_BUFL-1-iLBCdec_inst->blockl];
6844 for (i=0; i<plc_blockl; i++) {
6845 ftmp1 = (float) (i+1) / (float) (plc_blockl+1);
6846 *enh_bufPtr1 *= ftmp1;
6847 *enh_bufPtr1 += ((float)1.0-ftmp1)*
6848 plc_pred[plc_blockl-1-i];
6849 enh_bufPtr1--;
6850 }
6851 }
6853 if (iLBCdec_inst->mode==20) {
6854 /* Enhancer with 40 samples delay */
6855 for (iblock = 0; iblock<2; iblock++) {
6856 enhancer(out+iblock*ENH_BLOCKL, enh_buf,
6857 ENH_BUFL, (5+iblock)*ENH_BLOCKL+40,
6858 ENH_ALPHA0, enh_period, enh_plocsTbl,
6859 ENH_NBLOCKS_TOT);
6860 }
6861 } else if (iLBCdec_inst->mode==30) {
6862 /* Enhancer with 80 samples delay */
6863 for (iblock = 0; iblock<3; iblock++) {
6864 enhancer(out+iblock*ENH_BLOCKL, enh_buf,
6865 ENH_BUFL, (4+iblock)*ENH_BLOCKL,
6866 ENH_ALPHA0, enh_period, enh_plocsTbl,
6867 ENH_NBLOCKS_TOT);
6868 }
6869 }
6871 return (lag*2);
6872 }
6874 A.17. filter.h
6876 /******************************************************************
6878 iLBC Speech Coder ANSI-C Source Code
6880 filter.h
6882 Copyright (C) The Internet Society (2004).
6883 All Rights Reserved.
6885 ******************************************************************/
6890 Andersen, et al. Experimental [Page 123]
6892 RFC 3951 Internet Low Bit Rate Codec December 2004
6895 #ifndef __iLBC_FILTER_H
6896 #define __iLBC_FILTER_H
6898 void AllPoleFilter(
6899 float *InOut, /* (i/o) on entrance InOut[-orderCoef] to
6900 InOut[-1] contain the state of the
6901 filter (delayed samples). InOut[0] to
6902 InOut[lengthInOut-1] contain the filter
6903 input, on en exit InOut[-orderCoef] to
6904 InOut[-1] is unchanged and InOut[0] to
6905 InOut[lengthInOut-1] contain filtered
6906 samples */
6907 float *Coef,/* (i) filter coefficients, Coef[0] is assumed
6908 to be 1.0 */
6909 int lengthInOut,/* (i) number of input/output samples */
6910 int orderCoef /* (i) number of filter coefficients */
6911 );
6913 void AllZeroFilter(
6914 float *In, /* (i) In[0] to In[lengthInOut-1] contain
6915 filter input samples */
6916 float *Coef,/* (i) filter coefficients (Coef[0] is assumed
6917 to be 1.0) */
6918 int lengthInOut,/* (i) number of input/output samples */
6919 int orderCoef, /* (i) number of filter coefficients */
6920 float *Out /* (i/o) on entrance Out[-orderCoef] to Out[-1]
6921 contain the filter state, on exit Out[0]
6922 to Out[lengthInOut-1] contain filtered
6923 samples */
6924 );
6926 void ZeroPoleFilter(
6927 float *In, /* (i) In[0] to In[lengthInOut-1] contain filter
6928 input samples In[-orderCoef] to In[-1]
6929 contain state of all-zero section */
6930 float *ZeroCoef,/* (i) filter coefficients for all-zero
6931 section (ZeroCoef[0] is assumed to
6932 be 1.0) */
6933 float *PoleCoef,/* (i) filter coefficients for all-pole section
6934 (ZeroCoef[0] is assumed to be 1.0) */
6935 int lengthInOut,/* (i) number of input/output samples */
6936 int orderCoef, /* (i) number of filter coefficients */
6937 float *Out /* (i/o) on entrance Out[-orderCoef] to Out[-1]
6938 contain state of all-pole section. On
6939 exit Out[0] to Out[lengthInOut-1]
6940 contain filtered samples */
6941 );
6946 Andersen, et al. Experimental [Page 124]
6948 RFC 3951 Internet Low Bit Rate Codec December 2004
6951 void DownSample (
6952 float *In, /* (i) input samples */
6953 float *Coef, /* (i) filter coefficients */
6954 int lengthIn, /* (i) number of input samples */
6955 float *state, /* (i) filter state */
6956 float *Out /* (o) downsampled output */
6957 );
6959 #endif
6961 A.18. filter.c
6963 /******************************************************************
6965 iLBC Speech Coder ANSI-C Source Code
6967 filter.c
6969 Copyright (C) The Internet Society (2004).
6970 All Rights Reserved.
6972 ******************************************************************/
6974 #include "iLBC_define.h"
6976 /*----------------------------------------------------------------*
6977 * all-pole filter
6978 *---------------------------------------------------------------*/
6980 void AllPoleFilter(
6981 float *InOut, /* (i/o) on entrance InOut[-orderCoef] to
6982 InOut[-1] contain the state of the
6983 filter (delayed samples). InOut[0] to
6984 InOut[lengthInOut-1] contain the filter
6985 input, on en exit InOut[-orderCoef] to
6986 InOut[-1] is unchanged and InOut[0] to
6987 InOut[lengthInOut-1] contain filtered
6988 samples */
6989 float *Coef,/* (i) filter coefficients, Coef[0] is assumed
6990 to be 1.0 */
6991 int lengthInOut,/* (i) number of input/output samples */
6992 int orderCoef /* (i) number of filter coefficients */
6993 ){
6994 int n,k;
6996 for(n=0;n<lengthInOut;n++){
6997 for(k=1;k<=orderCoef;k++){
6998 *InOut -= Coef[k]*InOut[-k];
7002 Andersen, et al. Experimental [Page 125]
7004 RFC 3951 Internet Low Bit Rate Codec December 2004
7007 }
7008 InOut++;
7009 }
7010 }
7012 /*----------------------------------------------------------------*
7013 * all-zero filter
7014 *---------------------------------------------------------------*/
7016 void AllZeroFilter(
7017 float *In, /* (i) In[0] to In[lengthInOut-1] contain
7018 filter input samples */
7019 float *Coef,/* (i) filter coefficients (Coef[0] is assumed
7020 to be 1.0) */
7021 int lengthInOut,/* (i) number of input/output samples */
7022 int orderCoef, /* (i) number of filter coefficients */
7023 float *Out /* (i/o) on entrance Out[-orderCoef] to Out[-1]
7024 contain the filter state, on exit Out[0]
7025 to Out[lengthInOut-1] contain filtered
7026 samples */
7027 ){
7028 int n,k;
7030 for(n=0;n<lengthInOut;n++){
7031 *Out = Coef[0]*In[0];
7032 for(k=1;k<=orderCoef;k++){
7033 *Out += Coef[k]*In[-k];
7034 }
7035 Out++;
7036 In++;
7037 }
7038 }
7040 /*----------------------------------------------------------------*
7041 * pole-zero filter
7042 *---------------------------------------------------------------*/
7044 void ZeroPoleFilter(
7045 float *In, /* (i) In[0] to In[lengthInOut-1] contain
7046 filter input samples In[-orderCoef] to
7047 In[-1] contain state of all-zero
7048 section */
7049 float *ZeroCoef,/* (i) filter coefficients for all-zero
7050 section (ZeroCoef[0] is assumed to
7051 be 1.0) */
7052 float *PoleCoef,/* (i) filter coefficients for all-pole section
7053 (ZeroCoef[0] is assumed to be 1.0) */
7054 int lengthInOut,/* (i) number of input/output samples */
7058 Andersen, et al. Experimental [Page 126]
7060 RFC 3951 Internet Low Bit Rate Codec December 2004
7063 int orderCoef, /* (i) number of filter coefficients */
7064 float *Out /* (i/o) on entrance Out[-orderCoef] to Out[-1]
7065 contain state of all-pole section. On
7066 exit Out[0] to Out[lengthInOut-1]
7067 contain filtered samples */
7068 ){
7069 AllZeroFilter(In,ZeroCoef,lengthInOut,orderCoef,Out);
7070 AllPoleFilter(Out,PoleCoef,lengthInOut,orderCoef);
7071 }
7073 /*----------------------------------------------------------------*
7074 * downsample (LP filter and decimation)
7075 *---------------------------------------------------------------*/
7077 void DownSample (
7078 float *In, /* (i) input samples */
7079 float *Coef, /* (i) filter coefficients */
7080 int lengthIn, /* (i) number of input samples */
7081 float *state, /* (i) filter state */
7082 float *Out /* (o) downsampled output */
7083 ){
7084 float o;
7085 float *Out_ptr = Out;
7086 float *Coef_ptr, *In_ptr;
7087 float *state_ptr;
7088 int i, j, stop;
7090 /* LP filter and decimate at the same time */
7092 for (i = DELAY_DS; i < lengthIn; i+=FACTOR_DS)
7093 {
7094 Coef_ptr = &Coef[0];
7095 In_ptr = &In[i];
7096 state_ptr = &state[FILTERORDER_DS-2];
7098 o = (float)0.0;
7100 stop = (i < FILTERORDER_DS) ? i + 1 : FILTERORDER_DS;
7102 for (j = 0; j < stop; j++)
7103 {
7104 o += *Coef_ptr++ * (*In_ptr--);
7105 }
7106 for (j = i + 1; j < FILTERORDER_DS; j++)
7107 {
7108 o += *Coef_ptr++ * (*state_ptr--);
7109 }
7114 Andersen, et al. Experimental [Page 127]
7116 RFC 3951 Internet Low Bit Rate Codec December 2004
7119 *Out_ptr++ = o;
7120 }
7122 /* Get the last part (use zeros as input for the future) */
7124 for (i=(lengthIn+FACTOR_DS); i<(lengthIn+DELAY_DS);
7125 i+=FACTOR_DS) {
7127 o=(float)0.0;
7129 if (i<lengthIn) {
7130 Coef_ptr = &Coef[0];
7131 In_ptr = &In[i];
7132 for (j=0; j<FILTERORDER_DS; j++) {
7133 o += *Coef_ptr++ * (*Out_ptr--);
7134 }
7135 } else {
7136 Coef_ptr = &Coef[i-lengthIn];
7137 In_ptr = &In[lengthIn-1];
7138 for (j=0; j<FILTERORDER_DS-(i-lengthIn); j++) {
7139 o += *Coef_ptr++ * (*In_ptr--);
7140 }
7141 }
7142 *Out_ptr++ = o;
7143 }
7144 }
7146 A.19. FrameClassify.h
7148 /******************************************************************
7150 iLBC Speech Coder ANSI-C Source Code
7152 FrameClassify.h
7154 Copyright (C) The Internet Society (2004).
7155 All Rights Reserved.
7157 ******************************************************************/
7159 #ifndef __iLBC_FRAMECLASSIFY_H
7160 #define __iLBC_FRAMECLASSIFY_H
7162 int FrameClassify( /* index to the max-energy sub-frame */
7163 iLBC_Enc_Inst_t *iLBCenc_inst,
7164 /* (i/o) the encoder state structure */
7165 float *residual /* (i) lpc residual signal */
7166 );
7170 Andersen, et al. Experimental [Page 128]
7172 RFC 3951 Internet Low Bit Rate Codec December 2004
7175 #endif
7177 A.20. FrameClassify.c
7179 /******************************************************************
7181 iLBC Speech Coder ANSI-C Source Code
7183 FrameClassify.c
7185 Copyright (C) The Internet Society (2004).
7186 All Rights Reserved.
7188 ******************************************************************/
7190 #include "iLBC_define.h"
7192 /*---------------------------------------------------------------*
7193 * Classification of subframes to localize start state
7194 *--------------------------------------------------------------*/
7196 int FrameClassify( /* index to the max-energy sub-frame */
7197 iLBC_Enc_Inst_t *iLBCenc_inst,
7198 /* (i/o) the encoder state structure */
7199 float *residual /* (i) lpc residual signal */
7200 ) {
7201 float max_ssqEn, fssqEn[NSUB_MAX], bssqEn[NSUB_MAX], *pp;
7202 int n, l, max_ssqEn_n;
7203 const float ssqEn_win[NSUB_MAX-1]={(float)0.8,(float)0.9,
7204 (float)1.0,(float)0.9,(float)0.8};
7205 const float sampEn_win[5]={(float)1.0/(float)6.0,
7206 (float)2.0/(float)6.0, (float)3.0/(float)6.0,
7207 (float)4.0/(float)6.0, (float)5.0/(float)6.0};
7209 /* init the front and back energies to zero */
7211 memset(fssqEn, 0, NSUB_MAX*sizeof(float));
7212 memset(bssqEn, 0, NSUB_MAX*sizeof(float));
7214 /* Calculate front of first seqence */
7216 n=0;
7217 pp=residual;
7218 for (l=0; l<5; l++) {
7219 fssqEn[n] += sampEn_win[l] * (*pp) * (*pp);
7220 pp++;
7221 }
7222 for (l=5; l<SUBL; l++) {
7226 Andersen, et al. Experimental [Page 129]
7228 RFC 3951 Internet Low Bit Rate Codec December 2004
7231 fssqEn[n] += (*pp) * (*pp);
7232 pp++;
7233 }
7235 /* Calculate front and back of all middle sequences */
7237 for (n=1; n<iLBCenc_inst->nsub-1; n++) {
7238 pp=residual+n*SUBL;
7239 for (l=0; l<5; l++) {
7240 fssqEn[n] += sampEn_win[l] * (*pp) * (*pp);
7241 bssqEn[n] += (*pp) * (*pp);
7242 pp++;
7243 }
7244 for (l=5; l<SUBL-5; l++) {
7245 fssqEn[n] += (*pp) * (*pp);
7246 bssqEn[n] += (*pp) * (*pp);
7247 pp++;
7248 }
7249 for (l=SUBL-5; l<SUBL; l++) {
7250 fssqEn[n] += (*pp) * (*pp);
7251 bssqEn[n] += sampEn_win[SUBL-l-1] * (*pp) * (*pp);
7252 pp++;
7253 }
7254 }
7256 /* Calculate back of last seqence */
7258 n=iLBCenc_inst->nsub-1;
7259 pp=residual+n*SUBL;
7260 for (l=0; l<SUBL-5; l++) {
7261 bssqEn[n] += (*pp) * (*pp);
7262 pp++;
7263 }
7264 for (l=SUBL-5; l<SUBL; l++) {
7265 bssqEn[n] += sampEn_win[SUBL-l-1] * (*pp) * (*pp);
7266 pp++;
7267 }
7269 /* find the index to the weighted 80 sample with
7270 most energy */
7272 if (iLBCenc_inst->mode==20) l=1;
7273 else l=0;
7275 max_ssqEn=(fssqEn[0]+bssqEn[1])*ssqEn_win[l];
7276 max_ssqEn_n=1;
7277 for (n=2; n<iLBCenc_inst->nsub; n++) {
7282 Andersen, et al. Experimental [Page 130]
7284 RFC 3951 Internet Low Bit Rate Codec December 2004
7287 l++;
7288 if ((fssqEn[n-1]+bssqEn[n])*ssqEn_win[l] > max_ssqEn) {
7289 max_ssqEn=(fssqEn[n-1]+bssqEn[n]) *
7290 ssqEn_win[l];
7291 max_ssqEn_n=n;
7292 }
7293 }
7295 return max_ssqEn_n;
7296 }
7298 A.21. gainquant.h
7300 /******************************************************************
7302 iLBC Speech Coder ANSI-C Source Code
7304 gainquant.h
7306 Copyright (C) The Internet Society (2004).
7307 All Rights Reserved.
7309 ******************************************************************/
7311 #ifndef __iLBC_GAINQUANT_H
7312 #define __iLBC_GAINQUANT_H
7314 float gainquant(/* (o) quantized gain value */
7315 float in, /* (i) gain value */
7316 float maxIn,/* (i) maximum of gain value */
7317 int cblen, /* (i) number of quantization indices */
7318 int *index /* (o) quantization index */
7319 );
7321 float gaindequant( /* (o) quantized gain value */
7322 int index, /* (i) quantization index */
7323 float maxIn,/* (i) maximum of unquantized gain */
7324 int cblen /* (i) number of quantization indices */
7325 );
7327 #endif
7329 A.22. gainquant.c
7331 /******************************************************************
7333 iLBC Speech Coder ANSI-C Source Code
7338 Andersen, et al. Experimental [Page 131]
7340 RFC 3951 Internet Low Bit Rate Codec December 2004
7343 gainquant.c
7345 Copyright (C) The Internet Society (2004).
7346 All Rights Reserved.
7348 ******************************************************************/
7350 #include <string.h>
7351 #include <math.h>
7352 #include "constants.h"
7353 #include "filter.h"
7355 /*----------------------------------------------------------------*
7356 * quantizer for the gain in the gain-shape coding of residual
7357 *---------------------------------------------------------------*/
7359 float gainquant(/* (o) quantized gain value */
7360 float in, /* (i) gain value */
7361 float maxIn,/* (i) maximum of gain value */
7362 int cblen, /* (i) number of quantization indices */
7363 int *index /* (o) quantization index */
7364 ){
7365 int i, tindex;
7366 float minmeasure,measure, *cb, scale;
7368 /* ensure a lower bound on the scaling factor */
7370 scale=maxIn;
7372 if (scale<0.1) {
7373 scale=(float)0.1;
7374 }
7376 /* select the quantization table */
7378 if (cblen == 8) {
7379 cb = gain_sq3Tbl;
7380 } else if (cblen == 16) {
7381 cb = gain_sq4Tbl;
7382 } else {
7383 cb = gain_sq5Tbl;
7384 }
7386 /* select the best index in the quantization table */
7388 minmeasure=10000000.0;
7389 tindex=0;
7390 for (i=0; i<cblen; i++) {
7394 Andersen, et al. Experimental [Page 132]
7396 RFC 3951 Internet Low Bit Rate Codec December 2004
7399 measure=(in-scale*cb[i])*(in-scale*cb[i]);
7401 if (measure<minmeasure) {
7402 tindex=i;
7403 minmeasure=measure;
7404 }
7405 }
7406 *index=tindex;
7408 /* return the quantized value */
7410 return scale*cb[tindex];
7411 }
7413 /*----------------------------------------------------------------*
7414 * decoder for quantized gains in the gain-shape coding of
7415 * residual
7416 *---------------------------------------------------------------*/
7418 float gaindequant( /* (o) quantized gain value */
7419 int index, /* (i) quantization index */
7420 float maxIn,/* (i) maximum of unquantized gain */
7421 int cblen /* (i) number of quantization indices */
7422 ){
7423 float scale;
7425 /* obtain correct scale factor */
7427 scale=(float)fabs(maxIn);
7429 if (scale<0.1) {
7430 scale=(float)0.1;
7431 }
7433 /* select the quantization table and return the decoded value */
7435 if (cblen==8) {
7436 return scale*gain_sq3Tbl[index];
7437 } else if (cblen==16) {
7438 return scale*gain_sq4Tbl[index];
7439 }
7440 else if (cblen==32) {
7441 return scale*gain_sq5Tbl[index];
7442 }
7444 return 0.0;
7445 }
7450 Andersen, et al. Experimental [Page 133]
7452 RFC 3951 Internet Low Bit Rate Codec December 2004
7455 A.23. getCBvec.h
7457 /******************************************************************
7459 iLBC Speech Coder ANSI-C Source Code
7461 getCBvec.h
7463 Copyright (C) The Internet Society (2004).
7464 All Rights Reserved.
7466 ******************************************************************/
7468 #ifndef __iLBC_GETCBVEC_H
7469 #define __iLBC_GETCBVEC_H
7471 void getCBvec(
7472 float *cbvec, /* (o) Constructed codebook vector */
7473 float *mem, /* (i) Codebook buffer */
7474 int index, /* (i) Codebook index */
7475 int lMem, /* (i) Length of codebook buffer */
7476 int cbveclen/* (i) Codebook vector length */
7477 );
7479 #endif
7481 A.24. getCBvec.c
7483 /******************************************************************
7485 iLBC Speech Coder ANSI-C Source Code
7487 getCBvec.c
7489 Copyright (C) The Internet Society (2004).
7490 All Rights Reserved.
7492 ******************************************************************/
7494 #include "iLBC_define.h"
7495 #include "constants.h"
7496 #include <string.h>
7498 /*----------------------------------------------------------------*
7499 * Construct codebook vector for given index.
7500 *---------------------------------------------------------------*/
7502 void getCBvec(
7506 Andersen, et al. Experimental [Page 134]
7508 RFC 3951 Internet Low Bit Rate Codec December 2004
7511 float *cbvec, /* (o) Constructed codebook vector */
7512 float *mem, /* (i) Codebook buffer */
7513 int index, /* (i) Codebook index */
7514 int lMem, /* (i) Length of codebook buffer */
7515 int cbveclen/* (i) Codebook vector length */
7516 ){
7517 int j, k, n, memInd, sFilt;
7518 float tmpbuf[CB_MEML];
7519 int base_size;
7520 int ilow, ihigh;
7521 float alfa, alfa1;
7523 /* Determine size of codebook sections */
7525 base_size=lMem-cbveclen+1;
7527 if (cbveclen==SUBL) {
7528 base_size+=cbveclen/2;
7529 }
7531 /* No filter -> First codebook section */
7533 if (index<lMem-cbveclen+1) {
7535 /* first non-interpolated vectors */
7537 k=index+cbveclen;
7538 /* get vector */
7539 memcpy(cbvec, mem+lMem-k, cbveclen*sizeof(float));
7541 } else if (index < base_size) {
7543 k=2*(index-(lMem-cbveclen+1))+cbveclen;
7545 ihigh=k/2;
7546 ilow=ihigh-5;
7548 /* Copy first noninterpolated part */
7550 memcpy(cbvec, mem+lMem-k/2, ilow*sizeof(float));
7552 /* interpolation */
7554 alfa1=(float)0.2;
7555 alfa=0.0;
7556 for (j=ilow; j<ihigh; j++) {
7557 cbvec[j]=((float)1.0-alfa)*mem[lMem-k/2+j]+
7558 alfa*mem[lMem-k+j];
7562 Andersen, et al. Experimental [Page 135]
7564 RFC 3951 Internet Low Bit Rate Codec December 2004
7567 alfa+=alfa1;
7568 }
7570 /* Copy second noninterpolated part */
7572 memcpy(cbvec+ihigh, mem+lMem-k+ihigh,
7573 (cbveclen-ihigh)*sizeof(float));
7575 }
7577 /* Higher codebook section based on filtering */
7579 else {
7581 /* first non-interpolated vectors */
7583 if (index-base_size<lMem-cbveclen+1) {
7584 float tempbuff2[CB_MEML+CB_FILTERLEN+1];
7585 float *pos;
7586 float *pp, *pp1;
7588 memset(tempbuff2, 0,
7589 CB_HALFFILTERLEN*sizeof(float));
7590 memcpy(&tempbuff2[CB_HALFFILTERLEN], mem,
7591 lMem*sizeof(float));
7592 memset(&tempbuff2[lMem+CB_HALFFILTERLEN], 0,
7593 (CB_HALFFILTERLEN+1)*sizeof(float));
7595 k=index-base_size+cbveclen;
7596 sFilt=lMem-k;
7597 memInd=sFilt+1-CB_HALFFILTERLEN;
7599 /* do filtering */
7600 pos=cbvec;
7601 memset(pos, 0, cbveclen*sizeof(float));
7602 for (n=0; n<cbveclen; n++) {
7603 pp=&tempbuff2[memInd+n+CB_HALFFILTERLEN];
7604 pp1=&cbfiltersTbl[CB_FILTERLEN-1];
7605 for (j=0; j<CB_FILTERLEN; j++) {
7606 (*pos)+=(*pp++)*(*pp1--);
7607 }
7608 pos++;
7609 }
7610 }
7612 /* interpolated vectors */
7614 else {
7618 Andersen, et al. Experimental [Page 136]
7620 RFC 3951 Internet Low Bit Rate Codec December 2004
7623 float tempbuff2[CB_MEML+CB_FILTERLEN+1];
7625 float *pos;
7626 float *pp, *pp1;
7627 int i;
7629 memset(tempbuff2, 0,
7630 CB_HALFFILTERLEN*sizeof(float));
7631 memcpy(&tempbuff2[CB_HALFFILTERLEN], mem,
7632 lMem*sizeof(float));
7633 memset(&tempbuff2[lMem+CB_HALFFILTERLEN], 0,
7634 (CB_HALFFILTERLEN+1)*sizeof(float));
7636 k=2*(index-base_size-
7637 (lMem-cbveclen+1))+cbveclen;
7638 sFilt=lMem-k;
7639 memInd=sFilt+1-CB_HALFFILTERLEN;
7641 /* do filtering */
7642 pos=&tmpbuf[sFilt];
7643 memset(pos, 0, k*sizeof(float));
7644 for (i=0; i<k; i++) {
7645 pp=&tempbuff2[memInd+i+CB_HALFFILTERLEN];
7646 pp1=&cbfiltersTbl[CB_FILTERLEN-1];
7647 for (j=0; j<CB_FILTERLEN; j++) {
7648 (*pos)+=(*pp++)*(*pp1--);
7649 }
7650 pos++;
7651 }
7653 ihigh=k/2;
7654 ilow=ihigh-5;
7656 /* Copy first noninterpolated part */
7658 memcpy(cbvec, tmpbuf+lMem-k/2,
7659 ilow*sizeof(float));
7661 /* interpolation */
7663 alfa1=(float)0.2;
7664 alfa=0.0;
7665 for (j=ilow; j<ihigh; j++) {
7666 cbvec[j]=((float)1.0-alfa)*
7667 tmpbuf[lMem-k/2+j]+alfa*tmpbuf[lMem-k+j];
7668 alfa+=alfa1;
7669 }
7674 Andersen, et al. Experimental [Page 137]
7676 RFC 3951 Internet Low Bit Rate Codec December 2004
7679 /* Copy second noninterpolated part */
7681 memcpy(cbvec+ihigh, tmpbuf+lMem-k+ihigh,
7682 (cbveclen-ihigh)*sizeof(float));
7683 }
7684 }
7685 }
7687 A.25. helpfun.h
7689 /******************************************************************
7691 iLBC Speech Coder ANSI-C Source Code
7693 helpfun.h
7695 Copyright (C) The Internet Society (2004).
7696 All Rights Reserved.
7698 ******************************************************************/
7700 #ifndef __iLBC_HELPFUN_H
7701 #define __iLBC_HELPFUN_H
7703 void autocorr(
7704 float *r, /* (o) autocorrelation vector */
7705 const float *x, /* (i) data vector */
7706 int N, /* (i) length of data vector */
7707 int order /* largest lag for calculated
7708 autocorrelations */
7709 );
7711 void window(
7712 float *z, /* (o) the windowed data */
7713 const float *x, /* (i) the original data vector */
7714 const float *y, /* (i) the window */
7715 int N /* (i) length of all vectors */
7716 );
7718 void levdurb(
7719 float *a, /* (o) lpc coefficient vector starting
7720 with 1.0 */
7721 float *k, /* (o) reflection coefficients */
7722 float *r, /* (i) autocorrelation vector */
7723 int order /* (i) order of lpc filter */
7724 );
7726 void interpolate(
7730 Andersen, et al. Experimental [Page 138]
7732 RFC 3951 Internet Low Bit Rate Codec December 2004
7735 float *out, /* (o) the interpolated vector */
7736 float *in1, /* (i) the first vector for the
7737 interpolation */
7738 float *in2, /* (i) the second vector for the
7739 interpolation */
7740 float coef, /* (i) interpolation weights */
7741 int length /* (i) length of all vectors */
7742 );
7744 void bwexpand(
7745 float *out, /* (o) the bandwidth expanded lpc
7746 coefficients */
7747 float *in, /* (i) the lpc coefficients before bandwidth
7748 expansion */
7749 float coef, /* (i) the bandwidth expansion factor */
7750 int length /* (i) the length of lpc coefficient vectors */
7751 );
7753 void vq(
7754 float *Xq, /* (o) the quantized vector */
7755 int *index, /* (o) the quantization index */
7756 const float *CB,/* (i) the vector quantization codebook */
7757 float *X, /* (i) the vector to quantize */
7758 int n_cb, /* (i) the number of vectors in the codebook */
7759 int dim /* (i) the dimension of all vectors */
7760 );
7762 void SplitVQ(
7763 float *qX, /* (o) the quantized vector */
7764 int *index, /* (o) a vector of indexes for all vector
7765 codebooks in the split */
7766 float *X, /* (i) the vector to quantize */
7767 const float *CB,/* (i) the quantizer codebook */
7768 int nsplit, /* the number of vector splits */
7769 const int *dim, /* the dimension of X and qX */
7770 const int *cbsize /* the number of vectors in the codebook */
7771 );
7774 void sort_sq(
7775 float *xq, /* (o) the quantized value */
7776 int *index, /* (o) the quantization index */
7777 float x, /* (i) the value to quantize */
7778 const float *cb,/* (i) the quantization codebook */
7779 int cb_size /* (i) the size of the quantization codebook */
7780 );
7782 int LSF_check( /* (o) 1 for stable lsf vectors and 0 for
7786 Andersen, et al. Experimental [Page 139]
7788 RFC 3951 Internet Low Bit Rate Codec December 2004
7791 nonstable ones */
7792 float *lsf, /* (i) a table of lsf vectors */
7793 int dim, /* (i) the dimension of each lsf vector */
7794 int NoAn /* (i) the number of lsf vectors in the
7795 table */
7796 );
7798 #endif
7800 A.26. helpfun.c
7802 /******************************************************************
7804 iLBC Speech Coder ANSI-C Source Code
7806 helpfun.c
7808 Copyright (C) The Internet Society (2004).
7809 All Rights Reserved.
7811 ******************************************************************/
7813 #include <math.h>
7815 #include "iLBC_define.h"
7816 #include "constants.h"
7818 /*----------------------------------------------------------------*
7819 * calculation of auto correlation
7820 *---------------------------------------------------------------*/
7822 void autocorr(
7823 float *r, /* (o) autocorrelation vector */
7824 const float *x, /* (i) data vector */
7825 int N, /* (i) length of data vector */
7826 int order /* largest lag for calculated
7827 autocorrelations */
7828 ){
7829 int lag, n;
7830 float sum;
7832 for (lag = 0; lag <= order; lag++) {
7833 sum = 0;
7834 for (n = 0; n < N - lag; n++) {
7835 sum += x[n] * x[n+lag];
7836 }
7837 r[lag] = sum;
7838 }
7842 Andersen, et al. Experimental [Page 140]
7844 RFC 3951 Internet Low Bit Rate Codec December 2004
7847 }
7849 /*----------------------------------------------------------------*
7850 * window multiplication
7851 *---------------------------------------------------------------*/
7853 void window(
7854 float *z, /* (o) the windowed data */
7855 const float *x, /* (i) the original data vector */
7856 const float *y, /* (i) the window */
7857 int N /* (i) length of all vectors */
7858 ){
7859 int i;
7861 for (i = 0; i < N; i++) {
7862 z[i] = x[i] * y[i];
7863 }
7864 }
7866 /*----------------------------------------------------------------*
7867 * levinson-durbin solution for lpc coefficients
7868 *---------------------------------------------------------------*/
7870 void levdurb(
7871 float *a, /* (o) lpc coefficient vector starting
7872 with 1.0 */
7873 float *k, /* (o) reflection coefficients */
7874 float *r, /* (i) autocorrelation vector */
7875 int order /* (i) order of lpc filter */
7876 ){
7877 float sum, alpha;
7878 int m, m_h, i;
7880 a[0] = 1.0;
7882 if (r[0] < EPS) { /* if r[0] <= 0, set LPC coeff. to zero */
7883 for (i = 0; i < order; i++) {
7884 k[i] = 0;
7885 a[i+1] = 0;
7886 }
7887 } else {
7888 a[1] = k[0] = -r[1]/r[0];
7889 alpha = r[0] + r[1] * k[0];
7890 for (m = 1; m < order; m++){
7891 sum = r[m + 1];
7892 for (i = 0; i < m; i++){
7893 sum += a[i+1] * r[m - i];
7894 }
7898 Andersen, et al. Experimental [Page 141]
7900 RFC 3951 Internet Low Bit Rate Codec December 2004
7903 k[m] = -sum / alpha;
7904 alpha += k[m] * sum;
7905 m_h = (m + 1) >> 1;
7906 for (i = 0; i < m_h; i++){
7907 sum = a[i+1] + k[m] * a[m - i];
7908 a[m - i] += k[m] * a[i+1];
7909 a[i+1] = sum;
7910 }
7911 a[m+1] = k[m];
7912 }
7913 }
7914 }
7916 /*----------------------------------------------------------------*
7917 * interpolation between vectors
7918 *---------------------------------------------------------------*/
7920 void interpolate(
7921 float *out, /* (o) the interpolated vector */
7922 float *in1, /* (i) the first vector for the
7923 interpolation */
7924 float *in2, /* (i) the second vector for the
7925 interpolation */
7926 float coef, /* (i) interpolation weights */
7927 int length /* (i) length of all vectors */
7928 ){
7929 int i;
7930 float invcoef;
7932 invcoef = (float)1.0 - coef;
7933 for (i = 0; i < length; i++) {
7934 out[i] = coef * in1[i] + invcoef * in2[i];
7935 }
7936 }
7938 /*----------------------------------------------------------------*
7939 * lpc bandwidth expansion
7940 *---------------------------------------------------------------*/
7942 void bwexpand(
7943 float *out, /* (o) the bandwidth expanded lpc
7944 coefficients */
7945 float *in, /* (i) the lpc coefficients before bandwidth
7946 expansion */
7947 float coef, /* (i) the bandwidth expansion factor */
7948 int length /* (i) the length of lpc coefficient vectors */
7949 ){
7950 int i;
7954 Andersen, et al. Experimental [Page 142]
7956 RFC 3951 Internet Low Bit Rate Codec December 2004
7959 float chirp;
7961 chirp = coef;
7963 out[0] = in[0];
7964 for (i = 1; i < length; i++) {
7965 out[i] = chirp * in[i];
7966 chirp *= coef;
7967 }
7968 }
7970 /*----------------------------------------------------------------*
7971 * vector quantization
7972 *---------------------------------------------------------------*/
7974 void vq(
7975 float *Xq, /* (o) the quantized vector */
7976 int *index, /* (o) the quantization index */
7977 const float *CB,/* (i) the vector quantization codebook */
7978 float *X, /* (i) the vector to quantize */
7979 int n_cb, /* (i) the number of vectors in the codebook */
7980 int dim /* (i) the dimension of all vectors */
7981 ){
7982 int i, j;
7983 int pos, minindex;
7984 float dist, tmp, mindist;
7986 pos = 0;
7987 mindist = FLOAT_MAX;
7988 minindex = 0;
7989 for (j = 0; j < n_cb; j++) {
7990 dist = X[0] - CB[pos];
7991 dist *= dist;
7992 for (i = 1; i < dim; i++) {
7993 tmp = X[i] - CB[pos + i];
7994 dist += tmp*tmp;
7995 }
7997 if (dist < mindist) {
7998 mindist = dist;
7999 minindex = j;
8000 }
8001 pos += dim;
8002 }
8003 for (i = 0; i < dim; i++) {
8004 Xq[i] = CB[minindex*dim + i];
8005 }
8006 *index = minindex;
8010 Andersen, et al. Experimental [Page 143]
8012 RFC 3951 Internet Low Bit Rate Codec December 2004
8015 }
8017 /*----------------------------------------------------------------*
8018 * split vector quantization
8019 *---------------------------------------------------------------*/
8021 void SplitVQ(
8022 float *qX, /* (o) the quantized vector */
8023 int *index, /* (o) a vector of indexes for all vector
8024 codebooks in the split */
8025 float *X, /* (i) the vector to quantize */
8026 const float *CB,/* (i) the quantizer codebook */
8027 int nsplit, /* the number of vector splits */
8028 const int *dim, /* the dimension of X and qX */
8029 const int *cbsize /* the number of vectors in the codebook */
8030 ){
8031 int cb_pos, X_pos, i;
8033 cb_pos = 0;
8034 X_pos= 0;
8035 for (i = 0; i < nsplit; i++) {
8036 vq(qX + X_pos, index + i, CB + cb_pos, X + X_pos,
8037 cbsize[i], dim[i]);
8038 X_pos += dim[i];
8039 cb_pos += dim[i] * cbsize[i];
8040 }
8041 }
8043 /*----------------------------------------------------------------*
8044 * scalar quantization
8045 *---------------------------------------------------------------*/
8047 void sort_sq(
8048 float *xq, /* (o) the quantized value */
8049 int *index, /* (o) the quantization index */
8050 float x, /* (i) the value to quantize */
8051 const float *cb,/* (i) the quantization codebook */
8052 int cb_size /* (i) the size of the quantization codebook */
8053 ){
8054 int i;
8056 if (x <= cb[0]) {
8057 *index = 0;
8058 *xq = cb[0];
8059 } else {
8060 i = 0;
8061 while ((x > cb[i]) && i < cb_size - 1) {
8062 i++;
8066 Andersen, et al. Experimental [Page 144]
8068 RFC 3951 Internet Low Bit Rate Codec December 2004
8071 }
8073 if (x > ((cb[i] + cb[i - 1])/2)) {
8074 *index = i;
8075 *xq = cb[i];
8076 } else {
8077 *index = i - 1;
8078 *xq = cb[i - 1];
8079 }
8080 }
8081 }
8083 /*----------------------------------------------------------------*
8084 * check for stability of lsf coefficients
8085 *---------------------------------------------------------------*/
8087 int LSF_check( /* (o) 1 for stable lsf vectors and 0 for
8088 nonstable ones */
8089 float *lsf, /* (i) a table of lsf vectors */
8090 int dim, /* (i) the dimension of each lsf vector */
8091 int NoAn /* (i) the number of lsf vectors in the
8092 table */
8093 ){
8094 int k,n,m, Nit=2, change=0,pos;
8095 float tmp;
8096 static float eps=(float)0.039; /* 50 Hz */
8097 static float eps2=(float)0.0195;
8098 static float maxlsf=(float)3.14; /* 4000 Hz */
8099 static float minlsf=(float)0.01; /* 0 Hz */
8101 /* LSF separation check*/
8103 for (n=0; n<Nit; n++) { /* Run through a couple of times */
8104 for (m=0; m<NoAn; m++) { /* Number of analyses per frame */
8105 for (k=0; k<(dim-1); k++) {
8106 pos=m*dim+k;
8108 if ((lsf[pos+1]-lsf[pos])<eps) {
8110 if (lsf[pos+1]<lsf[pos]) {
8111 tmp=lsf[pos+1];
8112 lsf[pos+1]= lsf[pos]+eps2;
8113 lsf[pos]= lsf[pos+1]-eps2;
8114 } else {
8115 lsf[pos]-=eps2;
8116 lsf[pos+1]+=eps2;
8117 }
8118 change=1;
8122 Andersen, et al. Experimental [Page 145]
8124 RFC 3951 Internet Low Bit Rate Codec December 2004
8127 }
8129 if (lsf[pos]<minlsf) {
8130 lsf[pos]=minlsf;
8131 change=1;
8132 }
8134 if (lsf[pos]>maxlsf) {
8135 lsf[pos]=maxlsf;
8136 change=1;
8137 }
8138 }
8139 }
8140 }
8142 return change;
8143 }
8145 A.27. hpInput.h
8147 /******************************************************************
8149 iLBC Speech Coder ANSI-C Source Code
8151 hpInput.h
8153 Copyright (C) The Internet Society (2004).
8154 All Rights Reserved.
8156 ******************************************************************/
8158 #ifndef __iLBC_HPINPUT_H
8159 #define __iLBC_HPINPUT_H
8161 void hpInput(
8162 float *In, /* (i) vector to filter */
8163 int len, /* (i) length of vector to filter */
8164 float *Out, /* (o) the resulting filtered vector */
8165 float *mem /* (i/o) the filter state */
8166 );
8168 #endif
8170 A.28. hpInput.c
8172 /******************************************************************
8174 iLBC Speech Coder ANSI-C Source Code
8178 Andersen, et al. Experimental [Page 146]
8180 RFC 3951 Internet Low Bit Rate Codec December 2004
8183 hpInput.c
8185 Copyright (C) The Internet Society (2004).
8186 All Rights Reserved.
8188 ******************************************************************/
8190 #include "constants.h"
8192 /*----------------------------------------------------------------*
8193 * Input high-pass filter
8194 *---------------------------------------------------------------*/
8196 void hpInput(
8197 float *In, /* (i) vector to filter */
8198 int len, /* (i) length of vector to filter */
8199 float *Out, /* (o) the resulting filtered vector */
8200 float *mem /* (i/o) the filter state */
8201 ){
8202 int i;
8203 float *pi, *po;
8205 /* all-zero section*/
8207 pi = &In[0];
8208 po = &Out[0];
8209 for (i=0; i<len; i++) {
8210 *po = hpi_zero_coefsTbl[0] * (*pi);
8211 *po += hpi_zero_coefsTbl[1] * mem[0];
8212 *po += hpi_zero_coefsTbl[2] * mem[1];
8214 mem[1] = mem[0];
8215 mem[0] = *pi;
8216 po++;
8217 pi++;
8219 }
8221 /* all-pole section*/
8223 po = &Out[0];
8224 for (i=0; i<len; i++) {
8225 *po -= hpi_pole_coefsTbl[1] * mem[2];
8226 *po -= hpi_pole_coefsTbl[2] * mem[3];
8228 mem[3] = mem[2];
8229 mem[2] = *po;
8230 po++;
8234 Andersen, et al. Experimental [Page 147]
8236 RFC 3951 Internet Low Bit Rate Codec December 2004
8239 }
8240 }
8242 A.29. hpOutput.h
8244 /******************************************************************
8246 iLBC Speech Coder ANSI-C Source Code
8248 hpOutput.h
8250 Copyright (C) The Internet Society (2004).
8251 All Rights Reserved.
8253 ******************************************************************/
8255 #ifndef __iLBC_HPOUTPUT_H
8256 #define __iLBC_HPOUTPUT_H
8258 void hpOutput(
8259 float *In, /* (i) vector to filter */
8260 int len,/* (i) length of vector to filter */
8261 float *Out, /* (o) the resulting filtered vector */
8262 float *mem /* (i/o) the filter state */
8263 );
8265 #endif
8267 A.30. hpOutput.c
8269 /******************************************************************
8271 iLBC Speech Coder ANSI-C Source Code
8273 hpOutput.c
8275 Copyright (C) The Internet Society (2004).
8276 All Rights Reserved.
8278 ******************************************************************/
8280 #include "constants.h"
8282 /*----------------------------------------------------------------*
8283 * Output high-pass filter
8284 *---------------------------------------------------------------*/
8286 void hpOutput(
8290 Andersen, et al. Experimental [Page 148]
8292 RFC 3951 Internet Low Bit Rate Codec December 2004
8295 float *In, /* (i) vector to filter */
8296 int len,/* (i) length of vector to filter */
8297 float *Out, /* (o) the resulting filtered vector */
8298 float *mem /* (i/o) the filter state */
8299 ){
8300 int i;
8301 float *pi, *po;
8303 /* all-zero section*/
8305 pi = &In[0];
8306 po = &Out[0];
8307 for (i=0; i<len; i++) {
8308 *po = hpo_zero_coefsTbl[0] * (*pi);
8309 *po += hpo_zero_coefsTbl[1] * mem[0];
8310 *po += hpo_zero_coefsTbl[2] * mem[1];
8312 mem[1] = mem[0];
8313 mem[0] = *pi;
8314 po++;
8315 pi++;
8317 }
8319 /* all-pole section*/
8321 po = &Out[0];
8322 for (i=0; i<len; i++) {
8323 *po -= hpo_pole_coefsTbl[1] * mem[2];
8324 *po -= hpo_pole_coefsTbl[2] * mem[3];
8326 mem[3] = mem[2];
8327 mem[2] = *po;
8328 po++;
8329 }
8330 }
8332 A.31. iCBConstruct.h
8334 /******************************************************************
8336 iLBC Speech Coder ANSI-C Source Code
8338 iCBConstruct.h
8340 Copyright (C) The Internet Society (2004).
8341 All Rights Reserved.
8346 Andersen, et al. Experimental [Page 149]
8348 RFC 3951 Internet Low Bit Rate Codec December 2004
8351 ******************************************************************/
8353 #ifndef __iLBC_ICBCONSTRUCT_H
8354 #define __iLBC_ICBCONSTRUCT_H
8356 void index_conv_enc(
8357 int *index /* (i/o) Codebook indexes */
8358 );
8360 void index_conv_dec(
8361 int *index /* (i/o) Codebook indexes */
8362 );
8364 void iCBConstruct(
8365 float *decvector, /* (o) Decoded vector */
8366 int *index, /* (i) Codebook indices */
8367 int *gain_index,/* (i) Gain quantization indices */
8368 float *mem, /* (i) Buffer for codevector construction */
8369 int lMem, /* (i) Length of buffer */
8370 int veclen, /* (i) Length of vector */
8371 int nStages /* (i) Number of codebook stages */
8372 );
8374 #endif
8376 A.32. iCBConstruct.c
8378 /******************************************************************
8380 iLBC Speech Coder ANSI-C Source Code
8382 iCBConstruct.c
8384 Copyright (C) The Internet Society (2004).
8385 All Rights Reserved.
8387 ******************************************************************/
8389 #include <math.h>
8391 #include "iLBC_define.h"
8392 #include "gainquant.h"
8393 #include "getCBvec.h"
8395 /*----------------------------------------------------------------*
8396 * Convert the codebook indexes to make the search easier
8397 *---------------------------------------------------------------*/
8402 Andersen, et al. Experimental [Page 150]
8404 RFC 3951 Internet Low Bit Rate Codec December 2004
8407 void index_conv_enc(
8408 int *index /* (i/o) Codebook indexes */
8409 ){
8410 int k;
8412 for (k=1; k<CB_NSTAGES; k++) {
8414 if ((index[k]>=108)&&(index[k]<172)) {
8415 index[k]-=64;
8416 } else if (index[k]>=236) {
8417 index[k]-=128;
8418 } else {
8419 /* ERROR */
8420 }
8421 }
8422 }
8424 void index_conv_dec(
8425 int *index /* (i/o) Codebook indexes */
8426 ){
8427 int k;
8429 for (k=1; k<CB_NSTAGES; k++) {
8431 if ((index[k]>=44)&&(index[k]<108)) {
8432 index[k]+=64;
8433 } else if ((index[k]>=108)&&(index[k]<128)) {
8434 index[k]+=128;
8435 } else {
8436 /* ERROR */
8437 }
8438 }
8439 }
8441 /*----------------------------------------------------------------*
8442 * Construct decoded vector from codebook and gains.
8443 *---------------------------------------------------------------*/
8445 void iCBConstruct(
8446 float *decvector, /* (o) Decoded vector */
8447 int *index, /* (i) Codebook indices */
8448 int *gain_index,/* (i) Gain quantization indices */
8449 float *mem, /* (i) Buffer for codevector construction */
8450 int lMem, /* (i) Length of buffer */
8451 int veclen, /* (i) Length of vector */
8452 int nStages /* (i) Number of codebook stages */
8453 ){
8454 int j,k;
8458 Andersen, et al. Experimental [Page 151]
8460 RFC 3951 Internet Low Bit Rate Codec December 2004
8463 float gain[CB_NSTAGES];
8464 float cbvec[SUBL];
8466 /* gain de-quantization */
8468 gain[0] = gaindequant(gain_index[0], 1.0, 32);
8469 if (nStages > 1) {
8470 gain[1] = gaindequant(gain_index[1],
8471 (float)fabs(gain[0]), 16);
8472 }
8473 if (nStages > 2) {
8474 gain[2] = gaindequant(gain_index[2],
8475 (float)fabs(gain[1]), 8);
8476 }
8478 /* codebook vector construction and construction of
8479 total vector */
8481 getCBvec(cbvec, mem, index[0], lMem, veclen);
8482 for (j=0;j<veclen;j++){
8483 decvector[j] = gain[0]*cbvec[j];
8484 }
8485 if (nStages > 1) {
8486 for (k=1; k<nStages; k++) {
8487 getCBvec(cbvec, mem, index[k], lMem, veclen);
8488 for (j=0;j<veclen;j++) {
8489 decvector[j] += gain[k]*cbvec[j];
8490 }
8491 }
8492 }
8493 }
8495 A.33. iCBSearch.h
8497 /******************************************************************
8499 iLBC Speech Coder ANSI-C Source Code
8501 iCBSearch.h
8503 Copyright (C) The Internet Society (2004).
8504 All Rights Reserved.
8506 ******************************************************************/
8508 #ifndef __iLBC_ICBSEARCH_H
8509 #define __iLBC_ICBSEARCH_H
8514 Andersen, et al. Experimental [Page 152]
8516 RFC 3951 Internet Low Bit Rate Codec December 2004
8519 void iCBSearch(
8520 iLBC_Enc_Inst_t *iLBCenc_inst,
8521 /* (i) the encoder state structure */
8522 int *index, /* (o) Codebook indices */
8523 int *gain_index,/* (o) Gain quantization indices */
8524 float *intarget,/* (i) Target vector for encoding */
8525 float *mem, /* (i) Buffer for codebook construction */
8526 int lMem, /* (i) Length of buffer */
8527 int lTarget, /* (i) Length of vector */
8528 int nStages, /* (i) Number of codebook stages */
8529 float *weightDenum, /* (i) weighting filter coefficients */
8530 float *weightState, /* (i) weighting filter state */
8531 int block /* (i) the sub-block number */
8532 );
8534 #endif
8536 A.34. iCBSearch.c
8538 /******************************************************************
8540 iLBC Speech Coder ANSI-C Source Code
8542 iCBSearch.c
8544 Copyright (C) The Internet Society (2004).
8545 All Rights Reserved.
8547 ******************************************************************/
8549 #include <math.h>
8550 #include <string.h>
8552 #include "iLBC_define.h"
8553 #include "gainquant.h"
8554 #include "createCB.h"
8555 #include "filter.h"
8556 #include "constants.h"
8558 /*----------------------------------------------------------------*
8559 * Search routine for codebook encoding and gain quantization.
8560 *---------------------------------------------------------------*/
8562 void iCBSearch(
8563 iLBC_Enc_Inst_t *iLBCenc_inst,
8564 /* (i) the encoder state structure */
8565 int *index, /* (o) Codebook indices */
8566 int *gain_index,/* (o) Gain quantization indices */
8570 Andersen, et al. Experimental [Page 153]
8572 RFC 3951 Internet Low Bit Rate Codec December 2004
8575 float *intarget,/* (i) Target vector for encoding */
8576 float *mem, /* (i) Buffer for codebook construction */
8577 int lMem, /* (i) Length of buffer */
8578 int lTarget, /* (i) Length of vector */
8579 int nStages, /* (i) Number of codebook stages */
8580 float *weightDenum, /* (i) weighting filter coefficients */
8581 float *weightState, /* (i) weighting filter state */
8582 int block /* (i) the sub-block number */
8583 ){
8584 int i, j, icount, stage, best_index, range, counter;
8585 float max_measure, gain, measure, crossDot, ftmp;
8586 float gains[CB_NSTAGES];
8587 float target[SUBL];
8588 int base_index, sInd, eInd, base_size;
8589 int sIndAug=0, eIndAug=0;
8590 float buf[CB_MEML+SUBL+2*LPC_FILTERORDER];
8591 float invenergy[CB_EXPAND*128], energy[CB_EXPAND*128];
8592 float *pp, *ppi=0, *ppo=0, *ppe=0;
8593 float cbvectors[CB_MEML];
8594 float tene, cene, cvec[SUBL];
8595 float aug_vec[SUBL];
8597 memset(cvec,0,SUBL*sizeof(float));
8599 /* Determine size of codebook sections */
8601 base_size=lMem-lTarget+1;
8603 if (lTarget==SUBL) {
8604 base_size=lMem-lTarget+1+lTarget/2;
8605 }
8607 /* setup buffer for weighting */
8609 memcpy(buf,weightState,sizeof(float)*LPC_FILTERORDER);
8610 memcpy(buf+LPC_FILTERORDER,mem,lMem*sizeof(float));
8611 memcpy(buf+LPC_FILTERORDER+lMem,intarget,lTarget*sizeof(float));
8613 /* weighting */
8615 AllPoleFilter(buf+LPC_FILTERORDER, weightDenum,
8616 lMem+lTarget, LPC_FILTERORDER);
8618 /* Construct the codebook and target needed */
8620 memcpy(target, buf+LPC_FILTERORDER+lMem, lTarget*sizeof(float));
8622 tene=0.0;
8626 Andersen, et al. Experimental [Page 154]
8628 RFC 3951 Internet Low Bit Rate Codec December 2004
8631 for (i=0; i<lTarget; i++) {
8632 tene+=target[i]*target[i];
8633 }
8635 /* Prepare search over one more codebook section. This section
8636 is created by filtering the original buffer with a filter. */
8638 filteredCBvecs(cbvectors, buf+LPC_FILTERORDER, lMem);
8640 /* The Main Loop over stages */
8642 for (stage=0; stage<nStages; stage++) {
8644 range = search_rangeTbl[block][stage];
8646 /* initialize search measure */
8648 max_measure = (float)-10000000.0;
8649 gain = (float)0.0;
8650 best_index = 0;
8652 /* Compute cross dot product between the target
8653 and the CB memory */
8655 crossDot=0.0;
8656 pp=buf+LPC_FILTERORDER+lMem-lTarget;
8657 for (j=0; j<lTarget; j++) {
8658 crossDot += target[j]*(*pp++);
8659 }
8661 if (stage==0) {
8663 /* Calculate energy in the first block of
8664 'lTarget' samples. */
8665 ppe = energy;
8666 ppi = buf+LPC_FILTERORDER+lMem-lTarget-1;
8667 ppo = buf+LPC_FILTERORDER+lMem-1;
8669 *ppe=0.0;
8670 pp=buf+LPC_FILTERORDER+lMem-lTarget;
8671 for (j=0; j<lTarget; j++) {
8672 *ppe+=(*pp)*(*pp++);
8673 }
8675 if (*ppe>0.0) {
8676 invenergy[0] = (float) 1.0 / (*ppe + EPS);
8677 } else {
8678 invenergy[0] = (float) 0.0;
8682 Andersen, et al. Experimental [Page 155]
8684 RFC 3951 Internet Low Bit Rate Codec December 2004
8687 }
8688 ppe++;
8690 measure=(float)-10000000.0;
8692 if (crossDot > 0.0) {
8693 measure = crossDot*crossDot*invenergy[0];
8694 }
8695 }
8696 else {
8697 measure = crossDot*crossDot*invenergy[0];
8698 }
8700 /* check if measure is better */
8701 ftmp = crossDot*invenergy[0];
8703 if ((measure>max_measure) && (fabs(ftmp)<CB_MAXGAIN)) {
8704 best_index = 0;
8705 max_measure = measure;
8706 gain = ftmp;
8707 }
8709 /* loop over the main first codebook section,
8710 full search */
8712 for (icount=1; icount<range; icount++) {
8714 /* calculate measure */
8716 crossDot=0.0;
8717 pp = buf+LPC_FILTERORDER+lMem-lTarget-icount;
8719 for (j=0; j<lTarget; j++) {
8720 crossDot += target[j]*(*pp++);
8721 }
8723 if (stage==0) {
8724 *ppe++ = energy[icount-1] + (*ppi)*(*ppi) -
8725 (*ppo)*(*ppo);
8726 ppo--;
8727 ppi--;
8729 if (energy[icount]>0.0) {
8730 invenergy[icount] =
8731 (float)1.0/(energy[icount]+EPS);
8732 } else {
8733 invenergy[icount] = (float) 0.0;
8734 }
8738 Andersen, et al. Experimental [Page 156]
8740 RFC 3951 Internet Low Bit Rate Codec December 2004
8743 measure=(float)-10000000.0;
8745 if (crossDot > 0.0) {
8746 measure = crossDot*crossDot*invenergy[icount];
8747 }
8748 }
8749 else {
8750 measure = crossDot*crossDot*invenergy[icount];
8751 }
8753 /* check if measure is better */
8754 ftmp = crossDot*invenergy[icount];
8756 if ((measure>max_measure) && (fabs(ftmp)<CB_MAXGAIN)) {
8757 best_index = icount;
8758 max_measure = measure;
8759 gain = ftmp;
8760 }
8761 }
8763 /* Loop over augmented part in the first codebook
8764 * section, full search.
8765 * The vectors are interpolated.
8766 */
8768 if (lTarget==SUBL) {
8770 /* Search for best possible cb vector and
8771 compute the CB-vectors' energy. */
8772 searchAugmentedCB(20, 39, stage, base_size-lTarget/2,
8773 target, buf+LPC_FILTERORDER+lMem,
8774 &max_measure, &best_index, &gain, energy,
8775 invenergy);
8776 }
8778 /* set search range for following codebook sections */
8780 base_index=best_index;
8782 /* unrestricted search */
8784 if (CB_RESRANGE == -1) {
8785 sInd=0;
8786 eInd=range-1;
8787 sIndAug=20;
8788 eIndAug=39;
8789 }
8794 Andersen, et al. Experimental [Page 157]
8796 RFC 3951 Internet Low Bit Rate Codec December 2004
8799 /* restricted search around best index from first
8800 codebook section */
8802 else {
8803 /* Initialize search indices */
8804 sIndAug=0;
8805 eIndAug=0;
8806 sInd=base_index-CB_RESRANGE/2;
8807 eInd=sInd+CB_RESRANGE;
8809 if (lTarget==SUBL) {
8811 if (sInd<0) {
8813 sIndAug = 40 + sInd;
8814 eIndAug = 39;
8815 sInd=0;
8817 } else if ( base_index < (base_size-20) ) {
8819 if (eInd > range) {
8820 sInd -= (eInd-range);
8821 eInd = range;
8822 }
8823 } else { /* base_index >= (base_size-20) */
8825 if (sInd < (base_size-20)) {
8826 sIndAug = 20;
8827 sInd = 0;
8828 eInd = 0;
8829 eIndAug = 19 + CB_RESRANGE;
8831 if(eIndAug > 39) {
8832 eInd = eIndAug-39;
8833 eIndAug = 39;
8834 }
8835 } else {
8836 sIndAug = 20 + sInd - (base_size-20);
8837 eIndAug = 39;
8838 sInd = 0;
8839 eInd = CB_RESRANGE - (eIndAug-sIndAug+1);
8840 }
8841 }
8843 } else { /* lTarget = 22 or 23 */
8845 if (sInd < 0) {
8846 eInd -= sInd;
8850 Andersen, et al. Experimental [Page 158]
8852 RFC 3951 Internet Low Bit Rate Codec December 2004
8855 sInd = 0;
8856 }
8858 if(eInd > range) {
8859 sInd -= (eInd - range);
8860 eInd = range;
8861 }
8862 }
8863 }
8865 /* search of higher codebook section */
8867 /* index search range */
8868 counter = sInd;
8869 sInd += base_size;
8870 eInd += base_size;
8873 if (stage==0) {
8874 ppe = energy+base_size;
8875 *ppe=0.0;
8877 pp=cbvectors+lMem-lTarget;
8878 for (j=0; j<lTarget; j++) {
8879 *ppe+=(*pp)*(*pp++);
8880 }
8882 ppi = cbvectors + lMem - 1 - lTarget;
8883 ppo = cbvectors + lMem - 1;
8885 for (j=0; j<(range-1); j++) {
8886 *(ppe+1) = *ppe + (*ppi)*(*ppi) - (*ppo)*(*ppo);
8887 ppo--;
8888 ppi--;
8889 ppe++;
8890 }
8891 }
8893 /* loop over search range */
8895 for (icount=sInd; icount<eInd; icount++) {
8897 /* calculate measure */
8899 crossDot=0.0;
8900 pp=cbvectors + lMem - (counter++) - lTarget;
8902 for (j=0;j<lTarget;j++) {
8906 Andersen, et al. Experimental [Page 159]
8908 RFC 3951 Internet Low Bit Rate Codec December 2004
8911 crossDot += target[j]*(*pp++);
8912 }
8914 if (energy[icount]>0.0) {
8915 invenergy[icount] =(float)1.0/(energy[icount]+EPS);
8916 } else {
8917 invenergy[icount] =(float)0.0;
8918 }
8920 if (stage==0) {
8922 measure=(float)-10000000.0;
8924 if (crossDot > 0.0) {
8925 measure = crossDot*crossDot*
8926 invenergy[icount];
8927 }
8928 }
8929 else {
8930 measure = crossDot*crossDot*invenergy[icount];
8931 }
8933 /* check if measure is better */
8934 ftmp = crossDot*invenergy[icount];
8936 if ((measure>max_measure) && (fabs(ftmp)<CB_MAXGAIN)) {
8937 best_index = icount;
8938 max_measure = measure;
8939 gain = ftmp;
8940 }
8941 }
8943 /* Search the augmented CB inside the limited range. */
8945 if ((lTarget==SUBL)&&(sIndAug!=0)) {
8946 searchAugmentedCB(sIndAug, eIndAug, stage,
8947 2*base_size-20, target, cbvectors+lMem,
8948 &max_measure, &best_index, &gain, energy,
8949 invenergy);
8950 }
8952 /* record best index */
8954 index[stage] = best_index;
8956 /* gain quantization */
8958 if (stage==0){
8962 Andersen, et al. Experimental [Page 160]
8964 RFC 3951 Internet Low Bit Rate Codec December 2004
8968 if (gain<0.0){
8969 gain = 0.0;
8970 }
8972 if (gain>CB_MAXGAIN) {
8973 gain = (float)CB_MAXGAIN;
8974 }
8975 gain = gainquant(gain, 1.0, 32, &gain_index[stage]);
8976 }
8977 else {
8978 if (stage==1) {
8979 gain = gainquant(gain, (float)fabs(gains[stage-1]),
8980 16, &gain_index[stage]);
8981 } else {
8982 gain = gainquant(gain, (float)fabs(gains[stage-1]),
8983 8, &gain_index[stage]);
8984 }
8985 }
8987 /* Extract the best (according to measure)
8988 codebook vector */
8990 if (lTarget==(STATE_LEN-iLBCenc_inst->state_short_len)) {
8992 if (index[stage]<base_size) {
8993 pp=buf+LPC_FILTERORDER+lMem-lTarget-index[stage];
8994 } else {
8995 pp=cbvectors+lMem-lTarget-
8996 index[stage]+base_size;
8997 }
8998 } else {
9000 if (index[stage]<base_size) {
9001 if (index[stage]<(base_size-20)) {
9002 pp=buf+LPC_FILTERORDER+lMem-
9003 lTarget-index[stage];
9004 } else {
9005 createAugmentedVec(index[stage]-base_size+40,
9006 buf+LPC_FILTERORDER+lMem,aug_vec);
9007 pp=aug_vec;
9008 }
9009 } else {
9010 int filterno, position;
9012 filterno=index[stage]/base_size;
9013 position=index[stage]-filterno*base_size;
9018 Andersen, et al. Experimental [Page 161]
9020 RFC 3951 Internet Low Bit Rate Codec December 2004
9024 if (position<(base_size-20)) {
9025 pp=cbvectors+filterno*lMem-lTarget-
9026 index[stage]+filterno*base_size;
9027 } else {
9028 createAugmentedVec(
9029 index[stage]-(filterno+1)*base_size+40,
9030 cbvectors+filterno*lMem,aug_vec);
9031 pp=aug_vec;
9032 }
9033 }
9034 }
9036 /* Subtract the best codebook vector, according
9037 to measure, from the target vector */
9039 for (j=0;j<lTarget;j++) {
9040 cvec[j] += gain*(*pp);
9041 target[j] -= gain*(*pp++);
9042 }
9044 /* record quantized gain */
9046 gains[stage]=gain;
9048 }/* end of Main Loop. for (stage=0;... */
9050 /* Gain adjustment for energy matching */
9051 cene=0.0;
9052 for (i=0; i<lTarget; i++) {
9053 cene+=cvec[i]*cvec[i];
9054 }
9055 j=gain_index[0];
9057 for (i=gain_index[0]; i<32; i++) {
9058 ftmp=cene*gain_sq5Tbl[i]*gain_sq5Tbl[i];
9060 if ((ftmp<(tene*gains[0]*gains[0])) &&
9061 (gain_sq5Tbl[j]<(2.0*gains[0]))) {
9062 j=i;
9063 }
9064 }
9065 gain_index[0]=j;
9066 }
9074 Andersen, et al. Experimental [Page 162]
9076 RFC 3951 Internet Low Bit Rate Codec December 2004
9079 A.35. LPCdecode.h
9081 /******************************************************************
9083 iLBC Speech Coder ANSI-C Source Code
9085 LPC_decode.h
9087 Copyright (C) The Internet Society (2004).
9088 All Rights Reserved.
9090 ******************************************************************/
9092 #ifndef __iLBC_LPC_DECODE_H
9093 #define __iLBC_LPC_DECODE_H
9095 void LSFinterpolate2a_dec(
9096 float *a, /* (o) lpc coefficients for a sub-frame */
9097 float *lsf1, /* (i) first lsf coefficient vector */
9098 float *lsf2, /* (i) second lsf coefficient vector */
9099 float coef, /* (i) interpolation weight */
9100 int length /* (i) length of lsf vectors */
9101 );
9103 void SimplelsfDEQ(
9104 float *lsfdeq, /* (o) dequantized lsf coefficients */
9105 int *index, /* (i) quantization index */
9106 int lpc_n /* (i) number of LPCs */
9107 );
9109 void DecoderInterpolateLSF(
9110 float *syntdenum, /* (o) synthesis filter coefficients */
9111 float *weightdenum, /* (o) weighting denumerator
9112 coefficients */
9113 float *lsfdeq, /* (i) dequantized lsf coefficients */
9114 int length, /* (i) length of lsf coefficient vector */
9115 iLBC_Dec_Inst_t *iLBCdec_inst
9116 /* (i) the decoder state structure */
9117 );
9119 #endif
9130 Andersen, et al. Experimental [Page 163]
9132 RFC 3951 Internet Low Bit Rate Codec December 2004
9135 A.36. LPCdecode.c
9137 /******************************************************************
9139 iLBC Speech Coder ANSI-C Source Code
9141 LPC_decode.c
9143 Copyright (C) The Internet Society (2004).
9144 All Rights Reserved.
9146 ******************************************************************/
9148 #include <math.h>
9149 #include <string.h>
9151 #include "helpfun.h"
9152 #include "lsf.h"
9153 #include "iLBC_define.h"
9154 #include "constants.h"
9156 /*---------------------------------------------------------------*
9157 * interpolation of lsf coefficients for the decoder
9158 *--------------------------------------------------------------*/
9160 void LSFinterpolate2a_dec(
9161 float *a, /* (o) lpc coefficients for a sub-frame */
9162 float *lsf1, /* (i) first lsf coefficient vector */
9163 float *lsf2, /* (i) second lsf coefficient vector */
9164 float coef, /* (i) interpolation weight */
9165 int length /* (i) length of lsf vectors */
9166 ){
9167 float lsftmp[LPC_FILTERORDER];
9169 interpolate(lsftmp, lsf1, lsf2, coef, length);
9170 lsf2a(a, lsftmp);
9171 }
9173 /*---------------------------------------------------------------*
9174 * obtain dequantized lsf coefficients from quantization index
9175 *--------------------------------------------------------------*/
9177 void SimplelsfDEQ(
9178 float *lsfdeq, /* (o) dequantized lsf coefficients */
9179 int *index, /* (i) quantization index */
9180 int lpc_n /* (i) number of LPCs */
9181 ){
9182 int i, j, pos, cb_pos;
9186 Andersen, et al. Experimental [Page 164]
9188 RFC 3951 Internet Low Bit Rate Codec December 2004
9191 /* decode first LSF */
9193 pos = 0;
9194 cb_pos = 0;
9195 for (i = 0; i < LSF_NSPLIT; i++) {
9196 for (j = 0; j < dim_lsfCbTbl[i]; j++) {
9197 lsfdeq[pos + j] = lsfCbTbl[cb_pos +
9198 (long)(index[i])*dim_lsfCbTbl[i] + j];
9199 }
9200 pos += dim_lsfCbTbl[i];
9201 cb_pos += size_lsfCbTbl[i]*dim_lsfCbTbl[i];
9202 }
9204 if (lpc_n>1) {
9206 /* decode last LSF */
9208 pos = 0;
9209 cb_pos = 0;
9210 for (i = 0; i < LSF_NSPLIT; i++) {
9211 for (j = 0; j < dim_lsfCbTbl[i]; j++) {
9212 lsfdeq[LPC_FILTERORDER + pos + j] =
9213 lsfCbTbl[cb_pos +
9214 (long)(index[LSF_NSPLIT + i])*
9215 dim_lsfCbTbl[i] + j];
9216 }
9217 pos += dim_lsfCbTbl[i];
9218 cb_pos += size_lsfCbTbl[i]*dim_lsfCbTbl[i];
9219 }
9220 }
9221 }
9223 /*----------------------------------------------------------------*
9224 * obtain synthesis and weighting filters form lsf coefficients
9225 *---------------------------------------------------------------*/
9227 void DecoderInterpolateLSF(
9228 float *syntdenum, /* (o) synthesis filter coefficients */
9229 float *weightdenum, /* (o) weighting denumerator
9230 coefficients */
9231 float *lsfdeq, /* (i) dequantized lsf coefficients */
9232 int length, /* (i) length of lsf coefficient vector */
9233 iLBC_Dec_Inst_t *iLBCdec_inst
9234 /* (i) the decoder state structure */
9235 ){
9236 int i, pos, lp_length;
9237 float lp[LPC_FILTERORDER + 1], *lsfdeq2;
9242 Andersen, et al. Experimental [Page 165]
9244 RFC 3951 Internet Low Bit Rate Codec December 2004
9247 lsfdeq2 = lsfdeq + length;
9248 lp_length = length + 1;
9250 if (iLBCdec_inst->mode==30) {
9251 /* sub-frame 1: Interpolation between old and first */
9253 LSFinterpolate2a_dec(lp, iLBCdec_inst->lsfdeqold, lsfdeq,
9254 lsf_weightTbl_30ms[0], length);
9255 memcpy(syntdenum,lp,lp_length*sizeof(float));
9256 bwexpand(weightdenum, lp, LPC_CHIRP_WEIGHTDENUM,
9257 lp_length);
9259 /* sub-frames 2 to 6: interpolation between first
9260 and last LSF */
9262 pos = lp_length;
9263 for (i = 1; i < 6; i++) {
9264 LSFinterpolate2a_dec(lp, lsfdeq, lsfdeq2,
9265 lsf_weightTbl_30ms[i], length);
9266 memcpy(syntdenum + pos,lp,lp_length*sizeof(float));
9267 bwexpand(weightdenum + pos, lp,
9268 LPC_CHIRP_WEIGHTDENUM, lp_length);
9269 pos += lp_length;
9270 }
9271 }
9272 else {
9273 pos = 0;
9274 for (i = 0; i < iLBCdec_inst->nsub; i++) {
9275 LSFinterpolate2a_dec(lp, iLBCdec_inst->lsfdeqold,
9276 lsfdeq, lsf_weightTbl_20ms[i], length);
9277 memcpy(syntdenum+pos,lp,lp_length*sizeof(float));
9278 bwexpand(weightdenum+pos, lp, LPC_CHIRP_WEIGHTDENUM,
9279 lp_length);
9280 pos += lp_length;
9281 }
9282 }
9284 /* update memory */
9286 if (iLBCdec_inst->mode==30)
9287 memcpy(iLBCdec_inst->lsfdeqold, lsfdeq2,
9288 length*sizeof(float));
9289 else
9290 memcpy(iLBCdec_inst->lsfdeqold, lsfdeq,
9291 length*sizeof(float));
9293 }
9298 Andersen, et al. Experimental [Page 166]
9300 RFC 3951 Internet Low Bit Rate Codec December 2004
9303 A.37. LPCencode.h
9305 /******************************************************************
9307 iLBC Speech Coder ANSI-C Source Code
9309 LPCencode.h
9311 Copyright (C) The Internet Society (2004).
9312 All Rights Reserved.
9314 ******************************************************************/
9316 #ifndef __iLBC_LPCENCOD_H
9317 #define __iLBC_LPCENCOD_H
9319 void LPCencode(
9320 float *syntdenum, /* (i/o) synthesis filter coefficients
9321 before/after encoding */
9322 float *weightdenum, /* (i/o) weighting denumerator coefficients
9323 before/after encoding */
9324 int *lsf_index, /* (o) lsf quantization index */
9325 float *data, /* (i) lsf coefficients to quantize */
9326 iLBC_Enc_Inst_t *iLBCenc_inst
9327 /* (i/o) the encoder state structure */
9328 );
9330 #endif
9332 A.38. LPCencode.c
9334 /******************************************************************
9336 iLBC Speech Coder ANSI-C Source Code
9338 LPCencode.c
9340 Copyright (C) The Internet Society (2004).
9341 All Rights Reserved.
9343 ******************************************************************/
9345 #include <string.h>
9347 #include "iLBC_define.h"
9348 #include "helpfun.h"
9349 #include "lsf.h"
9350 #include "constants.h"
9354 Andersen, et al. Experimental [Page 167]
9356 RFC 3951 Internet Low Bit Rate Codec December 2004
9359 /*----------------------------------------------------------------*
9360 * lpc analysis (subrutine to LPCencode)
9361 *---------------------------------------------------------------*/
9363 void SimpleAnalysis(
9364 float *lsf, /* (o) lsf coefficients */
9365 float *data, /* (i) new data vector */
9366 iLBC_Enc_Inst_t *iLBCenc_inst
9367 /* (i/o) the encoder state structure */
9368 ){
9369 int k, is;
9370 float temp[BLOCKL_MAX], lp[LPC_FILTERORDER + 1];
9371 float lp2[LPC_FILTERORDER + 1];
9372 float r[LPC_FILTERORDER + 1];
9374 is=LPC_LOOKBACK+BLOCKL_MAX-iLBCenc_inst->blockl;
9375 memcpy(iLBCenc_inst->lpc_buffer+is,data,
9376 iLBCenc_inst->blockl*sizeof(float));
9378 /* No lookahead, last window is asymmetric */
9380 for (k = 0; k < iLBCenc_inst->lpc_n; k++) {
9382 is = LPC_LOOKBACK;
9384 if (k < (iLBCenc_inst->lpc_n - 1)) {
9385 window(temp, lpc_winTbl,
9386 iLBCenc_inst->lpc_buffer, BLOCKL_MAX);
9387 } else {
9388 window(temp, lpc_asymwinTbl,
9389 iLBCenc_inst->lpc_buffer + is, BLOCKL_MAX);
9390 }
9392 autocorr(r, temp, BLOCKL_MAX, LPC_FILTERORDER);
9393 window(r, r, lpc_lagwinTbl, LPC_FILTERORDER + 1);
9395 levdurb(lp, temp, r, LPC_FILTERORDER);
9396 bwexpand(lp2, lp, LPC_CHIRP_SYNTDENUM, LPC_FILTERORDER+1);
9398 a2lsf(lsf + k*LPC_FILTERORDER, lp2);
9399 }
9400 is=LPC_LOOKBACK+BLOCKL_MAX-iLBCenc_inst->blockl;
9401 memmove(iLBCenc_inst->lpc_buffer,
9402 iLBCenc_inst->lpc_buffer+LPC_LOOKBACK+BLOCKL_MAX-is,
9403 is*sizeof(float));
9404 }
9406 /*----------------------------------------------------------------*
9410 Andersen, et al. Experimental [Page 168]
9412 RFC 3951 Internet Low Bit Rate Codec December 2004
9415 * lsf interpolator and conversion from lsf to a coefficients
9416 * (subrutine to SimpleInterpolateLSF)
9417 *---------------------------------------------------------------*/
9419 void LSFinterpolate2a_enc(
9420 float *a, /* (o) lpc coefficients */
9421 float *lsf1,/* (i) first set of lsf coefficients */
9422 float *lsf2,/* (i) second set of lsf coefficients */
9423 float coef, /* (i) weighting coefficient to use between
9424 lsf1 and lsf2 */
9425 long length /* (i) length of coefficient vectors */
9426 ){
9427 float lsftmp[LPC_FILTERORDER];
9429 interpolate(lsftmp, lsf1, lsf2, coef, length);
9430 lsf2a(a, lsftmp);
9431 }
9433 /*----------------------------------------------------------------*
9434 * lsf interpolator (subrutine to LPCencode)
9435 *---------------------------------------------------------------*/
9437 void SimpleInterpolateLSF(
9438 float *syntdenum, /* (o) the synthesis filter denominator
9439 resulting from the quantized
9440 interpolated lsf */
9441 float *weightdenum, /* (o) the weighting filter denominator
9442 resulting from the unquantized
9443 interpolated lsf */
9444 float *lsf, /* (i) the unquantized lsf coefficients */
9445 float *lsfdeq, /* (i) the dequantized lsf coefficients */
9446 float *lsfold, /* (i) the unquantized lsf coefficients of
9447 the previous signal frame */
9448 float *lsfdeqold, /* (i) the dequantized lsf coefficients of
9449 the previous signal frame */
9450 int length, /* (i) should equate LPC_FILTERORDER */
9451 iLBC_Enc_Inst_t *iLBCenc_inst
9452 /* (i/o) the encoder state structure */
9453 ){
9454 int i, pos, lp_length;
9455 float lp[LPC_FILTERORDER + 1], *lsf2, *lsfdeq2;
9457 lsf2 = lsf + length;
9458 lsfdeq2 = lsfdeq + length;
9459 lp_length = length + 1;
9461 if (iLBCenc_inst->mode==30) {
9462 /* sub-frame 1: Interpolation between old and first
9466 Andersen, et al. Experimental [Page 169]
9468 RFC 3951 Internet Low Bit Rate Codec December 2004
9471 set of lsf coefficients */
9473 LSFinterpolate2a_enc(lp, lsfdeqold, lsfdeq,
9474 lsf_weightTbl_30ms[0], length);
9475 memcpy(syntdenum,lp,lp_length*sizeof(float));
9476 LSFinterpolate2a_enc(lp, lsfold, lsf,
9477 lsf_weightTbl_30ms[0], length);
9478 bwexpand(weightdenum, lp, LPC_CHIRP_WEIGHTDENUM, lp_length);
9480 /* sub-frame 2 to 6: Interpolation between first
9481 and second set of lsf coefficients */
9483 pos = lp_length;
9484 for (i = 1; i < iLBCenc_inst->nsub; i++) {
9485 LSFinterpolate2a_enc(lp, lsfdeq, lsfdeq2,
9486 lsf_weightTbl_30ms[i], length);
9487 memcpy(syntdenum + pos,lp,lp_length*sizeof(float));
9489 LSFinterpolate2a_enc(lp, lsf, lsf2,
9490 lsf_weightTbl_30ms[i], length);
9491 bwexpand(weightdenum + pos, lp,
9492 LPC_CHIRP_WEIGHTDENUM, lp_length);
9493 pos += lp_length;
9494 }
9495 }
9496 else {
9497 pos = 0;
9498 for (i = 0; i < iLBCenc_inst->nsub; i++) {
9499 LSFinterpolate2a_enc(lp, lsfdeqold, lsfdeq,
9500 lsf_weightTbl_20ms[i], length);
9501 memcpy(syntdenum+pos,lp,lp_length*sizeof(float));
9502 LSFinterpolate2a_enc(lp, lsfold, lsf,
9503 lsf_weightTbl_20ms[i], length);
9504 bwexpand(weightdenum+pos, lp,
9505 LPC_CHIRP_WEIGHTDENUM, lp_length);
9506 pos += lp_length;
9507 }
9508 }
9510 /* update memory */
9512 if (iLBCenc_inst->mode==30) {
9513 memcpy(lsfold, lsf2, length*sizeof(float));
9514 memcpy(lsfdeqold, lsfdeq2, length*sizeof(float));
9515 }
9516 else {
9517 memcpy(lsfold, lsf, length*sizeof(float));
9518 memcpy(lsfdeqold, lsfdeq, length*sizeof(float));
9522 Andersen, et al. Experimental [Page 170]
9524 RFC 3951 Internet Low Bit Rate Codec December 2004
9527 }
9528 }
9530 /*----------------------------------------------------------------*
9531 * lsf quantizer (subrutine to LPCencode)
9532 *---------------------------------------------------------------*/
9534 void SimplelsfQ(
9535 float *lsfdeq, /* (o) dequantized lsf coefficients
9536 (dimension FILTERORDER) */
9537 int *index, /* (o) quantization index */
9538 float *lsf, /* (i) the lsf coefficient vector to be
9539 quantized (dimension FILTERORDER ) */
9540 int lpc_n /* (i) number of lsf sets to quantize */
9541 ){
9542 /* Quantize first LSF with memoryless split VQ */
9543 SplitVQ(lsfdeq, index, lsf, lsfCbTbl, LSF_NSPLIT,
9544 dim_lsfCbTbl, size_lsfCbTbl);
9546 if (lpc_n==2) {
9547 /* Quantize second LSF with memoryless split VQ */
9548 SplitVQ(lsfdeq + LPC_FILTERORDER, index + LSF_NSPLIT,
9549 lsf + LPC_FILTERORDER, lsfCbTbl, LSF_NSPLIT,
9550 dim_lsfCbTbl, size_lsfCbTbl);
9551 }
9552 }
9554 /*----------------------------------------------------------------*
9555 * lpc encoder
9556 *---------------------------------------------------------------*/
9558 void LPCencode(
9559 float *syntdenum, /* (i/o) synthesis filter coefficients
9560 before/after encoding */
9561 float *weightdenum, /* (i/o) weighting denumerator
9562 coefficients before/after
9563 encoding */
9564 int *lsf_index, /* (o) lsf quantization index */
9565 float *data, /* (i) lsf coefficients to quantize */
9566 iLBC_Enc_Inst_t *iLBCenc_inst
9567 /* (i/o) the encoder state structure */
9568 ){
9569 float lsf[LPC_FILTERORDER * LPC_N_MAX];
9570 float lsfdeq[LPC_FILTERORDER * LPC_N_MAX];
9571 int change=0;
9573 SimpleAnalysis(lsf, data, iLBCenc_inst);
9574 SimplelsfQ(lsfdeq, lsf_index, lsf, iLBCenc_inst->lpc_n);
9578 Andersen, et al. Experimental [Page 171]
9580 RFC 3951 Internet Low Bit Rate Codec December 2004
9583 change=LSF_check(lsfdeq, LPC_FILTERORDER, iLBCenc_inst->lpc_n);
9584 SimpleInterpolateLSF(syntdenum, weightdenum,
9585 lsf, lsfdeq, iLBCenc_inst->lsfold,
9586 iLBCenc_inst->lsfdeqold, LPC_FILTERORDER, iLBCenc_inst);
9587 }
9589 A.39. lsf.h
9591 /******************************************************************
9593 iLBC Speech Coder ANSI-C Source Code
9595 lsf.h
9597 Copyright (C) The Internet Society (2004).
9598 All Rights Reserved.
9600 ******************************************************************/
9602 #ifndef __iLBC_LSF_H
9603 #define __iLBC_LSF_H
9605 void a2lsf(
9606 float *freq,/* (o) lsf coefficients */
9607 float *a /* (i) lpc coefficients */
9608 );
9610 void lsf2a(
9611 float *a_coef, /* (o) lpc coefficients */
9612 float *freq /* (i) lsf coefficients */
9613 );
9615 #endif
9617 A.40. lsf.c
9619 /******************************************************************
9621 iLBC Speech Coder ANSI-C Source Code
9623 lsf.c
9625 Copyright (C) The Internet Society (2004).
9626 All Rights Reserved.
9628 ******************************************************************/
9630 #include <string.h>
9634 Andersen, et al. Experimental [Page 172]
9636 RFC 3951 Internet Low Bit Rate Codec December 2004
9639 #include <math.h>
9641 #include "iLBC_define.h"
9643 /*----------------------------------------------------------------*
9644 * conversion from lpc coefficients to lsf coefficients
9645 *---------------------------------------------------------------*/
9647 void a2lsf(
9648 float *freq,/* (o) lsf coefficients */
9649 float *a /* (i) lpc coefficients */
9650 ){
9651 float steps[LSF_NUMBER_OF_STEPS] =
9652 {(float)0.00635, (float)0.003175, (float)0.0015875,
9653 (float)0.00079375};
9654 float step;
9655 int step_idx;
9656 int lsp_index;
9657 float p[LPC_HALFORDER];
9658 float q[LPC_HALFORDER];
9659 float p_pre[LPC_HALFORDER];
9660 float q_pre[LPC_HALFORDER];
9661 float old_p, old_q, *old;
9662 float *pq_coef;
9663 float omega, old_omega;
9664 int i;
9665 float hlp, hlp1, hlp2, hlp3, hlp4, hlp5;
9667 for (i=0; i<LPC_HALFORDER; i++) {
9668 p[i] = (float)-1.0 * (a[i + 1] + a[LPC_FILTERORDER - i]);
9669 q[i] = a[LPC_FILTERORDER - i] - a[i + 1];
9670 }
9672 p_pre[0] = (float)-1.0 - p[0];
9673 p_pre[1] = - p_pre[0] - p[1];
9674 p_pre[2] = - p_pre[1] - p[2];
9675 p_pre[3] = - p_pre[2] - p[3];
9676 p_pre[4] = - p_pre[3] - p[4];
9677 p_pre[4] = p_pre[4] / 2;
9679 q_pre[0] = (float)1.0 - q[0];
9680 q_pre[1] = q_pre[0] - q[1];
9681 q_pre[2] = q_pre[1] - q[2];
9682 q_pre[3] = q_pre[2] - q[3];
9683 q_pre[4] = q_pre[3] - q[4];
9684 q_pre[4] = q_pre[4] / 2;
9686 omega = 0.0;
9690 Andersen, et al. Experimental [Page 173]
9692 RFC 3951 Internet Low Bit Rate Codec December 2004
9695 old_omega = 0.0;
9697 old_p = FLOAT_MAX;
9698 old_q = FLOAT_MAX;
9700 /* Here we loop through lsp_index to find all the
9701 LPC_FILTERORDER roots for omega. */
9703 for (lsp_index = 0; lsp_index<LPC_FILTERORDER; lsp_index++) {
9705 /* Depending on lsp_index being even or odd, we
9706 alternatively solve the roots for the two LSP equations. */
9709 if ((lsp_index & 0x1) == 0) {
9710 pq_coef = p_pre;
9711 old = &old_p;
9712 } else {
9713 pq_coef = q_pre;
9714 old = &old_q;
9715 }
9717 /* Start with low resolution grid */
9719 for (step_idx = 0, step = steps[step_idx];
9720 step_idx < LSF_NUMBER_OF_STEPS;){
9722 /* cos(10piw) + pq(0)cos(8piw) + pq(1)cos(6piw) +
9723 pq(2)cos(4piw) + pq(3)cod(2piw) + pq(4) */
9725 hlp = (float)cos(omega * TWO_PI);
9726 hlp1 = (float)2.0 * hlp + pq_coef[0];
9727 hlp2 = (float)2.0 * hlp * hlp1 - (float)1.0 +
9728 pq_coef[1];
9729 hlp3 = (float)2.0 * hlp * hlp2 - hlp1 + pq_coef[2];
9730 hlp4 = (float)2.0 * hlp * hlp3 - hlp2 + pq_coef[3];
9731 hlp5 = hlp * hlp4 - hlp3 + pq_coef[4];
9734 if (((hlp5 * (*old)) <= 0.0) || (omega >= 0.5)){
9736 if (step_idx == (LSF_NUMBER_OF_STEPS - 1)){
9738 if (fabs(hlp5) >= fabs(*old)) {
9739 freq[lsp_index] = omega - step;
9740 } else {
9741 freq[lsp_index] = omega;
9742 }
9746 Andersen, et al. Experimental [Page 174]
9748 RFC 3951 Internet Low Bit Rate Codec December 2004
9753 if ((*old) >= 0.0){
9754 *old = (float)-1.0 * FLOAT_MAX;
9755 } else {
9756 *old = FLOAT_MAX;
9757 }
9759 omega = old_omega;
9760 step_idx = 0;
9762 step_idx = LSF_NUMBER_OF_STEPS;
9763 } else {
9765 if (step_idx == 0) {
9766 old_omega = omega;
9767 }
9769 step_idx++;
9770 omega -= steps[step_idx];
9772 /* Go back one grid step */
9774 step = steps[step_idx];
9775 }
9776 } else {
9778 /* increment omega until they are of different sign,
9779 and we know there is at least one root between omega
9780 and old_omega */
9781 *old = hlp5;
9782 omega += step;
9783 }
9784 }
9785 }
9787 for (i = 0; i<LPC_FILTERORDER; i++) {
9788 freq[i] = freq[i] * TWO_PI;
9789 }
9790 }
9792 /*----------------------------------------------------------------*
9793 * conversion from lsf coefficients to lpc coefficients
9794 *---------------------------------------------------------------*/
9796 void lsf2a(
9797 float *a_coef, /* (o) lpc coefficients */
9798 float *freq /* (i) lsf coefficients */
9802 Andersen, et al. Experimental [Page 175]
9804 RFC 3951 Internet Low Bit Rate Codec December 2004
9807 ){
9808 int i, j;
9809 float hlp;
9810 float p[LPC_HALFORDER], q[LPC_HALFORDER];
9811 float a[LPC_HALFORDER + 1], a1[LPC_HALFORDER],
9812 a2[LPC_HALFORDER];
9813 float b[LPC_HALFORDER + 1], b1[LPC_HALFORDER],
9814 b2[LPC_HALFORDER];
9816 for (i=0; i<LPC_FILTERORDER; i++) {
9817 freq[i] = freq[i] * PI2;
9818 }
9820 /* Check input for ill-conditioned cases. This part is not
9821 found in the TIA standard. It involves the following 2 IF
9822 blocks. If "freq" is judged ill-conditioned, then we first
9823 modify freq[0] and freq[LPC_HALFORDER-1] (normally
9824 LPC_HALFORDER = 10 for LPC applications), then we adjust
9825 the other "freq" values slightly */
9828 if ((freq[0] <= 0.0) || (freq[LPC_FILTERORDER - 1] >= 0.5)){
9831 if (freq[0] <= 0.0) {
9832 freq[0] = (float)0.022;
9833 }
9836 if (freq[LPC_FILTERORDER - 1] >= 0.5) {
9837 freq[LPC_FILTERORDER - 1] = (float)0.499;
9838 }
9840 hlp = (freq[LPC_FILTERORDER - 1] - freq[0]) /
9841 (float) (LPC_FILTERORDER - 1);
9843 for (i=1; i<LPC_FILTERORDER; i++) {
9844 freq[i] = freq[i - 1] + hlp;
9845 }
9846 }
9848 memset(a1, 0, LPC_HALFORDER*sizeof(float));
9849 memset(a2, 0, LPC_HALFORDER*sizeof(float));
9850 memset(b1, 0, LPC_HALFORDER*sizeof(float));
9851 memset(b2, 0, LPC_HALFORDER*sizeof(float));
9852 memset(a, 0, (LPC_HALFORDER+1)*sizeof(float));
9853 memset(b, 0, (LPC_HALFORDER+1)*sizeof(float));
9858 Andersen, et al. Experimental [Page 176]
9860 RFC 3951 Internet Low Bit Rate Codec December 2004
9863 /* p[i] and q[i] compute cos(2*pi*omega_{2j}) and
9864 cos(2*pi*omega_{2j-1} in eqs. 4.2.2.2-1 and 4.2.2.2-2.
9865 Note that for this code p[i] specifies the coefficients
9866 used in .Q_A(z) while q[i] specifies the coefficients used
9867 in .P_A(z) */
9869 for (i=0; i<LPC_HALFORDER; i++) {
9870 p[i] = (float)cos(TWO_PI * freq[2 * i]);
9871 q[i] = (float)cos(TWO_PI * freq[2 * i + 1]);
9872 }
9874 a[0] = 0.25;
9875 b[0] = 0.25;
9877 for (i= 0; i<LPC_HALFORDER; i++) {
9878 a[i + 1] = a[i] - 2 * p[i] * a1[i] + a2[i];
9879 b[i + 1] = b[i] - 2 * q[i] * b1[i] + b2[i];
9880 a2[i] = a1[i];
9881 a1[i] = a[i];
9882 b2[i] = b1[i];
9883 b1[i] = b[i];
9884 }
9886 for (j=0; j<LPC_FILTERORDER; j++) {
9888 if (j == 0) {
9889 a[0] = 0.25;
9890 b[0] = -0.25;
9891 } else {
9892 a[0] = b[0] = 0.0;
9893 }
9895 for (i=0; i<LPC_HALFORDER; i++) {
9896 a[i + 1] = a[i] - 2 * p[i] * a1[i] + a2[i];
9897 b[i + 1] = b[i] - 2 * q[i] * b1[i] + b2[i];
9898 a2[i] = a1[i];
9899 a1[i] = a[i];
9900 b2[i] = b1[i];
9901 b1[i] = b[i];
9902 }
9904 a_coef[j + 1] = 2 * (a[LPC_HALFORDER] + b[LPC_HALFORDER]);
9905 }
9907 a_coef[0] = 1.0;
9908 }
9914 Andersen, et al. Experimental [Page 177]
9916 RFC 3951 Internet Low Bit Rate Codec December 2004
9919 A.41. packing.h
9921 /******************************************************************
9923 iLBC Speech Coder ANSI-C Source Code
9925 packing.h
9927 Copyright (C) The Internet Society (2004).
9928 All Rights Reserved.
9930 ******************************************************************/
9932 #ifndef __PACKING_H
9933 #define __PACKING_H
9935 void packsplit(
9936 int *index, /* (i) the value to split */
9937 int *firstpart, /* (o) the value specified by most
9938 significant bits */
9939 int *rest, /* (o) the value specified by least
9940 significant bits */
9941 int bitno_firstpart, /* (i) number of bits in most
9942 significant part */
9943 int bitno_total /* (i) number of bits in full range
9944 of value */
9945 );
9947 void packcombine(
9948 int *index, /* (i/o) the msb value in the
9949 combined value out */
9950 int rest, /* (i) the lsb value */
9951 int bitno_rest /* (i) the number of bits in the
9952 lsb part */
9953 );
9955 void dopack(
9956 unsigned char **bitstream, /* (i/o) on entrance pointer to
9957 place in bitstream to pack
9958 new data, on exit pointer
9959 to place in bitstream to
9960 pack future data */
9961 int index, /* (i) the value to pack */
9962 int bitno, /* (i) the number of bits that the
9963 value will fit within */
9964 int *pos /* (i/o) write position in the
9965 current byte */
9966 );
9970 Andersen, et al. Experimental [Page 178]
9972 RFC 3951 Internet Low Bit Rate Codec December 2004
9975 void unpack(
9976 unsigned char **bitstream, /* (i/o) on entrance pointer to
9977 place in bitstream to
9978 unpack new data from, on
9979 exit pointer to place in
9980 bitstream to unpack future
9981 data from */
9982 int *index, /* (o) resulting value */
9983 int bitno, /* (i) number of bits used to
9984 represent the value */
9985 int *pos /* (i/o) read position in the
9986 current byte */
9987 );
9989 #endif
9991 A.42. packing.c
9993 /******************************************************************
9995 iLBC Speech Coder ANSI-C Source Code
9997 packing.c
9999 Copyright (C) The Internet Society (2004).
10000 All Rights Reserved.
10002 ******************************************************************/
10004 #include <math.h>
10005 #include <stdlib.h>
10007 #include "iLBC_define.h"
10008 #include "constants.h"
10009 #include "helpfun.h"
10010 #include "string.h"
10012 /*----------------------------------------------------------------*
10013 * splitting an integer into first most significant bits and
10014 * remaining least significant bits
10015 *---------------------------------------------------------------*/
10017 void packsplit(
10018 int *index, /* (i) the value to split */
10019 int *firstpart, /* (o) the value specified by most
10020 significant bits */
10021 int *rest, /* (o) the value specified by least
10022 significant bits */
10026 Andersen, et al. Experimental [Page 179]
10028 RFC 3951 Internet Low Bit Rate Codec December 2004
10031 int bitno_firstpart, /* (i) number of bits in most
10032 significant part */
10033 int bitno_total /* (i) number of bits in full range
10034 of value */
10035 ){
10036 int bitno_rest = bitno_total-bitno_firstpart;
10038 *firstpart = *index>>(bitno_rest);
10039 *rest = *index-(*firstpart<<(bitno_rest));
10040 }
10042 /*----------------------------------------------------------------*
10043 * combining a value corresponding to msb's with a value
10044 * corresponding to lsb's
10045 *---------------------------------------------------------------*/
10047 void packcombine(
10048 int *index, /* (i/o) the msb value in the
10049 combined value out */
10050 int rest, /* (i) the lsb value */
10051 int bitno_rest /* (i) the number of bits in the
10052 lsb part */
10053 ){
10054 *index = *index<<bitno_rest;
10055 *index += rest;
10056 }
10058 /*----------------------------------------------------------------*
10059 * packing of bits into bitstream, i.e., vector of bytes
10060 *---------------------------------------------------------------*/
10062 void dopack(
10063 unsigned char **bitstream, /* (i/o) on entrance pointer to
10064 place in bitstream to pack
10065 new data, on exit pointer
10066 to place in bitstream to
10067 pack future data */
10068 int index, /* (i) the value to pack */
10069 int bitno, /* (i) the number of bits that the
10070 value will fit within */
10071 int *pos /* (i/o) write position in the
10072 current byte */
10073 ){
10074 int posLeft;
10076 /* Clear the bits before starting in a new byte */
10078 if ((*pos)==0) {
10082 Andersen, et al. Experimental [Page 180]
10084 RFC 3951 Internet Low Bit Rate Codec December 2004
10087 **bitstream=0;
10088 }
10090 while (bitno>0) {
10092 /* Jump to the next byte if end of this byte is reached*/
10094 if (*pos==8) {
10095 *pos=0;
10096 (*bitstream)++;
10097 **bitstream=0;
10098 }
10100 posLeft=8-(*pos);
10102 /* Insert index into the bitstream */
10104 if (bitno <= posLeft) {
10105 **bitstream |= (unsigned char)(index<<(posLeft-bitno));
10106 *pos+=bitno;
10107 bitno=0;
10108 } else {
10109 **bitstream |= (unsigned char)(index>>(bitno-posLeft));
10111 *pos=8;
10112 index-=((index>>(bitno-posLeft))<<(bitno-posLeft));
10114 bitno-=posLeft;
10115 }
10116 }
10117 }
10119 /*----------------------------------------------------------------*
10120 * unpacking of bits from bitstream, i.e., vector of bytes
10121 *---------------------------------------------------------------*/
10123 void unpack(
10124 unsigned char **bitstream, /* (i/o) on entrance pointer to
10125 place in bitstream to
10126 unpack new data from, on
10127 exit pointer to place in
10128 bitstream to unpack future
10129 data from */
10130 int *index, /* (o) resulting value */
10131 int bitno, /* (i) number of bits used to
10132 represent the value */
10133 int *pos /* (i/o) read position in the
10134 current byte */
10138 Andersen, et al. Experimental [Page 181]
10140 RFC 3951 Internet Low Bit Rate Codec December 2004
10143 ){
10144 int BitsLeft;
10146 *index=0;
10148 while (bitno>0) {
10150 /* move forward in bitstream when the end of the
10151 byte is reached */
10153 if (*pos==8) {
10154 *pos=0;
10155 (*bitstream)++;
10156 }
10158 BitsLeft=8-(*pos);
10160 /* Extract bits to index */
10162 if (BitsLeft>=bitno) {
10163 *index+=((((**bitstream)<<(*pos)) & 0xFF)>>(8-bitno));
10165 *pos+=bitno;
10166 bitno=0;
10167 } else {
10169 if ((8-bitno)>0) {
10170 *index+=((((**bitstream)<<(*pos)) & 0xFF)>>
10171 (8-bitno));
10172 *pos=8;
10173 } else {
10174 *index+=(((int)(((**bitstream)<<(*pos)) & 0xFF))<<
10175 (bitno-8));
10176 *pos=8;
10177 }
10178 bitno-=BitsLeft;
10179 }
10180 }
10181 }
10183 A.43. StateConstructW.h
10185 /******************************************************************
10187 iLBC Speech Coder ANSI-C Source Code
10189 StateConstructW.h
10194 Andersen, et al. Experimental [Page 182]
10196 RFC 3951 Internet Low Bit Rate Codec December 2004
10199 Copyright (C) The Internet Society (2004).
10200 All Rights Reserved.
10202 ******************************************************************/
10204 #ifndef __iLBC_STATECONSTRUCTW_H
10205 #define __iLBC_STATECONSTRUCTW_H
10207 void StateConstructW(
10208 int idxForMax, /* (i) 6-bit index for the quantization of
10209 max amplitude */
10210 int *idxVec, /* (i) vector of quantization indexes */
10211 float *syntDenum, /* (i) synthesis filter denumerator */
10212 float *out, /* (o) the decoded state vector */
10213 int len /* (i) length of a state vector */
10214 );
10216 #endif
10218 A.44. StateConstructW.c
10220 /******************************************************************
10222 iLBC Speech Coder ANSI-C Source Code
10224 StateConstructW.c
10226 Copyright (C) The Internet Society (2004).
10227 All Rights Reserved.
10229 ******************************************************************/
10231 #include <math.h>
10232 #include <string.h>
10234 #include "iLBC_define.h"
10235 #include "constants.h"
10236 #include "filter.h"
10238 /*----------------------------------------------------------------*
10239 * decoding of the start state
10240 *---------------------------------------------------------------*/
10242 void StateConstructW(
10243 int idxForMax, /* (i) 6-bit index for the quantization of
10244 max amplitude */
10245 int *idxVec, /* (i) vector of quantization indexes */
10246 float *syntDenum, /* (i) synthesis filter denumerator */
10250 Andersen, et al. Experimental [Page 183]
10252 RFC 3951 Internet Low Bit Rate Codec December 2004
10255 float *out, /* (o) the decoded state vector */
10256 int len /* (i) length of a state vector */
10257 ){
10258 float maxVal, tmpbuf[LPC_FILTERORDER+2*STATE_LEN], *tmp,
10259 numerator[LPC_FILTERORDER+1];
10260 float foutbuf[LPC_FILTERORDER+2*STATE_LEN], *fout;
10261 int k,tmpi;
10263 /* decoding of the maximum value */
10265 maxVal = state_frgqTbl[idxForMax];
10266 maxVal = (float)pow(10,maxVal)/(float)4.5;
10268 /* initialization of buffers and coefficients */
10270 memset(tmpbuf, 0, LPC_FILTERORDER*sizeof(float));
10271 memset(foutbuf, 0, LPC_FILTERORDER*sizeof(float));
10272 for (k=0; k<LPC_FILTERORDER; k++) {
10273 numerator[k]=syntDenum[LPC_FILTERORDER-k];
10274 }
10275 numerator[LPC_FILTERORDER]=syntDenum[0];
10276 tmp = &tmpbuf[LPC_FILTERORDER];
10277 fout = &foutbuf[LPC_FILTERORDER];
10279 /* decoding of the sample values */
10281 for (k=0; k<len; k++) {
10282 tmpi = len-1-k;
10283 /* maxVal = 1/scal */
10284 tmp[k] = maxVal*state_sq3Tbl[idxVec[tmpi]];
10285 }
10287 /* circular convolution with all-pass filter */
10289 memset(tmp+len, 0, len*sizeof(float));
10290 ZeroPoleFilter(tmp, numerator, syntDenum, 2*len,
10291 LPC_FILTERORDER, fout);
10292 for (k=0;k<len;k++) {
10293 out[k] = fout[len-1-k]+fout[2*len-1-k];
10294 }
10295 }
10306 Andersen, et al. Experimental [Page 184]
10308 RFC 3951 Internet Low Bit Rate Codec December 2004
10311 A.45. StateSearchW.h
10313 /******************************************************************
10315 iLBC Speech Coder ANSI-C Source Code
10317 StateSearchW.h
10319 Copyright (C) The Internet Society (2004).
10320 All Rights Reserved.
10322 ******************************************************************/
10324 #ifndef __iLBC_STATESEARCHW_H
10325 #define __iLBC_STATESEARCHW_H
10327 void AbsQuantW(
10328 iLBC_Enc_Inst_t *iLBCenc_inst,
10329 /* (i) Encoder instance */
10330 float *in, /* (i) vector to encode */
10331 float *syntDenum, /* (i) denominator of synthesis filter */
10332 float *weightDenum, /* (i) denominator of weighting filter */
10333 int *out, /* (o) vector of quantizer indexes */
10334 int len, /* (i) length of vector to encode and
10335 vector of quantizer indexes */
10336 int state_first /* (i) position of start state in the
10337 80 vec */
10338 );
10340 void StateSearchW(
10341 iLBC_Enc_Inst_t *iLBCenc_inst,
10342 /* (i) Encoder instance */
10343 float *residual,/* (i) target residual vector */
10344 float *syntDenum, /* (i) lpc synthesis filter */
10345 float *weightDenum, /* (i) weighting filter denuminator */
10346 int *idxForMax, /* (o) quantizer index for maximum
10347 amplitude */
10348 int *idxVec, /* (o) vector of quantization indexes */
10349 int len, /* (i) length of all vectors */
10350 int state_first /* (i) position of start state in the
10351 80 vec */
10352 );
10355 #endif
10362 Andersen, et al. Experimental [Page 185]
10364 RFC 3951 Internet Low Bit Rate Codec December 2004
10367 A.46. StateSearchW.c
10369 /******************************************************************
10371 iLBC Speech Coder ANSI-C Source Code
10373 StateSearchW.c
10375 Copyright (C) The Internet Society (2004).
10376 All Rights Reserved.
10378 ******************************************************************/
10380 #include <math.h>
10381 #include <string.h>
10383 #include "iLBC_define.h"
10384 #include "constants.h"
10385 #include "filter.h"
10386 #include "helpfun.h"
10388 /*----------------------------------------------------------------*
10389 * predictive noise shaping encoding of scaled start state
10390 * (subrutine for StateSearchW)
10391 *---------------------------------------------------------------*/
10393 void AbsQuantW(
10394 iLBC_Enc_Inst_t *iLBCenc_inst,
10395 /* (i) Encoder instance */
10396 float *in, /* (i) vector to encode */
10397 float *syntDenum, /* (i) denominator of synthesis filter */
10398 float *weightDenum, /* (i) denominator of weighting filter */
10399 int *out, /* (o) vector of quantizer indexes */
10400 int len, /* (i) length of vector to encode and
10401 vector of quantizer indexes */
10402 int state_first /* (i) position of start state in the
10403 80 vec */
10404 ){
10405 float *syntOut;
10406 float syntOutBuf[LPC_FILTERORDER+STATE_SHORT_LEN_30MS];
10407 float toQ, xq;
10408 int n;
10409 int index;
10411 /* initialization of buffer for filtering */
10413 memset(syntOutBuf, 0, LPC_FILTERORDER*sizeof(float));
10418 Andersen, et al. Experimental [Page 186]
10420 RFC 3951 Internet Low Bit Rate Codec December 2004
10423 /* initialization of pointer for filtering */
10425 syntOut = &syntOutBuf[LPC_FILTERORDER];
10427 /* synthesis and weighting filters on input */
10429 if (state_first) {
10430 AllPoleFilter (in, weightDenum, SUBL, LPC_FILTERORDER);
10431 } else {
10432 AllPoleFilter (in, weightDenum,
10433 iLBCenc_inst->state_short_len-SUBL,
10434 LPC_FILTERORDER);
10435 }
10437 /* encoding loop */
10439 for (n=0; n<len; n++) {
10441 /* time update of filter coefficients */
10443 if ((state_first)&&(n==SUBL)){
10444 syntDenum += (LPC_FILTERORDER+1);
10445 weightDenum += (LPC_FILTERORDER+1);
10447 /* synthesis and weighting filters on input */
10448 AllPoleFilter (&in[n], weightDenum, len-n,
10449 LPC_FILTERORDER);
10451 } else if ((state_first==0)&&
10452 (n==(iLBCenc_inst->state_short_len-SUBL))) {
10453 syntDenum += (LPC_FILTERORDER+1);
10454 weightDenum += (LPC_FILTERORDER+1);
10456 /* synthesis and weighting filters on input */
10457 AllPoleFilter (&in[n], weightDenum, len-n,
10458 LPC_FILTERORDER);
10460 }
10462 /* prediction of synthesized and weighted input */
10464 syntOut[n] = 0.0;
10465 AllPoleFilter (&syntOut[n], weightDenum, 1,
10466 LPC_FILTERORDER);
10468 /* quantization */
10470 toQ = in[n]-syntOut[n];
10474 Andersen, et al. Experimental [Page 187]
10476 RFC 3951 Internet Low Bit Rate Codec December 2004
10479 sort_sq(&xq, &index, toQ, state_sq3Tbl, 8);
10480 out[n]=index;
10481 syntOut[n] = state_sq3Tbl[out[n]];
10483 /* update of the prediction filter */
10485 AllPoleFilter(&syntOut[n], weightDenum, 1,
10486 LPC_FILTERORDER);
10487 }
10488 }
10490 /*----------------------------------------------------------------*
10491 * encoding of start state
10492 *---------------------------------------------------------------*/
10494 void StateSearchW(
10495 iLBC_Enc_Inst_t *iLBCenc_inst,
10496 /* (i) Encoder instance */
10497 float *residual,/* (i) target residual vector */
10498 float *syntDenum, /* (i) lpc synthesis filter */
10499 float *weightDenum, /* (i) weighting filter denuminator */
10500 int *idxForMax, /* (o) quantizer index for maximum
10501 amplitude */
10502 int *idxVec, /* (o) vector of quantization indexes */
10503 int len, /* (i) length of all vectors */
10504 int state_first /* (i) position of start state in the
10505 80 vec */
10506 ){
10507 float dtmp, maxVal;
10508 float tmpbuf[LPC_FILTERORDER+2*STATE_SHORT_LEN_30MS];
10509 float *tmp, numerator[1+LPC_FILTERORDER];
10510 float foutbuf[LPC_FILTERORDER+2*STATE_SHORT_LEN_30MS], *fout;
10511 int k;
10512 float qmax, scal;
10514 /* initialization of buffers and filter coefficients */
10516 memset(tmpbuf, 0, LPC_FILTERORDER*sizeof(float));
10517 memset(foutbuf, 0, LPC_FILTERORDER*sizeof(float));
10518 for (k=0; k<LPC_FILTERORDER; k++) {
10519 numerator[k]=syntDenum[LPC_FILTERORDER-k];
10520 }
10521 numerator[LPC_FILTERORDER]=syntDenum[0];
10522 tmp = &tmpbuf[LPC_FILTERORDER];
10523 fout = &foutbuf[LPC_FILTERORDER];
10525 /* circular convolution with the all-pass filter */
10530 Andersen, et al. Experimental [Page 188]
10532 RFC 3951 Internet Low Bit Rate Codec December 2004
10535 memcpy(tmp, residual, len*sizeof(float));
10536 memset(tmp+len, 0, len*sizeof(float));
10537 ZeroPoleFilter(tmp, numerator, syntDenum, 2*len,
10538 LPC_FILTERORDER, fout);
10539 for (k=0; k<len; k++) {
10540 fout[k] += fout[k+len];
10541 }
10543 /* identification of the maximum amplitude value */
10545 maxVal = fout[0];
10546 for (k=1; k<len; k++) {
10548 if (fout[k]*fout[k] > maxVal*maxVal){
10549 maxVal = fout[k];
10550 }
10551 }
10552 maxVal=(float)fabs(maxVal);
10554 /* encoding of the maximum amplitude value */
10556 if (maxVal < 10.0) {
10557 maxVal = 10.0;
10558 }
10559 maxVal = (float)log10(maxVal);
10560 sort_sq(&dtmp, idxForMax, maxVal, state_frgqTbl, 64);
10562 /* decoding of the maximum amplitude representation value,
10563 and corresponding scaling of start state */
10565 maxVal=state_frgqTbl[*idxForMax];
10566 qmax = (float)pow(10,maxVal);
10567 scal = (float)(4.5)/qmax;
10568 for (k=0; k<len; k++){
10569 fout[k] *= scal;
10570 }
10572 /* predictive noise shaping encoding of scaled start state */
10574 AbsQuantW(iLBCenc_inst, fout,syntDenum,
10575 weightDenum,idxVec, len, state_first);
10576 }
10586 Andersen, et al. Experimental [Page 189]
10588 RFC 3951 Internet Low Bit Rate Codec December 2004
10591 A.47. syntFilter.h
10593 /******************************************************************
10595 iLBC Speech Coder ANSI-C Source Code
10597 syntFilter.h
10599 Copyright (C) The Internet Society (2004).
10600 All Rights Reserved.
10602 ******************************************************************/
10604 #ifndef __iLBC_SYNTFILTER_H
10605 #define __iLBC_SYNTFILTER_H
10607 void syntFilter(
10608 float *Out, /* (i/o) Signal to be filtered */
10609 float *a, /* (i) LP parameters */
10610 int len, /* (i) Length of signal */
10611 float *mem /* (i/o) Filter state */
10612 );
10614 #endif
10616 A.48. syntFilter.c
10618 /******************************************************************
10620 iLBC Speech Coder ANSI-C Source Code
10622 syntFilter.c
10624 Copyright (C) The Internet Society (2004).
10625 All Rights Reserved.
10627 ******************************************************************/
10629 #include "iLBC_define.h"
10631 /*----------------------------------------------------------------*
10632 * LP synthesis filter.
10633 *---------------------------------------------------------------*/
10635 void syntFilter(
10636 float *Out, /* (i/o) Signal to be filtered */
10637 float *a, /* (i) LP parameters */
10638 int len, /* (i) Length of signal */
10642 Andersen, et al. Experimental [Page 190]
10644 RFC 3951 Internet Low Bit Rate Codec December 2004
10647 float *mem /* (i/o) Filter state */
10648 ){
10649 int i, j;
10650 float *po, *pi, *pa, *pm;
10652 po=Out;
10654 /* Filter first part using memory from past */
10656 for (i=0; i<LPC_FILTERORDER; i++) {
10657 pi=&Out[i-1];
10658 pa=&a[1];
10659 pm=&mem[LPC_FILTERORDER-1];
10660 for (j=1; j<=i; j++) {
10661 *po-=(*pa++)*(*pi--);
10662 }
10663 for (j=i+1; j<LPC_FILTERORDER+1; j++) {
10664 *po-=(*pa++)*(*pm--);
10665 }
10666 po++;
10667 }
10669 /* Filter last part where the state is entirely in
10670 the output vector */
10672 for (i=LPC_FILTERORDER; i<len; i++) {
10673 pi=&Out[i-1];
10674 pa=&a[1];
10675 for (j=1; j<LPC_FILTERORDER+1; j++) {
10676 *po-=(*pa++)*(*pi--);
10677 }
10678 po++;
10679 }
10681 /* Update state vector */
10683 memcpy(mem, &Out[len-LPC_FILTERORDER],
10684 LPC_FILTERORDER*sizeof(float));
10685 }
10698 Andersen, et al. Experimental [Page 191]
10700 RFC 3951 Internet Low Bit Rate Codec December 2004
10703 Authors' Addresses
10705 Soren Vang Andersen
10706 Department of Communication Technology
10707 Aalborg University
10708 Fredrik Bajers Vej 7A
10709 9200 Aalborg
10710 Denmark
10712 Phone: ++45 9 6358627
10713 EMail: sva@kom.auc.dk
10716 Alan Duric
10717 Telio AS
10718 Stoperigt. 2
10719 Oslo, N-0250
10720 Norway
10722 Phone: +47 21673555
10723 EMail: alan.duric@telio.no
10726 Henrik Astrom
10727 Global IP Sound AB
10728 Olandsgatan 42
10729 Stockholm, S-11663
10730 Sweden
10732 Phone: +46 8 54553040
10733 EMail: henrik.astrom@globalipsound.com
10736 Roar Hagen
10737 Global IP Sound AB
10738 Olandsgatan 42
10739 Stockholm, S-11663
10740 Sweden
10742 Phone: +46 8 54553040
10743 EMail: roar.hagen@globalipsound.com
10754 Andersen, et al. Experimental [Page 192]
10756 RFC 3951 Internet Low Bit Rate Codec December 2004
10759 W. Bastiaan Kleijn
10760 Global IP Sound AB
10761 Olandsgatan 42
10762 Stockholm, S-11663
10763 Sweden
10765 Phone: +46 8 54553040
10766 EMail: bastiaan.kleijn@globalipsound.com
10769 Jan Linden
10770 Global IP Sound Inc.
10771 900 Kearny Street, suite 500
10772 San Francisco, CA-94133
10773 USA
10775 Phone: +1 415 397 2555
10776 EMail: jan.linden@globalipsound.com
10810 Andersen, et al. Experimental [Page 193]
10812 RFC 3951 Internet Low Bit Rate Codec December 2004
10815 Full Copyright Statement
10817 Copyright (C) The Internet Society (2004).
10819 This document is subject to the rights, licenses and restrictions
10820 contained in BCP 78, and except as set forth therein, the authors
10821 retain all their rights.
10823 This document and the information contained herein are provided on an
10824 "AS IS" basis and THE CONTRIBUTOR, THE ORGANIZATION HE/SHE REPRESENTS
10825 OR IS SPONSORED BY (IF ANY), THE INTERNET SOCIETY AND THE INTERNET
10826 ENGINEERING TASK FORCE DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED,
10827 INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE
10828 INFORMATION HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED
10829 WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
10831 Intellectual Property
10833 The IETF takes no position regarding the validity or scope of any
10834 Intellectual Property Rights or other rights that might be claimed to
10835 pertain to the implementation or use of the technology described in
10836 this document or the extent to which any license under such rights
10837 might or might not be available; nor does it represent that it has
10838 made any independent effort to identify any such rights. Information
10839 on the IETF's procedures with respect to rights in IETF Documents can
10840 be found in BCP 78 and BCP 79.
10842 Copies of IPR disclosures made to the IETF Secretariat and any
10843 assurances of licenses to be made available, or the result of an
10844 attempt made to obtain a general license or permission for the use of
10845 such proprietary rights by implementers or users of this
10846 specification can be obtained from the IETF on-line IPR repository at
10847 http://www.ietf.org/ipr.
10849 The IETF invites any interested party to bring to its attention any
10850 copyrights, patents or patent applications, or other proprietary
10851 rights that may cover technology that may be required to implement
10852 this standard. Please address the information to the IETF at ietf-
10853 ipr@ietf.org.
10856 Acknowledgement
10858 Funding for the RFC Editor function is currently provided by the
10859 Internet Society.
10866 Andersen, et al. Experimental [Page 194]