Skip to content

Commit

Permalink
Many more refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianFeldmann committed Sep 28, 2023
1 parent 3f1c7c7 commit 8239cb1
Show file tree
Hide file tree
Showing 36 changed files with 513 additions and 480 deletions.
18 changes: 8 additions & 10 deletions YUViewLib/src/common/FileInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,18 @@

#include "FileInfo.h"

std::vector<InfoItem> InfoItem::fromFFmpegLibraryPaths(const FFmpeg::LibraryPaths &paths)
std::vector<InfoItem>
InfoItem::fromFFmpegLibrariesInfo(const std::vector<FFmpeg::LibraryInfo> &librariesInfo)
{
std::vector<InfoItem> items;

auto addItem = [&items](QString name, const std::filesystem::path &path) {
const auto text = QString::fromStdString(path.filename().string());
const auto tooltip = QString::fromStdString(path.string());
for (const auto &libInfo : librariesInfo)
{
const auto name = QString::fromStdString(libInfo.name);
const auto text = QString::fromStdString(libInfo.path.filename().string());
const auto tooltip = QString::fromStdString(libInfo.path.string());
items.push_back(InfoItem(name, text, tooltip));
};

addItem("AVFormat", paths.avFormat);
addItem("AVCodec", paths.avCodec);
addItem("AVUtil", paths.avUtil);
addItem("SWResample", paths.swResample);
}

return items;
}
3 changes: 2 additions & 1 deletion YUViewLib/src/common/FileInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ struct InfoItem
{
}

static std::vector<InfoItem> fromFFmpegLibraryPaths(const FFmpeg::LibraryPaths &paths);
static std::vector<InfoItem>
fromFFmpegLibrariesInfo(const std::vector<FFmpeg::LibraryInfo> &librariesInfo);

