#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 channels, block_size, predicted;
OUTPUT "1 or more channels of decorrelated samples";
VAR channels "channels";
VAR block_size "block size";
VAR predicted "predicted";
VAR decorrelated "decorrelated";
if channels == 1 {
    for i <- 0 to block_size {
        decorrelated[0][i] <- predicted[0][i];
    }
} else {
    c <- channels - 1;
    for i <- 0 to block_size {
        if predicted[(c - 1)][i] >= 0 /*round toward zero*/ {
            decorrelated[c][i] <- predicted[c][i] +
                floor(predicted[(c - 1)][i] / 2);
        } else {
            decorrelated[c][i] <- predicted[c][i] +
                ceil(predicted[(c - 1)][i] / 2);
        }
    }

    for c <- (channels - 1) downto 0 {
        for i <- 0 to block_size {
            decorrelated[c][i] <-
                decorrelated[(c + 1)][i] - predicted[c][i];
        }
    }
}
return decorrelated;
