4 #include "libmythbase/mythconfig.h"
7 #include "libavcodec/avcodec.h"
28 if (m_debugLevel >= 1)
29 LOG(VB_COMMFLAG, LOG_INFO,
30 QString(
"BorderDetector debugLevel %1").arg(m_debugLevel));
36 m_timeReported =
false;
37 memset(&m_analyzeTime, 0,
sizeof(m_analyzeTime));
44 m_logoFinder = finder;
45 if (m_logoFinder ==
nullptr)
47 m_logo = m_logoFinder->
getTemplate(&m_logoRow, &m_logoCol,
48 &m_logoWidth, &m_logoHeight);
49 if (m_logo ==
nullptr)
51 LOG(VB_COMMFLAG, LOG_INFO,
52 QString(
"BorderDetector::setLogoState: %1x%2@(%3,%4)")
53 .arg(m_logoWidth).arg(m_logoHeight).arg(m_logoCol).arg(m_logoRow));
58 long long _frameno,
int *prow,
int *pcol,
int *pwidth,
int *pheight)
91 static constexpr
unsigned char kMaxRange = 32;
98 static constexpr
int kMaxLines = 2;
100 const int pgmwidth = pgm->linesize[0];
107 const int MAXOUTLIERS = pgmwidth * 12 / 1000;
113 const int VERTMARGIN = std::max(2, pgmheight * 1 / 60);
114 const int HORIZMARGIN = std::max(2, pgmwidth * 1 / 80);
121 const int VERTSLOP = std::max(kMaxLines, pgmheight * 1 / 120);
122 const int HORIZSLOP = std::max(kMaxLines, pgmwidth * 1 / 160);
124 int minrow = VERTMARGIN;
125 int mincol = HORIZMARGIN;
126 int maxrow1 = pgmheight - VERTMARGIN;
127 int maxcol1 = pgmwidth - HORIZMARGIN;
128 int newrow = minrow - 1;
129 int newcol = mincol - 1;
130 int newwidth = maxcol1 + 1 - mincol;
131 int newheight = maxrow1 + 1 - minrow;
135 auto start = nowAsDuration<std::chrono::microseconds>();
137 if (_frameno != kUncached && _frameno == m_frameNo)
144 uchar minval = UCHAR_MAX;
148 for (
int cc = mincol;
cc < maxcol1;
cc++)
152 for (
int rr = minrow; rr < maxrow1; rr++)
155 m_logoWidth, m_logoHeight))
158 uchar val = pgm->data[0][(rr * pgmwidth) +
cc];
159 int range = std::max(maxval, val) - std::min(minval, val) + 1;
160 if (range > kMaxRange)
162 if (outliers++ < MAXOUTLIERS)
165 if (lines++ < kMaxLines)
169 minval = std::min(val, minval);
170 maxval = std::max(val, maxval);
179 if (newcol != saved + 1 + HORIZSLOP)
181 newcol = std::min(maxcol1, saved + 1 + HORIZSLOP);
182 newwidth = std::max(0, maxcol1 - newcol);
187 goto monochromatic_frame;
198 for (
int cc = maxcol1 - 1;
cc >= mincol;
cc--)
202 for (
int rr = minrow; rr < maxrow1; rr++)
205 m_logoWidth, m_logoHeight))
208 uchar val = pgm->data[0][(rr * pgmwidth) +
cc];
209 int range = std::max(maxval, val) - std::min(minval, val) + 1;
210 if (range > kMaxRange)
212 if (outliers++ < MAXOUTLIERS)
215 if (lines++ < kMaxLines)
219 minval = std::min(val, minval);
220 maxval = std::max(val, maxval);
229 if (newwidth != saved - mincol - HORIZSLOP)
231 newwidth = std::max(0, saved - mincol - HORIZSLOP);
236 goto monochromatic_frame;
241 maxcol1 = mincol + newwidth;
249 for (
int rr = minrow; rr < maxrow1; rr++)
253 for (
int cc = mincol;
cc < maxcol1;
cc++)
256 m_logoWidth, m_logoHeight))
259 uchar val = pgm->data[0][(rr * pgmwidth) +
cc];
260 int range = std::max(maxval, val) - std::min(minval, val) + 1;
261 if (range > kMaxRange)
263 if (outliers++ < MAXOUTLIERS)
266 if (lines++ < kMaxLines)
270 minval = std::min(val, minval);
271 maxval = std::max(val, maxval);
280 if (newrow != saved + 1 + VERTSLOP)
282 newrow = std::min(maxrow1, saved + 1 + VERTSLOP);
283 newheight = std::max(0, maxrow1 - newrow);
288 goto monochromatic_frame;
296 for (
int rr = maxrow1 - 1; rr >= minrow; rr--)
300 for (
int cc = mincol;
cc < maxcol1;
cc++)
303 m_logoWidth, m_logoHeight))
306 uchar val = pgm->data[0][(rr * pgmwidth) +
cc];
307 int range = std::max(maxval, val) - std::min(minval, val) + 1;
308 if (range > kMaxRange)
310 if (outliers++ < MAXOUTLIERS)
313 if (lines++ < kMaxLines)
317 minval = std::min(val, minval);
318 maxval = std::max(val, maxval);
327 if (newheight != saved - minrow - VERTSLOP)
329 newheight = std::max(0, saved - minrow - VERTSLOP);
334 goto monochromatic_frame;
339 maxrow1 = minrow + newheight;
342 m_frameNo = _frameno;
346 m_height = newheight;
347 m_isMonochromatic =
false;
351 m_frameNo = _frameno;
355 m_height = newheight;
356 m_isMonochromatic =
true;
364 auto end = nowAsDuration<std::chrono::microseconds>();
365 m_analyzeTime += (end - start);
367 return m_isMonochromatic ? -1 : 0;
375 LOG(VB_COMMFLAG, LOG_INFO, QString(
"BD Time: analyze=%1s")
377 m_timeReported =
true;