18 changes: 6 additions & 12 deletions mythtv/programs/mythcommflag/PGMConverter.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// POSIX headers
#include <sys/time.h> // for gettimeofday

// Qt headers
#include <QSize>

Expand Down Expand Up @@ -73,11 +70,9 @@ PGMConverter::getImage(const MythVideoFrame *frame, long long _frameno,
int *pwidth, int *pheight)
{
#ifdef PGM_CONVERT_GREYSCALE
struct timeval start {};
struct timeval end {};
struct timeval elapsed {};
std::chrono::microseconds start {0us};
std::chrono::microseconds end {0us};
#endif /* PGM_CONVERT_GREYSCALE */

if (m_frameNo == _frameno)
goto out;

Expand All @@ -88,12 +83,11 @@ PGMConverter::getImage(const MythVideoFrame *frame, long long _frameno,
}

#ifdef PGM_CONVERT_GREYSCALE
(void)gettimeofday(&start, nullptr);
start = nowAsDuration<std::chrono::microseconds>();
if (m_copy->Copy(&m_pgm, frame, m_pgm.data[0], AV_PIX_FMT_GRAY8) < 0)
goto error;
(void)gettimeofday(&end, nullptr);
timersub(&end, &start, &elapsed);
timeradd(&m_convertTime, &elapsed, &m_convertTime);
end = nowAsDuration<std::chrono::microseconds>();
m_convertTime += (end - start);
#else /* !PGM_CONVERT_GREYSCALE */
if (av_image_fill_arrays(m_pgm.data, m_pgm.linesize,
frame->buf, AV_PIX_FMT_GRAY8, m_width, m_height,IMAGE_ALIGN) < 0)
Expand Down Expand Up @@ -123,7 +117,7 @@ PGMConverter::reportTime(void)
if (!m_timeReported)
{
LOG(VB_COMMFLAG, LOG_INFO, QString("PGM Time: convert=%1s")
.arg(strftimeval(&m_convertTime)));
.arg(strftimeval(m_convertTime)));
m_timeReported = true;
}
#endif /* PGM_CONVERT_GREYSCALE */
Expand Down
2 changes: 1 addition & 1 deletion mythtv/programs/mythcommflag/PGMConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class PGMConverter
int m_height {-1}; /* frame dimensions */
AVFrame m_pgm {}; /* grayscale frame */
#ifdef PGM_CONVERT_GREYSCALE
struct timeval m_convertTime {0,0};
std::chrono::microseconds m_convertTime {0us};
bool m_timeReported {false};
MythAVCopy *m_copy {nullptr};
#endif /* PGM_CONVERT_GREYSCALE */
Expand Down
25 changes: 10 additions & 15 deletions mythtv/programs/mythcommflag/PrePostRollFlagger.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <chrono> // for milliseconds
#include <thread> // for sleep_for

