#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 "two channels of shifted PCM samples, sample count of channels, bits-per-sample, the size of uncompressed LSBs in bytes, a list of uncompressed LSBs; default block size from encoding parameters";
INPUT "two channels of shifted PCM samples",
sample_count,bits_per_sample,interlacing_shift,interlacing_leftweight,
uncompressed_LSBs,LSB,encoding_parameters;
OUTPUT "interlaced frame, interlaced frame size";
VAR encoding_parameters "encoding parameters";
VAR LSB "LSB";
VAR uncompressed_LSBs "uncompressed LSBs";
VAR sample_count "sample count";
VAR bits_per_sample "bits-per-sample";
VAR block_size "block size";
VAR sample_size "sample size";
VAR channel "channel";
VAR QLP_coeff "QLP coefficient";
VAR QLP_coeff_count "QLP coefficient count";
VAR residual "residual block";
VAR residual_size "residual block size";
VAR compressed_frame "compressed frame";
VAR compressed_frame_size "compressed frame size";
VAR interlacing_shift "interlacing shift";
VAR interlacing_leftweight "interlacing leftweight";
VAR correlated "correlated";
FUNC correlate_channels "correlate channels" "alac:correlate_channels";
FUNC compute_QLP_coeff "compute QLP coefficient and residual"
"alac:compute_qlp_coeffs";
FUNC write_subframe_header "write subframe header"
"alac:write_subframe_header";
block_size <- encoding_parameters;
0 -> write 16 unsigned /*unused*/;
if sample_count != block_size {
    1 -> write 1 unsigned /*has sample count*/;
    uncompressed_LSBs -> write 2 unsigned /*uncompressed LSBs*/;
    0 -> write 1 unsigned /*not compressed*/;
    sample_count -> write 32 unsigned;
} else {
    0 -> write 1 unsigned /*has sample count*/;
    uncompressed_LSBs -> write 2 unsigned /*uncompressed LSBs*/;
    0 -> write 1 unsigned /*not compressed*/;
}
interlacing_shift -> write 8 unsigned;
interlacing_leftweight -> write 8 unsigned;
correlated[0],correlated[1] <-
correlate_channels(channel[0],
                   channel[1],
                   sample_count,
                   interlacing_shift,
                   interlacing_leftweight);
sample_size <- bits_per_sample - (uncompressed_LSBs * 8);
QLP_coeff[0],QLP_coeff_count[0],residual[0],residual_size[0] <-
compute_QLP_coeff(correlated[0], sample_count, sample_size);
QLP_coeff[1],QLP_coeff_count[1],residual[1],residual_size[1] <-
compute_QLP_coeff(correlated[1], sample_count, sample_size);
write_subframe_header(QLP_coeff[0],QLP_coeff_count[0]);
write_subframe_header(QLP_coeff[1],QLP_coeff_count[1]);
if uncompressed_LSBs > 0 {
    for i <- 0 to sample_count * 2 {
        LSB[i] -> write (uncompressed_LSBs * 8) unsigned;
    }
}
residual[0] -> write residual_size[0] unsigned;
residual[1] -> write residual_size[1] unsigned;
return compressed_frame,compressed_frame_size;
