static int vid_read_packet(AVFormatContext *s,

                           AVPacket *pkt)

{

    BVID_DemuxContext *vid = s->priv_data;

    AVIOContext *pb = s->pb;

    unsigned char block_type;

    int audio_length;

    int ret_value;



    if(vid->is_finished || pb->eof_reached)

        return AVERROR(EIO);



    block_type = avio_r8(pb);

    switch(block_type){

        case PALETTE_BLOCK:

            avio_seek(pb, -1, SEEK_CUR);     // include block type

            ret_value = av_get_packet(pb, pkt, 3 * 256 + 1);

            if(ret_value != 3 * 256 + 1){

                av_free_packet(pkt);

                return AVERROR(EIO);

            }

            pkt->stream_index = 0;

            return ret_value;



        case FIRST_AUDIO_BLOCK:

            avio_rl16(pb);

            // soundblaster DAC used for sample rate, as on specification page (link above)

            s->streams[1]->codec->sample_rate = 1000000 / (256 - avio_r8(pb));

            s->streams[1]->codec->bit_rate = s->streams[1]->codec->channels * s->streams[1]->codec->sample_rate * s->streams[1]->codec->bits_per_coded_sample;

        case AUDIO_BLOCK:

            audio_length = avio_rl16(pb);

            ret_value = av_get_packet(pb, pkt, audio_length);

            pkt->stream_index = 1;

            return ret_value != audio_length ? AVERROR(EIO) : ret_value;



        case VIDEO_P_FRAME:

        case VIDEO_YOFF_P_FRAME:

        case VIDEO_I_FRAME:

            return read_frame(vid, pb, pkt, block_type, s,

                              s->streams[0]->codec->width * s->streams[0]->codec->height);



        case EOF_BLOCK:

            if(vid->nframes != 0)

                av_log(s, AV_LOG_VERBOSE, "reached terminating character but not all frames read.\n");

            vid->is_finished = 1;

            return AVERROR(EIO);

        default:

            av_log(s, AV_LOG_ERROR, "unknown block (character = %c, decimal = %d, hex = %x)!!!\n",

                   block_type, block_type, block_type); return -1;

    }

}

static int vid_read_packet(AVFormatContext *s,

                           AVPacket *pkt)

{

    BVID_DemuxContext *vid = s->priv_data;

    AVIOContext *pb = s->pb;

    unsigned char block_type;

    int audio_length;

    int ret_value;



    if(vid->is_finished || pb->eof_reached)

        return AVERROR(EIO);



    block_type = avio_r8(pb);

    switch(block_type){

        case PALETTE_BLOCK:

            avio_seek(pb, -1, SEEK_CUR);     

            ret_value = av_get_packet(pb, pkt, 3 * 256 + 1);

            if(ret_value != 3 * 256 + 1){

                av_free_packet(pkt);

                return AVERROR(EIO);

            }

            pkt->stream_index = 0;

            return ret_value;



        case FIRST_AUDIO_BLOCK:

            avio_rl16(pb);

            

            s->streams[1]->codec->sample_rate = 1000000 / (256 - avio_r8(pb));

            s->streams[1]->codec->bit_rate = s->streams[1]->codec->channels * s->streams[1]->codec->sample_rate * s->streams[1]->codec->bits_per_coded_sample;

        case AUDIO_BLOCK:

            audio_length = avio_rl16(pb);

            ret_value = av_get_packet(pb, pkt, audio_length);

            pkt->stream_index = 1;

            return ret_value != audio_length ? AVERROR(EIO) : ret_value;



        case VIDEO_P_FRAME:

        case VIDEO_YOFF_P_FRAME:

        case VIDEO_I_FRAME:

            return read_frame(vid, pb, pkt, block_type, s,

                              s->streams[0]->codec->width * s->streams[0]->codec->height);



        case EOF_BLOCK:

            if(vid->nframes != 0)

                av_log(s, AV_LOG_VERBOSE, "reached terminating character but not all frames read.\n");

            vid->is_finished = 1;

            return AVERROR(EIO);

        default:

            av_log(s, AV_LOG_ERROR, "unknown block (character = %c, decimal = %d, hex = %x)!!!\n",

                   block_type, block_type, block_type); return -1;

    }

}

static int FUNC_0(AVFormatContext *VAR_0,

                           AVPacket *VAR_1)