#include "PrePostRollFlagger.h"
Expand Down Expand Up @@ -238,9 +237,9 @@ long long PrePostRollFlagger::findBreakInrange(long long startFrame,

while (m_player->GetEof() == kEofStateNone)
{
struct timeval startTime {};
std::chrono::microseconds startTime {0us};
if (m_stillRecording)
gettimeofday(&startTime, nullptr);
startTime = nowAsDuration<std::chrono::microseconds>();

MythVideoFrame* currentFrame = m_player->GetRawVideoFrame();
currentFrameNumber = currentFrame->m_frameNumber;
Expand Down Expand Up @@ -356,28 +355,24 @@ long long PrePostRollFlagger::findBreakInrange(long long startFrame,
m_recordingStartedAt.secsTo(MythDate::current());
int secondsFlagged = (int)(framesProcessed / m_fps);
int secondsBehind = secondsRecorded - secondsFlagged;
long usecPerFrame = (long)(1.0F / m_player->GetFrameRate() * 1000000);
auto usecPerFrame = floatusecs(1000000.0F / m_player->GetFrameRate());

struct timeval endTime {};
gettimeofday(&endTime, nullptr);
auto endTime = nowAsDuration<std::chrono::microseconds>();

long long usecSleep =
usecPerFrame -
(((endTime.tv_sec - startTime.tv_sec) * 1000000) +
(endTime.tv_usec - startTime.tv_usec));
floatusecs usecSleep = usecPerFrame - (endTime - startTime);

if (secondsBehind > requiredBuffer)
{
if (m_fullSpeed)
usecSleep = 0;
usecSleep = 0us;
else
usecSleep = (long)(usecSleep * 0.25);
usecSleep = usecSleep * 0.25;
}
else if (secondsBehind < requiredBuffer)
usecSleep = (long)(usecPerFrame * 1.5);
usecSleep = usecPerFrame * 1.5;

if (usecSleep > 0)
std::this_thread::sleep_for(std::chrono::microseconds(usecSleep));
if (usecSleep > 0us)
std::this_thread::sleep_for(usecSleep);
}

m_player->DiscardVideoFrame(currentFrame);
Expand Down
23 changes: 8 additions & 15 deletions mythtv/programs/mythcommflag/TemplateFinder.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// POSIX headers
#include <sys/time.h> /* gettimeofday */

// ANSI C headers
#include <cmath>
#include <cstdlib>
Expand Down Expand Up @@ -699,7 +696,7 @@ writeTemplate(const QString& tmplfile, const AVFrame *tmpl, const QString& dataf
TemplateFinder::TemplateFinder(std::shared_ptr<PGMConverter> pgmc,
std::shared_ptr<BorderDetector> bd,
std::shared_ptr<EdgeDetector> ed,
MythPlayer *player, int proglen,
MythPlayer *player, std::chrono::seconds proglen,
const QString& debugdir)
: m_pgmConverter(std::move(pgmc))
, m_borderDetector(std::move(bd))
Expand Down Expand Up @@ -728,16 +725,16 @@ TemplateFinder::TemplateFinder(std::shared_ptr<PGMConverter> pgmc,
*
* Sample half of the program length or 20 minutes, whichever is less.
*/
m_sampleTime = std::min(proglen / 2, 20 * 60);
m_sampleTime = std::min(proglen / 2, 20 * 60s);

const float fps = player->GetFrameRate();

m_frameInterval = (int)roundf(m_sampleTime * fps / samplesNeeded);
m_frameInterval = (int)roundf(m_sampleTime.count() * fps / samplesNeeded);
m_endFrame = 0 + (long long)m_frameInterval * samplesNeeded - 1;

LOG(VB_COMMFLAG, LOG_INFO,
QString("TemplateFinder: sampleTime=%1s, samplesNeeded=%2, endFrame=%3")
.arg(m_sampleTime).arg(samplesNeeded).arg(m_endFrame));
.arg(m_sampleTime.count()).arg(samplesNeeded).arg(m_endFrame));

/*
* debugLevel:
Expand Down Expand Up @@ -898,9 +895,6 @@ TemplateFinder::analyzeFrame(const MythVideoFrame *frame, long long frameno,
int cropcol = 0;
int cropwidth = 0;
int cropheight = 0;
struct timeval start {};
struct timeval end {};
struct timeval elapsed {};

if (frameno < m_nextFrame)
{
Expand All @@ -920,7 +914,7 @@ TemplateFinder::analyzeFrame(const MythVideoFrame *frame, long long frameno,
{
/* Not a blank frame. */

(void)gettimeofday(&start, nullptr);
auto start = nowAsDuration<std::chrono::microseconds>();

if (croprow < m_minContentRow)
m_minContentRow = croprow;
Expand Down Expand Up @@ -965,9 +959,8 @@ TemplateFinder::analyzeFrame(const MythVideoFrame *frame, long long frameno,
goto error;
}

(void)gettimeofday(&end, nullptr);
timersub(&end, &start, &elapsed);
timeradd(&m_analyzeTime, &elapsed, &m_analyzeTime);
auto end = nowAsDuration<std::chrono::microseconds>();
m_analyzeTime += (end - start);
}

if (m_nextFrame > m_endFrame)
Expand Down Expand Up @@ -1041,7 +1034,7 @@ TemplateFinder::reportTime(void) const
return -1;

LOG(VB_COMMFLAG, LOG_INFO, QString("TF Time: analyze=%1s")
.arg(strftimeval(&m_analyzeTime)));
.arg(strftimeval(m_analyzeTime)));
return 0;
}

