Opened 8 years ago

Closed 8 years ago

#5785 closed defect (invalid)

swr_drop_output misbehaviour

Reported by: Elon Musk Owned by:
Priority: normal Component: swresample
Version: git-master Keywords:
Cc: Blocked By:
Blocking: Reproduced by developer: no
Analyzed by developer: no

Description

Summary of the bug:
How to reproduce:

Try attached example code

Expected result: properly looped audio.
Actual result: Discarded samples.

Attachments (1)

resampling_audio.c (8.0 KB ) - added by Elon Musk 8 years ago.

Download all attachments as: .zip

Change History (7)

by Elon Musk, 8 years ago

Attachment: resampling_audio.c added

comment:1 by Michael Niedermayer, 8 years ago

The example seems buggy, it doesnt produce a sinewave that repeats every 1000 samples so droping 1000 will not produce a smooth output. If that is corrected with the patch below the output seems fine.
Or maybe iam missunderstanding the issue ?

diff --git a/doc/examples/resampling_audio.c b/doc/examples/resampling_audio.c
index 2dd2513..a698275 100644
--- a/doc/examples/resampling_audio.c
+++ b/doc/examples/resampling_audio.c
@@ -66,7 +66,7 @@ static void fill_samples(double *dst, int nb_samples, int nb_channels, int sampl
 {
     int i, j;
     double tincr = 1.0 / sample_rate, *dstp = dst;
-    const double c = 2 * M_PI * 440.0;
+    const double c = 2 * M_PI * 48000 / 100; // or 48000 / 1000
 
     /* generate sin tone with 440Hz frequency and duplicated channels */
     for (i = 0; i < nb_samples; i++) {

comment:2 by Elon Musk, 8 years ago

You misunderstood the issue. Problem is swr dropping 1000 samples, and reporting it dropped 0 samples. While it should not drop any sample.
When feeding it another round of samples it take into account previous value.

comment:3 by Michael Niedermayer, 8 years ago

swr_drop_output() drops exactly the number requested, thats how its documented as well. "Drops the specified number of output samples."
The return code is documented as ">= 0 on success, or a negative AVERROR code on failure" it has no relation to how many samples will be dropped, i would expect the number of actually dropped samples to be exactly what was requested.

Can you explain what you are trying to do ? / what usecase this is about ?

comment:4 by Elon Musk, 8 years ago

Usecase is seamless looping, without need to reinitializing swr.
See https://github.com/mpv-player/mpv/commit/814dacdd7d010407a4bea74b3a047079f49c5c39.

comment:5 by Michael Niedermayer, 8 years ago

If you want to bring the state back to the initial state calling
swr_close() and swr_init() should do that efficiently, this will not reinitialize the resampler

you can drop output samples too but this will not return to exactly the initial state in general.

swr_drop_output(swr_ctx,swr_get_out_samples(swr_ctx, 0));
would drop all the old data, but when new data comes in the buffers will get filled before output comes out, so this will result in an apparent loss of a few audio samples

you can also resample into a dummy buffer and throw that away which will look correct but is possibly not what you actually want because old data remains in the buffer and will be output

If theres some variant missing or can be improved, please say so

comment:6 by Elon Musk, 8 years ago

Resolution: invalid
Status: newclosed
Note: See TracTickets for help on using tickets.