/
FrameFilterTests.cs
86 lines (69 loc) · 2.42 KB
/
FrameFilterTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.IO;
using HeyRed.ImageSharp.AVCodecFormats;
using HeyRed.ImageSharp.AVCodecFormats.Mp4;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.PixelFormats;
using Xunit;
namespace ImageSharp.AVCodecFormats.Tests;
public class FrameFilterTests
{
private readonly string _testVideoDataPath;
public FrameFilterTests()
{
_testVideoDataPath = TestHelpers.GetTestDataPath();
}
/// <summary>
/// Adapted from https://github.com/FFmpeg/FFmpeg/blob/master/libavfilter/vf_blackframe.c
/// </summary>
/// <param name="frame"></param>
/// <param name="frameNum"></param>
/// <returns></returns>
private static bool BlackFrameFilter(ImageFrame frame, uint frameNum)
{
var image = (ImageFrame<Rgba32>)frame;
// The threshold below which a pixel value is considered black
int threshold = 32;
// The percentage of the pixels that have to be below the threshold
// for the frame to be considered black
int percents = 98;
int blackPixelsCount = 0;
image.ProcessPixelRows(accessor =>
{
for (int y = 0; y < accessor.Height; y++)
{
Span<Rgba32> pixelRow = accessor.GetRowSpan(y);
for (int x = 0; x < pixelRow.Length; x++)
{
Rgba32 pixel = pixelRow[x];
int value = (pixel.R + pixel.G + pixel.B) / 3;
if (value < threshold)
{
blackPixelsCount++;
}
}
}
});
int blackPercent = blackPixelsCount * 100 / (frame.Width * frame.Height);
return blackPercent >= percents;
}
[Theory]
[InlineData("av1.mp4", 9)]
[InlineData("black_frame.mp4", 8)]
public void BlackFrameFilterTest(string fileName, int expectedFramesCount)
{
var decoderOptions = new AVDecoderOptions
{
GeneralOptions = new DecoderOptions
{
MaxFrames = 10,
},
FrameFilter = BlackFrameFilter,
};
string filePath = Path.Combine(_testVideoDataPath, fileName);
using var inputStream = File.OpenRead(filePath);
using var image = Mp4Decoder.Instance.Decode(decoderOptions, inputStream);
Assert.Equal(expectedFramesCount, image.Frames.Count);
}
}