{

    BVID_DemuxContext *vid = VAR_0->priv_data;

    AVIOContext *pb = VAR_0->pb;

    unsigned char VAR_2;

    int VAR_3;

    int VAR_4;



    if(vid->is_finished || pb->eof_reached)

        return AVERROR(EIO);



    VAR_2 = avio_r8(pb);

    switch(VAR_2){

        case PALETTE_BLOCK:

            avio_seek(pb, -1, SEEK_CUR);     

            VAR_4 = av_get_packet(pb, VAR_1, 3 * 256 + 1);

            if(VAR_4 != 3 * 256 + 1){

                av_free_packet(VAR_1);

                return AVERROR(EIO);

            }

            VAR_1->stream_index = 0;

            return VAR_4;



        case FIRST_AUDIO_BLOCK:

            avio_rl16(pb);

            

            VAR_0->streams[1]->codec->sample_rate = 1000000 / (256 - avio_r8(pb));

            VAR_0->streams[1]->codec->bit_rate = VAR_0->streams[1]->codec->channels * VAR_0->streams[1]->codec->sample_rate * VAR_0->streams[1]->codec->bits_per_coded_sample;

        case AUDIO_BLOCK:

            VAR_3 = avio_rl16(pb);

            VAR_4 = av_get_packet(pb, VAR_1, VAR_3);

            VAR_1->stream_index = 1;

            return VAR_4 != VAR_3 ? AVERROR(EIO) : VAR_4;



        case VIDEO_P_FRAME:

        case VIDEO_YOFF_P_FRAME:

        case VIDEO_I_FRAME:

            return read_frame(vid, pb, VAR_1, VAR_2, VAR_0,

                              VAR_0->streams[0]->codec->width * VAR_0->streams[0]->codec->height);



        case EOF_BLOCK:

            if(vid->nframes != 0)

                av_log(VAR_0, AV_LOG_VERBOSE, "reached terminating character but not all frames read.\n");

            vid->is_finished = 1;

            return AVERROR(EIO);

        default:

            av_log(VAR_0, AV_LOG_ERROR, "unknown block (character = %c, decimal = %d, hex = %x)!!!\n",

                   VAR_2, VAR_2, VAR_2); return -1;

    }

}

static int FUNC_0(AVFormatContext *VAR_0,
AVPacket *VAR_1)
{ BVID_DemuxContext *vid = VAR_0->priv_data; AVIOContext *pb = VAR_0->pb; unsigned char VAR_2; int VAR_3; int VAR_4; if(vid->is_finished || pb->eof_reached)
return AVERROR(EIO); VAR_2 = avio_r8(pb); switch(VAR_2){ case PALETTE_BLOCK:
avio_seek(pb, -1, SEEK_CUR); VAR_4 = av_get_packet(pb, VAR_1, 3 * 256 + 1); if(VAR_4 != 3 * 256 + 1){ av_free_packet(VAR_1); return AVERROR(EIO); } VAR_1->stream_index = 0; return VAR_4; case FIRST_AUDIO_BLOCK:
avio_rl16(pb); VAR_0->streams[1]->codec->sample_rate = 1000000 / (256 - avio_r8(pb)); VAR_0->streams[1]->codec->bit_rate = VAR_0->streams[1]->codec->channels * VAR_0->streams[1]->codec->sample_rate * VAR_0->streams[1]->codec->bits_per_coded_sample; case AUDIO_BLOCK:
VAR_3 = avio_rl16(pb); VAR_4 = av_get_packet(pb, VAR_1, VAR_3); VAR_1->stream_index = 1; return VAR_4 != VAR_3 ? AVERROR(EIO) : VAR_4; case VIDEO_P_FRAME:
case VIDEO_YOFF_P_FRAME:
case VIDEO_I_FRAME:
return read_frame(vid, pb, VAR_1, VAR_2, VAR_0,
VAR_0->streams[0]->codec->width * VAR_0->streams[0]->codec->height); case EOF_BLOCK:
if(vid->nframes != 0)
av_log(VAR_0, AV_LOG_VERBOSE, "reached terminating character but not all frames read.\n"); vid->is_finished = 1; return AVERROR(EIO); default:
av_log(VAR_0, AV_LOG_ERROR, "unknown block (character = %c, decimal = %d, hex = %x)!!!\n",
VAR_2, VAR_2, VAR_2); return -1; } }