Vorbis_I_spec.html 167 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293
  1. <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Vorbis I specification</title><meta name="generator" content="DocBook XSL Stylesheets V1.71.0"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="article" lang="en"><div class="titlepage"><div><div><h1 class="title"><a name="id291327"></a>Vorbis I specification</h1></div><div><h3 class="corpauthor">Xiph.org Foundation</h3></div></div><hr></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="#vorbis-spec-intro">1. Introduction and Description</a></span></dt><dd><dl><dt><span class="section"><a href="#id311592">1.1. Overview</a></span></dt><dt><span class="section"><a href="#id258770">1.2. Decoder Configuration</a></span></dt><dt><span class="section"><a href="#id258461">1.3. High-level Decode Process</a></span></dt></dl></dd><dt><span class="section"><a href="#vorbis-spec-bitpacking">2. Bitpacking Convention</a></span></dt><dd><dl><dt><span class="section"><a href="#id304831">2.1. Overview</a></span></dt></dl></dd><dt><span class="section"><a href="#vorbis-spec-codebook">3. Probability Model and Codebooks</a></span></dt><dd><dl><dt><span class="section"><a href="#id310158">3.1. Overview</a></span></dt><dt><span class="section"><a href="#id310216">3.2. Packed codebook format</a></span></dt><dt><span class="section"><a href="#id316518">3.3. Use of the codebook abstraction</a></span></dt></dl></dd><dt><span class="section"><a href="#vorbis-spec-codec">4. Codec Setup and Packet Decode</a></span></dt><dd><dl><dt><span class="section"><a href="#id336024">4.1. Overview</a></span></dt><dt><span class="section"><a href="#id326710">4.2. Header decode and decode setup</a></span></dt><dt><span class="section"><a href="#id342709">4.3. Audio packet decode and synthesis</a></span></dt></dl></dd><dt><span class="section"><a href="#vorbis-spec-comment">5. comment field and header specification</a></span></dt><dd><dl><dt><span class="section"><a href="#id314030">5.1. Overview</a></span></dt><dt><span class="section"><a href="#id314058">5.2. Comment encoding</a></span></dt></dl></dd><dt><span class="section"><a href="#vorbis-spec-floor0">6. Floor type 0 setup and decode</a></span></dt><dd><dl><dt><span class="section"><a href="#id336814">6.1. Overview</a></span></dt><dt><span class="section"><a href="#id321046">6.2. Floor 0 format</a></span></dt></dl></dd><dt><span class="section"><a href="#vorbis-spec-floor1">7. Floor type 1 setup and decode</a></span></dt><dd><dl><dt><span class="section"><a href="#id336243">7.1. Overview</a></span></dt><dt><span class="section"><a href="#id334800">7.2. Floor 1 format</a></span></dt></dl></dd><dt><span class="section"><a href="#vorbis-spec-residue">8. Residue setup and decode</a></span></dt><dd><dl><dt><span class="section"><a href="#id320982">8.1. Overview</a></span></dt><dt><span class="section"><a href="#id307154">8.2. Residue format</a></span></dt><dt><span class="section"><a href="#id326310">8.3. residue 0</a></span></dt><dt><span class="section"><a href="#id326344">8.4. residue 1</a></span></dt><dt><span class="section"><a href="#id334893">8.5. residue 2</a></span></dt><dt><span class="section"><a href="#id334939">8.6. Residue decode</a></span></dt></dl></dd><dt><span class="section"><a href="#vorbis-spec-helper">9. Helper equations</a></span></dt><dd><dl><dt><span class="section"><a href="#id316603">9.1. Overview</a></span></dt><dt><span class="section"><a href="#id317505">9.2. Functions</a></span></dt></dl></dd><dt><span class="section"><a href="#vorbis-spec-tables">10. Tables</a></span></dt><dd><dl><dt><span class="section"><a href="#vorbis-spec-floor1_inverse_dB_table">10.1. floor1_inverse_dB_table</a></span></dt></dl></dd><dt><span class="appendix"><a href="#vorbis-over-ogg">1. Embedding Vorbis into an Ogg stream</a></span></dt><dd><dl><dt><span class="section"><a href="#id319760">1.1. Overview</a></span></dt><dd><dl><dt><span class="section"><a href="#id336562">1.1.1. Restrictions</a></span></dt><dt><span class="section"><a href="#id330723">1.1.2. MIME type</a></span></dt></dl></dd><dt><span class="section"><a href="#id328095">1.2. Encapsulation</a></span></dt></dl></dd><dt><span class="appendix"><a href="#vorbis-over-rtp">2. Vorbis encapsulation in RTP</a></span></dt><dt><span class="appendix"><a href="#footer">3. Colophon</a></span></dt></dl></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="vorbis-spec-intro"></a>1. Introduction and Description</h2></div><div><p class="releaseinfo">
  2. $Id: 01-introduction.xml 7186 2004-07-20 07:19:25Z xiphmont $
  3. </p></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id311592"></a>1.1. Overview</h3></div></div></div><p>
  4. This document provides a high level description of the Vorbis codec's
  5. construction. A bit-by-bit specification appears beginning in
  6. <a href="#vorbis-spec-codec" title="4. Codec Setup and Packet Decode">Section 4, &#8220;Codec Setup and Packet Decode&#8221;</a>.
  7. The later sections assume a high-level
  8. understanding of the Vorbis decode process, which is
  9. provided here.</p><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id317198"></a>1.1.1. Application</h4></div></div></div><p>
  10. Vorbis is a general purpose perceptual audio CODEC intended to allow
  11. maximum encoder flexibility, thus allowing it to scale competitively
  12. over an exceptionally wide range of bitrates. At the high
  13. quality/bitrate end of the scale (CD or DAT rate stereo, 16/24 bits)
  14. it is in the same league as MPEG-2 and MPC. Similarly, the 1.0
  15. encoder can encode high-quality CD and DAT rate stereo at below 48kbps
  16. without resampling to a lower rate. Vorbis is also intended for
  17. lower and higher sample rates (from 8kHz telephony to 192kHz digital
  18. masters) and a range of channel representations (monaural,
  19. polyphonic, stereo, quadraphonic, 5.1, ambisonic, or up to 255
  20. discrete channels).
  21. </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id315630"></a>1.1.2. Classification</h4></div></div></div><p>
  22. Vorbis I is a forward-adaptive monolithic transform CODEC based on the
  23. Modified Discrete Cosine Transform. The codec is structured to allow
  24. addition of a hybrid wavelet filterbank in Vorbis II to offer better
  25. transient response and reproduction using a transform better suited to
  26. localized time events.
  27. </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id323943"></a>1.1.3. Assumptions</h4></div></div></div><p>
  28. The Vorbis CODEC design assumes a complex, psychoacoustically-aware
  29. encoder and simple, low-complexity decoder. Vorbis decode is
  30. computationally simpler than mp3, although it does require more
  31. working memory as Vorbis has no static probability model; the vector
  32. codebooks used in the first stage of decoding from the bitstream are
  33. packed in their entirety into the Vorbis bitstream headers. In
  34. packed form, these codebooks occupy only a few kilobytes; the extent
  35. to which they are pre-decoded into a cache is the dominant factor in
  36. decoder memory usage.
  37. </p><p>
  38. Vorbis provides none of its own framing, synchronization or protection
  39. against errors; it is solely a method of accepting input audio,
  40. dividing it into individual frames and compressing these frames into
  41. raw, unformatted 'packets'. The decoder then accepts these raw
  42. packets in sequence, decodes them, synthesizes audio frames from
  43. them, and reassembles the frames into a facsimile of the original
  44. audio stream. Vorbis is a free-form variable bit rate (VBR) codec and packets have no
  45. minimum size, maximum size, or fixed/expected size. Packets
  46. are designed that they may be truncated (or padded) and remain
  47. decodable; this is not to be considered an error condition and is used
  48. extensively in bitrate management in peeling. Both the transport
  49. mechanism and decoder must allow that a packet may be any size, or
  50. end before or after packet decode expects.</p><p>
  51. Vorbis packets are thus intended to be used with a transport mechanism
  52. that provides free-form framing, sync, positioning and error correction
  53. in accordance with these design assumptions, such as Ogg (for file
  54. transport) or RTP (for network multicast). For purposes of a few
  55. examples in this document, we will assume that Vorbis is to be
  56. embedded in an Ogg stream specifically, although this is by no means a
  57. requirement or fundamental assumption in the Vorbis design.</p><p>
  58. The specification for embedding Vorbis into
  59. an Ogg transport stream is in <a href="#vorbis-over-ogg" title="1. Embedding Vorbis into an Ogg stream">Appendix 1, <i>Embedding Vorbis into an Ogg stream</i></a>.
  60. </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id324282"></a>1.1.4. Codec Setup and Probability Model</h4></div></div></div><p>
  61. Vorbis' heritage is as a research CODEC and its current design
  62. reflects a desire to allow multiple decades of continuous encoder
  63. improvement before running out of room within the codec specification.
  64. For these reasons, configurable aspects of codec setup intentionally
  65. lean toward the extreme of forward adaptive.</p><p>
  66. The single most controversial design decision in Vorbis (and the most
  67. unusual for a Vorbis developer to keep in mind) is that the entire
  68. probability model of the codec, the Huffman and VQ codebooks, is
  69. packed into the bitstream header along with extensive CODEC setup
  70. parameters (often several hundred fields). This makes it impossible,
  71. as it would be with MPEG audio layers, to embed a simple frame type
  72. flag in each audio packet, or begin decode at any frame in the stream
  73. without having previously fetched the codec setup header.
  74. </p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
  75. Vorbis <span class="emphasis"><em>can</em></span> initiate decode at any arbitrary packet within a
  76. bitstream so long as the codec has been initialized/setup with the
  77. setup headers.</p></div><p>
  78. Thus, Vorbis headers are both required for decode to begin and
  79. relatively large as bitstream headers go. The header size is
  80. unbounded, although for streaming a rule-of-thumb of 4kB or less is
  81. recommended (and Xiph.Org's Vorbis encoder follows this suggestion).</p><p>
  82. Our own design work indicates the primary liability of the
  83. required header is in mindshare; it is an unusual design and thus
  84. causes some amount of complaint among engineers as this runs against
  85. current design trends (and also points out limitations in some
  86. existing software/interface designs, such as Windows' ACM codec
  87. framework). However, we find that it does not fundamentally limit
  88. Vorbis' suitable application space.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id258744"></a>1.1.5. Format Specification</h4></div></div></div><p>
  89. The Vorbis format is well-defined by its decode specification; any
  90. encoder that produces packets that are correctly decoded by the
  91. reference Vorbis decoder described below may be considered a proper
  92. Vorbis encoder. A decoder must faithfully and completely implement
  93. the specification defined below (except where noted) to be considered
  94. a proper Vorbis decoder.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id258756"></a>1.1.6. Hardware Profile</h4></div></div></div><p>
  95. Although Vorbis decode is computationally simple, it may still run
  96. into specific limitations of an embedded design. For this reason,
  97. embedded designs are allowed to deviate in limited ways from the
  98. 'full' decode specification yet still be certified compliant. These
  99. optional omissions are labelled in the spec where relevant.</p></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id258770"></a>1.2. Decoder Configuration</h3></div></div></div><p>
  100. Decoder setup consists of configuration of multiple, self-contained
  101. component abstractions that perform specific functions in the decode
  102. pipeline. Each different component instance of a specific type is
  103. semantically interchangeable; decoder configuration consists both of
  104. internal component configuration, as well as arrangement of specific
  105. instances into a decode pipeline. Componentry arrangement is roughly
  106. as follows:</p><div class="mediaobject"><img src="components.png" alt="decoder pipeline configuration"></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id258803"></a>1.2.1. Global Config</h4></div></div></div><p>
  107. Global codec configuration consists of a few audio related fields
  108. (sample rate, channels), Vorbis version (always '0' in Vorbis I),
  109. bitrate hints, and the lists of component instances. All other
  110. configuration is in the context of specific components.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id258815"></a>1.2.2. Mode</h4></div></div></div><p>
  111. Each Vorbis frame is coded according to a master 'mode'. A bitstream
  112. may use one or many modes.</p><p>
  113. The mode mechanism is used to encode a frame according to one of
  114. multiple possible methods with the intention of choosing a method best
  115. suited to that frame. Different modes are, e.g. how frame size
  116. is changed from frame to frame. The mode number of a frame serves as a
  117. top level configuration switch for all other specific aspects of frame
  118. decode.</p><p>
  119. A 'mode' configuration consists of a frame size setting, window type
  120. (always 0, the Vorbis window, in Vorbis I), transform type (always
  121. type 0, the MDCT, in Vorbis I) and a mapping number. The mapping
  122. number specifies which mapping configuration instance to use for
  123. low-level packet decode and synthesis.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id258359"></a>1.2.3. Mapping</h4></div></div></div><p>
  124. A mapping contains a channel coupling description and a list of
  125. 'submaps' that bundle sets of channel vectors together for grouped
  126. encoding and decoding. These submaps are not references to external
  127. components; the submap list is internal and specific to a mapping.</p><p>
  128. A 'submap' is a configuration/grouping that applies to a subset of
  129. floor and residue vectors within a mapping. The submap functions as a
  130. last layer of indirection such that specific special floor or residue
  131. settings can be applied not only to all the vectors in a given mode,
  132. but also specific vectors in a specific mode. Each submap specifies
  133. the proper floor and residue instance number to use for decoding that
  134. submap's spectral floor and spectral residue vectors.</p><p>
  135. As an example:</p><p>
  136. Assume a Vorbis stream that contains six channels in the standard 5.1
  137. format. The sixth channel, as is normal in 5.1, is bass only.
  138. Therefore it would be wasteful to encode a full-spectrum version of it
  139. as with the other channels. The submapping mechanism can be used to
  140. apply a full range floor and residue encoding to channels 0 through 4,
  141. and a bass-only representation to the bass channel, thus saving space.
  142. In this example, channels 0-4 belong to submap 0 (which indicates use
  143. of a full-range floor) and channel 5 belongs to submap 1, which uses a
  144. bass-only representation.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id258391"></a>1.2.4. Floor</h4></div></div></div><p>
  145. Vorbis encodes a spectral 'floor' vector for each PCM channel. This
  146. vector is a low-resolution representation of the audio spectrum for
  147. the given channel in the current frame, generally used akin to a
  148. whitening filter. It is named a 'floor' because the Xiph.Org
  149. reference encoder has historically used it as a unit-baseline for
  150. spectral resolution.</p><p>
  151. A floor encoding may be of two types. Floor 0 uses a packed LSP
  152. representation on a dB amplitude scale and Bark frequency scale.
  153. Floor 1 represents the curve as a piecewise linear interpolated
  154. representation on a dB amplitude scale and linear frequency scale.
  155. The two floors are semantically interchangeable in
  156. encoding/decoding. However, floor type 1 provides more stable
  157. inter-frame behavior, and so is the preferred choice in all
  158. coupled-stereo and high bitrate modes. Floor 1 is also considerably
  159. less expensive to decode than floor 0.</p><p>
  160. Floor 0 is not to be considered deprecated, but it is of limited
  161. modern use. No known Vorbis encoder past Xiph.org's own beta 4 makes
  162. use of floor 0.</p><p>
  163. The values coded/decoded by a floor are both compactly formatted and
  164. make use of entropy coding to save space. For this reason, a floor
  165. configuration generally refers to multiple codebooks in the codebook
  166. component list. Entropy coding is thus provided as an abstraction,
  167. and each floor instance may choose from any and all available
  168. codebooks when coding/decoding.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id258423"></a>1.2.5. Residue</h4></div></div></div><p>
  169. The spectral residue is the fine structure of the audio spectrum
  170. once the floor curve has been subtracted out. In simplest terms, it
  171. is coded in the bitstream using cascaded (multi-pass) vector
  172. quantization according to one of three specific packing/coding
  173. algorithms numbered 0 through 2. The packing algorithm details are
  174. configured by residue instance. As with the floor components, the
  175. final VQ/entropy encoding is provided by external codebook instances
  176. and each residue instance may choose from any and all available
  177. codebooks.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id258437"></a>1.2.6. Codebooks</h4></div></div></div><p>
  178. Codebooks are a self-contained abstraction that perform entropy
  179. decoding and, optionally, use the entropy-decoded integer value as an
  180. offset into an index of output value vectors, returning the indicated
  181. vector of values.</p><p>
  182. The entropy coding in a Vorbis I codebook is provided by a standard
  183. Huffman binary tree representation. This tree is tightly packed using
  184. one of several methods, depending on whether codeword lengths are
  185. ordered or unordered, or the tree is sparse.</p><p>
  186. The codebook vector index is similarly packed according to index
  187. characteristic. Most commonly, the vector index is encoded as a
  188. single list of values of possible values that are then permuted into
  189. a list of n-dimensional rows (lattice VQ).</p></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id258461"></a>1.3. High-level Decode Process</h3></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id258467"></a>1.3.1. Decode Setup</h4></div></div></div><p>
  190. Before decoding can begin, a decoder must initialize using the
  191. bitstream headers matching the stream to be decoded. Vorbis uses
  192. three header packets; all are required, in-order, by this
  193. specification. Once set up, decode may begin at any audio packet
  194. belonging to the Vorbis stream. In Vorbis I, all packets after the
  195. three initial headers are audio packets. </p><p>
  196. The header packets are, in order, the identification
  197. header, the comments header, and the setup header.</p><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="id314074"></a>1.3.1.1. Identification Header</h5></div></div></div><p>
  198. The identification header identifies the bitstream as Vorbis, Vorbis
  199. version, and the simple audio characteristics of the stream such as
  200. sample rate and number of channels.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="id314086"></a>1.3.1.2. Comment Header</h5></div></div></div><p>
  201. The comment header includes user text comments ("tags") and a vendor
  202. string for the application/library that produced the bitstream. The
  203. encoding and proper use of the comment header is described in
  204. <a href="#vorbis-spec-comment" title="5. comment field and header specification">Section 5, &#8220;comment field and header specification&#8221;</a>.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="id314101"></a>1.3.1.3. Setup Header</h5></div></div></div><p>
  205. The setup header includes extensive CODEC setup information as well as
  206. the complete VQ and Huffman codebooks needed for decode.</p></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id314113"></a>1.3.2. Decode Procedure</h4></div></div></div><div class="highlights"><p>
  207. The decoding and synthesis procedure for all audio packets is
  208. fundamentally the same.
  209. </p><div class="orderedlist"><ol type="1"><li>decode packet type flag</li><li>decode mode number</li><li>decode window shape (long windows only)</li><li>decode floor</li><li>decode residue into residue vectors</li><li>inverse channel coupling of residue vectors</li><li>generate floor curve from decoded floor data</li><li>compute dot product of floor and residue, producing audio spectrum vector</li><li>inverse monolithic transform of audio spectrum vector, always an MDCT in Vorbis I</li><li>overlap/add left-hand output of transform with right-hand output of previous frame</li><li>store right hand-data from transform of current frame for future lapping</li><li>if not first frame, return results of overlap/add as audio result of current frame</li></ol></div><p>
  210. </p></div><p>
  211. Note that clever rearrangement of the synthesis arithmetic is
  212. possible; as an example, one can take advantage of symmetries in the
  213. MDCT to store the right-hand transform data of a partial MDCT for a
  214. 50% inter-frame buffer space savings, and then complete the transform
  215. later before overlap/add with the next frame. This optimization
  216. produces entirely equivalent output and is naturally perfectly legal.
  217. The decoder must be <span class="emphasis"><em>entirely mathematically equivalent</em></span> to the
  218. specification, it need not be a literal semantic implementation.</p><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="id314203"></a>1.3.2.1. Packet type decode</h5></div></div></div><p>
  219. Vorbis I uses four packet types. The first three packet types mark each
  220. of the three Vorbis headers described above. The fourth packet type
  221. marks an audio packet. All other packet types are reserved; packets
  222. marked with a reserved type should be ignored.</p><p>
  223. Following the three header packets, all packets in a Vorbis I stream
  224. are audio. The first step of audio packet decode is to read and
  225. verify the packet type; <span class="emphasis"><em>a non-audio packet when audio is expected
  226. indicates stream corruption or a non-compliant stream. The decoder
  227. must ignore the packet and not attempt decoding it to
  228. audio</em></span>.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="id314225"></a>1.3.2.2. Mode decode</h5></div></div></div><p>
  229. Vorbis allows an encoder to set up multiple, numbered packet 'modes',
  230. as described earlier, all of which may be used in a given Vorbis
  231. stream. The mode is encoded as an integer used as a direct offset into
  232. the mode instance index. </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="vorbis-spec-window"></a>1.3.2.3. Window shape decode (long windows only)</h5></div></div></div><p>
  233. Vorbis frames may be one of two PCM sample sizes specified during
  234. codec setup. In Vorbis I, legal frame sizes are powers of two from 64
  235. to 8192 samples. Aside from coupling, Vorbis handles channels as
  236. independent vectors and these frame sizes are in samples per channel.</p><p>
  237. Vorbis uses an overlapping transform, namely the MDCT, to blend one
  238. frame into the next, avoiding most inter-frame block boundary
  239. artifacts. The MDCT output of one frame is windowed according to MDCT
  240. requirements, overlapped 50% with the output of the previous frame and
  241. added. The window shape assures seamless reconstruction. </p><p>
  242. This is easy to visualize in the case of equal sized-windows:</p><div class="mediaobject"><img src="window1.png" alt="overlap of two equal-sized windows"></div><p>
  243. And slightly more complex in the case of overlapping unequal sized
  244. windows:</p><div class="mediaobject"><img src="window2.png" alt="overlap of a long and a short window"></div><p>
  245. In the unequal-sized window case, the window shape of the long window
  246. must be modified for seamless lapping as above. It is possible to
  247. correctly infer window shape to be applied to the current window from
  248. knowing the sizes of the current, previous and next window. It is
  249. legal for a decoder to use this method. However, in the case of a long
  250. window (short windows require no modification), Vorbis also codes two
  251. flag bits to specify pre- and post- window shape. Although not
  252. strictly necessary for function, this minor redundancy allows a packet
  253. to be fully decoded to the point of lapping entirely independently of
  254. any other packet, allowing easier abstraction of decode layers as well
  255. as allowing a greater level of easy parallelism in encode and
  256. decode.</p><p>
  257. A description of valid window functions for use with an inverse MDCT
  258. can be found in the paper
  259. &#8220;<span class="citetitle">
  260. <a href="http://www.iocon.com/resource/docs/ps/eusipco_corrected.ps" target="_top">
  261. The use of multirate filter banks for coding of high quality digital
  262. audio</a></span>&#8221;, by T. Sporer, K. Brandenburg and B. Edler. Vorbis windows
  263. all use the slope function
  264. <span class="inlinemediaobject"><span>$y = \sin(.5*\pi \, \sin^2((x+.5)/n*\pi))$</span></span>.
  265. </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="id339168"></a>1.3.2.4. floor decode</h5></div></div></div><p>
  266. Each floor is encoded/decoded in channel order, however each floor
  267. belongs to a 'submap' that specifies which floor configuration to
  268. use. All floors are decoded before residue decode begins.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="id339179"></a>1.3.2.5. residue decode</h5></div></div></div><p>
  269. Although the number of residue vectors equals the number of channels,
  270. channel coupling may mean that the raw residue vectors extracted
  271. during decode do not map directly to specific channels. When channel
  272. coupling is in use, some vectors will correspond to coupled magnitude
  273. or angle. The coupling relationships are described in the codec setup
  274. and may differ from frame to frame, due to different mode numbers.</p><p>
  275. Vorbis codes residue vectors in groups by submap; the coding is done
  276. in submap order from submap 0 through n-1. This differs from floors
  277. which are coded using a configuration provided by submap number, but
  278. are coded individually in channel order.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="id339196"></a>1.3.2.6. inverse channel coupling</h5></div></div></div><p>
  279. A detailed discussion of stereo in the Vorbis codec can be found in
  280. the document <a href="stereo.html" target="_top"><em class="citetitle">Stereo Channel Coupling in the
  281. Vorbis CODEC</em></a>. Vorbis is not limited to only stereo coupling, but
  282. the stereo document also gives a good overview of the generic coupling
  283. mechanism.</p><p>
  284. Vorbis coupling applies to pairs of residue vectors at a time;
  285. decoupling is done in-place a pair at a time in the order and using
  286. the vectors specified in the current mapping configuration. The
  287. decoupling operation is the same for all pairs, converting square
  288. polar representation (where one vector is magnitude and the second
  289. angle) back to Cartesian representation.</p><p>
  290. After decoupling, in order, each pair of vectors on the coupling list,
  291. the resulting residue vectors represent the fine spectral detail
  292. of each output channel.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="id339224"></a>1.3.2.7. generate floor curve</h5></div></div></div><p>
  293. The decoder may choose to generate the floor curve at any appropriate
  294. time. It is reasonable to generate the output curve when the floor
  295. data is decoded from the raw packet, or it can be generated after
  296. inverse coupling and applied to the spectral residue directly,
  297. combining generation and the dot product into one step and eliminating
  298. some working space.</p><p>
  299. Both floor 0 and floor 1 generate a linear-range, linear-domain output
  300. vector to be multiplied (dot product) by the linear-range,
  301. linear-domain spectral residue.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="id339240"></a>1.3.2.8. compute floor/residue dot product</h5></div></div></div><p>
  302. This step is straightforward; for each output channel, the decoder
  303. multiplies the floor curve and residue vectors element by element,
  304. producing the finished audio spectrum of each channel.</p><p>
  305. One point is worth mentioning about this dot product; a common mistake
  306. in a fixed point implementation might be to assume that a 32 bit
  307. fixed-point representation for floor and residue and direct
  308. multiplication of the vectors is sufficient for acceptable spectral
  309. depth in all cases because it happens to mostly work with the current
  310. Xiph.Org reference encoder.</p><p>
  311. However, floor vector values can span ~140dB (~24 bits unsigned), and
  312. the audio spectrum vector should represent a minimum of 120dB (~21
  313. bits with sign), even when output is to a 16 bit PCM device. For the
  314. residue vector to represent full scale if the floor is nailed to
  315. -140dB, it must be able to span 0 to +140dB. For the residue vector
  316. to reach full scale if the floor is nailed at 0dB, it must be able to
  317. represent -140dB to +0dB. Thus, in order to handle full range
  318. dynamics, a residue vector may span -140dB to +140dB entirely within
  319. spec. A 280dB range is approximately 48 bits with sign; thus the
  320. residue vector must be able to represent a 48 bit range and the dot
  321. product must be able to handle an effective 48 bit times 24 bit
  322. multiplication. This range may be achieved using large (64 bit or
  323. larger) integers, or implementing a movable binary point
  324. representation.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="id339268"></a>1.3.2.9. inverse monolithic transform (MDCT)</h5></div></div></div><p>
  325. The audio spectrum is converted back into time domain PCM audio via an
  326. inverse Modified Discrete Cosine Transform (MDCT). A detailed
  327. description of the MDCT is available in the paper <a href="http://www.iocon.com/resource/docs/ps/eusipco_corrected.ps" target="_top">&#8220;<span class="citetitle">The use of multirate filter banks for coding of high quality digital
  328. audio</span>&#8221;</a>, by T. Sporer, K. Brandenburg and B. Edler.</p><p>
  329. Note that the PCM produced directly from the MDCT is not yet finished
  330. audio; it must be lapped with surrounding frames using an appropriate
  331. window (such as the Vorbis window) before the MDCT can be considered
  332. orthogonal.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="id339292"></a>1.3.2.10. overlap/add data</h5></div></div></div><p>
  333. Windowed MDCT output is overlapped and added with the right hand data
  334. of the previous window such that the 3/4 point of the previous window
  335. is aligned with the 1/4 point of the current window (as illustrated in
  336. the window overlap diagram). At this point, the audio data between the
  337. center of the previous frame and the center of the current frame is
  338. now finished and ready to be returned. </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="id339304"></a>1.3.2.11. cache right hand data</h5></div></div></div><p>
  339. The decoder must cache the right hand portion of the current frame to
  340. be lapped with the left hand portion of the next frame.
  341. </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="id339314"></a>1.3.2.12. return finished audio data</h5></div></div></div><p>
  342. The overlapped portion produced from overlapping the previous and
  343. current frame data is finished data to be returned by the decoder.
  344. This data spans from the center of the previous window to the center
  345. of the current window. In the case of same-sized windows, the amount
  346. of data to return is one-half block consisting of and only of the
  347. overlapped portions. When overlapping a short and long window, much of
  348. the returned range is not actually overlap. This does not damage
  349. transform orthogonality. Pay attention however to returning the
  350. correct data range; the amount of data to be returned is:
  351. </p><pre class="programlisting">
  352. window_blocksize(previous_window)/4+window_blocksize(current_window)/4
  353. </pre><p>
  354. from the center of the previous window to the center of the current
  355. window.</p><p>
  356. Data is not returned from the first frame; it must be used to 'prime'
  357. the decode engine. The encoder accounts for this priming when
  358. calculating PCM offsets; after the first frame, the proper PCM output
  359. offset is '0' (as no data has been returned yet).</p></div></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="vorbis-spec-bitpacking"></a>2. Bitpacking Convention</h2></div><div><p class="releaseinfo">
  360. $Id: 02-bitpacking.xml 7186 2004-07-20 07:19:25Z xiphmont $
  361. </p></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id304831"></a>2.1. Overview</h3></div></div></div><p>
  362. The Vorbis codec uses relatively unstructured raw packets containing
  363. arbitrary-width binary integer fields. Logically, these packets are a
  364. bitstream in which bits are coded one-by-one by the encoder and then
  365. read one-by-one in the same monotonically increasing order by the
  366. decoder. Most current binary storage arrangements group bits into a
  367. native word size of eight bits (octets), sixteen bits, thirty-two bits
  368. or, less commonly other fixed word sizes. The Vorbis bitpacking
  369. convention specifies the correct mapping of the logical packet
  370. bitstream into an actual representation in fixed-width words.
  371. </p><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id304890"></a>2.1.1. octets, bytes and words</h4></div></div></div><p>
  372. In most contemporary architectures, a 'byte' is synonymous with an
  373. 'octet', that is, eight bits. This has not always been the case;
  374. seven, ten, eleven and sixteen bit 'bytes' have been used. For
  375. purposes of the bitpacking convention, a byte implies the native,
  376. smallest integer storage representation offered by a platform. On
  377. modern platforms, this is generally assumed to be eight bits (not
  378. necessarily because of the processor but because of the
  379. filesystem/memory architecture. Modern filesystems invariably offer
  380. bytes as the fundamental atom of storage). A 'word' is an integer
  381. size that is a grouped multiple of this smallest size.</p><p>
  382. The most ubiquitous architectures today consider a 'byte' to be an
  383. octet (eight bits) and a word to be a group of two, four or eight
  384. bytes (16, 32 or 64 bits). Note however that the Vorbis bitpacking
  385. convention is still well defined for any native byte size; Vorbis uses
  386. the native bit-width of a given storage system. This document assumes
  387. that a byte is one octet for purposes of example.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id304832"></a>2.1.2. bit order</h4></div></div></div><p>
  388. A byte has a well-defined 'least significant' bit (LSb), which is the
  389. only bit set when the byte is storing the two's complement integer
  390. value +1. A byte's 'most significant' bit (MSb) is at the opposite
  391. end of the byte. Bits in a byte are numbered from zero at the LSb to
  392. <span class="emphasis"><em>n</em></span> (<span class="emphasis"><em>n</em></span>=7 in an octet) for the
  393. MSb.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id327953"></a>2.1.3. byte order</h4></div></div></div><p>
  394. Words are native groupings of multiple bytes. Several byte orderings
  395. are possible in a word; the common ones are 3-2-1-0 ('big endian' or
  396. 'most significant byte first' in which the highest-valued byte comes
  397. first), 0-1-2-3 ('little endian' or 'least significant byte first' in
  398. which the lowest value byte comes first) and less commonly 3-1-2-0 and
  399. 0-2-1-3 ('mixed endian').</p><p>
  400. The Vorbis bitpacking convention specifies storage and bitstream
  401. manipulation at the byte, not word, level, thus host word ordering is
  402. of a concern only during optimization when writing high performance
  403. code that operates on a word of storage at a time rather than by byte.
  404. Logically, bytes are always coded and decoded in order from byte zero
  405. through byte <span class="emphasis"><em>n</em></span>.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id336382"></a>2.1.4. coding bits into byte sequences</h4></div></div></div><p>
  406. The Vorbis codec has need to code arbitrary bit-width integers, from
  407. zero to 32 bits wide, into packets. These integer fields are not
  408. aligned to the boundaries of the byte representation; the next field
  409. is written at the bit position at which the previous field ends.</p><p>
  410. The encoder logically packs integers by writing the LSb of a binary
  411. integer to the logical bitstream first, followed by next least
  412. significant bit, etc, until the requested number of bits have been
  413. coded. When packing the bits into bytes, the encoder begins by
  414. placing the LSb of the integer to be written into the least
  415. significant unused bit position of the destination byte, followed by
  416. the next-least significant bit of the source integer and so on up to
  417. the requested number of bits. When all bits of the destination byte
  418. have been filled, encoding continues by zeroing all bits of the next
  419. byte and writing the next bit into the bit position 0 of that byte.
  420. Decoding follows the same process as encoding, but by reading bits
  421. from the byte stream and reassembling them into integers.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id310906"></a>2.1.5. signedness</h4></div></div></div><p>
  422. The signedness of a specific number resulting from decode is to be
  423. interpreted by the decoder given decode context. That is, the three
  424. bit binary pattern 'b111' can be taken to represent either 'seven' as
  425. an unsigned integer, or '-1' as a signed, two's complement integer.
  426. The encoder and decoder are responsible for knowing if fields are to
  427. be treated as signed or unsigned.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id258112"></a>2.1.6. coding example</h4></div></div></div><p>
  428. Code the 4 bit integer value '12' [b1100] into an empty bytestream.
  429. Bytestream result:
  430. </p><pre class="screen">
  431. |
  432. V
  433. 7 6 5 4 3 2 1 0
  434. byte 0 [0 0 0 0 1 1 0 0] &lt;-
  435. byte 1 [ ]
  436. byte 2 [ ]
  437. byte 3 [ ]
  438. ...
  439. byte n [ ] bytestream length == 1 byte
  440. </pre><p>
  441. </p><p>
  442. Continue by coding the 3 bit integer value '-1' [b111]:
  443. </p><pre class="screen">
  444. |
  445. V
  446. 7 6 5 4 3 2 1 0
  447. byte 0 [0 1 1 1 1 1 0 0] &lt;-
  448. byte 1 [ ]
  449. byte 2 [ ]
  450. byte 3 [ ]
  451. ...
  452. byte n [ ] bytestream length == 1 byte
  453. </pre><p>
  454. </p><p>
  455. Continue by coding the 7 bit integer value '17' [b0010001]:
  456. </p><pre class="screen">
  457. |
  458. V
  459. 7 6 5 4 3 2 1 0
  460. byte 0 [1 1 1 1 1 1 0 0]
  461. byte 1 [0 0 0 0 1 0 0 0] &lt;-
  462. byte 2 [ ]
  463. byte 3 [ ]
  464. ...
  465. byte n [ ] bytestream length == 2 bytes
  466. bit cursor == 6
  467. </pre><p>
  468. </p><p>
  469. Continue by coding the 13 bit integer value '6969' [b110 11001110 01]:
  470. </p><pre class="screen">
  471. |
  472. V
  473. 7 6 5 4 3 2 1 0
  474. byte 0 [1 1 1 1 1 1 0 0]
  475. byte 1 [0 1 0 0 1 0 0 0]
  476. byte 2 [1 1 0 0 1 1 1 0]
  477. byte 3 [0 0 0 0 0 1 1 0] &lt;-
  478. ...
  479. byte n [ ] bytestream length == 4 bytes
  480. </pre><p>
  481. </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id258160"></a>2.1.7. decoding example</h4></div></div></div><p>
  482. Reading from the beginning of the bytestream encoded in the above example:
  483. </p><pre class="screen">
  484. |
  485. V
  486. 7 6 5 4 3 2 1 0
  487. byte 0 [1 1 1 1 1 1 0 0] &lt;-
  488. byte 1 [0 1 0 0 1 0 0 0]
  489. byte 2 [1 1 0 0 1 1 1 0]
  490. byte 3 [0 0 0 0 0 1 1 0] bytestream length == 4 bytes
  491. </pre><p>
  492. </p><p>
  493. We read two, two-bit integer fields, resulting in the returned numbers
  494. 'b00' and 'b11'. Two things are worth noting here:
  495. </p><div class="itemizedlist"><ul type="disc"><li><p>Although these four bits were originally written as a single
  496. four-bit integer, reading some other combination of bit-widths from the
  497. bitstream is well defined. There are no artificial alignment
  498. boundaries maintained in the bitstream.</p></li><li><p>The second value is the
  499. two-bit-wide integer 'b11'. This value may be interpreted either as
  500. the unsigned value '3', or the signed value '-1'. Signedness is
  501. dependent on decode context.</p></li></ul></div><p>
  502. </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id259912"></a>2.1.8. end-of-packet alignment</h4></div></div></div><p>
  503. The typical use of bitpacking is to produce many independent
  504. byte-aligned packets which are embedded into a larger byte-aligned
  505. container structure, such as an Ogg transport bitstream. Externally,
  506. each bytestream (encoded bitstream) must begin and end on a byte
  507. boundary. Often, the encoded bitstream is not an integer number of
  508. bytes, and so there is unused (uncoded) space in the last byte of a
  509. packet.</p><p>
  510. Unused space in the last byte of a bytestream is always zeroed during
  511. the coding process. Thus, should this unused space be read, it will
  512. return binary zeroes.</p><p>
  513. Attempting to read past the end of an encoded packet results in an
  514. 'end-of-packet' condition. End-of-packet is not to be considered an
  515. error; it is merely a state indicating that there is insufficient
  516. remaining data to fulfill the desired read size. Vorbis uses truncated
  517. packets as a normal mode of operation, and as such, decoders must
  518. handle reading past the end of a packet as a typical mode of
  519. operation. Any further read operations after an 'end-of-packet'
  520. condition shall also return 'end-of-packet'.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id259938"></a>2.1.9.  reading zero bits</h4></div></div></div><p>
  521. Reading a zero-bit-wide integer returns the value '0' and does not
  522. increment the stream cursor. Reading to the end of the packet (but
  523. not past, such that an 'end-of-packet' condition has not triggered)
  524. and then reading a zero bit integer shall succeed, returning 0, and
  525. not trigger an end-of-packet condition. Reading a zero-bit-wide
  526. integer after a previous read sets 'end-of-packet' shall also fail
  527. with 'end-of-packet'.</p></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="vorbis-spec-codebook"></a>3. Probability Model and Codebooks</h2></div><div><p class="releaseinfo">
  528. $Id: 03-codebook.xml 7186 2004-07-20 07:19:25Z xiphmont $
  529. </p></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id310158"></a>3.1. Overview</h3></div></div></div><p>
  530. Unlike practically every other mainstream audio codec, Vorbis has no
  531. statically configured probability model, instead packing all entropy
  532. decoding configuration, VQ and Huffman, into the bitstream itself in
  533. the third header, the codec setup header. This packed configuration
  534. consists of multiple 'codebooks', each containing a specific
  535. Huffman-equivalent representation for decoding compressed codewords as
  536. well as an optional lookup table of output vector values to which a
  537. decoded Huffman value is applied as an offset, generating the final
  538. decoded output corresponding to a given compressed codeword.</p><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id335318"></a>3.1.1. Bitwise operation</h4></div></div></div><p>
  539. The codebook mechanism is built on top of the vorbis bitpacker. Both
  540. the codebooks themselves and the codewords they decode are unrolled
  541. from a packet as a series of arbitrary-width values read from the
  542. stream according to <a href="#vorbis-spec-bitpacking" title="2. Bitpacking Convention">Section 2, &#8220;Bitpacking Convention&#8221;</a>.</p></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id310216"></a>3.2. Packed codebook format</h3></div></div></div><p>
  543. For purposes of the examples below, we assume that the storage
  544. system's native byte width is eight bits. This is not universally
  545. true; see <a href="#vorbis-spec-bitpacking" title="2. Bitpacking Convention">Section 2, &#8220;Bitpacking Convention&#8221;</a> for discussion
  546. relating to non-eight-bit bytes.</p><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id324957"></a>3.2.1. codebook decode</h4></div></div></div><p>
  547. A codebook begins with a 24 bit sync pattern, 0x564342:
  548. </p><pre class="screen">
  549. byte 0: [ 0 1 0 0 0 0 1 0 ] (0x42)
  550. byte 1: [ 0 1 0 0 0 0 1 1 ] (0x43)
  551. byte 2: [ 0 1 0 1 0 1 1 0 ] (0x56)
  552. </pre><p>
  553. 16 bit <code class="varname">[codebook_dimensions]</code> and 24 bit <code class="varname">[codebook_entries]</code> fields:
  554. </p><pre class="screen">
  555. byte 3: [ X X X X X X X X ]
  556. byte 4: [ X X X X X X X X ] [codebook_dimensions] (16 bit unsigned)
  557. byte 5: [ X X X X X X X X ]
  558. byte 6: [ X X X X X X X X ]
  559. byte 7: [ X X X X X X X X ] [codebook_entries] (24 bit unsigned)
  560. </pre><p>
  561. Next is the <code class="varname">[ordered]</code> bit flag:
  562. </p><pre class="screen">
  563. byte 8: [ X ] [ordered] (1 bit)
  564. </pre><p>
  565. Each entry, numbering a
  566. total of <code class="varname">[codebook_entries]</code>, is assigned a codeword length.
  567. We now read the list of codeword lengths and store these lengths in
  568. the array <code class="varname">[codebook_codeword_lengths]</code>. Decode of lengths is
  569. according to whether the <code class="varname">[ordered]</code> flag is set or unset.
  570. </p><div class="itemizedlist"><ul type="disc"><li><p>If the <code class="varname">[ordered]</code> flag is unset, the codeword list is not
  571. length ordered and the decoder needs to read each codeword length
  572. one-by-one.</p><p>The decoder first reads one additional bit flag, the
  573. <code class="varname">[sparse]</code> flag. This flag determines whether or not the
  574. codebook contains unused entries that are not to be included in the
  575. codeword decode tree:
  576. </p><pre class="screen">
  577. byte 8: [ X 1 ] [sparse] flag (1 bit)
  578. </pre><p>
  579. The decoder now performs for each of the <code class="varname">[codebook_entries]</code>
  580. codebook entries:
  581. </p><pre class="screen">
  582. 1) if([sparse] is set){
  583. 2) [flag] = read one bit;
  584. 3) if([flag] is set){
  585. 4) [length] = read a five bit unsigned integer;
  586. 5) codeword length for this entry is [length]+1;
  587. } else {
  588. 6) this entry is unused. mark it as such.
  589. }
  590. } else the sparse flag is not set {
  591. 7) [length] = read a five bit unsigned integer;
  592. 8) the codeword length for this entry is [length]+1;
  593. }
  594. </pre></li><li><p>If the <code class="varname">[ordered]</code> flag is set, the codeword list for this
  595. codebook is encoded in ascending length order. Rather than reading
  596. a length for every codeword, the encoder reads the number of
  597. codewords per length. That is, beginning at entry zero:
  598. </p><pre class="screen">
  599. 1) [current_entry] = 0;
  600. 2) [current_length] = read a five bit unsigned integer and add 1;
  601. 3) [number] = read <a href="#vorbis-spec-ilog" title="9.2.1. ilog">ilog</a>([codebook_entries] - [current_entry]) bits as an unsigned integer
  602. 4) set the entries [current_entry] through [current_entry]+[number]-1, inclusive,
  603. of the [codebook_codeword_lengths] array to [current_length]
  604. 5) set [current_entry] to [number] + [current_entry]
  605. 6) increment [current_length] by 1
  606. 7) if [current_entry] is greater than [codebook_entries] ERROR CONDITION;
  607. the decoder will not be able to read this stream.
  608. 8) if [current_entry] is less than [codebook_entries], repeat process starting at 3)
  609. 9) done.
  610. </pre></li></ul></div><p>
  611. After all codeword lengths have been decoded, the decoder reads the
  612. vector lookup table. Vorbis I supports three lookup types:
  613. </p><div class="orderedlist"><ol type="1"><li>No lookup</li><li>Implicitly populated value mapping (lattice VQ)</li><li>Explicitly populated value mapping (tessellated or 'foam'
  614. VQ)</li></ol></div><p>
  615. </p><p>
  616. The lookup table type is read as a four bit unsigned integer:
  617. </p><pre class="screen">
  618. 1) [codebook_lookup_type] = read four bits as an unsigned integer
  619. </pre><p>
  620. Codebook decode precedes according to <code class="varname">[codebook_lookup_type]</code>:
  621. </p><div class="itemizedlist"><ul type="disc"><li><p>Lookup type zero indicates no lookup to be read. Proceed past
  622. lookup decode.</p></li><li><p>Lookup types one and two are similar, differing only in the
  623. number of lookup values to be read. Lookup type one reads a list of
  624. values that are permuted in a set pattern to build a list of vectors,
  625. each vector of order <code class="varname">[codebook_dimensions]</code> scalars. Lookup
  626. type two builds the same vector list, but reads each scalar for each
  627. vector explicitly, rather than building vectors from a smaller list of
  628. possible scalar values. Lookup decode proceeds as follows:
  629. </p><pre class="screen">
  630. 1) [codebook_minimum_value] = <a href="#vorbis-spec-float32_unpack" title="9.2.2. float32_unpack">float32_unpack</a>( read 32 bits as an unsigned integer)
  631. 2) [codebook_delta_value] = <a href="#vorbis-spec-float32_unpack" title="9.2.2. float32_unpack">float32_unpack</a>( read 32 bits as an unsigned integer)
  632. 3) [codebook_value_bits] = read 4 bits as an unsigned integer and add 1
  633. 4) [codebook_sequence_p] = read 1 bit as a boolean flag
  634. if ( [codebook_lookup_type] is 1 ) {
  635. 5) [codebook_lookup_values] = <a href="#vorbis-spec-lookup1_values" title="9.2.3. lookup1_values">lookup1_values</a>(<code class="varname">[codebook_entries]</code>, <code class="varname">[codebook_dimensions]</code> )
  636. } else {
  637. 6) [codebook_lookup_values] = <code class="varname">[codebook_entries]</code> * <code class="varname">[codebook_dimensions]</code>
  638. }
  639. 7) read a total of [codebook_lookup_values] unsigned integers of [codebook_value_bits] each;
  640. store these in order in the array [codebook_multiplicands]
  641. </pre></li><li><p>A <code class="varname">[codebook_lookup_type]</code> of greater than two is reserved
  642. and indicates a stream that is not decodable by the specification in this
  643. document.</p></li></ul></div><p>
  644. </p><p>
  645. An 'end of packet' during any read operation in the above steps is
  646. considered an error condition rendering the stream undecodable.</p><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="id258959"></a>3.2.1.1. Huffman decision tree representation</h5></div></div></div><p>
  647. The <code class="varname">[codebook_codeword_lengths]</code> array and
  648. <code class="varname">[codebook_entries]</code> value uniquely define the Huffman decision
  649. tree used for entropy decoding.</p><p>
  650. Briefly, each used codebook entry (recall that length-unordered
  651. codebooks support unused codeword entries) is assigned, in order, the
  652. lowest valued unused binary Huffman codeword possible. Assume the
  653. following codeword length list:
  654. </p><pre class="screen">
  655. entry 0: length 2
  656. entry 1: length 4
  657. entry 2: length 4
  658. entry 3: length 4
  659. entry 4: length 4
  660. entry 5: length 2
  661. entry 6: length 3
  662. entry 7: length 3
  663. </pre><p>
  664. Assigning codewords in order (lowest possible value of the appropriate
  665. length to highest) results in the following codeword list:
  666. </p><pre class="screen">
  667. entry 0: length 2 codeword 00
  668. entry 1: length 4 codeword 0100
  669. entry 2: length 4 codeword 0101
  670. entry 3: length 4 codeword 0110
  671. entry 4: length 4 codeword 0111
  672. entry 5: length 2 codeword 10
  673. entry 6: length 3 codeword 110
  674. entry 7: length 3 codeword 111
  675. </pre><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
  676. Unlike most binary numerical values in this document, we
  677. intend the above codewords to be read and used bit by bit from left to
  678. right, thus the codeword '001' is the bit string 'zero, zero, one'.
  679. When determining 'lowest possible value' in the assignment definition
  680. above, the leftmost bit is the MSb.</p></div><p>
  681. It is clear that the codeword length list represents a Huffman
  682. decision tree with the entry numbers equivalent to the leaves numbered
  683. left-to-right:
  684. </p><div class="mediaobject"><img src="hufftree.png" alt="[huffman tree illustration]"></div><p>
  685. </p><p>
  686. As we assign codewords in order, we see that each choice constructs a
  687. new leaf in the leftmost possible position.</p><p>
  688. Note that it's possible to underspecify or overspecify a Huffman tree
  689. via the length list. In the above example, if codeword seven were
  690. eliminated, it's clear that the tree is unfinished:
  691. </p><div class="mediaobject"><img src="hufftree-under.png" alt="[underspecified huffman tree illustration]"></div><p>
  692. </p><p>
  693. Similarly, in the original codebook, it's clear that the tree is fully
  694. populated and a ninth codeword is impossible. Both underspecified and
  695. overspecified trees are an error condition rendering the stream
  696. undecodable.</p><p>
  697. Codebook entries marked 'unused' are simply skipped in the assigning
  698. process. They have no codeword and do not appear in the decision
  699. tree, thus it's impossible for any bit pattern read from the stream to
  700. decode to that entry number.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="id316419"></a>3.2.1.2. VQ lookup table vector representation</h5></div></div></div><p>
  701. Unpacking the VQ lookup table vectors relies on the following values:
  702. </p><pre class="programlisting">
  703. the [codebook_multiplicands] array
  704. [codebook_minimum_value]
  705. [codebook_delta_value]
  706. [codebook_sequence_p]
  707. [codebook_lookup_type]
  708. [codebook_entries]
  709. [codebook_dimensions]
  710. [codebook_lookup_values]
  711. </pre><p>
  712. </p><p>
  713. Decoding (unpacking) a specific vector in the vector lookup table
  714. proceeds according to <code class="varname">[codebook_lookup_type]</code>. The unpacked
  715. vector values are what a codebook would return during audio packet
  716. decode in a VQ context.</p><div class="section" lang="en"><div class="titlepage"><div><div><h6 class="title"><a name="id316444"></a>3.2.1.2.1. Vector value decode: Lookup type 1</h6></div></div></div><p>
  717. Lookup type one specifies a lattice VQ lookup table built
  718. algorithmically from a list of scalar values. Calculate (unpack) the
  719. final values of a codebook entry vector from the entries in
  720. <code class="varname">[codebook_multiplicands]</code> as follows (<code class="varname">[value_vector]</code>
  721. is the output vector representing the vector of values for entry number
  722. <code class="varname">[lookup_offset]</code> in this codebook):
  723. </p><pre class="screen">
  724. 1) [last] = 0;
  725. 2) [index_divisor] = 1;
  726. 3) iterate [i] over the range 0 ... [codebook_dimensions]-1 (once for each scalar value in the value vector) {
  727. 4) [multiplicand_offset] = ( [lookup_offset] divided by [index_divisor] using integer
  728. division ) integer modulo [codebook_lookup_values]
  729. 5) vector [value_vector] element [i] =
  730. ( [codebook_multiplicands] array element number [multiplicand_offset] ) *
  731. [codebook_delta_value] + [codebook_minimum_value] + [last];
  732. 6) if ( [codebook_sequence_p] is set ) then set [last] = vector [value_vector] element [i]
  733. 7) [index_divisor] = [index_divisor] * [codebook_lookup_values]
  734. }
  735. 8) vector calculation completed.
  736. </pre></div><div class="section" lang="en"><div class="titlepage"><div><div><h6 class="title"><a name="id316478"></a>3.2.1.2.2. Vector value decode: Lookup type 2</h6></div></div></div><p>
  737. Lookup type two specifies a VQ lookup table in which each scalar in
  738. each vector is explicitly set by the <code class="varname">[codebook_multiplicands]</code>
  739. array in a one-to-one mapping. Calculate [unpack] the
  740. final values of a codebook entry vector from the entries in
  741. <code class="varname">[codebook_multiplicands]</code> as follows (<code class="varname">[value_vector]</code>
  742. is the output vector representing the vector of values for entry number
  743. <code class="varname">[lookup_offset]</code> in this codebook):
  744. </p><pre class="screen">
  745. 1) [last] = 0;
  746. 2) [multiplicand_offset] = [lookup_offset] * [codebook_dimensions]
  747. 3) iterate [i] over the range 0 ... [codebook_dimensions]-1 (once for each scalar value in the value vector) {
  748. 4) vector [value_vector] element [i] =
  749. ( [codebook_multiplicands] array element number [multiplicand_offset] ) *
  750. [codebook_delta_value] + [codebook_minimum_value] + [last];
  751. 5) if ( [codebook_sequence_p] is set ) then set [last] = vector [value_vector] element [i]
  752. 6) increment [multiplicand_offset]
  753. }
  754. 7) vector calculation completed.
  755. </pre></div></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id316518"></a>3.3. Use of the codebook abstraction</h3></div></div></div><p>
  756. The decoder uses the codebook abstraction much as it does the
  757. bit-unpacking convention; a specific codebook reads a
  758. codeword from the bitstream, decoding it into an entry number, and then
  759. returns that entry number to the decoder (when used in a scalar
  760. entropy coding context), or uses that entry number as an offset into
  761. the VQ lookup table, returning a vector of values (when used in a context
  762. desiring a VQ value). Scalar or VQ context is always explicit; any call
  763. to the codebook mechanism requests either a scalar entry number or a
  764. lookup vector.</p><p>
  765. Note that VQ lookup type zero indicates that there is no lookup table;
  766. requesting decode using a codebook of lookup type 0 in any context
  767. expecting a vector return value (even in a case where a vector of
  768. dimension one) is forbidden. If decoder setup or decode requests such
  769. an action, that is an error condition rendering the packet
  770. undecodable.</p><p>
  771. Using a codebook to read from the packet bitstream consists first of
  772. reading and decoding the next codeword in the bitstream. The decoder
  773. reads bits until the accumulated bits match a codeword in the
  774. codebook. This process can be though of as logically walking the
  775. Huffman decode tree by reading one bit at a time from the bitstream,
  776. and using the bit as a decision boolean to take the 0 branch (left in
  777. the above examples) or the 1 branch (right in the above examples).
  778. Walking the tree finishes when the decode process hits a leaf in the
  779. decision tree; the result is the entry number corresponding to that
  780. leaf. Reading past the end of a packet propagates the 'end-of-stream'
  781. condition to the decoder.</p><p>
  782. When used in a scalar context, the resulting codeword entry is the
  783. desired return value.</p><p>
  784. When used in a VQ context, the codeword entry number is used as an
  785. offset into the VQ lookup table. The value returned to the decoder is
  786. the vector of scalars corresponding to this offset.</p></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="vorbis-spec-codec"></a>4. Codec Setup and Packet Decode</h2></div><div><p class="releaseinfo">
  787. $Id: 04-codec.xml 10466 2005-11-28 00:34:44Z giles $
  788. </p></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id336024"></a>4.1. Overview</h3></div></div></div><p>
  789. This document serves as the top-level reference document for the
  790. bit-by-bit decode specification of Vorbis I. This document assumes a
  791. high-level understanding of the Vorbis decode process, which is
  792. provided in <a href="#vorbis-spec-intro" title="1. Introduction and Description">Section 1, &#8220;Introduction and Description&#8221;</a>. <a href="#vorbis-spec-bitpacking" title="2. Bitpacking Convention">Section 2, &#8220;Bitpacking Convention&#8221;</a> covers reading and writing bit fields from
  793. and to bitstream packets.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id326710"></a>4.2. Header decode and decode setup</h3></div></div></div><p>
  794. A Vorbis bitstream begins with three header packets. The header
  795. packets are, in order, the identification header, the comments header,
  796. and the setup header. All are required for decode compliance. An
  797. end-of-packet condition during decoding the first or third header
  798. packet renders the stream undecodable. End-of-packet decoding the
  799. comment header is a non-fatal error condition.</p><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id337747"></a>4.2.1. Common header decode</h4></div></div></div><p>
  800. Each header packet begins with the same header fields.
  801. </p><pre class="screen">
  802. 1) [packet_type] : 8 bit value
  803. 2) 0x76, 0x6f, 0x72, 0x62, 0x69, 0x73: the characters 'v','o','r','b','i','s' as six octets
  804. </pre><p>
  805. Decode continues according to packet type; the identification header
  806. is type 1, the comment header type 3 and the setup header type 5
  807. (these types are all odd as a packet with a leading single bit of '0'
  808. is an audio packet). The packets must occur in the order of
  809. identification, comment, setup.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id317806"></a>4.2.2. Identification header</h4></div></div></div><p>
  810. The identification header is a short header of only a few fields used
  811. to declare the stream definitively as Vorbis, and provide a few externally
  812. relevant pieces of information about the audio stream. The
  813. identification header is coded as follows:</p><pre class="screen">
  814. 1) [vorbis_version] = read 32 bits as unsigned integer
  815. 2) [audio_channels] = read 8 bit integer as unsigned
  816. 3) [audio_sample_rate] = read 32 bits as unsigned integer
  817. 4) [bitrate_maximum] = read 32 bits as signed integer
  818. 5) [bitrate_nominal] = read 32 bits as signed integer
  819. 6) [bitrate_minimum] = read 32 bits as signed integer
  820. 7) [blocksize_0] = 2 exponent (read 4 bits as unsigned integer)
  821. 8) [blocksize_1] = 2 exponent (read 4 bits as unsigned integer)
  822. 9) [framing_flag] = read one bit
  823. </pre><p>
  824. <code class="varname">[vorbis_version]</code> is to read '0' in order to be compatible
  825. with this document. Both <code class="varname">[audio_channels]</code> and
  826. <code class="varname">[audio_sample_rate]</code> must read greater than zero. Allowed final
  827. blocksize values are 64, 128, 256, 512, 1024, 2048, 4096 and 8192 in
  828. Vorbis I. <code class="varname">[blocksize_0]</code> must be less than or equal to
  829. <code class="varname">[blocksize_1]</code>. The framing bit must be nonzero. Failure to
  830. meet any of these conditions renders a stream undecodable.</p><p>
  831. The bitrate fields above are used only as hints. The nominal bitrate
  832. field especially may be considerably off in purely VBR streams. The
  833. fields are meaningful only when greater than zero.</p><p>
  834. </p><div class="itemizedlist"><ul type="disc"><li>All three fields set to the same value implies a fixed rate, or tightly bounded, nearly fixed-rate bitstream</li><li>Only nominal set implies a VBR or ABR stream that averages the nominal bitrate</li><li>Maximum and or minimum set implies a VBR bitstream that obeys the bitrate limits</li><li>None set indicates the encoder does not care to speculate.</li></ul></div><p>
  835. </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id320370"></a>4.2.3. Comment header</h4></div></div></div><p>
  836. Comment header decode and data specification is covered in
  837. <a href="#vorbis-spec-comment" title="5. comment field and header specification">Section 5, &#8220;comment field and header specification&#8221;</a>.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id320384"></a>4.2.4. Setup header</h4></div></div></div><p>
  838. Vorbis codec setup is configurable to an extreme degree:
  839. </p><div class="mediaobject"><img src="components.png" alt="[decoder pipeline configuration]"></div><p>
  840. </p><p>
  841. The setup header contains the bulk of the codec setup information
  842. needed for decode. The setup header contains, in order, the lists of
  843. codebook configurations, time-domain transform configurations
  844. (placeholders in Vorbis I), floor configurations, residue
  845. configurations, channel mapping configurations and mode
  846. configurations. It finishes with a framing bit of '1'. Header decode
  847. proceeds in the following order:</p><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="id305504"></a>4.2.4.1. Codebooks</h5></div></div></div><div class="orderedlist"><ol type="1"><li><code class="varname">[vorbis_codebook_count]</code> = read eight bits as unsigned integer and add one</li><li>Decode <code class="varname">[vorbis_codebook_count]</code> codebooks in order as defined
  848. in <a href="#vorbis-spec-codebook" title="3. Probability Model and Codebooks">Section 3, &#8220;Probability Model and Codebooks&#8221;</a>. Save each configuration, in
  849. order, in an array of
  850. codebook configurations <code class="varname">[vorbis_codebook_configurations]</code>.</li></ol></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="id305539"></a>4.2.4.2. Time domain transforms</h5></div></div></div><p>
  851. These hooks are placeholders in Vorbis I. Nevertheless, the
  852. configuration placeholder values must be read to maintain bitstream
  853. sync.</p><div class="orderedlist"><ol type="1"><li><code class="varname">[vorbis_time_count]</code> = read 6 bits as unsigned integer and add one</li><li>read <code class="varname">[vorbis_time_count]</code> 16 bit values; each value should be zero. If any value is nonzero, this is an error condition and the stream is undecodable.</li></ol></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="id328542"></a>4.2.4.3. Floors</h5></div></div></div><p>
  854. Vorbis uses two floor types; header decode is handed to the decode
  855. abstraction of the appropriate type.</p><div class="orderedlist"><ol type="1"><li><code class="varname">[vorbis_floor_count]</code> = read 6 bits as unsigned integer and add one</li><li><p>For each <code class="varname">[i]</code> of <code class="varname">[vorbis_floor_count]</code> floor numbers:
  856. </p><div class="orderedlist"><ol type="a"><li>read the floor type: vector <code class="varname">[vorbis_floor_types]</code> element <code class="varname">[i]</code> =
  857. read 16 bits as unsigned integer</li><li>If the floor type is zero, decode the floor
  858. configuration as defined in <a href="#vorbis-spec-floor0" title="6. Floor type 0 setup and decode">Section 6, &#8220;Floor type 0 setup and decode&#8221;</a>; save
  859. this
  860. configuration in slot <code class="varname">[i]</code> of the floor configuration array <code class="varname">[vorbis_floor_configurations]</code>.</li><li>If the floor type is one,
  861. decode the floor configuration as defined in <a href="#vorbis-spec-floor1" title="7. Floor type 1 setup and decode">Section 7, &#8220;Floor type 1 setup and decode&#8221;</a>; save this configuration in slot <code class="varname">[i]</code> of the floor configuration array <code class="varname">[vorbis_floor_configurations]</code>.</li><li>If the the floor type is greater than one, this stream is undecodable; ERROR CONDITION</li></ol></div><p>
  862. </p></li></ol></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="id328635"></a>4.2.4.4. Residues</h5></div></div></div><p>
  863. Vorbis uses three residue types; header decode of each type is identical.
  864. </p><div class="orderedlist"><ol type="1"><li><code class="varname">[vorbis_residue_count]</code> = read 6 bits as unsigned integer and add one
  865. </li><li><p>For each of <code class="varname">[vorbis_residue_count]</code> residue numbers:
  866. </p><div class="orderedlist"><ol type="a"><li>read the residue type; vector <code class="varname">[vorbis_residue_types]</code> element <code class="varname">[i]</code> = read 16 bits as unsigned integer</li><li>If the residue type is zero,
  867. one or two, decode the residue configuration as defined in <a href="#vorbis-spec-residue" title="8. Residue setup and decode">Section 8, &#8220;Residue setup and decode&#8221;</a>; save this configuration in slot <code class="varname">[i]</code> of the residue configuration array <code class="varname">[vorbis_residue_configurations]</code>.</li><li>If the the residue type is greater than two, this stream is undecodable; ERROR CONDITION</li></ol></div><p>
  868. </p></li></ol></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="id275059"></a>4.2.4.5. Mappings</h5></div></div></div><p>
  869. Mappings are used to set up specific pipelines for encoding
  870. multichannel audio with varying channel mapping applications. Vorbis I
  871. uses a single mapping type (0), with implicit PCM channel mappings.</p><div class="orderedlist"><ol type="1"><li><code class="varname">[vorbis_mapping_count]</code> = read 6 bits as unsigned integer and add one</li><li><p>For each <code class="varname">[i]</code> of <code class="varname">[vorbis_mapping_count]</code> mapping numbers:
  872. </p><div class="orderedlist"><ol type="a"><li>read the mapping type: 16 bits as unsigned integer. There's no reason to save the mapping type in Vorbis I.</li><li>If the mapping type is nonzero, the stream is undecodable</li><li><p>If the mapping type is zero:
  873. </p><div class="orderedlist"><ol type="i"><li><p>read 1 bit as a boolean flag
  874. </p><div class="orderedlist"><ol type="A"><li>if set, <code class="varname">[vorbis_mapping_submaps]</code> = read 4 bits as unsigned integer and add one</li><li>if unset, <code class="varname">[vorbis_mapping_submaps]</code> = 1</li></ol></div><p>
  875. </p></li><li><p>read 1 bit as a boolean flag
  876. </p><div class="orderedlist"><ol type="A"><li><p>if set, square polar channel mapping is in use:
  877. </p><div class="orderedlist"><ol type="I"><li><code class="varname">[vorbis_mapping_coupling_steps]</code> = read 8 bits as unsigned integer and add one</li><li><p>for <code class="varname">[j]</code> each of <code class="varname">[vorbis_mapping_coupling_steps]</code> steps:
  878. </p><div class="orderedlist"><ol type="1"><li>vector <code class="varname">[vorbis_mapping_magnitude]</code> element <code class="varname">[j]</code>= read <a href="#vorbis-spec-ilog" title="9.2.1. ilog">ilog</a>(<code class="varname">[audio_channels]</code> - 1) bits as unsigned integer</li><li>vector <code class="varname">[vorbis_mapping_angle]</code> element <code class="varname">[j]</code>= read <a href="#vorbis-spec-ilog" title="9.2.1. ilog">ilog</a>(<code class="varname">[audio_channels]</code> - 1) bits as unsigned integer</li><li>the numbers read in the above two steps are channel numbers representing the channel to treat as magnitude and the channel to treat as angle, respectively. If for any coupling step the angle channel number equals the magnitude channel number, the magnitude channel number is greater than <code class="varname">[audio_channels]</code>-1, or the angle channel is greater than <code class="varname">[audio_channels]</code>-1, the stream is undecodable.</li></ol></div><p>
  879. </p></li></ol></div><p>
  880. </p></li><li>if unset, <code class="varname">[vorbis_mapping_coupling_steps]</code> = 0</li></ol></div><p>
  881. </p></li><li>read 2 bits (reserved field); if the value is nonzero, the stream is undecodable</li><li><p>if <code class="varname">[vorbis_mapping_submaps]</code> is greater than one, we read channel multiplex settings. For each <code class="varname">[j]</code> of <code class="varname">[audio_channels]</code> channels:</p><div class="orderedlist"><ol type="A"><li>vector <code class="varname">[vorbis_mapping_mux]</code> element <code class="varname">[j]</code> = read 4 bits as unsigned integer</li><li>if the value is greater than the highest numbered submap (<code class="varname">[vorbis_mapping_submaps]</code> - 1), this in an error condition rendering the stream undecodable</li></ol></div></li><li><p>for each submap <code class="varname">[j]</code> of <code class="varname">[vorbis_mapping_submaps]</code> submaps, read the floor and residue numbers for use in decoding that submap:</p><div class="orderedlist"><ol type="A"><li>read and discard 8 bits (the unused time configuration placeholder)</li><li>read 8 bits as unsigned integer for the floor number; save in vector <code class="varname">[vorbis_mapping_submap_floor]</code> element <code class="varname">[j]</code></li><li>verify the floor number is not greater than the highest number floor configured for the bitstream. If it is, the bitstream is undecodable</li><li>read 8 bits as unsigned integer for the residue number; save in vector <code class="varname">[vorbis_mapping_submap_residue]</code> element <code class="varname">[j]</code></li><li>verify the residue number is not greater than the highest number residue configured for the bitstream. If it is, the bitstream is undecodable</li></ol></div></li><li>save this mapping configuration in slot <code class="varname">[i]</code> of the mapping configuration array <code class="varname">[vorbis_mapping_configurations]</code>.</li></ol></div></li></ol></div><p>
  882. </p></li></ol></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="id342611"></a>4.2.4.6. Modes</h5></div></div></div><div class="orderedlist"><ol type="1"><li><code class="varname">[vorbis_mode_count]</code> = read 6 bits as unsigned integer and add one</li><li><p>For each of <code class="varname">[vorbis_mode_count]</code> mode numbers:</p><div class="orderedlist"><ol type="a"><li><code class="varname">[vorbis_mode_blockflag]</code> = read 1 bit</li><li><code class="varname">[vorbis_mode_windowtype]</code> = read 16 bits as unsigned integer</li><li><code class="varname">[vorbis_mode_transformtype]</code> = read 16 bits as unsigned integer</li><li><code class="varname">[vorbis_mode_mapping]</code> = read 8 bits as unsigned integer</li><li>verify ranges; zero is the only legal value in Vorbis I for
  883. <code class="varname">[vorbis_mode_windowtype]</code>
  884. and <code class="varname">[vorbis_mode_transformtype]</code>. <code class="varname">[vorbis_mode_mapping]</code> must not be greater than the highest number mapping in use. Any illegal values render the stream undecodable.</li><li>save this mode configuration in slot <code class="varname">[i]</code> of the mode configuration array
  885. <code class="varname">[vorbis_mode_configurations]</code>.</li></ol></div></li><li>read 1 bit as a framing flag. If unset, a framing error occurred and the stream is not
  886. decodable.</li></ol></div><p>
  887. After reading mode descriptions, setup header decode is complete.
  888. </p></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id342709"></a>4.3. Audio packet decode and synthesis</h3></div></div></div><p>
  889. Following the three header packets, all packets in a Vorbis I stream
  890. are audio. The first step of audio packet decode is to read and
  891. verify the packet type. <span class="emphasis"><em>A non-audio packet when audio is expected
  892. indicates stream corruption or a non-compliant stream. The decoder
  893. must ignore the packet and not attempt decoding it to audio</em></span>.
  894. </p><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id342724"></a>4.3.1. packet type, mode and window decode</h4></div></div></div><div class="orderedlist"><ol type="1"><li>read 1 bit <code class="varname">[packet_type]</code>; check that packet type is 0 (audio)</li><li>read <a href="#vorbis-spec-ilog" title="9.2.1. ilog">ilog</a>([vorbis_mode_count]-1) bits
  895. <code class="varname">[mode_number]</code></li><li>decode blocksize <code class="varname">[n]</code> is equal to <code class="varname">[blocksize_0]</code> if
  896. <code class="varname">[vorbis_mode_blockflag]</code> is 0, else <code class="varname">[n]</code> is equal to <code class="varname">[blocksize_1]</code>.</li><li><p>perform window selection and setup; this window is used later by the inverse MDCT:</p><div class="orderedlist"><ol type="a"><li><p>if this is a long window (the <code class="varname">[vorbis_mode_blockflag]</code> flag of this mode is
  897. set):</p><div class="orderedlist"><ol type="i"><li>read 1 bit for <code class="varname">[previous_window_flag]</code></li><li>read 1 bit for <code class="varname">[next_window_flag]</code></li><li>if <code class="varname">[previous_window_flag]</code> is not set, the left half
  898. of the window will be a hybrid window for lapping with a
  899. short block. See <a href="#vorbis-spec-window" title="1.3.2.3. Window shape decode (long windows only)">Section 1.3.2.3, &#8220;Window shape decode (long windows only)&#8221;</a> for an illustration of overlapping
  900. dissimilar
  901. windows. Else, the left half window will have normal long
  902. shape.</li><li>if <code class="varname">[next_window_flag]</code> is not set, the right half of
  903. the window will be a hybrid window for lapping with a short
  904. block. See <a href="#vorbis-spec-window" title="1.3.2.3. Window shape decode (long windows only)">Section 1.3.2.3, &#8220;Window shape decode (long windows only)&#8221;</a> for an
  905. illustration of overlapping dissimilar
  906. windows. Else, the left right window will have normal long
  907. shape.</li></ol></div></li><li> if this is a short window, the window is always the same
  908. short-window shape.</li></ol></div></li></ol></div><p>
  909. Vorbis windows all use the slope function y=sin(0.5 * &#960; * sin^2((x+.5)/n * &#960;)),
  910. where n is window size and x ranges 0...n-1, but dissimilar
  911. lapping requirements can affect overall shape. Window generation
  912. proceeds as follows:</p><div class="orderedlist"><ol type="1"><li> <code class="varname">[window_center]</code> = <code class="varname">[n]</code> / 2</li><li><p> if (<code class="varname">[vorbis_mode_blockflag]</code> is set and <code class="varname">[previous_window_flag]</code> is
  913. not set) then
  914. </p><div class="orderedlist"><ol type="a"><li><code class="varname">[left_window_start]</code> = <code class="varname">[n]</code>/4 -
  915. <code class="varname">[blocksize_0]</code>/4</li><li><code class="varname">[left_window_end]</code> = <code class="varname">[n]</code>/4 + <code class="varname">[blocksize_0]</code>/4</li><li><code class="varname">[left_n]</code> = <code class="varname">[blocksize_0]</code>/2</li></ol></div><p>
  916. else
  917. </p><div class="orderedlist"><ol type="a"><li><code class="varname">[left_window_start]</code> = 0</li><li><code class="varname">[left_window_end]</code> = <code class="varname">[window_center]</code></li><li><code class="varname">[left_n]</code> = <code class="varname">[n]</code>/2</li></ol></div></li><li><p> if (<code class="varname">[vorbis_mode_blockflag]</code> is set and <code class="varname">[next_window_flag]</code> is not
  918. set) then
  919. </p><div class="orderedlist"><ol type="a"><li><code class="varname">[right_window_start]</code> = <code class="varname">[n]*3</code>/4 -
  920. <code class="varname">[blocksize_0]</code>/4</li><li><code class="varname">[right_window_end]</code> = <code class="varname">[n]*3</code>/4 +
  921. <code class="varname">[blocksize_0]</code>/4</li><li><code class="varname">[right_n]</code> = <code class="varname">[blocksize_0]</code>/2</li></ol></div><p>
  922. else
  923. </p><div class="orderedlist"><ol type="a"><li><code class="varname">[right_window_start]</code> = <code class="varname">[window_center]</code></li><li><code class="varname">[right_window_end]</code> = <code class="varname">[n]</code></li><li><code class="varname">[right_n]</code> = <code class="varname">[n]</code>/2</li></ol></div></li><li> window from range 0 ... <code class="varname">[left_window_start]</code>-1 inclusive is zero</li><li> for <code class="varname">[i]</code> in range <code class="varname">[left_window_start]</code> ...
  924. <code class="varname">[left_window_end]</code>-1, window(<code class="varname">[i]</code>) = sin(.5 * &#960; * sin^2( (<code class="varname">[i]</code>-<code class="varname">[left_window_start]</code>+.5) / <code class="varname">[left_n]</code> * .5 * &#960;) )</li><li> window from range <code class="varname">[left_window_end]</code> ... <code class="varname">[right_window_start]</code>-1
  925. inclusive is one</li><li> for <code class="varname">[i]</code> in range <code class="varname">[right_window_start]</code> ... <code class="varname">[right_window_end]</code>-1, window(<code class="varname">[i]</code>) = sin(.5 * &#960; * sin^2( (<code class="varname">[i]</code>-<code class="varname">[right_window_start]</code>+.5) / <code class="varname">[right_n]</code> * .5 * &#960; + .5 * &#960;) )</li><li> window from range <code class="varname">[right_window_start]</code> ... <code class="varname">[n]</code>-1 is
  926. zero</li></ol></div><p>
  927. An end-of-packet condition up to this point should be considered an
  928. error that discards this packet from the stream. An end of packet
  929. condition past this point is to be considered a possible nominal
  930. occurrence.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id343132"></a>4.3.2. floor curve decode</h4></div></div></div><p>
  931. From this point on, we assume out decode context is using mode number
  932. <code class="varname">[mode_number]</code> from configuration array
  933. <code class="varname">[vorbis_mode_configurations]</code> and the map number
  934. <code class="varname">[vorbis_mode_mapping]</code> (specified by the current mode) taken
  935. from the mapping configuration array
  936. <code class="varname">[vorbis_mapping_configurations]</code>.</p><p>
  937. Floor curves are decoded one-by-one in channel order.</p><p>
  938. For each floor <code class="varname">[i]</code> of <code class="varname">[audio_channels]</code>
  939. </p><div class="orderedlist"><ol type="1"><li><code class="varname">[submap_number]</code> = element <code class="varname">[i]</code> of vector [vorbis_mapping_mux]</li><li><code class="varname">[floor_number]</code> = element <code class="varname">[submap_number]</code> of vector
  940. [vorbis_submap_floor]</li><li>if the floor type of this
  941. floor (vector <code class="varname">[vorbis_floor_types]</code> element
  942. <code class="varname">[floor_number]</code>) is zero then decode the floor for
  943. channel <code class="varname">[i]</code> according to the
  944. <a href="#vorbis-spec-floor0-decode" title="6.2.2. packet decode">Section 6.2.2, &#8220;packet decode&#8221;</a></li><li>if the type of this floor
  945. is one then decode the floor for channel <code class="varname">[i]</code> according
  946. to the <a href="#vorbis-spec-floor1-decode" title="7.2.2.1. packet decode">Section 7.2.2.1, &#8220;packet decode&#8221;</a></li><li>save the needed decoded floor information for channel for later synthesis</li><li>if the decoded floor returned 'unused', set vector <code class="varname">[no_residue]</code> element
  947. <code class="varname">[i]</code> to true, else set vector <code class="varname">[no_residue]</code> element <code class="varname">[i]</code> to
  948. false</li></ol></div><p>
  949. </p><p>
  950. An end-of-packet condition during floor decode shall result in packet
  951. decode zeroing all channel output vectors and skipping to the
  952. add/overlap output stage.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id343249"></a>4.3.3. nonzero vector propagate</h4></div></div></div><p>
  953. A possible result of floor decode is that a specific vector is marked
  954. 'unused' which indicates that that final output vector is all-zero
  955. values (and the floor is zero). The residue for that vector is not
  956. coded in the stream, save for one complication. If some vectors are
  957. used and some are not, channel coupling could result in mixing a
  958. zeroed and nonzeroed vector to produce two nonzeroed vectors.</p><p>
  959. for each <code class="varname">[i]</code> from 0 ... <code class="varname">[vorbis_mapping_coupling_steps]</code>-1
  960. </p><div class="orderedlist"><ol type="1"><li>if either <code class="varname">[no_residue]</code> entry for channel
  961. (<code class="varname">[vorbis_mapping_magnitude]</code> element <code class="varname">[i]</code>)
  962. or channel
  963. (<code class="varname">[vorbis_mapping_angle]</code> element <code class="varname">[i]</code>)
  964. are set to false, then both must be set to false. Note that an 'unused'
  965. floor has no decoded floor information; it is important that this is
  966. remembered at floor curve synthesis time.</li></ol></div><p>
  967. </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id343299"></a>4.3.4. residue decode</h4></div></div></div><p>
  968. Unlike floors, which are decoded in channel order, the residue vectors
  969. are decoded in submap order.</p><p>
  970. for each submap <code class="varname">[i]</code> in order from 0 ... <code class="varname">[vorbis_mapping_submaps]</code>-1</p><div class="orderedlist"><ol type="1"><li><code class="varname">[ch]</code> = 0</li><li><p>for each channel <code class="varname">[j]</code> in order from 0 ... <code class="varname">[audio_channels]</code> - 1</p><div class="orderedlist"><ol type="a"><li><p>if channel <code class="varname">[j]</code> in submap <code class="varname">[i]</code> (vector <code class="varname">[vorbis_mapping_mux]</code> element <code class="varname">[j]</code> is equal to <code class="varname">[i]</code>)</p><div class="orderedlist"><ol type="i"><li><p>if vector <code class="varname">[no_residue]</code> element <code class="varname">[j]</code> is true
  971. </p><div class="orderedlist"><ol type="A"><li>vector <code class="varname">[do_not_decode_flag]</code> element <code class="varname">[ch]</code> is set</li></ol></div><p>
  972. else
  973. </p><div class="orderedlist"><ol type="A"><li>vector <code class="varname">[do_not_decode_flag]</code> element <code class="varname">[ch]</code> is unset</li></ol></div></li><li>increment <code class="varname">[ch]</code></li></ol></div></li></ol></div></li><li><code class="varname">[residue_number]</code> = vector <code class="varname">[vorbis_mapping_submap_residue]</code> element <code class="varname">[i]</code></li><li><code class="varname">[residue_type]</code> = vector <code class="varname">[vorbis_residue_types]</code> element <code class="varname">[residue_number]</code></li><li>decode <code class="varname">[ch]</code> vectors using residue <code class="varname">[residue_number]</code>, according to type <code class="varname">[residue_type]</code>, also passing vector <code class="varname">[do_not_decode_flag]</code> to indicate which vectors in the bundle should not be decoded. Correct per-vector decode length is <code class="varname">[n]</code>/2.</li><li><code class="varname">[ch]</code> = 0</li><li><p>for each channel <code class="varname">[j]</code> in order from 0 ... <code class="varname">[audio_channels]</code></p><div class="orderedlist"><ol type="a"><li><p>if channel <code class="varname">[j]</code> is in submap <code class="varname">[i]</code> (vector <code class="varname">[vorbis_mapping_mux]</code> element <code class="varname">[j]</code> is equal to <code class="varname">[i]</code>)</p><div class="orderedlist"><ol type="i"><li>residue vector for channel <code class="varname">[j]</code> is set to decoded residue vector <code class="varname">[ch]</code></li><li>increment <code class="varname">[ch]</code></li></ol></div></li></ol></div></li></ol></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id343545"></a>4.3.5. inverse coupling</h4></div></div></div><p>
  974. for each <code class="varname">[i]</code> from <code class="varname">[vorbis_mapping_coupling_steps]</code>-1 descending to 0
  975. </p><div class="orderedlist"><ol type="1"><li><code class="varname">[magnitude_vector]</code> = the residue vector for channel
  976. (vector <code class="varname">[vorbis_mapping_magnitude]</code> element <code class="varname">[i]</code>)</li><li><code class="varname">[angle_vector]</code> = the residue vector for channel (vector
  977. <code class="varname">[vorbis_mapping_angle]</code> element <code class="varname">[i]</code>)</li><li><p>for each scalar value <code class="varname">[M]</code> in vector <code class="varname">[magnitude_vector]</code> and the corresponding scalar value <code class="varname">[A]</code> in vector <code class="varname">[angle_vector]</code>:</p><div class="orderedlist"><ol type="a"><li><p>if (<code class="varname">[M]</code> is greater than zero)
  978. </p><div class="orderedlist"><ol type="i"><li><p>if (<code class="varname">[A]</code> is greater than zero)
  979. </p><div class="orderedlist"><ol type="A"><li><code class="varname">[new_M]</code> = <code class="varname">[M]</code></li><li><code class="varname">[new_A]</code> = <code class="varname">[M]</code>-<code class="varname">[A]</code></li></ol></div><p>
  980. else
  981. </p><div class="orderedlist"><ol type="A"><li><code class="varname">[new_A]</code> = <code class="varname">[M]</code></li><li><code class="varname">[new_M]</code> = <code class="varname">[M]</code>+<code class="varname">[A]</code></li></ol></div><p>
  982. </p></li></ol></div><p>
  983. else
  984. </p><div class="orderedlist"><ol type="i"><li><p>if (<code class="varname">[A]</code> is greater than zero)
  985. </p><div class="orderedlist"><ol type="A"><li><code class="varname">[new_M]</code> = <code class="varname">[M]</code></li><li><code class="varname">[new_A]</code> = <code class="varname">[M]</code>+<code class="varname">[A]</code></li></ol></div><p>
  986. else
  987. </p><div class="orderedlist"><ol type="A"><li><code class="varname">[new_A]</code> = <code class="varname">[M]</code></li><li><code class="varname">[new_M]</code> = <code class="varname">[M]</code>-<code class="varname">[A]</code></li></ol></div><p>
  988. </p></li></ol></div><p>
  989. </p></li><li>set scalar value <code class="varname">[M]</code> in vector <code class="varname">[magnitude_vector]</code> to <code class="varname">[new_M]</code></li><li>set scalar value <code class="varname">[A]</code> in vector <code class="varname">[angle_vector]</code> to <code class="varname">[new_A]</code></li></ol></div></li></ol></div><p>
  990. </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id343790"></a>4.3.6. dot product</h4></div></div></div><p>
  991. For each channel, synthesize the floor curve from the decoded floor
  992. information, according to packet type. Note that the vector synthesis
  993. length for floor computation is <code class="varname">[n]</code>/2.</p><p>
  994. For each channel, multiply each element of the floor curve by each
  995. element of that channel's residue vector. The result is the dot
  996. product of the floor and residue vectors for each channel; the produced
  997. vectors are the length <code class="varname">[n]</code>/2 audio spectrum for each
  998. channel.</p><p>
  999. One point is worth mentioning about this dot product; a common mistake
  1000. in a fixed point implementation might be to assume that a 32 bit
  1001. fixed-point representation for floor and residue and direct
  1002. multiplication of the vectors is sufficient for acceptable spectral
  1003. depth in all cases because it happens to mostly work with the current
  1004. Xiph.Org reference encoder. </p><p>
  1005. However, floor vector values can span ~140dB (~24 bits unsigned), and
  1006. the audio spectrum vector should represent a minimum of 120dB (~21
  1007. bits with sign), even when output is to a 16 bit PCM device. For the
  1008. residue vector to represent full scale if the floor is nailed to
  1009. -140dB, it must be able to span 0 to +140dB. For the residue vector
  1010. to reach full scale if the floor is nailed at 0dB, it must be able to
  1011. represent -140dB to +0dB. Thus, in order to handle full range
  1012. dynamics, a residue vector may span -140dB to +140dB entirely within
  1013. spec. A 280dB range is approximately 48 bits with sign; thus the
  1014. residue vector must be able to represent a 48 bit range and the dot
  1015. product must be able to handle an effective 48 bit times 24 bit
  1016. multiplication. This range may be achieved using large (64 bit or
  1017. larger) integers, or implementing a movable binary point
  1018. representation.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id343829"></a>4.3.7. inverse MDCT</h4></div></div></div><p>
  1019. Convert the audio spectrum vector of each channel back into time
  1020. domain PCM audio via an inverse Modified Discrete Cosine Transform
  1021. (MDCT). A detailed description of the MDCT is available in the paper
  1022. <a href="http://www.iocon.com/resource/docs/ps/eusipco_corrected.ps" target="_top">&#8220;<span class="citetitle">The
  1023. use of multirate filter banks for coding of high quality digital
  1024. audio</span>&#8221;</a>, by T. Sporer, K. Brandenburg and B. Edler. The window
  1025. function used for the MDCT is the function described earlier.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id343850"></a>4.3.8. overlap_add</h4></div></div></div><p>
  1026. Windowed MDCT output is overlapped and added with the right hand data
  1027. of the previous window such that the 3/4 point of the previous window
  1028. is aligned with the 1/4 point of the current window (as illustrated in
  1029. <a href="#vorbis-spec-window" title="1.3.2.3. Window shape decode (long windows only)">Section 1.3.2.3, &#8220;Window shape decode (long windows only)&#8221;</a>). The overlapped portion
  1030. produced from overlapping the previous and current frame data is
  1031. finished data to be returned by the decoder. This data spans from the
  1032. center of the previous window to the center of the current window. In
  1033. the case of same-sized windows, the amount of data to return is
  1034. one-half block consisting of and only of the overlapped portions. When
  1035. overlapping a short and long window, much of the returned range does not
  1036. actually overlap. This does not damage transform orthogonality. Pay
  1037. attention however to returning the correct data range; the amount of
  1038. data to be returned is:
  1039. </p><pre class="programlisting">
  1040. window_blocksize(previous_window)/4+window_blocksize(current_window)/4
  1041. </pre><p>
  1042. from the center (element windowsize/2) of the previous window to the
  1043. center (element windowsize/2-1, inclusive) of the current window.</p><p>
  1044. Data is not returned from the first frame; it must be used to 'prime'
  1045. the decode engine. The encoder accounts for this priming when
  1046. calculating PCM offsets; after the first frame, the proper PCM output
  1047. offset is '0' (as no data has been returned yet).</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id343883"></a>4.3.9. output channel order</h4></div></div></div><p>
  1048. Vorbis I specifies only a channel mapping type 0. In mapping type 0,
  1049. channel mapping is implicitly defined as follows for standard audio
  1050. applications:</p><div class="variablelist"><dl><dt><span class="term">one channel</span></dt><dd>the stream is monophonic</dd><dt><span class="term">two channels</span></dt><dd>the stream is stereo. channel order: left, right</dd><dt><span class="term">three channels</span></dt><dd>the stream is a 1d-surround encoding. channel order: left,
  1051. center, right</dd><dt><span class="term">four channels</span></dt><dd>the stream is quadraphonic surround. channel order: front left,
  1052. front right, rear left, rear right</dd><dt><span class="term">five channels</span></dt><dd>the stream is five-channel surround. channel order: front left,
  1053. front center, front right, rear left, rear right</dd><dt><span class="term">six channels</span></dt><dd>the stream is 5.1 surround. channel order: front left, front
  1054. center, front right, rear left, rear right, LFE</dd><dt><span class="term">greater than six channels</span></dt><dd>channel use and order is defined by the application</dd></dl></div><p>
  1055. Applications using Vorbis for dedicated purposes may define channel
  1056. mapping as seen fit. Future channel mappings (such as three and four
  1057. channel <a href="http://www.ambisonic.net/" target="_top">Ambisonics</a>) will
  1058. make use of channel mappings other than mapping 0.</p></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="vorbis-spec-comment"></a>5. comment field and header specification</h2></div><div><p class="releaseinfo">
  1059. $Id: 05-comment.xml 11703 2006-07-17 16:33:17Z giles $
  1060. </p></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id314030"></a>5.1. Overview</h3></div></div></div><p>The Vorbis text comment header is the second (of three) header
  1061. packets that begin a Vorbis bitstream. It is meant for short text
  1062. comments, not arbitrary metadata; arbitrary metadata belongs in a
  1063. separate logical bitstream (usually an XML stream type) that provides
  1064. greater structure and machine parseability.</p><p>The comment field is meant to be used much like someone jotting a
  1065. quick note on the bottom of a CDR. It should be a little information to
  1066. remember the disc by and explain it to others; a short, to-the-point
  1067. text note that need not only be a couple words, but isn't going to be
  1068. more than a short paragraph. The essentials, in other words, whatever
  1069. they turn out to be, eg:
  1070. </p><div class="blockquote"><blockquote class="blockquote"><p>Honest Bob and the Factory-to-Dealer-Incentives, <em class="citetitle">I'm Still
  1071. Around</em>, opening for Moxy Früvous, 1997.</p></blockquote></div><p>
  1072. </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id314058"></a>5.2. Comment encoding</h3></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id322574"></a>5.2.1. Structure</h4></div></div></div><p>
  1073. The comment header is logically a list of eight-bit-clean vectors; the
  1074. number of vectors is bounded to 2^32-1 and the length of each vector
  1075. is limited to 2^32-1 bytes. The vector length is encoded; the vector
  1076. contents themselves are not null terminated. In addition to the vector
  1077. list, there is a single vector for vendor name (also 8 bit clean,
  1078. length encoded in 32 bits). For example, the 1.0 release of libvorbis
  1079. set the vendor string to "Xiph.Org libVorbis I 20020717".</p><p>The comment header is decoded as follows:
  1080. </p><pre class="programlisting">
  1081. 1) [vendor_length] = read an unsigned integer of 32 bits
  1082. 2) [vendor_string] = read a UTF-8 vector as [vendor_length] octets
  1083. 3) [user_comment_list_length] = read an unsigned integer of 32 bits
  1084. 4) iterate [user_comment_list_length] times {
  1085. 5) [length] = read an unsigned integer of 32 bits
  1086. 6) this iteration's user comment = read a UTF-8 vector as [length] octets
  1087. }
  1088. 7) [framing_bit] = read a single bit as boolean
  1089. 8) if ( [framing_bit] unset or end-of-packet ) then ERROR
  1090. 9) done.
  1091. </pre><p>
  1092. </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id326883"></a>5.2.2. Content vector format</h4></div></div></div><p>
  1093. The comment vectors are structured similarly to a UNIX environment variable.
  1094. That is, comment fields consist of a field name and a corresponding value and
  1095. look like:</p><div class="blockquote"><blockquote class="blockquote"><pre class="programlisting">
  1096. comment[0]="ARTIST=me";
  1097. comment[1]="TITLE=the sound of Vorbis";
  1098. </pre></blockquote></div><p>
  1099. The field name is case-insensitive and may consist of ASCII 0x20
  1100. through 0x7D, 0x3D ('=') excluded. ASCII 0x41 through 0x5A inclusive
  1101. (characters A-Z) is to be considered equivalent to ASCII 0x61 through
  1102. 0x7A inclusive (characters a-z).
  1103. </p><p>
  1104. The field name is immediately followed by ASCII 0x3D ('=');
  1105. this equals sign is used to terminate the field name.
  1106. </p><p>
  1107. 0x3D is followed by 8 bit clean UTF-8 encoded value of the
  1108. field contents to the end of the field.
  1109. </p><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="id322620"></a>5.2.2.1. Field names</h5></div></div></div><p>Below is a proposed, minimal list of standard field names with a
  1110. description of intended use. No single or group of field names is
  1111. mandatory; a comment header may contain one, all or none of the names
  1112. in this list.</p><div class="variablelist"><dl><dt><span class="term">TITLE</span></dt><dd>Track/Work name</dd><dt><span class="term">VERSION</span></dt><dd>The version field may be used to
  1113. differentiate multiple
  1114. versions of the same track title in a single collection. (e.g. remix
  1115. info)
  1116. </dd><dt><span class="term">ALBUM</span></dt><dd>The collection name to which this track belongs
  1117. </dd><dt><span class="term">TRACKNUMBER</span></dt><dd>The track number of this piece if part of a specific larger collection or album
  1118. </dd><dt><span class="term">ARTIST</span></dt><dd>The artist generally considered responsible for the work. In popular music this is usually the performing band or singer. For classical music it would be the composer. For an audio book it would be the author of the original text.
  1119. </dd><dt><span class="term">PERFORMER</span></dt><dd>The artist(s) who performed the work. In classical music this would be the conductor, orchestra, soloists. In an audio book it would be the actor who did the reading. In popular music this is typically the same as the ARTIST and is omitted.
  1120. </dd><dt><span class="term">COPYRIGHT</span></dt><dd>Copyright attribution, e.g., '2001 Nobody's Band' or '1999 Jack Moffitt'
  1121. </dd><dt><span class="term">LICENSE</span></dt><dd>License information, eg, 'All Rights Reserved', 'Any
  1122. Use Permitted', a URL to a license such as a Creative Commons license
  1123. ("www.creativecommons.org/blahblah/license.html") or the EFF Open
  1124. Audio License ('distributed under the terms of the Open Audio
  1125. License. see http://www.eff.org/IP/Open_licenses/eff_oal.html for
  1126. details'), etc.
  1127. </dd><dt><span class="term">ORGANIZATION</span></dt><dd>Name of the organization producing the track (i.e.
  1128. the 'record label')
  1129. </dd><dt><span class="term">DESCRIPTION</span></dt><dd>A short text description of the contents
  1130. </dd><dt><span class="term">GENRE</span></dt><dd>A short text indication of music genre
  1131. </dd><dt><span class="term">DATE</span></dt><dd>Date the track was recorded
  1132. </dd><dt><span class="term">LOCATION</span></dt><dd>Location where track was recorded
  1133. </dd><dt><span class="term">CONTACT</span></dt><dd>Contact information for the creators or distributors of the track. This could be a URL, an email address, the physical address of the producing label.
  1134. </dd><dt><span class="term">ISRC</span></dt><dd>International Standard Recording Code for the
  1135. track; see <a href="http://www.ifpi.org/isrc/" target="_top">the ISRC
  1136. intro page</a> for more information on ISRC numbers.
  1137. </dd></dl></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="id306349"></a>5.2.2.2. Implications</h5></div></div></div><p>Field names should not be 'internationalized'; this is a
  1138. concession to simplicity not an attempt to exclude the majority of
  1139. the world that doesn't speak English. Field <span class="emphasis"><em>contents</em></span>,
  1140. however, use the UTF-8 character encoding to allow easy representation
  1141. of any language.</p><p>We have the length of the entirety of the field and restrictions on
  1142. the field name so that the field name is bounded in a known way. Thus
  1143. we also have the length of the field contents.</p><p>Individual 'vendors' may use non-standard field names within
  1144. reason. The proper use of comment fields should be clear through
  1145. context at this point. Abuse will be discouraged.</p><p>There is no vendor-specific prefix to 'nonstandard' field names.
  1146. Vendors should make some effort to avoid arbitrarily polluting the
  1147. common namespace. We will generally collect the more useful tags
  1148. here to help with standardization.</p><p>Field names are not required to be unique (occur once) within a
  1149. comment header. As an example, assume a track was recorded by three
  1150. well know artists; the following is permissible, and encouraged:
  1151. </p><div class="blockquote"><blockquote class="blockquote"><pre class="programlisting">
  1152. ARTIST=Dizzy Gillespie
  1153. ARTIST=Sonny Rollins
  1154. ARTIST=Sonny Stitt
  1155. </pre></blockquote></div><p>
  1156. </p></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id306394"></a>5.2.3. Encoding</h4></div></div></div><p>
  1157. The comment header comprises the entirety of the second bitstream
  1158. header packet. Unlike the first bitstream header packet, it is not
  1159. generally the only packet on the second page and may not be restricted
  1160. to within the second bitstream page. The length of the comment header
  1161. packet is (practically) unbounded. The comment header packet is not
  1162. optional; it must be present in the bitstream even if it is
  1163. effectively empty.</p><p>
  1164. The comment header is encoded as follows (as per Ogg's standard
  1165. bitstream mapping which renders least-significant-bit of the word to be
  1166. coded into the least significant available bit of the current
  1167. bitstream octet first):
  1168. </p><div class="orderedlist"><ol type="1"><li>
  1169. Vendor string length (32 bit unsigned quantity specifying number of octets)
  1170. </li><li>
  1171. Vendor string ([vendor string length] octets coded from beginning of string to end of string, not null terminated)
  1172. </li><li>
  1173. Number of comment fields (32 bit unsigned quantity specifying number of fields)
  1174. </li><li>
  1175. Comment field 0 length (if [Number of comment fields]&gt;0; 32 bit unsigned quantity specifying number of octets)
  1176. </li><li>
  1177. Comment field 0 ([Comment field 0 length] octets coded from beginning of string to end of string, not null terminated)
  1178. </li><li>
  1179. Comment field 1 length (if [Number of comment fields]&gt;1...)...
  1180. </li></ol></div><p>
  1181. </p><p>
  1182. This is actually somewhat easier to describe in code; implementation of the above can be found in <code class="filename">vorbis/lib/info.c</code>, <code class="function">_vorbis_pack_comment()</code> and <code class="function">_vorbis_unpack_comment()</code>.
  1183. </p></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="vorbis-spec-floor0"></a>6. Floor type 0 setup and decode</h2></div><div><p class="releaseinfo">
  1184. $Id: 06-floor0.xml 10424 2005-11-23 08:44:18Z xiphmont $
  1185. </p></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id336814"></a>6.1. Overview</h3></div></div></div><p>
  1186. Vorbis floor type zero uses Line Spectral Pair (LSP, also alternately
  1187. known as Line Spectral Frequency or LSF) representation to encode a
  1188. smooth spectral envelope curve as the frequency response of the LSP
  1189. filter. This representation is equivalent to a traditional all-pole
  1190. infinite impulse response filter as would be used in linear predictive
  1191. coding; LSP representation may be converted to LPC representation and
  1192. vice-versa.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id321046"></a>6.2. Floor 0 format</h3></div></div></div><p>
  1193. Floor zero configuration consists of six integer fields and a list of
  1194. VQ codebooks for use in coding/decoding the LSP filter coefficient
  1195. values used by each frame. </p><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id313179"></a>6.2.1. header decode</h4></div></div></div><p>
  1196. Configuration information for instances of floor zero decodes from the
  1197. codec setup header (third packet). configuration decode proceeds as
  1198. follows:</p><pre class="screen">
  1199. 1) [floor0_order] = read an unsigned integer of 8 bits
  1200. 2) [floor0_rate] = read an unsigned integer of 16 bits
  1201. 3) [floor0_bark_map_size] = read an unsigned integer of 16 bits
  1202. 4) [floor0_amplitude_bits] = read an unsigned integer of six bits
  1203. 5) [floor0_amplitude_offset] = read an unsigned integer of eight bits
  1204. 6) [floor0_number_of_books] = read an unsigned integer of four bits and add 1
  1205. 7) if any of [floor0_order], [floor0_rate], [floor0_bark_map_size], [floor0_amplitude_bits],
  1206. [floor0_amplitude_offset] or [floor0_number_of_books] are less than zero, the stream is not decodable
  1207. 8) array [floor0_book_list] = read a list of [floor0_number_of_books] unsigned integers of eight bits each;
  1208. </pre><p>
  1209. An end-of-packet condition during any of these bitstream reads renders
  1210. this stream undecodable. In addition, any element of the array
  1211. <code class="varname">[floor0_book_list]</code> that is greater than the maximum codebook
  1212. number for this bitstream is an error condition that also renders the
  1213. stream undecodable.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="vorbis-spec-floor0-decode"></a>6.2.2. packet decode</h4></div></div></div><p>
  1214. Extracting a floor0 curve from an audio packet consists of first
  1215. decoding the curve amplitude and <code class="varname">[floor0_order]</code> LSP
  1216. coefficient values from the bitstream, and then computing the floor
  1217. curve, which is defined as the frequency response of the decoded LSP
  1218. filter.</p><p>
  1219. Packet decode proceeds as follows:</p><pre class="screen">
  1220. 1) [amplitude] = read an unsigned integer of [floor0_amplitude_bits] bits
  1221. 2) if ( [amplitude] is greater than zero ) {
  1222. 3) [coefficients] is an empty, zero length vector
  1223. 4) [booknumber] = read an unsigned integer of <a href="#vorbis-spec-ilog" title="9.2.1. ilog">ilog</a>( [floor0_number_of_books] ) bits
  1224. 5) if ( [booknumber] is greater than the highest number decode codebook ) then packet is undecodable
  1225. 6) [last] = zero;
  1226. 7) vector [temp_vector] = read vector from bitstream using codebook number [floor0_book_list] element [booknumber] in VQ context.
  1227. 8) add the scalar value [last] to each scalar in vector [temp_vector]
  1228. 9) [last] = the value of the last scalar in vector [temp_vector]
  1229. 10) concatenate [temp_vector] onto the end of the [coefficients] vector
  1230. 11) if (length of vector [coefficients] is less than [floor0_order], continue at step 6
  1231. }
  1232. 12) done.
  1233. </pre><p>
  1234. Take note of the following properties of decode:
  1235. </p><div class="itemizedlist"><ul type="disc"><li>An <code class="varname">[amplitude]</code> 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.</li><li>An end-of-packet condition during decode should be considered a
  1236. nominal occruence; if end-of-packet is reached during any read
  1237. operation above, floor decode is to return 'unused' status as if the
  1238. <code class="varname">[amplitude]</code> value had read zero at the beginning of decode.</li><li>The book number used for decode
  1239. can, in fact, be stored in the bitstream in <a href="#vorbis-spec-ilog" title="9.2.1. ilog">ilog</a>( <code class="varname">[floor0_number_of_books]</code> -
  1240. 1 ) bits. Nevertheless, the above specification is correct and values
  1241. greater than the maximum possible book value are reserved.</li><li>The number of scalars read into the vector <code class="varname">[coefficients]</code>
  1242. may be greater than <code class="varname">[floor0_order]</code>, the number actually
  1243. required for curve computation. For example, if the VQ codebook used
  1244. for the floor currently being decoded has a
  1245. <code class="varname">[codebook_dimensions]</code> value of three and
  1246. <code class="varname">[floor0_order]</code> is ten, the only way to fill all the needed
  1247. scalars in <code class="varname">[coefficients]</code> is to to read a total of twelve
  1248. scalars as four vectors of three scalars each. This is not an error
  1249. condition, and care must be taken not to allow a buffer overflow in
  1250. decode. The extra values are not used and may be ignored or discarded.</li></ul></div><p>
  1251. </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="vorbis-spec-floor0-synth"></a>6.2.3. curve computation</h4></div></div></div><p>
  1252. Given an <code class="varname">[amplitude]</code> integer and <code class="varname">[coefficients]</code>
  1253. vector from packet decode as well as the [floor0_order],
  1254. [floor0_rate], [floor0_bark_map_size], [floor0_amplitude_bits] and
  1255. [floor0_amplitude_offset] values from floor setup, and an output
  1256. vector size <code class="varname">[n]</code> specified by the decode process, we compute a
  1257. floor output vector.</p><p>
  1258. If the value <code class="varname">[amplitude]</code> is zero, the return value is a
  1259. length <code class="varname">[n]</code> vector with all-zero scalars. Otherwise, begin by
  1260. assuming the following definitions for the given vector to be
  1261. synthesized:</p><div class="informalequation"><div class="mediaobject"><img src="lspmap.png" alt="[lsp map equation]"></div></div><p>
  1262. The above is used to synthesize the LSP curve on a Bark-scale frequency
  1263. axis, then map the result to a linear-scale frequency axis.
  1264. Similarly, the below calculation synthesizes the output LSP curve <code class="varname">[output]</code> on a log
  1265. (dB) amplitude scale, mapping it to linear amplitude in the last step:</p><div class="orderedlist"><ol type="1"><li> <code class="varname">[i]</code> = 0 </li><li><p>if ( <code class="varname">[floor0_order]</code> is odd ) {
  1266. </p><div class="orderedlist"><ol type="a"><li><p>calculate <code class="varname">[p]</code> and <code class="varname">[q]</code> according to:
  1267. </p><div class="informalequation"><div class="mediaobject"><img src="oddlsp.png" alt="[equation for odd lsp]"></div></div><p>
  1268. </p></li></ol></div><p>
  1269. } else <code class="varname">[floor0_order]</code> is even {
  1270. </p><div class="orderedlist"><ol type="a"><li><p>calculate <code class="varname">[p]</code> and <code class="varname">[q]</code> according to:
  1271. </p><div class="informalequation"><div class="mediaobject"><img src="evenlsp.png" alt="[equation for even lsp]"></div></div><p>
  1272. </p></li></ol></div><p>
  1273. }
  1274. </p></li><li><p>calculate <code class="varname">[linear_floor_value]</code> according to:
  1275. </p><div class="informalequation"><div class="mediaobject"><img src="floorval.png" alt="[expression for floorval]"></div></div><p>
  1276. </p></li><li><code class="varname">[iteration_condition]</code> = map element <code class="varname">[i]</code></li><li><code class="varname">[output]</code> element <code class="varname">[i]</code> = <code class="varname">[linear_floor_value]</code></li><li>increment <code class="varname">[i]</code></li><li>if ( map element <code class="varname">[i]</code> is equal to <code class="varname">[iteration_condition]</code> ) continue at step 5</li><li>if ( <code class="varname">[i]</code> is less than <code class="varname">[n]</code> ) continue at step 2</li><li>done</li></ol></div></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="vorbis-spec-floor1"></a>7. Floor type 1 setup and decode</h2></div><div><p class="releaseinfo">
  1277. $Id: 07-floor1.xml 10466 2005-11-28 00:34:44Z giles $
  1278. </p></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id336243"></a>7.1. Overview</h3></div></div></div><p>
  1279. Vorbis floor type one uses a piecewise straight-line representation to
  1280. encode a spectral envelope curve. The representation plots this curve
  1281. mechanically on a linear frequency axis and a logarithmic (dB)
  1282. amplitude axis. The integer plotting algorithm used is similar to
  1283. Bresenham's algorithm.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id334800"></a>7.2. Floor 1 format</h3></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id316161"></a>7.2.1. model</h4></div></div></div><p>
  1284. Floor type one represents a spectral curve as a series of
  1285. line segments. Synthesis constructs a floor curve using iterative
  1286. prediction in a process roughly equivalent to the following simplified
  1287. description:</p><p>
  1288. </p><div class="itemizedlist"><ul type="disc"><li> the first line segment (base case) is a logical line spanning
  1289. from x_0,y_0 to x_1,y_1 where in the base case x_0=0 and x_1=[n], the
  1290. full range of the spectral floor to be computed.</li><li>the induction step chooses a point x_new within an existing
  1291. logical line segment and produces a y_new value at that point computed
  1292. from the existing line's y value at x_new (as plotted by the line) and
  1293. a difference value decoded from the bitstream packet.</li><li>floor computation produces two new line segments, one running from
  1294. x_0,y_0 to x_new,y_new and from x_new,y_new to x_1,y_1. This step is
  1295. performed logically even if y_new represents no change to the
  1296. amplitude value at x_new so that later refinement is additionally
  1297. bounded at x_new.</li><li>the induction step repeats, using a list of x values specified in
  1298. the codec setup header at floor 1 initialization time. Computation
  1299. is completed at the end of the x value list.</li></ul></div><p>
  1300. </p><p>
  1301. Consider the following example, with values chosen for ease of
  1302. understanding rather than representing typical configuration:</p><p>
  1303. For the below example, we assume a floor setup with an [n] of 128.
  1304. The list of selected X values in increasing order is
  1305. 0,16,32,48,64,80,96,112 and 128. In list order, the values interleave
  1306. as 0, 128, 64, 32, 96, 16, 48, 80 and 112. The corresponding
  1307. list-order Y values as decoded from an example packet are 110, 20, -5,
  1308. -45, 0, -25, -10, 30 and -10. We compute the floor in the following
  1309. way, beginning with the first line:</p><div class="mediaobject"><img src="floor1-1.png" alt="[graph of example floor]"></div><p>
  1310. We now draw new logical lines to reflect the correction to new_Y, and
  1311. iterate for X positions 32 and 96:</p><div class="mediaobject"><img src="floor1-2.png" alt="[graph of example floor]"></div><p>
  1312. Although the new Y value at X position 96 is unchanged, it is still
  1313. used later as an endpoint for further refinement. From here on, the
  1314. pattern should be clear; we complete the floor computation as follows:</p><div class="mediaobject"><img src="floor1-3.png" alt="[graph of example floor]"></div><div class="mediaobject"><img src="floor1-4.png" alt="[graph of example floor]"></div><p>
  1315. A more efficient algorithm with carefully defined integer rounding
  1316. behavior is used for actual decode, as described later. The actual
  1317. algorithm splits Y value computation and line plotting into two steps
  1318. with modifications to the above algorithm to eliminate noise
  1319. accumulation through integer roundoff/truncation. </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id317351"></a>7.2.2. header decode</h4></div></div></div><p>
  1320. A list of floor X values is stored in the packet header in interleaved
  1321. format (used in list order during packet decode and synthesis). This
  1322. list is split into partitions, and each partition is assigned to a
  1323. partition class. X positions 0 and [n] are implicit and do not belong
  1324. to an explicit partition or partition class.</p><p>
  1325. A partition class consists of a representation vector width (the
  1326. number of Y values which the partition class encodes at once), a
  1327. 'subclass' value representing the number of alternate entropy books
  1328. the partition class may use in representing Y values, the list of
  1329. [subclass] books and a master book used to encode which alternate
  1330. books were chosen for representation in a given packet. The
  1331. master/subclass mechanism is meant to be used as a flexible
  1332. representation cascade while still using codebooks only in a scalar
  1333. context.</p><pre class="screen">
  1334. 1) [floor1_partitions] = read 5 bits as unsigned integer
  1335. 2) [maximum_class] = -1
  1336. 3) iterate [i] over the range 0 ... [floor1_partitions]-1 {
  1337. 4) vector [floor1_partition_class_list] element [i] = read 4 bits as unsigned integer
  1338. }
  1339. 5) [maximum_class] = largest integer scalar value in vector [floor1_partition_class_list]
  1340. 6) iterate [i] over the range 0 ... [maximum_class] {
  1341. 7) vector [floor1_class_dimensions] element [i] = read 3 bits as unsigned integer and add 1
  1342. 8) vector [floor1_class_subclasses] element [i] = read 2 bits as unsigned integer
  1343. 9) if ( vector [floor1_class_subclasses] element [i] is nonzero ) {
  1344. 10) vector [floor1_class_masterbooks] element [i] = read 8 bits as unsigned integer
  1345. }
  1346. 11) iterate [j] over the range 0 ... (2 exponent [floor1_class_subclasses] element [i]) - 1 {
  1347. 12) array [floor1_subclass_books] element [i],[j] =
  1348. read 8 bits as unsigned integer and subtract one
  1349. }
  1350. }
  1351. 13) [floor1_multiplier] = read 2 bits as unsigned integer and add one
  1352. 14) [rangebits] = read 4 bits as unsigned integer
  1353. 15) vector [floor1_X_list] element [0] = 0
  1354. 16) vector [floor1_X_list] element [1] = 2 exponent [rangebits];
  1355. 17) [floor1_values] = 2
  1356. 18) iterate [i] over the range 0 ... [floor1_partitions]-1 {
  1357. 19) [current_class_number] = vector [floor1_partition_class_list] element [i]
  1358. 20) iterate [j] over the range 0 ... ([floor1_class_dimensions] element [current_class_number])-1 {
  1359. 21) vector [floor1_X_list] element ([floor1_values]) =
  1360. read [rangebits] bits as unsigned integer
  1361. 22) increment [floor1_values] by one
  1362. }
  1363. }
  1364. 23) done
  1365. </pre><p>
  1366. An end-of-packet condition while reading any aspect of a floor 1
  1367. configuration during setup renders a stream undecodable. In
  1368. addition, a <code class="varname">[floor1_class_masterbooks]</code> or
  1369. <code class="varname">[floor1_subclass_books]</code> scalar element greater than the
  1370. highest numbered codebook configured in this stream is an error
  1371. condition that renders the stream undecodable.</p><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="vorbis-spec-floor1-decode"></a>7.2.2.1. packet decode</h5></div></div></div><p>
  1372. Packet decode begins by checking the <code class="varname">[nonzero]</code> flag:</p><pre class="screen">
  1373. 1) [nonzero] = read 1 bit as boolean
  1374. </pre><p>
  1375. If <code class="varname">[nonzero]</code> is unset, that indicates this channel contained
  1376. no audio energy in this frame. Decode immediately returns a status
  1377. indicating this floor curve (and thus this channel) is unused this
  1378. frame. (A return status of 'unused' is different from decoding a
  1379. floor that has all points set to minimum representation amplitude,
  1380. which happens to be approximately -140dB).
  1381. </p><p>
  1382. Assuming <code class="varname">[nonzero]</code> is set, decode proceeds as follows:</p><pre class="screen">
  1383. 1) [range] = vector { 256, 128, 86, 64 } element ([floor1_multiplier]-1)
  1384. 2) vector [floor1_Y] element [0] = read <a href="#vorbis-spec-ilog" title="9.2.1. ilog">ilog</a>([range]-1) bits as unsigned integer
  1385. 3) vector [floor1_Y] element [1] = read <a href="#vorbis-spec-ilog" title="9.2.1. ilog">ilog</a>([range]-1) bits as unsigned integer
  1386. 4) [offset] = 2;
  1387. 5) iterate [i] over the range 0 ... [floor1_partitions]-1 {
  1388. 6) [class] = vector [floor1_partition_class] element [i]
  1389. 7) [cdim] = vector [floor1_class_dimensions] element [class]
  1390. 8) [cbits] = vector [floor1_class_subclasses] element [class]
  1391. 9) [csub] = (2 exponent [cbits])-1
  1392. 10) [cval] = 0
  1393. 11) if ( [cbits] is greater than zero ) {
  1394. 12) [cval] = read from packet using codebook number
  1395. (vector [floor1_class_masterbooks] element [class]) in scalar context
  1396. }
  1397. 13) iterate [j] over the range 0 ... [cdim]-1 {
  1398. 14) [book] = array [floor1_subclass_books] element [class],([cval] bitwise AND [csub])
  1399. 15) [cval] = [cval] right shifted [cbits] bits
  1400. 16) if ( [book] is not less than zero ) {
  1401. 17) vector [floor1_Y] element ([j]+[offset]) = read from packet using codebook
  1402. [book] in scalar context
  1403. } else [book] is less than zero {
  1404. 18) vector [floor1_Y] element ([j]+[offset]) = 0
  1405. }
  1406. }
  1407. 19) [offset] = [offset] + [cdim]
  1408. }
  1409. 20) done
  1410. </pre><p>
  1411. An end-of-packet condition during curve decode should be considered a
  1412. nominal occurrence; if end-of-packet is reached during any read
  1413. operation above, floor decode is to return 'unused' status as if the
  1414. <code class="varname">[nonzero]</code> flag had been unset at the beginning of decode.
  1415. </p><p>
  1416. Vector <code class="varname">[floor1_Y]</code> contains the values from packet decode
  1417. needed for floor 1 synthesis.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="vorbis-spec-floor1-synth"></a>7.2.2.2. curve computation</h5></div></div></div><p>
  1418. Curve computation is split into two logical steps; the first step
  1419. derives final Y amplitude values from the encoded, wrapped difference
  1420. values taken from the bitstream. The second step plots the curve
  1421. lines. Also, although zero-difference values are used in the
  1422. iterative prediction to find final Y values, these points are
  1423. conditionally skipped during final line computation in step two.
  1424. Skipping zero-difference values allows a smoother line fit. </p><p>
  1425. Although some aspects of the below algorithm look like inconsequential
  1426. optimizations, implementors are warned to follow the details closely.
  1427. Deviation from implementing a strictly equivalent algorithm can result
  1428. in serious decoding errors.</p><div class="section" lang="en"><div class="titlepage"><div><div><h6 class="title"><a name="id326536"></a>7.2.2.2.1. step 1: amplitude value synthesis</h6></div></div></div><p>
  1429. Unwrap the always-positive-or-zero values read from the packet into
  1430. +/- difference values, then apply to line prediction.</p><pre class="screen">
  1431. 1) [range] = vector { 256, 128, 86, 64 } element ([floor1_multiplier]-1)
  1432. 2) vector [floor1_step2_flag] element [0] = set
  1433. 3) vector [floor1_step2_flag] element [1] = set
  1434. 4) vector [floor1_final_Y] element [0] = vector [floor1_Y] element [0]
  1435. 5) vector [floor1_final_Y] element [1] = vector [floor1_Y] element [1]
  1436. 6) iterate [i] over the range 2 ... [floor1_values]-1 {
  1437. 7) [low_neighbor_offset] = <a href="#vorbis-spec-low_neighbor" title="9.2.4. low_neighbor">low_neighbor</a>([floor1_X_list],[i])
  1438. 8) [high_neighbor_offset] = <a href="#vorbis-spec-high_neighbor" title="9.2.4.1. high_neighbor">high_neighbor</a>([floor1_X_list],[i])
  1439. 9) [predicted] = <a href="#vorbis-spec-render_point" title="9.2.4.2. render_point">render_point</a>( vector [floor1_X_list] element [low_neighbor_offset],
  1440. vector [floor1_final_Y] element [low_neighbor_offset],
  1441. vector [floor1_X_list] element [high_neighbor_offset],
  1442. vector [floor1_final_Y] element [high_neighbor_offset],
  1443. vector [floor1_X_list] element [i] )
  1444. 10) [val] = vector [floor1_Y] element [i]
  1445. 11) [highroom] = [range] - [predicted]
  1446. 12) [lowroom] = [predicted]
  1447. 13) if ( [highroom] is less than [lowroom] ) {
  1448. 14) [room] = [highroom] * 2
  1449. } else [highroom] is not less than [lowroom] {
  1450. 15) [room] = [lowroom] * 2
  1451. }
  1452. 16) if ( [val] is nonzero ) {
  1453. 17) vector [floor1_step2_flag] element [low_neighbor_offset] = set
  1454. 18) vector [floor1_step2_flag] element [high_neighbor_offset] = set
  1455. 19) vector [floor1_step2_flag] element [i] = set
  1456. 20) if ( [val] is greater than or equal to [room] ) {
  1457. 21) if ( [highroom] is greater than [lowroom] ) {
  1458. 22) vector [floor1_final_Y] element [i] = [val] - [lowroom] + [predicted]
  1459. } else [highroom] is not greater than [lowroom] {
  1460. 23) vector [floor1_final_Y] element [i] = [predicted] - [val] + [highroom] - 1
  1461. }
  1462. } else [val] is less than [room] {
  1463. 24) if ([val] is odd) {
  1464. 25) vector [floor1_final_Y] element [i] =
  1465. [predicted] - (([val] + 1) divided by 2 using integer division)
  1466. } else [val] is even {
  1467. 26) vector [floor1_final_Y] element [i] =
  1468. [predicted] + ([val] / 2 using integer division)
  1469. }
  1470. }
  1471. } else [val] is zero {
  1472. 27) vector [floor1_step2_flag] element [i] = unset
  1473. 28) vector [floor1_final_Y] element [i] = [predicted]
  1474. }
  1475. }
  1476. 29) done
  1477. </pre></div><div class="section" lang="en"><div class="titlepage"><div><div><h6 class="title"><a name="id326571"></a>7.2.2.2.2. step 2: curve synthesis</h6></div></div></div><p>
  1478. Curve synthesis generates a return vector <code class="varname">[floor]</code> of length
  1479. <code class="varname">[n]</code> (where <code class="varname">[n]</code> is provided by the decode process
  1480. calling to floor decode). Floor 1 curve synthesis makes use of the
  1481. <code class="varname">[floor1_X_list]</code>, <code class="varname">[floor1_final_Y]</code> and
  1482. <code class="varname">[floor1_step2_flag]</code> vectors, as well as [floor1_multiplier]
  1483. and [floor1_values] values.</p><p>
  1484. Decode begins by sorting the scalars from vectors
  1485. <code class="varname">[floor1_X_list]</code>, <code class="varname">[floor1_final_Y]</code> and
  1486. <code class="varname">[floor1_step2_flag]</code> together into new vectors
  1487. <code class="varname">[floor1_X_list]'</code>, <code class="varname">[floor1_final_Y]'</code> and
  1488. <code class="varname">[floor1_step2_flag]'</code> according to ascending sort order of the
  1489. values in <code class="varname">[floor1_X_list]</code>. That is, sort the values of
  1490. <code class="varname">[floor1_X_list]</code> and then apply the same permutation to
  1491. elements of the other two vectors so that the X, Y and step2_flag
  1492. values still match.</p><p>
  1493. Then compute the final curve in one pass:</p><pre class="screen">
  1494. 1) [hx] = 0
  1495. 2) [lx] = 0
  1496. 3) [ly] = vector [floor1_final_Y]' element [0] * [floor1_multiplier]
  1497. 4) iterate [i] over the range 1 ... [floor1_values]-1 {
  1498. 5) if ( [floor1_step2_flag]' element [i] is set ) {
  1499. 6) [hy] = [floor1_final_Y]' element [i] * [floor1_multiplier]
  1500. 7) [hx] = [floor1_X_list]' element [i]
  1501. 8) <a href="#vorbis-spec-render_line" title="9.2.4.3. render_line">render_line</a>( [lx], [ly], [hx], [hy], [floor] )
  1502. 9) [lx] = [hx]
  1503. 10) [ly] = [hy]
  1504. }
  1505. }
  1506. 11) if ( [hx] is less than [n] ) {
  1507. 12) <a href="#vorbis-spec-render_line" title="9.2.4.3. render_line">render_line</a>( [hx], [hy], [n], [hy], [floor] )
  1508. }
  1509. 13) if ( [hx] is greater than [n] ) {
  1510. 14) truncate vector [floor] to [n] elements
  1511. }
  1512. 15) for each scalar in vector [floor], perform a lookup substitution using
  1513. the scalar value from [floor] as an offset into the vector <a href="#vorbis-spec-floor1_inverse_dB_table" title="10.1. floor1_inverse_dB_table">[floor1_inverse_dB_static_table]</a>
  1514. 16) done
  1515. </pre></div></div></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="vorbis-spec-residue"></a>8. Residue setup and decode</h2></div><div><p class="releaseinfo">
  1516. $Id: 08-residue.xml 13159 2007-06-21 05:22:35Z xiphmont $
  1517. </p></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id320982"></a>8.1. Overview</h3></div></div></div><p>
  1518. A residue vector represents the fine detail of the audio spectrum of
  1519. one channel in an audio frame after the encoder subtracts the floor
  1520. curve and performs any channel coupling. A residue vector may
  1521. represent spectral lines, spectral magnitude, spectral phase or
  1522. hybrids as mixed by channel coupling. The exact semantic content of
  1523. the vector does not matter to the residue abstraction.</p><p>
  1524. Whatever the exact qualities, the Vorbis residue abstraction codes the
  1525. residue vectors into the bitstream packet, and then reconstructs the
  1526. vectors during decode. Vorbis makes use of three different encoding
  1527. variants (numbered 0, 1 and 2) of the same basic vector encoding
  1528. abstraction.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id307154"></a>8.2. Residue format</h3></div></div></div><p>
  1529. Residue format partitions each vector in the vector bundle into chunks,
  1530. classifies each chunk, encodes the chunk classifications and finally
  1531. encodes the chunks themselves using the the specific VQ arrangement
  1532. defined for each selected classification.
  1533. The exact interleaving and partitioning vary by residue encoding number,
  1534. however the high-level process used to classify and encode the residue
  1535. vector is the same in all three variants.</p><p>
  1536. A set of coded residue vectors are all of the same length. High level
  1537. coding structure, ignoring for the moment exactly how a partition is
  1538. encoded and simply trusting that it is, is as follows:</p><p>
  1539. </p><div class="itemizedlist"><ul type="disc"><li><p>Each vector is partitioned into multiple equal sized chunks
  1540. according to configuration specified. If we have a vector size of
  1541. <span class="emphasis"><em>n</em></span>, a partition size <span class="emphasis"><em>residue_partition_size</em></span>, and a total
  1542. of <span class="emphasis"><em>ch</em></span> residue vectors, the total number of partitioned chunks
  1543. coded is <span class="emphasis"><em>n</em></span>/<span class="emphasis"><em>residue_partition_size</em></span>*<span class="emphasis"><em>ch</em></span>. It is
  1544. important to note that the integer division truncates. In the below
  1545. example, we assume an example <span class="emphasis"><em>residue_partition_size</em></span> of 8.</p></li><li><p>Each partition in each vector has a classification number that
  1546. specifies which of multiple configured VQ codebook setups are used to
  1547. decode that partition. The classification numbers of each partition
  1548. can be thought of as forming a vector in their own right, as in the
  1549. illustration below. Just as the residue vectors are coded in grouped
  1550. partitions to increase encoding efficiency, the classification vector
  1551. is also partitioned into chunks. The integer elements of each scalar
  1552. in a classification chunk are built into a single scalar that
  1553. represents the classification numbers in that chunk. In the below
  1554. example, the classification codeword encodes two classification
  1555. numbers.</p></li><li><p>The values in a residue vector may be encoded monolithically in a
  1556. single pass through the residue vector, but more often efficient
  1557. codebook design dictates that each vector is encoded as the additive
  1558. sum of several passes through the residue vector using more than one
  1559. VQ codebook. Thus, each residue value potentially accumulates values
  1560. from multiple decode passes. The classification value associated with
  1561. a partition is the same in each pass, thus the classification codeword
  1562. is coded only in the first pass.</p></li></ul></div><p>
  1563. </p><div class="mediaobject"><img src="residue-pack.png" alt="[illustration of residue vector format]"></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id326310"></a>8.3. residue 0</h3></div></div></div><p>
  1564. Residue 0 and 1 differ only in the way the values within a residue
  1565. partition are interleaved during partition encoding (visually treated
  1566. as a black box--or cyan box or brown box--in the above figure).</p><p>
  1567. Residue encoding 0 interleaves VQ encoding according to the
  1568. dimension of the codebook used to encode a partition in a specific
  1569. pass. The dimension of the codebook need not be the same in multiple
  1570. passes, however the partition size must be an even multiple of the
  1571. codebook dimension.</p><p>
  1572. As an example, assume a partition vector of size eight, to be encoded
  1573. by residue 0 using codebook sizes of 8, 4, 2 and 1:</p><pre class="programlisting">
  1574. original residue vector: [ 0 1 2 3 4 5 6 7 ]
  1575. codebook dimensions = 8 encoded as: [ 0 1 2 3 4 5 6 7 ]
  1576. codebook dimensions = 4 encoded as: [ 0 2 4 6 ], [ 1 3 5 7 ]
  1577. codebook dimensions = 2 encoded as: [ 0 4 ], [ 1 5 ], [ 2 6 ], [ 3 7 ]
  1578. codebook dimensions = 1 encoded as: [ 0 ], [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ], [ 6 ], [ 7 ]
  1579. </pre><p>
  1580. It is worth mentioning at this point that no configurable value in the
  1581. residue coding setup is restricted to a power of two.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id326344"></a>8.4. residue 1</h3></div></div></div><p>
  1582. Residue 1 does not interleave VQ encoding. It represents partition
  1583. vector scalars in order. As with residue 0, however, partition length
  1584. must be an integer multiple of the codebook dimension, although
  1585. dimension may vary from pass to pass.</p><p>
  1586. As an example, assume a partition vector of size eight, to be encoded
  1587. by residue 0 using codebook sizes of 8, 4, 2 and 1:</p><pre class="programlisting">
  1588. original residue vector: [ 0 1 2 3 4 5 6 7 ]
  1589. codebook dimensions = 8 encoded as: [ 0 1 2 3 4 5 6 7 ]
  1590. codebook dimensions = 4 encoded as: [ 0 1 2 3 ], [ 4 5 6 7 ]
  1591. codebook dimensions = 2 encoded as: [ 0 1 ], [ 2 3 ], [ 4 5 ], [ 6 7 ]
  1592. codebook dimensions = 1 encoded as: [ 0 ], [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ], [ 6 ], [ 7 ]
  1593. </pre></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id334893"></a>8.5. residue 2</h3></div></div></div><p>
  1594. Residue type two can be thought of as a variant of residue type 1.
  1595. Rather than encoding multiple passed-in vectors as in residue type 1,
  1596. the <span class="emphasis"><em>ch</em></span> passed in vectors of length <span class="emphasis"><em>n</em></span> are first
  1597. interleaved and flattened into a single vector of length
  1598. <span class="emphasis"><em>ch</em></span>*<span class="emphasis"><em>n</em></span>. Encoding then proceeds as in type 1. Decoding is
  1599. as in type 1 with decode interleave reversed. If operating on a single
  1600. vector to begin with, residue type 1 and type 2 are equivalent.</p><div class="mediaobject"><img src="residue2.png" alt="[illustration of residue type 2]"></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id334939"></a>8.6. Residue decode</h3></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id334945"></a>8.6.1. header decode</h4></div></div></div><p>
  1601. Header decode for all three residue types is identical.</p><pre class="programlisting">
  1602. 1) [residue_begin] = read 24 bits as unsigned integer
  1603. 2) [residue_end] = read 24 bits as unsigned integer
  1604. 3) [residue_partition_size] = read 24 bits as unsigned integer and add one
  1605. 4) [residue_classifications] = read 6 bits as unsigned integer and add one
  1606. 5) [residue_classbook] = read 8 bits as unsigned integer
  1607. </pre><p>
  1608. <code class="varname">[residue_begin]</code> and <code class="varname">[residue_end]</code> select the specific
  1609. sub-portion of each vector that is actually coded; it implements akin
  1610. to a bandpass where, for coding purposes, the vector effectively
  1611. begins at element <code class="varname">[residue_begin]</code> and ends at
  1612. <code class="varname">[residue_end]</code>. Preceding and following values in the unpacked
  1613. vectors are zeroed. Note that for residue type 2, these values as
  1614. well as <code class="varname">[residue_partition_size]</code>apply to the interleaved
  1615. vector, not the individual vectors before interleave.
  1616. <code class="varname">[residue_partition_size]</code> is as explained above,
  1617. <code class="varname">[residue_classifications]</code> is the number of possible
  1618. classification to which a partition can belong and
  1619. <code class="varname">[residue_classbook]</code> is the codebook number used to code
  1620. classification codewords. The number of dimensions in book
  1621. <code class="varname">[residue_classbook]</code> determines how many classification values
  1622. are grouped into a single classification codeword.</p><p>
  1623. Next we read a bitmap pattern that specifies which partition classes
  1624. code values in which passes.</p><pre class="programlisting">
  1625. 1) iterate [i] over the range 0 ... [residue_classifications]-1 {
  1626. 2) [high_bits] = 0
  1627. 3) [low_bits] = read 3 bits as unsigned integer
  1628. 4) [bitflag] = read one bit as boolean
  1629. 5) if ( [bitflag] is set ) then [high_bits] = read five bits as unsigned integer
  1630. 6) vector [residue_cascade] element [i] = [high_bits] * 8 + [low_bits]
  1631. }
  1632. 7) done
  1633. </pre><p>
  1634. Finally, we read in a list of book numbers, each corresponding to
  1635. specific bit set in the cascade bitmap. We loop over the possible
  1636. codebook classifications and the maximum possible number of encoding
  1637. stages (8 in Vorbis I, as constrained by the elements of the cascade
  1638. bitmap being eight bits):</p><pre class="programlisting">
  1639. 1) iterate [i] over the range 0 ... [residue_classifications]-1 {
  1640. 2) iterate [j] over the range 0 ... 7 {
  1641. 3) if ( vector [residue_cascade] element [i] bit [j] is set ) {
  1642. 4) array [residue_books] element [i][j] = read 8 bits as unsigned integer
  1643. } else {
  1644. 5) array [residue_books] element [i][j] = unused
  1645. }
  1646. }
  1647. }
  1648. 6) done
  1649. </pre><p>
  1650. An end-of-packet condition at any point in header decode renders the
  1651. stream undecodable. In addition, any codebook number greater than the
  1652. maximum numbered codebook set up in this stream also renders the
  1653. stream undecodable.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id325037"></a>8.6.2. packet decode</h4></div></div></div><p>
  1654. Format 0 and 1 packet decode is identical except for specific
  1655. partition interleave. Format 2 packet decode can be built out of the
  1656. format 1 decode process. Thus we describe first the decode
  1657. infrastructure identical to all three formats.</p><p>
  1658. In addition to configuration information, the residue decode process
  1659. is passed the number of vectors in the submap bundle and a vector of
  1660. flags indicating if any of the vectors are not to be decoded. If the
  1661. passed in number of vectors is 3 and vector number 1 is marked 'do not
  1662. decode', decode skips vector 1 during the decode loop. However, even
  1663. 'do not decode' vectors are allocated and zeroed.</p><p>
  1664. Depending on the values of <code class="varname">[residue_begin]</code> and
  1665. <code class="varname">[residue_end]</code>, it is obvious that the encoded
  1666. portion of a residue vector may be the entire possible residue vector
  1667. or some other strict subset of the actual residue vector size with
  1668. zero padding at either uncoded end. However, it is also possible to
  1669. set <code class="varname">[residue_begin]</code> and
  1670. <code class="varname">[residue_end]</code> to specify a range partially or
  1671. wholly beyond the maximum vector size. Before beginning residue
  1672. decode, limit <code class="varname">[residue_begin]</code> and
  1673. <code class="varname">[residue_end]</code> to the maximum possible vector size
  1674. as follows. We assume that the number of vectors being encoded,
  1675. <code class="varname">[ch]</code> is provided by the higher level decoding
  1676. process.</p><pre class="programlisting">
  1677. 1) [actual_size] = current blocksize/2;
  1678. 2) if residue encoding is format 2
  1679. 3) [actual_size] = [actual_size] * [ch];
  1680. 4) [limit_residue_begin] = maximum of ([residue_begin],[actual_size]);
  1681. 5) [limit_residue_end] = maximum of ([residue_end],[actual_size]);
  1682. </pre><p>
  1683. The following convenience values are conceptually useful to clarifying
  1684. the decode process:</p><pre class="programlisting">
  1685. 1) [classwords_per_codeword] = [codebook_dimensions] value of codebook [residue_classbook]
  1686. 2) [n_to_read] = [limit_residue_end] - [limit_residue_begin]
  1687. 3) [partitions_to_read] = [n_to_read] / [residue_partition_size]
  1688. </pre><p>
  1689. Packet decode proceeds as follows, matching the description offered earlier in the document. </p><pre class="programlisting">
  1690. 1) allocate and zero all vectors that will be returned.
  1691. 2) if ([n_to_read] is zero), stop; there is no residue to decode.
  1692. 3) iterate [pass] over the range 0 ... 7 {
  1693. 4) [partition_count] = 0
  1694. 5) while [partition_count] is less than [partitions_to_read]
  1695. 6) if ([pass] is zero) {
  1696. 7) iterate [j] over the range 0 .. [ch]-1 {
  1697. 8) if vector [j] is not marked 'do not decode' {
  1698. 9) [temp] = read from packet using codebook [residue_classbook] in scalar context
  1699. 10) iterate [i] descending over the range [classwords_per_codeword]-1 ... 0 {
  1700. 11) array [classifications] element [j],([i]+[partition_count]) =
  1701. [temp] integer modulo [residue_classifications]
  1702. 12) [temp] = [temp] / [residue_classifications] using integer division
  1703. }
  1704. }
  1705. }
  1706. }
  1707. 13) iterate [i] over the range 0 .. ([classwords_per_codeword] - 1) while [partition_count]
  1708. is also less than [partitions_to_read] {
  1709. 14) iterate [j] over the range 0 .. [ch]-1 {
  1710. 15) if vector [j] is not marked 'do not decode' {
  1711. 16) [vqclass] = array [classifications] element [j],[partition_count]
  1712. 17) [vqbook] = array [residue_books] element [vqclass],[pass]
  1713. 18) if ([vqbook] is not 'unused') {
  1714. 19) decode partition into output vector number [j], starting at scalar
  1715. offset [limit_residue_begin]+[partition_count]*[residue_partition_size] using
  1716. codebook number [vqbook] in VQ context
  1717. }
  1718. }
  1719. 20) increment [partition_count] by one
  1720. }
  1721. }
  1722. }
  1723. 21) done
  1724. </pre><p>
  1725. An end-of-packet condition during packet decode is to be considered a
  1726. nominal occurrence. Decode returns the result of vector decode up to
  1727. that point.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id341700"></a>8.6.3. format 0 specifics</h4></div></div></div><p>
  1728. Format zero decodes partitions exactly as described earlier in the
  1729. 'Residue Format: residue 0' section. The following pseudocode
  1730. presents the same algorithm. Assume:</p><p>
  1731. </p><div class="itemizedlist"><ul type="disc"><li> <code class="varname">[n]</code> is the value in <code class="varname">[residue_partition_size]</code></li><li><code class="varname">[v]</code> is the residue vector</li><li><code class="varname">[offset]</code> is the beginning read offset in [v]</li></ul></div><p>
  1732. </p><pre class="programlisting">
  1733. 1) [step] = [n] / [codebook_dimensions]
  1734. 2) iterate [i] over the range 0 ... [step]-1 {
  1735. 3) vector [entry_temp] = read vector from packet using current codebook in VQ context
  1736. 4) iterate [j] over the range 0 ... [codebook_dimensions]-1 {
  1737. 5) vector [v] element ([offset]+[i]+[j]*[step]) =
  1738. vector [v] element ([offset]+[i]+[j]*[step]) +
  1739. vector [entry_temp] element [j]
  1740. }
  1741. }
  1742. 6) done
  1743. </pre></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id341754"></a>8.6.4. format 1 specifics</h4></div></div></div><p>
  1744. Format 1 decodes partitions exactly as described earlier in the
  1745. 'Residue Format: residue 1' section. The following pseudocode
  1746. presents the same algorithm. Assume:</p><p>
  1747. </p><div class="itemizedlist"><ul type="disc"><li> <code class="varname">[n]</code> is the value in
  1748. <code class="varname">[residue_partition_size]</code></li><li><code class="varname">[v]</code> is the residue vector</li><li><code class="varname">[offset]</code> is the beginning read offset in [v]</li></ul></div><p>
  1749. </p><pre class="programlisting">
  1750. 1) [i] = 0
  1751. 2) vector [entry_temp] = read vector from packet using current codebook in VQ context
  1752. 3) iterate [j] over the range 0 ... [codebook_dimensions]-1 {
  1753. 4) vector [v] element ([offset]+[i]) =
  1754. vector [v] element ([offset]+[i]) +
  1755. vector [entry_temp] element [j]
  1756. 5) increment [i]
  1757. }
  1758. 6) if ( [i] is less than [n] ) continue at step 2
  1759. 7) done
  1760. </pre></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id341807"></a>8.6.5. format 2 specifics</h4></div></div></div><p>
  1761. Format 2 is reducible to format 1. It may be implemented as an additional step prior to and an additional post-decode step after a normal format 1 decode.
  1762. </p><p>
  1763. Format 2 handles 'do not decode' vectors differently than residue 0 or
  1764. 1; if all vectors are marked 'do not decode', no decode occurrs.
  1765. However, if at least one vector is to be decoded, all the vectors are
  1766. decoded. We then request normal format 1 to decode a single vector
  1767. representing all output channels, rather than a vector for each
  1768. channel. After decode, deinterleave the vector into independent vectors, one for each output channel. That is:</p><div class="orderedlist"><ol type="1"><li>If all vectors 0 through <span class="emphasis"><em>ch</em></span>-1 are marked 'do not decode', allocate and clear a single vector <code class="varname">[v]</code>of length <span class="emphasis"><em>ch*n</em></span> and skip step 2 below; proceed directly to the post-decode step.</li><li>Rather than performing format 1 decode to produce <span class="emphasis"><em>ch</em></span> vectors of length <span class="emphasis"><em>n</em></span> each, call format 1 decode to produce a single vector <code class="varname">[v]</code> of length <span class="emphasis"><em>ch*n</em></span>. </li><li><p>Post decode: Deinterleave the single vector <code class="varname">[v]</code> returned by format 1 decode as described above into <span class="emphasis"><em>ch</em></span> independent vectors, one for each outputchannel, according to:
  1769. </p><pre class="programlisting">
  1770. 1) iterate [i] over the range 0 ... [n]-1 {
  1771. 2) iterate [j] over the range 0 ... [ch]-1 {
  1772. 3) output vector number [j] element [i] = vector [v] element ([i] * [ch] + [j])
  1773. }
  1774. }
  1775. 4) done
  1776. </pre><p>
  1777. </p></li></ol></div></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="vorbis-spec-helper"></a>9. Helper equations</h2></div><div><p class="releaseinfo">
  1778. $Id: 09-helper.xml 7186 2004-07-20 07:19:25Z xiphmont $
  1779. </p></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id316603"></a>9.1. Overview</h3></div></div></div><p>
  1780. The equations below are used in multiple places by the Vorbis codec
  1781. specification. Rather than cluttering up the main specification
  1782. documents, they are defined here and referenced where appropriate.
  1783. </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id317505"></a>9.2. Functions</h3></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="vorbis-spec-ilog"></a>9.2.1. ilog</h4></div></div></div><p>
  1784. The "ilog(x)" function returns the position number (1 through n) of the highest set bit in the two's complement integer value
  1785. <code class="varname">[x]</code>. Values of <code class="varname">[x]</code> less than zero are defined to return zero.</p><pre class="programlisting">
  1786. 1) [return_value] = 0;
  1787. 2) if ( [x] is greater than zero ){
  1788. 3) increment [return_value];
  1789. 4) logical shift [x] one bit to the right, padding the MSb with zero
  1790. 5) repeat at step 2)
  1791. }
  1792. 6) done
  1793. </pre><p>
  1794. Examples:
  1795. </p><div class="itemizedlist"><ul type="disc"><li>ilog(0) = 0;</li><li>ilog(1) = 1;</li><li>ilog(2) = 2;</li><li>ilog(3) = 2;</li><li>ilog(4) = 3;</li><li>ilog(7) = 3;</li><li>ilog(negative number) = 0;</li></ul></div><p>
  1796. </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="vorbis-spec-float32_unpack"></a>9.2.2. float32_unpack</h4></div></div></div><p>
  1797. "float32_unpack(x)" is intended to translate the packed binary
  1798. representation of a Vorbis codebook float value into the
  1799. representation used by the decoder for floating point numbers. For
  1800. purposes of this example, we will unpack a Vorbis float32 into a
  1801. host-native floating point number.</p><pre class="programlisting">
  1802. 1) [mantissa] = [x] bitwise AND 0x1fffff (unsigned result)
  1803. 2) [sign] = [x] bitwise AND 0x80000000 (unsigned result)
  1804. 3) [exponent] = ( [x] bitwise AND 0x7fe00000) shifted right 21 bits (unsigned result)
  1805. 4) if ( [sign] is nonzero ) then negate [mantissa]
  1806. 5) return [mantissa] * ( 2 ^ ( [exponent] - 788 ) )
  1807. </pre></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="vorbis-spec-lookup1_values"></a>9.2.3. lookup1_values</h4></div></div></div><p>
  1808. "lookup1_values(codebook_entries,codebook_dimensions)" is used to
  1809. compute the correct length of the value index for a codebook VQ lookup
  1810. table of lookup type 1. The values on this list are permuted to
  1811. construct the VQ vector lookup table of size
  1812. <code class="varname">[codebook_entries]</code>.</p><p>
  1813. The return value for this function is defined to be 'the greatest
  1814. integer value for which <code class="varname">[return_value] to the power of
  1815. [codebook_dimensions] is less than or equal to
  1816. [codebook_entries]</code>'.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="vorbis-spec-low_neighbor"></a>9.2.4. low_neighbor</h4></div></div></div><p>
  1817. "low_neighbor(v,x)" finds the position <code class="varname">n</code> in vector <code class="varname">[v]</code> of
  1818. the greatest value scalar element for which <code class="varname">n</code> is less than
  1819. <code class="varname">[x]</code> and vector <code class="varname">[v]</code> element <code class="varname">n</code> is less
  1820. than vector <code class="varname">[v]</code> element <code class="varname">[x]</code>.</p><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="vorbis-spec-high_neighbor"></a>9.2.4.1. high_neighbor</h5></div></div></div><p>
  1821. "high_neighbor(v,x)" finds the position <code class="varname">n</code> in vector [v] of
  1822. the lowest value scalar element for which <code class="varname">n</code> is less than
  1823. <code class="varname">[x]</code> and vector <code class="varname">[v]</code> element <code class="varname">n</code> is greater
  1824. than vector <code class="varname">[v]</code> element <code class="varname">[x]</code>.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="vorbis-spec-render_point"></a>9.2.4.2. render_point</h5></div></div></div><p>
  1825. "render_point(x0,y0,x1,y1,X)" is used to find the Y value at point X
  1826. along the line specified by x0, x1, y0 and y1. This function uses an
  1827. integer algorithm to solve for the point directly without calculating
  1828. intervening values along the line.</p><pre class="programlisting">
  1829. 1) [dy] = [y1] - [y0]
  1830. 2) [adx] = [x1] - [x0]
  1831. 3) [ady] = absolute value of [dy]
  1832. 4) [err] = [ady] * ([X] - [x0])
  1833. 5) [off] = [err] / [adx] using integer division
  1834. 6) if ( [dy] is less than zero ) {
  1835. 7) [Y] = [y0] - [off]
  1836. } else {
  1837. 8) [Y] = [y0] + [off]
  1838. }
  1839. 9) done
  1840. </pre></div><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="vorbis-spec-render_line"></a>9.2.4.3. render_line</h5></div></div></div><p>
  1841. Floor decode type one uses the integer line drawing algorithm of
  1842. "render_line(x0, y0, x1, y1, v)" to construct an integer floor
  1843. curve for contiguous piecewise line segments. Note that it has not
  1844. been relevant elsewhere, but here we must define integer division as
  1845. rounding division of both positive and negative numbers toward zero.
  1846. </p><pre class="programlisting">
  1847. 1) [dy] = [y1] - [y0]
  1848. 2) [adx] = [x1] - [x0]
  1849. 3) [ady] = absolute value of [dy]
  1850. 4) [base] = [dy] / [adx] using integer division
  1851. 5) [x] = [x0]
  1852. 6) [y] = [y0]
  1853. 7) [err] = 0
  1854. 8) if ( [dy] is less than 0 ) {
  1855. 9) [sy] = [base] - 1
  1856. } else {
  1857. 10) [sy] = [base] + 1
  1858. }
  1859. 11) [ady] = [ady] - (absolute value of [base]) * [adx]
  1860. 12) vector [v] element [x] = [y]
  1861. 13) iterate [x] over the range [x0]+1 ... [x1]-1 {
  1862. 14) [err] = [err] + [ady];
  1863. 15) if ( [err] &gt;= [adx] ) {
  1864. 16) [err] = [err] - [adx]
  1865. 17) [y] = [y] + [sy]
  1866. } else {
  1867. 18) [y] = [y] + [base]
  1868. }
  1869. 19) vector [v] element [x] = [y]
  1870. }
  1871. </pre></div></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="vorbis-spec-tables"></a>10. Tables</h2></div><div><p class="releaseinfo">
  1872. $Id: 10-tables.xml 7186 2004-07-20 07:19:25Z xiphmont $
  1873. </p></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="vorbis-spec-floor1_inverse_dB_table"></a>10.1. floor1_inverse_dB_table</h3></div></div></div><p>
  1874. The vector <code class="varname">[floor1_inverse_dB_table]</code> is a 256 element static
  1875. lookup table consiting of the following values (read left to right
  1876. then top to bottom):</p><pre class="screen">
  1877. 1.0649863e-07, 1.1341951e-07, 1.2079015e-07, 1.2863978e-07,
  1878. 1.3699951e-07, 1.4590251e-07, 1.5538408e-07, 1.6548181e-07,
  1879. 1.7623575e-07, 1.8768855e-07, 1.9988561e-07, 2.1287530e-07,
  1880. 2.2670913e-07, 2.4144197e-07, 2.5713223e-07, 2.7384213e-07,
  1881. 2.9163793e-07, 3.1059021e-07, 3.3077411e-07, 3.5226968e-07,
  1882. 3.7516214e-07, 3.9954229e-07, 4.2550680e-07, 4.5315863e-07,
  1883. 4.8260743e-07, 5.1396998e-07, 5.4737065e-07, 5.8294187e-07,
  1884. 6.2082472e-07, 6.6116941e-07, 7.0413592e-07, 7.4989464e-07,
  1885. 7.9862701e-07, 8.5052630e-07, 9.0579828e-07, 9.6466216e-07,
  1886. 1.0273513e-06, 1.0941144e-06, 1.1652161e-06, 1.2409384e-06,
  1887. 1.3215816e-06, 1.4074654e-06, 1.4989305e-06, 1.5963394e-06,
  1888. 1.7000785e-06, 1.8105592e-06, 1.9282195e-06, 2.0535261e-06,
  1889. 2.1869758e-06, 2.3290978e-06, 2.4804557e-06, 2.6416497e-06,
  1890. 2.8133190e-06, 2.9961443e-06, 3.1908506e-06, 3.3982101e-06,
  1891. 3.6190449e-06, 3.8542308e-06, 4.1047004e-06, 4.3714470e-06,
  1892. 4.6555282e-06, 4.9580707e-06, 5.2802740e-06, 5.6234160e-06,
  1893. 5.9888572e-06, 6.3780469e-06, 6.7925283e-06, 7.2339451e-06,
  1894. 7.7040476e-06, 8.2047000e-06, 8.7378876e-06, 9.3057248e-06,
  1895. 9.9104632e-06, 1.0554501e-05, 1.1240392e-05, 1.1970856e-05,
  1896. 1.2748789e-05, 1.3577278e-05, 1.4459606e-05, 1.5399272e-05,
  1897. 1.6400004e-05, 1.7465768e-05, 1.8600792e-05, 1.9809576e-05,
  1898. 2.1096914e-05, 2.2467911e-05, 2.3928002e-05, 2.5482978e-05,
  1899. 2.7139006e-05, 2.8902651e-05, 3.0780908e-05, 3.2781225e-05,
  1900. 3.4911534e-05, 3.7180282e-05, 3.9596466e-05, 4.2169667e-05,
  1901. 4.4910090e-05, 4.7828601e-05, 5.0936773e-05, 5.4246931e-05,
  1902. 5.7772202e-05, 6.1526565e-05, 6.5524908e-05, 6.9783085e-05,
  1903. 7.4317983e-05, 7.9147585e-05, 8.4291040e-05, 8.9768747e-05,
  1904. 9.5602426e-05, 0.00010181521, 0.00010843174, 0.00011547824,
  1905. 0.00012298267, 0.00013097477, 0.00013948625, 0.00014855085,
  1906. 0.00015820453, 0.00016848555, 0.00017943469, 0.00019109536,
  1907. 0.00020351382, 0.00021673929, 0.00023082423, 0.00024582449,
  1908. 0.00026179955, 0.00027881276, 0.00029693158, 0.00031622787,
  1909. 0.00033677814, 0.00035866388, 0.00038197188, 0.00040679456,
  1910. 0.00043323036, 0.00046138411, 0.00049136745, 0.00052329927,
  1911. 0.00055730621, 0.00059352311, 0.00063209358, 0.00067317058,
  1912. 0.00071691700, 0.00076350630, 0.00081312324, 0.00086596457,
  1913. 0.00092223983, 0.00098217216, 0.0010459992, 0.0011139742,
  1914. 0.0011863665, 0.0012634633, 0.0013455702, 0.0014330129,
  1915. 0.0015261382, 0.0016253153, 0.0017309374, 0.0018434235,
  1916. 0.0019632195, 0.0020908006, 0.0022266726, 0.0023713743,
  1917. 0.0025254795, 0.0026895994, 0.0028643847, 0.0030505286,
  1918. 0.0032487691, 0.0034598925, 0.0036847358, 0.0039241906,
  1919. 0.0041792066, 0.0044507950, 0.0047400328, 0.0050480668,
  1920. 0.0053761186, 0.0057254891, 0.0060975636, 0.0064938176,
  1921. 0.0069158225, 0.0073652516, 0.0078438871, 0.0083536271,
  1922. 0.0088964928, 0.009474637, 0.010090352, 0.010746080,
  1923. 0.011444421, 0.012188144, 0.012980198, 0.013823725,
  1924. 0.014722068, 0.015678791, 0.016697687, 0.017782797,
  1925. 0.018938423, 0.020169149, 0.021479854, 0.022875735,
  1926. 0.024362330, 0.025945531, 0.027631618, 0.029427276,
  1927. 0.031339626, 0.033376252, 0.035545228, 0.037855157,
  1928. 0.040315199, 0.042935108, 0.045725273, 0.048696758,
  1929. 0.051861348, 0.055231591, 0.058820850, 0.062643361,
  1930. 0.066714279, 0.071049749, 0.075666962, 0.080584227,
  1931. 0.085821044, 0.091398179, 0.097337747, 0.10366330,
  1932. 0.11039993, 0.11757434, 0.12521498, 0.13335215,
  1933. 0.14201813, 0.15124727, 0.16107617, 0.17154380,
  1934. 0.18269168, 0.19456402, 0.20720788, 0.22067342,
  1935. 0.23501402, 0.25028656, 0.26655159, 0.28387361,
  1936. 0.30232132, 0.32196786, 0.34289114, 0.36517414,
  1937. 0.38890521, 0.41417847, 0.44109412, 0.46975890,
  1938. 0.50028648, 0.53279791, 0.56742212, 0.60429640,
  1939. 0.64356699, 0.68538959, 0.72993007, 0.77736504,
  1940. 0.82788260, 0.88168307, 0.9389798, 1.
  1941. </pre></div></div><div class="appendix" lang="en"><h2 class="title" style="clear: both"><a name="vorbis-over-ogg"></a>1. Embedding Vorbis into an Ogg stream</h2><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id319760"></a>1.1. Overview</h3></div></div></div><p>
  1942. This document describes using Ogg logical and physical transport
  1943. streams to encapsulate Vorbis compressed audio packet data into file
  1944. form.</p><p>
  1945. The <a href="#vorbis-spec-intro" title="1. Introduction and Description">Section 1, &#8220;Introduction and Description&#8221;</a> provides an overview of the construction
  1946. of Vorbis audio packets.</p><p>
  1947. The <a href="oggstream.html" target="_top">Ogg
  1948. bitstream overview</a> and <a href="framing.html" target="_top">Ogg logical
  1949. bitstream and framing spec</a> provide detailed descriptions of Ogg
  1950. transport streams. This specification document assumes a working
  1951. knowledge of the concepts covered in these named backround
  1952. documents. Please read them first.</p><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id336562"></a>1.1.1. Restrictions</h4></div></div></div><p>
  1953. The Ogg/Vorbis I specification currently dictates that Ogg/Vorbis
  1954. streams use Ogg transport streams in degenerate, unmultiplexed
  1955. form only. That is:
  1956. </p><div class="itemizedlist"><ul type="disc"><li>
  1957. A meta-headerless Ogg file encapsulates the Vorbis I packets
  1958. </li><li>
  1959. The Ogg stream may be chained, i.e. contain multiple, contigous logical streams (links).
  1960. </li><li>
  1961. The Ogg stream must be unmultiplexed (only one stream, a Vorbis audio stream, per link)
  1962. </li></ul></div><p>
  1963. </p><p>
  1964. This is not to say that it is not currently possible to multiplex
  1965. Vorbis with other media types into a multi-stream Ogg file. At the
  1966. time this document was written, Ogg was becoming a popular container
  1967. for low-bitrate movies consisting of DiVX video and Vorbis audio.
  1968. However, a 'Vorbis I audio file' is taken to imply Vorbis audio
  1969. existing alone within a degenerate Ogg stream. A compliant 'Vorbis
  1970. audio player' is not required to implement Ogg support beyond the
  1971. specific support of Vorbis within a degenrate ogg stream (naturally,
  1972. application authors are encouraged to support full multiplexed Ogg
  1973. handling).
  1974. </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id330723"></a>1.1.2. MIME type</h4></div></div></div><p>
  1975. The correct MIME type of any Ogg file is <code class="literal">application/ogg</code>.
  1976. However, if a file is a Vorbis I audio file (which implies a
  1977. degenerate Ogg stream including only unmultiplexed Vorbis audio), the
  1978. mime type <code class="literal">audio/x-vorbis</code> is also allowed.</p></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id328095"></a>1.2. Encapsulation</h3></div></div></div><p>
  1979. Ogg encapsulation of a Vorbis packet stream is straightforward.</p><div class="itemizedlist"><ul type="disc"><li>
  1980. The first Vorbis packet (the identification header), which
  1981. uniquely identifies a stream as Vorbis audio, is placed alone in the
  1982. first page of the logical Ogg stream. This results in a first Ogg
  1983. page of exactly 58 bytes at the very beginning of the logical stream.
  1984. </li><li>
  1985. This first page is marked 'beginning of stream' in the page flags.
  1986. </li><li>
  1987. The second and third vorbis packets (comment and setup
  1988. headers) may span one or more pages beginning on the second page of
  1989. the logical stream. However many pages they span, the third header
  1990. packet finishes the page on which it ends. The next (first audio) packet
  1991. must begin on a fresh page.
  1992. </li><li>
  1993. The granule position of these first pages containing only headers is zero.
  1994. </li><li>
  1995. The first audio packet of the logical stream begins a fresh Ogg page.
  1996. </li><li>
  1997. Packets are placed into ogg pages in order until the end of stream.
  1998. </li><li>
  1999. The last page is marked 'end of stream' in the page flags.
  2000. </li><li>
  2001. Vorbis packets may span page boundaries.
  2002. </li><li>
  2003. The granule position of pages containing Vorbis audio is in units
  2004. of PCM audio samples (per channel; a stereo stream's granule position
  2005. does not increment at twice the speed of a mono stream).
  2006. </li><li>
  2007. The granule position of a page represents the end PCM sample
  2008. position of the last packet <span class="emphasis"><em>completed</em></span> on that page.
  2009. A page that is entirely spanned by a single packet (that completes on a
  2010. subsequent page) has no granule position, and the granule position is
  2011. set to '-1'.
  2012. </li><li><p>
  2013. The granule (PCM) position of the first page need not indicate
  2014. that the stream started at position zero. Although the granule
  2015. position belongs to the last completed packet on the page and a
  2016. valid granule position must be positive, by
  2017. inference it may indicate that the PCM position of the beginning
  2018. of audio is positive or negative.
  2019. </p><div class="itemizedlist"><ul type="circle"><li>
  2020. A positive starting value simply indicates that this stream begins at
  2021. some positive time offset, potentially within a larger
  2022. program. This is a common case when connecting to the middle
  2023. of broadcast stream.
  2024. </li><li>
  2025. A negative value indicates that
  2026. output samples preceeding time zero should be discarded during
  2027. decoding; this technique is used to allow sample-granularity
  2028. editing of the stream start time of already-encoded Vorbis
  2029. streams. The number of samples to be discarded must not exceed
  2030. the overlap-add span of the first two audio packets.
  2031. </li></ul></div><p>
  2032. In both of these cases in which the initial audio PCM starting
  2033. offset is nonzero, the second finished audio packet must flush the
  2034. page on which it appears and the third packet begin a fresh page.
  2035. This allows the decoder to always be able to perform PCM position
  2036. adjustments before needing to return any PCM data from synthesis,
  2037. resulting in correct positioning information without any aditional
  2038. seeking logic.
  2039. </p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
  2040. Failure to do so should, at worst, cause a
  2041. decoder implementation to return incorrect positioning information
  2042. for seeking operations at the very beginning of the stream.
  2043. </p></div></li><li>
  2044. A granule position on the final page in a stream that indicates
  2045. less audio data than the final packet would normally return is used to
  2046. end the stream on other than even frame boundaries. The difference
  2047. between the actual available data returned and the declared amount
  2048. indicates how many trailing samples to discard from the decoding
  2049. process.
  2050. </li></ul></div></div></div><div class="appendix" lang="en"><h2 class="title" style="clear: both"><a name="vorbis-over-rtp"></a>2. Vorbis encapsulation in RTP</h2><pre class="literallayout">
  2051. <p>Please consult the internet draft <em class="citetitle">RTP Payload Format for Vorbis Encoded
  2052. Audio</em> for description of how to embed Vorbis audio in an RTP stream.</p>
  2053. </pre></div><div class="appendix" lang="en"><h2 class="title" style="clear: both"><a name="footer"></a>3. Colophon</h2><div class="mediaobject"><img src="white-xifish.png" alt="[Xiph.org logo]"></div><p>
  2054. Ogg is a <a href="http://www.xiph.org/" target="_top">Xiph.org Foundation</a> effort
  2055. to protect essential tenets of Internet multimedia from corporate
  2056. hostage-taking; Open Source is the net's greatest tool to keep
  2057. everyone honest. See <a href="http://www.xiph.org/about.html" target="_top">About
  2058. the Xiph.org Foundation</a> for details.
  2059. </p><p>
  2060. Ogg Vorbis is the first Ogg audio CODEC. Anyone may freely use and
  2061. distribute the Ogg and Vorbis specification, whether in a private,
  2062. public or corporate capacity. However, the Xiph.org Foundation and
  2063. the Ogg project (xiph.org) reserve the right to set the Ogg Vorbis
  2064. specification and certify specification compliance.</p><p>
  2065. Xiph.org's Vorbis software CODEC implementation is distributed under a
  2066. BSD-like license. This does not restrict third parties from
  2067. distributing independent implementations of Vorbis software under
  2068. other licenses.</p><p>
  2069. Ogg, Vorbis, Xiph.org Foundation and their logos are trademarks (tm)
  2070. of the <a href="http://www.xiph.org/" target="_top">Xiph.org Foundation</a>. These
  2071. pages are copyright (C) 1994-2007 Xiph.org Foundation. All rights
  2072. reserved.</p><p>
  2073. This document is set in DocBook XML.
  2074. </p></div></div></body></html>