diff mbox series

[FFmpeg-devel,2/2] avfilter/asrc_sine: correct accumulating phase error at every second

Message ID 20241106204954.13498-2-cus@passwd.hu
State New
Headers show
Series [FFmpeg-devel,1/2] avfilter/asrc_sine: rename some beep variables for more generic use | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 fail Make fate failed

Commit Message

Marton Balint Nov. 6, 2024, 8:47 p.m. UTC
This improves phase accuracy and for integer frequencies it makes the filter
bitexact periodic per every second and completely eliminates accumulating
errors.

This code intentionally uses the same counters as the beep support, so the
additional overhead should be very minor.

Signed-off-by: Marton Balint <cus@passwd.hu>
---
 libavfilter/asrc_sine.c           |  6 ++-
 tests/ref/fate/filter-concat      | 88 +++++++++++++++----------------
 tests/ref/fate/filter-concat-vfr  | 88 +++++++++++++++----------------
 tests/ref/fate/swr-async-firstpts |  2 +-
 4 files changed, 94 insertions(+), 90 deletions(-)
diff mbox series

Patch

diff --git a/libavfilter/asrc_sine.c b/libavfilter/asrc_sine.c
index de9815c9c1..0e8f5e79dc 100644
--- a/libavfilter/asrc_sine.c
+++ b/libavfilter/asrc_sine.c
@@ -42,6 +42,7 @@  typedef struct SineContext {
     int64_t pts;
     uint32_t phi;  ///< current phase of the sine (2pi = 1<<32)
     uint32_t dphi; ///< phase increment between two samples
+    uint32_t dphi_correction_per_second;
     unsigned index;
     unsigned beep_length;
     uint32_t phi_beep;  ///< current phase of the beep
@@ -150,6 +151,7 @@  static av_cold int init(AVFilterContext *ctx)
     if (!(sine->sin = av_malloc(sizeof(*sine->sin) << LOG_PERIOD)))
         return AVERROR(ENOMEM);
     sine->dphi = ldexp(sine->frequency, 32) / sine->sample_rate + 0.5;
+    sine->dphi_correction_per_second = (uint32_t)round(ldexp(sine->frequency, 32)) - sine->sample_rate * sine->dphi;
     make_sin_table(sine->sin);
 
     if (sine->beep_factor) {
@@ -245,8 +247,10 @@  static int activate(AVFilterContext *ctx)
             samples[i] += sine->sin[sine->phi_beep >> (32 - LOG_PERIOD)] * 2;
             sine->phi_beep += sine->dphi_beep;
         }
-        if (++sine->index == sine->sample_rate)
+        if (++sine->index == sine->sample_rate) {
+            sine->phi += sine->dphi_correction_per_second;
             sine->index = 0;
+        }
     }
 
     frame->pts = sine->pts;
diff --git a/tests/ref/fate/filter-concat b/tests/ref/fate/filter-concat
index 42879a45e0..b28360b0c1 100644
--- a/tests/ref/fate/filter-concat
+++ b/tests/ref/fate/filter-concat
@@ -115,50 +115,50 @@ 
 1,     129160,     129160,     1024,     2048, 0x532bfbdd
 1,     130184,     130184,     1024,     2048, 0xfc36f5cd
 1,     131208,     131208,     1024,     2048, 0x2e8f0699
