MythTV  master
SceneChangeDetector.cpp
Go to the documentation of this file.
1 // ANSI C headers
2 #include <cmath>
3 #include <cstdlib>
4 #include <utility>
5 
6 // MythTV headers
7 #include "mythcorecontext.h" /* gContext */
8 #include "mythplayer.h"
9 #include "mythlogging.h"
10 
11 // Commercial Flagging headers
12 #include "CommDetector2.h"
13 #include "FrameAnalyzer.h"
14 #include "quickselect.h"
15 #include "HistogramAnalyzer.h"
16 #include "SceneChangeDetector.h"
17 
18 using namespace commDetector2;
19 using namespace frameAnalyzer;
20 
21 namespace {
22 
23 int
24 scenechange_data_sort_desc_frequency(const void *aa, const void *bb)
25 {
26  /* Descending by frequency, then ascending by color. */
27  const auto *sc1 = (const struct SceneChangeDetector::scenechange_data*)aa;
28  const auto *sc2 = (const struct SceneChangeDetector::scenechange_data*)bb;
29  int freqdiff = sc2->frequency - sc1->frequency;
30  return freqdiff ? freqdiff : sc1->color - sc2->color;
31 }
32 
33 void
34 scenechange_data_init(SceneChangeDetector::SceneChangeData *scdata,
36 {
37  unsigned int ncolors = hh->size();
38 
39  for (unsigned int ii = 0; ii < ncolors; ii++)
40  {
41  (*scdata)[ii].color = ii;
42  (*scdata)[ii].frequency = (*hh)[ii];
43  }
44  qsort(scdata->data(), scdata->size(), sizeof(SceneChangeDetector::scenechange_data),
45  scenechange_data_sort_desc_frequency);
46 }
47 
49 scenechange_data_diff(const SceneChangeDetector::SceneChangeData *sc1,
51 {
52  /*
53  * Compute a notion of "difference" that takes into account the difference
54  * in relative frequencies of the dominant colors.
55  */
56  uint16_t diff = 0;
57  for (size_t ii = 0; ii < sizeof(*sc1)/sizeof((*sc1)[0]); ii++)
58  diff += abs((*sc1)[ii].frequency - (*sc2)[ii].frequency) +
59  abs((*sc1)[ii].color - (*sc2)[ii].color);
60  return diff;
61 }
62 
63 bool
64 writeData(const QString& filename, const std::vector<uint16_t>& scdiff)
65 {
66  QByteArray fname = filename.toLocal8Bit();
67  FILE *fp = fopen(fname.constData(), "w");
68  if (fp == nullptr)
69  return false;
70  for (auto value : scdiff)
71  (void)fprintf(fp, "%5u\n", value);
72  if (fclose(fp))
73  LOG(VB_COMMFLAG, LOG_ERR, QString("Error closing %1: %2")
74  .arg(filename).arg(strerror(errno)));
75  return true;
76 }
77 
78 void
79 computeChangeMap(FrameAnalyzer::FrameMap *changeMap,
80  const std::vector<uint16_t>& scdiff, uint16_t mindiff)
81 {
82  /*
83  * Look for sudden changes in histogram.
84  */
85  changeMap->clear();
86  for (size_t frameno = 0; frameno < scdiff.size(); frameno++)
87  {
88  if (scdiff[frameno] > mindiff)
89  changeMap->insert(frameno, 0);
90  }
91 }
92 
93 }; /* namespace */
94 
95 SceneChangeDetector::SceneChangeDetector(std::shared_ptr<HistogramAnalyzer> ha,
96  const QString& debugdir)
97  : m_histogramAnalyzer(std::move(ha))
98  , m_debugData(debugdir + "/SceneChangeDetector.txt")
99 {
100  LOG(VB_COMMFLAG, LOG_INFO, "SceneChangeDetector");
101 
102  /*
103  * debugLevel:
104  * 0: no debugging
105  * 2: extra verbosity [O(nframes)]
106  */
107  m_debugLevel = gCoreContext->GetNumSetting("SceneChangeDetectorDebugLevel", 0);
108 
109  if (m_debugLevel >= 1)
110  {
111  createDebugDirectory(debugdir,
112  QString("SceneChangeDetector debugLevel %1").arg(m_debugLevel));
113  m_debugSceneChange = true;
114  }
115 }
116 
117 
120  long long nframes)
121 {
123  m_histogramAnalyzer->MythPlayerInited(player, nframes);
124 
125  m_fps = player->GetFrameRate();
126 
127  m_scData.resize(nframes);
128  m_scDiff.resize(nframes);
129 
130  QSize video_disp_dim = player->GetVideoSize();
131 
132  LOG(VB_COMMFLAG, LOG_INFO,
133  QString("SceneChangeDetector::MythPlayerInited %1x%2")
134  .arg(video_disp_dim.width())
135  .arg(video_disp_dim.height()));
136 
137  return ares;
138 }
139 
141 SceneChangeDetector::analyzeFrame(const MythVideoFrame *frame, long long frameno,
142  long long *pNextFrame)
143 {
144  *pNextFrame = kNextFrame;
145 
146  if (m_histogramAnalyzer->analyzeFrame(frame, frameno) ==
148  return ANALYZE_OK;
149 
150  LOG(VB_COMMFLAG, LOG_ERR,
151  QString("SceneChangeDetector::analyzeFrame error at frame %1")
152  .arg(frameno));
153  return ANALYZE_ERROR;
154 }
155 
156 int
157 SceneChangeDetector::finished(long long nframes, bool final)
158 {
159  if (m_histogramAnalyzer->finished(nframes, final))
160  return -1;
161 
162  LOG(VB_COMMFLAG, LOG_INFO, QString("SceneChangeDetector::finished(%1)")
163  .arg(nframes));
164 
165  const HistogramAnalyzer::Histogram *histogram =
166  m_histogramAnalyzer->getHistograms();
167  for (long long frameno = 0; frameno < nframes; frameno++)
168  scenechange_data_init(&m_scData[frameno], &histogram[frameno]);
169  m_scDiff[0] = 0;
170  for (long long frameno = 1; frameno < nframes; frameno++)
171  m_scDiff[frameno] = scenechange_data_diff(&m_scData[frameno - 1],
172  &m_scData[frameno]);
173 
175  {
176  if (final && writeData(m_debugData, m_scDiff))
177  {
178  LOG(VB_COMMFLAG, LOG_INFO,
179  QString("SceneChangeDetector::finished wrote %1")
180  .arg(m_debugData));
181  m_sceneChangeDone = true;
182  }
183  }
184 
185  /* Identify all scene-change frames (changeMap). */
186  std::vector<uint16_t> scdiffsort = m_scDiff;
187  uint16_t mindiff = quick_select_ushort(scdiffsort.data(), nframes,
188  (int)(0.979472 * nframes));
189  LOG(VB_COMMFLAG, LOG_INFO,
190  QString("SceneChangeDetector::finished applying threshold value %1")
191  .arg(mindiff));
192  computeChangeMap(&m_changeMap, m_scDiff, mindiff);
193  if (m_debugLevel >= 2)
195 
196  return 0;
197 }
198 
199 int
201 {
202  return m_histogramAnalyzer->reportTime();
203 }
204 
205 /* vim: set expandtab tabstop=4 shiftwidth=4: */
SceneChangeDetector::m_histogramAnalyzer
std::shared_ptr< HistogramAnalyzer > m_histogramAnalyzer
Definition: SceneChangeDetector.h:48
FrameAnalyzer::ANALYZE_OK
@ ANALYZE_OK
Definition: FrameAnalyzer.h:37
CommDetector2.h
FrameAnalyzer::FrameMap
QMap< long long, long long > FrameMap
Definition: FrameAnalyzer.h:45
arg
arg(title).arg(filename).arg(doDelete))
quickselect.h
MythPlayer::GetFrameRate
float GetFrameRate(void) const
Definition: mythplayer.h:132
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
mythplayer.h
MythPlayer
Definition: mythplayer.h:83
SceneChangeDetector::scenechange_data
Definition: SceneChangeDetector.h:38
commDetector2::createDebugDirectory
void createDebugDirectory(const QString &dirname, const QString &comment)
Definition: CommDetector2.cpp:230
mythburn.FILE
int FILE
Definition: mythburn.py:139
mythlogging.h
SceneChangeDetector.h
SceneChangeDetector::finished
int finished(long long nframes, bool final) override
Definition: SceneChangeDetector.cpp:157
filename
QString filename
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:637
SceneChangeDetector::m_scDiff
std::vector< uint16_t > m_scDiff
Definition: SceneChangeDetector.h:53
SceneChangeDetector::reportTime
int reportTime(void) const override
Definition: SceneChangeDetector.cpp:200
FrameAnalyzer::kNextFrame
static const long long kNextFrame
Definition: FrameAnalyzer.h:59
FrameAnalyzer::analyzeFrameResult
analyzeFrameResult
Definition: FrameAnalyzer.h:36
FrameAnalyzer.h
FrameAnalyzer::ANALYZE_ERROR
@ ANALYZE_ERROR
Definition: FrameAnalyzer.h:38
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:60
SceneChangeDetector::m_scData
std::vector< SceneChangeData > m_scData
Definition: SceneChangeDetector.h:52
MythCoreContext::GetNumSetting
int GetNumSetting(const QString &key, int defaultval=0)
Definition: mythcorecontext.cpp:933
SceneChangeDetector::MythPlayerInited
enum analyzeFrameResult MythPlayerInited(MythPlayer *player, long long nframes) override
Definition: SceneChangeDetector.cpp:119
SceneChangeDetector::m_debugData
QString m_debugData
Definition: SceneChangeDetector.h:59
HistogramAnalyzer::Histogram
std::array< uint8_t, UCHAR_MAX+1 > Histogram
Definition: HistogramAnalyzer.h:35
HistogramAnalyzer.h
mythcorecontext.h
SceneChangeDetector::analyzeFrame
enum analyzeFrameResult analyzeFrame(const MythVideoFrame *frame, long long frameno, long long *pNextFrame) override
Definition: SceneChangeDetector.cpp:141
MythPlayer::GetVideoSize
QSize GetVideoSize(void) const
Definition: mythplayer.h:130
SceneChangeDetector::m_sceneChangeDone
bool m_sceneChangeDone
Definition: SceneChangeDetector.h:61
SceneChangeDetector::SceneChangeDetector
SceneChangeDetector(std::shared_ptr< HistogramAnalyzer > ha, const QString &debugdir)
Definition: SceneChangeDetector.cpp:95
commDetector2
Definition: CommDetector2.cpp:194
SceneChangeDetector::m_fps
float m_fps
Definition: SceneChangeDetector.h:49
uint16_t
unsigned short uint16_t
Definition: iso6937tables.h:3
frameAnalyzer
Definition: FrameAnalyzer.cpp:7
MythVideoFrame
Definition: mythframe.h:83
SceneChangeDetector::m_changeMap
FrameAnalyzer::FrameMap m_changeMap
Definition: SceneChangeDetector.h:55
SceneChangeDetector::SceneChangeData
std::array< scenechange_data, UCHAR_MAX+1 > SceneChangeData
Definition: SceneChangeDetector.h:42
SceneChangeDetector::m_debugLevel
int m_debugLevel
Definition: SceneChangeDetector.h:58
SceneChangeDetector::m_debugSceneChange
bool m_debugSceneChange
Definition: SceneChangeDetector.h:60
quick_select_ushort
unsigned short quick_select_ushort(unsigned short *arr, int nelems, int select)
Definition: quickselect.cpp:74
frameAnalyzer::frameAnalyzerReportMapms
void frameAnalyzerReportMapms(const FrameAnalyzer::FrameMap *frameMap, float fps, const char *comment)
Definition: FrameAnalyzer.cpp:52