draft-ietf-codec-opus.xml 110 KB
Newer Older
Gregory Maxwell's avatar
Gregory Maxwell committed
1
<?xml version="1.0" encoding="utf-8"?>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
2
3
4
<!DOCTYPE rfc SYSTEM 'rfc2629.dtd'>
<?rfc toc="yes" symrefs="yes" ?>

Jean-Marc Valin's avatar
Jean-Marc Valin committed
5
<rfc ipr="trust200902" category="std" docName="draft-ietf-codec-opus-04">
Jean-Marc Valin's avatar
Jean-Marc Valin committed
6
7

<front>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
8
<title abbrev="Interactive Audio Codec">Definition of the Opus Audio Codec</title>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
9
10
11
12
13
14
15
16
17
18
19
20


<author initials="JM" surname="Valin" fullname="Jean-Marc Valin">
<organization>Octasic Inc.</organization>
<address>
<postal>
<street>4101, Molson Street</street>
<city>Montreal</city>
<region>Quebec</region>
<code></code>
<country>Canada</country>
</postal>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
21
<phone>+1 514 282-8858</phone>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
22
23
24
25
26
<email>jean-marc.valin@octasic.com</email>
</address>
</author>

<author initials="K." surname="Vos" fullname="Koen Vos">
27
<organization>Skype Technologies S.A.</organization>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
28
29
<address>
<postal>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
30
<street>Stadsgarden 6</street>
31
<city>Stockholm</city>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
32
<region></region>
33
34
<code>11645</code>
<country>SE</country>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
35
</postal>
36
<phone>+46 855 921 989</phone>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
37
38
39
40
41
<email>koen.vos@skype.net</email>
</address>
</author>


Jean-Marc Valin's avatar
Jean-Marc Valin committed
42
<date day="9" month="March" year="2011" />
Jean-Marc Valin's avatar
Jean-Marc Valin committed
43
44
45
46
47
48
49

<area>General</area>

<workgroup></workgroup>

<abstract>
<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
50
This document describes the Opus codec, designed for interactive speech and audio 
Jean-Marc Valin's avatar
Jean-Marc Valin committed
51
transmission over the Internet.
Jean-Marc Valin's avatar
Jean-Marc Valin committed
52
53
54
55
56
57
58
59
</t>
</abstract>
</front>

<middle>

<section anchor="introduction" title="Introduction">
<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
60
We propose the Opus codec based on a linear prediction layer (LP) and an
Gregory Maxwell's avatar
Gregory Maxwell committed
61
MDCT-based layer. The main idea behind the proposal is that
Jean-Marc Valin's avatar
Jean-Marc Valin committed
62
the speech low frequencies are usually more efficiently coded using
Jean-Marc Valin's avatar
Jean-Marc Valin committed
63
linear prediction codecs (such as CELP variants), while music and higher speech frequencies
Jean-Marc Valin's avatar
Jean-Marc Valin committed
64
65
are more efficiently coded in the transform domain (e.g. MDCT). For low 
sampling rates, the MDCT layer is not useful and only the LP-based layer is
66
used. On the other hand, non-speech signals are not always adequately coded
Jean-Marc Valin's avatar
Jean-Marc Valin committed
67
68
using linear prediction, so for music only the MDCT-based layer is used.
</t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
69

Jean-Marc Valin's avatar
Jean-Marc Valin committed
70
<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
71
The Opus LP layer is based on the 
Jean-Marc Valin's avatar
Jean-Marc Valin committed
72
<eref target='http://developer.skype.com/silk'>SILK</eref> codec 
Jean-Marc Valin's avatar
Jean-Marc Valin committed
73
<xref target="SILK"></xref> while the MDCT layer is based on the 
Jean-Marc Valin's avatar
Jean-Marc Valin committed
74
<eref target='http://www.celt-codec.org/'>CELT</eref>  codec
Jean-Marc Valin's avatar
Jean-Marc Valin committed
75
 <xref target="CELT"></xref>.
Jean-Marc Valin's avatar
Jean-Marc Valin committed
76
77
</t>

Gregory Maxwell's avatar
Gregory Maxwell committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<t>The primary normative part of this specification is provided by the source
code part of the document. The codec contains significant amounts of fixed-point
arithmetic which must be performed exactly, including all rounding considerations,
and so any useful specification must make extensive use of domain-specific
symbolic language to adequately define these operations. Additionally, any
conflict between the symbolic representation and the included reference
implementation must be resolved. For the practical reasons of compatibility and
testability it would be advantageous to give the reference implementation to
have priority in any disagreement. The C language is also one of the most
widely understood human-readable symbolic representations for machine
behavior. For these reasons this RFC utilizes the reference implementation
as the sole symbolic representation of the codec.</t>

<t>While the symbolic representation is unambiguous and complete it is not
always the easiest way to understand the codec's operation. For this reason
this document also describes significant parts of the codec in english and
takes the opportunity to explain the rational behind many of the more
surprising elements of the design. These descriptions are intended to be
accurate and informative but the limitations of common english sometimes
result in ambiguity, so it is intended that the reader will always read
them alongside the symbolic representation. Numerous references to the
implementation are provided for this purpose. The descriptions sometimes
differs in ordering, or through mathematical simplification, from the
reference wherever such deviation made an explanation easier to understand.
For example, the right shift and left shift operations in the reference
implementation are often described using division and multiplication in the text.
In general, the text is focused on the 'what' and 'why' while the symbolic
representation most clearly provides the 'how'.
</t>

Jean-Marc Valin's avatar
Jean-Marc Valin committed
108
109
</section>

Jean-Marc Valin's avatar
Jean-Marc Valin committed
110
<section anchor="hybrid" title="Opus Codec">
111
112
113
114
115
116
117

<t>
In hybrid mode, each frame is coded first by the LP layer and then by the MDCT 
layer. In the current prototype, the cutoff frequency is 8 kHz. In the MDCT
layer, all bands below 8 kHz are discarded, such that there is no coding
redundancy between the two layers. Also both layers use the same instance of 
the range coder to encode the signal, which ensures that no "padding bits" are
Jean-Marc Valin's avatar
Jean-Marc Valin committed
118
119
120
121
wasted. The hybrid approach makes it easy to support both constant bit-rate
(CBR) and varaible bit-rate (VBR) coding. Although the SILK layer used is VBR,
it is easy to make the bit allocation of the CELT layer produce a final stream
that is CBR by using all the bits left unused by the SILK layer.
122
123
</t>

Jean-Marc Valin's avatar
Jean-Marc Valin committed
124
125
126
127
128
129
130
131
132
133
<t>
In addition to their frame size, the SILK and CELT codecs require
a look-ahead of 5.2 ms and 2.5 ms, respectively. SILK's look-ahead is due to
noise shaping estimation (5 ms) and the internal resampling (0.2 ms), while
CELT's look-ahead is due to the overlapping MDCT windows. To compensate for the
difference, the CELT encoder input is delayed by 2.7 ms. This ensures that low
frequencies and high frequencies arrive at the same time.
</t>


134
135
136
137
138
<section title="Source Code">
<t>
The source code is currently available in a
<eref target='git://git.xiph.org/users/jm/ietfcodec.git'>Git repository</eref> 
which references two other
Jean-Marc Valin's avatar
Jean-Marc Valin committed
139
140
141
repositories (for SILK and CELT). Development snapshots are provided at 
<eref target='http://opus-codec.org/'/>.

142
143
144
145
146
</t>
</section>

</section>

Jean-Marc Valin's avatar
Jean-Marc Valin committed
147
148
149
150
151
152
153
154
<section anchor="modes" title="Codec Modes">
<t>
There are three possible operating modes for the proposed prototype:
<list style="numbers">
<t>A linear prediction (LP) mode for use in low bit-rate connections with up to 8 kHz audio bandwidth (16 kHz sampling rate)</t>
<t>A hybrid (LP+MDCT) mode for full-bandwidth speech at medium bitrates</t>
<t>An MDCT-only mode for very low delay speech transmission as well as music transmission.</t>
</list>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
155
156
Each of these modes supports a number of difference frame sizes and sampling
rates. In order to distinguish between the various modes and configurations,
Jean-Marc Valin's avatar
Jean-Marc Valin committed
157
we define a single-byte table-of-contents (TOC) header that can used in the transport layer 
Jean-Marc Valin's avatar
Jean-Marc Valin committed
158
(e.g RTP) to signal this information. The following describes the proposed
Jean-Marc Valin's avatar
Jean-Marc Valin committed
159
TOC byte.
Jean-Marc Valin's avatar
Jean-Marc Valin committed
160
161
162
</t>

<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
163
The LP mode supports the following configurations (numbered from 0 to 11):
Jean-Marc Valin's avatar
Jean-Marc Valin committed
164
<list style="symbols">
Jean-Marc Valin's avatar
Jean-Marc Valin committed
165
166
167
<t>8 kHz:  10, 20, 40, 60 ms (0..3)</t>
<t>12 kHz: 10, 20, 40, 60 ms (4..7)</t>
<t>16 kHz: 10, 20, 40, 60 ms (8..11)</t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
168
169
170
171
172
</list>
for a total of 12 configurations.
</t>

<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
173
The hybrid mode supports the following configurations (numbered from 12 to 15):
Jean-Marc Valin's avatar
Jean-Marc Valin committed
174
<list style="symbols">
Jean-Marc Valin's avatar
Jean-Marc Valin committed
175
176
<t>32 kHz: 10, 20 ms (12..13)</t>
<t>48 kHz: 10, 20 ms (14..15)</t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
177
178
179
180
181
</list>
for a total of 4 configurations.
</t>

<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
182
The MDCT-only mode supports the following configurations (numbered from 16 to 31):
Jean-Marc Valin's avatar
Jean-Marc Valin committed
183
<list style="symbols">
Jean-Marc Valin's avatar
Jean-Marc Valin committed
184
185
186
187
<t>8 kHz:  2.5, 5, 10, 20 ms (16..19)</t>
<t>16 kHz: 2.5, 5, 10, 20 ms (20..23)</t>
<t>32 kHz: 2.5, 5, 10, 20 ms (24..27)</t>
<t>48 kHz: 2.5, 5, 10, 20 ms (28..31)</t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
188
</list>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
189
for a total of 16 configurations.
Jean-Marc Valin's avatar
Jean-Marc Valin committed
190
191
192
</t>

<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
193
There is thus a total of 32 configurations, encoded in 5 bits. On bit is used to signal mono vs stereo, which leaves 2 bits for the number of frames per packets (codes 0 to 3):
Jean-Marc Valin's avatar
Jean-Marc Valin committed
194
<list style="symbols">
Jean-Marc Valin's avatar
Jean-Marc Valin committed
195
196
<t>0:    1 frames in the packet</t>
<t>1:    2 frames in the packet, each with equal compressed size</t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
197
198
<t>2:    2 frames in the packet, with different compressed size</t>
<t>3:    arbitrary number of frames in the packet</t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
199
</list>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
200
201
For code 2, the TOC byte is followed by the length of the first frame, encoded as described below.
For code 3, the TOC byte is followed by a byte encoding the number of frames in the packet, with the MSB indicating VBR. In the VBR case, the byte indicating the number of frames is followed by N-1 frame 
Jean-Marc Valin's avatar
Jean-Marc Valin committed
202
203
lengths encoded as described below. As an additional limit, the audio duration contained
within a packet may not exceed 120 ms.
Jean-Marc Valin's avatar
Jean-Marc Valin committed
204
205
206
207
208
</t>

<t>
The compressed size of the frames (if needed) is indicated -- usually -- with one byte, with the following meaning:
<list style="symbols">
Jean-Marc Valin's avatar
Jean-Marc Valin committed
209
<t>0:          No frame (DTX or lost packet)</t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
210
211
<t>1-251:      Size of the frame in bytes</t>
<t>252-255:    A second byte is needed. The total size is (size[1]*4)+size[0]</t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
212
213
214
215
</list>
</t>

