framing.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <HTML><HEAD><TITLE>xiph.org: Ogg Vorbis documentation</TITLE>
  2. <BODY bgcolor="#ffffff" text="#202020" link="#006666" vlink="#000000">
  3. <nobr><a href="vorbis.html"><img src="white-ogg.png" border=0><img
  4. src="vorbisword2.png" border=0></a></nobr><p>
  5. <h1><font color=#000070>
  6. Ogg logical bitstream framing
  7. </font></h1>
  8. <em>Last update to this document: July 15, 1999</em><br>
  9. <h2>Ogg bitstreams</h2>
  10. Vorbis encodes short-time blocks of PCM data into raw packets of
  11. bit-packed data. These raw packets may be used directly by transport
  12. mechanisms that provide their own framing and packet-seperation
  13. mechanisms (such as UDP datagrams). For stream based storage (such as
  14. files) and transport (such as TCP streams or pipes), Vorbis uses the
  15. Ogg bitstream format to provide framing/sync, sync recapture
  16. after error, landmarks during seeking, and enough information to
  17. properly seperate data back into packets at the original packet
  18. boundaries without relying on decoding to find packet boundaries.<p>
  19. <h2>Design constraints for Ogg bitstreams</h2>
  20. <ol><li>True streaming; we must not need to seek to build a 100%
  21. complete bitstream.
  22. <li> Use no more than approximately 1-2% of bitstream bandwidth for
  23. packet boundary marking, high-level framing, sync and seeking.
  24. <li> Specification of absolute position within the original sample
  25. stream.
  26. <li> Simple mechanism to ease limited editing, such as a simplified
  27. concatenation mechanism.
  28. <li> Detection of corruption, recapture after error and direct, random
  29. access to data at arbitrary positions in the bitstream.
  30. </ol>
  31. <h2>Logical and Physical Bitstreams</h2>
  32. A <em>logical</em> Ogg bitstream is a contiguous stream of
  33. sequential pages belonging only to the logical bitstream. A
  34. <em>physical</em> Ogg bitstream is constructed from one or more
  35. than one logical Ogg bitstream (the simplest physical bitstream
  36. is simply a single logical bitstream). We describe below the exact
  37. formatting of an Ogg logical bitstream. Combining logical
  38. bitstreams into more complex physical bitstreams is described in the
  39. <a href="oggstream.html">Ogg bitstream overview</a>. The exact
  40. mapping of raw Vorbis packets into a valid Ogg Vorbis physical
  41. bitstream is described in <a href="vorbis-stream.html">Vorbis
  42. bitstream mapping</a>.
  43. <h2>Bitstream structure</h2>
  44. An Ogg stream is structured by dividing incoming packets into
  45. segments of up to 255 bytes and then wrapping a group of contiguous
  46. packet segments into a variable length page preceeded by a page
  47. header. Both the header size and page size are variable; the page
  48. header contains sizing information and checksum data to determine
  49. header/page size and data integrity.<p>
  50. The bitstream is captured (or recaptured) by looking for the beginning
  51. of a page, specifically the capture pattern. Once the capture pattern
  52. is found, the decoder verifies page sync and integrity by computing
  53. and comparing the checksum. At that point, the decoder can extract the
  54. packets themselves.<p>
  55. <h3>Packet segmentation</h3>
  56. Packets are logically divided into multiple segments before encoding
  57. into a page. Note that the segmentation and fragmentation process is a
  58. logical one; it's used to compute page header values and the original
  59. page data need not be disturbed, even when a packet spans page
  60. boundaries.<p>
  61. The raw packet is logically divided into [n] 255 byte segments and a
  62. last fractional segment of < 255 bytes. A packet size may well
  63. consist only of the trailing fractional segment, and a fractional
  64. segment may be zero length. These values, called "lacing values" are
  65. then saved and placed into the header segment table.<p>
  66. An example should make the basic concept clear:<p>
  67. <pre>
  68. <tt>
  69. raw packet:
  70. ___________________________________________
  71. |______________packet data__________________| 753 bytes
  72. lacing values for page header segment table: 255,255,243
  73. </tt>
  74. </pre>
  75. We simply add the lacing values for the total size; the last lacing
  76. value for a packet is always the value that is less than 255. Note
  77. that this encoding both avoids imposing a maximum packet size as well
  78. as imposing minimum overhead on small packets (as opposed to, eg,
  79. simply using two bytes at the head of every packet and having a max
  80. packet size of 32k. Small packets (<255, the typical case) are
  81. penalized with twice the segmentation overhead). Using the lacing
  82. values as suggested, small packets see the minimum possible
  83. byte-aligned overheade (1 byte) and large packets, over 512 bytes or
  84. so, see a fairly constant ~.5% overhead on encoding space.<p>
  85. Note that a lacing value of 255 implies that a second lacing value
  86. follows in the packet, and a value of < 255 marks the end of the
  87. packet after that many additional bytes. A packet of 255 bytes (or a
  88. multiple of 255 bytes) is terminated by a lacing value of 0:<p>
  89. <pre><tt>
  90. raw packet:
  91. _______________________________
  92. |________packet data____________| 255 bytes
  93. lacing values: 255, 0
  94. </tt></pre>
  95. Note also that a 'nil' (zero length) packet is not an error; it
  96. consists of nothing more than a lacing value of zero in the header.<p>
  97. <h3>Packets spanning pages</h3>
  98. Packets are not resticted to beginning and ending within a page,
  99. although individual segments are, by definition, required to do so.
  100. Packets are not restricted to a maximum size, although excessively
  101. large packets in the data stream are discouraged; the Ogg
  102. bitstream specification strongly recommends nominal page size of
  103. approximately 4-8kB (large packets are forseen as being useful for
  104. initialization data at the beginning of a logical bitstream).<p>
  105. After segmenting a packet, the encoder may decide not to place all the
  106. resulting segments into the current page; to do so, the encoder places
  107. the lacing values of the segments it wishes to belong to the current
  108. page into the current segment table, then finishes the page. The next
  109. page is begun with the first value in the segment table belonging to
  110. the next packet segment, thus continuing the packet (data in the
  111. packet body must also correspond properly to the lacing values in the
  112. spanned pages. The segment data in the first packet corresponding to
  113. the lacing values of the first page belong in that page; packet
  114. segments listed in the segment table of the following page must begin
  115. the page body of the subsequent page).<p>
  116. The last mechanic to spanning a page boundary is to set the header
  117. flag in the new page to indicate that the first lacing value in the
  118. segment table continues rather than begins a packet; a header flag of
  119. 0x01 is set to indicate a continued packet. Although mandatory, it
  120. is not actually algorithmically necessary; one could inspect the
  121. preceeding segment table to determine if the packet is new or
  122. continued. Adding the information to the packet_header flag allows a
  123. simpler design (with no overhead) that needs only inspect the current
  124. page header after frame capture. This also allows faster error
  125. recovery in the event that the packet originates in a corrupt
  126. preceeding page, implying that the previous page's segment table
  127. cannot be trusted.<p>
  128. Note that a packet can span an arbitrary number of pages; the above
  129. spanning process is repeated for each spanned page boundary. Also a
  130. 'zero termination' on a packet size that is an even multiple of 255
  131. must appear even if the lacing value appears in the next page as a
  132. zero-length continuation of the current packet. The header flag
  133. should be set to 0x01 to indicate that the packet spanned, even though
  134. the span is a nil case as far as data is concerned.<p>
  135. The encoding looks odd, but is properly optimized for speed and the
  136. expected case of the majority of packets being between 50 and 200
  137. bytes (note that it is designed such that packets of wildly different
  138. sizes can be handled within the model; placing packet size
  139. restrictions on the encoder would have only slightly simplified design
  140. in page generation and increased overall encoder complexity).<p>
  141. The main point behind tracking individual packets (and packet
  142. segments) is to allow more flexible encoding tricks that requiring
  143. explicit knowledge of packet size. An example is simple bandwidth
  144. limiting, implemented by simply truncating packets in the nominal case
  145. if the packet is arranged so that the least sensitive portion of the
  146. data comes last.<p>
  147. <h3>Page header</h3>
  148. The headering mechanism is designed to avoid copying and re-assembly
  149. of the packet data (ie, making the packet segmentation process a
  150. logical one); the header can be generated directly from incoming
  151. packet data. The encoder buffers packet data until it finishes a
  152. complete page at which point it writes the header followed by the
  153. buffered packet segments.<p>
  154. <h4>capture_pattern</h4>
  155. A header begins with a capture pattern that simplifies identifying
  156. pages; once the decoder has found the capture pattern it can do a more
  157. intensive job of verifying that it has in fact found a page boundary
  158. (as opposed to an inadvertant coincidence in the byte stream).<p>
  159. <pre><tt>
  160. byte value
  161. 0 0x4f 'O'
  162. 1 0x67 'g'
  163. 2 0x67 'g'
  164. 3 0x53 'S'
  165. </tt></pre>
  166. <h4>stream_structure_version</h4>
  167. The capture pattern is followed by the stream structure revision:
  168. <pre><tt>
  169. byte value
  170. 4 0x00
  171. </tt></pre>
  172. <h4>header_type_flag</h4>
  173. The header type flag identifies this page's context in the bitstream:
  174. <pre><tt>
  175. byte value
  176. 5 bitflags: 0x01: unset = fresh packet
  177. set = continued packet
  178. 0x02: unset = not first page of logical bitstream
  179. set = first page of logical bitstream (bos)
  180. 0x04: unset = not last page of logical bitstream
  181. set = last page of logical bitstream (eos)
  182. </tt></pre>
  183. <h4>PCM absolute position</h4>
  184. (This is packed in the same way the rest of Ogg data is packed;
  185. LSb of LSB first. Note that the 'position' data specifies a 'sample'
  186. number (eg, in a CD quality sample is four octets, 16 bits for left
  187. and 16 bits for right; in video it would be the frame number). The
  188. position specified is the total samples encoded after including all
  189. packets finished on this page (packets begun on this page but
  190. continuing on to thenext page do not count). The rationale here is
  191. that the position specified in the frame header of the last page
  192. tells how long the PCM data coded by the bitstream is. A truncated
  193. stream will still return the proper number of samples that can be
  194. decoded fully.
  195. <pre><tt>
  196. byte value
  197. 6 0xXX LSB
  198. 7 0xXX
  199. 8 0xXX
  200. 9 0xXX
  201. 10 0xXX
  202. 11 0xXX
  203. 12 0xXX
  204. 13 0xXX MSB
  205. </tt></pre>
  206. <h4>stream serial number</h4>
  207. Ogg allows for seperate logical bitstreams to be mixed at page
  208. granularity in a physical bitstream. The most common case would be
  209. sequential arrangement, but it is possible to interleave pages for
  210. two seperate bitstreams to be decoded concurrently. The serial
  211. number is the means by which pages physical pages are associated with
  212. a particular logical stream. Each logical stream must have a unique
  213. serial number within a physical stream:
  214. <pre><tt>
  215. byte value
  216. 14 0xXX LSB
  217. 15 0xXX
  218. 16 0xXX
  219. 17 0xXX MSB
  220. </tt></pre>
  221. <h4>page sequence no</h4>
  222. Page counter; lets us know if a page is lost (useful where packets
  223. span page boundaries).
  224. <pre><tt>
  225. byte value
  226. 18 0xXX LSB
  227. 19 0xXX
  228. 20 0xXX
  229. 21 0xXX MSB
  230. </tt></pre>
  231. <h4>page checksum</h4>
  232. 32 bit CRC value (direct algorithm, initial val and final XOR = 0,
  233. generator polynomial=0x04c11db7). The value is computed over the
  234. entire header (with the CRC field in the header set to zero) and then
  235. continued over the page. The CRC field is then filled with the
  236. computed value.<p>
  237. (A thorough discussion of CRC algorithms can be found in <a
  238. href="ftp://ftp.rocksoft.com/clients/rocksoft/papers/crc_v3.txt">"A
  239. Painless Guide to CRC Error Detection Algorithms"</a> by Ross
  240. Williams <a
  241. href="mailto:ross@guest.adelaide.edu.au">ross@guest.adelaide.edu.au</a>.)
  242. <pre><tt>
  243. byte value
  244. 22 0xXX LSB
  245. 23 0xXX
  246. 24 0xXX
  247. 25 0xXX MSB
  248. </tt></pre>
  249. <h4>page_segments</h4>
  250. The number of segment entries to appear in the segment table. The
  251. maximum number of 255 segments (255 bytes each) sets the maximum
  252. possible physical page size at 65307 bytes or just under 64kB (thus
  253. we know that a header corrupted so as destroy sizing/alignment
  254. information will not cause a runaway bitstream. We'll read in the
  255. page according to the corrupted size information that's guaranteed to
  256. be a reasonable size regardless, notice the checksum mismatch, drop
  257. sync and then look for recapture).<p>
  258. <pre><tt>
  259. byte value
  260. 26 0x00-0xff (0-255)
  261. </tt></pre>
  262. <h4>segment_table (containing packet lacing values)</h4>
  263. The lacing values for each packet segment physically appearing in
  264. this page are listed in contiguous order.
  265. <pre><tt>
  266. byte value
  267. 27 0x00-0xff (0-255)
  268. [...]
  269. n 0x00-0xff (0-255, n=page_segments+26)
  270. </tt></pre>
  271. Total page size is calculated directly from the known header size and
  272. lacing values in the segment table. Packet data segments follow
  273. immediately after the header.<p>
  274. Page headers typically impose a flat .25-.5% space overhead assuming
  275. nominal ~8k page sizes. The segmentation table needed for exact
  276. packet recovery in the streaming layer adds approximately .5-1%
  277. nominal assuming expected encoder behavior in the 44.1kHz, 128kbps
  278. stereo encodings.<p>
  279. <hr>
  280. <a href="http://www.xiph.org/">
  281. <img src="white-xifish.png" align=left border=0>
  282. </a>
  283. <font size=-2 color=#505050>
  284. Ogg is a <a href="http://www.xiph.org">Xiphophorus</a> effort to
  285. protect essential tenets of Internet multimedia from corporate
  286. hostage-taking; Open Source is the net's greatest tool to keep
  287. everyone honest. See <a href="http://www.xiph.org/about.html">About
  288. Xiphophorus</a> for details.
  289. <p>
  290. Ogg Vorbis is the first Ogg audio CODEC. Anyone may
  291. freely use and distribute the Ogg and Vorbis specification,
  292. whether in a private, public or corporate capacity. However,
  293. Xiphophorus and the Ogg project (xiph.org) reserve the right to set
  294. the Ogg/Vorbis specification and certify specification compliance.<p>
  295. Xiphophorus's Vorbis software CODEC implementation is distributed
  296. under the Lessr/Library GNU Public License. This does not restrict
  297. third parties from distributing independent implementations of Vorbis
  298. software under other licenses.<p>
  299. OggSquish, Vorbis, Xiphophorus and their logos are trademarks (tm) of
  300. <a href="http://www.xiph.org/">Xiphophorus</a>. These pages are
  301. copyright (C) 1994-2000 Xiphophorus. All rights reserved.<p>
  302. </body>