flac/include/FLAC/format.h
2002-07-15 05:26:50 +00:00

628 lines
21 KiB
C

/* libFLAC - Free Lossless Audio Codec library
* Copyright (C) 2000,2001,2002 Josh Coalson
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef FLAC__FORMAT_H
#define FLAC__FORMAT_H
#include "ordinals.h"
#ifdef __cplusplus
extern "C" {
#endif
/** \file include/FLAC/format.h
*
* \brief
* This module contains structure definitions for the representation
* of FLAC format components in memory. These are the basic
* structures used by the rest of the interfaces.
*
* See the detailed documentation in the
* \link flac_format format \endlink module.
*/
/** \defgroup flac_format FLAC/format.h: format components
* \ingroup flac
*
* \brief
* This module contains structure definitions for the representation
* of FLAC format components in memory. These are the basic
* structures used by the rest of the interfaces.
*
* First, you should be familiar with the XXX FLAC format XXX. Many
* of the values here follow directly from the specification. As a
* user of libFLAC, the interesting parts really are the structures
* that describe the frame header and metadata blocks.
*
* The format structures here are very primitive, designed to store
* information in an efficient way. Reading information from the
* structures is easy but creating or modifying them directly is
* more complex. For the most part, as a user of a library, editing
* is not necessary; however, for metadata blocks it is, so there are
* convenience functions provided in the XXX metadata XXX module
* to simplify the manipulation of metadata blocks.
*
* \note
* It's not the best convention, but symbols ending in _LEN are in bits
* and _LENGTH are in bytes. _LENGTH symbols are \#defines instead of
* global variables because they are usually used when declaring byte
* arrays and some compilers require compile-time knowledge of array
* sizes when declared on the stack.
*
* \{
*/
/*
Most of the values described in this file are defined by the FLAC
format specification. There is nothing to tune here.
*/
/** The minimum block size, in samples, permitted by the format. */
#define FLAC__MIN_BLOCK_SIZE (16u)
/** The maximum block size, in samples, permitted by the format. */
#define FLAC__MAX_BLOCK_SIZE (65535u)
/** The maximum number of channels permitted by the format. */
#define FLAC__MAX_CHANNELS (8u)
/** The minimum sample resolution permitted by the format. */
#define FLAC__MIN_BITS_PER_SAMPLE (4u)
/** The maximum sample resolution permitted by the format. */
#define FLAC__MAX_BITS_PER_SAMPLE (32u)
/** The maximum sample resolution permitted by libFLAC.
*
* \warning
* FLAC__MAX_BITS_PER_SAMPLE is the limit of the FLAC format. However,
* the reference encoder/decoder is currently limited to 24 bits because
* of prevalent 32-bit math, so make sure and use this value when
* appropriate.
*/
#define FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE (24u)
/** The maximum sample rate permitted by the format. The value is
* ((2 ** 16) - 1) * 10; see XXX format.html XXX as to why.
*/
#define FLAC__MAX_SAMPLE_RATE (655350u)
/** The maximum LPC order permitted by the format. */
#define FLAC__MAX_LPC_ORDER (32u)
/** The minimum quantized linear predictor coefficient precision
* permitted by the format.
*/
#define FLAC__MIN_QLP_COEFF_PRECISION (5u)
/** The maximum order of the fixed predictors permitted by the format. */
#define FLAC__MAX_FIXED_ORDER (4u)
/** The maximum Rice partition order permitted by the format. */
#define FLAC__MAX_RICE_PARTITION_ORDER (15u)
/* VERSION should come from configure */
#ifdef VERSION
/** The version string of the current library.
*
* \note
* This does not correspond to the shared library version number, which
* is used to determine binary compatibility.
*/
#define FLAC__VERSION_STRING VERSION
#endif
/** The byte string representation of the beginning of a FLAC stream. */
extern const FLAC__byte FLAC__STREAM_SYNC_STRING[4]; /* = "fLaC" */;
/** The 32-bit integer big-endian representation of the beginning of
* a FLAC stream.
*/
extern const unsigned FLAC__STREAM_SYNC; /* = 0x664C6143 */;
/** The length of the FLAC signature in bits. */
extern const unsigned FLAC__STREAM_SYNC_LEN; /* = 32 bits */;
/** The length of the FLAC signature in bytes. */
#define FLAC__STREAM_SYNC_LENGTH (4u)
/*****************************************************************************
* @@@ REMOVE?
* NOTE: Within the bitstream, all fixed-width numbers are big-endian coded.
* All numbers are unsigned unless otherwise noted.
*
*****************************************************************************/
/*****************************************************************************
*
* Subframe structures
*
*****************************************************************************/
/*****************************************************************************/
/** An enumeration of the available entropy coding methods. */
typedef enum {
FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE = 0
/**< Residual is coded by partitioning into contexts, each with it's own
* Rice parameter. */
} FLAC__EntropyCodingMethodType;
/** Maps a FLAC__EntropyCodingMethodType to a C string.
*
* Using a FLAC__EntropyCodingMethodType as the index to this array will
* give the string equivalent. The contents should not be modified.
*/
extern const char * const FLAC__EntropyCodingMethodTypeString[];
/** Header for a Rice partition. (XXX format XXX)
*/
typedef struct {
unsigned order;
/**< The partition order, i.e. # of contexts = 2 ** order. */
unsigned parameters[1 << FLAC__MAX_RICE_PARTITION_ORDER];
/**< The Rice parameters for each context. */
unsigned raw_bits[1 << FLAC__MAX_RICE_PARTITION_ORDER];
/**< Widths for escape-coded partitions. */
} FLAC__EntropyCodingMethod_PartitionedRice;
extern const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN; /**< == 4 (bits) */
extern const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; /**< == 4 (bits) */
extern const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN; /**< == 5 (bits) */
extern const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
/**< == (1<<FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN)-1 */
/** Header for the entropy coding method. (XXX format XXX)
*/
typedef struct {
FLAC__EntropyCodingMethodType type;
union {
FLAC__EntropyCodingMethod_PartitionedRice partitioned_rice;
} data;
} FLAC__EntropyCodingMethod;
extern const unsigned FLAC__ENTROPY_CODING_METHOD_TYPE_LEN; /**< == 2 (bits) */
/*****************************************************************************/
/** An enumeration of the available subframe types. */
typedef enum {
FLAC__SUBFRAME_TYPE_CONSTANT = 0, /**< constant signal */
FLAC__SUBFRAME_TYPE_VERBATIM = 1, /**< uncompressed signal */
FLAC__SUBFRAME_TYPE_FIXED = 2, /**< fixed polynomial prediction */
FLAC__SUBFRAME_TYPE_LPC = 3 /**< linear prediction */
} FLAC__SubframeType;
/** Maps a FLAC__SubframeType to a C string.
*
* Using a FLAC__SubframeType as the index to this array will
* give the string equivalent. The contents should not be modified.
*/
extern const char * const FLAC__SubframeTypeString[];
/** CONSTANT subframe. (XXX format XXX)
*/
typedef struct {
FLAC__int32 value; /**< The constant signal value. */
} FLAC__Subframe_Constant;
/** VERBATIM subframe. (XXX format XXX)
*/
typedef struct {
const FLAC__int32 *data; /**< A pointer to verbatim signal. */
} FLAC__Subframe_Verbatim;
/** FIXED subframe. (XXX format XXX)
*/
typedef struct {
FLAC__EntropyCodingMethod entropy_coding_method;
/**< The residual coding method. */
unsigned order;
/**< The polynomial order. */
FLAC__int32 warmup[FLAC__MAX_FIXED_ORDER];
/**< Warmup samples to prime the predictor, length == order. */
const FLAC__int32 *residual;
/**< The residual signal, length == (blocksize minus order) samples. */
} FLAC__Subframe_Fixed;
/** LPC subframe. (XXX format XXX)
*/
typedef struct {
FLAC__EntropyCodingMethod entropy_coding_method;
/**< The residual coding method. */
unsigned order;
/**< The FIR order. */
unsigned qlp_coeff_precision;
/**< Quantized FIR filter coefficient precision in bits. */
int quantization_level;
/**< The qlp coeff shift needed. */
FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
/**< FIR filter coefficients. */
FLAC__int32 warmup[FLAC__MAX_LPC_ORDER];
/**< Warmup samples to prime the predictor, length == order. */
const FLAC__int32 *residual;
/**< The residual signal, length == (blocksize minus order) samples. */
} FLAC__Subframe_LPC;
extern const unsigned FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN; /**< == 4 (bits) */
extern const unsigned FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN; /**< == 5 (bits) */
/** FLAC subframe structure. (XXX format XXX)
*/
typedef struct {
FLAC__SubframeType type;
union {
FLAC__Subframe_Constant constant;
FLAC__Subframe_Fixed fixed;
FLAC__Subframe_LPC lpc;
FLAC__Subframe_Verbatim verbatim;
} data;
unsigned wasted_bits;
} FLAC__Subframe;
extern const unsigned FLAC__SUBFRAME_ZERO_PAD_LEN; /**< == 1 (bit) */
extern const unsigned FLAC__SUBFRAME_TYPE_LEN; /**< == 6 (bits) */
extern const unsigned FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN; /**< == 1 (bit) */
extern const unsigned FLAC__SUBFRAME_TYPE_CONSTANT_BYTE_ALIGNED_MASK; /* = 0x00 */
extern const unsigned FLAC__SUBFRAME_TYPE_VERBATIM_BYTE_ALIGNED_MASK; /* = 0x02 */
extern const unsigned FLAC__SUBFRAME_TYPE_FIXED_BYTE_ALIGNED_MASK; /* = 0x10 */
extern const unsigned FLAC__SUBFRAME_TYPE_LPC_BYTE_ALIGNED_MASK; /* = 0x40 */
/*****************************************************************************/
/*****************************************************************************
*
* Frame structures
*
*****************************************************************************/
/** An enumeration of the available channel assignments. */
typedef enum {
FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT = 0, /**< independent channels */
FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE = 1, /**< left+side stereo */
FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE = 2, /**< right+side stereo */
FLAC__CHANNEL_ASSIGNMENT_MID_SIDE = 3 /**< mid+side stereo */
} FLAC__ChannelAssignment;
/** Maps a FLAC__ChannelAssignment to a C string.
*
* Using a FLAC__ChannelAssignment as the index to this array will
* give the string equivalent. The contents should not be modified.
*/
extern const char * const FLAC__ChannelAssignmentString[];
/** An enumeration of the possible frame numbering methods. */
typedef enum {
FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER, /**< number contains the frame number */
FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER /**< number contains the sample number of first sample in frame */
} FLAC__FrameNumberType;
/** Maps a FLAC__FrameNumberType to a C string.
*
* Using a FLAC__FrameNumberType as the index to this array will
* give the string equivalent. The contents should not be modified.
*/
extern const char * const FLAC__FrameNumberTypeString[];
/** FLAC frame header structure. (XXX format XXX)
*/
typedef struct {
unsigned blocksize;
/**< The number of samples per subframe. */
unsigned sample_rate;
/**< The sample rate in Hz. */
unsigned channels;
/**< The number of channels (== number of subframes). */
FLAC__ChannelAssignment channel_assignment;
/**< The channel assignment for the frame. */
unsigned bits_per_sample;
/**< The sample resolution. */
FLAC__FrameNumberType number_type;
/**< The numbering scheme used for the frame. */
union {
FLAC__uint32 frame_number;
FLAC__uint64 sample_number;
} number;
/**< The frame number or sample number of first sample in frame;
* use the \a number_type value to determine which to use. */
FLAC__uint8 crc;
/**< CRC-8 (polynomial = x^8 + x^2 + x^1 + x^0, initialized with 0) of the
* raw frame header bytes, meaning everything before the CRC byte including
* the sync code.
*/
} FLAC__FrameHeader;
extern const unsigned FLAC__FRAME_HEADER_SYNC; /**< == 0x3ffe; the frame header sync code */
extern const unsigned FLAC__FRAME_HEADER_SYNC_LEN; /**< == 14 (bits) */
extern const unsigned FLAC__FRAME_HEADER_RESERVED_LEN; /**< == 2 (bits) */
extern const unsigned FLAC__FRAME_HEADER_BLOCK_SIZE_LEN; /**< == 4 (bits) */
extern const unsigned FLAC__FRAME_HEADER_SAMPLE_RATE_LEN; /**< == 4 (bits) */
extern const unsigned FLAC__FRAME_HEADER_CHANNEL_ASSIGNMENT_LEN; /**< == 4 (bits) */
extern const unsigned FLAC__FRAME_HEADER_BITS_PER_SAMPLE_LEN; /**< == 3 (bits) */
extern const unsigned FLAC__FRAME_HEADER_ZERO_PAD_LEN; /**< == 1 (bit) */
extern const unsigned FLAC__FRAME_HEADER_CRC_LEN; /**< == 8 (bits) */
/** FLAC frame footer structure. (XXX format XXX)
*/
typedef struct {
FLAC__uint16 crc;
/**< CRC-16 (polynomial = x^16 + x^15 + x^2 + x^0, initialized with 0)
* of the bytes before the crc, back to and including the frame header
* sync code.
*/
} FLAC__FrameFooter;
extern const unsigned FLAC__FRAME_FOOTER_CRC_LEN; /**< == 16 (bits) */
/** FLAC frame structure. (XXX format XXX)
*/
typedef struct {
FLAC__FrameHeader header;
FLAC__Subframe subframes[FLAC__MAX_CHANNELS];
FLAC__FrameFooter footer;
} FLAC__Frame;
/*****************************************************************************/
/*****************************************************************************
*
* Meta-data structures
*
*****************************************************************************/
/** An enumeration of the available metadata block types. */
typedef enum {
FLAC__METADATA_TYPE_STREAMINFO = 0,
FLAC__METADATA_TYPE_PADDING = 1,
FLAC__METADATA_TYPE_APPLICATION = 2,
FLAC__METADATA_TYPE_SEEKTABLE = 3,
FLAC__METADATA_TYPE_VORBIS_COMMENT = 4
} FLAC__MetadataType;
/** Maps a FLAC__MetadataType to a C string.
*
* Using a FLAC__MetadataType as the index to this array will
* give the string equivalent. The contents should not be modified.
*/
extern const char * const FLAC__MetadataTypeString[];
/** FLAC STREAMINFO structure. (XXX format XXX)
*/
typedef struct {
unsigned min_blocksize, max_blocksize;
unsigned min_framesize, max_framesize;
unsigned sample_rate;
unsigned channels;
unsigned bits_per_sample;
FLAC__uint64 total_samples;
FLAC__byte md5sum[16];
} FLAC__StreamMetadata_StreamInfo;
extern const unsigned FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN; /**< == 16 (bits) */
extern const unsigned FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN; /**< == 16 (bits) */
extern const unsigned FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN; /**< == 24 (bits) */
extern const unsigned FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN; /**< == 24 (bits) */
extern const unsigned FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN; /**< == 20 (bits) */
extern const unsigned FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN; /**< == 3 (bits) */
extern const unsigned FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN; /**< == 5 (bits) */
extern const unsigned FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN; /**< == 36 (bits) */
extern const unsigned FLAC__STREAM_METADATA_STREAMINFO_MD5SUM_LEN; /**< == 128 (bits) */
/** The total stream length of the STREAMINFO block in bytes. */
#define FLAC__STREAM_METADATA_STREAMINFO_LENGTH (34u)
/** FLAC PADDING structure. (XXX format XXX)
*/
typedef struct {
int dummy;
/**< Conceptually this is an empty struct since we don't store the
* padding bytes. Empty structs are not allowed by some C compilers,
* hence the dummy.
*/
} FLAC__StreamMetadata_Padding;
/** FLAC APPLICATION structure. (XXX format XXX)
*/
typedef struct {
FLAC__byte id[4];
FLAC__byte *data;
} FLAC__StreamMetadata_Application;
extern const unsigned FLAC__STREAM_METADATA_APPLICATION_ID_LEN; /**< == 32 (bits) */
/** SeekPoint structure used in SEEKTABLE blocks. (XXX format XXX)
*/
typedef struct {
FLAC__uint64 sample_number;
/**< The sample number of the target frame. */
FLAC__uint64 stream_offset;
/**< The offset, in bytes, of the target frame with respect to
* beginning of the first frame. */
unsigned frame_samples;
/**< The number of samples in the target frame. */
} FLAC__StreamMetadata_SeekPoint;
extern const unsigned FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN; /**< == 64 (bits) */
extern const unsigned FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN; /**< == 64 (bits) */
extern const unsigned FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN; /**< == 16 (bits) */
/** The total stream length of a seek point in bytes. */
#define FLAC__STREAM_METADATA_SEEKPOINT_LENGTH (18u)
/** The value used in the \a sample_number field of
* FLAC__StreamMetadataSeekPoint used to indicate a placeholder
* point (== 0xffffffffffffffff).
*/
extern const FLAC__uint64 FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
/** FLAC SEEKTABLE structure. (XXX format XXX)
*
* \note From the format specification:
* - The seek points must be sorted by ascending sample number.
* - Each seek point's sample number must be the first sample of the
* target frame.
* - Each seek point's sample number must be unique within the table.
* - Existence of a SEEKTABLE block implies a correct setting of
* total_samples in the stream_info block.
* - Behavior is undefined when more than one SEEKTABLE block is
* present in a stream.
*/
typedef struct {
unsigned num_points;
FLAC__StreamMetadata_SeekPoint *points;
} FLAC__StreamMetadata_SeekTable;
/** Vorbis comment entry structure used in VORBIS_COMMENT blocks. (XXX format XXX)
*/
typedef struct {
FLAC__uint32 length;
FLAC__byte *entry;
} FLAC__StreamMetadata_VorbisComment_Entry;
extern const unsigned FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN; /**< == 32 (bits) */
/** FLAC VORBIS_COMMENT structure. (XXX format XXX)
*/
typedef struct {
FLAC__StreamMetadata_VorbisComment_Entry vendor_string;
FLAC__uint32 num_comments;
FLAC__StreamMetadata_VorbisComment_Entry *comments;
} FLAC__StreamMetadata_VorbisComment;
extern const unsigned FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN; /**< == 32 (bits) */
/*****************************************************************************
*
* 1: =1 if this is the last meta-data block, else =0
* 7: meta-data type (c.f. FLAC__MetadataType)
* 24: length (in bytes) of the block-specific data to follow
*---- -----------------
* 4 bytes total
*/
typedef struct {
FLAC__MetadataType type;
/**< The type of the metadata block; used determine which member of the
* \a data union to dereference. */
FLAC__bool is_last;
/**< \c true if this metadata block is the last, else \a false */
unsigned length;
/**< Length, in bytes, of the block data as it appears in the stream. */
union {
FLAC__StreamMetadata_StreamInfo stream_info;
FLAC__StreamMetadata_Padding padding;
FLAC__StreamMetadata_Application application;
FLAC__StreamMetadata_SeekTable seek_table;
FLAC__StreamMetadata_VorbisComment vorbis_comment;
} data;
/**< Polymorphic block data; use the \a type value to determine which
* to use. */
} FLAC__StreamMetadata;
extern const unsigned FLAC__STREAM_METADATA_IS_LAST_LEN; /**< == 1 (bit) */
extern const unsigned FLAC__STREAM_METADATA_TYPE_LEN; /**< == 7 (bits) */
extern const unsigned FLAC__STREAM_METADATA_LENGTH_LEN; /**< == 24 (bits) */
/** The total stream length of a metadata block header in bytes. */
#define FLAC__STREAM_METADATA_HEADER_LENGTH (4u)
/*****************************************************************************/
/*****************************************************************************
*
* Utility functions
*
*****************************************************************************/
/** Tests that a sample rate is valid for FLAC. Since the rules for valid
* sample rates are slightly complex, they are encapsulated in this function.
*
* \param sample_rate The sample rate to test for compliance.
* \retval FLAC__bool
* \c true if the given sample rate conforms to the specification, else
* \c false.
*/
FLAC__bool FLAC__format_sample_rate_is_valid(unsigned sample_rate);
/** Check a seektable to see if it conforms to the FLAC specification.
* See the format specification for limits on the contents of the
* seektable.
*
* \param object A pointer to a seek table to be checked.
* \assert
* \code object != NULL \endcode
* \retval FLAC__bool
* \c false if seek table is illegal, else \c true.
*/
FLAC__bool FLAC__format_seektable_is_legal(const FLAC__StreamMetadata_SeekTable *object);
/* \} */
#ifdef __cplusplus
}
#endif
#endif