static int read_header(AVFormatContext *s)

{

    BRSTMDemuxContext *b = s->priv_data;

    int bom, major, minor, codec, chunk;

    int64_t h1offset, pos, toffset;

    uint32_t size, asize, start = 0;

    AVStream *st;

    int ret = AVERROR_EOF;

    int loop = 0;

    int bfstm = !strcmp("bfstm", s->iformat->name);



    st = avformat_new_stream(s, NULL);

    if (!st)

        return AVERROR(ENOMEM);

    st->codec->codec_type = AVMEDIA_TYPE_AUDIO;



    avio_skip(s->pb, 4);



    bom = avio_rb16(s->pb);

    if (bom != 0xFEFF && bom != 0xFFFE) {

        av_log(s, AV_LOG_ERROR, "invalid byte order: %X\n", bom);

        return AVERROR_INVALIDDATA;

    }



    if (bom == 0xFFFE)

        b->little_endian = 1;



    if (!bfstm) {

        major = avio_r8(s->pb);

        minor = avio_r8(s->pb);

        avio_skip(s->pb, 4); // size of file

        size = read16(s);

        if (size < 14)

            return AVERROR_INVALIDDATA;



        avio_skip(s->pb, size - 14);

        pos = avio_tell(s->pb);

        if (avio_rl32(s->pb) != MKTAG('H','E','A','D'))

            return AVERROR_INVALIDDATA;

    } else {

        uint32_t info_offset = 0;

        uint16_t section_count, header_size, i;



        header_size = read16(s); // 6



        avio_skip(s->pb, 4); // Unknown constant 0x00030000

        avio_skip(s->pb, 4); // size of file

        section_count = read16(s);

        avio_skip(s->pb, 2); // padding

        for (i = 0; avio_tell(s->pb) < header_size

                    && !(start && info_offset)

                    && i < section_count; i++) {

            uint16_t flag = read16(s);

            avio_skip(s->pb, 2);

            switch (flag) {

            case 0x4000:

                info_offset = read32(s);

                /*info_size =*/ read32(s);

                break;

            case 0x4001:

                avio_skip(s->pb, 4); // seek offset

                avio_skip(s->pb, 4); // seek size

                break;

            case 0x4002:

                start = read32(s) + 8;

                avio_skip(s->pb, 4); //data_size = read32(s);

                break;

            case 0x4003:

                avio_skip(s->pb, 4); // REGN offset

                avio_skip(s->pb, 4); // REGN size

                break;

            }

        }



        if (!info_offset || !start)

            return AVERROR_INVALIDDATA;



        avio_skip(s->pb, info_offset - avio_tell(s->pb));

        pos = avio_tell(s->pb);

        if (avio_rl32(s->pb) != MKTAG('I','N','F','O'))

            return AVERROR_INVALIDDATA;

    }



    size = read32(s);

    if (size < 192)

        return AVERROR_INVALIDDATA;

    avio_skip(s->pb, 4); // unknown

    h1offset = read32(s);

    if (h1offset > size)

        return AVERROR_INVALIDDATA;

    avio_skip(s->pb, 12);

    toffset = read32(s) + 16LL;

    if (toffset > size)

        return AVERROR_INVALIDDATA;



    avio_skip(s->pb, pos + h1offset + 8 - avio_tell(s->pb));

    codec = avio_r8(s->pb);



    switch (codec) {

    case 0: codec = AV_CODEC_ID_PCM_S8_PLANAR;    break;

    case 1: codec = b->little_endian ?

                    AV_CODEC_ID_PCM_S16LE_PLANAR :

                    AV_CODEC_ID_PCM_S16BE_PLANAR; break;

    case 2: codec = b->little_endian ?

                    AV_CODEC_ID_ADPCM_THP_LE :

                    AV_CODEC_ID_ADPCM_THP;        break;

    default:

        avpriv_request_sample(s, "codec %d", codec);

        return AVERROR_PATCHWELCOME;

    }



    loop = avio_r8(s->pb); // loop flag

    st->codec->codec_id = codec;

    st->codec->channels = avio_r8(s->pb);

    if (!st->codec->channels)

        return AVERROR_INVALIDDATA;



    avio_skip(s->pb, 1); // padding



    st->codec->sample_rate = bfstm ? read32(s) : read16(s);

    if (!st->codec->sample_rate)

        return AVERROR_INVALIDDATA;



    if (!bfstm)

        avio_skip(s->pb, 2); // padding



    if (loop) {

        if (av_dict_set_int(&s->metadata, "loop_start",

                            av_rescale(read32(s), AV_TIME_BASE,

                                       st->codec->sample_rate),

                            0) < 0)

            return AVERROR(ENOMEM);

    } else {

        avio_skip(s->pb, 4);

    }



    st->start_time = 0;

    st->duration = read32(s);

    avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);



    if (!bfstm)

        start = read32(s);

    b->current_block = 0;

    b->block_count = read32(s);

    if (b->block_count > UINT16_MAX) {

        av_log(s, AV_LOG_WARNING, "too many blocks: %u\n", b->block_count);

        return AVERROR_INVALIDDATA;

    }



    b->block_size = read32(s);

    if (b->block_size > UINT32_MAX / st->codec->channels)

        return AVERROR_INVALIDDATA;



    b->samples_per_block = read32(s);

    b->last_block_used_bytes = read32(s);

    b->last_block_samples = read32(s);

    b->last_block_size = read32(s);

    if (b->last_block_size > UINT32_MAX / st->codec->channels)

        return AVERROR_INVALIDDATA;

    if (b->last_block_used_bytes > b->last_block_size)

        return AVERROR_INVALIDDATA;





    if (codec == AV_CODEC_ID_ADPCM_THP || codec == AV_CODEC_ID_ADPCM_THP_LE) {

        int ch;



        avio_skip(s->pb, pos + toffset - avio_tell(s->pb));

        if (!bfstm)

            toffset = read32(s) + 16LL;

        else

            toffset = toffset + read32(s) + st->codec->channels * 8 - 8;

        if (toffset > size)

            return AVERROR_INVALIDDATA;



        avio_skip(s->pb, pos + toffset - avio_tell(s->pb));

        b->table = av_mallocz(32 * st->codec->channels);

        if (!b->table)

            return AVERROR(ENOMEM);



        for (ch = 0; ch < st->codec->channels; ch++) {

            if (avio_read(s->pb, b->table + ch * 32, 32) != 32) {

                ret = AVERROR_INVALIDDATA;

                goto fail;

            }

            avio_skip(s->pb, bfstm ? 14 : 24);

        }

    }



    if (size < (avio_tell(s->pb) - pos)) {

        ret = AVERROR_INVALIDDATA;

        goto fail;

    }



    avio_skip(s->pb, size - (avio_tell(s->pb) - pos));



    while (!avio_feof(s->pb)) {

        chunk = avio_rl32(s->pb);

        size  = read32(s);

        if (size < 8) {

            ret = AVERROR_INVALIDDATA;

            goto fail;

        }

        size -= 8;

        switch (chunk) {

        case MKTAG('S','E','E','K'):

        case MKTAG('A','D','P','C'):

            if (codec != AV_CODEC_ID_ADPCM_THP &&

                codec != AV_CODEC_ID_ADPCM_THP_LE)

                goto skip;



            asize = b->block_count * st->codec->channels * 4;

            if (size < asize) {

                ret = AVERROR_INVALIDDATA;

                goto fail;

            }

            if (b->adpc) {

                av_log(s, AV_LOG_WARNING, "skipping additional ADPC chunk\n");

                goto skip;

            } else {

                b->adpc = av_mallocz(asize);

                if (!b->adpc) {

                    ret = AVERROR(ENOMEM);

                    goto fail;

                }

                if (bfstm && codec != AV_CODEC_ID_ADPCM_THP_LE) {

                    // Big-endian BFSTMs have little-endian SEEK tables

                    // for some strange reason.

                    int i;

                    for (i = 0; i < asize; i += 2) {

                        b->adpc[i+1] = avio_r8(s->pb);

                        b->adpc[i]   = avio_r8(s->pb);

                    }

                } else {

                    avio_read(s->pb, b->adpc, asize);

                }

                avio_skip(s->pb, size - asize);

            }

            break;

        case MKTAG('D','A','T','A'):

            if ((start < avio_tell(s->pb)) ||

                (!b->adpc && (codec == AV_CODEC_ID_ADPCM_THP ||

                              codec == AV_CODEC_ID_ADPCM_THP_LE))) {

                ret = AVERROR_INVALIDDATA;

                goto fail;

            }

            avio_skip(s->pb, start - avio_tell(s->pb));



            if (bfstm && (codec == AV_CODEC_ID_ADPCM_THP ||

                          codec == AV_CODEC_ID_ADPCM_THP_LE))

                avio_skip(s->pb, 24);



            b->data_start = avio_tell(s->pb);



            if (!bfstm && (major != 1 || minor))

                avpriv_request_sample(s, "Version %d.%d", major, minor);



            return 0;

        default:

            av_log(s, AV_LOG_WARNING, "skipping unknown chunk: %X\n", chunk);

skip:

            avio_skip(s->pb, size);

        }

    }



