draft-ietf-codec-oggopus.xml 77 KB

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