1 ////////////////////////////////////////////////////////////////////////////
2 // **** WAVPACK **** //
3 // Hybrid Lossless Wavefile Compressor //
4 // Copyright (c) 1998 - 2013 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 for
12 // the entropy decoding which is handled by the read_words.c module. For better
13 // efficiency, the conversion is isolated to tight loops that handle an entire
14 // buffer.
15
16 #include <stdlib.h>
17 #include <string.h>
18
19 #include "wavpack_local.h"
20
21 #ifdef OPT_ASM_X86
22 #define DECORR_STEREO_PASS_CONT unpack_decorr_stereo_pass_cont_x86
23 #define DECORR_STEREO_PASS_CONT_AVAILABLE unpack_cpu_has_feature_x86(CPU_FEATURE_MMX)
24 #define DECORR_MONO_PASS_CONT unpack_decorr_mono_pass_cont_x86
25 #elif defined(OPT_ASM_X64) && (defined (_WIN64) || defined(__CYGWIN__) || defined(__MINGW64__) || defined(__midipix__))
26 #define DECORR_STEREO_PASS_CONT unpack_decorr_stereo_pass_cont_x64win
27 #define DECORR_STEREO_PASS_CONT_AVAILABLE 1
28 #define DECORR_MONO_PASS_CONT unpack_decorr_mono_pass_cont_x64win
29 #elif defined(OPT_ASM_X64)
30 #define DECORR_STEREO_PASS_CONT unpack_decorr_stereo_pass_cont_x64
31 #define DECORR_STEREO_PASS_CONT_AVAILABLE 1
32 #define DECORR_MONO_PASS_CONT unpack_decorr_mono_pass_cont_x64
33 #elif defined(OPT_ASM_ARM)
34 #define DECORR_STEREO_PASS_CONT unpack_decorr_stereo_pass_cont_armv7
35 #define DECORR_STEREO_PASS_CONT_AVAILABLE 1
36 #define DECORR_MONO_PASS_CONT unpack_decorr_mono_pass_cont_armv7
37 #endif
38
39 #ifdef DECORR_STEREO_PASS_CONT
40 extern void DECORR_STEREO_PASS_CONT (struct decorr_pass *dpp, int32_t *buffer, int32_t sample_count, int32_t long_math);
41 extern void DECORR_MONO_PASS_CONT (struct decorr_pass *dpp, int32_t *buffer, int32_t sample_count, int32_t long_math);
42 #endif
43
44 // This flag provides the functionality of terminating the decoding and muting
45 // the output when a lossy sample appears to be corrupt. This is automatic
46 // for lossless files because a corrupt sample is unambigious, but for lossy
47 // data it might be possible for this to falsely trigger (although I have never
48 // seen it).
49
50 #define LOSSY_MUTE
51
52 ///////////////////////////// executable code ////////////////////////////////
53
54 // This monster actually unpacks the WavPack bitstream(s) into the specified
55 // buffer as 32-bit integers or floats (depending on original data). Lossy
56 // samples will be clipped to their original limits (i.e. 8-bit samples are
57 // clipped to -128/+127) but are still returned in longs. It is up to the
58 // caller to potentially reformat this for the final output including any
59 // multichannel distribution, block alignment or endian compensation. The
60 // function unpack_init() must have been called and the entire WavPack block
61 // must still be visible (although wps->blockbuff will not be accessed again).
62 // For maximum clarity, the function is broken up into segments that handle
63 // various modes. This makes for a few extra infrequent flag checks, but
64 // makes the code easier to follow because the nesting does not become so
65 // deep. For maximum efficiency, the conversion is isolated to tight loops
66 // that handle an entire buffer. The function returns the total number of
67 // samples unpacked, which can be less than the number requested if an error
68 // occurs or the end of the block is reached.
69
70 static void decorr_stereo_pass (struct decorr_pass *dpp, int32_t *buffer, int32_t sample_count);
71 static void decorr_mono_pass (struct decorr_pass *dpp, int32_t *buffer, int32_t sample_count);
72 static void fixup_samples (WavpackContext *wpc, int32_t *buffer, uint32_t sample_count);
73
unpack_samples(WavpackContext * wpc,int32_t * buffer,uint32_t sample_count)74 int32_t unpack_samples (WavpackContext *wpc, int32_t *buffer, uint32_t sample_count)
75 {
76 WavpackStream *wps = wpc->streams [wpc->current_stream];
77 uint32_t flags = wps->wphdr.flags, crc = wps->crc, i;
78 int32_t mute_limit = (1L << ((flags & MAG_MASK) >> MAG_LSB)) + 2;
79 int32_t correction [2], read_word, *bptr;
80 struct decorr_pass *dpp;
81 int tcount, m = 0;
82
83 // don't attempt to decode past the end of the block, but watch out for overflow!
84
85 if (wps->sample_index + sample_count > GET_BLOCK_INDEX (wps->wphdr) + wps->wphdr.block_samples &&
86 (uint32_t) (GET_BLOCK_INDEX (wps->wphdr) + wps->wphdr.block_samples - wps->sample_index) < sample_count)
87 sample_count = (uint32_t) (GET_BLOCK_INDEX (wps->wphdr) + wps->wphdr.block_samples - wps->sample_index);
88
89 if (GET_BLOCK_INDEX (wps->wphdr) > wps->sample_index || wps->wphdr.block_samples < sample_count)
90 wps->mute_error = TRUE;
91
92 if (wps->mute_error) {
93 if (wpc->reduced_channels == 1 || wpc->config.num_channels == 1 || (flags & MONO_FLAG))
94 memset (buffer, 0, sample_count * 4);
95 else
96 memset (buffer, 0, sample_count * 8);
97
98 wps->sample_index += sample_count;
99 return sample_count;
100 }
101
102 if ((flags & HYBRID_FLAG) && !wps->block2buff)
103 mute_limit = (mute_limit * 2) + 128;
104
105 //////////////// handle lossless or hybrid lossy mono data /////////////////
106
107 if (!wps->block2buff && (flags & MONO_DATA)) {
108 int32_t *eptr = buffer + sample_count;
109
110 if (flags & HYBRID_FLAG) {
111 i = sample_count;
112
113 for (bptr = buffer; bptr < eptr;)
114 if ((*bptr++ = get_word (wps, 0, NULL)) == WORD_EOF) {
115 i = (uint32_t)(bptr - buffer);
116 break;
117 }
118 }
119 else
120 i = get_words_lossless (wps, buffer, sample_count);
121
122 if (i != sample_count)
123 goto get_word_eof;
124
125 #ifdef DECORR_MONO_PASS_CONT
126 if (sample_count < 16)
127 for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++)
128 decorr_mono_pass (dpp, buffer, sample_count);
129 else
130 for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++) {
131 int pre_samples = (dpp->term > MAX_TERM) ? 2 : dpp->term;
132
133 decorr_mono_pass (dpp, buffer, pre_samples);
134
135 DECORR_MONO_PASS_CONT (dpp, buffer + pre_samples, sample_count - pre_samples,
136 ((flags & MAG_MASK) >> MAG_LSB) > 15);
137 }
138 #else
139 for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++)
140 decorr_mono_pass (dpp, buffer, sample_count);
141 #endif
142
143 #ifndef LOSSY_MUTE
144 if (!(flags & HYBRID_FLAG))
145 #endif
146 for (bptr = buffer; bptr < eptr; ++bptr) {
147 if (labs (bptr [0]) > mute_limit) {
148 i = (uint32_t)(bptr - buffer);
149 break;
150 }
151
152 crc = crc * 3 + bptr [0];
153 }
154 #ifndef LOSSY_MUTE
155 else
156 for (bptr = buffer; bptr < eptr; ++bptr)
157 crc = crc * 3 + bptr [0];
158 #endif
159 }
160
161 /////////////// handle lossless or hybrid lossy stereo data ///////////////
162
163 else if (!wps->block2buff && !(flags & MONO_DATA)) {
164 int32_t *eptr = buffer + (sample_count * 2);
165
166 if (flags & HYBRID_FLAG) {
167 i = sample_count;
168
169 for (bptr = buffer; bptr < eptr; bptr += 2)
170 if ((bptr [0] = get_word (wps, 0, NULL)) == WORD_EOF ||
171 (bptr [1] = get_word (wps, 1, NULL)) == WORD_EOF) {
172 i = (uint32_t)(bptr - buffer) / 2;
173 break;
174 }
175 }
176 else
177 i = get_words_lossless (wps, buffer, sample_count);
178
179 if (i != sample_count)
180 goto get_word_eof;
181
182 #ifdef DECORR_STEREO_PASS_CONT
183 if (sample_count < 16 || !DECORR_STEREO_PASS_CONT_AVAILABLE) {
184 for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++)
185 decorr_stereo_pass (dpp, buffer, sample_count);
186
187 m = sample_count & (MAX_TERM - 1);
188 }
189 else
190 for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++) {
191 int pre_samples = (dpp->term < 0 || dpp->term > MAX_TERM) ? 2 : dpp->term;
192
193 decorr_stereo_pass (dpp, buffer, pre_samples);
194
195 DECORR_STEREO_PASS_CONT (dpp, buffer + pre_samples * 2, sample_count - pre_samples,
196 ((flags & MAG_MASK) >> MAG_LSB) >= 16);
197 }
198 #else
199 for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++)
200 decorr_stereo_pass (dpp, buffer, sample_count);
201
202 m = sample_count & (MAX_TERM - 1);
203 #endif
204
205 if (flags & JOINT_STEREO)
206 for (bptr = buffer; bptr < eptr; bptr += 2) {
207 bptr [0] += (bptr [1] -= (bptr [0] >> 1));
208 crc += (crc << 3) + ((uint32_t) bptr [0] << 1) + bptr [0] + bptr [1];
209 }
210 else
211 for (bptr = buffer; bptr < eptr; bptr += 2)
212 crc += (crc << 3) + ((uint32_t) bptr [0] << 1) + bptr [0] + bptr [1];
213
214 #ifndef LOSSY_MUTE
215 if (!(flags & HYBRID_FLAG))
216 #endif
217 for (bptr = buffer; bptr < eptr; bptr += 16)
218 if (labs (bptr [0]) > mute_limit || labs (bptr [1]) > mute_limit) {
219 i = (uint32_t)(bptr - buffer) / 2;
220 break;
221 }
222 }
223
224 /////////////////// handle hybrid lossless mono data ////////////////////
225
226 else if ((flags & HYBRID_FLAG) && (flags & MONO_DATA))
227 for (bptr = buffer, i = 0; i < sample_count; ++i) {
228
229 if ((read_word = get_word (wps, 0, correction)) == WORD_EOF)
230 break;
231
232 for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++) {
233 int32_t sam, temp;
234 int k;
235
236 if (dpp->term > MAX_TERM) {
237 if (dpp->term & 1)
238 sam = 2 * dpp->samples_A [0] - dpp->samples_A [1];
239 else
240 sam = (3 * dpp->samples_A [0] - dpp->samples_A [1]) >> 1;
241
242 dpp->samples_A [1] = dpp->samples_A [0];
243 k = 0;
244 }
245 else {
246 sam = dpp->samples_A [m];
247 k = (m + dpp->term) & (MAX_TERM - 1);
248 }
249
250 temp = apply_weight (dpp->weight_A, sam) + read_word;
251 update_weight (dpp->weight_A, dpp->delta, sam, read_word);
252 dpp->samples_A [k] = read_word = temp;
253 }
254
255 m = (m + 1) & (MAX_TERM - 1);
256
257 if (flags & HYBRID_SHAPE) {
258 int shaping_weight = (wps->dc.shaping_acc [0] += wps->dc.shaping_delta [0]) >> 16;
259 int32_t temp = -apply_weight (shaping_weight, wps->dc.error [0]);
260
261 if ((flags & NEW_SHAPING) && shaping_weight < 0 && temp) {
262 if (temp == wps->dc.error [0])
263 temp = (temp < 0) ? temp + 1 : temp - 1;
264
265 wps->dc.error [0] = temp - correction [0];
266 }
267 else
268 wps->dc.error [0] = -correction [0];
269
270 read_word += correction [0] - temp;
271 }
272 else
273 read_word += correction [0];
274
275 crc += (crc << 1) + read_word;
276
277 if (labs (read_word) > mute_limit)
278 break;
279
280 *bptr++ = read_word;
281 }
282
283 //////////////////// handle hybrid lossless stereo data ///////////////////
284
285 else if (wps->block2buff && !(flags & MONO_DATA))
286 for (bptr = buffer, i = 0; i < sample_count; ++i) {
287 int32_t left, right, left2, right2;
288 int32_t left_c = 0, right_c = 0;
289
290 if ((left = get_word (wps, 0, correction)) == WORD_EOF ||
291 (right = get_word (wps, 1, correction + 1)) == WORD_EOF)
292 break;
293
294 if (flags & CROSS_DECORR) {
295 left_c = left + correction [0];
296 right_c = right + correction [1];
297
298 for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++) {
299 int32_t sam_A, sam_B;
300
301 if (dpp->term > 0) {
302 if (dpp->term > MAX_TERM) {
303 if (dpp->term & 1) {
304 sam_A = 2 * dpp->samples_A [0] - dpp->samples_A [1];
305 sam_B = 2 * dpp->samples_B [0] - dpp->samples_B [1];
306 }
307 else {
308 sam_A = (3 * dpp->samples_A [0] - dpp->samples_A [1]) >> 1;
309 sam_B = (3 * dpp->samples_B [0] - dpp->samples_B [1]) >> 1;
310 }
311 }
312 else {
313 sam_A = dpp->samples_A [m];
314 sam_B = dpp->samples_B [m];
315 }
316
317 left_c += apply_weight (dpp->weight_A, sam_A);
318 right_c += apply_weight (dpp->weight_B, sam_B);
319 }
320 else if (dpp->term == -1) {
321 left_c += apply_weight (dpp->weight_A, dpp->samples_A [0]);
322 right_c += apply_weight (dpp->weight_B, left_c);
323 }
324 else {
325 right_c += apply_weight (dpp->weight_B, dpp->samples_B [0]);
326
327 if (dpp->term == -3)
328 left_c += apply_weight (dpp->weight_A, dpp->samples_A [0]);
329 else
330 left_c += apply_weight (dpp->weight_A, right_c);
331 }
332 }
333
334 if (flags & JOINT_STEREO)
335 left_c += (right_c -= (left_c >> 1));
336 }
337
338 for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++) {
339 int32_t sam_A, sam_B;
340
341 if (dpp->term > 0) {
342 int k;
343
344 if (dpp->term > MAX_TERM) {
345 if (dpp->term & 1) {
346 sam_A = 2 * dpp->samples_A [0] - dpp->samples_A [1];
347 sam_B = 2 * dpp->samples_B [0] - dpp->samples_B [1];
348 }
349 else {
350 sam_A = (3 * dpp->samples_A [0] - dpp->samples_A [1]) >> 1;
351 sam_B = (3 * dpp->samples_B [0] - dpp->samples_B [1]) >> 1;
352 }
353
354 dpp->samples_A [1] = dpp->samples_A [0];
355 dpp->samples_B [1] = dpp->samples_B [0];
356 k = 0;
357 }
358 else {
359 sam_A = dpp->samples_A [m];
360 sam_B = dpp->samples_B [m];
361 k = (m + dpp->term) & (MAX_TERM - 1);
362 }
363
364 left2 = apply_weight (dpp->weight_A, sam_A) + left;
365 right2 = apply_weight (dpp->weight_B, sam_B) + right;
366
367 update_weight (dpp->weight_A, dpp->delta, sam_A, left);
368 update_weight (dpp->weight_B, dpp->delta, sam_B, right);
369
370 dpp->samples_A [k] = left = left2;
371 dpp->samples_B [k] = right = right2;
372 }
373 else if (dpp->term == -1) {
374 left2 = left + apply_weight (dpp->weight_A, dpp->samples_A [0]);
375 update_weight_clip (dpp->weight_A, dpp->delta, dpp->samples_A [0], left);
376 left = left2;
377 right2 = right + apply_weight (dpp->weight_B, left2);
378 update_weight_clip (dpp->weight_B, dpp->delta, left2, right);
379 dpp->samples_A [0] = right = right2;
380 }
381 else {
382 right2 = right + apply_weight (dpp->weight_B, dpp->samples_B [0]);
383 update_weight_clip (dpp->weight_B, dpp->delta, dpp->samples_B [0], right);
384 right = right2;
385
386 if (dpp->term == -3) {
387 right2 = dpp->samples_A [0];
388 dpp->samples_A [0] = right;
389 }
390
391 left2 = left + apply_weight (dpp->weight_A, right2);
392 update_weight_clip (dpp->weight_A, dpp->delta, right2, left);
393 dpp->samples_B [0] = left = left2;
394 }
395 }
396
397 m = (m + 1) & (MAX_TERM - 1);
398
399 if (!(flags & CROSS_DECORR)) {
400 left_c = left + correction [0];
401 right_c = right + correction [1];
402
403 if (flags & JOINT_STEREO)
404 left_c += (right_c -= (left_c >> 1));
405 }
406
407 if (flags & JOINT_STEREO)
408 left += (right -= (left >> 1));
409
410 if (flags & HYBRID_SHAPE) {
411 int shaping_weight;
412 int32_t temp;
413
414 correction [0] = left_c - left;
415 shaping_weight = (wps->dc.shaping_acc [0] += wps->dc.shaping_delta [0]) >> 16;
416 temp = -apply_weight (shaping_weight, wps->dc.error [0]);
417
418 if ((flags & NEW_SHAPING) && shaping_weight < 0 && temp) {
419 if (temp == wps->dc.error [0])
420 temp = (temp < 0) ? temp + 1 : temp - 1;
421
422 wps->dc.error [0] = temp - correction [0];
423 }
424 else
425 wps->dc.error [0] = -correction [0];
426
427 left = left_c - temp;
428 correction [1] = right_c - right;
429 shaping_weight = (wps->dc.shaping_acc [1] += wps->dc.shaping_delta [1]) >> 16;
430 temp = -apply_weight (shaping_weight, wps->dc.error [1]);
431
432 if ((flags & NEW_SHAPING) && shaping_weight < 0 && temp) {
433 if (temp == wps->dc.error [1])
434 temp = (temp < 0) ? temp + 1 : temp - 1;
435
436 wps->dc.error [1] = temp - correction [1];
437 }
438 else
439 wps->dc.error [1] = -correction [1];
440
441 right = right_c - temp;
442 }
443 else {
444 left = left_c;
445 right = right_c;
446 }
447
448 if (labs (left) > mute_limit || labs (right) > mute_limit)
449 break;
450
451 crc += (crc << 3) + ((uint32_t) left << 1) + left + right;
452 *bptr++ = left;
453 *bptr++ = right;
454 }
455 else
456 i = 0; /* this line can't execute, but suppresses compiler warning */
457
458 get_word_eof:
459 if (i != sample_count) {
460 memset (buffer, 0, sample_count * (flags & MONO_FLAG ? 4 : 8));
461 wps->mute_error = TRUE;
462 i = sample_count;
463
464 if (bs_is_open (&wps->wvxbits))
465 bs_close_read (&wps->wvxbits);
466 }
467
468 if (m)
469 for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++)
470 if (dpp->term > 0 && dpp->term <= MAX_TERM) {
471 int32_t temp_A [MAX_TERM], temp_B [MAX_TERM];
472 int k;
473
474 memcpy (temp_A, dpp->samples_A, sizeof (dpp->samples_A));
475 memcpy (temp_B, dpp->samples_B, sizeof (dpp->samples_B));
476
477 for (k = 0; k < MAX_TERM; k++) {
478 dpp->samples_A [k] = temp_A [m];
479 dpp->samples_B [k] = temp_B [m];
480 m = (m + 1) & (MAX_TERM - 1);
481 }
482 }
483
484 fixup_samples (wpc, buffer, i);
485
486 if ((flags & FLOAT_DATA) && (wpc->open_flags & OPEN_NORMALIZE))
487 WavpackFloatNormalize (buffer, (flags & MONO_DATA) ? i : i * 2,
488 127 - wps->float_norm_exp + wpc->norm_offset);
489
490 if (flags & FALSE_STEREO) {
491 int32_t *dptr = buffer + i * 2;
492 int32_t *sptr = buffer + i;
493 int32_t c = i;
494
495 while (c--) {
496 *--dptr = *--sptr;
497 *--dptr = *sptr;
498 }
499 }
500
501 wps->sample_index += i;
502 wps->crc = crc;
503
504 return i;
505 }
506
507 // General function to perform mono decorrelation pass on specified buffer
508 // (although since this is the reverse function it might technically be called
509 // "correlation" instead). This version handles all sample resolutions and
510 // weight deltas. The dpp->samples_X[] data is returned normalized for term
511 // values 1-8.
512
decorr_mono_pass(struct decorr_pass * dpp,int32_t * buffer,int32_t sample_count)513 static void decorr_mono_pass (struct decorr_pass *dpp, int32_t *buffer, int32_t sample_count)
514 {
515 int32_t delta = dpp->delta, weight_A = dpp->weight_A;
516 int32_t *bptr, *eptr = buffer + sample_count, sam_A;
517 int m, k;
518
519 switch (dpp->term) {
520
521 case 17:
522 for (bptr = buffer; bptr < eptr; bptr++) {
523 sam_A = 2 * dpp->samples_A [0] - dpp->samples_A [1];
524 dpp->samples_A [1] = dpp->samples_A [0];
525 dpp->samples_A [0] = apply_weight (weight_A, sam_A) + bptr [0];
526 update_weight (weight_A, delta, sam_A, bptr [0]);
527 bptr [0] = dpp->samples_A [0];
528 }
529
530 break;
531
532 case 18:
533 for (bptr = buffer; bptr < eptr; bptr++) {
534 sam_A = (3 * dpp->samples_A [0] - dpp->samples_A [1]) >> 1;
535 dpp->samples_A [1] = dpp->samples_A [0];
536 dpp->samples_A [0] = apply_weight (weight_A, sam_A) + bptr [0];
537 update_weight (weight_A, delta, sam_A, bptr [0]);
538 bptr [0] = dpp->samples_A [0];
539 }
540
541 break;
542
543 default:
544 for (m = 0, k = dpp->term & (MAX_TERM - 1), bptr = buffer; bptr < eptr; bptr++) {
545 sam_A = dpp->samples_A [m];
546 dpp->samples_A [k] = apply_weight (weight_A, sam_A) + bptr [0];
547 update_weight (weight_A, delta, sam_A, bptr [0]);
548 bptr [0] = dpp->samples_A [k];
549 m = (m + 1) & (MAX_TERM - 1);
550 k = (k + 1) & (MAX_TERM - 1);
551 }
552
553 if (m) {
554 int32_t temp_samples [MAX_TERM];
555
556 memcpy (temp_samples, dpp->samples_A, sizeof (dpp->samples_A));
557
558 for (k = 0; k < MAX_TERM; k++, m++)
559 dpp->samples_A [k] = temp_samples [m & (MAX_TERM - 1)];
560 }
561
562 break;
563 }
564
565 dpp->weight_A = weight_A;
566 }
567
568 // General function to perform stereo decorrelation pass on specified buffer
569 // (although since this is the reverse function it might technically be called
570 // "correlation" instead). This version handles all sample resolutions and
571 // weight deltas. The dpp->samples_X[] data is *not* returned normalized for
572 // term values 1-8, so it should be normalized if it is going to be used to
573 // call this function again.
574
decorr_stereo_pass(struct decorr_pass * dpp,int32_t * buffer,int32_t sample_count)575 static void decorr_stereo_pass (struct decorr_pass *dpp, int32_t *buffer, int32_t sample_count)
576 {
577 int32_t *bptr, *eptr = buffer + (sample_count * 2);
578 int m, k;
579
580 switch (dpp->term) {
581 case 17:
582 for (bptr = buffer; bptr < eptr; bptr += 2) {
583 int32_t sam, tmp;
584
585 sam = 2 * dpp->samples_A [0] - dpp->samples_A [1];
586 dpp->samples_A [1] = dpp->samples_A [0];
587 bptr [0] = dpp->samples_A [0] = apply_weight (dpp->weight_A, sam) + (tmp = bptr [0]);
588 update_weight (dpp->weight_A, dpp->delta, sam, tmp);
589
590 sam = 2 * dpp->samples_B [0] - dpp->samples_B [1];
591 dpp->samples_B [1] = dpp->samples_B [0];
592 bptr [1] = dpp->samples_B [0] = apply_weight (dpp->weight_B, sam) + (tmp = bptr [1]);
593 update_weight (dpp->weight_B, dpp->delta, sam, tmp);
594 }
595
596 break;
597
598 case 18:
599 for (bptr = buffer; bptr < eptr; bptr += 2) {
600 int32_t sam, tmp;
601
602 sam = dpp->samples_A [0] + ((dpp->samples_A [0] - dpp->samples_A [1]) >> 1);
603 dpp->samples_A [1] = dpp->samples_A [0];
604 bptr [0] = dpp->samples_A [0] = apply_weight (dpp->weight_A, sam) + (tmp = bptr [0]);
605 update_weight (dpp->weight_A, dpp->delta, sam, tmp);
606
607 sam = dpp->samples_B [0] + ((dpp->samples_B [0] - dpp->samples_B [1]) >> 1);
608 dpp->samples_B [1] = dpp->samples_B [0];
609 bptr [1] = dpp->samples_B [0] = apply_weight (dpp->weight_B, sam) + (tmp = bptr [1]);
610 update_weight (dpp->weight_B, dpp->delta, sam, tmp);
611 }
612
613 break;
614
615 default:
616 for (m = 0, k = dpp->term & (MAX_TERM - 1), bptr = buffer; bptr < eptr; bptr += 2) {
617 int32_t sam;
618
619 sam = dpp->samples_A [m];
620 dpp->samples_A [k] = apply_weight (dpp->weight_A, sam) + bptr [0];
621 update_weight (dpp->weight_A, dpp->delta, sam, bptr [0]);
622 bptr [0] = dpp->samples_A [k];
623
624 sam = dpp->samples_B [m];
625 dpp->samples_B [k] = apply_weight (dpp->weight_B, sam) + bptr [1];
626 update_weight (dpp->weight_B, dpp->delta, sam, bptr [1]);
627 bptr [1] = dpp->samples_B [k];
628
629 m = (m + 1) & (MAX_TERM - 1);
630 k = (k + 1) & (MAX_TERM - 1);
631 }
632
633 break;
634
635 case -1:
636 for (bptr = buffer; bptr < eptr; bptr += 2) {
637 int32_t sam;
638
639 sam = bptr [0] + apply_weight (dpp->weight_A, dpp->samples_A [0]);
640 update_weight_clip (dpp->weight_A, dpp->delta, dpp->samples_A [0], bptr [0]);
641 bptr [0] = sam;
642 dpp->samples_A [0] = bptr [1] + apply_weight (dpp->weight_B, sam);
643 update_weight_clip (dpp->weight_B, dpp->delta, sam, bptr [1]);
644 bptr [1] = dpp->samples_A [0];
645 }
646
647 break;
648
649 case -2:
650 for (bptr = buffer; bptr < eptr; bptr += 2) {
651 int32_t sam;
652
653 sam = bptr [1] + apply_weight (dpp->weight_B, dpp->samples_B [0]);
654 update_weight_clip (dpp->weight_B, dpp->delta, dpp->samples_B [0], bptr [1]);
655 bptr [1] = sam;
656 dpp->samples_B [0] = bptr [0] + apply_weight (dpp->weight_A, sam);
657 update_weight_clip (dpp->weight_A, dpp->delta, sam, bptr [0]);
658 bptr [0] = dpp->samples_B [0];
659 }
660
661 break;
662
663 case -3:
664 for (bptr = buffer; bptr < eptr; bptr += 2) {
665 int32_t sam_A, sam_B;
666
667 sam_A = bptr [0] + apply_weight (dpp->weight_A, dpp->samples_A [0]);
668 update_weight_clip (dpp->weight_A, dpp->delta, dpp->samples_A [0], bptr [0]);
669 sam_B = bptr [1] + apply_weight (dpp->weight_B, dpp->samples_B [0]);
670 update_weight_clip (dpp->weight_B, dpp->delta, dpp->samples_B [0], bptr [1]);
671 bptr [0] = dpp->samples_B [0] = sam_A;
672 bptr [1] = dpp->samples_A [0] = sam_B;
673 }
674
675 break;
676 }
677 }
678
679 // This is a helper function for unpack_samples() that applies several final
680 // operations. First, if the data is 32-bit float data, then that conversion
681 // is done in the float.c module (whether lossy or lossless) and we return.
682 // Otherwise, if the extended integer data applies, then that operation is
683 // executed first. If the unpacked data is lossy (and not corrected) then
684 // it is clipped and shifted in a single operation. Otherwise, if it's
685 // lossless then the last step is to apply the final shift (if any).
686
fixup_samples(WavpackContext * wpc,int32_t * buffer,uint32_t sample_count)687 static void fixup_samples (WavpackContext *wpc, int32_t *buffer, uint32_t sample_count)
688 {
689 WavpackStream *wps = wpc->streams [wpc->current_stream];
690 uint32_t flags = wps->wphdr.flags;
691 int lossy_flag = (flags & HYBRID_FLAG) && !wps->block2buff;
692 int shift = (flags & SHIFT_MASK) >> SHIFT_LSB;
693
694 if (flags & FLOAT_DATA) {
695 float_values (wps, buffer, (flags & MONO_DATA) ? sample_count : sample_count * 2);
696 return;
697 }
698
699 if (flags & INT32_DATA) {
700 uint32_t count = (flags & MONO_DATA) ? sample_count : sample_count * 2;
701 int sent_bits = wps->int32_sent_bits & 0x1f, zeros = wps->int32_zeros & 0x1f;
702 int ones = wps->int32_ones & 0x1f, dups = wps->int32_dups & 0x1f;
703 uint32_t data, mask = (1U << sent_bits) - 1;
704 int32_t *dptr = buffer;
705
706 if (bs_is_open (&wps->wvxbits)) {
707 uint32_t crc = wps->crc_x;
708
709 while (count--) {
710 // if (sent_bits) {
711 getbits (&data, sent_bits, &wps->wvxbits);
712 *dptr = ((uint32_t) *dptr << sent_bits) | (data & mask);
713 // }
714
715 if (zeros)
716 *(uint32_t*)dptr <<= zeros;
717 else if (ones)
718 *dptr = ((uint32_t)(*dptr + 1) << ones) - 1;
719 else if (dups)
720 *dptr = ((uint32_t)(*dptr + (*dptr & 1)) << dups) - (*dptr & 1);
721
722 crc = crc * 9 + (*dptr & 0xffff) * 3 + ((*dptr >> 16) & 0xffff);
723 dptr++;
724 }
725
726 wps->crc_x = crc;
727 }
728 else if (!sent_bits && (zeros + ones + dups)) {
729 while (lossy_flag && (flags & BYTES_STORED) == 3 && shift < 8) {
730 if (zeros)
731 zeros--;
732 else if (ones)
733 ones--;
734 else if (dups)
735 dups--;
736 else
737 break;
738
739 shift++;
740 }
741
742 while (count--) {
743 if (zeros)
744 *(uint32_t*)dptr <<= zeros;
745 else if (ones)
746 *dptr = ((uint32_t)(*dptr + 1) << ones) - 1;
747 else if (dups)
748 *dptr = ((uint32_t)(*dptr + (*dptr & 1)) << dups) - (*dptr & 1);
749
750 dptr++;
751 }
752 }
753 else
754 shift += zeros + sent_bits + ones + dups;
755 }
756
757 shift &= 0x1f;
758
759 if (lossy_flag) {
760 int32_t min_value, max_value, min_shifted, max_shifted;
761
762 switch (flags & BYTES_STORED) {
763 case 0:
764 min_shifted = (uint32_t)(min_value = -128 >> shift) << shift;
765 max_shifted = (max_value = 127 >> shift) << shift;
766 break;
767
768 case 1:
769 min_shifted = (uint32_t)(min_value = -32768 >> shift) << shift;
770 max_shifted = (max_value = 32767 >> shift) << shift;
771 break;
772
773 case 2:
774 min_shifted = (uint32_t)(min_value = -8388608 >> shift) << shift;
775 max_shifted = (max_value = 8388607 >> shift) << shift;
776 break;
777
778 case 3: default: /* "default" suppresses compiler warning */
779 min_shifted = (uint32_t)(min_value = (int32_t) 0x80000000 >> shift) << shift;
780 max_shifted = (max_value = (int32_t) 0x7fffffff >> shift) << shift;
781 break;
782 }
783
784 if (!(flags & MONO_DATA))
785 sample_count *= 2;
786
787 while (sample_count--) {
788 if (*buffer < min_value)
789 *buffer++ = min_shifted;
790 else if (*buffer > max_value)
791 *buffer++ = max_shifted;
792 else
793 *(uint32_t*)buffer++ <<= shift;
794 }
795 }
796 else if (shift) {
797 if (!(flags & MONO_DATA))
798 sample_count *= 2;
799
800 while (sample_count--)
801 *(uint32_t*)buffer++ <<= shift;
802 }
803 }
804
805 // This function checks the crc value(s) for an unpacked block, returning the
806 // number of actual crc errors detected for the block. The block must be
807 // completely unpacked before this test is valid. For losslessly unpacked
808 // blocks of float or extended integer data the extended crc is also checked.
809 // Note that WavPack's crc is not a CCITT approved polynomial algorithm, but
810 // is a much simpler method that is virtually as robust for real world data.
811
check_crc_error(WavpackContext * wpc)812 int check_crc_error (WavpackContext *wpc)
813 {
814 int result = 0, stream;
815
816 for (stream = 0; stream < wpc->num_streams; stream++) {
817 WavpackStream *wps = wpc->streams [stream];
818
819 if (wps->crc != wps->wphdr.crc)
820 ++result;
821 else if (bs_is_open (&wps->wvxbits) && wps->crc_x != wps->crc_wvx)
822 ++result;
823 }
824
825 return result;
826 }
827