<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
216
The maximum size representable is 255*4+255=1275 bytes. For 20 ms frames, that 
Jean-Marc Valin's avatar
Jean-Marc Valin committed
217
218
219
220
221
222
represents a bit-rate of 510 kb/s, which is really the highest rate anyone would want 
to use in stereo mode (beyond that point, lossless codecs would be more appropriate).
</t>

<section anchor="examples" title="Examples">
<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
223
Simplest case: one narrowband mono 20-ms SILK frame
Jean-Marc Valin's avatar
Jean-Marc Valin committed
224
225
226
227
228
229
230
231
</t>

<t>
<figure>
<artwork><![CDATA[
 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jean-Marc Valin's avatar
Jean-Marc Valin committed
232
|    1    |0|0|0|               compressed data...              |
Jean-Marc Valin's avatar
Jean-Marc Valin committed
233
234
235
236
237
238
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
]]></artwork>
</figure>
</t>

<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
239
Two 48 kHz mono 5 ms CELT frames of the same compressed size:
Jean-Marc Valin's avatar
Jean-Marc Valin committed
240
241
242
243
244
245
246
247
</t>

<t>
<figure>
<artwork><![CDATA[
 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jean-Marc Valin's avatar
Jean-Marc Valin committed
248
|    29   |0|0|1|               compressed data...              |
Jean-Marc Valin's avatar
Jean-Marc Valin committed
249
250
251
252
253
254
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
]]></artwork>
</figure>
</t>

<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
255
Two 48 kHz mono 20-ms hybrid frames of different compressed size:
Jean-Marc Valin's avatar
Jean-Marc Valin committed
256
257
258
259
260
261
262
263
</t>

<t>
<figure>
<artwork><![CDATA[
 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jean-Marc Valin's avatar
Jean-Marc Valin committed
264
265
266
|    15   |0|1|1|       2       |   frame size  |compressed data|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                       compressed data...                      |
Jean-Marc Valin's avatar
Jean-Marc Valin committed
267
268
269
270
271
272
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
]]></artwork>
</figure>
</t>

<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
273
Four 48 kHz stereo 20-ms CELT frame of the same compressed size:
Jean-Marc Valin's avatar
Jean-Marc Valin committed
274
275
276
277
278
279
280
281
282

</t>

<t>
<figure>
<artwork><![CDATA[
 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jean-Marc Valin's avatar
Jean-Marc Valin committed
283
|    31   |1|1|0|       4       |      compressed data...       |
Jean-Marc Valin's avatar
Jean-Marc Valin committed
284
285
286
287
288
289
290
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
]]></artwork>
</figure>
</t>
</section>


291
292
</section>

Jean-Marc Valin's avatar
Jean-Marc Valin committed
293
<section title="Opus Decoder">
294
<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
295
296
297
298
299
The Opus decoder consists of two main blocks: the SILK decoder and the CELT decoder. 
The output of the Opus decode is the sum of the outputs from the SILK and CELT decoders
with proper sample rate conversion and delay compensation as illustrated in the
block diagram below. At any given time, one or both of the SILK and CELT decoders
may be active. 
300
301
<figure>
<artwork>
302
<![CDATA[
Jean-Marc Valin's avatar
Jean-Marc Valin committed
303
304
305
306
307
308
309
310
311
312
313
                       +-------+    +----------+
                       | SILK  |    |  sample  |
                    +->|encoder|--->|   rate   |----+
bit-    +-------+   |  |       |    |conversion|    v
stream  | Range |---+  +-------+    +----------+  /---\  audio
------->|decoder|                                 | + |------>
        |       |---+  +-------+    +----------+  \---/
        +-------+   |  | CELT  |    | Delay    |    ^
                    +->|decoder|----| compens- |----+
                       |       |    | ation    |
                       +-------+    +----------+
314
315
316
]]>
</artwork>
</figure>
317
318
</t>

Jean-Marc Valin's avatar
Jean-Marc Valin committed
319
<section anchor="range-decoder" title="Range Decoder">
320
<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
321
322
323
324
325
326
327
328
329
The range decoder extracts the symbols and integers encoded using the range encoder in
<xref target="range-encoder"></xref>. The range decoder maintains an internal
state vector composed of the two-tuple (dif,rng), representing the
difference between the high end of the current range and the actual
coded value, and the size of the current range, respectively. Both
dif and rng are 32-bit unsigned integer values. rng is initialized to
2^7. dif is initialized to rng minus the top 7 bits of the first
input octet. Then the range is immediately normalized, using the
procedure described in the following section.
330
331
</t>

Jean-Marc Valin's avatar
Jean-Marc Valin committed
332
<section anchor="decoding-symbols" title="Decoding Symbols">
333
<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
334
335
336
337
338
   Decoding symbols is a two-step process. The first step determines
   a value fs that lies within the range of some symbol in the current
   context. The second step updates the range decoder state with the
   three-tuple (fl,fh,ft) corresponding to that symbol, as defined in
   <xref target="encoding-symbols"></xref>.
339
340
</t>
<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
341
342
343
344
345
   The first step is implemented by ec_decode() 
   (rangedec.c), 
   and computes fs = ft-min((dif-1)/(rng/ft)+1,ft), where ft is
   the sum of the frequency counts in the current context, as described
   in <xref target="encoding-symbols"></xref>. The divisions here are exact integer division. 
346
347
</t>
<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
348
349
350
351
352
   In the reference implementation, a special version of ec_decode()
   called ec_decode_bin() (rangeenc.c) is defined using
   the parameter ftb instead of ft. It is mathematically equivalent to
   calling ec_decode() with ft = (1&lt;&lt;ftb), but avoids one of the
   divisions.
353
354
</t>
<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
355
356
357
358
359
   The decoder then identifies the symbol in the current context
   corresponding to fs; i.e., the one whose three-tuple (fl,fh,ft)
   satisfies fl &lt;= fs &lt; fh. This tuple is used to update the decoder
   state according to dif = dif - (rng/ft)*(ft-fh), and if fl is greater
   than zero, rng = (rng/ft)*(fh-fl), or otherwise rng = rng - (rng/ft)*(ft-fh). After this update, the range is normalized.
360
361
362
</t>
<t>
   To normalize the range, the following process is repeated until
Jean-Marc Valin's avatar
Jean-Marc Valin committed
363
364
365
366
367
368
369
370
371
   rng > 2^23. First, rng is set to (rng&lt;8)&amp;0xFFFFFFFF. Then the next
   8 bits of input are read into sym, using the remaining bit from the
   previous input octet as the high bit of sym, and the top 7 bits of the
   next octet for the remaining bits of sym. If no more input octets
   remain, zero bits are used instead. Then, dif is set to
   (dif&lt;&lt;8)-sym&amp;0xFFFFFFFF (i.e., using wrap-around if the subtraction
   overflows a 32-bit register). Finally, if dif is larger than 2^31,
   dif is then set to dif - 2^31. This process is carried out by
   ec_dec_normalize() (rangedec.c).
372
373
374
</t>
</section>

Jean-Marc Valin's avatar
Jean-Marc Valin committed
375
<section anchor="decoding-ints" title="Decoding Uniformly Distributed Integers">
376
<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
377
378
379
380
   Functions ec_dec_uint() or ec_dec_bits() are based on ec_decode() and
   decode one of N equiprobable symbols, each with a frequency of 1,
   where N may be as large as 2^32-1. Because ec_decode() is limited to
   a total frequency of 2^16-1, this is done by decoding a series of
381
382
383
   symbols in smaller contexts.
</t>
<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
384
385
386
387
388
389
390
391
392
393
   ec_dec_bits() (entdec.c) is defined, like
   ec_decode_bin(), to take a single parameter ftb, with ftb &lt; 32.
   and ftb &lt; 32, and produces an ftb-bit decoded integer value, t,
   initialized to zero. While ftb is greater than 8, it decodes the next
   8 most significant bits of the integer, s = ec_decode_bin(8), updates
   the decoder state with the 3-tuple (s,s+1,256), adds those bits to
   the current value of t, t = t&lt;&lt;8 | s, and subtracts 8 from ftb. Then
   it decodes the remaining bits of the integer, s = ec_decode_bin(ftb),
   updates the decoder state with the 3 tuple (s,s+1,1&lt;&lt;ftb), and adds
   those bits to the final values of t, t = t&lt;&lt;ftb | s.
394
395
</t>
<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
396
397
398
399
400
401
402
403
404
405
406
407
408
   ec_dec_uint() (entdec.c) takes a single parameter,
   ft, which is not necessarily a power of two, and returns an integer,
   t, with a value between 0 and ft-1, inclusive, which is initialized to zero. Let
   ftb be the location of the highest 1 bit in the two's-complement
   representation of (ft-1), or -1 if no bits are set. If ftb>8, then
   the top 8 bits of t are decoded using t = ec_decode((ft-1>>ftb-8)+1),
   the decoder state is updated with the three-tuple
   (s,s+1,(ft-1>>ftb-8)+1), and the remaining bits are decoded with
   t = t&lt;&lt;ftb-8|ec_dec_bits(ftb-8). If, at this point, t >= ft, then
   the current frame is corrupt, and decoding should stop. If the
   original value of ftb was not greater than 8, then t is decoded with
   t = ec_decode(ft), and the decoder state is updated with the
   three-tuple (t,t+1,ft).
409
410
411
</t>
</section>

Jean-Marc Valin's avatar
Jean-Marc Valin committed
412
<section anchor="decoder-tell" title="Current Bit Usage">
413
<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
414
   The bit allocation routines in CELT need to be able to determine a
415
   conservative upper bound on the number of bits that have been used
Jean-Marc Valin's avatar
Jean-Marc Valin committed
416
417
418
419
420
421
422
   to decode from the current frame thus far. This drives allocation
   decisions which must match those made in the encoder. This is
   computed in the reference implementation to fractional bit precision
   by the function ec_dec_tell() (rangedec.c). Like all
   operations in the range decoder, it must be implemented in a
   bit-exact manner, and must produce exactly the same value returned by
   ec_enc_tell() after encoding the same symbols.
423
424
425
426
427
</t>
</section>

</section>

Jean-Marc Valin's avatar
Jean-Marc Valin committed
428
429
430
431
432
      <section anchor='outline_decoder' title='SILK Decoder'>
        <t>
          At the receiving end, the received packets are by the range decoder split into a number of frames contained in the packet. Each of which contains the necessary information to reconstruct a 20 ms frame of the output signal.
        </t>
        <section title="Decoder Modules">
433
          <t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
            An overview of the decoder is given in <xref target="decoder_figure" />.
            <figure align="center" anchor="decoder_figure">
              <artwork align="center">
                <![CDATA[
   
   +---------+    +------------+    
-->| Range   |--->| Decode     |---------------------------+
 1 | Decoder | 2  | Parameters |----------+       5        |
   +---------+    +------------+     4    |                |
                       3 |                |                |
                        \/               \/               \/
                  +------------+   +------------+   +------------+
                  | Generate   |-->| LTP        |-->| LPC        |-->
                  | Excitation |   | Synthesis  |   | Synthesis  | 6
                  +------------+   +------------+   +------------+

1: Range encoded bitstream
2: Coded parameters
3: Pulses and gains
4: Pitch lags and LTP coefficients
5: LPC coefficients
6: Decoded signal
]]>
              </artwork>
              <postamble>Decoder block diagram.</postamble>
            </figure>
460
461
          </t>

Jean-Marc Valin's avatar
Jean-Marc Valin committed
462
          <section title='Range Decoder'>
463
            <t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