QString name{};
QString text{};
Expand Down
43 changes: 19 additions & 24 deletions YUViewLib/src/decoder/decoderFFmpeg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ decoderFFmpeg::decoderFFmpeg(FFmpeg::AVCodecParametersWrapper codecpar, bool cac
return;

auto codecID = this->ff.getCodecIDWrapper(codecpar.getCodecID());
this->codecName = codecID.getCodecName();
this->codecName = QString::fromStdString(codecID.getCodecName());
if (!this->createDecoder(codecID, codecpar))
{
this->setError("Error creating the needed decoder.");
Expand Down Expand Up @@ -269,29 +269,25 @@ void decoderFFmpeg::copyCurImageToBuffer()

void decoderFFmpeg::cacheCurStatistics()
{
// Copy the statistics of the current frame to the buffer
DEBUG_FFMPEG("decoderFFmpeg::cacheCurStatistics");

// Try to get the motion information
auto sideData = this->ff.getSideData(frame, FFmpeg::AV_FRAME_DATA_MOTION_VECTORS);
if (sideData)
const auto sideData = this->ff.getSideData(frame, FFmpeg::AV_FRAME_DATA_MOTION_VECTORS);
if (!sideData)
return;

const auto motionVectors = sideData.getMotionVectors();
for (const auto &motionVector : motionVectors)
{
const auto nrMVs = sideData.getNumberMotionVectors();
for (size_t i = 0; i < nrMVs; i++)
{
auto mvs = sideData.getMotionVector(unsigned(i));

// dst marks the center of the current block so the block position is:
const int blockX = mvs.dst_x - mvs.w / 2;
const int blockY = mvs.dst_y - mvs.h / 2;
const int16_t mvX = mvs.dst_x - mvs.src_x;
const int16_t mvY = mvs.dst_y - mvs.src_y;

this->statisticsData->at(mvs.source < 0 ? 0 : 1)
.addBlockValue(blockX, blockY, mvs.w, mvs.h, (int)mvs.source);
this->statisticsData->at(mvs.source < 0 ? 2 : 3)
.addBlockVector(blockX, blockY, mvs.w, mvs.h, mvX, mvY);
}
// dst marks the center of the current block so the block position is:
const int blockX = motionVector.dst_x - motionVector.w / 2;
const int blockY = motionVector.dst_y - motionVector.h / 2;
const int16_t mvX = motionVector.dst_x - motionVector.src_x;
const int16_t mvY = motionVector.dst_y - motionVector.src_y;

this->statisticsData->at(motionVector.source < 0 ? 0 : 1)
.addBlockValue(blockX, blockY, motionVector.w, motionVector.h, (int)motionVector.source);
this->statisticsData->at(motionVector.source < 0 ? 2 : 3)
.addBlockVector(blockX, blockY, motionVector.w, motionVector.h, mvX, mvY);
}
}

Expand Down Expand Up @@ -321,8 +317,7 @@ bool decoderFFmpeg::pushData(QByteArray &data)

std::vector<InfoItem> decoderFFmpeg::getDecoderInfo() const
{
const auto libraryPaths = this->ff.getLibraryPaths();
return InfoItem::fromFFmpegLibraryPaths(libraryPaths);
return InfoItem::fromFFmpegLibrariesInfo(this->ff.getLibrariesInfo());
}

bool decoderFFmpeg::pushAVPacket(FFmpeg::AVPacketWrapper &pkt)
Expand Down Expand Up @@ -450,7 +445,7 @@ bool decoderFFmpeg::createDecoder(FFmpeg::AVCodecIDWrapper codecID,
this->videoCodec = this->ff.findDecoder(codecID);
if (!this->videoCodec)
return this->setErrorB(QStringLiteral("Could not find a video decoder for the given codec ") +
codecID.getCodecName());
QString::fromStdString(codecID.getCodecName()));

if (this->decCtx)
return this->setErrorB(QStringLiteral("Decoder context already allocated."));
Expand Down
16 changes: 9 additions & 7 deletions YUViewLib/src/ffmpeg/AVCodecContextWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,11 @@ AVCodecContextWrapper::AVCodecContextWrapper()
this->codec = nullptr;
}

AVCodecContextWrapper::AVCodecContextWrapper(AVCodecContext *c, LibraryVersion v)
AVCodecContextWrapper::AVCodecContextWrapper(AVCodecContext * c,
const LibraryVersions &libraryVersions)
{
this->codec = c;
this->libVer = v;
this->codec = c;
this->libraryVersions = libraryVersions;
this->update();
}

Expand Down Expand Up @@ -478,7 +479,7 @@ void AVCodecContextWrapper::update()
if (this->codec == nullptr)
return;

if (this->libVer.avcodec.major == 56)
if (this->libraryVersions.avcodec.major == 56)
{
auto p = reinterpret_cast<AVCodecContext_56 *>(this->codec);
this->codec_type = p->codec_type;
Expand Down Expand Up @@ -562,7 +563,7 @@ void AVCodecContextWrapper::update()
this->color_range = p->color_range;
this->chroma_sample_location = p->chroma_sample_location;
}
else if (libVer.avcodec.major == 57)
else if (this->libraryVersions.avcodec.major == 57)
{
auto p = reinterpret_cast<AVCodecContext_57 *>(this->codec);
this->codec_type = p->codec_type;
Expand Down Expand Up @@ -646,7 +647,7 @@ void AVCodecContextWrapper::update()
this->color_range = p->color_range;
this->chroma_sample_location = p->chroma_sample_location;
}
else if (libVer.avcodec.major == 58)
else if (this->libraryVersions.avcodec.major == 58)
{
auto p = reinterpret_cast<AVCodecContext_58 *>(this->codec);
this->codec_type = p->codec_type;
Expand Down Expand Up @@ -730,7 +731,8 @@ void AVCodecContextWrapper::update()
this->color_range = p->color_range;
this->chroma_sample_location = p->chroma_sample_location;
}
else if (libVer.avcodec.major == 59 || libVer.avcodec.major == 60)
else if (this->libraryVersions.avcodec.major == 59 || //
this->libraryVersions.avcodec.major == 60)
{
auto p = reinterpret_cast<AVCodecContext_59_60 *>(this->codec);
this->codec_type = p->codec_type;
Expand Down
4 changes: 2 additions & 2 deletions YUViewLib/src/ffmpeg/AVCodecContextWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class AVCodecContextWrapper
{
public:
AVCodecContextWrapper();
AVCodecContextWrapper(AVCodecContext *c, LibraryVersion v);
AVCodecContextWrapper(AVCodecContext *c, const LibraryVersions &libraryVersions);

explicit operator bool() const { return this->codec != nullptr; };

Expand Down Expand Up @@ -142,7 +142,7 @@ class AVCodecContextWrapper
AVChromaLocation chroma_sample_location{};

AVCodecContext *codec{};
LibraryVersion libVer{};
LibraryVersions libraryVersions{};
};

} // namespace FFmpeg
15 changes: 9 additions & 6 deletions YUViewLib/src/ffmpeg/AVCodecIDWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ class AVCodecIDWrapper
{
public:
AVCodecIDWrapper() {}
AVCodecIDWrapper(AVCodecID codecID, QString codecName) : codecID(codecID), codecName(codecName) {}
AVCodecIDWrapper(AVCodecID codecID, std::string codecName)
: codecID(codecID), codecName(codecName)
{
}

QString getCodecName() const { return this->codecName; }
AVCodecID getCodecID() const { return this->codecID; }
std::string getCodecName() const { return this->codecName; }
AVCodecID getCodecID() const { return this->codecID; }

void setCodecID(AVCodecID id) { this->codecID = id; }

Expand All @@ -58,15 +61,15 @@ class AVCodecIDWrapper

bool isNone() const
{
return this->codecName.isEmpty() || this->codecName == "unknown_codec" ||
return this->codecName.empty() || this->codecName == "unknown_codec" ||
this->codecName == "none";
}

bool operator==(const AVCodecIDWrapper &a) const { return codecID == a.codecID; }

private:
AVCodecID codecID{AV_CODEC_ID_NONE};
QString codecName;
AVCodecID codecID{AV_CODEC_ID_NONE};
std::string codecName;
};

} // namespace FFmpeg
63 changes: 41 additions & 22 deletions YUViewLib/src/ffmpeg/AVCodecParametersWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ typedef struct AVCodecParameters_57_58_59_60

} // namespace