-1,     132232,     132232,     1024,     2048, 0x52382578
-1,     133256,     133256,     1024,     2048, 0x97ed1a28
-1,     134280,     134280,     1024,     2048, 0xabcdf73f
-1,     135304,     135304,     1024,     2048, 0x3a24082c
-1,     136328,     136328,     1024,     2048, 0xbe1cfc3d
-1,     137352,     137352,     1024,     2048, 0xad5800a5
-1,     138376,     138376,     1024,     2048, 0x90b80522
-1,     139400,     139400,     1024,     2048, 0x1fa1f912
-1,     140424,     140424,     1024,     2048, 0x733a0878
-1,     141448,     141448,     1024,     2048, 0x9a3eee47
-1,     142472,     142472,     1024,     2048, 0x5d900759
-1,     143496,     143496,     1024,     2048, 0x1287f540
-1,     144520,     144520,     1024,     2048, 0x941cfe5d
-1,     145544,     145544,     1024,     2048, 0x1587f8a9
-1,     146568,     146568,     1024,     2048, 0xb9e7f888
-1,     147592,     147592,     1024,     2048, 0xe9defbe2
-1,     148616,     148616,     1024,     2048, 0x3a5ef312
-1,     149640,     149640,     1024,     2048, 0xdcbe0544
-1,     150664,     150664,     1024,     2048, 0xbe51ecc5
-1,     151688,     151688,     1024,     2048, 0x21a60721
-1,     152712,     152712,     1024,     2048, 0xf29ff318
-1,     153736,     153736,     1024,     2048, 0xcd4c02ea
-1,     154760,     154760,     1024,     2048, 0xa424faac
-1,     155784,     155784,     1024,     2048, 0xbaedfdab
-1,     156808,     156808,     1024,     2048, 0xcbff047c
-1,     157832,     157832,     1024,     2048, 0x9ac8f96b
-1,     158856,     158856,     1024,     2048, 0x43220bee
-1,     159880,     159880,     1024,     2048, 0x547bf351
-1,     160904,     160904,     1024,     2048, 0x7dd10d6e
-1,     161928,     161928,     1024,     2048, 0x77cbf603
-1,     162952,     162952,     1024,     2048, 0xb6fcff50
-1,     163976,     163976,     1024,     2048, 0x927bfde5
-1,     165000,     165000,     1024,     2048, 0x5bd0fca5
-1,     166024,     166024,     1024,     2048, 0x672cff2a
-1,     167048,     167048,     1024,     2048, 0x3e3ef01c
-1,     168072,     168072,     1024,     2048, 0xe52607af
-1,     169096,     169096,     1024,     2048, 0x66bceaf5
-1,     170120,     170120,     1024,     2048, 0xe065046b
-1,     171144,     171144,     1024,     2048, 0x350bf21f
-1,     172168,     172168,     1024,     2048, 0x60b1fca4
-1,     173192,     173192,     1024,     2048, 0x8b1efa55
-1,     174216,     174216,     1024,     2048, 0xf86ff855
-1,     175240,     175240,     1024,     2048, 0x6934061b
-1,     176264,     176264,      136,      272, 0x4a458a45
+1,     132232,     132232,     1024,     2048, 0x50f22578
+1,     133256,     133256,     1024,     2048, 0x77731a21
+1,     134280,     134280,     1024,     2048, 0xb6eff73f
+1,     135304,     135304,     1024,     2048, 0x2c5a0824
+1,     136328,     136328,     1024,     2048, 0xd9f2fc3e
+1,     137352,     137352,     1024,     2048, 0xd352019b
+1,     138376,     138376,     1024,     2048, 0xf839042a
+1,     139400,     139400,     1024,     2048, 0xeb40fa0c
+1,     140424,     140424,     1024,     2048, 0x839a087e
+1,     141448,     141448,     1024,     2048, 0xad72ee4d
+1,     142472,     142472,     1024,     2048, 0x73da075f
+1,     143496,     143496,     1024,     2048, 0x2257f542
+1,     144520,     144520,     1024,     2048, 0x71f1ff60
+1,     145544,     145544,     1024,     2048, 0x1b33f8ab
+1,     146568,     146568,     1024,     2048, 0xc6a3f888
+1,     147592,     147592,     1024,     2048, 0xfea8fbe7
+1,     148616,     148616,     1024,     2048, 0x41def316
+1,     149640,     149640,     1024,     2048, 0xc958053c
+1,     150664,     150664,     1024,     2048, 0xd4cdeccc
+1,     151688,     151688,     1024,     2048, 0x05460718
+1,     152712,     152712,     1024,     2048, 0xfa69f317
+1,     153736,     153736,     1024,     2048, 0xcfd402e7
+1,     154760,     154760,     1024,     2048, 0xa74cfaaa
+1,     155784,     155784,     1024,     2048, 0xb7f3fdad
+1,     156808,     156808,     1024,     2048, 0x020f037f
+1,     157832,     157832,     1024,     2048, 0x8b4af96b
+1,     158856,     158856,     1024,     2048, 0x0cbc0ce9
+1,     159880,     159880,     1024,     2048, 0x48a1f34e
+1,     160904,     160904,     1024,     2048, 0x8ba50d71
+1,     161928,     161928,     1024,     2048, 0x6e6df606
+1,     162952,     162952,     1024,     2048, 0xb178ff51
+1,     163976,     163976,     1024,     2048, 0xe8fbfce6
+1,     165000,     165000,     1024,     2048, 0x53c4fca4
+1,     166024,     166024,     1024,     2048, 0xf4f0003e
+1,     167048,     167048,     1024,     2048, 0x2546f01b
+1,     168072,     168072,     1024,     2048, 0xe96207b2
+1,     169096,     169096,     1024,     2048, 0x5444eaf3
+1,     170120,     170120,     1024,     2048, 0xe0a10467
+1,     171144,     171144,     1024,     2048, 0x5b57f225
+1,     172168,     172168,     1024,     2048, 0x3aa5fc9f
+1,     173192,     173192,     1024,     2048, 0x9242fa54
+1,     174216,     174216,     1024,     2048, 0xe9c9f854
+1,     175240,     175240,     1024,     2048, 0x40310517
+1,     176264,     176264,      136,      272, 0x47578a43
 0,         20,         20,        1,   230400, 0x88c4d19a
 1,     176400,     176400,     1024,     2048, 0xdb0cfe95
 1,     177424,     177424,     1024,     2048, 0xcff3fdf1