Expand Down
13 changes: 10 additions & 3 deletions mythtv/programs/mythcommflag/TemplateFinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ class TemplateFinder : public FrameAnalyzer
TemplateFinder(std::shared_ptr<PGMConverter> pgmc,
std::shared_ptr<BorderDetector> bd,
std::shared_ptr<EdgeDetector> ed,
MythPlayer *player, int proglen, const QString& debugdir);
MythPlayer *player, std::chrono::seconds proglen,
const QString& debugdir);
TemplateFinder(std::shared_ptr<PGMConverter> pgmc,
std::shared_ptr<BorderDetector> bd,
std::shared_ptr<EdgeDetector> ed,
MythPlayer *player, int proglen, const QString& debugdir) :
TemplateFinder(std::move(pgmc), std::move(bd), std::move(ed),
player, std::chrono::seconds(proglen), debugdir) {};
~TemplateFinder(void) override;

/* FrameAnalyzer interface. */
Expand All @@ -60,7 +67,7 @@ class TemplateFinder : public FrameAnalyzer
std::shared_ptr<BorderDetector> m_borderDetector {nullptr};
std::shared_ptr<EdgeDetector> m_edgeDetector {nullptr};

unsigned int m_sampleTime {20 * 60}; /* amount of time to analyze */
std::chrono::seconds m_sampleTime {20min}; /* amount of time to analyze */
int m_frameInterval; /* analyze every <Interval> frames */
long long m_endFrame {0}; /* end of logo detection */
long long m_nextFrame {0}; /* next desired frame */
Expand Down Expand Up @@ -94,7 +101,7 @@ class TemplateFinder : public FrameAnalyzer
bool m_debugFrames {false};
bool m_tmplValid {false};
bool m_tmplDone {false};
struct timeval m_analyzeTime {0,0};
std::chrono::microseconds m_analyzeTime {0us};
};

#endif /* !TEMPLATEFINDER_H */
Expand Down
17 changes: 6 additions & 11 deletions mythtv/programs/mythcommflag/TemplateMatcher.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// POSIX headers
#include <sys/time.h> /* gettimeofday */

// ANSI C headers
#include <cstdlib>
#include <cmath>
Expand Down Expand Up @@ -439,17 +436,16 @@ TemplateMatcher::analyzeFrame(const MythVideoFrame *frame, long long frameno,
const AVFrame *edges = nullptr;
int pgmwidth = 0;
int pgmheight = 0;
struct timeval start {};
struct timeval end {};
struct timeval elapsed {};
std::chrono::microseconds start {0us};
std::chrono::microseconds end {0us};

*pNextFrame = kNextFrame;

const AVFrame *pgm = m_pgmConverter->getImage(frame, frameno, &pgmwidth, &pgmheight);
if (pgm == nullptr)
goto error;

(void)gettimeofday(&start, nullptr);
start = nowAsDuration<std::chrono::microseconds>();

if (pgm_crop(&m_cropped, pgm, pgmheight, m_tmplRow, m_tmplCol,
m_tmplWidth, m_tmplHeight))
Expand All @@ -462,9 +458,8 @@ TemplateMatcher::analyzeFrame(const MythVideoFrame *frame, long long frameno,
if (pgm_match(m_tmpl, edges, m_tmplHeight, JITTER_RADIUS, &m_matches[frameno]))
goto error;

(void)gettimeofday(&end, nullptr);
timersub(&end, &start, &elapsed);
timeradd(&m_analyzeTime, &elapsed, &m_analyzeTime);
end = nowAsDuration<std::chrono::microseconds>();
m_analyzeTime += (end - start);

return ANALYZE_OK;

Expand Down Expand Up @@ -581,7 +576,7 @@ TemplateMatcher::reportTime(void) const
return -1;

LOG(VB_COMMFLAG, LOG_INFO, QString("TM Time: analyze=%1s")
.arg(strftimeval(&m_analyzeTime)));
.arg(strftimeval(m_analyzeTime)));
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion mythtv/programs/mythcommflag/TemplateMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class TemplateMatcher : public FrameAnalyzer
bool m_debugMatches {false};
bool m_debugRemoveRunts {false};
bool m_matchesDone {false};
struct timeval m_analyzeTime {0,0};
std::chrono::microseconds m_analyzeTime {0us};
};

#endif /* !TEMPLATEMATCHER_H */
Expand Down
1 change: 0 additions & 1 deletion mythtv/programs/mythcommflag/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

// POSIX headers
#include <unistd.h>
#include <sys/time.h> // for gettimeofday

// ANSI C headers
#include <cstdlib>
Expand Down