static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)

{

    int size;

    FFMContext *ffm = s->priv_data;

    int duration, ret;



    switch(ffm->read_state) {

    case READ_HEADER:

        if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)

            return ret;



        av_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",

               avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);

        if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=

            FRAME_HEADER_SIZE)

            return -1;

        if (ffm->header[1] & FLAG_DTS)

            if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)

                return -1;

        ffm->read_state = READ_DATA;

        /* fall thru */

    case READ_DATA:

        size = AV_RB24(ffm->header + 2);

        if ((ret = ffm_is_avail_data(s, size)) < 0)

            return ret;



        duration = AV_RB24(ffm->header + 5);



        av_new_packet(pkt, size);

        pkt->stream_index = ffm->header[0];

        if ((unsigned)pkt->stream_index >= s->nb_streams) {

            av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);

            av_free_packet(pkt);

            ffm->read_state = READ_HEADER;

            return -1;

        }

        pkt->pos = avio_tell(s->pb);

        if (ffm->header[1] & FLAG_KEY_FRAME)

            pkt->flags |= AV_PKT_FLAG_KEY;



        ffm->read_state = READ_HEADER;

        if (ffm_read_data(s, pkt->data, size, 0) != size) {

            /* bad case: desynchronized packet. we cancel all the packet loading */

            av_free_packet(pkt);

            return -1;

        }

        pkt->pts = AV_RB64(ffm->header+8);

        if (ffm->header[1] & FLAG_DTS)

            pkt->dts = pkt->pts - AV_RB32(ffm->header+16);

        else

            pkt->dts = pkt->pts;

        pkt->duration = duration;

        break;

    }

    return 0;

}

static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)

{

    int size;

    FFMContext *ffm = s->priv_data;

    int duration, ret;



    switch(ffm->read_state) {

    case READ_HEADER:

        if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)

            return ret;



        av_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",

               avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);

        if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=

            FRAME_HEADER_SIZE)

            return -1;

        if (ffm->header[1] & FLAG_DTS)

            if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)

                return -1;

        ffm->read_state = READ_DATA;

        

    case READ_DATA:

        size = AV_RB24(ffm->header + 2);

        if ((ret = ffm_is_avail_data(s, size)) < 0)

            return ret;



        duration = AV_RB24(ffm->header + 5);



        av_new_packet(pkt, size);

        pkt->stream_index = ffm->header[0];

        if ((unsigned)pkt->stream_index >= s->nb_streams) {

            av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);

            av_free_packet(pkt);

            ffm->read_state = READ_HEADER;

            return -1;

        }

        pkt->pos = avio_tell(s->pb);

        if (ffm->header[1] & FLAG_KEY_FRAME)

            pkt->flags |= AV_PKT_FLAG_KEY;



        ffm->read_state = READ_HEADER;

        if (ffm_read_data(s, pkt->data, size, 0) != size) {

            

            av_free_packet(pkt);

            return -1;

        }

        pkt->pts = AV_RB64(ffm->header+8);

        if (ffm->header[1] & FLAG_DTS)

            pkt->dts = pkt->pts - AV_RB32(ffm->header+16);

        else

            pkt->dts = pkt->pts;

        pkt->duration = duration;

        break;

    }

    return 0;

}

static int FUNC_0(AVFormatContext *VAR_0, AVPacket *VAR_1)

