idnits 2.17.1 draft-ietf-codec-oggopus-06.txt: Checking boilerplate required by RFC 5378 and the IETF Trust (see https://trustee.ietf.org/license-info): ---------------------------------------------------------------------------- No issues found here. Checking nits according to https://www.ietf.org/id-info/1id-guidelines.txt: ---------------------------------------------------------------------------- No issues found here. Checking nits according to https://www.ietf.org/id-info/checklist : ---------------------------------------------------------------------------- No issues found here. Miscellaneous warnings: ---------------------------------------------------------------------------- == The copyright year in the IETF Trust and authors Copyright Line does not match the current year -- The document date (October 18, 2014) is 3477 days in the past. Is this intentional? Checking references for intended status: Proposed Standard ---------------------------------------------------------------------------- (See RFCs 3967 and 4897 for information about using normative references to lower-maturity documents in RFCs) -- Looks like a reference, but probably isn't: '1' on line 1341 ** Downref: Normative reference to an Informational RFC: RFC 3533 -- Possible downref: Non-RFC (?) normative reference: ref. 'EBU-R128' -- Obsolete informational reference (is this intentional?): RFC 6982 (Obsoleted by RFC 7942) Summary: 1 error (**), 0 flaws (~~), 1 warning (==), 4 comments (--). Run idnits with the --verbose option for more detailed information about the items above. -------------------------------------------------------------------------------- 2 codec T. Terriberry 3 Internet-Draft Mozilla Corporation 4 Intended status: Standards Track R. Lee 5 Expires: April 21, 2015 Voicetronix 6 R. Giles 7 Mozilla Corporation 8 October 18, 2014 10 Ogg Encapsulation for the Opus Audio Codec 11 draft-ietf-codec-oggopus-06 13 Abstract 15 This document defines the Ogg encapsulation for the Opus interactive 16 speech and audio codec. This allows data encoded in the Opus format 17 to be stored in an Ogg logical bitstream. Ogg encapsulation provides 18 Opus with a long-term storage format supporting all of the essential 19 features, including metadata, fast and accurate seeking, corruption 20 detection, recapture after errors, low overhead, and the ability to 21 multiplex Opus with other codecs (including video) with minimal 22 buffering. It also provides a live streamable format, capable of 23 delivery over a reliable stream-oriented transport, without requiring 24 all the data, or even the total length of the data, up-front, in a 25 form that is identical to the on-disk storage format. 27 Status of This Memo 29 This Internet-Draft is submitted in full conformance with the 30 provisions of BCP 78 and BCP 79. 32 Internet-Drafts are working documents of the Internet Engineering 33 Task Force (IETF). Note that other groups may also distribute 34 working documents as Internet-Drafts. The list of current Internet- 35 Drafts is at http://datatracker.ietf.org/drafts/current/. 37 Internet-Drafts are draft documents valid for a maximum of six months 38 and may be updated, replaced, or obsoleted by other documents at any 39 time. It is inappropriate to use Internet-Drafts as reference 40 material or to cite them other than as "work in progress." 42 This Internet-Draft will expire on April 21, 2015. 44 Copyright Notice 46 Copyright (c) 2014 IETF Trust and the persons identified as the 47 document authors. All rights reserved. 49 This document is subject to BCP 78 and the IETF Trust's Legal 50 Provisions Relating to IETF Documents 51 (http://trustee.ietf.org/license-info) in effect on the date of 52 publication of this document. Please review these documents 53 carefully, as they describe your rights and restrictions with respect 54 to this document. Code Components extracted from this document must 55 include Simplified BSD License text as described in Section 4.e of 56 the Trust Legal Provisions and are provided without warranty as 57 described in the Simplified BSD License. 59 Table of Contents 61 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . 2 62 2. Terminology . . . . . . . . . . . . . . . . . . . . . . . . . 3 63 3. Packet Organization . . . . . . . . . . . . . . . . . . . . . 3 64 4. Granule Position . . . . . . . . . . . . . . . . . . . . . . 5 65 4.1. Repairing Gaps in Real-time Streams . . . . . . . . . . . 6 66 4.2. Pre-skip . . . . . . . . . . . . . . . . . . . . . . . . 7 67 4.3. PCM Sample Position . . . . . . . . . . . . . . . . . . . 8 68 4.4. End Trimming . . . . . . . . . . . . . . . . . . . . . . 9 69 4.5. Restrictions on the Initial Granule Position . . . . . . 9 70 4.6. Seeking and Pre-roll . . . . . . . . . . . . . . . . . . 10 71 5. Header Packets . . . . . . . . . . . . . . . . . . . . . . . 10 72 5.1. Identification Header . . . . . . . . . . . . . . . . . . 10 73 5.1.1. Channel Mapping . . . . . . . . . . . . . . . . . . . 15 74 5.2. Comment Header . . . . . . . . . . . . . . . . . . . . . 20 75 5.2.1. Tag Definitions . . . . . . . . . . . . . . . . . . . 22 76 6. Packet Size Limits . . . . . . . . . . . . . . . . . . . . . 24 77 7. Encoder Guidelines . . . . . . . . . . . . . . . . . . . . . 24 78 7.1. LPC Extrapolation . . . . . . . . . . . . . . . . . . . . 25 79 7.2. Continuous Chaining . . . . . . . . . . . . . . . . . . . 26 80 8. Implementation Status . . . . . . . . . . . . . . . . . . . . 26 81 9. Security Considerations . . . . . . . . . . . . . . . . . . . 26 82 10. Content Type . . . . . . . . . . . . . . . . . . . . . . . . 27 83 11. IANA Considerations . . . . . . . . . . . . . . . . . . . . . 27 84 12. Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . 27 85 13. Copying Conditions . . . . . . . . . . . . . . . . . . . . . 28 86 14. References . . . . . . . . . . . . . . . . . . . . . . . . . 28 87 14.1. Normative References . . . . . . . . . . . . . . . . . . 28 88 14.2. Informative References . . . . . . . . . . . . . . . . . 28 89 14.3. URIs . . . . . . . . . . . . . . . . . . . . . . . . . . 29 90 Authors' Addresses . . . . . . . . . . . . . . . . . . . . . . . 30 92 1. Introduction 94 The IETF Opus codec is a low-latency audio codec optimized for both 95 voice and general-purpose audio. See [RFC6716] for technical 96 details. This document defines the encapsulation of Opus in a 97 continuous, logical Ogg bitstream [RFC3533]. 99 Ogg bitstreams are made up of a series of 'pages', each of which 100 contains data from one or more 'packets'. Pages are the fundamental 101 unit of multiplexing in an Ogg stream. Each page is associated with 102 a particular logical stream and contains a capture pattern and 103 checksum, flags to mark the beginning and end of the logical stream, 104 and a 'granule position' that represents an absolute position in the 105 stream, to aid seeking. A single page can contain up to 65,025 106 octets of packet data from up to 255 different packets. Packets MAY 107 be split arbitrarily across pages, and continued from one page to the 108 next (allowing packets much larger than would fit on a single page). 109 Each page contains 'lacing values' that indicate how the data is 110 partitioned into packets, allowing a demuxer to recover the packet 111 boundaries without examining the encoded data. A packet is said to 112 'complete' on a page when the page contains the final lacing value 113 corresponding to that packet. 115 This encapsulation defines the contents of the packet data, including 116 the necessary headers, the organization of those packets into a 117 logical stream, and the interpretation of the codec-specific granule 118 position field. It does not attempt to describe or specify the 119 existing Ogg container format. Readers unfamiliar with the basic 120 concepts mentioned above are encouraged to review the details in 121 [RFC3533]. 123 2. Terminology 125 The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", 126 "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and 127 "OPTIONAL" in this document are to be interpreted as described in 128 [RFC2119]. 130 Implementations that fail to satisfy one or more "MUST" requirements 131 are considered non-compliant. Implementations that satisfy all 132 "MUST" requirements, but fail to satisfy one or more "SHOULD" 133 requirements are said to be "conditionally compliant". All other 134 implementations are "unconditionally compliant". 136 3. Packet Organization 138 An Ogg Opus stream is organized as follows. 140 There are two mandatory header packets. The granule position of the 141 pages on which these packets complete MUST be zero. 143 The first packet in the logical Ogg bitstream MUST contain the 144 identification (ID) header, which uniquely identifies a stream as 145 Opus audio. The format of this header is defined in Section 5.1. It 146 MUST be placed alone (without any other packet data) on the first 147 page of the logical Ogg bitstream, and MUST complete on that page. 148 This page MUST have its 'beginning of stream' flag set. 150 The second packet in the logical Ogg bitstream MUST contain the 151 comment header, which contains user-supplied metadata. The format of 152 this header is defined in Section 5.2. It MAY span one or more 153 pages, beginning on the second page of the logical stream. However 154 many pages it spans, the comment header packet MUST finish the page 155 on which it completes. 157 All subsequent pages are audio data pages, and the Ogg packets they 158 contain are audio data packets. Each audio data packet contains one 159 Opus packet for each of N different streams, where N is typically one 160 for mono or stereo, but MAY be greater than one for multichannel 161 audio. The value N is specified in the ID header (see 162 Section 5.1.1), and is fixed over the entire length of the logical 163 Ogg bitstream. 165 The first N-1 Opus packets, if any, are packed one after another into 166 the Ogg packet, using the self-delimiting framing from Appendix B of 167 [RFC6716]. The remaining Opus packet is packed at the end of the Ogg 168 packet using the regular, undelimited framing from Section 3 of 169 [RFC6716]. All of the Opus packets in a single Ogg packet MUST be 170 constrained to have the same duration. A decoder SHOULD treat any 171 Opus packet whose duration is different from that of the first Opus 172 packet in an Ogg packet as if it were a malformed Opus packet with an 173 invalid TOC sequence. 175 The coding mode (SILK, Hybrid, or CELT), audio bandwidth, channel 176 count, duration (frame size), and number of frames per packet, are 177 indicated in the TOC (table of contents) sequence at the beginning of 178 each Opus packet, as described in Section 3.1 of [RFC6716]. The 179 combination of mode, audio bandwidth, and frame size is referred to 180 as the configuration of an Opus packet. 182 The first audio data page SHOULD NOT have the 'continued packet' flag 183 set (which would indicate the first audio data packet is continued 184 from a previous page). Packets MUST be placed into Ogg pages in 185 order until the end of stream. Audio packets MAY span page 186 boundaries. A decoder MUST treat a zero-octet audio data packet as 187 if it were a malformed Opus packet as described in Section 3.4 188 of [RFC6716]. 190 The last page SHOULD have the 'end of stream' flag set, but 191 implementations need to be prepared to deal with truncated streams 192 that do not have a page marked 'end of stream'. The final packet on 193 the last page SHOULD NOT be a continued packet, i.e., the final 194 lacing value SHOULD be less than 255. There MUST NOT be any more 195 pages in an Opus logical bitstream after a page marked 'end of 196 stream'. 198 4. Granule Position 200 The granule position of an audio data page encodes the total number 201 of PCM samples in the stream up to and including the last fully- 202 decodable sample from the last packet completed on that page. A page 203 that is entirely spanned by a single packet (that completes on a 204 subsequent page) has no granule position, and the granule position 205 field MUST be set to the special value '-1' in two's complement. 207 The granule position of an audio data page is in units of PCM audio 208 samples at a fixed rate of 48 kHz (per channel; a stereo stream's 209 granule position does not increment at twice the speed of a mono 210 stream). It is possible to run an Opus decoder at other sampling 211 rates, but the value in the granule position field always counts 212 samples assuming a 48 kHz decoding rate, and the rest of this 213 specification makes the same assumption. 215 The duration of an Opus packet can be any multiple of 2.5 ms, up to a 216 maximum of 120 ms. This duration is encoded in the TOC sequence at 217 the beginning of each packet. The number of samples returned by a 218 decoder corresponds to this duration exactly, even for the first few 219 packets. For example, a 20 ms packet fed to a decoder running at 220 48 kHz will always return 960 samples. A demuxer can parse the TOC 221 sequence at the beginning of each Ogg packet to work backwards or 222 forwards from a packet with a known granule position (i.e., the last 223 packet completed on some page) in order to assign granule positions 224 to every packet, or even every individual sample. The one exception 225 is the last page in the stream, as described below. 227 All other pages with completed packets after the first MUST have a 228 granule position equal to the number of samples contained in packets 229 that complete on that page plus the granule position of the most 230 recent page with completed packets. This guarantees that a demuxer 231 can assign individual packets the same granule position when working 232 forwards as when working backwards. For this to work, there cannot 233 be any gaps. 235 4.1. Repairing Gaps in Real-time Streams 237 In order to support capturing a real-time stream that has lost or not 238 transmitted packets, a muxer SHOULD emit packets that explicitly 239 request the use of Packet Loss Concealment (PLC) in place of the 240 missing packets. Only gaps that are a multiple of 2.5 ms are 241 repairable, as these are the only durations that can be created by 242 packet loss or discontinuous transmission. Muxers need not handle 243 other gap sizes. Creating the necessary packets involves 244 synthesizing a TOC byte (defined in Section 3.1 of [RFC6716])--and 245 whatever additional internal framing is needed--to indicate the 246 packet duration for each stream. The actual length of each missing 247 Opus frame inside the packet is zero bytes, as defined in 248 Section 3.2.1 of [RFC6716]. 250 Zero-byte frames MAY be packed into packets using any of codes 0, 1, 251 2, or 3. When successive frames have the same configuration, the 252 higher code packings reduce overhead. Likewise, if the TOC 253 configuration matches, the muxer MAY further combine the empty frames 254 with previous or subsequent non-zero-length frames (using code 2 or 255 VBR code 3). 257 [RFC6716] does not impose any requirements on the PLC, but this 258 section outlines choices that are expected to have a positive 259 influence on most PLC implementations, including the reference 260 implementation. Synthesized TOC sequences SHOULD maintain the same 261 mode, audio bandwidth, channel count, and frame size as the previous 262 packet (if any). This is the simplest and usually the most well- 263 tested case for the PLC to handle and it covers all losses that do 264 not include a configuration switch, as defined in Section 4.5 265 of [RFC6716]. 267 When a previous packet is available, keeping the audio bandwidth and 268 channel count the same allows the PLC to provide maximum continuity 269 in the concealment data it generates. However, if the size of the 270 gap is not a multiple of the most recent frame size, then the frame 271 size will have to change for at least some frames. Such changes 272 SHOULD be delayed as long as possible to simplify things for PLC 273 implementations. 275 As an example, a 95 ms gap could be encoded as nineteen 5 ms frames 276 in two bytes with a single CBR code 3 packet. If the previous frame 277 size was 20 ms, using four 20 ms frames followed by three 5 ms frames 278 requires 4 bytes (plus an extra byte of Ogg lacing overhead), but 279 allows the PLC to use its well-tested steady state behavior for as 280 long as possible. The total bitrate of the latter approach, 281 including Ogg overhead, is about 0.4 kbps, so the impact on file size 282 is minimal. 284 Changing modes is discouraged, since this causes some decoder 285 implementations to reset their PLC state. However, SILK and Hybrid 286 mode frames cannot fill gaps that are not a multiple of 10 ms. If 287 switching to CELT mode is needed to match the gap size, a muxer 288 SHOULD do so at the end of the gap to allow the PLC to function for 289 as long as possible. 291 In the example above, if the previous frame was a 20 ms SILK mode 292 frame, the better solution is to synthesize a packet describing four 293 20 ms SILK frames, followed by a packet with a single 10 ms SILK 294 frame, and finally a packet with a 5 ms CELT frame, to fill the 95 ms 295 gap. This also requires four bytes to describe the synthesized 296 packet data (two bytes for a CBR code 3 and one byte each for two 297 code 0 packets) but three bytes of Ogg lacing overhead are needed to 298 mark the packet boundaries. At 0.6 kbps, this is still a minimal 299 bitrate impact over a naive, low quality solution. 301 Since medium-band audio is an option only in the SILK mode, wideband 302 frames SHOULD be generated if switching from that configuration to 303 CELT mode, to ensure that any PLC implementation which does try to 304 migrate state between the modes will be able to preserve all of the 305 available audio bandwidth. 307 4.2. Pre-skip 309 There is some amount of latency introduced during the decoding 310 process, to allow for overlap in the CELT mode, stereo mixing in the 311 SILK mode, and resampling. The encoder might have introduced 312 additional latency through its own resampling and analysis (though 313 the exact amount is not specified). Therefore, the first few samples 314 produced by the decoder do not correspond to real input audio, but 315 are instead composed of padding inserted by the encoder to compensate 316 for this latency. These samples need to be stored and decoded, as 317 Opus is an asymptotically convergent predictive codec, meaning the 318 decoded contents of each frame depend on the recent history of 319 decoder inputs. However, a decoder will want to skip these samples 320 after decoding them. 322 A 'pre-skip' field in the ID header (see Section 5.1) signals the 323 number of samples which SHOULD be skipped (decoded but discarded) at 324 the beginning of the stream. This amount need not be a multiple of 325 2.5 ms, MAY be smaller than a single packet, or MAY span the contents 326 of several packets. These samples are not valid audio, and SHOULD 327 NOT be played. 329 For example, if the first Opus frame uses the CELT mode, it will 330 always produce 120 samples of windowed overlap-add data. However, 331 the overlap data is initially all zeros (since there is no prior 332 frame), meaning this cannot, in general, accurately represent the 333 original audio. The SILK mode requires additional delay to account 334 for its analysis and resampling latency. The encoder delays the 335 original audio to avoid this problem. 337 The pre-skip field MAY also be used to perform sample-accurate 338 cropping of already encoded streams. In this case, a value of at 339 least 3840 samples (80 ms) provides sufficient history to the decoder 340 that it will have converged before the stream's output begins. 342 4.3. PCM Sample Position 344 The PCM sample position is determined from the granule position using 345 the formula 347 'PCM sample position' = 'granule position' - 'pre-skip' . 349 For example, if the granule position of the first audio data page is 350 59,971, and the pre-skip is 11,971, then the PCM sample position of 351 the last decoded sample from that page is 48,000. 353 This can be converted into a playback time using the formula 355 'PCM sample position' 356 'playback time' = --------------------- . 357 48000.0 359 The initial PCM sample position before any samples are played is 360 normally '0'. In this case, the PCM sample position of the first 361 audio sample to be played starts at '1', because it marks the time on 362 the clock _after_ that sample has been played, and a stream that is 363 exactly one second long has a final PCM sample position of '48000', 364 as in the example here. 366 Vorbis streams use a granule position smaller than the number of 367 audio samples contained in the first audio data page to indicate that 368 some of those samples are trimmed from the output (see 369 [vorbis-trim]). However, to do so, Vorbis requires that the first 370 audio data page contains exactly two packets, in order to allow the 371 decoder to perform PCM position adjustments before needing to return 372 any PCM data. Opus uses the pre-skip mechanism for this purpose 373 instead, since the encoder MAY introduce more than a single packet's 374 worth of latency, and since very large packets in streams with a very 375 large number of channels might not fit on a single page. 377 4.4. End Trimming 379 The page with the 'end of stream' flag set MAY have a granule 380 position that indicates the page contains less audio data than would 381 normally be returned by decoding up through the final packet. This 382 is used to end the stream somewhere other than an even frame 383 boundary. The granule position of the most recent audio data page 384 with completed packets is used to make this determination, or '0' is 385 used if there were no previous audio data pages with a completed 386 packet. The difference between these granule positions indicates how 387 many samples to keep after decoding the packets that completed on the 388 final page. The remaining samples are discarded. The number of 389 discarded samples SHOULD be no larger than the number decoded from 390 the last packet. 392 4.5. Restrictions on the Initial Granule Position 394 The granule position of the first audio data page with a completed 395 packet MAY be larger than the number of samples contained in packets 396 that complete on that page, however it MUST NOT be smaller, unless 397 that page has the 'end of stream' flag set. Allowing a granule 398 position larger than the number of samples allows the beginning of a 399 stream to be cropped or a live stream to be joined without rewriting 400 the granule position of all the remaining pages. This means that the 401 PCM sample position just before the first sample to be played MAY be 402 larger than '0'. Synchronization when multiplexing with other 403 logical streams still uses the PCM sample position relative to '0' to 404 compute sample times. This does not affect the behavior of pre-skip: 405 exactly 'pre-skip' samples SHOULD be skipped from the beginning of 406 the decoded output, even if the initial PCM sample position is 407 greater than zero. 409 On the other hand, a granule position that is smaller than the number 410 of decoded samples prevents a demuxer from working backwards to 411 assign each packet or each individual sample a valid granule 412 position, since granule positions are non-negative. A decoder MUST 413 reject as invalid any stream where the granule position is smaller 414 than the number of samples contained in packets that complete on the 415 first audio data page with a completed packet, unless that page has 416 the 'end of stream' flag set. It MAY defer this action until it 417 decodes the last packet completed on that page. 419 If that page has the 'end of stream' flag set, a demuxer MUST reject 420 as invalid any stream where its granule position is smaller than the 421 'pre-skip' amount. This would indicate that there are more samples 422 to be skipped from the initial decoded output than exist in the 423 stream. If the granule position is smaller than the number of 424 decoded samples produced by the packets that complete on that page, 425 then a demuxer MUST use an initial granule position of '0', and can 426 work forwards from '0' to timestamp individual packets. If the 427 granule position is larger than the number of decoded samples 428 available, then the demuxer MUST still work backwards as described 429 above, even if the 'end of stream' flag is set, to determine the 430 initial granule position, and thus the initial PCM sample position. 431 Both of these will be greater than '0' in this case. 433 4.6. Seeking and Pre-roll 435 Seeking in Ogg files is best performed using a bisection search for a 436 page whose granule position corresponds to a PCM position at or 437 before the seek target. With appropriately weighted bisection, 438 accurate seeking can be performed with just three or four bisections 439 even in multi-gigabyte files. See [seeking] for general 440 implementation guidance. 442 When seeking within an Ogg Opus stream, the decoder SHOULD start 443 decoding (and discarding the output) at least 3840 samples (80 ms) 444 prior to the seek target in order to ensure that the output audio is 445 correct by the time it reaches the seek target. This 'pre-roll' is 446 separate from, and unrelated to, the 'pre-skip' used at the beginning 447 of the stream. If the point 80 ms prior to the seek target comes 448 before the initial PCM sample position, the decoder SHOULD start 449 decoding from the beginning of the stream, applying pre-skip as 450 normal, regardless of whether the pre-skip is larger or smaller than 451 80 ms, and then continue to discard samples to reach the seek target 452 (if any). 454 5. Header Packets 456 An Opus stream contains exactly two mandatory header packets: an 457 identification header and a comment header. 459 5.1. Identification Header 460 0 1 2 3 461 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 462 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 463 | 'O' | 'p' | 'u' | 's' | 464 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 465 | 'H' | 'e' | 'a' | 'd' | 466 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 467 | Version = 1 | Channel Count | Pre-skip | 468 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 469 | Input Sample Rate (Hz) | 470 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 471 | Output Gain (Q7.8 in dB) | Mapping Family| | 472 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ : 473 | | 474 : Optional Channel Mapping Table... : 475 | | 476 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 478 Figure 1: ID Header Packet 480 The fields in the identification (ID) header have the following 481 meaning: 483 1. *Magic Signature*: 485 This is an 8-octet (64-bit) field that allows codec 486 identification and is human-readable. It contains, in order, the 487 magic numbers: 489 0x4F 'O' 491 0x70 'p' 493 0x75 'u' 495 0x73 's' 497 0x48 'H' 499 0x65 'e' 501 0x61 'a' 503 0x64 'd' 505 Starting with "Op" helps distinguish it from audio data packets, 506 as this is an invalid TOC sequence. 508 2. *Version* (8 bits, unsigned): 510 The version number MUST always be '1' for this version of the 511 encapsulation specification. Implementations SHOULD treat 512 streams where the upper four bits of the version number match 513 that of a recognized specification as backwards-compatible with 514 that specification. That is, the version number can be split 515 into "major" and "minor" version sub-fields, with changes to the 516 "minor" sub-field (in the lower four bits) signaling compatible 517 changes. For example, a decoder implementing this specification 518 SHOULD accept any stream with a version number of '15' or less, 519 and SHOULD assume any stream with a version number '16' or 520 greater is incompatible. The initial version '1' was chosen to 521 keep implementations from relying on this octet as a null 522 terminator for the "OpusHead" string. 524 3. *Output Channel Count* 'C' (8 bits, unsigned): 526 This is the number of output channels. This might be different 527 than the number of encoded channels, which can change on a 528 packet-by-packet basis. This value MUST NOT be zero. The 529 maximum allowable value depends on the channel mapping family, 530 and might be as large as 255. See Section 5.1.1 for details. 532 4. *Pre-skip* (16 bits, unsigned, little endian): 534 This is the number of samples (at 48 kHz) to discard from the 535 decoder output when starting playback, and also the number to 536 subtract from a page's granule position to calculate its PCM 537 sample position. When cropping the beginning of existing Ogg 538 Opus streams, a pre-skip of at least 3,840 samples (80 ms) is 539 RECOMMENDED to ensure complete convergence in the decoder. 541 5. *Input Sample Rate* (32 bits, unsigned, little endian): 543 This field is _not_ the sample rate to use for playback of the 544 encoded data. 546 Opus can switch between internal audio bandwidths of 4, 6, 8, 12, 547 and 20 kHz. Each packet in the stream can have a different audio 548 bandwidth. Regardless of the audio bandwidth, the reference 549 decoder supports decoding any stream at a sample rate of 8, 12, 550 16, 24, or 48 kHz. The original sample rate of the encoder input 551 is not preserved by the lossy compression. 553 An Ogg Opus player SHOULD select the playback sample rate 554 according to the following procedure: 556 1. If the hardware supports 48 kHz playback, decode at 48 kHz. 558 2. Otherwise, if the hardware's highest available sample rate is 559 a supported rate, decode at this sample rate. 561 3. Otherwise, if the hardware's highest available sample rate is 562 less than 48 kHz, decode at the next highest supported rate 563 above this and resample. 565 4. Otherwise, decode at 48 kHz and resample. 567 However, the 'Input Sample Rate' field allows the encoder to pass 568 the sample rate of the original input stream as metadata. This 569 is useful when the user requires the output sample rate to match 570 the input sample rate. For example, a non-player decoder writing 571 PCM format samples to disk might choose to resample the output 572 audio back to the original input sample rate to reduce surprise 573 to the user, who might reasonably expect to get back a file with 574 the same sample rate as the one they fed to the encoder. 576 A value of zero indicates 'unspecified'. Encoders SHOULD write 577 the actual input sample rate or zero, but decoder implementations 578 which do something with this field SHOULD take care to behave 579 sanely if given crazy values (e.g., do not actually upsample the 580 output to 10 MHz if requested). 582 6. *Output Gain* (16 bits, signed, little endian): 584 This is a gain to be applied by the decoder. It is 20*log10 of 585 the factor to scale the decoder output by to achieve the desired 586 playback volume, stored in a 16-bit, signed, two's complement 587 fixed-point value with 8 fractional bits (i.e., Q7.8). 589 To apply the gain, a decoder could use 591 sample *= pow(10, output_gain/(20.0*256)) , 593 where output_gain is the raw 16-bit value from the header. 595 Virtually all players and media frameworks SHOULD apply it by 596 default. If a player chooses to apply any volume adjustment or 597 gain modification, such as the R128_TRACK_GAIN (see Section 5.2), 598 the adjustment MUST be applied in addition to this output gain in 599 order to achieve playback at the normalized volume. 601 An encoder SHOULD set this field to zero, and instead apply any 602 gain prior to encoding, when this is possible and does not 603 conflict with the user's wishes. A nonzero output gain indicates 604 the gain was adjusted after encoding, or that a user wished to 605 adjust the gain for playback while preserving the ability to 606 recover the original signal amplitude. 608 Although the output gain has enormous range (+/- 128 dB, enough 609 to amplify inaudible sounds to the threshold of physical pain), 610 most applications can only reasonably use a small portion of this 611 range around zero. The large range serves in part to ensure that 612 gain can always be losslessly transferred between OpusHead and 613 R128 gain tags (see below) without saturating. 615 7. *Channel Mapping Family* (8 bits, unsigned): 617 This octet indicates the order and semantic meaning of the output 618 channels. 620 Each possible value of this octet indicates a mapping family, 621 which defines a set of allowed channel counts, and the ordered 622 set of channel names for each allowed channel count. The details 623 are described in Section 5.1.1. 625 8. *Channel Mapping Table*: This table defines the mapping from 626 encoded streams to output channels. It is omitted when the 627 channel mapping family is 0, but REQUIRED otherwise. Its 628 contents are specified in Section 5.1.1. 630 All fields in the ID headers are REQUIRED, except for the channel 631 mapping table, which is omitted when the channel mapping family is 0. 632 Implementations SHOULD reject ID headers which do not contain enough 633 data for these fields, even if they contain a valid Magic Signature. 634 Future versions of this specification, even backwards-compatible 635 versions, might include additional fields in the ID header. If an ID 636 header has a compatible major version, but a larger minor version, an 637 implementation MUST NOT reject it for containing additional data not 638 specified here. However, implementations MAY reject streams in which 639 the ID header does not complete on the first page. 641 5.1.1. Channel Mapping 643 An Ogg Opus stream allows mapping one number of Opus streams (N) to a 644 possibly larger number of decoded channels (M+N) to yet another 645 number of output channels (C), which might be larger or smaller than 646 the number of decoded channels. The order and meaning of these 647 channels are defined by a channel mapping, which consists of the 648 'channel mapping family' octet and, for channel mapping families 649 other than family 0, a channel mapping table, as illustrated in 650 Figure 2. 652 0 1 2 3 653 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 654 +-+-+-+-+-+-+-+-+ 655 | Stream Count | 656 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 657 | Coupled Count | Channel Mapping... : 658 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 660 Figure 2: Channel Mapping Table 662 The fields in the channel mapping table have the following meaning: 664 1. *Stream Count* 'N' (8 bits, unsigned): 666 This is the total number of streams encoded in each Ogg packet. 667 This value is necessary to correctly parse the packed Opus 668 packets inside an Ogg packet, as described in Section 3. This 669 value MUST NOT be zero, as without at least one Opus packet with 670 a valid TOC sequence, a demuxer cannot recover the duration of an 671 Ogg packet. 673 For channel mapping family 0, this value defaults to 1, and is 674 not coded. 676 2. *Coupled Stream Count* 'M' (8 bits, unsigned): This is the number 677 of streams whose decoders are to be configured to produce two 678 channels. This MUST be no larger than the total number of 679 streams, N. 681 Each packet in an Opus stream has an internal channel count of 1 682 or 2, which can change from packet to packet. This is selected 683 by the encoder depending on the bitrate and the audio being 684 encoded. The original channel count of the encoder input is not 685 preserved by the lossy compression. 687 Regardless of the internal channel count, any Opus stream can be 688 decoded as mono (a single channel) or stereo (two channels) by 689 appropriate initialization of the decoder. The 'coupled stream 690 count' field indicates that the first M Opus decoders are to be 691 initialized for stereo output, and the remaining N-M decoders are 692 to be initialized for mono only. The total number of decoded 693 channels, (M+N), MUST be no larger than 255, as there is no way 694 to index more channels than that in the channel mapping. 696 For channel mapping family 0, this value defaults to C-1 (i.e., 0 697 for mono and 1 for stereo), and is not coded. 699 3. *Channel Mapping* (8*C bits): This contains one octet per output 700 channel, indicating which decoded channel is to be used for each 701 one. Let 'index' be the value of this octet for a particular 702 output channel. This value MUST either be smaller than (M+N), or 703 be the special value 255. If 'index' is less than 2*M, the 704 output MUST be taken from decoding stream ('index'/2) as stereo 705 and selecting the left channel if 'index' is even, and the right 706 channel if 'index' is odd. If 'index' is 2*M or larger, but less 707 than 255, the output MUST be taken from decoding stream 708 ('index'-M) as mono. If 'index' is 255, the corresponding output 709 channel MUST contain pure silence. 711 The number of output channels, C, is not constrained to match the 712 number of decoded channels (M+N). A single index value MAY 713 appear multiple times, i.e., the same decoded channel might be 714 mapped to multiple output channels. Some decoded channels might 715 not be assigned to any output channel, as well. 717 For channel mapping family 0, the first index defaults to 0, and 718 if C==2, the second index defaults to 1. Neither index is coded. 720 After producing the output channels, the channel mapping family 721 determines the semantic meaning of each one. There are three defined 722 mapping families in this specification. 724 5.1.1.1. Channel Mapping Family 0 726 Allowed numbers of channels: 1 or 2. RTP mapping. 728 o 1 channel: monophonic (mono). 730 o 2 channels: stereo (left, right). 732 *Special mapping*: This channel mapping value also indicates that the 733 contents consists of a single Opus stream that is stereo if and only 734 if C==2, with stream index 0 mapped to output channel 0 (mono, or 735 left channel) and stream index 1 mapped to output channel 1 (right 736 channel) if stereo. When the 'channel mapping family' octet has this 737 value, the channel mapping table MUST be omitted from the ID header 738 packet. 740 5.1.1.2. Channel Mapping Family 1 742 Allowed numbers of channels: 1...8. Vorbis channel order. 744 Each channel is assigned to a speaker location in a conventional 745 surround arrangement. Specific locations depend on the number of 746 channels, and are given below in order of the corresponding channel 747 indicies. 749 o 1 channel: monophonic (mono). 751 o 2 channels: stereo (left, right). 753 o 3 channels: linear surround (left, center, right) 755 o 4 channels: quadraphonic (front left, front right, rear left, 756 rear right). 758 o 5 channels: 5.0 surround (front left, front center, front right, 759 rear left, rear right). 761 o 6 channels: 5.1 surround (front left, front center, front right, 762 rear left, rear right, LFE). 764 o 7 channels: 6.1 surround (front left, front center, front right, 765 side left, side right, rear center, LFE). 767 o 8 channels: 7.1 surround (front left, front center, front right, 768 side left, side right, rear left, rear right, LFE) 770 This set of surround options and speaker location orderings is the 771 same as those used by the Vorbis codec [vorbis-mapping]. The 772 ordering is different from the one used by the WAVE 773 [wave-multichannel] and FLAC [flac] formats, so correct ordering 774 requires permutation of the output channels when decoding to or 775 encoding from those formats. 'LFE' here refers to a Low Frequency 776 Effects, often mapped to a subwoofer with no particular spatial 777 position. Implementations SHOULD identify 'side' or 'rear' speaker 778 locations with 'surround' and 'back' as appropriate when interfacing 779 with audio formats or systems which prefer that terminology. 781 5.1.1.3. Channel Mapping Family 255 783 Allowed numbers of channels: 1...255. No defined channel meaning. 785 Channels are unidentified. General-purpose players SHOULD NOT 786 attempt to play these streams, and offline decoders MAY deinterleave 787 the output into separate PCM files, one per channel. Decoders SHOULD 788 NOT produce output for channels mapped to stream index 255 (pure 789 silence) unless they have no other way to indicate the index of non- 790 silent channels. 792 5.1.1.4. Undefined Channel Mappings 794 The remaining channel mapping families (2...254) are reserved. A 795 decoder encountering a reserved channel mapping family value SHOULD 796 act as though the value is 255. 798 5.1.1.5. Downmixing 800 An Ogg Opus player MUST play any Ogg Opus stream with a channel 801 mapping family of 0 or 1, even if the number of channels does not 802 match the physically connected audio hardware. Players SHOULD 803 perform channel mixing to increase or reduce the number of channels 804 as needed. 806 Implementations MAY use the following matricies to implement 807 downmixing from multichannel files using Channel Mapping Family 1 808 (Section 5.1.1.2), which are known to give acceptable results for 809 stereo. Matricies for 3 and 4 channels are normalized so each 810 coefficent row sums to 1 to avoid clipping. For 5 or more channels 811 they are normalized to 2 as a compromise between clipping and dynamic 812 range reduction. 814 In these matricies the front left and front right channels are 815 generally passed through directly. When a surround channel is split 816 between both the left and right stereo channels, coefficients are 817 chosen so their squares sum to 1, which helps preserve the perceived 818 intensity. Rear channels are mixed more diffusely or attenuated to 819 maintain focus on the front channels. 821 L output = ( 0.585786 * left + 0.414214 * center ) 822 R output = ( 0.414214 * center + 0.585786 * right ) 824 Exact coefficient values are 1 and 1/sqrt(2), multiplied by 1/(1 + 1/ 825 sqrt(2)) for normalization. 827 Figure 3: Stereo downmix matrix for the linear surround channel 828 mapping 830 / \ / \ / FL \ 831 | L output | | 0.422650 0.000000 0.366025 0.211325 | | FR | 832 | R output | = | 0.000000 0.422650 0.211325 0.366025 | | RL | 833 \ / \ / \ RR / 835 Exact coefficient values are 1, sqrt(3)/2 and 1/2, multiplied by 1/ 836 (1 + sqrt(3)/2 + 1/2) for normalization. 838 Figure 4: Stereo downmix matrix for the quadraphonic channel mapping 840 / FL \ 841 / \ / \ | FC | 842 | L | | 0.650802 0.460186 0.000000 0.563611 0.325401 | | FR | 843 | R | = | 0.000000 0.460186 0.650802 0.325401 0.563611 | | RL | 844 \ / \ / | RR | 845 \ / 847 Exact coefficient values are 1, 1/sqrt(2), sqrt(3)/2 and 1/2, 848 multiplied by 2/(1 + 1/sqrt(2) + sqrt(3)/2 + 1/2) for normalization. 850 Figure 5: Stereo downmix matrix for the 5.0 surround mapping 852 /FL \ 853 / \ / \ |FC | 854 |L| | 0.529067 0.374107 0.000000 0.458186 0.264534 0.374107 | |FR | 855 |R| = | 0.000000 0.374107 0.529067 0.264534 0.458186 0.374107 | |RL | 856 \ / \ / |RR | 857 \LFE/ 859 Exact coefficient values are 1, 1/sqrt(2), sqrt(3)/2 and 1/2, 860 multiplied by 2/(1 + 1/sqrt(2) + sqrt(3)/2 + 1/2 + 1/sqrt(2)) for 861 normalization. 863 Figure 6: Stereo downmix matrix for the 5.1 surround mapping 865 / \ 866 | 0.455310 0.321953 0.000000 0.394310 0.227655 0.278819 0.321953 | 867 | 0.000000 0.321953 0.455310 0.227655 0.394310 0.278819 0.321953 | 868 \ / 870 Exact coefficient values are 1, 1/sqrt(2), sqrt(3)/2, 1/2 and sqrt(3) 871 /2/sqrt(2), multiplied by 2/(1 + 1/sqrt(2) + sqrt(3)/2 + 1/2 + 872 sqrt(3)/2/sqrt(2) + 1/sqrt(2)) for normalization. The coeffients are 873 in the same order as in Section 5.1.1.2, and the matricies above. 875 Figure 7: Stereo downmix matrix for the 6.1 surround mapping 877 / \ 878 | .388631 .274804 .000000 .336565 .194316 .336565 .194316 .274804 | 879 | .000000 .274804 .388631 .194316 .336565 .194316 .336565 .274804 | 880 \ / 882 Exact coefficient values are 1, 1/sqrt(2), sqrt(3)/2 and 1/2, 883 multiplied by 2/(2 + 2/sqrt(2) + sqrt(3)) for normalization. The 884 coeffients are in the same order as in Section 5.1.1.2, and the 885 matricies above. 887 Figure 8: Stereo downmix matrix for the 7.1 surround mapping 889 5.2. Comment Header 891 0 1 2 3 892 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 893 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 894 | 'O' | 'p' | 'u' | 's' | 895 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 896 | 'T' | 'a' | 'g' | 's' | 897 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 898 | Vendor String Length | 899 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 900 | | 901 : Vendor String... : 902 | | 903 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 904 | User Comment List Length | 905 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 906 | User Comment #0 String Length | 907 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 908 | | 909 : User Comment #0 String... : 910 | | 911 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 912 | User Comment #1 String Length | 913 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 914 : : 916 Figure 9: Comment Header Packet 918 The comment header consists of a 64-bit magic signature, followed by 919 data in the same format as the [vorbis-comment] header used in Ogg 920 Vorbis, except (like Ogg Theora and Speex) the final "framing bit" 921 specified in the Vorbis spec is not present. 923 1. *Magic Signature*: 925 This is an 8-octet (64-bit) field that allows codec 926 identification and is human-readable. It contains, in order, the 927 magic numbers: 929 0x4F 'O' 931 0x70 'p' 933 0x75 'u' 935 0x73 's' 937 0x54 'T' 939 0x61 'a' 941 0x67 'g' 943 0x73 's' 945 Starting with "Op" helps distinguish it from audio data packets, 946 as this is an invalid TOC sequence. 948 2. *Vendor String Length* (32 bits, unsigned, little endian): 950 This field gives the length of the following vendor string, in 951 octets. It MUST NOT indicate that the vendor string is longer 952 than the rest of the packet. 954 3. *Vendor String* (variable length, UTF-8 vector): 956 This is a simple human-readable tag for vendor information, 957 encoded as a UTF-8 string [RFC3629]. No terminating null octet 958 is necessary. 960 This tag is intended to identify the codec encoder and 961 encapsulation implementations, for tracing differences in 962 technical behavior. User-facing encoding applications can use 963 the 'ENCODER' user comment tag to identify themselves. 965 4. *User Comment List Length* (32 bits, unsigned, little endian): 967 This field indicates the number of user-supplied comments. It 968 MAY indicate there are zero user-supplied comments, in which case 969 there are no additional fields in the packet. It MUST NOT 970 indicate that there are so many comments that the comment string 971 lengths would require more data than is available in the rest of 972 the packet. 974 5. *User Comment #i String Length* (32 bits, unsigned, little 975 endian): 977 This field gives the length of the following user comment string, 978 in octets. There is one for each user comment indicated by the 979 'user comment list length' field. It MUST NOT indicate that the 980 string is longer than the rest of the packet. 982 6. *User Comment #i String* (variable length, UTF-8 vector): 984 This field contains a single user comment string. There is one 985 for each user comment indicated by the 'user comment list length' 986 field. 988 The vendor string length and user comment list length are REQUIRED, 989 and implementations SHOULD reject comment headers that do not contain 990 enough data for these fields, or that do not contain enough data for 991 the corresponding vendor string or user comments they describe. 992 Making this check before allocating the associated memory to contain 993 the data helps prevent a possible Denial-of-Service (DoS) attack from 994 small comment headers that claim to contain strings longer than the 995 entire packet or more user comments than than could possibly fit in 996 the packet. 998 Immediately following the user comment list, the comment header MAY 999 contain zero-padding or other binary data which is not specified 1000 here. If the least-significant bit of the first byte of this data is 1001 1, then editors SHOULD preserve the contents of this data when 1002 updating the tags, but if this bit is 0, all such data MAY be treated 1003 as padding, and truncated or discarded as desired. 1005 5.2.1. Tag Definitions 1007 The user comment strings follow the NAME=value format described by 1008 [vorbis-comment] with the same recommended tag names: ARTIST, TITLE, 1009 DATE, ALBUM, and so on. 1011 Two new comment tags are introduced here: 1013 An optional gain for track nomalization 1015 R128_TRACK_GAIN=-573 1017 representing the volume shift needed to normalize the track's volume 1018 during isolated playback, in random shuffle, and so on. The gain is 1019 a Q7.8 fixed point number in dB, as in the ID header's 'output gain' 1020 field. 1022 This tag is similar to the REPLAYGAIN_TRACK_GAIN tag in 1023 Vorbis [replay-gain], except that the normal volume reference is the 1024 [EBU-R128] standard. 1026 An optional gain for album nomalization 1028 R128_ALBUM_GAIN=111 1030 representing the volume shift needed to normalize the overall volume 1031 when played as part of a particular collection of tracks. The gain 1032 is also a Q7.8 fixed point number in dB, as in the ID header's 1033 'output gain' field. 1035 An Ogg Opus stream MUST NOT have more than one of each tag, and if 1036 present their values MUST be an integer from -32768 to 32767, 1037 inclusive, represented in ASCII as a base 10 number with no 1038 whitespace. A leading '+' or '-' character is valid. Leading zeros 1039 are also permitted, but the value MUST be represented by no more than 1040 6 characters. Other non-digit characters MUST NOT be present. 1042 If present, R128_TRACK_GAIN and R128_ALBUM_GAIN MUST correctly 1043 represent the R128 normalization gain relative to the 'output gain' 1044 field specified in the ID header. If a player chooses to make use of 1045 the R128_TRACK_GAIN tag or the R128_ALBUM_GAIN tag, it MUST apply 1046 those gains _in addition_ to the 'output gain' value. If a tool 1047 modifies the ID header's 'output gain' field, it MUST also update or 1048 remove the R128_TRACK_GAIN and R128_ALBUM_GAIN comment tags if 1049 present. An encoder SHOULD assume that by default tools will respect 1050 the 'output gain' field, and not the comment tag. 1052 To avoid confusion with multiple normalization schemes, an Opus 1053 comment header SHOULD NOT contain any of the REPLAYGAIN_TRACK_GAIN, 1054 REPLAYGAIN_TRACK_PEAK, REPLAYGAIN_ALBUM_GAIN, or 1055 REPLAYGAIN_ALBUM_PEAK tags. [EBU-R128] normalization is preferred to 1056 the earlier REPLAYGAIN schemes because of its clear definition and 1057 adoption by industry. Peak normalizations are difficult to calculate 1058 reliably for lossy codecs because of variation in excursion heights 1059 due to decoder differences. In the authors' investigations they were 1060 not applied consistently or broadly enough to merit inclusion here. 1062 6. Packet Size Limits 1064 Technically, valid Opus packets can be arbitrarily large due to the 1065 padding format, although the amount of non-padding data they can 1066 contain is bounded. These packets might be spread over a similarly 1067 enormous number of Ogg pages. Encoders SHOULD use no more padding 1068 than is necessary to make a variable bitrate (VBR) stream constant 1069 bitrate (CBR). Decoders SHOULD avoid attempting to allocate 1070 excessive amounts of memory when presented with a very large packet. 1071 The presence of an extremely large packet in the stream could 1072 indicate a memory exhaustion attack or stream corruption. Decoders 1073 SHOULD reject a packet that is too large to process, and display a 1074 warning message. 1076 In an Ogg Opus stream, the largest possible valid packet that does 1077 not use padding has a size of (61,298*N - 2) octets, or about 60 kB 1078 per Opus stream. With 255 streams, this is 15,630,988 octets 1079 (14.9 MB) and can span up to 61,298 Ogg pages, all but one of which 1080 will have a granule position of -1. This is of course a very extreme 1081 packet, consisting of 255 streams, each containing 120 ms of audio 1082 encoded as 2.5 ms frames, each frame using the maximum possible 1083 number of octets (1275) and stored in the least efficient manner 1084 allowed (a VBR code 3 Opus packet). Even in such a packet, most of 1085 the data will be zeros as 2.5 ms frames cannot actually use all 1086 1275 octets. The largest packet consisting of entirely useful data 1087 is (15,326*N - 2) octets, or about 15 kB per stream. This 1088 corresponds to 120 ms of audio encoded as 10 ms frames in either SILK 1089 or Hybrid mode, but at a data rate of over 1 Mbps, which makes little 1090 sense for the quality achieved. A more reasonable limit is 1091 (7,664*N - 2) octets, or about 7.5 kB per stream. This corresponds 1092 to 120 ms of audio encoded as 20 ms stereo CELT mode frames, with a 1093 total bitrate just under 511 kbps (not counting the Ogg encapsulation 1094 overhead). With N=8, the maximum number of channels currently 1095 defined by mapping family 1, this gives a maximum packet size of 1096 61,310 octets, or just under 60 kB. This is still quite 1097 conservative, as it assumes each output channel is taken from one 1098 decoded channel of a stereo packet. An implementation could 1099 reasonably choose any of these numbers for its internal limits. 1101 7. Encoder Guidelines 1103 When encoding Opus streams, Ogg muxers SHOULD take into account the 1104 algorithmic delay of the Opus encoder. 1106 In encoders derived from the reference implementation, the number of 1107 samples can be queried with: 1109 opus_encoder_ctl(encoder_state, OPUS_GET_LOOKAHEAD(&delay_samples)); 1111 To achieve good quality in the very first samples of a stream, the 1112 Ogg encoder MAY use linear predictive coding (LPC) extrapolation 1113 [linear-prediction] to generate at least 120 extra samples at the 1114 beginning to avoid the Opus encoder having to encode a discontinuous 1115 signal. For an input file containing 'length' samples, the Ogg 1116 encoder SHOULD set the pre-skip header value to 1117 delay_samples+extra_samples, encode at least 1118 length+delay_samples+extra_samples samples, and set the granulepos of 1119 the last page to length+delay_samples+extra_samples. This ensures 1120 that the encoded file has the same duration as the original, with no 1121 time offset. The best way to pad the end of the stream is to also 1122 use LPC extrapolation, but zero-padding is also acceptable. 1124 7.1. LPC Extrapolation 1126 The first step in LPC extrapolation is to compute linear prediction 1127 coefficients. [lpc-sample] When extending the end of the signal, 1128 order-N (typically with N ranging from 8 to 40) LPC analysis is 1129 performed on a window near the end of the signal. The last N samples 1130 are used as memory to an infinite impulse response (IIR) filter. 1132 The filter is then applied on a zero input to extrapolate the end of 1133 the signal. Let a(k) be the kth LPC coefficient and x(n) be the nth 1134 sample of the signal, each new sample past the end of the signal is 1135 computed as: 1137 N 1138 --- 1139 x(n) = \ a(k)*x(n-k) 1140 / 1141 --- 1142 k=1 1144 The process is repeated independently for each channel. It is 1145 possible to extend the beginning of the signal by applying the same 1146 process backward in time. When extending the beginning of the 1147 signal, it is best to apply a "fade in" to the extrapolated signal, 1148 e.g. by multiplying it by a half-Hanning window [hanning]. 1150 7.2. Continuous Chaining 1152 In some applications, such as Internet radio, it is desirable to cut 1153 a long stream into smaller chains, e.g. so the comment header can be 1154 updated. This can be done simply by separating the input streams 1155 into segments and encoding each segment independently. The drawback 1156 of this approach is that it creates a small discontinuity at the 1157 boundary due to the lossy nature of Opus. An encoder MAY avoid this 1158 discontinuity by using the following procedure: 1160 1. Encode the last frame of the first segment as an independent 1161 frame by turning off all forms of inter-frame prediction. De- 1162 emphasis is allowed. 1164 2. Set the granulepos of the last page to a point near the end of 1165 the last frame. 1167 3. Begin the second segment with a copy of the last frame of the 1168 first segment. 1170 4. Set the pre-skip value of the second stream in such a way as to 1171 properly join the two streams. 1173 5. Continue the encoding process normally from there, without any 1174 reset to the encoder. 1176 In encoders derived from the reference implementation, inter-frame 1177 prediction can be turned off by calling: 1179 opus_encoder_ctl(encoder_state, OPUS_SET_PREDICTION_DISABLED(1)); 1181 For best results, this implementation requires that prediction be 1182 explicitly enabled again before resuming normal encoding, even after 1183 a reset. 1185 8. Implementation Status 1187 A brief summary of major implementations of this draft is available 1188 at [1], along with their status. 1190 [Note to RFC Editor: please remove this entire section before final 1191 publication per [RFC6982].] 1193 9. Security Considerations 1195 Implementations of the Opus codec need to take appropriate security 1196 considerations into account, as outlined in [RFC4732]. This is just 1197 as much a problem for the container as it is for the codec itself. 1199 It is extremely important for the decoder to be robust against 1200 malicious payloads. Malicious payloads MUST NOT cause the decoder to 1201 overrun its allocated memory or to take an excessive amount of 1202 resources to decode. Although problems in encoders are typically 1203 rarer, the same applies to the encoder. Malicious audio streams MUST 1204 NOT cause the encoder to misbehave because this would allow an 1205 attacker to attack transcoding gateways. 1207 Like most other container formats, Ogg Opus streams SHOULD NOT be 1208 used with insecure ciphers or cipher modes that are vulnerable to 1209 known-plaintext attacks. Elements such as the Ogg page capture 1210 pattern and the magic signatures in the ID header and the comment 1211 header all have easily predictable values, in addition to various 1212 elements of the codec data itself. 1214 10. Content Type 1216 An "Ogg Opus file" consists of one or more sequentially multiplexed 1217 segments, each containing exactly one Ogg Opus stream. The 1218 RECOMMENDED mime-type for Ogg Opus files is "audio/ogg". 1220 If more specificity is desired, one MAY indicate the presence of Opus 1221 streams using the codecs parameter defined in [RFC6381], e.g., 1223 audio/ogg; codecs=opus 1225 for an Ogg Opus file. 1227 The RECOMMENDED filename extension for Ogg Opus files is '.opus'. 1229 When Opus is concurrently multiplexed with other streams in an Ogg 1230 container, one SHOULD use one of the "audio/ogg", "video/ogg", or 1231 "application/ogg" mime-types, as defined in [RFC5334]. Such streams 1232 are not strictly "Ogg Opus files" as described above, since they 1233 contain more than a single Opus stream per sequentially multiplexed 1234 segment, e.g. video or multiple audio tracks. In such cases the the 1235 '.opus' filename extension is NOT RECOMMENDED. 1237 11. IANA Considerations 1239 This document has no actions for IANA. 1241 12. Acknowledgments 1243 Thanks to Greg Maxwell, Christopher "Monty" Montgomery, and Jean-Marc 1244 Valin for their valuable contributions to this document. Additional 1245 thanks to Andrew D'Addesio, Greg Maxwell, and Vincent Penqeurc'h for 1246 their feedback based on early implementations. 1248 13. Copying Conditions 1250 The authors agree to grant third parties the irrevocable right to 1251 copy, use, and distribute the work, with or without modification, in 1252 any medium, without royalty, provided that, unless separate 1253 permission is granted, redistributed modified works do not contain 1254 misleading author, version, name of work, or endorsement information. 1256 14. References 1258 14.1. Normative References 1260 [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate 1261 Requirement Levels", BCP 14, RFC 2119, March 1997. 1263 [RFC3533] Pfeiffer, S., "The Ogg Encapsulation Format Version 0", 1264 RFC 3533, May 2003. 1266 [RFC3629] Yergeau, F., "UTF-8, a transformation format of ISO 1267 10646", STD 63, RFC 3629, November 2003. 1269 [RFC5334] Goncalves, I., Pfeiffer, S., and C. Montgomery, "Ogg Media 1270 Types", RFC 5334, September 2008. 1272 [RFC6381] Gellens, R., Singer, D., and P. Frojdh, "The 'Codecs' and 1273 'Profiles' Parameters for "Bucket" Media Types", RFC 6381, 1274 August 2011. 1276 [RFC6716] Valin, JM., Vos, K., and T. Terriberry, "Definition of the 1277 Opus Audio Codec", RFC 6716, September 2012. 1279 [EBU-R128] 1280 EBU Technical Committee, "Loudness Recommendation EBU 1281 R128", August 2011, . 1283 [vorbis-comment] 1284 Montgomery, C., "Ogg Vorbis I Format Specification: 1285 Comment Field and Header Specification", July 2002, 1286 . 1288 14.2. Informative References 1290 [RFC4732] Handley, M., Rescorla, E., and IAB, "Internet Denial-of- 1291 Service Considerations", RFC 4732, December 2006. 1293 [RFC6982] Sheffer, Y. and A. Farrel, "Improving Awareness of Running 1294 Code: The Implementation Status Section", RFC 6982, July 1295 2013. 1297 [flac] Coalson, J., "FLAC - Free Lossless Audio Codec Format 1298 Description", January 2008, . 1301 [hanning] Wikipedia, "Hann window", May 2013, . 1305 [linear-prediction] 1306 Wikipedia, "Linear Predictive Coding", January 2014, 1307 . 1309 [lpc-sample] 1310 Degener, J. and C. Bormann, "Autocorrelation LPC coeff 1311 generation algorithm (Vorbis source code)", November 1994, 1312 . 1314 [replay-gain] 1315 Parker, C. and M. Leese, "VorbisComment: Replay Gain", 1316 June 2009, . 1319 [seeking] Pfeiffer, S., Parker, C., and G. Maxwell, "Granulepos 1320 Encoding and How Seeking Really Works", May 2012, . 1323 [vorbis-mapping] 1324 Montgomery, C., "The Vorbis I Specification, Section 4.3.9 1325 Output Channel Order", January 2010, . 1328 [vorbis-trim] 1329 Montgomery, C., "The Vorbis I Specification, Appendix A: 1330 Embedding Vorbis into an Ogg stream", November 2008, 1331 . 1334 [wave-multichannel] 1335 Microsoft Corporation, "Multiple Channel Audio Data and 1336 WAVE Files", March 2007, . 1339 14.3. URIs 1341 [1] https://wiki.xiph.org/OggOpusImplementation 1343 Authors' Addresses 1345 Timothy B. Terriberry 1346 Mozilla Corporation 1347 650 Castro Street 1348 Mountain View, CA 94041 1349 USA 1351 Phone: +1 650 903-0800 1352 Email: tterribe@xiph.org 1354 Ron Lee 1355 Voicetronix 1356 246 Pulteney Street, Level 1 1357 Adelaide, SA 5000 1358 Australia 1360 Phone: +61 8 8232 9112 1361 Email: ron@debian.org 1363 Ralph Giles 1364 Mozilla Corporation 1365 163 West Hastings Street 1366 Vancouver, BC V6B 1H5 1367 Canada 1369 Phone: +1 778 785 1540 1370 Email: giles@xiph.org