c78afe06beee804540cd546b64d4df7d3113b405
[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) array [floor0_book_list] = read a list of [floor0_number_of_books] unsigned integers of eight bits each;
53 </screen>
54
55 <para>
56 An end-of-packet condition during any of these bitstream reads renders
57 this stream undecodable.  In addition, any element of the array
58 <varname>[floor0_book_list]</varname> that is greater than the maximum codebook
59 number for this bitstream is an error condition that also renders the
60 stream undecodable.</para>
61
62 </section>
63
64 <section id="vorbis-spec-floor0-decode">
65 <title>packet decode</title>
66
67 <para>
68 Extracting a floor0 curve from an audio packet consists of first
69 decoding the curve amplitude and <varname>[floor0_order]</varname> LSP
70 coefficient values from the bitstream, and then computing the floor
71 curve, which is defined as the frequency response of the decoded LSP
72 filter.</para>
73
74 <para>
75 Packet decode proceeds as follows:</para>
76 <screen>
77   1) [amplitude] = read an unsigned integer of [floor0_amplitude_bits] bits
78   2) if ( [amplitude] is greater than zero ) {
79        3) [coefficients] is an empty, zero length vector
80        4) [booknumber] = read an unsigned integer of <link linkend="vorbis-spec-ilog">ilog</link>( [floor0_number_of_books] ) bits
81        5) if ( [booknumber] is greater than the highest number decode codebook ) then packet is undecodable
82        6) [last] = zero;
83        7) vector [temp_vector] = read vector from bitstream using codebook number [floor0_book_list] element [booknumber] in VQ context.
84        8) add the scalar value [last] to each scalar in vector [temp_vector]
85        9) [last] = the value of the last scalar in vector [temp_vector]
86       10) concatenate [temp_vector] onto the end of the [coefficients] vector
87       11) if (length of vector [coefficients] is less than [floor0_order], continue at step 6
88
89      }
90
91  12) done.
92  
93 </screen>
94
95 <para>
96 Take note of the following properties of decode:
97 <itemizedlist>
98  <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>
99  <listitem><simpara>An end-of-packet condition during decode should be considered a
100 nominal occruence; if end-of-packet is reached during any read
101 operation above, floor decode is to return 'unused' status as if the
102 <varname>[amplitude]</varname> value had read zero at the beginning of decode.</simpara></listitem>
103
104  <listitem><simpara>The book number used for decode
105 can, in fact, be stored in the bitstream in <link linkend="vorbis-spec-ilog">ilog</link>( <varname>[floor0_number_of_books]</varname> -
106 1 ) bits.  Nevertheless, the above specification is correct and values
107 greater than the maximum possible book value are reserved.</simpara></listitem>
108
109  <listitem><simpara>The number of scalars read into the vector <varname>[coefficients]</varname>
110 may be greater than <varname>[floor0_order]</varname>, the number actually
111 required for curve computation.  For example, if the VQ codebook used
112 for the floor currently being decoded has a
113 <varname>[codebook_dimensions]</varname> value of three and
114 <varname>[floor0_order]</varname> is ten, the only way to fill all the needed
115 scalars in <varname>[coefficients]</varname> is to to read a total of twelve
116 scalars as four vectors of three scalars each.  This is not an error
117 condition, and care must be taken not to allow a buffer overflow in
118 decode. The extra values are not used and may be ignored or discarded.</simpara></listitem>
119 </itemizedlist>
120 </para>
121
122 </section>
123
124 <section id="vorbis-spec-floor0-synth">
125 <title>curve computation</title>
126
127 <para>
128 Given an <varname>[amplitude]</varname> integer and <varname>[coefficients]</varname>
129 vector from packet decode as well as the [floor0_order],
130 [floor0_rate], [floor0_bark_map_size], [floor0_amplitude_bits] and
131 [floor0_amplitude_offset] values from floor setup, and an output
132 vector size <varname>[n]</varname> specified by the decode process, we compute a
133 floor output vector.</para>
134
135 <para>
136 If the value <varname>[amplitude]</varname> is zero, the return value is a
137 length <varname>[n]</varname> vector with all-zero scalars.  Otherwise, begin by
138 assuming the following definitions for the given vector to be
139 synthesized:</para>
140
141 <informalequation>
142  <mediaobject>
143   <textobject><phrase>[lsp map equation]</phrase></textobject>
144   <textobject role="tex"><phrase>
145    <![CDATA[
146    \begin{math}
147      \mathrm{map}_i = \left\{
148        \begin{array}{ll}
149           \min (  
150             \mathtt{floor0\_bark\_map\_size} - 1,
151             foobar
152           ) & \textrm{for } i \in [0,n-1] \\
153           -1 & \textrm{for } i = n
154         \end{array}
155       \right.
156     \end {math}
157     
158     where
159     
160     \begin{math}
161     foobar = 
162       \left\lfloor
163         \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})} 
164       \right\rfloor
165     \end{math}
166                   
167     and
168                 
169     \begin{math}
170       \mathrm{bark}(x) = 13.1 \arctan (.00074x) + 2.24 \arctan (.0000000185x^2 + .0001x)
171     \end{math}
172     ]]>
173    </phrase></textobject>
174   <imageobject><imagedata  fileref="lspmap.png"/></imageobject>
175  </mediaobject>
176 </informalequation>
177
178 <para>
179 The above is used to synthesize the LSP curve on a Bark-scale frequency
180 axis, then map the result to a linear-scale frequency axis.
181 Similarly, the below calculation synthesizes the output LSP curve <varname>[output]</varname> on a log
182 (dB) amplitude scale, mapping it to linear amplitude in the last step:</para>
183
184 <orderedlist>
185  <listitem><simpara> <varname>[i]</varname> = 0 </simpara></listitem>
186  <listitem><para>if ( <varname>[floor0_order]</varname> is odd ) {
187   <orderedlist>
188    <listitem><para>calculate <varname>[p]</varname> and <varname>[q]</varname> according to:
189         <informalequation>
190          <mediaobject>
191           <textobject><phrase>[equation for odd lsp]</phrase></textobject>
192           <textobject role="tex"><phrase>
193            <![CDATA[
194            \begin{eqnarray*}
195              p & = & (1 - \cos^2\omega)\prod_{j=0}^{(\mathtt{order}-3)/2} 4 (\cos c_{2j+1} - \cos \omega)^2 \\
196              q & = & \frac{1}{4} \prod_{j=0}^{(\mathtt{order}-1)/2} 4 (\cos c_{2j+1} - \cos \omega)^2
197            \end{eqnarray*}
198            ]]>
199           </phrase></textobject>
200           <imageobject><imagedata fileref="oddlsp.png"/></imageobject>
201          </mediaobject>
202         </informalequation>
203    </para></listitem>
204   </orderedlist>
205   } else <varname>[floor0_order]</varname> is even {
206   <orderedlist>
207    <listitem><para>calculate <varname>[p]</varname> and <varname>[q]</varname> according to:
208         <informalequation>
209          <mediaobject>
210           <textobject><phrase>[equation for even lsp]</phrase></textobject>
211           <textobject role="tex"><phrase>
212            <![CDATA[
213            \begin{eqnarray*}
214              p & = & \frac{(1 - \cos^2\omega)}{2} \prod_{j=0}^{(\mathtt{order}-2)/2} 4 (\cos c_{2j} - \cos \omega)^2 \\
215              q & = & \frac{(1 + \cos^2\omega)}{2} \prod_{j=0}^{(\mathtt{order}-2)/2} 4 (\cos c_{2j} - \cos \omega)^2
216            \end{eqnarray*}
217            ]]>
218           </phrase></textobject>
219           <imageobject><imagedata fileref="evenlsp.png"/></imageobject>
220          </mediaobject>
221         </informalequation>
222    </para></listitem>
223   </orderedlist> 
224   }
225  </para></listitem>
226  <listitem><para>calculate <varname>[linear_floor_value]</varname> according to:
227      <informalequation>
228       <mediaobject>
229        <textobject><phrase>[expression for floorval]</phrase></textobject>
230        <textobject role="tex"><phrase>
231          <![CDATA[
232          \begin{math}
233            \exp \left( .11512925 \left(\frac{\mathtt{amplitude} \cdot \mathtt{floor0\_amplitute\_offset}}{(2^{\mathtt{floor0\_amplitude\_bits}}-1)\sqrt{p+q}}
234                   - \mathtt{floor0\_amplitude\_offset} \right) \right)
235          \end{math}
236          ]]>
237        </phrase></textobject>
238        <imageobject><imagedata fileref="floorval.png"/></imageobject>
239       </mediaobject>
240      </informalequation>
241  </para></listitem>
242  <listitem><simpara><varname>[iteration_condition]</varname> = map element <varname>[i]</varname></simpara></listitem>
243  <listitem><simpara><varname>[output]</varname> element <varname>[i]</varname> = <varname>[linear_floor_value]</varname></simpara></listitem>
244  <listitem><simpara>increment <varname>[i]</varname></simpara></listitem>
245  <listitem><simpara>if ( map element <varname>[i]</varname> is equal to <varname>[iteration_condition]</varname> ) continue at step 5</simpara></listitem>
246  <listitem><simpara>if ( <varname>[i]</varname> is less than <varname>[n]</varname> ) continue at step 2</simpara></listitem>
247  <listitem><simpara>done</simpara></listitem>
248 </orderedlist>
249
250 </section>
251
252 </section>
253
254 </section>
255