464
              The range decoder decodes the encoded parameters from the received bitstream. Output from this function includes the pulses and gains for the excitation signal generation, as well as LTP and LSF codebook indices, which are needed for decoding LTP and LPC coefficients needed for LTP and LPC synthesis filtering the excitation signal, respectively.
465
466
467
            </t>
          </section>

Jean-Marc Valin's avatar
Jean-Marc Valin committed
468
          <section title='Decode Parameters'>
469
            <t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
470
              Pulses and gains are decoded from the parameters that was decoded by the range decoder.
471
            </t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
472

473
            <t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
474
475
476
              When a voiced frame is decoded and LTP codebook selection and indices are received, LTP coefficients are decoded using the selected codebook by choosing the vector that corresponds to the given codebook index in that codebook. This is done for each of the four subframes.
              The LPC coefficients are decoded from the LSF codebook by first adding the chosen vectors, one vector from each stage of the codebook. The resulting LSF vector is stabilized using the same method that was used in the encoder, see
              <xref target='lsf_stabilizer_overview_section' />. The LSF coefficients are then converted to LPC coefficients, and passed on to the LPC synthesis filter.
477
478
479
            </t>
          </section>

Jean-Marc Valin's avatar
Jean-Marc Valin committed
480
          <section title='Generate Excitation'>
481
            <t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
482
              The pulses signal is multiplied with the quantization gain to create the excitation signal.
483
484
485
            </t>
          </section>

Jean-Marc Valin's avatar
Jean-Marc Valin committed
486
          <section title='LTP Synthesis'>
487
            <t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
488
489
              For voiced speech, the excitation signal e(n) is input to an LTP synthesis filter that will recreate the long term correlation that was removed in the LTP analysis filter and generate an LPC excitation signal e_LPC(n), according to
              <figure align="center">
490
491
                <artwork align="center">
                  <![CDATA[
Jean-Marc Valin's avatar
Jean-Marc Valin committed
492
493
494
495
496
                   d
                  __
e_LPC(n) = e(n) + \  e(n - L - i) * b_i,
                  /_
                 i=-d
497
498
499
]]>
                </artwork>
              </figure>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
500
              using the pitch lag L, and the decoded LTP coefficients b_i.
501

Jean-Marc Valin's avatar
Jean-Marc Valin committed
502
              For unvoiced speech, the output signal is simply a copy of the excitation signal, i.e., e_LPC(n) = e(n).
503
            </t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
504
          </section>
505

Jean-Marc Valin's avatar
Jean-Marc Valin committed
506
          <section title='LPC Synthesis'>
507
            <t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
508
              In a similar manner, the short-term correlation that was removed in the LPC analysis filter is recreated in the LPC synthesis filter. The LPC excitation signal e_LPC(n) is filtered using the LTP coefficients a_i, according to
509
510
511
              <figure align="center">
                <artwork align="center">
                  <![CDATA[
Jean-Marc Valin's avatar
Jean-Marc Valin committed
512
513
514
515
516
517
                 d_LPC
                  __
y(n) = e_LPC(n) + \  e_LPC(n - i) * a_i,
                  /_
                  i=1
]]>
518
519
                </artwork>
              </figure>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
520
              where d_LPC is the LPC synthesis filter order, and y(n) is the decoded output signal.
521
522
            </t>
          </section>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
523
524
        </section>
      </section>
525
526


Jean-Marc Valin's avatar
Jean-Marc Valin committed
527
<section title="CELT Decoder">
528

Jean-Marc Valin's avatar
Jean-Marc Valin committed
529
530
<t>
Insert decoder figure.
531

Jean-Marc Valin's avatar
Jean-Marc Valin committed
532
</t>
533

Jean-Marc Valin's avatar
Jean-Marc Valin committed
534
535
536
537
<texttable anchor='table_example'>
<ttcol align='center'>Symbol(s)</ttcol>
<ttcol align='center'>PDF</ttcol>
<ttcol align='center'>Condition</ttcol>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
538
539
<c>silence</c>      <c>[32767, 1]/32768</c> <c></c>
<c>post-filter</c>  <c>[1, 1]/2</c> <c></c>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
540
541
542
543
<c>octave</c>       <c>uniform (6)</c><c>post-filter</c>
<c>period</c>       <c>raw bits (4+octave)</c><c>post-filter</c>
<c>gain</c>         <c>raw bits (3)</c><c>post-filter</c>
<c>tapset</c>       <c>[2, 1, 1]/4</c><c>post-filter</c>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
544
545
<c>transient</c>    <c>[7, 1]/8</c><c></c>
<c>intra</c>        <c>[7, 1]/8</c><c></c>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
546
<c>coarse energy</c><c><xref target="energy-decoding"/></c><c></c>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
547
<c>tf_change</c>    <c><xref target="transient-decoding"/></c><c></c>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
548
<c>tf_select</c>    <c>[1, 1]/2</c><c><xref target="transient-decoding"/></c>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
549
<c>spread</c>       <c>[7, 2, 21, 2]/32</c><c></c>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
550
<c>dyn. alloc.</c>  <c><xref target="allocation"/></c><c></c>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
551
<c>alloc. trim</c>  <c>[2, 2, 5, 10, 22, 46, 22, 10, 5, 2, 2]/128</c><c></c>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
552
553
554
<c>skip</c>         <c>[1, 1]/2</c><c><xref target="allocation"/></c>
<c>intensity</c>    <c>uniform</c><c><xref target="allocation"/></c>
<c>dual</c>         <c>[1, 1]/2</c><c></c>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
555
<c>fine energy</c>  <c><xref target="energy-decoding"/></c><c></c>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
556
<c>residual</c>     <c><xref target="PVQ-decoder"/></c><c></c>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
557
<c>anti-collapse</c><c>[1, 1]/2</c><c><xref target="anti-collapse"/></c>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
558
<c>finalize</c>     <c><xref target="energy-decoding"/></c><c></c>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
559
<postamble>Order of the symbols in the CELT section of the bit-stream.</postamble>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
560
561
562
563
564
565
566
567
568
569
570
</texttable>

<t>
The decoder extracts information from the range-coded bit-stream in the order
described in the figure above. In some circumstances, it is 
possible for a decoded value to be out of range due to a very small amount of redundancy
in the encoding of large integers by the range coder.
In that case, the decoder should assume there has been an error in the coding, 
decoding, or transmission and SHOULD take measures to conceal the error and/or report
to the application that a problem has occurred.
</t>
571

Jean-Marc Valin's avatar
Jean-Marc Valin committed
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
<section anchor="transient-decoding" title="Transient Decoding">
<t>
The <spanx style="emph">transient</spanx> flag encoded in the bit-stream has a
probability of 1/8. When it is set, then the MDCT coefficients represent multiple 
short MDCTs in the frame. When not set, the coefficients represent a single
long MDCT for the frame. In addition to the global transient flag is a per-band
binary flag to change the time-frequency (tf) resolution independently in each band. The 
change in tf resolution is defined in tf_select_table[][] in celt.c and depends
on the frame size, whether the transient flag is set, and the value of tf_select.
The tf_select flag uses a 1/2 probability, but is only decoded 
if it can have an impact on the result knowing the value of all per-band
tf_change flags. 
</t>
</section>

Jean-Marc Valin's avatar
Jean-Marc Valin committed
587
<section anchor="energy-decoding" title="Energy Envelope Decoding">
Jean-Marc Valin's avatar
Jean-Marc Valin committed
588

Jean-Marc Valin's avatar
Jean-Marc Valin committed
589
<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
It is important to quantize the energy with sufficient resolution because
any energy quantization error cannot be compensated for at a later
stage. Regardless of the resolution used for encoding the shape of a band,
it is perceptually important to preserve the energy in each band. CELT uses a 
three-step coarse-fine-fine strategy for encoding the energy in the base-2 log
domain, as implemented in quant_bands.c</t>

<section anchor="coarse-energy-decoding" title="Coarse energy decoding">
<t>
Coarse quantization of the energy uses a fixed resolution of 6 dB
(integer part of base-2 log). To minimize the bitrate, prediction is applied
both in time (using the previous frame) and in frequency (using the previous
bands). The part of the prediction that is based on the
previous frame can be disabled, creating an "intra" frame where the energy
is coded without reference to prior frames. The decoder first reads the intra flag
to determine what prediction is used.
The 2-D z-transform of
the prediction filter is: A(z_l, z_b)=(1-a*z_l^-1)*(1-z_b^-1)/(1-b*z_b^-1)
where b is the band index and l is the frame index. The prediction coefficients
applied depend on the frame size in use when not using intra energy and a=0 b=4915/32768
when using intra energy.
The time-domain prediction is based on the final fine quantization of the previous
frame, while the frequency domain (within the current frame) prediction is based
on coarse quantization only (because the fine quantization has not been computed
yet). The prediction is clamped internally so that fixed point implementations with
limited dynamic range to not suffer desynchronization.  
We approximate the ideal
probability distribution of the prediction error using a Laplace distribution
with seperate parameters for each frame size in intra and inter-frame modes. The
coarse energy quantization is performed by unquant_coarse_energy() and 
unquant_coarse_energy_impl() (quant_bands.c). The encoding of the Laplace-distributed values is
implemented in ec_laplace_decode() (laplace.c).
Jean-Marc Valin's avatar
Jean-Marc Valin committed
622
</t>
623

Jean-Marc Valin's avatar
Jean-Marc Valin committed
624
625
626
</section>

<section anchor="fine-energy-decoding" title="Fine energy quantization">
Jean-Marc Valin's avatar
Jean-Marc Valin committed
627
<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
628
629
630
631
632
633
The number of bits assigned to fine energy quantization in each band is determined
by the bit allocation computation described in <xref target="allocation"></xref>. 
Let B_i be the number of fine energy bits 
for band i; the refinement is an integer f in the range [0,2^B_i-1]. The mapping between f
and the correction applied to the coarse energy is equal to (f+1/2)/2^B_i - 1/2. Fine
energy quantization is implemented in quant_fine_energy() (quant_bands.c). 
Jean-Marc Valin's avatar
Jean-Marc Valin committed
634
</t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
<t>
When some bits are left "unused" after all other flags have been decoded, these bits
are assigned to a "final" step of fine allocation. In effect, these bits are used
to add one extra fine energy bit per band per channel. The allocation process 
determines two <spanx style="emph">priorities</spanx> for the final fine bits. 
Any remaining bits are first assigned only to bands of priority 0, starting 
from band 0 and going up. If all bands of priority 0 have received one bit per
channel, then bands of priority 1 are assigned an extra bit per channel, 
starting from band 0. If any bit is left after this, they are left unused.
This is implemented in unquant_energy_finalise() (quant_bands.c).
</t>

</section> <!-- fine energy -->

</section> <!-- Energy decode -->

Jean-Marc Valin's avatar
Jean-Marc Valin committed
651
<section anchor="allocation" title="Bit allocation">
Gregory Maxwell's avatar
Gregory Maxwell committed
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
<t>Many codecs transmit significant amounts of side information for
the purpose of controlling bit allocation within a frame. Often this
side information controls bit usage indirectly and must be carefully
selected to achieve the desired rate constraints.</t>

<t>The band-energy normalized structure of Opus MDCT mode ensures that a
constant bit allocation for the shape content of a band will result in a
roughly constant tone to noise ratio, which provides for fairly consistent
perceptual performance. The effectiveness of this approach is the result of
two factors: The band energy, which is understood to be perceptually
important on its own, is always preserved regardless of the shape precision and because
the constant tone-to-noise ratio implies a constant intra-band noise to masking ratio.
Intra-band masking is the strongest of the perceptual masking effects. This structure
means that the ideal allocation is more consistent from frame to frame than
it is for other codecs without an equivalent structure.</t>

