#This work is licensed under the
#Creative Commons Attribution-Share Alike 3.0 United States License.
#To view a copy of this license, visit
#http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter to
#Creative Commons,
#171 Second Street, Suite 300,
#San Francisco, California, 94105, USA.

INPUT "WavPack file data",sub_block_size,channel_count,decorrelation_term_count;
OUTPUT "a list of signed decorrelation weights per channel";
VAR sub_block_size "sub block size";
VAR decorrelation_term_count "term count";
VAR channel_count "channel count";
VAR decorrelation_weights "decorrelation weights";
VAR weight_count "decorrelation weight count";
weight_count <- sub_block_size;
assert weight_count <= decorrelation_term_count * channel_count;
for p <- decorrelation_term_count downto 0 {
    for c <- 0 to channel_count {
        if weight_count > 0 {
            v[p][c] <- read 8 signed;
            if v[p][c] > 0 {
                decorrelation_weights[p][c] <- v[p][c] * 2 ^ 3 +
                   floor((v[p][c] * 2 ^ 3 + 2 ^ 6) / 2 ^ 7);
            } elif v[p][c] == 0 {
                decorrelation_weights[p][c] <- 0;
            } elif v[p][c] < 0 {
                decorrelation_weights[p][c] <- v[p][c] * 2 ^ 3;
            }
            weight_count <- weight_count - 1;
        } else /*leftover weights are 0*/ {
            decorrelation_weights[p][c] <- 0;
        }
    }
}
return decorrelation_weights;
