Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH] swr/swresample: avoid reapplication of firstpts
@ 2023-12-12 11:20 Gyan Doshi
  2023-12-14 19:13 ` Michael Niedermayer
  0 siblings, 1 reply; 3+ messages in thread
From: Gyan Doshi @ 2023-12-12 11:20 UTC (permalink / raw)
  To: ffmpeg-devel

During a resampling operation where

1) user has specified first_pts
2) SWR_FLAG_RESAMPLE is not set initially (directly or otherwise)
3) first_pts has been fulfilled (always using hard compensation)

then upon first encountering a delay where a soft compensation is
required, swr_set_compensation will lead to another init of swr which
will reset outpts to the specified firstpts thus leading to an output
frame having its pts = firstpts. When the next input frame is received,
swr will see a large delay and inject silence from firstpts to the
current frame's pts. This can lead to severe desync and in worst case,
loss of audio playback.

Parameter initcount added to keep track of swr_init calls of the swrcontext
and to ultimately avoid enforcing firstpts again.

Fixes #4131.
---
 libswresample/swresample.c          | 7 +++++--
 libswresample/swresample_internal.h | 1 +
 libswresample/version.h             | 2 +-
 3 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/libswresample/swresample.c b/libswresample/swresample.c
index f2a9b40474..008af3610f 100644
--- a/libswresample/swresample.c
+++ b/libswresample/swresample.c
@@ -375,8 +375,9 @@ av_cold int swr_init(struct SwrContext *s){
     if (s->firstpts_in_samples != AV_NOPTS_VALUE) {
         if (!s->async && s->min_compensation >= FLT_MAX/2)
             s->async = 1;
-        s->firstpts =
-        s->outpts   = s->firstpts_in_samples * s->out_sample_rate;
+        if (!s->initcount)
+            s->firstpts =
+            s->outpts   = s->firstpts_in_samples * s->out_sample_rate;
     } else
         s->firstpts = AV_NOPTS_VALUE;
 
@@ -458,6 +459,7 @@ av_assert0(s->out.ch_count);
     if(!s->resample && !s->rematrix && !s->channel_map && !s->dither.method){
         s->full_convert = swri_audio_convert_alloc(s->out_sample_fmt,
                                                    s-> in_sample_fmt, s-> in.ch_count, NULL, 0);
+        s->initcount++;
         return 0;
     }
 
@@ -510,6 +512,7 @@ av_assert0(s->out.ch_count);
             goto fail;
     }
 
+    s->initcount++;
     return 0;
 fail:
     swr_close(s);
diff --git a/libswresample/swresample_internal.h b/libswresample/swresample_internal.h
index ad902d73fa..71d0c60e9f 100644
--- a/libswresample/swresample_internal.h
+++ b/libswresample/swresample_internal.h
@@ -145,6 +145,7 @@ struct SwrContext {
     float max_soft_compensation;                    ///< swr maximum soft compensation in seconds over soft_compensation_duration
     float async;                                    ///< swr simple 1 parameter async, similar to ffmpegs -async
     int64_t firstpts_in_samples;                    ///< swr first pts in samples
+    int initcount;                                  ///< swr no. of times the swrcontext has been inited
 
     int resample_first;                             ///< 1 if resampling must come first, 0 if rematrixing
     int rematrix;                                   ///< flag to indicate if rematrixing is needed (basically if input and output layouts mismatch)
diff --git a/libswresample/version.h b/libswresample/version.h
index 46a4e2fc62..fe4469ae6b 100644
--- a/libswresample/version.h
+++ b/libswresample/version.h
@@ -31,7 +31,7 @@
 #include "version_major.h"
 
 #define LIBSWRESAMPLE_VERSION_MINOR  13
-#define LIBSWRESAMPLE_VERSION_MICRO 100
+#define LIBSWRESAMPLE_VERSION_MICRO 101
 
 #define LIBSWRESAMPLE_VERSION_INT  AV_VERSION_INT(LIBSWRESAMPLE_VERSION_MAJOR, \
                                                   LIBSWRESAMPLE_VERSION_MINOR, \
-- 
2.39.1

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-12-15  7:02 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-12 11:20 [FFmpeg-devel] [PATCH] swr/swresample: avoid reapplication of firstpts Gyan Doshi
2023-12-14 19:13 ` Michael Niedermayer
2023-12-15  7:02   ` Gyan Doshi

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