static int wsaud_read_packet(AVFormatContext *s,

                             AVPacket *pkt)

{

    AVIOContext *pb = s->pb;

    unsigned char preamble[AUD_CHUNK_PREAMBLE_SIZE];

    unsigned int chunk_size;

    int ret = 0;

    AVStream *st = s->streams[0];



    if (avio_read(pb, preamble, AUD_CHUNK_PREAMBLE_SIZE) !=

        AUD_CHUNK_PREAMBLE_SIZE)

        return AVERROR(EIO);



    /* validate the chunk */

    if (AV_RL32(&preamble[4]) != AUD_CHUNK_SIGNATURE)

        return AVERROR_INVALIDDATA;



    chunk_size = AV_RL16(&preamble[0]);



    if (st->codec->codec_id == AV_CODEC_ID_WESTWOOD_SND1) {

        /* For Westwood SND1 audio we need to add the output size and input

           size to the start of the packet to match what is in VQA.

           Specifically, this is needed to signal when a packet should be

           decoding as raw 8-bit pcm or variable-size ADPCM. */

        int out_size = AV_RL16(&preamble[2]);

        if ((ret = av_new_packet(pkt, chunk_size + 4)))

            return ret;

        if ((ret = avio_read(pb, &pkt->data[4], chunk_size)) != chunk_size)

            return ret < 0 ? ret : AVERROR(EIO);

        AV_WL16(&pkt->data[0], out_size);

        AV_WL16(&pkt->data[2], chunk_size);



        pkt->duration = out_size;

    } else {

        ret = av_get_packet(pb, pkt, chunk_size);

        if (ret != chunk_size)

            return AVERROR(EIO);



        /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */

        pkt->duration = (chunk_size * 2) / st->codec->channels;

    }

    pkt->stream_index = st->index;



    return ret;

}

static int wsaud_read_packet(AVFormatContext *s,

                             AVPacket *pkt)

{

    AVIOContext *pb = s->pb;

    unsigned char preamble[AUD_CHUNK_PREAMBLE_SIZE];

    unsigned int chunk_size;

    int ret = 0;

    AVStream *st = s->streams[0];



    if (avio_read(pb, preamble, AUD_CHUNK_PREAMBLE_SIZE) !=

        AUD_CHUNK_PREAMBLE_SIZE)

        return AVERROR(EIO);



    

    if (AV_RL32(&preamble[4]) != AUD_CHUNK_SIGNATURE)

        return AVERROR_INVALIDDATA;



    chunk_size = AV_RL16(&preamble[0]);



    if (st->codec->codec_id == AV_CODEC_ID_WESTWOOD_SND1) {

        







        int out_size = AV_RL16(&preamble[2]);

        if ((ret = av_new_packet(pkt, chunk_size + 4)))

            return ret;

        if ((ret = avio_read(pb, &pkt->data[4], chunk_size)) != chunk_size)

            return ret < 0 ? ret : AVERROR(EIO);

        AV_WL16(&pkt->data[0], out_size);

        AV_WL16(&pkt->data[2], chunk_size);



        pkt->duration = out_size;

    } else {

        ret = av_get_packet(pb, pkt, chunk_size);

        if (ret != chunk_size)

            return AVERROR(EIO);



        

        pkt->duration = (chunk_size * 2) / st->codec->channels;

    }

    pkt->stream_index = st->index;



    return ret;

}

static int FUNC_0(AVFormatContext *VAR_0,

                             AVPacket *VAR_1)

{

    AVIOContext *pb = VAR_0->pb;

    unsigned char VAR_2[AUD_CHUNK_PREAMBLE_SIZE];

    unsigned int VAR_3;

    int VAR_4 = 0;

    AVStream *st = VAR_0->streams[0];



    if (avio_read(pb, VAR_2, AUD_CHUNK_PREAMBLE_SIZE) !=

        AUD_CHUNK_PREAMBLE_SIZE)

        return AVERROR(EIO);



    

    if (AV_RL32(&VAR_2[4]) != AUD_CHUNK_SIGNATURE)

        return AVERROR_INVALIDDATA;



    VAR_3 = AV_RL16(&VAR_2[0]);



    if (st->codec->codec_id == AV_CODEC_ID_WESTWOOD_SND1) {

        







        int VAR_5 = AV_RL16(&VAR_2[2]);

        if ((VAR_4 = av_new_packet(VAR_1, VAR_3 + 4)))

            return VAR_4;

        if ((VAR_4 = avio_read(pb, &VAR_1->data[4], VAR_3)) != VAR_3)

            return VAR_4 < 0 ? VAR_4 : AVERROR(EIO);

        AV_WL16(&VAR_1->data[0], VAR_5);

        AV_WL16(&VAR_1->data[2], VAR_3);



        VAR_1->duration = VAR_5;

    } else {

        VAR_4 = av_get_packet(pb, VAR_1, VAR_3);

        if (VAR_4 != VAR_3)

            return AVERROR(EIO);



        

        VAR_1->duration = (VAR_3 * 2) / st->codec->channels;

    }

    VAR_1->stream_index = st->index;



    return VAR_4;

}

static int FUNC_0(AVFormatContext *VAR_0,
AVPacket *VAR_1)
{ AVIOContext *pb = VAR_0->pb; unsigned char VAR_2[AUD_CHUNK_PREAMBLE_SIZE]; unsigned int VAR_3; int VAR_4 = 0; AVStream *st = VAR_0->streams[0]; if (avio_read(pb, VAR_2, AUD_CHUNK_PREAMBLE_SIZE) !=
AUD_CHUNK_PREAMBLE_SIZE)
return AVERROR(EIO); if (AV_RL32(&VAR_2[4]) != AUD_CHUNK_SIGNATURE)
return AVERROR_INVALIDDATA; VAR_3 = AV_RL16(&VAR_2[0]); if (st->codec->codec_id == AV_CODEC_ID_WESTWOOD_SND1) { int VAR_5 = AV_RL16(&VAR_2[2]); if ((VAR_4 = av_new_packet(VAR_1, VAR_3 + 4)))
return VAR_4; if ((VAR_4 = avio_read(pb, &VAR_1->data[4], VAR_3)) != VAR_3)
return VAR_4 < 0 ? VAR_4 : AVERROR(EIO); AV_WL16(&VAR_1->data[0], VAR_5); AV_WL16(&VAR_1->data[2], VAR_3); VAR_1->duration = VAR_5; } else { VAR_4 = av_get_packet(pb, VAR_1, VAR_3); if (VAR_4 != VAR_3)
return AVERROR(EIO); VAR_1->duration = (VAR_3 * 2) / st->codec->channels; } VAR_1->stream_index = st->index; return VAR_4; }