summaryrefslogtreecommitdiff
path: root/apps/codecs/libwavpack/wavpack.h
diff options
context:
space:
mode:
authorChristian Gmeiner <christian.gmeiner@gmail.com>2005-02-25 17:05:30 +0000
committerChristian Gmeiner <christian.gmeiner@gmail.com>2005-02-25 17:05:30 +0000
commite449d88b3e6b584998f8f38ed61467c35ca74466 (patch)
tree307e87242fd5fbf45d7424bb5afad17b9dd34429 /apps/codecs/libwavpack/wavpack.h
parent234489a449e13d99b76daff61ff7774226d21a5b (diff)
downloadrockbox-e449d88b3e6b584998f8f38ed61467c35ca74466.tar.gz
rockbox-e449d88b3e6b584998f8f38ed61467c35ca74466.zip
Initial import of libwavpack
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@6056 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/codecs/libwavpack/wavpack.h')
-rw-r--r--apps/codecs/libwavpack/wavpack.h330
1 files changed, 330 insertions, 0 deletions
diff --git a/apps/codecs/libwavpack/wavpack.h b/apps/codecs/libwavpack/wavpack.h
new file mode 100644
index 0000000000..af9d88dfe6
--- /dev/null
+++ b/apps/codecs/libwavpack/wavpack.h
@@ -0,0 +1,330 @@
1////////////////////////////////////////////////////////////////////////////
2// **** WAVPACK **** //
3// Hybrid Lossless Wavefile Compressor //
4// Copyright (c) 1998 - 2004 Conifer Software. //
5// All Rights Reserved. //
6// Distributed under the BSD Software License (see license.txt) //
7////////////////////////////////////////////////////////////////////////////
8
9// wavpack.h
10
11#include <sys/types.h>
12
13// This header file contains all the definitions required by WavPack.
14
15typedef unsigned char uchar;
16#if !defined(__GNUC__) || defined(WIN32)
17typedef unsigned short ushort;
18typedef unsigned long ulong;
19typedef unsigned int uint;
20#elif defined(__APPLE__)
21typedef unsigned long ulong;
22#endif
23
24// This structure is used to access the individual fields of 32-bit ieee
25// floating point numbers. This will not be compatible with compilers that
26// allocate bit fields from the most significant bits, although I'm not sure
27// how common that is.
28
29typedef struct {
30 unsigned mantissa : 23;
31 unsigned exponent : 8;
32 unsigned sign : 1;
33} f32;
34
35#include <stdio.h>
36
37#define FALSE 0
38#define TRUE 1
39
40////////////////////////////// WavPack Header /////////////////////////////////
41
42// Note that this is the ONLY structure that is written to (or read from)
43// WavPack 4.0 files, and is the preamble to every block in both the .wv
44// and .wvc files.
45
46typedef struct {
47 char ckID [4];
48 ulong ckSize;
49 short version;
50 uchar track_no, index_no;
51 ulong total_samples, block_index, block_samples, flags, crc;
52} WavpackHeader;
53
54#define WavpackHeaderFormat "4LS2LLLLL"
55
56// or-values for "flags"
57
58#define BYTES_STORED 3 // 1-4 bytes/sample
59#define MONO_FLAG 4 // not stereo
60#define HYBRID_FLAG 8 // hybrid mode
61#define JOINT_STEREO 0x10 // joint stereo
62#define CROSS_DECORR 0x20 // no-delay cross decorrelation
63#define HYBRID_SHAPE 0x40 // noise shape (hybrid mode only)
64#define FLOAT_DATA 0x80 // ieee 32-bit floating point data
65
66#define INT32_DATA 0x100 // special extended int handling
67#define HYBRID_BITRATE 0x200 // bitrate noise (hybrid mode only)
68#define HYBRID_BALANCE 0x400 // balance noise (hybrid stereo mode only)
69
70#define INITIAL_BLOCK 0x800 // initial block of multichannel segment
71#define FINAL_BLOCK 0x1000 // final block of multichannel segment
72
73#define SHIFT_LSB 13
74#define SHIFT_MASK (0x1fL << SHIFT_LSB)
75
76#define MAG_LSB 18
77#define MAG_MASK (0x1fL << MAG_LSB)
78
79#define SRATE_LSB 23
80#define SRATE_MASK (0xfL << SRATE_LSB)
81
82#define IGNORED_FLAGS 0x18000000 // reserved, but ignore if encountered
83#define NEW_SHAPING 0x20000000 // use IIR filter for negative shaping
84#define UNKNOWN_FLAGS 0xC0000000 // also reserved, but refuse decode if
85 // encountered
86
87//////////////////////////// WavPack Metadata /////////////////////////////////
88
89// This is an internal representation of metadata.
90
91typedef struct {
92 long byte_length;
93 void *data;
94 uchar id;
95} WavpackMetadata;
96
97#define ID_OPTIONAL_DATA 0x20
98#define ID_ODD_SIZE 0x40
99#define ID_LARGE 0x80
100
101#define ID_DUMMY 0x0
102#define ID_ENCODER_INFO 0x1
103#define ID_DECORR_TERMS 0x2
104#define ID_DECORR_WEIGHTS 0x3
105#define ID_DECORR_SAMPLES 0x4
106#define ID_ENTROPY_VARS 0x5
107#define ID_HYBRID_PROFILE 0x6
108#define ID_SHAPING_WEIGHTS 0x7
109#define ID_FLOAT_INFO 0x8
110#define ID_INT32_INFO 0x9
111#define ID_WV_BITSTREAM 0xa
112#define ID_WVC_BITSTREAM 0xb
113#define ID_WVX_BITSTREAM 0xc
114#define ID_CHANNEL_INFO 0xd
115
116#define ID_RIFF_HEADER (ID_OPTIONAL_DATA | 0x1)
117#define ID_RIFF_TRAILER (ID_OPTIONAL_DATA | 0x2)
118#define ID_REPLAY_GAIN (ID_OPTIONAL_DATA | 0x3)
119#define ID_CUESHEET (ID_OPTIONAL_DATA | 0x4)
120#define ID_CONFIG_BLOCK (ID_OPTIONAL_DATA | 0x5)
121#define ID_MD5_CHECKSUM (ID_OPTIONAL_DATA | 0x6)
122
123///////////////////////// WavPack Configuration ///////////////////////////////
124
125// This internal structure is used during encode to provide configuration to
126// the encoding engine and during decoding to provide fle information back to
127// the higher level functions. Not all fields are used in both modes.
128
129typedef struct {
130 int bits_per_sample, bytes_per_sample;
131 int qmode, flags, xmode, num_channels, float_norm_exp;
132 long block_samples, extra_flags, sample_rate, channel_mask;
133} WavpackConfig;
134
135#define CONFIG_BYTES_STORED 3 // 1-4 bytes/sample
136#define CONFIG_MONO_FLAG 4 // not stereo
137#define CONFIG_HYBRID_FLAG 8 // hybrid mode
138#define CONFIG_JOINT_STEREO 0x10 // joint stereo
139#define CONFIG_CROSS_DECORR 0x20 // no-delay cross decorrelation
140#define CONFIG_HYBRID_SHAPE 0x40 // noise shape (hybrid mode only)
141#define CONFIG_FLOAT_DATA 0x80 // ieee 32-bit floating point data
142
143#define CONFIG_ADOBE_MODE 0x100 // "adobe" mode for 32-bit floats
144#define CONFIG_FAST_FLAG 0x200 // fast mode
145#define CONFIG_VERY_FAST_FLAG 0x400 // double fast
146#define CONFIG_HIGH_FLAG 0x800 // high quality mode
147#define CONFIG_VERY_HIGH_FLAG 0x1000 // double high (not used yet)
148#define CONFIG_BITRATE_KBPS 0x2000 // bitrate is kbps, not bits / sample
149#define CONFIG_AUTO_SHAPING 0x4000 // automatic noise shaping
150#define CONFIG_SHAPE_OVERRIDE 0x8000 // shaping mode specified
151#define CONFIG_JOINT_OVERRIDE 0x10000 // joint-stereo mode specified
152#define CONFIG_COPY_TIME 0x20000 // copy file-time from source
153#define CONFIG_CREATE_EXE 0x40000 // create executable (not yet)
154#define CONFIG_CREATE_WVC 0x80000 // create correction file
155#define CONFIG_OPTIMIZE_WVC 0x100000 // maximize bybrid compression
156#define CONFIG_QUALITY_MODE 0x200000 // psychoacoustic quality mode
157#define CONFIG_RAW_FLAG 0x400000 // raw mode (not implemented yet)
158#define CONFIG_CALC_NOISE 0x800000 // calc noise in hybrid mode
159#define CONFIG_LOSSY_MODE 0x1000000 // obsolete (for information)
160#define CONFIG_EXTRA_MODE 0x2000000 // extra processing mode
161#define CONFIG_SKIP_WVX 0x4000000 // no wvx stream w/ floats & big ints
162#define CONFIG_MD5_CHECKSUM 0x8000000 // compute & store MD5 signature
163#define CONFIG_QUIET_MODE 0x10000000 // don't report progress %
164
165//////////////////////////////// WavPack Stream ///////////////////////////////
166
167// This internal structure contains everything required to handle a WavPack
168// "stream", which is defined as a stereo or mono stream of audio samples. For
169// multichannel audio several of these would be required. Each stream contains
170// pointers to hold a complete allocated block of WavPack data, although it's
171// possible to decode WavPack blocks without buffering an entire block.
172
173typedef long (*read_stream)(void *, long);
174
175typedef struct bs {
176 uchar *buf, *end, *ptr;
177 void (*wrap)(struct bs *bs);
178 ulong file_bytes, sr;
179 int error, bc;
180 read_stream file;
181} Bitstream;
182
183#define MAX_NTERMS 16
184#define MAX_TERM 8
185
186struct decorr_pass {
187 short term, delta, weight_A, weight_B;
188 long samples_A [MAX_TERM], samples_B [MAX_TERM];
189};
190
191typedef struct {
192 WavpackHeader wphdr;
193
194 int num_terms, mute_error;
195 ulong sample_index, crc;
196 Bitstream wvbits;
197
198 uchar int32_sent_bits, int32_zeros, int32_ones, int32_dups;
199 uchar float_flags, float_shift, float_max_exp, float_norm_exp;
200
201 struct decorr_pass decorr_passes [MAX_NTERMS];
202
203 struct {
204 ulong bitrate_delta [2], bitrate_acc [2];
205 ulong median [3] [2], slow_level [2], error_limit [2];
206 ulong pend_data, holding_one, zeros_acc;
207 int holding_zero, pend_count;
208 } w;
209} WavpackStream;
210
211// flags for float_flags:
212
213#define FLOAT_SHIFT_ONES 1 // bits left-shifted into float = '1'
214#define FLOAT_SHIFT_SAME 2 // bits left-shifted into float are the same
215#define FLOAT_SHIFT_SENT 4 // bits shifted into float are sent literally
216#define FLOAT_ZEROS_SENT 8 // "zeros" are not all real zeros
217#define FLOAT_NEG_ZEROS 0x10 // contains negative zeros
218#define FLOAT_EXCEPTIONS 0x20 // contains exceptions (inf, nan, etc.)
219
220/////////////////////////////// WavPack Context ///////////////////////////////
221
222// This internal structure holds everything required to encode or decode WavPack
223// files. It is recommended that direct access to this structure be minimized
224// and the provided utilities used instead.
225
226typedef struct {
227 WavpackConfig config;
228 WavpackStream stream;
229
230 uchar read_buffer [1024];
231 char error_message [80];
232
233 read_stream infile;
234 ulong total_samples, crc_errors, first_flags;
235 int open_flags, norm_offset, reduced_channels, lossy_blocks;
236
237} WavpackContext;
238
239//////////////////////// function prototypes and macros //////////////////////
240
241#define CLEAR(destin) memset (&destin, 0, sizeof (destin));
242
243// bits.c
244
245void bs_open_read (Bitstream *bs, uchar *buffer_start, uchar *buffer_end, read_stream file, ulong file_bytes);
246
247#define bs_is_open(bs) ((bs)->ptr != NULL)
248
249#define getbit(bs) ( \
250 (((bs)->bc) ? \
251 ((bs)->bc--, (bs)->sr & 1) : \
252 (((++((bs)->ptr) != (bs)->end) ? (void) 0 : (bs)->wrap (bs)), (bs)->bc = 7, ((bs)->sr = *((bs)->ptr)) & 1) \
253 ) ? \
254 ((bs)->sr >>= 1, 1) : \
255 ((bs)->sr >>= 1, 0) \
256)
257
258#define getbits(value, nbits, bs) { \
259 while ((nbits) > (bs)->bc) { \
260 if (++((bs)->ptr) == (bs)->end) (bs)->wrap (bs); \
261 (bs)->sr |= (long)*((bs)->ptr) << (bs)->bc; \
262 (bs)->bc += 8; \
263 } \
264 *(value) = (bs)->sr; \
265 (bs)->sr >>= (nbits); \
266 (bs)->bc -= (nbits); \
267}
268
269void little_endian_to_native (void *data, char *format);
270void native_to_little_endian (void *data, char *format);
271
272// unpack.c
273
274int unpack_init (WavpackContext *wpc);
275int init_wv_bitstream (WavpackContext *wpc, WavpackMetadata *wpmd);
276int read_decorr_terms (WavpackStream *wps, WavpackMetadata *wpmd);
277int read_decorr_weights (WavpackStream *wps, WavpackMetadata *wpmd);
278int read_decorr_samples (WavpackStream *wps, WavpackMetadata *wpmd);
279int read_float_info (WavpackStream *wps, WavpackMetadata *wpmd);
280int read_int32_info (WavpackStream *wps, WavpackMetadata *wpmd);
281int read_channel_info (WavpackContext *wpc, WavpackMetadata *wpmd);
282int read_config_info (WavpackContext *wpc, WavpackMetadata *wpmd);
283long unpack_samples (WavpackContext *wpc, long *buffer, ulong sample_count);
284int check_crc_error (WavpackContext *wpc);
285
286// metadata.c stuff
287
288int read_metadata_buff (WavpackContext *wpc, WavpackMetadata *wpmd);
289int process_metadata (WavpackContext *wpc, WavpackMetadata *wpmd);
290
291// words.c stuff
292
293int read_entropy_vars (WavpackStream *wps, WavpackMetadata *wpmd);
294int read_hybrid_profile (WavpackStream *wps, WavpackMetadata *wpmd);
295long get_word (WavpackStream *wps, int chan);
296long exp2s (int log);
297int restore_weight (char weight);
298
299#define WORD_EOF (1L << 31)
300
301// float.c
302
303int read_float_info (WavpackStream *wps, WavpackMetadata *wpmd);
304void float_values (WavpackStream *wps, long *values, long num_values);
305void float_normalize (long *values, long num_values, int delta_exp);
306
307// wputils.c
308
309WavpackContext *WavpackOpenFileInput (read_stream infile, char *error);
310
311int WavpackGetMode (WavpackContext *wpc);
312
313#define MODE_WVC 0x1
314#define MODE_LOSSLESS 0x2
315#define MODE_HYBRID 0x4
316#define MODE_FLOAT 0x8
317#define MODE_VALID_TAG 0x10
318#define MODE_HIGH 0x20
319#define MODE_FAST 0x40
320
321ulong WavpackUnpackSamples (WavpackContext *wpc, long *buffer, ulong samples);
322ulong WavpackGetNumSamples (WavpackContext *wpc);
323ulong WavpackGetSampleIndex (WavpackContext *wpc);
324int WavpackGetNumErrors (WavpackContext *wpc);
325int WavpackLossyBlocks (WavpackContext *wpc);
326ulong WavpackGetSampleRate (WavpackContext *wpc);
327int WavpackGetBitsPerSample (WavpackContext *wpc);
328int WavpackGetBytesPerSample (WavpackContext *wpc);
329int WavpackGetNumChannels (WavpackContext *wpc);
330int WavpackGetReducedChannels (WavpackContext *wpc);