Skip to content

Commit

Permalink
Added google test and moved a first test over (file name guessing test).
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianFeldmann committed Jul 7, 2024
1 parent 4b03932 commit 0f4e2bb
Show file tree
Hide file tree
Showing 46 changed files with 746 additions and 585 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "submodules/googletest"]
path = submodules/googletest
url = [email protected]:google/googletest.git
4 changes: 4 additions & 0 deletions YUView.pro
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ YUViewLib.subdir = YUViewLib
YUViewApp.depends = YUViewLib

UNITTESTS {
SUBDIRS += Googletest
Googletest.subdir = submodules/googletest-qmake

SUBDIRS += YUViewUnitTest
YUViewUnitTest.subdir = YUViewUnitTest
YUViewUnitTest.depends = Googletest
YUViewUnitTest.depends = YUViewLib
}
24 changes: 23 additions & 1 deletion YUViewLib/src/common/Formatting.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,31 @@ static std::ostream &operator<<(std::ostream &stream, const Size &size)
return stream;
}

static std::string to_string(const Size &size)
inline std::string to_string(const Size &size)
{
std::ostringstream stream;
stream << size;
return stream.str();
}

inline std::string to_string(const bool b)
{
return b ? "True" : "False";
}

inline std::string stringReplaceAll(std::string str, char value, char replacement)
{
std::replace(str.begin(), str.end(), value, replacement);
return str;
}

inline std::string stringReplaceAll(std::string str, std::initializer_list<char> values, char replacement)
{
std::replace_if(
str.begin(),
str.end(),
[&values](const char value)
{ return std::find(values.begin(), values.end(), value) != values.end(); },
replacement);
return str;
}
5 changes: 0 additions & 5 deletions YUViewLib/src/common/Functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ QStringList toQStringList(const std::vector<std::string> &stringVec);
std::string toLower(std::string str);
ByteVector readData(std::istream &istream, const size_t nrBytes);

inline std::string boolToString(bool b)
{
return b ? "True" : "False";
}

template <typename T> unsigned clipToUnsigned(T val)
{
static_assert(std::is_signed<T>::value, "T must must be a signed type");
Expand Down
161 changes: 0 additions & 161 deletions YUViewLib/src/filesource/FileSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@

#include <QDateTime>
#include <QDir>
#include <QRegularExpression>
#include <QSettings>
#include <QtGlobal>
#ifdef Q_OS_WIN
Expand Down Expand Up @@ -142,166 +141,6 @@ QString FileSource::getAbsoluteFilePath() const
return this->isFileOpened ? this->fileInfo.absoluteFilePath() : QString();
}

