From patchwork Mon Jul 17 00:26:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Niedermayer X-Patchwork-Id: 42773 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:6da1:b0:131:a7d0:bc6d with SMTP id gl33csp5078472pzb; Sun, 16 Jul 2023 17:27:18 -0700 (PDT) X-Google-Smtp-Source: APBJJlEiV+F1d5/Ngiz79UDwVU55D5V46mnb+PvCj+SJCMwTDJE85kwuPlQKNbnnCAGcoAOqYoVl X-Received: by 2002:a05:6512:1183:b0:4f9:5c04:af07 with SMTP id g3-20020a056512118300b004f95c04af07mr5650820lfr.26.1689553638021; Sun, 16 Jul 2023 17:27:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689553637; cv=none; d=google.com; s=arc-20160816; b=Iv/yecM/3MBmDsbkruXxLyceBRhSx5yEpBSbHQxD5/otFsSr8bWyjmvMWj1LhdiW0Q U7NHgm7rNULKa+nCRf6AzpaEZi9jSLf+BnznyMHpTjzT95R7pM13C1io8J35fyidDUrX DgRmh0gjS3zbbK1V9e5ZDIrvfkpDVCBTua4Nm3NFA820QE4l+cN2bowUkhW9S0HHqbMQ tvOoBG9rjJJ9zGEFvLklRsyYw973+vG+XpgmcOS+enN6r6D/VSkTYPZ45KL+qGHhDbSr TZ6+gUiVYAwO3Ti/IjnsD1iDZIzlQgJCD6ccSRdYPTZIqjJTe0MI8B6a2wCFgNKMxdc6 FoeQ== 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:message-id:date:to:from :delivered-to; bh=JKiLVHTmy3asdc/XWQwpqhjqsqqu04g8h4neOwEgdqo=; fh=YYwLYmpaV0Fpw/rxmSKNRLS2XzDkAlGbHATiKOPtZrY=; b=rcvBDC6LvEBCe7iCDDDPcp62U6O6COu8I+5e7M7LNe+qhEj55WrZHcSP9FbOZ/qbL2 ZIicD9L8C9IkX6GL7bAqNG7HZs85EbJH203IHajhm23M70c0gJqZbPpWSm0O37dqRJBc UAhk0RewJ4REJ/dLdX69MRU4RTTgIWtBj4XYvM3euO4FtH09Gnc8Q4clasXdV+bPQG04 RV0lKQ+2IZ9Www6lUaqemlNdMyzdgfT6ZpXz69ZQQVccJV+nwBiA9LLibItJamp4sdLF mJItSEILivpnT41Id6BcdGwK2XfsbgoK+dxYZckIYyET+xeFHY14AiB4vhk5ih/uqDqX rgCA== 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 y7-20020aa7c247000000b0051e04eb3321si13513877edo.519.2023.07.16.17.27.17; Sun, 16 Jul 2023 17:27: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 D618A68C4FC; Mon, 17 Jul 2023 03:27:12 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net [217.70.183.198]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D209468C200 for ; Mon, 17 Jul 2023 03:27:06 +0300 (EEST) Received: by mail.gandi.net (Postfix) with ESMTPSA id C91F4C0002 for ; Mon, 17 Jul 2023 00:27:05 +0000 (UTC) From: Michael Niedermayer To: FFmpeg development discussions and patches Date: Mon, 17 Jul 2023 02:26:58 +0200 Message-Id: <20230717002704.3092192-1-michael@niedermayer.cc> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-GND-Sasl: michael@niedermayer.cc Subject: [FFmpeg-devel] [PATCH 1/7] avradio/sdrdemux: icarrier just needs phase 2 block size 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: oQ8nktE+dm3o Signed-off-by: Michael Niedermayer --- libavradio/sdrdemux.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libavradio/sdrdemux.c b/libavradio/sdrdemux.c index 5a6a9b8894..a545e7cb4c 100644 --- a/libavradio/sdrdemux.c +++ b/libavradio/sdrdemux.c @@ -1543,7 +1543,7 @@ int ff_sdr_common_init(AVFormatContext *s) sdr->fm_iside = av_malloc(sizeof(*sdr->fm_iside) * 2 * sdr->fm_block_size_p2); sdr->fm_block = av_malloc(sizeof(*sdr->fm_block) * 2 * sdr->fm_block_size); sdr->fm_iblock = av_malloc(sizeof(*sdr->fm_iblock) * 2 * sdr->fm_block_size); - sdr->fm_icarrier = av_malloc(sizeof(*sdr->fm_icarrier) * 2 * sdr->fm_block_size); + sdr->fm_icarrier = av_malloc(sizeof(*sdr->fm_icarrier) * 2 * sdr->fm_block_size_p2); sdr->fm_window = av_malloc(sizeof(*sdr->fm_window) * 2 * sdr->fm_block_size); if (!sdr->windowed_block || !sdr->len2block || !sdr->block || !sdr->window || !sdr->fm_window_p2 || !sdr->fm_iside || From patchwork Mon Jul 17 00:26:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Niedermayer X-Patchwork-Id: 42775 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:6da1:b0:131:a7d0:bc6d with SMTP id gl33csp5078522pzb; Sun, 16 Jul 2023 17:27:29 -0700 (PDT) X-Google-Smtp-Source: APBJJlHmmAJeUETCVmSASOZDiE+Jtxw6DknV6/ikyrT6PxdS4e3QFR5iayWsx8ES40ZgvLDigDWy X-Received: by 2002:a17:906:73d9:b0:997:b843:7cb2 with SMTP id n25-20020a17090673d900b00997b8437cb2mr3250253ejl.60.1689553649526; Sun, 16 Jul 2023 17:27:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689553649; cv=none; d=google.com; s=arc-20160816; b=0c8VB/VQPZi78HZ9eEqMdyb4vfnrLIKSqG3LYunWwvpG55KzXsNN0AzArYx6ay+zSX WAe3ZRWviYpeRcXxqZ/7sc0OzpPTfISMgdvp3ISs8ZsIS6AnJ4UPeTmPNCeHbpcYlmYW bjt31ILdG1O0LidQTLcZpynEWL5u/RtQtskfeuh5TahYvxolXbe3P+ya6ueTK/IQqi7D smWoRPwh1Lz2IjPDTbOmmrqs7tIvCI9VLVm9hMoDZfCE8at+oRxcxM5uWAEBWKsXaoZy vnJtOj6uPKZz+Q5cgzHHaLWk0OxHzz73oHcYtTcv3H19Jp/QxVg3ooYd2uuoBq2TJLGk uIHg== 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=df6o6s9/hi7ZornAjnFdRdOabpbqZgeNdpm+McXh0Bo=; fh=YYwLYmpaV0Fpw/rxmSKNRLS2XzDkAlGbHATiKOPtZrY=; b=MWiOJ9u+oLmUN195wy3xjHAtMw1MKZXKU4Ncz2POC9OR2ye2QolfBqS6uVldjluov0 JuIaThMSpF8KEyQ1HXnBEHNyFzTNwdSnIyBw1yjl0fa7WB8swrltXjQ4GPzbVKkxgE0F Ld6pb7O87zGVFOdigh9A1IG7gMbWw3GAQPrn17oGQ6RavopAJbHyVx/10WJRhmXnREwn MJOfmYsAzIuxVbR9JQOI0Z2DAI4RHQ8CCnM9fgJk06KOQC0F9dejwNHAk15xw6bDGefj 4Z2UMmgEi694KGH9jFZinvsRDuHKlvDCpIn47pe9zz00E1Jr2I7eqnhHS5Yfw2r+y/UV X/SA== 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 zo13-20020a170906ff4d00b0099318060d40si12439692ejb.459.2023.07.16.17.27.29; Sun, 16 Jul 2023 17:27:29 -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 1372A68C5D3; Mon, 17 Jul 2023 03:27:16 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net [217.70.183.196]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D2C6968C4F3 for ; Mon, 17 Jul 2023 03:27:07 +0300 (EEST) Received: by mail.gandi.net (Postfix) with ESMTPSA id 1223BE0003 for ; Mon, 17 Jul 2023 00:27:06 +0000 (UTC) From: Michael Niedermayer To: FFmpeg development discussions and patches Date: Mon, 17 Jul 2023 02:26:59 +0200 Message-Id: <20230717002704.3092192-2-michael@niedermayer.cc> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230717002704.3092192-1-michael@niedermayer.cc> References: <20230717002704.3092192-1-michael@niedermayer.cc> MIME-Version: 1.0 X-GND-Sasl: michael@niedermayer.cc Subject: [FFmpeg-devel] [PATCH 2/7] avradio/sdr: compensate for RTLSDR frequency limitations 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: B2aBxFu7SwGn Signed-off-by: Michael Niedermayer --- 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) From patchwork Mon Jul 17 00:27:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Niedermayer X-Patchwork-Id: 42776 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:6da1:b0:131:a7d0:bc6d with SMTP id gl33csp5078566pzb; Sun, 16 Jul 2023 17:27:38 -0700 (PDT) X-Google-Smtp-Source: APBJJlGiIchsnoYWHh7ZnbC6qMcDkt2EfaZkQmLVS7rUavGeLXJecfftQMX5uueynRfxC1pJA17F X-Received: by 2002:a50:ee0d:0:b0:51d:e498:201 with SMTP id g13-20020a50ee0d000000b0051de4980201mr9536112eds.27.1689553657783; Sun, 16 Jul 2023 17:27:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689553657; cv=none; d=google.com; s=arc-20160816; b=eBHZI/w38F9q8VkfrMD+O5RMJEghOJL/QlYgG+nIEEaTwIxe7dMXqsmlRcHAVm36gl uc4iRihpUFFZeJtDxwDEtczm9VX2srOXdy64TH/QCyLfjBHTuxYmWQi8LrXqtjWcJk+K j3qp6x1641lyEMxDIAq8n0tlg0izcjruTzYbiS22S2ti9U40OHK7L+YgeuWY/pEvfqT3 SHd3ruUo17WMcB7fHtlcUBrSiA7vk9f6kf2X4vrmLt+KbSsnPQ/i0bnHF6UrqAnVSg9R PLW92BbShzyIC/e4EHFwsucqdpAWrmOj3z91Uog0U4OvyKdZOPJ0sUBfwZ/1WSRSAcY6 4isg== 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=e+MtykN0YHX6vJ5cs91Q2AUpS0IQQCzQXS5ejsk6EFs=; fh=YYwLYmpaV0Fpw/rxmSKNRLS2XzDkAlGbHATiKOPtZrY=; b=fK4kWuk425R672vQFLEadybZFbX5E3bShEr4T1iD5LZukzaYKttVpTrNcvZMlF2QZF jGl+3Pm/uf0dGs/5gDcPkDlyfM+GBEHHOvisMuS+J1uhFb8AWlVHRngOwBVtA0Gje8AL vPEma4CxHqBBqyM2g/4H55u8C8BP5zihWhoAT++4uCAjRydWwcqqqpozn6j6v32Gc9mR rXZ6A6gKHML3MOQGfcNA+dGxTkDKZSzj3SPEyCjScL6P28qHQ8ci7sSS8V1V2iwOpOEi G2hbeiysVdJwW3zSFju6nfQRTpcLJruBDSxRz/kES2+GtQOSH2puHDs/Hr7BMDaNDGWB ELsA== 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 u15-20020aa7d98f000000b0051e270fe57bsi12357935eds.503.2023.07.16.17.27.37; Sun, 16 Jul 2023 17:27:37 -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 3A57468C622; Mon, 17 Jul 2023 03:27:17 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net [217.70.183.201]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0D3EE68BE1B for ; Mon, 17 Jul 2023 03:27:09 +0300 (EEST) Received: by mail.gandi.net (Postfix) with ESMTPSA id 434C01BF203 for ; Mon, 17 Jul 2023 00:27:07 +0000 (UTC) From: Michael Niedermayer To: FFmpeg development discussions and patches Date: Mon, 17 Jul 2023 02:27:00 +0200 Message-Id: <20230717002704.3092192-3-michael@niedermayer.cc> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230717002704.3092192-1-michael@niedermayer.cc> References: <20230717002704.3092192-1-michael@niedermayer.cc> MIME-Version: 1.0 X-GND-Sasl: michael@niedermayer.cc Subject: [FFmpeg-devel] [PATCH 3/7] avradio/sdrinradio: Print all tunable elements 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: pPOCauONVVpr Signed-off-by: Michael Niedermayer --- libavradio/sdrinradio.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libavradio/sdrinradio.c b/libavradio/sdrinradio.c index f078d27e7b..3956c18375 100644 --- a/libavradio/sdrinradio.c +++ b/libavradio/sdrinradio.c @@ -184,6 +184,10 @@ static int sdrindev_initial_hw_setup(AVFormatContext *s) names = SoapySDRDevice_listAntennas(soapy, SOAPY_SDR_RX, 0, &length); print_and_free_list(s, names, length, "Antennas"); + //Go over all tunable elements + names = SoapySDRDevice_listFrequencies(soapy, SOAPY_SDR_RX, 0, &length); + print_and_free_list(s, names, length, "Tunables"); + //Go over all Gain Elements and print them names = SoapySDRDevice_listGains(soapy, SOAPY_SDR_RX, 0, &length); print_and_free_list(s, names, length, "Rx Gain Elements"); From patchwork Mon Jul 17 00:27:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Niedermayer X-Patchwork-Id: 42774 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:6da1:b0:131:a7d0:bc6d with SMTP id gl33csp5078603pzb; Sun, 16 Jul 2023 17:27:45 -0700 (PDT) X-Google-Smtp-Source: APBJJlEiIth5XdJbdGO/JPa97PDF7mqv4vDkw+qfg0nvPnlL23cRrA/eeHmaEjR0hXKewU6FB7wf X-Received: by 2002:aa7:c9d6:0:b0:51e:4fa:14e9 with SMTP id i22-20020aa7c9d6000000b0051e04fa14e9mr9876827edt.29.1689553665480; Sun, 16 Jul 2023 17:27:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689553665; cv=none; d=google.com; s=arc-20160816; b=vC5o2hflrkk5BQRgytGYMl8Q31IN53azrDqOsJVD63r1RfwD0N+DdEXdpqk/w9AOb0 j7tOUzEishMSIln23njl0976qiQebrwVRm8eDZOG5x8dIPfnHNKN1Sq1s6S9x1yhAOT2 vqlWWyWRAbwAU00zXLDtVScKSxhJUtapTkcvNpjcWT+BvZ6HOQo1lxfeikthaVQDpcKT xW3gCJBt+58D6KNONxIObTOEav58GaxGFWC6Z8F22RO7tbdUiFSpHXxp0VZFwuovdnTp SzB8HgIuoD69WIBQVZkz4GTpGHqV2eQ/O5quL+kPQkbDqP8PvRqVoHvi0EJkmUCdxq7l yoJA== 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=lQhEDJnk4ScmepR2REoH5Tyq1SUA8Vw4bVELCGMc7Z8=; fh=YYwLYmpaV0Fpw/rxmSKNRLS2XzDkAlGbHATiKOPtZrY=; b=N7AehaDkgGEkiGRl1ZPZ4yqnpxQjFc2fvKVGHbOVjcN0fUsBikn07xhP1GihEQ3sxF fZS37svYB3X4f7bBfgQf3A2NGAff2vnX2DJqlDGMQOg28c0RaV2gj6EtHT3K6NcJyhE5 sTpWDHLd764YIoUSEid1Mzmv7PMnwKMGl0m55h/QyhTmWT0S5zwXMqcCSgkyh2Z4eR/n MTwfLlhAh8Hlh5mjLV4qzRz6eBbL7dYRJYtuH0Abn8rTAUztrflj5YlNfDHATBkbCQHv V3ynKi6TjPfIcFO11eDQVZbonIJy7ta2O7SfBg/ox2EKuJ/tiYepSAtEeNZ3sSKrl/2B RRJg== 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 f26-20020a056402151a00b00521777afc15si2889919edw.288.2023.07.16.17.27.45; Sun, 16 Jul 2023 17:27:45 -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 3F1DD68C642; Mon, 17 Jul 2023 03:27:18 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net [217.70.183.198]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9DF9768C36A for ; Mon, 17 Jul 2023 03:27:09 +0300 (EEST) Received: by mail.gandi.net (Postfix) with ESMTPSA id 0C2CAC0004 for ; Mon, 17 Jul 2023 00:27:08 +0000 (UTC) From: Michael Niedermayer To: FFmpeg development discussions and patches Date: Mon, 17 Jul 2023 02:27:01 +0200 Message-Id: <20230717002704.3092192-4-michael@niedermayer.cc> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230717002704.3092192-1-michael@niedermayer.cc> References: <20230717002704.3092192-1-michael@niedermayer.cc> MIME-Version: 1.0 X-GND-Sasl: michael@niedermayer.cc Subject: [FFmpeg-devel] [PATCH 4/7] avradio/sdrdemux: Use a local noise floor in FM Probing 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: 7J0SnZlZNill The global one caused problems when the spectrum is not flat Signed-off-by: Michael Niedermayer --- libavradio/sdrdemux.c | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/libavradio/sdrdemux.c b/libavradio/sdrdemux.c index 5a3af23a74..a4c590acf2 100644 --- a/libavradio/sdrdemux.c +++ b/libavradio/sdrdemux.c @@ -861,17 +861,20 @@ static int probe_fm(SDRContext *sdr) int i; int bandwidth_f = sdr->fm_bandwidth; int half_bw_i = bandwidth_f * (int64_t)sdr->block_size / sdr->sdr_sample_rate; + int floor_bw_i = 10*1000 * (int64_t)sdr->block_size / sdr->sdr_sample_rate; float last_score[3] = {FLT_MAX, FLT_MAX, FLT_MAX}; int border_i = (sdr->sdr_sample_rate - FFMIN(sdr->bandwidth, sdr->sdr_sample_rate*7/8)) * sdr->block_size / sdr->sdr_sample_rate; - double noise_floor = FLT_MAX; if (2*half_bw_i > 2*sdr->block_size) return 0; - for (int pass = 0; pass < 2; pass ++) { + { double avg[2] = {0}, tri = 0; + double floor[2] = {0}; double mean = 0; double center = 0; + float floor_compensation = half_bw_i * half_bw_i / (float)floor_bw_i; + for (i = 0; ilen2block[i]; tri += i*sdr->len2block[i]; @@ -883,12 +886,19 @@ static int probe_fm(SDRContext *sdr) mean += i*sdr->len2block[i]; } + for (i=0; ilen2block[i]; + for (i=2*half_bw_i - floor_bw_i; i<2*half_bw_i; i++) + floor[1] += sdr->len2block[i]; + for(i = half_bw_i; i<2*sdr->block_size - half_bw_i; i++) { double b = avg[0] + sdr->len2block[i]; avg[0] += sdr->len2block[i] - sdr->len2block[i - half_bw_i]; avg[1] -= sdr->len2block[i] - sdr->len2block[i + half_bw_i]; b += avg[1]; tri += avg[1] - avg[0]; + floor[0] += sdr->len2block[i - half_bw_i + floor_bw_i] - sdr->len2block[i - half_bw_i]; + floor[1] -= sdr->len2block[i + half_bw_i - floor_bw_i] - sdr->len2block[i + half_bw_i]; mean += (i+half_bw_i)*sdr->len2block[i+half_bw_i]; center = mean / b; @@ -897,17 +907,24 @@ static int probe_fm(SDRContext *sdr) if (i < border_i || i > 2*sdr->block_size - border_i) continue; - if (pass == 0) { - noise_floor = FFMIN(noise_floor, tri); - } else { + { + float noise_floor; + if (i - half_bw_i < border_i) { + noise_floor = floor[1]; + } else if (i + half_bw_i >= 2*sdr->block_size - border_i) { + noise_floor = floor[0]; + } else + noise_floor = (floor[0] + floor[1])/2; + noise_floor *= floor_compensation; + last_score[2] = last_score[1]; last_score[1] = last_score[0]; - last_score[0] = tri / (noise_floor); + last_score[0] = tri; if (last_score[1] >= last_score[0] && last_score[1] > last_score[2] && - last_score[1] > sdr->fm_threshold) { - double score = last_score[1]; + last_score[1] / noise_floor > sdr->fm_threshold) { + double score = last_score[1] / noise_floor; 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); From patchwork Mon Jul 17 00:27:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Niedermayer X-Patchwork-Id: 42777 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:6da1:b0:131:a7d0:bc6d with SMTP id gl33csp5078644pzb; Sun, 16 Jul 2023 17:27:54 -0700 (PDT) X-Google-Smtp-Source: APBJJlGoa5uJWxXgwlu+Zduh5chVJ9sHTrAO5SK/7iCzBERILFKc1pHOqBlJ+Tk9THJ7tDzZEIEq X-Received: by 2002:a05:6512:e9a:b0:4f8:7055:6f7e with SMTP id bi26-20020a0565120e9a00b004f870556f7emr8238546lfb.44.1689553673785; Sun, 16 Jul 2023 17:27:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689553673; cv=none; d=google.com; s=arc-20160816; b=IdHVDeTsir+7OZL6Ysob3v9/xFzqZi5vlIwLah9qHTUyHYKR2rDP9Arkf+q6kV3YqY pRR5AHkC4+NmtMDXxS19N6AQ87P7ZFWsLWCEk4FCOqLxnQtHLiACOp0vWDCa4jGCm0iV ZZxeoK+lwh7vepBW35nNNySb8rF3eUAkUAKlmfcJ0ViCL+BI0WEY0TmWZo/M8ZUndCb9 IWyA8kTPqhAptUU0zzMMN6VWVlSpiX48gjrxXdjeofqZfb7luVmR716YzBlfPgepK6iB 1T1r/qMesAoNMB7B5nftraaqxE2m9wM9rs0PCO/qILgA1VgS7qnbeVLpKMChQgd0rIxB ClxA== 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=NApvw4BHOrW9bi2LKxGxc+W51oDm/4b5laceUmDHteM=; fh=YYwLYmpaV0Fpw/rxmSKNRLS2XzDkAlGbHATiKOPtZrY=; b=y+KODqxP0GLP5ijsYhPOgPPr3YKrb3/01KQvNY1D199FzYC81yd+aUcvZsIGFOGL31 WKyRC07xDmc7DMqy1GcqkeRSIJKsacExj2aJnwh1RSYHDqgL5DpMYYHHulTMT0NMMxcR 13JHoRupSQE4xrehZNWxag//TSsOFXQAV6Uzft3CqoI6B+hkwFGurFLAFn31JSnhSyPN juY6JYPqLzVg/Wid21zj/b6iIBkslG+99UfZkNA9Rv5LtlAFWq2ybSs3zw7gv4+cY4St mfTolaEXSiTqQAcHxeKmn/jwo9tb8eIcm/SdC1wD5MuFTWIf/srMklrWTv7fFNlGOqH8 JefA== 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 n5-20020a056402514500b0051e339bd2b1si13101138edd.435.2023.07.16.17.27.53; Sun, 16 Jul 2023 17:27:53 -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 4F6F168C68A; Mon, 17 Jul 2023 03:27:20 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from relay3-d.mail.gandi.net (relay3-d.mail.gandi.net [217.70.183.195]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id EDEF568C5B9 for ; Mon, 17 Jul 2023 03:27:10 +0300 (EEST) Received: by mail.gandi.net (Postfix) with ESMTPSA id 3348A60002 for ; Mon, 17 Jul 2023 00:27:09 +0000 (UTC) From: Michael Niedermayer To: FFmpeg development discussions and patches Date: Mon, 17 Jul 2023 02:27:02 +0200 Message-Id: <20230717002704.3092192-5-michael@niedermayer.cc> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230717002704.3092192-1-michael@niedermayer.cc> References: <20230717002704.3092192-1-michael@niedermayer.cc> MIME-Version: 1.0 X-GND-Sasl: michael@niedermayer.cc Subject: [FFmpeg-devel] [PATCH 5/7] avradio/sdrdemux: adjust bandwidth to 200khz for FM probing 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: V0AiRKX1Fdzd This improves the local noise floor value Signed-off-by: Michael Niedermayer --- libavradio/sdrdemux.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libavradio/sdrdemux.c b/libavradio/sdrdemux.c index a4c590acf2..a0b80785ef 100644 --- a/libavradio/sdrdemux.c +++ b/libavradio/sdrdemux.c @@ -860,7 +860,7 @@ static int probe_fm(SDRContext *sdr) { int i; int bandwidth_f = sdr->fm_bandwidth; - int half_bw_i = bandwidth_f * (int64_t)sdr->block_size / sdr->sdr_sample_rate; + int half_bw_i = 200*1000 * (int64_t)sdr->block_size / sdr->sdr_sample_rate; int floor_bw_i = 10*1000 * (int64_t)sdr->block_size / sdr->sdr_sample_rate; float last_score[3] = {FLT_MAX, FLT_MAX, FLT_MAX}; int border_i = (sdr->sdr_sample_rate - FFMIN(sdr->bandwidth, sdr->sdr_sample_rate*7/8)) * sdr->block_size / sdr->sdr_sample_rate; From patchwork Mon Jul 17 00:27:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Niedermayer X-Patchwork-Id: 42778 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:6da1:b0:131:a7d0:bc6d with SMTP id gl33csp5078686pzb; Sun, 16 Jul 2023 17:28:02 -0700 (PDT) X-Google-Smtp-Source: APBJJlGbPPMuDaJ7TEXNcHY2Kk3/xADUQoe4MRQROfcd8CkIMsAxpA53hSbXiqjl7SW7yA0kGU8n X-Received: by 2002:a05:6512:3a85:b0:4f7:4098:9905 with SMTP id q5-20020a0565123a8500b004f740989905mr7638363lfu.65.1689553681854; Sun, 16 Jul 2023 17:28:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689553681; cv=none; d=google.com; s=arc-20160816; b=HHQZ2CKDFKq7sia2QQzGgWj/qoeDZI7qGD6ORs//oemCEasYYqeaj1wGrBfSAc/AgU EdYDhH5UeQzFI/Oc/RsPoa/Hej9TeRIEtI2F2q88OW5JV8psEDQh5UIIRgoeSwiZV75D Sg2Eqh5wr67qRIxvl6xyyrzFFA5rK9KtrEEOHlL/bHjGwn5zyhP5U95inpHvG8bx3zZc R/ditGasay8Yty6PUMMsfisXC7UmQ3oKTg+fDJ6zf/jpmCfaqfQcgQPElXH0VxL+8jx0 UrJXajdV1D2QYpe+AcbG3QtB1ZJ2mlK6FMyTMRE1DZ6BkaBS8p7/nPByqBgXfzU/SD05 sLpA== 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=oOZX40WrYLJmpCh94ktvxqswOOoE4nmaTiGWTk6gWsw=; fh=YYwLYmpaV0Fpw/rxmSKNRLS2XzDkAlGbHATiKOPtZrY=; b=sjpYu3iwg5mew2gdoyVrLXw1hFbiLmkclC94zehhb9wsYdvEd9VkS+dS7JekXDD9SI 5u2tLYRnczQbkZ0IYuVOX+10NPKml62UvLMzqFhpt1paSy8MwsSC/NTlRMB1kv+RSF9i B5JPAy3Z0GGyAtdMnI98zH0dZLlIX6sVjtuLXMq5+LetrmpP6iBBCzMaNlM+nQQAbjsx 1e6VHFPvhE1V79cK2qD1w586xN6FKzynpGeWVRLZC+hOmDbwrFEGN70WigddLXTmvr+g 3hbo3CmYk8l5KzX7cmyx2WnDqK7x6em2F+xVLZqYB3pvKQS9VGalSRDGtQ/rBrA714Hu fOmw== 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 e25-20020a056402089900b00521830574c0si2091464edy.204.2023.07.16.17.28.01; Sun, 16 Jul 2023 17:28:01 -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 6B7FC68C69A; Mon, 17 Jul 2023 03:27:21 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net [217.70.183.198]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 94A1F68C584 for ; Mon, 17 Jul 2023 03:27:11 +0300 (EEST) Received: by mail.gandi.net (Postfix) with ESMTPSA id 0164CC0003 for ; Mon, 17 Jul 2023 00:27:10 +0000 (UTC) From: Michael Niedermayer To: FFmpeg development discussions and patches Date: Mon, 17 Jul 2023 02:27:03 +0200 Message-Id: <20230717002704.3092192-6-michael@niedermayer.cc> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230717002704.3092192-1-michael@niedermayer.cc> References: <20230717002704.3092192-1-michael@niedermayer.cc> MIME-Version: 1.0 X-GND-Sasl: michael@niedermayer.cc Subject: [FFmpeg-devel] [PATCH 6/7] avradio/sdr: Support setting gain value manually and automatic 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: DgM3qAss7eur Signed-off-by: Michael Niedermayer --- libavradio/sdr.h | 23 +++++++++++++++++- libavradio/sdrdemux.c | 53 ++++++++++++++++++++++++++++++++++++++++- libavradio/sdrinradio.c | 41 +++++++++++++++++++++++++++++-- 3 files changed, 113 insertions(+), 4 deletions(-) diff --git a/libavradio/sdr.h b/libavradio/sdr.h index dc20415457..395b056531 100644 --- a/libavradio/sdr.h +++ b/libavradio/sdr.h @@ -68,6 +68,12 @@ typedef enum Modulation { //QAM, PSK, ... } Modulation; +typedef enum SDR_GAIN { + GAIN_DEFAULT = -3, + GAIN_SW_AGC = -2, + GAIN_SDR_AGC = -1, +} SDR_GAIN; + #define HISTOGRAMM_SIZE 9 typedef struct Station { @@ -105,6 +111,7 @@ typedef struct Station { typedef struct FIFOElement { int64_t center_frequency; + float gain; void *halfblock; } FIFOElement; @@ -146,7 +153,15 @@ typedef struct SDRContext { int64_t min_center_freq; int64_t max_center_freq; int sdr_sample_rate; - int sdr_agc; + float min_gain; + float max_gain; + int sdr_gain; + float agc_min_headroom; + float agc_max_headroom; + float agc_max_headroom_time; + int agc_low_time; + float agc_gain; ///< current gain, should be accessed only by buffer thread after init + atomic_int wanted_gain; int sdr_adcc; int64_t bandwidth; int64_t last_pts; @@ -212,6 +227,12 @@ typedef struct SDRContext { */ int64_t (*set_frequency_callback)(struct SDRContext *sdr, int64_t frequency); + /** + * Setup the hardware for the requested gain + * This must only be called from the buffer thread after setup (or more mutex calls are needed) + */ + int (*set_gain_callback)(struct SDRContext *sdr, float gain); + /** * Read from the hardware, block if nothing available with a reasonable timeout * diff --git a/libavradio/sdrdemux.c b/libavradio/sdrdemux.c index a0b80785ef..5214aea7be 100644 --- a/libavradio/sdrdemux.c +++ b/libavradio/sdrdemux.c @@ -1409,6 +1409,7 @@ static void *soapy_needs_bigger_buffers_worker(SDRContext *sdr) FIFOElement fifo_element; int remaining, ret; int empty_blocks, full_blocks; + float wanted_gain = atomic_load(&sdr->wanted_gain) / 65536.0; //i wish av_fifo was thread safe pthread_mutex_lock(&sdr->mutex); @@ -1444,9 +1445,17 @@ static void *soapy_needs_bigger_buffers_worker(SDRContext *sdr) //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 } + if (sdr->sdr_gain == GAIN_SW_AGC && + fabs(wanted_gain - sdr->agc_gain) > 0.001 && + sdr->set_gain_callback + ) { + sdr->set_gain_callback(sdr, wanted_gain); + sdr->agc_gain = wanted_gain; + } pthread_mutex_unlock(&sdr->mutex); fifo_element.center_frequency = block_counter > 0 ? sdr->freq : 0; + fifo_element.gain = sdr->agc_gain; //we make only small changes so slightly mixing should be ok remaining = sdr->block_size; while (remaining && !atomic_load(&sdr->close_requested)) { @@ -1624,6 +1633,7 @@ 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->wanted_gain, lrint((sdr->min_gain + sdr->max_gain) * 65536 / 2)); ret = pthread_mutex_init(&sdr->mutex, NULL); if (ret) { av_log(s, AV_LOG_ERROR, "pthread_mutex_init failed: %s\n", strerror(ret)); @@ -1886,6 +1896,37 @@ process_next_block: } } + float smaller_block_gain = FFMIN(fifo_element[0].gain, fifo_element[1].gain); + float bigger_block_gain = FFMAX(fifo_element[0].gain, fifo_element[1].gain); + + if (sdr->sdr_gain == GAIN_SW_AGC) { + float inmax = 0; + float wanted_gain = atomic_load(&sdr->wanted_gain) / 65536.0; + // We only check 25% of the data to safe computations + int start = 3*sdr->block_size / 4; + int end = 5*sdr->block_size / 4; + for (i = start; i < end; i++) { + float v = fmaxf(fabsf(sdr->windowed_block[i].re), fabsf(sdr->windowed_block[i].im)); + inmax = fmaxf(inmax, v); + } + + if (inmax > 1.0 - sdr->agc_min_headroom && wanted_gain > sdr->min_gain) { + //according to docs this is a dB scale, in reality it beheaves differnt to that + //Because of this we will try to just make small changes and not assume too much + wanted_gain = FFMIN(wanted_gain, FFMAX(smaller_block_gain - 1.0, smaller_block_gain * 0.9)); + + sdr->agc_low_time = 0; + } else if (inmax < 1.0 - sdr->agc_max_headroom && wanted_gain < sdr->max_gain) { + sdr->agc_low_time += sdr->block_size; + if (sdr->agc_low_time > sdr->agc_max_headroom_time * sdr->sdr_sample_rate) { + sdr->agc_low_time = 0; + wanted_gain = FFMAX(wanted_gain, FFMIN(bigger_block_gain + 1.0, bigger_block_gain * 1.1)); + } + } else + sdr->agc_low_time = 0; + atomic_store(&sdr->wanted_gain, (int)lrint(wanted_gain * 65536)); + } + inject_block_into_fifo(sdr, sdr->empty_block_fifo, &fifo_element[0], "Cannot pass next buffer, freeing it\n"); #ifdef SYN_TEST //synthetic test signal static int64_t synp=0; @@ -2141,7 +2182,17 @@ const AVOption ff_sdr_options[] = { { "rtlsdr_fixes" , "workaround rtlsdr issues", OFFSET(rtlsdr_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_agc" , "sdr automatic gain control", OFFSET(sdr_agc), AV_OPT_TYPE_BOOL , {.i64 = 1}, -1, 1, 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"}, + { "default_gain", "Never touch gain", 0, AV_OPT_TYPE_CONST, {.i64 = GAIN_DEFAULT}, 0, 0, DEC, "gain"}, + + { "agc_min_headroom", "AGC min headroom", OFFSET(agc_min_headroom), AV_OPT_TYPE_FLOAT, {.dbl = 0.4}, 0, 1.0, DEC}, + { "agc_max_headroom", "AGC max headroom", OFFSET(agc_max_headroom), AV_OPT_TYPE_FLOAT, {.dbl = 0.8}, 0, 1.0, DEC}, + { "agc_max_headroom_time", "AGC max headroom time", OFFSET(agc_max_headroom_time), AV_OPT_TYPE_FLOAT, {.dbl = 0.1}, 0, INT_MAX, DEC}, + { "min_gain", "minimum gain", OFFSET(min_gain ), AV_OPT_TYPE_FLOAT , {.dbl = 0}, 0, INT_MAX, DEC}, + { "max_gain", "maximum gain", OFFSET(max_gain ), AV_OPT_TYPE_FLOAT , {.dbl = 0}, 0, INT_MAX, DEC}, + { "sdr_adcc" ,"sdr automatic dc correction", OFFSET(sdr_adcc), AV_OPT_TYPE_BOOL , {.i64 = -1}, -1, 1, DEC}, { "min_freq", "minimum frequency", OFFSET(min_freq ), AV_OPT_TYPE_INT64 , {.i64 = 0}, 0, INT64_MAX, DEC}, { "max_freq", "maximum frequency", OFFSET(max_freq ), AV_OPT_TYPE_INT64 , {.i64 = 0}, 0, INT64_MAX, DEC}, diff --git a/libavradio/sdrinradio.c b/libavradio/sdrinradio.c index 3956c18375..63a9cade78 100644 --- a/libavradio/sdrinradio.c +++ b/libavradio/sdrinradio.c @@ -68,6 +68,30 @@ static int sdrindev_read_callback(SDRContext *sdr, FIFOElement *fifo_element, in return ret; } +static int sdrindev_set_gain_callback(SDRContext *sdr, float gain) +{ + AVFormatContext *avfmt = sdr->avfmt; + SoapySDRDevice *soapy = sdr->soapy; + + if (sdr->sdr_gain == GAIN_DEFAULT) + return 0; + + if (soapy) { + int ret = SoapySDRDevice_setGainMode(soapy, SOAPY_SDR_RX, 0, sdr->sdr_gain == GAIN_SDR_AGC); + if (ret) { + av_log(avfmt, AV_LOG_WARNING, "Failed to set gain mode %d (%s)\n", sdr->sdr_gain == GAIN_SDR_AGC, SoapySDRDevice_lastError()); + } + + if (sdr->sdr_gain != GAIN_SDR_AGC) { + ret = SoapySDRDevice_setGain(soapy, SOAPY_SDR_RX, 0, gain); + if (ret) { + av_log(avfmt, AV_LOG_WARNING, "Failed to set gain to %f (%s)\n", gain, SoapySDRDevice_lastError()); + } + } + } + return 0; +} + static int64_t sdrindev_set_frequency_callback(SDRContext *sdr, int64_t freq) { AVFormatContext *avfmt = sdr->avfmt; @@ -135,6 +159,7 @@ static int sdrindev_initial_hw_setup(AVFormatContext *s) sdr->read_callback = sdrindev_read_callback; sdr->set_frequency_callback = sdrindev_set_frequency_callback; + sdr->set_gain_callback = sdrindev_set_gain_callback; // Go over all available soapy devices // Print the usable ones, and choose one unless the user has choosen one @@ -195,8 +220,10 @@ static int sdrindev_initial_hw_setup(AVFormatContext *s) //Inform the user if AGC is supported and setup AGC as requested by the user has_agc = SoapySDRDevice_hasGainMode(soapy, SOAPY_SDR_RX, 0); av_log(s, AV_LOG_INFO, "RX AGC Supported: %s\n", has_agc ? "yes" : "no"); - if (has_agc && sdr->sdr_agc >= 0) - SoapySDRDevice_setGainMode(soapy, SOAPY_SDR_RX, 0, sdr->sdr_agc); + if (!has_agc && sdr->sdr_gain == GAIN_SDR_AGC) { + av_log(s, AV_LOG_WARNING, "hardware AGC unsupported switching to software AGC\n"); + sdr->sdr_gain = GAIN_SW_AGC; + } //Inform the user if automatic DC correction is supported and setup DC correction as requested by the user has_adcc = SoapySDRDevice_hasDCOffsetMode(soapy, SOAPY_SDR_RX, 0); @@ -208,6 +235,16 @@ static int sdrindev_initial_hw_setup(AVFormatContext *s) range = SoapySDRDevice_getGainRange(soapy, SOAPY_SDR_RX, 0); av_log(s, AV_LOG_INFO, "Rx Gain range: %f dB - %f dB\n", range.minimum, range.maximum); + if (!sdr->min_gain) + sdr->min_gain = range.minimum; + + if (!sdr->max_gain) + sdr->max_gain = range.maximum; + if (sdr->min_gain > sdr->max_gain) { + av_log(s, AV_LOG_ERROR, "Invalid gain range\n"); + return AVERROR(EINVAL); + } + //Inform the user about the Frequency ranges available, verify the range requested by the user and set the range if the user has not specified one ranges = SoapySDRDevice_getFrequencyRange(soapy, SOAPY_SDR_RX, 0, &length); av_log(s, AV_LOG_INFO, "Rx freq ranges: "); From patchwork Mon Jul 17 00:27:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Niedermayer X-Patchwork-Id: 42779 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:6da1:b0:131:a7d0:bc6d with SMTP id gl33csp5078743pzb; Sun, 16 Jul 2023 17:28:12 -0700 (PDT) X-Google-Smtp-Source: APBJJlEoDTn4nKCrXRXijA0Ha8OE1w3FzaRWwcdAJBICvIdXKWlrDb9Rzq5hVrLnEgjZ/wNgeAzT X-Received: by 2002:aa7:cd7b:0:b0:51d:9362:5060 with SMTP id ca27-20020aa7cd7b000000b0051d93625060mr9740408edb.1.1689553692182; Sun, 16 Jul 2023 17:28:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689553692; cv=none; d=google.com; s=arc-20160816; b=cMDk9yjotJrLMHL7PJFK7W5TEMRPkucfZNO0zkXY1lR67hXvbIaraP58ZnRw4AcrwS fnHwiSUjh60jP8H4T4CrHqgQJ2m/k1ramdlhZGUGeH9/xC4ldj1ZVPOOp1fvAuxnOpYD pYSVRF3GMC0TgpdLUGlc7ftXo+5FEDjsKzIeC1BxtVdiYTO9HnfcT9ZLn4u3yq4vU/f/ +h/yd2tS6ms6ir/n8lBA74B3xZi6lDO9ohUYaeTuL7rJf5PUvyxfpP06vR+LvVGFbNy/ c2eaIm+YWk+rpoILRMHaZzmQyBYhqfUItYjH9WboK5zFUHJ7CgB4hkngoeeSjt2rVakL ovuQ== 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=fNBjb4OqOoLld/auiGMmRoV3WM8lzbuNr4adJI/wjwQ=; fh=YYwLYmpaV0Fpw/rxmSKNRLS2XzDkAlGbHATiKOPtZrY=; b=cpy0Z/INVU4hfdZZwp2mC5C8iGFuvw4Qu6PcUiw3e8vBBubxYnm1Do33DWUeCPWwfa paKB+jo1RVXziqH6EU/XEVKMgFVMGlwSv99mYiXRsjKa/poW5Hoa5kZgoeEEas2IHoWg MSGkQFpm7jbqRHwJiEsz+lGXLF0o+RZjmzHnIq3RNZO8vj9yhY23YNrkxgp5F16t9gEP giFsfawicVPFECtA243JukOIBFfDq+SC9O2y0DzgOAHcrwfESbP3TdKl18zPcFxKLDn7 0v9oSuCOfT31w+bnGxFBor50yXvmYSD236V5vXHbH/Z1BJjffpTaRFwnRxWO59JsZkM6 YyoA== 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 w1-20020aa7dcc1000000b0051e16629455si5571656edu.42.2023.07.16.17.28.09; Sun, 16 Jul 2023 17:28:12 -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 683F168C6AC; Mon, 17 Jul 2023 03:27:22 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net [217.70.183.196]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 592C368C612 for ; Mon, 17 Jul 2023 03:27:12 +0300 (EEST) Received: by mail.gandi.net (Postfix) with ESMTPSA id C481DE0005 for ; Mon, 17 Jul 2023 00:27:11 +0000 (UTC) From: Michael Niedermayer To: FFmpeg development discussions and patches Date: Mon, 17 Jul 2023 02:27:04 +0200 Message-Id: <20230717002704.3092192-7-michael@niedermayer.cc> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230717002704.3092192-1-michael@niedermayer.cc> References: <20230717002704.3092192-1-michael@niedermayer.cc> MIME-Version: 1.0 X-GND-Sasl: michael@niedermayer.cc Subject: [FFmpeg-devel] [PATCH 7/7] avradio/sdr: workaround inverted gain parameter on sdrplay 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: GbcRYxoEp7/Y Signed-off-by: Michael Niedermayer --- libavradio/sdr.h | 1 + libavradio/sdrdemux.c | 1 + libavradio/sdrinradio.c | 7 +++++++ 3 files changed, 9 insertions(+) diff --git a/libavradio/sdr.h b/libavradio/sdr.h index 395b056531..27ec1db4f3 100644 --- a/libavradio/sdr.h +++ b/libavradio/sdr.h @@ -257,6 +257,7 @@ typedef struct SDRContext { int missing_streams; int rtlsdr_fixes; + int sdrplay_fixes; } SDRContext; typedef struct ModulationDescriptor { diff --git a/libavradio/sdrdemux.c b/libavradio/sdrdemux.c index 5214aea7be..1b3061a406 100644 --- a/libavradio/sdrdemux.c +++ b/libavradio/sdrdemux.c @@ -2180,6 +2180,7 @@ const AVOption ff_sdr_options[] = { { "driver" , "sdr driver name" , OFFSET(driver_name), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC}, { "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}, { "gain" , "sdr overall gain", OFFSET(sdr_gain), AV_OPT_TYPE_INT , {.i64 = GAIN_SDR_AGC}, -3, INT_MAX, DEC, "gain"}, diff --git a/libavradio/sdrinradio.c b/libavradio/sdrinradio.c index 63a9cade78..f824a1d190 100644 --- a/libavradio/sdrinradio.c +++ b/libavradio/sdrinradio.c @@ -76,6 +76,11 @@ static int sdrindev_set_gain_callback(SDRContext *sdr, float gain) if (sdr->sdr_gain == GAIN_DEFAULT) return 0; + //sdrplay has a inverted gain range, not using max_gain as this is a user parameter + if (sdr->sdrplay_fixes > 0) { + gain = FFMIN(48 - gain, 45); + } + if (soapy) { int ret = SoapySDRDevice_setGainMode(soapy, SOAPY_SDR_RX, 0, sdr->sdr_gain == GAIN_SDR_AGC); if (ret) { @@ -192,6 +197,8 @@ static int sdrindev_initial_hw_setup(AVFormatContext *s) if (sdr->rtlsdr_fixes < 0) sdr->rtlsdr_fixes = !strcmp(sdr->driver_name, "rtlsdr"); + if (sdr->sdrplay_fixes < 0) + sdr->sdrplay_fixes = !strcmp(sdr->driver_name, "sdrplay"); SoapySDRKwargs_set(&args, "driver", sdr->driver_name); sdr->soapy = soapy = SoapySDRDevice_make(&args);