Skip to content

Commit

Permalink
Fix remaining tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianFeldmann committed Nov 29, 2024
1 parent 985fc94 commit bb497d5
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 151 deletions.
40 changes: 21 additions & 19 deletions YUViewLib/src/filesource/FrameFormatGuess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ GuessedFrameFormat guessFrameSizeFPSAndBitDepthFromName(const std::string &name)
// Something_2160x1440_60Hz_8_more.yuv Second matches Something_2160x1440_60_more.yuv or
// Something_2160x1440_60.yuv Thirs matches Something_2160x1440_more.yuv or
// Something_2160x1440.yuv
const auto REGEXP_LIST = {"([0-9]+)(?:x|X|\\*)([0-9]+)_([0-9]+)(?:Hz)?_([0-9]+)b?[\\._]",
"([0-9]+)(?:x|X|\\*)([0-9]+)_([0-9]+)(?:Hz)?[\\._]",
"([0-9]+)(?:x|X|\\*)([0-9]+)[\\._]"};
const auto REGEXP_LIST = {"([0-9]+)(?:x|\\*)([0-9]+)_([0-9]+)(?:hz)?_([0-9]+)b?[\\._]",
"([0-9]+)(?:x|\\*)([0-9]+)_([0-9]+)(?:hz)?[\\._]",
"([0-9]+)(?:x|\\*)([0-9]+)[\\._]"};

