#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 "packet containing one or more MLP frames";
OUTPUT "PCM frames";
VAR mlp_frame_size "MLP frame size";
VAR mlp_frames_remain "MLP frames remain";
VAR pcm_frames "PCM frames";
VAR restart_header "restart header";
VAR decoding_parameters "decoding parameters";
VAR max_channel "max channel";
VAR min_channel "min channel";
VAR max_matrix_channel "max matrix channel";
VAR noise_shift "noise shift";
VAR noise_gen_seed "noise gen seed";
VAR channel_assignment "channel assignment";
VAR flags "flags";
VAR block_size "block size";
VAR matrix_count "matrix count";
VAR output_shift "output shift";
VAR quant_step_size "quant step size";
VAR channel_params "channel parameters";
FUNC decode_frame "decode frame" "mlp:framedecoding";
/*initialize default parameters for each possible substream*/;
for s <- 0 to 2 {
    /*because MLP streams should begin with a restart header, these values should be immediately overwritten*/;
    min_channel[s],max_channel[s],max_matrix_channel[s],noise_shift[s],
    noise_gen_seed[s],channel_assignment[s],block_size[s],matrix_count[s] <- 0;
    restart_header[s] <-
    min_channel[s],max_channel[s],max_matrix_channel[s],noise_shift[s],
    noise_gen_seed[s],channel_assignment[s];
    flags[s] <- [0, 0, 0, 0, 0, 0, 0, 0];
    output_shift[s] <- [];
    quant_step_size[s] <- [];
    channel_params[s] <- [];
    decoding_parameters[s] <-
    flags[s],block_size[s],
    matrix_count[s],
    output_shift[s],quant_step_size[s],
    channel_params[s];
}
while mlp_frames_remain {
    skip 4;
    mlp_frame_size <- (read 12 unsigned) * 2 /*includes this 32 bit header*/;
    skip 16;
    pcm_frames,restart_header,decoding_parameters
    <- decode_frame(restart_header,decoding_parameters);
    return pcm_frames;
}
