h4tr3d / avcpp

C++ wrapper for FFmpeg
Other
459 stars 80 forks source link

Av::Timestamp overflow #140

Open nijiancong opened 3 months ago

nijiancong commented 3 months ago

During an audio live stream transcoding, after utilizing AudioResampler::pop() for approximately 12 hours, the returned AudioSamples::pts() restarted from 0(https://github.com/h4tr3d/avcpp/blob/5e167a4a997f1963221da5bcdfcbe38aa46116ea/src/audioresampler.cpp#L189), prompting me to test the Timestamp addition operation. After a certain number of iterations, the results obtained from "+=" and "+" operations differ. "+=" continues to increment, whereas "+" operations result in overflow.

#include <avcpp/timestamp.h>
#include <avcpp/audioresampler.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char* argv[])
{
    int64_t i = 0;
    av::Timestamp t(0, av::Rational{1, 48000});
    while (true) {
        t = t + av::Timestamp{1024, t.timebase()};
        // t += av::Timestamp{1024, t.timebase()};
        if (t.seconds() < 1) {
            printf("[%" PRId64 "]++++t=%0.3lf\n", i, t.seconds());
        }
        if (i % 1000000 == 0) {
            printf("t=%0.3lf\n", t.seconds());
        }
        i++;
    }
    return 0;
}