framing.html 14 KB

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