1 ////////////////////////////////////////////////////////////////////////////
2 //                           **** DSDPACK ****                            //
3 //         Lossless DSD (Direct Stream Digital) Audio Compressor          //
4 //                Copyright (c) 2013 - 2016 David Bryant.                 //
5 //                          All Rights Reserved.                          //
6 //      Distributed under the BSD Software License (see license.txt)      //
7 ////////////////////////////////////////////////////////////////////////////
8 
9 // pack_dsd.c
10 
11 // This module actually handles the compression of the DSD audio data.
12 
13 #ifdef ENABLE_DSD
14 
15 #include <stdlib.h>
16 #include <string.h>
17 #include <math.h>
18 
19 #include "wavpack_local.h"
20 
21 ///////////////////////////// executable code ////////////////////////////////
22 
23 // This function initializes everything required to pack WavPack DSD bitstreams
24 // and must be called BEFORE any other function in this module.
25 
pack_dsd_init(WavpackContext * wpc)26 void pack_dsd_init (WavpackContext *wpc)
27 {
28     WavpackStream *wps = wpc->streams [wpc->current_stream];
29 
30     wps->sample_index = 0;
31 }
32 
33 // Pack an entire block of samples (either mono or stereo) into a completed
34 // WavPack block. This function is actually a shell for pack_samples() and
35 // performs tasks like handling any shift required by the format, preprocessing
36 // of floating point data or integer data over 24 bits wide, and implementing
37 // the "extra" mode (via the extra?.c modules). It is assumed that there is
38 // sufficient space for the completed block at "wps->blockbuff" and that
39 // "wps->blockend" points to the end of the available space. A return value of
40 // FALSE indicates an error.
41 
42 // Pack an entire block of samples (either mono or stereo) into a completed
43 // WavPack block. It is assumed that there is sufficient space for the
44 // completed block at "wps->blockbuff" and that "wps->blockend" points to the
45 // end of the available space. A return value of FALSE indicates an error.
46 // Any unsent metadata is transmitted first, then required metadata for this
47 // block is sent, and finally the compressed integer data is sent. If a "wpx"
48 // stream is required for floating point data or large integer data, then this
49 // must be handled outside this function. To find out how much data was written
50 // the caller must look at the ckSize field of the written WavpackHeader, NOT
51 // the one in the WavpackStream.
52 
53 static int encode_buffer_high (WavpackStream *wps, int32_t *buffer, int num_samples, unsigned char *destination);
54 static int encode_buffer_fast (WavpackStream *wps, int32_t *buffer, int num_samples, unsigned char *destination);
55 
pack_dsd_block(WavpackContext * wpc,int32_t * buffer)56 int pack_dsd_block (WavpackContext *wpc, int32_t *buffer)
57 {
58     WavpackStream *wps = wpc->streams [wpc->current_stream];
59     uint32_t flags = wps->wphdr.flags, mult = wpc->dsd_multiplier, data_count;
60     uint32_t sample_count = wps->wphdr.block_samples;
61     unsigned char *dsd_encoding, dsd_power = 0;
62     int32_t res;
63 
64     // This code scans stereo data to check whether it can be stored as mono data
65     // (i.e., all L/R samples identical).
66 
67     if (!(flags & MONO_FLAG)) {
68         int32_t *sptr, *dptr, i;
69 
70         for (sptr = buffer, i = 0; i < (int32_t) sample_count; sptr += 2, i++)
71             if ((sptr [0] ^ sptr [1]) & 0xff)
72                 break;
73 
74         if (i == sample_count) {
75             wps->wphdr.flags = flags |= FALSE_STEREO;
76             dptr = buffer;
77             sptr = buffer;
78 
79             for (i = sample_count; i--; sptr++)
80                 *dptr++ = *sptr++;
81         }
82         else
83             wps->wphdr.flags = flags &= ~FALSE_STEREO;
84     }
85 
86     wps->wphdr.ckSize = sizeof (WavpackHeader) - 8;
87     memcpy (wps->blockbuff, &wps->wphdr, sizeof (WavpackHeader));
88 
89     if (wpc->metacount) {
90         WavpackMetadata *wpmdp = wpc->metadata;
91 
92         while (wpc->metacount) {
93             copy_metadata (wpmdp, wps->blockbuff, wps->blockend);
94             wpc->metabytes -= wpmdp->byte_length;
95             free_metadata (wpmdp++);
96             wpc->metacount--;
97         }
98 
99         free (wpc->metadata);
100         wpc->metadata = NULL;
101     }
102 
103     if (!sample_count)
104         return TRUE;
105 
106     send_general_metadata (wpc);
107     memcpy (&wps->wphdr, wps->blockbuff, sizeof (WavpackHeader));
108 
109     dsd_encoding = wps->blockbuff + ((WavpackHeader *) wps->blockbuff)->ckSize + 12;
110 
111     while (mult >>= 1)
112         dsd_power++;
113 
114     *dsd_encoding++ = dsd_power;
115 
116     if (wpc->config.flags & CONFIG_HIGH_FLAG) {
117         int fast_res = encode_buffer_fast (wps, buffer, sample_count, dsd_encoding);
118 
119         res = encode_buffer_high (wps, buffer, sample_count, dsd_encoding);
120 
121         if ((fast_res != -1) && (res == -1 || res > fast_res))
122             res = encode_buffer_fast (wps, buffer, sample_count, dsd_encoding);
123     }
124     else
125         res = encode_buffer_fast (wps, buffer, sample_count, dsd_encoding);
126 
127     if (res == -1) {
128         int num_samples = sample_count * ((flags & MONO_DATA) ? 1 : 2);
129         uint32_t crc = 0xffffffff;
130 
131         *dsd_encoding++ = 0;
132 
133         data_count = num_samples + 2;
134 
135         while (num_samples--)
136             crc += (crc << 1) + (*dsd_encoding++ = *buffer++);
137 
138         ((WavpackHeader *) wps->blockbuff)->crc = crc;
139     }
140     else
141         data_count = res + 1;
142 
143     if (data_count) {
144         unsigned char *cptr = wps->blockbuff + ((WavpackHeader *) wps->blockbuff)->ckSize + 8;
145 
146         if (data_count & 1) {
147             cptr [data_count + 4] = 0;
148             *cptr++ = ID_DSD_BLOCK | ID_LARGE | ID_ODD_SIZE;
149             data_count++;
150         }
151         else
152             *cptr++ = ID_DSD_BLOCK | ID_LARGE;
153 
154         *cptr++ = data_count >> 1;
155         *cptr++ = data_count >> 9;
156         *cptr++ = data_count >> 17;
157         ((WavpackHeader *) wps->blockbuff)->ckSize += data_count + 4;
158     }
159 
160     wps->sample_index += sample_count;
161     return TRUE;
162 }
163 
164 /*------------------------------------------------------------------------------------------------------------------------*/
165 
166 // #define DSD_BYTE_READY(low,high) (((low) >> 24) == ((high) >> 24))
167 // #define DSD_BYTE_READY(low,high) (!(((low) ^ (high)) >> 24))
168 #define DSD_BYTE_READY(low,high) (!(((low) ^ (high)) & 0xff000000))
169 
170 #define MAX_PROBABILITY     0xa0    // set to 0xff to disable RLE encoding for probabilities table
171 
172 #if (MAX_PROBABILITY < 0xff)
173 
rle_encode(unsigned char * src,int bcount,unsigned char * destination)174 static int rle_encode (unsigned char *src, int bcount, unsigned char *destination)
175 {
176     int max_rle_zeros = 0xff - MAX_PROBABILITY;
177     unsigned char *dp = destination;
178     int zcount = 0;
179 
180     while (bcount--) {
181         if (*src) {
182             while (zcount) {
183                 *dp++ = MAX_PROBABILITY + (zcount > max_rle_zeros ? max_rle_zeros : zcount);
184                 zcount -= (zcount > max_rle_zeros ? max_rle_zeros : zcount);
185             }
186 
187             *dp++ = *src++;
188         }
189         else {
190             zcount++;
191             src++;
192         }
193     }
194 
195     while (zcount) {
196         *dp++ = MAX_PROBABILITY + (zcount > max_rle_zeros ? max_rle_zeros : zcount);
197         zcount -= (zcount > max_rle_zeros ? max_rle_zeros : zcount);
198     }
199 
200     *dp++ = 0;
201 
202     return (int)(dp - destination);
203 }
204 
205 #endif
206 
calculate_probabilities(int hist[256],unsigned char probs[256],unsigned short prob_sums[256])207 static void calculate_probabilities (int hist [256], unsigned char probs [256], unsigned short prob_sums [256])
208 {
209     int divisor, min_value, max_value, sum_values;
210     int min_hits = 0x7fffffff, max_hits = 0, i;
211 
212     for (i = 0; i < 256; ++i) {
213         if (hist [i] < min_hits) min_hits = hist [i];
214         if (hist [i] > max_hits) max_hits = hist [i];
215     }
216 
217     if (max_hits == 0) {
218         memset (probs, 0, sizeof (*probs) * 256);
219         memset (prob_sums, 0, sizeof (*prob_sums) * 256);
220         return;
221     }
222 
223 //  fprintf (stderr, "process_histogram(): hits = %d to %d\n", min_hits, max_hits);
224 
225     if (max_hits > MAX_PROBABILITY)
226         divisor = ((max_hits << 8) + (MAX_PROBABILITY >> 1)) / MAX_PROBABILITY;
227     else
228         divisor = 0;
229 
230     while (1) {
231         min_value = 0x7fffffff; max_value = 0; sum_values = 0;
232 
233         for (i = 0; i < 256; ++i) {
234             int value;
235 
236             if (hist [i]) {
237                 if (divisor) {
238                     if (!(value = ((hist [i] << 8) + (divisor >> 1)) / divisor))
239                         value = 1;
240                 }
241                 else
242                     value = hist [i];
243 
244                 if (value < min_value) min_value = value;
245                 if (value > max_value) max_value = value;
246             }
247             else
248                 value = 0;
249 
250             prob_sums [i] = sum_values += value;
251             probs [i] = value;
252         }
253 
254         if (max_value > MAX_PROBABILITY) {
255             divisor++;
256             continue;
257         }
258 
259 #if 0   // this code reduces probability values when they are completely redundant (i.e., common divisor), but
260         // this doesn't really happen often enough to make it worthwhile
261 
262         if (min_value > 1) {
263             for (i = 0; i < 256; ++i)
264                 if (probs [i] % min_value)
265                     break;
266 
267             if (i == 256) {
268                 for (i = 0; i < 256; ++i) {
269                     prob_sums [i] /= min_value;
270                     probs [i] /= min_value;
271                 }
272 
273                 // fprintf (stderr, "fixed min_value = %d, divisor = %d, probs_sum = %d\n", min_value, divisor, prob_sums [255]);
274             }
275         }
276 #endif
277 
278         break;
279     }
280 }
281 
encode_buffer_fast(WavpackStream * wps,int32_t * buffer,int num_samples,unsigned char * destination)282 static int encode_buffer_fast (WavpackStream *wps, int32_t *buffer, int num_samples, unsigned char *destination)
283 {
284     uint32_t flags = wps->wphdr.flags, crc = 0xffffffff;
285     unsigned int low = 0, high = 0xffffffff, mult;
286     unsigned short (*summed_probabilities) [256];
287     unsigned char (*probabilities) [256];
288     unsigned char *dp = destination, *ep;
289     int history_bins, bc, p0 = 0, p1 = 0;
290     int total_summed_probabilities = 0;
291     int (*histogram) [256];
292     int32_t *bp = buffer;
293     char history_bits;
294 
295     if (!(flags & MONO_DATA))
296         num_samples *= 2;
297 
298     if (num_samples < 280)
299         return -1;
300     else if (num_samples < 560)
301         history_bits = 0;
302     else if (num_samples < 1725)
303         history_bits = 1;
304     else if (num_samples < 5000)
305         history_bits = 2;
306     else if (num_samples < 14000)
307         history_bits = 3;
308     else if (num_samples < 28000)
309         history_bits = 4;
310     else if (num_samples < 76000)
311         history_bits = 5;
312     else if (num_samples < 130000)
313         history_bits = 6;
314     else if (num_samples < 300000)
315         history_bits = 7;
316     else
317         history_bits = 8;
318 
319     if (history_bits > MAX_HISTORY_BITS)
320         history_bits = MAX_HISTORY_BITS;
321 
322     history_bins = 1 << history_bits;
323     histogram = malloc (sizeof (*histogram) * history_bins);
324     memset (histogram, 0, sizeof (*histogram) * history_bins);
325     probabilities = malloc (sizeof (*probabilities) * history_bins);
326     summed_probabilities = malloc (sizeof (*summed_probabilities) * history_bins);
327 
328     bc = num_samples;
329 
330     if (flags & MONO_DATA)
331         while (bc--) {
332             crc += (crc << 1) + (*bp & 0xff);
333             histogram [p0] [*bp & 0xff]++;
334             p0 = *bp++ & (history_bins-1);
335         }
336     else
337         while (bc--) {
338             crc += (crc << 1) + (*bp & 0xff);
339             histogram [p0] [*bp & 0xff]++;
340             p0 = p1;
341             p1 = *bp++ & (history_bins-1);
342         }
343 
344     for (p0 = 0; p0 < history_bins; p0++) {
345         calculate_probabilities (histogram [p0], probabilities [p0], summed_probabilities [p0]);
346         total_summed_probabilities += summed_probabilities [p0] [255];
347     }
348 
349     ((WavpackHeader *) wps->blockbuff)->crc = crc;
350 
351     // This code detects the case where the required value lookup tables grow silly big and cuts them back down. This would
352     // normally only happen with large blocks or poorly compressible data. The target is to guarantee that the total memory
353     // required for all three decode tables will be 2K bytes per history bin.
354 
355     while (total_summed_probabilities > history_bins * MAX_BYTES_PER_BIN) {
356         int max_sum = 0, sum_values = 0, largest_bin = 0;
357 
358         for (p0 = 0; p0 < history_bins; ++p0)
359             if (summed_probabilities [p0] [255] > max_sum) {
360                 max_sum = summed_probabilities [p0] [255];
361                 largest_bin = p0;
362             }
363 
364         total_summed_probabilities -= max_sum;
365         p0 = largest_bin;
366 
367         for (p1 = 0; p1 < 256; ++p1)
368             summed_probabilities [p0] [p1] = sum_values += probabilities [p0] [p1] = (probabilities [p0] [p1] + 1) >> 1;
369 
370         total_summed_probabilities += summed_probabilities [p0] [255];
371         // fprintf (stderr, "processed bin 0x%02x, bin: %d --> %d, new sum = %d\n",
372         //     p0, max_sum, summed_probabilities [p0] [255], total_summed_probabilities);
373     }
374 
375     free (histogram);
376     bp = buffer;
377     bc = num_samples;
378     *dp++ = 1;
379     *dp++ = history_bits;
380     *dp++ = MAX_PROBABILITY;
381     ep = destination + num_samples - 10;
382 
383 #if (MAX_PROBABILITY < 0xff)
384     dp += rle_encode ((unsigned char *) probabilities, sizeof (*probabilities) * history_bins, dp);
385 #else
386     memcpy (dp, probabilities, sizeof (*probabilities) * history_bins);
387     dp += sizeof (*probabilities) * history_bins;
388 #endif
389 
390     p0 = p1 = 0;
391 
392     while (dp < ep && bc--) {
393 
394         mult = (high - low) / summed_probabilities [p0] [255];
395 
396         if (!mult) {
397             high = low;
398 
399             while (DSD_BYTE_READY (high, low)) {
400                 *dp++ = high >> 24;
401                 high = (high << 8) | 0xff;
402                 low <<= 8;
403             }
404 
405             mult = (high - low) / summed_probabilities [p0] [255];
406         }
407 
408         if (*bp & 0xff)
409             low += summed_probabilities [p0] [(*bp & 0xff)-1] * mult;
410 
411         high = low + probabilities [p0] [*bp & 0xff] * mult - 1;
412 
413         while (DSD_BYTE_READY (high, low)) {
414             *dp++ = high >> 24;
415             high = (high << 8) | 0xff;
416             low <<= 8;
417         }
418 
419         if (flags & MONO_DATA)
420             p0 = *bp++ & (history_bins-1);
421         else {
422             p0 = p1;
423             p1 = *bp++ & (history_bins-1);
424         }
425     }
426 
427     high = low;
428 
429     while (DSD_BYTE_READY (high, low)) {
430         *dp++ = high >> 24;
431         high = (high << 8) | 0xff;
432         low <<= 8;
433     }
434 
435     free (summed_probabilities);
436     free (probabilities);
437 
438     if (dp < ep)
439         return (int)(dp - destination);
440     else
441         return -1;
442 }
443 
444 /*------------------------------------------------------------------------------------------------------------------------*/
445 
446 #define PTABLE_BITS 8
447 #define PTABLE_BINS (1<<PTABLE_BITS)
448 #define PTABLE_MASK (PTABLE_BINS-1)
449 
450 #define INITIAL_TERM (1536/PTABLE_BINS)
451 
452 #define UP   0x010000fe
453 #define DOWN 0x00010000
454 #define DECAY 8
455 
456 #define PRECISION 20
457 #define VALUE_ONE (1 << PRECISION)
458 #define PRECISION_USE 12
459 
460 #define RATE_S 20
461 
init_ptable(int * table,int rate_i,int rate_s)462 static void init_ptable (int *table, int rate_i, int rate_s)
463 {
464     int value = 0x808000, rate = rate_i << 8, c, i;
465 
466     for (c = (rate + 128) >> 8; c--;)
467         value += (DOWN - value) >> DECAY;
468 
469     for (i = 0; i < PTABLE_BINS/2; ++i) {
470         table [i] = value;
471         table [PTABLE_BINS-1-i] = 0x100ffff - value;
472 
473         if (value > 0x010000) {
474             rate += (rate * rate_s + 128) >> 8;
475 
476             for (c = (rate + 64) >> 7; c--;)
477                 value += (DOWN - value) >> DECAY;
478         }
479     }
480 }
481 
normalize_ptable(int * ptable)482 static int normalize_ptable (int *ptable)
483 {
484     int rate = 0, min_error, error_sum, i;
485     int ntable [PTABLE_BINS];
486 
487     init_ptable (ntable, rate, RATE_S);
488 
489     for (min_error = i = 0; i < PTABLE_BINS; ++i)
490         min_error += abs (ptable [i] - ntable [i]) >> 8;
491 
492     while (1) {
493         init_ptable (ntable, ++rate, RATE_S);
494 
495         for (error_sum = i = 0; i < PTABLE_BINS; ++i)
496             error_sum += abs (ptable [i] - ntable [i]) >> 8;
497 
498         if (error_sum < min_error)
499             min_error = error_sum;
500         else
501             break;
502     }
503 
504     return rate - 1;
505 }
506 
encode_buffer_high(WavpackStream * wps,int32_t * buffer,int num_samples,unsigned char * destination)507 static int encode_buffer_high (WavpackStream *wps, int32_t *buffer, int num_samples, unsigned char *destination)
508 {
509     int channel, stereo = (wps->wphdr.flags & MONO_DATA) ? 0 : 1;
510     uint32_t crc = 0xffffffff, high = 0xffffffff, low = 0;
511     unsigned char *dp = destination, *ep;
512     DSDfilters *sp;
513 
514     if (num_samples * (stereo + 1) < 280)
515         return -1;
516 
517     *dp++ = 3;
518     ep = destination + num_samples * (stereo + 1) - 10;
519 
520     if (!wps->sample_index) {
521         if (!wps->dsd.ptable)
522             wps->dsd.ptable = malloc (PTABLE_BINS * sizeof (*wps->dsd.ptable));
523 
524         init_ptable (wps->dsd.ptable, INITIAL_TERM, RATE_S);
525 
526         for (channel = 0; channel < 2; ++channel) {
527             sp = wps->dsd.filters + channel;
528 
529             sp->filter1 = sp->filter2 = sp->filter3 = sp->filter4 = sp->filter5 = VALUE_ONE / 2;
530             sp->filter6 = sp->factor = 0;
531         }
532 
533         *dp++ = INITIAL_TERM;
534         *dp++ = RATE_S;
535     }
536     else {
537         int rate = normalize_ptable (wps->dsd.ptable);
538         init_ptable (wps->dsd.ptable, rate, RATE_S);
539         *dp++ = rate;
540         *dp++ = RATE_S;
541     }
542 
543     for (channel = 0; channel <= stereo; ++channel) {
544         sp = wps->dsd.filters + channel;
545 
546         *dp = sp->filter1 >> (PRECISION - 8);
547         sp->filter1 = *dp++ << (PRECISION - 8);
548 
549         *dp = sp->filter2 >> (PRECISION - 8);
550         sp->filter2 = *dp++ << (PRECISION - 8);
551 
552         *dp = sp->filter3 >> (PRECISION - 8);
553         sp->filter3 = *dp++ << (PRECISION - 8);
554 
555         *dp = sp->filter4 >> (PRECISION - 8);
556         sp->filter4 = *dp++ << (PRECISION - 8);
557 
558         *dp = sp->filter5 >> (PRECISION - 8);
559         sp->filter5 = *dp++ << (PRECISION - 8);
560 
561         *dp++ = sp->factor;
562         *dp++ = sp->factor >> 8;
563         sp->filter6 = 0;
564         sp->factor = (int32_t)((uint32_t) sp->factor << 16) >> 16;
565     }
566 
567     sp = wps->dsd.filters;
568 
569     while (dp < ep && num_samples--) {
570         int bitcount = 8;
571 
572         crc += (crc << 1) + (sp->byte = *buffer++ & 0xff);
573         sp [0].value = sp [0].filter1 - sp [0].filter5 + ((sp [0].filter6 * sp [0].factor) >> 2);
574 
575         if (stereo) {
576             crc += (crc << 1) + (sp [1].byte = *buffer++ & 0xff);
577             sp [1].value = sp [1].filter1 - sp [1].filter5 + ((sp [1].filter6 * sp [1].factor) >> 2);
578         }
579 
580         while (bitcount--) {
581             int32_t *pp = wps->dsd.ptable + ((sp [0].value >> (PRECISION - PRECISION_USE)) & PTABLE_MASK);
582 
583             if (sp [0].byte & 0x80) {
584                 high = low + ((high - low) >> 8) * (*pp >> 16);
585                 *pp += (UP - *pp) >> DECAY;
586                 sp [0].filter0 = -1;
587             }
588             else {
589                 low += 1 + ((high - low) >> 8) * (*pp >> 16);
590                 *pp += (DOWN - *pp) >> DECAY;
591                 sp [0].filter0 = 0;
592             }
593 
594             while (DSD_BYTE_READY (high, low)) {
595                 *dp++ = high >> 24;
596                 high = (high << 8) | 0xff;
597                 low <<= 8;
598             }
599 
600             sp [0].value += sp [0].filter6 * 8;
601             sp [0].factor += (((sp [0].value ^ sp [0].filter0) >> 31) | 1) & ((sp [0].value ^ (sp [0].value - (sp [0].filter6 * 16))) >> 31);
602             sp [0].filter1 += ((sp [0].filter0 & VALUE_ONE) - sp [0].filter1) >> 6;
603             sp [0].filter2 += ((sp [0].filter0 & VALUE_ONE) - sp [0].filter2) >> 4;
604             sp [0].filter3 += (sp [0].filter2 - sp [0].filter3) >> 4;
605             sp [0].filter4 += (sp [0].filter3 - sp [0].filter4) >> 4;
606             sp [0].value = (sp [0].filter4 - sp [0].filter5) >> 4;
607             sp [0].filter5 += sp [0].value;
608             sp [0].filter6 += (sp [0].value - sp [0].filter6) >> 3;
609             sp [0].value = sp [0].filter1 - sp [0].filter5 + ((sp [0].filter6 * sp [0].factor) >> 2);
610             sp [0].byte <<= 1;
611 
612             if (!stereo)
613                 continue;
614 
615             pp = wps->dsd.ptable + ((sp [1].value >> (PRECISION - PRECISION_USE)) & PTABLE_MASK);
616 
617             if (sp [1].byte & 0x80) {
618                 high = low + ((high - low) >> 8) * (*pp >> 16);
619                 *pp += (UP - *pp) >> DECAY;
620                 sp [1].filter0 = -1;
621             }
622             else {
623                 low += 1 + ((high - low) >> 8) * (*pp >> 16);
624                 *pp += (DOWN - *pp) >> DECAY;
625                 sp [1].filter0 = 0;
626             }
627 
628             while (DSD_BYTE_READY (high, low)) {
629                 *dp++ = high >> 24;
630                 high = (high << 8) | 0xff;
631                 low <<= 8;
632             }
633 
634             sp [1].value += sp [1].filter6 * 8;
635             sp [1].factor += (((sp [1].value ^ sp [1].filter0) >> 31) | 1) & ((sp [1].value ^ (sp [1].value - (sp [1].filter6 * 16))) >> 31);
636             sp [1].filter1 += ((sp [1].filter0 & VALUE_ONE) - sp [1].filter1) >> 6;
637             sp [1].filter2 += ((sp [1].filter0 & VALUE_ONE) - sp [1].filter2) >> 4;
638             sp [1].filter3 += (sp [1].filter2 - sp [1].filter3) >> 4;
639             sp [1].filter4 += (sp [1].filter3 - sp [1].filter4) >> 4;
640             sp [1].value = (sp [1].filter4 - sp [1].filter5) >> 4;
641             sp [1].filter5 += sp [1].value;
642             sp [1].filter6 += (sp [1].value - sp [1].filter6) >> 3;
643             sp [1].value = sp [1].filter1 - sp [1].filter5 + ((sp [1].filter6 * sp [1].factor) >> 2);
644             sp [1].byte <<= 1;
645         }
646 
647         sp [0].factor -= (sp->factor + 512) >> 10;
648 
649         if (stereo)
650             sp [1].factor -= (sp [1].factor + 512) >> 10;
651     }
652 
653     ((WavpackHeader *) wps->blockbuff)->crc = crc;
654     high = low;
655 
656     while (DSD_BYTE_READY (high, low)) {
657         *dp++ = high >> 24;
658         high = (high << 8) | 0xff;
659         low <<= 8;
660     }
661 
662     if (dp < ep)
663         return (int)(dp - destination);
664     else
665         return -1;
666 }
667 
668 #endif      // ENABLE_DSD
669