static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code)

{

    AVFormatContext *s = nut->avf;

    AVIOContext *bc    = s->pb;

    int size, stream_id, discard;

    int64_t pts, last_IP_pts;

    StreamContext *stc;

    uint8_t header_idx;



    size = decode_frame_header(nut, &pts, &stream_id, &header_idx, frame_code);

    if (size < 0)

        return size;



    stc = &nut->stream[stream_id];



    if (stc->last_flags & FLAG_KEY)

        stc->skip_until_key_frame = 0;



    discard     = s->streams[stream_id]->discard;

    last_IP_pts = s->streams[stream_id]->last_IP_pts;

    if ((discard >= AVDISCARD_NONKEY && !(stc->last_flags & FLAG_KEY)) ||

        (discard >= AVDISCARD_BIDIR  && last_IP_pts != AV_NOPTS_VALUE &&

         last_IP_pts > pts) ||

        discard >= AVDISCARD_ALL ||

        stc->skip_until_key_frame) {

        avio_skip(bc, size);

        return 1;

    }



    if (av_new_packet(pkt, size + nut->header_len[header_idx]) < 0)

        return AVERROR(ENOMEM);

    memcpy(pkt->data, nut->header[header_idx], nut->header_len[header_idx]);

    pkt->pos = avio_tell(bc); // FIXME

    if (stc->last_flags & FLAG_SM_DATA) {

        int sm_size;

        if (read_sm_data(s, bc, pkt, 0, pkt->pos + size) < 0)

            return AVERROR_INVALIDDATA;

        if (read_sm_data(s, bc, pkt, 1, pkt->pos + size) < 0)

            return AVERROR_INVALIDDATA;

        sm_size = avio_tell(bc) - pkt->pos;

        size      -= sm_size;

        pkt->size -= sm_size;

    }



    avio_read(bc, pkt->data + nut->header_len[header_idx], size);



    pkt->stream_index = stream_id;

    if (stc->last_flags & FLAG_KEY)

        pkt->flags |= AV_PKT_FLAG_KEY;

    pkt->pts = pts;



    return 0;

}

static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code)

{

    AVFormatContext *s = nut->avf;

    AVIOContext *bc    = s->pb;

    int size, stream_id, discard;

    int64_t pts, last_IP_pts;

    StreamContext *stc;

    uint8_t header_idx;



    size = decode_frame_header(nut, &pts, &stream_id, &header_idx, frame_code);

    if (size < 0)

        return size;



    stc = &nut->stream[stream_id];



    if (stc->last_flags & FLAG_KEY)

        stc->skip_until_key_frame = 0;



    discard     = s->streams[stream_id]->discard;

    last_IP_pts = s->streams[stream_id]->last_IP_pts;

    if ((discard >= AVDISCARD_NONKEY && !(stc->last_flags & FLAG_KEY)) ||

        (discard >= AVDISCARD_BIDIR  && last_IP_pts != AV_NOPTS_VALUE &&

         last_IP_pts > pts) ||

        discard >= AVDISCARD_ALL ||

        stc->skip_until_key_frame) {

        avio_skip(bc, size);

        return 1;

    }



    if (av_new_packet(pkt, size + nut->header_len[header_idx]) < 0)

        return AVERROR(ENOMEM);

    memcpy(pkt->data, nut->header[header_idx], nut->header_len[header_idx]);

    pkt->pos = avio_tell(bc); 

    if (stc->last_flags & FLAG_SM_DATA) {

        int sm_size;

        if (read_sm_data(s, bc, pkt, 0, pkt->pos + size) < 0)

            return AVERROR_INVALIDDATA;

        if (read_sm_data(s, bc, pkt, 1, pkt->pos + size) < 0)

            return AVERROR_INVALIDDATA;

        sm_size = avio_tell(bc) - pkt->pos;

        size      -= sm_size;

        pkt->size -= sm_size;

    }



    avio_read(bc, pkt->data + nut->header_len[header_idx], size);



    pkt->stream_index = stream_id;

    if (stc->last_flags & FLAG_KEY)

        pkt->flags |= AV_PKT_FLAG_KEY;

    pkt->pts = pts;



    return 0;

}

    avio_read(bc, pkt->data + nut->header_len[header_idx], size);
static int FUNC_0(NUTContext *VAR_0, AVPacket *VAR_1, int VAR_2)

