ogg-multiplex.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 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, h4, h4 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>Page Multiplexing and Ordering in a Physical Ogg Stream</h1>
  58. <p>The low-level mechanisms of an Ogg stream (as described in the Ogg
  59. Bitstream Overview) provide means for mixing multiple logical streams
  60. and media types into a single linear-chronological stream. This
  61. document specifies the high-level arrangement and use of page
  62. structure to multiplex multiple streams of mixed media type within a
  63. physical Ogg stream.</p>
  64. <h2>Design Elements</h2>
  65. <p>The design and arrangement of the Ogg container format is governed by
  66. several high-level design decisions that form the reasoning behind
  67. specific low-level design decisions.</p>
  68. <h3>Linear media</h3>
  69. <p>The Ogg bitstream is intended to encapsulate chronological,
  70. time-linear mixed media into a single delivery stream or file. The
  71. design is such that an application can always encode and/or decode a
  72. full-featured bitstream in one pass with no seeking and minimal
  73. buffering. Seeking to provide optimized encoding (such as two-pass
  74. encoding) or interactive decoding (such as scrubbing or instant
  75. replay) is not disallowed or discouraged, however no bitstream feature
  76. must require nonlinear operation on the bitstream.</p>
  77. <h3>Multiplexing</h3>
  78. <p>Ogg bitstreams multiplex multiple logical streams into a single
  79. physical stream at the page level. Each page contains an abstract
  80. time stamp (the Granule Position) that represents an absolute time
  81. landmark within the stream. After the pages representing stream
  82. headers (all logical stream headers occur at the beginning of a
  83. physical bitstream section before any logical stream data), logical
  84. stream data pages are arranged in a physical bitstream in strict
  85. non-decreasing order by chronological absolute time as
  86. specified by the granule position.</p>
  87. <p>The only exception to arranging pages in strictly ascending time order
  88. by granule position is those pages that do not set the granule
  89. position value. This is a special case when exceptionally large
  90. packets span multiple pages; the specifics of handling this special
  91. case are described later under 'Continuous and Discontinuous
  92. Streams'.</p>
  93. <h3>Seeking</h3>
  94. <p>Ogg is designed to use an interpolated bisection search to
  95. implement exact positional seeking. Interpolated bisection search is
  96. a spec-mandated mechanism.</p>
  97. <p><i>An index may improve objective performance, but it seldom
  98. improves subjective performance outside of a few high-latency use
  99. cases and adds no additional functionality as bisection search
  100. delivers the same functionality for both one- and two-pass stream
  101. types. For these reasons, use of indexes is discouraged, except in
  102. cases where an index provides demonstrable and noticeable performance
  103. improvement.</i></p>
  104. <p>Seek operations are by absolute time; a direct bisection search must
  105. find the exact time position requested. Information in the Ogg
  106. bitstream is arranged such that all information to be presented for
  107. playback from the desired seek point will occur at or after the
  108. desired seek point. Seek operations are neither 'fuzzy' nor
  109. heuristic.</p>
  110. <p><i>Although key frame handling in video appears to be an exception to
  111. "all needed playback information lies ahead of a given seek",
  112. key frames can still be handled directly within this indexless
  113. framework. Seeking to a key frame in video (as well as seeking in other
  114. media types with analogous restraints) is handled as two seeks; first
  115. a seek to the desired time which extracts state information that
  116. decodes to the time of the last key frame, followed by a second seek
  117. directly to the key frame. The location of the previous key frame is
  118. embedded as state information in the granulepos; this mechanism is
  119. described in more detail later.</i></p>
  120. <h3>Continuous and Discontinuous Streams</h3>
  121. <p>Logical streams within a physical Ogg stream belong to one of two
  122. categories, "Continuous" streams and "Discontinuous" streams.
  123. Although these are discussed in more detail later, the distinction is
  124. important to a high-level understanding of how to buffer an Ogg
  125. stream.</p>
  126. <p>A stream that provides a gapless, time-continuous media type with a
  127. fine-grained timebase is considered to be 'Continuous'. A continuous
  128. stream should never be starved of data. Clear examples of continuous
  129. data types include broadcast audio and video.</p>
  130. <p>A stream that delivers data in a potentially irregular pattern or with
  131. widely spaced timing gaps is considered to be 'Discontinuous'. A
  132. discontinuous stream may be best thought of as data representing
  133. scattered events; although they happen in order, they are typically
  134. unconnected data often located far apart. One possible example of a
  135. discontinuous stream types would be captioning. Although it's
  136. possible to design captions as a continuous stream type, it's most
  137. natural to think of captions as widely spaced pieces of text with
  138. little happening between.</p>
  139. <p>The fundamental design distinction between continuous and
  140. discontinuous streams concerns buffering.</p>
  141. <h3>Buffering</h3>
  142. <p>Because a continuous stream is, by definition, gapless, Ogg buffering
  143. is based on the simple premise of never allowing any active continuous
  144. stream to starve for data during decode; buffering proceeds ahead
  145. until all continuous streams in a physical stream have data ready to
  146. decode on demand.</p>
  147. <p>Discontinuous stream data may occur on a fairly regular basis, but the
  148. timing of, for example, a specific caption is impossible to predict
  149. with certainty in most captioning systems. Thus the buffering system
  150. should take discontinuous data 'as it comes' rather than working ahead
  151. (for a potentially unbounded period) to look for future discontinuous
  152. data. As such, discontinuous streams are ignored when managing
  153. buffering; their pages simply 'fall out' of the stream when continuous
  154. streams are handled properly.</p>
  155. <p>Buffering requirements need not be explicitly declared or managed for
  156. the encoded stream; the decoder simply reads as much data as is
  157. necessary to keep all continuous stream types gapless (also ensuring
  158. discontinuous data arrives in time) and no more, resulting in optimum
  159. implicit buffer usage for a given stream. Because all pages of all
  160. data types are stamped with absolute timing information within the
  161. stream, inter-stream synchronization timing is always explicitly
  162. maintained without the need for explicitly declared buffer-ahead
  163. hinting.</p>
  164. <p>Further details, mechanisms and reasons for the differing arrangement
  165. and behavior of continuous and discontinuous streams is discussed
  166. later.</p>
  167. <h3>Whole-stream navigation</h3>
  168. <p>Ogg is designed so that the simplest navigation operations treat the
  169. physical Ogg stream as a whole summary of its streams, rather than
  170. navigating each interleaved stream as a separate entity.</p>
  171. <p>First Example: seeking to a desired time position in a multiplexed (or
  172. unmultiplexed) Ogg stream can be accomplished through a bisection
  173. search on time position of all pages in the stream (as encoded in the
  174. granule position). More powerful searches (such as a key frame-aware
  175. seek within video) are also possible with additional search
  176. complexity, but similar computational complexity.</p>
  177. <p>Second Example: A bitstream section may consist of three multiplexed
  178. streams of differing lengths. The result of multiplexing these
  179. streams should be thought of as a single mixed stream with a length
  180. equal to the longest of the three component streams. Although it is
  181. also possible to think of the multiplexed results as three concurrent
  182. streams of different lengths and it is possible to recover the three
  183. original streams, it will also become obvious that once multiplexed,
  184. it isn't possible to find the internal lengths of the component
  185. streams without a linear search of the whole bitstream section.
  186. However, it is possible to find the length of the whole bitstream
  187. section easily (in near-constant time per section) just as it is for a
  188. single-media unmultiplexed stream.</p>
  189. <h2>Granule Position</h2>
  190. <h3>Description</h3>
  191. <p>The Granule Position is a signed 64 bit field appearing in the header
  192. of every Ogg page. Although the granule position represents absolute
  193. time within a logical stream, its value does not necessarily directly
  194. encode a simple timestamp. It may represent frames elapsed (as in
  195. Vorbis), a simple timestamp, or a more complex bit-division encoding
  196. (such as in Theora). The exact encoding of the granule position is up
  197. to a specific codec.</p>
  198. <p>The granule position is governed by the following rules:</p>
  199. <ul>
  200. <li>Granule Position must always increase forward or remain equal from
  201. page to page, be unset, or be zero for a header page. The absolute
  202. time to which any correct sequence of granule position maps must
  203. similarly always increase forward or remain equal. <i>(A codec may
  204. make use of data, such as a control sequence, that only affects codec
  205. working state without producing data and thus advancing granule
  206. position and time. Although the packet sequence number increases in
  207. this case, the granule position, and thus the time position, do
  208. not.)</i></li>
  209. <li>Granule position may only be unset if there no packet defining a
  210. time boundary on the page (that is, if no packet in a continuous
  211. stream ends on the page, or no packet in a discontinuous stream begins
  212. on the page. This will be discussed in more detail under Continuous
  213. and Discontinuous streams).</li>
  214. <li>A codec must be able to translate a given granule position value
  215. to a unique, deterministic absolute time value through direct
  216. calculation. A codec is not required to be able to translate an
  217. absolute time value into a unique granule position value.</li>
  218. <li>Codecs shall choose a granule position definition that allows that
  219. codec means to seek as directly as possible to an immediately
  220. decodable point, such as the bit-divided granule position encoding of
  221. Theora allows the codec to seek efficiently to key frame without using
  222. an index. That is, additional information other than absolute time
  223. may be encoded into a granule position value so long as the granule
  224. position obeys the above points.</li>
  225. </ul>
  226. <h4>Example: timestamp</h4>
  227. <p>In general, a codec/stream type should choose the simplest granule
  228. position encoding that addresses its requirements. The examples here
  229. are by no means exhaustive of the possibilities within Ogg.</p>
  230. <p>A simple granule position could encode a timestamp directly. For
  231. example, a granule position that encoded milliseconds from beginning
  232. of stream would allow a logical stream length of over 100,000,000,000
  233. days before beginning a new logical stream (to avoid the granule
  234. position wrapping).</p>
  235. <h4>Example: framestamp</h4>
  236. <p>A simple millisecond timestamp granule encoding might suit many stream
  237. types, but a millisecond resolution is inappropriate to, eg, most
  238. audio encodings where exact single-sample resolution is generally a
  239. requirement. A millisecond is both too large a granule and often does
  240. not represent an integer number of samples.</p>
  241. <p>In the event that audio frames are always encoded as the same number of
  242. samples, the granule position could simply be a linear count of frames
  243. since beginning of stream. This has the advantages of being exact and
  244. efficient. Position in time would simply be <tt>[granule_position] *
  245. [samples_per_frame] / [samples_per_second]</tt>.</p>
  246. <h4>Example: samplestamp (Vorbis)</h4>
  247. <p>Frame counting is insufficient in codecs such as Vorbis where an audio
  248. frame [packet] encodes a variable number of samples. In Vorbis's
  249. case, the granule position is a count of the number of raw samples
  250. from the beginning of stream; the absolute time of
  251. a granule position is <tt>[granule_position] /
  252. [samples_per_second]</tt>.</p>
  253. <h4>Example: bit-divided framestamp (Theora)</h4>
  254. <p>Some video codecs may be able to use the simple framestamp scheme for
  255. granule position. However, most modern video codecs introduce at
  256. least the following complications:</p>
  257. <ul>
  258. <li>video frames are relatively far apart compared to audio samples;
  259. for this reason, the point at which a video frame changes to the next
  260. frame is usually a strictly defined offset within the frame 'period'.
  261. That is, video at 50fps could just as easily define frame transitions
  262. &lt;.015, .035, .055...&gt; as at &lt;.00, .02, .04...&gt;.</li>
  263. <li>frame rates often include drop-frames, leap-frames or other
  264. rational-but-non-integer timings.</li>
  265. <li>Decode must begin at a 'key frame' or 'I frame'. Keyframes usually
  266. occur relatively seldom.</li>
  267. </ul>
  268. <p>The first two points can be handled straightforwardly via the fact
  269. that the codec has complete control mapping granule position to
  270. absolute time; non-integer frame rates and offsets can be set in the
  271. codec's initial header, and the rest is just arithmetic.</p>
  272. <p>The third point appears trickier at first glance, but it too can be
  273. handled through the granule position mapping mechanism. Here we
  274. arrange the granule position in such a way that granule positions of
  275. key frames are easy to find. Divide the granule position into two
  276. fields; the most-significant bits are an absolute frame counter, but
  277. it's only updated at each key frame. The least significant bits encode
  278. the number of frames since the last key frame. In this way, each
  279. granule position both encodes the absolute time of the current frame
  280. as well as the absolute time of the last key frame.</p>
  281. <p>Seeking to a most recent preceding key frame is then accomplished by
  282. first seeking to the original desired point, inspecting the granulepos
  283. of the resulting video page, extracting from that granulepos the
  284. absolute time of the desired key frame, and then seeking directly to
  285. that key frame's page. Of course, it's still possible for an
  286. application to ignore key frames and use a simpler seeking algorithm
  287. (decode would be unable to present decoded video until the next
  288. key frame). Surprisingly many player applications do choose the
  289. simpler approach.</p>
  290. <h3>granule position, packets and pages</h3>
  291. <p>Although each packet of data in a logical stream theoretically has a
  292. specific granule position, only one granule position is encoded
  293. per page. It is possible to encode a logical stream such that each
  294. page contains only a single packet (so that granule positions are
  295. preserved for each packet), however a one-to-one packet/page mapping
  296. is not intended to be the general case.</p>
  297. <p>Because Ogg functions at the page, not packet, level, this
  298. once-per-page time information provides Ogg with the finest-grained
  299. time information is can use. Ogg passes this granule positioning data
  300. to the codec (along with the packets extracted from a page); it is the
  301. responsibility of codecs to track timing information at granularities
  302. finer than a single page.</p>
  303. <h3>start-time and end-time positioning</h3>
  304. <p>A granule position represents the <em>instantaneous time location
  305. between two pages</em>. However, continuous streams and discontinuous
  306. streams differ on whether the granulepos represents the end-time of
  307. the data on a page or the start-time. Continuous streams are
  308. 'end-time' encoded; the granulepos represents the point in time
  309. immediately after the last data decoded from a page. Discontinuous
  310. streams are 'start-time' encoded; the granulepos represents the point
  311. in time of the first data decoded from the page.</p>
  312. <p>An Ogg stream type is declared continuous or discontinuous by its
  313. codec. A given codec may support both continuous and discontinuous
  314. operation so long as any given logical stream is continuous or
  315. discontinuous for its entirety and the codec is able to ascertain (and
  316. inform the Ogg layer) as to which after decoding the initial stream
  317. header. The majority of codecs will always be continuous (such as
  318. Vorbis) or discontinuous (such as Writ).</p>
  319. <p>Start- and end-time encoding do not affect multiplexing sort-order;
  320. pages are still sorted by the absolute time a given granulepos maps to
  321. regardless of whether that granulepos represents start- or
  322. end-time.</p>
  323. <h2>Multiplex/Demultiplex Division of Labor</h2>
  324. <p>The Ogg multiplex/demultiplex layer provides mechanisms for encoding
  325. raw packets into Ogg pages, decoding Ogg pages back into the original
  326. codec packets, determining the logical structure of an Ogg stream, and
  327. navigating through and synchronizing with an Ogg stream at a desired
  328. stream location. Strict multiplex/demultiplex operations are entirely
  329. in the Ogg domain and require no intervention from codecs.</p>
  330. <p>Implementation of more complex operations does require codec
  331. knowledge, however. Unlike other framing systems, Ogg maintains
  332. strict separation between framing and the framed bitstream data; Ogg
  333. does not replicate codec-specific information in the page/framing
  334. data, nor does Ogg blur the line between framing and stream
  335. data/metadata. Because Ogg is fully data-agnostic toward the data it
  336. frames, operations which require specifics of bitstream data (such as
  337. 'seek to key frame') also require interaction with the codec layer
  338. (because, in this example, the Ogg layer is not aware of the concept
  339. of key frames). This is different from systems that blur the
  340. separation between framing and stream data in order to simplify the
  341. separation of code. The Ogg system purposely keeps the distinction in
  342. data simple so that later codec innovations are not constrained by
  343. framing design.</p>
  344. <p>For this reason, however, complex seeking operations require
  345. interaction with the codecs in order to decode the granule position of
  346. a given stream type back to absolute time or in order to find
  347. 'decodable points' such as key frames in video.</p>
  348. <h2>Unsorted Discussion Points</h2>
  349. <p>flushes around key frames? RFC suggestion: repaginating or building a
  350. stream this way is nice but not required</p>
  351. <h2>Appendix A: multiplexing examples</h2>
  352. <div id="copyright">
  353. The Xiph Fish Logo is a
  354. trademark (&trade;) of Xiph.Org.<br/>
  355. These pages &copy; 1994 - 2005 Xiph.Org. All rights reserved.
  356. </div>
  357. </body>
  358. </html>