for (const auto &regExpStr : REGEXP_LIST)
{
Expand Down Expand Up @@ -111,20 +111,19 @@ GuessedFrameFormat guessFrameSizeAndFrameRateFromResolutionIndicators(const std:

std::optional<Size> guessFrameSizeFromAcronymResolutionIndicators(const std::string &name)
{
const auto nameLower = functions::toLower(name);
if (nameLower.find("_cif") != std::string::npos)
if (name.find("_cif") != std::string::npos)
return Size(352, 288);
else if (nameLower.find("_qcif") != std::string::npos)
else if (name.find("_qcif") != std::string::npos)
return Size(176, 144);
else if (nameLower.find("_4cif") != std::string::npos)
else if (name.find("_4cif") != std::string::npos)
return Size(704, 576);
else if (nameLower.find("UHD") != std::string::npos)
else if (name.find("uhd") != std::string::npos)
return Size(3840, 2160);
else if (nameLower.find("HD") != std::string::npos)
else if (name.find("hd") != std::string::npos)
return Size(1920, 1080);
else if (nameLower.find("1080p") != std::string::npos)
else if (name.find("1080p") != std::string::npos)
return Size(1920, 1080);
else if (nameLower.find("720p") != std::string::npos)
else if (name.find("720p") != std::string::npos)
return Size(1280, 720);

return {};
Expand All @@ -133,7 +132,7 @@ std::optional<Size> guessFrameSizeFromAcronymResolutionIndicators(const std::str
std::optional<int> guessFPSFromFPSOrHzIndicators(const std::string &name)
{
std::smatch frameSizeMatch;
const std::regex strExpr("([0-9]+)(FPS|fps|HZ|Hz)");
const std::regex strExpr("([0-9]+)(fps|hz)");
if (std::regex_search(name, frameSizeMatch, strExpr))
return toUnsigned(frameSizeMatch[1].str());
return {};
Expand All @@ -142,7 +141,7 @@ std::optional<int> guessFPSFromFPSOrHzIndicators(const std::string &name)
std::optional<unsigned> guessBitDepthFromName(const std::string &name)
{
const auto REGEXP_LIST = {
"(8|9|10|12|16)-?(BIT|Bit|bit)", // E.g. 10bit, 10BIT, 10-bit, 10-BIT
"(8|9|10|12|16)-?(bit)", // E.g. 10bit, 10BIT, 10-bit, 10-BIT
"(?:_|\\.|-)(8|9|10|12|16)b(?:_|\\.|-)" // E.g. _16b_ .8b. -12b-
};

Expand Down Expand Up @@ -208,25 +207,28 @@ GuessedFrameFormat guessFrameFormat(const FileInfoForGuess &fileInfo)

for (auto const &name : {fileInfo.filename, fileInfo.parentFolderName})
{
const auto nameLower = functions::toLower(name);

if (!result.frameSize)
result = guessFrameSizeFPSAndBitDepthFromName(name);
result = guessFrameSizeFPSAndBitDepthFromName(nameLower);

if (!result.frameSize)
result = guessFrameSizeAndFrameRateFromResolutionIndicators(name);
result = guessFrameSizeAndFrameRateFromResolutionIndicators(nameLower);

if (!result.frameSize)
result.frameSize = guessFrameSizeFromAcronymResolutionIndicators(name);
result.frameSize = guessFrameSizeFromAcronymResolutionIndicators(nameLower);

if (!result.frameSize)
continue;

if (!result.frameRate)
result.frameRate = guessFPSFromFPSOrHzIndicators(name);
result.frameRate = guessFPSFromFPSOrHzIndicators(nameLower);

if (!result.bitDepth)
result.bitDepth = guessBitDepthFromName(name);
result.bitDepth = guessBitDepthFromName(nameLower);

result.dataLayout = guessIsPackedFromName(name);
if (!result.dataLayout)
result.dataLayout = guessIsPackedFromName(nameLower);
}

return result;
Expand Down
14 changes: 12 additions & 2 deletions YUViewLib/src/video/rgb/PixelFormatRGBGuess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ using filesource::frameFormatGuess::GuessedFrameFormat;
namespace video::rgb
{

namespace
{

const auto DEFAULT_PIXEL_FORMAT = PixelFormatRGB(8, DataLayout::Packed, ChannelOrder::RGB);

DataLayout findDataLayoutInName(const std::string &fileName)
{
std::string matcher = "(?:_|\\.|-)(packed|planar)(?:_|\\.|-)";
Expand Down Expand Up @@ -74,6 +79,8 @@ bool doesPixelFormatMatchFileSize(const PixelFormatRGB &pixelFormat,
return isFileSizeAMultipleOfFrameSize;
}

} // namespace

std::optional<PixelFormatRGB> checkForPixelFormatIndicatorInName(
const std::string &filename, const Size &frameSize, const std::optional<std::int64_t> &fileSize)
{
Expand Down Expand Up @@ -141,7 +148,7 @@ std::optional<PixelFormatRGB> checkForPixelFormatIndicatorInFileExtension(

for (const auto &[channelOrder, name] : ChannelOrderMapper)
{
if (fileExtension == functions::toLower(name))
if (fileExtension == ("." + functions::toLower(name)))
{
auto format = PixelFormatRGB(8, DataLayout::Packed, channelOrder);
if (doesPixelFormatMatchFileSize(format, frameSize, fileSize))
Expand All @@ -160,7 +167,7 @@ std::optional<PixelFormatRGB> checkSpecificFileExtensions(
{
const auto fileExtension = std::filesystem::path(filename).extension();

if (fileExtension == "cmyk")
if (fileExtension == ".cmyk")
{
const auto format = PixelFormatRGB(8, DataLayout::Packed, ChannelOrder::RGB, AlphaMode::Last);
if (doesPixelFormatMatchFileSize(format, frameSize, fileSize))
Expand Down Expand Up @@ -194,6 +201,9 @@ PixelFormatRGB guessPixelFormatFromSizeAndName(const GuessedFrameFormat &guessed
functions::toLower(fileInfo.parentFolderName), frameSize, fileSize))
return *pixelFormat;

if (guessedFrameFormat.frameSize)
return DEFAULT_PIXEL_FORMAT;

return {};
}

Expand Down
30 changes: 9 additions & 21 deletions YUViewLib/src/video/yuv/PixelFormatYUVGuess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,11 @@ Subsampling findSubsamplingTypeIndicatorInName(const std::string &name)
return Subsampling::UNKNOWN;
}

std::vector<unsigned>
getDetectionBitDepthList(const std::optional<unsigned> &bitDepthToForceAsFirst)
std::vector<unsigned> getDetectionBitDepthList(const std::optional<unsigned> &detectedBitrate)
{
std::vector<unsigned> bitDepthList;
if (bitDepthToForceAsFirst && *bitDepthToForceAsFirst >= 8 && *bitDepthToForceAsFirst <= 16)
bitDepthList.push_back(*bitDepthToForceAsFirst);

for (auto bitDepth : {10u, 12u, 14u, 16u, 8u})
{
if (bitDepth == bitDepthToForceAsFirst)
bitDepthList.push_back(bitDepth);
}
return bitDepthList;
if (detectedBitrate)
return {*detectedBitrate};
return {10u, 12u, 14u, 16u, 8u};
}

std::vector<Subsampling> getDetectionSubsamplingList(Subsampling subsamplingToForceAsFirst,
Expand All @@ -96,7 +88,7 @@ std::vector<Subsampling> getDetectionSubsamplingList(Subsampling subsamplingToFo
if (subsamplingToForceAsFirst != Subsampling::UNKNOWN)
subsamplingList.push_back(subsamplingToForceAsFirst);
for (auto subsampling : detectionSubsampleList)
if (subsampling == subsamplingToForceAsFirst)
if (subsampling != subsamplingToForceAsFirst)
subsamplingList.push_back(subsampling);
return subsamplingList;
}
Expand Down Expand Up @@ -247,7 +239,7 @@ checkSpecificFileExtensions(const GuessedFrameFormat &guessedFrameFormat,
{
const auto fileExtension = std::filesystem::path(fileInfo.filename).extension();

if (fileExtension == "raw")
if (fileExtension == ".raw")
{
const auto rawBayerFormat =
PixelFormatYUV(Subsampling::YUV_400, guessedFrameFormat.bitDepth.value_or(8));
Expand All @@ -256,7 +248,7 @@ checkSpecificFileExtensions(const GuessedFrameFormat &guessedFrameFormat,
return rawBayerFormat;
}

if (fileExtension == "v210" || fileExtension == "V210")
if (fileExtension == ".v210" || fileExtension == ".V210")
{
const auto v210Format = PixelFormatYUV(PredefinedPixelFormat::V210);
if (doesPixelFormatMatchFileSize(v210Format, *guessedFrameFormat.frameSize, fileInfo.fileSize))
Expand Down Expand Up @@ -383,22 +375,18 @@ checkForSubsamplingIndiatorInName(const std::string &name,
std::optional<PixelFormatYUV> ignoreNameAndJustCheckIfSomeBasicFormatsMatchTheFileSize(
const GuessedFrameFormat &guessedFrameFormat, const FileInfoForGuess &fileInfo)
{
// Nothing using the name worked so far. Check some formats. The first one that matches the file
// size wins.
const auto testSubsamplings =
std::vector<Subsampling>({Subsampling::YUV_420, Subsampling::YUV_444, Subsampling::YUV_422});

std::vector<int> testBitDepths;
if (guessedFrameFormat.bitDepth != 0)
// We already extracted a bit depth from the name. Only try that.
if (guessedFrameFormat.bitDepth)
testBitDepths.push_back(*guessedFrameFormat.bitDepth);
else
// We don't know the bit depth. Try different values.
testBitDepths = {8, 9, 10, 12, 14, 16};

for (const auto &subsampling : testSubsamplings)
{
for (auto bd : testBitDepths)
for (const auto bd : testBitDepths)
{
auto fmt = PixelFormatYUV(subsampling, bd, PlaneOrder::YUV);
if (doesPixelFormatMatchFileSize(fmt, *guessedFrameFormat.frameSize, fileInfo.fileSize))
Expand Down
26 changes: 14 additions & 12 deletions YUViewUnitTest/filesource/FormatGuessingParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,23 @@
namespace filesource::frameFormatGuess::test
{

static std::string
formatFileInfoForGuessAndGuessedFrameFormat(const FileInfoForGuess &fileInfoForGuess,
const GuessedFrameFormat &guessedFrameFormat)
static std::string formatFileInfoForGuessForTestName(const FileInfoForGuess &fileInfoForGuess)
{
auto name = yuviewTest::formatTestName("TestName",
fileInfoForGuess.filename,
"ParentPath",
fileInfoForGuess.parentFolderName,
"fileSize",
fileInfoForGuess.fileSize,
"Size",
return yuviewTest::formatTestName("Filename",
fileInfoForGuess.filename,
"parentFolderName",
fileInfoForGuess.parentFolderName,
"fileSize",
fileInfoForGuess.fileSize);
}

static std::string formatGuessedFrameFormatForTestName(const GuessedFrameFormat &guessedFrameFormat)
{
auto name = yuviewTest::formatTestName("frameSize",
guessedFrameFormat.frameSize,
"fps",
"frameRate",
guessedFrameFormat.frameRate,
"BitDepth",
"bitDepth",
guessedFrameFormat.bitDepth);

name += "_DataLayout_";
Expand Down
22 changes: 17 additions & 5 deletions YUViewUnitTest/filesource/guessFormatFromFilenameTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class GuessFormatFromFilenameTest : public TestWithParam<TestParam>
std::string getTestName(const testing::TestParamInfo<TestParam> &testParam)
{
const auto [fileInfoForGuess, expectedFormat] = testParam.param;
return formatFileInfoForGuessAndGuessedFrameFormat(fileInfoForGuess, expectedFormat);
return formatFileInfoForGuessForTestName(fileInfoForGuess) + "_" +
formatGuessedFrameFormatForTestName(expectedFormat);
}

TEST_P(GuessFormatFromFilenameTest, TestFormatFromFilename)
Expand All @@ -66,6 +67,7 @@ INSTANTIATE_TEST_SUITE_P(
FilesourceTest,
GuessFormatFromFilenameTest,
Values(
// Resolution must use an 'x' (case irrelevant) or a '*' between width/height
std::make_pair(FileInfoForGuess({"something_1920x1080.yuv", "", {}}),
ExpectedGuessResult({Size(1920, 1080), {}, {}, {}})),
std::make_pair(FileInfoForGuess({"something_295x289.yuv", "", {}}),
Expand All @@ -78,15 +80,18 @@ INSTANTIATE_TEST_SUITE_P(
ExpectedGuessResult({Size(1920, 1080), {}, {}, {}})),
std::make_pair(FileInfoForGuess({"something_1920x1080_something.yuv", "", {}}),
ExpectedGuessResult({Size(1920, 1080), {}, {}, {}})),

// Other characters are not supported
std::make_pair(FileInfoForGuess({"something_1920_1080.yuv", "", {}}),
ExpectedGuessResult({Size(0, 0), {}, {}, {}})),
ExpectedGuessResult({{}, {}, {}, {}})),
std::make_pair(FileInfoForGuess({"something_19201080.yuv", "", {}}),
ExpectedGuessResult({Size(0, 0), {}, {}, {}})),
ExpectedGuessResult({{}, {}, {}, {}})),
std::make_pair(FileInfoForGuess({"something_1280-720.yuv", "", {}}),
ExpectedGuessResult({Size(0, 0), {}, {}, {}})),
ExpectedGuessResult({{}, {}, {}, {}})),
std::make_pair(FileInfoForGuess({"something_1920-1080_something.yuv", "", {}}),
ExpectedGuessResult({Size(0, 0), {}, {}, {}})),
ExpectedGuessResult({{}, {}, {}, {}})),

// Test fps detection with 'xxxhz' or 'xxfps'. Cases should not matter.
std::make_pair(FileInfoForGuess({"something_1920x1080_25.yuv", "", {}}),
ExpectedGuessResult({Size(1920, 1080), 25, {}, {}})),
std::make_pair(FileInfoForGuess({"something_1920x1080_999.yuv", "", {}}),
Expand All @@ -99,10 +104,17 @@ INSTANTIATE_TEST_SUITE_P(
ExpectedGuessResult({Size(1920, 1080), 60, {}, {}})),
std::make_pair(FileInfoForGuess({"something_1920x1080_60HZ.yuv", "", {}}),
ExpectedGuessResult({Size(1920, 1080), 60, {}, {}})),
std::make_pair(FileInfoForGuess({"something_1920x1080_60hZ.yuv", "", {}}),
ExpectedGuessResult({Size(1920, 1080), 60, {}, {}})),
std::make_pair(FileInfoForGuess({"something_1920x1080_60fps.yuv", "", {}}),
ExpectedGuessResult({Size(1920, 1080), 60, {}, {}})),
std::make_pair(FileInfoForGuess({"something_1920x1080_60FPS.yuv", "", {}}),
ExpectedGuessResult({Size(1920, 1080), 60, {}, {}})),
std::make_pair(FileInfoForGuess({"something_1920x1080_60fPs.yuv", "", {}}),
ExpectedGuessResult({Size(1920, 1080), 60, {}, {}})),
// The indicator can even be anywhere
std::make_pair(FileInfoForGuess({"something240fPssomething_1920x1080.yuv", "", {}}),
ExpectedGuessResult({Size(1920, 1080), 240, {}, {}})),

std::make_pair(FileInfoForGuess({"something_1920x1080_25_8.yuv", "", {}}),
ExpectedGuessResult({Size(1920, 1080), 25, 8, {}})),
Expand Down
Loading

0 comments on commit bb497d5

Please sign in to comment.