[FFmpeg-devel] [PATCH 2/7] avradio/sdr: compensate for RTLSDR frequency limitations

Michael Niedermayer michael at niedermayer.cc
Mon Jul 17 03:26:59 EEST 2023


Signed-off-by: Michael Niedermayer <michael at niedermayer.cc>
---
 libavradio/sdr.h        |  2 +-
 libavradio/sdrdemux.c   |  9 ++++-----
 libavradio/sdrinradio.c | 10 ++++++++--
 3 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/libavradio/sdr.h b/libavradio/sdr.h
index ac6b7dffe0..dc20415457 100644
--- a/libavradio/sdr.h
+++ b/libavradio/sdr.h
@@ -210,7 +210,7 @@ typedef struct SDRContext {
     /**
      * Setup the hardware for the requested frequency
      */
-    int (*set_frequency_callback)(struct SDRContext *sdr, int64_t frequency);
+    int64_t (*set_frequency_callback)(struct SDRContext *sdr, int64_t frequency);
 
     /**
      * Read from the hardware, block if nothing available with a reasonable timeout
diff --git a/libavradio/sdrdemux.c b/libavradio/sdrdemux.c
index a545e7cb4c..5a3af23a74 100644
--- a/libavradio/sdrdemux.c
+++ b/libavradio/sdrdemux.c
@@ -1129,11 +1129,10 @@ ModulationDescriptor ff_sdr_modulation_descs[] = {
 int ff_sdr_set_freq(SDRContext *sdr, int64_t freq)
 {
     freq = av_clip64(freq, sdr->min_center_freq, sdr->max_center_freq);
-
     if (sdr->set_frequency_callback) {
-        int ret = sdr->set_frequency_callback(sdr, freq);
-        if (ret < 0)
-            return ret;
+        freq = sdr->set_frequency_callback(sdr, freq);
+        if (freq < 0)
+            return freq;
     }
 
     sdr->freq = freq;
@@ -1421,7 +1420,7 @@ static void *soapy_needs_bigger_buffers_worker(SDRContext *sdr)
         if (sdr->seek_direction && block_counter > 5) {
             sdr->wanted_freq = snap2band(sdr, sdr->wanted_freq, sdr->seek_direction*sdr->bandwidth*0.5);
         }
-        if (sdr->wanted_freq != sdr->freq) {
+        if (fabs(sdr->wanted_freq - sdr->freq) > 1500) {
             //We could use a seperate MUTEX for the FIFO and for soapy
             ff_sdr_set_freq(sdr, sdr->wanted_freq);
             //This shouldnt really cause any problem if we just continue on error except that we continue returning data with the previous target frequency range
diff --git a/libavradio/sdrinradio.c b/libavradio/sdrinradio.c
index 0e7442fddf..f078d27e7b 100644
--- a/libavradio/sdrinradio.c
+++ b/libavradio/sdrinradio.c
@@ -68,7 +68,7 @@ static int sdrindev_read_callback(SDRContext *sdr, FIFOElement *fifo_element, in
     return ret;
 }
 
-static int sdrindev_set_frequency_callback(SDRContext *sdr, int64_t freq)
+static int64_t sdrindev_set_frequency_callback(SDRContext *sdr, int64_t freq)
 {
     AVFormatContext *avfmt = sdr->avfmt;
     SoapySDRDevice *soapy = sdr->soapy;
@@ -83,6 +83,12 @@ static int sdrindev_set_frequency_callback(SDRContext *sdr, int64_t freq)
                 } else
                     sdr->current_direct_samp = value;
             }
+            //The R820T has a 16 bit fractional PLL which can do only multiplies of 439.45
+            //Its more complex but this approximation works
+            //It has to be noted that SOAPY does not tell us about this, instead saopy
+            //pretends whatever we ask for we get exactly, but we dont
+            //For more details see: michelebavaro.blogspot.com/2014/05/gnss-carrier-phase-rtlsdr-and.html
+            freq = lrint(freq / 439.45) * 439.45;
         }
 
         if (SoapySDRDevice_setFrequency(soapy, SOAPY_SDR_RX, 0, freq, NULL) != 0) {
@@ -90,7 +96,7 @@ static int sdrindev_set_frequency_callback(SDRContext *sdr, int64_t freq)
             return AVERROR_EXTERNAL;
         }
     }
-    return 0;
+    return freq;
 }
 
 static void print_and_free_list(AVFormatContext *s, char** names, size_t length, const char *title)
-- 
2.31.1



More information about the ffmpeg-devel mailing list