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

{

    if (s->correct_ts_overflow && st->pts_wrap_bits != 64 &&

        st->pts_wrap_reference == AV_NOPTS_VALUE && st->first_dts != AV_NOPTS_VALUE) {

        int i;



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

        int64_t pts_wrap_reference = st->first_dts - 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 = (st->first_dts < (1LL<<st->pts_wrap_bits) - (1LL<<st->pts_wrap_bits-3)) ||

            (st->first_dts < (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)

{

    if (s->correct_ts_overflow && st->pts_wrap_bits != 64 &&

        st->pts_wrap_reference == AV_NOPTS_VALUE && st->first_dts != AV_NOPTS_VALUE) {

        int i;



        

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

        

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

            (st->first_dts < (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 (s->correct_ts_overflow && st->pts_wrap_bits != 64 &&
static int FUNC_0(AVFormatContext *VAR_0, AVStream *VAR_1, int VAR_2)

{

    if (VAR_0->correct_ts_overflow && VAR_1->pts_wrap_bits != 64 &&

        VAR_1->pts_wrap_reference == AV_NOPTS_VALUE && VAR_1->first_dts != AV_NOPTS_VALUE) {

        int VAR_3;



        

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

        

        int VAR_4 = (VAR_1->first_dts < (1LL<<VAR_1->pts_wrap_bits) - (1LL<<VAR_1->pts_wrap_bits-3)) ||

            (VAR_1->first_dts < (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_5 = av_find_default_stream_index(VAR_0);

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

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

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

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

                }

            }

            else {

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

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

            }

        }

        else {

            AVProgram *program = first_program;

            while (program) {

                if (program->pts_wrap_reference != AV_NOPTS_VALUE) {

                    pts_wrap_reference = program->pts_wrap_reference;

                    VAR_4 = program->VAR_4;

                    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_3=0; VAR_3<program->nb_stream_indexes; VAR_3++) {

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

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

                    }



                    program->pts_wrap_reference = pts_wrap_reference;

                    program->VAR_4 = VAR_4;

                }

                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)
{ if (VAR_0->correct_ts_overflow && VAR_1->pts_wrap_bits != 64 &&
VAR_1->pts_wrap_reference == AV_NOPTS_VALUE && VAR_1->first_dts != AV_NOPTS_VALUE) { int VAR_3; int64_t pts_wrap_reference = VAR_1->first_dts - av_rescale(60, VAR_1->time_base.den, VAR_1->time_base.num); int VAR_4 = (VAR_1->first_dts < (1LL<<VAR_1->pts_wrap_bits) - (1LL<<VAR_1->pts_wrap_bits-3)) ||
(VAR_1->first_dts < (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_5 = av_find_default_stream_index(VAR_0); if (VAR_0->streams[VAR_5]->pts_wrap_reference == AV_NOPTS_VALUE) { for (VAR_3=0; VAR_3<VAR_0->nb_streams; VAR_3++) { VAR_0->streams[VAR_3]->pts_wrap_reference = pts_wrap_reference; VAR_0->streams[VAR_3]->VAR_4 = VAR_4; } } else { VAR_1->pts_wrap_reference = VAR_0->streams[VAR_5]->pts_wrap_reference; VAR_1->VAR_4 = VAR_0->streams[VAR_5]->VAR_4; } } else { AVProgram *program = first_program; while (program) { if (program->pts_wrap_reference != AV_NOPTS_VALUE) { pts_wrap_reference = program->pts_wrap_reference; VAR_4 = program->VAR_4; 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_3=0; VAR_3<program->nb_stream_indexes; VAR_3++) { VAR_0->streams[program->VAR_2[VAR_3]]->pts_wrap_reference = pts_wrap_reference; VAR_0->streams[program->VAR_2[VAR_3]]->VAR_4 = VAR_4; } program->pts_wrap_reference = pts_wrap_reference; program->VAR_4 = VAR_4; } program = av_find_program_from_stream(VAR_0, program, VAR_2); } } return 1; } return 0; }