/src/wavpack/src/unpack.c
Line | Count | Source (jump to first uncovered line) |
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 | 59.5k | #define DECORR_STEREO_PASS_CONT unpack_decorr_stereo_pass_cont_x64 |
31 | 70.4k | #define DECORR_STEREO_PASS_CONT_AVAILABLE 1 |
32 | 23.4k | #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 ASMCALL DECORR_STEREO_PASS_CONT (struct decorr_pass *dpp, int32_t *buffer, int32_t sample_count, int32_t long_math); |
41 | | extern void ASMCALL 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 unambiguous, 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 (WavpackStream *wps, int32_t *buffer, uint32_t sample_count); |
73 | | |
74 | | int32_t unpack_samples (WavpackStream *wps, int32_t *buffer, uint32_t sample_count) |
75 | 420k | { |
76 | 420k | uint32_t flags = wps->wphdr.flags, crc = wps->crc, i; |
77 | 420k | int32_t mute_limit = (1L << ((flags & MAG_MASK) >> MAG_LSB)) + 2; |
78 | 420k | int32_t correction [2], read_word, *bptr; |
79 | 420k | struct decorr_pass *dpp; |
80 | 420k | int tcount, m = 0; |
81 | | |
82 | | // don't attempt to decode past the end of the block, but watch out for overflow! |
83 | | |
84 | 420k | if (wps->sample_index + sample_count > GET_BLOCK_INDEX (wps->wphdr) + wps->wphdr.block_samples && |
85 | 420k | (uint32_t) (GET_BLOCK_INDEX (wps->wphdr) + wps->wphdr.block_samples - wps->sample_index) < sample_count) |
86 | 98.9k | sample_count = (uint32_t) (GET_BLOCK_INDEX (wps->wphdr) + wps->wphdr.block_samples - wps->sample_index); |
87 | | |
88 | 420k | if (GET_BLOCK_INDEX (wps->wphdr) > wps->sample_index || wps->wphdr.block_samples < sample_count) |
89 | 62.3k | wps->mute_error = TRUE; |
90 | | |
91 | 420k | if (wps->mute_error) { |
92 | 254k | if (wps->wpc->reduced_channels == 1 || wps->wpc->config.num_channels == 1 || (flags & MONO_FLAG)) |
93 | 41.6k | memset (buffer, 0, sample_count * 4); |
94 | 212k | else |
95 | 212k | memset (buffer, 0, sample_count * 8); |
96 | | |
97 | 254k | wps->sample_index += sample_count; |
98 | 254k | return sample_count; |
99 | 254k | } |
100 | | |
101 | 165k | if ((flags & HYBRID_FLAG) && !wps->block2buff) |
102 | 90.9k | mute_limit = (mute_limit * 2) + 128; |
103 | | |
104 | | //////////////// handle lossless or hybrid lossy mono data ///////////////// |
105 | | |
106 | 165k | if (!wps->block2buff && (flags & MONO_DATA)) { |
107 | 83.5k | int32_t *eptr = buffer + sample_count; |
108 | | |
109 | 83.5k | if (flags & HYBRID_FLAG) { |
110 | 44.4k | i = sample_count; |
111 | | |
112 | 51.4M | for (bptr = buffer; bptr < eptr;) |
113 | 51.3M | if ((*bptr++ = get_word (wps, 0, NULL)) == WORD_EOF) { |
114 | 355 | i = (uint32_t)(bptr - buffer); |
115 | 355 | break; |
116 | 355 | } |
117 | 44.4k | } |
118 | 39.1k | else |
119 | 39.1k | i = get_words_lossless (wps, buffer, sample_count); |
120 | | |
121 | 83.5k | if (i != sample_count) |
122 | 650 | goto get_word_eof; |
123 | | |
124 | 82.9k | #ifdef DECORR_MONO_PASS_CONT |
125 | 82.9k | if (sample_count < 16) |
126 | 5.96k | for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++) |
127 | 1.79k | decorr_mono_pass (dpp, buffer, sample_count); |
128 | 78.7k | else |
129 | 102k | for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++) { |
130 | 23.4k | int pre_samples = (dpp->term > MAX_TERM) ? 2 : dpp->term; |
131 | | |
132 | 23.4k | decorr_mono_pass (dpp, buffer, pre_samples); |
133 | | |
134 | 23.4k | DECORR_MONO_PASS_CONT (dpp, buffer + pre_samples, sample_count - pre_samples, |
135 | 23.4k | ((flags & MAG_MASK) >> MAG_LSB) > 15); |
136 | 23.4k | } |
137 | | #else |
138 | | for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++) |
139 | | decorr_mono_pass (dpp, buffer, sample_count); |
140 | | #endif |
141 | | |
142 | | #ifndef LOSSY_MUTE |
143 | | if (!(flags & HYBRID_FLAG)) |
144 | | #endif |
145 | 82.7M | for (bptr = buffer; bptr < eptr; ++bptr) { |
146 | 82.6M | if (labs (bptr [0]) > mute_limit) { |
147 | 1.29k | i = (uint32_t)(bptr - buffer); |
148 | 1.29k | break; |
149 | 1.29k | } |
150 | | |
151 | 82.6M | crc = crc * 3 + bptr [0]; |
152 | 82.6M | } |
153 | | #ifndef LOSSY_MUTE |
154 | | else |
155 | | for (bptr = buffer; bptr < eptr; ++bptr) |
156 | | crc = crc * 3 + bptr [0]; |
157 | | #endif |
158 | 82.9k | } |
159 | | |
160 | | /////////////// handle lossless or hybrid lossy stereo data /////////////// |
161 | | |
162 | 82.0k | else if (!wps->block2buff && !(flags & MONO_DATA)) { |
163 | 82.0k | int32_t *eptr = buffer + (sample_count * 2); |
164 | | |
165 | 82.0k | if (flags & HYBRID_FLAG) { |
166 | 46.5k | i = sample_count; |
167 | | |
168 | 50.6M | for (bptr = buffer; bptr < eptr; bptr += 2) |
169 | 50.5M | if ((bptr [0] = get_word (wps, 0, NULL)) == WORD_EOF || |
170 | 50.5M | (bptr [1] = get_word (wps, 1, NULL)) == WORD_EOF) { |
171 | 1.45k | i = (uint32_t)(bptr - buffer) / 2; |
172 | 1.45k | break; |
173 | 1.45k | } |
174 | 46.5k | } |
175 | 35.4k | else |
176 | 35.4k | i = get_words_lossless (wps, buffer, sample_count); |
177 | | |
178 | 82.0k | if (i != sample_count) |
179 | 1.78k | goto get_word_eof; |
180 | | |
181 | 80.2k | #ifdef DECORR_STEREO_PASS_CONT |
182 | 80.2k | if (sample_count < 16 || !DECORR_STEREO_PASS_CONT_AVAILABLE) { |
183 | 14.9k | for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++) |
184 | 5.11k | decorr_stereo_pass (dpp, buffer, sample_count); |
185 | | |
186 | 9.83k | m = sample_count & (MAX_TERM - 1); |
187 | 9.83k | } |
188 | 70.4k | else |
189 | 130k | for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++) { |
190 | 59.5k | int pre_samples = (dpp->term < 0 || dpp->term > MAX_TERM) ? 2 : dpp->term; |
191 | | |
192 | 59.5k | decorr_stereo_pass (dpp, buffer, pre_samples); |
193 | | |
194 | 59.5k | DECORR_STEREO_PASS_CONT (dpp, buffer + pre_samples * 2, sample_count - pre_samples, |
195 | 59.5k | ((flags & MAG_MASK) >> MAG_LSB) >= 16); |
196 | 59.5k | } |
197 | | #else |
198 | | for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++) |
199 | | decorr_stereo_pass (dpp, buffer, sample_count); |
200 | | |
201 | | m = sample_count & (MAX_TERM - 1); |
202 | | #endif |
203 | | |
204 | 80.2k | if (flags & JOINT_STEREO) |
205 | 24.3M | for (bptr = buffer; bptr < eptr; bptr += 2) { |
206 | 24.3M | bptr [0] += (bptr [1] -= (bptr [0] >> 1)); |
207 | 24.3M | crc += (crc << 3) + ((uint32_t) bptr [0] << 1) + bptr [0] + bptr [1]; |
208 | 24.3M | } |
209 | 58.8k | else |
210 | 57.1M | for (bptr = buffer; bptr < eptr; bptr += 2) |
211 | 57.0M | crc += (crc << 3) + ((uint32_t) bptr [0] << 1) + bptr [0] + bptr [1]; |
212 | | |
213 | | #ifndef LOSSY_MUTE |
214 | | if (!(flags & HYBRID_FLAG)) |
215 | | #endif |
216 | 9.35M | for (bptr = buffer; bptr < eptr; bptr += 16) |
217 | 9.27M | if (labs (bptr [0]) > mute_limit || labs (bptr [1]) > mute_limit) { |
218 | 1.33k | i = (uint32_t)(bptr - buffer) / 2; |
219 | 1.33k | break; |
220 | 1.33k | } |
221 | 80.2k | } |
222 | | |
223 | | /////////////////// handle hybrid lossless mono data //////////////////// |
224 | | |
225 | 18.4E | else if ((flags & HYBRID_FLAG) && (flags & MONO_DATA)) |
226 | 0 | for (bptr = buffer, i = 0; i < sample_count; ++i) { |
227 | |
|
228 | 0 | if ((read_word = get_word (wps, 0, correction)) == WORD_EOF) |
229 | 0 | break; |
230 | | |
231 | 0 | for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++) { |
232 | 0 | int32_t sam, temp; |
233 | 0 | int k; |
234 | |
|
235 | 0 | if (dpp->term > MAX_TERM) { |
236 | 0 | if (dpp->term & 1) |
237 | 0 | sam = 2 * dpp->samples_A [0] - dpp->samples_A [1]; |
238 | 0 | else |
239 | 0 | sam = (3 * dpp->samples_A [0] - dpp->samples_A [1]) >> 1; |
240 | |
|
241 | 0 | dpp->samples_A [1] = dpp->samples_A [0]; |
242 | 0 | k = 0; |
243 | 0 | } |
244 | 0 | else { |
245 | 0 | sam = dpp->samples_A [m]; |
246 | 0 | k = (m + dpp->term) & (MAX_TERM - 1); |
247 | 0 | } |
248 | |
|
249 | 0 | temp = apply_weight (dpp->weight_A, sam) + read_word; |
250 | 0 | update_weight (dpp->weight_A, dpp->delta, sam, read_word); |
251 | 0 | dpp->samples_A [k] = read_word = temp; |
252 | 0 | } |
253 | |
|
254 | 0 | m = (m + 1) & (MAX_TERM - 1); |
255 | |
|
256 | 0 | if (flags & HYBRID_SHAPE) { |
257 | 0 | int shaping_weight = (wps->dc.shaping_acc [0] += wps->dc.shaping_delta [0]) >> 16; |
258 | 0 | int32_t temp = -apply_weight (shaping_weight, wps->dc.error [0]); |
259 | |
|
260 | 0 | if ((flags & NEW_SHAPING) && shaping_weight < 0 && temp) { |
261 | 0 | if (temp == wps->dc.error [0]) |
262 | 0 | temp = (temp < 0) ? temp + 1 : temp - 1; |
263 | |
|
264 | 0 | wps->dc.error [0] = temp - correction [0]; |
265 | 0 | } |
266 | 0 | else |
267 | 0 | wps->dc.error [0] = -correction [0]; |
268 | |
|
269 | 0 | read_word += correction [0] - temp; |
270 | 0 | } |
271 | 0 | else |
272 | 0 | read_word += correction [0]; |
273 | |
|
274 | 0 | crc += (crc << 1) + read_word; |
275 | |
|
276 | 0 | if (labs (read_word) > mute_limit) |
277 | 0 | break; |
278 | | |
279 | 0 | *bptr++ = read_word; |
280 | 0 | } |
281 | | |
282 | | //////////////////// handle hybrid lossless stereo data /////////////////// |
283 | | |
284 | 18.4E | else if (wps->block2buff && !(flags & MONO_DATA)) |
285 | 0 | for (bptr = buffer, i = 0; i < sample_count; ++i) { |
286 | 0 | int32_t left, right, left2, right2; |
287 | 0 | int32_t left_c = 0, right_c = 0; |
288 | |
|
289 | 0 | if ((left = get_word (wps, 0, correction)) == WORD_EOF || |
290 | 0 | (right = get_word (wps, 1, correction + 1)) == WORD_EOF) |
291 | 0 | break; |
292 | | |
293 | 0 | if (flags & CROSS_DECORR) { |
294 | 0 | left_c = left + correction [0]; |
295 | 0 | right_c = right + correction [1]; |
296 | |
|
297 | 0 | for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++) { |
298 | 0 | int32_t sam_A, sam_B; |
299 | |
|
300 | 0 | if (dpp->term > 0) { |
301 | 0 | if (dpp->term > MAX_TERM) { |
302 | 0 | if (dpp->term & 1) { |
303 | 0 | sam_A = 2 * dpp->samples_A [0] - dpp->samples_A [1]; |
304 | 0 | sam_B = 2 * dpp->samples_B [0] - dpp->samples_B [1]; |
305 | 0 | } |
306 | 0 | else { |
307 | 0 | sam_A = (3 * dpp->samples_A [0] - dpp->samples_A [1]) >> 1; |
308 | 0 | sam_B = (3 * dpp->samples_B [0] - dpp->samples_B [1]) >> 1; |
309 | 0 | } |
310 | 0 | } |
311 | 0 | else { |
312 | 0 | sam_A = dpp->samples_A [m]; |
313 | 0 | sam_B = dpp->samples_B [m]; |
314 | 0 | } |
315 | |
|
316 | 0 | left_c += apply_weight (dpp->weight_A, sam_A); |
317 | 0 | right_c += apply_weight (dpp->weight_B, sam_B); |
318 | 0 | } |
319 | 0 | else if (dpp->term == -1) { |
320 | 0 | left_c += apply_weight (dpp->weight_A, dpp->samples_A [0]); |
321 | 0 | right_c += apply_weight (dpp->weight_B, left_c); |
322 | 0 | } |
323 | 0 | else { |
324 | 0 | right_c += apply_weight (dpp->weight_B, dpp->samples_B [0]); |
325 | |
|
326 | 0 | if (dpp->term == -3) |
327 | 0 | left_c += apply_weight (dpp->weight_A, dpp->samples_A [0]); |
328 | 0 | else |
329 | 0 | left_c += apply_weight (dpp->weight_A, right_c); |
330 | 0 | } |
331 | 0 | } |
332 | |
|
333 | 0 | if (flags & JOINT_STEREO) |
334 | 0 | left_c += (right_c -= (left_c >> 1)); |
335 | 0 | } |
336 | |
|
337 | 0 | for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++) { |
338 | 0 | int32_t sam_A, sam_B; |
339 | |
|
340 | 0 | if (dpp->term > 0) { |
341 | 0 | int k; |
342 | |
|
343 | 0 | if (dpp->term > MAX_TERM) { |
344 | 0 | if (dpp->term & 1) { |
345 | 0 | sam_A = 2 * dpp->samples_A [0] - dpp->samples_A [1]; |
346 | 0 | sam_B = 2 * dpp->samples_B [0] - dpp->samples_B [1]; |
347 | 0 | } |
348 | 0 | else { |
349 | 0 | sam_A = (3 * dpp->samples_A [0] - dpp->samples_A [1]) >> 1; |
350 | 0 | sam_B = (3 * dpp->samples_B [0] - dpp->samples_B [1]) >> 1; |
351 | 0 | } |
352 | |
|
353 | 0 | dpp->samples_A [1] = dpp->samples_A [0]; |
354 | 0 | dpp->samples_B [1] = dpp->samples_B [0]; |
355 | 0 | k = 0; |
356 | 0 | } |
357 | 0 | else { |
358 | 0 | sam_A = dpp->samples_A [m]; |
359 | 0 | sam_B = dpp->samples_B [m]; |
360 | 0 | k = (m + dpp->term) & (MAX_TERM - 1); |
361 | 0 | } |
362 | |
|
363 | 0 | left2 = apply_weight (dpp->weight_A, sam_A) + left; |
364 | 0 | right2 = apply_weight (dpp->weight_B, sam_B) + right; |
365 | |
|
366 | 0 | update_weight (dpp->weight_A, dpp->delta, sam_A, left); |
367 | 0 | update_weight (dpp->weight_B, dpp->delta, sam_B, right); |
368 | |
|
369 | 0 | dpp->samples_A [k] = left = left2; |
370 | 0 | dpp->samples_B [k] = right = right2; |
371 | 0 | } |
372 | 0 | else if (dpp->term == -1) { |
373 | 0 | left2 = left + apply_weight (dpp->weight_A, dpp->samples_A [0]); |
374 | 0 | update_weight_clip (dpp->weight_A, dpp->delta, dpp->samples_A [0], left); |
375 | 0 | left = left2; |
376 | 0 | right2 = right + apply_weight (dpp->weight_B, left2); |
377 | 0 | update_weight_clip (dpp->weight_B, dpp->delta, left2, right); |
378 | 0 | dpp->samples_A [0] = right = right2; |
379 | 0 | } |
380 | 0 | else { |
381 | 0 | right2 = right + apply_weight (dpp->weight_B, dpp->samples_B [0]); |
382 | 0 | update_weight_clip (dpp->weight_B, dpp->delta, dpp->samples_B [0], right); |
383 | 0 | right = right2; |
384 | |
|
385 | 0 | if (dpp->term == -3) { |
386 | 0 | right2 = dpp->samples_A [0]; |
387 | 0 | dpp->samples_A [0] = right; |
388 | 0 | } |
389 | |
|
390 | 0 | left2 = left + apply_weight (dpp->weight_A, right2); |
391 | 0 | update_weight_clip (dpp->weight_A, dpp->delta, right2, left); |
392 | 0 | dpp->samples_B [0] = left = left2; |
393 | 0 | } |
394 | 0 | } |
395 | |
|
396 | 0 | m = (m + 1) & (MAX_TERM - 1); |
397 | |
|
398 | 0 | if (!(flags & CROSS_DECORR)) { |
399 | 0 | left_c = left + correction [0]; |
400 | 0 | right_c = right + correction [1]; |
401 | |
|
402 | 0 | if (flags & JOINT_STEREO) |
403 | 0 | left_c += (right_c -= (left_c >> 1)); |
404 | 0 | } |
405 | |
|
406 | 0 | if (flags & JOINT_STEREO) |
407 | 0 | left += (right -= (left >> 1)); |
408 | |
|
409 | 0 | if (flags & HYBRID_SHAPE) { |
410 | 0 | int shaping_weight; |
411 | 0 | int32_t temp; |
412 | |
|
413 | 0 | correction [0] = left_c - left; |
414 | 0 | shaping_weight = (wps->dc.shaping_acc [0] += wps->dc.shaping_delta [0]) >> 16; |
415 | 0 | temp = -apply_weight (shaping_weight, wps->dc.error [0]); |
416 | |
|
417 | 0 | if ((flags & NEW_SHAPING) && shaping_weight < 0 && temp) { |
418 | 0 | if (temp == wps->dc.error [0]) |
419 | 0 | temp = (temp < 0) ? temp + 1 : temp - 1; |
420 | |
|
421 | 0 | wps->dc.error [0] = temp - correction [0]; |
422 | 0 | } |
423 | 0 | else |
424 | 0 | wps->dc.error [0] = -correction [0]; |
425 | |
|
426 | 0 | left = left_c - temp; |
427 | 0 | correction [1] = right_c - right; |
428 | 0 | shaping_weight = (wps->dc.shaping_acc [1] += wps->dc.shaping_delta [1]) >> 16; |
429 | 0 | temp = -apply_weight (shaping_weight, wps->dc.error [1]); |
430 | |
|
431 | 0 | if ((flags & NEW_SHAPING) && shaping_weight < 0 && temp) { |
432 | 0 | if (temp == wps->dc.error [1]) |
433 | 0 | temp = (temp < 0) ? temp + 1 : temp - 1; |
434 | |
|
435 | 0 | wps->dc.error [1] = temp - correction [1]; |
436 | 0 | } |
437 | 0 | else |
438 | 0 | wps->dc.error [1] = -correction [1]; |
439 | |
|
440 | 0 | right = right_c - temp; |
441 | 0 | } |
442 | 0 | else { |
443 | 0 | left = left_c; |
444 | 0 | right = right_c; |
445 | 0 | } |
446 | |
|
447 | 0 | if (labs (left) > mute_limit || labs (right) > mute_limit) |
448 | 0 | break; |
449 | | |
450 | 0 | crc += (crc << 3) + ((uint32_t) left << 1) + left + right; |
451 | 0 | *bptr++ = left; |
452 | 0 | *bptr++ = right; |
453 | 0 | } |
454 | 18.4E | else |
455 | 18.4E | i = 0; /* this line can't execute, but suppresses compiler warning */ |
456 | | |
457 | 165k | get_word_eof: |
458 | 165k | if (i != sample_count) { |
459 | 5.06k | memset (buffer, 0, sample_count * (flags & MONO_FLAG ? 4 : 8)); |
460 | 5.06k | wps->mute_error = TRUE; |
461 | 5.06k | i = sample_count; |
462 | | |
463 | 5.06k | if (bs_is_open (&wps->wvxbits)) |
464 | 300 | bs_close_read (&wps->wvxbits); |
465 | 5.06k | } |
466 | | |
467 | 165k | if (m) |
468 | 3.61k | for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++) |
469 | 2.57k | if (dpp->term > 0 && dpp->term <= MAX_TERM) { |
470 | 851 | int32_t temp_A [MAX_TERM], temp_B [MAX_TERM]; |
471 | 851 | int k; |
472 | | |
473 | 851 | memcpy (temp_A, dpp->samples_A, sizeof (dpp->samples_A)); |
474 | 851 | memcpy (temp_B, dpp->samples_B, sizeof (dpp->samples_B)); |
475 | | |
476 | 7.65k | for (k = 0; k < MAX_TERM; k++) { |
477 | 6.80k | dpp->samples_A [k] = temp_A [m]; |
478 | 6.80k | dpp->samples_B [k] = temp_B [m]; |
479 | 6.80k | m = (m + 1) & (MAX_TERM - 1); |
480 | 6.80k | } |
481 | 851 | } |
482 | | |
483 | 165k | fixup_samples (wps, buffer, i); |
484 | | |
485 | 165k | if ((flags & FLOAT_DATA) && (wps->wpc->open_flags & OPEN_NORMALIZE)) |
486 | 51.1k | WavpackFloatNormalize (buffer, (flags & MONO_DATA) ? i : i * 2, |
487 | 51.1k | 127 - wps->float_norm_exp + wps->wpc->norm_offset); |
488 | | |
489 | 165k | if (flags & FALSE_STEREO) { |
490 | 53.1k | int32_t *dptr = buffer + i * 2; |
491 | 53.1k | int32_t *sptr = buffer + i; |
492 | 53.1k | int32_t c = i; |
493 | | |
494 | 57.4M | while (c--) { |
495 | 57.4M | *--dptr = *--sptr; |
496 | 57.4M | *--dptr = *sptr; |
497 | 57.4M | } |
498 | 53.1k | } |
499 | | |
500 | 165k | wps->sample_index += i; |
501 | 165k | wps->crc = crc; |
502 | | |
503 | | // If we just finished this block, then it's time to check if the applicable checksums match. Like |
504 | | // other decoding errors, these are indicated by setting the mute_error flag. We also clear the |
505 | | // buffer to reduce the chances of noise bursts getting through. |
506 | | |
507 | 165k | if (!wps->mute_error && wps->sample_index == GET_BLOCK_INDEX (wps->wphdr) + wps->wphdr.block_samples) { |
508 | 17.1k | if (wps->crc != wps->wphdr.crc || (bs_is_open (&wps->wvxbits) && wps->crc_x != wps->crc_wvx)) { |
509 | 12.9k | memset (buffer, 0, sample_count * (flags & MONO_FLAG ? 4 : 8)); |
510 | 12.9k | wps->mute_error = TRUE; |
511 | 12.9k | } |
512 | 17.1k | } |
513 | | |
514 | 165k | return i; |
515 | 165k | } |
516 | | |
517 | | // General function to perform mono decorrelation pass on specified buffer |
518 | | // (although since this is the reverse function it might technically be called |
519 | | // "correlation" instead). This version handles all sample resolutions and |
520 | | // weight deltas. The dpp->samples_X[] data is returned normalized for term |
521 | | // values 1-8. |
522 | | |
523 | | static void decorr_mono_pass (struct decorr_pass *dpp, int32_t *buffer, int32_t sample_count) |
524 | 25.2k | { |
525 | 25.2k | int32_t delta = dpp->delta, weight_A = dpp->weight_A; |
526 | 25.2k | int32_t *bptr, *eptr = buffer + sample_count, sam_A; |
527 | 25.2k | int m, k; |
528 | | |
529 | 25.2k | switch (dpp->term) { |
530 | | |
531 | 7.13k | case 17: |
532 | 20.4k | for (bptr = buffer; bptr < eptr; bptr++) { |
533 | 13.3k | sam_A = 2 * dpp->samples_A [0] - dpp->samples_A [1]; |
534 | 13.3k | dpp->samples_A [1] = dpp->samples_A [0]; |
535 | 13.3k | dpp->samples_A [0] = apply_weight (weight_A, sam_A) + bptr [0]; |
536 | 13.3k | update_weight (weight_A, delta, sam_A, bptr [0]); |
537 | 13.3k | bptr [0] = dpp->samples_A [0]; |
538 | 13.3k | } |
539 | | |
540 | 7.13k | break; |
541 | | |
542 | 7.24k | case 18: |
543 | 20.7k | for (bptr = buffer; bptr < eptr; bptr++) { |
544 | 13.4k | sam_A = (3 * dpp->samples_A [0] - dpp->samples_A [1]) >> 1; |
545 | 13.4k | dpp->samples_A [1] = dpp->samples_A [0]; |
546 | 13.4k | dpp->samples_A [0] = apply_weight (weight_A, sam_A) + bptr [0]; |
547 | 13.4k | update_weight (weight_A, delta, sam_A, bptr [0]); |
548 | 13.4k | bptr [0] = dpp->samples_A [0]; |
549 | 13.4k | } |
550 | | |
551 | 7.24k | break; |
552 | | |
553 | 10.8k | default: |
554 | 65.7k | for (m = 0, k = dpp->term & (MAX_TERM - 1), bptr = buffer; bptr < eptr; bptr++) { |
555 | 54.8k | sam_A = dpp->samples_A [m]; |
556 | 54.8k | dpp->samples_A [k] = apply_weight (weight_A, sam_A) + bptr [0]; |
557 | 54.8k | update_weight (weight_A, delta, sam_A, bptr [0]); |
558 | 54.8k | bptr [0] = dpp->samples_A [k]; |
559 | 54.8k | m = (m + 1) & (MAX_TERM - 1); |
560 | 54.8k | k = (k + 1) & (MAX_TERM - 1); |
561 | 54.8k | } |
562 | | |
563 | 10.8k | if (m) { |
564 | 9.35k | int32_t temp_samples [MAX_TERM]; |
565 | | |
566 | 9.35k | memcpy (temp_samples, dpp->samples_A, sizeof (dpp->samples_A)); |
567 | | |
568 | 84.1k | for (k = 0; k < MAX_TERM; k++, m++) |
569 | 74.8k | dpp->samples_A [k] = temp_samples [m & (MAX_TERM - 1)]; |
570 | 9.35k | } |
571 | | |
572 | 10.8k | break; |
573 | 25.2k | } |
574 | | |
575 | 25.2k | dpp->weight_A = weight_A; |
576 | 25.2k | } |
577 | | |
578 | | // General function to perform stereo decorrelation pass on specified buffer |
579 | | // (although since this is the reverse function it might technically be called |
580 | | // "correlation" instead). This version handles all sample resolutions and |
581 | | // weight deltas. The dpp->samples_X[] data is *not* returned normalized for |
582 | | // term values 1-8, so it should be normalized if it is going to be used to |
583 | | // call this function again. |
584 | | |
585 | | static void decorr_stereo_pass (struct decorr_pass *dpp, int32_t *buffer, int32_t sample_count) |
586 | 64.6k | { |
587 | 64.6k | int32_t *bptr, *eptr = buffer + (sample_count * 2); |
588 | 64.6k | int m, k; |
589 | | |
590 | 64.6k | switch (dpp->term) { |
591 | 8.41k | case 17: |
592 | 24.6k | for (bptr = buffer; bptr < eptr; bptr += 2) { |
593 | 16.2k | int32_t sam, tmp; |
594 | | |
595 | 16.2k | sam = 2 * dpp->samples_A [0] - dpp->samples_A [1]; |
596 | 16.2k | dpp->samples_A [1] = dpp->samples_A [0]; |
597 | 16.2k | bptr [0] = dpp->samples_A [0] = apply_weight (dpp->weight_A, sam) + (tmp = bptr [0]); |
598 | 16.2k | update_weight (dpp->weight_A, dpp->delta, sam, tmp); |
599 | | |
600 | 16.2k | sam = 2 * dpp->samples_B [0] - dpp->samples_B [1]; |
601 | 16.2k | dpp->samples_B [1] = dpp->samples_B [0]; |
602 | 16.2k | bptr [1] = dpp->samples_B [0] = apply_weight (dpp->weight_B, sam) + (tmp = bptr [1]); |
603 | 16.2k | update_weight (dpp->weight_B, dpp->delta, sam, tmp); |
604 | 16.2k | } |
605 | | |
606 | 8.41k | break; |
607 | | |
608 | 9.71k | case 18: |
609 | 29.2k | for (bptr = buffer; bptr < eptr; bptr += 2) { |
610 | 19.5k | int32_t sam, tmp; |
611 | | |
612 | 19.5k | sam = dpp->samples_A [0] + ((dpp->samples_A [0] - dpp->samples_A [1]) >> 1); |
613 | 19.5k | dpp->samples_A [1] = dpp->samples_A [0]; |
614 | 19.5k | bptr [0] = dpp->samples_A [0] = apply_weight (dpp->weight_A, sam) + (tmp = bptr [0]); |
615 | 19.5k | update_weight (dpp->weight_A, dpp->delta, sam, tmp); |
616 | | |
617 | 19.5k | sam = dpp->samples_B [0] + ((dpp->samples_B [0] - dpp->samples_B [1]) >> 1); |
618 | 19.5k | dpp->samples_B [1] = dpp->samples_B [0]; |
619 | 19.5k | bptr [1] = dpp->samples_B [0] = apply_weight (dpp->weight_B, sam) + (tmp = bptr [1]); |
620 | 19.5k | update_weight (dpp->weight_B, dpp->delta, sam, tmp); |
621 | 19.5k | } |
622 | | |
623 | 9.71k | break; |
624 | | |
625 | 12.2k | default: |
626 | 66.0k | for (m = 0, k = dpp->term & (MAX_TERM - 1), bptr = buffer; bptr < eptr; bptr += 2) { |
627 | 53.8k | int32_t sam; |
628 | | |
629 | 53.8k | sam = dpp->samples_A [m]; |
630 | 53.8k | dpp->samples_A [k] = apply_weight (dpp->weight_A, sam) + bptr [0]; |
631 | 53.8k | update_weight (dpp->weight_A, dpp->delta, sam, bptr [0]); |
632 | 53.8k | bptr [0] = dpp->samples_A [k]; |
633 | | |
634 | 53.8k | sam = dpp->samples_B [m]; |
635 | 53.8k | dpp->samples_B [k] = apply_weight (dpp->weight_B, sam) + bptr [1]; |
636 | 53.8k | update_weight (dpp->weight_B, dpp->delta, sam, bptr [1]); |
637 | 53.8k | bptr [1] = dpp->samples_B [k]; |
638 | | |
639 | 53.8k | m = (m + 1) & (MAX_TERM - 1); |
640 | 53.8k | k = (k + 1) & (MAX_TERM - 1); |
641 | 53.8k | } |
642 | | |
643 | 12.2k | break; |
644 | | |
645 | 9.33k | case -1: |
646 | 26.8k | for (bptr = buffer; bptr < eptr; bptr += 2) { |
647 | 17.5k | int32_t sam; |
648 | | |
649 | 17.5k | sam = bptr [0] + apply_weight (dpp->weight_A, dpp->samples_A [0]); |
650 | 17.5k | update_weight_clip (dpp->weight_A, dpp->delta, dpp->samples_A [0], bptr [0]); |
651 | 17.5k | bptr [0] = sam; |
652 | 17.5k | dpp->samples_A [0] = bptr [1] + apply_weight (dpp->weight_B, sam); |
653 | 17.5k | update_weight_clip (dpp->weight_B, dpp->delta, sam, bptr [1]); |
654 | 17.5k | bptr [1] = dpp->samples_A [0]; |
655 | 17.5k | } |
656 | | |
657 | 9.33k | break; |
658 | | |
659 | 12.0k | case -2: |
660 | 38.5k | for (bptr = buffer; bptr < eptr; bptr += 2) { |
661 | 26.4k | int32_t sam; |
662 | | |
663 | 26.4k | sam = bptr [1] + apply_weight (dpp->weight_B, dpp->samples_B [0]); |
664 | 26.4k | update_weight_clip (dpp->weight_B, dpp->delta, dpp->samples_B [0], bptr [1]); |
665 | 26.4k | bptr [1] = sam; |
666 | 26.4k | dpp->samples_B [0] = bptr [0] + apply_weight (dpp->weight_A, sam); |
667 | 26.4k | update_weight_clip (dpp->weight_A, dpp->delta, sam, bptr [0]); |
668 | 26.4k | bptr [0] = dpp->samples_B [0]; |
669 | 26.4k | } |
670 | | |
671 | 12.0k | break; |
672 | | |
673 | 12.9k | case -3: |
674 | 37.5k | for (bptr = buffer; bptr < eptr; bptr += 2) { |
675 | 24.6k | int32_t sam_A, sam_B; |
676 | | |
677 | 24.6k | sam_A = bptr [0] + apply_weight (dpp->weight_A, dpp->samples_A [0]); |
678 | 24.6k | update_weight_clip (dpp->weight_A, dpp->delta, dpp->samples_A [0], bptr [0]); |
679 | 24.6k | sam_B = bptr [1] + apply_weight (dpp->weight_B, dpp->samples_B [0]); |
680 | 24.6k | update_weight_clip (dpp->weight_B, dpp->delta, dpp->samples_B [0], bptr [1]); |
681 | 24.6k | bptr [0] = dpp->samples_B [0] = sam_A; |
682 | 24.6k | bptr [1] = dpp->samples_A [0] = sam_B; |
683 | 24.6k | } |
684 | | |
685 | 12.9k | break; |
686 | 64.6k | } |
687 | 64.6k | } |
688 | | |
689 | | // This is a helper function for unpack_samples() that applies several final |
690 | | // operations. First, if the data is 32-bit float data, then that conversion |
691 | | // is done in the float.c module (whether lossy or lossless) and we return. |
692 | | // Otherwise, if the extended integer data applies, then that operation is |
693 | | // executed first. If the unpacked data is lossy (and not corrected) then |
694 | | // it is clipped and shifted in a single operation. Otherwise, if it's |
695 | | // lossless then the last step is to apply the final shift (if any). |
696 | | |
697 | | static void fixup_samples (WavpackStream *wps, int32_t *buffer, uint32_t sample_count) |
698 | 165k | { |
699 | 165k | uint32_t flags = wps->wphdr.flags; |
700 | 165k | int lossy_flag = (flags & HYBRID_FLAG) && !wps->block2buff; |
701 | 165k | int shift = (flags & SHIFT_MASK) >> SHIFT_LSB; |
702 | | |
703 | 165k | if (flags & FLOAT_DATA) { |
704 | 51.1k | float_values (wps, buffer, (flags & MONO_DATA) ? sample_count : sample_count * 2); |
705 | 51.1k | return; |
706 | 51.1k | } |
707 | | |
708 | 114k | if (flags & INT32_DATA) { |
709 | 67.9k | uint32_t count = (flags & MONO_DATA) ? sample_count : sample_count * 2; |
710 | 67.9k | int sent_bits = wps->int32_sent_bits & 0x1f, zeros = wps->int32_zeros & 0x1f; |
711 | 67.9k | int ones = wps->int32_ones & 0x1f, dups = wps->int32_dups & 0x1f; |
712 | 67.9k | uint32_t data, mask = (1U << sent_bits) - 1; |
713 | 67.9k | int32_t *dptr = buffer; |
714 | | |
715 | 67.9k | if (bs_is_open (&wps->wvxbits)) { |
716 | 10.1k | int max_width = wps->int32_max_width; |
717 | 10.1k | uint32_t crc = wps->crc_x; |
718 | | |
719 | 14.0M | while (count--) { |
720 | 14.0M | if (sent_bits) { |
721 | 1.82M | if (max_width) { |
722 | 974k | int32_t pvalue = *dptr < 0 ? ~*dptr : *dptr; |
723 | 974k | int width = count_bits (pvalue) + sent_bits; |
724 | 974k | int bits_to_read = sent_bits; |
725 | | |
726 | 974k | if (width <= max_width || (bits_to_read -= width - max_width) > 0) { |
727 | 617k | getbits (&data, bits_to_read, &wps->wvxbits); |
728 | 617k | data &= (1U << bits_to_read) - 1; |
729 | 617k | *dptr = (((uint32_t) *dptr << bits_to_read) | data) << (sent_bits - bits_to_read); |
730 | 617k | } |
731 | 357k | else |
732 | 357k | *dptr = (uint32_t) *dptr << sent_bits; |
733 | 974k | } |
734 | 849k | else { |
735 | 849k | getbits (&data, sent_bits, &wps->wvxbits); |
736 | 849k | *dptr = ((uint32_t) *dptr << sent_bits) | (data & mask); |
737 | 849k | } |
738 | 1.82M | } |
739 | | |
740 | 14.0M | if (zeros) |
741 | 862k | *(uint32_t*)dptr <<= zeros; |
742 | 13.1M | else if (ones) |
743 | 459k | *dptr = ((uint32_t)(*dptr + 1) << ones) - 1; |
744 | 12.7M | else if (dups) |
745 | 419k | *dptr = ((uint32_t)(*dptr + (*dptr & 1)) << dups) - (*dptr & 1); |
746 | | |
747 | 14.0M | crc = crc * 9 + (*dptr & 0xffff) * 3 + ((*dptr >> 16) & 0xffff); |
748 | 14.0M | dptr++; |
749 | 14.0M | } |
750 | | |
751 | 10.1k | wps->crc_x = crc; |
752 | 10.1k | } |
753 | 57.8k | else if (!sent_bits && (zeros + ones + dups)) { |
754 | 9.25k | while (lossy_flag && (flags & BYTES_STORED) == 3 && shift < 8) { |
755 | 6.31k | if (zeros) |
756 | 2.20k | zeros--; |
757 | 4.11k | else if (ones) |
758 | 2.36k | ones--; |
759 | 1.75k | else if (dups) |
760 | 1.24k | dups--; |
761 | 503 | else |
762 | 503 | break; |
763 | | |
764 | 5.81k | shift++; |
765 | 5.81k | } |
766 | | |
767 | 2.83M | while (count--) { |
768 | 2.82M | if (zeros) |
769 | 1.00M | *(uint32_t*)dptr <<= zeros; |
770 | 1.82M | else if (ones) |
771 | 480k | *dptr = ((uint32_t)(*dptr + 1) << ones) - 1; |
772 | 1.34M | else if (dups) |
773 | 743k | *dptr = ((uint32_t)(*dptr + (*dptr & 1)) << dups) - (*dptr & 1); |
774 | | |
775 | 2.82M | dptr++; |
776 | 2.82M | } |
777 | 3.44k | } |
778 | 54.3k | else |
779 | 54.3k | shift += zeros + sent_bits + ones + dups; |
780 | 67.9k | } |
781 | | |
782 | 114k | shift &= 0x1f; |
783 | | |
784 | 114k | if (lossy_flag) { |
785 | 51.0k | int32_t min_value, max_value, min_shifted, max_shifted; |
786 | | |
787 | 51.0k | switch (flags & BYTES_STORED) { |
788 | 13.0k | case 0: |
789 | 13.0k | min_shifted = (uint32_t)(min_value = -128 >> shift) << shift; |
790 | 13.0k | max_shifted = (max_value = 127 >> shift) << shift; |
791 | 13.0k | break; |
792 | | |
793 | 12.1k | case 1: |
794 | 12.1k | min_shifted = (uint32_t)(min_value = -32768 >> shift) << shift; |
795 | 12.1k | max_shifted = (max_value = 32767 >> shift) << shift; |
796 | 12.1k | break; |
797 | | |
798 | 10.3k | case 2: |
799 | 10.3k | min_shifted = (uint32_t)(min_value = -8388608 >> shift) << shift; |
800 | 10.3k | max_shifted = (max_value = 8388607 >> shift) << shift; |
801 | 10.3k | break; |
802 | | |
803 | 15.5k | case 3: default: /* "default" suppresses compiler warning */ |
804 | 15.5k | min_shifted = (uint32_t)(min_value = (int32_t) 0x80000000 >> shift) << shift; |
805 | 15.5k | max_shifted = (max_value = (int32_t) 0x7fffffff >> shift) << shift; |
806 | 15.5k | break; |
807 | 51.0k | } |
808 | | |
809 | 51.0k | if (!(flags & MONO_DATA)) |
810 | 26.5k | sample_count *= 2; |
811 | | |
812 | 110M | while (sample_count--) { |
813 | 110M | if (*buffer < min_value) |
814 | 7.85M | *buffer++ = min_shifted; |
815 | 103M | else if (*buffer > max_value) |
816 | 8.05M | *buffer++ = max_shifted; |
817 | 94.9M | else |
818 | 94.9M | *(uint32_t*)buffer++ <<= shift; |
819 | 110M | } |
820 | 51.0k | } |
821 | 63.4k | else if (shift) { |
822 | 55.6k | if (!(flags & MONO_DATA)) |
823 | 26.1k | sample_count *= 2; |
824 | | |
825 | 79.4M | while (sample_count--) |
826 | 79.3M | *(uint32_t*)buffer++ <<= shift; |
827 | 55.6k | } |
828 | 114k | } |