AVCodecParametersWrapper::AVCodecParametersWrapper(AVCodecParameters *p, LibraryVersion v)
AVCodecParametersWrapper::AVCodecParametersWrapper(AVCodecParameters * p,
const LibraryVersions &libraryVersionsv)
{
this->param = p;
this->libVer = v;
this->param = p;
this->libraryVersions = libraryVersions;
this->update();
}

Expand Down Expand Up @@ -229,8 +230,10 @@ QStringPairList AVCodecParametersWrapper::getInfoText()

void AVCodecParametersWrapper::setClearValues()
{
if (this->libVer.avformat.major == 57 || this->libVer.avformat.major == 58 ||
this->libVer.avformat.major == 59 || this->libVer.avformat.major == 60)
if (this->libraryVersions.avformat.major == 57 || //
this->libraryVersions.avformat.major == 58 || //
this->libraryVersions.avformat.major == 59 || //
this->libraryVersions.avformat.major == 60)
{
auto p = reinterpret_cast<AVCodecParameters_57_58_59_60 *>(this->param);
p->codec_type = AVMEDIA_TYPE_UNKNOWN;
Expand Down Expand Up @@ -265,8 +268,10 @@ void AVCodecParametersWrapper::setClearValues()

void AVCodecParametersWrapper::setAVMediaType(AVMediaType type)
{
if (this->libVer.avformat.major == 57 || this->libVer.avformat.major == 58 ||
this->libVer.avformat.major == 59 || this->libVer.avformat.major == 60)
if (this->libraryVersions.avformat.major == 57 || //
this->libraryVersions.avformat.major == 58 || //
this->libraryVersions.avformat.major == 59 || //
this->libraryVersions.avformat.major == 60)
{
auto p = reinterpret_cast<AVCodecParameters_57_58_59_60 *>(this->param);
p->codec_type = type;
Expand All @@ -276,8 +281,10 @@ void AVCodecParametersWrapper::setAVMediaType(AVMediaType type)

void AVCodecParametersWrapper::setAVCodecID(AVCodecID id)
{
if (this->libVer.avformat.major == 57 || this->libVer.avformat.major == 58 ||
this->libVer.avformat.major == 59 || this->libVer.avformat.major == 60)
if (this->libraryVersions.avformat.major == 57 || //
this->libraryVersions.avformat.major == 58 || //
this->libraryVersions.avformat.major == 59 || //
this->libraryVersions.avformat.major == 60)
{
auto p = reinterpret_cast<AVCodecParameters_57_58_59_60 *>(this->param);
p->codec_id = id;
Expand All @@ -287,8 +294,10 @@ void AVCodecParametersWrapper::setAVCodecID(AVCodecID id)

void AVCodecParametersWrapper::setExtradata(QByteArray data)
{
if (this->libVer.avformat.major == 57 || this->libVer.avformat.major == 58 ||
this->libVer.avformat.major == 59 || this->libVer.avformat.major == 60)
if (this->libraryVersions.avformat.major == 57 || //
this->libraryVersions.avformat.major == 58 || //
this->libraryVersions.avformat.major == 59 || //
this->libraryVersions.avformat.major == 60)
{
this->extradata = data;
auto p = reinterpret_cast<AVCodecParameters_57_58_59_60 *>(this->param);
Expand All @@ -299,8 +308,10 @@ void AVCodecParametersWrapper::setExtradata(QByteArray data)

void AVCodecParametersWrapper::setSize(Size size)
{
if (this->libVer.avformat.major == 57 || this->libVer.avformat.major == 58 ||
this->libVer.avformat.major == 59 || this->libVer.avformat.major == 60)
if (this->libraryVersions.avformat.major == 57 || //
this->libraryVersions.avformat.major == 58 || //
this->libraryVersions.avformat.major == 59 || //
this->libraryVersions.avformat.major == 60)
{
auto p = reinterpret_cast<AVCodecParameters_57_58_59_60 *>(this->param);
p->width = size.width;
Expand All @@ -312,8 +323,10 @@ void AVCodecParametersWrapper::setSize(Size size)

void AVCodecParametersWrapper::setAVPixelFormat(AVPixelFormat format)
{
if (this->libVer.avformat.major == 57 || this->libVer.avformat.major == 58 ||
this->libVer.avformat.major == 59 || this->libVer.avformat.major == 60)
if (this->libraryVersions.avformat.major == 57 || //
this->libraryVersions.avformat.major == 58 || //
this->libraryVersions.avformat.major == 59 || //
this->libraryVersions.avformat.major == 60)
{
auto p = reinterpret_cast<AVCodecParameters_57_58_59_60 *>(this->param);
p->format = format;
Expand All @@ -323,8 +336,10 @@ void AVCodecParametersWrapper::setAVPixelFormat(AVPixelFormat format)

void AVCodecParametersWrapper::setProfileLevel(int profile, int level)
{
if (this->libVer.avformat.major == 57 || this->libVer.avformat.major == 58 ||
this->libVer.avformat.major == 59 || this->libVer.avformat.major == 60)
if (this->libraryVersions.avformat.major == 57 || //
this->libraryVersions.avformat.major == 58 || //
this->libraryVersions.avformat.major == 59 || //
this->libraryVersions.avformat.major == 60)
{
auto p = reinterpret_cast<AVCodecParameters_57_58_59_60 *>(this->param);
p->profile = profile;
Expand All @@ -336,8 +351,10 @@ void AVCodecParametersWrapper::setProfileLevel(int profile, int level)

void AVCodecParametersWrapper::setSampleAspectRatio(int num, int den)
{
if (this->libVer.avformat.major == 57 || this->libVer.avformat.major == 58 ||
this->libVer.avformat.major == 59 || this->libVer.avformat.major == 60)
if (this->libraryVersions.avformat.major == 57 || //
this->libraryVersions.avformat.major == 58 || //
this->libraryVersions.avformat.major == 59 || //
this->libraryVersions.avformat.major == 60)
{
auto p = reinterpret_cast<AVCodecParameters_57_58_59_60 *>(param);
AVRational ratio;
Expand All @@ -353,13 +370,15 @@ void AVCodecParametersWrapper::update()
if (this->param == nullptr)
return;

if (this->libVer.avformat.major == 56)
if (this->libraryVersions.avformat.major == 56)
{
// This data structure does not exist in avformat major version 56.
this->param = nullptr;
}
else if (this->libVer.avformat.major == 57 || this->libVer.avformat.major == 58 ||
this->libVer.avformat.major == 59 || this->libVer.avformat.major == 60)
else if (this->libraryVersions.avformat.major == 57 || //
this->libraryVersions.avformat.major == 58 || //
this->libraryVersions.avformat.major == 59 || //
this->libraryVersions.avformat.major == 60)
{
auto p = reinterpret_cast<AVCodecParameters_57_58_59_60 *>(this->param);

Expand Down
4 changes: 2 additions & 2 deletions YUViewLib/src/ffmpeg/AVCodecParametersWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class AVCodecParametersWrapper
{
public:
AVCodecParametersWrapper() = default;
AVCodecParametersWrapper(AVCodecParameters *p, LibraryVersion v);
AVCodecParametersWrapper(AVCodecParameters *p, const LibraryVersions &libraryVersions);
explicit operator bool() const { return this->param != nullptr; }
QStringPairList getInfoText();

Expand Down Expand Up @@ -93,7 +93,7 @@ class AVCodecParametersWrapper
int video_delay{};

AVCodecParameters *param{};
LibraryVersion libVer{};
LibraryVersions libraryVersions{};
};

} // namespace FFmpeg
Loading

0 comments on commit 8239cb1

Please sign in to comment.