draft-ietf-codec-oggopus.xml 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE rfc SYSTEM 'rfc2629.dtd' [
  3. <!ENTITY rfc2119 PUBLIC '' 'http://xml.resource.org/public/rfc/bibxml/reference.RFC.2119.xml'>
  4. <!ENTITY rfc3533 PUBLIC '' 'http://xml.resource.org/public/rfc/bibxml/reference.RFC.3533.xml'>
  5. <!ENTITY rfc3629 PUBLIC '' 'http://xml.resource.org/public/rfc/bibxml/reference.RFC.3629.xml'>
  6. <!ENTITY rfc4732 PUBLIC '' 'http://xml.resource.org/public/rfc/bibxml/reference.RFC.4732.xml'>
  7. <!ENTITY rfc5226 PUBLIC '' 'http://xml.resource.org/public/rfc/bibxml/reference.RFC.5226.xml'>
  8. <!ENTITY rfc5334 PUBLIC '' 'http://xml.resource.org/public/rfc/bibxml/reference.RFC.5334.xml'>
  9. <!ENTITY rfc6381 PUBLIC '' 'http://xml.resource.org/public/rfc/bibxml/reference.RFC.6381.xml'>
  10. <!ENTITY rfc6716 PUBLIC '' 'http://xml.resource.org/public/rfc/bibxml/reference.RFC.6716.xml'>
  11. <!ENTITY rfc6982 PUBLIC '' 'http://xml.resource.org/public/rfc/bibxml/reference.RFC.6982.xml'>
  12. <!ENTITY rfc7587 PUBLIC '' 'http://xml.resource.org/public/rfc/bibxml/reference.RFC.7587.xml'>
  13. ]>
  14. <?rfc toc="yes" symrefs="yes" ?>
  15. <rfc ipr="trust200902" category="std" docName="draft-ietf-codec-oggopus-09"
  16. updates="5334">
  17. <front>
  18. <title abbrev="Ogg Opus">Ogg Encapsulation for the Opus Audio Codec</title>
  19. <author initials="T.B." surname="Terriberry" fullname="Timothy B. Terriberry">
  20. <organization>Mozilla Corporation</organization>
  21. <address>
  22. <postal>
  23. <street>650 Castro Street</street>
  24. <city>Mountain View</city>
  25. <region>CA</region>
  26. <code>94041</code>
  27. <country>USA</country>
  28. </postal>
  29. <phone>+1 650 903-0800</phone>
  30. <email>tterribe@xiph.org</email>
  31. </address>
  32. </author>
  33. <author initials="R." surname="Lee" fullname="Ron Lee">
  34. <organization>Voicetronix</organization>
  35. <address>
  36. <postal>
  37. <street>246 Pulteney Street, Level 1</street>
  38. <city>Adelaide</city>
  39. <region>SA</region>
  40. <code>5000</code>
  41. <country>Australia</country>
  42. </postal>
  43. <phone>+61 8 8232 9112</phone>
  44. <email>ron@debian.org</email>
  45. </address>
  46. </author>
  47. <author initials="R." surname="Giles" fullname="Ralph Giles">
  48. <organization>Mozilla Corporation</organization>
  49. <address>
  50. <postal>
  51. <street>163 West Hastings Street</street>
  52. <city>Vancouver</city>
  53. <region>BC</region>
  54. <code>V6B 1H5</code>
  55. <country>Canada</country>
  56. </postal>
  57. <phone>+1 778 785 1540</phone>
  58. <email>giles@xiph.org</email>
  59. </address>
  60. </author>
  61. <date day="23" month="November" year="2015"/>
  62. <area>RAI</area>
  63. <workgroup>codec</workgroup>
  64. <abstract>
  65. <t>
  66. This document defines the Ogg encapsulation for the Opus interactive speech and
  67. audio codec.
  68. This allows data encoded in the Opus format to be stored in an Ogg logical
  69. bitstream.
  70. </t>
  71. </abstract>
  72. </front>
  73. <middle>
  74. <section anchor="intro" title="Introduction">
  75. <t>
  76. The IETF Opus codec is a low-latency audio codec optimized for both voice and
  77. general-purpose audio.
  78. See <xref target="RFC6716"/> for technical details.
  79. This document defines the encapsulation of Opus in a continuous, logical Ogg
  80. bitstream&nbsp;<xref target="RFC3533"/>.
  81. Ogg encapsulation provides Opus with a long-term storage format supporting
  82. all of the essential features, including metadata, fast and accurate seeking,
  83. corruption detection, recapture after errors, low overhead, and the ability to
  84. multiplex Opus with other codecs (including video) with minimal buffering.
  85. It also provides a live streamable format, capable of delivery over a reliable
  86. stream-oriented transport, without requiring all the data, or even the total
  87. length of the data, up-front, in a form that is identical to the on-disk
  88. storage format.
  89. </t>
  90. <t>
  91. Ogg bitstreams are made up of a series of 'pages', each of which contains data
  92. from one or more 'packets'.
  93. Pages are the fundamental unit of multiplexing in an Ogg stream.
  94. Each page is associated with a particular logical stream and contains a capture
  95. pattern and checksum, flags to mark the beginning and end of the logical
  96. stream, and a 'granule position' that represents an absolute position in the
  97. stream, to aid seeking.
  98. A single page can contain up to 65,025 octets of packet data from up to 255
  99. different packets.
  100. Packets can be split arbitrarily across pages, and continued from one page to
  101. the next (allowing packets much larger than would fit on a single page).
  102. Each page contains 'lacing values' that indicate how the data is partitioned
  103. into packets, allowing a demultiplexer (demuxer) to recover the packet
  104. boundaries without examining the encoded data.
  105. A packet is said to 'complete' on a page when the page contains the final
  106. lacing value corresponding to that packet.
  107. </t>
  108. <t>
  109. This encapsulation defines the contents of the packet data, including
  110. the necessary headers, the organization of those packets into a logical
  111. stream, and the interpretation of the codec-specific granule position field.
  112. It does not attempt to describe or specify the existing Ogg container format.
  113. Readers unfamiliar with the basic concepts mentioned above are encouraged to
  114. review the details in <xref target="RFC3533"/>.
  115. </t>
  116. </section>
  117. <section anchor="terminology" title="Terminology">
  118. <t>
  119. The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD",
  120. "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and "OPTIONAL" in this
  121. document are to be interpreted as described in <xref target="RFC2119"/>.
  122. </t>
  123. </section>
  124. <section anchor="packet_organization" title="Packet Organization">
  125. <t>
  126. An Ogg Opus stream is organized as follows.
  127. </t>
  128. <t>
  129. There are two mandatory header packets.
  130. The first packet in the logical Ogg bitstream MUST contain the identification
  131. (ID) header, which uniquely identifies a stream as Opus audio.
  132. The format of this header is defined in <xref target="id_header"/>.
  133. It is placed alone (without any other packet data) on the first page of
  134. the logical Ogg bitstream, and completes on that page.
  135. This page has its 'beginning of stream' flag set.
  136. </t>
  137. <t>
  138. The second packet in the logical Ogg bitstream MUST contain the comment header,
  139. which contains user-supplied metadata.
  140. The format of this header is defined in <xref target="comment_header"/>.
  141. It MAY span multiple pages, beginning on the second page of the logical
  142. stream.
  143. However many pages it spans, the comment header packet MUST finish the page on
  144. which it completes.
  145. </t>
  146. <t>
  147. All subsequent pages are audio data pages, and the Ogg packets they contain are
  148. audio data packets.
  149. Each audio data packet contains one Opus packet for each of N different
  150. streams, where N is typically one for mono or stereo, but MAY be greater than
  151. one for multichannel audio.
  152. The value N is specified in the ID header (see
  153. <xref target="channel_mapping"/>), and is fixed over the entire length of the
  154. logical Ogg bitstream.
  155. </t>
  156. <t>
  157. The first (N&nbsp;-&nbsp;1) Opus packets, if any, are packed one after another
  158. into the Ogg packet, using the self-delimiting framing from Appendix&nbsp;B of
  159. <xref target="RFC6716"/>.
  160. The remaining Opus packet is packed at the end of the Ogg packet using the
  161. regular, undelimited framing from Section&nbsp;3 of <xref target="RFC6716"/>.
  162. All of the Opus packets in a single Ogg packet MUST be constrained to have the
  163. same duration.
  164. An implementation of this specification SHOULD treat any Opus packet whose
  165. duration is different from that of the first Opus packet in an Ogg packet as
  166. if it were a malformed Opus packet with an invalid Table Of Contents (TOC)
  167. sequence.
  168. </t>
  169. <t>
  170. The TOC sequence at the beginning of each Opus packet indicates the coding
  171. mode, audio bandwidth, channel count, duration (frame size), and number of
  172. frames per packet, as described in Section&nbsp;3.1
  173. of&nbsp;<xref target="RFC6716"/>.
  174. The coding mode is one of SILK, Hybrid, or Constrained Energy Lapped Transform
  175. (CELT).
  176. The combination of coding mode, audio bandwidth, and frame size is referred to
  177. as the configuration of an Opus packet.
  178. </t>
  179. <t>
  180. Packets are placed into Ogg pages in order until the end of stream.
  181. Audio data packets might span page boundaries.
  182. The first audio data page could have the 'continued packet' flag set
  183. (indicating the first audio data packet is continued from a previous page) if,
  184. for example, it was a live stream joined mid-broadcast, with the headers
  185. pasted on the front.
  186. A demuxer SHOULD NOT attempt to decode the data for the first packet on a page
  187. with the 'continued packet' flag set if the previous page with packet data
  188. does not end in a continued packet (i.e., did not end with a lacing value of
  189. 255) or if the page sequence numbers are not consecutive, unless the demuxer
  190. has some special knowledge that would allow it to interpret this data
  191. despite the missing pieces.
  192. An implementation MUST treat a zero-octet audio data packet as if it were a
  193. malformed Opus packet as described in
  194. Section&nbsp;3.4 of&nbsp;<xref target="RFC6716"/>.
  195. </t>
  196. <t>
  197. A logical stream ends with a page with the 'end of stream' flag set, but
  198. implementations need to be prepared to deal with truncated streams that do not
  199. have a page marked 'end of stream'.
  200. There is no reason for the final packet on the last page to be a continued
  201. packet, i.e., for the final lacing value to be less than 255.
  202. However, demuxers might encounter such streams, possibly as the result of a
  203. transfer that did not complete or of corruption.
  204. A demuxer SHOULD NOT attempt to decode the data from a packet that continues
  205. onto a subsequent page (i.e., when the page ends with a lacing value of 255)
  206. if the next page with packet data does not have the 'continued packet' flag
  207. set or does not exist, or if the page sequence numbers are not consecutive,
  208. unless the demuxer has some special knowledge that would allow it to interpret
  209. this data despite the missing pieces.
  210. There MUST NOT be any more pages in an Opus logical bitstream after a page
  211. marked 'end of stream'.
  212. </t>
  213. </section>
  214. <section anchor="granpos" title="Granule Position">
  215. <t>
  216. The granule position MUST be zero for the ID header page and the
  217. page where the comment header completes.
  218. That is, the first page in the logical stream, and the last header
  219. page before the first audio data page both have a granule position of zero.
  220. </t>
  221. <t>
  222. The granule position of an audio data page encodes the total number of PCM
  223. samples in the stream up to and including the last fully-decodable sample from
  224. the last packet completed on that page.
  225. The granule position of the first audio data page will usually be larger than
  226. zero, as described in <xref target="start_granpos_restrictions"/>.
  227. </t>
  228. <t>
  229. A page that is entirely spanned by a single packet (that completes on a
  230. subsequent page) has no granule position, and the granule position field is
  231. set to the special value '-1' in two's complement.
  232. </t>
  233. <t>
  234. The granule position of an audio data page is in units of PCM audio samples at
  235. a fixed rate of 48&nbsp;kHz (per channel; a stereo stream's granule position
  236. does not increment at twice the speed of a mono stream).
  237. It is possible to run an Opus decoder at other sampling rates, but the value
  238. in the granule position field always counts samples assuming a 48&nbsp;kHz
  239. decoding rate, and the rest of this specification makes the same assumption.
  240. </t>
  241. <t>
  242. The duration of an Opus packet can be any multiple of 2.5&nbsp;ms, up to a
  243. maximum of 120&nbsp;ms.
  244. This duration is encoded in the TOC sequence at the beginning of each packet.
  245. The number of samples returned by a decoder corresponds to this duration
  246. exactly, even for the first few packets.
  247. For example, a 20&nbsp;ms packet fed to a decoder running at 48&nbsp;kHz will
  248. always return 960&nbsp;samples.
  249. A demuxer can parse the TOC sequence at the beginning of each Ogg packet to
  250. work backwards or forwards from a packet with a known granule position (i.e.,
  251. the last packet completed on some page) in order to assign granule positions
  252. to every packet, or even every individual sample.
  253. The one exception is the last page in the stream, as described below.
  254. </t>
  255. <t>
  256. All other pages with completed packets after the first MUST have a granule
  257. position equal to the number of samples contained in packets that complete on
  258. that page plus the granule position of the most recent page with completed
  259. packets.
  260. This guarantees that a demuxer can assign individual packets the same granule
  261. position when working forwards as when working backwards.
  262. For this to work, there cannot be any gaps.
  263. </t>
  264. <section anchor="gap-repair" title="Repairing Gaps in Real-time Streams">
  265. <t>
  266. In order to support capturing a real-time stream that has lost or not
  267. transmitted packets, a multiplexer (muxer) SHOULD emit packets that explicitly
  268. request the use of Packet Loss Concealment (PLC) in place of the missing
  269. packets.
  270. Implementations that fail to do so still MUST NOT increment the granule
  271. position for a page by anything other than the number of samples contained in
  272. packets that actually complete on that page.
  273. </t>
  274. <t>
  275. Only gaps that are a multiple of 2.5&nbsp;ms are repairable, as these are the
  276. only durations that can be created by packet loss or discontinuous
  277. transmission.
  278. Muxers need not handle other gap sizes.
  279. Creating the necessary packets involves synthesizing a TOC byte (defined in
  280. Section&nbsp;3.1 of&nbsp;<xref target="RFC6716"/>)&mdash;and whatever
  281. additional internal framing is needed&mdash;to indicate the packet duration
  282. for each stream.
  283. The actual length of each missing Opus frame inside the packet is zero bytes,
  284. as defined in Section&nbsp;3.2.1 of&nbsp;<xref target="RFC6716"/>.
  285. </t>
  286. <t>
  287. Zero-byte frames MAY be packed into packets using any of codes&nbsp;0, 1,
  288. 2, or&nbsp;3.
  289. When successive frames have the same configuration, the higher code packings
  290. reduce overhead.
  291. Likewise, if the TOC configuration matches, the muxer MAY further combine the
  292. empty frames with previous or subsequent non-zero-length frames (using
  293. code&nbsp;2 or VBR code&nbsp;3).
  294. </t>
  295. <t>
  296. <xref target="RFC6716"/> does not impose any requirements on the PLC, but this
  297. section outlines choices that are expected to have a positive influence on
  298. most PLC implementations, including the reference implementation.
  299. Synthesized TOC sequences SHOULD maintain the same mode, audio bandwidth,
  300. channel count, and frame size as the previous packet (if any).
  301. This is the simplest and usually the most well-tested case for the PLC to
  302. handle and it covers all losses that do not include a configuration switch,
  303. as defined in Section&nbsp;4.5 of&nbsp;<xref target="RFC6716"/>.
  304. </t>
  305. <t>
  306. When a previous packet is available, keeping the audio bandwidth and channel
  307. count the same allows the PLC to provide maximum continuity in the concealment
  308. data it generates.
  309. However, if the size of the gap is not a multiple of the most recent frame
  310. size, then the frame size will have to change for at least some frames.
  311. Such changes SHOULD be delayed as long as possible to simplify
  312. things for PLC implementations.
  313. </t>
  314. <t>
  315. As an example, a 95&nbsp;ms gap could be encoded as nineteen 5&nbsp;ms frames
  316. in two bytes with a single CBR code&nbsp;3 packet.
  317. If the previous frame size was 20&nbsp;ms, using four 20&nbsp;ms frames
  318. followed by three 5&nbsp;ms frames requires 4&nbsp;bytes (plus an extra byte
  319. of Ogg lacing overhead), but allows the PLC to use its well-tested steady
  320. state behavior for as long as possible.
  321. The total bitrate of the latter approach, including Ogg overhead, is about
  322. 0.4&nbsp;kbps, so the impact on file size is minimal.
  323. </t>
  324. <t>
  325. Changing modes is discouraged, since this causes some decoder implementations
  326. to reset their PLC state.
  327. However, SILK and Hybrid mode frames cannot fill gaps that are not a multiple
  328. of 10&nbsp;ms.
  329. If switching to CELT mode is needed to match the gap size, a muxer SHOULD do
  330. so at the end of the gap to allow the PLC to function for as long as possible.
  331. </t>
  332. <t>
  333. In the example above, if the previous frame was a 20&nbsp;ms SILK mode frame,
  334. the better solution is to synthesize a packet describing four 20&nbsp;ms SILK
  335. frames, followed by a packet with a single 10&nbsp;ms SILK
  336. frame, and finally a packet with a 5&nbsp;ms CELT frame, to fill the 95&nbsp;ms
  337. gap.
  338. This also requires four bytes to describe the synthesized packet data (two
  339. bytes for a CBR code 3 and one byte each for two code 0 packets) but three
  340. bytes of Ogg lacing overhead are needed to mark the packet boundaries.
  341. At 0.6 kbps, this is still a minimal bitrate impact over a naive, low quality
  342. solution.
  343. </t>
  344. <t>
  345. Since medium-band audio is an option only in the SILK mode, wideband frames
  346. SHOULD be generated if switching from that configuration to CELT mode, to
  347. ensure that any PLC implementation which does try to migrate state between
  348. the modes will be able to preserve all of the available audio bandwidth.
  349. </t>
  350. </section>
  351. <section anchor="preskip" title="Pre-skip">
  352. <t>
  353. There is some amount of latency introduced during the decoding process, to
  354. allow for overlap in the CELT mode, stereo mixing in the SILK mode, and
  355. resampling.
  356. The encoder might have introduced additional latency through its own resampling
  357. and analysis (though the exact amount is not specified).
  358. Therefore, the first few samples produced by the decoder do not correspond to
  359. real input audio, but are instead composed of padding inserted by the encoder
  360. to compensate for this latency.
  361. These samples need to be stored and decoded, as Opus is an asymptotically
  362. convergent predictive codec, meaning the decoded contents of each frame depend
  363. on the recent history of decoder inputs.
  364. However, a player will want to skip these samples after decoding them.
  365. </t>
  366. <t>
  367. A 'pre-skip' field in the ID header (see <xref target="id_header"/>) signals
  368. the number of samples that SHOULD be skipped (decoded but discarded) at the
  369. beginning of the stream, though some specific applications might have a reason
  370. for looking at that data.
  371. This amount need not be a multiple of 2.5&nbsp;ms, MAY be smaller than a single
  372. packet, or MAY span the contents of several packets.
  373. These samples are not valid audio.
  374. </t>
  375. <t>
  376. For example, if the first Opus frame uses the CELT mode, it will always
  377. produce 120 samples of windowed overlap-add data.
  378. However, the overlap data is initially all zeros (since there is no prior
  379. frame), meaning this cannot, in general, accurately represent the original
  380. audio.
  381. The SILK mode requires additional delay to account for its analysis and
  382. resampling latency.
  383. The encoder delays the original audio to avoid this problem.
  384. </t>
  385. <t>
  386. The pre-skip field MAY also be used to perform sample-accurate cropping of
  387. already encoded streams.
  388. In this case, a value of at least 3840&nbsp;samples (80&nbsp;ms) provides
  389. sufficient history to the decoder that it will have converged
  390. before the stream's output begins.
  391. </t>
  392. </section>
  393. <section anchor="pcm_sample_position" title="PCM Sample Position">
  394. <t>
  395. The PCM sample position is determined from the granule position using the
  396. formula
  397. </t>
  398. <figure align="center">
  399. <artwork align="center"><![CDATA[
  400. 'PCM sample position' = 'granule position' - 'pre-skip' .
  401. ]]></artwork>
  402. </figure>
  403. <t>
  404. For example, if the granule position of the first audio data page is 59,971,
  405. and the pre-skip is 11,971, then the PCM sample position of the last decoded
  406. sample from that page is 48,000.
  407. </t>
  408. <t>
  409. This can be converted into a playback time using the formula
  410. </t>
  411. <figure align="center">
  412. <artwork align="center"><![CDATA[
  413. 'PCM sample position'
  414. 'playback time' = --------------------- .
  415. 48000.0
  416. ]]></artwork>
  417. </figure>
  418. <t>
  419. The initial PCM sample position before any samples are played is normally '0'.
  420. In this case, the PCM sample position of the first audio sample to be played
  421. starts at '1', because it marks the time on the clock
  422. <spanx style="emph">after</spanx> that sample has been played, and a stream
  423. that is exactly one second long has a final PCM sample position of '48000',
  424. as in the example here.
  425. </t>
  426. <t>
  427. Vorbis streams use a granule position smaller than the number of audio samples
  428. contained in the first audio data page to indicate that some of those samples
  429. are trimmed from the output (see <xref target="vorbis-trim"/>).
  430. However, to do so, Vorbis requires that the first audio data page contains
  431. exactly two packets, in order to allow the decoder to perform PCM position
  432. adjustments before needing to return any PCM data.
  433. Opus uses the pre-skip mechanism for this purpose instead, since the encoder
  434. might introduce more than a single packet's worth of latency, and since very
  435. large packets in streams with a very large number of channels might not fit
  436. on a single page.
  437. </t>
  438. </section>
  439. <section anchor="end_trimming" title="End Trimming">
  440. <t>
  441. The page with the 'end of stream' flag set MAY have a granule position that
  442. indicates the page contains less audio data than would normally be returned by
  443. decoding up through the final packet.
  444. This is used to end the stream somewhere other than an even frame boundary.
  445. The granule position of the most recent audio data page with completed packets
  446. is used to make this determination, or '0' is used if there were no previous
  447. audio data pages with a completed packet.
  448. The difference between these granule positions indicates how many samples to
  449. keep after decoding the packets that completed on the final page.
  450. The remaining samples are discarded.
  451. The number of discarded samples SHOULD be no larger than the number decoded
  452. from the last packet.
  453. </t>
  454. </section>
  455. <section anchor="start_granpos_restrictions"
  456. title="Restrictions on the Initial Granule Position">
  457. <t>
  458. The granule position of the first audio data page with a completed packet MAY
  459. be larger than the number of samples contained in packets that complete on
  460. that page, however it MUST NOT be smaller, unless that page has the 'end of
  461. stream' flag set.
  462. Allowing a granule position larger than the number of samples allows the
  463. beginning of a stream to be cropped or a live stream to be joined without
  464. rewriting the granule position of all the remaining pages.
  465. This means that the PCM sample position just before the first sample to be
  466. played MAY be larger than '0'.
  467. Synchronization when multiplexing with other logical streams still uses the PCM
  468. sample position relative to '0' to compute sample times.
  469. This does not affect the behavior of pre-skip: exactly 'pre-skip' samples
  470. SHOULD be skipped from the beginning of the decoded output, even if the
  471. initial PCM sample position is greater than zero.
  472. </t>
  473. <t>
  474. On the other hand, a granule position that is smaller than the number of
  475. decoded samples prevents a demuxer from working backwards to assign each
  476. packet or each individual sample a valid granule position, since granule
  477. positions are non-negative.
  478. An implementation MUST reject as invalid any stream where the granule position
  479. is smaller than the number of samples contained in packets that complete on
  480. the first audio data page with a completed packet, unless that page has the
  481. 'end of stream' flag set.
  482. It MAY defer this action until it decodes the last packet completed on that
  483. page.
  484. </t>
  485. <t>
  486. If that page has the 'end of stream' flag set, a demuxer MUST reject as invalid
  487. any stream where its granule position is smaller than the 'pre-skip' amount.
  488. This would indicate that there are more samples to be skipped from the initial
  489. decoded output than exist in the stream.
  490. If the granule position is smaller than the number of decoded samples produced
  491. by the packets that complete on that page, then a demuxer MUST use an initial
  492. granule position of '0', and can work forwards from '0' to timestamp
  493. individual packets.
  494. If the granule position is larger than the number of decoded samples available,
  495. then the demuxer MUST still work backwards as described above, even if the
  496. 'end of stream' flag is set, to determine the initial granule position, and
  497. thus the initial PCM sample position.
  498. Both of these will be greater than '0' in this case.
  499. </t>
  500. </section>
  501. <section anchor="seeking_and_preroll" title="Seeking and Pre-roll">
  502. <t>
  503. Seeking in Ogg files is best performed using a bisection search for a page
  504. whose granule position corresponds to a PCM position at or before the seek
  505. target.
  506. With appropriately weighted bisection, accurate seeking can be performed in
  507. just one or two bisections on average, even in multi-gigabyte files.
  508. See <xref target="seeking"/> for an example of general implementation guidance.
  509. </t>
  510. <t>
  511. When seeking within an Ogg Opus stream, an implementation SHOULD start decoding
  512. (and discarding the output) at least 3840&nbsp;samples (80&nbsp;ms) prior to
  513. the seek target in order to ensure that the output audio is correct by the
  514. time it reaches the seek target.
  515. This 'pre-roll' is separate from, and unrelated to, the 'pre-skip' used at the
  516. beginning of the stream.
  517. If the point 80&nbsp;ms prior to the seek target comes before the initial PCM
  518. sample position, an implementation SHOULD start decoding from the beginning of
  519. the stream, applying pre-skip as normal, regardless of whether the pre-skip is
  520. larger or smaller than 80&nbsp;ms, and then continue to discard samples
  521. to reach the seek target (if any).
  522. </t>
  523. </section>
  524. </section>
  525. <section anchor="headers" title="Header Packets">
  526. <t>
  527. An Ogg Opus logical stream contains exactly two mandatory header packets:
  528. an identification header and a comment header.
  529. </t>
  530. <section anchor="id_header" title="Identification Header">
  531. <figure anchor="id_header_packet" title="ID Header Packet" align="center">
  532. <artwork align="center"><![CDATA[
  533. 0 1 2 3
  534. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  535. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  536. | 'O' | 'p' | 'u' | 's' |
  537. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  538. | 'H' | 'e' | 'a' | 'd' |
  539. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  540. | Version = 1 | Channel Count | Pre-skip |
  541. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  542. | Input Sample Rate (Hz) |
  543. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  544. | Output Gain (Q7.8 in dB) | Mapping Family| |
  545. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ :
  546. | |
  547. : Optional Channel Mapping Table... :
  548. | |
  549. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  550. ]]></artwork>
  551. </figure>
  552. <t>
  553. The fields in the identification (ID) header have the following meaning:
  554. <list style="numbers">
  555. <t>Magic Signature:
  556. <vspace blankLines="1"/>
  557. This is an 8-octet (64-bit) field that allows codec identification and is
  558. human-readable.
  559. It contains, in order, the magic numbers:
  560. <list style="empty">
  561. <t>0x4F 'O'</t>
  562. <t>0x70 'p'</t>
  563. <t>0x75 'u'</t>
  564. <t>0x73 's'</t>
  565. <t>0x48 'H'</t>
  566. <t>0x65 'e'</t>
  567. <t>0x61 'a'</t>
  568. <t>0x64 'd'</t>
  569. </list>
  570. Starting with "Op" helps distinguish it from audio data packets, as this is an
  571. invalid TOC sequence.
  572. <vspace blankLines="1"/>
  573. </t>
  574. <t>Version (8 bits, unsigned):
  575. <vspace blankLines="1"/>
  576. The version number MUST always be '1' for this version of the encapsulation
  577. specification.
  578. Implementations SHOULD treat streams where the upper four bits of the version
  579. number match that of a recognized specification as backwards-compatible with
  580. that specification.
  581. That is, the version number can be split into "major" and "minor" version
  582. sub-fields, with changes to the "minor" sub-field (in the lower four bits)
  583. signaling compatible changes.
  584. For example, an implementation of this specification SHOULD accept any stream
  585. with a version number of '15' or less, and SHOULD assume any stream with a
  586. version number '16' or greater is incompatible.
  587. The initial version '1' was chosen to keep implementations from relying on this
  588. octet as a null terminator for the "OpusHead" string.
  589. <vspace blankLines="1"/>
  590. </t>
  591. <t>Output Channel Count 'C' (8 bits, unsigned):
  592. <vspace blankLines="1"/>
  593. This is the number of output channels.
  594. This might be different than the number of encoded channels, which can change
  595. on a packet-by-packet basis.
  596. This value MUST NOT be zero.
  597. The maximum allowable value depends on the channel mapping family, and might be
  598. as large as 255.
  599. See <xref target="channel_mapping"/> for details.
  600. <vspace blankLines="1"/>
  601. </t>
  602. <t>Pre-skip (16 bits, unsigned, little
  603. endian):
  604. <vspace blankLines="1"/>
  605. This is the number of samples (at 48&nbsp;kHz) to discard from the decoder
  606. output when starting playback, and also the number to subtract from a page's
  607. granule position to calculate its PCM sample position.
  608. When cropping the beginning of existing Ogg Opus streams, a pre-skip of at
  609. least 3,840&nbsp;samples (80&nbsp;ms) is RECOMMENDED to ensure complete
  610. convergence in the decoder.
  611. <vspace blankLines="1"/>
  612. </t>
  613. <t>Input Sample Rate (32 bits, unsigned, little
  614. endian):
  615. <vspace blankLines="1"/>
  616. This is the sample rate of the original input (before encoding), in Hz.
  617. This field is <spanx style="emph">not</spanx> the sample rate to use for
  618. playback of the encoded data.
  619. <vspace blankLines="1"/>
  620. Opus can switch between internal audio bandwidths of 4, 6, 8, 12, and
  621. 20&nbsp;kHz.
  622. Each packet in the stream can have a different audio bandwidth.
  623. Regardless of the audio bandwidth, the reference decoder supports decoding any
  624. stream at a sample rate of 8, 12, 16, 24, or 48&nbsp;kHz.
  625. The original sample rate of the audio passed to the encoder is not preserved
  626. by the lossy compression.
  627. <vspace blankLines="1"/>
  628. An Ogg Opus player SHOULD select the playback sample rate according to the
  629. following procedure:
  630. <list style="numbers">
  631. <t>If the hardware supports 48&nbsp;kHz playback, decode at 48&nbsp;kHz.</t>
  632. <t>Otherwise, if the hardware's highest available sample rate is a supported
  633. rate, decode at this sample rate.</t>
  634. <t>Otherwise, if the hardware's highest available sample rate is less than
  635. 48&nbsp;kHz, decode at the next higher Opus supported rate above the highest
  636. available hardware rate and resample.</t>
  637. <t>Otherwise, decode at 48&nbsp;kHz and resample.</t>
  638. </list>
  639. However, the 'Input Sample Rate' field allows the muxer to pass the sample
  640. rate of the original input stream as metadata.
  641. This is useful when the user requires the output sample rate to match the
  642. input sample rate.
  643. For example, when not playing the output, an implementation writing PCM format
  644. samples to disk might choose to resample the audio back to the original input
  645. sample rate to reduce surprise to the user, who might reasonably expect to get
  646. back a file with the same sample rate.
  647. <vspace blankLines="1"/>
  648. A value of zero indicates 'unspecified'.
  649. Muxers SHOULD write the actual input sample rate or zero, but implementations
  650. which do something with this field SHOULD take care to behave sanely if given
  651. crazy values (e.g., do not actually upsample the output to 10 MHz if
  652. requested).
  653. Implementations SHOULD support input sample rates between 8&nbsp;kHz and
  654. 192&nbsp;kHz (inclusive).
  655. Rates outside this range MAY be ignored by falling back to the default rate of
  656. 48&nbsp;kHz instead.
  657. <vspace blankLines="1"/>
  658. </t>
  659. <t>Output Gain (16 bits, signed, little endian):
  660. <vspace blankLines="1"/>
  661. This is a gain to be applied when decoding.
  662. It is 20*log10 of the factor by which to scale the decoder output to achieve
  663. the desired playback volume, stored in a 16-bit, signed, two's complement
  664. fixed-point value with 8 fractional bits (i.e., Q7.8).
  665. <vspace blankLines="1"/>
  666. To apply the gain, an implementation could use
  667. <figure align="center">
  668. <artwork align="center"><![CDATA[
  669. sample *= pow(10, output_gain/(20.0*256)) ,
  670. ]]></artwork>
  671. </figure>
  672. where output_gain is the raw 16-bit value from the header.
  673. <vspace blankLines="1"/>
  674. Players and media frameworks SHOULD apply it by default.
  675. If a player chooses to apply any volume adjustment or gain modification, such
  676. as the R128_TRACK_GAIN (see <xref target="comment_header"/>), the adjustment
  677. MUST be applied in addition to this output gain in order to achieve playback
  678. at the normalized volume.
  679. <vspace blankLines="1"/>
  680. A muxer SHOULD set this field to zero, and instead apply any gain prior to
  681. encoding, when this is possible and does not conflict with the user's wishes.
  682. A nonzero output gain indicates the gain was adjusted after encoding, or that
  683. a user wished to adjust the gain for playback while preserving the ability
  684. to recover the original signal amplitude.
  685. <vspace blankLines="1"/>
  686. Although the output gain has enormous range (+/- 128 dB, enough to amplify
  687. inaudible sounds to the threshold of physical pain), most applications can
  688. only reasonably use a small portion of this range around zero.
  689. The large range serves in part to ensure that gain can always be losslessly
  690. transferred between OpusHead and R128 gain tags (see below) without
  691. saturating.
  692. <vspace blankLines="1"/>
  693. </t>
  694. <t>Channel Mapping Family (8 bits, unsigned):
  695. <vspace blankLines="1"/>
  696. This octet indicates the order and semantic meaning of the output channels.
  697. <vspace blankLines="1"/>
  698. Each currently specified value of this octet indicates a mapping family, which
  699. defines a set of allowed channel counts, and the ordered set of channel names
  700. for each allowed channel count.
  701. The details are described in <xref target="channel_mapping"/>.
  702. </t>
  703. <t>Channel Mapping Table:
  704. This table defines the mapping from encoded streams to output channels.
  705. Its contents are specified in <xref target="channel_mapping"/>.
  706. </t>
  707. </list>
  708. </t>
  709. <t>
  710. All fields in the ID headers are REQUIRED, except for the channel mapping
  711. table, which MUST be omitted when the channel mapping family is 0, but
  712. is REQUIRED otherwise.
  713. Implementations SHOULD reject streams with ID headers that do not contain
  714. enough data for these fields, even if they contain a valid Magic Signature.
  715. Future versions of this specification, even backwards-compatible versions,
  716. might include additional fields in the ID header.
  717. If an ID header has a compatible major version, but a larger minor version,
  718. an implementation MUST NOT reject it for containing additional data not
  719. specified here, provided it still completes on the first page.
  720. </t>
  721. <section anchor="channel_mapping" title="Channel Mapping">
  722. <t>
  723. An Ogg Opus stream allows mapping one number of Opus streams (N) to a possibly
  724. larger number of decoded channels (M&nbsp;+&nbsp;N) to yet another number of
  725. output channels (C), which might be larger or smaller than the number of
  726. decoded channels.
  727. The order and meaning of these channels are defined by a channel mapping,
  728. which consists of the 'channel mapping family' octet and, for channel mapping
  729. families other than family&nbsp;0, a channel mapping table, as illustrated in
  730. <xref target="channel_mapping_table"/>.
  731. </t>
  732. <figure anchor="channel_mapping_table" title="Channel Mapping Table"
  733. align="center">
  734. <artwork align="center"><![CDATA[
  735. 0 1 2 3
  736. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  737. +-+-+-+-+-+-+-+-+
  738. | Stream Count |
  739. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  740. | Coupled Count | Channel Mapping... :
  741. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  742. ]]></artwork>
  743. </figure>
  744. <t>
  745. The fields in the channel mapping table have the following meaning:
  746. <list style="numbers" counter="8">
  747. <t>Stream Count 'N' (8 bits, unsigned):
  748. <vspace blankLines="1"/>
  749. This is the total number of streams encoded in each Ogg packet.
  750. This value is necessary to correctly parse the packed Opus packets inside an
  751. Ogg packet, as described in <xref target="packet_organization"/>.
  752. This value MUST NOT be zero, as without at least one Opus packet with a valid
  753. TOC sequence, a demuxer cannot recover the duration of an Ogg packet.
  754. <vspace blankLines="1"/>
  755. For channel mapping family&nbsp;0, this value defaults to 1, and is not coded.
  756. <vspace blankLines="1"/>
  757. </t>
  758. <t>Coupled Stream Count 'M' (8 bits, unsigned):
  759. This is the number of streams whose decoders are to be configured to produce
  760. two channels (stereo).
  761. This MUST be no larger than the total number of streams, N.
  762. <vspace blankLines="1"/>
  763. Each packet in an Opus stream has an internal channel count of 1 or 2, which
  764. can change from packet to packet.
  765. This is selected by the encoder depending on the bitrate and the audio being
  766. encoded.
  767. The original channel count of the audio passed to the encoder is not
  768. necessarily preserved by the lossy compression.
  769. <vspace blankLines="1"/>
  770. Regardless of the internal channel count, any Opus stream can be decoded as
  771. mono (a single channel) or stereo (two channels) by appropriate initialization
  772. of the decoder.
  773. The 'coupled stream count' field indicates that the decoders for the first M
  774. Opus streams are to be initialized for stereo (two-channel) output, and the
  775. remaining (N&nbsp;-&nbsp;M) decoders are to be initialized for mono (a single
  776. channel) only.
  777. The total number of decoded channels, (M&nbsp;+&nbsp;N), MUST be no larger than
  778. 255, as there is no way to index more channels than that in the channel
  779. mapping.
  780. <vspace blankLines="1"/>
  781. For channel mapping family&nbsp;0, this value defaults to (C&nbsp;-&nbsp;1)
  782. (i.e., 0 for mono and 1 for stereo), and is not coded.
  783. <vspace blankLines="1"/>
  784. </t>
  785. <t>Channel Mapping (8*C bits):
  786. This contains one octet per output channel, indicating which decoded channel
  787. is to be used for each one.
  788. Let 'index' be the value of this octet for a particular output channel.
  789. This value MUST either be smaller than (M&nbsp;+&nbsp;N), or be the special
  790. value 255.
  791. If 'index' is less than 2*M, the output MUST be taken from decoding stream
  792. ('index'/2) as stereo and selecting the left channel if 'index' is even, and
  793. the right channel if 'index' is odd.
  794. If 'index' is 2*M or larger, but less than 255, the output MUST be taken from
  795. decoding stream ('index'&nbsp;-&nbsp;M) as mono.
  796. If 'index' is 255, the corresponding output channel MUST contain pure silence.
  797. <vspace blankLines="1"/>
  798. The number of output channels, C, is not constrained to match the number of
  799. decoded channels (M&nbsp;+&nbsp;N).
  800. A single index value MAY appear multiple times, i.e., the same decoded channel
  801. might be mapped to multiple output channels.
  802. Some decoded channels might not be assigned to any output channel, as well.
  803. <vspace blankLines="1"/>
  804. For channel mapping family&nbsp;0, the first index defaults to 0, and if
  805. C&nbsp;==&nbsp;2, the second index defaults to 1.
  806. Neither index is coded.
  807. </t>
  808. </list>
  809. </t>
  810. <t>
  811. After producing the output channels, the channel mapping family determines the
  812. semantic meaning of each one.
  813. There are three defined mapping families in this specification.
  814. </t>
  815. <section anchor="channel_mapping_0" title="Channel Mapping Family 0">
  816. <t>
  817. Allowed numbers of channels: 1 or 2.
  818. RTP mapping.
  819. This is the same channel interpretation as <xref target="RFC7587"/>.
  820. </t>
  821. <t>
  822. <list style="symbols">
  823. <t>1 channel: monophonic (mono).</t>
  824. <t>2 channels: stereo (left, right).</t>
  825. </list>
  826. Special mapping: This channel mapping value also
  827. indicates that the contents consists of a single Opus stream that is stereo if
  828. and only if C&nbsp;==&nbsp;2, with stream index&nbsp;0 mapped to output
  829. channel&nbsp;0 (mono, or left channel) and stream index&nbsp;1 mapped to
  830. output channel&nbsp;1 (right channel) if stereo.
  831. When the 'channel mapping family' octet has this value, the channel mapping
  832. table MUST be omitted from the ID header packet.
  833. </t>
  834. </section>
  835. <section anchor="channel_mapping_1" title="Channel Mapping Family 1">
  836. <t>
  837. Allowed numbers of channels: 1...8.
  838. Vorbis channel order (see below).
  839. </t>
  840. <t>
  841. Each channel is assigned to a speaker location in a conventional surround
  842. arrangement.
  843. Specific locations depend on the number of channels, and are given below
  844. in order of the corresponding channel indices.
  845. <list style="symbols">
  846. <t>1 channel: monophonic (mono).</t>
  847. <t>2 channels: stereo (left, right).</t>
  848. <t>3 channels: linear surround (left, center, right)</t>
  849. <t>4 channels: quadraphonic (front&nbsp;left, front&nbsp;right, rear&nbsp;left, rear&nbsp;right).</t>
  850. <t>5 channels: 5.0 surround (front&nbsp;left, front&nbsp;center, front&nbsp;right, rear&nbsp;left, rear&nbsp;right).</t>
  851. <t>6 channels: 5.1 surround (front&nbsp;left, front&nbsp;center, front&nbsp;right, rear&nbsp;left, rear&nbsp;right, LFE).</t>
  852. <t>7 channels: 6.1 surround (front&nbsp;left, front&nbsp;center, front&nbsp;right, side&nbsp;left, side&nbsp;right, rear&nbsp;center, LFE).</t>
  853. <t>8 channels: 7.1 surround (front&nbsp;left, front&nbsp;center, front&nbsp;right, side&nbsp;left, side&nbsp;right, rear&nbsp;left, rear&nbsp;right, LFE)</t>
  854. </list>
  855. </t>
  856. <t>
  857. This set of surround options and speaker location orderings is the same
  858. as those used by the Vorbis codec <xref target="vorbis-mapping"/>.
  859. The ordering is different from the one used by the
  860. WAVE <xref target="wave-multichannel"/> and
  861. Free Lossless Audio Codec (FLAC) <xref target="flac"/> formats,
  862. so correct ordering requires permutation of the output channels when decoding
  863. to or encoding from those formats.
  864. 'LFE' here refers to a Low Frequency Effects channel, often mapped to a
  865. subwoofer with no particular spatial position.
  866. Implementations SHOULD identify 'side' or 'rear' speaker locations with
  867. 'surround' and 'back' as appropriate when interfacing with audio formats
  868. or systems which prefer that terminology.
  869. </t>
  870. </section>
  871. <section anchor="channel_mapping_255"
  872. title="Channel Mapping Family 255">
  873. <t>
  874. Allowed numbers of channels: 1...255.
  875. No defined channel meaning.
  876. </t>
  877. <t>
  878. Channels are unidentified.
  879. General-purpose players SHOULD NOT attempt to play these streams.
  880. Offline implementations MAY deinterleave the output into separate PCM files,
  881. one per channel.
  882. Implementations SHOULD NOT produce output for channels mapped to stream index
  883. 255 (pure silence) unless they have no other way to indicate the index of
  884. non-silent channels.
  885. </t>
  886. </section>
  887. <section anchor="channel_mapping_undefined"
  888. title="Undefined Channel Mappings">
  889. <t>
  890. The remaining channel mapping families (2...254) are reserved.
  891. A demuxer implementation encountering a reserved channel mapping family value
  892. SHOULD act as though the value is 255.
  893. </t>
  894. </section>
  895. <section anchor="downmix" title="Downmixing">
  896. <t>
  897. An Ogg Opus player MUST support any valid channel mapping with a channel
  898. mapping family of 0 or 1, even if the number of channels does not match the
  899. physically connected audio hardware.
  900. Players SHOULD perform channel mixing to increase or reduce the number of
  901. channels as needed.
  902. </t>
  903. <t>
  904. Implementations MAY use the following matrices to implement downmixing from
  905. multichannel files using <xref target="channel_mapping_1">Channel Mapping
  906. Family 1</xref>, which are known to give acceptable results for stereo.
  907. Matrices for 3 and 4 channels are normalized so each coefficient row sums
  908. to 1 to avoid clipping.
  909. For 5 or more channels they are normalized to 2 as a compromise between
  910. clipping and dynamic range reduction.
  911. </t>
  912. <t>
  913. In these matrices the front left and front right channels are generally
  914. passed through directly.
  915. When a surround channel is split between both the left and right stereo
  916. channels, coefficients are chosen so their squares sum to 1, which
  917. helps preserve the perceived intensity.
  918. Rear channels are mixed more diffusely or attenuated to maintain focus
  919. on the front channels.
  920. </t>
  921. <figure anchor="downmix-matrix-3"
  922. title="Stereo downmix matrix for the linear surround channel mapping"
  923. align="center">
  924. <artwork align="center"><![CDATA[
  925. L output = ( 0.585786 * left + 0.414214 * center )
  926. R output = ( 0.414214 * center + 0.585786 * right )
  927. ]]></artwork>
  928. <postamble>
  929. Exact coefficient values are 1 and 1/sqrt(2), multiplied by
  930. 1/(1&nbsp;+&nbsp;1/sqrt(2)) for normalization.
  931. </postamble>
  932. </figure>
  933. <figure anchor="downmix-matrix-4"
  934. title="Stereo downmix matrix for the quadraphonic channel mapping"
  935. align="center">
  936. <artwork align="center"><![CDATA[
  937. / \ / \ / FL \
  938. | L output | | 0.422650 0.000000 0.366025 0.211325 | | FR |
  939. | R output | = | 0.000000 0.422650 0.211325 0.366025 | | RL |
  940. \ / \ / \ RR /
  941. ]]></artwork>
  942. <postamble>
  943. Exact coefficient values are 1, sqrt(3)/2 and 1/2, multiplied by
  944. 1/(1&nbsp;+&nbsp;sqrt(3)/2&nbsp;+&nbsp;1/2) for normalization.
  945. </postamble>
  946. </figure>
  947. <figure anchor="downmix-matrix-5"
  948. title="Stereo downmix matrix for the 5.0 surround mapping"
  949. align="center">
  950. <artwork align="center"><![CDATA[
  951. / FL \
  952. / \ / \ | FC |
  953. | L | | 0.650802 0.460186 0.000000 0.563611 0.325401 | | FR |
  954. | R | = | 0.000000 0.460186 0.650802 0.325401 0.563611 | | RL |
  955. \ / \ / | RR |
  956. \ /
  957. ]]></artwork>
  958. <postamble>
  959. Exact coefficient values are 1, 1/sqrt(2), sqrt(3)/2 and 1/2, multiplied by
  960. 2/(1&nbsp;+&nbsp;1/sqrt(2)&nbsp;+&nbsp;sqrt(3)/2&nbsp;+&nbsp;1/2)
  961. for normalization.
  962. </postamble>
  963. </figure>
  964. <figure anchor="downmix-matrix-6"
  965. title="Stereo downmix matrix for the 5.1 surround mapping"
  966. align="center">
  967. <artwork align="center"><![CDATA[
  968. /FL \
  969. / \ / \ |FC |
  970. |L| | 0.529067 0.374107 0.000000 0.458186 0.264534 0.374107 | |FR |
  971. |R| = | 0.000000 0.374107 0.529067 0.264534 0.458186 0.374107 | |RL |
  972. \ / \ / |RR |
  973. \LFE/
  974. ]]></artwork>
  975. <postamble>
  976. Exact coefficient values are 1, 1/sqrt(2), sqrt(3)/2 and 1/2, multiplied by
  977. 2/(1&nbsp;+&nbsp;1/sqrt(2)&nbsp;+&nbsp;sqrt(3)/2&nbsp;+&nbsp;1/2 + 1/sqrt(2))
  978. for normalization.
  979. </postamble>
  980. </figure>
  981. <figure anchor="downmix-matrix-7"
  982. title="Stereo downmix matrix for the 6.1 surround mapping"
  983. align="center">
  984. <artwork align="center"><![CDATA[
  985. / \
  986. | 0.455310 0.321953 0.000000 0.394310 0.227655 0.278819 0.321953 |
  987. | 0.000000 0.321953 0.455310 0.227655 0.394310 0.278819 0.321953 |
  988. \ /
  989. ]]></artwork>
  990. <postamble>
  991. Exact coefficient values are 1, 1/sqrt(2), sqrt(3)/2, 1/2 and
  992. sqrt(3)/2/sqrt(2), multiplied by
  993. 2/(1&nbsp;+&nbsp;1/sqrt(2)&nbsp;+&nbsp;sqrt(3)/2&nbsp;+&nbsp;1/2 +
  994. sqrt(3)/2/sqrt(2) + 1/sqrt(2)) for normalization.
  995. The coefficients are in the same order as in <xref target="channel_mapping_1" />,
  996. and the matrices above.
  997. </postamble>
  998. </figure>
  999. <figure anchor="downmix-matrix-8"
  1000. title="Stereo downmix matrix for the 7.1 surround mapping"
  1001. align="center">
  1002. <artwork align="center"><![CDATA[
  1003. / \
  1004. | .388631 .274804 .000000 .336565 .194316 .336565 .194316 .274804 |
  1005. | .000000 .274804 .388631 .194316 .336565 .194316 .336565 .274804 |
  1006. \ /
  1007. ]]></artwork>
  1008. <postamble>
  1009. Exact coefficient values are 1, 1/sqrt(2), sqrt(3)/2 and 1/2, multiplied by
  1010. 2/(2&nbsp;+&nbsp;2/sqrt(2)&nbsp;+&nbsp;sqrt(3)) for normalization.
  1011. The coefficients are in the same order as in <xref target="channel_mapping_1" />,
  1012. and the matrices above.
  1013. </postamble>
  1014. </figure>
  1015. </section>
  1016. </section> <!-- end channel_mapping_table -->
  1017. </section> <!-- end id_header -->
  1018. <section anchor="comment_header" title="Comment Header">
  1019. <figure anchor="comment_header_packet" title="Comment Header Packet"
  1020. align="center">
  1021. <artwork align="center"><![CDATA[
  1022. 0 1 2 3
  1023. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  1024. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1025. | 'O' | 'p' | 'u' | 's' |
  1026. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1027. | 'T' | 'a' | 'g' | 's' |
  1028. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1029. | Vendor String Length |
  1030. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1031. | |
  1032. : Vendor String... :
  1033. | |
  1034. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1035. | User Comment List Length |
  1036. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1037. | User Comment #0 String Length |
  1038. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1039. | |
  1040. : User Comment #0 String... :
  1041. | |
  1042. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1043. | User Comment #1 String Length |
  1044. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1045. : :
  1046. ]]></artwork>
  1047. </figure>
  1048. <t>
  1049. The comment header consists of a 64-bit magic signature, followed by data in
  1050. the same format as the <xref target="vorbis-comment"/> header used in Ogg
  1051. Vorbis, except (like Ogg Theora and Speex) the final "framing bit" specified
  1052. in the Vorbis spec is not present.
  1053. <list style="numbers">
  1054. <t>Magic Signature:
  1055. <vspace blankLines="1"/>
  1056. This is an 8-octet (64-bit) field that allows codec identification and is
  1057. human-readable.
  1058. It contains, in order, the magic numbers:
  1059. <list style="empty">
  1060. <t>0x4F 'O'</t>
  1061. <t>0x70 'p'</t>
  1062. <t>0x75 'u'</t>
  1063. <t>0x73 's'</t>
  1064. <t>0x54 'T'</t>
  1065. <t>0x61 'a'</t>
  1066. <t>0x67 'g'</t>
  1067. <t>0x73 's'</t>
  1068. </list>
  1069. Starting with "Op" helps distinguish it from audio data packets, as this is an
  1070. invalid TOC sequence.
  1071. <vspace blankLines="1"/>
  1072. </t>
  1073. <t>Vendor String Length (32 bits, unsigned, little endian):
  1074. <vspace blankLines="1"/>
  1075. This field gives the length of the following vendor string, in octets.
  1076. It MUST NOT indicate that the vendor string is longer than the rest of the
  1077. packet.
  1078. <vspace blankLines="1"/>
  1079. </t>
  1080. <t>Vendor String (variable length, UTF-8 vector):
  1081. <vspace blankLines="1"/>
  1082. This is a simple human-readable tag for vendor information, encoded as a UTF-8
  1083. string&nbsp;<xref target="RFC3629"/>.
  1084. No terminating null octet is necessary.
  1085. <vspace blankLines="1"/>
  1086. This tag is intended to identify the codec encoder and encapsulation
  1087. implementations, for tracing differences in technical behavior.
  1088. User-facing applications can use the 'ENCODER' user comment tag to identify
  1089. themselves.
  1090. <vspace blankLines="1"/>
  1091. </t>
  1092. <t>User Comment List Length (32 bits, unsigned, little endian):
  1093. <vspace blankLines="1"/>
  1094. This field indicates the number of user-supplied comments.
  1095. It MAY indicate there are zero user-supplied comments, in which case there are
  1096. no additional fields in the packet.
  1097. It MUST NOT indicate that there are so many comments that the comment string
  1098. lengths would require more data than is available in the rest of the packet.
  1099. <vspace blankLines="1"/>
  1100. </t>
  1101. <t>User Comment #i String Length (32 bits, unsigned, little endian):
  1102. <vspace blankLines="1"/>
  1103. This field gives the length of the following user comment string, in octets.
  1104. There is one for each user comment indicated by the 'user comment list length'
  1105. field.
  1106. It MUST NOT indicate that the string is longer than the rest of the packet.
  1107. <vspace blankLines="1"/>
  1108. </t>
  1109. <t>User Comment #i String (variable length, UTF-8 vector):
  1110. <vspace blankLines="1"/>
  1111. This field contains a single user comment string.
  1112. There is one for each user comment indicated by the 'user comment list length'
  1113. field.
  1114. </t>
  1115. </list>
  1116. </t>
  1117. <t>
  1118. The vendor string length and user comment list length are REQUIRED, and
  1119. implementations SHOULD reject comment headers that do not contain enough data
  1120. for these fields, or that do not contain enough data for the corresponding
  1121. vendor string or user comments they describe.
  1122. Making this check before allocating the associated memory to contain the data
  1123. helps prevent a possible Denial-of-Service (DoS) attack from small comment
  1124. headers that claim to contain strings longer than the entire packet or more
  1125. user comments than than could possibly fit in the packet.
  1126. </t>
  1127. <t>
  1128. Immediately following the user comment list, the comment header MAY
  1129. contain zero-padding or other binary data which is not specified here.
  1130. If the least-significant bit of the first byte of this data is 1, then editors
  1131. SHOULD preserve the contents of this data when updating the tags, but if this
  1132. bit is 0, all such data MAY be treated as padding, and truncated or discarded
  1133. as desired.
  1134. This allows informal experimentation with the format of this binary data until
  1135. it can be specified later.
  1136. </t>
  1137. <t>
  1138. The comment header can be arbitrarily large and might be spread over a large
  1139. number of Ogg pages.
  1140. Implementations MUST avoid attempting to allocate excessive amounts of memory
  1141. when presented with a very large comment header.
  1142. To accomplish this, implementations MAY reject a comment header larger than
  1143. 125,829,120&nbsp;octets, and MAY ignore individual comments that are not fully
  1144. contained within the first 61,440 octets of the comment header.
  1145. </t>
  1146. <section anchor="comment_format" title="Tag Definitions">
  1147. <t>
  1148. The user comment strings follow the NAME=value format described by
  1149. <xref target="vorbis-comment"/> with the same recommended tag names:
  1150. ARTIST, TITLE, DATE, ALBUM, and so on.
  1151. </t>
  1152. <t>
  1153. Two new comment tags are introduced here:
  1154. </t>
  1155. <t>First, an optional gain for track normalization:</t>
  1156. <figure align="center">
  1157. <artwork align="left"><![CDATA[
  1158. R128_TRACK_GAIN=-573
  1159. ]]></artwork>
  1160. </figure>
  1161. <t>
  1162. representing the volume shift needed to normalize the track's volume
  1163. during isolated playback, in random shuffle, and so on.
  1164. The gain is a Q7.8 fixed point number in dB, as in the ID header's 'output
  1165. gain' field.
  1166. This tag is similar to the REPLAYGAIN_TRACK_GAIN tag in
  1167. Vorbis&nbsp;<xref target="replay-gain"/>, except that the normal volume
  1168. reference is the <xref target="EBU-R128"/> standard.
  1169. </t>
  1170. <t>Second, an optional gain for album normalization:</t>
  1171. <figure align="center">
  1172. <artwork align="left"><![CDATA[
  1173. R128_ALBUM_GAIN=111
  1174. ]]></artwork>
  1175. </figure>
  1176. <t>
  1177. representing the volume shift needed to normalize the overall volume when
  1178. played as part of a particular collection of tracks.
  1179. The gain is also a Q7.8 fixed point number in dB, as in the ID header's
  1180. 'output gain' field.
  1181. </t>
  1182. <t>
  1183. An Ogg Opus stream MUST NOT have more than one of each of these tags, and if
  1184. present their values MUST be an integer from -32768 to 32767, inclusive,
  1185. represented in ASCII as a base 10 number with no whitespace.
  1186. A leading '+' or '-' character is valid.
  1187. Leading zeros are also permitted, but the value MUST be represented by
  1188. no more than 6 characters.
  1189. Other non-digit characters MUST NOT be present.
  1190. </t>
  1191. <t>
  1192. If present, R128_TRACK_GAIN and R128_ALBUM_GAIN MUST correctly represent
  1193. the R128 normalization gain relative to the 'output gain' field specified
  1194. in the ID header.
  1195. If a player chooses to make use of the R128_TRACK_GAIN tag or the
  1196. R128_ALBUM_GAIN tag, it MUST apply those gains
  1197. <spanx style="emph">in addition</spanx> to the 'output gain' value.
  1198. If a tool modifies the ID header's 'output gain' field, it MUST also update or
  1199. remove the R128_TRACK_GAIN and R128_ALBUM_GAIN comment tags if present.
  1200. A muxer SHOULD place the gain it wants other tools to use by default into the
  1201. 'output gain' field, and not the comment tag.
  1202. </t>
  1203. <t>
  1204. To avoid confusion with multiple normalization schemes, an Opus comment header
  1205. SHOULD NOT contain any of the REPLAYGAIN_TRACK_GAIN, REPLAYGAIN_TRACK_PEAK,
  1206. REPLAYGAIN_ALBUM_GAIN, or REPLAYGAIN_ALBUM_PEAK tags, unless they are only
  1207. to be used in some context where there is guaranteed to be no such confusion.
  1208. <xref target="EBU-R128"/> normalization is preferred to the earlier
  1209. REPLAYGAIN schemes because of its clear definition and adoption by industry.
  1210. Peak normalizations are difficult to calculate reliably for lossy codecs
  1211. because of variation in excursion heights due to decoder differences.
  1212. In the authors' investigations they were not applied consistently or broadly
  1213. enough to merit inclusion here.
  1214. </t>
  1215. </section> <!-- end comment_format -->
  1216. </section> <!-- end comment_header -->
  1217. </section> <!-- end headers -->
  1218. <section anchor="packet_size_limits" title="Packet Size Limits">
  1219. <t>
  1220. Technically, valid Opus packets can be arbitrarily large due to the padding
  1221. format, although the amount of non-padding data they can contain is bounded.
  1222. These packets might be spread over a similarly enormous number of Ogg pages.
  1223. When encoding, implementations SHOULD limit the use of padding in audio data
  1224. packets to no more than is necessary to make a variable bitrate (VBR) stream
  1225. constant bitrate (CBR), unless they have no reasonable way to determine what
  1226. is necessary.
  1227. Demuxers SHOULD reject audio data packets (treat them as if they were malformed
  1228. Opus packets with an invalid TOC sequence) larger than 61,440 octets per
  1229. Opus stream, unless they have a specific reason for allowing extra padding.
  1230. Such packets necessarily contain more padding than needed to make a stream CBR.
  1231. Demuxers MUST avoid attempting to allocate excessive amounts of memory when
  1232. presented with a very large packet.
  1233. Demuxers MAY reject or partially process audio data packets larger than
  1234. 61,440&nbsp;octets in an Ogg Opus stream with channel mapping families&nbsp;0
  1235. or&nbsp;1.
  1236. Demuxers MAY reject or partially process audio data packets in any Ogg Opus
  1237. stream if the packet is larger than 61,440&nbsp;octets and also larger than
  1238. 7,680&nbsp;octets per Opus stream.
  1239. The presence of an extremely large packet in the stream could indicate a
  1240. memory exhaustion attack or stream corruption.
  1241. </t>
  1242. <t>
  1243. In an Ogg Opus stream, the largest possible valid packet that does not use
  1244. padding has a size of (61,298*N&nbsp;-&nbsp;2) octets.
  1245. With 255&nbsp;streams, this is 15,630,988&nbsp;octets and can
  1246. span up to 61,298&nbsp;Ogg pages, all but one of which will have a granule
  1247. position of -1.
  1248. This is of course a very extreme packet, consisting of 255&nbsp;streams, each
  1249. containing 120&nbsp;ms of audio encoded as 2.5&nbsp;ms frames, each frame
  1250. using the maximum possible number of octets (1275) and stored in the least
  1251. efficient manner allowed (a VBR code&nbsp;3 Opus packet).
  1252. Even in such a packet, most of the data will be zeros as 2.5&nbsp;ms frames
  1253. cannot actually use all 1275&nbsp;octets.
  1254. </t>
  1255. <t>
  1256. The largest packet consisting of entirely useful data is
  1257. (15,326*N&nbsp;-&nbsp;2) octets.
  1258. This corresponds to 120&nbsp;ms of audio encoded as 10&nbsp;ms frames in either
  1259. SILK or Hybrid mode, but at a data rate of over 1&nbsp;Mbps, which makes little
  1260. sense for the quality achieved.
  1261. </t>
  1262. <t>
  1263. A more reasonable limit is (7,664*N&nbsp;-&nbsp;2) octets.
  1264. This corresponds to 120&nbsp;ms of audio encoded as 20&nbsp;ms stereo CELT mode
  1265. frames, with a total bitrate just under 511&nbsp;kbps (not counting the Ogg
  1266. encapsulation overhead).
  1267. For channel mapping family 1, N=8 provides a reasonable upper bound, as it
  1268. allows for each of the 8 possible output channels to be decoded from a
  1269. separate stereo Opus stream.
  1270. This gives a size of 61,310&nbsp;octets, which is rounded up to a multiple of
  1271. 1,024&nbsp;octets to yield the audio data packet size of 61,440&nbsp;octets
  1272. that any implementation is expected to be able to process successfully.
  1273. </t>
  1274. </section>
  1275. <section anchor="encoder" title="Encoder Guidelines">
  1276. <t>
  1277. When encoding Opus streams, Ogg muxers SHOULD take into account the
  1278. algorithmic delay of the Opus encoder.
  1279. </t>
  1280. <t>
  1281. In encoders derived from the reference
  1282. implementation&nbsp;<xref target="RFC6716"/>, the number of samples can be
  1283. queried with:
  1284. </t>
  1285. <figure align="center">
  1286. <artwork align="center"><![CDATA[
  1287. opus_encoder_ctl(encoder_state, OPUS_GET_LOOKAHEAD(&delay_samples));
  1288. ]]></artwork>
  1289. </figure>
  1290. <t>
  1291. To achieve good quality in the very first samples of a stream, implementations
  1292. MAY use linear predictive coding (LPC) extrapolation to generate at least 120
  1293. extra samples at the beginning to avoid the Opus encoder having to encode a
  1294. discontinuous signal.
  1295. For more information on linear prediction, see
  1296. <xref target="linear-prediction"/>.
  1297. For an input file containing 'length' samples, the implementation SHOULD set
  1298. the pre-skip header value to (delay_samples&nbsp;+&nbsp;extra_samples), encode
  1299. at least (length&nbsp;+&nbsp;delay_samples&nbsp;+&nbsp;extra_samples)
  1300. samples, and set the granule position of the last page to
  1301. (length&nbsp;+&nbsp;delay_samples&nbsp;+&nbsp;extra_samples).
  1302. This ensures that the encoded file has the same duration as the original, with
  1303. no time offset. The best way to pad the end of the stream is to also use LPC
  1304. extrapolation, but zero-padding is also acceptable.
  1305. </t>
  1306. <section anchor="lpc" title="LPC Extrapolation">
  1307. <t>
  1308. The first step in LPC extrapolation is to compute linear prediction
  1309. coefficients. <xref target="lpc-sample"/>
  1310. When extending the end of the signal, order-N (typically with N ranging from 8
  1311. to 40) LPC analysis is performed on a window near the end of the signal.
  1312. The last N samples are used as memory to an infinite impulse response (IIR)
  1313. filter.
  1314. </t>
  1315. <t>
  1316. The filter is then applied on a zero input to extrapolate the end of the signal.
  1317. Let a(k) be the kth LPC coefficient and x(n) be the nth sample of the signal,
  1318. each new sample past the end of the signal is computed as:
  1319. </t>
  1320. <figure align="center">
  1321. <artwork align="center"><![CDATA[
  1322. N
  1323. ---
  1324. x(n) = \ a(k)*x(n-k)
  1325. /
  1326. ---
  1327. k=1
  1328. ]]></artwork>
  1329. </figure>
  1330. <t>
  1331. The process is repeated independently for each channel.
  1332. It is possible to extend the beginning of the signal by applying the same
  1333. process backward in time.
  1334. When extending the beginning of the signal, it is best to apply a "fade in" to
  1335. the extrapolated signal, e.g. by multiplying it by a half-Hanning window
  1336. <xref target="hanning"/>.
  1337. </t>
  1338. </section>
  1339. <section anchor="continuous_chaining" title="Continuous Chaining">
  1340. <t>
  1341. In some applications, such as Internet radio, it is desirable to cut a long
  1342. stream into smaller chains, e.g. so the comment header can be updated.
  1343. This can be done simply by separating the input streams into segments and
  1344. encoding each segment independently.
  1345. The drawback of this approach is that it creates a small discontinuity
  1346. at the boundary due to the lossy nature of Opus.
  1347. A muxer MAY avoid this discontinuity by using the following procedure:
  1348. <list style="numbers">
  1349. <t>Encode the last frame of the first segment as an independent frame by
  1350. turning off all forms of inter-frame prediction.
  1351. De-emphasis is allowed.</t>
  1352. <t>Set the granule position of the last page to a point near the end of the
  1353. last frame.</t>
  1354. <t>Begin the second segment with a copy of the last frame of the first
  1355. segment.</t>
  1356. <t>Set the pre-skip value of the second stream in such a way as to properly
  1357. join the two streams.</t>
  1358. <t>Continue the encoding process normally from there, without any reset to
  1359. the encoder.</t>
  1360. </list>
  1361. </t>
  1362. <t>
  1363. In encoders derived from the reference implementation, inter-frame prediction
  1364. can be turned off by calling:
  1365. </t>
  1366. <figure align="center">
  1367. <artwork align="center"><![CDATA[
  1368. opus_encoder_ctl(encoder_state, OPUS_SET_PREDICTION_DISABLED(1));
  1369. ]]></artwork>
  1370. </figure>
  1371. <t>
  1372. For best results, this implementation requires that prediction be explicitly
  1373. enabled again before resuming normal encoding, even after a reset.
  1374. </t>
  1375. </section>
  1376. </section>
  1377. <section anchor="implementation" title="Implementation Status">
  1378. <t>
  1379. A brief summary of major implementations of this draft is available
  1380. at <eref target="https://wiki.xiph.org/OggOpusImplementation"/>,
  1381. along with their status.
  1382. </t>
  1383. <t>
  1384. [Note to RFC Editor: please remove this entire section before
  1385. final publication per <xref target="RFC6982"/>, along with
  1386. its references.]
  1387. </t>
  1388. </section>
  1389. <section anchor="security" title="Security Considerations">
  1390. <t>
  1391. Implementations of the Opus codec need to take appropriate security
  1392. considerations into account, as outlined in <xref target="RFC4732"/>.
  1393. This is just as much a problem for the container as it is for the codec itself.
  1394. Robustness against malicious payloads is extremely important.
  1395. Malicious payloads MUST NOT cause an implementation to overrun its allocated
  1396. memory or to take an excessive amount of resources to decode.
  1397. Although problems in encoding applications are typically rarer, the same
  1398. applies to the muxer.
  1399. Malicious audio input streams MUST NOT cause an implementation to overrun its
  1400. allocated memory or consume excessive resources because this would allow an
  1401. attacker to attack transcoding gateways.
  1402. </t>
  1403. <t>
  1404. Like most other container formats, Ogg Opus streams SHOULD NOT be used with
  1405. insecure ciphers or cipher modes that are vulnerable to known-plaintext
  1406. attacks.
  1407. Elements such as the Ogg page capture pattern and the magic signatures in the
  1408. ID header and the comment header all have easily predictable values, in
  1409. addition to various elements of the codec data itself.
  1410. </t>
  1411. </section>
  1412. <section anchor="content_type" title="Content Type">
  1413. <t>
  1414. An "Ogg Opus file" consists of one or more sequentially multiplexed segments,
  1415. each containing exactly one Ogg Opus stream.
  1416. The RECOMMENDED mime-type for Ogg Opus files is "audio/ogg".
  1417. </t>
  1418. <t>
  1419. If more specificity is desired, one MAY indicate the presence of Opus streams
  1420. using the codecs parameter defined in <xref target="RFC6381"/> and
  1421. <xref target="RFC5334"/>, e.g.,
  1422. </t>
  1423. <figure>
  1424. <artwork align="center"><![CDATA[
  1425. audio/ogg; codecs=opus
  1426. ]]></artwork>
  1427. </figure>
  1428. <t>
  1429. for an Ogg Opus file.
  1430. </t>
  1431. <t>
  1432. The RECOMMENDED filename extension for Ogg Opus files is '.opus'.
  1433. </t>
  1434. <t>
  1435. When Opus is concurrently multiplexed with other streams in an Ogg container,
  1436. one SHOULD use one of the "audio/ogg", "video/ogg", or "application/ogg"
  1437. mime-types, as defined in <xref target="RFC5334"/>.
  1438. Such streams are not strictly "Ogg Opus files" as described above,
  1439. since they contain more than a single Opus stream per sequentially
  1440. multiplexed segment, e.g. video or multiple audio tracks.
  1441. In such cases the the '.opus' filename extension is NOT RECOMMENDED.
  1442. </t>
  1443. <t>
  1444. In either case, this document updates <xref target="RFC5334"/>
  1445. to add 'opus' as a codecs parameter value with char[8]: 'OpusHead'
  1446. as Codec Identifier.
  1447. </t>
  1448. </section>
  1449. <section anchor="iana" title="IANA Considerations">
  1450. <t>
  1451. This document updates the IANA Media Types registry to add .opus
  1452. as a file extension for "audio/ogg", and to add itself as a reference
  1453. alongside <xref target="RFC5334"/> for "audio/ogg", "video/ogg", and
  1454. "application/ogg" Media Types.
  1455. </t>
  1456. <t>
  1457. This document defines a new registry "Opus Channel Mapping Families" to
  1458. indicate how the semantic meanings of the channels in a multi-channel Opus
  1459. stream are described.
  1460. IANA SHALL create a new name space of "Opus Channel Mapping Families".
  1461. All maintenance within and additions to the contents of this name space MUST be
  1462. according to the "Specification Requried with Expert Review" registration
  1463. policy as defined in <xref target="RFC5226"/>.
  1464. Each registry entry consists of a Channel Mapping Family Number, which is
  1465. specified in decimal in the range 0 to 255, inclusive, and a Reference (or
  1466. list of references)
  1467. Each Reference must point to sufficient documentation to describe what
  1468. information is coded in the Opus identification header for this channel
  1469. mapping family, how a demuxer determines the Stream Count ('N') and Coupled
  1470. Stream Count ('M') from this information, and how it determines the proper
  1471. interpretation of each of the decoded channels.
  1472. </t>
  1473. <t>
  1474. This document defines three initial assignments for this registry.
  1475. </t>
  1476. <texttable>
  1477. <ttcol>Value</ttcol><ttcol>Reference</ttcol>
  1478. <c>0</c><c>[RFCXXXX] <xref target="channel_mapping_0"/></c>
  1479. <c>1</c><c>[RFCXXXX] <xref target="channel_mapping_1"/></c>
  1480. <c>255</c><c>[RFCXXXX] <xref target="channel_mapping_255"/></c>
  1481. </texttable>
  1482. <t>
  1483. The designated expert will determine if the Reference points to a specification
  1484. that meets the requirements for permanence and ready availability laid out
  1485. in&nbsp;<xref target="RFC5226"/> and that it specifies the information
  1486. described above with sufficient clarity to allow interoperable
  1487. implementations.
  1488. </t>
  1489. </section>
  1490. <section anchor="Acknowledgments" title="Acknowledgments">
  1491. <t>
  1492. Thanks to Ben Campbell, Mark Harris, Greg Maxwell, Christopher "Monty"
  1493. Montgomery, Jean-Marc Valin, and Mo Zanaty for their valuable contributions to
  1494. this document.
  1495. Additional thanks to Andrew D'Addesio, Greg Maxwell, and Vincent Penquerc'h for
  1496. their feedback based on early implementations.
  1497. </t>
  1498. </section>
  1499. <section title="RFC Editor Notes">
  1500. <t>
  1501. In&nbsp;<xref target="iana"/>, "RFCXXXX" is to be replaced with the RFC number
  1502. assigned to this draft.
  1503. </t>
  1504. <t>
  1505. In the Copyright Notice at the start of the document, the following paragraph
  1506. is to be appended after the regular copyright notice text:
  1507. </t>
  1508. <t>
  1509. "The licenses granted by the IETF Trust to this RFC under Section&nbsp;3.c of
  1510. the Trust Legal Provisions shall also include the right to extract text from
  1511. Sections&nbsp;1 through&nbsp;14 of this RFC and create derivative works from
  1512. these extracts, and to copy, publish, display, and distribute such derivative
  1513. works in any medium and for any purpose, provided that no such derivative work
  1514. shall be presented, displayed, or published in a manner that states or implies
  1515. that it is part of this RFC or any other IETF Document."
  1516. </t>
  1517. </section>
  1518. </middle>
  1519. <back>
  1520. <references title="Normative References">
  1521. &rfc2119;
  1522. &rfc3533;
  1523. &rfc3629;
  1524. &rfc4732;
  1525. &rfc5226;
  1526. &rfc5334;
  1527. &rfc6381;
  1528. &rfc6716;
  1529. <reference anchor="EBU-R128" target="https://tech.ebu.ch/loudness">
  1530. <front>
  1531. <title>Loudness Recommendation EBU R128</title>
  1532. <author>
  1533. <organization>EBU Technical Committee</organization>
  1534. </author>
  1535. <date month="August" year="2011"/>
  1536. </front>
  1537. </reference>
  1538. <reference anchor="vorbis-comment"
  1539. target="https://www.xiph.org/vorbis/doc/v-comment.html">
  1540. <front>
  1541. <title>Ogg Vorbis I Format Specification: Comment Field and Header
  1542. Specification</title>
  1543. <author initials="C." surname="Montgomery"
  1544. fullname="Christopher &quot;Monty&quot; Montgomery"/>
  1545. <date month="July" year="2002"/>
  1546. </front>
  1547. </reference>
  1548. </references>
  1549. <references title="Informative References">
  1550. <!--?rfc include="http://xml.resource.org/public/rfc/bibxml/reference.RFC.3550.xml"?-->
  1551. &rfc6982;
  1552. &rfc7587;
  1553. <reference anchor="flac"
  1554. target="https://xiph.org/flac/format.html">
  1555. <front>
  1556. <title>FLAC - Free Lossless Audio Codec Format Description</title>
  1557. <author initials="J." surname="Coalson" fullname="Josh Coalson"/>
  1558. <date month="January" year="2008"/>
  1559. </front>
  1560. </reference>
  1561. <reference anchor="hanning"
  1562. target="https://en.wikipedia.org/wiki/Hamming_function#Hann_.28Hanning.29_window">
  1563. <front>
  1564. <title>Hann window</title>
  1565. <author>
  1566. <organization>Wikipedia</organization>
  1567. </author>
  1568. <date month="May" year="2013"/>
  1569. </front>
  1570. </reference>
  1571. <reference anchor="linear-prediction"
  1572. target="https://en.wikipedia.org/wiki/Linear_predictive_coding">
  1573. <front>
  1574. <title>Linear Predictive Coding</title>
  1575. <author>
  1576. <organization>Wikipedia</organization>
  1577. </author>
  1578. <date month="January" year="2014"/>
  1579. </front>
  1580. </reference>
  1581. <reference anchor="lpc-sample"
  1582. target="https://svn.xiph.org/trunk/vorbis/lib/lpc.c">
  1583. <front>
  1584. <title>Autocorrelation LPC coeff generation algorithm
  1585. (Vorbis source code)</title>
  1586. <author initials="J." surname="Degener" fullname="Jutta Degener"/>
  1587. <author initials="C." surname="Bormann" fullname="Carsten Bormann"/>
  1588. <date month="November" year="1994"/>
  1589. </front>
  1590. </reference>
  1591. <reference anchor="replay-gain"
  1592. target="https://wiki.xiph.org/VorbisComment#Replay_Gain">
  1593. <front>
  1594. <title>VorbisComment: Replay Gain</title>
  1595. <author initials="C." surname="Parker" fullname="Conrad Parker"/>
  1596. <author initials="M." surname="Leese" fullname="Martin Leese"/>
  1597. <date month="June" year="2009"/>
  1598. </front>
  1599. </reference>
  1600. <reference anchor="seeking"
  1601. target="https://wiki.xiph.org/Seeking">
  1602. <front>
  1603. <title>Granulepos Encoding and How Seeking Really Works</title>
  1604. <author initials="S." surname="Pfeiffer" fullname="Silvia Pfeiffer"/>
  1605. <author initials="C." surname="Parker" fullname="Conrad Parker"/>
  1606. <author initials="G." surname="Maxwell" fullname="Greg Maxwell"/>
  1607. <date month="May" year="2012"/>
  1608. </front>
  1609. </reference>
  1610. <reference anchor="vorbis-mapping"
  1611. target="https://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-810004.3.9">
  1612. <front>
  1613. <title>The Vorbis I Specification, Section 4.3.9 Output Channel Order</title>
  1614. <author initials="C." surname="Montgomery"
  1615. fullname="Christopher &quot;Monty&quot; Montgomery"/>
  1616. <date month="January" year="2010"/>
  1617. </front>
  1618. </reference>
  1619. <reference anchor="vorbis-trim"
  1620. target="https://xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-132000A.2">
  1621. <front>
  1622. <title>The Vorbis I Specification, Appendix&nbsp;A: Embedding Vorbis
  1623. into an Ogg stream</title>
  1624. <author initials="C." surname="Montgomery"
  1625. fullname="Christopher &quot;Monty&quot; Montgomery"/>
  1626. <date month="November" year="2008"/>
  1627. </front>
  1628. </reference>
  1629. <reference anchor="wave-multichannel"
  1630. target="http://msdn.microsoft.com/en-us/windows/hardware/gg463006.aspx">
  1631. <front>
  1632. <title>Multiple Channel Audio Data and WAVE Files</title>
  1633. <author>
  1634. <organization>Microsoft Corporation</organization>
  1635. </author>
  1636. <date month="March" year="2007"/>
  1637. </front>
  1638. </reference>
  1639. </references>
  1640. </back>
  1641. </rfc>