diff --git a/tests/ref/fate/filter-concat-vfr b/tests/ref/fate/filter-concat-vfr
index 3d984a4968..d237cd164c 100644
--- a/tests/ref/fate/filter-concat-vfr
+++ b/tests/ref/fate/filter-concat-vfr
@@ -125,50 +125,50 @@ 
 0,    2933333,    2933333,    66667,   230400, 0x6a0c196b
 1,     130184,     130184,     1024,     2048, 0xfc36f5cd
 1,     131208,     131208,     1024,     2048, 0x2e8f0699
-1,     132232,     132232,     1024,     2048, 0x52382578
-1,     133256,     133256,     1024,     2048, 0x97ed1a28
-1,     134280,     134280,     1024,     2048, 0xabcdf73f
-1,     135304,     135304,     1024,     2048, 0x3a24082c
-1,     136328,     136328,     1024,     2048, 0xbe1cfc3d
-1,     137352,     137352,     1024,     2048, 0xad5800a5
-1,     138376,     138376,     1024,     2048, 0x90b80522
-1,     139400,     139400,     1024,     2048, 0x1fa1f912
-1,     140424,     140424,     1024,     2048, 0x733a0878
-1,     141448,     141448,     1024,     2048, 0x9a3eee47
-1,     142472,     142472,     1024,     2048, 0x5d900759
-1,     143496,     143496,     1024,     2048, 0x1287f540
-1,     144520,     144520,     1024,     2048, 0x941cfe5d
-1,     145544,     145544,     1024,     2048, 0x1587f8a9
-1,     146568,     146568,     1024,     2048, 0xb9e7f888
-1,     147592,     147592,     1024,     2048, 0xe9defbe2
-1,     148616,     148616,     1024,     2048, 0x3a5ef312
-1,     149640,     149640,     1024,     2048, 0xdcbe0544
-1,     150664,     150664,     1024,     2048, 0xbe51ecc5
-1,     151688,     151688,     1024,     2048, 0x21a60721
-1,     152712,     152712,     1024,     2048, 0xf29ff318
-1,     153736,     153736,     1024,     2048, 0xcd4c02ea
-1,     154760,     154760,     1024,     2048, 0xa424faac
-1,     155784,     155784,     1024,     2048, 0xbaedfdab
-1,     156808,     156808,     1024,     2048, 0xcbff047c
-1,     157832,     157832,     1024,     2048, 0x9ac8f96b
-1,     158856,     158856,     1024,     2048, 0x43220bee
-1,     159880,     159880,     1024,     2048, 0x547bf351
-1,     160904,     160904,     1024,     2048, 0x7dd10d6e
-1,     161928,     161928,     1024,     2048, 0x77cbf603
-1,     162952,     162952,     1024,     2048, 0xb6fcff50
-1,     163976,     163976,     1024,     2048, 0x927bfde5
-1,     165000,     165000,     1024,     2048, 0x5bd0fca5
-1,     166024,     166024,     1024,     2048, 0x672cff2a
-1,     167048,     167048,     1024,     2048, 0x3e3ef01c
-1,     168072,     168072,     1024,     2048, 0xe52607af
-1,     169096,     169096,     1024,     2048, 0x66bceaf5
-1,     170120,     170120,     1024,     2048, 0xe065046b
-1,     171144,     171144,     1024,     2048, 0x350bf21f
-1,     172168,     172168,     1024,     2048, 0x60b1fca4
-1,     173192,     173192,     1024,     2048, 0x8b1efa55
-1,     174216,     174216,     1024,     2048, 0xf86ff855
-1,     175240,     175240,     1024,     2048, 0x6934061b
-1,     176264,     176264,      136,      272, 0x4a458a45
+1,     132232,     132232,     1024,     2048, 0x50f22578
+1,     133256,     133256,     1024,     2048, 0x77731a21
+1,     134280,     134280,     1024,     2048, 0xb6eff73f
+1,     135304,     135304,     1024,     2048, 0x2c5a0824
+1,     136328,     136328,     1024,     2048, 0xd9f2fc3e
+1,     137352,     137352,     1024,     2048, 0xd352019b
+1,     138376,     138376,     1024,     2048, 0xf839042a
+1,     139400,     139400,     1024,     2048, 0xeb40fa0c
+1,     140424,     140424,     1024,     2048, 0x839a087e
+1,     141448,     141448,     1024,     2048, 0xad72ee4d
+1,     142472,     142472,     1024,     2048, 0x73da075f
+1,     143496,     143496,     1024,     2048, 0x2257f542
+1,     144520,     144520,     1024,     2048, 0x71f1ff60
+1,     145544,     145544,     1024,     2048, 0x1b33f8ab
+1,     146568,     146568,     1024,     2048, 0xc6a3f888
+1,     147592,     147592,     1024,     2048, 0xfea8fbe7
+1,     148616,     148616,     1024,     2048, 0x41def316
+1,     149640,     149640,     1024,     2048, 0xc958053c
+1,     150664,     150664,     1024,     2048, 0xd4cdeccc
+1,     151688,     151688,     1024,     2048, 0x05460718
+1,     152712,     152712,     1024,     2048, 0xfa69f317
+1,     153736,     153736,     1024,     2048, 0xcfd402e7
+1,     154760,     154760,     1024,     2048, 0xa74cfaaa
+1,     155784,     155784,     1024,     2048, 0xb7f3fdad
+1,     156808,     156808,     1024,     2048, 0x020f037f
+1,     157832,     157832,     1024,     2048, 0x8b4af96b
+1,     158856,     158856,     1024,     2048, 0x0cbc0ce9
+1,     159880,     159880,     1024,     2048, 0x48a1f34e
+1,     160904,     160904,     1024,     2048, 0x8ba50d71
+1,     161928,     161928,     1024,     2048, 0x6e6df606
+1,     162952,     162952,     1024,     2048, 0xb178ff51
+1,     163976,     163976,     1024,     2048, 0xe8fbfce6
+1,     165000,     165000,     1024,     2048, 0x53c4fca4
+1,     166024,     166024,     1024,     2048, 0xf4f0003e
+1,     167048,     167048,     1024,     2048, 0x2546f01b
+1,     168072,     168072,     1024,     2048, 0xe96207b2
+1,     169096,     169096,     1024,     2048, 0x5444eaf3
+1,     170120,     170120,     1024,     2048, 0xe0a10467
+1,     171144,     171144,     1024,     2048, 0x5b57f225
+1,     172168,     172168,     1024,     2048, 0x3aa5fc9f
+1,     173192,     173192,     1024,     2048, 0x9242fa54
+1,     174216,     174216,     1024,     2048, 0xe9c9f854
+1,     175240,     175240,     1024,     2048, 0x40310517
+1,     176264,     176264,      136,      272, 0x47578a43
 0,    4000000,    4000000,   125000,   230400, 0x88c4d19a
 1,     176400,     176400,     1024,     2048, 0xdb0cfe95
 1,     177424,     177424,     1024,     2048, 0xcff3fdf1
diff --git a/tests/ref/fate/swr-async-firstpts b/tests/ref/fate/swr-async-firstpts
index 3f6b290bab..beb43a47ea 100644
--- a/tests/ref/fate/swr-async-firstpts
+++ b/tests/ref/fate/swr-async-firstpts
@@ -15,7 +15,7 @@ 
 0,        919,        919,      149,      298, 0xc273245f
 0,       1068,       1068,      136,      272, 0xedeb6e0a
 0,       1204,       1204,       98,      196, 0xea18668e
-0,       1302,       1302,       98,      196, 0x412861e7
+0,       1302,       1302,       98,      196, 0x41cc61e8
 0,       1400,       1400,       98,      196, 0x7ec361b2
 0,       1498,       1498,      110,      220, 0xf3ae6a6a
 0,       1608,       1608,      108,      216, 0xab2f6c93