From ceebc28d4b56f4721c3e47af912a4c60dc1bfdf2 Mon Sep 17 00:00:00 2001 From: Nikolaj Olsson Date: Sun, 30 May 2021 08:30:48 +0200 Subject: [PATCH] Improve picking best image w PGS/fading - thx peter-qgd :) Fix #5066 --- src/libse/BluRaySup/BluRaySupParser.cs | 42 ++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/src/libse/BluRaySup/BluRaySupParser.cs b/src/libse/BluRaySup/BluRaySupParser.cs index 8e300212e..8f5a7a5c8 100644 --- a/src/libse/BluRaySup/BluRaySupParser.cs +++ b/src/libse/BluRaySup/BluRaySupParser.cs @@ -844,7 +844,10 @@ namespace Nikse.SubtitleEdit.Core.BluRaySup } } - // merge images that are the same (probably due to fade) + // Merge images that are the same (probably due to fade) + // First we find groups with same pixels + var removeIndices = new List(); + int deleteNo = 0; for (int pcsIndex = pcsList.Count - 1; pcsIndex > 0; pcsIndex--) { var cur = pcsList[pcsIndex]; @@ -879,11 +882,38 @@ namespace Nikse.SubtitleEdit.Core.BluRaySup if (remove) { - prev.EndTime = cur.EndTime; - pcsList.RemoveAt(pcsIndex); + if (!removeIndices.Any(p => p.Number == deleteNo && p.Index == pcsIndex - 1)) + { + removeIndices.Add(new DeleteIndex { Number = deleteNo, Index = pcsIndex - 1 }); + } + if (!removeIndices.Any(p => p.Number == deleteNo && p.Index == pcsIndex)) + { + removeIndices.Add(new DeleteIndex { Number = deleteNo, Index = pcsIndex }); + } } } } + else + { + deleteNo++; + } + } + + // Use the middle image of each group with same pixels + foreach (var group in removeIndices.GroupBy(p => p.Number).OrderBy(p => p.Key)) + { + var arr = group.OrderByDescending(p => p.Index).ToArray(); + var middle = (int)Math.Round(group.Count() / 2.0); + var middleElement = arr[middle]; + pcsList[middleElement.Index].StartTime = pcsList[arr.Last().Index].StartTime; + pcsList[middleElement.Index].EndTime = pcsList[arr.First().Index].EndTime; + foreach (var deleteIndex in group.OrderByDescending(p => p.Index)) + { + if (deleteIndex != middleElement) + { + pcsList.RemoveAt(deleteIndex.Index); + } + } } // save last palette @@ -899,6 +929,12 @@ namespace Nikse.SubtitleEdit.Core.BluRaySup return pcsList; } + public class DeleteIndex + { + public int Number { get; set; } + public int Index { get; set; } + } + private static bool ByteArraysEqual(byte[] b1, byte[] b2) { if (b1 == b2)