gcc 3.x was miscompiling this function at -O2 and above. Rearrange things
[platform/upstream/libvorbis.git] / doc / vorbis-spec-floor0.html
1 <HTML><HEAD><TITLE>xiph.org: Ogg Vorbis documentation</TITLE>
2 <BODY bgcolor="#ffffff" text="#202020" link="#006666" vlink="#000000">
3 <nobr><img src="white-ogg.png"><img src="vorbisword2.png"></nobr><p>
4
5 <h1><font color=#000070>
6 Ogg Vorbis I format specification: floor type 0 setup and decode
7 </font></h1>
8
9 <em>Last update to this document: July 19, 2002</em><br>
10
11 <h1>Overview</h1>
12
13 Vorbis floor type zero uses Line Spectral Pair [LSP, also alternately
14 known as Line Spectral Frequency or LSF] representation to encode a
15 smooth spectral envelope curve as the frequency response of the LSP
16 filter.  This representation is equivalent to a traditional all-pole
17 infinite impulse response filter as would be used in linear predictive
18 coding; LSP representation may be converted to LPC representation and
19 vice-versa.<p>
20
21 <h1>Floor 0 format</h1>
22
23 Floor zero configuration consists of six integer fields and a list of
24 VQ codebooks for use in coding/decoding the LSP filter coefficient
25 values used by each frame. <p>
26
27 <h2>header decode</h2>
28
29 Configuration information for instances of floor zero decodes from the
30 codec setup header (third packet).  configuration decode proceeds as
31 follows:<p>
32
33 <pre>
34   1) [floor0_order] = read an unsigned integer of 8 bits
35   2) [floor0_rate] = read an unsigned integer of 16 bits
36   3) [floor0_bark_map_size] = read an unsigned integer of 16 bits
37   4) [floor0_amplitude_bits] = read an unsigned integer of six bits
38   5) [floor0_amplitude_offset] = read an unsigned integer of eight bits
39   6) [floor0_number_of_books] = read an unsigned integer of four bits and add 1
40   7) if any of [floor0_order], [floor0_rate], [floor0_bark_map_size], [floor0_amplitude_bits],
41      [floor0_amplitude_offset] or [floor0_number_of_books] are less than zero, the stream is not decodable
42   8) array [floor0_book_list] = read a list of [floor0_number_of_books] unsigned integers of eight bits each;
43 </pre>
44
45 An end-of-packet condition during any of these bitstream reads renders
46 this stream undecodable.  In addition, any element of the array
47 <tt>[floor0_book_list]</tt> that is greater than the maximum codebook
48 number for this bitstream is an error condition that also renders the
49 stream undecodable.
50
51 <a name=decode>
52 <h2>packet decode</h2></a>
53
54 Extracting a floor0 curve from an audio packet consists of first
55 decoding the curve amplitude and <tt>[floor0_order]</tt> LSP
56 coefficient values from the bitstream, and then computing the floor
57 curve, which is defined as the frequency response of the decoded LSP
58 filter.<p>
59
60 Packet decode proceeds as follows:<p>
61 <pre>
62   1) [amplitude] = read an unsigned integer of [floor0_amplitude_bits] bits
63   2) if ( [amplitude] is greater than zero ) {
64        3) [coefficients] is an empty, zero length vector
65
66        4) [booknumber] = read an unsigned integer of <a href="helper.html#ilog">ilog</a>( [floor0_number_of_books] ) bits
67        5) if ( [booknumber] is greater than the highest number decode codebook ) then packet is undecodable
68        6) [lastval] = zero;
69        7) vector [temp_vector] = read vector from bitstream using codebook number [booknumber] in VQ context.
70        8) add the scalar value [last] to each scalar in vector [temp_vector]
71        9) [last] = the value of the last scalar in vector [temp_vector]
72       10) concatenate [temp_vector] onto the end of the [coefficients] vector
73       11) if (length of vector [coefficients] is less than [floor0_order], continue at step 6
74
75      }
76
77  12) done.
78  
79 </pre>
80
81 Take note of the following properties of decode:
82 <ul>
83 <li>An <tt>[amplitude]</tt> 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.<p>
84 <li>An end-of-packet condition during decode should be considered a
85 nominal occruence; if end-of-packet is reached during any read
86 operation above, floor decode is to return 'unused' status as if the
87 <tt>[amplitude]</tt> value had read zero at the beginning of decode.
88
89 <li>The book number used for decode
90 can, in fact, be stored in the bitstream in <a
91 href="helper.html#ilog">ilog</a>( <tt>[floor0_number_of_books]</tt> -
92 1 ) bits.  Nevertheless, the above specification is correct and values
93 greater than the maximum possible book value are reserved. <p>
94
95 <li>The number of scalars read into the vector <tt>[coefficients]</tt>
96 may be greater than <tt>[floor0_order]</tt>, the number actually
97 required for curve computation.  For example, if the VQ codebook used
98 for the floor currently being decoded has a
99 <tt>[codebook_dimensions]</tt> value of three and
100 <tt>[floor0_order]</tt> is ten, the only way to fill all the needed
101 scalars in <tt>[coefficients]</tt> is to to read a total of twelve
102 scalars as four vectors of three scalars each.  This is not an error
103 condition, and care must be taken not to allow a buffer overflow in
104 decode. The extra values are not used and may be ignored or discarded.
105 </ul>
106  
107 <a name=synth>
108 <h2>curve computation</h2></a>
109
110 Given an <tt>[amplitude]</tt> integer and <tt>[coefficients]</tt>
111 vector from packet decode as well as the [floor0_order],
112 [floor0_rate], [floor0_bark_map_size], [floor0_amplitude_bits] and
113 [floor0_amplitude_offset] values from floor setup, and an output
114 vector size <tt>[n]</tt> specified by the decode process, we compute a
115 floor output vector.<p>
116
117 If the value <tt>[amplitude]</tt> is zero, the return value is a
118 length <tt>[n]</tt> vector with all-zero scalars.  Otherwise, begin by
119 assuming the following definitions for the given vector to be
120 synthesized:<p>
121
122 <img src="lspmap.png"><p>
123
124 The above is used to synthesize the LSP curve on a Bark-scale frequency
125 axis, then map the result to a linear-scale frequency axis.
126 Similarly, the below calculation synthesizes the output LSP curve [output] on a log
127 (dB) amplitude scale, mapping it to linear amplitude in the last step:<p>
128
129 <pre>
130   1) [i] = 0
131   2) if ( [floor0_order] is odd ) {
132
133        3) calculate [p] and [q] according to: <br>
134           <img src="oddlsp.png">
135
136      } else [floor0_order] is even {
137
138        4) calculate [p] and [q] according to: <br>
139           <img src="evenlsp.png">
140
141      }
142
143   5) calculate [linear_floor_value] according to:<br>
144      <img src="floorval.png">
145
146   6) [iteration_condition] = map element [i]
147   7) [output] element [i] = [linear_floor_value]
148   8) increment [i]
149   9) if ( map element [i] is equal to [iteration_condition] ) continue at step 7
150  10) if ( [i] is less than [n] ) continue at step 2
151  11) done
152
153 </pre>
154
155 <hr>
156 <a href="http://www.xiph.org/">
157 <img src="white-xifish.png" align=left border=0>
158 </a>
159 <font size=-2 color=#505050>
160
161 Ogg is a <a href="http://www.xiph.org">Xiph.org Foundation</a> effort
162 to protect essential tenets of Internet multimedia from corporate
163 hostage-taking; Open Source is the net's greatest tool to keep
164 everyone honest. See <a href="http://www.xiph.org/about.html">About
165 the Xiph.org Foundation</a> for details.
166 <p>
167
168 Ogg Vorbis is the first Ogg audio CODEC.  Anyone may freely use and
169 distribute the Ogg and Vorbis specification, whether in a private,
170 public or corporate capacity.  However, the Xiph.org Foundation and
171 the Ogg project (xiph.org) reserve the right to set the Ogg Vorbis
172 specification and certify specification compliance.<p>
173
174 Xiph.org's Vorbis software CODEC implementation is distributed under a
175 BSD-like license.  This does not restrict third parties from
176 distributing independent implementations of Vorbis software under
177 other licenses.<p>
178
179 Ogg, Vorbis, Xiph.org Foundation and their logos are trademarks (tm)
180 of the <a href="http://www.xiph.org/">Xiph.org Foundation</a>.  These
181 pages are copyright (C) 1994-2002 Xiph.org Foundation. All rights
182 reserved.<p>
183
184 </body>
185