/
DetectLetterbox.cpp
374 lines (341 loc) · 13.7 KB
/
DetectLetterbox.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// MythTV
#include "mythcorecontext.h"
#include "mythplayer.h"
#include "DetectLetterbox.h"
#define LOC QString("DetectLetterbox: ")
#define NUMBER_OF_DETECTION_LINES 3 // How many lines are we looking at
#define THRESHOLD 5 // Y component has to not vary more than this in the bars
#define HORIZONTAL_THRESHOLD 4 // How tolerant are we that the image has horizontal edges
DetectLetterbox::DetectLetterbox(MythPlayer* const Player)
{
int dbAdjustFill = gCoreContext->GetNumSetting("AdjustFill", 0);
m_isDetectLetterbox = dbAdjustFill >= kAdjustFill_AutoDetect_DefaultOff;
m_detectLetterboxDefaultMode =
static_cast<AdjustFillMode>(std::max(static_cast<int>(kAdjustFill_Off),
dbAdjustFill - kAdjustFill_AutoDetect_DefaultOff));
m_detectLetterboxDetectedMode = Player->GetAdjustFill();
m_detectLetterboxLimit = gCoreContext->GetNumSetting("DetectLeterboxLimit", 75);
m_player = Player;
}
/** \fn DetectLetterbox::Detect(VideoFrame*)
* \brief Detects if this frame is or is not letterboxed
*
* If a change is detected detectLetterboxSwitchFrame and
* detectLetterboxDetectedMode are set.
*/
void DetectLetterbox::Detect(VideoFrame *Frame)
{
if (!Frame || !GetDetectLetterbox())
return;
if (!m_player->GetVideoOutput())
return;
unsigned char *buf = Frame->buf;
const FramePitches pitches = Frame->pitches;
const FrameOffsets offsets = Frame->offsets;
const int height = Frame->height;
// If the black bars is larger than this limit we switch to Half or Full Mode
// const int fullLimit = static_cast<int>(((height - width * 9 / 16) / 2) * m_detectLetterboxLimit / 100);
// const int halfLimit = (static_cast<int>(((height - width * 9 / 14) / 2) * m_detectLetterboxLimit / 100);
// If the black bars is larger than this limit we switch to Half or Full Mode
const int fullLimit = static_cast<int>((height * (1 - m_player->GetVideoAspect() * 9 / 16) / 2) * m_detectLetterboxLimit / 100);
const int halfLimit = static_cast<int>((height * (1 - m_player->GetVideoAspect() * 9 / 14) / 2) * m_detectLetterboxLimit / 100);
// Lines to scan for black letterbox edge
const std::array<int,3> xPos
{ Frame->width / 4, Frame->width / 2, Frame->width * 3 / 4} ;
int topHits = 0;
int bottomHits = 0;
int minTop = 0;
int minBottom = 0;
int maxTop = 0;
int maxBottom = 0;
std::array<int,3> topHit = { 0, 0, 0 };
std::array<int,3> bottomHit = { 0, 0, 0 };
switch (Frame->codec)
{
case FMT_YV12:
case FMT_YUV420P9:
case FMT_YUV420P10:
case FMT_YUV420P12:
case FMT_YUV420P14:
case FMT_YUV420P16:
case FMT_NV12:
case FMT_P010:
case FMT_P016:
if (!m_firstFrameChecked || m_frameType != Frame->codec)
{
m_firstFrameChecked = Frame->frameNumber;
LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("'%1' frame format detected")
.arg(format_description(Frame->codec)));
}
m_frameType = Frame->codec;
break;
default:
LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("'%1' frame format is not supported")
.arg(format_description(Frame->codec)));
m_isDetectLetterbox = false;
return;
}
if (Frame->frameNumber < 0)
{
LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Strange frame number %1")
.arg(Frame->frameNumber));
return;
}
if (m_player->GetVideoAspect() > 1.5F)
{
if (m_detectLetterboxDetectedMode != m_detectLetterboxDefaultMode)
{
LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("The source is already in widescreen (aspect: %1)")
.arg(static_cast<double>(m_player->GetVideoAspect())));
m_detectLetterboxLock.lock();
m_detectLetterboxConsecutiveCounter = 0;
m_detectLetterboxDetectedMode = m_detectLetterboxDefaultMode;
m_detectLetterboxSwitchFrame = Frame->frameNumber;
m_detectLetterboxLock.unlock();
}
else
{
m_detectLetterboxConsecutiveCounter++;
}
LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("The source is already in widescreen (aspect: %1)")
.arg(static_cast<double>(m_player->GetVideoAspect())));
m_isDetectLetterbox = false;
return;
}
// Establish the level of light in the edge
int averageY = 0;
for (int pos : xPos)
{
averageY += buf[offsets[0] + 5 * pitches[0] + pos];
averageY += buf[offsets[0] + (height - 6) * pitches[0] + pos];
}
averageY /= NUMBER_OF_DETECTION_LINES * 2;
if (averageY > 64) // Too bright to be a letterbox border
averageY = 0;
// Note - for 10/12 bit etc we only sample the most significant byte
bool triplanar = format_is_420(m_frameType);
int depth = ColorDepth(m_frameType);
int leftshift = depth > 8 ? 1 : 0;
int rightshift = depth > 8 ? 0 : 1;
// Scan the detection lines
for (int y = 5; y < height / 4; y++) // skip first pixels incase of noise in the edge
{
for (int detectionLine = 0; detectionLine < NUMBER_OF_DETECTION_LINES; detectionLine++)
{
int Y = buf[offsets[0] + y * pitches[0] + (xPos[detectionLine] << leftshift)];
int U = 0;
int V = 0;
if (triplanar)
{
U = buf[offsets[1] + (y>>1) * pitches[1] + (xPos[detectionLine] >> rightshift)];
V = buf[offsets[2] + (y>>1) * pitches[2] + (xPos[detectionLine] >> rightshift)];
}
else
{
int offset = offsets[1] + ((y >> 1) * pitches[1]) + (xPos[detectionLine & ~0x1] << leftshift);
U = buf[offset];
V = buf[offset + (1 << leftshift)];
}
if ((!topHit[detectionLine]) &&
( Y > averageY + THRESHOLD || Y < averageY - THRESHOLD ||
U < 128 - 32 || U > 128 + 32 ||
V < 128 - 32 || V > 128 + 32 ))
{
topHit[detectionLine] = y;
topHits++;
if (!minTop)
minTop = y;
maxTop = y;
}
Y = buf[offsets[0] + (height-y-1) * pitches[0] + (xPos[detectionLine] << leftshift)];
if (triplanar)
{
U = buf[offsets[1] + ((height-y-1) >> 1) * pitches[1] + (xPos[detectionLine] >> rightshift)];
V = buf[offsets[2] + ((height-y-1) >> 1) * pitches[2] + (xPos[detectionLine] >> rightshift)];
}
else
{
int offset = offsets[1] + (((height - y -1) >> 1) * pitches[1]) + (xPos[detectionLine & ~0x1] << leftshift);
U = buf[offset];
V = buf[offset + (1 << leftshift)];
}
if ((!bottomHit[detectionLine]) &&
( Y > averageY + THRESHOLD || Y < averageY - THRESHOLD ||
U < 128 - 32 || U > 128 + 32 ||
V < 128 - 32 || V > 128 + 32 ))
{
bottomHit[detectionLine] = y;
bottomHits++;
if (!minBottom)
minBottom = y;
maxBottom = y;
}
}
if (topHits == NUMBER_OF_DETECTION_LINES && bottomHits == NUMBER_OF_DETECTION_LINES)
break;
}
if (topHits != NUMBER_OF_DETECTION_LINES)
maxTop = height / 4;
if (!minTop)
minTop = height / 4;
if (bottomHits != NUMBER_OF_DETECTION_LINES)
maxBottom = height / 4;
if (!minBottom)
minBottom = height / 4;
bool horizontal = (((minTop != 0) && (maxTop - minTop < HORIZONTAL_THRESHOLD)) &&
((minBottom != 0) && (maxBottom - minBottom < HORIZONTAL_THRESHOLD)));
if (m_detectLetterboxSwitchFrame > Frame->frameNumber) // user is reversing
{
m_detectLetterboxLock.lock();
m_detectLetterboxDetectedMode = m_player->GetAdjustFill();
m_detectLetterboxSwitchFrame = -1;
m_detectLetterboxPossibleHalfFrame = -1;
m_detectLetterboxPossibleFullFrame = -1;
m_detectLetterboxLock.unlock();
}
if (minTop < halfLimit || minBottom < halfLimit)
m_detectLetterboxPossibleHalfFrame = -1;
if (minTop < fullLimit || minBottom < fullLimit)
m_detectLetterboxPossibleFullFrame = -1;
if (m_detectLetterboxDetectedMode != kAdjustFill_Full)
{
if (m_detectLetterboxPossibleHalfFrame == -1 &&
minTop > halfLimit && minBottom > halfLimit)
{
m_detectLetterboxPossibleHalfFrame = Frame->frameNumber;
}
}
else
{
if (m_detectLetterboxPossibleHalfFrame == -1 &&
minTop < fullLimit && minBottom < fullLimit)
{
m_detectLetterboxPossibleHalfFrame = Frame->frameNumber;
}
}
if (m_detectLetterboxPossibleFullFrame == -1 &&
minTop > fullLimit && minBottom > fullLimit)
{
m_detectLetterboxPossibleFullFrame = Frame->frameNumber;
}
if (maxTop < halfLimit || maxBottom < halfLimit) // Not too restrictive when switching to off
{
// No Letterbox
if (m_detectLetterboxDetectedMode != m_detectLetterboxDefaultMode)
{
LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Non Letterbox detected on line: %1 (limit: %2)")
.arg(std::min(maxTop, maxBottom)).arg(halfLimit));
m_detectLetterboxLock.lock();
m_detectLetterboxConsecutiveCounter = 0;
m_detectLetterboxDetectedMode = m_detectLetterboxDefaultMode;
m_detectLetterboxSwitchFrame = Frame->frameNumber;
m_detectLetterboxLock.unlock();
}
else
{
m_detectLetterboxConsecutiveCounter++;
}
}
else if (horizontal && minTop > halfLimit && minBottom > halfLimit &&
maxTop < fullLimit && maxBottom < fullLimit)
{
// Letterbox (with narrow bars)
if (m_detectLetterboxDetectedMode != kAdjustFill_Half)
{
LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Narrow Letterbox detected on line: %1 (limit: %2) frame: %3")
.arg(minTop).arg(halfLimit)
.arg(m_detectLetterboxPossibleHalfFrame));
m_detectLetterboxLock.lock();
m_detectLetterboxConsecutiveCounter = 0;
if (m_detectLetterboxDetectedMode == kAdjustFill_Full &&
m_detectLetterboxSwitchFrame != -1)
{
// Do not change switch frame if switch to Full mode has not been executed yet
}
else
{
m_detectLetterboxSwitchFrame = m_detectLetterboxPossibleHalfFrame;
}
m_detectLetterboxDetectedMode = kAdjustFill_Half;
m_detectLetterboxLock.unlock();
}
else
{
m_detectLetterboxConsecutiveCounter++;
}
}
else if (horizontal && minTop > fullLimit && minBottom > fullLimit)
{
// Letterbox
m_detectLetterboxPossibleHalfFrame = -1;
if (m_detectLetterboxDetectedMode != kAdjustFill_Full)
{
LOG(VB_PLAYBACK, LOG_INFO, LOC +
QString("Detected Letterbox on line: %1 (limit: %2) frame: %3").arg(minTop)
.arg(fullLimit).arg(m_detectLetterboxPossibleFullFrame));
m_detectLetterboxLock.lock();
m_detectLetterboxConsecutiveCounter = 0;
m_detectLetterboxDetectedMode = kAdjustFill_Full;
m_detectLetterboxSwitchFrame = m_detectLetterboxPossibleFullFrame;
m_detectLetterboxLock.unlock();
}
else
{
m_detectLetterboxConsecutiveCounter++;
}
}
else
{
if (m_detectLetterboxConsecutiveCounter <= 3)
m_detectLetterboxConsecutiveCounter = 0;
}
}
/** \fn DetectLetterbox::SwitchTo(VideoFrame*)
* \brief Switch to the mode detected by DetectLetterbox
*
* Switch fill mode if a switch was detected for this frame.
*/
void DetectLetterbox::SwitchTo(VideoFrame *Frame)
{
if (!GetDetectLetterbox())
return;
if (m_detectLetterboxSwitchFrame == -1)
return;
m_detectLetterboxLock.lock();
if (m_detectLetterboxSwitchFrame <= Frame->frameNumber &&
m_detectLetterboxConsecutiveCounter > 3)
{
if (m_player->GetAdjustFill() != m_detectLetterboxDetectedMode)
{
LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Switched to '%1' on frame %2 (%3)")
.arg(toString(m_detectLetterboxDetectedMode))
.arg(Frame->frameNumber)
.arg(m_detectLetterboxSwitchFrame));
m_player->GetVideoOutput()->ToggleAdjustFill(m_detectLetterboxDetectedMode);
m_player->ReinitOSD();
}
m_detectLetterboxSwitchFrame = -1;
}
else if (m_detectLetterboxSwitchFrame <= Frame->frameNumber)
{
LOG(VB_PLAYBACK, LOG_INFO, LOC +
QString("Not Switched to '%1' on frame %2 "
"(%3) Not enough consecutive detections (%4)")
.arg(toString(m_detectLetterboxDetectedMode))
.arg(Frame->frameNumber).arg(m_detectLetterboxSwitchFrame)
.arg(m_detectLetterboxConsecutiveCounter));
}
m_detectLetterboxLock.unlock();
}
void DetectLetterbox::SetDetectLetterbox(bool Detect)
{
m_isDetectLetterbox = Detect;
m_detectLetterboxSwitchFrame = -1;
m_detectLetterboxDetectedMode = m_player->GetAdjustFill();
m_firstFrameChecked = 0;
}
bool DetectLetterbox::GetDetectLetterbox() const
{
return m_isDetectLetterbox;
}
/* vim: set expandtab tabstop=4 shiftwidth=4: */