1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002 Josh Coalson
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #ifndef FLAC__FORMAT_H
21 #define FLAC__FORMAT_H
29 /* changing the following values to be higher will break the framing and hence the stream format, so DON'T! */
30 #define FLAC__MIN_BLOCK_SIZE (16u)
31 #define FLAC__MAX_BLOCK_SIZE (65535u)
32 #define FLAC__MAX_CHANNELS (8u)
33 #define FLAC__MIN_BITS_PER_SAMPLE (4u)
34 #define FLAC__MAX_BITS_PER_SAMPLE (32u)
36 * NOTE: the above value is the limit of the FLAC format. However, the
37 * reference encoder/decoder is currently limited to 24 bits because of
38 * prevalent 32-bit math, so make sure and use the following value when
41 #define FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE (24u)
42 /* the following is ((2 ** 16) - 1) * 10; see format.html as to why */
43 #define FLAC__MAX_SAMPLE_RATE (655350u)
44 #define FLAC__MAX_LPC_ORDER (32u)
45 #define FLAC__MIN_QLP_COEFF_PRECISION (5u)
46 /* changing FLAC__MAX_FIXED_ORDER also means changing all of fixed.c and more, so DON'T! */
47 #define FLAC__MAX_FIXED_ORDER (4u)
48 #define FLAC__MAX_RICE_PARTITION_ORDER (15u)
50 /* VERSION should come from configure */
52 #define FLAC__VERSION_STRING VERSION
55 extern const FLAC__byte FLAC__STREAM_SYNC_STRING[4]; /* = "fLaC" */;
56 extern const unsigned FLAC__STREAM_SYNC; /* = 0x664C6143 */;
57 extern const unsigned FLAC__STREAM_SYNC_LEN; /* = 32 bits */;
59 #define FLAC__STREAM_SYNC_LENGTH (4u)
62 /*****************************************************************************
64 * NOTE: Within the bitstream, all fixed-width numbers are big-endian coded.
65 * All numbers are unsigned unless otherwise noted.
67 * NOTE: It's not the best convention, but symbols ending in _LEN are in bits
68 * and _LENGTH are in bytes. _LENGTH symbols are #defines instead of
69 * global variables because they are usually when declaring arrays and
70 * some compilers require compile-time knowledge of array sizes when
71 * declared on the stack.
73 *****************************************************************************/
76 /*****************************************************************************
80 *****************************************************************************/
82 /*****************************************************************************/
85 FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE = 0
86 } FLAC__EntropyCodingMethodType;
87 extern const char * const FLAC__EntropyCodingMethodTypeString[];
89 /*****************************************************************************
91 * 4: partition order => (2 ** order) subdivisions
95 unsigned parameters[1 << FLAC__MAX_RICE_PARTITION_ORDER];
96 unsigned raw_bits[1 << FLAC__MAX_RICE_PARTITION_ORDER];
97 } FLAC__EntropyCodingMethod_PartitionedRice;
99 extern const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN; /* = 4 bits */
100 extern const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; /* = 4 bits */
101 extern const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN; /* = 5 bits */
103 extern const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER; /* = (1<<FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN)-1 */
105 /*****************************************************************************
107 * 2: entropy coding method:
108 * 00: partitioned rice coding
110 * ?: entropy coding method data
113 FLAC__EntropyCodingMethodType type;
115 FLAC__EntropyCodingMethod_PartitionedRice partitioned_rice;
117 } FLAC__EntropyCodingMethod;
119 extern const unsigned FLAC__ENTROPY_CODING_METHOD_TYPE_LEN; /* = 2 bits */
121 /*****************************************************************************/
124 FLAC__SUBFRAME_TYPE_CONSTANT = 0,
125 FLAC__SUBFRAME_TYPE_VERBATIM = 1,
126 FLAC__SUBFRAME_TYPE_FIXED = 2,
127 FLAC__SUBFRAME_TYPE_LPC = 3
128 } FLAC__SubframeType;
129 extern const char * const FLAC__SubframeTypeString[];
131 /*****************************************************************************
133 * n: constant value for signal; n = frame's bits-per-sample
137 } FLAC__Subframe_Constant;
139 /*****************************************************************************
141 * n*i: unencoded signal; n = frame's bits-per-sample, i = frame's blocksize
144 const FLAC__int32 *data;
145 } FLAC__Subframe_Verbatim;
147 /*****************************************************************************
149 * n: unencoded warm-up samples (n = fixed-predictor order * bits per sample)
150 * ?: entropy coding method info
151 * ?: encoded residual ((blocksize minus fixed-predictor order) samples)
152 * The order is stored in the main subframe header
155 FLAC__EntropyCodingMethod entropy_coding_method;
157 FLAC__int32 warmup[FLAC__MAX_FIXED_ORDER];
158 const FLAC__int32 *residual;
159 } FLAC__Subframe_Fixed;
161 /*****************************************************************************
163 * n: unencoded warm-up samples (n = lpc order * bits per sample)
164 * 4: (qlp coeff precision in bits)-1 (1111 = invalid, use to check for erroneous sync)
165 * 5: qlp shift needed in bits (signed)
166 * n: unencoded predictor coefficients (n = lpc order * qlp coeff precision)
167 * ?: entropy coding method info
168 * ?: encoded residual ((blocksize minus lpc order) samples)
169 * The order is stored in the main subframe header
172 FLAC__EntropyCodingMethod entropy_coding_method;
174 unsigned qlp_coeff_precision;
175 int quantization_level;
176 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
177 FLAC__int32 warmup[FLAC__MAX_LPC_ORDER];
178 const FLAC__int32 *residual;
179 } FLAC__Subframe_LPC;
181 extern const unsigned FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN; /* = 4 bits */
182 extern const unsigned FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN; /* = 5 bits */
184 /*****************************************************************************
186 * 1: zero pad, to prevent sync-fooling string of 1s (use to check for erroneous sync)
188 * 000000: constant value
192 * 001xxx: fixed predictor, xxx=order <= 4, else reserved
194 * 1xxxxx: lpc, xxxxx=order-1
195 * 1: 'wasted bits' flag
196 * 0: no wasted bits in source subblock
197 * 1: all samples in source subblock contain n 0 least significant bits. n-1 follows, unary coded, i.e. n=3, 001 follows, n=7, 0000001 follows.
198 * ?: unary coded (n-1)
199 * ?: subframe-specific data (c.f. FLAC__Subframe_*)
202 FLAC__SubframeType type;
204 FLAC__Subframe_Constant constant;
205 FLAC__Subframe_Fixed fixed;
206 FLAC__Subframe_LPC lpc;
207 FLAC__Subframe_Verbatim verbatim;
209 unsigned wasted_bits;
212 extern const unsigned FLAC__SUBFRAME_ZERO_PAD_LEN; /* = 1 bit */
213 extern const unsigned FLAC__SUBFRAME_TYPE_LEN; /* = 6 bits */
214 extern const unsigned FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN; /* = 1 bit */
216 extern const unsigned FLAC__SUBFRAME_TYPE_CONSTANT_BYTE_ALIGNED_MASK; /* = 0x00 */
217 extern const unsigned FLAC__SUBFRAME_TYPE_VERBATIM_BYTE_ALIGNED_MASK; /* = 0x02 */
218 extern const unsigned FLAC__SUBFRAME_TYPE_FIXED_BYTE_ALIGNED_MASK; /* = 0x10 */
219 extern const unsigned FLAC__SUBFRAME_TYPE_LPC_BYTE_ALIGNED_MASK; /* = 0x40 */
221 /*****************************************************************************/
224 /*****************************************************************************
228 *****************************************************************************/
231 FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT = 0,
232 FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE = 1,
233 FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE = 2,
234 FLAC__CHANNEL_ASSIGNMENT_MID_SIDE = 3
235 } FLAC__ChannelAssignment;
236 extern const char * const FLAC__ChannelAssignmentString[];
239 FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER,
240 FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER
241 } FLAC__FrameNumberType;
242 extern const char * const FLAC__FrameNumberTypeString[];
244 /*****************************************************************************
246 * 14: sync code '11111111111110'
248 * 00: currently required value
250 * 4: blocksize in samples
251 * 0000: get from stream header => implies constant blocksize throughout stream
252 * 0001: 192 samples (AES/EBU) => implies constant blocksize throughout stream
253 * 0010-0101: 576 * (2^(n-2)) samples, i.e. 576/1152/2304/4608 => implies constant blocksize throughout stream
254 * 0110: get 8 bit (blocksize-1) from end of header => possibly variable blocksize throughout stream unless it's the last frame
255 * 0111: get 16 bit (blocksize-1) from end of header => possibly variable blocksize throughout stream unless it's the last frame
256 * 1000-1111: 256 * (2^(n-8)) samples, i.e. 256/512/1024/2048/4096/8192/16384/32768 => implies constant blocksize throughout stream
258 * 0000: get from stream header
259 * 0001-0011: reserved
268 * 1100: get 8 bit sample rate (in kHz) from end of header
269 * 1101: get 16 bit sample rate (in Hz) from end of header
270 * 1110: get 16 bit sample rate (in tens of Hz) from end of header
271 * 1111: invalid, to prevent sync-fooling string of 1s (use to check for erroneous sync)
272 * 4: channel assignment
273 * 0000-0111: (number of independent channels)-1. when == 0001, channel 0 is the left channel and channel 1 is the right
274 * 1000: left/side stereo : channel 0 is the left channel, channel 1 is the side(difference) channel
275 * 1001: right/side stereo: channel 0 is the side(difference) channel, channel 1 is the right channel
276 * 1010: mid/side stereo : channel 0 is the mid(average) channel, channel 1 is the side(difference) channel
277 * 1011-1111: reserved
278 * 3: sample size in bits
279 * 000: get from stream header
280 * 001: 8 bits per sample
281 * 010: 12 bits per sample
283 * 100: 16 bits per sample
284 * 101: 20 bits per sample
285 * 110: 24 bits per sample
287 * 1: zero pad, to prevent sync-fooling string of 1s (use to check for erroneous sync)
288 * ?: if(variable blocksize)
289 * 8-56: 'UTF-8' coded sample number (decoded number is 0-36 bits) (use to check for erroneous sync)
291 * 8-48: 'UTF-8' coded frame number (decoded number is 0-31 bits) (use to check for erroneous sync)
292 * ?: if(blocksize bits == 11x)
293 * 8/16 bit (blocksize-1)
294 * ?: if(sample rate bits == 11xx)
295 * 8/16 bit sample rate
296 * 8: CRC-8 (polynomial = x^8 + x^2 + x^1 + x^0, initialized with 0) of everything before the crc, including the sync code
299 unsigned blocksize; /* in samples */
300 unsigned sample_rate; /* in Hz */
302 FLAC__ChannelAssignment channel_assignment;
303 unsigned bits_per_sample;
304 FLAC__FrameNumberType number_type;
306 FLAC__uint32 frame_number;
307 FLAC__uint64 sample_number;
312 extern const unsigned FLAC__FRAME_HEADER_SYNC; /* = 0x3ffe */
313 extern const unsigned FLAC__FRAME_HEADER_SYNC_LEN; /* = 14 bits */
314 extern const unsigned FLAC__FRAME_HEADER_RESERVED_LEN; /* = 2 bits */
315 extern const unsigned FLAC__FRAME_HEADER_BLOCK_SIZE_LEN; /* = 4 bits */
316 extern const unsigned FLAC__FRAME_HEADER_SAMPLE_RATE_LEN; /* = 4 bits */
317 extern const unsigned FLAC__FRAME_HEADER_CHANNEL_ASSIGNMENT_LEN; /* = 4 bits */
318 extern const unsigned FLAC__FRAME_HEADER_BITS_PER_SAMPLE_LEN; /* = 3 bits */
319 extern const unsigned FLAC__FRAME_HEADER_ZERO_PAD_LEN; /* = 1 bit */
320 extern const unsigned FLAC__FRAME_HEADER_CRC_LEN; /* = 8 bits */
322 /*****************************************************************************
324 * 16: CRC-16 (polynomial = x^16 + x^15 + x^2 + x^0, initialized with 0) of everything before the crc, back to and including the frame header sync code
330 extern const unsigned FLAC__FRAME_FOOTER_CRC_LEN; /* = 16 bits */
333 FLAC__FrameHeader header;
334 FLAC__Subframe subframes[FLAC__MAX_CHANNELS];
335 FLAC__FrameFooter footer;
338 /*****************************************************************************/
341 /*****************************************************************************
343 * Meta-data structures
345 *****************************************************************************/
348 FLAC__METADATA_TYPE_STREAMINFO = 0,
349 FLAC__METADATA_TYPE_PADDING = 1,
350 FLAC__METADATA_TYPE_APPLICATION = 2,
351 FLAC__METADATA_TYPE_SEEKTABLE = 3,
352 FLAC__METADATA_TYPE_VORBIS_COMMENT = 4
353 } FLAC__MetadataType;
354 extern const char * const FLAC__MetadataTypeString[];
356 /*****************************************************************************
358 * 16: minimum blocksize (in samples) of all blocks in the stream
359 * 16: maximum blocksize (in samples) of all blocks in the stream
360 * 24: minimum framesize (in bytes) of all frames in the stream; 0 => unknown
361 * 24: maximum framesize (in bytes) of all frames in the stream; 0 => unknown
362 * 20: sample rate in Hz, 0 is invalid
363 * 3: (number of channels)-1
364 * 5: (bits per sample)-1
365 * 36: total samples, 0 => unknown
366 *128: MD5 digest of the original unencoded audio data
367 *---- -----------------
371 unsigned min_blocksize, max_blocksize;
372 unsigned min_framesize, max_framesize;
373 unsigned sample_rate;
375 unsigned bits_per_sample;
376 FLAC__uint64 total_samples;
377 FLAC__byte md5sum[16];
378 } FLAC__StreamMetadata_StreamInfo;
380 extern const unsigned FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN; /* = 16 bits */
381 extern const unsigned FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN; /* = 16 bits */
382 extern const unsigned FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN; /* = 24 bits */
383 extern const unsigned FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN; /* = 24 bits */
384 extern const unsigned FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN; /* = 20 bits */
385 extern const unsigned FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN; /* = 3 bits */
386 extern const unsigned FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN; /* = 5 bits */
387 extern const unsigned FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN; /* = 36 bits */
388 extern const unsigned FLAC__STREAM_METADATA_STREAMINFO_MD5SUM_LEN; /* = 128 bits */
390 #define FLAC__STREAM_METADATA_STREAMINFO_LENGTH (34u)
392 /*****************************************************************************
395 *----- -----------------
399 int dummy; /* conceptually this is an empty struct since we don't store the padding bytes */
400 /* empty structs are allowed by C++ but not C, hence the 'dummy' */
401 } FLAC__StreamMetadata_Padding;
403 /*****************************************************************************
405 * 32: Registered application ID
406 * n: Application data
407 *------- -----------------
413 } FLAC__StreamMetadata_Application;
415 extern const unsigned FLAC__STREAM_METADATA_APPLICATION_ID_LEN; /* = 32 bits */
417 /*****************************************************************************
419 * 64: sample number of target frame
420 * 64: offset, in bytes, of target frame with respect to beginning of first frame
421 * 16: number of samples in the target frame
422 *----- -----------------
426 FLAC__uint64 sample_number;
427 FLAC__uint64 stream_offset;
428 unsigned frame_samples;
429 } FLAC__StreamMetadata_SeekPoint;
431 extern const unsigned FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN; /* = 64 bits */
432 extern const unsigned FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN; /* = 64 bits */
433 extern const unsigned FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN; /* = 16 bits */
435 #define FLAC__STREAM_METADATA_SEEKPOINT_LENGTH (18u)
437 extern const FLAC__uint64 FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER; /* = 0xffffffffffffffff */
439 /*****************************************************************************
441 * 0: num_points is implied by the metadata block 'length' field (i.e. num_points = length / 18)
442 * n*18*8: seek points (n = num_points, 18 is the size of a seek point in bytes)
443 * ------- -----------------
446 * NOTE: the seek points must be sorted by ascending sample number.
447 * NOTE: each seek point's sample number must be the first sample of the target frame.
448 * NOTE: each seek point's sample number must be unique within the table.
449 * NOTE: existence of a SEEKTABLE block implies a correct setting of total_samples in the stream_info block.
450 * NOTE: behavior is undefined when more than one SEEKTABLE block is present in a stream.
454 FLAC__StreamMetadata_SeekPoint *points;
455 } FLAC__StreamMetadata_SeekTable;
457 /*****************************************************************************
459 * 32: Entry length in bytes (WATCHOUT: this is little-endian coded)
460 * n: Entry (n = 8 * length)
461 *-------- -----------------
467 } FLAC__StreamMetadata_VorbisComment_Entry;
469 extern const unsigned FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN; /* = 32 bits */
471 /*****************************************************************************
473 * n: Vendor string entry
474 * 32: Number of comment fields (WATCHOUT: this is little-endian coded)
476 *------------ -----------------
477 * (32+m+n)/8 bytes total
480 FLAC__StreamMetadata_VorbisComment_Entry vendor_string;
481 FLAC__uint32 num_comments;
482 FLAC__StreamMetadata_VorbisComment_Entry *comments;
483 } FLAC__StreamMetadata_VorbisComment;
485 extern const unsigned FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN; /* = 32 bits */
487 /*****************************************************************************
489 * 1: =1 if this is the last meta-data block, else =0
490 * 7: meta-data type (c.f. FLAC__MetadataType)
491 * 24: length (in bytes) of the block-specific data to follow
492 *---- -----------------
496 FLAC__MetadataType type;
498 unsigned length; /* in bytes */
500 FLAC__StreamMetadata_StreamInfo stream_info;
501 FLAC__StreamMetadata_Padding padding;
502 FLAC__StreamMetadata_Application application;
503 FLAC__StreamMetadata_SeekTable seek_table;
504 FLAC__StreamMetadata_VorbisComment vorbis_comment;
506 } FLAC__StreamMetadata;
508 extern const unsigned FLAC__STREAM_METADATA_IS_LAST_LEN; /* = 1 bit */
509 extern const unsigned FLAC__STREAM_METADATA_TYPE_LEN; /* = 7 bits */
510 extern const unsigned FLAC__STREAM_METADATA_LENGTH_LEN; /* = 24 bits */
512 #define FLAC__STREAM_METADATA_HEADER_LENGTH (4u)
514 /*****************************************************************************/
517 /*****************************************************************************
521 *****************************************************************************/
524 * Since the rules for valid sample rates are slightly complex, they are
527 FLAC__bool FLAC__format_is_valid_sample_rate(unsigned sample_rate);