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 ////////////////////////////////////////////////////////////////////////////
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
21 static void strcpy_loc (char *dst
, char *src
) { while ((*dst
++ = *src
++) != 0); }
25 ///////////////////////////// executable code ////////////////////////////////
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.
33 int unpack_init (WavpackContext
*wpc
)
35 WavpackStream
*wps
= &wpc
->stream
;
38 if (wps
->wphdr
.block_samples
&& wps
->wphdr
.block_index
!= (uint32_t) -1)
39 wps
->sample_index
= wps
->wphdr
.block_index
;
41 wps
->mute_error
= FALSE
;
42 wps
->crc
= 0xffffffff;
44 CLEAR (wps
->decorr_passes
);
47 while (read_metadata_buff (wpc
, &wpmd
)) {
48 if (!process_metadata (wpc
, &wpmd
)) {
49 strcpy_loc (wpc
->error_message
, "invalid metadata!");
53 if (wpmd
.id
== ID_WV_BITSTREAM
)
57 if (wps
->wphdr
.block_samples
&& !bs_is_open (&wps
->wvbits
)) {
58 strcpy_loc (wpc
->error_message
, "invalid WavPack file!");
62 if (wps
->wphdr
.block_samples
) {
63 if ((wps
->wphdr
.flags
& INT32_DATA
) && wps
->int32_sent_bits
)
64 wpc
->lossy_blocks
= TRUE
;
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
;
74 // This function initialzes the main bitstream for audio samples, which must
75 // be in the "wv" file.
77 int init_wv_bitstream (WavpackContext
*wpc
, WavpackMetadata
*wpmd
)
79 WavpackStream
*wps
= &wpc
->stream
;
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));
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
97 int read_decorr_terms (WavpackStream
*wps
, WavpackMetadata
*wpmd
)
99 int termcnt
= wpmd
->byte_length
;
100 uchar
*byteptr
= wpmd
->data
;
101 struct decorr_pass
*dpp
;
103 if (termcnt
> MAX_NTERMS
)
106 wps
->num_terms
= termcnt
;
108 for (dpp
= wps
->decorr_passes
+ termcnt
- 1; termcnt
--; dpp
--) {
109 dpp
->term
= (int)(*byteptr
& 0x1f) - 5;
110 dpp
->delta
= (*byteptr
++ >> 5) & 0x7;
112 if (!dpp
->term
|| dpp
->term
< -3 || (dpp
->term
> MAX_TERM
&& dpp
->term
< 17) || dpp
->term
> 18)
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.
125 int read_decorr_weights (WavpackStream
*wps
, WavpackMetadata
*wpmd
)
127 int termcnt
= wpmd
->byte_length
, tcount
;
128 signed char *byteptr
= wpmd
->data
;
129 struct decorr_pass
*dpp
;
131 if (!(wps
->wphdr
.flags
& MONO_DATA
))
134 if (termcnt
> wps
->num_terms
)
137 for (tcount
= wps
->num_terms
, dpp
= wps
->decorr_passes
; tcount
--; dpp
++)
138 dpp
->weight_A
= dpp
->weight_B
= 0;
140 while (--dpp
>= wps
->decorr_passes
&& termcnt
--) {
141 dpp
->weight_A
= restore_weight (*byteptr
++);
143 if (!(wps
->wphdr
.flags
& MONO_DATA
))
144 dpp
->weight_B
= restore_weight (*byteptr
++);
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.
158 int read_decorr_samples (WavpackStream
*wps
, WavpackMetadata
*wpmd
)
160 uchar
*byteptr
= wpmd
->data
;
161 uchar
*endptr
= byteptr
+ wpmd
->byte_length
;
162 struct decorr_pass
*dpp
;
165 for (tcount
= wps
->num_terms
, dpp
= wps
->decorr_passes
; tcount
--; dpp
++) {
166 CLEAR (dpp
->samples_A
);
167 CLEAR (dpp
->samples_B
);
170 if (wps
->wphdr
.version
== 0x402 && (wps
->wphdr
.flags
& HYBRID_FLAG
)) {
173 if (!(wps
->wphdr
.flags
& MONO_DATA
))
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)));
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)));
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)));
195 int m
= 0, cnt
= dpp
->term
;
198 dpp
->samples_A
[m
] = exp2s ((short)(byteptr
[0] + (byteptr
[1] << 8)));
201 if (!(wps
->wphdr
.flags
& MONO_DATA
)) {
202 dpp
->samples_B
[m
] = exp2s ((short)(byteptr
[0] + (byteptr
[1] << 8)));
210 return byteptr
== endptr
;
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.
217 int read_int32_info (WavpackStream
*wps
, WavpackMetadata
*wpmd
)
219 int bytecnt
= wpmd
->byte_length
;
220 char *byteptr
= wpmd
->data
;
225 wps
->int32_sent_bits
= *byteptr
++;
226 wps
->int32_zeros
= *byteptr
++;
227 wps
->int32_ones
= *byteptr
++;
228 wps
->int32_dups
= *byteptr
;
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.
236 int read_channel_info (WavpackContext
*wpc
, WavpackMetadata
*wpmd
)
238 int bytecnt
= wpmd
->byte_length
, shift
= 0;
239 char *byteptr
= wpmd
->data
;
242 if (!bytecnt
|| bytecnt
> 5)
245 wpc
->config
.num_channels
= *byteptr
++;
248 mask
|= (uint32_t) *byteptr
++ << shift
;
252 wpc
->config
.channel_mask
= mask
;
256 // Read configuration information from metadata.
258 int read_config_info (WavpackContext
*wpc
, WavpackMetadata
*wpmd
)
260 int bytecnt
= wpmd
->byte_length
;
261 uchar
*byteptr
= wpmd
->data
;
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;
273 // Read non-standard sampling rate from metadata.
275 int read_sample_rate (WavpackContext
*wpc
, WavpackMetadata
*wpmd
)
277 int bytecnt
= wpmd
->byte_length
;
278 uchar
*byteptr
= wpmd
->data
;
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;
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.
305 #if defined(CPU_COLDFIRE)
306 extern void decorr_stereo_pass_cont_mcf5249 (struct decorr_pass
*dpp
, int32_t *buffer
, int32_t sample_count
);
307 #elif defined(CPU_ARM)
308 extern void decorr_stereo_pass_cont_arm (struct decorr_pass
*dpp
, int32_t *buffer
, int32_t sample_count
);
309 extern void decorr_stereo_pass_cont_arml (struct decorr_pass
*dpp
, int32_t *buffer
, int32_t sample_count
);
311 static void decorr_stereo_pass_cont (struct decorr_pass
*dpp
, int32_t *buffer
, int32_t sample_count
);
314 static void decorr_mono_pass (struct decorr_pass
*dpp
, int32_t *buffer
, int32_t sample_count
);
315 static void decorr_stereo_pass (struct decorr_pass
*dpp
, int32_t *buffer
, int32_t sample_count
);
316 static void fixup_samples (WavpackStream
*wps
, int32_t *buffer
, uint32_t sample_count
);
318 int32_t unpack_samples (WavpackContext
*wpc
, int32_t *buffer
, uint32_t sample_count
)
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
;
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
;
330 if (wps
->mute_error
) {
331 memset (buffer
, 0, sample_count
* (flags
& MONO_FLAG
? 4 : 8));
332 wps
->sample_index
+= sample_count
;
336 if (flags
& HYBRID_FLAG
)
339 ///////////////////// handle version 4 mono data /////////////////////////
341 if (flags
& MONO_DATA
) {
342 eptr
= buffer
+ sample_count
;
343 i
= get_words (buffer
, sample_count
, flags
, &wps
->w
, &wps
->wvbits
);
345 for (tcount
= wps
->num_terms
, dpp
= wps
->decorr_passes
; tcount
--; dpp
++)
346 decorr_mono_pass (dpp
, buffer
, sample_count
);
348 for (bptr
= buffer
; bptr
< eptr
; ++bptr
) {
349 if (labs (bptr
[0]) > mute_limit
) {
354 crc
= crc
* 3 + bptr
[0];
358 //////////////////// handle version 4 stereo data ////////////////////////
361 eptr
= buffer
+ (sample_count
* 2);
362 i
= get_words (buffer
, sample_count
, flags
, &wps
->w
, &wps
->wvbits
);
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
);
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);
376 decorr_stereo_pass_cont_arm (dpp
, buffer
+ 16, sample_count
- 8);
378 decorr_stereo_pass_cont (dpp
, buffer
+ 16, sample_count
- 8);
382 if (flags
& JOINT_STEREO
)
383 for (bptr
= buffer
; bptr
< eptr
; bptr
+= 2) {
384 bptr
[0] += (bptr
[1] -= (bptr
[0] >> 1));
386 if (labs (bptr
[0]) > mute_limit
|| labs (bptr
[1]) > mute_limit
) {
387 i
= (bptr
- buffer
) / 2;
391 crc
= (crc
* 3 + bptr
[0]) * 3 + bptr
[1];
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;
400 crc
= (crc
* 3 + bptr
[0]) * 3 + bptr
[1];
404 if (i
!= sample_count
) {
405 memset (buffer
, 0, sample_count
* (flags
& MONO_FLAG
? 4 : 8));
406 wps
->mute_error
= TRUE
;
410 fixup_samples (wps
, buffer
, i
);
412 if (flags
& FALSE_STEREO
) {
413 int32_t *dptr
= buffer
+ i
* 2;
414 int32_t *sptr
= buffer
+ i
;
423 wps
->sample_index
+= i
;
429 static void decorr_stereo_pass (struct decorr_pass
*dpp
, int32_t *buffer
, int32_t sample_count
)
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
;
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];
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];
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];
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];
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
];
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
];
483 m
= (m
+ 1) & (MAX_TERM
- 1);
484 k
= (k
+ 1) & (MAX_TERM
- 1);
488 int32_t temp_samples
[MAX_TERM
];
490 memcpy (temp_samples
, dpp
->samples_A
, sizeof (dpp
->samples_A
));
492 for (k
= 0; k
< MAX_TERM
; k
++, m
++)
493 dpp
->samples_A
[k
] = temp_samples
[m
& (MAX_TERM
- 1)];
495 memcpy (temp_samples
, dpp
->samples_B
, sizeof (dpp
->samples_B
));
497 for (k
= 0; k
< MAX_TERM
; k
++, m
++)
498 dpp
->samples_B
[k
] = temp_samples
[m
& (MAX_TERM
- 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]);
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];
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]);
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];
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
;
540 dpp
->weight_A
= weight_A
;
541 dpp
->weight_B
= weight_B
;
544 #if (!defined(CPU_COLDFIRE) && !defined(CPU_ARM))
546 static void decorr_stereo_pass_cont (struct decorr_pass
*dpp
, int32_t *buffer
, int32_t sample_count
)
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
;
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
);
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
);
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];
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
);
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
);
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];
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
);
593 bptr
[1] = apply_weight (weight_B
, tptr
[1]) + (sam_A
= bptr
[1]);
594 update_weight (weight_B
, delta
, tptr
[1], sam_A
);
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
;
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
);
612 dpp
->samples_A
[0] = bptr
[-1];
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
);
623 dpp
->samples_B
[0] = bptr
[-2];
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
);
634 dpp
->samples_A
[0] = bptr
[-1];
635 dpp
->samples_B
[0] = bptr
[-2];
639 dpp
->weight_A
= weight_A
;
640 dpp
->weight_B
= weight_B
;
645 static void decorr_mono_pass (struct decorr_pass
*dpp
, int32_t *buffer
, int32_t sample_count
)
647 int32_t delta
= dpp
->delta
, weight_A
= dpp
->weight_A
;
648 int32_t *bptr
, *eptr
= buffer
+ sample_count
, sam_A
;
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];
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];
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);
686 int32_t temp_samples
[MAX_TERM
];
688 memcpy (temp_samples
, dpp
->samples_A
, sizeof (dpp
->samples_A
));
690 for (k
= 0; k
< MAX_TERM
; k
++, m
++)
691 dpp
->samples_A
[k
] = temp_samples
[m
& (MAX_TERM
- 1)];
697 dpp
->weight_A
= weight_A
;
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).
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.
713 static void fixup_samples (WavpackStream
*wps
, int32_t *buffer
, uint32_t sample_count
)
715 uint32_t flags
= wps
->wphdr
.flags
;
716 int shift
= (flags
& SHIFT_MASK
) >> SHIFT_LSB
;
718 shift
+= 21 - (flags
& BYTES_STORED
) * 8; // this provides RockBox with 28-bit (+sign)
720 if (flags
& FLOAT_DATA
) {
721 float_values (wps
, buffer
, (flags
& MONO_DATA
) ? sample_count
: sample_count
* 2);
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
;
731 if (!(flags
& HYBRID_FLAG
) && !sent_bits
&& (zeros
+ ones
+ dups
))
736 *dptr
= ((*dptr
+ 1) << ones
) - 1;
738 *dptr
= ((*dptr
+ (*dptr
& 1)) << dups
) - (*dptr
& 1);
743 shift
+= zeros
+ sent_bits
+ ones
+ dups
;
747 if (!(flags
& MONO_DATA
))
750 while (sample_count
--)
753 else if (shift
< 0) {
756 if (!(flags
& MONO_DATA
))
759 while (sample_count
--)
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.
771 int check_crc_error (WavpackContext
*wpc
)
773 WavpackStream
*wps
= &wpc
->stream
;
776 if (wps
->crc
!= wps
->wphdr
.crc
)