FileSource::fileFormat_t FileSource::guessFormatFromFilename() const
{
FileSource::fileFormat_t format;

// We are going to check two strings (one after the other) for indicators on the frames size, fps
// and bit depth. 1: The file name, 2: The folder name that the file is contained in.

auto dirName = this->fileInfo.absoluteDir().dirName();
auto fileName = this->fileInfo.fileName();
if (fileName.isEmpty())
return format;

for (auto const &name : {fileName, dirName})
{
// First, we will try to get a frame size from the name
if (!format.frameSize.isValid())
{
// The regular expressions to match. They are sorted from most detailed to least so that the
// most detailed ones are tested first.
auto regExprList =
QStringList()
<< "([0-9]+)(?:x|X|\\*)([0-9]+)_([0-9]+)(?:Hz)?_([0-9]+)b?[\\._]" // Something_2160x1440_60_8_more.yuv
// or
// Something_2160x1440_60_8b.yuv
// or
// Something_2160x1440_60Hz_8_more.yuv
<< "([0-9]+)(?:x|X|\\*)([0-9]+)_([0-9]+)(?:Hz)?[\\._]" // Something_2160x1440_60_more.yuv
// or Something_2160x1440_60.yuv
<< "([0-9]+)(?:x|X|\\*)([0-9]+)[\\._]"; // Something_2160x1440_more.yuv or
// Something_2160x1440.yuv

for (auto regExpStr : regExprList)
{
QRegularExpression exp(regExpStr);
auto match = exp.match(name);
if (match.hasMatch())
{
auto widthString = match.captured(1);
auto heightString = match.captured(2);
format.frameSize = Size(widthString.toInt(), heightString.toInt());

auto rateString = match.captured(3);
if (!rateString.isEmpty())
format.frameRate = rateString.toDouble();

auto bitDepthString = match.captured(4);
if (!bitDepthString.isEmpty())
format.bitDepth = bitDepthString.toUInt();

break; // Don't check the following expressions
}
}
}

// try resolution indicators with framerate: "1080p50", "720p24" ...
if (!format.frameSize.isValid())
{
QRegularExpression rx1080p("1080p([0-9]+)");
auto matchIt = rx1080p.globalMatch(name);
if (matchIt.hasNext())
{
auto match = matchIt.next();
format.frameSize = Size(1920, 1080);
auto frameRateString = match.captured(1);
format.frameRate = frameRateString.toInt();
}
}
if (!format.frameSize.isValid())
{
QRegularExpression rx720p("720p([0-9]+)");
auto matchIt = rx720p.globalMatch(name);
if (matchIt.hasNext())
{
auto match = matchIt.next();
format.frameSize = Size(1280, 720);
auto frameRateString = match.captured(1);
format.frameRate = frameRateString.toInt();
}
}

if (!format.frameSize.isValid())
{
// try to find resolution indicators (e.g. 'cif', 'hd') in file name
if (name.contains("_cif", Qt::CaseInsensitive))
format.frameSize = Size(352, 288);
else if (name.contains("_qcif", Qt::CaseInsensitive))
format.frameSize = Size(176, 144);
else if (name.contains("_4cif", Qt::CaseInsensitive))
format.frameSize = Size(704, 576);
else if (name.contains("UHD", Qt::CaseSensitive))
format.frameSize = Size(3840, 2160);
else if (name.contains("HD", Qt::CaseSensitive))
format.frameSize = Size(1920, 1080);
else if (name.contains("1080p", Qt::CaseSensitive))
format.frameSize = Size(1920, 1080);
else if (name.contains("720p", Qt::CaseSensitive))
format.frameSize = Size(1280, 720);
}

// Second, if we were able to get a frame size but no frame rate, we will try to get a frame
// rate.
if (format.frameSize.isValid() && format.frameRate == -1)
{
// Look for: 24fps, 50fps, 24FPS, 50FPS
QRegularExpression rxFPS("([0-9]+)fps", QRegularExpression::CaseInsensitiveOption);
auto match = rxFPS.match(name);
if (match.hasMatch())
{
auto frameRateString = match.captured(1);
format.frameRate = frameRateString.toInt();
}
}
if (format.frameSize.isValid() && format.frameRate == -1)
{
// Look for: 24Hz, 50Hz, 24HZ, 50HZ
QRegularExpression rxHZ("([0-9]+)HZ", QRegularExpression::CaseInsensitiveOption);
auto match = rxHZ.match(name);
if (match.hasMatch())
{
QString frameRateString = match.captured(1);
format.frameRate = frameRateString.toInt();
}
}

// Third, if we were able to get a frame size but no bit depth, we try to get a bit depth.
if (format.frameSize.isValid() && format.bitDepth == 0)
{
for (unsigned bd : {8, 9, 10, 12, 16})
{
// Look for: 10bit, 10BIT, 10-bit, 10-BIT
if (name.contains(QString("%1bit").arg(bd), Qt::CaseInsensitive) ||
name.contains(QString("%1-bit").arg(bd), Qt::CaseInsensitive))
{
format.bitDepth = bd;
break;
}
// Look for bit depths like: _16b_ .8b. -12b-
QRegularExpression exp(QString("(?:_|\\.|-)%1b(?:_|\\.|-)").arg(bd));
auto match = exp.match(name);
if (match.hasMatch())
{
format.bitDepth = bd;
break;
}
}
}

// If we were able to get a frame size, try to get an indicator for packed formats
if (format.frameSize.isValid())
{
QRegularExpression exp("(?:_|\\.|-)packed(?:_|\\.|-)");
auto match = exp.match(name);
if (match.hasMatch())
format.packed = true;
}
}

return format;
}

// If you are loading a playlist and you have an absolute path and a relative path, this function
// will return the absolute path (if a file with that absolute path exists) or convert the relative
// path to an absolute one and return that (if that file exists). If neither exists the empty string
Expand Down
9 changes: 0 additions & 9 deletions YUViewLib/src/filesource/FileSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,6 @@ class FileSource : public QObject
virtual bool seek(int64_t pos) { return !this->isFileOpened ? false : this->srcFile.seek(pos); }
int64_t pos() { return !this->isFileOpened ? 0 : this->srcFile.pos(); }

struct fileFormat_t
{
Size frameSize;
int frameRate{-1};
unsigned bitDepth{};
bool packed{false};
};
fileFormat_t guessFormatFromFilename() const;

// Get the file size in bytes

// Read the given number of bytes starting at startPos into the QByteArray out
Expand Down
Loading

0 comments on commit 0f4e2bb

Please sign in to comment.