From 8aa9ac82d4eae895753d1854fbcd4795aff92312 Mon Sep 17 00:00:00 2001 From: Fey Date: Wed, 28 Jan 2026 18:04:51 +0100 Subject: [PATCH] Added a entropy-calculating filter. --- .../Math/EntropyCalculatorStream.cs | 27 ++++++----- .../Math/PidEntryCalculatorFilter.cs | 45 +++++++++++++++++++ 2 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 skyscraper8/Skyscraper/Math/PidEntryCalculatorFilter.cs diff --git a/skyscraper8/Skyscraper/Math/EntropyCalculatorStream.cs b/skyscraper8/Skyscraper/Math/EntropyCalculatorStream.cs index 0b8d80c..df05e48 100644 --- a/skyscraper8/Skyscraper/Math/EntropyCalculatorStream.cs +++ b/skyscraper8/Skyscraper/Math/EntropyCalculatorStream.cs @@ -54,18 +54,21 @@ namespace skyscraper8.Skyscraper.Math double p_i = (double)freq[i] / (double)internalPosition; if (p_i > 0) newEntropy -= p_i * System.Math.Log2(p_i); - } - - //Raise events if we feel like it - if (newEntropy != Entropy) - { - double oldEntropy = Entropy; - double oldPercentage = Percentage; - double newPercentage = (newEntropy / 8.0) * 100.0; - if (newEntropy > Entropy) - EntropyRising?.Invoke(oldEntropy, newEntropy, oldPercentage, newPercentage); - else if (newEntropy < Entropy) - EntropyFalling?.Invoke(oldEntropy, newEntropy, oldPercentage, newPercentage); + } + + //Raise events if we feel like it + if (EntropyRising != null || EntropyFalling != null) + { + if (newEntropy != Entropy) + { + double oldEntropy = Entropy; + double oldPercentage = Percentage; + double newPercentage = (newEntropy / 8.0) * 100.0; + if (newEntropy > Entropy) + EntropyRising?.Invoke(oldEntropy, newEntropy, oldPercentage, newPercentage); + else if (newEntropy < Entropy) + EntropyFalling?.Invoke(oldEntropy, newEntropy, oldPercentage, newPercentage); + } } _entropy = newEntropy; diff --git a/skyscraper8/Skyscraper/Math/PidEntryCalculatorFilter.cs b/skyscraper8/Skyscraper/Math/PidEntryCalculatorFilter.cs new file mode 100644 index 0000000..58d7817 --- /dev/null +++ b/skyscraper8/Skyscraper/Math/PidEntryCalculatorFilter.cs @@ -0,0 +1,45 @@ +using skyscraper5.Mpeg2; +using skyscraper5.src.Mpeg2.PacketFilter; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace skyscraper8.Skyscraper.Math +{ + public class PidEntryCalculatorFilter : IPacketFilter + { + public bool PassPacket(TsPacket packet) + { + if (entropyCalculatorStreams == null) + entropyCalculatorStreams = new EntropyCalculatorStream[0x1fff]; + + if (packet.PayloadFlagSet) + { + uint pid = packet.PID; + if (entropyCalculatorStreams[pid] == null) + entropyCalculatorStreams[pid] = new EntropyCalculatorStream(); + entropyCalculatorStreams[pid].Write(packet.Payload); + } + return true; + } + + private EntropyCalculatorStream[] entropyCalculatorStreams; + + public double GetEntropy(int pid) + { + if (pid < 0) + throw new ArgumentOutOfRangeException(nameof(pid)); + + if (pid > entropyCalculatorStreams.Length) + throw new ArgumentOutOfRangeException(nameof(pid)); + + EntropyCalculatorStream stream = entropyCalculatorStreams[pid]; + if (stream == null) + return double.NaN; + + return stream.Entropy; + } + } +}