#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 "metadata function, whether metadata is nondecoder data, the length of sub block's data in bytes";
OUTPUT "sub block header data in 2 or 4 bytes";
VAR metadata_function "metadata function";
VAR nondecoder_data "nondecoder data";
VAR sub_block_size "sub block size";
VAR sub_block_header "sub block header";
metadata_function -> write 5 unsigned;
nondecoder_data -> write 1 unsigned;
(sub_block_size % 2) -> write 1 unsigned /*actual size 1 less*/;
if sub_block_size > (255 * 2) {
    1 -> write 1 unsigned;
    ceil(sub_block_size / 2) -> write 24 unsigned;
} else {
    0 -> write 1 unsigned;
    ceil(sub_block_size / 2) -> write 8 unsigned;
}
return sub_block_header;