fail:

    read_close(s);



    return ret;

}

static int read_header(AVFormatContext *s)

{

    BRSTMDemuxContext *b = s->priv_data;

    int bom, major, minor, codec, chunk;

    int64_t h1offset, pos, toffset;

    uint32_t size, asize, start = 0;

    AVStream *st;

    int ret = AVERROR_EOF;

    int loop = 0;

    int bfstm = !strcmp("bfstm", s->iformat->name);



    st = avformat_new_stream(s, NULL);

    if (!st)

        return AVERROR(ENOMEM);

    st->codec->codec_type = AVMEDIA_TYPE_AUDIO;



    avio_skip(s->pb, 4);



    bom = avio_rb16(s->pb);

    if (bom != 0xFEFF && bom != 0xFFFE) {

        av_log(s, AV_LOG_ERROR, "invalid byte order: %X\n", bom);

        return AVERROR_INVALIDDATA;

    }



    if (bom == 0xFFFE)

        b->little_endian = 1;



    if (!bfstm) {

        major = avio_r8(s->pb);

        minor = avio_r8(s->pb);

        avio_skip(s->pb, 4); 

        size = read16(s);

        if (size < 14)

            return AVERROR_INVALIDDATA;



        avio_skip(s->pb, size - 14);

        pos = avio_tell(s->pb);

        if (avio_rl32(s->pb) != MKTAG('H','E','A','D'))

            return AVERROR_INVALIDDATA;

    } else {

        uint32_t info_offset = 0;

        uint16_t section_count, header_size, i;



        header_size = read16(s); 



        avio_skip(s->pb, 4); 

        avio_skip(s->pb, 4); 

        section_count = read16(s);

        avio_skip(s->pb, 2); 

        for (i = 0; avio_tell(s->pb) < header_size

                    && !(start && info_offset)

                    && i < section_count; i++) {

            uint16_t flag = read16(s);

            avio_skip(s->pb, 2);

            switch (flag) {

            case 0x4000:

                info_offset = read32(s);

                 read32(s);

                break;

            case 0x4001:

                avio_skip(s->pb, 4); 

                avio_skip(s->pb, 4); 

                break;

            case 0x4002:

                start = read32(s) + 8;

                avio_skip(s->pb, 4); 

                break;

            case 0x4003:

                avio_skip(s->pb, 4); 

                avio_skip(s->pb, 4); 

                break;

            }

        }



        if (!info_offset || !start)

            return AVERROR_INVALIDDATA;



        avio_skip(s->pb, info_offset - avio_tell(s->pb));

        pos = avio_tell(s->pb);

        if (avio_rl32(s->pb) != MKTAG('I','N','F','O'))

            return AVERROR_INVALIDDATA;

    }



    size = read32(s);

    if (size < 192)

        return AVERROR_INVALIDDATA;

    avio_skip(s->pb, 4); 

    h1offset = read32(s);

    if (h1offset > size)

        return AVERROR_INVALIDDATA;

    avio_skip(s->pb, 12);

    toffset = read32(s) + 16LL;

    if (toffset > size)

        return AVERROR_INVALIDDATA;



    avio_skip(s->pb, pos + h1offset + 8 - avio_tell(s->pb));

    codec = avio_r8(s->pb);



    switch (codec) {

    case 0: codec = AV_CODEC_ID_PCM_S8_PLANAR;    break;

    case 1: codec = b->little_endian ?

                    AV_CODEC_ID_PCM_S16LE_PLANAR :

                    AV_CODEC_ID_PCM_S16BE_PLANAR; break;

    case 2: codec = b->little_endian ?

                    AV_CODEC_ID_ADPCM_THP_LE :

                    AV_CODEC_ID_ADPCM_THP;        break;

    default:

        avpriv_request_sample(s, "codec %d", codec);

        return AVERROR_PATCHWELCOME;

    }



    loop = avio_r8(s->pb); 

    st->codec->codec_id = codec;

    st->codec->channels = avio_r8(s->pb);

    if (!st->codec->channels)

        return AVERROR_INVALIDDATA;



    avio_skip(s->pb, 1); 



    st->codec->sample_rate = bfstm ? read32(s) : read16(s);

    if (!st->codec->sample_rate)

        return AVERROR_INVALIDDATA;



    if (!bfstm)

        avio_skip(s->pb, 2); 



    if (loop) {

        if (av_dict_set_int(&s->metadata, "loop_start",

                            av_rescale(read32(s), AV_TIME_BASE,

                                       st->codec->sample_rate),

                            0) < 0)

            return AVERROR(ENOMEM);

    } else {

        avio_skip(s->pb, 4);

    }



    st->start_time = 0;

    st->duration = read32(s);

    avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);



    if (!bfstm)

        start = read32(s);

    b->current_block = 0;

    b->block_count = read32(s);

    if (b->block_count > UINT16_MAX) {

        av_log(s, AV_LOG_WARNING, "too many blocks: %u\n", b->block_count);

        return AVERROR_INVALIDDATA;

    }



    b->block_size = read32(s);

    if (b->block_size > UINT32_MAX / st->codec->channels)

        return AVERROR_INVALIDDATA;



    b->samples_per_block = read32(s);

    b->last_block_used_bytes = read32(s);

    b->last_block_samples = read32(s);

    b->last_block_size = read32(s);

    if (b->last_block_size > UINT32_MAX / st->codec->channels)

        return AVERROR_INVALIDDATA;

    if (b->last_block_used_bytes > b->last_block_size)

        return AVERROR_INVALIDDATA;





    if (codec == AV_CODEC_ID_ADPCM_THP || codec == AV_CODEC_ID_ADPCM_THP_LE) {

        int ch;



        avio_skip(s->pb, pos + toffset - avio_tell(s->pb));

        if (!bfstm)

            toffset = read32(s) + 16LL;

        else

            toffset = toffset + read32(s) + st->codec->channels * 8 - 8;

        if (toffset > size)

            return AVERROR_INVALIDDATA;



        avio_skip(s->pb, pos + toffset - avio_tell(s->pb));

        b->table = av_mallocz(32 * st->codec->channels);

        if (!b->table)

            return AVERROR(ENOMEM);



        for (ch = 0; ch < st->codec->channels; ch++) {

            if (avio_read(s->pb, b->table + ch * 32, 32) != 32) {

                ret = AVERROR_INVALIDDATA;

                goto fail;

            }

            avio_skip(s->pb, bfstm ? 14 : 24);

        }

    }



    if (size < (avio_tell(s->pb) - pos)) {

        ret = AVERROR_INVALIDDATA;

        goto fail;

    }



    avio_skip(s->pb, size - (avio_tell(s->pb) - pos));



    while (!avio_feof(s->pb)) {

        chunk = avio_rl32(s->pb);

        size  = read32(s);

        if (size < 8) {

            ret = AVERROR_INVALIDDATA;

            goto fail;

        }

        size -= 8;

        switch (chunk) {

        case MKTAG('S','E','E','K'):

        case MKTAG('A','D','P','C'):

            if (codec != AV_CODEC_ID_ADPCM_THP &&

                codec != AV_CODEC_ID_ADPCM_THP_LE)

                goto skip;



            asize = b->block_count * st->codec->channels * 4;

            if (size < asize) {

                ret = AVERROR_INVALIDDATA;

                goto fail;

            }

            if (b->adpc) {

                av_log(s, AV_LOG_WARNING, "skipping additional ADPC chunk\n");

                goto skip;

            } else {

                b->adpc = av_mallocz(asize);

                if (!b->adpc) {

                    ret = AVERROR(ENOMEM);

                    goto fail;

                }

                if (bfstm && codec != AV_CODEC_ID_ADPCM_THP_LE) {

                    

                    

                    int i;

                    for (i = 0; i < asize; i += 2) {

                        b->adpc[i+1] = avio_r8(s->pb);

                        b->adpc[i]   = avio_r8(s->pb);

                    }

                } else {

                    avio_read(s->pb, b->adpc, asize);

                }

                avio_skip(s->pb, size - asize);

            }

            break;

        case MKTAG('D','A','T','A'):

            if ((start < avio_tell(s->pb)) ||

                (!b->adpc && (codec == AV_CODEC_ID_ADPCM_THP ||

                              codec == AV_CODEC_ID_ADPCM_THP_LE))) {

                ret = AVERROR_INVALIDDATA;

                goto fail;

            }

            avio_skip(s->pb, start - avio_tell(s->pb));



            if (bfstm && (codec == AV_CODEC_ID_ADPCM_THP ||

                          codec == AV_CODEC_ID_ADPCM_THP_LE))

                avio_skip(s->pb, 24);



            b->data_start = avio_tell(s->pb);



            if (!bfstm && (major != 1 || minor))

                avpriv_request_sample(s, "Version %d.%d", major, minor);



            return 0;

        default:

            av_log(s, AV_LOG_WARNING, "skipping unknown chunk: %X\n", chunk);

skip:

            avio_skip(s->pb, size);

        }

    }



