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 1/9] avradio/sdr: use AVTree for candidate stations
@ 2023-07-07 17:22 Michael Niedermayer
  2023-07-07 17:22 ` [FFmpeg-devel] [PATCH 2/9] avradio/sdr: Consolidate candidate station entries Michael Niedermayer
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: Michael Niedermayer @ 2023-07-07 17:22 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

This is needed so we can keep more information about stations without
performance issues

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavradio/sdr.h      |   4 +-
 libavradio/sdrdemux.c | 126 ++++++++++++++++++++++++++++++++++++------
 2 files changed, 110 insertions(+), 20 deletions(-)

diff --git a/libavradio/sdr.h b/libavradio/sdr.h
index 84abdbe1d9..4d6887a296 100644
--- a/libavradio/sdr.h
+++ b/libavradio/sdr.h
@@ -30,6 +30,7 @@
 #include "libavutil/opt.h"
 #include "libavutil/time.h"
 #include "libavutil/thread.h"
+#include "libavutil/tree.h"
 #include "libavutil/tx.h"
 #include "libavformat/avformat.h"
 
@@ -131,8 +132,7 @@ typedef struct SDRContext {
      * Current list of detected stations, these can be overlapping and low quality detections.
      * Used for probing. Stations are not removed from this when added to station.
      */
-    Station **candidate_station;
-    int nb_candidate_stations;
+    struct AVTreeNode *station_root;
     int width, height;
     int single_ch_audio_st_index;
     int waterfall_st_index;
diff --git a/libavradio/sdrdemux.c b/libavradio/sdrdemux.c
index d3f0368d7d..4ac360f71b 100644
--- a/libavradio/sdrdemux.c
+++ b/libavradio/sdrdemux.c
@@ -46,6 +46,7 @@
 #include "libavutil/opt.h"
 #include "libavutil/time.h"
 #include "libavutil/thread.h"
+#include "libavutil/tree.h"
 #include "libavutil/tx.h"
 #include "libavutil/xga_font_data.h"
 #include "libavcodec/kbdwin.h"
@@ -118,6 +119,55 @@ static void free_station(Station *station)
     av_free(station);
 }
 
+typedef struct FindStationContext {
+    double freq;
+    double range;
+    Station **station_list;
+    int station_list_size;
+    int nb_stations;
+} FindStationContext;
+
+static int find_station_cmp(void *opaque, void *elem)
+{
+    FindStationContext *c = opaque;
+    Station *station = elem;
+    double distance = station->frequency - c->freq;
+    if (distance < -c->range)
+        return -1;
+    if (distance >  c->range)
+        return  1;
+    return 0;
+}
+
+static int find_station_enu(void *opaque, void *elem)
+{
+    FindStationContext *c = opaque;
+    if (c->nb_stations < c->station_list_size) {
+        c->station_list[c->nb_stations++] = elem;
+    } else
+        av_log(NULL, AV_LOG_WARNING, "find station reached list size of %d\n", c->station_list_size);
+
+    return 0;
+}
+
+/**
+ * Find stations within the given parameters.
+ * @param[out] station_list array to return stations in
+ * @param nb_stations size of station array
+ * @returns number of stations found
+ */
+static int find_stations(SDRContext *sdr, double freq, double range, Station **station_list, int station_list_size)
+{
+    FindStationContext find_station_context;
+    find_station_context.freq = freq;
+    find_station_context.range = range;
+    find_station_context.station_list = station_list;
+    find_station_context.station_list_size = station_list_size;
+    find_station_context.nb_stations = 0;
+    av_tree_enumerate(sdr->station_root, &find_station_context, find_station_cmp, find_station_enu);
+    return find_station_context.nb_stations;
+}
+
 static int create_station(SDRContext *sdr, Station *candidate_station) {
     enum Modulation modulation  = candidate_station->modulation;
     double freq                 = candidate_station->frequency;
@@ -165,14 +215,17 @@ static int create_station(SDRContext *sdr, Station *candidate_station) {
         best_station->timeout = 0;
         return best_station_index;
     }
-    for (i=0; i<sdr->nb_candidate_stations; i++) {
+    Station *station_list[1000];
+    int nb_stations = find_stations(sdr, sdr->block_center_freq, sdr->sdr_sample_rate*0.5, station_list, FF_ARRAY_ELEMS(station_list));
+
+    for (i=0; i<nb_stations; i++) {
         int freq_precission = modulation == AM ? 5 : 50;
-        double delta = fabs(sdr->candidate_station[i]->frequency - freq);
+        double delta = fabs(station_list[i]->frequency - freq);
         // Station already added, or we have 2 rather close stations
-        if (modulation == sdr->candidate_station[i]->modulation && delta < freq_precission && sdr->candidate_station[i] != candidate_station) {
+        if (modulation == station_list[i]->modulation && delta < freq_precission && station_list[i] != candidate_station) {
             nb_candidate_match++;
         }
-        if (modulation != sdr->candidate_station[i]->modulation && delta < (bandwidth + sdr->candidate_station[i]->bandwidth)/2.1)
+        if (modulation != station_list[i]->modulation && delta < (bandwidth + station_list[i]->bandwidth)/2.1)
             nb_candidate_conflict++;
     }
     //if we have a recent conflict with an established station, skip this one
@@ -225,18 +278,51 @@ static int create_station(SDRContext *sdr, Station *candidate_station) {
 
 static void create_stations(SDRContext *sdr)
 {
-    for(int i = 0; i<sdr->nb_candidate_stations; i++) {
-        Station *candidate_station = sdr->candidate_station[i];
-        create_station(sdr, candidate_station);
+    Station *station_list[1000];
+
+    if (!sdr->block_center_freq)
+        return;
+
+    int nb_stations = find_stations(sdr, sdr->block_center_freq, sdr->sdr_sample_rate*0.5, station_list, FF_ARRAY_ELEMS(station_list));
+
+    for(int i = 0; i<nb_stations; i++) {
+        create_station(sdr, station_list[i]);
     }
 }
 
+static int station_cmp(const void *key, const void *b)
+{
+    const Station *sa = key;
+    const Station *sb = b;
+    return 2*((sa->frequency  > sb->frequency ) - (sa->frequency  < sb-> frequency))
+             +(sa->modulation > sb->modulation) - (sa->modulation < sb->modulation);
+}
+
+static void *tree_insert(struct AVTreeNode **rootp, void *key,
+                  int (*cmp)(const void *key, const void *b),
+                  struct AVTreeNode **next)
+{
+    if (!*next)
+        *next = av_mallocz(av_tree_node_size); //FIXME check ENOMEM
+    return av_tree_insert(rootp, key, cmp, next);
+}
+
+static void *tree_remove(struct AVTreeNode **rootp, void *key,
+                  int (*cmp)(const void *key, const void *b), struct AVTreeNode **next)
+{
+    av_freep(next);
+    return av_tree_insert(rootp, key, cmp, next);
+}
+
 /**
  * remove stations which we no longer receive well
  * Especially with AM and weather conditions stations disapear, this keeps things a bit more tidy
  */
 static void decay_stations(SDRContext *sdr)
 {
+    Station *station_list[1000];
+    int nb_stations = find_stations(sdr, sdr->block_center_freq, sdr->bandwidth*0.5, station_list, FF_ARRAY_ELEMS(station_list));
+
     for (int i=0; i<sdr->nb_stations; i++) {
         Station *station = sdr->station[i];
 
@@ -252,12 +338,15 @@ static void decay_stations(SDRContext *sdr)
         }
     }
 
-    for (int i=0; i<sdr->nb_candidate_stations; i++) {
-        Station *station = sdr->candidate_station[i];
+    for (int i=0; i<nb_stations; i++) {
+        Station *station = station_list[i];
 
         if (station->timeout++ > CANDIDATE_STATION_TIMEOUT) {
+            struct AVTreeNode *next = NULL;
+            tree_remove(&sdr->station_root, station, station_cmp, &next);
+            av_freep(&next);
+
             free_station(station);
-            sdr->candidate_station[i--] = sdr->candidate_station[--sdr->nb_candidate_stations];
         }
     }
 }
@@ -265,25 +354,26 @@ static void decay_stations(SDRContext *sdr)
 static int create_candidate_station(SDRContext *sdr, enum Modulation modulation, double freq, int64_t bandwidth, int64_t bandwidth_p2, float score) {
     Station *station;
     void *tmp;
-
-    tmp = av_realloc_array(sdr->candidate_station, sdr->nb_candidate_stations+1, sizeof(*sdr->candidate_station));
-    if (!tmp)
-        return AVERROR(ENOMEM);
-    sdr->candidate_station = tmp;
+    struct AVTreeNode *next = NULL;
 
     station = av_mallocz(sizeof(*station));
     if (!station)
         return AVERROR(ENOMEM);
 
-    sdr->candidate_station[sdr->nb_candidate_stations++] = station;
-
     station->modulation   = modulation;
     station->frequency    = freq;
     station->bandwidth    = bandwidth;
     station->bandwidth_p2 = bandwidth_p2;
     station->score        = score;
 
-    return sdr->nb_candidate_stations - 1;
+    tmp = tree_insert(&sdr->station_root, station, station_cmp, &next);
+    if (tmp && tmp != station) {
+        //unlikely
+        av_freep(&station);
+    }
+    av_freep(&next);
+
+    return 1;
 }
 
 static void probe_common(SDRContext *sdr)
-- 
2.31.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] 10+ messages in thread

* [FFmpeg-devel] [PATCH 2/9] avradio/sdr: Consolidate candidate station entries
  2023-07-07 17:22 [FFmpeg-devel] [PATCH 1/9] avradio/sdr: use AVTree for candidate stations Michael Niedermayer
@ 2023-07-07 17:22 ` Michael Niedermayer
  2023-07-07 17:22 ` [FFmpeg-devel] [PATCH 3/9] avradio/sdr: consolidate the candidate station list with the main list Michael Niedermayer
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Michael Niedermayer @ 2023-07-07 17:22 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

also average frequcnies in candiddate station detections

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavradio/sdr.h      |  1 +
 libavradio/sdrdemux.c | 45 ++++++++++++++++++++++++++++++++++++-------
 2 files changed, 39 insertions(+), 7 deletions(-)

diff --git a/libavradio/sdr.h b/libavradio/sdr.h
index 4d6887a296..29465df0a1 100644
--- a/libavradio/sdr.h
+++ b/libavradio/sdr.h
@@ -70,6 +70,7 @@ typedef struct Station {
     char *name;
     enum Modulation modulation;
     double frequency;
+    int nb_frequency;       ///< number of detections which are used to compute the frequency
     int64_t bandwidth;
     int64_t bandwidth_p2;
     float score;
diff --git a/libavradio/sdrdemux.c b/libavradio/sdrdemux.c
index 4ac360f71b..41eda615ae 100644
--- a/libavradio/sdrdemux.c
+++ b/libavradio/sdrdemux.c
@@ -150,6 +150,12 @@ static int find_station_enu(void *opaque, void *elem)
     return 0;
 }
 
+static int free_station_enu(void *opaque, void *elem)
+{
+    free_station(elem);
+    return 0;
+}
+
 /**
  * Find stations within the given parameters.
  * @param[out] station_list array to return stations in
@@ -218,15 +224,16 @@ static int create_station(SDRContext *sdr, Station *candidate_station) {
     Station *station_list[1000];
     int nb_stations = find_stations(sdr, sdr->block_center_freq, sdr->sdr_sample_rate*0.5, station_list, FF_ARRAY_ELEMS(station_list));
 
+    nb_candidate_match += candidate_station->nb_frequency - 1;
     for (i=0; i<nb_stations; i++) {
         int freq_precission = modulation == AM ? 5 : 50;
         double delta = fabs(station_list[i]->frequency - freq);
         // Station already added, or we have 2 rather close stations
         if (modulation == station_list[i]->modulation && delta < freq_precission && station_list[i] != candidate_station) {
-            nb_candidate_match++;
+            nb_candidate_match += station_list[i]->nb_frequency;
         }
         if (modulation != station_list[i]->modulation && delta < (bandwidth + station_list[i]->bandwidth)/2.1)
-            nb_candidate_conflict++;
+            nb_candidate_conflict += station_list[i]->nb_frequency;
     }
     //if we have a recent conflict with an established station, skip this one
     if (conflict < CANDIDATE_STATION_TIMEOUT)
@@ -355,20 +362,41 @@ static int create_candidate_station(SDRContext *sdr, enum Modulation modulation,
     Station *station;
     void *tmp;
     struct AVTreeNode *next = NULL;
+    Station *station_list[1000];
+    double snapdistance = modulation == AM ? 5 : 50;
+    int nb_stations = find_stations(sdr, freq, snapdistance, station_list, FF_ARRAY_ELEMS(station_list));
+
+    if (nb_stations) {
+        for(int i = 1; i<nb_stations; i++)
+            if (station_list[0]->modulation != modulation ||
+                (station_list[i]->modulation == modulation &&
+                 fabs(station_list[0]->frequency - freq) > fabs(station_list[i]->frequency - freq)))
+                station_list[0] = station_list[i];
+        nb_stations = station_list[0]->modulation == modulation;
+    }
 
-    station = av_mallocz(sizeof(*station));
-    if (!station)
-        return AVERROR(ENOMEM);
+    if (!nb_stations) {
+        station = av_mallocz(sizeof(*station));
+        if (!station)
+            return AVERROR(ENOMEM);
+        station->frequency = freq;
+    } else {
+        station = station_list[0];
+        // We will update the frequency so we need to reinsert
+        tree_remove(&sdr->station_root, station, station_cmp, &next);
+        station->frequency = station->nb_frequency * station->frequency + freq;
+        station->timeout   = 0;
+    }
+    station->frequency /= ++station->nb_frequency;
 
     station->modulation   = modulation;
-    station->frequency    = freq;
     station->bandwidth    = bandwidth;
     station->bandwidth_p2 = bandwidth_p2;
     station->score        = score;
 
     tmp = tree_insert(&sdr->station_root, station, station_cmp, &next);
     if (tmp && tmp != station) {
-        //unlikely
+        //This will not happen in real C implementations but floats allow odd things in theory
         av_freep(&station);
     }
     av_freep(&next);
@@ -2025,6 +2053,9 @@ int avpriv_sdr_read_close(AVFormatContext *s)
     }
     sdr->nb_stations = 0;
     av_freep(&sdr->station);
+    av_tree_enumerate(sdr->station_root, NULL, NULL, free_station_enu);
+    av_tree_destroy(sdr->station_root);
+    sdr->station_root = NULL;
 
     av_freep(&sdr->windowed_block);
     av_freep(&sdr->block);
-- 
2.31.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] 10+ messages in thread

* [FFmpeg-devel] [PATCH 3/9] avradio/sdr: consolidate the candidate station list with the main list
  2023-07-07 17:22 [FFmpeg-devel] [PATCH 1/9] avradio/sdr: use AVTree for candidate stations Michael Niedermayer
  2023-07-07 17:22 ` [FFmpeg-devel] [PATCH 2/9] avradio/sdr: Consolidate candidate station entries Michael Niedermayer
