1 <?xml version="1.0" encoding="utf-8"?>
2 <!DOCTYPE rfc SYSTEM 'rfc2629.dtd' [
3 <!ENTITY rfc2119 PUBLIC '' 'https://xml2rfc.tools.ietf.org/tools/xml2rfc/public/rfc/bibxml/reference.RFC.2119.xml'>
4 <!ENTITY rfc3533 PUBLIC '' 'https://xml2rfc.tools.ietf.org/tools/xml2rfc/public/rfc/bibxml/reference.RFC.3533.xml'>
5 <!ENTITY rfc3629 PUBLIC '' 'https://xml2rfc.tools.ietf.org/tools/xml2rfc/public/rfc/bibxml/reference.RFC.3629.xml'>
6 <!ENTITY rfc4732 PUBLIC '' 'https://xml2rfc.tools.ietf.org/tools/xml2rfc/public/rfc/bibxml/reference.RFC.4732.xml'>
7 <!ENTITY rfc5334 PUBLIC '' 'https://xml2rfc.tools.ietf.org/tools/xml2rfc/public/rfc/bibxml/reference.RFC.5334.xml'>
8 <!ENTITY rfc6381 PUBLIC '' 'https://xml2rfc.tools.ietf.org/tools/xml2rfc/public/rfc/bibxml/reference.RFC.6381.xml'>
9 <!ENTITY rfc6716 PUBLIC '' 'https://xml2rfc.tools.ietf.org/tools/xml2rfc/public/rfc/bibxml/reference.RFC.6716.xml'>
11 <?rfc toc="yes" symrefs="yes" ?>
13 <rfc ipr="trust200902" category="std" docName="draft-ietf-codec-oggopus-02">
16 <title abbrev="Ogg Opus">Ogg Encapsulation for the Opus Audio Codec</title>
17 <author initials="T.B." surname="Terriberry" fullname="Timothy B. Terriberry">
18 <organization>Mozilla Corporation</organization>
21 <street>650 Castro Street</street>
22 <city>Mountain View</city>
25 <country>USA</country>
27 <phone>+1 650 903-0800</phone>
28 <email>tterribe@xiph.org</email>
32 <author initials="R." surname="Lee" fullname="Ron Lee">
33 <organization>Voicetronix</organization>
36 <street>246 Pulteney Street, Level 1</street>
40 <country>Australia</country>
42 <phone>+61 8 8232 9112</phone>
43 <email>ron@debian.org</email>
47 <author initials="R." surname="Giles" fullname="Ralph Giles">
48 <organization>Mozilla Corporation</organization>
51 <street>163 West Hastings Street</street>
52 <city>Vancouver</city>
55 <country>Canada</country>
57 <phone>+1 778 785 1540</phone>
58 <email>giles@xiph.org</email>
62 <date day="17" month="January" year="2014"/>
64 <workgroup>codec</workgroup>
68 This document defines the Ogg encapsulation for the Opus interactive speech and
70 This allows data encoded in the Opus format to be stored in an Ogg logical
72 Ogg encapsulation provides Opus with a long-term storage format supporting
73 all of the essential features, including metadata, fast and accurate seeking,
74 corruption detection, recapture after errors, low overhead, and the ability to
75 multiplex Opus with other codecs (including video) with minimal buffering.
76 It also provides a live streamable format, capable of delivery over a reliable
77 stream-oriented transport, without requiring all the data, or even the total
78 length of the data, up-front, in a form that is identical to the on-disk
85 <section anchor="intro" title="Introduction">
87 The IETF Opus codec is a low-latency audio codec optimized for both voice and
88 general-purpose audio.
89 See <xref target="RFC6716"/> for technical details.
90 This document defines the encapsulation of Opus in a continuous, logical Ogg
91 bitstream <xref target="RFC3533"/>.
94 Ogg bitstreams are made up of a series of 'pages', each of which contains data
95 from one or more 'packets'.
96 Pages are the fundamental unit of multiplexing in an Ogg stream.
97 Each page is associated with a particular logical stream and contains a capture
98 pattern and checksum, flags to mark the beginning and end of the logical
99 stream, and a 'granule position' that represents an absolute position in the
100 stream, to aid seeking.
101 A single page can contain up to 65,025 octets of packet data from up to 255
103 Packets may be split arbitrarily across pages, and continued from one page to
104 the next (allowing packets much larger than would fit on a single page).
105 Each page contains 'lacing values' that indicate how the data is partitioned
106 into packets, allowing a demuxer to recover the packet boundaries without
107 examining the encoded data.
108 A packet is said to 'complete' on a page when the page contains the final
109 lacing value corresponding to that packet.
112 This encapsulation defines the required contents of the packet data, including
113 the necessary headers, the organization of those packets into a logical
114 stream, and the interpretation of the codec-specific granule position field.
115 It does not attempt to describe or specify the existing Ogg container format.
116 Readers unfamiliar with the basic concepts mentioned above are encouraged to
117 review the details in <xref target="RFC3533"/>.
122 <section anchor="terminology" title="Terminology">
124 The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD",
125 "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be
126 interpreted as described in <xref target="RFC2119"/>.
130 Implementations that fail to satisfy one or more "MUST" requirements are
131 considered non-compliant.
132 Implementations that satisfy all "MUST" requirements, but fail to satisfy one
133 or more "SHOULD" requirements are said to be "conditionally compliant".
134 All other implementations are "unconditionally compliant".
139 <section anchor="packet_organization" title="Packet Organization">
141 An Opus stream is organized as follows.
144 There are two mandatory header packets.
145 The granule position of the pages on which these packets complete MUST be zero.
148 The first packet in the logical Ogg bitstream MUST contain the identification
149 (ID) header, which uniquely identifies a stream as Opus audio.
150 The format of this header is defined in <xref target="id_header"/>.
151 It MUST be placed alone (without any other packet data) on the first page of
152 the logical Ogg bitstream, and must complete on that page.
153 This page MUST have its 'beginning of stream' flag set.
156 The second packet in the logical Ogg bitstream MUST contain the comment header,
157 which contains user-supplied metadata.
158 The format of this header is defined in <xref target="comment_header"/>.
159 It MAY span one or more pages, beginning on the second page of the logical
161 However many pages it spans, the comment header packet MUST finish the page on
165 All subsequent pages are audio data pages, and the Ogg packets they contain are
167 Each audio data packet contains one Opus packet for each of N different
168 streams, where N is typically one for mono or stereo, but may be greater than
169 one for, e.g., multichannel audio.
170 The value N is specified in the ID header (see
171 <xref target="channel_mapping"/>), and is fixed over the entire length of the
172 logical Ogg bitstream.
175 The first N-1 Opus packets, if any, are packed one after another into the Ogg
176 packet, using the self-delimiting framing from Appendix B of
177 <xref target="RFC6716"/>.
178 The remaining Opus packet is packed at the end of the Ogg packet using the
179 regular, undelimited framing from Section 3 of <xref target="RFC6716"/>.
180 All of the Opus packets in a single Ogg packet MUST be constrained to have the
182 The duration and coding modes of each Opus packet are contained in the
183 TOC (table of contents) sequence in the first few bytes.
184 A decoder SHOULD treat any Opus packet whose duration is different from that of
185 the first Opus packet in an Ogg packet as if it were an Opus packet with an
186 illegal TOC sequence.
189 The first audio data page SHOULD NOT have the 'continued packet' flag set
190 (which would indicate the first audio data packet is continued from a previous
192 Packets MUST be placed into Ogg pages in order until the end of stream.
193 Audio packets MAY span page boundaries.
194 A decoder MUST treat a zero-octet audio data packet as if it were an Opus
195 packet with an illegal TOC sequence.
196 The last page SHOULD have the 'end of stream' flag set, but implementations
197 should be prepared to deal with truncated streams that do not have a page
198 marked 'end of stream'.
199 The final packet on the last page SHOULD NOT be a continued packet, i.e., the
200 final lacing value should be less than 255.
201 There MUST NOT be any more pages in an Opus logical bitstream after a page
202 marked 'end of stream'.
206 <section anchor="granpos" title="Granule Position">
208 The granule position of an audio data page encodes the total number of PCM
209 samples in the stream up to and including the last fully-decodable sample from
210 the last packet completed on that page.
211 A page that is entirely spanned by a single packet (that completes on a
212 subsequent page) has no granule position, and the granule position field MUST
213 be set to the special value '-1' in two's complement.
217 The granule position of an audio data page is in units of PCM audio samples at
218 a fixed rate of 48 kHz (per channel; a stereo stream's granule position
219 does not increment at twice the speed of a mono stream).
220 It is possible to run an Opus decoder at other sampling rates, but the value
221 in the granule position field always counts samples assuming a 48 kHz
222 decoding rate, and the rest of this specification makes the same assumption.
226 The duration of an Opus packet may be any multiple of 2.5 ms, up to a
227 maximum of 120 ms.
228 This duration is encoded in the TOC sequence at the beginning of each packet.
229 The number of samples returned by a decoder corresponds to this duration
230 exactly, even for the first few packets.
231 For example, a 20 ms packet fed to a decoder running at 48 kHz will
232 always return 960 samples.
233 A demuxer can parse the TOC sequence at the beginning of each Ogg packet to
234 work backwards or forwards from a packet with a known granule position (i.e.,
235 the last packet completed on some page) in order to assign granule positions
236 to every packet, or even every individual sample.
237 The one exception is the last page in the stream, as described below.
241 All other pages with completed packets after the first MUST have a granule
242 position equal to the number of samples contained in packets that complete on
243 that page plus the granule position of the most recent page with completed
245 This guarantees that a demuxer can assign individual packets the same granule
246 position when working forwards as when working backwards.
247 For this to work, there cannot be any gaps.
250 <section anchor="gap-repair" title="Repairing Gaps in Real-time Streams">
252 In order to support capturing a real-time stream that has lost or not
253 transmitted packets, a muxer SHOULD emit packets that explicitly request the
254 use of Packet Loss Concealment (PLC) in place of the missing packets.
255 Only gaps that are a multiple of 2.5 ms are repairable, as these are the
256 only durations that can be created by packet loss or discontinuous
258 Muxers need not handle other gap sizes.
259 Creating the necessary packets involves synthesizing a TOC byte (defined in
260 Section 3.1 of <xref target="RFC6716"/>)—and whatever
261 additional internal framing is needed—to indicate the packet duration
263 The actual length of each missing Opus frame inside the packet is zero bytes,
264 as defined in Section 3.2.1 of <xref target="RFC6716"/>.
268 <xref target="RFC6716"/> does not impose any requirements on the PLC, but this
269 section outlines choices that are expected to have a positive influence on
270 most PLC implementations, including the reference implementation.
271 Synthesized TOC bytes SHOULD maintain the same mode, audio bandwidth,
272 channel count, and frame size as the previous packet (if any).
273 This is the simplest and usually the most well-tested case for the PLC to
274 handle and it covers all losses that do not include a configuration switch,
275 as defined in Section 4.5 of <xref target="RFC6716"/>.
279 When a previous packet is available, keeping the audio bandwidth and channel
280 count the same allows the PLC to provide maximum continuity in the concealment
282 However, if the size of the gap is not a multiple of the most recent frame
283 size, then the frame size will have to change for at least some frames.
284 Such changes SHOULD be delayed as long as possible to simplify
285 things for PLC implementations.
289 As an example, a 95 ms gap could be encoded as nineteen 5 ms frames
290 in two bytes with a single CBR code 3 packet.
291 If the previous frame size was 20 ms, using four 20 ms frames
292 followed by three 5 ms frames requires 4 bytes (plus an extra byte
293 of Ogg lacing overhead), but allows the PLC to use its well-tested steady
294 state behavior for as long as possible.
295 The total bitrate of the latter approach, including Ogg overhead, is about
296 0.4 kbps, so the impact on file size is minimal.
300 Changing modes is discouraged, since this causes some decoder implementations
301 to reset their PLC state.
302 However, SILK and Hybrid mode frames cannot fill gaps that are not a multiple
304 If switching to CELT mode is needed to match the gap size, a muxer SHOULD do
305 so at the end of the gap to allow the PLC to function for as long as possible.
309 In the example above, if the previous frame was a 20 ms SILK mode frame,
310 the better solution is to synthesize a packet describing four 20 ms SILK
311 frames, followed by a packet with a single 10 ms SILK
312 frame, and finally a packet with a 5 ms CELT frame, to fill the 95 ms
314 This also requires four bytes to describe the synthesized packet data (two
315 bytes for a CBR code 3 and one byte each for two code 0 packets) but three
316 bytes of Ogg lacing overhead are required to mark the packet boundaries.
317 At 0.6 kbps, this is still a minimal bitrate impact over a naive, low quality
322 Since medium-band audio is only supported in the SILK modes, wideband frames
323 SHOULD be generated if switching from it to CELT modes, to ensure that
324 any PLC implementation that does try to migrate state between the modes
325 will be able to preserve all of the available audio bandwidth.
329 Matching synthetic TOC byte(s) MAY be packed into packets using any of
330 codes 0, 1, 2, or 3.
331 If the TOC configuration matches, the muxer MAY further combine the empty
332 frames with previous or subsequent non-zero-length frames (using code 2
337 <section anchor="preskip" title="Pre-skip">
339 There is some amount of latency introduced during the decoding process, to
340 allow for overlap in the CELT modes, stereo mixing in the SILK modes, and
341 resampling, and the encoder will introduce even more latency (though the exact
342 amount is not specified).
343 Therefore, the first few samples produced by the decoder do not correspond to
344 real input audio, but are instead composed of padding inserted by the encoder
345 to compensate for this latency.
346 These samples need to be stored and decoded, as Opus is an asymptotically
347 convergent predictive codec, meaning the decoded contents of each frame depend
348 on the recent history of decoder inputs.
349 However, a decoder will want to skip these samples after decoding them.
353 A 'pre-skip' field in the ID header (see <xref target="id_header"/>) signals
354 the number of samples which SHOULD be skipped (decoded but discarded) at the
355 beginning of the stream.
356 This provides sufficient history to the decoder so that it has already
357 converged before the stream's output begins.
358 It may also be used to perform sample-accurate cropping of existing encoded
360 This amount need not be a multiple of 2.5 ms, may be smaller than a single
361 packet, or may span the contents of several packets.
365 <section anchor="pcm_sample_position" title="PCM Sample Position">
367 The PCM sample position is determined from the granule position using the
369 <figure align="center">
370 <artwork align="center"><![CDATA[
371 'PCM sample position' = 'granule position' - 'pre-skip' .
377 For example, if the granule position of the first audio data page is 59,971,
378 and the pre-skip is 11,971, then the PCM sample position of the last decoded
379 sample from that page is 48,000.
380 This can be converted into a playback time using the formula
381 <figure align="center">
382 <artwork align="center"><![CDATA[
383 'PCM sample position'
384 'playback time' = --------------------- .
391 The initial PCM sample position before any samples are played is normally '0'.
392 In this case, the PCM sample position of the first audio sample to be played
393 starts at '1', because it marks the time on the clock
394 <spanx style="emph">after</spanx> that sample has been played, and a stream
395 that is exactly one second long has a final PCM sample position of '48000',
396 as in the example here.
400 Vorbis streams use a granule position smaller than the number of audio samples
401 contained in the first audio data page to indicate that some of those samples
402 must be trimmed from the output (see <xref target="vorbis-trim"/>).
403 However, to do so, Vorbis requires that the first audio data page contains
404 exactly two packets, in order to allow the decoder to perform PCM position
405 adjustments before needing to return any PCM data.
406 Opus uses the pre-skip mechanism for this purpose instead, since the encoder
407 may introduce more than a single packet's worth of latency, and since very
408 large packets in streams with a very large number of channels might not fit
413 <section anchor="end_trimming" title="End Trimming">
415 The page with the 'end of stream' flag set MAY have a granule position that
416 indicates the page contains less audio data than would normally be returned by
417 decoding up through the final packet.
418 This is used to end the stream somewhere other than an even frame boundary.
419 The granule position of the most recent audio data page with completed packets
420 is used to make this determination, or '0' is used if there were no previous
421 audio data pages with a completed packet.
422 The difference between these granule positions indicates how many samples to
423 keep after decoding the packets that completed on the final page.
424 The remaining samples are discarded.
425 The number of discarded samples SHOULD be no larger than the number decoded
426 from the last packet.
430 <section anchor="start_granpos_restrictions"
431 title="Restrictions on the Initial Granule Position">
433 The granule position of the first audio data page with a completed packet MAY
434 be larger than the number of samples contained in packets that complete on
435 that page, however it MUST NOT be smaller, unless that page has the 'end of
437 Allowing a granule position larger than the number of samples allows the
438 beginning of a stream to be cropped or a live stream to be joined without
439 rewriting the granule position of all the remaining pages.
440 This means that the PCM sample position just before the first sample to be
441 played may be larger than '0'.
442 Synchronization when multiplexing with other logical streams still uses the PCM
443 sample position relative to '0' to compute sample times.
444 This does not affect the behavior of pre-skip: exactly 'pre-skip' samples
445 should be skipped from the beginning of the decoded output, even if the
446 initial PCM sample position is greater than zero.
450 On the other hand, a granule position that is smaller than the number of
451 decoded samples prevents a demuxer from working backwards to assign each
452 packet or each individual sample a valid granule position, since granule
453 positions must be non-negative.
454 A decoder MUST reject as invalid any stream where the granule position is
455 smaller than the number of samples contained in packets that complete on the
456 first audio data page with a completed packet, unless that page has the 'end
458 It MAY defer this action until it decodes the last packet completed on that
463 If that page has the 'end of stream' flag set, a demuxer MUST reject as invalid
464 any stream where its granule position is smaller than the 'pre-skip' amount.
465 This would indicate that more samples should be skipped from the initial
466 decoded output than exist in the stream.
467 If the granule position is smaller than the number of decoded samples produced
468 by the packets that complete on that page, then a demuxer MUST use an initial
469 granule position of '0', and can work forwards from '0' to timestamp
471 If the granule position is larger than the number of decoded samples available,
472 then the demuxer MUST still work backwards as described above, even if the
473 'end of stream' flag is set, to determine the initial granule position, and
474 thus the initial PCM sample position.
475 Both of these will be greater than '0' in this case.
479 <section anchor="seeking_and_preroll" title="Seeking and Pre-roll">
481 Seeking in Ogg files is best performed using a bisection search for a page
482 whose granule position corresponds to a PCM position at or before the seek
484 With appropriately weighted bisection, accurate seeking can be performed with
485 just three or four bisections even in multi-gigabyte files.
486 See <xref target="seeking"/> for general implementation guidance.
490 When seeking within an Ogg Opus stream, the decoder SHOULD start decoding (and
491 discarding the output) at least 3840 samples (80 ms) prior to the
492 seek target in order to ensure that the output audio is correct by the time it
493 reaches the seek target.
494 This 'pre-roll' is separate from, and unrelated to, the 'pre-skip' used at the
495 beginning of the stream.
496 If the point 80 ms prior to the seek target comes before the initial PCM
497 sample position, the decoder SHOULD start decoding from the beginning of the
498 stream, applying pre-skip as normal, regardless of whether the pre-skip is
499 larger or smaller than 80 ms, and then continue to discard the samples
500 required to reach the seek target (if any).
506 <section anchor="headers" title="Header Packets">
508 An Opus stream contains exactly two mandatory header packets:
509 an identification header and a comment header.
512 <section anchor="id_header" title="Identification Header">
514 <figure anchor="id_header_packet" title="ID Header Packet" align="center">
515 <artwork align="center"><![CDATA[
517 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
518 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
519 | 'O' | 'p' | 'u' | 's' |
520 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
521 | 'H' | 'e' | 'a' | 'd' |
522 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
523 | Version = 1 | Channel Count | Pre-skip |
524 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
525 | Input Sample Rate (Hz) |
526 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
527 | Output Gain (Q7.8 in dB) | Mapping Family| |
528 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ :
530 : Optional Channel Mapping Table... :
532 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
537 The fields in the identification (ID) header have the following meaning:
538 <list style="numbers">
539 <t><spanx style="strong">Magic Signature</spanx>:
540 <vspace blankLines="1"/>
541 This is an 8-octet (64-bit) field that allows codec identification and is
543 It contains, in order, the magic numbers:
554 Starting with "Op" helps distinguish it from audio data packets, as this is an
555 invalid TOC sequence.
556 <vspace blankLines="1"/>
558 <t><spanx style="strong">Version</spanx> (8 bits, unsigned):
559 <vspace blankLines="1"/>
560 The version number MUST always be '1' for this version of the encapsulation
562 Implementations SHOULD treat streams where the upper four bits of the version
563 number match that of a recognized specification as backwards-compatible with
565 That is, the version number can be split into "major" and "minor" version
566 sub-fields, with changes to the "minor" sub-field (in the lower four bits)
567 signaling compatible changes.
568 For example, a decoder implementing this specification SHOULD accept any stream
569 with a version number of '15' or less, and SHOULD assume any stream with a
570 version number '16' or greater is incompatible.
571 The initial version '1' was chosen to keep implementations from relying on this
572 octet as a null terminator for the "OpusHead" string.
573 <vspace blankLines="1"/>
575 <t><spanx style="strong">Output Channel Count</spanx> 'C' (8 bits, unsigned):
576 <vspace blankLines="1"/>
577 This is the number of output channels.
578 This might be different than the number of encoded channels, which can change
579 on a packet-by-packet basis.
580 This value MUST NOT be zero.
581 The maximum allowable value depends on the channel mapping family, and might be
583 See <xref target="channel_mapping"/> for details.
584 <vspace blankLines="1"/>
586 <t><spanx style="strong">Pre-skip</spanx> (16 bits, unsigned, little
588 <vspace blankLines="1"/>
589 This is the number of samples (at 48 kHz) to discard from the decoder
590 output when starting playback, and also the number to subtract from a page's
591 granule position to calculate its PCM sample position.
592 When cropping the beginning of existing Ogg Opus streams, a pre-skip of at
593 least 3,840 samples (80 ms) is RECOMMENDED to ensure complete
594 convergence in the decoder.
595 <vspace blankLines="1"/>
597 <t><spanx style="strong">Input Sample Rate</spanx> (32 bits, unsigned, little
599 <vspace blankLines="1"/>
600 This field is <spanx style="emph">not</spanx> the sample rate to use for
601 playback of the encoded data.
602 <vspace blankLines="1"/>
603 Opus has a handful of coding modes, with internal audio bandwidths of 4, 6, 8,
605 Each packet in the stream may have a different audio bandwidth.
606 Regardless of the audio bandwidth, the reference decoder supports decoding any
607 stream at a sample rate of 8, 12, 16, 24, or 48 kHz.
608 The original sample rate of the encoder input is not preserved by the lossy
610 <vspace blankLines="1"/>
611 An Ogg Opus player SHOULD select the playback sample rate according to the
613 <list style="numbers">
614 <t>If the hardware supports 48 kHz playback, decode at 48 kHz.</t>
615 <t>Otherwise, if the hardware's highest available sample rate is a supported
616 rate, decode at this sample rate.</t>
617 <t>Otherwise, if the hardware's highest available sample rate is less than
618 48 kHz, decode at the highest supported rate above this and resample.</t>
619 <t>Otherwise, decode at 48 kHz and resample.</t>
621 However, the 'Input Sample Rate' field allows the encoder to pass the sample
622 rate of the original input stream as metadata.
623 This may be useful when the user requires the output sample rate to match the
625 For example, a non-player decoder writing PCM format samples to disk might
626 choose to resample the output audio back to the original input sample rate to
627 reduce surprise to the user, who might reasonably expect to get back a file
628 with the same sample rate as the one they fed to the encoder.
629 <vspace blankLines="1"/>
630 A value of zero indicates 'unspecified'.
631 Encoders SHOULD write the actual input sample rate or zero, but decoder
632 implementations which do something with this field SHOULD take care to behave
633 sanely if given crazy values (e.g., do not actually upsample the output to
634 10 MHz if requested).
635 <vspace blankLines="1"/>
637 <t><spanx style="strong">Output Gain</spanx> (16 bits, signed, little
639 <vspace blankLines="1"/>
640 This is a gain to be applied by the decoder.
641 It is 20*log10 of the factor to scale the decoder output by to achieve the
642 desired playback volume, stored in a 16-bit, signed, two's complement
643 fixed-point value with 8 fractional bits (i.e., Q7.8).
644 To apply the gain, a decoder could use
645 <figure align="center">
646 <artwork align="center"><![CDATA[
647 sample *= pow(10, output_gain/(20.0*256)) ,
650 where output_gain is the raw 16-bit value from the header.
651 <vspace blankLines="1"/>
652 Virtually all players and media frameworks should apply it by default.
653 If a player chooses to apply any volume adjustment or gain modification, such
654 as the R128_TRACK_GAIN (see <xref target="comment_header"/>) or a user-facing
655 volume knob, the adjustment MUST be applied in addition to this output gain in
656 order to achieve playback at the desired volume.
657 <vspace blankLines="1"/>
658 An encoder SHOULD set this field to zero, and instead apply any gain prior to
659 encoding, when this is possible and does not conflict with the user's wishes.
660 The output gain should only be nonzero when the gain is adjusted after
661 encoding, or when the user wishes to adjust the gain for playback while
662 preserving the ability to recover the original signal amplitude.
663 <vspace blankLines="1"/>
664 Although the output gain has enormous range (+/- 128 dB, enough to amplify
665 inaudible sounds to the threshold of physical pain), most applications can
666 only reasonably use a small portion of this range around zero.
667 The large range serves in part to ensure that gain can always be losslessly
668 transferred between OpusHead and R128_TRACK_GAIN (see below) without
670 <vspace blankLines="1"/>
672 <t><spanx style="strong">Channel Mapping Family</spanx> (8 bits,
674 <vspace blankLines="1"/>
675 This octet indicates the order and semantic meaning of the various channels
676 encoded in each Ogg packet.
677 <vspace blankLines="1"/>
678 Each possible value of this octet indicates a mapping family, which defines a
679 set of allowed channel counts, and the ordered set of channel names for each
680 allowed channel count.
681 The details are described in <xref target="channel_mapping"/>.
683 <t><spanx style="strong">Channel Mapping Table</spanx>:
684 This table defines the mapping from encoded streams to output channels.
685 It is omitted when the channel mapping family is 0, but REQUIRED otherwise.
686 Its contents are specified in <xref target="channel_mapping"/>.
692 All fields in the ID headers are REQUIRED, except for the channel mapping
693 table, which is omitted when the channel mapping family is 0.
694 Implementations SHOULD reject ID headers which do not contain enough data for
695 these fields, even if they contain a valid Magic Signature.
696 Future versions of this specification, even backwards-compatible versions,
697 might include additional fields in the ID header.
698 If an ID header has a compatible major version, but a larger minor version,
699 an implementation MUST NOT reject it for containing additional data not
701 However, implementations MAY reject streams in which the ID header does not
702 complete on the first page.
705 <section anchor="channel_mapping" title="Channel Mapping">
707 An Ogg Opus stream allows mapping one number of Opus streams (N) to a possibly
708 larger number of decoded channels (M+N) to yet another number of output
709 channels (C), which might be larger or smaller than the number of decoded
711 The order and meaning of these channels are defined by a channel mapping,
712 which consists of the 'channel mapping family' octet and, for channel mapping
713 families other than family 0, a channel mapping table, as illustrated in
714 <xref target="channel_mapping_table"/>.
717 <figure anchor="channel_mapping_table" title="Channel Mapping Table"
719 <artwork align="center"><![CDATA[
721 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
724 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
725 | Coupled Count | Channel Mapping... :
726 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
731 The fields in the channel mapping table have the following meaning:
732 <list style="numbers" counter="8">
733 <t><spanx style="strong">Stream Count</spanx> 'N' (8 bits, unsigned):
734 <vspace blankLines="1"/>
735 This is the total number of streams encoded in each Ogg packet.
736 This value is required to correctly parse the packed Opus packets inside an
737 Ogg packet, as described in <xref target="packet_organization"/>.
738 This value MUST NOT be zero, as without at least one Opus packet with a valid
739 TOC sequence, a demuxer cannot recover the duration of an Ogg packet.
740 <vspace blankLines="1"/>
741 For channel mapping family 0, this value defaults to 1, and is not coded.
742 <vspace blankLines="1"/>
744 <t><spanx style="strong">Coupled Stream Count</spanx> 'M' (8 bits, unsigned):
745 This is the number of streams whose decoders should be configured to produce
747 This MUST be no larger than the total number of streams, N.
748 <vspace blankLines="1"/>
749 Each packet in an Opus stream has an internal channel count of 1 or 2, which
750 can change from packet to packet.
751 This is selected by the encoder depending on the bitrate and the audio being
753 The original channel count of the encoder input is not preserved by the lossy
755 <vspace blankLines="1"/>
756 Regardless of the internal channel count, any Opus stream can be decoded as
757 mono (a single channel) or stereo (two channels) by appropriate initialization
759 The 'coupled stream count' field indicates that the first M Opus decoders are
760 to be initialized in stereo mode, and the remaining N-M decoders are to be
761 initialized in mono mode.
762 The total number of decoded channels, (M+N), MUST be no larger than 255, as
763 there is no way to index more channels than that in the channel mapping.
764 <vspace blankLines="1"/>
765 For channel mapping family 0, this value defaults to C-1 (i.e., 0 for mono
766 and 1 for stereo), and is not coded.
767 <vspace blankLines="1"/>
769 <t><spanx style="strong">Channel Mapping</spanx> (8*C bits):
770 This contains one octet per output channel, indicating which decoded channel
771 should be used for each one.
772 Let 'index' be the value of this octet for a particular output channel.
773 This value MUST either be smaller than (M+N), or be the special value 255.
774 If 'index' is less than 2*M, the output MUST be taken from decoding stream
775 ('index'/2) as stereo and selecting the left channel if 'index' is even, and
776 the right channel if 'index' is odd.
777 If 'index' is 2*M or larger, the output MUST be taken from decoding stream
779 If 'index' is 255, the corresponding output channel MUST contain pure silence.
780 <vspace blankLines="1"/>
781 The number of output channels, C, is not constrained to match the number of
782 decoded channels (M+N).
783 A single index value MAY appear multiple times, i.e., the same decoded channel
784 might be mapped to multiple output channels.
785 Some decoded channels might not be assigned to any output channel, as well.
786 <vspace blankLines="1"/>
787 For channel mapping family 0, the first index defaults to 0, and if C==2,
788 the second index defaults to 1.
789 Neither index is coded.
795 After producing the output channels, the channel mapping family determines the
796 semantic meaning of each one.
797 Currently there are three defined mapping families, although more may be added.
800 <section anchor="channel_mapping_0" title="Channel Mapping Family 0">
802 Allowed numbers of channels: 1 or 2.
806 <list style="symbols">
807 <t>1 channel: monophonic (mono).</t>
808 <t>2 channels: stereo (left, right).</t>
810 <spanx style="strong">Special mapping</spanx>: This channel mapping value also
811 indicates that the contents consists of a single Opus stream that is stereo if
812 and only if C==2, with stream index 0 mapped to output channel 0 (mono, or
813 left channel) and stream index 1 mapped to output channel 1 (right channel)
815 When the 'channel mapping family' octet has this value, the channel mapping
816 table MUST be omitted from the ID header packet.
820 <section anchor="channel_mapping_1" title="Channel Mapping Family 1">
822 Allowed numbers of channels: 1...8.
823 Vorbis channel order.
826 Each channel is assigned to a speaker location in a conventional surround
828 Specific locations depend on the number of channels, and are given below
829 in order of the corresponding channel indicies.
830 <list style="symbols">
831 <t>1 channel: monophonic (mono).</t>
832 <t>2 channels: stereo (left, right).</t>
833 <t>3 channels: linear surround (left, center, right)</t>
834 <t>4 channels: quadraphonic (front left, front right, rear left, rear right).</t>
835 <t>5 channels: 5.0 surround (front left, front center, front right, rear left, rear right).</t>
836 <t>6 channels: 5.1 surround (front left, front center, front right, rear left, rear right, LFE).</t>
837 <t>7 channels: 6.1 surround (front left, front center, front right, side left, side right, rear center, LFE).</t>
838 <t>8 channels: 7.1 surround (front left, front center, front right, side left, side right, rear left, rear right, LFE)</t>
840 This set of surround configurations and speaker location orderings is the same
841 as the one used by the Vorbis codec <xref target="vorbis-mapping"/>.
842 The ordering is different from the one used by the
843 WAVE <xref target="wave-multichannel"/> and
844 FLAC <xref target="flac"/> formats,
845 so correct ordering requires permutation of the output channels when encoding
846 from or decoding to those formats.
847 'LFE' here refers to a Low Frequency Effects, often mapped to a subwoofer
848 with no particular spacial position.
849 Implementations SHOULD identify 'side' or 'rear' speaker locations with
850 'surround' and 'back' as appropriate when interfacing with audio formats
851 or systems which prefer that terminology.
855 <section anchor="channel_mapping_255"
856 title="Channel Mapping Family 255">
858 Allowed numbers of channels: 1...255.
859 No defined channel meaning.
862 Channels are unidentified.
863 General-purpose players SHOULD NOT attempt to play these streams, and offline
864 decoders MAY deinterleave the output into separate PCM files, one per channel.
865 Decoders SHOULD NOT produce output for channels mapped to stream index 255
866 (pure silence) unless they have no other way to indicate the index of
871 <section anchor="channel_mapping_undefined"
872 title="Undefined Channel Mappings">
874 The remaining channel mapping families (2...254) are reserved.
875 A decoder encountering a reserved channel mapping family value SHOULD act as
876 though the value is 255.
880 <section anchor="downmix" title="Downmixing">
882 An Ogg Opus player MUST play any Ogg Opus stream with a channel mapping family
883 of 0 or 1, even if the number of channels does not match the physically
884 connected audio hardware.
885 Players SHOULD perform channel mixing to increase or reduce the number of
890 Implementations MAY use the following matricies to implement downmixing from
891 multichannel files using <xref target="channel_mapping_1">Channel Mapping
892 Family 1</xref>, which are known to give acceptable results for stereo.
893 Matricies for 3 and 4 channels are normalized so each coefficent row sums
894 to 1 to avoid clipping.
895 For 5 or more channels they are normalized to 2 as a compromize between
896 clipping and dynamic range reduction.
899 In these matricies the front left and front right channels are generally
900 passed through directly.
901 When a surround channel is split between both the left and right stereo
902 channels, coefficients are chosen so their squares sum to 1, which
903 helps preserve the perceived intensity.
904 Rear channels are mixed more diffusely or attenuated to maintain focus
905 on the front channels.
908 <figure anchor="downmix-matrix-3"
909 title="Stereo downmix matrix for the linear surround channel mapping"
911 <artwork align="center"><![CDATA[
912 Left output = ( 0.585786 * left + 0.414214 * center )
913 Right output = ( 0.414214 * center + 0.585786 * right )
916 Exact coefficient values are 1 and 1/sqrt(2), multiplied by
917 1/(1 + 1/sqrt(2)) for normalization.
921 <figure anchor="downmix-matrix-4"
922 title="Stereo downmix matrix for the quadraphonic channel mapping"
924 <artwork align="center"><![CDATA[
926 | L output | | 0.422650 0.000000 0.366025 0.211325 | | FR |
927 | R output | = | 0.000000 0.422650 0.211325 0.366025 | | RL |
931 Exact coefficient values are 1, sqrt(3)/2 and 1/2, multiplied by
932 1/(1 + sqrt(3)/2 + 1/2) for normalization.
936 <figure anchor="downmix-matrix-5"
937 title="Stereo downmix matrix for the 5.0 surround mapping"
939 <artwork align="center"><![CDATA[
942 | L | | 0.650802 0.460186 0.000000 0.563611 0.325401 | | FR |
943 | R | = | 0.000000 0.460186 0.650802 0.325401 0.563611 | | RL |
948 Exact coefficient values are 1, 1/sqrt(2), sqrt(3)/2 and 1/2, multiplied by
949 2/(1 + 1/sqrt(2) + sqrt(3)/2 + 1/2)
954 <figure anchor="downmix-matrix-6"
955 title="Stereo downmix matrix for the 5.1 surround mapping"
957 <artwork align="center"><![CDATA[
960 |L| | 0.529067 0.374107 0.000000 0.458186 0.264534 0.374107 | |FR |
961 |R| = | 0.000000 0.374107 0.529067 0.264534 0.458186 0.374107 | |RL |
966 Exact coefficient values are 1, 1/sqrt(2), sqrt(3)/2 and 1/2, multiplied by
967 2/(1 + 1/sqrt(2) + sqrt(3)/2 + 1/2 + 1/sqrt(2))
972 <figure anchor="downmix-matrix-7"
973 title="Stereo downmix matrix for the 6.1 surround mapping"
975 <artwork align="center"><![CDATA[
977 | 0.455310 0.321953 0.000000 0.394310 0.227655 0.278819 0.321953 |
978 | 0.000000 0.321953 0.455310 0.227655 0.394310 0.278819 0.321953 |
982 Exact coefficient values are 1, 1/sqrt(2), sqrt(3)/2, 1/2 and
983 sqrt(3)/2/sqrt(2), multiplied by
984 2/(1 + 1/sqrt(2) + sqrt(3)/2 + 1/2 +
985 sqrt(3)/2/sqrt(2) + 1/sqrt(2)) for normalization.
986 The coeffients are in the same order as in <xref target="channel_mapping_1" />,
987 and the matricies above.
991 <figure anchor="downmix-matrix-8"
992 title="Stereo downmix matrix for the 7.1 surround mapping"
994 <artwork align="center"><![CDATA[
996 | .388631 .274804 .000000 .336565 .194316 .336565 .194316 .274804 |
997 | .000000 .274804 .388631 .194316 .336565 .194316 .336565 .274804 |
1001 Exact coefficient values are 1, 1/sqrt(2), sqrt(3)/2 and 1/2, multiplied by
1002 2/(2 + 2/sqrt(2) + sqrt(3)) for normalization.
1003 The coeffients are in the same order as in <xref target="channel_mapping_1" />,
1004 and the matricies above.
1010 </section> <!-- end channel_mapping_table -->
1012 </section> <!-- end id_header -->
1014 <section anchor="comment_header" title="Comment Header">
1016 <figure anchor="comment_header_packet" title="Comment Header Packet"
1018 <artwork align="center"><![CDATA[
1020 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
1021 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1022 | 'O' | 'p' | 'u' | 's' |
1023 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1024 | 'T' | 'a' | 'g' | 's' |
1025 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1026 | Vendor String Length |
1027 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1029 : Vendor String... :
1031 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1032 | User Comment List Length |
1033 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1034 | User Comment #0 String Length |
1035 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1037 : User Comment #0 String... :
1039 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1040 | User Comment #1 String Length |
1041 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1047 The comment header consists of a 64-bit magic signature, followed by data in
1048 the same format as the <xref target="vorbis-comment"/> header used in Ogg
1049 Vorbis, except (like Ogg Theora and Speex) the final "framing bit" specified
1050 in the Vorbis spec is not present.
1051 <list style="numbers">
1052 <t><spanx style="strong">Magic Signature</spanx>:
1053 <vspace blankLines="1"/>
1054 This is an 8-octet (64-bit) field that allows codec identification and is
1056 It contains, in order, the magic numbers:
1057 <list style="empty">
1067 Starting with "Op" helps distinguish it from audio data packets, as this is an
1068 invalid TOC sequence.
1069 <vspace blankLines="1"/>
1071 <t><spanx style="strong">Vendor String Length</spanx> (32 bits, unsigned,
1073 <vspace blankLines="1"/>
1074 This field gives the length of the following vendor string, in octets.
1075 It MUST NOT indicate that the vendor string is longer than the rest of the
1077 <vspace blankLines="1"/>
1079 <t><spanx style="strong">Vendor String</spanx> (variable length, UTF-8 vector):
1080 <vspace blankLines="1"/>
1081 This is a simple human-readable tag for vendor information, encoded as a UTF-8
1082 string <xref target="RFC3629"/>.
1083 No terminating null octet is required.
1084 <vspace blankLines="1"/>
1085 This tag is intended to identify the codec encoder and encapsulation
1086 implementations, for tracing differences in technical behavior.
1087 User-facing encoding applications can use the 'ENCODER' user comment tag
1088 to identify themselves.
1089 <vspace blankLines="1"/>
1091 <t><spanx style="strong">User Comment List Length</spanx> (32 bits, unsigned,
1093 <vspace blankLines="1"/>
1094 This field indicates the number of user-supplied comments.
1095 It MAY indicate there are zero user-supplied comments, in which case there are
1096 no additional fields in the packet.
1097 It MUST NOT indicate that there are so many comments that the comment string
1098 lengths would require more data than is available in the rest of the packet.
1099 <vspace blankLines="1"/>
1101 <t><spanx style="strong">User Comment #i String Length</spanx> (32 bits,
1102 unsigned, little endian):
1103 <vspace blankLines="1"/>
1104 This field gives the length of the following user comment string, in octets.
1105 There is one for each user comment indicated by the 'user comment list length'
1107 It MUST NOT indicate that the string is longer than the rest of the packet.
1108 <vspace blankLines="1"/>
1110 <t><spanx style="strong">User Comment #i String</spanx> (variable length, UTF-8
1112 <vspace blankLines="1"/>
1113 This field contains a single user comment string.
1114 There is one for each user comment indicated by the 'user comment list length'
1121 The vendor string length and user comment list length are REQUIRED, and
1122 implementations SHOULD reject comment headers that do not contain enough data
1123 for these fields, or that do not contain enough data for the corresponding
1124 vendor string or user comments they describe.
1125 Making this check before allocating the associated memory to contain the data
1126 may help prevent a possible Denial-of-Service (DoS) attack from small comment
1127 headers that claim to contain strings longer than the entire packet or more
1128 user comments than than could possibly fit in the packet.
1132 The user comment strings follow the NAME=value format described by
1133 <xref target="vorbis-comment"/> with the same recommended tag names.
1134 One new comment tag is introduced for Ogg Opus:
1135 <figure align="center">
1136 <artwork align="left"><![CDATA[
1137 R128_TRACK_GAIN=-573
1140 representing the volume shift needed to normalize the track's volume.
1141 The gain is a Q7.8 fixed point number in dB, as in the ID header's 'output
1143 This tag is similar to the REPLAYGAIN_TRACK_GAIN tag in
1144 Vorbis <xref target="replay-gain"/>, except that the normal volume
1145 reference is the <xref target="EBU-R128"/> standard.
1148 An Ogg Opus file MUST NOT have more than one such tag, and if present its
1149 value MUST be an integer from -32768 to 32767, inclusive, represented in
1150 ASCII with no whitespace.
1151 If present, it MUST correctly represent the R128 normalization gain relative
1152 to the 'output gain' field specified in the ID header.
1153 If a player chooses to make use of the R128_TRACK_GAIN tag, it MUST be
1154 applied <spanx style="emph">in addition</spanx> to the 'output gain' value.
1155 If an encoder wishes to use R128 normalization, and the output gain is not
1156 otherwise constrained or specified, the encoder SHOULD write the R128 gain
1157 into the 'output gain' field and store a tag containing "R128_TRACK_GAIN=0".
1158 That is, it should assume that by default tools will respect the 'output gain'
1159 field, and not the comment tag.
1160 If a tool modifies the ID header's 'output gain' field, it MUST also update or
1161 remove the R128_TRACK_GAIN comment tag.
1164 To avoid confusion with multiple normalization schemes, an Opus comment header
1165 SHOULD NOT contain any of the REPLAYGAIN_TRACK_GAIN, REPLAYGAIN_TRACK_PEAK,
1166 REPLAYGAIN_ALBUM_GAIN, or REPLAYGAIN_ALBUM_PEAK tags.
1169 There is no Opus comment tag corresponding to REPLAYGAIN_ALBUM_GAIN.
1170 That information should instead be stored in the ID header's 'output gain'
1177 <section anchor="packet_size_limits" title="Packet Size Limits">
1179 Technically valid Opus packets can be arbitrarily large due to the padding
1180 format, although the amount of non-padding data they can contain is bounded.
1181 These packets might be spread over a similarly enormous number of Ogg pages.
1182 Encoders SHOULD use no more padding than required to make a variable bitrate
1183 (VBR) stream constant bitrate (CBR).
1184 Decoders SHOULD avoid attempting to allocate excessive amounts of memory when
1185 presented with a very large packet.
1186 The presence of an extremely large packet in the stream could indicate a
1187 memory exhaustion attack or stream corruption.
1188 Decoders SHOULD reject a packet that is too large to process, and display a
1192 In an Ogg Opus stream, the largest possible valid packet that does not use
1193 padding has a size of (61,298*N - 2) octets, or about 60 kB per
1195 With 255 streams, this is 15,630,988 octets (14.9 MB) and can
1196 span up to 61,298 Ogg pages, all but one of which will have a granule
1198 This is of course a very extreme packet, consisting of 255 streams, each
1199 containing 120 ms of audio encoded as 2.5 ms frames, each frame
1200 using the maximum possible number of octets (1275) and stored in the least
1201 efficient manner allowed (a VBR code 3 Opus packet).
1202 Even in such a packet, most of the data will be zeros as 2.5 ms frames
1203 cannot actually use all 1275 octets.
1204 The largest packet consisting of entirely useful data is
1205 (15,326*N - 2) octets, or about 15 kB per stream.
1206 This corresponds to 120 ms of audio encoded as 10 ms frames in either
1207 SILK or Hybrid mode, but at a data rate of over 1 Mbps, which makes little
1208 sense for the quality achieved.
1209 A more reasonable limit is (7,664*N - 2) octets, or about 7.5 kB
1211 This corresponds to 120 ms of audio encoded as 20 ms stereo CELT mode
1212 frames, with a total bitrate just under 511 kbps (not counting the Ogg
1213 encapsulation overhead).
1214 With N=8, the maximum number of channels currently defined by mapping
1215 family 1, this gives a maximum packet size of 61,310 octets, or just
1217 This is still quite conservative, as it assumes each output channel is taken
1218 from one decoded channel of a stereo packet.
1219 An implementation could reasonably choose any of these numbers for its internal
1224 <section anchor="encoder" title="Encoder Guidelines">
1226 When encoding Opus files, Ogg encoders should take into account the
1227 algorithmic delay of the Opus encoder.
1229 <figure align="center">
1231 In encoders derived from the reference implementation, the number of
1232 samples can be queried with:
1234 <artwork align="center"><![CDATA[
1235 opus_encoder_ctl(encoder_state, OPUS_GET_LOOKAHEAD, &samples_delay);
1239 To achieve good quality in the very first samples of a stream, the Ogg encoder
1240 MAY use LPC extrapolation to generate at least 120 extra samples
1241 (extra_samples) at the beginning to avoid the Opus encoder having to encode
1242 a discontinuous signal.
1243 For an input file containing length samples, the Ogg encoder SHOULD set the
1244 preskip header flag to samples_delay+extra_samples, encode at least
1245 length+samples_delay+extra_samples samples, and set the granulepos of the last
1246 page to length+samples_delay+extra_samples.
1247 This ensures that the encoded file has the same duration as the original, with
1248 no time offset. The best way to pad the end of the stream is to also use LPC
1249 extrapolation, but zero-padding is also acceptable.
1252 <section anchor="lpc" title="LPC Extrapolation">
1254 The first step in LPC extrapolation is to compute linear prediction
1256 When extending the end of the signal, order-N (typically with N ranging from 8
1257 to 40) LPC analysis is performed on a window near the end of the signal.
1258 The last N samples are used as memory to an infinite impulse response (IIR)
1261 <figure align="center">
1263 The filter is then applied on a zero input to extrapolate the end of the signal.
1264 Let a(k) be the kth LPC coefficient and x(n) be the nth sample of the signal,
1265 each new sample past the end of the signal is computed as:
1267 <artwork align="center"><![CDATA[
1270 x(n) = \ a(k)*x(n-k)
1277 The process is repeated independently for each channel.
1278 It is possible to extend the beginning of the signal by applying the same
1279 process backward in time.
1280 When extending the beginning of the signal, it is best to apply a "fade in" to
1281 the extrapolated signal, e.g. by multiplying it by a half-Hanning window
1282 <xref target="hanning"/>.
1287 <section anchor="continuous_chaining" title="Continuous Chaining">
1289 In some applications, such as Internet radio, it is desirable to cut a long
1290 streams into smaller chains, e.g. so the comment header can be updated.
1291 This can be done simply by separating the input streams into segments and
1292 encoding each segment independently.
1293 The drawback of this approach is that it creates a small discontinuity
1294 at the boundary due to the lossy nature of Opus.
1295 An encoder MAY avoid this discontinuity by using the following procedure:
1296 <list style="numbers">
1297 <t>Encode the last frame of the first segment as an independent frame by
1298 turning off all forms of inter-frame prediction.
1299 De-emphasis is allowed.</t>
1300 <t>Set the granulepos of the last page to a point near the end of the last
1302 <t>Begin the second segment with a copy of the last frame of the first
1304 <t>Set the preskip flag of the second stream in such a way as to properly
1305 join the two streams.</t>
1306 <t>Continue the encoding process normally from there, without any reset to
1314 <section anchor="implementation" title="Implementation Status">
1316 A brief summary of major implementations of this draft is available
1317 at <eref target="https://wiki.xiph.org/OggOpusImplementation"/>,
1318 along with their status.
1321 [Note to RFC Editor: please remove this entire section before
1322 final publication per <xref target="draft-sheffer-running-code"/>.]
1326 <section anchor="security" title="Security Considerations">
1328 Implementations of the Opus codec need to take appropriate security
1329 considerations into account, as outlined in <xref target="RFC4732"/>.
1330 This is just as much a problem for the container as it is for the codec itself.
1331 It is extremely important for the decoder to be robust against malicious
1333 Malicious payloads must not cause the decoder to overrun its allocated memory
1334 or to take an excessive amount of resources to decode.
1335 Although problems in encoders are typically rarer, the same applies to the
1337 Malicious audio streams must not cause the encoder to misbehave because this
1338 would allow an attacker to attack transcoding gateways.
1342 Like most other container formats, Ogg Opus files should not be used with
1343 insecure ciphers or cipher modes that are vulnerable to known-plaintext
1345 Elements such as the Ogg page capture pattern and the magic signatures in the
1346 ID header and the comment header all have easily predictable values, in
1347 addition to various elements of the codec data itself.
1351 <section anchor="content_type" title="Content Type">
1353 An "Ogg Opus file" consists of one or more sequentially multiplexed segments,
1354 each containing exactly one Ogg Opus stream.
1355 The RECOMMENDED mime-type for Ogg Opus files is "audio/ogg".
1360 If more specificity is desired, one MAY indicate the presence of Opus streams
1361 using the codecs parameter defined in <xref target="RFC6381"/>, e.g.,
1363 <artwork align="center"><![CDATA[
1364 audio/ogg; codecs=opus
1367 for an Ogg Opus file.
1372 The RECOMMENDED filename extension for Ogg Opus files is '.opus'.
1376 When Opus is concurrently multiplexed with other streams in an Ogg container,
1377 one SHOULD use one of the "audio/ogg", "video/ogg", or "application/ogg"
1378 mime-types, as defined in <xref target="RFC5334"/>.
1379 Such streams are not strictly "Ogg Opus files" as described above,
1380 since they contain more than a single Opus stream per sequentially
1381 multiplexed segment, e.g. video or multiple audio tracks.
1382 In such cases the the '.opus' filename extension is NOT RECOMMENDED.
1386 <section title="IANA Considerations">
1388 This document has no actions for IANA.
1392 <section anchor="Acknowledgments" title="Acknowledgments">
1394 Thanks to Greg Maxwell, Christopher "Monty" Montgomery, and Jean-Marc Valin for
1395 their valuable contributions to this document.
1396 Additional thanks to Andrew D'Addesio, Greg Maxwell, and Vincent Penqeurc'h for
1397 their feedback based on early implementations.
1401 <section title="Copying Conditions">
1403 The authors agree to grant third parties the irrevocable right to copy, use,
1404 and distribute the work, with or without modification, in any medium, without
1405 royalty, provided that, unless separate permission is granted, redistributed
1406 modified works do not contain misleading author, version, name of work, or
1407 endorsement information.
1413 <references title="Normative References">
1421 <reference anchor="EBU-R128" target="http://tech.ebu.ch/loudness">
1423 <title>"Loudness Recommendation EBU R128</title>
1424 <author fullname="EBU Technical Committee"/>
1425 <date month="August" year="2011"/>
1429 <reference anchor="vorbis-comment"
1430 target="http://www.xiph.org/vorbis/doc/v-comment.html">
1432 <title>Ogg Vorbis I Format Specification: Comment Field and Header
1433 Specification</title>
1434 <author initials="C." surname="Montgomery"
1435 fullname="Christopher "Monty" Montgomery"/>
1436 <date month="July" year="2002"/>
1442 <references title="Informative References">
1444 <!--?rfc include="http://xml.resource.org/public/rfc/bibxml/reference.RFC.3550.xml"?-->
1447 <reference anchor="draft-sheffer-running-code"
1448 target="https://tools.ietf.org/html/draft-sheffer-running-code-05#section-2">
1450 <title>Improving "Rough Consensus" with Running Code</title>
1451 <author initials="Y." surname="Sheffer" fullname="Yaron Sheffer"/>
1452 <author initials="A." surname="Farrel" fullname="Adrian Farrel"/>
1453 <date month="May" year="2013"/>
1457 <reference anchor="flac"
1458 target="https://xiph.org/flac/format.html">
1460 <title>FLAC - Free Lossless Audio Codec Format Description</title>
1461 <author initials="J." surname="Coalson" fullname="Josh Coalson"/>
1462 <date month="January" year="2008"/>
1466 <reference anchor="hanning"
1467 target="http://en.wikipedia.org/wiki/Hamming_function#Hann_.28Hanning.29_window">
1469 <title>"Hann window</title>
1470 <author fullname="Wikipedia"/>
1471 <date month="May" year="2013"/>
1475 <reference anchor="replay-gain"
1476 target="http://wiki.xiph.org/VorbisComment#Replay_Gain">
1478 <title>VorbisComment: Replay Gain</title>
1479 <author initials="C." surname="Parker" fullname="Conrad Parker"/>
1480 <author initials="M." surname="Leese" fullname="Martin Leese"/>
1481 <date month="June" year="2009"/>
1485 <reference anchor="seeking"
1486 target="http://wiki.xiph.org/Seeking">
1488 <title>Granulepos Encoding and How Seeking Really Works</title>
1489 <author initials="S." surname="Pfeiffer" fullname="Silvia Pfeiffer"/>
1490 <author initials="C." surname="Parker" fullname="Conrad Parker"/>
1491 <author initials="G." surname="Maxwell" fullname="Greg Maxwell"/>
1492 <date month="May" year="2012"/>
1496 <reference anchor="vorbis-mapping"
1497 target="http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-800004.3.9">
1499 <title>The Vorbis I Specification, Section 4.3.9 Output Channel Order</title>
1500 <author initials="C." surname="Montgomery"
1501 fullname="Christopher "Monty" Montgomery"/>
1502 <date month="January" year="2010"/>
1506 <reference anchor="vorbis-trim"
1507 target="http://xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-130000A.2">
1509 <title>The Vorbis I Specification, Appendix A: Embedding Vorbis
1510 into an Ogg stream</title>
1511 <author initials="C." surname="Montgomery"
1512 fullname="Christopher "Monty" Montgomery"/>
1513 <date month="November" year="2008"/>
1517 <reference anchor="wave-multichannel"
1518 target="http://msdn.microsoft.com/en-us/windows/hardware/gg463006.aspx">
1520 <title>Multiple Channel Audio Data and WAVE Files</title>
1521 <author fullname="Microsoft Corporation"/>
1522 <date month="March" year="2007"/>