#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 "the FLAC file stream",block_size,subframe_bps,order;
OUTPUT "decoded signed PCM samples";
VAR block_size "block size";
VAR subframe_bps "subframe's bits-per-sample";
VAR sample "sample";
VAR order "predictor order";
VAR residual "residual";
VAR QLP_precision "QLP precision";
VAR QLP_shift "QLP shift needed";
VAR QLP_coeff "QLP coefficient";
FUNC read_residual_block "read residual block" "flac:decode_residual";
for i <- 0 to order /*warm-up samples*/ {
    sample[i] <- read (subframe_bps) signed;
}
QLP_precision <- (read 4 unsigned) + 1;
QLP_shift <- max(read 5 signed, 0) /*negative shifts are no-ops*/;
for i <- 0 to order {
    QLP_coeff[i] <- read (QLP_precision) signed;
}
;
residual <- read_residual_block(block_size, order);
;
for i <- order to block_size {
    sample[i] <- floor(frac(sum j <- 0 to order - 1
                            {QLP_coeff[j] * sample[i - j - 1]},
                            2 ^ QLP_shift)) + residual[i - order];
}
return sample;