fail:

    read_close(s);



    return ret;

}

    if (!st->codec->sample_rate)
static int FUNC_0(AVFormatContext *VAR_0)

{

    BRSTMDemuxContext *b = VAR_0->priv_data;

    int VAR_1, VAR_2, VAR_3, VAR_4, VAR_5;

    int64_t h1offset, pos, toffset;

    uint32_t size, asize, start = 0;

    AVStream *st;

    int VAR_6 = AVERROR_EOF;

    int VAR_7 = 0;

    int VAR_8 = !strcmp("VAR_8", VAR_0->iformat->name);



    st = avformat_new_stream(VAR_0, NULL);

    if (!st)

        return AVERROR(ENOMEM);

    st->VAR_4->codec_type = AVMEDIA_TYPE_AUDIO;



    avio_skip(VAR_0->pb, 4);



    VAR_1 = avio_rb16(VAR_0->pb);

    if (VAR_1 != 0xFEFF && VAR_1 != 0xFFFE) {

        av_log(VAR_0, AV_LOG_ERROR, "invalid byte order: %X\n", VAR_1);

        return AVERROR_INVALIDDATA;

    }



    if (VAR_1 == 0xFFFE)

        b->little_endian = 1;



    if (!VAR_8) {

        VAR_2 = avio_r8(VAR_0->pb);

        VAR_3 = avio_r8(VAR_0->pb);

        avio_skip(VAR_0->pb, 4); 

        size = read16(VAR_0);

        if (size < 14)

            return AVERROR_INVALIDDATA;



        avio_skip(VAR_0->pb, size - 14);

        pos = avio_tell(VAR_0->pb);

        if (avio_rl32(VAR_0->pb) != MKTAG('H','E','A','D'))

            return AVERROR_INVALIDDATA;

    } else {

        uint32_t info_offset = 0;

        uint16_t section_count, header_size, VAR_10;



        header_size = read16(VAR_0); 



        avio_skip(VAR_0->pb, 4); 

        avio_skip(VAR_0->pb, 4); 

        section_count = read16(VAR_0);

        avio_skip(VAR_0->pb, 2); 

        for (VAR_10 = 0; avio_tell(VAR_0->pb) < header_size

                    && !(start && info_offset)

                    && VAR_10 < section_count; VAR_10++) {

            uint16_t flag = read16(VAR_0);

            avio_skip(VAR_0->pb, 2);

            switch (flag) {

            case 0x4000:

                info_offset = read32(VAR_0);

                 read32(VAR_0);

                break;

            case 0x4001:

                avio_skip(VAR_0->pb, 4); 

                avio_skip(VAR_0->pb, 4); 

                break;

            case 0x4002:

                start = read32(VAR_0) + 8;

                avio_skip(VAR_0->pb, 4); 

                break;

            case 0x4003:

                avio_skip(VAR_0->pb, 4); 

                avio_skip(VAR_0->pb, 4); 

                break;

            }

        }



        if (!info_offset || !start)

            return AVERROR_INVALIDDATA;



        avio_skip(VAR_0->pb, info_offset - avio_tell(VAR_0->pb));

        pos = avio_tell(VAR_0->pb);

        if (avio_rl32(VAR_0->pb) != MKTAG('I','N','F','O'))

            return AVERROR_INVALIDDATA;

    }



    size = read32(VAR_0);

    if (size < 192)

        return AVERROR_INVALIDDATA;

    avio_skip(VAR_0->pb, 4); 

    h1offset = read32(VAR_0);

    if (h1offset > size)

        return AVERROR_INVALIDDATA;

    avio_skip(VAR_0->pb, 12);

    toffset = read32(VAR_0) + 16LL;

    if (toffset > size)

        return AVERROR_INVALIDDATA;



    avio_skip(VAR_0->pb, pos + h1offset + 8 - avio_tell(VAR_0->pb));

    VAR_4 = avio_r8(VAR_0->pb);



    switch (VAR_4) {

    case 0: VAR_4 = AV_CODEC_ID_PCM_S8_PLANAR;    break;

    case 1: VAR_4 = b->little_endian ?

                    AV_CODEC_ID_PCM_S16LE_PLANAR :

                    AV_CODEC_ID_PCM_S16BE_PLANAR; break;

    case 2: VAR_4 = b->little_endian ?

                    AV_CODEC_ID_ADPCM_THP_LE :

                    AV_CODEC_ID_ADPCM_THP;        break;

    default:

        avpriv_request_sample(VAR_0, "VAR_4 %d", VAR_4);

        return AVERROR_PATCHWELCOME;

    }



    VAR_7 = avio_r8(VAR_0->pb); 

    st->VAR_4->codec_id = VAR_4;

    st->VAR_4->channels = avio_r8(VAR_0->pb);

    if (!st->VAR_4->channels)

        return AVERROR_INVALIDDATA;



    avio_skip(VAR_0->pb, 1); 



    st->VAR_4->sample_rate = VAR_8 ? read32(VAR_0) : read16(VAR_0);

    if (!st->VAR_4->sample_rate)

        return AVERROR_INVALIDDATA;



    if (!VAR_8)

        avio_skip(VAR_0->pb, 2); 



    if (VAR_7) {

        if (av_dict_set_int(&VAR_0->metadata, "loop_start",

                            av_rescale(read32(VAR_0), AV_TIME_BASE,

                                       st->VAR_4->sample_rate),

                            0) < 0)

            return AVERROR(ENOMEM);

    } else {

        avio_skip(VAR_0->pb, 4);

    }



    st->start_time = 0;

    st->duration = read32(VAR_0);

    avpriv_set_pts_info(st, 64, 1, st->VAR_4->sample_rate);



    if (!VAR_8)

        start = read32(VAR_0);

    b->current_block = 0;

    b->block_count = read32(VAR_0);

    if (b->block_count > UINT16_MAX) {

        av_log(VAR_0, AV_LOG_WARNING, "too many blocks: %u\n", b->block_count);

        return AVERROR_INVALIDDATA;

    }



    b->block_size = read32(VAR_0);

    if (b->block_size > UINT32_MAX / st->VAR_4->channels)

        return AVERROR_INVALIDDATA;



    b->samples_per_block = read32(VAR_0);

    b->last_block_used_bytes = read32(VAR_0);

    b->last_block_samples = read32(VAR_0);

    b->last_block_size = read32(VAR_0);

    if (b->last_block_size > UINT32_MAX / st->VAR_4->channels)

        return AVERROR_INVALIDDATA;

    if (b->last_block_used_bytes > b->last_block_size)

        return AVERROR_INVALIDDATA;





    if (VAR_4 == AV_CODEC_ID_ADPCM_THP || VAR_4 == AV_CODEC_ID_ADPCM_THP_LE) {

        int VAR_9;



        avio_skip(VAR_0->pb, pos + toffset - avio_tell(VAR_0->pb));

        if (!VAR_8)

            toffset = read32(VAR_0) + 16LL;

        else

            toffset = toffset + read32(VAR_0) + st->VAR_4->channels * 8 - 8;

        if (toffset > size)

            return AVERROR_INVALIDDATA;



        avio_skip(VAR_0->pb, pos + toffset - avio_tell(VAR_0->pb));

        b->table = av_mallocz(32 * st->VAR_4->channels);

        if (!b->table)

            return AVERROR(ENOMEM);



        for (VAR_9 = 0; VAR_9 < st->VAR_4->channels; VAR_9++) {

            if (avio_read(VAR_0->pb, b->table + VAR_9 * 32, 32) != 32) {

                VAR_6 = AVERROR_INVALIDDATA;

                goto fail;

            }

            avio_skip(VAR_0->pb, VAR_8 ? 14 : 24);

        }

    }



    if (size < (avio_tell(VAR_0->pb) - pos)) {

        VAR_6 = AVERROR_INVALIDDATA;

        goto fail;

    }



    avio_skip(VAR_0->pb, size - (avio_tell(VAR_0->pb) - pos));



    while (!avio_feof(VAR_0->pb)) {

        VAR_5 = avio_rl32(VAR_0->pb);

        size  = read32(VAR_0);

        if (size < 8) {

            VAR_6 = AVERROR_INVALIDDATA;

            goto fail;

        }

        size -= 8;

        switch (VAR_5) {

        case MKTAG('S','E','E','K'):

        case MKTAG('A','D','P','C'):

            if (VAR_4 != AV_CODEC_ID_ADPCM_THP &&

                VAR_4 != AV_CODEC_ID_ADPCM_THP_LE)

                goto skip;



            asize = b->block_count * st->VAR_4->channels * 4;

            if (size < asize) {

                VAR_6 = AVERROR_INVALIDDATA;

                goto fail;

            }

            if (b->adpc) {

                av_log(VAR_0, AV_LOG_WARNING, "skipping additional ADPC VAR_5\n");

                goto skip;

            } else {

                b->adpc = av_mallocz(asize);

                if (!b->adpc) {

                    VAR_6 = AVERROR(ENOMEM);

                    goto fail;

                }

                if (VAR_8 && VAR_4 != AV_CODEC_ID_ADPCM_THP_LE) {

                    

                    

                    int VAR_10;

                    for (VAR_10 = 0; VAR_10 < asize; VAR_10 += 2) {

                        b->adpc[VAR_10+1] = avio_r8(VAR_0->pb);

                        b->adpc[VAR_10]   = avio_r8(VAR_0->pb);

                    }

                } else {

                    avio_read(VAR_0->pb, b->adpc, asize);

                }

                avio_skip(VAR_0->pb, size - asize);

            }

            break;

        case MKTAG('D','A','T','A'):

            if ((start < avio_tell(VAR_0->pb)) ||

                (!b->adpc && (VAR_4 == AV_CODEC_ID_ADPCM_THP ||

                              VAR_4 == AV_CODEC_ID_ADPCM_THP_LE))) {

                VAR_6 = AVERROR_INVALIDDATA;

                goto fail;

            }

            avio_skip(VAR_0->pb, start - avio_tell(VAR_0->pb));



            if (VAR_8 && (VAR_4 == AV_CODEC_ID_ADPCM_THP ||

                          VAR_4 == AV_CODEC_ID_ADPCM_THP_LE))

                avio_skip(VAR_0->pb, 24);



            b->data_start = avio_tell(VAR_0->pb);



            if (!VAR_8 && (VAR_2 != 1 || VAR_3))

                avpriv_request_sample(VAR_0, "Version %d.%d", VAR_2, VAR_3);



            return 0;

        default:

            av_log(VAR_0, AV_LOG_WARNING, "skipping unknown VAR_5: %X\n", VAR_5);

skip:

            avio_skip(VAR_0->pb, size);

        }

    }



fail:

    read_close(VAR_0);



    return VAR_6;

}

