summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libwavpack/wavpack.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libwavpack/wavpack.h')
-rw-r--r--lib/rbcodec/codecs/libwavpack/wavpack.h447
1 files changed, 447 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libwavpack/wavpack.h b/lib/rbcodec/codecs/libwavpack/wavpack.h
new file mode 100644
index 0000000000..b15a176f33
--- /dev/null
+++ b/lib/rbcodec/codecs/libwavpack/wavpack.h
@@ -0,0 +1,447 @@
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#ifdef CODEC
11#include "codeclib.h"
12#endif
13#include <inttypes.h>
14
15// This header file contains all the definitions required by WavPack.
16
17typedef unsigned char uchar;
18typedef unsigned short ushort;
19#if 0 // unused and causing compiler errrors
20typedef unsigned int uint;
21#endif
22
23#include <stdio.h>
24
25#define FALSE 0
26#define TRUE 1
27
28////////////////////////////// WavPack Header /////////////////////////////////
29
30// Note that this is the ONLY structure that is written to (or read from)
31// WavPack 4.0 files, and is the preamble to every block in both the .wv
32// and .wvc files.
33
34typedef struct {
35 char ckID [4];
36 uint32_t ckSize;
37 short version;
38 uchar track_no, index_no;
39 uint32_t total_samples, block_index, block_samples, flags, crc;
40} WavpackHeader;
41
42#define WavpackHeaderFormat "4LS2LLLLL"
43
44// or-values for "flags"
45
46#define BYTES_STORED 3 // 1-4 bytes/sample
47#define MONO_FLAG 4 // not stereo
48#define HYBRID_FLAG 8 // hybrid mode
49#define JOINT_STEREO 0x10 // joint stereo
50#define CROSS_DECORR 0x20 // no-delay cross decorrelation
51#define HYBRID_SHAPE 0x40 // noise shape (hybrid mode only)
52#define FLOAT_DATA 0x80 // ieee 32-bit floating point data
53
54#define INT32_DATA 0x100 // special extended int handling
55#define HYBRID_BITRATE 0x200 // bitrate noise (hybrid mode only)
56#define HYBRID_BALANCE 0x400 // balance noise (hybrid stereo mode only)
57
58#define INITIAL_BLOCK 0x800 // initial block of multichannel segment
59#define FINAL_BLOCK 0x1000 // final block of multichannel segment
60
61#define SHIFT_LSB 13
62#define SHIFT_MASK (0x1fL << SHIFT_LSB)
63
64#define MAG_LSB 18
65#define MAG_MASK (0x1fL << MAG_LSB)
66
67#define SRATE_LSB 23
68#define SRATE_MASK (0xfL << SRATE_LSB)
69
70#define FALSE_STEREO 0x40000000 // block is stereo, but data is mono
71
72#define IGNORED_FLAGS 0x18000000 // reserved, but ignore if encountered
73#define NEW_SHAPING 0x20000000 // use IIR filter for negative shaping
74#define UNKNOWN_FLAGS 0x80000000 // also reserved, but refuse decode if
75 // encountered
76
77#define MONO_DATA (MONO_FLAG | FALSE_STEREO)
78
79#define MIN_STREAM_VERS 0x402 // lowest stream version we'll decode
80#define MAX_STREAM_VERS 0x410 // highest stream version we'll decode
81#define CUR_STREAM_VERS 0x403 // stream version we are writing now
82
83//////////////////////////// WavPack Metadata /////////////////////////////////
84
85// This is an internal representation of metadata.
86
87typedef struct {
88 uchar temp_data [64];
89 int32_t byte_length;
90 void *data;
91 uchar id;
92} WavpackMetadata;
93
94#define ID_OPTIONAL_DATA 0x20
95#define ID_ODD_SIZE 0x40
96#define ID_LARGE 0x80
97
98#define ID_DUMMY 0x0
99#define ID_ENCODER_INFO 0x1
100#define ID_DECORR_TERMS 0x2
101#define ID_DECORR_WEIGHTS 0x3
102#define ID_DECORR_SAMPLES 0x4
103#define ID_ENTROPY_VARS 0x5
104#define ID_HYBRID_PROFILE 0x6
105#define ID_SHAPING_WEIGHTS 0x7
106#define ID_FLOAT_INFO 0x8
107#define ID_INT32_INFO 0x9
108#define ID_WV_BITSTREAM 0xa
109#define ID_WVC_BITSTREAM 0xb
110#define ID_WVX_BITSTREAM 0xc
111#define ID_CHANNEL_INFO 0xd
112
113#define ID_RIFF_HEADER (ID_OPTIONAL_DATA | 0x1)
114#define ID_RIFF_TRAILER (ID_OPTIONAL_DATA | 0x2)
115#define ID_REPLAY_GAIN (ID_OPTIONAL_DATA | 0x3)
116#define ID_CUESHEET (ID_OPTIONAL_DATA | 0x4)
117#define ID_CONFIG_BLOCK (ID_OPTIONAL_DATA | 0x5)
118#define ID_MD5_CHECKSUM (ID_OPTIONAL_DATA | 0x6)
119#define ID_SAMPLE_RATE (ID_OPTIONAL_DATA | 0x7)
120
121///////////////////////// WavPack Configuration ///////////////////////////////
122
123// This internal structure is used during encode to provide configuration to
124// the encoding engine and during decoding to provide fle information back to
125// the higher level functions. Not all fields are used in both modes.
126
127typedef struct {
128 int bits_per_sample, bytes_per_sample;
129 int flags, num_channels, float_norm_exp;
130 uint32_t sample_rate, channel_mask;
131} WavpackConfig;
132
133#define CONFIG_BYTES_STORED 3 // 1-4 bytes/sample
134#define CONFIG_MONO_FLAG 4 // not stereo
135#define CONFIG_HYBRID_FLAG 8 // hybrid mode
136#define CONFIG_JOINT_STEREO 0x10 // joint stereo
137#define CONFIG_CROSS_DECORR 0x20 // no-delay cross decorrelation
138#define CONFIG_HYBRID_SHAPE 0x40 // noise shape (hybrid mode only)
139#define CONFIG_FLOAT_DATA 0x80 // ieee 32-bit floating point data
140
141#define CONFIG_ADOBE_MODE 0x100 // "adobe" mode for 32-bit floats
142#define CONFIG_FAST_FLAG 0x200 // fast mode
143#define CONFIG_VERY_FAST_FLAG 0x400 // double fast
144#define CONFIG_HIGH_FLAG 0x800 // high quality mode
145#define CONFIG_VERY_HIGH_FLAG 0x1000 // double high (not used yet)
146#define CONFIG_BITRATE_KBPS 0x2000 // bitrate is kbps, not bits / sample
147#define CONFIG_AUTO_SHAPING 0x4000 // automatic noise shaping
148#define CONFIG_SHAPE_OVERRIDE 0x8000 // shaping mode specified
149#define CONFIG_JOINT_OVERRIDE 0x10000 // joint-stereo mode specified
150#define CONFIG_COPY_TIME 0x20000 // copy file-time from source
151#define CONFIG_CREATE_EXE 0x40000 // create executable (not yet)
152#define CONFIG_CREATE_WVC 0x80000 // create correction file
153#define CONFIG_OPTIMIZE_WVC 0x100000 // maximize bybrid compression
154#define CONFIG_QUALITY_MODE 0x200000 // psychoacoustic quality mode
155#define CONFIG_RAW_FLAG 0x400000 // raw mode (not implemented yet)
156#define CONFIG_CALC_NOISE 0x800000 // calc noise in hybrid mode
157#define CONFIG_LOSSY_MODE 0x1000000 // obsolete (for information)
158#define CONFIG_EXTRA_MODE 0x2000000 // extra processing mode
159#define CONFIG_SKIP_WVX 0x4000000 // no wvx stream w/ floats & big ints
160#define CONFIG_MD5_CHECKSUM 0x8000000 // compute & store MD5 signature
161#define CONFIG_QUIET_MODE 0x10000000 // don't report progress %
162
163//////////////////////////////// WavPack Stream ///////////////////////////////
164
165// This internal structure contains everything required to handle a WavPack
166// "stream", which is defined as a stereo or mono stream of audio samples. For
167// multichannel audio several of these would be required. Each stream contains
168// pointers to hold a complete allocated block of WavPack data, although it's
169// possible to decode WavPack blocks without buffering an entire block.
170
171typedef int32_t (*read_stream)(void *, int32_t);
172
173typedef struct bs {
174 uchar *buf, *end, *ptr;
175 void (*wrap)(struct bs *bs);
176 uint32_t file_bytes, sr;
177 int error, bc;
178 read_stream file;
179} Bitstream;
180
181#define MAX_NTERMS 16
182#define MAX_TERM 8
183
184struct decorr_pass {
185 short term, delta, weight_A, weight_B;
186 int32_t samples_A [MAX_TERM], samples_B [MAX_TERM];
187};
188
189struct entropy_data {
190 uint32_t median [3], slow_level, error_limit;
191};
192
193struct words_data {
194 uint32_t bitrate_delta [2], bitrate_acc [2];
195 uint32_t pend_data, holding_one, zeros_acc;
196 int holding_zero, pend_count;
197 struct entropy_data c [2];
198};
199
200typedef struct {
201 WavpackHeader wphdr;
202 Bitstream wvbits;
203
204 struct words_data w;
205
206 int num_terms, mute_error;
207 uint32_t sample_index, crc;
208 int32_t block_bytes_left;
209
210 uchar int32_sent_bits, int32_zeros, int32_ones, int32_dups;
211 uchar float_flags, float_shift, float_max_exp, float_norm_exp;
212 uchar *blockbuff, *blockend;
213
214 struct decorr_pass decorr_passes [MAX_NTERMS];
215
216} WavpackStream;
217
218// flags for float_flags:
219
220#define FLOAT_SHIFT_ONES 1 // bits left-shifted into float = '1'
221#define FLOAT_SHIFT_SAME 2 // bits left-shifted into float are the same
222#define FLOAT_SHIFT_SENT 4 // bits shifted into float are sent literally
223#define FLOAT_ZEROS_SENT 8 // "zeros" are not all real zeros
224#define FLOAT_NEG_ZEROS 0x10 // contains negative zeros
225#define FLOAT_EXCEPTIONS 0x20 // contains exceptions (inf, nan, etc.)
226
227/////////////////////////////// WavPack Context ///////////////////////////////
228
229// This internal structure holds everything required to encode or decode WavPack
230// files. It is recommended that direct access to this structure be minimized
231// and the provided utilities used instead.
232
233typedef struct {
234 WavpackStream stream;
235 WavpackConfig config;
236
237 uchar *wrapper_data;
238 int wrapper_bytes;
239
240 uchar read_buffer [1024];
241 char error_message [80];
242
243 read_stream infile;
244 uint32_t total_samples, crc_errors, first_flags;
245 int open_flags, norm_offset, reduced_channels, lossy_blocks;
246
247} WavpackContext;
248
249//////////////////////// function prototypes and macros //////////////////////
250
251#define CLEAR(destin) memset (&destin, 0, sizeof (destin));
252
253// bits.c
254
255void bs_open_read (Bitstream *bs, uchar *buffer_start, uchar *buffer_end, read_stream file, uint32_t file_bytes);
256void bs_open_write (Bitstream *bs, uchar *buffer_start, uchar *buffer_end);
257uint32_t bs_close_write (Bitstream *bs);
258
259#define bs_is_open(bs) ((bs)->ptr != NULL)
260
261#define getbit(bs) ( \
262 (((bs)->bc) ? \
263 ((bs)->bc--, (bs)->sr & 1) : \
264 (((++((bs)->ptr) != (bs)->end) ? (void) 0 : (bs)->wrap (bs)), (bs)->bc = 7, ((bs)->sr = *((bs)->ptr)) & 1) \
265 ) ? \
266 ((bs)->sr >>= 1, 1) : \
267 ((bs)->sr >>= 1, 0) \
268)
269
270#define getbits(value, nbits, bs) { \
271 while ((nbits) > (bs)->bc) { \
272 if (++((bs)->ptr) == (bs)->end) (bs)->wrap (bs); \
273 (bs)->sr |= (int32_t)*((bs)->ptr) << (bs)->bc; \
274 (bs)->bc += 8; \
275 } \
276 *(value) = (bs)->sr; \
277 if ((bs)->bc > 32) { \
278 (bs)->bc -= (nbits); \
279 (bs)->sr = *((bs)->ptr) >> (8 - (bs)->bc); \
280 } \
281 else { \
282 (bs)->bc -= (nbits); \
283 (bs)->sr >>= (nbits); \
284 } \
285}
286
287#define putbit(bit, bs) { if (bit) (bs)->sr |= (1 << (bs)->bc); \
288 if (++((bs)->bc) == 8) { \
289 *((bs)->ptr) = (bs)->sr; \
290 (bs)->sr = (bs)->bc = 0; \
291 if (++((bs)->ptr) == (bs)->end) (bs)->wrap (bs); \
292 }}
293
294#define putbit_0(bs) { \
295 if (++((bs)->bc) == 8) { \
296 *((bs)->ptr) = (bs)->sr; \
297 (bs)->sr = (bs)->bc = 0; \
298 if (++((bs)->ptr) == (bs)->end) (bs)->wrap (bs); \
299 }}
300
301#define putbit_1(bs) { (bs)->sr |= (1 << (bs)->bc); \
302 if (++((bs)->bc) == 8) { \
303 *((bs)->ptr) = (bs)->sr; \
304 (bs)->sr = (bs)->bc = 0; \
305 if (++((bs)->ptr) == (bs)->end) (bs)->wrap (bs); \
306 }}
307
308#define putbits(value, nbits, bs) { \
309 (bs)->sr |= (int32_t)(value) << (bs)->bc; \
310 if (((bs)->bc += (nbits)) >= 8) \
311 do { \
312 *((bs)->ptr) = (bs)->sr; \
313 (bs)->sr >>= 8; \
314 if (++((bs)->ptr) == (bs)->end) (bs)->wrap (bs); \
315 } while (((bs)->bc -= 8) >= 8); \
316}
317
318void little_endian_to_native (void *data, char *format);
319void native_to_little_endian (void *data, char *format);
320
321// These macros implement the weight application and update operations
322// that are at the heart of the decorrelation loops. Note that when there
323// are several alternative versions of the same macro (marked with PERFCOND)
324// then the versions are functionally equivalent with respect to WavPack
325// decoding and the user should choose the one that provides the best
326// performance. This may be easier to check when NOT using the assembly
327// language optimizations.
328
329#if 1 // PERFCOND
330#define apply_weight_i(weight, sample) ((weight * sample + 512) >> 10)
331#else
332#define apply_weight_i(weight, sample) ((((weight * sample) >> 8) + 2) >> 2)
333#endif
334
335#define apply_weight_f(weight, sample) (((((sample & 0xffff) * weight) >> 9) + \
336 (((sample & ~0xffff) >> 9) * weight) + 1) >> 1)
337
338#if 1 // PERFCOND
339#define apply_weight(weight, sample) (sample != (short) sample ? \
340 apply_weight_f (weight, sample) : apply_weight_i (weight, sample))
341#else
342#define apply_weight(weight, sample) ((int32_t)((weight * (int64_t) sample + 512) >> 10))
343#endif
344
345#if 0 // PERFCOND
346#define update_weight(weight, delta, source, result) \
347 if (source && result) { int32_t s = (int32_t) (source ^ result) >> 31; weight = (delta ^ s) + (weight - s); }
348#elif 1
349#define update_weight(weight, delta, source, result) \
350 if (source && result) weight += (((source ^ result) >> 30) | 1) * delta
351#else
352#define update_weight(weight, delta, source, result) \
353 if (source && result) (source ^ result) < 0 ? (weight -= delta) : (weight += delta)
354#endif
355
356#define update_weight_clip(weight, delta, source, result) \
357 if (source && result && ((source ^ result) < 0 ? (weight -= delta) < -1024 : (weight += delta) > 1024)) \
358 weight = weight < 0 ? -1024 : 1024
359
360// unpack.c
361
362int unpack_init (WavpackContext *wpc);
363int init_wv_bitstream (WavpackContext *wpc, WavpackMetadata *wpmd);
364int read_decorr_terms (WavpackStream *wps, WavpackMetadata *wpmd);
365int read_decorr_weights (WavpackStream *wps, WavpackMetadata *wpmd);
366int read_decorr_samples (WavpackStream *wps, WavpackMetadata *wpmd);
367int read_float_info (WavpackStream *wps, WavpackMetadata *wpmd);
368int read_int32_info (WavpackStream *wps, WavpackMetadata *wpmd);
369int read_channel_info (WavpackContext *wpc, WavpackMetadata *wpmd);
370int read_config_info (WavpackContext *wpc, WavpackMetadata *wpmd);
371int read_sample_rate (WavpackContext *wpc, WavpackMetadata *wpmd);
372int32_t unpack_samples (WavpackContext *wpc, int32_t *buffer, uint32_t sample_count);
373int check_crc_error (WavpackContext *wpc);
374
375// pack.c
376
377void pack_init (WavpackContext *wpc);
378int pack_start_block (WavpackContext *wpc);
379int pack_samples (WavpackContext *wpc, int32_t *buffer, uint32_t sample_count);
380int pack_finish_block (WavpackContext *wpc);
381
382// metadata.c stuff
383
384int read_metadata_buff (WavpackContext *wpc, WavpackMetadata *wpmd);
385int process_metadata (WavpackContext *wpc, WavpackMetadata *wpmd);
386int copy_metadata (WavpackMetadata *wpmd, uchar *buffer_start, uchar *buffer_end);
387void free_metadata (WavpackMetadata *wpmd);
388
389// words.c stuff
390
391void init_words (WavpackStream *wps);
392int read_entropy_vars (WavpackStream *wps, WavpackMetadata *wpmd);
393void write_entropy_vars (WavpackStream *wps, WavpackMetadata *wpmd);
394int read_hybrid_profile (WavpackStream *wps, WavpackMetadata *wpmd);
395int32_t get_words (int32_t *buffer, int nsamples, uint32_t flags,
396 struct words_data *w, Bitstream *bs);
397void send_word_lossless (int32_t value, int chan,
398 struct words_data *w, Bitstream *bs);
399void send_words (int32_t *buffer, int nsamples, uint32_t flags,
400 struct words_data *w, Bitstream *bs);
401void flush_word (struct words_data *w, Bitstream *bs);
402int log2s (int32_t value);
403int32_t exp2s (int log);
404signed char store_weight (int weight);
405int restore_weight (signed char weight);
406
407#define WORD_EOF (1L << 31)
408
409// float.c
410
411int read_float_info (WavpackStream *wps, WavpackMetadata *wpmd);
412void float_values (WavpackStream *wps, int32_t *values, int32_t num_values);
413void float_normalize (int32_t *values, int32_t num_values, int delta_exp);
414
415// wputils.c
416
417WavpackContext *WavpackOpenFileInput (read_stream infile, char *error);
418
419int WavpackGetMode (WavpackContext *wpc);
420
421#define MODE_WVC 0x1
422#define MODE_LOSSLESS 0x2
423#define MODE_HYBRID 0x4
424#define MODE_FLOAT 0x8
425#define MODE_VALID_TAG 0x10
426#define MODE_HIGH 0x20
427#define MODE_FAST 0x40
428
429uint32_t WavpackUnpackSamples (WavpackContext *wpc, int32_t *buffer, uint32_t samples);
430uint32_t WavpackGetNumSamples (WavpackContext *wpc);
431uint32_t WavpackGetSampleIndex (WavpackContext *wpc);
432int WavpackGetNumErrors (WavpackContext *wpc);
433int WavpackLossyBlocks (WavpackContext *wpc);
434uint32_t WavpackGetSampleRate (WavpackContext *wpc);
435int WavpackGetBitsPerSample (WavpackContext *wpc);
436int WavpackGetBytesPerSample (WavpackContext *wpc);
437int WavpackGetNumChannels (WavpackContext *wpc);
438int WavpackGetReducedChannels (WavpackContext *wpc);
439WavpackContext *WavpackOpenFileOutput (void);
440int WavpackSetConfiguration (WavpackContext *wpc, WavpackConfig *config, uint32_t total_samples);
441void WavpackAddWrapper (WavpackContext *wpc, void *data, uint32_t bcount);
442int WavpackStartBlock (WavpackContext *wpc, uchar *begin, uchar *end);
443int WavpackPackSamples (WavpackContext *wpc, int32_t *sample_buffer, uint32_t sample_count);
444uint32_t WavpackFinishBlock (WavpackContext *wpc);
445void WavpackUpdateNumSamples (WavpackContext *wpc, void *first_block);
446void *WavpackGetWrapperLocation (void *first_block);
447