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, int proglen, const QString& debugdir);
38  ~TemplateFinder(void) override;
39 
40  /* FrameAnalyzer interface. */
41  const char *name(void) const override // FrameAnalyzer
42  { return "TemplateFinder"; }
44  long long nframes) override; // FrameAnalyzer
45  enum analyzeFrameResult analyzeFrame(const VideoFrame *frame,
46  long long frameno, long long *pNextFrame) override; // FrameAnalyzer
47  int finished(long long nframes, bool final) override; // FrameAnalyzer
48  int reportTime(void) const override; // FrameAnalyzer
49  FrameMap GetMap(unsigned int /*index*/) const override // FrameAnalyzer
50  { FrameMap map; return map; }
51 
52  /* TemplateFinder implementation. */
53  const struct AVFrame *getTemplate(int *prow, int *pcol,
54  int *pwidth, int *pheight) const;
55 
56 private:
57  int resetBuffers(int newwidth, int newheight);
58 
59  std::shared_ptr<PGMConverter> m_pgmConverter {nullptr};
60  std::shared_ptr<BorderDetector> m_borderDetector {nullptr};
61  std::shared_ptr<EdgeDetector> m_edgeDetector {nullptr};
62 
63  unsigned int m_sampleTime {20 * 60}; /* amount of time to analyze */
64  int m_frameInterval; /* analyze every <Interval> frames */
65  long long m_endFrame {0}; /* end of logo detection */
66  long long m_nextFrame {0}; /* next desired frame */
67 
68  int m_width {-1}; /* dimensions of frames */
69  int m_height {-1}; /* dimensions of frames */
70  unsigned int *m_scores {nullptr}; /* pixel "edge" scores */
71 
72  int m_minContentRow {INT_MAX}; /* limits of content area of images */
73  int m_minContentCol {INT_MAX};
74  int m_maxContentRow1 {INT_MAX}; /* minrow + height ("maxrow + 1") */
75  int m_maxContentCol1 {INT_MAX}; /* mincol + width ("maxcol + 1") */
76 
77  AVFrame m_tmpl {}; /* logo-matching template */
78  int m_tmplRow {-1};
79  int m_tmplCol {-1};
80  int m_tmplWidth {-1};
81  int m_tmplHeight {-1};
82 
83  AVFrame m_cropped {}; /* cropped version of frame */
84  int m_cwidth {-1}; /* cropped width */
85  int m_cheight {-1}; /* cropped height */
86 
87  /* Debugging. */
88  int m_debugLevel {0};
89  QString m_debugDir;
90  QString m_debugData; /* filename: template location */
91  QString m_debugTmpl; /* filename: logo template */
92  bool m_debugTemplate {false};
93  bool m_debugEdgeCounts {false};
94  bool m_debugFrames {false};
95  bool m_tmplValid {false};
96  bool m_tmplDone {false};
97  struct timeval m_analyzeTime {0,0};
98 };
99 
100 #endif /* !TEMPLATEFINDER_H */
101 
102 /* vim: set expandtab tabstop=4 shiftwidth=4: */
TemplateFinder::m_tmplValid
bool m_tmplValid
Definition: TemplateFinder.h:95
TemplateFinder::m_debugTemplate
bool m_debugTemplate
Definition: TemplateFinder.h:92
TemplateFinder::m_debugData
QString m_debugData
Definition: TemplateFinder.h:90
TemplateFinder::m_width
int m_width
Definition: TemplateFinder.h:68
TemplateFinder::m_tmplWidth
int m_tmplWidth
Definition: TemplateFinder.h:80
TemplateFinder::m_minContentCol
int m_minContentCol
Definition: TemplateFinder.h:73
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.cpp:699
TemplateFinder::m_edgeDetector
std::shared_ptr< EdgeDetector > m_edgeDetector
Definition: TemplateFinder.h:61
TemplateFinder::m_maxContentCol1
int m_maxContentCol1
Definition: TemplateFinder.h:75
BorderDetector
Definition: BorderDetector.h:19
TemplateFinder::m_scores
unsigned int * m_scores
Definition: TemplateFinder.h:70
MythPlayer
Definition: mythplayer.h:164
TemplateFinder::m_maxContentRow1
int m_maxContentRow1
Definition: TemplateFinder.h:74
VideoFrame
Definition: mythframe.h:137
TemplateFinder::m_tmplDone
bool m_tmplDone
Definition: TemplateFinder.h:96
TemplateFinder::m_borderDetector
std::shared_ptr< BorderDetector > m_borderDetector
Definition: TemplateFinder.h:60
TemplateFinder::MythPlayerInited
enum analyzeFrameResult MythPlayerInited(MythPlayer *player, long long nframes) override
Definition: TemplateFinder.cpp:772
TemplateFinder::m_debugDir
QString m_debugDir
Definition: TemplateFinder.h:89
TemplateFinder::m_tmplRow
int m_tmplRow
Definition: TemplateFinder.h:78
AVFrame
struct AVFrame AVFrame
Definition: BorderDetector.h:15
TemplateFinder::name
const char * name(void) const override
Definition: TemplateFinder.h:41
FrameAnalyzer
Definition: FrameAnalyzer.h:28
TemplateFinder
Definition: TemplateFinder.h:30
TemplateFinder::m_analyzeTime
Definition: TemplateFinder.h:97
TemplateFinder::m_debugLevel
int m_debugLevel
Definition: TemplateFinder.h:88
TemplateFinder::m_tmpl
AVFrame m_tmpl
Definition: TemplateFinder.h:77
TemplateFinder::reportTime
int reportTime(void) const override
Definition: TemplateFinder.cpp:1035
FrameAnalyzer::analyzeFrameResult
analyzeFrameResult
Definition: FrameAnalyzer.h:36
FrameAnalyzer.h
TemplateFinder::GetMap
FrameMap GetMap(unsigned int) const override
Definition: TemplateFinder.h:49
TemplateFinder::m_cheight
int m_cheight
Definition: TemplateFinder.h:85
TemplateFinder::finished
int finished(long long nframes, bool final) override
Definition: TemplateFinder.cpp:990
TemplateFinder::resetBuffers
int resetBuffers(int newwidth, int newheight)
Definition: TemplateFinder.cpp:839
EdgeDetector
Definition: EdgeDetector.h:27
TemplateFinder::m_debugTmpl
QString m_debugTmpl
Definition: TemplateFinder.h:91
PGMConverter
Definition: PGMConverter.h:29
TemplateFinder::analyzeFrame
enum analyzeFrameResult analyzeFrame(const VideoFrame *frame, long long frameno, long long *pNextFrame) override
Definition: TemplateFinder.cpp:862
TemplateFinder::m_cropped
AVFrame m_cropped
Definition: TemplateFinder.h:83
TemplateFinder::m_sampleTime
unsigned int m_sampleTime
Definition: TemplateFinder.h:63
TemplateFinder::m_tmplHeight
int m_tmplHeight
Definition: TemplateFinder.h:81
TemplateFinder::m_debugEdgeCounts
bool m_debugEdgeCounts
Definition: TemplateFinder.h:93
TemplateFinder::m_debugFrames
bool m_debugFrames
Definition: TemplateFinder.h:94
TemplateFinder::m_minContentRow
int m_minContentRow
Definition: TemplateFinder.h:72
TemplateFinder::m_height
int m_height
Definition: TemplateFinder.h:69
TemplateFinder::m_frameInterval
int m_frameInterval
Definition: TemplateFinder.h:64
TemplateFinder::m_cwidth
int m_cwidth
Definition: TemplateFinder.h:84
TemplateFinder::m_nextFrame
long long m_nextFrame
Definition: TemplateFinder.h:66
TemplateFinder::getTemplate
const struct AVFrame * getTemplate(int *prow, int *pcol, int *pwidth, int *pheight) const
Definition: TemplateFinder.cpp:1049
TemplateFinder::~TemplateFinder
~TemplateFinder(void) override
Definition: TemplateFinder.cpp:764
TemplateFinder::m_endFrame
long long m_endFrame
Definition: TemplateFinder.h:65
TemplateFinder::m_tmplCol
int m_tmplCol
Definition: TemplateFinder.h:79
TemplateFinder::m_pgmConverter
std::shared_ptr< PGMConverter > m_pgmConverter
Definition: TemplateFinder.h:59