MythTV  master
TemplateFinder.h
Go to the documentation of this file.
1 /*
2  * TemplateFinder
3  *
4  * Attempt to infer the existence of a static template across a series of
5  * images by looking for stable edges. Some ideas are taken from
6  * http://thomashargrove.com/logo-detection/
7  *
8  * By operating on the edges of images rather than the image data itself, both
9  * opaque and transparent templates are robustly located (if they exist).
10  * Hopefully the "core" portion of animated templates is sufficiently large and
11  * stable enough for animated templates to also be discovered.
12  *
13  * This TemplateFinder only expects to successfully discover non- or
14  * minimally-moving templates. Templates that change position during the
15  * sequence of images will cause this algorithm to fail.
16  */
17 
18 #ifndef TEMPLATEFINDER_H
19 #define TEMPLATEFINDER_H
20 
21 extern "C" {
22 #include "libavcodec/avcodec.h" /* AVFrame */
23 }
24 #include "FrameAnalyzer.h"
25 
26 class PGMConverter;
27 class BorderDetector;
28 class EdgeDetector;
29 
31 {
32 public:
33  /* Ctor/dtor. */
34  TemplateFinder(std::shared_ptr<PGMConverter> pgmc,
35  std::shared_ptr<BorderDetector> bd,
36  std::shared_ptr<EdgeDetector> ed,
37  MythPlayer *player, std::chrono::seconds proglen,
38  const QString& debugdir);
39  TemplateFinder(std::shared_ptr<PGMConverter> pgmc,
40  std::shared_ptr<BorderDetector> bd,
41  std::shared_ptr<EdgeDetector> ed,
42  MythPlayer *player, int proglen, const QString& debugdir) :
43  TemplateFinder(std::move(pgmc), std::move(bd), std::move(ed),
44  player, std::chrono::seconds(proglen), debugdir) {};
45  ~TemplateFinder(void) override;
46 
47  /* FrameAnalyzer interface. */
48  const char *name(void) const override // FrameAnalyzer
49  { return "TemplateFinder"; }
51  long long nframes) override; // FrameAnalyzer
53  long long frameno, long long *pNextFrame) override; // FrameAnalyzer
54  int finished(long long nframes, bool final) override; // FrameAnalyzer
55  int reportTime(void) const override; // FrameAnalyzer
56  FrameMap GetMap(unsigned int /*index*/) const override // FrameAnalyzer
57  { FrameMap map; return map; }
58 
59  /* TemplateFinder implementation. */
60  const struct AVFrame *getTemplate(int *prow, int *pcol,
61  int *pwidth, int *pheight) const;
62 
63 private:
64  int resetBuffers(int newwidth, int newheight);
65 
66  std::shared_ptr<PGMConverter> m_pgmConverter {nullptr};
67  std::shared_ptr<BorderDetector> m_borderDetector {nullptr};
68  std::shared_ptr<EdgeDetector> m_edgeDetector {nullptr};
69 
70  std::chrono::seconds m_sampleTime {20min}; /* amount of time to analyze */
71  int m_frameInterval; /* analyze every <Interval> frames */
72  long long m_endFrame {0}; /* end of logo detection */
73  long long m_nextFrame {0}; /* next desired frame */
74 
75  int m_width {-1}; /* dimensions of frames */
76  int m_height {-1}; /* dimensions of frames */
77  unsigned int *m_scores {nullptr}; /* pixel "edge" scores */
78 
79  int m_minContentRow {INT_MAX}; /* limits of content area of images */
80  int m_minContentCol {INT_MAX};
81  int m_maxContentRow1 {INT_MAX}; /* minrow + height ("maxrow + 1") */
82  int m_maxContentCol1 {INT_MAX}; /* mincol + width ("maxcol + 1") */
83 
84  AVFrame m_tmpl {}; /* logo-matching template */
85  int m_tmplRow {-1};
86  int m_tmplCol {-1};
87  int m_tmplWidth {-1};
88  int m_tmplHeight {-1};
89 
90  AVFrame m_cropped {}; /* cropped version of frame */
91  int m_cwidth {-1}; /* cropped width */
92  int m_cheight {-1}; /* cropped height */
93 
94  /* Debugging. */
95  int m_debugLevel {0};
96  QString m_debugDir;
97  QString m_debugData; /* filename: template location */
98  QString m_debugTmpl; /* filename: logo template */
99  bool m_debugTemplate {false};
100  bool m_debugEdgeCounts {false};
101  bool m_debugFrames {false};
102  bool m_tmplValid {false};
103  bool m_tmplDone {false};
104  std::chrono::microseconds m_analyzeTime {0us};
105 };
106 
107 #endif /* !TEMPLATEFINDER_H */
108 
109 /* vim: set expandtab tabstop=4 shiftwidth=4: */
TemplateFinder::m_tmplValid
bool m_tmplValid
Definition: TemplateFinder.h:102
TemplateFinder::m_debugTemplate
bool m_debugTemplate
Definition: TemplateFinder.h:99
TemplateFinder::m_debugData
QString m_debugData
Definition: TemplateFinder.h:97
TemplateFinder::m_width
int m_width
Definition: TemplateFinder.h:75
TemplateFinder::m_analyzeTime
std::chrono::microseconds m_analyzeTime
Definition: TemplateFinder.h:104
TemplateFinder::m_tmplWidth
int m_tmplWidth
Definition: TemplateFinder.h:87
TemplateFinder::m_minContentCol
int m_minContentCol
Definition: TemplateFinder.h:80
FrameAnalyzer::FrameMap
QMap< long long, long long > FrameMap
Definition: FrameAnalyzer.h:45
TemplateFinder::TemplateFinder
TemplateFinder(std::shared_ptr< PGMConverter > pgmc, std::shared_ptr< BorderDetector > bd, std::shared_ptr< EdgeDetector > ed, MythPlayer *player, int proglen, const QString &debugdir)
Definition: TemplateFinder.h:39
TemplateFinder::m_edgeDetector
std::shared_ptr< EdgeDetector > m_edgeDetector
Definition: TemplateFinder.h:68
TemplateFinder::m_maxContentCol1
int m_maxContentCol1
Definition: TemplateFinder.h:82
BorderDetector
Definition: BorderDetector.h:19
TemplateFinder::m_scores
unsigned int * m_scores
Definition: TemplateFinder.h:77
MythPlayer
Definition: mythplayer.h:86
TemplateFinder::m_maxContentRow1
int m_maxContentRow1
Definition: TemplateFinder.h:81
TemplateFinder::m_tmplDone
bool m_tmplDone
Definition: TemplateFinder.h:103
TemplateFinder::m_borderDetector
std::shared_ptr< BorderDetector > m_borderDetector
Definition: TemplateFinder.h:67
TemplateFinder::MythPlayerInited
enum analyzeFrameResult MythPlayerInited(MythPlayer *player, long long nframes) override
Definition: TemplateFinder.cpp:770
TemplateFinder::m_debugDir
QString m_debugDir
Definition: TemplateFinder.h:96
TemplateFinder::m_tmplRow
int m_tmplRow
Definition: TemplateFinder.h:85
AVFrame
struct AVFrame AVFrame
Definition: BorderDetector.h:15
TemplateFinder::name
const char * name(void) const override
Definition: TemplateFinder.h:48
FrameAnalyzer
Definition: FrameAnalyzer.h:28
TemplateFinder
Definition: TemplateFinder.h:30
TemplateFinder::m_sampleTime
std::chrono::seconds m_sampleTime
Definition: TemplateFinder.h:70
TemplateFinder::m_debugLevel
int m_debugLevel
Definition: TemplateFinder.h:95
TemplateFinder::m_tmpl
AVFrame m_tmpl
Definition: TemplateFinder.h:84
TemplateFinder::reportTime
int reportTime(void) const override
Definition: TemplateFinder.cpp:1028
FrameAnalyzer::analyzeFrameResult
analyzeFrameResult
Definition: FrameAnalyzer.h:36
TemplateFinder::TemplateFinder
TemplateFinder(std::shared_ptr< PGMConverter > pgmc, std::shared_ptr< BorderDetector > bd, std::shared_ptr< EdgeDetector > ed, MythPlayer *player, std::chrono::seconds proglen, const QString &debugdir)
Definition: TemplateFinder.cpp:696
FrameAnalyzer.h
TemplateFinder::GetMap
FrameMap GetMap(unsigned int) const override
Definition: TemplateFinder.h:56
TemplateFinder::m_cheight
int m_cheight
Definition: TemplateFinder.h:92
TemplateFinder::finished
int finished(long long nframes, bool final) override
Definition: TemplateFinder.cpp:983
TemplateFinder::resetBuffers
int resetBuffers(int newwidth, int newheight)
Definition: TemplateFinder.cpp:836
EdgeDetector
Definition: EdgeDetector.h:27
TemplateFinder::m_debugTmpl
QString m_debugTmpl
Definition: TemplateFinder.h:98
PGMConverter
Definition: PGMConverter.h:30
TemplateFinder::m_cropped
AVFrame m_cropped
Definition: TemplateFinder.h:90
std
Definition: mythchrono.h:23
TemplateFinder::m_tmplHeight
int m_tmplHeight
Definition: TemplateFinder.h:88
TemplateFinder::m_debugEdgeCounts
bool m_debugEdgeCounts
Definition: TemplateFinder.h:100
TemplateFinder::m_debugFrames
bool m_debugFrames
Definition: TemplateFinder.h:101
TemplateFinder::m_minContentRow
int m_minContentRow
Definition: TemplateFinder.h:79
TemplateFinder::analyzeFrame
enum analyzeFrameResult analyzeFrame(const MythVideoFrame *frame, long long frameno, long long *pNextFrame) override
Definition: TemplateFinder.cpp:859
TemplateFinder::m_height
int m_height
Definition: TemplateFinder.h:76
TemplateFinder::m_frameInterval
int m_frameInterval
Definition: TemplateFinder.h:71
MythVideoFrame
Definition: mythframe.h:88
TemplateFinder::m_cwidth
int m_cwidth
Definition: TemplateFinder.h:91
TemplateFinder::m_nextFrame
long long m_nextFrame
Definition: TemplateFinder.h:73
TemplateFinder::getTemplate
const struct AVFrame * getTemplate(int *prow, int *pcol, int *pwidth, int *pheight) const
Definition: TemplateFinder.cpp:1042
TemplateFinder::~TemplateFinder
~TemplateFinder(void) override
Definition: TemplateFinder.cpp:762
TemplateFinder::m_endFrame
long long m_endFrame
Definition: TemplateFinder.h:72
TemplateFinder::m_tmplCol
int m_tmplCol
Definition: TemplateFinder.h:86
TemplateFinder::m_pgmConverter
std::shared_ptr< PGMConverter > m_pgmConverter
Definition: TemplateFinder.h:66