#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 block_channels,
decorrelation_term_count, decorrelation_terms, decorrelation_samples;
OUTPUT decorrelation_samples;
VAR block_channels "block channels";
VAR decorrelation_term_count "term count";
VAR block_channel_count "block channel count";
VAR decorrelation_terms "terms";
VAR decorrelation_samples "samples";
FUNC wv_log2 "encode log2 value" "wavpack:encode_wv_log2";
FUNC wv_exp2 "decode exp2 value" "wavpack:exp2";
for p <- 0 to decorrelation_term_count {
    for c <- 0 to block_channel_count {
        if 17 <= decorrelation_terms[p] <= 18 /*2 samples per channel*/ {
            for i <- 0 to 2 {
                s[p][c][i] <- wv_log2(decorrelation_samples[p][c][i]);
                decorrelation_samples[p][c][i] <- wv_exp2(s[p][c][i]);
            }
        } elif 1 <= decorrelation_terms[p] <= 8 /*"term" samples per channel*/ {
            for i <- 0 to decorrelation_terms[p] {
                s[p][c][i] <- wv_log2(decorrelation_samples[p][c][i]);
                decorrelation_samples[p][c][i] <- wv_exp2(s[p][c][i]);
            }
        } elif -3 <= decorrelation_terms[p] <= -1 /*1 sample per channel*/ {
            s[p][c][0] <- wv_log2(decorrelation_samples[p][c][0]);
            decorrelation_samples[p][c][0] <- wv_exp2(s[p][c][0]);
        }
    }
}
return decorrelation_samples;
