static int update_wrap_reference(AVFormatContext *s, AVStream *st, int stream_index, AVPacket *pkt)

{

    int64_t ref = pkt->dts;



    if (ref == AV_NOPTS_VALUE)

        ref = pkt->pts;

    if (ref == AV_NOPTS_VALUE)

        return 0;

    ref &= (1LL<<st->pts_wrap_bits)-1;



    if (s->correct_ts_overflow && st->pts_wrap_bits < 63 &&

        st->pts_wrap_reference == AV_NOPTS_VALUE) {

        int i;



        // reference time stamp should be 60 s before first time stamp

        int64_t pts_wrap_reference = ref - av_rescale(60, st->time_base.den, st->time_base.num);

        // if first time stamp is not more than 1/8 and 60s before the wrap point, subtract rather than add wrap offset

        int pts_wrap_behavior = (ref < (1LL<<st->pts_wrap_bits) - (1LL<<st->pts_wrap_bits-3)) ||

            (ref < (1LL<<st->pts_wrap_bits) - av_rescale(60, st->time_base.den, st->time_base.num)) ?

            AV_PTS_WRAP_ADD_OFFSET : AV_PTS_WRAP_SUB_OFFSET;



        AVProgram *first_program = av_find_program_from_stream(s, NULL, stream_index);



        if (!first_program) {

            int default_stream_index = av_find_default_stream_index(s);

            if (s->streams[default_stream_index]->pts_wrap_reference == AV_NOPTS_VALUE) {

                for (i=0; i<s->nb_streams; i++) {

                    s->streams[i]->pts_wrap_reference = pts_wrap_reference;

                    s->streams[i]->pts_wrap_behavior = pts_wrap_behavior;

                }

            }

            else {

                st->pts_wrap_reference = s->streams[default_stream_index]->pts_wrap_reference;

                st->pts_wrap_behavior = s->streams[default_stream_index]->pts_wrap_behavior;

            }

        }

        else {

            AVProgram *program = first_program;

            while (program) {

                if (program->pts_wrap_reference != AV_NOPTS_VALUE) {

                    pts_wrap_reference = program->pts_wrap_reference;

                    pts_wrap_behavior = program->pts_wrap_behavior;

                    break;

                }

                program = av_find_program_from_stream(s, program, stream_index);

            }



            // update every program with differing pts_wrap_reference

            program = first_program;

            while(program) {

                if (program->pts_wrap_reference != pts_wrap_reference) {

                    for (i=0; i<program->nb_stream_indexes; i++) {

                        s->streams[program->stream_index[i]]->pts_wrap_reference = pts_wrap_reference;

                        s->streams[program->stream_index[i]]->pts_wrap_behavior = pts_wrap_behavior;

                    }



                    program->pts_wrap_reference = pts_wrap_reference;

                    program->pts_wrap_behavior = pts_wrap_behavior;

                }

                program = av_find_program_from_stream(s, program, stream_index);

            }

        }

        return 1;

    }

    return 0;

}

static int update_wrap_reference(AVFormatContext *s, AVStream *st, int stream_index, AVPacket *pkt)