<t>Because the bit allocation is used to drive the decoding of the range-coder
stream it MUST be recovered exactly so that identical coding decisions are 
made in the encoder and decoder. Any deviation from the reference's resulting
bit allocation will result in corrupted output, though implementers are 
free to implement the procedure in any way which produces identical results.</t>

<t>Because all of the information required to decode a frame must be derived
from that frame alone in order to retain robustness to packet loss the
overhead of explicitly signaling the allocation would be considerable,
especially for low-latency (small frame size) applications, 
even though the allocation is relatively static.</t>

<t>For this reason, in the MDCT mode Opus uses a primarily implicit bit
allocation. The available bit-stream capacity is known in advance to both
the encoder and decoder without additional signaling, ultimately from the
packet sizes expressed by a higher level protocol. Using this information
the codec interpolates an allocation from a hard-coded table.</t>

<t>While the band-energy structure effectively models intra-band masking,
it ignores the weaker inter-band masking, band-temporal masking, and
other less significant perceptual effects. While these effects can
often be ignored they can become significant for particular samples. One
mechanism available to encoders would be to simply increase the overall
rate for these frames, but this is not possible in a constant rate mode
and can be fairly inefficient. As a result three explicitly signaled
mechanisms are provided to alter the implicit allocation:</t>

<t>
<list style="symbols">
<t>Band boost</t>
<t>Allocation trim</t>
<t>band skipping</t>
</list>
701
702
</t>

Gregory Maxwell's avatar
Gregory Maxwell committed
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
<t>The first of these mechanisms, band boost, allows an encoder to boost
the allocation in specific bands. The second, allocation trim, works by
biasing the overall allocation towards higher or lower frequency bands. The third, band
skipping, selects which low-precision high frequency bands
will be allocated no shape bits at all.</t>

<t>In stereo mode there are also two additional parameters 
potentially coded as part of the allocation procedure: a parameter to allow the
selective elimination of allocation for the 'side' in jointly coded bands,
and a flag to deactivate joint coding. These values are not signaled if
they would be meaningless in the overall context of the allocation.</t>

<t>Because every signaled adjustment increases overhead and implementation
complexity none were included speculatively: The reference encoder makes use
of all of these mechanisms. While the decision logic in the reference was
found to be effective enough to justify the overhead and complexity further
analysis techniques may be discovered which increase the effectiveness of these 
parameters. As with other signaled parameters, encoder is free to choose the
values in any manner but unless a technique is known to deliver superior
perceptual results the methods used by the reference implementation should be
used.</t>

<t>The process of allocation consists of the following steps: determining the per-band
maximum allocation vector, decoding the boosts, decoding the tilt, determining
the remaining capacity the frame, searching the mode table for the
entry nearest but not exceeding the available space (subject to the tilt, boosts, band
Jean-Marc Valin's avatar
Jean-Marc Valin committed
729
maxima, and band minima), linear interpolation, reallocation of
Gregory Maxwell's avatar
Gregory Maxwell committed
730
731
732
733
734
735
736
737
738
739
740
741
742
743
unused bits with concurrent skip decoding, determination of the 
fine-energy vs shape split, and final reallocation. This process results
in an shape allocation per-band (in 1/8th bit units), a per-band fine-energy
allocation (in 1 bit per channel units), a set of band priorities for
controlling the use of remaining bits at the end of the frame, and a 
remaining balance of unallocated space which is usually zero except
at very high rates.</t>

<t>The maximum allocation vector is an approximation of the maximum space
which can be used by each band for a given mode. The value is 
approximate because the shape encoding is variable rate (due
to entropy coding of splitting parameters). Setting the maximum too low reduces the 
maximum achievable quality in a band while setting it too high
may result in waste: bit-stream capacity available at the end
Jean-Marc Valin's avatar
Jean-Marc Valin committed
744
of the frame which can not be put to any use. The maxima 
Gregory Maxwell's avatar
Gregory Maxwell committed
745
specified by the codec reflect the average maximum. In the reference
Jean-Marc Valin's avatar
Jean-Marc Valin committed
746
the maxima are provided partially computed form, in order to fit in less
Gregory Maxwell's avatar
Gregory Maxwell committed
747
748
749
750
memory, as a static table (XXX cache.caps). Implementations are expected
to simply use the same table data but the procedure for generating
this table is included in rate.c as part of compute_pulse_cache().</t>

Jean-Marc Valin's avatar
Jean-Marc Valin committed
751
<t>To convert the values in cache.caps into the actual maxima: First
Gregory Maxwell's avatar
Gregory Maxwell committed
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
set nbBands to the maximum number of bands for this mode and stereo to
zero if stereo is not in use and one otherwise. For each band assign N
to the number of MDCT bins covered by the band (for one channel), set LM
to the shift value for the frame size (e.g. 0 for 120, 1 for 240, 3 for 480)
then set i to nbBands*(2*LM+stereo). Then set the maximum for the band to
the i-th index of cache.caps + 64 and multiply by the number of channels
in the current frame (one or two) and by N then divide the result by 4
using truncating integer division. The resulting vector will be called
cap[]. The elements fit in signed 16 bit integers but do not fit in 8 bits.
This procedure is implemented in the reference in the function init_caps() in celt.c.
</t>

<t>The band boosts are represented by a series of binary symbols which
are coded with very low probability. Each band can potentially be boosted
multiple times, subject to the frame actually having enough room to obey
the boost and having enough room to code the boost symbol. The default
coding cost for a boost starts out at six bits, but subsequent boosts
in a band cost only a single bit and every time a band is boosted the
initial cost is reduced (down to a minimum of two). Since the initial
cost of coding a boost is 6 bits the coding cost of the boost symbols when
completely unused is 0.48 bits/frame for a 21 band mode (21*-log2(1-1/2^6)).</t>

<t>To decode the band boosts: First set 'dynalloc_logp' to 6, the initial
amount of storage required to signal a boost in bits, 'total_bits' to the
size of the frame in 8th-bits, 'total_boost' to zero, and 'tell' to the total number
of 8th bits decoded
so far. For each band from the coding start (0 normally, but 17 in hybrid mode)
to the coding end (which changes depending on the signaled bandwidth): Set 'width'
to the number of MDCT bins in this band for all channels. Take the larger of width
and 64, then the minimum of that value and the width times eight and set 'quanta'
to the result. This represents a boost step size of six bits subject to limits
of 1/bit/sample and 1/8th bit/sample. Set 'boost' to zero and 'dynalloc_loop_logp'
to dynalloc_logp. While dynalloc_loop_log (the current worst case symbol cost) in
8th bits plus tell is less than total_bits plus total_boost and boost is less than cap[] for this
band: Decode a bit from the bitstream with a with dynalloc_loop_logp as the cost
of a one, update tell to reflect the current used capacity, if the decoded value
is zero break the  loop otherwise add quanta to boost and total_boost, subtract quanta from 
total_bits, and set dynalloc_loop_log to 1. When the while loop finishes
boost contains the boost for this band. If boost is non-zero and dynalloc_logp
is greater than 2 decrease dynalloc_logp.  Once this process has been
execute on all bands the band boosts have been decoded. This procedure
is implemented around line 2352 of celt.c.</t>

<t>At very low rates it's possible that there won't be enough available
space to execute the inner loop even once. In these cases band boost
is not possible but its overhead is completely eliminated. Because of the
high cost of band boost when activated a reasonable encoder should not be 
using it at very low rates. The reference implements its dynalloc decision
logic at around 1269 of celt.c</t>

<t>The allocation trim is a integer value from 0-10. The default value of
5 indicates no trim. The trim parameter is entropy coded in order to
lower the coding cost of less extreme adjustments. Values lower than 
5 bias the allocation towards lower frequencies and values above 5
bias it towards higher frequencies. Like other signaled parameters, signaling
of the trim is gated so that it is not included if there is insufficient space
available in the bitstream. To decode the trim first set
the trim value to 5 then iff the count of decoded 8th bits so far (ec_tell_frac)
plus 48 (6 bits) is less than or equal to the total frame size in 8th
bits minus total_boost (a product of the above band boost procedure) then
decode the trim value using the inverse CDF {127, 126, 124, 119, 109, 87, 41, 19, 9, 4, 2, 0}.</t>

<t>Stereo parameters</t>

<t>Anti-collapse reservation</t>

<t>The allocation computation first begins by setting up some initial conditions.
'total' is set to the available remaining 8th bits, computed by taking the
size of the coded frame times 8 and subtracting ec_tell_frac(). From this value one (8th bit)
is subtracted to assure that the resulting allocation will be conservative. 'anti_collapse_rsv'
is set to 8 (8th bits) iff the frame is a transient, LM is greater than 1, and total is
greater than or equal to (LM+2) * 8. Total is then decremented by anti_collapse_rsv and clamped
to be equal to or greater than zero. 'skip_rsv' is set to 8 (8th bits) if total is greater than
8, otherwise it is zero. Total is then decremented by skip_rsv. This reserves space for the
final skipping flag.</t>

<t>If the current frame is stereo intensity_rsv is set to the conservative log2 in 8th bits
of the number of coded bands for this frame (given by the table LOG2_FRAC_TABLE). If 
intensity_rsv is greater than total then intensity_rsv is set to zero otherwise total is
decremented by intensity_rsv, and if total is still greater than 8 dual_stereo_rsv is
set to 8 and total is decremented by dual_stereo_rsv.</t>

<t>The allocation process then computes a vector representing the hard minimum amounts allocation
any band will receive for shape. This minimum is higher than the technical limit of the PVQ
process, but very low rate allocations produce excessively an sparse spectrum and these bands
are better served by having no allocation at all. For each coded band set thresh[band] to
twenty-four times the number of MDCT bins in the band and divide by 16. If 8 times the number
of channels is greater, use that instead. This sets the minimum allocation to one bit per channel
or 48 128th bits per MDCT bin, whichever is greater. The band size dependent part of this
value is not scaled by the channel count because at the very low rates where this limit is 
applicable there will usually be no bits allocated to the side.</t>

<t>The previously decoded allocation trim is used to derive a vector of per-band adjustments,
'trim_offsets[]'. For each coded band take the alloc_trim and subtract 5 and LM then multiply 
the result by number of channels, the number MDCT bins in the shortest frame size for this mode,
the number remaining bands, 2^LM, and 8. Then divide this value by 64. Finally, if the
number of MDCT bins in the band per channel is only one 8 times the number of channels is subtracted
in order to diminish the allocation by one bit because width 1 bands receive greater benefit
from the coarse energy coding.</t>


Jean-Marc Valin's avatar
Jean-Marc Valin committed
853
</section>
854

Gregory Maxwell's avatar
Gregory Maxwell committed
855

Jean-Marc Valin's avatar
Jean-Marc Valin committed
856
<section anchor="PVQ-decoder" title="Shape Decoder">
Jean-Marc Valin's avatar
Jean-Marc Valin committed
857
<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
858
859
860
861
862
863
864
865
866
867
868
869
870
In each band, the normalized <spanx style="emph">shape</spanx> is encoded
using a vector quantization scheme called a "Pyramid vector quantizer". 
</t>

<t>In
the simplest case, the number of bits allocated in 
<xref target="allocation"></xref> is converted to a number of pulses as described
by <xref target="bits-pulses"></xref>. Knowing the number of pulses and the
number of samples in the band, the decoder calculates the size of the codebook
as detailed in <xref target="cwrs-decoder"></xref>. The size is used to decode 
an unsigned integer (uniform probability model), which is the codeword index.
This index is converted into the corresponding vector as explained in
<xref target="cwrs-decoder"></xref>. This vector is then scaled to unit norm.
Jean-Marc Valin's avatar
Jean-Marc Valin committed
871
</t>
872

