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

    AVFormatContext *s= nut->avf;

    StreamContext *stream;

    ByteIOContext *bc = &s->pb;

    int size, flags, size_mul, size_lsb, stream_id;

    int key_frame = 0;

    int64_t pts = 0;

    const int prefix_len= frame_type == 2 ? 8+1 : 1;

    const int64_t frame_start= url_ftell(bc) - prefix_len;



    flags= nut->frame_code[frame_code].flags;

    size_mul= nut->frame_code[frame_code].size_mul;

    size_lsb= nut->frame_code[frame_code].size_lsb;

    stream_id= nut->frame_code[frame_code].stream_id_plus1 - 1;



    if(flags & FLAG_FRAME_TYPE){

        reset(s);

        if(get_packetheader(nut, bc, prefix_len, 0) < 0)

            return -1;

        if(frame_type!=2)

            frame_type= 1;

    }



    if(stream_id==-1)

        stream_id= get_v(bc);

    if(stream_id >= s->nb_streams){

        av_log(s, AV_LOG_ERROR, "illegal stream_id\n");

        return -1;

    }

    stream= &nut->stream[stream_id];



//    av_log(s, AV_LOG_DEBUG, "ft:%d ppts:%d %d %d\n", frame_type, stream->lru_pts_delta[0], stream->lru_pts_delta[1], stream->lru_pts_delta[2]);

    

    if(flags & FLAG_PRED_KEY_FRAME){

        if(flags & FLAG_KEY_FRAME)

            key_frame= !stream->last_key_frame;

        else

            key_frame= stream->last_key_frame;

    }else{

        key_frame= !!(flags & FLAG_KEY_FRAME);

    }



    if(flags & FLAG_PTS){

        if(flags & FLAG_FULL_PTS){

            pts= get_v(bc);

            if(frame_type && key_frame){

                av_add_index_entry(

                    s->streams[stream_id], 

                    frame_start, 

                    pts, 

                    frame_start - nut->stream[stream_id].last_sync_pos,

                    AVINDEX_KEYFRAME);

                nut->stream[stream_id].last_sync_pos= frame_start;

                assert(nut->packet_start == frame_start);

            }

        }else{

            int64_t mask = (1<<stream->msb_timestamp_shift)-1;

            int64_t delta= stream->last_pts - mask/2;

            pts= ((get_v(bc) - delta)&mask) + delta;

        }

    }else{

        pts= stream->last_pts + stream->lru_pts_delta[(flags&12)>>2];

    }

  

    if(size_mul <= size_lsb){

        size= stream->lru_size[size_lsb - size_mul];

    }else{

        if(flags & FLAG_DATA_SIZE)

            size= size_mul*get_v(bc) + size_lsb;

        else

            size= size_lsb;

    }

      