{

    int64_t ref = pkt->dts;



    if (ref == AV_NOPTS_VALUE)

        ref = pkt->pts;

    if (ref == AV_NOPTS_VALUE)

        return 0;

    ref &= (1LL<<st->pts_wrap_bits)-1;



    if (s->correct_ts_overflow && st->pts_wrap_bits < 63 &&

        st->pts_wrap_reference == AV_NOPTS_VALUE) {

        int i;



        

        int64_t pts_wrap_reference = ref - av_rescale(60, st->time_base.den, st->time_base.num);

        

        int pts_wrap_behavior = (ref < (1LL<<st->pts_wrap_bits) - (1LL<<st->pts_wrap_bits-3)) ||

            (ref < (1LL<<st->pts_wrap_bits) - av_rescale(60, st->time_base.den, st->time_base.num)) ?

            AV_PTS_WRAP_ADD_OFFSET : AV_PTS_WRAP_SUB_OFFSET;



        AVProgram *first_program = av_find_program_from_stream(s, NULL, stream_index);



        if (!first_program) {

            int default_stream_index = av_find_default_stream_index(s);

            if (s->streams[default_stream_index]->pts_wrap_reference == AV_NOPTS_VALUE) {

                for (i=0; i<s->nb_streams; i++) {

                    s->streams[i]->pts_wrap_reference = pts_wrap_reference;

                    s->streams[i]->pts_wrap_behavior = pts_wrap_behavior;

                }

            }

            else {

                st->pts_wrap_reference = s->streams[default_stream_index]->pts_wrap_reference;

                st->pts_wrap_behavior = s->streams[default_stream_index]->pts_wrap_behavior;

            }

        }

        else {

            AVProgram *program = first_program;

            while (program) {

                if (program->pts_wrap_reference != AV_NOPTS_VALUE) {

                    pts_wrap_reference = program->pts_wrap_reference;

                    pts_wrap_behavior = program->pts_wrap_behavior;

                    break;

                }

                program = av_find_program_from_stream(s, program, stream_index);

            }



            

            program = first_program;

            while(program) {

                if (program->pts_wrap_reference != pts_wrap_reference) {

                    for (i=0; i<program->nb_stream_indexes; i++) {

                        s->streams[program->stream_index[i]]->pts_wrap_reference = pts_wrap_reference;

                        s->streams[program->stream_index[i]]->pts_wrap_behavior = pts_wrap_behavior;

                    }



                    program->pts_wrap_reference = pts_wrap_reference;

                    program->pts_wrap_behavior = pts_wrap_behavior;

                }

                program = av_find_program_from_stream(s, program, stream_index);

            }

        }

        return 1;

    }

    return 0;

}

    if (ref == AV_NOPTS_VALUE)     if (s->correct_ts_overflow && st->pts_wrap_bits < 63 &&         st->pts_wrap_reference == AV_NOPTS_VALUE) {     return 0;
static int FUNC_0(AVFormatContext *VAR_0, AVStream *VAR_1, int VAR_2, AVPacket *VAR_3)

{

    int64_t ref = VAR_3->dts;



    if (ref == AV_NOPTS_VALUE)

        ref = VAR_3->pts;

    if (ref == AV_NOPTS_VALUE)

        return 0;

    ref &= (1LL<<VAR_1->pts_wrap_bits)-1;



    if (VAR_0->correct_ts_overflow && VAR_1->pts_wrap_bits < 63 &&

        VAR_1->pts_wrap_reference == AV_NOPTS_VALUE) {

        int VAR_4;



        

        int64_t pts_wrap_reference = ref - av_rescale(60, VAR_1->time_base.den, VAR_1->time_base.num);

        

        int VAR_5 = (ref < (1LL<<VAR_1->pts_wrap_bits) - (1LL<<VAR_1->pts_wrap_bits-3)) ||

            (ref < (1LL<<VAR_1->pts_wrap_bits) - av_rescale(60, VAR_1->time_base.den, VAR_1->time_base.num)) ?

            AV_PTS_WRAP_ADD_OFFSET : AV_PTS_WRAP_SUB_OFFSET;



        AVProgram *first_program = av_find_program_from_stream(VAR_0, NULL, VAR_2);



        if (!first_program) {

            int VAR_6 = av_find_default_stream_index(VAR_0);

            if (VAR_0->streams[VAR_6]->pts_wrap_reference == AV_NOPTS_VALUE) {

                for (VAR_4=0; VAR_4<VAR_0->nb_streams; VAR_4++) {

                    VAR_0->streams[VAR_4]->pts_wrap_reference = pts_wrap_reference;

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

                }

            }

            else {

                VAR_1->pts_wrap_reference = VAR_0->streams[VAR_6]->pts_wrap_reference;

                VAR_1->VAR_5 = VAR_0->streams[VAR_6]->VAR_5;

            }

        }

        else {

            AVProgram *program = first_program;

            while (program) {

                if (program->pts_wrap_reference != AV_NOPTS_VALUE) {

                    pts_wrap_reference = program->pts_wrap_reference;

                    VAR_5 = program->VAR_5;

                    break;

                }

                program = av_find_program_from_stream(VAR_0, program, VAR_2);

            }



            

            program = first_program;

            while(program) {

                if (program->pts_wrap_reference != pts_wrap_reference) {

                    for (VAR_4=0; VAR_4<program->nb_stream_indexes; VAR_4++) {

                        VAR_0->streams[program->VAR_2[VAR_4]]->pts_wrap_reference = pts_wrap_reference;

                        VAR_0->streams[program->VAR_2[VAR_4]]->VAR_5 = VAR_5;

                    }



                    program->pts_wrap_reference = pts_wrap_reference;

                    program->VAR_5 = VAR_5;

                }

                program = av_find_program_from_stream(VAR_0, program, VAR_2);

            }

        }

        return 1;

    }

    return 0;

}

static int FUNC_0(AVFormatContext *VAR_0, AVStream *VAR_1, int VAR_2, AVPacket *VAR_3)
{ int64_t ref = VAR_3->dts; if (ref == AV_NOPTS_VALUE)
ref = VAR_3->pts; if (ref == AV_NOPTS_VALUE)
return 0; ref &= (1LL<<VAR_1->pts_wrap_bits)-1; if (VAR_0->correct_ts_overflow && VAR_1->pts_wrap_bits < 63 &&
VAR_1->pts_wrap_reference == AV_NOPTS_VALUE) { int VAR_4; int64_t pts_wrap_reference = ref - av_rescale(60, VAR_1->time_base.den, VAR_1->time_base.num); int VAR_5 = (ref < (1LL<<VAR_1->pts_wrap_bits) - (1LL<<VAR_1->pts_wrap_bits-3)) ||
(ref < (1LL<<VAR_1->pts_wrap_bits) - av_rescale(60, VAR_1->time_base.den, VAR_1->time_base.num)) ?
AV_PTS_WRAP_ADD_OFFSET : AV_PTS_WRAP_SUB_OFFSET; AVProgram *first_program = av_find_program_from_stream(VAR_0, NULL, VAR_2); if (!first_program) { int VAR_6 = av_find_default_stream_index(VAR_0); if (VAR_0->streams[VAR_6]->pts_wrap_reference == AV_NOPTS_VALUE) { for (VAR_4=0; VAR_4<VAR_0->nb_streams; VAR_4++) { VAR_0->streams[VAR_4]->pts_wrap_reference = pts_wrap_reference; VAR_0->streams[VAR_4]->VAR_5 = VAR_5; } } else { VAR_1->pts_wrap_reference = VAR_0->streams[VAR_6]->pts_wrap_reference; VAR_1->VAR_5 = VAR_0->streams[VAR_6]->VAR_5; } } else { AVProgram *program = first_program; while (program) { if (program->pts_wrap_reference != AV_NOPTS_VALUE) { pts_wrap_reference = program->pts_wrap_reference; VAR_5 = program->VAR_5; break; } program = av_find_program_from_stream(VAR_0, program, VAR_2); } program = first_program; while(program) { if (program->pts_wrap_reference != pts_wrap_reference) { for (VAR_4=0; VAR_4<program->nb_stream_indexes; VAR_4++) { VAR_0->streams[program->VAR_2[VAR_4]]->pts_wrap_reference = pts_wrap_reference; VAR_0->streams[program->VAR_2[VAR_4]]->VAR_5 = VAR_5; } program->pts_wrap_reference = pts_wrap_reference; program->VAR_5 = VAR_5; } program = av_find_program_from_stream(VAR_0, program, VAR_2); } } return 1; } return 0; }