{

    AVFormatContext *s = VAR_0->avf;

    AVIOContext *bc    = s->pb;

    int VAR_3, VAR_4, VAR_5;

    int64_t pts, last_IP_pts;

    StreamContext *stc;

    uint8_t header_idx;



    VAR_3 = decode_frame_header(VAR_0, &pts, &VAR_4, &header_idx, VAR_2);

    if (VAR_3 < 0)

        return VAR_3;



    stc = &VAR_0->stream[VAR_4];



    if (stc->last_flags & FLAG_KEY)

        stc->skip_until_key_frame = 0;



    VAR_5     = s->streams[VAR_4]->VAR_5;

    last_IP_pts = s->streams[VAR_4]->last_IP_pts;

    if ((VAR_5 >= AVDISCARD_NONKEY && !(stc->last_flags & FLAG_KEY)) ||

        (VAR_5 >= AVDISCARD_BIDIR  && last_IP_pts != AV_NOPTS_VALUE &&

         last_IP_pts > pts) ||

        VAR_5 >= AVDISCARD_ALL ||

        stc->skip_until_key_frame) {

        avio_skip(bc, VAR_3);

        return 1;

    }



    if (av_new_packet(VAR_1, VAR_3 + VAR_0->header_len[header_idx]) < 0)

        return AVERROR(ENOMEM);

    memcpy(VAR_1->data, VAR_0->header[header_idx], VAR_0->header_len[header_idx]);

    VAR_1->pos = avio_tell(bc); 

    if (stc->last_flags & FLAG_SM_DATA) {

        int VAR_6;

        if (read_sm_data(s, bc, VAR_1, 0, VAR_1->pos + VAR_3) < 0)

            return AVERROR_INVALIDDATA;

        if (read_sm_data(s, bc, VAR_1, 1, VAR_1->pos + VAR_3) < 0)

            return AVERROR_INVALIDDATA;

        VAR_6 = avio_tell(bc) - VAR_1->pos;

        VAR_3      -= VAR_6;

        VAR_1->VAR_3 -= VAR_6;

    }



    avio_read(bc, VAR_1->data + VAR_0->header_len[header_idx], VAR_3);



    VAR_1->stream_index = VAR_4;

    if (stc->last_flags & FLAG_KEY)

        VAR_1->flags |= AV_PKT_FLAG_KEY;

    VAR_1->pts = pts;



    return 0;

}

static int FUNC_0(NUTContext *VAR_0, AVPacket *VAR_1, int VAR_2)
{ AVFormatContext *s = VAR_0->avf; AVIOContext *bc    = s->pb; int VAR_3, VAR_4, VAR_5; int64_t pts, last_IP_pts; StreamContext *stc; uint8_t header_idx; VAR_3 = decode_frame_header(VAR_0, &pts, &VAR_4, &header_idx, VAR_2); if (VAR_3 < 0)
return VAR_3; stc = &VAR_0->stream[VAR_4]; if (stc->last_flags & FLAG_KEY)
stc->skip_until_key_frame = 0; VAR_5     = s->streams[VAR_4]->VAR_5; last_IP_pts = s->streams[VAR_4]->last_IP_pts; if ((VAR_5 >= AVDISCARD_NONKEY && !(stc->last_flags & FLAG_KEY)) ||
(VAR_5 >= AVDISCARD_BIDIR  && last_IP_pts != AV_NOPTS_VALUE &&
last_IP_pts > pts) ||
VAR_5 >= AVDISCARD_ALL ||
stc->skip_until_key_frame) { avio_skip(bc, VAR_3); return 1; } if (av_new_packet(VAR_1, VAR_3 + VAR_0->header_len[header_idx]) < 0)
return AVERROR(ENOMEM); memcpy(VAR_1->data, VAR_0->header[header_idx], VAR_0->header_len[header_idx]); VAR_1->pos = avio_tell(bc); if (stc->last_flags & FLAG_SM_DATA) { int VAR_6; if (read_sm_data(s, bc, VAR_1, 0, VAR_1->pos + VAR_3) < 0)
return AVERROR_INVALIDDATA; if (read_sm_data(s, bc, VAR_1, 1, VAR_1->pos + VAR_3) < 0)
return AVERROR_INVALIDDATA; VAR_6 = avio_tell(bc) - VAR_1->pos; VAR_3      -= VAR_6; VAR_1->VAR_3 -= VAR_6; } avio_read(bc, VAR_1->data + VAR_0->header_len[header_idx], VAR_3); VAR_1->stream_index = VAR_4; if (stc->last_flags & FLAG_KEY)
VAR_1->flags |= AV_PKT_FLAG_KEY; VAR_1->pts = pts; return 0; }