def parse_fmt(fmt):
    """given a fmt block BitstreamReader (without the 8 byte header)
    returns (channels, sample_rate, bits_per_sample, channel_mask)
    where channel_mask is a ChannelMask object and the rest are ints
    may raise ValueError if the fmt chunk is invalid
    or IOError if an error occurs parsing the chunk"""
    from audiotools import ChannelMask
    (compression, channels, sample_rate, bytes_per_second, block_align,
        bits_per_sample) = fmt.parse('16u 16u 32u 32u 16u 16u')
    if compression == 1:
        if channels == 1:
            channel_mask = ChannelMask.from_fields(front_center=True)
        elif channels == 2:
            channel_mask = ChannelMask.from_fields(front_left=True,
                front_right=True)
        elif channels == 3:
            channel_mask = ChannelMask.from_fields(front_left=True,
                front_right=True, front_center=True)
        elif channels == 4:
            channel_mask = ChannelMask.from_fields(front_left=True,
                front_right=True, back_left=True, back_right=True)
        elif channels == 5:
            channel_mask = ChannelMask.from_fields(front_left=True,
                front_right=True, back_left=True, back_right=True,
                front_center=True)
        elif channels == 6:
            channel_mask = ChannelMask.from_fields(front_left=True,
                front_right=True, back_left=True, back_right=True,
                front_center=True, low_frequency=True)
        else:
            channel_mask = ChannelMask(0)
        return channels, sample_rate, bits_per_sample, channel_mask
    elif compression == 65534:
        cb_size, valid_bits_per_sample, channel_mask, sub_format = fmt.parse(
            '16u 16u 32u 16b')
        if (sub_format != b'\x01\x00\x00\x00\x00\x00\x10\x00' +
            b'\x80\x00\x00\xaa\x008\x9bq'):
            raise ValueError('invalid WAVE sub-format')
        else:
            channel_mask = ChannelMask(channel_mask)
            return channels, sample_rate, bits_per_sample, channel_mask
    else:
        raise ValueError('unsupported WAVE compression')