873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
<section anchor="bits-pulses" title="Bits to Pulses">
<t>
Although the allocation is performed in 1/8th bit units, the quantization requires
an integer number of pulses K. To do this, the encoder searches for the value
of K that produces the number of bits that is the nearest to the allocated value
(rounding down if exactly half-way between two values), subject to not exceeding
the total number of bits available. For efficiency reasons the search is performed against a
precomputated allocation table which only permits some K values for each N. The number of
codebooks entries can be computed as explained in <xref target="cwrs-encoding"></xref>. The difference
between the number of bits allocated and the number of bits used is accumulated to a
<spanx style="emph">balance</spanx> (initialised to zero) that helps adjusting the
allocation for the next bands. One third of the balance is applied to the
bit allocation of the each band to help achieving the target allocation. The only
exceptions are the band before the last and the last band, for which half the balance
and the whole balance are applied, respectively.
</t>
</section>

Jean-Marc Valin's avatar
Jean-Marc Valin committed
891
<section anchor="cwrs-decoder" title="Index Decoding">
Jean-Marc Valin's avatar
Jean-Marc Valin committed
892
893
894
895
896
897
898
899
900

<t>
The codeword is decoded as a uniformly-distributed integer value
by decode_pulses() (cwrs.c).
The codeword is converted from a unique index in the same way as specified in 
<xref target="PVQ"></xref>. The indexing is based on the calculation of V(N,K) 
(denoted N(L,K) in <xref target="PVQ"></xref>), which is the number of possible
combinations of K pulses 
in N samples. The number of combinations can be computed recursively as 
901
V(N,K) = V(N-1,K) + V(N,K-1) + V(N-1,K-1), with V(N,0) = 1 and V(0,K) = 0, K != 0. 
Jean-Marc Valin's avatar
Jean-Marc Valin committed
902
903
904
905
906
907
908
909
910
911
912
There are many different ways to compute V(N,K), including pre-computed tables and direct
use of the recursive formulation. The reference implementation applies the recursive
formulation one line (or column) at a time to save on memory use,
along with an alternate,
univariate recurrence to initialise an arbitrary line, and direct
polynomial solutions for small N. All of these methods are
equivalent, and have different trade-offs in speed, memory usage, and
code size. Implementations MAY use any methods they like, as long as
they are equivalent to the mathematical definition.
</t>

Jean-Marc Valin's avatar
Jean-Marc Valin committed
913
914
915
916
917
918
<t>
The decoding of the codeword from the index is performed as specified in 
<xref target="PVQ"></xref>, as implemented in function
decode_pulses() (cwrs.c).
</t>
</section>
919

Jean-Marc Valin's avatar
Jean-Marc Valin committed
920
<section anchor="spreading" title="Spreading">
Jean-Marc Valin's avatar
Jean-Marc Valin committed
921
922
<t>
</t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
923
</section>
924

Jean-Marc Valin's avatar
Jean-Marc Valin committed
925
926
927
928
<section anchor="split" title="Split decoding">
<t>
To avoid the need for multi-precision calculations when decoding PVQ codevectors,
the maximum size allowed for codebooks is 32 bits. When larger codebooks are
Jean-Marc Valin's avatar
Jean-Marc Valin committed
929
930
931
932
933
934
935
needed, the vector is instead split in two sub-vectors of size N/2. 
A quantized gain parameter with precision
derived from the current allocation is entropy coded to represent the relative
gains of each side of the split and the entire decoding process is recursively
applied. Multiple levels of splitting may be applied up to a frame size 
dependent limit. The same recursive mechanism is applied for the joint coding
of stereo audio.
Jean-Marc Valin's avatar
Jean-Marc Valin committed
936
</t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
937

Jean-Marc Valin's avatar
Jean-Marc Valin committed
938
</section>
939

Jean-Marc Valin's avatar
Jean-Marc Valin committed
940
<section anchor="tf-change" title="Time-Frequency change">
941
942
<t>
</t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
943
</section>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
944
945
946
947


</section>

Jean-Marc Valin's avatar
Jean-Marc Valin committed
948
949
950
951
952
953
<section anchor="anti-collapse" title="Anti-collapse processing">
<t>
When the frame has the transient bit set...
</t>
</section>

Jean-Marc Valin's avatar
Jean-Marc Valin committed
954
<section anchor="denormalization" title="Denormalization">
Jean-Marc Valin's avatar
Jean-Marc Valin committed
955
<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
956
957
958
959
Just like each band was normalized in the encoder, the last step of the decoder before
the inverse MDCT is to denormalize the bands. Each decoded normalized band is
multiplied by the square root of the decoded energy. This is done by denormalise_bands()
(bands.c).
Jean-Marc Valin's avatar
Jean-Marc Valin committed
960
961
962
</t>
</section>

Jean-Marc Valin's avatar
Jean-Marc Valin committed
963
964
965
966
967
968
969
970
<section anchor="inverse-mdct" title="Inverse MDCT">
<t>The inverse MDCT implementation has no special characteristics. The
input is N frequency-domain samples and the output is 2*N time-domain 
samples, while scaling by 1/2. The output is windowed using the same window 
as the encoder. The IMDCT and windowing are performed by mdct_backward
(mdct.c). If a time-domain pre-emphasis 
window was applied in the encoder, the (inverse) time-domain de-emphasis window
is applied on the IMDCT result. 
Jean-Marc Valin's avatar
Jean-Marc Valin committed
971
</t>
Gregory Maxwell's avatar
Gregory Maxwell committed
972

Jean-Marc Valin's avatar
Jean-Marc Valin committed
973
<section anchor="post-filter" title="Post-filter">
Jean-Marc Valin's avatar
Jean-Marc Valin committed
974
<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
The output of the inverse MDCT (after weighted overlap-add) is sent to the
post-filter. Although the post-filter is applied at the end, the post-filter
parameters are encoded at the beginning, just after the silence flag.
The post-filter can be switched on or off using one bit (logp=1).
If the post-filter is enabled, then the octave is decoded as an integer value
between 0 and 6 of uniform probability. Once the octave is known, the fine pitch
within the octave is decoded using 4+octave raw bits. The final pitch period
is equal to (16&lt;&lt;octave)+fine_pitch-1 so it is bounded between 15 and 1022,
inclusively. Next, the gain is decoded as three raw bits and is equal to 
G=3*(int_gain+1)/32. The set of post-filter taps is decoded last using 
a pdf equal to [2, 1, 1]/4. Tapset zero corresponds to the filter coefficients
g0 = 0.3066406250, g1 = 0.2170410156, g2 = 0.1296386719. Tapset one
corresponds to the filter coefficients g0 = 0.4638671875, g1 = 0.2680664062,
g2 = 0, and tapset two uses filter coefficients g0 = 0.7998046875,
g1 = 0.1000976562, g2 = 0.
Jean-Marc Valin's avatar
Jean-Marc Valin committed
990
991
992
</t>

<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
993
994
995
996
997
998
999
1000
1001
The post-filter response is thus computed as:
              <figure align="center">
                <artwork align="center">
                  <![CDATA[
   y(n) = x(n) + G*(g0*y(n-T) + g1*(y(n-T+1)+y(n-T+1)) 
                              + g2*(y(n-T+2)+y(n-T+2)))
]]>
                </artwork>
              </figure>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1002

Jean-Marc Valin's avatar
Jean-Marc Valin committed
1003
1004
1005
During a transition between different gains, a smooth transition is calculated
using the square of the MDCT window. It is important that values of y(n) be 
interpolated one at a time such that the past value of y(n) used is interpolated.
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1006
</t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1007
</section>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1008

Jean-Marc Valin's avatar
Jean-Marc Valin committed
1009
<section anchor="deemphasis" title="De-emphasis">
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1010
<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1011
1012
1013
After the post-filter, 
the signal is de-emphasized using the inverse of the pre-emphasis filter 
used in the encoder: 1/A(z)=1/(1-alpha_p*z^-1), where alpha_p=0.8500061035.
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1014
</t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1015
</section>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1016

Jean-Marc Valin's avatar
Jean-Marc Valin committed
1017
</section>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1018

Jean-Marc Valin's avatar
Jean-Marc Valin committed
1019
<section anchor="Packet Loss Concealment" title="Packet Loss Concealment (PLC)">
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1020
<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1021
1022
1023
1024
1025
1026
1027
1028
1029
Packet loss concealment (PLC) is an optional decoder-side feature which 
SHOULD be included when transmitting over an unreliable channel. Because 
PLC is not part of the bit-stream, there are several possible ways to 
implement PLC with different complexity/quality trade-offs. The PLC in
the reference implementation finds a periodicity in the decoded
signal and repeats the windowed waveform using the pitch offset. The windowed
waveform is overlapped in such a way as to preserve the time-domain aliasing
cancellation with the previous frame and the next frame. This is implemented 
in celt_decode_lost() (mdct.c).
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1030
1031
1032
</t>
</section>

Jean-Marc Valin's avatar
Jean-Marc Valin committed
1033
</section>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1034
1035
1036
1037

</section>


Jean-Marc Valin's avatar
Jean-Marc Valin committed
1038
1039
1040
<!--  ******************************************************************* -->
<!--  **************************   OPUS ENCODER   *********************** -->
<!--  ******************************************************************* -->
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1041

Jean-Marc Valin's avatar
Jean-Marc Valin committed
1042
<section title="Codec Encoder">
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1043
<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1044
1045
1046
Opus encoder block diagram.
<figure>
<artwork>
1047
<![CDATA[
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
         +----------+    +-------+
         |  sample  |    | SILK  |
      +->|   rate   |--->|encoder|--+
      |  |conversion|    |       |  |
audio |  +----------+    +-------+  |    +-------+
------+                             +--->| Range |
      |  +-------+                       |encoder|---->
      |  | CELT  |                  +--->|       | bit-stream
      +->|encoder|------------------+    +-------+
         |       |
         +-------+
]]>
</artwork>
</figure>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1062
1063
</t>

Jean-Marc Valin's avatar
Jean-Marc Valin committed
1064
<section anchor="range-encoder" title="Range Coder">
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1065
<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1066
1067
1068
1069
1070
1071
Opus uses an entropy coder based upon <xref target="range-coding"></xref>, 
which is itself a rediscovery of the FIFO arithmetic code introduced by <xref target="coding-thesis"></xref>.
It is very similar to arithmetic encoding, except that encoding is done with
digits in any base instead of with bits, 
so it is faster when using larger bases (i.e.: an octet). All of the
calculations in the range coder must use bit-exact integer arithmetic.
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1072
1073
1074
</t>

<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1075
1076
1077
1078
1079
1080
1081
The range coder also acts as the bit-packer for Opus. It is
used in three different ways, to encode:
<list style="symbols">
<t>entropy-coded symbols with a fixed probability model using ec_encode(), (rangeenc.c)</t>
<t>integers from 0 to 2^M-1 using ec_enc_uint() or ec_enc_bits(), (entenc.c)</t>
<t>integers from 0 to N-1 (where N is not a power of two) using ec_enc_uint(). (entenc.c)</t>
</list>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1082
1083
1084
</t>

<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1085
1086
1087
1088
1089
1090
1091
1092
The range encoder maintains an internal state vector composed of the
four-tuple (low,rng,rem,ext), representing the low end of the current
range, the size of the current range, a single buffered output octet,
and a count of additional carry-propagating output octets. Both rng
and low are 32-bit unsigned integer values, rem is an octet value or
the special value -1, and ext is an integer with at least 16 bits.
This state vector is initialized at the start of each each frame to
the value (0,2^31,-1,0).
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1093
1094
1095
</t>

<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1096
1097
1098
1099
1100
Each symbol is drawn from a finite alphabet and coded in a separate
context which describes the size of the alphabet and the relative
frequency of each symbol in that alphabet. Opus only uses static
contexts; they are not adapted to the statistics of the data that is
coded.
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1101
1102
</t>

