History

swr/swresample: avoid reapplication of firstpts

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 firstpts initialized to AV_NOPTS_VALUE in swr_alloc and then checked in swr_init to avoid resetting outpts, thus avoiding reapplication of firstpts. Fixes #4131.

Gyan Doshi committed 2 years ago

Commit be8a4f81 parent 2ad0b8e4 files · +31 −2Browse files

Unified · Split

4 files
  1. libswresample/options.c +1 −0
  2. libswresample/swresample.c +3 −2
  3. tests/fate/libswresample.mak +3 −0
  4. tests/ref/fate/swr-async-firstpts +24 −0
libswresample/options.c+1−0
@@ -171,6 +171,7 @@ av_cold struct SwrContext *swr_alloc(void){ · 170 unmodified lines
171 if(s){171 if(s){
172 s->av_class= &av_class;172 s->av_class= &av_class;
173 av_opt_set_defaults(s);173 av_opt_set_defaults(s);
174 s->firstpts = AV_NOPTS_VALUE;
174 }175 }
175 return s;176 return s;
176}177}
libswresample/swresample.c+3−2
@@ -375,8 +375,9 @@ av_cold int swr_init(struct SwrContext *s){ · 374 unmodified lines
375 if (s->firstpts_in_samples != AV_NOPTS_VALUE) {375 if (s->firstpts_in_samples != AV_NOPTS_VALUE) {
376 if (!s->async && s->min_compensation >= FLT_MAX/2)376 if (!s->async && s->min_compensation >= FLT_MAX/2)
377 s->async = 1;377 s->async = 1;
378 s->firstpts =378 if (s->firstpts == AV_NOPTS_VALUE)
379 s->outpts = s->firstpts_in_samples * s->out_sample_rate;379 s->firstpts =
380 s->outpts = s->firstpts_in_samples * s->out_sample_rate;
380 } else381 } else
381 s->firstpts = AV_NOPTS_VALUE;382 s->firstpts = AV_NOPTS_VALUE;
382 383
tests/fate/libswresample.mak+3−0
@@ -1082,6 +1082,9 @@ $(call CROSS_TEST,$(SAMPLERATES_LITE),ARESAMPLE_EXACT_LIN_ASYNC,s32p,s32le,s16) · 1081 unmodified lines
1082$(call CROSS_TEST,$(SAMPLERATES_LITE),ARESAMPLE_EXACT_LIN_ASYNC,fltp,f32le,s16)1082$(call CROSS_TEST,$(SAMPLERATES_LITE),ARESAMPLE_EXACT_LIN_ASYNC,fltp,f32le,s16)
1083$(call CROSS_TEST,$(SAMPLERATES_LITE),ARESAMPLE_EXACT_LIN_ASYNC,dblp,f64le,s16)1083$(call CROSS_TEST,$(SAMPLERATES_LITE),ARESAMPLE_EXACT_LIN_ASYNC,dblp,f64le,s16)
1084 1084
1085FATE_SWR_RESAMPLE-$(call FILTERDEMDEC, ARESAMPLE ASETPTS ATRIM SINE, , PCM_S16LE, LAVFI_INDEV) += fate-swr-async-firstpts
1086fate-swr-async-firstpts: CMD = framecrc -auto_conversion_filters -copyts -f lavfi -i "sine=r=1000:samples_per_frame=100,asetpts=PTS+S+S*floor(ld(1)/4)+st(1\,ld(1)+1)*0,atrim=end=2" -filter:a aresample=async=300:first_pts=0
1087
1085FATE_SWR_RESAMPLE-$(call FILTERDEMDECENCMUX, ARESAMPLE, WAV, PCM_S16LE, PCM_S16LE, WAV) += $(FATE_SWR_RESAMPLE)1088FATE_SWR_RESAMPLE-$(call FILTERDEMDECENCMUX, ARESAMPLE, WAV, PCM_S16LE, PCM_S16LE, WAV) += $(FATE_SWR_RESAMPLE)
1086fate-swr-resample: $(FATE_SWR_RESAMPLE-yes)1089fate-swr-resample: $(FATE_SWR_RESAMPLE-yes)
1087FATE_SWR += $(FATE_SWR_RESAMPLE-yes)1090FATE_SWR += $(FATE_SWR_RESAMPLE-yes)
tests/ref/fate/swr-async-firstpts+24−0
@@ -0,0 +1,24 @@
1#tb 0: 1/1000
2#media_type 0: audio
3#codec_id 0: pcm_s16le
4#sample_rate 0: 1000
5#channel_layout_name 0: mono
60, 0, 0, 132, 264, 0xc2981f45
70, 132, 132, 68, 136, 0xe78e468d
80, 200, 200, 100, 200, 0xd55c67d0
90, 300, 300, 100, 200, 0xd55c67d0
100, 400, 400, 100, 200, 0xd55c67d0
110, 500, 500, 93, 186, 0x85ca5db4
120, 593, 593, 110, 220, 0xa2655d0b
130, 703, 703, 108, 216, 0x95cb6f01
140, 811, 811, 108, 216, 0xf35668b8
150, 919, 919, 149, 298, 0xc273245f
160, 1068, 1068, 136, 272, 0xedeb6e0a
170, 1204, 1204, 98, 196, 0xea18668e
180, 1302, 1302, 98, 196, 0x412861e7
190, 1400, 1400, 98, 196, 0x7ec361b2
200, 1498, 1498, 110, 220, 0xf3ae6a6a
210, 1608, 1608, 108, 216, 0xab2f6c93
220, 1716, 1716, 107, 214, 0x50de6eb9
230, 1823, 1823, 106, 212, 0x67b8656d
240, 1929, 1929, 18, 36, 0x2b7911c6

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t