static int FUNC_0(AVFormatContext *VAR_0)
{ BRSTMDemuxContext *b = VAR_0->priv_data; int VAR_1, VAR_2, VAR_3, VAR_4, VAR_5; int64_t h1offset, pos, toffset; uint32_t size, asize, start = 0; AVStream *st; int VAR_6 = AVERROR_EOF; int VAR_7 = 0; int VAR_8 = !strcmp("VAR_8", VAR_0->iformat->name); st = avformat_new_stream(VAR_0, NULL); if (!st)
return AVERROR(ENOMEM); st->VAR_4->codec_type = AVMEDIA_TYPE_AUDIO; avio_skip(VAR_0->pb, 4); VAR_1 = avio_rb16(VAR_0->pb); if (VAR_1 != 0xFEFF && VAR_1 != 0xFFFE) { av_log(VAR_0, AV_LOG_ERROR, "invalid byte order: %X\n", VAR_1); return AVERROR_INVALIDDATA; } if (VAR_1 == 0xFFFE)
b->little_endian = 1; if (!VAR_8) { VAR_2 = avio_r8(VAR_0->pb); VAR_3 = avio_r8(VAR_0->pb); avio_skip(VAR_0->pb, 4); size = read16(VAR_0); if (size < 14)
return AVERROR_INVALIDDATA; avio_skip(VAR_0->pb, size - 14); pos = avio_tell(VAR_0->pb); if (avio_rl32(VAR_0->pb) != MKTAG('H','E','A','D'))
return AVERROR_INVALIDDATA; } else { uint32_t info_offset = 0; uint16_t section_count, header_size, VAR_10; header_size = read16(VAR_0); avio_skip(VAR_0->pb, 4); avio_skip(VAR_0->pb, 4); section_count = read16(VAR_0); avio_skip(VAR_0->pb, 2); for (VAR_10 = 0; avio_tell(VAR_0->pb) < header_size && !(start && info_offset)
&& VAR_10 < section_count; VAR_10++) { uint16_t flag = read16(VAR_0); avio_skip(VAR_0->pb, 2); switch (flag) { case 0x4000:
info_offset = read32(VAR_0); read32(VAR_0); break; case 0x4001:
avio_skip(VAR_0->pb, 4); avio_skip(VAR_0->pb, 4); break; case 0x4002:
start = read32(VAR_0) + 8; avio_skip(VAR_0->pb, 4); break; case 0x4003:
avio_skip(VAR_0->pb, 4); avio_skip(VAR_0->pb, 4); break; } } if (!info_offset || !start)
return AVERROR_INVALIDDATA; avio_skip(VAR_0->pb, info_offset - avio_tell(VAR_0->pb)); pos = avio_tell(VAR_0->pb); if (avio_rl32(VAR_0->pb) != MKTAG('I','N','F','O'))
return AVERROR_INVALIDDATA; } size = read32(VAR_0); if (size < 192)
return AVERROR_INVALIDDATA; avio_skip(VAR_0->pb, 4); h1offset = read32(VAR_0); if (h1offset > size)
return AVERROR_INVALIDDATA; avio_skip(VAR_0->pb, 12); toffset = read32(VAR_0) + 16LL; if (toffset > size)
return AVERROR_INVALIDDATA; avio_skip(VAR_0->pb, pos + h1offset + 8 - avio_tell(VAR_0->pb)); VAR_4 = avio_r8(VAR_0->pb); switch (VAR_4) { case 0: VAR_4 = AV_CODEC_ID_PCM_S8_PLANAR;    break; case 1: VAR_4 = b->little_endian ?
AV_CODEC_ID_PCM_S16LE_PLANAR :
AV_CODEC_ID_PCM_S16BE_PLANAR; break; case 2: VAR_4 = b->little_endian ?
AV_CODEC_ID_ADPCM_THP_LE :
AV_CODEC_ID_ADPCM_THP;        break; default:
avpriv_request_sample(VAR_0, "VAR_4 %d", VAR_4); return AVERROR_PATCHWELCOME; } VAR_7 = avio_r8(VAR_0->pb); st->VAR_4->codec_id = VAR_4; st->VAR_4->channels = avio_r8(VAR_0->pb); if (!st->VAR_4->channels)
return AVERROR_INVALIDDATA; avio_skip(VAR_0->pb, 1); st->VAR_4->sample_rate = VAR_8 ? read32(VAR_0) : read16(VAR_0); if (!st->VAR_4->sample_rate)
return AVERROR_INVALIDDATA; if (!VAR_8)
avio_skip(VAR_0->pb, 2); if (VAR_7) { if (av_dict_set_int(&VAR_0->metadata, "loop_start",
av_rescale(read32(VAR_0), AV_TIME_BASE,
st->VAR_4->sample_rate),
0) < 0)
return AVERROR(ENOMEM); } else { avio_skip(VAR_0->pb, 4); } st->start_time = 0; st->duration = read32(VAR_0); avpriv_set_pts_info(st, 64, 1, st->VAR_4->sample_rate); if (!VAR_8)
start = read32(VAR_0); b->current_block = 0; b->block_count = read32(VAR_0); if (b->block_count > UINT16_MAX) { av_log(VAR_0, AV_LOG_WARNING, "too many blocks: %u\n", b->block_count); return AVERROR_INVALIDDATA; } b->block_size = read32(VAR_0); if (b->block_size > UINT32_MAX / st->VAR_4->channels)
return AVERROR_INVALIDDATA; b->samples_per_block = read32(VAR_0); b->last_block_used_bytes = read32(VAR_0); b->last_block_samples = read32(VAR_0); b->last_block_size = read32(VAR_0); if (b->last_block_size > UINT32_MAX / st->VAR_4->channels)
return AVERROR_INVALIDDATA; if (b->last_block_used_bytes > b->last_block_size)
return AVERROR_INVALIDDATA; if (VAR_4 == AV_CODEC_ID_ADPCM_THP || VAR_4 == AV_CODEC_ID_ADPCM_THP_LE) { int VAR_9; avio_skip(VAR_0->pb, pos + toffset - avio_tell(VAR_0->pb)); if (!VAR_8)
toffset = read32(VAR_0) + 16LL; else
toffset = toffset + read32(VAR_0) + st->VAR_4->channels * 8 - 8; if (toffset > size)
return AVERROR_INVALIDDATA; avio_skip(VAR_0->pb, pos + toffset - avio_tell(VAR_0->pb)); b->table = av_mallocz(32 * st->VAR_4->channels); if (!b->table)
return AVERROR(ENOMEM); for (VAR_9 = 0; VAR_9 < st->VAR_4->channels; VAR_9++) { if (avio_read(VAR_0->pb, b->table + VAR_9 * 32, 32) != 32) { VAR_6 = AVERROR_INVALIDDATA; goto fail; } avio_skip(VAR_0->pb, VAR_8 ? 14 : 24); } } if (size < (avio_tell(VAR_0->pb) - pos)) { VAR_6 = AVERROR_INVALIDDATA; goto fail; } avio_skip(VAR_0->pb, size - (avio_tell(VAR_0->pb) - pos)); while (!avio_feof(VAR_0->pb)) { VAR_5 = avio_rl32(VAR_0->pb); size  = read32(VAR_0); if (size < 8) { VAR_6 = AVERROR_INVALIDDATA; goto fail; } size -= 8; switch (VAR_5) { case MKTAG('S','E','E','K'):
case MKTAG('A','D','P','C'):
if (VAR_4 != AV_CODEC_ID_ADPCM_THP &&
VAR_4 != AV_CODEC_ID_ADPCM_THP_LE)
goto skip; asize = b->block_count * st->VAR_4->channels * 4; if (size < asize) { VAR_6 = AVERROR_INVALIDDATA; goto fail; } if (b->adpc) { av_log(VAR_0, AV_LOG_WARNING, "skipping additional ADPC VAR_5\n"); goto skip; } else { b->adpc = av_mallocz(asize); if (!b->adpc) { VAR_6 = AVERROR(ENOMEM); goto fail; } if (VAR_8 && VAR_4 != AV_CODEC_ID_ADPCM_THP_LE) { int VAR_10; for (VAR_10 = 0; VAR_10 < asize; VAR_10 += 2) { b->adpc[VAR_10+1] = avio_r8(VAR_0->pb); b->adpc[VAR_10]   = avio_r8(VAR_0->pb); } } else { avio_read(VAR_0->pb, b->adpc, asize); } avio_skip(VAR_0->pb, size - asize); } break; case MKTAG('D','A','T','A'):
if ((start < avio_tell(VAR_0->pb)) ||
(!b->adpc && (VAR_4 == AV_CODEC_ID_ADPCM_THP ||
VAR_4 == AV_CODEC_ID_ADPCM_THP_LE))) { VAR_6 = AVERROR_INVALIDDATA; goto fail; } avio_skip(VAR_0->pb, start - avio_tell(VAR_0->pb)); if (VAR_8 && (VAR_4 == AV_CODEC_ID_ADPCM_THP ||
VAR_4 == AV_CODEC_ID_ADPCM_THP_LE))
avio_skip(VAR_0->pb, 24); b->data_start = avio_tell(VAR_0->pb); if (!VAR_8 && (VAR_2 != 1 || VAR_3))
avpriv_request_sample(VAR_0, "Version %d.%d", VAR_2, VAR_3); return 0; default:
av_log(VAR_0, AV_LOG_WARNING, "skipping unknown VAR_5: %X\n", VAR_5); skip:
avio_skip(VAR_0->pb, size); } } fail:
read_close(VAR_0); return VAR_6; }