Jean-Marc Valin's avatar
Jean-Marc Valin committed
1103
<section anchor="encoding-symbols" title="Encoding Symbols">
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1104
<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
   The main encoding function is ec_encode() (rangeenc.c),
   which takes as an argument a three-tuple (fl,fh,ft)
   describing the range of the symbol to be encoded in the current
   context, with 0 &lt;= fl &lt; fh &lt;= ft &lt;= 65535. The values of this tuple
   are derived from the probability model for the symbol. Let f(i) be
   the frequency of the ith symbol in the current context. Then the
   three-tuple corresponding to the kth symbol is given by
   <![CDATA[
fl=sum(f(i),i<k), fh=fl+f(i), and ft=sum(f(i)).
]]>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1115
1116
</t>
<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1117
1118
1119
1120
1121
   ec_encode() updates the state of the encoder as follows. If fl is
   greater than zero, then low = low + rng - (rng/ft)*(ft-fl) and 
   rng = (rng/ft)*(fh-fl). Otherwise, low is unchanged and
   rng = rng - (rng/ft)*(fh-fl). The divisions here are exact integer
   division. After this update, the range is normalized.
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1122
</t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
<t>
   To normalize the range, the following process is repeated until
   rng > 2^23. First, the top 9 bits of low, (low>>23), are placed into
   a carry buffer. Then, low is set to <![CDATA[(low << 8 & 0x7FFFFFFF) and rng
   is set to (rng<<8)]]>. This process is carried out by
   ec_enc_normalize() (rangeenc.c).
</t>
<t>
   The 9 bits produced in each iteration of the normalization loop
   consist of 8 data bits and a carry flag. The final value of the
   output bits is not determined until carry propagation is accounted
   for. Therefore the reference implementation buffers a single
   (non-propagating) output octet and keeps a count of additional
   propagating (0xFF) output octets. An implementation MAY choose to use
   any mathematically equivalent scheme to perform carry propagation.
</t>
<t>
   The function ec_enc_carry_out() (rangeenc.c) performs
   this buffering. It takes a 9-bit input value, c, from the normalization
   8-bit output and a carry bit. If c is 0xFF, then ext is incremented
   and no octets are output. Otherwise, if rem is not the special value
   -1, then the octet (rem+(c>>8)) is output. Then ext octets are output
   with the value 0 if the carry bit is set, or 0xFF if it is not, and
   rem is set to the lower 8 bits of c. After this, ext is set to zero.
</t>
<t>
   In the reference implementation, a special version of ec_encode()
   called ec_encode_bin() (rangeenc.c) is defined to
   take a two-tuple (fl,ftb), where <![CDATA[0 <= fl < 2^ftb and ftb < 16. It is
   mathematically equivalent to calling ec_encode() with the three-tuple
   (fl,fl+1,1<<ftb)]]>, but avoids using division.
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1154

Jean-Marc Valin's avatar
Jean-Marc Valin committed
1155
</t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1156
1157
</section>

Jean-Marc Valin's avatar
Jean-Marc Valin committed
1158
<section anchor="encoding-ints" title="Encoding Uniformly Distributed Integers">
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1159
<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1160
1161
1162
1163
1164
   Functions ec_enc_uint() or ec_enc_bits() are based on ec_encode() and 
   encode one of N equiprobable symbols, each with a frequency of 1,
   where N may be as large as 2^32-1. Because ec_encode() is limited to
   a total frequency of 2^16-1, this is done by encoding a series of
   symbols in smaller contexts.
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1165
1166
</t>
<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
   ec_enc_bits() (entenc.c) is defined, like
   ec_encode_bin(), to take a two-tuple (fl,ftb), with <![CDATA[0 <= fl < 2^ftb
   and ftb < 32. While ftb is greater than 8, it encodes bits (ftb-8) to
   (ftb-1) of fl, e.g., (fl>>ftb-8&0xFF) using ec_encode_bin() and
   subtracts 8 from ftb. Then, it encodes the remaining bits of fl, e.g.,
   (fl&(1<<ftb)-1)]]>, again using ec_encode_bin().
</t>
<t>
   ec_enc_uint() (entenc.c) takes a two-tuple (fl,ft),
   where ft is not necessarily a power of two. Let ftb be the location
   of the highest 1 bit in the two's-complement representation of
   (ft-1), or -1 if no bits are set. If ftb>8, then the top 8 bits of fl
   are encoded using ec_encode() with the three-tuple
   (fl>>ftb-8,(fl>>ftb-8)+1,(ft-1>>ftb-8)+1), and the remaining bits
   are encoded with ec_enc_bits using the two-tuple
   <![CDATA[(fl&(1<<ftb-8)-1,ftb-8). Otherwise, fl is encoded with ec_encode()
   directly using the three-tuple (fl,fl+1,ft)]]>.
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1184
</t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1185
</section>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1186

Jean-Marc Valin's avatar
Jean-Marc Valin committed
1187
<section anchor="encoder-finalizing" title="Finalizing the Stream">
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1188
<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
   After all symbols are encoded, the stream must be finalized by
   outputting a value inside the current range. Let end be the integer
   in the interval [low,low+rng) with the largest number of trailing
   zero bits. Then while end is not zero, the top 9 bits of end, e.g.,
   <![CDATA[(end>>23), are sent to the carry buffer, and end is replaced by
   (end<<8&0x7FFFFFFF). Finally, if the value in carry buffer, rem, is]]>
   neither zero nor the special value -1, or the carry count, ext, is
   greater than zero, then 9 zero bits are sent to the carry buffer.
   After the carry buffer is finished outputting octets, the rest of the
   output buffer is padded with zero octets. Finally, rem is set to the
   special value -1. This process is implemented by ec_enc_done()
   (rangeenc.c).
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1201
</t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1202
1203
1204
</section>

<section anchor="encoder-tell" title="Current Bit Usage">
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1205
<t>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
   The bit allocation routines in Opus need to be able to determine a
   conservative upper bound on the number of bits that have been used
   to encode the current frame thus far. This drives allocation
   decisions and ensures that the range code will not overflow the
   output buffer. This is computed in the reference implementation to
   fractional bit precision by the function ec_enc_tell() 
   (rangeenc.c).
   Like all operations in the range encoder, it must
   be implemented in a bit-exact manner.
</t>
</section>