//av_log(s, AV_LOG_DEBUG, "fs:%lld fc:%d ft:%d kf:%d pts:%lld size:%d\n", frame_start, frame_code, frame_type, key_frame, pts, size);



    if(url_ftell(bc) - nut->packet_start + size > nut->written_packet_size){

        av_log(s, AV_LOG_ERROR, "frame size too large\n");

        return -1;

    }

    

    av_new_packet(pkt, size);

    get_buffer(bc, pkt->data, size);

    pkt->stream_index = stream_id;

    if (key_frame)

	pkt->flags |= PKT_FLAG_KEY;

    pkt->pts = pts * AV_TIME_BASE * stream->rate_den / stream->rate_num;



    update(nut, stream_id, frame_start, frame_type, frame_code, key_frame, size, pts);

    

    return 0;

}

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

    AVFormatContext *s= nut->avf;

    StreamContext *stream;

    ByteIOContext *bc = &s->pb;

    int size, flags, size_mul, size_lsb, stream_id;

    int key_frame = 0;

    int64_t pts = 0;

    const int prefix_len= frame_type == 2 ? 8+1 : 1;

    const int64_t frame_start= url_ftell(bc) - prefix_len;



    flags= nut->frame_code[frame_code].flags;

    size_mul= nut->frame_code[frame_code].size_mul;

    size_lsb= nut->frame_code[frame_code].size_lsb;

    stream_id= nut->frame_code[frame_code].stream_id_plus1 - 1;



    if(flags & FLAG_FRAME_TYPE){

        reset(s);

        if(get_packetheader(nut, bc, prefix_len, 0) < 0)

            return -1;

        if(frame_type!=2)

            frame_type= 1;

    }



    if(stream_id==-1)

        stream_id= get_v(bc);

    if(stream_id >= s->nb_streams){

        av_log(s, AV_LOG_ERROR, "illegal stream_id\n");

        return -1;

    }

    stream= &nut->stream[stream_id];





    

    if(flags & FLAG_PRED_KEY_FRAME){

        if(flags & FLAG_KEY_FRAME)

            key_frame= !stream->last_key_frame;

        else

            key_frame= stream->last_key_frame;

    }else{

        key_frame= !!(flags & FLAG_KEY_FRAME);

    }



    if(flags & FLAG_PTS){

        if(flags & FLAG_FULL_PTS){

            pts= get_v(bc);

            if(frame_type && key_frame){

                av_add_index_entry(

                    s->streams[stream_id], 

                    frame_start, 

                    pts, 

                    frame_start - nut->stream[stream_id].last_sync_pos,

                    AVINDEX_KEYFRAME);

                nut->stream[stream_id].last_sync_pos= frame_start;

                assert(nut->packet_start == frame_start);

            }

        }else{

            int64_t mask = (1<<stream->msb_timestamp_shift)-1;

            int64_t delta= stream->last_pts - mask/2;

            pts= ((get_v(bc) - delta)&mask) + delta;

        }

    }else{

        pts= stream->last_pts + stream->lru_pts_delta[(flags&12)>>2];

    }

  

    if(size_mul <= size_lsb){

        size= stream->lru_size[size_lsb - size_mul];

    }else{

        if(flags & FLAG_DATA_SIZE)

            size= size_mul*get_v(bc) + size_lsb;

        else

            size= size_lsb;

    }

      





    if(url_ftell(bc) - nut->packet_start + size > nut->written_packet_size){

        av_log(s, AV_LOG_ERROR, "frame size too large\n");

        return -1;

    }

    

    av_new_packet(pkt, size);

    get_buffer(bc, pkt->data, size);

    pkt->stream_index = stream_id;

    if (key_frame)

	pkt->flags |= PKT_FLAG_KEY;

    pkt->pts = pts * AV_TIME_BASE * stream->rate_den / stream->rate_num;



    update(nut, stream_id, frame_start, frame_type, frame_code, key_frame, size, pts);

    

    return 0;

}

static int FUNC_0(NUTContext *VAR_0, AVPacket *VAR_1, int VAR_2, int VAR_3){

    AVFormatContext *s= VAR_0->avf;

    StreamContext *stream;

    ByteIOContext *bc = &s->pb;

    int VAR_4, VAR_5, VAR_6, VAR_7, VAR_8;

    int VAR_9 = 0;

    int64_t pts = 0;

    const int VAR_10= VAR_3 == 2 ? 8+1 : 1;

    const int64_t VAR_11= url_ftell(bc) - VAR_10;



    VAR_5= VAR_0->VAR_2[VAR_2].VAR_5;

    VAR_6= VAR_0->VAR_2[VAR_2].VAR_6;

    VAR_7= VAR_0->VAR_2[VAR_2].VAR_7;

    VAR_8= VAR_0->VAR_2[VAR_2].stream_id_plus1 - 1;



    if(VAR_5 & FLAG_FRAME_TYPE){

        reset(s);

        if(get_packetheader(VAR_0, bc, VAR_10, 0) < 0)

            return -1;

        if(VAR_3!=2)

            VAR_3= 1;

    }



    if(VAR_8==-1)

        VAR_8= get_v(bc);

    if(VAR_8 >= s->nb_streams){

        av_log(s, AV_LOG_ERROR, "illegal VAR_8\n");

        return -1;

    }

    stream= &VAR_0->stream[VAR_8];





    

    if(VAR_5 & FLAG_PRED_KEY_FRAME){

        if(VAR_5 & FLAG_KEY_FRAME)

            VAR_9= !stream->last_key_frame;

        else

            VAR_9= stream->last_key_frame;

    }else{

        VAR_9= !!(VAR_5 & FLAG_KEY_FRAME);

    }



    if(VAR_5 & FLAG_PTS){

        if(VAR_5 & FLAG_FULL_PTS){

            pts= get_v(bc);

            if(VAR_3 && VAR_9){

                av_add_index_entry(

                    s->streams[VAR_8], 

                    VAR_11, 

                    pts, 

                    VAR_11 - VAR_0->stream[VAR_8].last_sync_pos,

                    AVINDEX_KEYFRAME);

                VAR_0->stream[VAR_8].last_sync_pos= VAR_11;

                assert(VAR_0->packet_start == VAR_11);

            }

        }else{

            int64_t mask = (1<<stream->msb_timestamp_shift)-1;

            int64_t delta= stream->last_pts - mask/2;

            pts= ((get_v(bc) - delta)&mask) + delta;

        }

    }else{

        pts= stream->last_pts + stream->lru_pts_delta[(VAR_5&12)>>2];

    }

  

    if(VAR_6 <= VAR_7){

        VAR_4= stream->lru_size[VAR_7 - VAR_6];

    }else{

        if(VAR_5 & FLAG_DATA_SIZE)

            VAR_4= VAR_6*get_v(bc) + VAR_7;

        else

            VAR_4= VAR_7;

    }

      





    if(url_ftell(bc) - VAR_0->packet_start + VAR_4 > VAR_0->written_packet_size){

        av_log(s, AV_LOG_ERROR, "frame VAR_4 too large\n");

        return -1;

    }

    

    av_new_packet(VAR_1, VAR_4);

    get_buffer(bc, VAR_1->data, VAR_4);

    VAR_1->stream_index = VAR_8;

    if (VAR_9)

	VAR_1->VAR_5 |= PKT_FLAG_KEY;

    VAR_1->pts = pts * AV_TIME_BASE * stream->rate_den / stream->rate_num;



    update(VAR_0, VAR_8, VAR_11, VAR_3, VAR_2, VAR_9, VAR_4, pts);

    

    return 0;

}

