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