6ad6367feeb120bc3bd0ec9f76c9ee9194bd21b2
[platform/upstream/libvorbis.git] / doc / xml / 06-floor0.xml
1 <?xml version="1.0" standalone="no"?>
2 <!DOCTYPE section PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
3                 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" [
4
5 ]>
6
7 <section id="vorbis-spec-floor0">
8 <sectioninfo>
9 <releaseinfo>
10   $Id$
11 </releaseinfo>
12 </sectioninfo>  
13 <title>Floor type 0 setup and decode</title>
14
15
16 <section>
17 <title>Overview</title>
18
19 <para>
20 Vorbis floor type zero uses Line Spectral Pair (LSP, also alternately
21 known as Line Spectral Frequency or LSF) representation to encode a
22 smooth spectral envelope curve as the frequency response of the LSP
23 filter.  This representation is equivalent to a traditional all-pole
24 infinite impulse response filter as would be used in linear predictive
25 coding; LSP representation may be converted to LPC representation and
26 vice-versa.</para>
27
28 </section>
29
30 <section>
31 <title>Floor 0 format</title>
32
33 <para>
34 Floor zero configuration consists of six integer fields and a list of
35 VQ codebooks for use in coding/decoding the LSP filter coefficient
36 values used by each frame. </para>
37
38 <section><title>header decode</title>
39
40 <para>
41 Configuration information for instances of floor zero decodes from the
42 codec setup header (third packet).  configuration decode proceeds as
43 follows:</para>
44
45 <screen>
46   1) [floor0_order] = read an unsigned integer of 8 bits
47   2) [floor0_rate] = read an unsigned integer of 16 bits
48   3) [floor0_bark_map_size] = read an unsigned integer of 16 bits
49   4) [floor0_amplitude_bits] = read an unsigned integer of six bits
50   5) [floor0_amplitude_offset] = read an unsigned integer of eight bits
51   6) [floor0_number_of_books] = read an unsigned integer of four bits and add 1
52   7) if any of [floor0_order], [floor0_rate], [floor0_bark_map_size], [floor0_amplitude_bits],
53      [floor0_amplitude_offset] or [floor0_number_of_books] are less than zero, the stream is not decodable
54   8) array [floor0_book_list] = read a list of [floor0_number_of_books] unsigned integers of eight bits each;
55 </screen>
56
57 <para>
58 An end-of-packet condition during any of these bitstream reads renders
59 this stream undecodable.  In addition, any element of the array
60 <varname>[floor0_book_list]</varname> that is greater than the maximum codebook
61 number for this bitstream is an error condition that also renders the
62 stream undecodable.</para>
63
64 </section>
65
66 <section id="vorbis-spec-floor0-decode">
67 <title>packet decode</title>
68
69 <para>
70 Extracting a floor0 curve from an audio packet consists of first
71 decoding the curve amplitude and <varname>[floor0_order]</varname> LSP
72 coefficient values from the bitstream, and then computing the floor
73 curve, which is defined as the frequency response of the decoded LSP
74 filter.</para>
75
76 <para>
77 Packet decode proceeds as follows:</para>
78 <screen>
79   1) [amplitude] = read an unsigned integer of [floor0_amplitude_bits] bits
80   2) if ( [amplitude] is greater than zero ) {
81        3) [coefficients] is an empty, zero length vector
82
83        4) [booknumber] = read an unsigned integer of <link linkend="vorbis-spec-ilog">ilog</link>( [floor0_number_of_books] ) bits
84        5) if ( [booknumber] is greater than the highest number decode codebook ) then packet is undecodable
85        6) [last] = zero;
86        7) vector [temp_vector] = read vector from bitstream using codebook number [booknumber] in VQ context.
87        8) add the scalar value [last] to each scalar in vector [temp_vector]
88        9) [last] = the value of the last scalar in vector [temp_vector]
89       10) concatenate [temp_vector] onto the end of the [coefficients] vector
90       11) if (length of vector [coefficients] is less than [floor0_order], continue at step 6
91
92      }
93
94  12) done.
95  
96 </screen>
97
98 <para>
99 Take note of the following properties of decode:
100 <itemizedlist>
101  <listitem><simpara>An <varname>[amplitude]</varname> value of zero must result in a return code that indicates this channel is unused in this frame (the output of the channel will be all-zeroes in synthesis).  Several later stages of decode don't occur for an unused channel.</simpara></listitem>
102  <listitem><simpara>An end-of-packet condition during decode should be considered a
103 nominal occruence; if end-of-packet is reached during any read
104 operation above, floor decode is to return 'unused' status as if the
105 <varname>[amplitude]</varname> value had read zero at the beginning of decode.</simpara></listitem>
106
107  <listitem><simpara>The book number used for decode
108 can, in fact, be stored in the bitstream in <link linkend="vorbis-spec-ilog">ilog</link>( <varname>[floor0_number_of_books]</varname> -
109 1 ) bits.  Nevertheless, the above specification is correct and values
110 greater than the maximum possible book value are reserved.</simpara></listitem>
111
112  <listitem><simpara>The number of scalars read into the vector <varname>[coefficients]</varname>
113 may be greater than <varname>[floor0_order]</varname>, the number actually
114 required for curve computation.  For example, if the VQ codebook used
115 for the floor currently being decoded has a
116 <varname>[codebook_dimensions]</varname> value of three and
117 <varname>[floor0_order]</varname> is ten, the only way to fill all the needed
118 scalars in <varname>[coefficients]</varname> is to to read a total of twelve
119 scalars as four vectors of three scalars each.  This is not an error
120 condition, and care must be taken not to allow a buffer overflow in
121 decode. The extra values are not used and may be ignored or discarded.</simpara></listitem>
122 </itemizedlist>
123 </para>
124
125 </section>
126
127 <section id="vorbis-spec-floor0-synth">
128 <title>curve computation</title>
129
130 <para>
131 Given an <varname>[amplitude]</varname> integer and <varname>[coefficients]</varname>
132 vector from packet decode as well as the [floor0_order],
133 [floor0_rate], [floor0_bark_map_size], [floor0_amplitude_bits] and
134 [floor0_amplitude_offset] values from floor setup, and an output
135 vector size <varname>[n]</varname> specified by the decode process, we compute a
136 floor output vector.</para>
137
138 <para>
139 If the value <varname>[amplitude]</varname> is zero, the return value is a
140 length <varname>[n]</varname> vector with all-zero scalars.  Otherwise, begin by
141 assuming the following definitions for the given vector to be
142 synthesized:</para>
143
144 <informalequation>
145  <mediaobject>
146   <textobject><phrase>[lsp map equation]</phrase></textobject>
147   <textobject role="tex"><phrase>
148    <![CDATA[
149    \begin{math}
150      \mathrm{map}_i = \left\{
151        \begin{array}{ll}
152           \min (  
153             \mathtt{floor0\_bark\_map\_size} - 1,
154             foobar
155           ) & \textrm{for } i \in [0,n-1] \\
156           -1 & \textrm{for } i = n
157         \end{array}
158       \right.
159     \end {math}
160     
161     where
162     
163     \begin{math}
164     foobar = 
165       \left\lfloor
166         \mathrm{bark}\left(\frac{\mathtt{floor0\_rate} \cdot i}{2n}\right) \cdot \frac{\mathtt{floor0\_bark\_map\_size}} {\mathrm{bark}(.5 \cdot \mathtt{floor0\_rate})} 
167       \right\rfloor
168     \end{math}
169                   
170     and
171                 
172     \begin{math}
173       \mathrm{bark}(x) = 13.1 \arctan (.00074x) + 2.24 \arctan (.0000000158x^2)+.0001x
174     \end{math}
175     ]]>
176    </phrase></textobject>
177   <imageobject><imagedata  fileref="lspmap.png"/></imageobject>
178  </mediaobject>
179 </informalequation>
180
181 <para>
182 The above is used to synthesize the LSP curve on a Bark-scale frequency
183 axis, then map the result to a linear-scale frequency axis.
184 Similarly, the below calculation synthesizes the output LSP curve <varname>[output]</varname> on a log
185 (dB) amplitude scale, mapping it to linear amplitude in the last step:</para>
186
187 <orderedlist>
188  <listitem><simpara> <varname>[i]</varname> = 0 </simpara></listitem>
189  <listitem><para>if ( <varname>[floor0_order]</varname> is odd ) {
190   <orderedlist>
191    <listitem><para>calculate <varname>[p]</varname> and <varname>[q]</varname> according to:
192         <informalequation>
193          <mediaobject>
194           <textobject><phrase>[equation for odd lsp]</phrase></textobject>
195           <textobject role="tex"><phrase>
196            <![CDATA[
197            \begin{eqnarray*}
198              p & = & (1 - \cos^2\omega)\prod_{j=0}^{(\mathtt{order}-3)/2} 4 (\cos c_{2j+1} - \cos \omega)^2 \\
199              q & = & \frac{1}{4} \prod_{j=0}^{(\mathtt{order}-1)/2} 4 (\cos c_{2j+1} - \cos \omega)^2
200            \end{eqnarray*}
201            ]]>
202           </phrase></textobject>
203           <imageobject><imagedata fileref="oddlsp.png"/></imageobject>
204          </mediaobject>
205         </informalequation>
206    </para></listitem>
207   </orderedlist>
208   } else <varname>[floor0_order]</varname> is even {
209   <orderedlist>
210    <listitem><para>calculate <varname>[p]</varname> and <varname>[q]</varname> according to:
211         <informalequation>
212          <mediaobject>
213           <textobject><phrase>[equation for even lsp]</phrase></textobject>
214           <textobject role="tex"><phrase>
215            <![CDATA[
216            \begin{eqnarray*}
217              p & = & \frac{(1 - \cos^2\omega)}{2} \prod_{j=0}^{(\mathtt{order}-2)/2} 4 (\cos c_{2j} - \cos \omega)^2 \\
218              q & = & \frac{(1 + \cos^2\omega)}{2} \prod_{j=0}^{(\mathtt{order}-2)/2} 4 (\cos c_{2j} - \cos \omega)^2
219            \end{eqnarray*}
220            ]]>
221           </phrase></textobject>
222           <imageobject><imagedata fileref="evenlsp.png"/></imageobject>
223          </mediaobject>
224         </informalequation>
225    </para></listitem>
226   </orderedlist> 
227   }
228  </para></listitem>
229  <listitem><para>calculate <varname>[linear_floor_value]</varname> according to:
230      <informalequation>
231       <mediaobject>
232        <textobject><phrase>[expression for floorval]</phrase></textobject>
233        <textobject role="tex"><phrase>
234          <![CDATA[
235          \begin{math}
236            \exp \left( .11512925 \left(\frac{\mathtt{amplitude} \cdot \mathtt{floor0\_amplitute\_offset}}{(2^{\mathtt{floor0\_amplitude\_bits}}-1)\sqrt{p+q}}
237                   - \mathtt{floor0\_amplitude\_offset} \right) \right)
238          \end{math}
239          ]]>
240        </phrase></textobject>
241        <imageobject><imagedata fileref="floorval.png"/></imageobject>
242       </mediaobject>
243      </informalequation>
244  </para></listitem>
245  <listitem><simpara><varname>[iteration_condition]</varname> = map element <varname>[i]</varname></simpara></listitem>
246  <listitem><simpara><varname>[output]</varname> element <varname>[i]</varname> = <varname>[linear_floor_value]</varname></simpara></listitem>
247  <listitem><simpara>increment <varname>[i]</varname></simpara></listitem>
248  <listitem><simpara>if ( map element <varname>[i]</varname> is equal to <varname>[iteration_condition]</varname> ) continue at step 5</simpara></listitem>
249  <listitem><simpara>if ( <varname>[i]</varname> is less than <varname>[n]</varname> ) continue at step 2</simpara></listitem>
250  <listitem><simpara>done</simpara></listitem>
251 </orderedlist>
252
253 </section>
254
255 </section>
256
257 </section>
258