#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 "a WavPack file stream",block_data_size,block_samples,channel_count;
OUTPUT "a set of decoding paramters";
VAR block_data_size "block data size";
VAR block_samples "block samples";
VAR channel_count "channel count";
VAR metadata_function "metadata function";
VAR nondecoder_data "nondecoder data";
VAR actual_size_1_less "actual size 1 less";
VAR sub_block_size "sub block size";
VAR decorrelation_term_count "term count";
VAR decorrelation_terms "decorrelation terms";
VAR decorrelation_deltas "decorrelation deltas";
VAR decorrelation_weights "decorrelation weights";
VAR decorrelation_samples "decorrelation samples";
VAR entropies "entropy";
VAR residuals "residuals";
VAR zero_bits "zero bits";
VAR one_bits "one bits";
VAR duplicate_bits "duplicate bits";
FUNC read_decorrelation_terms "read decorrelation terms" "wavpack:read_decorrelation_terms";
FUNC read_decorrelation_weights "read decorrelation weights" "wavpack:read_decorrelation_weights";
FUNC read_decorrelation_samples "read decorrelation samples" "wavpack:read_decorrelation_samples";
FUNC read_entropy_variables "read entropy variables" "wavpack:read_entropy_variables";
FUNC read_extended_integers "read extended integers" "wavpack:read_extended_integers";
FUNC read_bitstream "read bitstream" "wavpack:read_bitstream";
while block_data_size > 0 {
    metadata_function <- read 5 unsigned;
    nondecoder_data <- read 1 unsigned;
    actual_size_1_less <- read 1 unsigned;
    if (read 1 unsigned) == 0 /*large sub block*/ {
        sub_block_size <- (read 8 unsigned) * 2 - actual_size_1_less;
    } else {
        sub_block_size <- (read 24 unsigned) * 2 - actual_size_1_less;
    }
    if nondecoder_data == 0 {
        switch metadata_function {
            case 2 {
                decorrelation_term_count,
                decorrelation_terms,
                decorrelation_deltas <-
                read_decorrelation_terms(sub_block_size);
            }
            case 3 {
                decorrelation_weights <-
                read_decorrelation_weights(sub_block_size,
                                           channel_count,
                                           decorrelation_term_count);
            }
            case 4 {
                decorrelation_samples <-
                read_decorrelation_samples(sub_block_size,
                                           channel_count,
                                           decorrelation_term_count,
                                           decorrelation_terms);
            }
            case 5 {
                entropies <-
                read_entropy_variables(sub_block_size,
                                       channel_count);
            }
            case 9 {
                zero_bits,one_bits,duplicate_bits <-
                read_extended_integers();
            }
            case 10 {
                residuals <-
                read_bitstream(block_samples,
                               channel_count,
                               entropies);

            }
            default {
                skip sub_block_size bytes;
            }
        }
    } else {
        skip sub_block_size bytes;
    }
    if actual_size_1_less == 1 {
        skip 1 bytes;
    }
}
return decorrelation_term_count,
decorrelation_terms,
decorrelation_deltas,
decorrelation_weights,
decorrelation_samples,
residuals,
zero_bits,one_bits,duplicate_bits;