fix size of application id in struct
[platform/upstream/flac.git] / include / FLAC / format.h
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000,2001  Josh Coalson
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #ifndef FLAC__FORMAT_H
21 #define FLAC__FORMAT_H
22
23 #include "ordinals.h"
24
25 /* changing the following values to be higher will break the framing and hence the stream format, so DON'T! */
26 #define FLAC__MIN_BLOCK_SIZE (16u)
27 #define FLAC__MAX_BLOCK_SIZE (65535u)
28 #define FLAC__MAX_CHANNELS (8u)
29 #define FLAC__MIN_BITS_PER_SAMPLE (4u)
30 /*NOTE: only up to 24 because of the current predictor coefficient quantization and the fact we use int32s for all work */
31 #define FLAC__MAX_BITS_PER_SAMPLE (24u)
32 /* the following is ((2 ** 20) - 1) div 10 */
33 #define FLAC__MAX_SAMPLE_RATE (1048570u)
34 #define FLAC__MAX_LPC_ORDER (32u)
35 #define FLAC__MIN_QLP_COEFF_PRECISION (5u)
36 /* changing FLAC__MAX_FIXED_ORDER also means changing all of fixed.c and more, so DON'T! */
37 #define FLAC__MAX_FIXED_ORDER (4u)
38 #define FLAC__MAX_RICE_PARTITION_ORDER (15u)
39
40 /* VERSION should come from configure */
41 #ifdef VERSION
42 #define FLAC__VERSION_STRING VERSION
43 #else
44 #define FLAC__VERSION_STRING "0.10"
45 #endif
46
47 extern const byte     FLAC__STREAM_SYNC_STRING[4]; /* = "fLaC" */;
48 extern const unsigned FLAC__STREAM_SYNC; /* = 0x664C6143 */;
49 extern const unsigned FLAC__STREAM_SYNC_LEN; /* = 32 bits */;
50
51
52 /*****************************************************************************
53  *
54  * NOTE: Within the bitstream, all fixed-width numbers are big-endian coded.
55  *       All numbers are unsigned unless otherwise noted.
56  *
57  *****************************************************************************/
58
59
60 /*****************************************************************************
61  *
62  * Subframe structures
63  *
64  *****************************************************************************/
65
66 /*****************************************************************************/
67
68 typedef enum {
69         FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE = 0
70 } FLAC__EntropyCodingMethodType;
71 extern const char *FLAC__EntropyCodingMethodTypeString[];
72
73 /*****************************************************************************
74  *
75  *  4: partition order => (2 ** order) subdivisions
76  */
77 typedef struct {
78         unsigned order;
79         unsigned parameters[1 << FLAC__MAX_RICE_PARTITION_ORDER];
80         unsigned raw_bits[1 << FLAC__MAX_RICE_PARTITION_ORDER];
81 } FLAC__EntropyCodingMethod_PartitionedRice;
82
83 extern const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN; /* = 4 bits */
84 extern const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; /* = 4 bits */
85 extern const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN; /* = 5 bits */
86
87 extern const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER; /* = (1<<FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN)-1 */
88
89 /*****************************************************************************
90  *
91  *  2: entropy coding method:
92  *     00: partitioned rice coding
93  *     01-11: reserved
94  *  ?: entropy coding method data
95  */
96 typedef struct {
97         FLAC__EntropyCodingMethodType type;
98         union {
99                 FLAC__EntropyCodingMethod_PartitionedRice partitioned_rice;
100         } data;
101 } FLAC__EntropyCodingMethod;
102
103 extern const unsigned FLAC__ENTROPY_CODING_METHOD_TYPE_LEN; /* = 2 bits */
104
105 /*****************************************************************************/
106
107 typedef enum {
108         FLAC__SUBFRAME_TYPE_CONSTANT = 0,
109         FLAC__SUBFRAME_TYPE_VERBATIM = 1,
110         FLAC__SUBFRAME_TYPE_FIXED = 2,
111         FLAC__SUBFRAME_TYPE_LPC = 3
112 } FLAC__SubframeType;
113 extern const char *FLAC__SubframeTypeString[];
114
115 /*****************************************************************************
116  *
117  * n: constant value for signal; n = frame's bits-per-sample
118  */
119 typedef struct {
120         int32 value;
121 } FLAC__Subframe_Constant;
122
123 /*****************************************************************************
124  *
125  * n*i: unencoded signal; n = frame's bits-per-sample, i = frame's blocksize
126  */
127 typedef struct {
128         const int32 *data;
129 } FLAC__Subframe_Verbatim;
130
131 /*****************************************************************************
132  *
133  *  n: unencoded warm-up samples (n = fixed-predictor order * bits per sample)
134  *  ?: entropy coding method info
135  *  ?: encoded residual ((blocksize minus fixed-predictor order) samples)
136  *  The order is stored in the main subframe header
137  */
138 typedef struct {
139         FLAC__EntropyCodingMethod entropy_coding_method;
140         unsigned order;
141         int32 warmup[FLAC__MAX_FIXED_ORDER];
142         const int32 *residual;
143 } FLAC__Subframe_Fixed;
144
145 /*****************************************************************************
146  *
147  *  n: unencoded warm-up samples (n = lpc order * bits per sample)
148  *  4: (qlp coeff precision in bits)-1 (1111 = invalid, use to check for erroneous sync)
149  *  5: qlp shift needed in bits (signed)
150  *  n: unencoded predictor coefficients (n = lpc order * qlp coeff precision)
151  *  ?: entropy coding method info
152  *  ?: encoded residual ((blocksize minus lpc order) samples)
153  *  The order is stored in the main subframe header
154  */
155 typedef struct {
156         FLAC__EntropyCodingMethod entropy_coding_method;
157         unsigned order;
158         unsigned qlp_coeff_precision;
159         int quantization_level;
160         int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
161         int32 warmup[FLAC__MAX_LPC_ORDER];
162         const int32 *residual;
163 } FLAC__Subframe_LPC;
164
165 extern const unsigned FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN; /* = 4 bits */
166 extern const unsigned FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN; /* = 5 bits */
167
168 /*****************************************************************************
169  *
170  *  1: zero pad, to prevent sync-fooling string of 1s (use to check for erroneous sync)
171  *  6: subframe type
172  *       000000: constant value
173  *       000001: verbatim
174  *       00001x: reserved
175  *       0001xx: reserved
176  *       001xxx: fixed predictor, xxx=order <= 4, else reserved
177  *       01xxxx: reserved
178  *       1xxxxx: lpc, xxxxx=order-1
179  *  1: 'wasted bits' flag
180  *       0: no wasted bits in source subblock
181  *       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.
182  *             ?: unary coded (n-1)
183  *  ?: subframe-specific data (c.f. FLAC__Subframe_*)
184  */
185 typedef struct {
186         FLAC__SubframeType type;
187         union {
188                 FLAC__Subframe_Constant constant;
189                 FLAC__Subframe_Fixed fixed;
190                 FLAC__Subframe_LPC lpc;
191                 FLAC__Subframe_Verbatim verbatim;
192         } data;
193         unsigned wasted_bits;
194 } FLAC__Subframe;
195
196 extern const unsigned FLAC__SUBFRAME_ZERO_PAD_LEN; /* = 1 bit */
197 extern const unsigned FLAC__SUBFRAME_TYPE_LEN; /* = 6 bits */
198 extern const unsigned FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN; /* = 1 bit */
199
200 extern const unsigned FLAC__SUBFRAME_TYPE_CONSTANT_BYTE_ALIGNED_MASK; /* = 0x00 */
201 extern const unsigned FLAC__SUBFRAME_TYPE_VERBATIM_BYTE_ALIGNED_MASK; /* = 0x02 */
202 extern const unsigned FLAC__SUBFRAME_TYPE_FIXED_BYTE_ALIGNED_MASK; /* = 0x10 */
203 extern const unsigned FLAC__SUBFRAME_TYPE_LPC_BYTE_ALIGNED_MASK; /* = 0x40 */
204
205 /*****************************************************************************/
206
207
208 /*****************************************************************************
209  *
210  * Frame structures
211  *
212  *****************************************************************************/
213
214 typedef enum {
215         FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT = 0,
216         FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE = 1,
217         FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE = 2,
218         FLAC__CHANNEL_ASSIGNMENT_MID_SIDE = 3
219 } FLAC__ChannelAssignment;
220 extern const char *FLAC__ChannelAssignmentString[];
221
222 /*****************************************************************************
223  *
224  * 14: sync code '11111111111110'
225  *  2: reserved
226  *        00: currently required value
227  *        01-11: reserved
228  *  4: blocksize in samples
229  *        0000: get from stream header => implies constant blocksize throughout stream
230  *        0001: 192 samples (AES/EBU) => implies constant blocksize throughout stream
231  *        0010-0101: 576 * (2^(n-2)) samples, i.e. 576/1152/2304/4608 => implies constant blocksize throughout stream
232  *        0110: get 8 bit (blocksize-1) from end of header => possibly variable blocksize throughout stream unless it's the last frame
233  *        0111: get 16 bit (blocksize-1) from end of header => possibly variable blocksize throughout stream unless it's the last frame
234  *        1000-1111: 256 * (2^(n-8)) samples, i.e. 256/512/1024/2048/4096/8192/16384/32768 => implies constant blocksize throughout stream
235  *  4: sample rate:
236  *        0000: get from stream header
237  *        0001-0011: reserved
238  *        0100: 8kHz
239  *        0101: 16kHz
240  *        0110: 22.05kHz
241  *        0111: 24kHz
242  *        1000: 32kHz
243  *        1001: 44.1kHz
244  *        1010: 48kHz
245  *        1011: 96kHz
246  *        1100: get 8 bit sample rate (in kHz) from end of header
247  *        1101: get 16 bit sample rate (in Hz) from end of header
248  *        1110: get 16 bit sample rate (in tens of Hz) from end of header
249  *        1111: invalid, to prevent sync-fooling string of 1s (use to check for erroneous sync)
250  *  4: channel assignment
251  *     0000-0111: (number of independent channels)-1.  when == 0001, channel 0 is the left channel and channel 1 is the right
252  *     1000: left/side stereo : channel 0 is the left             channel, channel 1 is the side(difference) channel
253  *     1001: right/side stereo: channel 0 is the side(difference) channel, channel 1 is the right            channel
254  *     1010: mid/side stereo  : channel 0 is the mid(average)     channel, channel 1 is the side(difference) channel
255  *     1011-1111: reserved
256  *  3: sample size in bits
257  *        000: get from stream header
258  *        001: 8 bits per sample
259  *        010: 12 bits per sample
260  *        011: reserved
261  *        100: 16 bits per sample
262  *        101: 20 bits per sample
263  *        110: 24 bits per sample
264  *        111: reserved
265  *  1: zero pad, to prevent sync-fooling string of 1s (use to check for erroneous sync)
266  *  ?: if(variable blocksize)
267  *        8-56: 'UTF-8' coded sample number (decoded number is 0-36 bits) (use to check for erroneous sync)
268  *     else
269  *        8-48: 'UTF-8' coded frame number (decoded number is 0-31 bits) (use to check for erroneous sync)
270  *  ?: if(blocksize bits == 11x)
271  *        8/16 bit (blocksize-1)
272  *  ?: if(sample rate bits == 11xx)
273  *        8/16 bit sample rate
274  *  8: CRC-8 (polynomial = x^8 + x^2 + x^1 + x^0, initialized with 0) of everything before the crc, including the sync code
275  */
276 typedef struct {
277         unsigned blocksize; /* in samples */
278         unsigned sample_rate; /* in Hz */
279         unsigned channels;
280         FLAC__ChannelAssignment channel_assignment;
281         unsigned bits_per_sample;
282         union {
283                 uint32 frame_number;
284                 uint64 sample_number;
285         } number;
286         uint8 crc;
287 } FLAC__FrameHeader;
288
289 extern const unsigned FLAC__FRAME_HEADER_SYNC; /* = 0x3ffe */
290 extern const unsigned FLAC__FRAME_HEADER_SYNC_LEN; /* = 14 bits */
291 extern const unsigned FLAC__FRAME_HEADER_RESERVED_LEN; /* = 2 bits */
292 extern const unsigned FLAC__FRAME_HEADER_BLOCK_SIZE_LEN; /* = 4 bits */
293 extern const unsigned FLAC__FRAME_HEADER_SAMPLE_RATE_LEN; /* = 4 bits */
294 extern const unsigned FLAC__FRAME_HEADER_CHANNEL_ASSIGNMENT_LEN; /* = 4 bits */
295 extern const unsigned FLAC__FRAME_HEADER_BITS_PER_SAMPLE_LEN; /* = 3 bits */
296 extern const unsigned FLAC__FRAME_HEADER_ZERO_PAD_LEN; /* = 1 bit */
297 extern const unsigned FLAC__FRAME_HEADER_CRC_LEN; /* = 8 bits */
298
299 /*****************************************************************************
300  *
301  * 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
302  */
303 typedef struct {
304         uint16 crc;
305 } FLAC__FrameFooter;
306
307 extern const unsigned FLAC__FRAME_FOOTER_CRC_LEN; /* = 16 bits */
308
309 typedef struct {
310         FLAC__FrameHeader header;
311         FLAC__Subframe subframes[FLAC__MAX_CHANNELS];
312         FLAC__FrameFooter footer;
313 } FLAC__Frame;
314
315 /*****************************************************************************/
316
317
318 /*****************************************************************************
319  *
320  * Meta-data structures
321  *
322  *****************************************************************************/
323
324 typedef enum {
325         FLAC__METADATA_TYPE_STREAMINFO = 0,
326         FLAC__METADATA_TYPE_PADDING = 1,
327         FLAC__METADATA_TYPE_APPLICATION = 2,
328         FLAC__METADATA_TYPE_SEEKTABLE = 3
329 } FLAC__MetaDataType;
330 extern const char *FLAC__MetaDataTypeString[];
331
332 /*****************************************************************************
333  *
334  * 16: minimum blocksize (in samples) of all blocks in the stream
335  * 16: maximum blocksize (in samples) of all blocks in the stream
336  * 24: minimum framesize (in bytes) of all frames in the stream; 0 => unknown
337  * 24: maximum framesize (in bytes) of all frames in the stream; 0 => unknown
338  * 20: sample rate in Hz, 0 is invalid
339  *  3: (number of channels)-1
340  *  5: (bits per sample)-1
341  * 36: total samples, 0 => unknown
342  *128: MD5 digest of the original unencoded audio data
343  *---- -----------------
344  * 34  bytes total
345  */
346 typedef struct {
347         unsigned min_blocksize, max_blocksize;
348         unsigned min_framesize, max_framesize;
349         unsigned sample_rate;
350         unsigned channels;
351         unsigned bits_per_sample;
352         uint64 total_samples;
353         byte md5sum[16];
354 } FLAC__StreamMetaData_StreamInfo;
355
356 extern const unsigned FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN; /* = 16 bits */
357 extern const unsigned FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN; /* = 16 bits */
358 extern const unsigned FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN; /* = 24 bits */
359 extern const unsigned FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN; /* = 24 bits */
360 extern const unsigned FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN; /* = 20 bits */
361 extern const unsigned FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN; /* = 3 bits */
362 extern const unsigned FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN; /* = 5 bits */
363 extern const unsigned FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN; /* = 36 bits */
364 extern const unsigned FLAC__STREAM_METADATA_STREAMINFO_MD5SUM_LEN; /* = 128 bits */
365 extern const unsigned FLAC__STREAM_METADATA_STREAMINFO_LENGTH; /* = 34 bytes */
366
367 /*****************************************************************************
368  *
369  *   n: '0' bits
370  *----- -----------------
371  * n/8  bytes total
372  */
373 typedef struct {
374         int dummy; /* conceptually this is an empty struct since we don't store the padding bytes */
375                    /* empty structs are allowed by C++ but not C, hence the 'dummy' */
376 } FLAC__StreamMetaData_Padding;
377
378 /*****************************************************************************
379  *
380  *    32: Registered application ID
381  *     n: Application data
382  *------- -----------------
383  * 4+n/8  bytes total
384  */
385 typedef struct {
386         byte id[4];
387         byte *data;
388 } FLAC__StreamMetaData_Application;
389
390 extern const unsigned FLAC__STREAM_METADATA_APPLICATION_ID_LEN; /* = 32 bits */
391
392 /*****************************************************************************
393  *
394  *  64: sample number of target frame
395  *  64: offset, in bytes, of target frame with respect to beginning of first frame
396  *  16: number of samples in the target frame
397  *----- -----------------
398  *  18  bytes total
399  */
400 typedef struct {
401         uint64 sample_number;
402         uint64 stream_offset;
403         unsigned frame_samples;
404 } FLAC__StreamMetaData_SeekPoint;
405
406 extern const unsigned FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN; /* = 64 bits */
407 extern const unsigned FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN; /* = 64 bits */
408 extern const unsigned FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN; /* = 16 bits */
409 extern const unsigned FLAC__STREAM_METADATA_SEEKPOINT_LEN; /* = 18 bytes */
410
411 extern const uint64 FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER; /* = 0xffffffffffffffff */
412
413 /*****************************************************************************
414  *
415  *      0: num_points is implied by the metadata block 'length' field (i.e. num_points = length / 18)
416  * n*18*8: seek points (n = num_points, 18 is the size of a seek point in bytes)
417  * ------- -----------------
418  *   n*18  bytes total
419  *
420  * NOTE: the seek points must be sorted by ascending sample number.
421  * NOTE: each seek point's sample number must be the first sample of the target frame.
422  * NOTE: each seek point's sample number must be unique within the table.
423  * NOTE: existence of a SEEKTABLE block implies a correct setting of total_samples in the stream_info block.
424  * NOTE: behavior is undefined when more than one SEEKTABLE block is present in a stream.
425  */
426 typedef struct {
427         unsigned num_points;
428         FLAC__StreamMetaData_SeekPoint *points;
429 } FLAC__StreamMetaData_SeekTable;
430
431 /*****************************************************************************
432  *
433  *  1: =1 if this is the last meta-data block, else =0
434  *  7: meta-data type (c.f. FLAC__MetaDataType)
435  * 24: length (in bytes) of the block-specific data to follow
436  *---- -----------------
437  *  4  bytes total
438  */
439 typedef struct {
440         FLAC__MetaDataType type;
441         bool is_last;
442         unsigned length; /* in bytes */
443         union {
444                 FLAC__StreamMetaData_StreamInfo stream_info;
445                 FLAC__StreamMetaData_Padding padding;
446                 FLAC__StreamMetaData_Application application;
447                 FLAC__StreamMetaData_SeekTable seek_table;
448         } data;
449 } FLAC__StreamMetaData;
450
451 extern const unsigned FLAC__STREAM_METADATA_IS_LAST_LEN; /* = 1 bits */
452 extern const unsigned FLAC__STREAM_METADATA_TYPE_LEN; /* = 7 bits */
453 extern const unsigned FLAC__STREAM_METADATA_LENGTH_LEN; /* = 24 bits */
454
455 /*****************************************************************************/
456
457
458 /*****************************************************************************
459  *
460  * Stream structures
461  *
462  *****************************************************************************/
463
464 typedef struct {
465         FLAC__StreamMetaData_StreamInfo stream_info;
466         FLAC__Frame *frames;
467 } FLAC__Stream;
468
469 /*****************************************************************************/
470
471 #endif