Skip to content

Commit

Permalink
Merge pull request #784 from ElderOrb/audio_data_alignment
Browse files Browse the repository at this point in the history
Audio data alignment
  • Loading branch information
dericed authored Aug 26, 2023
2 parents 0798b69 + fef460e commit 74fdec1
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Project/QtCreator/qctools-QtAVPlayer
10 changes: 8 additions & 2 deletions Source/Cli/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Core/CommonStats.h"
#include "Core/FileInformation.h"
#include <QDir>
#include <Core/logging.h>

Cli::Cli() : indexOfStreamWithKnownFrameCount(0), statsFileBytesWritten(0), statsFileBytesTotal(0), statsFileBytesUploaded(0), statsFileBytesToUpload(0)
{
Expand All @@ -15,6 +16,7 @@ int Cli::exec(QCoreApplication &a)
std::string appName = "qcli";
std::string copyright = "Copyright (C): 2013-2020, BAVC.\nCopyright (C): 2018-2020, RiceCapades LLC & MediaArea.net SARL.";
Preferences prefs;
Logging logging;

QString input;
QString output;
Expand Down Expand Up @@ -53,6 +55,9 @@ int Cli::exec(QCoreApplication &a)
} else if(a.arguments().at(i) == "-u")
{
uploadToSignalServer = true;
} else if(a.arguments().at(i) == "--log")
{
logging.enable();
} else if(a.arguments().at(i) == "-uf")
{
forceUploadToSignalServer = true;
Expand Down Expand Up @@ -567,10 +572,11 @@ int Cli::exec(QCoreApplication &a)

bool mkvReport = output.endsWith(".qctools.mkv");
bool xmlGzReport = output.endsWith(".xml.gz");
bool xmlReport = output.endsWith(".xml");

if(!output.isEmpty() && !xmlGzReport && !mkvReport)
if(!output.isEmpty() && !xmlGzReport && !mkvReport && !xmlReport)
{
std::cout << "warning: non-standard extension (not *.xml.gz) has been specified for output file. " << std::endl;
std::cout << "warning: non-standard extension (not *qctools.mkv, *.xml.gz or *.xml) has been specified for output file. " << std::endl;
}

QFile file(output);
Expand Down
2 changes: 2 additions & 0 deletions Source/Cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ void messageHandler(QtMsgType type, const QMessageLogContext &context, const QSt

int main(int argc, char *argv[])
{
qputenv("QT_AVPLAYER_NO_HWDEVICE", "1");

qInstallMessageHandler(messageHandler);
QCoreApplication a(argc, argv);

Expand Down
11 changes: 9 additions & 2 deletions Source/Core/AudioStats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,9 @@ void AudioStats::parseFrame(tinyxml2::XMLElement *Frame)
//***************************************************************************

//---------------------------------------------------------------------------
void AudioStats::StatsFromFrame (struct AVFrame* Frame, int, int)
void AudioStats::StatsFromFrame (const QAVFrame& frame, int, int)
{
auto Frame = frame.frame();
AVDictionary * m= Frame->metadata;
AVDictionaryEntry* e=NULL;
bool statsMapInitialized = !statsValueInfoByKeys.empty();
Expand Down Expand Up @@ -249,8 +250,10 @@ void AudioStats::StatsFromFrame (struct AVFrame* Frame, int, int)
}

//---------------------------------------------------------------------------
void AudioStats::TimeStampFromFrame (struct AVFrame* Frame, size_t FramePos)
void AudioStats::TimeStampFromFrame (const QAVFrame& frame, size_t FramePos)
{
auto Frame = frame.frame();

if (Frequency==0)
return; // Not supported

Expand All @@ -259,9 +262,13 @@ void AudioStats::TimeStampFromFrame (struct AVFrame* Frame, size_t FramePos)

x[0][FramePos]=FramePos;

/* can't use old way, take pts directly from frame
int64_t ts=(Frame->pts == AV_NOPTS_VALUE)?Frame->pkt_dts : Frame->pts; // Using DTS is PTS is not available
if (ts==AV_NOPTS_VALUE && FramePos)
ts=(int64_t)((FirstTimeStamp+x[1][FramePos-1]+durations[FramePos-1])*Frequency); // If time stamp is not present, creating a fake one from last frame duration
*/

int64_t ts = frame.pts() * 1000;
if (ts!=AV_NOPTS_VALUE)
{
if (FirstTimeStamp==DBL_MAX)
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/AudioStats.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class AudioStats : public CommonStats

// External data
virtual void parseFrame(tinyxml2::XMLElement* frame);
void StatsFromFrame(struct AVFrame* Frame, int Width, int Height);
void TimeStampFromFrame(struct AVFrame* Frame, size_t FramePos);
void StatsFromFrame(const QAVFrame& Frame, int Width, int Height);
void TimeStampFromFrame(const QAVFrame& Frame, size_t FramePos);
std::string StatsToXML(const activefilters& filters);
};

Expand Down
5 changes: 3 additions & 2 deletions Source/Core/CommonStats.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace tinyxml2 {
class XMLElement;
}

class QAVFrame;
class CommonStats
{
public:
Expand Down Expand Up @@ -70,8 +71,8 @@ class CommonStats

// External data
void StatsFromExternalData_Finish() {Frequency=1; StatsFinish();}
virtual void StatsFromFrame(struct AVFrame* Frame, int Width, int Height) = 0;
virtual void TimeStampFromFrame(struct AVFrame* Frame, size_t FramePos) = 0;
virtual void StatsFromFrame(const QAVFrame& Frame, int Width, int Height) = 0;
virtual void TimeStampFromFrame(const QAVFrame& Frame, size_t FramePos) = 0;
virtual void StatsFinish();
virtual std::string StatsToXML(const activefilters& filters) = 0;

Expand Down
28 changes: 21 additions & 7 deletions Source/Core/FileInformation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,7 @@ FileInformation::FileInformation (SignalServer* signalServer, const QString &Fil
QEventLoop loop;
QMetaObject::Connection c;
c = connect(m_mediaParser, &QAVPlayer::mediaStatusChanged, this, [&, this]() {
qDebug() << "m_mediaParser status after loading: " << m_mediaParser->mediaStatus();
loop.exit();
QObject::disconnect(c);
});
Expand Down Expand Up @@ -983,15 +984,18 @@ FileInformation::FileInformation (SignalServer* signalServer, const QString &Fil
}
}

for(auto& filter : filters) {
qDebug() << "applying filters: " << filter;
}
m_mediaParser->setFilters(filters);

QObject::connect(m_mediaParser, &QAVPlayer::audioFrame, m_mediaParser, [this](const QAVAudioFrame &frame) {
qDebug() << "audio frame came from: " << frame.filterName() << frame.stream() << frame.stream().index();

auto stat = Stats[frame.stream().index()];

stat->TimeStampFromFrame(frame.frame(), stat->x_Current);
stat->StatsFromFrame(frame.frame(), 0, 0);
stat->TimeStampFromFrame(frame, stat->x_Current);
stat->StatsFromFrame(frame, 0, 0);

},
// Qt::QueuedConnection
Expand All @@ -1004,8 +1008,8 @@ FileInformation::FileInformation (SignalServer* signalServer, const QString &Fil
if(frame.filterName() == "stats") {
auto stat = Stats[frame.stream().index()];

stat->TimeStampFromFrame(frame.frame(), stat->x_Current);
stat->StatsFromFrame(frame.frame(), frame.size().width(), frame.size().height());
stat->TimeStampFromFrame(frame, stat->x_Current);
stat->StatsFromFrame(frame, frame.size().width(), frame.size().height());

} else if(frame.filterName().startsWith(panelOutputPrefix)) {
auto indexString = frame.filterName().mid(panelOutputPrefix.length());
Expand All @@ -1028,6 +1032,8 @@ FileInformation::FileInformation (SignalServer* signalServer, const QString &Fil
);

QObject::connect(m_mediaParser, &QAVPlayer::mediaStatusChanged, [this](QAVPlayer::MediaStatus status) {
qDebug() << "m_mediaParser => mediaStatusChanged: " << status;

if(status == QAVPlayer::EndOfMedia) {

for (size_t Pos=0; Pos<Stats.size(); Pos++)
Expand All @@ -1037,6 +1043,10 @@ FileInformation::FileInformation (SignalServer* signalServer, const QString &Fil
m_parsed = true;
Q_EMIT parsingCompleted(true);
}
else if(status == QAVPlayer::InvalidMedia)
{
Q_EMIT parsingCompleted(false);
}
});

QObject::connect(m_mediaParser, &QAVPlayer::stateChanged, [this](QAVPlayer::State state) {
Expand Down Expand Up @@ -1245,13 +1255,13 @@ void FileInformation::Export_XmlGz (const QString &ExportFileName, const activef

if(file->open(QIODevice::ReadWrite))
{
if(name.endsWith(".qctools.xml"))
if (name.endsWith(".qctools.xml"))
{
auto bytesLeft = Data.str().size();
auto writePtr = DataS.c_str();
auto totalBytesWritten = 0;

while(bytesLeft) {
while (bytesLeft) {
auto bytesToWrite = std::min(size_t(Buffer_Size), bytesLeft);
auto bytesWritten = file->write(writePtr, bytesToWrite);
totalBytesWritten += bytesWritten;
Expand All @@ -1261,9 +1271,13 @@ void FileInformation::Export_XmlGz (const QString &ExportFileName, const activef
writePtr += bytesToWrite;
bytesLeft -= bytesWritten;

if(bytesWritten != bytesToWrite)
if (bytesWritten != bytesToWrite)
break;
}
}
else if (name.endsWith(".xml"))
{
file->write(DataS.c_str(), DataS.length());
} else {
char* Buffer=new char[Buffer_Size];
z_stream strm;
Expand Down
7 changes: 5 additions & 2 deletions Source/Core/VideoStats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,9 @@ void VideoStats::parseFrame(tinyxml2::XMLElement *Frame)

//---------------------------------------------------------------------------

void VideoStats::StatsFromFrame (struct AVFrame* Frame, int Width, int Height)
void VideoStats::StatsFromFrame (const QAVFrame& frame, int Width, int Height)
{
auto Frame = frame.frame();
AVDictionary * m= Frame->metadata;
AVDictionaryEntry* e=NULL;
bool statsMapInitialized = !statsValueInfoByKeys.empty();
Expand Down Expand Up @@ -368,8 +369,10 @@ void VideoStats::StatsFromFrame (struct AVFrame* Frame, int Width, int Height)
}

//---------------------------------------------------------------------------
void VideoStats::TimeStampFromFrame (struct AVFrame* Frame, size_t FramePos)
void VideoStats::TimeStampFromFrame (const QAVFrame& frame, size_t FramePos)
{
auto Frame = frame.frame();

if (Frequency==0)
return; // Not supported

Expand Down
4 changes: 2 additions & 2 deletions Source/Core/VideoStats.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class VideoStats : public CommonStats

// External data
virtual void parseFrame(tinyxml2::XMLElement* frame);
void StatsFromFrame(struct AVFrame* Frame, int Width, int Height);
void TimeStampFromFrame(struct AVFrame* Frame, size_t FramePos);
void StatsFromFrame(const QAVFrame& Frame, int Width, int Height);
void TimeStampFromFrame(const QAVFrame& Frame, size_t FramePos);
std::string StatsToXML(const activefilters& filters);

int getWidth() const;
Expand Down
2 changes: 2 additions & 0 deletions Source/GUI/mainwindow_More.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,8 @@ void MainWindow::addFile(const QString &FileName)
if (FileName.isEmpty())
return;

qDebug() << "addFile: " << FileName;

// Launch analysis
FileInformation* Temp=new FileInformation(signalServer, FileName, Prefs->ActiveFilters, Prefs->ActiveAllTracks, preferences->getActivePanels(), preferences->createQCvaultFileNameString(FileName));
if (!Temp->isValid() && !Temp->hasStats())
Expand Down

0 comments on commit 74fdec1

Please sign in to comment.