static int FUNC_0(NUTContext *VAR_0, AVPacket *VAR_1, int VAR_2, int VAR_3){ AVFormatContext *s= VAR_0->avf; StreamContext *stream; ByteIOContext *bc = &s->pb; int VAR_4, VAR_5, VAR_6, VAR_7, VAR_8; int VAR_9 = 0; int64_t pts = 0; const int VAR_10= VAR_3 == 2 ? 8+1 : 1; const int64_t VAR_11= url_ftell(bc) - VAR_10; VAR_5= VAR_0->VAR_2[VAR_2].VAR_5; VAR_6= VAR_0->VAR_2[VAR_2].VAR_6; VAR_7= VAR_0->VAR_2[VAR_2].VAR_7; VAR_8= VAR_0->VAR_2[VAR_2].stream_id_plus1 - 1; if(VAR_5 & FLAG_FRAME_TYPE){ reset(s); if(get_packetheader(VAR_0, bc, VAR_10, 0) < 0)
return -1; if(VAR_3!=2)
VAR_3= 1; } if(VAR_8==-1)
VAR_8= get_v(bc); if(VAR_8 >= s->nb_streams){ av_log(s, AV_LOG_ERROR, "illegal VAR_8\n"); return -1; } stream= &VAR_0->stream[VAR_8]; if(VAR_5 & FLAG_PRED_KEY_FRAME){ if(VAR_5 & FLAG_KEY_FRAME)
VAR_9= !stream->last_key_frame; else
VAR_9= stream->last_key_frame; }else{ VAR_9= !!(VAR_5 & FLAG_KEY_FRAME); } if(VAR_5 & FLAG_PTS){ if(VAR_5 & FLAG_FULL_PTS){ pts= get_v(bc); if(VAR_3 && VAR_9){ av_add_index_entry(
s->streams[VAR_8],
VAR_11,
pts,
VAR_11 - VAR_0->stream[VAR_8].last_sync_pos,
AVINDEX_KEYFRAME); VAR_0->stream[VAR_8].last_sync_pos= VAR_11; assert(VAR_0->packet_start == VAR_11); } }else{ int64_t mask = (1<<stream->msb_timestamp_shift)-1; int64_t delta= stream->last_pts - mask/2; pts= ((get_v(bc) - delta)&mask) + delta; } }else{ pts= stream->last_pts + stream->lru_pts_delta[(VAR_5&12)>>2]; } if(VAR_6 <= VAR_7){ VAR_4= stream->lru_size[VAR_7 - VAR_6]; }else{ if(VAR_5 & FLAG_DATA_SIZE)
VAR_4= VAR_6*get_v(bc) + VAR_7; else
VAR_4= VAR_7; } if(url_ftell(bc) - VAR_0->packet_start + VAR_4 > VAR_0->written_packet_size){ av_log(s, AV_LOG_ERROR, "frame VAR_4 too large\n"); return -1; } av_new_packet(VAR_1, VAR_4); get_buffer(bc, VAR_1->data, VAR_4); VAR_1->stream_index = VAR_8; if (VAR_9)
VAR_1->VAR_5 |= PKT_FLAG_KEY; VAR_1->pts = pts * AV_TIME_BASE * stream->rate_den / stream->rate_num; update(VAR_0, VAR_8, VAR_11, VAR_3, VAR_2, VAR_9, VAR_4, pts); return 0; }