Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Michael Niedermayer <michael@niedermayer.cc>
To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Subject: Re: [FFmpeg-devel] [PATCH] rtpdec: Fix RTP timestamp wraparound in Producer Reference Time
Date: Fri, 6 Jun 2025 23:56:54 +0200
Message-ID: <20250606215654.GA29660@pb2> (raw)
In-Reply-To: <20250606085414.59143-1-peron.clem@gmail.com>


[-- Attachment #1.1: Type: text/plain, Size: 2498 bytes --]

On Fri, Jun 06, 2025 at 10:54:14AM +0200, Clément Péron wrote:
> The rtp_set_prft() function incorrectly calculates the timestamp delta
> when RTP timestamps wrap around the 32-bit boundary. The current code:
> 
>     delta_timestamp = (int64_t)timestamp - (int64_t)s->last_rtcp_timestamp;
> 
> treats both timestamps as large positive values, causing wraparound to
> produce a large negative delta instead of the correct small positive delta.
> 
> For example, with a 90kHz video clock:
> - last_rtcp_timestamp = 0xFFFFFF00 (near wraparound)
> - timestamp = 0x00000100 (after wraparound)
> - Current result: delta ≈ -4.3 billion ticks ≈ -47,721 seconds
> - Expected result: delta ≈ +512 ticks ≈ +0.006 seconds
> 
> This causes prft->wallclock to jump backward by approximately:
> - 90kHz video: ~47,721 seconds (~13.25 hours)
> - 48kHz audio: ~89,478 seconds (~24.9 hours)
> - 8kHz audio: ~536,871 seconds (~6.2 days)
> 
> Fix by casting the subtraction result to int32_t, which correctly
> handles wraparound through modular arithmetic:
> 
>     delta_timestamp = (int32_t)(timestamp - s->last_rtcp_timestamp);
> 
> This ensures the delta is always in the range [-2^31, 2^31-1], making
> wraparound produce the correct small positive values.
> 
> Fixes timing jumps in applications that rely on Producer Reference Time
> for media synchronization.
> ---
>  libavformat/rtpdec.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/libavformat/rtpdec.c b/libavformat/rtpdec.c
> index 729bf83685..7a658ad698 100644
> --- a/libavformat/rtpdec.c
> +++ b/libavformat/rtpdec.c
> @@ -642,7 +642,7 @@ static int rtp_set_prft(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestamp) {
>          return AVERROR(ENOMEM);
>  
>      rtcp_time = ff_parse_ntp_time(s->last_rtcp_ntp_time) - NTP_OFFSET_US;
> -    delta_timestamp = (int64_t)timestamp - (int64_t)s->last_rtcp_timestamp;
> +    delta_timestamp = (int32_t)(timestamp - s->last_rtcp_timestamp);
>      delta_time = av_rescale_q(delta_timestamp, s->st->time_base, AV_TIME_BASE_Q);

assuming the change is correct, the type of delta_timestamp can be
changed to int32_t

thx

[...]

-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

If you fake or manipulate statistics in a paper in physics you will never
get a job again.
If you fake or manipulate statistics in a paper in medicin you will get
a job for life at the pharma industry.

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

[-- Attachment #2: Type: text/plain, Size: 251 bytes --]

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

      parent reply	other threads:[~2025-06-06 21:57 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-06  8:54 Clément Péron
2025-06-06  8:56 ` Clément Péron
2025-06-06 21:56 ` Michael Niedermayer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250606215654.GA29660@pb2 \
    --to=michael@niedermayer.cc \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git