From: Michael Niedermayer <michael@niedermayer.cc>
To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Subject: [FFmpeg-devel] [PATCH 11/14] avradio/sdr: More atomics, less Mutexes
Date: Tue, 18 Jul 2023 23:45:39 +0200
Message-ID: <20230718214542.685375-11-michael@niedermayer.cc> (raw)
In-Reply-To: <20230718214542.685375-1-michael@niedermayer.cc>
Maybe this is cleaner
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
libavradio/sdr.h | 5 +++--
libavradio/sdrdemux.c | 38 +++++++++++++++++++++++---------------
2 files changed, 26 insertions(+), 17 deletions(-)
diff --git a/libavradio/sdr.h b/libavradio/sdr.h
index 27ec1db4f3..3f76aec2a6 100644
--- a/libavradio/sdr.h
+++ b/libavradio/sdr.h
@@ -172,6 +172,7 @@ typedef struct SDRContext {
AVComplexFloat *windowed_block;
int64_t block_center_freq; ///< center frequency the current block contains
int64_t station_freq;
+ int64_t user_wanted_freq;
int sample_size;
double sample_scale;
@@ -218,8 +219,8 @@ typedef struct SDRContext {
AVFifo *empty_block_fifo;
AVFifo *full_block_fifo;
atomic_int close_requested;
- int64_t wanted_freq; ///< center frequency we want the hw to provide next
- int seek_direction; ///< if a seek is requested this is -1 or 1 otherwise 0
+ atomic_int_least64_t wanted_freq; ///< center frequency we want the hw to provide next, only written to by main thread
+ atomic_int seek_direction; ///< if a seek is requested this is -1 or 1 otherwise 0, only written to by main thread
int skip_probe;
/**
diff --git a/libavradio/sdrdemux.c b/libavradio/sdrdemux.c
index d5ea2d85b4..a9ddf93733 100644
--- a/libavradio/sdrdemux.c
+++ b/libavradio/sdrdemux.c
@@ -1351,14 +1351,12 @@ static int snap2station(SDRContext *sdr, int *seek_direction) {
return ret;
}
- pthread_mutex_lock(&sdr->mutex);
- *seek_direction =
- sdr->seek_direction = 0;
- sdr->wanted_freq = wanted_freq;
+ *seek_direction = 0;
+ atomic_store(&sdr->seek_direction, 0);
+ atomic_store(&sdr->wanted_freq, wanted_freq);
//200*1000 had artifacts
- av_log(avfmt, AV_LOG_DEBUG, "request f = %"PRId64"\n", sdr->wanted_freq);
- pthread_mutex_unlock(&sdr->mutex);
+ av_log(avfmt, AV_LOG_DEBUG, "request f = %"PRId64"\n", atomic_load(&sdr->wanted_freq));
return 1;
}
@@ -1426,6 +1424,8 @@ static void *soapy_needs_bigger_buffers_worker(SDRContext *sdr)
{
AVFormatContext *avfmt = sdr->avfmt;
unsigned block_counter = 0;
+ int64_t local_wanted_freq = 0;
+ int64_t last_wanted_freq = 0;
sdr->remaining_file_block_size = 0;
@@ -1436,6 +1436,8 @@ static void *soapy_needs_bigger_buffers_worker(SDRContext *sdr)
int remaining, ret;
int empty_blocks, full_blocks;
float wanted_gain = atomic_load(&sdr->wanted_gain) / 65536.0;
+ int64_t wanted_freq = atomic_load(&sdr->wanted_freq);
+ int seek_direction = atomic_load(&sdr->seek_direction);
//i wish av_fifo was thread safe
pthread_mutex_lock(&sdr->mutex);
@@ -1457,16 +1459,22 @@ static void *soapy_needs_bigger_buffers_worker(SDRContext *sdr)
block_counter ++;
pthread_mutex_lock(&sdr->mutex);
+ // Has the main thread changed the wanted frequency ? if so lets reset our loop to it
+ if (wanted_freq != last_wanted_freq) {
+ local_wanted_freq =
+ last_wanted_freq = wanted_freq;
+ }
+
// we try to get 2 clean blocks after windowing, to improve chances scanning doesnt miss too much
// First block after parameter change is not reliable, we do not assign it any frequency
// 2 blocks are needed with windowing to get a clean FFT output
// Thus > 3 is the minimum for the next frequency update if we want to do something reliable with the data
- if (sdr->seek_direction && block_counter > 5) {
- sdr->wanted_freq = snap2band(sdr, sdr->wanted_freq, sdr->seek_direction*sdr->bandwidth*0.5);
+ if (seek_direction && block_counter > 5) {
+ local_wanted_freq = snap2band(sdr, local_wanted_freq, seek_direction*sdr->bandwidth*0.5);
}
- if (fabs(sdr->wanted_freq - sdr->freq) > 1500) {
+ if (fabs(local_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);
+ ff_sdr_set_freq(sdr, local_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
//And theres not much else we can do, an error message was already printed by ff_sdr_set_freq() in that case
block_counter = 0; // we just changed the frequency, do not trust the next blocks content
@@ -1662,6 +1670,8 @@ int ff_sdr_common_init(AVFormatContext *s)
av_fifo_auto_grow_limit(sdr-> full_block_fifo, sdr->sdr_sample_rate / sdr->block_size);
atomic_init(&sdr->close_requested, 0);
+ atomic_init(&sdr->seek_direction, 0);
+ atomic_init(&sdr->wanted_freq, sdr->user_wanted_freq);
atomic_init(&sdr->wanted_gain, lrint((sdr->min_gain + sdr->max_gain) * 65536 / 2));
ret = pthread_mutex_init(&sdr->mutex, NULL);
if (ret) {
@@ -1810,8 +1820,8 @@ process_next_block:
ret = av_fifo_peek(sdr->full_block_fifo, &fifo_element, 2, 0);
if (ret >= 0)
av_fifo_drain2(sdr->full_block_fifo, 1);
- seek_direction = sdr->seek_direction; //This doesnt need a mutex here at all but tools might complain
pthread_mutex_unlock(&sdr->mutex);
+ seek_direction = atomic_load(&sdr->seek_direction);
if (ret < 0) {
av_log(s, AV_LOG_DEBUG, "EAGAIN on not enough data\n");
@@ -2094,9 +2104,7 @@ int ff_sdr_read_seek(AVFormatContext *s, int stream_index,
return ret;
//snap2station found no station lets command the thread to seek
if (!ret) {
- pthread_mutex_lock(&sdr->mutex);
- sdr->seek_direction = dir;
- pthread_mutex_unlock(&sdr->mutex);
+ atomic_store(&sdr->seek_direction, dir);
flush_fifo(sdr, sdr->full_block_fifo);
}
@@ -2207,7 +2215,7 @@ const AVOption ff_sdr_options[] = {
{ "rtlsdr_fixes" , "workaround rtlsdr issues", OFFSET(rtlsdr_fixes), AV_OPT_TYPE_INT , {.i64 = -1}, -1, 1, DEC},
{ "sdrplay_fixes" , "workaround sdrplay issues", OFFSET(sdrplay_fixes), AV_OPT_TYPE_INT , {.i64 = -1}, -1, 1, DEC},
{ "sdr_sr" , "sdr sample rate" , OFFSET(sdr_sample_rate ), AV_OPT_TYPE_INT , {.i64 = 0}, 0, INT_MAX, DEC},
- { "sdr_freq", "sdr frequency" , OFFSET(wanted_freq), AV_OPT_TYPE_INT64 , {.i64 = 9000000}, 0, INT64_MAX, DEC},
+ { "sdr_freq", "sdr frequency" , OFFSET(user_wanted_freq), AV_OPT_TYPE_INT64 , {.i64 = 9000000}, 0, INT64_MAX, DEC},
{ "gain" , "sdr overall gain", OFFSET(sdr_gain), AV_OPT_TYPE_INT , {.i64 = GAIN_SDR_AGC}, -3, INT_MAX, DEC, "gain"},
{ "sdr_agc", "SDR AGC (if supported)", 0, AV_OPT_TYPE_CONST, {.i64 = GAIN_SDR_AGC}, 0, 0, DEC, "gain"},
{ "sw_agc", "Software AGC", 0, AV_OPT_TYPE_CONST, {.i64 = GAIN_SW_AGC}, 0, 0, DEC, "gain"},
--
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".
next prev parent reply other threads:[~2023-07-18 21:47 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-18 21:45 [FFmpeg-devel] [PATCH 01/14] avradio/sdrdemux: Add Mittelwelle / Mediumwave / Mediumfrequency band Michael Niedermayer
2023-07-18 21:45 ` [FFmpeg-devel] [PATCH 02/14] avradio/sdrdemux: Fix corner case in snap2band Michael Niedermayer
2023-07-18 21:45 ` [FFmpeg-devel] [PATCH 03/14] avradio/sdrdemux: Fix seeking to stations at the edge of the range Michael Niedermayer
2023-07-18 21:45 ` [FFmpeg-devel] [PATCH 04/14] avradio/sdrdemux: snap2station() should look only at the vissible window Michael Niedermayer
2023-07-18 21:45 ` [FFmpeg-devel] [PATCH 05/14] avradio/sdr: snap2station() documentation Michael Niedermayer
2023-07-18 21:45 ` [FFmpeg-devel] [PATCH 06/14] avradio/sdrdemux: do not hack bandwidth value in random places Michael Niedermayer
2023-07-18 21:45 ` [FFmpeg-devel] [PATCH 07/14] avradio/sdrdemux: snap2station consider more distant stations Michael Niedermayer
2023-07-18 21:45 ` [FFmpeg-devel] [PATCH 08/14] tests: Add avradio/sdrdemux tests Michael Niedermayer
2023-07-21 18:42 ` Michael Niedermayer
2023-07-21 23:51 ` Michael Niedermayer
2023-07-18 21:45 ` [FFmpeg-devel] [PATCH 09/14] avradio/sdr: Remove code setting frequency from variable being 0 Michael Niedermayer
2023-07-18 21:45 ` [FFmpeg-devel] [PATCH 10/14] avradio/sdrdemux: no need to set wanted_freq from sdrfile_initial_setup() Michael Niedermayer
2023-07-18 21:45 ` Michael Niedermayer [this message]
2023-07-18 21:45 ` [FFmpeg-devel] [PATCH 12/14] avradio/sdrdemux: + vs. - bug Michael Niedermayer
2023-07-18 21:45 ` [FFmpeg-devel] [PATCH 13/14] avradio/sdrdemux: snap2band doesnt change anything so use const Michael Niedermayer
2023-07-18 21:45 ` [FFmpeg-devel] [PATCH 14/14] avradio/sdrdemux: Fix wraparound with seeking Michael Niedermayer
2023-07-22 13:30 ` [FFmpeg-devel] [PATCH 01/14] avradio/sdrdemux: Add Mittelwelle / Mediumwave / Mediumfrequency band Michael Niedermayer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230718214542.685375-11-michael@niedermayer.cc \
--to=michael@niedermayer.cc \
--cc=ffmpeg-devel@ffmpeg.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
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