{

    int VAR_2;

    FFMContext *ffm = VAR_0->priv_data;

    int VAR_3, VAR_4;



    switch(ffm->read_state) {

    case READ_HEADER:

        if ((VAR_4 = ffm_is_avail_data(VAR_0, FRAME_HEADER_SIZE+4)) < 0)

            return VAR_4;



        av_dlog(VAR_0, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" VAR_2=%"PRIx64"\n",

               avio_tell(VAR_0->pb), VAR_0->pb->pos, ffm->write_index, ffm->file_size);

        if (ffm_read_data(VAR_0, ffm->header, FRAME_HEADER_SIZE, 1) !=

            FRAME_HEADER_SIZE)

            return -1;

        if (ffm->header[1] & FLAG_DTS)

            if (ffm_read_data(VAR_0, ffm->header+16, 4, 1) != 4)

                return -1;

        ffm->read_state = READ_DATA;

        

    case READ_DATA:

        VAR_2 = AV_RB24(ffm->header + 2);

        if ((VAR_4 = ffm_is_avail_data(VAR_0, VAR_2)) < 0)

            return VAR_4;



        VAR_3 = AV_RB24(ffm->header + 5);



        av_new_packet(VAR_1, VAR_2);

        VAR_1->stream_index = ffm->header[0];

        if ((unsigned)VAR_1->stream_index >= VAR_0->nb_streams) {

            av_log(VAR_0, AV_LOG_ERROR, "invalid stream index %d\n", VAR_1->stream_index);

            av_free_packet(VAR_1);

            ffm->read_state = READ_HEADER;

            return -1;

        }

        VAR_1->pos = avio_tell(VAR_0->pb);

        if (ffm->header[1] & FLAG_KEY_FRAME)

            VAR_1->flags |= AV_PKT_FLAG_KEY;



        ffm->read_state = READ_HEADER;

        if (ffm_read_data(VAR_0, VAR_1->data, VAR_2, 0) != VAR_2) {

            

            av_free_packet(VAR_1);

            return -1;

        }

        VAR_1->pts = AV_RB64(ffm->header+8);

        if (ffm->header[1] & FLAG_DTS)

            VAR_1->dts = VAR_1->pts - AV_RB32(ffm->header+16);

        else

            VAR_1->dts = VAR_1->pts;

        VAR_1->VAR_3 = VAR_3;

        break;

    }

    return 0;

}

static int FUNC_0(AVFormatContext *VAR_0, AVPacket *VAR_1)
{ int VAR_2; FFMContext *ffm = VAR_0->priv_data; int VAR_3, VAR_4; switch(ffm->read_state) { case READ_HEADER:
if ((VAR_4 = ffm_is_avail_data(VAR_0, FRAME_HEADER_SIZE+4)) < 0)
return VAR_4; av_dlog(VAR_0, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" VAR_2=%"PRIx64"\n",
avio_tell(VAR_0->pb), VAR_0->pb->pos, ffm->write_index, ffm->file_size); if (ffm_read_data(VAR_0, ffm->header, FRAME_HEADER_SIZE, 1) !=
FRAME_HEADER_SIZE)
return -1; if (ffm->header[1] & FLAG_DTS)
if (ffm_read_data(VAR_0, ffm->header+16, 4, 1) != 4)
return -1; ffm->read_state = READ_DATA; case READ_DATA:
VAR_2 = AV_RB24(ffm->header + 2); if ((VAR_4 = ffm_is_avail_data(VAR_0, VAR_2)) < 0)
return VAR_4; VAR_3 = AV_RB24(ffm->header + 5); av_new_packet(VAR_1, VAR_2); VAR_1->stream_index = ffm->header[0]; if ((unsigned)VAR_1->stream_index >= VAR_0->nb_streams) { av_log(VAR_0, AV_LOG_ERROR, "invalid stream index %d\n", VAR_1->stream_index); av_free_packet(VAR_1); ffm->read_state = READ_HEADER; return -1; } VAR_1->pos = avio_tell(VAR_0->pb); if (ffm->header[1] & FLAG_KEY_FRAME)
VAR_1->flags |= AV_PKT_FLAG_KEY; ffm->read_state = READ_HEADER; if (ffm_read_data(VAR_0, VAR_1->data, VAR_2, 0) != VAR_2) { av_free_packet(VAR_1); return -1; } VAR_1->pts = AV_RB64(ffm->header+8); if (ffm->header[1] & FLAG_DTS)
VAR_1->dts = VAR_1->pts - AV_RB32(ffm->header+16); else
VAR_1->dts = VAR_1->pts; VAR_1->VAR_3 = VAR_3; break; } return 0; }