Added a entropy-calculating filter.
All checks were successful
🚀 Pack skyscraper8 / make-zip (push) Successful in 3m54s

This commit is contained in:
Fey 2026-01-28 18:04:51 +01:00
parent d1ba3976cb
commit 8aa9ac82d4
2 changed files with 60 additions and 12 deletions

View File

@ -54,18 +54,21 @@ namespace skyscraper8.Skyscraper.Math
double p_i = (double)freq[i] / (double)internalPosition; double p_i = (double)freq[i] / (double)internalPosition;
if (p_i > 0) if (p_i > 0)
newEntropy -= p_i * System.Math.Log2(p_i); newEntropy -= p_i * System.Math.Log2(p_i);
} }
//Raise events if we feel like it //Raise events if we feel like it
if (newEntropy != Entropy) if (EntropyRising != null || EntropyFalling != null)
{ {
double oldEntropy = Entropy; if (newEntropy != Entropy)
double oldPercentage = Percentage; {
double newPercentage = (newEntropy / 8.0) * 100.0; double oldEntropy = Entropy;
if (newEntropy > Entropy) double oldPercentage = Percentage;
EntropyRising?.Invoke(oldEntropy, newEntropy, oldPercentage, newPercentage); double newPercentage = (newEntropy / 8.0) * 100.0;
else if (newEntropy < Entropy) if (newEntropy > Entropy)
EntropyFalling?.Invoke(oldEntropy, newEntropy, oldPercentage, newPercentage); EntropyRising?.Invoke(oldEntropy, newEntropy, oldPercentage, newPercentage);
else if (newEntropy < Entropy)
EntropyFalling?.Invoke(oldEntropy, newEntropy, oldPercentage, newPercentage);
}
} }
_entropy = newEntropy; _entropy = newEntropy;

View File

@ -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;
}
}
}