summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libwavpack/unpack.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libwavpack/unpack.c')
-rw-r--r--lib/rbcodec/codecs/libwavpack/unpack.c780
1 files changed, 780 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libwavpack/unpack.c b/lib/rbcodec/codecs/libwavpack/unpack.c
new file mode 100644
index 0000000000..69252f24ad
--- /dev/null
+++ b/lib/rbcodec/codecs/libwavpack/unpack.c
@@ -0,0 +1,780 @@
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// unpack.c
10
11// This module actually handles the decompression of the audio data, except
12// for the entropy decoding which is handled by the words.c module. For
13// maximum efficiency, the conversion is isolated to tight loops that handle
14// an entire buffer.
15
16#include "wavpack.h"
17
18#include <stdlib.h>
19#include <string.h>
20
21static void strcpy_loc (char *dst, char *src) { while ((*dst++ = *src++) != 0); }
22
23#define LOSSY_MUTE
24
25///////////////////////////// executable code ////////////////////////////////
26
27// This function initializes everything required to unpack a WavPack block
28// and must be called before unpack_samples() is called to obtain audio data.
29// It is assumed that the WavpackHeader has been read into the wps->wphdr
30// (in the current WavpackStream). This is where all the metadata blocks are
31// scanned up to the one containing the audio bitstream.
32
33int unpack_init (WavpackContext *wpc)
34{
35 WavpackStream *wps = &wpc->stream;
36 WavpackMetadata wpmd;
37
38 if (wps->wphdr.block_samples && wps->wphdr.block_index != (uint32_t) -1)
39 wps->sample_index = wps->wphdr.block_index;
40
41 wps->mute_error = FALSE;
42 wps->crc = 0xffffffff;
43 CLEAR (wps->wvbits);
44 CLEAR (wps->decorr_passes);
45 CLEAR (wps->w);
46
47 while (read_metadata_buff (wpc, &wpmd)) {
48 if (!process_metadata (wpc, &wpmd)) {
49 strcpy_loc (wpc->error_message, "invalid metadata!");
50 return FALSE;
51 }
52
53 if (wpmd.id == ID_WV_BITSTREAM)
54 break;
55 }
56
57 if (wps->wphdr.block_samples && !bs_is_open (&wps->wvbits)) {
58 strcpy_loc (wpc->error_message, "invalid WavPack file!");
59 return FALSE;
60 }
61
62 if (wps->wphdr.block_samples) {
63 if ((wps->wphdr.flags & INT32_DATA) && wps->int32_sent_bits)
64 wpc->lossy_blocks = TRUE;
65
66 if ((wps->wphdr.flags & FLOAT_DATA) &&
67 wps->float_flags & (FLOAT_EXCEPTIONS | FLOAT_ZEROS_SENT | FLOAT_SHIFT_SENT | FLOAT_SHIFT_SAME))
68 wpc->lossy_blocks = TRUE;
69 }
70
71 return TRUE;
72}
73
74// This function initialzes the main bitstream for audio samples, which must
75// be in the "wv" file.
76
77int init_wv_bitstream (WavpackContext *wpc, WavpackMetadata *wpmd)
78{
79 WavpackStream *wps = &wpc->stream;
80
81 if (wpmd->data)
82 bs_open_read (&wps->wvbits, wpmd->data, (unsigned char *) wpmd->data + wpmd->byte_length, NULL, 0);
83 else if (wpmd->byte_length)
84 bs_open_read (&wps->wvbits, wpc->read_buffer, wpc->read_buffer + sizeof (wpc->read_buffer),
85 wpc->infile, wpmd->byte_length + (wpmd->byte_length & 1));
86
87 return TRUE;
88}
89
90// Read decorrelation terms from specified metadata block into the
91// decorr_passes array. The terms range from -3 to 8, plus 17 & 18;
92// other values are reserved and generate errors for now. The delta
93// ranges from 0 to 7 with all values valid. Note that the terms are
94// stored in the opposite order in the decorr_passes array compared
95// to packing.
96
97int read_decorr_terms (WavpackStream *wps, WavpackMetadata *wpmd)
98{
99 int termcnt = wpmd->byte_length;
100 uchar *byteptr = wpmd->data;
101 struct decorr_pass *dpp;
102
103 if (termcnt > MAX_NTERMS)
104 return FALSE;
105
106 wps->num_terms = termcnt;
107
108 for (dpp = wps->decorr_passes + termcnt - 1; termcnt--; dpp--) {
109 dpp->term = (int)(*byteptr & 0x1f) - 5;
110 dpp->delta = (*byteptr++ >> 5) & 0x7;
111
112 if (!dpp->term || dpp->term < -3 || (dpp->term > MAX_TERM && dpp->term < 17) || dpp->term > 18)
113 return FALSE;
114 }
115
116 return TRUE;
117}
118
119// Read decorrelation weights from specified metadata block into the
120// decorr_passes array. The weights range +/-1024, but are rounded and
121// truncated to fit in signed chars for metadata storage. Weights are
122// separate for the two channels and are specified from the "last" term
123// (first during encode). Unspecified weights are set to zero.
124
125int read_decorr_weights (WavpackStream *wps, WavpackMetadata *wpmd)
126{
127 int termcnt = wpmd->byte_length, tcount;
128 signed char *byteptr = wpmd->data;
129 struct decorr_pass *dpp;
130
131 if (!(wps->wphdr.flags & MONO_DATA))
132 termcnt /= 2;
133
134 if (termcnt > wps->num_terms)
135 return FALSE;
136
137 for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++)
138 dpp->weight_A = dpp->weight_B = 0;
139
140 while (--dpp >= wps->decorr_passes && termcnt--) {
141 dpp->weight_A = restore_weight (*byteptr++);
142
143 if (!(wps->wphdr.flags & MONO_DATA))
144 dpp->weight_B = restore_weight (*byteptr++);
145 }
146
147 return TRUE;
148}
149
150// Read decorrelation samples from specified metadata block into the
151// decorr_passes array. The samples are signed 32-bit values, but are
152// converted to signed log2 values for storage in metadata. Values are
153// stored for both channels and are specified from the "last" term
154// (first during encode) with unspecified samples set to zero. The
155// number of samples stored varies with the actual term value, so
156// those must obviously come first in the metadata.
157
158int read_decorr_samples (WavpackStream *wps, WavpackMetadata *wpmd)
159{
160 uchar *byteptr = wpmd->data;
161 uchar *endptr = byteptr + wpmd->byte_length;
162 struct decorr_pass *dpp;
163 int tcount;
164
165 for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++) {
166 CLEAR (dpp->samples_A);
167 CLEAR (dpp->samples_B);
168 }
169
170 if (wps->wphdr.version == 0x402 && (wps->wphdr.flags & HYBRID_FLAG)) {
171 byteptr += 2;
172
173 if (!(wps->wphdr.flags & MONO_DATA))
174 byteptr += 2;
175 }
176
177 while (dpp-- > wps->decorr_passes && byteptr < endptr)
178 if (dpp->term > MAX_TERM) {
179 dpp->samples_A [0] = exp2s ((short)(byteptr [0] + (byteptr [1] << 8)));
180 dpp->samples_A [1] = exp2s ((short)(byteptr [2] + (byteptr [3] << 8)));
181 byteptr += 4;
182
183 if (!(wps->wphdr.flags & MONO_DATA)) {
184 dpp->samples_B [0] = exp2s ((short)(byteptr [0] + (byteptr [1] << 8)));
185 dpp->samples_B [1] = exp2s ((short)(byteptr [2] + (byteptr [3] << 8)));
186 byteptr += 4;
187 }
188 }
189 else if (dpp->term < 0) {
190 dpp->samples_A [0] = exp2s ((short)(byteptr [0] + (byteptr [1] << 8)));
191 dpp->samples_B [0] = exp2s ((short)(byteptr [2] + (byteptr [3] << 8)));
192 byteptr += 4;
193 }
194 else {
195 int m = 0, cnt = dpp->term;
196
197 while (cnt--) {
198 dpp->samples_A [m] = exp2s ((short)(byteptr [0] + (byteptr [1] << 8)));
199 byteptr += 2;
200
201 if (!(wps->wphdr.flags & MONO_DATA)) {
202 dpp->samples_B [m] = exp2s ((short)(byteptr [0] + (byteptr [1] << 8)));
203 byteptr += 2;
204 }
205
206 m++;
207 }
208 }
209
210 return byteptr == endptr;
211}
212
213// Read the int32 data from the specified metadata into the specified stream.
214// This data is used for integer data that has more than 24 bits of magnitude
215// or, in some cases, used to eliminate redundant bits from any audio stream.
216
217int read_int32_info (WavpackStream *wps, WavpackMetadata *wpmd)
218{
219 int bytecnt = wpmd->byte_length;
220 char *byteptr = wpmd->data;
221
222 if (bytecnt != 4)
223 return FALSE;
224
225 wps->int32_sent_bits = *byteptr++;
226 wps->int32_zeros = *byteptr++;
227 wps->int32_ones = *byteptr++;
228 wps->int32_dups = *byteptr;
229 return TRUE;
230}
231
232// Read multichannel information from metadata. The first byte is the total
233// number of channels and the following bytes represent the channel_mask
234// as described for Microsoft WAVEFORMATEX.
235
236int read_channel_info (WavpackContext *wpc, WavpackMetadata *wpmd)
237{
238 int bytecnt = wpmd->byte_length, shift = 0;
239 char *byteptr = wpmd->data;
240 uint32_t mask = 0;
241
242 if (!bytecnt || bytecnt > 5)
243 return FALSE;
244
245 wpc->config.num_channels = *byteptr++;
246
247 while (--bytecnt) {
248 mask |= (uint32_t) *byteptr++ << shift;
249 shift += 8;
250 }
251
252 wpc->config.channel_mask = mask;
253 return TRUE;
254}
255
256// Read configuration information from metadata.
257
258int read_config_info (WavpackContext *wpc, WavpackMetadata *wpmd)
259{
260 int bytecnt = wpmd->byte_length;
261 uchar *byteptr = wpmd->data;
262
263 if (bytecnt >= 3) {
264 wpc->config.flags &= 0xff;
265 wpc->config.flags |= (int32_t) *byteptr++ << 8;
266 wpc->config.flags |= (int32_t) *byteptr++ << 16;
267 wpc->config.flags |= (int32_t) *byteptr << 24;
268 }
269
270 return TRUE;
271}
272
273// Read non-standard sampling rate from metadata.
274
275int read_sample_rate (WavpackContext *wpc, WavpackMetadata *wpmd)
276{
277 int bytecnt = wpmd->byte_length;
278 uchar *byteptr = wpmd->data;
279
280 if (bytecnt == 3) {
281 wpc->config.sample_rate = (int32_t) *byteptr++;
282 wpc->config.sample_rate |= (int32_t) *byteptr++ << 8;
283 wpc->config.sample_rate |= (int32_t) *byteptr++ << 16;
284 }
285
286 return TRUE;
287}
288
289// This monster actually unpacks the WavPack bitstream(s) into the specified
290// buffer as 32-bit integers or floats (depending on orignal data). Lossy
291// samples will be clipped to their original limits (i.e. 8-bit samples are
292// clipped to -128/+127) but are still returned in int32_ts. It is up to the
293// caller to potentially reformat this for the final output including any
294// multichannel distribution, block alignment or endian compensation. The
295// function unpack_init() must have been called and the entire WavPack block
296// must still be visible (although wps->blockbuff will not be accessed again).
297// For maximum clarity, the function is broken up into segments that handle
298// various modes. This makes for a few extra infrequent flag checks, but
299// makes the code easier to follow because the nesting does not become so
300// deep. For maximum efficiency, the conversion is isolated to tight loops
301// that handle an entire buffer. The function returns the total number of
302// samples unpacked, which can be less than the number requested if an error
303// occurs or the end of the block is reached.
304
305#if defined(CPU_COLDFIRE)
306extern void decorr_stereo_pass_cont_mcf5249 (struct decorr_pass *dpp, int32_t *buffer, int32_t sample_count);
307#elif defined(CPU_ARM)
308extern void decorr_stereo_pass_cont_arm (struct decorr_pass *dpp, int32_t *buffer, int32_t sample_count);
309extern void decorr_stereo_pass_cont_arml (struct decorr_pass *dpp, int32_t *buffer, int32_t sample_count);
310#else
311static void decorr_stereo_pass_cont (struct decorr_pass *dpp, int32_t *buffer, int32_t sample_count);
312#endif
313
314static void decorr_mono_pass (struct decorr_pass *dpp, int32_t *buffer, int32_t sample_count);
315static void decorr_stereo_pass (struct decorr_pass *dpp, int32_t *buffer, int32_t sample_count);
316static void fixup_samples (WavpackStream *wps, int32_t *buffer, uint32_t sample_count);
317
318int32_t unpack_samples (WavpackContext *wpc, int32_t *buffer, uint32_t sample_count)
319{
320 WavpackStream *wps = &wpc->stream;
321 uint32_t flags = wps->wphdr.flags, crc = wps->crc, i;
322 int32_t mute_limit = (1L << ((flags & MAG_MASK) >> MAG_LSB)) + 2;
323 struct decorr_pass *dpp;
324 int32_t *bptr, *eptr;
325 int tcount;
326
327 if (wps->sample_index + sample_count > wps->wphdr.block_index + wps->wphdr.block_samples)
328 sample_count = wps->wphdr.block_index + wps->wphdr.block_samples - wps->sample_index;
329
330 if (wps->mute_error) {
331 memset (buffer, 0, sample_count * (flags & MONO_FLAG ? 4 : 8));
332 wps->sample_index += sample_count;
333 return sample_count;
334 }
335
336 if (flags & HYBRID_FLAG)
337 mute_limit *= 2;
338
339 ///////////////////// handle version 4 mono data /////////////////////////
340
341 if (flags & MONO_DATA) {
342 eptr = buffer + sample_count;
343 i = get_words (buffer, sample_count, flags, &wps->w, &wps->wvbits);
344
345 for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++)
346 decorr_mono_pass (dpp, buffer, sample_count);
347
348 for (bptr = buffer; bptr < eptr; ++bptr) {
349 if (labs (bptr [0]) > mute_limit) {
350 i = bptr - buffer;
351 break;
352 }
353
354 crc = crc * 3 + bptr [0];
355 }
356 }
357
358 //////////////////// handle version 4 stereo data ////////////////////////
359
360 else {
361 eptr = buffer + (sample_count * 2);
362 i = get_words (buffer, sample_count, flags, &wps->w, &wps->wvbits);
363
364 if (sample_count < 16)
365 for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++)
366 decorr_stereo_pass (dpp, buffer, sample_count);
367 else
368 for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++) {
369 decorr_stereo_pass (dpp, buffer, 8);
370#if defined(CPU_COLDFIRE)
371 decorr_stereo_pass_cont_mcf5249 (dpp, buffer + 16, sample_count - 8);
372#elif defined(CPU_ARM)
373 if (((flags & MAG_MASK) >> MAG_LSB) > 15)
374 decorr_stereo_pass_cont_arml (dpp, buffer + 16, sample_count - 8);
375 else
376 decorr_stereo_pass_cont_arm (dpp, buffer + 16, sample_count - 8);
377#else
378 decorr_stereo_pass_cont (dpp, buffer + 16, sample_count - 8);
379#endif
380 }
381
382 if (flags & JOINT_STEREO)
383 for (bptr = buffer; bptr < eptr; bptr += 2) {
384 bptr [0] += (bptr [1] -= (bptr [0] >> 1));
385
386 if (labs (bptr [0]) > mute_limit || labs (bptr [1]) > mute_limit) {
387 i = (bptr - buffer) / 2;
388 break;
389 }
390
391 crc = (crc * 3 + bptr [0]) * 3 + bptr [1];
392 }
393 else
394 for (bptr = buffer; bptr < eptr; bptr += 2) {
395 if (labs (bptr [0]) > mute_limit || labs (bptr [1]) > mute_limit) {
396 i = (bptr - buffer) / 2;
397 break;
398 }
399
400 crc = (crc * 3 + bptr [0]) * 3 + bptr [1];
401 }
402 }
403
404 if (i != sample_count) {
405 memset (buffer, 0, sample_count * (flags & MONO_FLAG ? 4 : 8));
406 wps->mute_error = TRUE;
407 i = sample_count;
408 }
409
410 fixup_samples (wps, buffer, i);
411
412 if (flags & FALSE_STEREO) {
413 int32_t *dptr = buffer + i * 2;
414 int32_t *sptr = buffer + i;
415 int32_t c = i;
416
417 while (c--) {
418 *--dptr = *--sptr;
419 *--dptr = *sptr;
420 }
421 }
422
423 wps->sample_index += i;
424 wps->crc = crc;
425
426 return i;
427}
428
429static void decorr_stereo_pass (struct decorr_pass *dpp, int32_t *buffer, int32_t sample_count)
430{
431 int32_t delta = dpp->delta, weight_A = dpp->weight_A, weight_B = dpp->weight_B;
432 int32_t *bptr, *eptr = buffer + (sample_count * 2), sam_A, sam_B;
433 int m, k;
434
435 switch (dpp->term) {
436
437 case 17:
438 for (bptr = buffer; bptr < eptr; bptr += 2) {
439 sam_A = 2 * dpp->samples_A [0] - dpp->samples_A [1];
440 dpp->samples_A [1] = dpp->samples_A [0];
441 dpp->samples_A [0] = apply_weight (weight_A, sam_A) + bptr [0];
442 update_weight (weight_A, delta, sam_A, bptr [0]);
443 bptr [0] = dpp->samples_A [0];
444
445 sam_A = 2 * dpp->samples_B [0] - dpp->samples_B [1];
446 dpp->samples_B [1] = dpp->samples_B [0];
447 dpp->samples_B [0] = apply_weight (weight_B, sam_A) + bptr [1];
448 update_weight (weight_B, delta, sam_A, bptr [1]);
449 bptr [1] = dpp->samples_B [0];
450 }
451
452 break;
453
454 case 18:
455 for (bptr = buffer; bptr < eptr; bptr += 2) {
456 sam_A = (3 * dpp->samples_A [0] - dpp->samples_A [1]) >> 1;
457 dpp->samples_A [1] = dpp->samples_A [0];
458 dpp->samples_A [0] = apply_weight (weight_A, sam_A) + bptr [0];
459 update_weight (weight_A, delta, sam_A, bptr [0]);
460 bptr [0] = dpp->samples_A [0];
461
462 sam_A = (3 * dpp->samples_B [0] - dpp->samples_B [1]) >> 1;
463 dpp->samples_B [1] = dpp->samples_B [0];
464 dpp->samples_B [0] = apply_weight (weight_B, sam_A) + bptr [1];
465 update_weight (weight_B, delta, sam_A, bptr [1]);
466 bptr [1] = dpp->samples_B [0];
467 }
468
469 break;
470
471 default:
472 for (m = 0, k = dpp->term & (MAX_TERM - 1), bptr = buffer; bptr < eptr; bptr += 2) {
473 sam_A = dpp->samples_A [m];
474 dpp->samples_A [k] = apply_weight (weight_A, sam_A) + bptr [0];
475 update_weight (weight_A, delta, sam_A, bptr [0]);
476 bptr [0] = dpp->samples_A [k];
477
478 sam_A = dpp->samples_B [m];
479 dpp->samples_B [k] = apply_weight (weight_B, sam_A) + bptr [1];
480 update_weight (weight_B, delta, sam_A, bptr [1]);
481 bptr [1] = dpp->samples_B [k];
482
483 m = (m + 1) & (MAX_TERM - 1);
484 k = (k + 1) & (MAX_TERM - 1);
485 }
486
487 if (m) {
488 int32_t temp_samples [MAX_TERM];
489
490 memcpy (temp_samples, dpp->samples_A, sizeof (dpp->samples_A));
491
492 for (k = 0; k < MAX_TERM; k++, m++)
493 dpp->samples_A [k] = temp_samples [m & (MAX_TERM - 1)];
494
495 memcpy (temp_samples, dpp->samples_B, sizeof (dpp->samples_B));
496
497 for (k = 0; k < MAX_TERM; k++, m++)
498 dpp->samples_B [k] = temp_samples [m & (MAX_TERM - 1)];
499 }
500
501 break;
502
503 case -1:
504 for (bptr = buffer; bptr < eptr; bptr += 2) {
505 sam_A = bptr [0] + apply_weight (weight_A, dpp->samples_A [0]);
506 update_weight_clip (weight_A, delta, dpp->samples_A [0], bptr [0]);
507 bptr [0] = sam_A;
508 dpp->samples_A [0] = bptr [1] + apply_weight (weight_B, sam_A);
509 update_weight_clip (weight_B, delta, sam_A, bptr [1]);
510 bptr [1] = dpp->samples_A [0];
511 }
512
513 break;
514
515 case -2:
516 for (bptr = buffer; bptr < eptr; bptr += 2) {
517 sam_B = bptr [1] + apply_weight (weight_B, dpp->samples_B [0]);
518 update_weight_clip (weight_B, delta, dpp->samples_B [0], bptr [1]);
519 bptr [1] = sam_B;
520 dpp->samples_B [0] = bptr [0] + apply_weight (weight_A, sam_B);
521 update_weight_clip (weight_A, delta, sam_B, bptr [0]);
522 bptr [0] = dpp->samples_B [0];
523 }
524
525 break;
526
527 case -3:
528 for (bptr = buffer; bptr < eptr; bptr += 2) {
529 sam_A = bptr [0] + apply_weight (weight_A, dpp->samples_A [0]);
530 update_weight_clip (weight_A, delta, dpp->samples_A [0], bptr [0]);
531 sam_B = bptr [1] + apply_weight (weight_B, dpp->samples_B [0]);
532 update_weight_clip (weight_B, delta, dpp->samples_B [0], bptr [1]);
533 bptr [0] = dpp->samples_B [0] = sam_A;
534 bptr [1] = dpp->samples_A [0] = sam_B;
535 }
536
537 break;
538 }
539
540 dpp->weight_A = weight_A;
541 dpp->weight_B = weight_B;
542}
543
544#if (!defined(CPU_COLDFIRE) && !defined(CPU_ARM))
545
546static void decorr_stereo_pass_cont (struct decorr_pass *dpp, int32_t *buffer, int32_t sample_count)
547{
548 int32_t delta = dpp->delta, weight_A = dpp->weight_A, weight_B = dpp->weight_B;
549 int32_t *bptr, *tptr, *eptr = buffer + (sample_count * 2), sam_A, sam_B;
550 int k, i;
551
552 switch (dpp->term) {
553
554 case 17:
555 for (bptr = buffer; bptr < eptr; bptr += 2) {
556 sam_A = 2 * bptr [-2] - bptr [-4];
557 bptr [0] = apply_weight (weight_A, sam_A) + (sam_B = bptr [0]);
558 update_weight (weight_A, delta, sam_A, sam_B);
559
560 sam_A = 2 * bptr [-1] - bptr [-3];
561 bptr [1] = apply_weight (weight_B, sam_A) + (sam_B = bptr [1]);
562 update_weight (weight_B, delta, sam_A, sam_B);
563 }
564
565 dpp->samples_B [0] = bptr [-1];
566 dpp->samples_A [0] = bptr [-2];
567 dpp->samples_B [1] = bptr [-3];
568 dpp->samples_A [1] = bptr [-4];
569 break;
570
571 case 18:
572 for (bptr = buffer; bptr < eptr; bptr += 2) {
573 sam_A = (3 * bptr [-2] - bptr [-4]) >> 1;
574 bptr [0] = apply_weight (weight_A, sam_A) + (sam_B = bptr [0]);
575 update_weight (weight_A, delta, sam_A, sam_B);
576
577 sam_A = (3 * bptr [-1] - bptr [-3]) >> 1;
578 bptr [1] = apply_weight (weight_B, sam_A) + (sam_B = bptr [1]);
579 update_weight (weight_B, delta, sam_A, sam_B);
580 }
581
582 dpp->samples_B [0] = bptr [-1];
583 dpp->samples_A [0] = bptr [-2];
584 dpp->samples_B [1] = bptr [-3];
585 dpp->samples_A [1] = bptr [-4];
586 break;
587
588 default:
589 for (bptr = buffer, tptr = buffer - (dpp->term * 2); bptr < eptr; bptr += 2, tptr += 2) {
590 bptr [0] = apply_weight (weight_A, tptr [0]) + (sam_A = bptr [0]);
591 update_weight (weight_A, delta, tptr [0], sam_A);
592
593 bptr [1] = apply_weight (weight_B, tptr [1]) + (sam_A = bptr [1]);
594 update_weight (weight_B, delta, tptr [1], sam_A);
595 }
596
597 for (k = dpp->term - 1, i = 8; i--; k--) {
598 dpp->samples_B [k & (MAX_TERM - 1)] = *--bptr;
599 dpp->samples_A [k & (MAX_TERM - 1)] = *--bptr;
600 }
601
602 break;
603
604 case -1:
605 for (bptr = buffer; bptr < eptr; bptr += 2) {
606 bptr [0] = apply_weight (weight_A, bptr [-1]) + (sam_A = bptr [0]);
607 update_weight_clip (weight_A, delta, bptr [-1], sam_A);
608 bptr [1] = apply_weight (weight_B, bptr [0]) + (sam_A = bptr [1]);
609 update_weight_clip (weight_B, delta, bptr [0], sam_A);
610 }
611
612 dpp->samples_A [0] = bptr [-1];
613 break;
614
615 case -2:
616 for (bptr = buffer; bptr < eptr; bptr += 2) {
617 bptr [1] = apply_weight (weight_B, bptr [-2]) + (sam_A = bptr [1]);
618 update_weight_clip (weight_B, delta, bptr [-2], sam_A);
619 bptr [0] = apply_weight (weight_A, bptr [1]) + (sam_A = bptr [0]);
620 update_weight_clip (weight_A, delta, bptr [1], sam_A);
621 }
622
623 dpp->samples_B [0] = bptr [-2];
624 break;
625
626 case -3:
627 for (bptr = buffer; bptr < eptr; bptr += 2) {
628 bptr [0] = apply_weight (weight_A, bptr [-1]) + (sam_A = bptr [0]);
629 update_weight_clip (weight_A, delta, bptr [-1], sam_A);
630 bptr [1] = apply_weight (weight_B, bptr [-2]) + (sam_A = bptr [1]);
631 update_weight_clip (weight_B, delta, bptr [-2], sam_A);
632 }
633
634 dpp->samples_A [0] = bptr [-1];
635 dpp->samples_B [0] = bptr [-2];
636 break;
637 }
638
639 dpp->weight_A = weight_A;
640 dpp->weight_B = weight_B;
641}
642
643#endif
644
645static void decorr_mono_pass (struct decorr_pass *dpp, int32_t *buffer, int32_t sample_count)
646{
647 int32_t delta = dpp->delta, weight_A = dpp->weight_A;
648 int32_t *bptr, *eptr = buffer + sample_count, sam_A;
649 int m, k;
650
651 switch (dpp->term) {
652
653 case 17:
654 for (bptr = buffer; bptr < eptr; bptr++) {
655 sam_A = 2 * dpp->samples_A [0] - dpp->samples_A [1];
656 dpp->samples_A [1] = dpp->samples_A [0];
657 dpp->samples_A [0] = apply_weight (weight_A, sam_A) + bptr [0];
658 update_weight (weight_A, delta, sam_A, bptr [0]);
659 bptr [0] = dpp->samples_A [0];
660 }
661
662 break;
663
664 case 18:
665 for (bptr = buffer; bptr < eptr; bptr++) {
666 sam_A = (3 * dpp->samples_A [0] - dpp->samples_A [1]) >> 1;
667 dpp->samples_A [1] = dpp->samples_A [0];
668 dpp->samples_A [0] = apply_weight (weight_A, sam_A) + bptr [0];
669 update_weight (weight_A, delta, sam_A, bptr [0]);
670 bptr [0] = dpp->samples_A [0];
671 }
672
673 break;
674
675 default:
676 for (m = 0, k = dpp->term & (MAX_TERM - 1), bptr = buffer; bptr < eptr; bptr++) {
677 sam_A = dpp->samples_A [m];
678 dpp->samples_A [k] = apply_weight (weight_A, sam_A) + bptr [0];
679 update_weight (weight_A, delta, sam_A, bptr [0]);
680 bptr [0] = dpp->samples_A [k];
681 m = (m + 1) & (MAX_TERM - 1);
682 k = (k + 1) & (MAX_TERM - 1);
683 }
684
685 if (m) {
686 int32_t temp_samples [MAX_TERM];
687
688 memcpy (temp_samples, dpp->samples_A, sizeof (dpp->samples_A));
689
690 for (k = 0; k < MAX_TERM; k++, m++)
691 dpp->samples_A [k] = temp_samples [m & (MAX_TERM - 1)];
692 }
693
694 break;
695 }
696
697 dpp->weight_A = weight_A;
698}
699
700
701// This is a helper function for unpack_samples() that applies several final
702// operations. First, if the data is 32-bit float data, then that conversion
703// is done in the float.c module (whether lossy or lossless) and we return.
704// Otherwise, if the extended integer data applies, then that operation is
705// executed first. If the unpacked data is lossy (and not corrected) then
706// it is clipped and shifted in a single operation. Otherwise, if it's
707// lossless then the last step is to apply the final shift (if any).
708
709// This function has been modified for RockBox to return all integer samples
710// as 28-bits, and clipping (for lossy mode) has been eliminated because this
711// now happens in the dsp module.
712
713static void fixup_samples (WavpackStream *wps, int32_t *buffer, uint32_t sample_count)
714{
715 uint32_t flags = wps->wphdr.flags;
716 int shift = (flags & SHIFT_MASK) >> SHIFT_LSB;
717
718 shift += 21 - (flags & BYTES_STORED) * 8; // this provides RockBox with 28-bit (+sign)
719
720 if (flags & FLOAT_DATA) {
721 float_values (wps, buffer, (flags & MONO_DATA) ? sample_count : sample_count * 2);
722 return;
723 }
724
725 if (flags & INT32_DATA) {
726 uint32_t count = (flags & MONO_DATA) ? sample_count : sample_count * 2;
727 int sent_bits = wps->int32_sent_bits, zeros = wps->int32_zeros;
728 int ones = wps->int32_ones, dups = wps->int32_dups;
729 int32_t *dptr = buffer;
730
731 if (!(flags & HYBRID_FLAG) && !sent_bits && (zeros + ones + dups))
732 while (count--) {
733 if (zeros)
734 *dptr <<= zeros;
735 else if (ones)
736 *dptr = ((*dptr + 1) << ones) - 1;
737 else if (dups)
738 *dptr = ((*dptr + (*dptr & 1)) << dups) - (*dptr & 1);
739
740 dptr++;
741 }
742 else
743 shift += zeros + sent_bits + ones + dups;
744 }
745
746 if (shift > 0) {
747 if (!(flags & MONO_DATA))
748 sample_count *= 2;
749
750 while (sample_count--)
751 *buffer++ <<= shift;
752 }
753 else if (shift < 0) {
754 shift = -shift;
755
756 if (!(flags & MONO_DATA))
757 sample_count *= 2;
758
759 while (sample_count--)
760 *buffer++ >>= shift;
761 }
762}
763
764// This function checks the crc value(s) for an unpacked block, returning the
765// number of actual crc errors detected for the block. The block must be
766// completely unpacked before this test is valid. For losslessly unpacked
767// blocks of float or extended integer data the extended crc is also checked.
768// Note that WavPack's crc is not a CCITT approved polynomial algorithm, but
769// is a much simpler method that is virtually as robust for real world data.
770
771int check_crc_error (WavpackContext *wpc)
772{
773 WavpackStream *wps = &wpc->stream;
774 int result = 0;
775
776 if (wps->crc != wps->wphdr.crc)
777 ++result;
778
779 return result;
780}