1 ////////////////////////////////////////////////////////////////////////////
2 //                           **** WAVPACK ****                            //
3 //                  Hybrid Lossless Wavefile Compressor                   //
4 //              Copyright (c) 1998 - 2013 Conifer Software.               //
5 //               MMX optimizations (c) 2006 Joachim Henke                 //
6 //                          All Rights Reserved.                          //
7 //      Distributed under the BSD Software License (see license.txt)      //
8 ////////////////////////////////////////////////////////////////////////////
9 
10 // pack.c
11 
12 // This module actually handles the compression of the audio data, except for
13 // the entropy encoding which is handled by the write_words.c module. For better
14 // efficiency, the conversion is isolated to tight loops that handle an entire
15 // buffer.
16 
17 #include <stdlib.h>
18 #include <string.h>
19 #include <math.h>
20 
21 #include "wavpack_local.h"
22 #include "decorr_tables.h"      // contains data, only include from this module!
23 
24 ///////////////////////////// executable code ////////////////////////////////
25 
26 // This function initializes everything required to pack WavPack bitstreams
27 // and must be called BEFORE any other function in this module.
28 
pack_init(WavpackContext * wpc)29 void pack_init (WavpackContext *wpc)
30 {
31     WavpackStream *wps = wpc->streams [wpc->current_stream];
32 
33     wps->sample_index = 0;
34     wps->delta_decay = 2.0;
35     CLEAR (wps->decorr_passes);
36     CLEAR (wps->dc);
37 
38 #ifdef SKIP_DECORRELATION
39     wpc->config.xmode = 0;
40 #endif
41 
42     /* although we set the term and delta values here for clarity, they're
43      * actually hardcoded in the analysis function for speed
44      */
45 
46     CLEAR (wps->analysis_pass);
47     wps->analysis_pass.term = 18;
48     wps->analysis_pass.delta = 2;
49 
50     if (wpc->config.flags & CONFIG_AUTO_SHAPING) {
51         if (wpc->config.flags & CONFIG_OPTIMIZE_WVC)
52             wps->dc.shaping_acc [0] = wps->dc.shaping_acc [1] = -(512L << 16);
53         else if (wpc->config.sample_rate >= 64000)
54             wps->dc.shaping_acc [0] = wps->dc.shaping_acc [1] = 1024L << 16;
55         else
56             wpc->config.flags |= CONFIG_DYNAMIC_SHAPING;
57     }
58     else {
59         int32_t weight = (int32_t) floor (wpc->config.shaping_weight * 1024.0 + 0.5);
60 
61         if (weight <= -1000)
62             weight = -1000;
63 
64         wps->dc.shaping_acc [0] = wps->dc.shaping_acc [1] = weight << 16;
65     }
66 
67     if (wpc->config.flags & CONFIG_DYNAMIC_SHAPING)
68         wps->dc.shaping_data = malloc (wpc->max_samples * sizeof (*wps->dc.shaping_data));
69 
70     if (!wpc->config.xmode)
71         wps->num_passes = 0;
72     else if (wpc->config.xmode == 1)
73         wps->num_passes = 2;
74     else if (wpc->config.xmode == 2)
75         wps->num_passes = 4;
76     else
77         wps->num_passes = 9;
78 
79     if (wpc->config.flags & CONFIG_VERY_HIGH_FLAG) {
80         wps->num_decorrs = NUM_VERY_HIGH_SPECS;
81         wps->decorr_specs = very_high_specs;
82     }
83     else if (wpc->config.flags & CONFIG_HIGH_FLAG) {
84         wps->num_decorrs = NUM_HIGH_SPECS;
85         wps->decorr_specs = high_specs;
86     }
87     else if (wpc->config.flags & CONFIG_FAST_FLAG) {
88         wps->num_decorrs = NUM_FAST_SPECS;
89         wps->decorr_specs = fast_specs;
90     }
91     else {
92         wps->num_decorrs = NUM_DEFAULT_SPECS;
93         wps->decorr_specs = default_specs;
94     }
95 
96     init_words (wps);
97 }
98 
99 // Allocate room for and copy the decorrelation terms from the decorr_passes
100 // array into the specified metadata structure. Both the actual term id and
101 // the delta are packed into single characters.
102 
write_decorr_terms(WavpackStream * wps,WavpackMetadata * wpmd)103 static void write_decorr_terms (WavpackStream *wps, WavpackMetadata *wpmd)
104 {
105     int tcount = wps->num_terms;
106     struct decorr_pass *dpp;
107     char *byteptr;
108 
109     byteptr = wpmd->data = malloc (tcount + 1);
110     wpmd->id = ID_DECORR_TERMS;
111 
112     for (dpp = wps->decorr_passes; tcount--; ++dpp)
113         *byteptr++ = ((dpp->term + 5) & 0x1f) | ((dpp->delta << 5) & 0xe0);
114 
115     wpmd->byte_length = (int32_t)(byteptr - (char *) wpmd->data);
116 }
117 
118 // Allocate room for and copy the decorrelation term weights from the
119 // decorr_passes array into the specified metadata structure. The weights
120 // range +/-1024, but are rounded and truncated to fit in signed chars for
121 // metadata storage. Weights are separate for the two channels
122 
write_decorr_weights(WavpackStream * wps,WavpackMetadata * wpmd)123 static void write_decorr_weights (WavpackStream *wps, WavpackMetadata *wpmd)
124 {
125     struct decorr_pass *dpp = wps->decorr_passes;
126     int tcount = wps->num_terms, i;
127     char *byteptr;
128 
129     byteptr = wpmd->data = malloc ((tcount * 2) + 1);
130     wpmd->id = ID_DECORR_WEIGHTS;
131 
132     for (i = wps->num_terms - 1; i >= 0; --i)
133         if (store_weight (dpp [i].weight_A) ||
134             (!(wps->wphdr.flags & MONO_DATA) && store_weight (dpp [i].weight_B)))
135                 break;
136 
137     tcount = i + 1;
138 
139     for (i = 0; i < wps->num_terms; ++i) {
140         if (i < tcount) {
141             dpp [i].weight_A = restore_weight (*byteptr++ = store_weight (dpp [i].weight_A));
142 
143             if (!(wps->wphdr.flags & MONO_DATA))
144                 dpp [i].weight_B = restore_weight (*byteptr++ = store_weight (dpp [i].weight_B));
145         }
146         else
147             dpp [i].weight_A = dpp [i].weight_B = 0;
148     }
149 
150     wpmd->byte_length = (int32_t)(byteptr - (char *) wpmd->data);
151 }
152 
153 // Allocate room for and copy the decorrelation samples from the decorr_passes
154 // array into the specified metadata structure. The samples are signed 32-bit
155 // values, but are converted to signed log2 values for storage in metadata.
156 // Values are stored for both channels and are specified from the first term
157 // with unspecified samples set to zero. The number of samples stored varies
158 // with the actual term value, so those must obviously be specified before
159 // these in the metadata list. Any number of terms can have their samples
160 // specified from no terms to all the terms, however I have found that
161 // sending more than the first term's samples is a waste. The "wcount"
162 // variable can be set to the number of terms to have their samples stored.
163 
write_decorr_samples(WavpackStream * wps,WavpackMetadata * wpmd)164 static void write_decorr_samples (WavpackStream *wps, WavpackMetadata *wpmd)
165 {
166     int tcount = wps->num_terms, wcount = 1, temp;
167     struct decorr_pass *dpp;
168     unsigned char *byteptr;
169 
170     byteptr = wpmd->data = malloc (256);
171     wpmd->id = ID_DECORR_SAMPLES;
172 
173     for (dpp = wps->decorr_passes; tcount--; ++dpp)
174         if (wcount) {
175             if (dpp->term > MAX_TERM) {
176                 dpp->samples_A [0] = wp_exp2s (temp = wp_log2s (dpp->samples_A [0]));
177                 *byteptr++ = temp;
178                 *byteptr++ = temp >> 8;
179                 dpp->samples_A [1] = wp_exp2s (temp = wp_log2s (dpp->samples_A [1]));
180                 *byteptr++ = temp;
181                 *byteptr++ = temp >> 8;
182 
183                 if (!(wps->wphdr.flags & MONO_DATA)) {
184                     dpp->samples_B [0] = wp_exp2s (temp = wp_log2s (dpp->samples_B [0]));
185                     *byteptr++ = temp;
186                     *byteptr++ = temp >> 8;
187                     dpp->samples_B [1] = wp_exp2s (temp = wp_log2s (dpp->samples_B [1]));
188                     *byteptr++ = temp;
189                     *byteptr++ = temp >> 8;
190                 }
191             }
192             else if (dpp->term < 0) {
193                 dpp->samples_A [0] = wp_exp2s (temp = wp_log2s (dpp->samples_A [0]));
194                 *byteptr++ = temp;
195                 *byteptr++ = temp >> 8;
196                 dpp->samples_B [0] = wp_exp2s (temp = wp_log2s (dpp->samples_B [0]));
197                 *byteptr++ = temp;
198                 *byteptr++ = temp >> 8;
199             }
200             else {
201                 int m = 0, cnt = dpp->term;
202 
203                 while (cnt--) {
204                     dpp->samples_A [m] = wp_exp2s (temp = wp_log2s (dpp->samples_A [m]));
205                     *byteptr++ = temp;
206                     *byteptr++ = temp >> 8;
207 
208                     if (!(wps->wphdr.flags & MONO_DATA)) {
209                         dpp->samples_B [m] = wp_exp2s (temp = wp_log2s (dpp->samples_B [m]));
210                         *byteptr++ = temp;
211                         *byteptr++ = temp >> 8;
212                     }
213 
214                     m++;
215                 }
216             }
217 
218             wcount--;
219         }
220         else {
221             CLEAR (dpp->samples_A);
222             CLEAR (dpp->samples_B);
223         }
224 
225     wpmd->byte_length = (int32_t)(byteptr - (unsigned char *) wpmd->data);
226 }
227 
228 // Allocate room for and copy the noise shaping info into the specified
229 // metadata structure. These would normally be written to the
230 // "correction" file and are used for lossless reconstruction of
231 // hybrid data. The "delta" parameter is not yet used in encoding as it
232 // will be part of the "quality" mode.
233 
write_shaping_info(WavpackStream * wps,WavpackMetadata * wpmd)234 static void write_shaping_info (WavpackStream *wps, WavpackMetadata *wpmd)
235 {
236     char *byteptr;
237     int temp;
238 
239     byteptr = wpmd->data = malloc (12);
240     wpmd->id = ID_SHAPING_WEIGHTS;
241 
242     wps->dc.error [0] = wp_exp2s (temp = wp_log2s (wps->dc.error [0]));
243     *byteptr++ = temp;
244     *byteptr++ = temp >> 8;
245     wps->dc.shaping_acc [0] = wp_exp2s (temp = wp_log2s (wps->dc.shaping_acc [0]));
246     *byteptr++ = temp;
247     *byteptr++ = temp >> 8;
248 
249     if (!(wps->wphdr.flags & MONO_DATA)) {
250         wps->dc.error [1] = wp_exp2s (temp = wp_log2s (wps->dc.error [1]));
251         *byteptr++ = temp;
252         *byteptr++ = temp >> 8;
253         wps->dc.shaping_acc [1] = wp_exp2s (temp = wp_log2s (wps->dc.shaping_acc [1]));
254         *byteptr++ = temp;
255         *byteptr++ = temp >> 8;
256     }
257 
258     if (wps->dc.shaping_delta [0] | wps->dc.shaping_delta [1]) {
259         wps->dc.shaping_delta [0] = wp_exp2s (temp = wp_log2s (wps->dc.shaping_delta [0]));
260         *byteptr++ = temp;
261         *byteptr++ = temp >> 8;
262 
263         if (!(wps->wphdr.flags & MONO_DATA)) {
264             wps->dc.shaping_delta [1] = wp_exp2s (temp = wp_log2s (wps->dc.shaping_delta [1]));
265             *byteptr++ = temp;
266             *byteptr++ = temp >> 8;
267         }
268     }
269 
270     wpmd->byte_length = (int32_t)(byteptr - (char *) wpmd->data);
271 }
272 
273 // Allocate room for and copy the int32 data values into the specified
274 // metadata structure. This data is used for integer data that has more
275 // than 24 bits of magnitude or, in some cases, it's used to eliminate
276 // redundant bits from any audio stream.
277 
write_int32_info(WavpackStream * wps,WavpackMetadata * wpmd)278 static void write_int32_info (WavpackStream *wps, WavpackMetadata *wpmd)
279 {
280     char *byteptr;
281 
282     byteptr = wpmd->data = malloc (4);
283     wpmd->id = ID_INT32_INFO;
284     *byteptr++ = wps->int32_sent_bits;
285     *byteptr++ = wps->int32_zeros;
286     *byteptr++ = wps->int32_ones;
287     *byteptr++ = wps->int32_dups;
288     wpmd->byte_length = (int32_t)(byteptr - (char *) wpmd->data);
289 }
290 
write_float_info(WavpackStream * wps,WavpackMetadata * wpmd)291 static void write_float_info (WavpackStream *wps, WavpackMetadata *wpmd)
292 {
293     char *byteptr;
294 
295     byteptr = wpmd->data = malloc (4);
296     wpmd->id = ID_FLOAT_INFO;
297     *byteptr++ = wps->float_flags;
298     *byteptr++ = wps->float_shift;
299     *byteptr++ = wps->float_max_exp;
300     *byteptr++ = wps->float_norm_exp;
301     wpmd->byte_length = (int32_t)(byteptr - (char *) wpmd->data);
302 }
303 
304 // Allocate room for and copy the multichannel information into the specified
305 // metadata structure. The first byte is the total number of channels and the
306 // following bytes represent the channel_mask as described for Microsoft
307 // WAVEFORMATEX.
308 
write_channel_info(WavpackContext * wpc,WavpackMetadata * wpmd)309 static void write_channel_info (WavpackContext *wpc, WavpackMetadata *wpmd)
310 {
311     uint32_t mask = wpc->config.channel_mask;
312     char *byteptr = wpmd->data = malloc (8);
313 
314     wpmd->id = ID_CHANNEL_INFO;
315 
316     if (wpc->num_streams > OLD_MAX_STREAMS) {       // if > 8 streams, use 6 or 7 bytes (breaks old decoders
317         *byteptr++ = wpc->config.num_channels - 1;  // that could only handle 8 streams) and allow (in theory)
318         *byteptr++ = wpc->num_streams - 1;          // up to 4096 channels
319         *byteptr++ = (((wpc->num_streams - 1) >> 4) & 0xf0) | (((wpc->config.num_channels - 1) >> 8) & 0xf);
320         *byteptr++ = mask;
321         *byteptr++ = (mask >> 8);
322         *byteptr++ = (mask >> 16);
323 
324         if (mask & 0xff000000)                      // this will break versions < 5.0, but is RF64-specific
325             *byteptr++ = (mask >> 24);
326     }
327     else {                                          // otherwise use only 1 to 5 bytes
328         *byteptr++ = wpc->config.num_channels;
329 
330         while (mask) {
331             *byteptr++ = mask;
332             mask >>= 8;
333         }
334     }
335 
336     wpmd->byte_length = (int32_t)(byteptr - (char *) wpmd->data);
337 }
338 
339 // Allocate room for and copy the multichannel identities into the specified
340 // metadata structure. Data is an array of unsigned characters representing
341 // any channels in the file that DO NOT match one the 18 Microsoft standard
342 // channels (and are represented in the channel mask). A value of 0 is not
343 // allowed and 0xff means an unknown or undefined channel identity.
344 
write_channel_identities_info(WavpackContext * wpc,WavpackMetadata * wpmd)345 static void write_channel_identities_info (WavpackContext *wpc, WavpackMetadata *wpmd)
346 {
347     wpmd->byte_length = (int) strlen ((char *) wpc->channel_identities);
348     wpmd->data = strdup ((char *) wpc->channel_identities);
349     wpmd->id = ID_CHANNEL_IDENTITIES;
350 }
351 
352 // Allocate room for and copy the configuration information into the specified
353 // metadata structure. Currently, we just store the upper 3 bytes of
354 // config.flags and only in the first block of audio data. Note that this is
355 // for informational purposes not required for playback or decoding (like
356 // whether high or fast mode was specified).
357 
write_config_info(WavpackContext * wpc,WavpackMetadata * wpmd)358 static void write_config_info (WavpackContext *wpc, WavpackMetadata *wpmd)
359 {
360     char *byteptr;
361 
362     byteptr = wpmd->data = malloc (8);
363     wpmd->id = ID_CONFIG_BLOCK;
364     *byteptr++ = (char) (wpc->config.flags >> 8);
365     *byteptr++ = (char) (wpc->config.flags >> 16);
366     *byteptr++ = (char) (wpc->config.flags >> 24);
367 
368     if (wpc->config.flags & CONFIG_EXTRA_MODE)
369         *byteptr++ = (char) wpc->config.xmode;
370 
371     // for the 5.0.0 alpha, we wrote the qmode flags here, but this
372     // has been replaced with the new_config block
373     // *byteptr++ = (char) wpc->config.qmode;
374 
375     wpmd->byte_length = (int32_t)(byteptr - (char *) wpmd->data);
376 }
377 
378 // Allocate room for and copy the "new" configuration information into the
379 // specified metadata structure. This is all the stuff introduced with version
380 // 5.0 and includes the qmode flags (big-endian, etc.) and CAF extended
381 // channel layouts (including optional reordering). Even if there is no new
382 // configuration, we still send the empty metadata block to signal a 5.0 file.
383 
write_new_config_info(WavpackContext * wpc,WavpackMetadata * wpmd)384 static void write_new_config_info (WavpackContext *wpc, WavpackMetadata *wpmd)
385 {
386     char *byteptr = wpmd->data = malloc (260);
387 
388     wpmd->id = ID_NEW_CONFIG_BLOCK;
389 
390     if (wpc->file_format || (wpc->config.qmode & 0xff) || wpc->channel_layout) {
391         *byteptr++ = (char) wpc->file_format;
392         *byteptr++ = (char) wpc->config.qmode;
393 
394         if (wpc->channel_layout) {
395             int nchans = wpc->channel_layout & 0xff;
396 
397             *byteptr++ = (char) ((wpc->channel_layout & 0xff0000) >> 16);
398 
399             if (wpc->channel_reordering || nchans != wpc->config.num_channels)
400                 *byteptr++ = (char) nchans;
401 
402             if (wpc->channel_reordering) {
403                 int i, num_to_send = 0;
404 
405                 // to save space, don't send redundant reorder string bytes
406 
407                 for (i = 0; i < nchans; ++i)
408                     if (wpc->channel_reordering [i] != i)
409                         num_to_send = i + 1;
410 
411                 if (num_to_send) {
412                     memcpy (byteptr, wpc->channel_reordering, num_to_send);
413                     byteptr += num_to_send;
414                 }
415             }
416         }
417     }
418 
419     wpmd->byte_length = (int32_t)(byteptr - (char *) wpmd->data);
420 }
421 
422 // Allocate room for and copy the non-standard sampling rate into the specified
423 // metadata structure. We normally store the lower 3 bytes of the sampling rate,
424 // unless 4 bytes are required (introduced in version 5). Note that this would
425 // only be used when the sampling rate was not included in the table of 15
426 // "standard" values.
427 
write_sample_rate(WavpackContext * wpc,WavpackMetadata * wpmd)428 static void write_sample_rate (WavpackContext *wpc, WavpackMetadata *wpmd)
429 {
430     char *byteptr;
431 
432     byteptr = wpmd->data = malloc (4);
433     wpmd->id = ID_SAMPLE_RATE;
434     *byteptr++ = (char) (wpc->config.sample_rate);
435     *byteptr++ = (char) (wpc->config.sample_rate >> 8);
436     *byteptr++ = (char) (wpc->config.sample_rate >> 16);
437 
438     // handle 4-byte sampling rates for scientific applications, etc.
439 
440     if (wpc->config.sample_rate & 0x7f000000)
441         *byteptr++ = (char) (wpc->config.sample_rate >> 24) & 0x7f;
442 
443     wpmd->byte_length = (int32_t)(byteptr - (char *) wpmd->data);
444 }
445 
446 // Pack an entire block of samples (either mono or stereo) into a completed
447 // WavPack block. This function is actually a shell for pack_samples() and
448 // performs tasks like handling any shift required by the format, preprocessing
449 // of floating point data or integer data over 24 bits wide, and implementing
450 // the "extra" mode (via the extra?.c modules). It is assumed that there is
451 // sufficient space for the completed block at "wps->blockbuff" and that
452 // "wps->blockend" points to the end of the available space. A return value of
453 // FALSE indicates an error.
454 
455 static int scan_int32_data (WavpackStream *wps, int32_t *values, int32_t num_values);
456 static void scan_int32_quick (WavpackStream *wps, int32_t *values, int32_t num_values);
457 static void send_int32_data (WavpackStream *wps, int32_t *values, int32_t num_values);
458 static int scan_redundancy (int32_t *values, int32_t num_values);
459 static int pack_samples (WavpackContext *wpc, int32_t *buffer);
460 static void bs_open_write (Bitstream *bs, void *buffer_start, void *buffer_end);
461 static uint32_t bs_close_write (Bitstream *bs);
462 
pack_block(WavpackContext * wpc,int32_t * buffer)463 int pack_block (WavpackContext *wpc, int32_t *buffer)
464 {
465     WavpackStream *wps = wpc->streams [wpc->current_stream];
466     uint32_t flags = wps->wphdr.flags, sflags = wps->wphdr.flags;
467     int32_t sample_count = wps->wphdr.block_samples, *orig_data = NULL;
468     int dynamic_shaping_done = FALSE;
469 
470     // This is done first because this code can potentially change the size of the block about to
471     // be encoded. This can happen because the dynamic noise shaping algorithm wants to send a
472     // shorter block because the desired noise-shaping profile is changing quickly. It can also
473     // be that the --merge-blocks feature wants to create a longer block because it combines areas
474     // with equal redundancy. These are not applicable for anything besides the first stream of
475     // the file and they are not applicable with float data or >24-bit data.
476 
477     if (!wpc->current_stream && !(flags & FLOAT_DATA) && (flags & MAG_MASK) >> MAG_LSB < 24) {
478         if ((wpc->config.flags & CONFIG_DYNAMIC_SHAPING) && !wpc->config.block_samples) {
479             dynamic_noise_shaping (wpc, buffer, TRUE);
480             sample_count = wps->wphdr.block_samples;
481             dynamic_shaping_done = TRUE;
482         }
483         else if (wpc->block_boundary && sample_count >= (int32_t) wpc->block_boundary * 2) {
484             int bc = sample_count / wpc->block_boundary, chans = (flags & MONO_DATA) ? 1 : 2;
485             int res = scan_redundancy (buffer, wpc->block_boundary * chans), i;
486 
487             for (i = 1; i < bc; ++i)
488                 if (res != scan_redundancy (buffer + (i * wpc->block_boundary * chans),
489                     wpc->block_boundary * chans)) {
490                         sample_count = wps->wphdr.block_samples = wpc->block_boundary * i;
491                         break;
492                     }
493         }
494     }
495 
496     // This code scans stereo data to check whether it can be stored as mono data
497     // (i.e., all L/R samples identical). Only available with MAX_STREAM_VERS.
498 
499     if (!(flags & MONO_FLAG) && wpc->stream_version == MAX_STREAM_VERS) {
500         int32_t lor = 0, diff = 0;
501         int32_t *sptr, *dptr, i;
502 
503         for (sptr = buffer, i = 0; i < (int32_t) sample_count; sptr += 2, i++) {
504             lor |= sptr [0] | sptr [1];
505             diff |= sptr [0] ^ sptr [1];
506 
507             if (lor && diff)
508                 break;
509         }
510 
511         if (i == sample_count && lor && !diff) {
512             flags &= ~(JOINT_STEREO | CROSS_DECORR | HYBRID_BALANCE);
513             wps->wphdr.flags = flags |= FALSE_STEREO;
514             dptr = buffer;
515             sptr = buffer;
516 
517             for (i = sample_count; i--; sptr++)
518                 *dptr++ = *sptr++;
519 
520             if (!wps->false_stereo) {
521                 wps->false_stereo = 1;
522                 wps->num_terms = 0;
523                 init_words (wps);
524             }
525         }
526         else if (wps->false_stereo) {
527             wps->false_stereo = 0;
528             wps->num_terms = 0;
529             init_words (wps);
530         }
531     }
532 
533     // This is where we handle any fixed shift which occurs when the integer size does not evenly fit
534     // in bytes (like 12-bit or 20-bit) and is the same for the entire file (not based on scanning)
535 
536     if (flags & SHIFT_MASK) {
537         int shift = (flags & SHIFT_MASK) >> SHIFT_LSB;
538         int mag = (flags & MAG_MASK) >> MAG_LSB;
539         uint32_t cnt = sample_count;
540         int32_t *ptr = buffer;
541 
542         if (flags & MONO_DATA)
543             while (cnt--)
544                 *ptr++ >>= shift;
545         else
546             while (cnt--) {
547                 *ptr++ >>= shift;
548                 *ptr++ >>= shift;
549             }
550 
551         if ((mag -= shift) < 0)
552             flags &= ~MAG_MASK;
553         else
554             flags -= (1 << MAG_LSB) * shift;
555 
556         wps->wphdr.flags = flags;
557     }
558 
559     // The regular WavPack decorrelation and entropy encoding can handle up to 24-bit integer data. If
560     // we have float data or integers larger than 24-bit, then we have to potentially do extra processing.
561     // For lossy encoding, we can simply convert this data in-place to 24-bit data and encode and sent
562     // that, along with some metadata about how to restore the original format (even if the restoration
563     // is not exact). However, for lossless operation we must make a copy of the original data that will
564     // be used to create a "extension stream" that will allow verbatim restoration of the original data.
565     // In the hybrid mode that extension goes in the correction file, otherwise it goes in the mail file.
566 
567     if ((flags & FLOAT_DATA) || (flags & MAG_MASK) >> MAG_LSB >= 24) {      // if float data or >24-bit integers...
568 
569         // if lossless we have to copy the data to use later...
570 
571         if ((!(flags & HYBRID_FLAG) || wpc->wvc_flag) && !(wpc->config.flags & CONFIG_SKIP_WVX)) {
572             orig_data = malloc (sizeof (f32) * ((flags & MONO_DATA) ? sample_count : sample_count * 2));
573             memcpy (orig_data, buffer, sizeof (f32) * ((flags & MONO_DATA) ? sample_count : sample_count * 2));
574 
575             if (flags & FLOAT_DATA) {                                       // if lossless float data come here
576                 wps->float_norm_exp = wpc->config.float_norm_exp;
577 
578                 if (!scan_float_data (wps, (f32 *) buffer, (flags & MONO_DATA) ? sample_count : sample_count * 2)) {
579                     free (orig_data);
580                     orig_data = NULL;
581                 }
582             }
583             else {                                                          // otherwise lossless > 24-bit integers
584                 if (!scan_int32_data (wps, buffer, (flags & MONO_DATA) ? sample_count : sample_count * 2)) {
585                     free (orig_data);
586                     orig_data = NULL;
587                 }
588             }
589         }
590         else {                                                              // otherwise, we're lossy, so no copy
591             if (flags & FLOAT_DATA) {
592                 wps->float_norm_exp = wpc->config.float_norm_exp;
593 
594                 if (scan_float_data (wps, (f32 *) buffer, (flags & MONO_DATA) ? sample_count : sample_count * 2))
595                     wpc->lossy_blocks = TRUE;
596             }
597             else if (scan_int32_data (wps, buffer, (flags & MONO_DATA) ? sample_count : sample_count * 2))
598                 wpc->lossy_blocks = TRUE;
599         }
600 
601         // if there's any chance of magnitude change, clear the noise-shaping error term
602         // and also reset the entropy encoder (which this does)
603 
604         wps->dc.error [0] = wps->dc.error [1] = 0;
605         wps->num_terms = 0;
606     }
607     // if 24-bit integers or less we do a "quick" scan which just scans for redundancy and does NOT set the flag's "magnitude" value
608     else {
609         scan_int32_quick (wps, buffer, (flags & MONO_DATA) ? sample_count : sample_count * 2);
610 
611         if (wps->shift != wps->int32_zeros + wps->int32_ones + wps->int32_dups) {   // detect a change in any redundancy shifting here
612             wps->shift = wps->int32_zeros + wps->int32_ones + wps->int32_dups;
613             wps->dc.error [0] = wps->dc.error [1] = 0;                              // on a change, clear the noise-shaping error term and
614             wps->num_terms = 0;                                                     // also reset the entropy encoder (which this does)
615         }
616     }
617 
618     if ((wpc->config.flags & CONFIG_DYNAMIC_SHAPING) && !dynamic_shaping_done)      // calculate dynamic noise profile
619         dynamic_noise_shaping (wpc, buffer, FALSE);
620 
621     // In some cases we need to start the decorrelation and entropy encoding from scratch. This
622     // could be because we switched from stereo to mono encoding or because the magnitude of
623     // the data changed, or just because this is the first block.
624 
625     if (!wps->num_passes && !wps->num_terms) {
626         wps->num_passes = 1;
627 
628         if (flags & MONO_DATA)
629             execute_mono (wpc, buffer, 1, 0);
630         else
631             execute_stereo (wpc, buffer, 1, 0);
632 
633         wps->num_passes = 0;
634     }
635 
636     // actually pack the block here and return on an error (which pretty much can only be a block buffer overrun)
637 
638     if (!pack_samples (wpc, buffer)) {
639         wps->wphdr.flags = sflags;
640 
641         if (orig_data)
642             free (orig_data);
643 
644         return FALSE;
645     }
646     else
647         wps->wphdr.flags = sflags;
648 
649     // potentially move any unused dynamic noise shaping profile data to use next time
650 
651     if (wps->dc.shaping_data) {
652         if (wps->dc.shaping_samples != sample_count)
653             memmove (wps->dc.shaping_data, wps->dc.shaping_data + sample_count,
654                 (wps->dc.shaping_samples - sample_count) * sizeof (*wps->dc.shaping_data));
655 
656         wps->dc.shaping_samples -= sample_count;
657     }
658 
659     // finally, if we're doing lossless float data or lossless >24-bit integers, this is where we take the
660     // original data that we saved earlier and create the "extension" stream containing the information
661     // required to refine the "lossy" 24-bit data into the lossless original
662 
663     if (orig_data) {
664         uint32_t data_count;
665         unsigned char *cptr;
666 
667         if (wpc->wvc_flag)
668             cptr = wps->block2buff + ((WavpackHeader *) wps->block2buff)->ckSize + 8;
669         else
670             cptr = wps->blockbuff + ((WavpackHeader *) wps->blockbuff)->ckSize + 8;
671 
672         bs_open_write (&wps->wvxbits, cptr + 8, wpc->wvc_flag ? wps->block2end : wps->blockend);
673 
674         if (flags & FLOAT_DATA)
675             send_float_data (wps, (f32*) orig_data, (flags & MONO_DATA) ? sample_count : sample_count * 2);
676         else
677             send_int32_data (wps, orig_data, (flags & MONO_DATA) ? sample_count : sample_count * 2);
678 
679         data_count = bs_close_write (&wps->wvxbits);
680         free (orig_data);
681 
682         if (data_count) {
683             if (data_count != (uint32_t) -1) {
684                 *cptr++ = ID_WVX_BITSTREAM | ID_LARGE;
685                 *cptr++ = (data_count += 4) >> 1;
686                 *cptr++ = data_count >> 9;
687                 *cptr++ = data_count >> 17;
688                 *cptr++ = wps->crc_x;
689                 *cptr++ = wps->crc_x >> 8;
690                 *cptr++ = wps->crc_x >> 16;
691                 *cptr = wps->crc_x >> 24;
692 
693                 if (wpc->wvc_flag)
694                     ((WavpackHeader *) wps->block2buff)->ckSize += data_count + 4;
695                 else
696                     ((WavpackHeader *) wps->blockbuff)->ckSize += data_count + 4;
697             }
698             else
699                 return FALSE;
700         }
701     }
702 
703     return TRUE;
704 }
705 
706 // Quickly scan a buffer of long integer data and determine whether any
707 // redundancy in the LSBs can be used to reduce the data's magnitude. If yes,
708 // then the INT32_DATA flag is set and the int32 parameters are set. This
709 // version is designed to terminate as soon as it figures out that no
710 // redundancy is available so that it can be used for all files.
711 
scan_int32_quick(WavpackStream * wps,int32_t * values,int32_t num_values)712 static void scan_int32_quick (WavpackStream *wps, int32_t *values, int32_t num_values)
713 {
714     uint32_t magdata = 0, ordata = 0, xordata = 0, anddata = ~0;
715     int total_shift = 0;
716     int32_t *dp, count;
717 
718     wps->int32_sent_bits = wps->int32_zeros = wps->int32_ones = wps->int32_dups = 0;
719 
720     for (dp = values, count = num_values; count--; dp++) {
721         magdata |= (*dp < 0) ? ~*dp : *dp;
722         xordata |= *dp ^ -(*dp & 1);
723         anddata &= *dp;
724         ordata |= *dp;
725 
726         if ((ordata & 1) && !(anddata & 1) && (xordata & 2))
727             return;
728     }
729 
730     wps->wphdr.flags &= ~MAG_MASK;
731 
732     while (magdata) {
733         wps->wphdr.flags += 1 << MAG_LSB;
734         magdata >>= 1;
735     }
736 
737     if (!(wps->wphdr.flags & MAG_MASK))
738         return;
739 
740     if (!(ordata & 1))
741         while (!(ordata & 1)) {
742             wps->wphdr.flags -= 1 << MAG_LSB;
743             wps->int32_zeros++;
744             total_shift++;
745             ordata >>= 1;
746         }
747     else if (anddata & 1)
748         while (anddata & 1) {
749             wps->wphdr.flags -= 1 << MAG_LSB;
750             wps->int32_ones++;
751             total_shift++;
752             anddata >>= 1;
753         }
754     else if (!(xordata & 2))
755         while (!(xordata & 2)) {
756             wps->wphdr.flags -= 1 << MAG_LSB;
757             wps->int32_dups++;
758             total_shift++;
759             xordata >>= 1;
760         }
761 
762     if (total_shift) {
763         wps->wphdr.flags |= INT32_DATA;
764 
765         for (dp = values, count = num_values; count--; dp++)
766             *dp >>= total_shift;
767     }
768 }
769 
scan_redundancy(int32_t * values,int32_t num_values)770 static int scan_redundancy (int32_t *values, int32_t num_values)
771 {
772     uint32_t ordata = 0, xordata = 0, anddata = ~0;
773     int redundant_bits = 0;
774     int32_t *dp, count;
775 
776     for (dp = values, count = num_values; count--; dp++) {
777         xordata |= *dp ^ -(*dp & 1);
778         anddata &= *dp;
779         ordata |= *dp;
780 
781         if ((ordata & 1) && !(anddata & 1) && (xordata & 2))
782             return 0;
783     }
784 
785     if (!ordata || anddata == ~0 || !xordata)
786         return 0;
787 
788     if (!(ordata & 1))
789         while (!(ordata & 1)) {
790             redundant_bits++;
791             ordata >>= 1;
792         }
793     else if (anddata & 1)
794         while (anddata & 1) {
795             redundant_bits = (redundant_bits + 1) | 0x40;
796             anddata >>= 1;
797         }
798     else if (!(xordata & 2))
799         while (!(xordata & 2)) {
800             redundant_bits = (redundant_bits + 1) | 0x80;
801             redundant_bits++;
802             xordata >>= 1;
803         }
804 
805     return redundant_bits;
806 }
807 
808 // Scan a buffer of long integer data and determine whether any redundancy in
809 // the LSBs can be used to reduce the data's magnitude. If yes, then the
810 // INT32_DATA flag is set and the int32 parameters are set. If bits must still
811 // be transmitted literally to get down to 24 bits (which is all the integer
812 // compression code can handle) then we return TRUE to indicate that a wvx
813 // stream must be created in either lossless mode.
814 
scan_int32_data(WavpackStream * wps,int32_t * values,int32_t num_values)815 static int scan_int32_data (WavpackStream *wps, int32_t *values, int32_t num_values)
816 {
817     uint32_t magdata = 0, ordata = 0, xordata = 0, anddata = ~0;
818     uint32_t crc = 0xffffffff;
819     int total_shift = 0;
820     int32_t *dp, count;
821 
822     wps->int32_sent_bits = wps->int32_zeros = wps->int32_ones = wps->int32_dups = 0;
823 
824     for (dp = values, count = num_values; count--; dp++) {
825         crc = crc * 9 + (*dp & 0xffff) * 3 + ((*dp >> 16) & 0xffff);
826         magdata |= (*dp < 0) ? ~*dp : *dp;
827         xordata |= *dp ^ -(*dp & 1);
828         anddata &= *dp;
829         ordata |= *dp;
830     }
831 
832     wps->crc_x = crc;
833     wps->wphdr.flags &= ~MAG_MASK;
834 
835     while (magdata) {
836         wps->wphdr.flags += 1 << MAG_LSB;
837         magdata >>= 1;
838     }
839 
840     if (!((wps->wphdr.flags & MAG_MASK) >> MAG_LSB)) {
841         wps->wphdr.flags &= ~INT32_DATA;
842         return FALSE;
843     }
844 
845     if (!(ordata & 1))
846         while (!(ordata & 1)) {
847             wps->wphdr.flags -= 1 << MAG_LSB;
848             wps->int32_zeros++;
849             total_shift++;
850             ordata >>= 1;
851         }
852     else if (anddata & 1)
853         while (anddata & 1) {
854             wps->wphdr.flags -= 1 << MAG_LSB;
855             wps->int32_ones++;
856             total_shift++;
857             anddata >>= 1;
858         }
859     else if (!(xordata & 2))
860         while (!(xordata & 2)) {
861             wps->wphdr.flags -= 1 << MAG_LSB;
862             wps->int32_dups++;
863             total_shift++;
864             xordata >>= 1;
865         }
866 
867     if (((wps->wphdr.flags & MAG_MASK) >> MAG_LSB) > 23) {
868         wps->int32_sent_bits = (unsigned char)(((wps->wphdr.flags & MAG_MASK) >> MAG_LSB) - 23);
869         total_shift += wps->int32_sent_bits;
870         wps->wphdr.flags &= ~MAG_MASK;
871         wps->wphdr.flags += 23 << MAG_LSB;
872     }
873 
874     if (total_shift) {
875         wps->wphdr.flags |= INT32_DATA;
876 
877         for (dp = values, count = num_values; count--; dp++)
878             *dp >>= total_shift;
879     }
880 
881     return wps->int32_sent_bits;
882 }
883 
884 // For the specified buffer values and the int32 parameters stored in "wps",
885 // send the literal bits required to the "wvxbits" bitstream.
886 
send_int32_data(WavpackStream * wps,int32_t * values,int32_t num_values)887 static void send_int32_data (WavpackStream *wps, int32_t *values, int32_t num_values)
888 {
889     int sent_bits = wps->int32_sent_bits, pre_shift;
890     int32_t mask = (1 << sent_bits) - 1;
891     int32_t count, value, *dp;
892 
893     pre_shift = wps->int32_zeros + wps->int32_ones + wps->int32_dups;
894 
895     if (sent_bits)
896         for (dp = values, count = num_values; count--; dp++) {
897             value = (*dp >> pre_shift) & mask;
898             putbits (value, sent_bits, &wps->wvxbits);
899         }
900 }
901 
send_general_metadata(WavpackContext * wpc)902 void send_general_metadata (WavpackContext *wpc)
903 {
904     WavpackStream *wps = wpc->streams [wpc->current_stream];
905     uint32_t flags = wps->wphdr.flags;
906     WavpackMetadata wpmd;
907 
908     if ((flags & SRATE_MASK) == SRATE_MASK && wpc->config.sample_rate != 44100) {
909         write_sample_rate (wpc, &wpmd);
910         copy_metadata (&wpmd, wps->blockbuff, wps->blockend);
911         free_metadata (&wpmd);
912     }
913 
914     if ((flags & INITIAL_BLOCK) &&
915         (wpc->config.num_channels > 2 ||
916         wpc->config.channel_mask != 0x5 - wpc->config.num_channels)) {
917             write_channel_info (wpc, &wpmd);
918             copy_metadata (&wpmd, wps->blockbuff, wps->blockend);
919             free_metadata (&wpmd);
920 
921             if (wpc->channel_identities) {
922                 write_channel_identities_info (wpc, &wpmd);
923                 copy_metadata (&wpmd, wps->blockbuff, wps->blockend);
924                 free_metadata (&wpmd);
925             }
926     }
927 
928     if ((flags & INITIAL_BLOCK) && !wps->sample_index) {
929         write_config_info (wpc, &wpmd);
930         copy_metadata (&wpmd, wps->blockbuff, wps->blockend);
931         free_metadata (&wpmd);
932     }
933 
934     if (flags & INITIAL_BLOCK) {
935         write_new_config_info (wpc, &wpmd);
936         copy_metadata (&wpmd, wps->blockbuff, wps->blockend);
937         free_metadata (&wpmd);
938     }
939 }
940 
941 // Pack an entire block of samples (either mono or stereo) into a completed
942 // WavPack block. It is assumed that there is sufficient space for the
943 // completed block at "wps->blockbuff" and that "wps->blockend" points to the
944 // end of the available space. A return value of FALSE indicates an error.
945 // Any unsent metadata is transmitted first, then required metadata for this
946 // block is sent, and finally the compressed integer data is sent. If a "wpx"
947 // stream is required for floating point data or large integer data, then this
948 // must be handled outside this function. To find out how much data was written
949 // the caller must look at the ckSize field of the written WavpackHeader, NOT
950 // the one in the WavpackStream.
951 
952 #ifdef OPT_ASM_X86
953     #define DECORR_STEREO_PASS(a,b,c) do {              \
954         if (pack_cpu_has_feature_x86 (CPU_FEATURE_MMX)) \
955             pack_decorr_stereo_pass_x86 (a, b, c);      \
956         else decorr_stereo_pass (a, b, c); } while (0)
957     #define DECORR_MONO_BUFFER pack_decorr_mono_buffer_x86
958     #define SCAN_MAX_MAGNITUDE(a,b)                     \
959         (pack_cpu_has_feature_x86 (CPU_FEATURE_MMX) ?   \
960             scan_max_magnitude_x86 (a, b) :             \
961             scan_max_magnitude (a, b))
962 #elif defined(OPT_ASM_X64) && (defined (_WIN64) || defined(__CYGWIN__) || defined(__MINGW64__) || defined(__midipix__))
963     #define DECORR_STEREO_PASS pack_decorr_stereo_pass_x64win
964     #define DECORR_MONO_BUFFER pack_decorr_mono_buffer_x64win
965     #define SCAN_MAX_MAGNITUDE scan_max_magnitude_x64win
966 #elif defined(OPT_ASM_X64)
967     #define DECORR_STEREO_PASS pack_decorr_stereo_pass_x64
968     #define DECORR_MONO_BUFFER pack_decorr_mono_buffer_x64
969     #define SCAN_MAX_MAGNITUDE scan_max_magnitude_x64
970 #else
971     #define DECORR_STEREO_PASS decorr_stereo_pass
972     #define DECORR_MONO_BUFFER decorr_mono_buffer
973     #define SCAN_MAX_MAGNITUDE scan_max_magnitude
974 #endif
975 
976 uint32_t DECORR_MONO_BUFFER (int32_t *buffer, struct decorr_pass *decorr_passes, int32_t num_terms, int32_t sample_count);
977 
978 #ifdef OPT_ASM_X86
979 void decorr_stereo_pass (struct decorr_pass *dpp, int32_t *buffer, int32_t sample_count);
980 void pack_decorr_stereo_pass_x86 (struct decorr_pass *dpp, int32_t *buffer, int32_t sample_count);
981 uint32_t scan_max_magnitude (int32_t *values, int32_t num_values);
982 uint32_t scan_max_magnitude_x86 (int32_t *values, int32_t num_values);
983 #else
984 void DECORR_STEREO_PASS (struct decorr_pass *dpp, int32_t *buffer, int32_t sample_count);
985 uint32_t SCAN_MAX_MAGNITUDE (int32_t *values, int32_t num_values);
986 #endif
987 
988 // This macro controls the "repack" function where a block of samples will be repacked with
989 // fewer terms if a single residual exceeds the specified magnitude threshold.
990 
991 #define REPACK_SAFE_NUM_TERMS 5                 // 5 terms is always okay (and we truncate to this)
992 
pack_samples(WavpackContext * wpc,int32_t * buffer)993 static int pack_samples (WavpackContext *wpc, int32_t *buffer)
994 {
995     WavpackStream *wps = wpc->streams [wpc->current_stream], saved_stream;
996     uint32_t flags = wps->wphdr.flags, repack_possible, data_count, crc, crc2, i;
997     uint32_t sample_count = wps->wphdr.block_samples, repack_mask;
998     int32_t *bptr, *saved_buffer = NULL;
999     struct decorr_pass *dpp;
1000     WavpackMetadata wpmd;
1001 
1002     crc = crc2 = 0xffffffff;
1003 
1004     if (!(flags & HYBRID_FLAG) && (flags & MONO_DATA)) {
1005         int32_t *eptr = buffer + sample_count;
1006 
1007         for (bptr = buffer; bptr < eptr;)
1008             crc += (crc << 1) + *bptr++;
1009 
1010         if (wps->num_passes)
1011             execute_mono (wpc, buffer, !wps->num_terms, 1);
1012     }
1013     else if (!(flags & HYBRID_FLAG) && !(flags & MONO_DATA)) {
1014         int32_t *eptr = buffer + (sample_count * 2);
1015 
1016         for (bptr = buffer; bptr < eptr; bptr += 2)
1017             crc += (crc << 3) + ((uint32_t)bptr [0] << 1) + bptr [0] + bptr [1];
1018 
1019         if (wps->num_passes) {
1020             execute_stereo (wpc, buffer, !wps->num_terms, 1);
1021             flags = wps->wphdr.flags;
1022         }
1023     }
1024     else if ((flags & HYBRID_FLAG) && (flags & MONO_DATA)) {
1025         if (wps->num_passes)
1026             execute_mono (wpc, buffer, !wps->num_terms, 0);
1027     }
1028     else if ((flags & HYBRID_FLAG) && !(flags & MONO_DATA)) {
1029         if (wps->num_passes) {
1030             execute_stereo (wpc, buffer, !wps->num_terms, 0);
1031             flags = wps->wphdr.flags;
1032         }
1033     }
1034 
1035     wps->wphdr.ckSize = sizeof (WavpackHeader) - 8;
1036     memcpy (wps->blockbuff, &wps->wphdr, sizeof (WavpackHeader));
1037 
1038     if (wpc->metacount) {
1039         WavpackMetadata *wpmdp = wpc->metadata;
1040 
1041         while (wpc->metacount) {
1042             copy_metadata (wpmdp, wps->blockbuff, wps->blockend);
1043             wpc->metabytes -= wpmdp->byte_length;
1044             free_metadata (wpmdp++);
1045             wpc->metacount--;
1046         }
1047 
1048         free (wpc->metadata);
1049         wpc->metadata = NULL;
1050     }
1051 
1052     if (!sample_count)
1053         return TRUE;
1054 
1055     memcpy (&wps->wphdr, wps->blockbuff, sizeof (WavpackHeader));
1056     repack_possible = !wps->num_passes && wps->num_terms > REPACK_SAFE_NUM_TERMS;
1057     repack_mask = (flags & MAG_MASK) >> MAG_LSB >= 16 ? 0xF0000000 : 0xFFF00000;
1058     saved_stream = *wps;
1059 
1060     if (repack_possible && !(flags & HYBRID_FLAG)) {
1061         saved_buffer = malloc (sample_count * sizeof (int32_t) * (flags & MONO_DATA ? 1 : 2));
1062         memcpy (saved_buffer, buffer, sample_count * sizeof (int32_t) * (flags & MONO_DATA ? 1 : 2));
1063     }
1064 
1065     // This code is written as a loop, but in the overwhelming majority of cases it executes only once.
1066     // If one of the higher modes is being used and a residual exceeds a certain threshold, then the
1067     // block will be repacked using fewer decorrelation terms. Note that this has only been triggered
1068     // by pathological audio samples designed to trigger it...in practice this might never happen. Note
1069     // that this only applies to the "high" and "very high" modes and only when packing directly
1070     // (i.e. without the "extra" modes that will have already checked magnitude).
1071 
1072     do {
1073         short *shaping_array = wps->dc.shaping_array;
1074         int tcount, lossy = FALSE, m = 0;
1075         double noise_acc = 0.0, noise;
1076         uint32_t max_magnitude = 0;
1077 
1078         write_decorr_terms (wps, &wpmd);
1079         copy_metadata (&wpmd, wps->blockbuff, wps->blockend);
1080         free_metadata (&wpmd);
1081 
1082         write_decorr_weights (wps, &wpmd);
1083         copy_metadata (&wpmd, wps->blockbuff, wps->blockend);
1084         free_metadata (&wpmd);
1085 
1086         write_decorr_samples (wps, &wpmd);
1087         copy_metadata (&wpmd, wps->blockbuff, wps->blockend);
1088         free_metadata (&wpmd);
1089 
1090         write_entropy_vars (wps, &wpmd);
1091         copy_metadata (&wpmd, wps->blockbuff, wps->blockend);
1092         free_metadata (&wpmd);
1093 
1094         if (flags & HYBRID_FLAG) {
1095             write_hybrid_profile (wps, &wpmd);
1096             copy_metadata (&wpmd, wps->blockbuff, wps->blockend);
1097             free_metadata (&wpmd);
1098         }
1099 
1100         if (flags & FLOAT_DATA) {
1101             write_float_info (wps, &wpmd);
1102             copy_metadata (&wpmd, wps->blockbuff, wps->blockend);
1103             free_metadata (&wpmd);
1104         }
1105 
1106         if (flags & INT32_DATA) {
1107             write_int32_info (wps, &wpmd);
1108             copy_metadata (&wpmd, wps->blockbuff, wps->blockend);
1109             free_metadata (&wpmd);
1110         }
1111 
1112         send_general_metadata (wpc);
1113         bs_open_write (&wps->wvbits, wps->blockbuff + ((WavpackHeader *) wps->blockbuff)->ckSize + 12, wps->blockend);
1114 
1115         if (wpc->wvc_flag) {
1116             wps->wphdr.ckSize = sizeof (WavpackHeader) - 8;
1117             memcpy (wps->block2buff, &wps->wphdr, sizeof (WavpackHeader));
1118 
1119             if (flags & HYBRID_SHAPE) {
1120                 write_shaping_info (wps, &wpmd);
1121                 copy_metadata (&wpmd, wps->block2buff, wps->block2end);
1122                 free_metadata (&wpmd);
1123             }
1124 
1125             bs_open_write (&wps->wvcbits, wps->block2buff + ((WavpackHeader *) wps->block2buff)->ckSize + 12, wps->block2end);
1126         }
1127 
1128         /////////////////////// handle lossless mono mode /////////////////////////
1129 
1130         if (!(flags & HYBRID_FLAG) && (flags & MONO_DATA)) {
1131             if (!wps->num_passes) {
1132                 max_magnitude = DECORR_MONO_BUFFER (buffer, wps->decorr_passes, wps->num_terms, sample_count);
1133                 m = sample_count & (MAX_TERM - 1);
1134             }
1135 
1136             send_words_lossless (wps, buffer, sample_count);
1137         }
1138 
1139         //////////////////// handle the lossless stereo mode //////////////////////
1140 
1141         else if (!(flags & HYBRID_FLAG) && !(flags & MONO_DATA)) {
1142             if (!wps->num_passes) {
1143                 if (flags & JOINT_STEREO) {
1144                     int32_t *eptr = buffer + (sample_count * 2);
1145 
1146                     for (bptr = buffer; bptr < eptr; bptr += 2)
1147                         bptr [1] += ((bptr [0] -= bptr [1]) >> 1);
1148                 }
1149 
1150                 for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount-- ; dpp++)
1151                     DECORR_STEREO_PASS (dpp, buffer, sample_count);
1152 
1153                 m = sample_count & (MAX_TERM - 1);
1154 
1155                 if (repack_possible)
1156                     max_magnitude = SCAN_MAX_MAGNITUDE (buffer, sample_count * 2);
1157             }
1158 
1159             send_words_lossless (wps, buffer, sample_count);
1160         }
1161 
1162         /////////////////// handle the lossy/hybrid mono mode /////////////////////
1163 
1164         else if ((flags & HYBRID_FLAG) && (flags & MONO_DATA))
1165             for (bptr = buffer, i = 0; i < sample_count; ++i) {
1166                 int32_t code, temp;
1167                 int shaping_weight;
1168 
1169                 crc2 += (crc2 << 1) + (code = *bptr++);
1170 
1171                 if (flags & HYBRID_SHAPE) {
1172                     if (shaping_array)
1173                         shaping_weight = *shaping_array++;
1174                     else
1175                         shaping_weight = (wps->dc.shaping_acc [0] += wps->dc.shaping_delta [0]) >> 16;
1176 
1177                     temp = -apply_weight (shaping_weight, wps->dc.error [0]);
1178 
1179                     if ((flags & NEW_SHAPING) && shaping_weight < 0 && temp) {
1180                         if (temp == wps->dc.error [0])
1181                             temp = (temp < 0) ? temp + 1 : temp - 1;
1182 
1183                         wps->dc.error [0] = -code;
1184                         code += temp;
1185                     }
1186                     else
1187                         wps->dc.error [0] = -(code += temp);
1188                 }
1189 
1190                 for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount-- ; dpp++)
1191                     if (dpp->term > MAX_TERM) {
1192                         if (dpp->term & 1)
1193                             dpp->samples_A [2] = 2 * dpp->samples_A [0] - dpp->samples_A [1];
1194                         else
1195                             dpp->samples_A [2] = (3 * dpp->samples_A [0] - dpp->samples_A [1]) >> 1;
1196 
1197                         code -= (dpp->aweight_A = apply_weight (dpp->weight_A, dpp->samples_A [2]));
1198                     }
1199                     else
1200                         code -= (dpp->aweight_A = apply_weight (dpp->weight_A, dpp->samples_A [m]));
1201 
1202                 max_magnitude |= (code < 0 ? ~code : code);
1203                 code = send_word (wps, code, 0);
1204 
1205                 while (--dpp >= wps->decorr_passes) {
1206                     if (dpp->term > MAX_TERM) {
1207                         update_weight (dpp->weight_A, dpp->delta, dpp->samples_A [2], code);
1208                         dpp->samples_A [1] = dpp->samples_A [0];
1209                         dpp->samples_A [0] = (code += dpp->aweight_A);
1210                     }
1211                     else {
1212                         int32_t sam = dpp->samples_A [m];
1213 
1214                         update_weight (dpp->weight_A, dpp->delta, sam, code);
1215                         dpp->samples_A [(m + dpp->term) & (MAX_TERM - 1)] = (code += dpp->aweight_A);
1216                     }
1217                 }
1218 
1219                 wps->dc.error [0] += code;
1220                 m = (m + 1) & (MAX_TERM - 1);
1221 
1222                 if ((crc += (crc << 1) + code) != crc2)
1223                     lossy = TRUE;
1224 
1225                 if (wpc->config.flags & CONFIG_CALC_NOISE) {
1226                     noise = code - bptr [-1];
1227 
1228                     noise_acc += noise *= noise;
1229                     wps->dc.noise_ave = (wps->dc.noise_ave * 0.99) + (noise * 0.01);
1230 
1231                     if (wps->dc.noise_ave > wps->dc.noise_max)
1232                         wps->dc.noise_max = wps->dc.noise_ave;
1233                 }
1234             }
1235 
1236         /////////////////// handle the lossy/hybrid stereo mode ///////////////////
1237 
1238         else if ((flags & HYBRID_FLAG) && !(flags & MONO_DATA))
1239             for (bptr = buffer, i = 0; i < sample_count; ++i) {
1240                 int32_t left, right, temp;
1241                 int shaping_weight;
1242 
1243                 left = *bptr++;
1244                 crc2 += (crc2 << 3) + ((uint32_t)left << 1) + left + (right = *bptr++);
1245 
1246                 if (flags & HYBRID_SHAPE) {
1247                     if (shaping_array)
1248                         shaping_weight = *shaping_array++;
1249                     else
1250                         shaping_weight = (wps->dc.shaping_acc [0] += wps->dc.shaping_delta [0]) >> 16;
1251 
1252                     temp = -apply_weight (shaping_weight, wps->dc.error [0]);
1253 
1254                     if ((flags & NEW_SHAPING) && shaping_weight < 0 && temp) {
1255                         if (temp == wps->dc.error [0])
1256                             temp = (temp < 0) ? temp + 1 : temp - 1;
1257 
1258                         wps->dc.error [0] = -left;
1259                         left += temp;
1260                     }
1261                     else
1262                         wps->dc.error [0] = -(left += temp);
1263 
1264                     if (!shaping_array)
1265                         shaping_weight = (wps->dc.shaping_acc [1] += wps->dc.shaping_delta [1]) >> 16;
1266 
1267                     temp = -apply_weight (shaping_weight, wps->dc.error [1]);
1268 
1269                     if ((flags & NEW_SHAPING) && shaping_weight < 0 && temp) {
1270                         if (temp == wps->dc.error [1])
1271                             temp = (temp < 0) ? temp + 1 : temp - 1;
1272 
1273                         wps->dc.error [1] = -right;
1274                         right += temp;
1275                     }
1276                     else
1277                         wps->dc.error [1] = -(right += temp);
1278                 }
1279 
1280                 if (flags & JOINT_STEREO)
1281                     right += ((left -= right) >> 1);
1282 
1283                 for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount-- ; dpp++)
1284                     if (dpp->term > MAX_TERM) {
1285                         if (dpp->term & 1) {
1286                             dpp->samples_A [2] = 2 * dpp->samples_A [0] - dpp->samples_A [1];
1287                             dpp->samples_B [2] = 2 * dpp->samples_B [0] - dpp->samples_B [1];
1288                         }
1289                         else {
1290                             dpp->samples_A [2] = (3 * dpp->samples_A [0] - dpp->samples_A [1]) >> 1;
1291                             dpp->samples_B [2] = (3 * dpp->samples_B [0] - dpp->samples_B [1]) >> 1;
1292                         }
1293 
1294                         left -= (dpp->aweight_A = apply_weight (dpp->weight_A, dpp->samples_A [2]));
1295                         right -= (dpp->aweight_B = apply_weight (dpp->weight_B, dpp->samples_B [2]));
1296                     }
1297                     else if (dpp->term > 0) {
1298                         left -= (dpp->aweight_A = apply_weight (dpp->weight_A, dpp->samples_A [m]));
1299                         right -= (dpp->aweight_B = apply_weight (dpp->weight_B, dpp->samples_B [m]));
1300                     }
1301                     else {
1302                         if (dpp->term == -1)
1303                             dpp->samples_B [0] = left;
1304                         else if (dpp->term == -2)
1305                             dpp->samples_A [0] = right;
1306 
1307                         left -= (dpp->aweight_A = apply_weight (dpp->weight_A, dpp->samples_A [0]));
1308                         right -= (dpp->aweight_B = apply_weight (dpp->weight_B, dpp->samples_B [0]));
1309                     }
1310 
1311                 max_magnitude |= (left < 0 ? ~left : left) | (right < 0 ? ~right : right);
1312                 left = send_word (wps, left, 0);
1313                 right = send_word (wps, right, 1);
1314 
1315                 while (--dpp >= wps->decorr_passes)
1316                     if (dpp->term > MAX_TERM) {
1317                         update_weight (dpp->weight_A, dpp->delta, dpp->samples_A [2], left);
1318                         update_weight (dpp->weight_B, dpp->delta, dpp->samples_B [2], right);
1319 
1320                         dpp->samples_A [1] = dpp->samples_A [0];
1321                         dpp->samples_B [1] = dpp->samples_B [0];
1322 
1323                         dpp->samples_A [0] = (left += dpp->aweight_A);
1324                         dpp->samples_B [0] = (right += dpp->aweight_B);
1325                     }
1326                     else if (dpp->term > 0) {
1327                         int k = (m + dpp->term) & (MAX_TERM - 1);
1328 
1329                         update_weight (dpp->weight_A, dpp->delta, dpp->samples_A [m], left);
1330                         dpp->samples_A [k] = (left += dpp->aweight_A);
1331 
1332                         update_weight (dpp->weight_B, dpp->delta, dpp->samples_B [m], right);
1333                         dpp->samples_B [k] = (right += dpp->aweight_B);
1334                     }
1335                     else {
1336                         if (dpp->term == -1) {
1337                             dpp->samples_B [0] = left + dpp->aweight_A;
1338                             dpp->aweight_B = apply_weight (dpp->weight_B, dpp->samples_B [0]);
1339                         }
1340                         else if (dpp->term == -2) {
1341                             dpp->samples_A [0] = right + dpp->aweight_B;
1342                             dpp->aweight_A = apply_weight (dpp->weight_A, dpp->samples_A [0]);
1343                         }
1344 
1345                         update_weight_clip (dpp->weight_A, dpp->delta, dpp->samples_A [0], left);
1346                         update_weight_clip (dpp->weight_B, dpp->delta, dpp->samples_B [0], right);
1347                         dpp->samples_B [0] = (left += dpp->aweight_A);
1348                         dpp->samples_A [0] = (right += dpp->aweight_B);
1349                     }
1350 
1351                 if (flags & JOINT_STEREO)
1352                     left += (right -= (left >> 1));
1353 
1354                 wps->dc.error [0] += left;
1355                 wps->dc.error [1] += right;
1356                 m = (m + 1) & (MAX_TERM - 1);
1357 
1358                 if ((crc += (crc << 3) + ((uint32_t)left << 1) + left + right) != crc2)
1359                     lossy = TRUE;
1360 
1361                 if (wpc->config.flags & CONFIG_CALC_NOISE) {
1362                     noise = (double)(left - bptr [-2]) * (left - bptr [-2]);
1363                     noise += (double)(right - bptr [-1]) * (right - bptr [-1]);
1364 
1365                     noise_acc += noise /= 2.0;
1366                     wps->dc.noise_ave = (wps->dc.noise_ave * 0.99) + (noise * 0.01);
1367 
1368                     if (wps->dc.noise_ave > wps->dc.noise_max)
1369                         wps->dc.noise_max = wps->dc.noise_ave;
1370                 }
1371             }
1372 
1373         if (m)
1374             for (tcount = wps->num_terms, dpp = wps->decorr_passes; tcount--; dpp++)
1375                 if (dpp->term > 0 && dpp->term <= MAX_TERM) {
1376                     int32_t temp_A [MAX_TERM], temp_B [MAX_TERM];
1377                     int k;
1378 
1379                     memcpy (temp_A, dpp->samples_A, sizeof (dpp->samples_A));
1380                     memcpy (temp_B, dpp->samples_B, sizeof (dpp->samples_B));
1381 
1382                     for (k = 0; k < MAX_TERM; k++) {
1383                         dpp->samples_A [k] = temp_A [m];
1384                         dpp->samples_B [k] = temp_B [m];
1385                         m = (m + 1) & (MAX_TERM - 1);
1386                     }
1387                 }
1388 
1389         if (wpc->config.flags & CONFIG_CALC_NOISE)
1390             wps->dc.noise_sum += noise_acc;
1391 
1392         flush_word (wps);
1393         data_count = bs_close_write (&wps->wvbits);
1394 
1395         if (data_count) {
1396             if (data_count != (uint32_t) -1) {
1397                 unsigned char *cptr = wps->blockbuff + ((WavpackHeader *) wps->blockbuff)->ckSize + 8;
1398 
1399                 *cptr++ = ID_WV_BITSTREAM | ID_LARGE;
1400                 *cptr++ = data_count >> 1;
1401                 *cptr++ = data_count >> 9;
1402                 *cptr++ = data_count >> 17;
1403                 ((WavpackHeader *) wps->blockbuff)->ckSize += data_count + 4;
1404             }
1405             else
1406                 return FALSE;
1407         }
1408 
1409         ((WavpackHeader *) wps->blockbuff)->crc = crc;
1410 
1411         if (wpc->wvc_flag) {
1412             data_count = bs_close_write (&wps->wvcbits);
1413 
1414             if (data_count && lossy) {
1415                 if (data_count != (uint32_t) -1) {
1416                     unsigned char *cptr = wps->block2buff + ((WavpackHeader *) wps->block2buff)->ckSize + 8;
1417 
1418                     *cptr++ = ID_WVC_BITSTREAM | ID_LARGE;
1419                     *cptr++ = data_count >> 1;
1420                     *cptr++ = data_count >> 9;
1421                     *cptr++ = data_count >> 17;
1422                     ((WavpackHeader *) wps->block2buff)->ckSize += data_count + 4;
1423                 }
1424                 else
1425                     return FALSE;
1426             }
1427 
1428             ((WavpackHeader *) wps->block2buff)->crc = crc2;
1429         }
1430         else if (lossy)
1431             wpc->lossy_blocks = TRUE;
1432 
1433         // we're done with the entire block, so now we check if our threshold for a "repack" was hit
1434 
1435         if (repack_possible && wps->num_terms > REPACK_SAFE_NUM_TERMS && (max_magnitude & repack_mask)) {
1436             *wps = saved_stream;
1437             wps->num_terms = REPACK_SAFE_NUM_TERMS;
1438             memcpy (wps->blockbuff, &wps->wphdr, sizeof (WavpackHeader));
1439 
1440             if (saved_buffer)
1441                 memcpy (buffer, saved_buffer, sample_count * sizeof (int32_t) * (flags & MONO_DATA ? 1 : 2));
1442 
1443             if (flags & HYBRID_FLAG)
1444                 crc = crc2 = 0xffffffff;
1445         }
1446         else {
1447             // if we actually did repack the block with fewer terms, we detect that here
1448             // and clean up so that we return to the original term count...otherwise we just
1449             // free the saved_buffer (if allocated) and break out of the loop
1450             if (wps->num_terms != saved_stream.num_terms) {
1451                 int ti;
1452 
1453                 for (ti = wps->num_terms; ti < saved_stream.num_terms; ++ti) {
1454                     wps->decorr_passes [ti].weight_A = wps->decorr_passes [ti].weight_B = 0;
1455                     CLEAR (wps->decorr_passes [ti].samples_A);
1456                     CLEAR (wps->decorr_passes [ti].samples_B);
1457                 }
1458 
1459                 wps->num_terms = saved_stream.num_terms;
1460             }
1461 
1462             if (saved_buffer)
1463                 free (saved_buffer);
1464 
1465             break;
1466         }
1467 
1468     } while (1);
1469 
1470     wps->sample_index += sample_count;
1471     return TRUE;
1472 }
1473 
1474 #if !defined(OPT_ASM_X64)
1475 
1476 // This is the "C" version of the stereo decorrelation pass function. There
1477 // are assembly optimized versions of this that can be used if available.
1478 // It performs a single pass of stereo decorrelation, in place, as specified
1479 // by the decorr_pass structure. Note that this function does NOT return the
1480 // dpp->samples_X[] values in the "normalized" positions for terms 1-8, so if
1481 // the number of samples is not a multiple of MAX_TERM, these must be moved if
1482 // they are to be used somewhere else.
1483 
decorr_stereo_pass(struct decorr_pass * dpp,int32_t * buffer,int32_t sample_count)1484 void decorr_stereo_pass (struct decorr_pass *dpp, int32_t *buffer, int32_t sample_count)
1485 {
1486     int32_t *bptr, *eptr = buffer + (sample_count * 2);
1487     int m, k;
1488 
1489     switch (dpp->term) {
1490         case 17:
1491             for (bptr = buffer; bptr < eptr; bptr += 2) {
1492                 int32_t sam, tmp;
1493 
1494                 sam = 2 * dpp->samples_A [0] - dpp->samples_A [1];
1495                 dpp->samples_A [1] = dpp->samples_A [0];
1496                 bptr [0] = tmp = (dpp->samples_A [0] = bptr [0]) - apply_weight (dpp->weight_A, sam);
1497                 update_weight (dpp->weight_A, dpp->delta, sam, tmp);
1498 
1499                 sam = 2 * dpp->samples_B [0] - dpp->samples_B [1];
1500                 dpp->samples_B [1] = dpp->samples_B [0];
1501                 bptr [1] = tmp = (dpp->samples_B [0] = bptr [1]) - apply_weight (dpp->weight_B, sam);
1502                 update_weight (dpp->weight_B, dpp->delta, sam, tmp);
1503             }
1504 
1505             break;
1506 
1507         case 18:
1508             for (bptr = buffer; bptr < eptr; bptr += 2) {
1509                 int32_t sam, tmp;
1510 
1511                 sam = dpp->samples_A [0] + ((dpp->samples_A [0] - dpp->samples_A [1]) >> 1);
1512                 dpp->samples_A [1] = dpp->samples_A [0];
1513                 bptr [0] = tmp = (dpp->samples_A [0] = bptr [0]) - apply_weight (dpp->weight_A, sam);
1514                 update_weight (dpp->weight_A, dpp->delta, sam, tmp);
1515 
1516                 sam = dpp->samples_B [0] + ((dpp->samples_B [0] - dpp->samples_B [1]) >> 1);
1517                 dpp->samples_B [1] = dpp->samples_B [0];
1518                 bptr [1] = tmp = (dpp->samples_B [0] = bptr [1]) - apply_weight (dpp->weight_B, sam);
1519                 update_weight (dpp->weight_B, dpp->delta, sam, tmp);
1520             }
1521 
1522             break;
1523 
1524         default:
1525             for (m = 0, k = dpp->term & (MAX_TERM - 1), bptr = buffer; bptr < eptr; bptr += 2) {
1526                 int32_t sam, tmp;
1527 
1528                 sam = dpp->samples_A [m];
1529                 bptr [0] = tmp = (dpp->samples_A [k] = bptr [0]) - apply_weight (dpp->weight_A, sam);
1530                 update_weight (dpp->weight_A, dpp->delta, sam, tmp);
1531 
1532                 sam = dpp->samples_B [m];
1533                 bptr [1] = tmp = (dpp->samples_B [k] = bptr [1]) - apply_weight (dpp->weight_B, sam);
1534                 update_weight (dpp->weight_B, dpp->delta, sam, tmp);
1535 
1536                 m = (m + 1) & (MAX_TERM - 1);
1537                 k = (k + 1) & (MAX_TERM - 1);
1538             }
1539 
1540             break;
1541 
1542         case -1:
1543             for (bptr = buffer; bptr < eptr; bptr += 2) {
1544                 int32_t sam_A, sam_B, tmp;
1545 
1546                 sam_A = dpp->samples_A [0];
1547                 bptr [0] = tmp = (sam_B = bptr [0]) - apply_weight (dpp->weight_A, sam_A);
1548                 update_weight_clip (dpp->weight_A, dpp->delta, sam_A, tmp);
1549 
1550                 bptr [1] = tmp = (dpp->samples_A [0] = bptr [1]) - apply_weight (dpp->weight_B, sam_B);
1551                 update_weight_clip (dpp->weight_B, dpp->delta, sam_B, tmp);
1552             }
1553 
1554             break;
1555 
1556         case -2:
1557             for (bptr = buffer; bptr < eptr; bptr += 2) {
1558                 int32_t sam_A, sam_B, tmp;
1559 
1560                 sam_B = dpp->samples_B [0];
1561                 bptr [1] = tmp = (sam_A = bptr [1]) - apply_weight (dpp->weight_B, sam_B);
1562                 update_weight_clip (dpp->weight_B, dpp->delta, sam_B, tmp);
1563 
1564                 bptr [0] = tmp = (dpp->samples_B [0] = bptr [0]) - apply_weight (dpp->weight_A, sam_A);
1565                 update_weight_clip (dpp->weight_A, dpp->delta, sam_A, tmp);
1566             }
1567 
1568             break;
1569 
1570         case -3:
1571             for (bptr = buffer; bptr < eptr; bptr += 2) {
1572                 int32_t sam_A, sam_B, tmp;
1573 
1574                 sam_A = dpp->samples_A [0];
1575                 sam_B = dpp->samples_B [0];
1576 
1577                 dpp->samples_A [0] = tmp = bptr [1];
1578                 bptr [1] = tmp -= apply_weight (dpp->weight_B, sam_B);
1579                 update_weight_clip (dpp->weight_B, dpp->delta, sam_B, tmp);
1580 
1581                 dpp->samples_B [0] = tmp = bptr [0];
1582                 bptr [0] = tmp -= apply_weight (dpp->weight_A, sam_A);
1583                 update_weight_clip (dpp->weight_A, dpp->delta, sam_A, tmp);
1584             }
1585 
1586             break;
1587     }
1588 }
1589 
1590 // This is the "C" version of the magnitude scanning function. There are
1591 // assembly optimized versions of this that can be used if available. This
1592 // function scans a buffer of signed 32-bit ints and returns the magnitude
1593 // of the largest sample, with a power-of-two resolution. It might be more
1594 // useful to return the actual maximum absolute value (and this function
1595 // could do that without breaking anything), but that implementation would
1596 // likely be slower. Instead, this simply returns the "or" of all the
1597 // values "xor"d with their own sign.
1598 
scan_max_magnitude(int32_t * values,int32_t num_values)1599 uint32_t scan_max_magnitude (int32_t *values, int32_t num_values)
1600 {
1601     uint32_t magnitude = 0;
1602 
1603     while (num_values--)
1604         magnitude |= (*values < 0) ? ~*values++ : *values++;
1605 
1606     return magnitude;
1607 }
1608 
1609 #endif
1610 
1611 #if !defined(OPT_ASM_X86) && !defined(OPT_ASM_X64)
1612 
1613 // This is the "C" version of the mono decorrelation pass function. There
1614 // are assembly optimized versions of this that are be used if available.
1615 // It decorrelates a buffer of mono samples, in place, as specified by the array
1616 // of decorr_pass structures. Note that this function does NOT return the
1617 // dpp->samples_X[] values in the "normalized" positions for terms 1-8, so if
1618 // the number of samples is not a multiple of MAX_TERM, these must be moved if
1619 // they are to be used somewhere else. The magnitude of the output samples is
1620 // accumulated and returned (see scan_max_magnitude() for more details).
1621 
decorr_mono_buffer(int32_t * buffer,struct decorr_pass * decorr_passes,int32_t num_terms,int32_t sample_count)1622 uint32_t decorr_mono_buffer (int32_t *buffer, struct decorr_pass *decorr_passes, int32_t num_terms, int32_t sample_count)
1623 {
1624     uint32_t max_magnitude = 0;
1625     struct decorr_pass *dpp;
1626     int tcount, i;
1627 
1628     for (i = 0; i < sample_count; ++i) {
1629         int32_t code = *buffer;
1630 
1631         for (tcount = num_terms, dpp = decorr_passes; tcount--; dpp++) {
1632             int32_t sam;
1633 
1634             if (dpp->term > MAX_TERM) {
1635                 if (dpp->term & 1)
1636                     sam = 2 * dpp->samples_A [0] - dpp->samples_A [1];
1637                 else
1638                     sam = (3 * dpp->samples_A [0] - dpp->samples_A [1]) >> 1;
1639 
1640                 dpp->samples_A [1] = dpp->samples_A [0];
1641                 dpp->samples_A [0] = code;
1642             }
1643             else {
1644                 sam = dpp->samples_A [i & (MAX_TERM - 1)];
1645                 dpp->samples_A [(i + dpp->term) & (MAX_TERM - 1)] = code;
1646             }
1647 
1648             code -= apply_weight (dpp->weight_A, sam);
1649             update_weight (dpp->weight_A, dpp->delta, sam, code);
1650         }
1651 
1652         *buffer++ = code;
1653         max_magnitude |= (code < 0) ? ~code : code;
1654     }
1655 
1656     return max_magnitude;
1657 }
1658 
1659 #endif
1660 
1661 //////////////////////////////////////////////////////////////////////////////
1662 // This function returns the accumulated RMS noise as a double if the       //
1663 // CALC_NOISE bit was set in the WavPack header. The peak noise can also be //
1664 // returned if desired. See wavpack.c for the calculations required to      //
1665 // convert this into decibels of noise below full scale.                    //
1666 //////////////////////////////////////////////////////////////////////////////
1667 
WavpackGetEncodedNoise(WavpackContext * wpc,double * peak)1668 double WavpackGetEncodedNoise (WavpackContext *wpc, double *peak)
1669 {
1670     WavpackStream *wps = wpc->streams [wpc->current_stream];
1671 
1672     if (peak)
1673         *peak = wps->dc.noise_max;
1674 
1675     return wps->dc.noise_sum;
1676 }
1677 
1678 // Open the specified BitStream using the specified buffer pointers. It is
1679 // assumed that enough buffer space has been allocated for all data that will
1680 // be written, otherwise an error will be generated.
1681 
1682 static void bs_write (Bitstream *bs);
1683 
bs_open_write(Bitstream * bs,void * buffer_start,void * buffer_end)1684 static void bs_open_write (Bitstream *bs, void *buffer_start, void *buffer_end)
1685 {
1686     bs->error = bs->sr = bs->bc = 0;
1687     bs->ptr = bs->buf = buffer_start;
1688     bs->end = buffer_end;
1689     bs->wrap = bs_write;
1690 }
1691 
1692 // This function is only called from the putbit() and putbits() macros when
1693 // the buffer is full, which is now flagged as an error.
1694 
bs_write(Bitstream * bs)1695 static void bs_write (Bitstream *bs)
1696 {
1697     bs->ptr = bs->buf;
1698     bs->error = 1;
1699 }
1700 
1701 // This function forces a flushing write of the specified BitStream, and
1702 // returns the total number of bytes written into the buffer.
1703 
bs_close_write(Bitstream * bs)1704 static uint32_t bs_close_write (Bitstream *bs)
1705 {
1706     uint32_t bytes_written;
1707 
1708     if (bs->error)
1709         return (uint32_t) -1;
1710 
1711     while (1) {
1712         while (bs->bc)
1713             putbit_1 (bs);
1714 
1715         bytes_written = (uint32_t)(bs->ptr - bs->buf) * sizeof (*(bs->ptr));
1716 
1717         if (bytes_written & 1) {
1718             putbit_1 (bs);
1719         }
1720         else
1721             break;
1722     };
1723 
1724     CLEAR (*bs);
1725     return bytes_written;
1726 }
1727