</section>

        <section title='SILK Encoder'>
          <t>
            In the following, we focus on the core encoder and describe its components. For simplicity, we will refer to the core encoder simply as the encoder in the remainder of this document. An overview of the encoder is given in <xref target="encoder_figure" />.
          </t>

          <figure align="center" anchor="encoder_figure">
            <artwork align="center">
              <![CDATA[
                                                              +---+
                               +----------------------------->|   |
        +---------+            |     +---------+              |   |
        |Voice    |            |     |LTP      |              |   |
 +----->|Activity |-----+      +---->|Scaling  |---------+--->|   |
 |      |Detector |  3  |      |     |Control  |<+  12   |    |   |
 |      +---------+     |      |     +---------+ |       |    |   |
 |                      |      |     +---------+ |       |    |   |
 |                      |      |     |Gains    | |  11   |    |   |
 |                      |      |  +->|Processor|-|---+---|--->| R |
 |                      |      |  |  |         | |   |   |    | a |
 |                     \/      |  |  +---------+ |   |   |    | n |
 |                 +---------+ |  |  +---------+ |   |   |    | g |
 |                 |Pitch    | |  |  |LSF      | |   |   |    | e |
 |              +->|Analysis |-+  |  |Quantizer|-|---|---|--->|   |
 |              |  |         |4|  |  |         | | 8 |   |    | E |->
 |              |  +---------+ |  |  +---------+ |   |   |    | n |14
 |              |              |  |   9/\  10|   |   |   |    | c |
 |              |              |  |    |    \/   |   |   |    | o |
 |              |  +---------+ |  |  +----------+|   |   |    | d |
 |              |  |Noise    | +--|->|Prediction|+---|---|--->| e |
 |              +->|Shaping  |-|--+  |Analysis  || 7 |   |    | r |
 |              |  |Analysis |5|  |  |          ||   |   |    |   |
 |              |  +---------+ |  |  +----------+|   |   |    |   |
 |              |              |  |       /\     |   |   |    |   |
 |              |    +---------|--|-------+      |   |   |    |   |
 |              |    |        \/  \/            \/  \/  \/    |   |
 |  +---------+ |    |      +---------+       +------------+  |   |
 |  |High-Pass| |    |      |         |       |Noise       |  |   |
-+->|Filter   |-+----+----->|Prefilter|------>|Shaping     |->|   |
1   |         |      2      |         |   6   |Quantization|13|   |
    +---------+             +---------+       +------------+  +---+

1:  Input speech signal
2:  High passed input signal
3:  Voice activity estimate
4:  Pitch lags (per 5 ms) and voicing decision (per 20 ms)
5:  Noise shaping quantization coefficients
  - Short term synthesis and analysis 
    noise shaping coefficients (per 5 ms)
  - Long term synthesis and analysis noise 
    shaping coefficients (per 5 ms and for voiced speech only)
  - Noise shaping tilt (per 5 ms)
  - Quantizer gain/step size (per 5 ms)
6:  Input signal filtered with analysis noise shaping filters
7:  Short and long term prediction coefficients
    LTP (per 5 ms) and LPC (per 20 ms)
8:  LSF quantization indices
9:  LSF coefficients
10: Quantized LSF coefficients 
11: Processed gains, and synthesis noise shape coefficients
12: LTP state scaling coefficient. Controlling error propagation
   / prediction gain trade-off
13: Quantized signal
14: Range encoded bitstream

]]>
            </artwork>
            <postamble>Encoder block diagram.</postamble>
          </figure>

          <section title='Voice Activity Detection'>
            <t>
              The input signal is processed by a VAD (Voice Activity Detector) to produce a measure of voice activity, and also spectral tilt and signal-to-noise estimates, for each frame. The VAD uses a sequence of half-band filterbanks to split the signal in four subbands: 0 - Fs/16, Fs/16 - Fs/8, Fs/8 - Fs/4, and Fs/4 - Fs/2, where Fs is the sampling frequency, that is, 8, 12, 16 or 24 kHz. The lowest subband, from 0 - Fs/16 is high-pass filtered with a first-order MA (Moving Average) filter (with transfer function H(z) = 1-z^(-1)) to reduce the energy at the lowest frequencies. For each frame, the signal energy per subband is computed. In each subband, a noise level estimator tracks the background noise level and an SNR (Signal-to-Noise Ratio) value is computed as the logarithm of the ratio of energy to noise level. Using these intermediate variables, the following parameters are calculated for use in other SILK modules:
              <list style="symbols">
                <t>
                  Average SNR. The average of the subband SNR values.
                </t>

                <t>
                  Smoothed subband SNRs. Temporally smoothed subband SNR values.
                </t>

                <t>
                  Speech activity level. Based on the average SNR and a weighted average of the subband energies.
                </t>

                <t>
                  Spectral tilt. A weighted average of the subband SNRs, with positive weights for the low subbands and negative weights for the high subbands.
                </t>
              </list>
            </t>
          </section>

          <section title='High-Pass Filter'>
            <t>
              The input signal is filtered by a high-pass filter to remove the lowest part of the spectrum that contains little speech energy and may contain background noise. This is a second order ARMA (Auto Regressive Moving Average) filter with a cut-off frequency around 70 Hz.
            </t>
            <t>
              In the future, a music detector may also be used to lower the cut-off frequency when the input signal is detected to be music rather than speech.
            </t>
          </section>

          <section title='Pitch Analysis' anchor='pitch_estimator_overview_section'>
            <t>
              The high-passed input signal is processed by the open loop pitch estimator shown in <xref target='pitch_estimator_figure' />.
              <figure align="center" anchor="pitch_estimator_figure">
                <artwork align="center">
                  <![CDATA[
                                 +--------+  +----------+     
                                 |2 x Down|  |Time-     |      
                              +->|sampling|->|Correlator|     |
                              |  |        |  |          |     |4
                              |  +--------+  +----------+    \/
                              |                    | 2    +-------+
                              |                    |  +-->|Speech |5
    +---------+    +--------+ |                   \/  |   |Type   |->
    |LPC      |    |Down    | |              +----------+ |       |
 +->|Analysis | +->|sample  |-+------------->|Time-     | +-------+
 |  |         | |  |to 8 kHz|                |Correlator|----------->
 |  +---------+ |  +--------+                |__________|          6
 |       |      |                                  |3
 |      \/      |                                 \/ 
 |  +---------+ |                            +----------+
 |  |Whitening| |                            |Time-     |    
-+->|Filter   |-+--------------------------->|Correlator|----------->
1   |         |                              |          |          7
    +---------+                              +----------+ 
                                            
1: Input signal
2: Lag candidates from stage 1
3: Lag candidates from stage 2
4: Correlation threshold
5: Voiced/unvoiced flag
6: Pitch correlation
7: Pitch lags 
]]>
                </artwork>
                <postamble>Block diagram of the pitch estimator.</postamble>
              </figure>
              The pitch analysis finds a binary voiced/unvoiced classification, and, for frames classified as voiced, four pitch lags per frame - one for each 5 ms subframe - and a pitch correlation indicating the periodicity of the signal. The input is first whitened using a Linear Prediction (LP) whitening filter, where the coefficients are computed through standard Linear Prediction Coding (LPC) analysis. The order of the whitening filter is 16 for best results, but is reduced to 12 for medium complexity and 8 for low complexity modes. The whitened signal is analyzed to find pitch lags for which the time correlation is high. The analysis consists of three stages for reducing the complexity:
              <list style="symbols">
                <t>In the first stage, the whitened signal is downsampled to 4 kHz (from 8 kHz) and the current frame is correlated to a signal delayed by a range of lags, starting from a shortest lag corresponding to 500 Hz, to a longest lag corresponding to 56 Hz.</t>

                <t>
                  The second stage operates on a 8 kHz signal ( downsampled from 12, 16 or 24 kHz ) and measures time correlations only near the lags corresponding to those that had sufficiently high correlations in the first stage. The resulting correlations are adjusted for a small bias towards short lags to avoid ending up with a multiple of the true pitch lag. The highest adjusted correlation is compared to a threshold depending on:
                  <list style="symbols">
                    <t>
                      Whether the previous frame was classified as voiced
                    </t>
                    <t>
                      The speech activity level
                    </t>
                    <t>
                      The spectral tilt.
                    </t>
                  </list>
                  If the threshold is exceeded, the current frame is classified as voiced and the lag with the highest adjusted correlation is stored for a final pitch analysis of the highest precision in the third stage.
                </t>
                <t>
                  The last stage operates directly on the whitened input signal to compute time correlations for each of the four subframes independently in a narrow range around the lag with highest correlation from the second stage.
                </t>
              </list>
            </t>
          </section>

          <section title='Noise Shaping Analysis' anchor='noise_shaping_analysis_overview_section'>
            <t>
              The noise shaping analysis finds gains and filter coefficients used in the prefilter and noise shaping quantizer. These parameters are chosen such that they will fulfil several requirements:
              <list style="symbols">
                <t>Balancing quantization noise and bitrate. The quantization gains determine the step size between reconstruction levels of the excitation signal. Therefore, increasing the quantization gain amplifies quantization noise, but also reduces the bitrate by lowering the entropy of the quantization indices.</t>
                <t>Spectral shaping of the quantization noise; the noise shaping quantizer is capable of reducing quantization noise in some parts of the spectrum at the cost of increased noise in other parts without substantially changing the bitrate. By shaping the noise such that it follows the signal spectrum, it becomes less audible. In practice, best results are obtained by making the shape of the noise spectrum slightly flatter than the signal spectrum.</t>
                <t>Deemphasizing spectral valleys; by using different coefficients in the analysis and synthesis part of the prefilter and noise shaping quantizer, the levels of the spectral valleys can be decreased relative to the levels of the spectral peaks such as speech formants and harmonics. This reduces the entropy of the signal, which is the difference between the coded signal and the quantization noise, thus lowering the bitrate.</t>
                <t>Matching the levels of the decoded speech formants to the levels of the original speech formants; an adjustment gain and a first order tilt coefficient are computed to compensate for the effect of the noise shaping quantization on the level and spectral tilt.</t>
              </list>
            </t>
            <t>
              <figure align="center" anchor="noise_shape_analysis_spectra_figure">
                <artwork align="center">
                  <![CDATA[
  / \   ___
   |   // \\
   |  //   \\     ____
   |_//     \\___//  \\         ____
   | /  ___  \   /    \\       //  \\
 P |/  /   \  \_/      \\_____//    \\
 o |  /     \     ____  \     /      \\
 w | /       \___/    \  \___/  ____  \\___ 1
 e |/                  \       /    \  \    
 r |                    \_____/      \  \__ 2
   |                                  \     
   |                                   \___ 3
   |
   +---------------------------------------->
                    Frequency

1: Input signal spectrum
2: Deemphasized and level matched spectrum
3: Quantization noise spectrum
]]>
                </artwork>
                <postamble>Noise shaping and spectral de-emphasis illustration.</postamble>
              </figure>
              <xref target='noise_shape_analysis_spectra_figure' /> shows an example of an input signal spectrum (1). After de-emphasis and level matching, the spectrum has deeper valleys (2). The quantization noise spectrum (3) more or less follows the input signal spectrum, while having slightly less pronounced peaks. The entropy, which provides a lower bound on the bitrate for encoding the excitation signal, is proportional to the area between the deemphasized spectrum (2) and the quantization noise spectrum (3). Without de-emphasis, the entropy is proportional to the area between input spectrum (1) and quantization noise (3) - clearly higher.
            </t>

            <t>
              The transformation from input signal to deemphasized signal can be described as a filtering operation with a filter
              <figure align="center">
                <artwork align="center">
                  <![CDATA[
                                     Wana(z)
H(z) = G * ( 1 - c_tilt * z^(-1) ) * -------
                                     Wsyn(z),
            ]]>
                </artwork>
              </figure>
              having an adjustment gain G, a first order tilt adjustment filter with
              tilt coefficient c_tilt, and where
              <figure align="center">
                <artwork align="center">
                  <![CDATA[
               16                                 d
               __                                __
Wana(z) = (1 - \ (a_ana(k) * z^(-k))*(1 - z^(-L) \ b_ana(k)*z^(-k)),
               /_                                /_  
               k=1                               k=-d
            ]]>
                </artwork>
              </figure>
              is the analysis part of the de-emphasis filter, consisting of the short-term shaping filter with coefficients a_ana(k), and the long-term shaping filter with coefficients b_ana(k) and pitch lag L. The parameter d determines the number of long-term shaping filter taps.
            </t>

            <t>
              Similarly, but without the tilt adjustment, the synthesis part can be written as
              <figure align="center">
                <artwork align="center">
                  <![CDATA[
               16                                 d
               __                                __
Wsyn(z) = (1 - \ (a_syn(k) * z^(-k))*(1 - z^(-L) \ b_syn(k)*z^(-k)).
               /_                                /_  
               k=1                               k=-d
            ]]>
                </artwork>
              </figure>
            </t>
            <t>
              All noise shaping parameters are computed and applied per subframe of 5 milliseconds. First, an LPC analysis is performed on a windowed signal block of 15 milliseconds. The signal block has a look-ahead of 5 milliseconds relative to the current subframe, and the window is an asymmetric sine window. The LPC analysis is done with the autocorrelation method, with an order of 16 for best quality or 12 in low complexity operation. The quantization gain is found as the square-root of the residual energy from the LPC analysis, multiplied by a value inversely proportional to the coding quality control parameter and the pitch correlation.
            </t>
            <t>
              Next we find the two sets of short-term noise shaping coefficients a_ana(k) and a_syn(k), by applying different amounts of bandwidth expansion to the coefficients found in the LPC analysis. This bandwidth expansion moves the roots of the LPC polynomial towards the origo, using the formulas
              <figure align="center">
                <artwork align="center">
                  <![CDATA[
 a_ana(k) = a(k)*g_ana^k, and
 a_syn(k) = a(k)*g_syn^k,
            ]]>
                </artwork>
              </figure>
              where a(k) is the k'th LPC coefficient and the bandwidth expansion factors g_ana and g_syn are calculated as
              <figure align="center">
                <artwork align="center">
                  <![CDATA[
g_ana = 0.94 - 0.02*C, and
g_syn = 0.94 + 0.02*C,
            ]]>
                </artwork>
              </figure>
              where C is the coding quality control parameter between 0 and 1. Applying more bandwidth expansion to the analysis part than to the synthesis part gives the desired de-emphasis of spectral valleys in between formants.
            </t>

            <t>
              The long-term shaping is applied only during voiced frames. It uses three filter taps, described by
              <figure align="center">
                <artwork align="center">
                  <![CDATA[
b_ana = F_ana * [0.25, 0.5, 0.25], and
b_syn = F_syn * [0.25, 0.5, 0.25].
            ]]>
                </artwork>
              </figure>
              For unvoiced frames these coefficients are set to 0. The multiplication factors F_ana and F_syn are chosen between 0 and 1, depending on the coding quality control parameter, as well as the calculated pitch correlation and smoothed subband SNR of the lowest subband. By having F_ana less than F_syn, the pitch harmonics are emphasized relative to the valleys in between the harmonics.
            </t>

            <t>
              The tilt coefficient c_tilt is for unvoiced frames chosen as
              <figure align="center">
                <artwork align="center">
                  <![CDATA[
c_tilt = 0.4, and as
c_tilt = 0.04 + 0.06 * C
            ]]>
                </artwork>
              </figure>
              for voiced frames, where C again is the coding quality control parameter and is between 0 and 1.
            </t>
            <t>
              The adjustment gain G serves to correct any level mismatch between original and decoded signal that might arise from the noise shaping and de-emphasis. This gain is computed as the ratio of the prediction gain of the short-term analysis and synthesis filter coefficients. The prediction gain of an LPC synthesis filter is the square-root of the output energy when the filter is excited by a unit-energy impulse on the input. An efficient way to compute the prediction gain is by first computing the reflection coefficients from the LPC coefficients through the step-down algorithm, and extracting the prediction gain from the reflection coefficients as
              <figure align="center">
                <artwork align="center">
                  <![CDATA[
               K
              ___
 predGain = ( | | 1 - (r_k)^2 )^(-0.5),
              k=1
            ]]>
                </artwork>
              </figure>
              where r_k is the k'th reflection coefficient.
            </t>

            <t>
              Initial values for the quantization gains are computed as the square-root of the residual energy of the LPC analysis, adjusted by the coding quality control parameter. These quantization gains are later adjusted based on the results of the prediction analysis.
            </t>
          </section>

          <section title='Prefilter'>
            <t>
              In the prefilter the input signal is filtered using the spectral valley de-emphasis filter coefficients from the noise shaping analysis, see <xref target='noise_shaping_analysis_overview_section' />. By applying only the noise shaping analysis filter to the input signal, it provides the input to the noise shaping quantizer.
            </t>
          </section>
          <section title='Prediction Analysis' anchor='pred_ana_overview_section'>
            <t>
              The prediction analysis is performed in one of two ways depending on how the pitch estimator classified the frame. The processing for voiced and unvoiced speech are described in <xref target='pred_ana_voiced_overview_section' /> and <xref target='pred_ana_unvoiced_overview_section' />, respectively. Inputs to this function include the pre-whitened signal from the pitch estimator, see <xref target='pitch_estimator_overview_section' />.
            </t>

            <section title='Voiced Speech' anchor='pred_ana_voiced_overview_section'>
              <t>
                For a frame of voiced speech the pitch pulses will remain dominant in the pre-whitened input signal. Further whitening is desirable as it leads to higher quality at the same available bit-rate. To achieve this, a Long-Term Prediction (LTP) analysis is carried out to estimate the coefficients of a fifth order LTP filter for each of four sub-frames. The LTP coefficients are used to find an LTP residual signal with the simulated output signal as input to obtain better modelling of the output signal. This LTP residual signal is the input to an LPC analysis where the LPCs are estimated using Burgs method, such that the residual energy is minimized. The estimated LPCs are converted to a Line Spectral Frequency (LSF) vector, and quantized as described in <xref target='lsf_quantizer_overview_section' />. After quantization, the quantized LSF vector is converted to LPC coefficients and hence by using these quantized coefficients the encoder remains fully synchronized with the decoder. The LTP coefficients are quantized using a method described in <xref target='ltp_quantizer_overview_section' />. The quantized LPC and LTP coefficients are now used to filter the high-pass filtered input signal and measure a residual energy for each of the four subframes.
              </t>
            </section>
            <section title='Unvoiced Speech' anchor='pred_ana_unvoiced_overview_section'>
              <t>
                For a speech signal that has been classified as unvoiced there is no need for LTP filtering as it has already been determined that the pre-whitened input signal is not periodic enough within the allowed pitch period range for an LTP analysis to be worth-while the cost in terms of complexity and rate. Therefore, the pre-whitened input signal is discarded and instead the high-pass filtered input signal is used for LPC analysis using Burgs method. The resulting LPC coefficients are converted to an LSF vector, quantized as described in the following section and transformed back to obtain quantized LPC coefficients. The quantized LPC coefficients are used to filter the high-pass filtered input signal and measure a residual energy for each of the four subframes.
              </t>
            </section>
          </section>

          <section title='LSF Quantization' anchor='lsf_quantizer_overview_section'>
            <t>The purpose of quantization in general is to significantly lower the bit rate at the cost of some introduced distortion. A higher rate should always result in lower distortion, and lowering the rate will generally lead to higher distortion. A commonly used but generally sub-optimal approach is to use a quantization method with a constant rate where only the error is minimized when quantizing.</t>
            <section title='Rate-Distortion Optimization'>
              <t>Instead, we minimize an objective function that consists of a weighted sum of rate and distortion, and use a codebook with an associated non-uniform rate table. Thus, we take into account that the probability mass function for selecting the codebook entries are by no means guaranteed to be uniform in our scenario. The advantage of this approach is that it ensures that rarely used codebook vector centroids, which are modelling statistical outliers in the training set can be quantized with a low error but with a relatively high cost in terms of a high rate. At the same time this approach also provides the advantage that frequently used centroids are modelled with low error and a relatively low rate. This approach will lead to equal or lower distortion than the fixed rate codebook at any given average rate, provided that the data is similar to the data used for training the codebook.</t>
            </section>

            <section title='Error Mapping' anchor='lsf_error_mapping_overview_section'>
              <t>
                Instead of minimizing the error in the LSF domain, we map the errors to better approximate spectral distortion by applying an individual weight to each element in the error vector. The weight vectors are calculated for each input vector using the Inverse Harmonic Mean Weighting (IHMW) function proposed by Laroia et al., see <xref target="laroia-icassp" />.
                Consequently, we solve the following minimization problem, i.e.,
                <figure align="center">
                  <artwork align="center">
                    <![CDATA[
LSF_q = argmin { (LSF - c)' * W * (LSF - c) + mu * rate },
        c in C
            ]]>
                  </artwork>
                </figure>
                where LSF_q is the quantized vector, LSF is the input vector to be quantized, and c is the quantized LSF vector candidate taken from the set C of all possible outcomes of the codebook.
              </t>
            </section>
            <section title='Multi-Stage Vector Codebook'>
              <t>
                We arrange the codebook in a multiple stage structure to achieve a quantizer that is both memory efficient and highly scalable in terms of computational complexity, see e.g. <xref target="sinervo-norsig" />. In the first stage the input is the LSF vector to be quantized, and in any other stage s > 1, the input is the quantization error from the previous stage, see <xref target='lsf_quantizer_structure_overview_figure' />.
                <figure align="center" anchor="lsf_quantizer_structure_overview_figure">
                  <artwork align="center">
                    <![CDATA[
      Stage 1:           Stage 2:                Stage S:
    +----------+       +----------+            +----------+
    |  c_{1,1} |       |  c_{2,1} |            |  c_{S,1} | 
LSF +----------+ res_1 +----------+  res_{S-1} +----------+
--->|  c_{1,2} |------>|  c_{2,2} |--> ... --->|  c_{S,2} |--->
    +----------+       +----------+            +----------+ res_S =
        ...                ...                     ...      LSF-LSF_q
    +----------+       +----------+            +----------+ 
    |c_{1,M1-1}|       |c_{2,M2-1}|            |c_{S,MS-1}|
    +----------+       +----------+            +----------+     
    | c_{1,M1} |       | c_{2,M2} |            | c_{S,MS} |
    +----------+       +----------+            +----------+
]]>
                  </artwork>
                  <postamble>Multi-Stage LSF Vector Codebook Structure.</postamble>
                </figure>
              </t>

              <t>
                By storing total of M codebook vectors, i.e.,
                <figure align="center">
                  <artwork align="center">
                    <![CDATA[
     S
    __
M = \  Ms,
    /_
    s=1
]]>
                  </artwork>
                </figure>
                where M_s is the number of vectors in stage s, we obtain a total of
                <figure align="center">
                  <artwork align="center">
                    <![CDATA[
     S
    ___
T = | | Ms
    s=1
]]>
                  </artwork>
                </figure>
                possible combinations for generating the quantized vector. It is for example possible to represent 2^36 uniquely combined vectors using only 216 vectors in memory, as done in SILK for voiced speech at all sample frequencies above 8 kHz.
              </t>
            </section>
            <section title='Survivor Based Codebook Search'>
              <t>
                This number of possible combinations is far too high for a full search to be carried out for each frame so for all stages but the last, i.e., s smaller than S, only the best min( L, Ms ) centroids are carried over to stage s+1. In each stage the objective function, i.e., the weighted sum of accumulated bit-rate and distortion, is evaluated for each codebook vector entry and the results are sorted. Only the best paths and the corresponding quantization errors are considered in the next stage. In the last stage S the single best path through the multistage codebook is determined. By varying the maximum number of survivors from each stage to the next L, the complexity can be adjusted in real-time at the cost of a potential increase when evaluating the objective function for the resulting quantized vector. This approach scales all the way between the two extremes, L=1 being a greedy search, and the desirable but infeasible full search, L=T/MS. In fact, a performance almost as good as what can be achieved with the infeasible full search can be obtained at a substantially lower complexity by using this approach, see e.g. <xref target='leblanc-tsap' />.
              </t>
            </section>
            <section title='LSF Stabilization' anchor='lsf_stabilizer_overview_section'>
              <t>If the input is stable, finding the best candidate will usually result in the quantized vector also being stable, but due to the multi-stage approach it could in theory happen that the best quantization candidate is unstable and because of this there is a need to explicitly ensure that the quantized vectors are stable. Therefore we apply a LSF stabilization method which ensures that the LSF parameters are within valid range, increasingly sorted, and have minimum distances between each other and the border values that have been pre-determined as the 0.01 percentile distance values from a large training set.</t>
            </section>
            <section title='Off-Line Codebook Training'>
              <t>
                The vectors and rate tables for the multi-stage codebook have been trained by minimizing the average of the objective function for LSF vectors from a large training set.
              </t>
            </section>
          </section>

          <section title='LTP Quantization' anchor='ltp_quantizer_overview_section'>
            <t>
              For voiced frames, the prediction analysis described in <xref target='pred_ana_voiced_overview_section' /> resulted in four sets (one set per subframe) of five LTP coefficients, plus four weighting matrices. Also, the LTP coefficients for each subframe are quantized using entropy constrained vector quantization. A total of three vector codebooks are available for quantization, with different rate-distortion trade-offs. The three codebooks have 10, 20 and 40 vectors and average rates of about 3, 4, and 5 bits per vector, respectively. Consequently, the first codebook has larger average quantization distortion at a lower rate, whereas the last codebook has smaller average quantization distortion at a higher rate. Given the weighting matrix W_ltp and LTP vector b, the weighted rate-distortion measure for a codebook vector cb_i with rate r_i is give by
              <figure align="center">
                <artwork align="center">
                  <![CDATA[
 RD = u * (b - cb_i)' * W_ltp * (b - cb_i) + r_i,
]]>
                </artwork>
              </figure>
              where u is a fixed, heuristically-determined parameter balancing the distortion and rate. Which codebook gives the best performance for a given LTP vector depends on the weighting matrix for that LTP vector. For example, for a low valued W_ltp, it is advantageous to use the codebook with 10 vectors as it has a lower average rate. For a large W_ltp, on the other hand, it is often better to use the codebook with 40 vectors, as it is more likely to contain the best codebook vector.
              The weighting matrix W_ltp depends mostly on two aspects of the input signal. The first is the periodicity of the signal; the more periodic the larger W_ltp. The second is the change in signal energy in the current subframe, relative to the signal one pitch lag earlier. A decaying energy leads to a larger W_ltp than an increasing energy. Both aspects do not fluctuate very fast which causes the W_ltp matrices for different subframes of one frame often to be similar. As a result, one of the three codebooks typically gives good performance for all subframes. Therefore the codebook search for the subframe LTP vectors is constrained to only allow codebook vectors to be chosen from the same codebook, resulting in a rate reduction.
            </t>

            <t>
              To find the best codebook, each of the three vector codebooks is used to quantize all subframe LTP vectors and produce a combined weighted rate-distortion measure for each vector codebook and the vector codebook with the lowest combined rate-distortion over all subframes is chosen. The quantized LTP vectors are used in the noise shaping quantizer, and the index of the codebook plus the four indices for the four subframe codebook vectors are passed on to the range encoder.
            </t>
          </section>


          <section title='Noise Shaping Quantizer'>
            <t>
              The noise shaping quantizer independently shapes the signal and coding noise spectra to obtain a perceptually higher quality at the same bitrate.
            </t>
            <t>
              The prefilter output signal is multiplied with a compensation gain G computed in the noise shaping analysis. Then the output of a synthesis shaping filter is added, and the output of a prediction filter is subtracted to create a residual signal. The residual signal is multiplied by the inverse quantized quantization gain from the noise shaping analysis, and input to a scalar quantizer. The quantization indices of the scalar quantizer represent a signal of pulses that is input to the pyramid range encoder. The scalar quantizer also outputs a quantization signal, which is multiplied by the quantized quantization gain from the noise shaping analysis to create an excitation signal. The output of the prediction filter is added to the excitation signal to form the quantized output signal y(n). The quantized output signal y(n) is input to the synthesis shaping and prediction filters.
            </t>

          </section>

          <section title='Range Encoder'>
            <t>
              Range encoding is a well known method for entropy coding in which a bitstream sequence is continually updated with every new symbol, based on the probability for that symbol. It is similar to arithmetic coding but rather than being restricted to generating binary output symbols, it can generate symbols in any chosen number base. In SILK all side information is range encoded. Each quantized parameter has its own cumulative density function based on histograms for the quantization indices obtained by running a training database.
            </t>

            <section title='Bitstream Encoding Details'>
              <t>
                TBD.
              </t>
            </section>
          </section>
        </section>


<section title="CELT Encoder">
<t>
Copy from CELT draft.
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1691
1692
</t>

1693
1694
1695
1696
1697
1698
1699
<section anchor="prefilter" title="Pre-filter"