From patchwork Tue Jul 18 21:45:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Niedermayer X-Patchwork-Id: 42819 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:b813:b0:130:ccc6:6c4b with SMTP id fi19csp781911pzb; Tue, 18 Jul 2023 14:47:17 -0700 (PDT) X-Google-Smtp-Source: APBJJlHZ/EHXygliDtekQ+Vm1glktOZn/BbBtpECetdGNfWKneAtqMipjUUe2ZWuQpQO0W3bH6AE X-Received: by 2002:a05:6512:282b:b0:4f9:607a:6508 with SMTP id cf43-20020a056512282b00b004f9607a6508mr547126lfb.50.1689716837551; Tue, 18 Jul 2023 14:47:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689716837; cv=none; d=google.com; s=arc-20160816; b=Z/4OiuUE9XsRBIfKRwwa5ig8GWWibM4E1txuT9hW+uOZWDKGBwfvT5dFPtWr3aV1Cu mSFQgAv516WQioa1CsxAhImK/giT6yebKOfua2qBtdAF5XwLDtQ7GIXd6hR3vupaYgBt wrCTqJfajItuEd+OmaUVwa04v7rLkZLz9/zHVfJNpwbu0NIYAv1v/eqDz9kCeQwFBrf8 L4vTFL0DvbwCaPm0JDxBNw6oHIgapWUNLcYJnwTrU90ggHqk5bobaoDRKk8fobwkaWUJ E09FGKEJLtwnOv9ObTPlGwImET3Y3LKXpTVzJ72NammmpeN0dIraR33L2wrHEUqqvT73 1M8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=BawymE77p4ghfw4f8mGueMo0uDsYBUXV4sxlhrOYt8o=; fh=YYwLYmpaV0Fpw/rxmSKNRLS2XzDkAlGbHATiKOPtZrY=; b=bCFpHNzOZd/KnHF81SrslGyQxh9xjtREmWgrjF5ARjD2dRY6ftgub3RRnBmjxeBHYc PB+z2ykkDzaQYbe02FR+gXtnHqK28mIdAGz/fDss/RpKnufFsCiVe1MUIalFNE6VEoY2 lWODszi87nUARdpQst4GyccM8BnV8vchD6Ouy15yLq5hbpjxI5RYmxgk4EwNpCpZbcYe yJo5X09DVfXkzQxe7iN1yfgknbUYZevZmio6wLeCcAAH+L0cEw0zEyKAoxtT/brDbtxL b6YOsP4P1GN3YnUqgCwe63BBOoKRl8gDIRseisVIRJ4iKE4/1FGsRouaYkcCuVAmDgt9 kNgw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id k21-20020a05640212d500b0051e05151467si1768755edx.224.2023.07.18.14.47.17; Tue, 18 Jul 2023 14:47:17 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1E8FB68C5F6; Wed, 19 Jul 2023 00:46:09 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from relay2-d.mail.gandi.net (relay2-d.mail.gandi.net [217.70.183.194]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D1E0C68C5DB for ; Wed, 19 Jul 2023 00:46:05 +0300 (EEST) Received: by mail.gandi.net (Postfix) with ESMTPSA id 337B240007 for ; Tue, 18 Jul 2023 21:46:05 +0000 (UTC) From: Michael Niedermayer To: FFmpeg development discussions and patches Date: Tue, 18 Jul 2023 23:45:39 +0200 Message-Id: <20230718214542.685375-11-michael@niedermayer.cc> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230718214542.685375-1-michael@niedermayer.cc> References: <20230718214542.685375-1-michael@niedermayer.cc> MIME-Version: 1.0 X-GND-Sasl: michael@niedermayer.cc Subject: [FFmpeg-devel] [PATCH 11/14] avradio/sdr: More atomics, less Mutexes X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: ej1jRrdWKTYp Maybe this is cleaner Signed-off-by: Michael Niedermayer --- 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"},