idnits 2.17.1 draft-ietf-cellar-flac-01.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 : ---------------------------------------------------------------------------- ** The document seems to lack a Security Considerations section. ** The document seems to lack an IANA Considerations section. (See Section 2.2 of https://www.ietf.org/id-info/checklist for how to handle the case when there are no actions for IANA.) ** There are 56 instances of too long lines in the document, the longest one being 6 characters in excess of 72. == There are 2 instances of lines with non-RFC2606-compliant FQDNs in the document. Miscellaneous warnings: ---------------------------------------------------------------------------- == The copyright year in the IETF Trust and authors Copyright Line does not match the current year -- The document date (27 April 2021) is 1094 days in the past. Is this intentional? Checking references for intended status: Informational ---------------------------------------------------------------------------- No issues found here. Summary: 3 errors (**), 0 flaws (~~), 2 warnings (==), 1 comment (--). Run idnits with the --verbose option for more detailed information about the items above. -------------------------------------------------------------------------------- 2 cellar M. Sandelman 3 Internet-Draft 4 Intended status: Informational A. Weaver 5 Expires: 29 October 2021 27 April 2021 7 Free Lossless Audio Codec 8 draft-ietf-cellar-flac-01 10 Abstract 12 This document defines FLAC, which stands for Free Lossless Audio 13 Codec, a free, open source codec for lossless audio compression and 14 decompression. 16 Status of This Memo 18 This Internet-Draft is submitted in full conformance with the 19 provisions of BCP 78 and BCP 79. 21 Internet-Drafts are working documents of the Internet Engineering 22 Task Force (IETF). Note that other groups may also distribute 23 working documents as Internet-Drafts. The list of current Internet- 24 Drafts is at https://datatracker.ietf.org/drafts/current/. 26 Internet-Drafts are draft documents valid for a maximum of six months 27 and may be updated, replaced, or obsoleted by other documents at any 28 time. It is inappropriate to use Internet-Drafts as reference 29 material or to cite them other than as "work in progress." 31 This Internet-Draft will expire on 29 October 2021. 33 Copyright Notice 35 Copyright (c) 2021 IETF Trust and the persons identified as the 36 document authors. All rights reserved. 38 This document is subject to BCP 78 and the IETF Trust's Legal 39 Provisions Relating to IETF Documents (https://trustee.ietf.org/ 40 license-info) in effect on the date of publication of this document. 41 Please review these documents carefully, as they describe your rights 42 and restrictions with respect to this document. Code Components 43 extracted from this document must include Simplified BSD License text 44 as described in Section 4.e of the Trust Legal Provisions and are 45 provided without warranty as described in the Simplified BSD License. 47 Table of Contents 49 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . 3 50 2. Notation and Conventions . . . . . . . . . . . . . . . . . . 3 51 3. Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . 3 52 4. Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 53 5. Architecture . . . . . . . . . . . . . . . . . . . . . . . . 4 54 6. Definitions . . . . . . . . . . . . . . . . . . . . . . . . . 5 55 7. Blocking . . . . . . . . . . . . . . . . . . . . . . . . . . 6 56 8. Interchannel Decorrelation . . . . . . . . . . . . . . . . . 6 57 9. Prediction . . . . . . . . . . . . . . . . . . . . . . . . . 7 58 10. Residual Coding . . . . . . . . . . . . . . . . . . . . . . . 8 59 11. Format . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 60 11.1. Conventions . . . . . . . . . . . . . . . . . . . . . . 12 61 11.2. STREAM . . . . . . . . . . . . . . . . . . . . . . . . . 13 62 11.3. METADATA_BLOCK . . . . . . . . . . . . . . . . . . . . . 13 63 11.4. METADATA_BLOCK_HEADER . . . . . . . . . . . . . . . . . 13 64 11.5. BLOCK_TYPE . . . . . . . . . . . . . . . . . . . . . . . 14 65 11.6. METADATA_BLOCK_DATA . . . . . . . . . . . . . . . . . . 14 66 11.7. METADATA_BLOCK_STREAMINFO . . . . . . . . . . . . . . . 15 67 11.8. METADATA_BLOCK_PADDING . . . . . . . . . . . . . . . . . 16 68 11.9. METADATA_BLOCK_APPLICATION . . . . . . . . . . . . . . . 16 69 11.10. METADATA_BLOCK_SEEKTABLE . . . . . . . . . . . . . . . . 16 70 11.11. SEEKPOINT . . . . . . . . . . . . . . . . . . . . . . . 17 71 11.12. METADATA_BLOCK_VORBIS_COMMENT . . . . . . . . . . . . . 17 72 11.13. METADATA_BLOCK_CUESHEET . . . . . . . . . . . . . . . . 18 73 11.14. CUESHEET_TRACK . . . . . . . . . . . . . . . . . . . . . 19 74 11.15. CUESHEET_TRACK_INDEX . . . . . . . . . . . . . . . . . . 20 75 11.16. METADATA_BLOCK_PICTURE . . . . . . . . . . . . . . . . . 20 76 11.17. PICTURE_TYPE . . . . . . . . . . . . . . . . . . . . . . 21 77 11.18. FRAME . . . . . . . . . . . . . . . . . . . . . . . . . 22 78 11.19. FRAME_HEADER . . . . . . . . . . . . . . . . . . . . . . 23 79 11.19.1. FRAME HEADER RESERVED . . . . . . . . . . . . . . . 23 80 11.19.2. BLOCKING STRATEGY . . . . . . . . . . . . . . . . . 24 81 11.19.3. INTERCHANNEL SAMPLE BLOCK SIZE . . . . . . . . . . 24 82 11.19.4. SAMPLE RATE . . . . . . . . . . . . . . . . . . . . 25 83 11.19.5. CHANNEL ASSIGNMENT . . . . . . . . . . . . . . . . 26 84 11.19.6. SAMPLE SIZE . . . . . . . . . . . . . . . . . . . . 27 85 11.19.7. FRAME HEADER RESERVED2 . . . . . . . . . . . . . . 27 86 11.19.8. CODED NUMBER . . . . . . . . . . . . . . . . . . . 27 87 11.19.9. BLOCK SIZE INT . . . . . . . . . . . . . . . . . . 28 88 11.19.10. SAMPLE RATE INT . . . . . . . . . . . . . . . . . . 28 89 11.19.11. FRAME CRC . . . . . . . . . . . . . . . . . . . . . 28 90 11.20. FRAME_FOOTER . . . . . . . . . . . . . . . . . . . . . . 28 91 11.21. SUBFRAME . . . . . . . . . . . . . . . . . . . . . . . . 29 92 11.22. SUBFRAME_HEADER . . . . . . . . . . . . . . . . . . . . 29 93 11.22.1. SUBFRAME TYPE . . . . . . . . . . . . . . . . . . . 29 94 11.22.2. WASTED BITS PER SAMPLE FLAG . . . . . . . . . . . . 30 96 11.23. SUBFRAME_CONSTANT . . . . . . . . . . . . . . . . . . . 30 97 11.24. SUBFRAME_FIXED . . . . . . . . . . . . . . . . . . . . . 30 98 11.25. SUBFRAME_LPC . . . . . . . . . . . . . . . . . . . . . . 31 99 11.26. SUBFRAME_VERBATIM . . . . . . . . . . . . . . . . . . . 31 100 11.27. RESIDUAL . . . . . . . . . . . . . . . . . . . . . . . . 31 101 11.27.1. RESIDUAL_CODING_METHOD . . . . . . . . . . . . . . 32 102 11.27.2. RESIDUAL_CODING_METHOD_PARTITIONED_EXP_GOLOMB . . . 32 103 11.27.3. RESIDUAL_CODING_METHOD_PARTITIONED_EXP_GOLOMB2 . . 33 104 11.27.4. ENCODED RESIDUAL . . . . . . . . . . . . . . . . . 34 105 12. Normative References . . . . . . . . . . . . . . . . . . . . 34 106 Authors' Addresses . . . . . . . . . . . . . . . . . . . . . . . 34 108 1. Introduction 110 This is a detailed description of the FLAC format. There is also a 111 companion document that describes FLAC-to-Ogg mapping 112 (https://xiph.org/flac/ogg_mapping.html). 114 For a user-oriented overview, see About the FLAC Format 115 (https://xiph.org/flac/documentation_format_overview.html). 117 2. Notation and Conventions 119 The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", 120 "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and 121 "OPTIONAL" in this document are to be interpreted as described in BCP 122 14 [RFC2119] [RFC8174] when, and only when, they appear in all 123 capitals, as shown here. 125 3. Acknowledgments 127 FLAC owes much to the many people who have advanced the audio 128 compression field so freely. For instance: - A. J. Robinson 129 (http://svr-www.eng.cam.ac.uk/~ajr/) for his work on Shorten 130 (http://svr-www.eng.cam.ac.uk/reports/abstracts/robinson_tr156.html); 131 his paper is a good starting point on some of the basic methods used 132 by FLAC. FLAC trivially extends and improves the fixed predictors, 133 LPC coefficient quantization, and Exponential-Golomb coding used in 134 Shorten. - S. W. Golomb 135 (https://web.archive.org/web/20040215005354/http://csi.usc.edu/ 136 faculty/golomb.html) and Robert F. Rice; their universal codes are 137 used by FLAC's entropy coder. - N. Levinson and J. Durbin; the 138 reference encoder uses an algorithm developed and refined by them for 139 determining the LPC coefficients from the autocorrelation 140 coefficients. - And of course, Claude Shannon 141 (http://en.wikipedia.org/wiki/Claude_Shannon) 143 4. Scope 145 It is a known fact that no algorithm can losslessly compress all 146 possible input, so most compressors restrict themselves to a useful 147 domain and try to work as well as possible within that domain. 148 FLAC's domain is audio data. Though it can losslessly code any 149 input, only certain kinds of input will get smaller. FLAC exploits 150 the fact that audio data typically has a high degree of sample-to- 151 sample correlation. 153 Within the audio domain, there are many possible subdomains. For 154 example: low bitrate speech, high-bitrate multi-channel music, etc. 155 FLAC itself does not target a specific subdomain, but many of the 156 default parameters of the reference encoder are tuned to CD-quality 157 music data (i.e. 44.1 kHz, 2 channel, 16 bits per sample). The 158 effect of the encoding parameters on different kinds of audio data 159 will be examined later. 161 5. Architecture 163 Similar to many audio coders, a FLAC encoder has the following 164 stages: 166 * "Blocking" (see section on Blocking (#blocking)). The input is 167 broken up into many contiguous blocks. With FLAC, the blocks MAY 168 vary in size. The optimal size of the block is usually affected 169 by many factors, including the sample rate, spectral 170 characteristics over time, etc. Though FLAC allows the block size 171 to vary within a stream, the reference encoder uses a fixed block 172 size. 174 * "Interchannel Decorrelation" (see section on Interchannel 175 Decorrelation (#interchannel-decorrelation)). In the case of 176 stereo streams, the encoder will create mid and side signals based 177 on the average and difference (respectively) of the left and right 178 channels. The encoder will then pass the best form of the signal 179 to the next stage. 181 * "Prediction" (see section on Prediction (#prediction)). The block 182 is passed through a prediction stage where the encoder tries to 183 find a mathematical description (usually an approximate one) of 184 the signal. This description is typically much smaller than the 185 raw signal itself. Since the methods of prediction are known to 186 both the encoder and decoder, only the parameters of the predictor 187 need be included in the compressed stream. FLAC currently uses 188 four different classes of predictors, but the format has reserved 189 space for additional methods. FLAC allows the class of predictor 190 to change from block to block, or even within the channels of a 191 block. 193 * "Residual Coding" (See section on Residual Coding (#residual- 194 coding)). If the predictor does not describe the signal exactly, 195 the difference between the original signal and the predicted 196 signal (called the error or residual signal) MUST be coded 197 losslessly. If the predictor is effective, the residual signal 198 will require fewer bits per sample than the original signal. FLAC 199 currently uses only one method for encoding the residual, but the 200 format has reserved space for additional methods. FLAC allows the 201 residual coding method to change from block to block, or even 202 within the channels of a block. 204 In addition, FLAC specifies a metadata system, which allows arbitrary 205 information about the stream to be included at the beginning of the 206 stream. 208 6. Definitions 210 Many terms like "block" and "frame" are used to mean different things 211 in different encoding schemes. For example, a frame in MP3 212 corresponds to many samples across several channels, whereas an S/ 213 PDIF frame represents just one sample for each channel. The 214 definitions we use for FLAC follow. Note that when we talk about 215 blocks and subblocks we are referring to the raw unencoded audio data 216 that is the input to the encoder, and when we talk about frames and 217 subframes, we are referring to the FLAC-encoded data. 219 * *Block*: One or more audio samples that span several channels. 221 * *Subblock*: One or more audio samples within a channel. A block 222 contains one subblock for each channel, and all subblocks contain 223 the same number of samples. 225 * *Blocksize*: The number of samples in any of a block's subblocks. 226 For example, a one second block sampled at 44.1 kHz has a 227 blocksize of 44100, regardless of the number of channels. 229 * *Frame*: A frame header plus one or more subframes. 231 * *Subframe*: A subframe header plus one or more encoded samples 232 from a given channel. All subframes within a frame will contain 233 the same number of samples. 235 * *Exponential-Golomb coding*: One of Robert Rice's universal coding 236 schemes, FLAC's residual coder, compresses data by writing the 237 number of bits to be read minus 1, before writing the actual 238 value. 240 * *LPC*: Linear predictive coding (https://en.wikipedia.org/wiki/ 241 Linear_predictive_coding). 243 7. Blocking 245 The size used for blocking the audio data has a direct effect on the 246 compression ratio. If the block size is too small, the resulting 247 large number of frames mean that excess bits will be wasted on frame 248 headers. If the block size is too large, the characteristics of the 249 signal MAY vary so much that the encoder will be unable to find a 250 good predictor. In order to simplify encoder/decoder design, FLAC 251 imposes a minimum block size of 16 samples, and a maximum block size 252 of 65535 samples. This range covers the optimal size for all of the 253 audio data FLAC supports. 255 Currently the reference encoder uses a fixed block size, optimized on 256 the sample rate of the input. Future versions MAY vary the block 257 size depending on the characteristics of the signal. 259 Blocked data is passed to the predictor stage one subblock (channel) 260 at a time. Each subblock is independently coded into a subframe, and 261 the subframes are concatenated into a frame. Because each channel is 262 coded separately, one channel of a stereo frame MAY be encoded as a 263 constant subframe, and the other an LPC subframe. 265 8. Interchannel Decorrelation 267 In stereo streams, many times there is an exploitable amount of 268 correlation between the left and right channels. FLAC allows the 269 frames of stereo streams to have different channel assignments, and 270 an encoder MAY choose to use the best representation on a frame-by- 271 frame basis. 273 * *Independent*. The left and right channels are coded 274 independently. 276 * *Mid-side*. The left and right channels are transformed into mid 277 and side channels. The mid channel is the midpoint (average) of 278 the left and right signals, and the side is the difference signal 279 (left minus right). 281 * *Left-side*. The left channel and side channel are coded. 283 * *Right-side*. The right channel and side channel are coded. 285 Surprisingly, the left-side and right-side forms can be the most 286 efficient in many frames, even though the raw number of bits per 287 sample needed for the original signal is slightly more than that 288 needed for independent or mid-side coding. 290 9. Prediction 292 FLAC uses four methods for modeling the input signal: 294 1. *Verbatim*. This is essentially a zero-order predictor of the 295 signal. The predicted signal is zero, meaning the residual is 296 the signal itself, and the compression is zero. This is the 297 baseline against which the other predictors are measured. If you 298 feed random data to the encoder, the verbatim predictor will 299 probably be used for every subblock. Since the raw signal is not 300 actually passed through the residual coding stage (it is added to 301 the stream 'verbatim'), the encoding results will not be the same 302 as a zero-order linear predictor. 304 2. *Constant*. This predictor is used whenever the subblock is pure 305 DC ("digital silence"), i.e. a constant value throughout. The 306 signal is run-length encoded and added to the stream. 308 3. *Fixed linear predictor*. FLAC uses a class of computationally- 309 efficient fixed linear predictors (for a good description, see 310 audiopak (http://www.hpl.hp.com/techreports/1999/HPL- 311 1999-144.pdf) and shorten (http://svr- 312 www.eng.cam.ac.uk/reports/abstracts/robinson_tr156.html)). FLAC 313 adds a fourth-order predictor to the zero-to-third-order 314 predictors used by Shorten. Since the predictors are fixed, the 315 predictor order is the only parameter that needs to be stored in 316 the compressed stream. The error signal is then passed to the 317 residual coder. 319 4. *FIR Linear prediction*. For more accurate modeling (at a cost of 320 slower encoding), FLAC supports up to 32nd order FIR linear 321 prediction (again, for information on linear prediction, see 322 audiopak (http://www.hpl.hp.com/techreports/1999/HPL- 323 1999-144.pdf) and shorten (http://svr- 324 www.eng.cam.ac.uk/reports/abstracts/robinson_tr156.html)). The 325 reference encoder uses the Levinson-Durbin method for calculating 326 the LPC coefficients from the autocorrelation coefficients, and 327 the coefficients are quantized before computing the residual. 328 Whereas encoders such as Shorten used a fixed quantization for 329 the entire input, FLAC allows the quantized coefficient precision 330 to vary from subframe to subframe. The FLAC reference encoder 331 estimates the optimal precision to use based on the block size 332 and dynamic range of the original signal. 334 10. Residual Coding 336 FLAC uses Exponential-Golomb (a variant of Rice) coding as its 337 residual encoder. You can learn more about exp-golomb coding 338 (https://en.wikipedia.org/wiki/Exponential-Golomb_coding) on 339 Wikipedia. 341 FLAC currently defines two similar methods for the coding of the 342 error signal from the prediction stage. The error signal is coded 343 using Exponential-Golomb codes in one of two ways: 345 1. the encoder estimates a single exp-golomb parameter based on the 346 variance of the residual and exp-golomb codes the entire residual 347 using this parameter; 349 2. the residual is partitioned into several equal-length regions of 350 contiguous samples, and each region is coded with its own exp- 351 golomb parameter based on the region's mean. 353 (Note that the first method is a special case of the second method 354 with one partition, except the exp-golomb parameter is based on the 355 residual variance instead of the mean.) 357 The FLAC format has reserved space for other coding methods. Some 358 possibilities for volunteers would be to explore better context- 359 modeling of the exp-golomb parameter, or Huffman coding. See LOCO-I 360 (http://www.hpl.hp.com/techreports/98/HPL-98-193.html) and pucrunch ( 361 http://web.archive.org/web/20140827133312/http://www.cs.tut.fi/~alber 362 t/Dev/pucrunch/packing.html) for descriptions of several universal 363 codes. 365 11. Format 367 This section specifies the FLAC bitstream format. FLAC has no format 368 version information, but it does contain reserved space in several 369 places. Future versions of the format MAY use this reserved space 370 safely without breaking the format of older streams. Older decoders 371 MAY choose to abort decoding or skip data encoded with newer methods. 372 Apart from reserved patterns, in places the format specifies invalid 373 patterns, meaning that the patterns MAY never appear in any valid 374 bitstream, in any prior, present, or future versions of the format. 375 These invalid patterns are usually used to make the synchronization 376 mechanism more robust. 378 All numbers used in a FLAC bitstream MUST be integers; there are no 379 floating-point representations. All numbers MUST be big-endian 380 coded. All numbers MUST be unsigned unless otherwise specified. 382 Before the formal description of the stream, an overview might be 383 helpful. 385 * A FLAC bitstream consists of the "fLaC" (i.e. 0x664C6143) marker 386 at the beginning of the stream, followed by a mandatory metadata 387 block (called the STREAMINFO block), any number of other metadata 388 blocks, then the audio frames. 390 * FLAC supports up to 128 kinds of metadata blocks; currently the 391 following are defined: 393 - "STREAMINFO": This block has information about the whole 394 stream, like sample rate, number of channels, total number of 395 samples, etc. It MUST be present as the first metadata block 396 in the stream. Other metadata blocks MAY follow, and ones that 397 the decoder doesn't understand, it will skip. 399 - "PADDING": This block allows for an arbitrary amount of 400 padding. The contents of a PADDING block have no meaning. 401 This block is useful when it is known that metadata will be 402 edited after encoding; the user can instruct the encoder to 403 reserve a PADDING block of sufficient size so that when 404 metadata is added, it will simply overwrite the padding (which 405 is relatively quick) instead of having to insert it into the 406 right place in the existing file (which would normally require 407 rewriting the entire file). 409 - "APPLICATION": This block is for use by third-party 410 applications. The only mandatory field is a 32-bit identifier. 411 This ID is granted upon request to an application by the FLAC 412 maintainers. The remainder is of the block is defined by the 413 registered application. Visit the registration page 414 (https://xiph.org/flac/id.html) if you would like to register 415 an ID for your application with FLAC. 417 - "SEEKTABLE": This is an OPTIONAL block for storing seek points. 418 It is possible to seek to any given sample in a FLAC stream 419 without a seek table, but the delay can be unpredictable since 420 the bitrate MAY vary widely within a stream. By adding seek 421 points to a stream, this delay can be significantly reduced. 422 Each seek point takes 18 bytes, so 1% resolution within a 423 stream adds less than 2K. There can be only one SEEKTABLE in a 424 stream, but the table can have any number of seek points. 425 There is also a special 'placeholder' seekpoint which will be 426 ignored by decoders but which can be used to reserve space for 427 future seek point insertion. 429 - "VORBIS_COMMENT": This block is for storing a list of human- 430 readable name/value pairs. Values are encoded using UTF-8. It 431 is an implementation of the Vorbis comment specification 432 (http://xiph.org/vorbis/doc/v-comment.html) (without the 433 framing bit). This is the only officially supported tagging 434 mechanism in FLAC. There MUST be only zero or one 435 VORBIS_COMMENT blocks in a stream. In some external 436 documentation, Vorbis comments are called FLAC tags to lessen 437 confusion. 439 - "CUESHEET": This block is for storing various information that 440 can be used in a cue sheet. It supports track and index 441 points, compatible with Red Book CD digital audio discs, as 442 well as other CD-DA metadata such as media catalog number and 443 track ISRCs. The CUESHEET block is especially useful for 444 backing up CD-DA discs, but it can be used as a general purpose 445 cueing mechanism for playback. 447 - "PICTURE": This block is for storing pictures associated with 448 the file, most commonly cover art from CDs. There MAY be more 449 than one PICTURE block in a file. The picture format is 450 similar to the APIC frame in ID3v2 (http://www.id3.org/ 451 id3v2.4.0-frames). The PICTURE block has a type, MIME type, 452 and UTF-8 description like ID3v2, and supports external linking 453 via URL (though this is discouraged). The differences are that 454 there is no uniqueness constraint on the description field, and 455 the MIME type is mandatory. The FLAC PICTURE block also 456 includes the resolution, color depth, and palette size so that 457 the client can search for a suitable picture without having to 458 scan them all. 460 * The audio data is composed of one or more audio frames. Each 461 frame consists of a frame header, which contains a sync code, 462 information about the frame like the block size, sample rate, 463 number of channels, et cetera, and an 8-bit CRC. The frame header 464 also contains either the sample number of the first sample in the 465 frame (for variable-blocksize streams), or the frame number (for 466 fixed-blocksize streams). This allows for fast, sample-accurate 467 seeking to be performed. Following the frame header are encoded 468 subframes, one for each channel, and finally, the frame is zero- 469 padded to a byte boundary. Each subframe has its own header that 470 specifies how the subframe is encoded. 472 * Since a decoder MAY start decoding in the middle of a stream, 473 there MUST be a method to determine the start of a frame. A 474 14-bit sync code begins each frame. The sync code will not appear 475 anywhere else in the frame header. However, since it MAY appear 476 in the subframes, the decoder has two other ways of ensuring a 477 correct sync. The first is to check that the rest of the frame 478 header contains no invalid data. Even this is not foolproof since 479 valid header patterns can still occur within the subframes. The 480 decoder's final check is to generate an 8-bit CRC of the frame 481 header and compare this to the CRC stored at the end of the frame 482 header. 484 * Again, since a decoder MAY start decoding at an arbitrary frame in 485 the stream, each frame header MUST contain some basic information 486 about the stream because the decoder MAY not have access to the 487 STREAMINFO metadata block at the start of the stream. This 488 information includes sample rate, bits per sample, number of 489 channels, etc. Since the frame header is pure overhead, it has a 490 direct effect on the compression ratio. To keep the frame header 491 as small as possible, FLAC uses lookup tables for the most 492 commonly used values for frame parameters. For instance, the 493 sample rate part of the frame header is specified using 4 bits. 494 Eight of the bit patterns correspond to the commonly used sample 495 rates of 8, 16, 22.05, 24, 32, 44.1, 48 or 96 kHz. However, odd 496 sample rates can be specified by using one of the 'hint' bit 497 patterns, directing the decoder to find the exact sample rate at 498 the end of the frame header. The same method is used for 499 specifying the block size and bits per sample. In this way, the 500 frame header size stays small for all of the most common forms of 501 audio data. 503 * Individual subframes (one for each channel) are coded separately 504 within a frame, and appear serially in the stream. In other 505 words, the encoded audio data is NOT channel-interleaved. This 506 reduces decoder complexity at the cost of requiring larger decode 507 buffers. Each subframe has its own header specifying the 508 attributes of the subframe, like prediction method and order, 509 residual coding parameters, etc. The header is followed by the 510 encoded audio data for that channel. 512 * "FLAC" specifies a subset of itself as the Subset format. The 513 purpose of this is to ensure that any streams encoded according to 514 the Subset are truly "streamable", meaning that a decoder that 515 cannot seek within the stream can still pick up in the middle of 516 the stream and start decoding. It also makes hardware decoder 517 implementations more practical by limiting the encoding parameters 518 such that decoder buffer sizes and other resource requirements can 519 be easily determined. *flac* generates Subset streams by default 520 unless the "--lax" command-line option is used. The Subset makes 521 the following limitations on what MAY be used in the stream: 523 * The blocksize bits in the "FRAME_HEADER" (see FRAME_HEADER section 524 (#frameheader)) MUST be 0b0001-0b1110. The blocksize MUST be <= 525 16384; if the sample rate is <= 48000 Hz, the blocksize MUST be <= 526 4608 = 2^9 * 3^2. 528 * The sample rate bits in the "FRAME_HEADER" MUST be 0b0001-0b1110. 530 * The bits-per-sample bits in the "FRAME_HEADER" MUST be 531 0b001-0b111. 533 * If the sample rate is <= 48000 Hz, the filter order in "LPC 534 subframes" (see SUBFRAME_LPC section (#subframelpc)) MUST be less 535 than or equal to 12, i.e. the subframe type bits in the 536 "SUBFRAME_HEADER" (see SUBFRAME_HEADER section (#subframeheader)) 537 SHOULD NOT be 0b101100-0b111111. 539 * The Rice partition order in an "exp-golomb coded residual section" 540 (see RESIDUAL_CODING_METHOD_PARTITIONE_EXP_GOLOMB section 541 (#residualcodingmethodpartitionedexpgolomb)) MUST be less than or 542 equal to 8. 544 11.1. Conventions 546 The following tables constitute a formal description of the FLAC 547 format. Values expressed as "u(n)" represent unsigned big-endian 548 integer using "n" bits. "n" may be expressed as an equation using "*" 549 (multiplication), "/" (division), "+" (addition), or "-" 550 (subtraction). An inclusive range of the number of bits expressed 551 may be represented with an ellipsis, such as "u(m...n)". The name of 552 a value followed by an asterisk "*" indicates zero or more 553 occurrences of the value. The name of a value followed by a plus 554 sign "+" indicates one or more occurrences of the value. 556 11.2. STREAM 558 +===========================+=====================================+ 559 | Data | Description | 560 +===========================+=====================================+ 561 | u(32) | "fLaC", the FLAC stream marker in | 562 | | ASCII, meaning byte 0 of the stream | 563 | | is 0x66, followed by 0x4C 0x61 0x43 | 564 +---------------------------+-------------------------------------+ 565 | METADATA_BLOCK_STREAMINFO | This is the mandatory STREAMINFO | 566 | | metadata block that has the basic | 567 | | properties of the stream. | 568 +---------------------------+-------------------------------------+ 569 | "METADATA_BLOCK"* | Zero or more metadata blocks | 570 +---------------------------+-------------------------------------+ 571 | "FRAME"+ | One or more audio frames | 572 +---------------------------+-------------------------------------+ 574 Table 1 576 11.3. METADATA_BLOCK 578 +=======================+========================================+ 579 | Data | Description | 580 +=======================+========================================+ 581 | METADATA_BLOCK_HEADER | A block header that specifies the type | 582 | | and size of the metadata block data. | 583 +-----------------------+----------------------------------------+ 584 | METADATA_BLOCK_DATA | | 585 +-----------------------+----------------------------------------+ 587 Table 2 589 11.4. METADATA_BLOCK_HEADER 591 +=======+=========================================================+ 592 | Data | Description | 593 +=======+=========================================================+ 594 | u(1) | Last-metadata-block flag: '1' if this block is the last | 595 | | metadata block before the audio blocks, '0' otherwise. | 596 +-------+---------------------------------------------------------+ 597 | u(7) | BLOCK_TYPE | 598 +-------+---------------------------------------------------------+ 599 | u(24) | Length (in bytes) of metadata to follow (does not | 600 | | include the size of the "METADATA_BLOCK_HEADER") | 601 +-------+---------------------------------------------------------+ 603 Table 3 605 11.5. BLOCK_TYPE 607 +=========+====================================================+ 608 | Value | Description | 609 +=========+====================================================+ 610 | 0 | STREAMINFO | 611 +---------+----------------------------------------------------+ 612 | 1 | PADDING | 613 +---------+----------------------------------------------------+ 614 | 2 | APPLICATION | 615 +---------+----------------------------------------------------+ 616 | 3 | SEEKTABLE | 617 +---------+----------------------------------------------------+ 618 | 4 | VORBIS_COMMENT | 619 +---------+----------------------------------------------------+ 620 | 5 | CUESHEET | 621 +---------+----------------------------------------------------+ 622 | 6 | PICTURE | 623 +---------+----------------------------------------------------+ 624 | 7 - 126 | reserved | 625 +---------+----------------------------------------------------+ 626 | 127 | invalid, to avoid confusion with a frame sync code | 627 +---------+----------------------------------------------------+ 629 Table 4 631 11.6. METADATA_BLOCK_DATA 633 +====================================+=============+ 634 | Data | Description | 635 +====================================+=============+ 636 | "METADATA_BLOCK_STREAMINFO" || | The block | 637 | "METADATA_BLOCK_PADDING" || | data MUST | 638 | "METADATA_BLOCK_APPLICATION" || | match the | 639 | "METADATA_BLOCK_SEEKTABLE" || | block type | 640 | "METADATA_BLOCK_VORBIS_COMMENT" || | in the | 641 | "METADATA_BLOCK_CUESHEET" || | block | 642 | "METADATA_BLOCK_PICTURE" | header. | 643 +------------------------------------+-------------+ 645 Table 5 647 11.7. METADATA_BLOCK_STREAMINFO 649 +========+=================================================+ 650 | Data | Description | 651 +========+=================================================+ 652 | u(16) | The minimum block size (in samples) used in the | 653 | | stream. | 654 +--------+-------------------------------------------------+ 655 | u(16) | The maximum block size (in samples) used in the | 656 | | stream. (Minimum blocksize == maximum | 657 | | blocksize) implies a fixed-blocksize stream. | 658 +--------+-------------------------------------------------+ 659 | u(24) | The minimum frame size (in bytes) used in the | 660 | | stream. A value of "0" signifies that the | 661 | | value is not known. | 662 +--------+-------------------------------------------------+ 663 | u(24) | The maximum frame size (in bytes) used in the | 664 | | stream. A value of "0" signifies that the | 665 | | value is not known. | 666 +--------+-------------------------------------------------+ 667 | u(20) | Sample rate in Hz. Though 20 bits are | 668 | | available, the maximum sample rate is limited | 669 | | by the structure of frame headers to 655350 Hz. | 670 | | Also, a value of 0 is invalid. | 671 +--------+-------------------------------------------------+ 672 | u(3) | (number of channels)-1. FLAC supports from 1 | 673 | | to 8 channels | 674 +--------+-------------------------------------------------+ 675 | u(5) | (bits per sample)-1. FLAC supports from 4 to | 676 | | 32 bits per sample. Currently the reference | 677 | | encoder and decoders only support up to 24 bits | 678 | | per sample. | 679 +--------+-------------------------------------------------+ 680 | u(36) | Total samples in stream. 'Samples' means | 681 | | inter-channel sample, i.e. one second of 44.1 | 682 | | kHz audio will have 44100 samples regardless of | 683 | | the number of channels. A value of zero here | 684 | | means the number of total samples is unknown. | 685 +--------+-------------------------------------------------+ 686 | u(128) | MD5 signature of the unencoded audio data. | 687 | | This allows the decoder to determine if an | 688 | | error exists in the audio data even when the | 689 | | error does not result in an invalid bitstream. | 690 +--------+-------------------------------------------------+ 692 Table 6 694 NOTE 695 * FLAC specifies a minimum block size of 16 and a maximum block size 696 of 65535, meaning the bit patterns corresponding to the numbers 697 0-15 in the minimum blocksize and maximum blocksize fields are 698 invalid. 700 11.8. METADATA_BLOCK_PADDING 702 +======+========================================+ 703 | Data | Description | 704 +======+========================================+ 705 | u(n) | n '0' bits (n MUST be a multiple of 8) | 706 +------+----------------------------------------+ 708 Table 7 710 11.9. METADATA_BLOCK_APPLICATION 712 +=======+===========================================+ 713 | Data | Description | 714 +=======+===========================================+ 715 | u(32) | Registered application ID. (Visit the | 716 | | registration page (https://xiph.org/flac/ | 717 | | id.html) to register an ID with FLAC.) | 718 +-------+-------------------------------------------+ 719 | u(n) | Application data (n MUST be a multiple of | 720 | | 8) | 721 +-------+-------------------------------------------+ 723 Table 8 725 11.10. METADATA_BLOCK_SEEKTABLE 727 +==============+==========================+ 728 | Data | Description | 729 +==============+==========================+ 730 | "SEEKPOINT"+ | One or more seek points. | 731 +--------------+--------------------------+ 733 Table 9 735 NOTE - The number of seek points is implied by the metadata header 736 'length' field, i.e. equal to length / 18. 738 11.11. SEEKPOINT 740 +=======+==========================================================+ 741 | Data | Description | 742 +=======+==========================================================+ 743 | u(64) | Sample number of first sample in the target frame, or | 744 | | "0xFFFFFFFFFFFFFFFF" for a placeholder point. | 745 +-------+----------------------------------------------------------+ 746 | u(64) | Offset (in bytes) from the first byte of the first frame | 747 | | header to the first byte of the target frame's header. | 748 +-------+----------------------------------------------------------+ 749 | u(16) | Number of samples in the target frame. | 750 +-------+----------------------------------------------------------+ 752 Table 10 754 NOTES 756 * For placeholder points, the second and third field values are 757 undefined. 759 * Seek points within a table MUST be sorted in ascending order by 760 sample number. 762 * Seek points within a table MUST be unique by sample number, with 763 the exception of placeholder points. 765 * The previous two notes imply that there MAY be any number of 766 placeholder points, but they MUST all occur at the end of the 767 table. 769 11.12. METADATA_BLOCK_VORBIS_COMMENT 771 +======+===========================================================+ 772 | Data | Description | 773 +======+===========================================================+ 774 | u(n) | Also known as FLAC tags, the contents of a vorbis comment | 775 | | packet as specified here (http://www.xiph.org/vorbis/doc/ | 776 | | v-comment.html) (without the framing bit). Note that the | 777 | | vorbis comment spec allows for on the order of 2^64 bytes | 778 | | of data where as the FLAC metadata block is limited to | 779 | | 2^24 bytes. Given the stated purpose of vorbis comments, | 780 | | i.e. human-readable textual information, this limit is | 781 | | unlikely to be restrictive. Also note that the 32-bit | 782 | | field lengths are little-endian coded according to the | 783 | | vorbis spec, as opposed to the usual big-endian coding of | 784 | | fixed-length integers in the rest of FLAC. | 785 +------+-----------------------------------------------------------+ 786 Table 11 788 11.13. METADATA_BLOCK_CUESHEET 790 +===================+==============================================+ 791 | Data | Description | 792 +===================+==============================================+ 793 | u(128*8) | Media catalog number, in ASCII printable | 794 | | characters 0x20-0x7E. In general, the media | 795 | | catalog number SHOULD be 0 to 128 bytes | 796 | | long; any unused characters SHOULD be right- | 797 | | padded with NUL characters. For CD-DA, this | 798 | | is a thirteen digit number, followed by 115 | 799 | | NUL bytes. | 800 +-------------------+----------------------------------------------+ 801 | u(64) | The number of lead-in samples. This field | 802 | | has meaning only for CD-DA cuesheets; for | 803 | | other uses it SHOULD be 0. For CD-DA, the | 804 | | lead-in is the TRACK 00 area where the table | 805 | | of contents is stored; more precisely, it is | 806 | | the number of samples from the first sample | 807 | | of the media to the first sample of the | 808 | | first index point of the first track. | 809 | | According to the Red Book, the lead-in MUST | 810 | | be silence and CD grabbing software does not | 811 | | usually store it; additionally, the lead-in | 812 | | MUST be at least two seconds but MAY be | 813 | | longer. For these reasons the lead-in | 814 | | length is stored here so that the absolute | 815 | | position of the first track can be computed. | 816 | | Note that the lead-in stored here is the | 817 | | number of samples up to the first index | 818 | | point of the first track, not necessarily to | 819 | | INDEX 01 of the first track; even the first | 820 | | track MAY have INDEX 00 data. | 821 +-------------------+----------------------------------------------+ 822 | u(1) | "1" if the CUESHEET corresponds to a Compact | 823 | | Disc, else "0". | 824 +-------------------+----------------------------------------------+ 825 | u(7+258*8) | Reserved. All bits MUST be set to zero. | 826 +-------------------+----------------------------------------------+ 827 | u(8) | The number of tracks. Must be at least 1 | 828 | | (because of the requisite lead-out track). | 829 | | For CD-DA, this number MUST be no more than | 830 | | 100 (99 regular tracks and one lead-out | 831 | | track). | 832 +-------------------+----------------------------------------------+ 833 | "CUESHEET_TRACK"+ | One or more tracks. A CUESHEET block is | 834 | | REQUIRED to have a lead-out track; it is | 835 | | always the last track in the CUESHEET. For | 836 | | CD-DA, the lead-out track number MUST be 170 | 837 | | as specified by the Red Book, otherwise it | 838 | | MUST be 255. | 839 +-------------------+----------------------------------------------+ 841 Table 12 843 11.14. CUESHEET_TRACK 845 +=======================+=================================================+ 846 |Data |Description | 847 +=======================+=================================================+ 848 |u(64) |Track offset in samples, relative to the | 849 | |beginning of the FLAC audio stream. It is the | 850 | |offset to the first index point of the track. | 851 | |(Note how this differs from CD-DA, where the | 852 | |track's offset in the TOC is that of the track's | 853 | |INDEX 01 even if there is an INDEX 00.) For CD- | 854 | |DA, the offset MUST be evenly divisible by 588 | 855 | |samples (588 samples = 44100 samples/s * 1/75 s).| 856 +-----------------------+-------------------------------------------------+ 857 |u(8) |Track number. A track number of 0 is not allowed| 858 | |to avoid conflicting with the CD-DA spec, which | 859 | |reserves this for the lead-in. For CD-DA the | 860 | |number MUST be 1-99, or 170 for the lead-out; for| 861 | |non-CD-DA, the track number MUST for 255 for the | 862 | |lead-out. It is not REQUIRED but encouraged to | 863 | |start with track 1 and increase sequentially. | 864 | |Track numbers MUST be unique within a CUESHEET. | 865 +-----------------------+-------------------------------------------------+ 866 |u(12*8) |Track ISRC. This is a 12-digit alphanumeric | 867 | |code; see here (http://isrc.ifpi.org/) and here | 868 | |(http://www.disctronics.co.uk/technology/cdaudio/| 869 | |cdaud_isrc.htm). A value of 12 ASCII NUL | 870 | |characters MAY be used to denote absence of an | 871 | |ISRC. | 872 +-----------------------+-------------------------------------------------+ 873 |u(1) |The track type: 0 for audio, 1 for non-audio. | 874 | |This corresponds to the CD-DA Q-channel control | 875 | |bit 3. | 876 +-----------------------+-------------------------------------------------+ 877 |u(1) |The pre-emphasis flag: 0 for no pre-emphasis, 1 | 878 | |for pre-emphasis. This corresponds to the CD-DA | 879 | |Q-channel control bit 5; see here | 880 | |(http://www.chipchapin.com/CDMedia/cdda9.php3). | 881 +-----------------------+-------------------------------------------------+ 882 |u(6+13*8) |Reserved. All bits MUST be set to zero. | 883 +-----------------------+-------------------------------------------------+ 884 |u(8) |The number of track index points. There MUST be | 885 | |at least one index in every track in a CUESHEET | 886 | |except for the lead-out track, which MUST have | 887 | |zero. For CD-DA, this number SHOULD NOT be more | 888 | |than 100. | 889 +-----------------------+-------------------------------------------------+ 890 |"CUESHEET_TRACK_INDEX"+|For all tracks except the lead-out track, one or | 891 | |more track index points. | 892 +-----------------------+-------------------------------------------------+ 894 Table 13 896 11.15. CUESHEET_TRACK_INDEX 898 +========+=========================================================+ 899 | Data | Description | 900 +========+=========================================================+ 901 | u(64) | Offset in samples, relative to the track offset, of the | 902 | | index point. For CD-DA, the offset MUST be evenly | 903 | | divisible by 588 samples (588 samples = 44100 samples/s | 904 | | * 1/75 s). Note that the offset is from the beginning | 905 | | of the track, not the beginning of the audio data. | 906 +--------+---------------------------------------------------------+ 907 | u(8) | The index point number. For CD-DA, an index number of | 908 | | 0 corresponds to the track pre-gap. The first index in | 909 | | a track MUST have a number of 0 or 1, and subsequently, | 910 | | index numbers MUST increase by 1. Index numbers MUST | 911 | | be unique within a track. | 912 +--------+---------------------------------------------------------+ 913 | u(3*8) | Reserved. All bits MUST be set to zero. | 914 +--------+---------------------------------------------------------+ 916 Table 14 918 11.16. METADATA_BLOCK_PICTURE 920 +========+==================================================+ 921 | Data | Description | 922 +========+==================================================+ 923 | u(32) | The PICTURE_TYPE according to the ID3v2 APIC | 924 | | frame. | 925 +--------+--------------------------------------------------+ 926 | u(32) | The length of the MIME type string in bytes. | 927 +--------+--------------------------------------------------+ 928 | u(n*8) | The MIME type string, in printable ASCII | 929 | | characters 0x20-0x7E. The MIME type MAY also be | 930 | | "-->" to signify that the data part is a URL of | 931 | | the picture instead of the picture data itself. | 932 +--------+--------------------------------------------------+ 933 | u(32) | The length of the description string in bytes. | 934 +--------+--------------------------------------------------+ 935 | u(n*8) | The description of the picture, in UTF-8. | 936 +--------+--------------------------------------------------+ 937 | u(32) | The width of the picture in pixels. | 938 +--------+--------------------------------------------------+ 939 | u(32) | The height of the picture in pixels. | 940 +--------+--------------------------------------------------+ 941 | u(32) | The color depth of the picture in bits-per- | 942 | | pixel. | 943 +--------+--------------------------------------------------+ 944 | u(32) | For indexed-color pictures (e.g. GIF), the | 945 | | number of colors used, or "0" for non-indexed | 946 | | pictures. | 947 +--------+--------------------------------------------------+ 948 | u(32) | The length of the picture data in bytes. | 949 +--------+--------------------------------------------------+ 950 | u(n*8) | The binary picture data. | 951 +--------+--------------------------------------------------+ 953 Table 15 955 11.17. PICTURE_TYPE 957 +=======+=====================================+ 958 | Value | Description | 959 +=======+=====================================+ 960 | 0 | Other | 961 +-------+-------------------------------------+ 962 | 1 | 32x32 pixels 'file icon' (PNG only) | 963 +-------+-------------------------------------+ 964 | 2 | Other file icon | 965 +-------+-------------------------------------+ 966 | 3 | Cover (front) | 967 +-------+-------------------------------------+ 968 | 4 | Cover (back) | 969 +-------+-------------------------------------+ 970 | 5 | Leaflet page | 971 +-------+-------------------------------------+ 972 | 6 | Media (e.g. label side of CD) | 973 +-------+-------------------------------------+ 974 | 7 | Lead artist/lead performer/soloist | 975 +-------+-------------------------------------+ 976 | 8 | Artist/performer | 977 +-------+-------------------------------------+ 978 | 9 | Conductor | 979 +-------+-------------------------------------+ 980 | 10 | Band/Orchestra | 981 +-------+-------------------------------------+ 982 | 11 | Composer | 983 +-------+-------------------------------------+ 984 | 12 | Lyricist/text writer | 985 +-------+-------------------------------------+ 986 | 13 | Recording Location | 987 +-------+-------------------------------------+ 988 | 14 | During recording | 989 +-------+-------------------------------------+ 990 | 15 | During performance | 991 +-------+-------------------------------------+ 992 | 16 | Movie/video screen capture | 993 +-------+-------------------------------------+ 994 | 17 | A bright colored fish | 995 +-------+-------------------------------------+ 996 | 18 | Illustration | 997 +-------+-------------------------------------+ 998 | 19 | Band/artist logotype | 999 +-------+-------------------------------------+ 1000 | 20 | Publisher/Studio logotype | 1001 +-------+-------------------------------------+ 1003 Table 16 1005 Other values are reserved and SHOULD NOT be used. There MAY only be 1006 one each of picture type 1 and 2 in a file. 1008 11.18. FRAME 1010 +==============+=================================+ 1011 | Data | Description | 1012 +==============+=================================+ 1013 | FRAME_HEADER | | 1014 +--------------+---------------------------------+ 1015 | "SUBFRAME"+ | One SUBFRAME per channel. | 1016 +--------------+---------------------------------+ 1017 | u(?) | Zero-padding to byte alignment. | 1018 +--------------+---------------------------------+ 1019 | FRAME_FOOTER | | 1020 +--------------+---------------------------------+ 1022 Table 17 1024 11.19. FRAME_HEADER 1026 +=======+================================+ 1027 | Data | Description | 1028 +=======+================================+ 1029 | u(14) | Sync code '0b11111111111110' | 1030 +-------+--------------------------------+ 1031 | u(1) | FRAME HEADER RESERVED | 1032 +-------+--------------------------------+ 1033 | u(1) | BLOCKING STRATEGY | 1034 +-------+--------------------------------+ 1035 | u(4) | INTERCHANNEL SAMPLE BLOCK SIZE | 1036 +-------+--------------------------------+ 1037 | u(4) | SAMPLE RATE | 1038 +-------+--------------------------------+ 1039 | u(4) | CHANNEL ASSIGNMENT | 1040 +-------+--------------------------------+ 1041 | u(3) | SAMPLE SIZE | 1042 +-------+--------------------------------+ 1043 | u(1) | FRAME HEADER RESERVED2 | 1044 +-------+--------------------------------+ 1045 | u(?) | CODED NUMBER | 1046 +-------+--------------------------------+ 1047 | u(?) | BLOCK SIZE INT | 1048 +-------+--------------------------------+ 1049 | u(?) | SAMPLE RATE INT | 1050 +-------+--------------------------------+ 1051 | u(8) | FRAME CRC | 1052 +-------+--------------------------------+ 1054 Table 18 1056 11.19.1. FRAME HEADER RESERVED 1058 +=======+=========================+ 1059 | Value | Description | 1060 +=======+=========================+ 1061 | 0 | mandatory value | 1062 +-------+-------------------------+ 1063 | 1 | reserved for future use | 1064 +-------+-------------------------+ 1066 Table 19 1068 FRAME HEADER RESERVED MUST remain reserved for "0" in order for a 1069 FLAC frame's initial 15 bits to be distinguishable from the start of 1070 an MPEG audio frame (see also (http://lists.xiph.org/pipermail/flac- 1071 dev/2008-December/002607.html)). 1073 11.19.2. BLOCKING STRATEGY 1075 +=======+==================================+ 1076 | Value | Description | 1077 +=======+==================================+ 1078 | 0 | fixed-blocksize stream; frame | 1079 | | header encodes the frame number | 1080 +-------+----------------------------------+ 1081 | 1 | variable-blocksize stream; frame | 1082 | | header encodes the sample number | 1083 +-------+----------------------------------+ 1085 Table 20 1087 The "BLOCKING STRATEGY" bit MUST be the same throughout the entire 1088 stream. 1090 The "BLOCKING STRATEGY" bit determines how to calculate the sample 1091 number of the first sample in the frame. If the bit is "0" (fixed- 1092 blocksize), the frame header encodes the frame number as above, and 1093 the frame's starting sample number will be the frame number times the 1094 blocksize. If it is "1" (variable-blocksize), the frame header 1095 encodes the frame's starting sample number itself. (In the case of a 1096 fixed-blocksize stream, only the last block MAY be shorter than the 1097 stream blocksize; its starting sample number will be calculated as 1098 the frame number times the previous frame's blocksize, or zero if it 1099 is the first frame). 1101 11.19.3. INTERCHANNEL SAMPLE BLOCK SIZE 1103 +=================+=========================================+ 1104 | Value | Description | 1105 +=================+=========================================+ 1106 | 0b0000 | reserved | 1107 +-----------------+-----------------------------------------+ 1108 | 0b0001 | 192 samples | 1109 +-----------------+-----------------------------------------+ 1110 | 0b0010 - 0b0101 | 576 * (2^(n-2)) samples, i.e. 576, | 1111 | | 1152, 2304 or 4608 | 1112 +-----------------+-----------------------------------------+ 1113 | 0b0110 | get 8 bit (blocksize-1) from end of | 1114 | | header | 1115 +-----------------+-----------------------------------------+ 1116 | 0b0111 | get 16 bit (blocksize-1) from end of | 1117 | | header | 1118 +-----------------+-----------------------------------------+ 1119 | 0b1000 - 0b1111 | 256 * (2^(n-8)) samples, i.e. 256, 512, | 1120 | | 1024, 2048, 4096, 8192, 16384 or 32768 | 1121 +-----------------+-----------------------------------------+ 1123 Table 21 1125 11.19.4. SAMPLE RATE 1127 +========+=====================================================+ 1128 | Value | Description | 1129 +========+=====================================================+ 1130 | 0b0000 | get from STREAMINFO metadata block | 1131 +--------+-----------------------------------------------------+ 1132 | 0b0001 | 88.2 kHz | 1133 +--------+-----------------------------------------------------+ 1134 | 0b0010 | 176.4 kHz | 1135 +--------+-----------------------------------------------------+ 1136 | 0b0011 | 192 kHz | 1137 +--------+-----------------------------------------------------+ 1138 | 0b0100 | 8 kHz | 1139 +--------+-----------------------------------------------------+ 1140 | 0b0101 | 16 kHz | 1141 +--------+-----------------------------------------------------+ 1142 | 0b0110 | 22.05 kHz | 1143 +--------+-----------------------------------------------------+ 1144 | 0b0111 | 24 kHz | 1145 +--------+-----------------------------------------------------+ 1146 | 0b1000 | 32 kHz | 1147 +--------+-----------------------------------------------------+ 1148 | 0b1001 | 44.1 kHz | 1149 +--------+-----------------------------------------------------+ 1150 | 0b1010 | 48 kHz | 1151 +--------+-----------------------------------------------------+ 1152 | 0b1011 | 96 kHz | 1153 +--------+-----------------------------------------------------+ 1154 | 0b1100 | get 8 bit sample rate (in kHz) from end of header | 1155 +--------+-----------------------------------------------------+ 1156 | 0b1101 | get 16 bit sample rate (in Hz) from end of header | 1157 +--------+-----------------------------------------------------+ 1158 | 0b1110 | get 16 bit sample rate (in daHz) from end of header | 1159 +--------+-----------------------------------------------------+ 1160 | 0b1111 | invalid, to prevent sync-fooling string of 1s | 1161 +--------+-----------------------------------------------------+ 1163 Table 22 1165 11.19.5. CHANNEL ASSIGNMENT 1167 For values 0b0000-0b0111, the value represents the (number of 1168 independent channels)-1. Where defined, the channel order follows 1169 SMPTE/ITU-R recommendations. 1171 +==========+======================================================+ 1172 | Value | Description | 1173 +==========+======================================================+ 1174 | 0b0000 | 1 channel: mono | 1175 +----------+------------------------------------------------------+ 1176 | 0b0001 | 2 channels: left, right | 1177 +----------+------------------------------------------------------+ 1178 | 0b0010 | 3 channels: left, right, center | 1179 +----------+------------------------------------------------------+ 1180 | 0b0011 | 4 channels: front left, front right, back left, back | 1181 | | right | 1182 +----------+------------------------------------------------------+ 1183 | 0b0100 | 5 channels: front left, front right, front center, | 1184 | | back/surround left, back/surround right | 1185 +----------+------------------------------------------------------+ 1186 | 0b0101 | 6 channels: front left, front right, front center, | 1187 | | LFE, back/surround left, back/surround right | 1188 +----------+------------------------------------------------------+ 1189 | 0b0110 | 7 channels: front left, front right, front center, | 1190 | | LFE, back center, side left, side right | 1191 +----------+------------------------------------------------------+ 1192 | 0b0111 | 8 channels: front left, front right, front center, | 1193 | | LFE, back left, back right, side left, side right | 1194 +----------+------------------------------------------------------+ 1195 | 0b1000 | left/side stereo: channel 0 is the left channel, | 1196 | | channel 1 is the side(difference) channel | 1197 +----------+------------------------------------------------------+ 1198 | 0b1001 | right/side stereo: channel 0 is the side(difference) | 1199 | | channel, channel 1 is the right channel | 1200 +----------+------------------------------------------------------+ 1201 | 0b1010 | mid/side stereo: channel 0 is the mid(average) | 1202 | | channel, channel 1 is the side(difference) channel | 1203 +----------+------------------------------------------------------+ 1204 | 0b1011 - | reserved | 1205 | 0b1111 | | 1206 +----------+------------------------------------------------------+ 1208 Table 23 1210 11.19.6. SAMPLE SIZE 1212 +=======+====================================+ 1213 | Value | Description | 1214 +=======+====================================+ 1215 | 0b000 | get from STREAMINFO metadata block | 1216 +-------+------------------------------------+ 1217 | 0b001 | 8 bits per sample | 1218 +-------+------------------------------------+ 1219 | 0b010 | 12 bits per sample | 1220 +-------+------------------------------------+ 1221 | 0b011 | reserved | 1222 +-------+------------------------------------+ 1223 | 0b100 | 16 bits per sample | 1224 +-------+------------------------------------+ 1225 | 0b101 | 20 bits per sample | 1226 +-------+------------------------------------+ 1227 | 0b110 | 24 bits per sample | 1228 +-------+------------------------------------+ 1229 | 0b111 | reserved | 1230 +-------+------------------------------------+ 1232 Table 24 1234 For subframes that encode a difference channel, the sample size is 1235 one bit larger than the sample size of the frame, in order to be able 1236 to encode the difference between extreme values. 1238 11.19.7. FRAME HEADER RESERVED2 1240 +=======+=========================+ 1241 | Value | Description | 1242 +=======+=========================+ 1243 | 0 | mandatory value | 1244 +-------+-------------------------+ 1245 | 1 | reserved for future use | 1246 +-------+-------------------------+ 1248 Table 25 1250 11.19.8. CODED NUMBER 1252 Frame/Sample numbers are encoded using the UTF-8 format, from BEFORE 1253 it was limited to 4 bytes by RFC3629, this variant supports the 1254 original 7 byte maximum. 1256 Note to implementors: All Unicode compliant UTF-8 decoders and 1257 encoders are limited to 4 bytes, it's best to just write your own one 1258 off solution. 1260 if(variable blocksize) 1261 `u(8...56)`: "UTF-8" coded sample number (decoded number is 36 bits) 1262 else 1263 `u(8...48)`: "UTF-8" coded frame number (decoded number is 31 bits) 1265 11.19.9. BLOCK SIZE INT 1267 if(`INTERCHANNEL SAMPLE BLOCK SIZE` == 0b0110) 1268 8 bit (blocksize-1) 1269 else if(`INTERCHANNEL SAMPLE BLOCK SIZE` == 0b0111) 1270 16 bit (blocksize-1) 1272 11.19.10. SAMPLE RATE INT 1274 if(`SAMPLE RATE` == 0b1100) 1275 8 bit sample rate (in kHz) 1276 else if(`SAMPLE RATE` == 0b1101) 1277 16 bit sample rate (in Hz) 1278 else if(`SAMPLE RATE` == 0b1110) 1279 16 bit sample rate (in daHz) 1281 11.19.11. FRAME CRC 1283 CRC-8 (polynomial = x^8 + x^2 + x^1 + x^0, initialized with 0) of 1284 everything before the CRC, including the sync code 1286 11.20. FRAME_FOOTER 1288 +=======+===================================================+ 1289 | Data | Description | 1290 +=======+===================================================+ 1291 | u(16) | CRC-16 (polynomial = x^16 + x^15 + x^2 + x^0, | 1292 | | initialized with 0) of everything before the CRC, | 1293 | | back to and including the frame header sync code | 1294 +-------+---------------------------------------------------+ 1296 Table 26 1298 11.21. SUBFRAME 1300 +============================================+======================+ 1301 | Data | Description | 1302 +============================================+======================+ 1303 | SUBFRAME_HEADER | | 1304 +--------------------------------------------+----------------------+ 1305 | "SUBFRAME_CONSTANT" || "SUBFRAME_FIXED" || | The SUBFRAME_HEADER | 1306 | "SUBFRAME_LPC" || "SUBFRAME_VERBATIM" | specifies which | 1307 | | one. | 1308 +--------------------------------------------+----------------------+ 1310 Table 27 1312 11.22. SUBFRAME_HEADER 1314 +========+========================================================+ 1315 | Data | Description | 1316 +========+========================================================+ 1317 | u(1) | Zero bit padding, to prevent sync-fooling string of 1s | 1318 +--------+--------------------------------------------------------+ 1319 | u(6) | "SUBFRAME TYPE" (see section on SUBFRAME TYPE | 1320 | | (#subframe-type)) | 1321 +--------+--------------------------------------------------------+ 1322 | u(1+k) | "WASTED BITS PER SAMPLE FLAG" (see section on WASTED | 1323 | | BITS PER SAMPLE FLAG (#wasted-bits-per-sample-flag)) | 1324 +--------+--------------------------------------------------------+ 1326 Table 28 1328 11.22.1. SUBFRAME TYPE 1330 +==========+================================+ 1331 | Value | Description | 1332 +==========+================================+ 1333 | 0b000000 | SUBFRAME_CONSTANT | 1334 +----------+--------------------------------+ 1335 | 0b000001 | SUBFRAME_VERBATIM | 1336 +----------+--------------------------------+ 1337 | 0b00001x | reserved | 1338 +----------+--------------------------------+ 1339 | 0b0001xx | reserved | 1340 +----------+--------------------------------+ 1341 | 0b001xxx | if(xxx <= 4) "SUBFRAME_FIXED", | 1342 | | xxx=order; else reserved | 1343 +----------+--------------------------------+ 1344 | 0b01xxxx | reserved | 1345 +----------+--------------------------------+ 1346 | 0b1xxxxx | "SUBFRAME_LPC", xxxxx=order-1 | 1347 +----------+--------------------------------+ 1349 Table 29 1351 11.22.2. WASTED BITS PER SAMPLE FLAG 1353 +=======+==============================================+ 1354 | Value | Description | 1355 +=======+==============================================+ 1356 | 0 | no wasted bits-per-sample in source | 1357 | | subblock, k=0 | 1358 +-------+----------------------------------------------+ 1359 | 1 | k wasted bits-per-sample in source subblock, | 1360 | | k-1 follows, unary coded; e.g. k=3 => 0b001 | 1361 | | follows, k=7 => 0b0000001 follows. | 1362 +-------+----------------------------------------------+ 1364 Table 30 1366 The size of the samples stored in the subframe is the subframe sample 1367 size reduced by k bits. Decoded samples must be shifted left by k 1368 bits. 1370 11.23. SUBFRAME_CONSTANT 1372 +======+========================================+ 1373 | Data | Description | 1374 +======+========================================+ 1375 | u(n) | Unencoded constant value of the | 1376 | | subblock, n = frame's bits-per-sample. | 1377 +------+----------------------------------------+ 1379 Table 31 1381 11.24. SUBFRAME_FIXED 1383 +==========+========================================+ 1384 | Data | Description | 1385 +==========+========================================+ 1386 | u(n) | Unencoded warm-up samples (n = frame's | 1387 | | bits-per-sample * predictor order). | 1388 +----------+----------------------------------------+ 1389 | RESIDUAL | Encoded residual | 1390 +----------+----------------------------------------+ 1392 Table 32 1394 11.25. SUBFRAME_LPC 1396 +==========+========================================================+ 1397 | Data | Description | 1398 +==========+========================================================+ 1399 | u(n) | Unencoded warm-up samples (n = frame's bits- | 1400 | | per-sample * lpc order). | 1401 +----------+--------------------------------------------------------+ 1402 | u(4) | (quantized linear predictor coefficients' | 1403 | | precision in bits)-1 (NOTE: 0b1111 is invalid). | 1404 +----------+--------------------------------------------------------+ 1405 | u(5) | Quantized linear predictor coefficient shift | 1406 | | needed in bits (NOTE: this number is signed | 1407 | | two's-complement). | 1408 +----------+--------------------------------------------------------+ 1409 | u(n) | Unencoded predictor coefficients (n = qlp coeff | 1410 | | precision * lpc order) (NOTE: the coefficients | 1411 | | are signed two's-complement). | 1412 +----------+--------------------------------------------------------+ 1413 | RESIDUAL | Encoded residual | 1414 +----------+--------------------------------------------------------+ 1416 Table 33 1418 11.26. SUBFRAME_VERBATIM 1420 +=========+===============================================+ 1421 | Data | Description | 1422 +=========+===============================================+ 1423 | u(n\*i) | Unencoded subblock, where "n" is frame's | 1424 | | bits-per-sample and "i" is frame's blocksize. | 1425 +---------+-----------------------------------------------+ 1427 Table 34 1429 11.27. RESIDUAL 1431 +==================================================+======================+ 1432 |Data |Description | 1433 +==================================================+======================+ 1434 |u(2) |RESIDUAL_CODING_METHOD| 1435 +--------------------------------------------------+----------------------+ 1436 |"RESIDUAL_CODING_METHOD_PARTITIONED_EXP_GOLOMB" ||| | 1437 |"RESIDUAL_CODING_METHOD_PARTITIONED_EXP_GOLOMB2" | | 1438 +--------------------------------------------------+----------------------+ 1440 Table 35 1442 11.27.1. RESIDUAL_CODING_METHOD 1444 +=======+========================================================+ 1445 | Value | Description | 1446 +=======+========================================================+ 1447 | 0b00 | partitioned Exp-Golomb coding with 4-bit Exp-Golomb | 1448 | | parameter; | 1449 | | RESIDUAL_CODING_METHOD_PARTITIONED_EXP_GOLOMB follows | 1450 +-------+--------------------------------------------------------+ 1451 | 0b01 | partitioned Exp-Golomb coding with 5-bit Exp-Golomb | 1452 | | parameter; | 1453 | | RESIDUAL_CODING_METHOD_PARTITIONED_EXP_GOLOMB2 follows | 1454 +-------+--------------------------------------------------------+ 1455 | 0b10 | reserved | 1456 | - | | 1457 | 0b11 | | 1458 +-------+--------------------------------------------------------+ 1460 Table 36 1462 11.27.2. RESIDUAL_CODING_METHOD_PARTITIONED_EXP_GOLOMB 1464 +=========================+===================================+ 1465 | Data | Description | 1466 +=========================+===================================+ 1467 | u(4) | Partition order. | 1468 +-------------------------+-----------------------------------+ 1469 | "EXP_GOLOMB_PARTITION"+ | There will be 2^order partitions. | 1470 +-------------------------+-----------------------------------+ 1472 Table 37 1474 11.27.2.1. EXP_GOLOMB_PARTITION 1476 +==========+====================================================+ 1477 | Data | Description | 1478 +==========+====================================================+ 1479 | u(4(+5)) | "EXP-GOLOMB PARTITION ENCODING PARAMETER" (see | 1480 | | section on EXP-GOLOMB PARTITION ENCODING PARAMETER | 1481 | | (#exp-golomb-partition-encoding-parameter)) | 1482 +----------+----------------------------------------------------+ 1483 | u(?) | "ENCODED RESIDUAL" (see section on ENCODED | 1484 | | RESIDUAL (#encoded-residual)) | 1485 +----------+----------------------------------------------------+ 1487 Table 38 1489 11.27.2.2. EXP GOLOMB PARTITION ENCODING PARAMETER 1491 +==========+==========================================+ 1492 | Value | Description | 1493 +==========+==========================================+ 1494 | 0b0000 - | Exp-golomb parameter. | 1495 | 0b1110 | | 1496 +----------+------------------------------------------+ 1497 | 0b1111 | Escape code, meaning the partition is in | 1498 | | unencoded binary form using n bits per | 1499 | | sample; n follows as a 5-bit number. | 1500 +----------+------------------------------------------+ 1502 Table 39 1504 11.27.3. RESIDUAL_CODING_METHOD_PARTITIONED_EXP_GOLOMB2 1506 +==========================+===================================+ 1507 | Data | Description | 1508 +==========================+===================================+ 1509 | u(4) | Partition order. | 1510 +--------------------------+-----------------------------------+ 1511 | "EXP-GOLOMB2_PARTITION"+ | There will be 2^order partitions. | 1512 +--------------------------+-----------------------------------+ 1514 Table 40 1516 11.27.3.1. EXP_GOLOMB2_PARTITION 1518 +==========+=====================================================+ 1519 | Data | Description | 1520 +==========+=====================================================+ 1521 | u(5(+5)) | "EXP-GOLOMB2 PARTITION ENCODING PARAMETER" (see | 1522 | | section on EXP-GOLOMB2 PARTITION ENCODING PARAMETER | 1523 | | (#expgolomb2-partition-encoding-parameter)) | 1524 +----------+-----------------------------------------------------+ 1525 | u(?) | "ENCODED RESIDUAL" (see section on ENCODED RESIDUAL | 1526 | | (#encoded-residual)) | 1527 +----------+-----------------------------------------------------+ 1529 Table 41 1531 11.27.3.2. EXP-GOLOMB2 PARTITION ENCODING PARAMETER 1533 +===========+==========================================+ 1534 | Value | Description | 1535 +===========+==========================================+ 1536 | 0b00000 - | Exp-golomb parameter. | 1537 | 0b11110 | | 1538 +-----------+------------------------------------------+ 1539 | 0b11111 | Escape code, meaning the partition is in | 1540 | | unencoded binary form using n bits per | 1541 | | sample; n follows as a 5-bit number. | 1542 +-----------+------------------------------------------+ 1544 Table 42 1546 11.27.4. ENCODED RESIDUAL 1548 The number of samples (n) in the partition is determined as follows: 1550 * if the partition order is zero, n = frame's blocksize - predictor 1551 order 1553 * else if this is not the first partition of the subframe, n = 1554 (frame's blocksize / (2^partition order)) 1556 * else n = (frame's blocksize / (2^partition order)) - predictor 1557 order 1559 12. Normative References 1561 [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate 1562 Requirement Levels", BCP 14, RFC 2119, 1563 DOI 10.17487/RFC2119, March 1997, 1564 . 1566 [RFC8174] Leiba, B., "Ambiguity of Uppercase vs Lowercase in RFC 1567 2119 Key Words", BCP 14, RFC 8174, DOI 10.17487/RFC8174, 1568 May 2017, . 1570 Authors' Addresses 1572 Michael Sandelman 1574 Email: mcr@sandelman.ca 1576 Andrew Weaver 1577 Email: theandrewjw@gmail.com