Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Av::Timestamp overflow #140

Closed
nijiancong opened this issue Aug 13, 2024 · 1 comment
Closed

Av::Timestamp overflow #140

nijiancong opened this issue Aug 13, 2024 · 1 comment
Assignees
Labels

Comments

@nijiancong
Copy link

During an audio live stream transcoding, after utilizing AudioResampler::pop() for approximately 12 hours, the returned AudioSamples::pts() restarted from 0(

m_nextPts = dst.pts() + Timestamp{dst.samplesCount(), dst.timeBase()};
), 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;
}
@h4tr3d h4tr3d self-assigned this Jan 11, 2025
@h4tr3d h4tr3d added the bug label Jan 11, 2025
@h4tr3d
Copy link
Owner

h4tr3d commented Jan 12, 2025

Sounds like it is feature of the FFmpeg av_add_stable() and its usage in the operator+(a,b) implementation. For the first look, av_add_stable() always expects, that right value always less than left value. In the sample above, overflow will be reduced just by chanding operands order:

t = av::Timestamp{1024, t.timebase()} + t;

Yep, t is greater then 1024/tb: for the some reason of the operator+(a,b) implementattion rhs and lhs values swapped.

Will try to fix.

@h4tr3d h4tr3d closed this as completed in f984467 Jan 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants