#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 "effective channel count, number of decorrelation terms, a decorrelation weight per pass per channel";
OUTPUT "a decorrelation weights sub block, the sub block size";
VAR effective_channels "effective channels";
VAR sub_block "sub block";
VAR decorrelation_term_count "term count";
VAR decorrelation_weights "weights";
VAR effective_channel_count "effective channel count";
FUNC write_sub_block_header "write sub block header"
"wavpack:write_sub_block_header";
write_sub_block_header(3, 0, decorrelation_term_count * effective_channels);
for p <- decorrelation_term_count downto 0 {
    for c <- 0 to effective_channels {
         if decorrelation_weights[p][c] > 0 {
             floor((decorrelation_weights[p][c] -
                   ((decorrelation_weights[p][c] + 2 ^ 6) / 2 ^ 7) + 4) / 2 ^ 3)
                 -> write 8 signed;
         } elif decorrelation_weights[p][c] == 0 {
              0 -> write 8 signed;
         } else {
              floor((decorrelation_weights[p][c] + 4) / 2 ^ 3)
                  -> write 8 signed;
         }
    }
}
return sub_block, 2 + (decorrelation_term_count * effective_channels);