@ 2023-07-07 17:22 ` Michael Niedermayer
  2023-07-07 17:22 ` [FFmpeg-devel] [PATCH 4/9] avradio/sdr: Eliminate station list Michael Niedermayer
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Michael Niedermayer @ 2023-07-07 17:22 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

This way any updates to one is reflected in the other
---
 libavradio/sdr.h      |  1 +
 libavradio/sdrdemux.c | 53 ++++++++++++++++++++++---------------------
 2 files changed, 28 insertions(+), 26 deletions(-)

diff --git a/libavradio/sdr.h b/libavradio/sdr.h
index 29465df0a1..4b1543efd3 100644
--- a/libavradio/sdr.h
+++ b/libavradio/sdr.h
@@ -74,6 +74,7 @@ typedef struct Station {
     int64_t bandwidth;
     int64_t bandwidth_p2;
     float score;
+    int in_station_list;    ///< non zero if this station is in the station list
     int timeout;            //since how many blocks was this detectable but not detected
     int multiplex_index;    //DAB can have multiple stations on one frequency
 
diff --git a/libavradio/sdrdemux.c b/libavradio/sdrdemux.c
index 41eda615ae..7744ecdac0 100644
--- a/libavradio/sdrdemux.c
+++ b/libavradio/sdrdemux.c
@@ -180,7 +180,6 @@ static int create_station(SDRContext *sdr, Station *candidate_station) {
     int64_t bandwidth           = candidate_station->bandwidth;
     int64_t bandwidth_p2        = candidate_station->bandwidth_p2;
     float score                 = candidate_station->score;
-    Station *station;
     void *tmp;
     int i;
     Station *best_station = NULL;
@@ -191,6 +190,9 @@ static int create_station(SDRContext *sdr, Station *candidate_station) {
     int nb_candidate_conflict = 0;
     int nb_candidate_match = 0;
 
+    if (candidate_station->in_station_list)
+        return 0;
+
     for (i=0; i<sdr->nb_stations; i++) {
         double delta = fabs(sdr->station[i]->frequency - freq);
         // Station already added, or we have 2 rather close stations
@@ -215,10 +217,16 @@ static int create_station(SDRContext *sdr, Station *candidate_station) {
                     best_station->score, score,
                     best_station->frequency - freq, best_station->frequency, freq
             );
-            best_station->frequency  = freq;
-            best_station->score      = score;
+
+            if (best_station->stream) {
+                candidate_station->stream = best_station->stream;
+                best_station->stream = NULL;
+                candidate_station->stream->station = candidate_station;
+            }
+            candidate_station->in_station_list = 1;
+            best_station->in_station_list = 0;
+            sdr->station[best_station_index] = candidate_station;
         }
-        best_station->timeout = 0;
         return best_station_index;
     }
     Station *station_list[1000];
@@ -227,17 +235,21 @@ static int create_station(SDRContext *sdr, Station *candidate_station) {
     nb_candidate_match += candidate_station->nb_frequency - 1;
     for (i=0; i<nb_stations; i++) {
         int freq_precission = modulation == AM ? 5 : 50;
-        double delta = fabs(station_list[i]->frequency - freq);
+        Station *s = station_list[i];
+        double delta = fabs(s->frequency - freq);
+
         // Station already added, or we have 2 rather close stations
-        if (modulation == station_list[i]->modulation && delta < freq_precission && station_list[i] != candidate_station) {
-            nb_candidate_match += station_list[i]->nb_frequency;
+        if (modulation == s->modulation && delta < freq_precission && s != candidate_station) {
+            nb_candidate_match += s->nb_frequency;
         }
-        if (modulation != station_list[i]->modulation && delta < (bandwidth + station_list[i]->bandwidth)/2.1)
-            nb_candidate_conflict += station_list[i]->nb_frequency;
+        if (modulation != s->modulation && delta < (bandwidth + s->bandwidth)/2.1)
+            nb_candidate_conflict += s->nb_frequency;
     }
     //if we have a recent conflict with an established station, skip this one
     if (conflict < CANDIDATE_STATION_TIMEOUT)
         return -1;
+    if (conflict < candidate_station->timeout)
+        return -1;
 
     //AM detection is less reliable ATM so we dont want it to override FM stations
     if (modulation == AM && conflict < INT_MAX)
@@ -255,7 +267,8 @@ static int create_station(SDRContext *sdr, Station *candidate_station) {
             // We recheck that the stations we remove are not active because floating point could round differently
             if (sdr->station[i]->stream == NULL &&
                 modulation != sdr->station[i]->modulation && delta < (bandwidth + sdr->station[i]->bandwidth)/2.1) {
-                free_station(sdr->station[i]);
+
+                sdr->station[i]->in_station_list = 0;
                 sdr->station[i--] = sdr->station[--sdr->nb_stations];
             }
         }
@@ -266,17 +279,8 @@ static int create_station(SDRContext *sdr, Station *candidate_station) {
         return AVERROR(ENOMEM);
     sdr->station = tmp;
 
-    station = av_mallocz(sizeof(*station));
-    if (!station)
-        return AVERROR(ENOMEM);
-
-    sdr->station[sdr->nb_stations++] = station;
-
-    station->modulation   = modulation;
-    station->frequency    = freq;
-    station->bandwidth    = bandwidth;
-    station->bandwidth_p2 = bandwidth_p2;
-    station->score        = score;
+    sdr->station[sdr->nb_stations++] = candidate_station;
+    candidate_station->in_station_list = 1;
 
     av_log(sdr, AV_LOG_INFO, "create_station %d f:%f bw:%"PRId64"/%"PRId64" score: %f\n", modulation, freq, bandwidth, bandwidth_p2, score);
 
@@ -340,7 +344,7 @@ static void decay_stations(SDRContext *sdr)
             continue;
 
         if (station->timeout++ > STATION_TIMEOUT) {
-            free_station(station);
+            station->in_station_list = 0;
             sdr->station[i--] = sdr->station[--sdr->nb_stations];
         }
     }
@@ -348,7 +352,7 @@ static void decay_stations(SDRContext *sdr)
     for (int i=0; i<nb_stations; i++) {
         Station *station = station_list[i];
 
-        if (station->timeout++ > CANDIDATE_STATION_TIMEOUT) {
+        if (station->timeout++ > CANDIDATE_STATION_TIMEOUT && !station->in_station_list) {
             struct AVTreeNode *next = NULL;
             tree_remove(&sdr->station_root, station, station_cmp, &next);
             av_freep(&next);
@@ -2048,9 +2052,6 @@ int avpriv_sdr_read_close(AVFormatContext *s)
         sst->frame_size = 0;
     }
 
-    for (i = 0; i < sdr->nb_stations; i++) {
-        free_station(sdr->station[i]);
-    }
     sdr->nb_stations = 0;
     av_freep(&sdr->station);
     av_tree_enumerate(sdr->station_root, NULL, NULL, free_station_enu);
-- 
2.31.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] 10+ messages in thread

* [FFmpeg-devel] [PATCH 4/9] avradio/sdr: Eliminate station list
  2023-07-07 17:22 [FFmpeg-devel] [PATCH 1/9] avradio/sdr: use AVTree for candidate stations Michael Niedermayer
  2023-07-07 17:22 ` [FFmpeg-devel] [PATCH 2/9] avradio/sdr: Consolidate candidate station entries Michael Niedermayer
  2023-07-07 17:22 ` [FFmpeg-devel] [PATCH 3/9] avradio/sdr: consolidate the candidate station list with the main list Michael Niedermayer
@ 2023-07-07 17:22 ` Michael Niedermayer
  2023-07-07 17:22 ` [FFmpeg-devel] [PATCH 5/9] avradio/sdr: Allow user to adjust FM/AM thresholds Michael Niedermayer
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Michael Niedermayer @ 2023-07-07 17:22 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

Its redudnant with the AVTree information
Makes the code simpler

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavradio/sdr.h      |   9 ----
 libavradio/sdrdemux.c | 107 ++++++++++++++++++++++--------------------
 2 files changed, 55 insertions(+), 61 deletions(-)

diff --git a/libavradio/sdr.h b/libavradio/sdr.h
index 4b1543efd3..00066850f9 100644
--- a/libavradio/sdr.h
+++ b/libavradio/sdr.h
@@ -125,15 +125,6 @@ typedef struct SDRContext {
     char *dump_url;
     int fileheader_size;
     AVIOContext *dump_avio;
-    /**
-     * Current list of unambigously detected stations
-     */
-    Station **station;
-    int nb_stations;
-    /**
-     * Current list of detected stations, these can be overlapping and low quality detections.
-     * Used for probing. Stations are not removed from this when added to station.
-     */
     struct AVTreeNode *station_root;
     int width, height;
     int single_ch_audio_st_index;
diff --git a/libavradio/sdrdemux.c b/libavradio/sdrdemux.c
index 7744ecdac0..769cf3ade6 100644
--- a/libavradio/sdrdemux.c
+++ b/libavradio/sdrdemux.c
@@ -183,7 +183,6 @@ static int create_station(SDRContext *sdr, Station *candidate_station) {
     void *tmp;
     int i;
     Station *best_station = NULL;
-    int best_station_index = -1;
     float drift = bandwidth/3.0;
     double best_distance = drift;
     int conflict = INT_MAX;
@@ -193,27 +192,32 @@ static int create_station(SDRContext *sdr, Station *candidate_station) {
     if (candidate_station->in_station_list)
         return 0;
 
-    for (i=0; i<sdr->nb_stations; i++) {
-        double delta = fabs(sdr->station[i]->frequency - freq);
+    Station *station_list[1000];
+    int nb_stations = find_stations(sdr, sdr->block_center_freq, sdr->sdr_sample_rate*0.5, station_list, FF_ARRAY_ELEMS(station_list));
+    for (i=0; i<nb_stations; i++) {
+        Station *s = station_list[i];
+        double delta = fabs(s->frequency - freq);
+
+        if (!s->in_station_list)
+            continue;
+
         // Station already added, or we have 2 rather close stations
         //FIXME we want to make sure that the stronger station is not skiped but we also dont want to add a station twice
-        if (modulation == sdr->station[i]->modulation && delta < best_distance) {
+        if (modulation == s->modulation && delta < best_distance) {
             best_distance = delta;
-            best_station = sdr->station[i];
-            best_station_index = i;
+            best_station = s;
         }
-        if (modulation != sdr->station[i]->modulation && delta < (bandwidth + sdr->station[i]->bandwidth)/2.1) {
-            conflict = FFMIN(conflict, sdr->station[i]->timeout);
+        if (modulation !=s->modulation && delta < (bandwidth + s->bandwidth)/2.1) {
+            conflict = FFMIN(conflict, s->timeout);
             // special case, lets not remove an actively listen to station, this can be done too but that needs more thought
-            if (sdr->station[i]->stream)
+            if (s->stream)
                 conflict = 0;
         }
     }
     if (best_station) {
         if (score > best_station->score && conflict == INT_MAX) {
             int log_level = fabs(best_station->frequency - freq) < 3.0 ? AV_LOG_DEBUG : AV_LOG_WARNING;
-            av_log(sdr->avfmt, log_level, "Update station %d score:%f -> %f freq: %f %f -> %f\n",
-                    best_station_index,
+            av_log(sdr->avfmt, log_level, "Update station score:%f -> %f freq: %f %f -> %f\n",
                     best_station->score, score,
                     best_station->frequency - freq, best_station->frequency, freq
             );
@@ -225,12 +229,9 @@ static int create_station(SDRContext *sdr, Station *candidate_station) {
             }
             candidate_station->in_station_list = 1;
             best_station->in_station_list = 0;
-            sdr->station[best_station_index] = candidate_station;
         }
-        return best_station_index;
+        return 2;
     }
-    Station *station_list[1000];
-    int nb_stations = find_stations(sdr, sdr->block_center_freq, sdr->sdr_sample_rate*0.5, station_list, FF_ARRAY_ELEMS(station_list));
 
     nb_candidate_match += candidate_station->nb_frequency - 1;
     for (i=0; i<nb_stations; i++) {
@@ -262,29 +263,23 @@ static int create_station(SDRContext *sdr, Station *candidate_station) {
         return -1;
 
     if (conflict < INT_MAX) {
-        for (i=0; i<sdr->nb_stations; i++) {
-            double delta = fabs(sdr->station[i]->frequency - freq);
+        for (i=0; i<nb_stations; i++) {
+            Station *s = station_list[i];
+            double delta = fabs(s->frequency - freq);
             // We recheck that the stations we remove are not active because floating point could round differently
-            if (sdr->station[i]->stream == NULL &&
-                modulation != sdr->station[i]->modulation && delta < (bandwidth + sdr->station[i]->bandwidth)/2.1) {
+            if (s->stream == NULL &&
+                modulation != s->modulation && delta < (bandwidth + s->bandwidth)/2.1) {
 
-                sdr->station[i]->in_station_list = 0;
-                sdr->station[i--] = sdr->station[--sdr->nb_stations];
+                s->in_station_list = 0;
             }
         }
     }
 
-    tmp = av_realloc_array(sdr->station, sdr->nb_stations+1, sizeof(*sdr->station));
-    if (!tmp)
-        return AVERROR(ENOMEM);
-    sdr->station = tmp;
-
-    sdr->station[sdr->nb_stations++] = candidate_station;
     candidate_station->in_station_list = 1;
 
     av_log(sdr, AV_LOG_INFO, "create_station %d f:%f bw:%"PRId64"/%"PRId64" score: %f\n", modulation, freq, bandwidth, bandwidth_p2, score);
 
-    return sdr->nb_stations - 1;
+    return 1;
 }
 
 static void create_stations(SDRContext *sdr)
@@ -334,8 +329,8 @@ static void decay_stations(SDRContext *sdr)
     Station *station_list[1000];
     int nb_stations = find_stations(sdr, sdr->block_center_freq, sdr->bandwidth*0.5, station_list, FF_ARRAY_ELEMS(station_list));
 
-    for (int i=0; i<sdr->nb_stations; i++) {
-        Station *station = sdr->station[i];
+    for (int i=0; i<nb_stations; i++) {
+        Station *station = station_list[i];
 
         if (station->frequency - station->bandwidth/2 < sdr->block_center_freq - sdr->bandwidth/2 ||
             station->frequency + station->bandwidth/2 > sdr->block_center_freq + sdr->bandwidth/2)
@@ -343,21 +338,18 @@ static void decay_stations(SDRContext *sdr)
         if (station->stream)
             continue;
 
-        if (station->timeout++ > STATION_TIMEOUT) {
-            station->in_station_list = 0;
-            sdr->station[i--] = sdr->station[--sdr->nb_stations];
-        }
-    }
-
-    for (int i=0; i<nb_stations; i++) {
-        Station *station = station_list[i];
-
-        if (station->timeout++ > CANDIDATE_STATION_TIMEOUT && !station->in_station_list) {
-            struct AVTreeNode *next = NULL;
-            tree_remove(&sdr->station_root, station, station_cmp, &next);
-            av_freep(&next);
+        if (station->in_station_list) {
+            if (station->timeout++ > STATION_TIMEOUT) {
+                station->in_station_list = 0;
+            }
+        } else {
+            if (station->timeout++ > CANDIDATE_STATION_TIMEOUT) {
+                struct AVTreeNode *next = NULL;
+                tree_remove(&sdr->station_root, station, station_cmp, &next);
+                av_freep(&next);
 
-            free_station(station);
+                free_station(station);
+            }
         }
     }
 }
@@ -1209,6 +1201,8 @@ static int snap2station(SDRContext *sdr, int *seek_direction) {
     double current_freq;
     double best_distance = INT64_MAX;
     Station *best_station = NULL;
+    Station *station_list[1000];
+    int nb_stations = find_stations(sdr, sdr->block_center_freq, sdr->sdr_sample_rate*0.5, station_list, FF_ARRAY_ELEMS(station_list));
 
     if (sst->station) {
         current_freq = sst->station->frequency;
@@ -1217,10 +1211,13 @@ static int snap2station(SDRContext *sdr, int *seek_direction) {
     } else
         current_freq = sdr->block_center_freq;
 
-    for(int i = 0; i<sdr->nb_stations; i++) {
-        Station *station = sdr->station[i];
+    for(int i = 0; i<nb_stations; i++) {
+        Station *station = station_list[i];
         double distance = station->frequency - current_freq;
 
+        if (!station->in_station_list)
+            continue;
+
         if (distance * *seek_direction < 0 || station == sst->station)
             continue;
         distance = fabs(distance);
@@ -1685,14 +1682,20 @@ static int vissualization(SDRContext *sdr, AVStream *st, AVPacket *pkt)
         for (int y= h2; y<h; y++)
             memcpy(pkt->data + 4*y*w, sst->frame_buffer + 4*(y + sst->frame_buffer_line - h2)*w, 4*w);
 
-        for(int station_index = 0; station_index<sdr->nb_stations; station_index++) {
-            Station *s = sdr->station[station_index];
+        Station *station_list[1000];
+        int nb_stations = find_stations(sdr, sdr->block_center_freq, sdr->sdr_sample_rate*0.6, station_list, FF_ARRAY_ELEMS(station_list));
+        for(int station_index = 0; station_index<nb_stations; station_index++) {
+            Station *s = station_list[station_index];
             double f = s->frequency;
             int xmid  = 256*( f     - sdr->block_center_freq + sdr->sdr_sample_rate/2) * w / sdr->sdr_sample_rate;
             char text[20];
             int color = s->stream ? 64 : 32;
             int size = s->stream ? 181 : 128;
             int xd = size, yd = size;
+
+            if (!s->in_station_list)
+                continue;
+
             snprintf(text, sizeof(text), "%s %f Mhz",
                      modulation_descs[s->modulation].shortname,
                      f/1000000);
@@ -1928,8 +1931,10 @@ process_next_block:
             }
         } else {
             av_assert0(sdr->mode == AllStationMode);
-            for(int i = 0; i<sdr->nb_stations; i++) {
-                Station *station = sdr->station[i];
+            Station *station_list[1000];
+            int nb_stations = find_stations(sdr, sdr->block_center_freq, sdr->sdr_sample_rate*0.5, station_list, FF_ARRAY_ELEMS(station_list));
+            for(int i = 0; i<nb_stations; i++) {
+                Station *station = station_list[i];
                 if (!station->stream) {
                     /* audio stream */
                     AVStream *st = avformat_new_stream(s, NULL);
@@ -2052,8 +2057,6 @@ int avpriv_sdr_read_close(AVFormatContext *s)
         sst->frame_size = 0;
     }
 
-    sdr->nb_stations = 0;
-    av_freep(&sdr->station);
     av_tree_enumerate(sdr->station_root, NULL, NULL, free_station_enu);
     av_tree_destroy(sdr->station_root);
     sdr->station_root = NULL;
-- 
2.31.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] 10+ messages in thread

* [FFmpeg-devel] [PATCH 5/9] avradio/sdr: Allow user to adjust FM/AM thresholds
  2023-07-07 17:22 [FFmpeg-devel] [PATCH 1/9] avradio/sdr: use AVTree for candidate stations Michael Niedermayer
                   ` (2 preceding siblings ...)
  2023-07-07 17:22 ` [FFmpeg-devel] [PATCH 4/9] avradio/sdr: Eliminate station list Michael Niedermayer
@ 2023-07-07 17:22 ` Michael Niedermayer
  2023-07-07 17:22 ` [FFmpeg-devel] [PATCH 6/9] avradio/sdrdemux: count timeout irrespective of a station being active Michael Niedermayer
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Michael Niedermayer @ 2023-07-07 17:22 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 doc/demuxers.texi     |  9 +++++++++
 libavradio/sdr.h      |  3 +++
 libavradio/sdrdemux.c | 18 +++++++-----------
 3 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/doc/demuxers.texi b/doc/demuxers.texi
index fc2ab9fc27..81c46ce08f 100644
--- a/doc/demuxers.texi
+++ b/doc/demuxers.texi
@@ -956,6 +956,15 @@ for debuging.
 @item kbd_alpha
 Kaiser Bessel derived window parameter
 
+@item am_threshold
+AM station detection threshold
+
+@item fm_threshold
+FM station detection threshold, a lower value allows detection of weaker stations but
+it can also result in the detection of SDR artifacts such as reflections of strong FM stations
+as weak stations. Future versions may be better able to separate weak stations from artifacts
+looking like weak stations.
+
 @item am_mode
 AM Demodulation method. Several different methods are supported.
 @table @samp
diff --git a/libavradio/sdr.h b/libavradio/sdr.h
index 00066850f9..838fb1cef7 100644
--- a/libavradio/sdr.h
+++ b/libavradio/sdr.h
@@ -152,6 +152,9 @@ typedef struct SDRContext {
     int emphasis_mode;
     int am_fft_ref;
 
+    float am_threshold;
+    float fm_threshold;
+
     pthread_t hw_thread;
     int thread_started;
     pthread_mutex_t mutex;                  ///< Mutex to protect common variable between mainthread and hw_thread, and also to protect soapy from concurrent calls
diff --git a/libavradio/sdrdemux.c b/libavradio/sdrdemux.c
index 769cf3ade6..ad3e253ced 100644
--- a/libavradio/sdrdemux.c
+++ b/libavradio/sdrdemux.c
@@ -64,17 +64,9 @@
 #define STATION_TIMEOUT 100 ///< The number of frames after which a station is removed if it was not detected
 #define CANDIDATE_STATION_TIMEOUT 4
 
-/*
- * 100 detects nothing
- * 50 detects a good bit but not all
- */
-#define AM_THRESHOLD 20
-
 #define AM_MAX23 0.06     //smaller causes failure on synthetic signals
 #define AM_MAX4  0.02
 
-#define FM_THRESHOLD  50 //TODO adjust
-
 //Least squares fit at 1khz points of frequency response shown by Frank McClatchie, FM SYSTEMS, INC. 800-235-6960
 static double emphasis75us(int f)
 {
@@ -529,7 +521,7 @@ static int probe_am(SDRContext *sdr)
             continue;
 
         //TODO also check for symmetry in the spectrum
-        if (mid > 0 && score > AM_THRESHOLD &&
+        if (mid > 0 && score > sdr->am_threshold &&
             sdr->len2block[i - 1] <  mid          && sdr->len2block[i + 1] <= mid &&
             sdr->len2block[i - 2] <  mid*AM_MAX23 && sdr->len2block[i + 2] <  mid*AM_MAX23 &&
             sdr->len2block[i - 3] <  mid*AM_MAX23 && sdr->len2block[i + 3] <  mid*AM_MAX23
@@ -583,7 +575,7 @@ static double find_am_carrier(SDRContext *sdr, const AVComplexFloat *data, int d
         avg += len2block[i + index];
     score = len * mid / (avg - mid);
     //find optimal frequency for this block if we have a carrier
-    if (score > AM_THRESHOLD / 4) {
+    if (score > sdr->am_threshold / 4) {
         double peak_i = find_peak_macleod(sdr, data, i_max, data_len, NULL);
         if (peak_i < 0)
             return peak_i;
@@ -858,7 +850,7 @@ static int probe_fm(SDRContext *sdr)
 
                 if (last_score[1] >= last_score[0] &&
                     last_score[1] > last_score[2] &&
-                    last_score[1] > FM_THRESHOLD) {
+                    last_score[1] > sdr->fm_threshold) {
 
                     float rmax   = max_in_range(sdr, i-half_bw_i/4, i+half_bw_i/4);
                     int lowcount = countbelow(sdr, i-half_bw_i/4, i+half_bw_i/4, rmax / 100);
@@ -2122,6 +2114,10 @@ const AVOption avpriv_sdr_options[] = {
         { "emphasis75us", "FM De-Emphasis 75us", 0, AV_OPT_TYPE_CONST, {.i64 = EMPHASIS_75us}, 0, 0, DEC, "fm_emphasis"},
         { "emphasis50us", "FM De-Emphasis 50us", 0, AV_OPT_TYPE_CONST, {.i64 = EMPHASIS_50us}, 0, 0, DEC, "fm_emphasis"},
         { "none"        , "No FM De-Emphasis"  , 0, AV_OPT_TYPE_CONST, {.i64 = EMPHASIS_NONE}, 0, 0, DEC, "fm_emphasis"},
+
+    { "am_threshold"     , "AM detection threshold", OFFSET(am_threshold), AV_OPT_TYPE_FLOAT, {.dbl = 20}, 0, FLT_MAX, DEC},
+    { "fm_threshold"     , "FM detection threshold", OFFSET(fm_threshold), AV_OPT_TYPE_FLOAT, {.dbl = 50}, 0, FLT_MAX, DEC},
+
     { NULL },
 };
 
-- 
2.31.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] 10+ messages in thread

* [FFmpeg-devel] [PATCH 6/9] avradio/sdrdemux: count timeout irrespective of a station being active
  2023-07-07 17:22 [FFmpeg-devel] [PATCH 1/9] avradio/sdr: use AVTree for candidate stations Michael Niedermayer
                   ` (3 preceding siblings ...)
  2023-07-07 17:22 ` [FFmpeg-devel] [PATCH 5/9] avradio/sdr: Allow user to adjust FM/AM thresholds Michael Niedermayer
@ 2023-07-07 17:22 ` Michael Niedermayer
  2023-07-07 17:22 ` [FFmpeg-devel] [PATCH 7/9] avradio/sdr: Compute and use detection histogram Michael Niedermayer
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Michael Niedermayer @ 2023-07-07 17:22 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavradio/sdrdemux.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/libavradio/sdrdemux.c b/libavradio/sdrdemux.c
index ad3e253ced..6cb9d3b70a 100644
--- a/libavradio/sdrdemux.c
+++ b/libavradio/sdrdemux.c
@@ -327,12 +327,11 @@ static void decay_stations(SDRContext *sdr)
         if (station->frequency - station->bandwidth/2 < sdr->block_center_freq - sdr->bandwidth/2 ||
             station->frequency + station->bandwidth/2 > sdr->block_center_freq + sdr->bandwidth/2)
             continue;
-        if (station->stream)
-            continue;
 
         if (station->in_station_list) {
             if (station->timeout++ > STATION_TIMEOUT) {
-                station->in_station_list = 0;
+                if (!station->stream)
+                    station->in_station_list = 0;
             }
         } else {
             if (station->timeout++ > CANDIDATE_STATION_TIMEOUT) {
-- 
2.31.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] 10+ messages in thread

* [FFmpeg-devel] [PATCH 7/9] avradio/sdr: Compute and use detection histogram
  2023-07-07 17:22 [FFmpeg-devel] [PATCH 1/9] avradio/sdr: use AVTree for candidate stations Michael Niedermayer
                   ` (4 preceding siblings ...)
  2023-07-07 17:22 ` [FFmpeg-devel] [PATCH 6/9] avradio/sdrdemux: count timeout irrespective of a station being active Michael Niedermayer
@ 2023-07-07 17:22 ` Michael Niedermayer
  2023-07-07 17:22 ` [FFmpeg-devel] [PATCH 8/9] avradio/sdrdemux: increase the FM station frequency tolerance Michael Niedermayer
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Michael Niedermayer @ 2023-07-07 17:22 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

By analyzing the behavior of the detectability of stations with
different SDR settings we can separate some SDR artifacts from
weak stations.

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavradio/sdr.h      |  5 +++++
 libavradio/sdrdemux.c | 36 +++++++++++++++++++++++++++++++++++-
 2 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/libavradio/sdr.h b/libavradio/sdr.h
index 838fb1cef7..1053e45efe 100644
--- a/libavradio/sdr.h
+++ b/libavradio/sdr.h
@@ -66,6 +66,8 @@ typedef enum Modulation {
     //QAM, PSK, ...
 } Modulation;
 
+#define HISTOGRAMM_SIZE 9
+
 typedef struct Station {
     char *name;
     enum Modulation modulation;
@@ -78,6 +80,9 @@ typedef struct Station {
     int timeout;            //since how many blocks was this detectable but not detected
     int multiplex_index;    //DAB can have multiple stations on one frequency
 
+    int detection_per_mix_frequency[HISTOGRAMM_SIZE];
+    int non_detection_per_mix_frequency[HISTOGRAMM_SIZE];
+
     struct SDRStream *stream;
 } Station;
 
diff --git a/libavradio/sdrdemux.c b/libavradio/sdrdemux.c
index 6cb9d3b70a..1fc528317c 100644
--- a/libavradio/sdrdemux.c
+++ b/libavradio/sdrdemux.c
@@ -111,6 +111,23 @@ static void free_station(Station *station)
     av_free(station);
 }
 
+static inline int histogram_index(SDRContext *sdr, double f)
+{
+    f = HISTOGRAMM_SIZE*((f - sdr->block_center_freq) / sdr->sdr_sample_rate + 0.5);
+    return av_clip((int)f, 0, HISTOGRAMM_SIZE-1);
+}
+
+static int histogram_score(Station *s)
+{
+    int score = 0;
+    for(int i = 0; i<HISTOGRAMM_SIZE; i++) {
+        score +=
+             (5*s->detection_per_mix_frequency[i] > s->non_detection_per_mix_frequency[i])
+            -(5*s->detection_per_mix_frequency[i] < s->non_detection_per_mix_frequency[i]);
+    }
+    return score;
+}
+
 typedef struct FindStationContext {
     double freq;
     double range;
@@ -184,6 +201,10 @@ static int create_station(SDRContext *sdr, Station *candidate_station) {
     if (candidate_station->in_station_list)
         return 0;
 
+    // suspect looking histogram
+    if (histogram_score(candidate_station) <= 0)
+        return 0;
+
     Station *station_list[1000];
     int nb_stations = find_stations(sdr, sdr->block_center_freq, sdr->sdr_sample_rate*0.5, station_list, FF_ARRAY_ELEMS(station_list));
     for (i=0; i<nb_stations; i++) {
@@ -328,8 +349,20 @@ static void decay_stations(SDRContext *sdr)
             station->frequency + station->bandwidth/2 > sdr->block_center_freq + sdr->bandwidth/2)
             continue;
 
+        if (station->timeout)
+            station->non_detection_per_mix_frequency[histogram_index(sdr, station->frequency)] ++;
+
         if (station->in_station_list) {
-            if (station->timeout++ > STATION_TIMEOUT) {
+            int station_timeout = STATION_TIMEOUT;
+            int hs = histogram_score(station);
+
+            if (hs == 0) {
+                station_timeout = 5; //give the station a moment to be properly detected and then discard it
+            } else if(hs < 0) {
+                station_timeout = 0; //probably not a station
+            }
+
+            if (station->timeout++ > station_timeout) {
                 if (!station->stream)
                     station->in_station_list = 0;
             }
@@ -376,6 +409,7 @@ static int create_candidate_station(SDRContext *sdr, enum Modulation modulation,
     }
     station->frequency /= ++station->nb_frequency;
 
+    station->detection_per_mix_frequency[histogram_index(sdr, freq)] ++;
     station->modulation   = modulation;
     station->bandwidth    = bandwidth;
     station->bandwidth_p2 = bandwidth_p2;
-- 
2.31.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] 10+ messages in thread

* [FFmpeg-devel] [PATCH 8/9] avradio/sdrdemux: increase the FM station frequency tolerance
  2023-07-07 17:22 [FFmpeg-devel] [PATCH 1/9] avradio/sdr: use AVTree for candidate stations Michael Niedermayer
                   ` (5 preceding siblings ...)
  2023-07-07 17:22 ` [FFmpeg-devel] [PATCH 7/9] avradio/sdr: Compute and use detection histogram Michael Niedermayer
@ 2023-07-07 17:22 ` Michael Niedermayer
  2023-07-07 17:22 ` [FFmpeg-devel] [PATCH 9/9] avradio/sdrdemux: Use 2 differnt FM station detectors Michael Niedermayer
  2023-07-08 15:42 ` [FFmpeg-devel] [PATCH 1/9] avradio/sdr: use AVTree for candidate stations Michael Niedermayer
  8 siblings, 0 replies; 10+ messages in thread
From: Michael Niedermayer @ 2023-07-07 17:22 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

The current FM station probing code is quite inaccurate with the
detected frequency.
Doing better requires some extra computations, maybe this is good
enough

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavradio/sdrdemux.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libavradio/sdrdemux.c b/libavradio/sdrdemux.c
index 1fc528317c..4c1ec7b51c 100644
--- a/libavradio/sdrdemux.c
+++ b/libavradio/sdrdemux.c
@@ -248,7 +248,7 @@ static int create_station(SDRContext *sdr, Station *candidate_station) {
 
     nb_candidate_match += candidate_station->nb_frequency - 1;
     for (i=0; i<nb_stations; i++) {
-        int freq_precission = modulation == AM ? 5 : 50;
+        int freq_precission = modulation == AM ? 5 : 500;
         Station *s = station_list[i];
         double delta = fabs(s->frequency - freq);
 
@@ -383,7 +383,7 @@ static int create_candidate_station(SDRContext *sdr, enum Modulation modulation,
     void *tmp;
     struct AVTreeNode *next = NULL;
     Station *station_list[1000];
-    double snapdistance = modulation == AM ? 5 : 50;
+    double snapdistance = modulation == AM ? 5 : 500;
     int nb_stations = find_stations(sdr, freq, snapdistance, station_list, FF_ARRAY_ELEMS(station_list));
 
     if (nb_stations) {
-- 
2.31.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] 10+ messages in thread

* [FFmpeg-devel] [PATCH 9/9] avradio/sdrdemux: Use 2 differnt FM station detectors
  2023-07-07 17:22 [FFmpeg-devel] [PATCH 1/9] avradio/sdr: use AVTree for candidate stations Michael Niedermayer
                   ` (6 preceding siblings ...)
  2023-07-07 17:22 ` [FFmpeg-devel] [PATCH 8/9] avradio/sdrdemux: increase the FM station frequency tolerance Michael Niedermayer
@ 2023-07-07 17:22 ` Michael Niedermayer
  2023-07-08 15:42 ` [FFmpeg-devel] [PATCH 1/9] avradio/sdr: use AVTree for candidate stations Michael Niedermayer
  8 siblings, 0 replies; 10+ messages in thread
From: Michael Niedermayer @ 2023-07-07 17:22 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

Detect station if both lie within 1kHz of each other
This eliminates some artifacts being detected

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavradio/sdrdemux.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/libavradio/sdrdemux.c b/libavradio/sdrdemux.c
index 4c1ec7b51c..bed74ebd26 100644
--- a/libavradio/sdrdemux.c
+++ b/libavradio/sdrdemux.c
@@ -855,13 +855,17 @@ static int probe_fm(SDRContext *sdr)
 
     for (int pass = 0; pass < 2; pass ++) {
         double avg[2] = {0}, tri = 0;
+        double mean = 0;
+        double center = 0;
         for (i = 0; i<half_bw_i; i++) {
             avg[0] += sdr->len2block[i];
             tri    += i*sdr->len2block[i];
         }
+        mean = tri;
         for (; i<2*half_bw_i; i++) {
             avg[1] += sdr->len2block[i];
             tri    += (2*half_bw_i-i)*sdr->len2block[i];
+            mean   += i*sdr->len2block[i];
         }
 
         for(i = half_bw_i; i<2*sdr->block_size - half_bw_i; i++) {
@@ -871,6 +875,10 @@ static int probe_fm(SDRContext *sdr)
             b += avg[1];
             tri += avg[1] - avg[0];
 
+            mean += (i+half_bw_i)*sdr->len2block[i+half_bw_i];
+            center = mean / b;
+            mean -= (i-half_bw_i)*sdr->len2block[i-half_bw_i];
+
             if (i < border_i || i > 2*sdr->block_size - border_i)
                 continue;
 
@@ -884,6 +892,7 @@ static int probe_fm(SDRContext *sdr)
                 if (last_score[1] >= last_score[0] &&
                     last_score[1] > last_score[2] &&
                     last_score[1] > sdr->fm_threshold) {
+                    double score = last_score[1];
 
                     float rmax   = max_in_range(sdr, i-half_bw_i/4, i+half_bw_i/4);
                     int lowcount = countbelow(sdr, i-half_bw_i/4, i+half_bw_i/4, rmax / 100);
@@ -898,7 +907,12 @@ static int probe_fm(SDRContext *sdr)
                     if (peak_i < 0)
                         continue;
                     av_assert0(fabs(peak_i-i) < 2);
-                    create_candidate_station(sdr, FM, peak_i * 0.5 * sdr->sdr_sample_rate / sdr->block_size + sdr->block_center_freq - sdr->sdr_sample_rate/2, bandwidth_f, bandwidth_p2, last_score[1]);
+                    double f = peak_i * 0.5 * sdr->sdr_sample_rate / sdr->block_size + sdr->block_center_freq - sdr->sdr_sample_rate/2;
+                    double f2 = center * 0.5 * sdr->sdr_sample_rate / sdr->block_size + sdr->block_center_freq - sdr->sdr_sample_rate/2;
+
+                    if (fabs(f2 - f) > 1000)
+                        continue;
+                    create_candidate_station(sdr, FM, f2, bandwidth_f, bandwidth_p2, score);
                 }
             }
         }
-- 
2.31.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] 10+ messages in thread

* Re: [FFmpeg-devel] [PATCH 1/9] avradio/sdr: use AVTree for candidate stations
  2023-07-07 17:22 [FFmpeg-devel] [PATCH 1/9] avradio/sdr: use AVTree for candidate stations Michael Niedermayer
                   ` (7 preceding siblings ...)
  2023-07-07 17:22 ` [FFmpeg-devel] [PATCH 9/9] avradio/sdrdemux: Use 2 differnt FM station detectors Michael Niedermayer
@ 2023-07-08 15:42 ` Michael Niedermayer
  8 siblings, 0 replies; 10+ messages in thread
From: Michael Niedermayer @ 2023-07-08 15:42 UTC (permalink / raw)
  To: FFmpeg development discussions and patches


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

On Fri, Jul 07, 2023 at 07:22:16PM +0200, Michael Niedermayer wrote:
> This is needed so we can keep more information about stations without
> performance issues
> 
> Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
> ---
>  libavradio/sdr.h      |   4 +-
>  libavradio/sdrdemux.c | 126 ++++++++++++++++++++++++++++++++++++------
>  2 files changed, 110 insertions(+), 20 deletions(-)

will apply patchset

[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

The real ebay dictionary, page 1
"Used only once"    - "Some unspecified defect prevented a second use"
"In good condition" - "Can be repaird by experienced expert"
"As is" - "You wouldnt want it even if you were payed for it, if you knew ..."

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

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

end of thread, other threads:[~2023-07-08 15:42 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-07 17:22 [FFmpeg-devel] [PATCH 1/9] avradio/sdr: use AVTree for candidate stations Michael Niedermayer
2023-07-07 17:22 ` [FFmpeg-devel] [PATCH 2/9] avradio/sdr: Consolidate candidate station entries Michael Niedermayer
2023-07-07 17:22 ` [FFmpeg-devel] [PATCH 3/9] avradio/sdr: consolidate the candidate station list with the main list Michael Niedermayer
2023-07-07 17:22 ` [FFmpeg-devel] [PATCH 4/9] avradio/sdr: Eliminate station list Michael Niedermayer
2023-07-07 17:22 ` [FFmpeg-devel] [PATCH 5/9] avradio/sdr: Allow user to adjust FM/AM thresholds Michael Niedermayer
2023-07-07 17:22 ` [FFmpeg-devel] [PATCH 6/9] avradio/sdrdemux: count timeout irrespective of a station being active Michael Niedermayer
2023-07-07 17:22 ` [FFmpeg-devel] [PATCH 7/9] avradio/sdr: Compute and use detection histogram Michael Niedermayer
2023-07-07 17:22 ` [FFmpeg-devel] [PATCH 8/9] avradio/sdrdemux: increase the FM station frequency tolerance Michael Niedermayer
2023-07-07 17:22 ` [FFmpeg-devel] [PATCH 9/9] avradio/sdrdemux: Use 2 differnt FM station detectors Michael Niedermayer
2023-07-08 15:42 ` [FFmpeg-devel] [PATCH 1/9] avradio/sdr: use AVTree for candidate stations Michael Niedermayer

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