summaryrefslogtreecommitdiff
path: root/apps/codecs/libwavpack/unpack.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/libwavpack/unpack.c')
-rw-r--r--apps/codecs/libwavpack/unpack.c869
1 files changed, 418 insertions, 451 deletions
diff --git a/apps/codecs/libwavpack/unpack.c b/apps/codecs/libwavpack/unpack.c
index 5afaac3659..aaab2aa928 100644
--- a/apps/codecs/libwavpack/unpack.c
+++ b/apps/codecs/libwavpack/unpack.c
@@ -1,8 +1,8 @@
1//////////////////////////////////////////////////////////////////////////// 1////////////////////////////////////////////////////////////////////////////
2// **** WAVPACK **** // 2// **** WAVPACK **** //
3// Hybrid Lossless Wavefile Compressor // 3// Hybrid Lossless Wavefile Compressor //
4// Copyright (c) 1998 - 2004 Conifer Software. // 4// Copyright (c) 1998 - 2004 Conifer Software. //
5// All Rights Reserved. // 5// All Rights Reserved. //
6// Distributed under the BSD Software License (see license.txt) // 6// Distributed under the BSD Software License (see license.txt) //
7//////////////////////////////////////////////////////////////////////////// 7////////////////////////////////////////////////////////////////////////////
8 8
@@ -15,46 +15,13 @@
15 15
16#include "wavpack.h" 16#include "wavpack.h"
17 17
18#include <stdlib.h>
18#include <string.h> 19#include <string.h>
19#include <math.h>
20 20
21static void strcpy_loc (char *dst, char *src) { while (*src) *dst++ = *src++; *dst = 0; } 21static void strcpy_loc (char *dst, char *src) { while ((*dst++ = *src++) != 0); }
22 22
23#define LOSSY_MUTE 23#define LOSSY_MUTE
24 24
25//////////////////////////////// local macros /////////////////////////////////
26
27// these macros implement the weight application and update operations
28// that are at the heart of the decorrelation loops
29
30#if 0 // PERFCOND
31#define apply_weight_i(weight, sample) ((weight * sample + 512) >> 10)
32#else
33#define apply_weight_i(weight, sample) ((((weight * sample) >> 8) + 2) >> 2)
34#endif
35
36#define apply_weight_f(weight, sample) (((((sample & 0xffff) * weight) >> 9) + \
37 (((sample & ~0xffff) >> 9) * weight) + 1) >> 1)
38
39#if 1 // PERFCOND
40#define apply_weight(weight, sample) (sample != (short) sample ? \
41 apply_weight_f (weight, sample) : apply_weight_i (weight, sample))
42#else
43#define apply_weight(weight, sample) ((int32_t)((weight * (int64_t) sample + 512) >> 10))
44#endif
45
46#if 0 // PERFCOND
47#define update_weight(weight, delta, source, result) \
48 if (source && result) weight -= ((((source ^ result) >> 30) & 2) - 1) * delta;
49#else
50#define update_weight(weight, delta, source, result) \
51 if (source && result) (source ^ result) < 0 ? (weight -= delta) : (weight += delta);
52#endif
53
54#define update_weight_clip(weight, delta, source, result) \
55 if (source && result && ((source ^ result) < 0 ? (weight -= delta) < -1024 : (weight += delta) > 1024)) \
56 weight = weight < 0 ? -1024 : 1024;
57
58///////////////////////////// executable code //////////////////////////////// 25///////////////////////////// executable code ////////////////////////////////
59 26
60// This function initializes everything required to unpack a WavPack block 27// This function initializes everything required to unpack a WavPack block
@@ -69,7 +36,7 @@ int unpack_init (WavpackContext *wpc)
69 WavpackMetadata wpmd; 36 WavpackMetadata wpmd;
70 37
71 if (wps->wphdr.block_samples && wps->wphdr.block_index != (ulong) -1) 38 if (wps->wphdr.block_samples && wps->wphdr.block_index != (ulong) -1)
72 wps->sample_index = wps->wphdr.block_index; 39 wps->sample_index = wps->wphdr.block_index;
73 40
74 wps->mute_error = FALSE; 41 wps->mute_error = FALSE;
75 wps->crc = 0xffffffff; 42 wps->crc = 0xffffffff;
@@ -78,27 +45,27 @@ int unpack_init (WavpackContext *wpc)
78 CLEAR (wps->w); 45 CLEAR (wps->w);
79 46
80 while (read_metadata_buff (wpc, &wpmd)) { 47 while (read_metadata_buff (wpc, &wpmd)) {
81 if (!process_metadata (wpc, &wpmd)) { 48 if (!process_metadata (wpc, &wpmd)) {
82 strcpy_loc (wpc->error_message, "invalid metadata!"); 49 strcpy_loc (wpc->error_message, "invalid metadata!");
83 return FALSE; 50 return FALSE;
84 } 51 }
85 52
86 if (wpmd.id == ID_WV_BITSTREAM) 53 if (wpmd.id == ID_WV_BITSTREAM)
87 break; 54 break;
88 } 55 }
89 56
90 if (wps->wphdr.block_samples && !bs_is_open (&wps->wvbits)) { 57 if (wps->wphdr.block_samples && !bs_is_open (&wps->wvbits)) {
91 strcpy_loc (wpc->error_message, "invalid WavPack file!"); 58 strcpy_loc (wpc->error_message, "invalid WavPack file!");
92 return FALSE; 59 return FALSE;
93 } 60 }
94 61
95 if (wps->wphdr.block_samples) { 62 if (wps->wphdr.block_samples) {
96 if ((wps->wphdr.flags & INT32_DATA) && wps->int32_sent_bits) 63 if ((wps->wphdr.flags & INT32_DATA) && wps->int32_sent_bits)
97 wpc->lossy_blocks = TRUE; 64 wpc->lossy_blocks = TRUE;
98 65
99 if ((wps->wphdr.flags & FLOAT_DATA) && 66 if ((wps->wphdr.flags & FLOAT_DATA) &&
100 wps->float_flags & (FLOAT_EXCEPTIONS | FLOAT_ZEROS_SENT | FLOAT_SHIFT_SENT | FLOAT_SHIFT_SAME)) 67 wps->float_flags & (FLOAT_EXCEPTIONS | FLOAT_ZEROS_SENT | FLOAT_SHIFT_SENT | FLOAT_SHIFT_SAME))
101 wpc->lossy_blocks = TRUE; 68 wpc->lossy_blocks = TRUE;
102 } 69 }
103 70
104 return TRUE; 71 return TRUE;
@@ -112,10 +79,10 @@ int init_wv_bitstream (WavpackContext *wpc, WavpackMetadata *wpmd)
112 WavpackStream *wps = &wpc->stream; 79 WavpackStream *wps = &wpc->stream;
113 80
114 if (wpmd->data) 81 if (wpmd->data)
115 bs_open_read (&wps->wvbits, wpmd->data, (char *) wpmd->data + wpmd->byte_length, NULL, 0); 82 bs_open_read (&wps->wvbits, wpmd->data, (char *) wpmd->data + wpmd->byte_length, NULL, 0);
116 else if (wpmd->byte_length) 83 else if (wpmd->byte_length)
117 bs_open_read (&wps->wvbits, wpc->read_buffer, wpc->read_buffer + sizeof (wpc->read_buffer), 84 bs_open_read (&wps->wvbits, wpc->read_buffer, wpc->read_buffer + sizeof (wpc->read_buffer),
118 wpc->infile, wpmd->byte_length + (wpmd->byte_length & 1)); 85 wpc->infile, wpmd->byte_length + (wpmd->byte_length & 1));
119 86
120 return TRUE; 87 return TRUE;
121} 88}
@@ -134,16 +101,16 @@ int read_decorr_terms (WavpackStream *wps, WavpackMetadata *wpmd)
134 struct decorr_pass *dpp; 101 struct decorr_pass *dpp;
135 102
136 if (termcnt > MAX_NTERMS) 103 if (termcnt > MAX_NTERMS)
137 return FALSE; 104 return FALSE;
138 105
139 wps->num_terms = termcnt; 106 wps->num_terms = termcnt;
140 107
141 for (dpp = wps->decorr_passes + termcnt - 1; termcnt--; dpp--) { 108 for (dpp = wps->decorr_passes + termcnt - 1; termcnt--; dpp--) {
142 dpp->term = (int)(*byteptr & 0x1f) - 5; 109 dpp->term = (int)(*byteptr & 0x1f) - 5;
143 dpp->delta = (*byteptr++ >> 5) & 0x7; 110 dpp->delta = (*byteptr++ >> 5) & 0x7;
144 111
145 if (!dpp->term || dpp->term < -3 || (dpp->term > MAX_TERM && dpp->term < 17) || dpp->term > 18) 112 if (!dpp->term || dpp->term < -3 || (dpp->term > MAX_TERM && dpp->term < 17) || dpp->term > 18)
146 return FALSE; 113 return FALSE;
147 } 114 }
148 115
149 return TRUE; 116 return TRUE;
@@ -162,19 +129,19 @@ int read_decorr_weights (WavpackStream *wps, WavpackMetadata *wpmd)
162 struct decorr_pass *dpp; 129 struct decorr_pass *dpp;
163 130
164 if (!(wps->wphdr.flags & MONO_FLAG)) 131 if (!(wps->wphdr.flags & MONO_FLAG))
165 termcnt /= 2; 132 termcnt /= 2;
166 133
167 if (termcnt > wps->num_terms) 134 if (termcnt > wps->num_terms)
168 return FALSE; 135 return FALSE;
169 136
170 for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++) 137 for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++)
171 dpp->weight_A = dpp->weight_B = 0; 138 dpp->weight_A = dpp->weight_B = 0;
172 139
173 while (--dpp >= wps->decorr_passes && termcnt--) { 140 while (--dpp >= wps->decorr_passes && termcnt--) {
174 dpp->weight_A = restore_weight (*byteptr++); 141 dpp->weight_A = restore_weight (*byteptr++);
175 142
176 if (!(wps->wphdr.flags & MONO_FLAG)) 143 if (!(wps->wphdr.flags & MONO_FLAG))
177 dpp->weight_B = restore_weight (*byteptr++); 144 dpp->weight_B = restore_weight (*byteptr++);
178 } 145 }
179 146
180 return TRUE; 147 return TRUE;
@@ -196,49 +163,49 @@ int read_decorr_samples (WavpackStream *wps, WavpackMetadata *wpmd)
196 int tcount; 163 int tcount;
197 164
198 for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++) { 165 for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++) {
199 CLEAR (dpp->samples_A); 166 CLEAR (dpp->samples_A);
200 CLEAR (dpp->samples_B); 167 CLEAR (dpp->samples_B);
201 } 168 }
202 169
203 if (wps->wphdr.version == 0x402 && (wps->wphdr.flags & HYBRID_FLAG)) { 170 if (wps->wphdr.version == 0x402 && (wps->wphdr.flags & HYBRID_FLAG)) {
204 byteptr += 2; 171 byteptr += 2;
205 172
206 if (!(wps->wphdr.flags & MONO_FLAG)) 173 if (!(wps->wphdr.flags & MONO_FLAG))
207 byteptr += 2; 174 byteptr += 2;
208 } 175 }
209 176
210 while (dpp-- > wps->decorr_passes && byteptr < endptr) 177 while (dpp-- > wps->decorr_passes && byteptr < endptr)
211 if (dpp->term > MAX_TERM) { 178 if (dpp->term > MAX_TERM) {
212 dpp->samples_A [0] = exp2s ((short)(byteptr [0] + (byteptr [1] << 8))); 179 dpp->samples_A [0] = exp2s ((short)(byteptr [0] + (byteptr [1] << 8)));
213 dpp->samples_A [1] = exp2s ((short)(byteptr [2] + (byteptr [3] << 8))); 180 dpp->samples_A [1] = exp2s ((short)(byteptr [2] + (byteptr [3] << 8)));
214 byteptr += 4; 181 byteptr += 4;
215 182
216 if (!(wps->wphdr.flags & MONO_FLAG)) { 183 if (!(wps->wphdr.flags & MONO_FLAG)) {
217 dpp->samples_B [0] = exp2s ((short)(byteptr [0] + (byteptr [1] << 8))); 184 dpp->samples_B [0] = exp2s ((short)(byteptr [0] + (byteptr [1] << 8)));
218 dpp->samples_B [1] = exp2s ((short)(byteptr [2] + (byteptr [3] << 8))); 185 dpp->samples_B [1] = exp2s ((short)(byteptr [2] + (byteptr [3] << 8)));
219 byteptr += 4; 186 byteptr += 4;
220 } 187 }
221 } 188 }
222 else if (dpp->term < 0) { 189 else if (dpp->term < 0) {
223 dpp->samples_A [0] = exp2s ((short)(byteptr [0] + (byteptr [1] << 8))); 190 dpp->samples_A [0] = exp2s ((short)(byteptr [0] + (byteptr [1] << 8)));
224 dpp->samples_B [0] = exp2s ((short)(byteptr [2] + (byteptr [3] << 8))); 191 dpp->samples_B [0] = exp2s ((short)(byteptr [2] + (byteptr [3] << 8)));
225 byteptr += 4; 192 byteptr += 4;
226 } 193 }
227 else { 194 else {
228 int m = 0, cnt = dpp->term; 195 int m = 0, cnt = dpp->term;
229 196
230 while (cnt--) { 197 while (cnt--) {
231 dpp->samples_A [m] = exp2s ((short)(byteptr [0] + (byteptr [1] << 8))); 198 dpp->samples_A [m] = exp2s ((short)(byteptr [0] + (byteptr [1] << 8)));
232 byteptr += 2; 199 byteptr += 2;
233 200
234 if (!(wps->wphdr.flags & MONO_FLAG)) { 201 if (!(wps->wphdr.flags & MONO_FLAG)) {
235 dpp->samples_B [m] = exp2s ((short)(byteptr [0] + (byteptr [1] << 8))); 202 dpp->samples_B [m] = exp2s ((short)(byteptr [0] + (byteptr [1] << 8)));
236 byteptr += 2; 203 byteptr += 2;
237 } 204 }
238 205
239 m++; 206 m++;
240 } 207 }
241 } 208 }
242 209
243 return byteptr == endptr; 210 return byteptr == endptr;
244} 211}
@@ -253,7 +220,7 @@ int read_int32_info (WavpackStream *wps, WavpackMetadata *wpmd)
253 char *byteptr = wpmd->data; 220 char *byteptr = wpmd->data;
254 221
255 if (bytecnt != 4) 222 if (bytecnt != 4)
256 return FALSE; 223 return FALSE;
257 224
258 wps->int32_sent_bits = *byteptr++; 225 wps->int32_sent_bits = *byteptr++;
259 wps->int32_zeros = *byteptr++; 226 wps->int32_zeros = *byteptr++;
@@ -273,13 +240,13 @@ int read_channel_info (WavpackContext *wpc, WavpackMetadata *wpmd)
273 ulong mask = 0; 240 ulong mask = 0;
274 241
275 if (!bytecnt || bytecnt > 5) 242 if (!bytecnt || bytecnt > 5)
276 return FALSE; 243 return FALSE;
277 244
278 wpc->config.num_channels = *byteptr++; 245 wpc->config.num_channels = *byteptr++;
279 246
280 while (--bytecnt) { 247 while (--bytecnt) {
281 mask |= (ulong) *byteptr++ << shift; 248 mask |= (ulong) *byteptr++ << shift;
282 shift += 8; 249 shift += 8;
283 } 250 }
284 251
285 wpc->config.channel_mask = mask; 252 wpc->config.channel_mask = mask;
@@ -294,10 +261,10 @@ int read_config_info (WavpackContext *wpc, WavpackMetadata *wpmd)
294 uchar *byteptr = wpmd->data; 261 uchar *byteptr = wpmd->data;
295 262
296 if (bytecnt >= 3) { 263 if (bytecnt >= 3) {
297 wpc->config.flags &= 0xff; 264 wpc->config.flags &= 0xff;
298 wpc->config.flags |= (long) *byteptr++ << 8; 265 wpc->config.flags |= (long) *byteptr++ << 8;
299 wpc->config.flags |= (long) *byteptr++ << 16; 266 wpc->config.flags |= (long) *byteptr++ << 16;
300 wpc->config.flags |= (long) *byteptr << 24; 267 wpc->config.flags |= (long) *byteptr << 24;
301 } 268 }
302 269
303 return TRUE; 270 return TRUE;
@@ -339,88 +306,88 @@ long unpack_samples (WavpackContext *wpc, long *buffer, ulong sample_count)
339 int tcount; 306 int tcount;
340 307
341 if (wps->sample_index + sample_count > wps->wphdr.block_index + wps->wphdr.block_samples) 308 if (wps->sample_index + sample_count > wps->wphdr.block_index + wps->wphdr.block_samples)
342 sample_count = wps->wphdr.block_index + wps->wphdr.block_samples - wps->sample_index; 309 sample_count = wps->wphdr.block_index + wps->wphdr.block_samples - wps->sample_index;
343 310
344 if (wps->mute_error) { 311 if (wps->mute_error) {
345 memset (buffer, 0, sample_count * (flags & MONO_FLAG ? 4 : 8)); 312 memset (buffer, 0, sample_count * (flags & MONO_FLAG ? 4 : 8));
346 wps->sample_index += sample_count; 313 wps->sample_index += sample_count;
347 return sample_count; 314 return sample_count;
348 } 315 }
349 316
350 if (flags & HYBRID_FLAG) 317 if (flags & HYBRID_FLAG)
351 mute_limit *= 2; 318 mute_limit *= 2;
352 319
353 ///////////////////// handle version 4 mono data ///////////////////////// 320 ///////////////////// handle version 4 mono data /////////////////////////
354 321
355 if (flags & MONO_FLAG) { 322 if (flags & MONO_FLAG) {
356 eptr = buffer + sample_count; 323 eptr = buffer + sample_count;
357 i = get_words (wps, 1, sample_count, buffer); 324 i = get_words (buffer, sample_count, flags, &wps->w, &wps->wvbits);
358 325
359 for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++) 326 for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++)
360 decorr_mono_pass (dpp, buffer, sample_count); 327 decorr_mono_pass (dpp, buffer, sample_count);
361 328
362 for (bptr = buffer; bptr < eptr; ++bptr) { 329 for (bptr = buffer; bptr < eptr; ++bptr) {
363 if (labs (bptr [0]) > mute_limit) { 330 if (labs (bptr [0]) > mute_limit) {
364 i = bptr - buffer; 331 i = bptr - buffer;
365 break; 332 break;
366 } 333 }
367 334
368 crc = crc * 3 + bptr [0]; 335 crc = crc * 3 + bptr [0];
369 } 336 }
370 } 337 }
371 338
372 //////////////////// handle version 4 stereo data //////////////////////// 339 //////////////////// handle version 4 stereo data ////////////////////////
373 340
374 else { 341 else {
375 eptr = buffer + (sample_count * 2); 342 eptr = buffer + (sample_count * 2);
376 i = get_words (wps, 2, sample_count, buffer); 343 i = get_words (buffer, sample_count, flags, &wps->w, &wps->wvbits);
377 344
378 if (sample_count < 16) 345 if (sample_count < 16)
379 for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++) 346 for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++)
380 decorr_stereo_pass (dpp, buffer, sample_count); 347 decorr_stereo_pass (dpp, buffer, sample_count);
381 else 348 else
382 for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++) { 349 for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++) {
383 decorr_stereo_pass (dpp, buffer, 8); 350 decorr_stereo_pass (dpp, buffer, 8);
384#if CONFIG_CPU==MCF5249 && !defined(SIMULATOR) 351#if CONFIG_CPU==MCF5249 && !defined(SIMULATOR)
385 decorr_stereo_pass_cont_mcf5249 (dpp, buffer + 16, sample_count - 8); 352 decorr_stereo_pass_cont_mcf5249 (dpp, buffer + 16, sample_count - 8);
386#else 353#else
387 decorr_stereo_pass_cont (dpp, buffer + 16, sample_count - 8); 354 decorr_stereo_pass_cont (dpp, buffer + 16, sample_count - 8);
388#endif 355#endif
389 } 356 }
390 357
391 if (flags & JOINT_STEREO) 358 if (flags & JOINT_STEREO)
392 for (bptr = buffer; bptr < eptr; bptr += 2) { 359 for (bptr = buffer; bptr < eptr; bptr += 2) {
393 bptr [0] += (bptr [1] -= (bptr [0] >> 1)); 360 bptr [0] += (bptr [1] -= (bptr [0] >> 1));
394 361
395 if (labs (bptr [0]) > mute_limit || labs (bptr [1]) > mute_limit) { 362 if (labs (bptr [0]) > mute_limit || labs (bptr [1]) > mute_limit) {
396 i = (bptr - buffer) / 2; 363 i = (bptr - buffer) / 2;
397 break; 364 break;
398 } 365 }
399 366
400 crc = (crc * 3 + bptr [0]) * 3 + bptr [1]; 367 crc = (crc * 3 + bptr [0]) * 3 + bptr [1];
401 } 368 }
402 else 369 else
403 for (bptr = buffer; bptr < eptr; bptr += 2) { 370 for (bptr = buffer; bptr < eptr; bptr += 2) {
404 if (labs (bptr [0]) > mute_limit || labs (bptr [1]) > mute_limit) { 371 if (labs (bptr [0]) > mute_limit || labs (bptr [1]) > mute_limit) {
405 i = (bptr - buffer) / 2; 372 i = (bptr - buffer) / 2;
406 break; 373 break;
407 } 374 }
408 375
409 crc = (crc * 3 + bptr [0]) * 3 + bptr [1]; 376 crc = (crc * 3 + bptr [0]) * 3 + bptr [1];
410 } 377 }
411 } 378 }
412 379
413 if (i != sample_count) { 380 if (i != sample_count) {
414 memset (buffer, 0, sample_count * (flags & MONO_FLAG ? 4 : 8)); 381 memset (buffer, 0, sample_count * (flags & MONO_FLAG ? 4 : 8));
415 wps->mute_error = TRUE; 382 wps->mute_error = TRUE;
416 i = sample_count; 383 i = sample_count;
417 } 384 }
418 385
419 fixup_samples (wps, buffer, i); 386 fixup_samples (wps, buffer, i);
420 387
421 if (flags & FLOAT_DATA) 388 if (flags & FLOAT_DATA)
422 float_normalize (buffer, (flags & MONO_FLAG) ? i : i * 2, 389 float_normalize (buffer, (flags & MONO_FLAG) ? i : i * 2,
423 127 - wps->float_norm_exp + wpc->norm_offset); 390 127 - wps->float_norm_exp + wpc->norm_offset);
424 391
425 wps->sample_index += i; 392 wps->sample_index += i;
426 wps->crc = crc; 393 wps->crc = crc;
@@ -436,107 +403,107 @@ static void decorr_stereo_pass (struct decorr_pass *dpp, long *buffer, long samp
436 403
437 switch (dpp->term) { 404 switch (dpp->term) {
438 405
439 case 17: 406 case 17:
440 for (bptr = buffer; bptr < eptr; bptr += 2) { 407 for (bptr = buffer; bptr < eptr; bptr += 2) {
441 sam_A = 2 * dpp->samples_A [0] - dpp->samples_A [1]; 408 sam_A = 2 * dpp->samples_A [0] - dpp->samples_A [1];
442 dpp->samples_A [1] = dpp->samples_A [0]; 409 dpp->samples_A [1] = dpp->samples_A [0];
443 dpp->samples_A [0] = apply_weight (weight_A, sam_A) + bptr [0]; 410 dpp->samples_A [0] = apply_weight (weight_A, sam_A) + bptr [0];
444 update_weight (weight_A, delta, sam_A, bptr [0]); 411 update_weight (weight_A, delta, sam_A, bptr [0]);
445 bptr [0] = dpp->samples_A [0]; 412 bptr [0] = dpp->samples_A [0];
446 413
447 sam_A = 2 * dpp->samples_B [0] - dpp->samples_B [1]; 414 sam_A = 2 * dpp->samples_B [0] - dpp->samples_B [1];
448 dpp->samples_B [1] = dpp->samples_B [0]; 415 dpp->samples_B [1] = dpp->samples_B [0];
449 dpp->samples_B [0] = apply_weight (weight_B, sam_A) + bptr [1]; 416 dpp->samples_B [0] = apply_weight (weight_B, sam_A) + bptr [1];
450 update_weight (weight_B, delta, sam_A, bptr [1]); 417 update_weight (weight_B, delta, sam_A, bptr [1]);
451 bptr [1] = dpp->samples_B [0]; 418 bptr [1] = dpp->samples_B [0];
452 } 419 }
453 420
454 break; 421 break;
455 422
456 case 18: 423 case 18:
457 for (bptr = buffer; bptr < eptr; bptr += 2) { 424 for (bptr = buffer; bptr < eptr; bptr += 2) {
458 sam_A = (3 * dpp->samples_A [0] - dpp->samples_A [1]) >> 1; 425 sam_A = (3 * dpp->samples_A [0] - dpp->samples_A [1]) >> 1;
459 dpp->samples_A [1] = dpp->samples_A [0]; 426 dpp->samples_A [1] = dpp->samples_A [0];
460 dpp->samples_A [0] = apply_weight (weight_A, sam_A) + bptr [0]; 427 dpp->samples_A [0] = apply_weight (weight_A, sam_A) + bptr [0];
461 update_weight (weight_A, delta, sam_A, bptr [0]); 428 update_weight (weight_A, delta, sam_A, bptr [0]);
462 bptr [0] = dpp->samples_A [0]; 429 bptr [0] = dpp->samples_A [0];
463 430
464 sam_A = (3 * dpp->samples_B [0] - dpp->samples_B [1]) >> 1; 431 sam_A = (3 * dpp->samples_B [0] - dpp->samples_B [1]) >> 1;
465 dpp->samples_B [1] = dpp->samples_B [0]; 432 dpp->samples_B [1] = dpp->samples_B [0];
466 dpp->samples_B [0] = apply_weight (weight_B, sam_A) + bptr [1]; 433 dpp->samples_B [0] = apply_weight (weight_B, sam_A) + bptr [1];
467 update_weight (weight_B, delta, sam_A, bptr [1]); 434 update_weight (weight_B, delta, sam_A, bptr [1]);
468 bptr [1] = dpp->samples_B [0]; 435 bptr [1] = dpp->samples_B [0];
469 } 436 }
470 437
471 break; 438 break;
472 439
473 default: 440 default:
474 for (m = 0, k = dpp->term & (MAX_TERM - 1), bptr = buffer; bptr < eptr; bptr += 2) { 441 for (m = 0, k = dpp->term & (MAX_TERM - 1), bptr = buffer; bptr < eptr; bptr += 2) {
475 sam_A = dpp->samples_A [m]; 442 sam_A = dpp->samples_A [m];
476 dpp->samples_A [k] = apply_weight (weight_A, sam_A) + bptr [0]; 443 dpp->samples_A [k] = apply_weight (weight_A, sam_A) + bptr [0];
477 update_weight (weight_A, delta, sam_A, bptr [0]); 444 update_weight (weight_A, delta, sam_A, bptr [0]);
478 bptr [0] = dpp->samples_A [k]; 445 bptr [0] = dpp->samples_A [k];
479 446
480 sam_A = dpp->samples_B [m]; 447 sam_A = dpp->samples_B [m];
481 dpp->samples_B [k] = apply_weight (weight_B, sam_A) + bptr [1]; 448 dpp->samples_B [k] = apply_weight (weight_B, sam_A) + bptr [1];
482 update_weight (weight_B, delta, sam_A, bptr [1]); 449 update_weight (weight_B, delta, sam_A, bptr [1]);
483 bptr [1] = dpp->samples_B [k]; 450 bptr [1] = dpp->samples_B [k];
484 451
485 m = (m + 1) & (MAX_TERM - 1); 452 m = (m + 1) & (MAX_TERM - 1);
486 k = (k + 1) & (MAX_TERM - 1); 453 k = (k + 1) & (MAX_TERM - 1);
487 } 454 }
488 455
489 if (m) { 456 if (m) {
490 long temp_samples [MAX_TERM]; 457 long temp_samples [MAX_TERM];
491 458
492 memcpy (temp_samples, dpp->samples_A, sizeof (dpp->samples_A)); 459 memcpy (temp_samples, dpp->samples_A, sizeof (dpp->samples_A));
493 460
494 for (k = 0; k < MAX_TERM; k++, m++) 461 for (k = 0; k < MAX_TERM; k++, m++)
495 dpp->samples_A [k] = temp_samples [m & (MAX_TERM - 1)]; 462 dpp->samples_A [k] = temp_samples [m & (MAX_TERM - 1)];
496 463
497 memcpy (temp_samples, dpp->samples_B, sizeof (dpp->samples_B)); 464 memcpy (temp_samples, dpp->samples_B, sizeof (dpp->samples_B));
498 465
499 for (k = 0; k < MAX_TERM; k++, m++) 466 for (k = 0; k < MAX_TERM; k++, m++)
500 dpp->samples_B [k] = temp_samples [m & (MAX_TERM - 1)]; 467 dpp->samples_B [k] = temp_samples [m & (MAX_TERM - 1)];
501 } 468 }
502 469
503 break; 470 break;
504 471
505 case -1: 472 case -1:
506 for (bptr = buffer; bptr < eptr; bptr += 2) { 473 for (bptr = buffer; bptr < eptr; bptr += 2) {
507 sam_A = bptr [0] + apply_weight (weight_A, dpp->samples_A [0]); 474 sam_A = bptr [0] + apply_weight (weight_A, dpp->samples_A [0]);
508 update_weight_clip (weight_A, delta, dpp->samples_A [0], bptr [0]); 475 update_weight_clip (weight_A, delta, dpp->samples_A [0], bptr [0]);
509 bptr [0] = sam_A; 476 bptr [0] = sam_A;
510 dpp->samples_A [0] = bptr [1] + apply_weight (weight_B, sam_A); 477 dpp->samples_A [0] = bptr [1] + apply_weight (weight_B, sam_A);
511 update_weight_clip (weight_B, delta, sam_A, bptr [1]); 478 update_weight_clip (weight_B, delta, sam_A, bptr [1]);
512 bptr [1] = dpp->samples_A [0]; 479 bptr [1] = dpp->samples_A [0];
513 } 480 }
514 481
515 break; 482 break;
516 483
517 case -2: 484 case -2:
518 for (bptr = buffer; bptr < eptr; bptr += 2) { 485 for (bptr = buffer; bptr < eptr; bptr += 2) {
519 sam_B = bptr [1] + apply_weight (weight_B, dpp->samples_B [0]); 486 sam_B = bptr [1] + apply_weight (weight_B, dpp->samples_B [0]);
520 update_weight_clip (weight_B, delta, dpp->samples_B [0], bptr [1]); 487 update_weight_clip (weight_B, delta, dpp->samples_B [0], bptr [1]);
521 bptr [1] = sam_B; 488 bptr [1] = sam_B;
522 dpp->samples_B [0] = bptr [0] + apply_weight (weight_A, sam_B); 489 dpp->samples_B [0] = bptr [0] + apply_weight (weight_A, sam_B);
523 update_weight_clip (weight_A, delta, sam_B, bptr [0]); 490 update_weight_clip (weight_A, delta, sam_B, bptr [0]);
524 bptr [0] = dpp->samples_B [0]; 491 bptr [0] = dpp->samples_B [0];
525 } 492 }
526 493
527 break; 494 break;
528 495
529 case -3: 496 case -3:
530 for (bptr = buffer; bptr < eptr; bptr += 2) { 497 for (bptr = buffer; bptr < eptr; bptr += 2) {
531 sam_A = bptr [0] + apply_weight (weight_A, dpp->samples_A [0]); 498 sam_A = bptr [0] + apply_weight (weight_A, dpp->samples_A [0]);
532 update_weight_clip (weight_A, delta, dpp->samples_A [0], bptr [0]); 499 update_weight_clip (weight_A, delta, dpp->samples_A [0], bptr [0]);
533 sam_B = bptr [1] + apply_weight (weight_B, dpp->samples_B [0]); 500 sam_B = bptr [1] + apply_weight (weight_B, dpp->samples_B [0]);
534 update_weight_clip (weight_B, delta, dpp->samples_B [0], bptr [1]); 501 update_weight_clip (weight_B, delta, dpp->samples_B [0], bptr [1]);
535 bptr [0] = dpp->samples_B [0] = sam_A; 502 bptr [0] = dpp->samples_B [0] = sam_A;
536 bptr [1] = dpp->samples_A [0] = sam_B; 503 bptr [1] = dpp->samples_A [0] = sam_B;
537 } 504 }
538 505
539 break; 506 break;
540 } 507 }
541 508
542 dpp->weight_A = weight_A; 509 dpp->weight_A = weight_A;
@@ -553,89 +520,89 @@ static void decorr_stereo_pass_cont (struct decorr_pass *dpp, long *buffer, long
553 520
554 switch (dpp->term) { 521 switch (dpp->term) {
555 522
556 case 17: 523 case 17:
557 for (bptr = buffer; bptr < eptr; bptr += 2) { 524 for (bptr = buffer; bptr < eptr; bptr += 2) {
558 sam_A = 2 * bptr [-2] - bptr [-4]; 525 sam_A = 2 * bptr [-2] - bptr [-4];
559 bptr [0] = apply_weight (weight_A, sam_A) + (sam_B = bptr [0]); 526 bptr [0] = apply_weight (weight_A, sam_A) + (sam_B = bptr [0]);
560 update_weight (weight_A, delta, sam_A, sam_B); 527 update_weight (weight_A, delta, sam_A, sam_B);
561 528
562 sam_A = 2 * bptr [-1] - bptr [-3]; 529 sam_A = 2 * bptr [-1] - bptr [-3];
563 bptr [1] = apply_weight (weight_B, sam_A) + (sam_B = bptr [1]); 530 bptr [1] = apply_weight (weight_B, sam_A) + (sam_B = bptr [1]);
564 update_weight (weight_B, delta, sam_A, sam_B); 531 update_weight (weight_B, delta, sam_A, sam_B);
565 } 532 }
566 533
567 dpp->samples_B [0] = bptr [-1]; 534 dpp->samples_B [0] = bptr [-1];
568 dpp->samples_A [0] = bptr [-2]; 535 dpp->samples_A [0] = bptr [-2];
569 dpp->samples_B [1] = bptr [-3]; 536 dpp->samples_B [1] = bptr [-3];
570 dpp->samples_A [1] = bptr [-4]; 537 dpp->samples_A [1] = bptr [-4];
571 break; 538 break;
572 539
573 case 18: 540 case 18:
574 for (bptr = buffer; bptr < eptr; bptr += 2) { 541 for (bptr = buffer; bptr < eptr; bptr += 2) {
575 sam_A = (3 * bptr [-2] - bptr [-4]) >> 1; 542 sam_A = (3 * bptr [-2] - bptr [-4]) >> 1;
576 bptr [0] = apply_weight (weight_A, sam_A) + (sam_B = bptr [0]); 543 bptr [0] = apply_weight (weight_A, sam_A) + (sam_B = bptr [0]);
577 update_weight (weight_A, delta, sam_A, sam_B); 544 update_weight (weight_A, delta, sam_A, sam_B);
578 545
579 sam_A = (3 * bptr [-1] - bptr [-3]) >> 1; 546 sam_A = (3 * bptr [-1] - bptr [-3]) >> 1;
580 bptr [1] = apply_weight (weight_B, sam_A) + (sam_B = bptr [1]); 547 bptr [1] = apply_weight (weight_B, sam_A) + (sam_B = bptr [1]);
581 update_weight (weight_B, delta, sam_A, sam_B); 548 update_weight (weight_B, delta, sam_A, sam_B);
582 } 549 }
583 550
584 dpp->samples_B [0] = bptr [-1]; 551 dpp->samples_B [0] = bptr [-1];
585 dpp->samples_A [0] = bptr [-2]; 552 dpp->samples_A [0] = bptr [-2];
586 dpp->samples_B [1] = bptr [-3]; 553 dpp->samples_B [1] = bptr [-3];
587 dpp->samples_A [1] = bptr [-4]; 554 dpp->samples_A [1] = bptr [-4];
588 break; 555 break;
589 556
590 default: 557 default:
591 for (bptr = buffer, tptr = buffer - (dpp->term * 2); bptr < eptr; bptr += 2, tptr += 2) { 558 for (bptr = buffer, tptr = buffer - (dpp->term * 2); bptr < eptr; bptr += 2, tptr += 2) {
592 bptr [0] = apply_weight (weight_A, tptr [0]) + (sam_A = bptr [0]); 559 bptr [0] = apply_weight (weight_A, tptr [0]) + (sam_A = bptr [0]);
593 update_weight (weight_A, delta, tptr [0], sam_A); 560 update_weight (weight_A, delta, tptr [0], sam_A);
594 561
595 bptr [1] = apply_weight (weight_B, tptr [1]) + (sam_A = bptr [1]); 562 bptr [1] = apply_weight (weight_B, tptr [1]) + (sam_A = bptr [1]);
596 update_weight (weight_B, delta, tptr [1], sam_A); 563 update_weight (weight_B, delta, tptr [1], sam_A);
597 } 564 }
598 565
599 for (k = dpp->term - 1, i = 8; i--; k--) { 566 for (k = dpp->term - 1, i = 8; i--; k--) {
600 dpp->samples_B [k & (MAX_TERM - 1)] = *--bptr; 567 dpp->samples_B [k & (MAX_TERM - 1)] = *--bptr;
601 dpp->samples_A [k & (MAX_TERM - 1)] = *--bptr; 568 dpp->samples_A [k & (MAX_TERM - 1)] = *--bptr;
602 } 569 }
603 570
604 break; 571 break;
605 572
606 case -1: 573 case -1:
607 for (bptr = buffer; bptr < eptr; bptr += 2) { 574 for (bptr = buffer; bptr < eptr; bptr += 2) {
608 bptr [0] = apply_weight (weight_A, bptr [-1]) + (sam_A = bptr [0]); 575 bptr [0] = apply_weight (weight_A, bptr [-1]) + (sam_A = bptr [0]);
609 update_weight_clip (weight_A, delta, bptr [-1], sam_A); 576 update_weight_clip (weight_A, delta, bptr [-1], sam_A);
610 bptr [1] = apply_weight (weight_B, bptr [0]) + (sam_A = bptr [1]); 577 bptr [1] = apply_weight (weight_B, bptr [0]) + (sam_A = bptr [1]);
611 update_weight_clip (weight_B, delta, bptr [0], sam_A); 578 update_weight_clip (weight_B, delta, bptr [0], sam_A);
612 } 579 }
613 580
614 dpp->samples_A [0] = bptr [-1]; 581 dpp->samples_A [0] = bptr [-1];
615 break; 582 break;
616 583
617 case -2: 584 case -2:
618 for (bptr = buffer; bptr < eptr; bptr += 2) { 585 for (bptr = buffer; bptr < eptr; bptr += 2) {
619 bptr [1] = apply_weight (weight_B, bptr [-2]) + (sam_A = bptr [1]); 586 bptr [1] = apply_weight (weight_B, bptr [-2]) + (sam_A = bptr [1]);
620 update_weight_clip (weight_B, delta, bptr [-2], sam_A); 587 update_weight_clip (weight_B, delta, bptr [-2], sam_A);
621 bptr [0] = apply_weight (weight_A, bptr [1]) + (sam_A = bptr [0]); 588 bptr [0] = apply_weight (weight_A, bptr [1]) + (sam_A = bptr [0]);
622 update_weight_clip (weight_A, delta, bptr [1], sam_A); 589 update_weight_clip (weight_A, delta, bptr [1], sam_A);
623 } 590 }
624 591
625 dpp->samples_B [0] = bptr [-2]; 592 dpp->samples_B [0] = bptr [-2];
626 break; 593 break;
627 594
628 case -3: 595 case -3:
629 for (bptr = buffer; bptr < eptr; bptr += 2) { 596 for (bptr = buffer; bptr < eptr; bptr += 2) {
630 bptr [0] = apply_weight (weight_A, bptr [-1]) + (sam_A = bptr [0]); 597 bptr [0] = apply_weight (weight_A, bptr [-1]) + (sam_A = bptr [0]);
631 update_weight_clip (weight_A, delta, bptr [-1], sam_A); 598 update_weight_clip (weight_A, delta, bptr [-1], sam_A);
632 bptr [1] = apply_weight (weight_B, bptr [-2]) + (sam_A = bptr [1]); 599 bptr [1] = apply_weight (weight_B, bptr [-2]) + (sam_A = bptr [1]);
633 update_weight_clip (weight_B, delta, bptr [-2], sam_A); 600 update_weight_clip (weight_B, delta, bptr [-2], sam_A);
634 } 601 }
635 602
636 dpp->samples_A [0] = bptr [-1]; 603 dpp->samples_A [0] = bptr [-1];
637 dpp->samples_B [0] = bptr [-2]; 604 dpp->samples_B [0] = bptr [-2];
638 break; 605 break;
639 } 606 }
640 607
641 dpp->weight_A = weight_A; 608 dpp->weight_A = weight_A;
@@ -652,48 +619,48 @@ static void decorr_mono_pass (struct decorr_pass *dpp, long *buffer, long sample
652 619
653 switch (dpp->term) { 620 switch (dpp->term) {
654 621
655 case 17: 622 case 17:
656 for (bptr = buffer; bptr < eptr; bptr++) { 623 for (bptr = buffer; bptr < eptr; bptr++) {
657 sam_A = 2 * dpp->samples_A [0] - dpp->samples_A [1]; 624 sam_A = 2 * dpp->samples_A [0] - dpp->samples_A [1];
658 dpp->samples_A [1] = dpp->samples_A [0]; 625 dpp->samples_A [1] = dpp->samples_A [0];
659 dpp->samples_A [0] = apply_weight (weight_A, sam_A) + bptr [0]; 626 dpp->samples_A [0] = apply_weight (weight_A, sam_A) + bptr [0];
660 update_weight (weight_A, delta, sam_A, bptr [0]); 627 update_weight (weight_A, delta, sam_A, bptr [0]);
661 bptr [0] = dpp->samples_A [0]; 628 bptr [0] = dpp->samples_A [0];
662 } 629 }
663 630
664 break; 631 break;
665 632
666 case 18: 633 case 18:
667 for (bptr = buffer; bptr < eptr; bptr++) { 634 for (bptr = buffer; bptr < eptr; bptr++) {
668 sam_A = (3 * dpp->samples_A [0] - dpp->samples_A [1]) >> 1; 635 sam_A = (3 * dpp->samples_A [0] - dpp->samples_A [1]) >> 1;
669 dpp->samples_A [1] = dpp->samples_A [0]; 636 dpp->samples_A [1] = dpp->samples_A [0];
670 dpp->samples_A [0] = apply_weight (weight_A, sam_A) + bptr [0]; 637 dpp->samples_A [0] = apply_weight (weight_A, sam_A) + bptr [0];
671 update_weight (weight_A, delta, sam_A, bptr [0]); 638 update_weight (weight_A, delta, sam_A, bptr [0]);
672 bptr [0] = dpp->samples_A [0]; 639 bptr [0] = dpp->samples_A [0];
673 } 640 }
674 641
675 break; 642 break;
676 643
677 default: 644 default:
678 for (m = 0, k = dpp->term & (MAX_TERM - 1), bptr = buffer; bptr < eptr; bptr++) { 645 for (m = 0, k = dpp->term & (MAX_TERM - 1), bptr = buffer; bptr < eptr; bptr++) {
679 sam_A = dpp->samples_A [m]; 646 sam_A = dpp->samples_A [m];
680 dpp->samples_A [k] = apply_weight (weight_A, sam_A) + bptr [0]; 647 dpp->samples_A [k] = apply_weight (weight_A, sam_A) + bptr [0];
681 update_weight (weight_A, delta, sam_A, bptr [0]); 648 update_weight (weight_A, delta, sam_A, bptr [0]);
682 bptr [0] = dpp->samples_A [k]; 649 bptr [0] = dpp->samples_A [k];
683 m = (m + 1) & (MAX_TERM - 1); 650 m = (m + 1) & (MAX_TERM - 1);
684 k = (k + 1) & (MAX_TERM - 1); 651 k = (k + 1) & (MAX_TERM - 1);
685 } 652 }
686 653
687 if (m) { 654 if (m) {
688 long temp_samples [MAX_TERM]; 655 long temp_samples [MAX_TERM];
689 656
690 memcpy (temp_samples, dpp->samples_A, sizeof (dpp->samples_A)); 657 memcpy (temp_samples, dpp->samples_A, sizeof (dpp->samples_A));
691 658
692 for (k = 0; k < MAX_TERM; k++, m++) 659 for (k = 0; k < MAX_TERM; k++, m++)
693 dpp->samples_A [k] = temp_samples [m & (MAX_TERM - 1)]; 660 dpp->samples_A [k] = temp_samples [m & (MAX_TERM - 1)];
694 } 661 }
695 662
696 break; 663 break;
697 } 664 }
698 665
699 dpp->weight_A = weight_A; 666 dpp->weight_A = weight_A;
@@ -714,76 +681,76 @@ static void fixup_samples (WavpackStream *wps, long *buffer, ulong sample_count)
714 int shift = (flags & SHIFT_MASK) >> SHIFT_LSB; 681 int shift = (flags & SHIFT_MASK) >> SHIFT_LSB;
715 682
716 if (flags & FLOAT_DATA) { 683 if (flags & FLOAT_DATA) {
717 float_values (wps, buffer, (flags & MONO_FLAG) ? sample_count : sample_count * 2); 684 float_values (wps, buffer, (flags & MONO_FLAG) ? sample_count : sample_count * 2);
718 return; 685 return;
719 } 686 }
720 687
721 if (flags & INT32_DATA) { 688 if (flags & INT32_DATA) {
722 ulong count = (flags & MONO_FLAG) ? sample_count : sample_count * 2; 689 ulong count = (flags & MONO_FLAG) ? sample_count : sample_count * 2;
723 int sent_bits = wps->int32_sent_bits, zeros = wps->int32_zeros; 690 int sent_bits = wps->int32_sent_bits, zeros = wps->int32_zeros;
724 int ones = wps->int32_ones, dups = wps->int32_dups; 691 int ones = wps->int32_ones, dups = wps->int32_dups;
725// ulong mask = (1 << sent_bits) - 1; 692// ulong mask = (1 << sent_bits) - 1;
726 long *dptr = buffer; 693 long *dptr = buffer;
727 694
728 if (!(flags & HYBRID_FLAG) && !sent_bits && (zeros + ones + dups)) 695 if (!(flags & HYBRID_FLAG) && !sent_bits && (zeros + ones + dups))
729 while (count--) { 696 while (count--) {
730 if (zeros) 697 if (zeros)
731 *dptr <<= zeros; 698 *dptr <<= zeros;
732 else if (ones) 699 else if (ones)
733 *dptr = ((*dptr + 1) << ones) - 1; 700 *dptr = ((*dptr + 1) << ones) - 1;
734 else if (dups) 701 else if (dups)
735 *dptr = ((*dptr + (*dptr & 1)) << dups) - (*dptr & 1); 702 *dptr = ((*dptr + (*dptr & 1)) << dups) - (*dptr & 1);
736 703
737 dptr++; 704 dptr++;
738 } 705 }
739 else 706 else
740 shift += zeros + sent_bits + ones + dups; 707 shift += zeros + sent_bits + ones + dups;
741 } 708 }
742 709
743 if (flags & HYBRID_FLAG) { 710 if (flags & HYBRID_FLAG) {
744 long min_value, max_value, min_shifted, max_shifted; 711 long min_value, max_value, min_shifted, max_shifted;
745 712
746 switch (flags & BYTES_STORED) { 713 switch (flags & BYTES_STORED) {
747 case 0: 714 case 0:
748 min_shifted = (min_value = -128 >> shift) << shift; 715 min_shifted = (min_value = -128 >> shift) << shift;
749 max_shifted = (max_value = 127 >> shift) << shift; 716 max_shifted = (max_value = 127 >> shift) << shift;
750 break; 717 break;
751 718
752 case 1: 719 case 1:
753 min_shifted = (min_value = -32768 >> shift) << shift; 720 min_shifted = (min_value = -32768 >> shift) << shift;
754 max_shifted = (max_value = 32767 >> shift) << shift; 721 max_shifted = (max_value = 32767 >> shift) << shift;
755 break; 722 break;
756 723
757 case 2: 724 case 2:
758 min_shifted = (min_value = -8388608 >> shift) << shift; 725 min_shifted = (min_value = -8388608 >> shift) << shift;
759 max_shifted = (max_value = 8388607 >> shift) << shift; 726 max_shifted = (max_value = 8388607 >> shift) << shift;
760 break; 727 break;
761 728
762 case 3: 729 case 3:
763 default: 730 default:
764 min_shifted = (min_value = (long) 0x80000000 >> shift) << shift; 731 min_shifted = (min_value = (long) 0x80000000 >> shift) << shift;
765 max_shifted = (max_value = (long) 0x7FFFFFFF >> shift) << shift; 732 max_shifted = (max_value = (long) 0x7FFFFFFF >> shift) << shift;
766 break; 733 break;
767 } 734 }
768 735
769 if (!(flags & MONO_FLAG)) 736 if (!(flags & MONO_FLAG))
770 sample_count *= 2; 737 sample_count *= 2;
771 738
772 while (sample_count--) { 739 while (sample_count--) {
773 if (*buffer < min_value) 740 if (*buffer < min_value)
774 *buffer++ = min_shifted; 741 *buffer++ = min_shifted;
775 else if (*buffer > max_value) 742 else if (*buffer > max_value)
776 *buffer++ = max_shifted; 743 *buffer++ = max_shifted;
777 else 744 else
778 *buffer++ <<= shift; 745 *buffer++ <<= shift;
779 } 746 }
780 } 747 }
781 else if (shift) { 748 else if (shift) {
782 if (!(flags & MONO_FLAG)) 749 if (!(flags & MONO_FLAG))
783 sample_count *= 2; 750 sample_count *= 2;
784 751
785 while (sample_count--) 752 while (sample_count--)
786 *buffer++ <<= shift; 753 *buffer++ <<= shift;
787 } 754 }
788} 755}
789 756
@@ -800,7 +767,7 @@ int check_crc_error (WavpackContext *wpc)
800 int result = 0; 767 int result = 0;
801 768
802 if (wps->crc != wps->wphdr.crc) 769 if (wps->crc != wps->wphdr.crc)
803 ++result; 770 ++result;
804 771
805 return result; 772 return result;
806} 773}