Skip to content

Commit

Permalink
Bugfix-FFmpegWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
rlauuzo committed Apr 20, 2024
1 parent 9abcf25 commit 3b4c4bf
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void TestIndexGeneration()
{77, 5},
};

var actual = FFmpegWrapper.CreateInvertedIndex(Guid.NewGuid(), fpr);
var actual = FFmpegWrapper.CreateInvertedIndex(Guid.NewGuid(), fpr, AnalysisMode.Introduction);

Assert.Equal(expected, actual);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ public ChromaprintAnalyzer(ILogger<ChromaprintAnalyzer> logger)
var rhsRanges = new List<TimeRange>();

// Generate inverted indexes for the left and right episodes.
var lhsIndex = FFmpegWrapper.CreateInvertedIndex(lhsId, lhsPoints);
var rhsIndex = FFmpegWrapper.CreateInvertedIndex(rhsId, rhsPoints);
var lhsIndex = FFmpegWrapper.CreateInvertedIndex(lhsId, lhsPoints, this._analysisMode);
var rhsIndex = FFmpegWrapper.CreateInvertedIndex(rhsId, rhsPoints, this._analysisMode);
var indexShifts = new HashSet<int>();

// For all audio points in the left episode, check if the right episode has a point which matches exactly.
Expand Down
23 changes: 10 additions & 13 deletions ConfusedPolarBear.Plugin.IntroSkipper/FFmpegWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
Expand All @@ -14,8 +15,6 @@ namespace ConfusedPolarBear.Plugin.IntroSkipper;
/// </summary>
public static class FFmpegWrapper
{
private static readonly object InvertedIndexCacheLock = new();

/// <summary>
/// Used with FFmpeg's silencedetect filter to extract the start and end times of silence.
/// </summary>
Expand All @@ -34,7 +33,7 @@ public static class FFmpegWrapper

private static Dictionary<string, string> ChromaprintLogs { get; set; } = new();

private static Dictionary<Guid, Dictionary<uint, int>> InvertedIndexCache { get; set; } = new();
private static ConcurrentDictionary<AnalysisMode, ConcurrentDictionary<Guid, Dictionary<uint, int>>> InvertedIndexCache { get; set; } = new();

/// <summary>
/// Check that the installed version of ffmpeg supports chromaprint.
Expand Down Expand Up @@ -137,15 +136,16 @@ public static uint[] Fingerprint(QueuedEpisode episode, AnalysisMode mode)
/// </summary>
/// <param name="id">Episode ID.</param>
/// <param name="fingerprint">Chromaprint fingerprint.</param>
/// <param name="mode">Mode.</param>
/// <returns>Inverted index.</returns>
public static Dictionary<uint, int> CreateInvertedIndex(Guid id, uint[] fingerprint)
public static Dictionary<uint, int> CreateInvertedIndex(Guid id, uint[] fingerprint, AnalysisMode mode)
{
lock (InvertedIndexCacheLock)
var innerDictionary = InvertedIndexCache.GetOrAdd(mode, _ => new ConcurrentDictionary<Guid, Dictionary<uint, int>>());

// Check if cached for the ID
if (innerDictionary.TryGetValue(id, out var cached))
{
if (InvertedIndexCache.TryGetValue(id, out var cached))
{
return cached;
}
return cached;
}

var invIndex = new Dictionary<uint, int>();
Expand All @@ -159,10 +159,7 @@ public static uint[] Fingerprint(QueuedEpisode episode, AnalysisMode mode)
invIndex[point] = i;
}

lock (InvertedIndexCacheLock)
{
InvertedIndexCache[id] = invIndex;
}
innerDictionary[id] = invIndex;

return invIndex;
}
Expand Down

0 comments on commit 3b4c4bf

Please sign in to comment.