-
Notifications
You must be signed in to change notification settings - Fork 0
/
ambithread.c
418 lines (345 loc) · 10.5 KB
/
ambithread.c
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
* ambithread.c
*
* Copyright (C) 2013 - Christian Völlinger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <vdr/plugin.h>
#include "softhdservice.h"
#include "ambithread.h"
#include "config.h"
//***************************************************************************
// Class cAmbiThread
//***************************************************************************
//***************************************************************************
// Object
//***************************************************************************
cAmbiThread::cAmbiThread()
{
loopActive = false;
image = 0;
imageSize = 0;
imageWidth = 0;
imageHeight = 0;
xBarHeight = 0;
yBarWidth = 0;
lastxBarHeight = 0;
lastyBarWidth = 0;
barsChanged = true;
osd3dChanged = true;
softHdPlugin = cPluginManager::GetPlugin("softhddevice");
if (!softHdPlugin)
softHdPlugin = cPluginManager::GetPlugin("softhdcuvid");
int softHdGrabService = (softHdPlugin && softHdPlugin->Service(ATMO1_GRAB_SERVICE, 0));
if (!softHdGrabService)
error("Can't find softhddevice %s!", softHdPlugin ? "service" : "plugin");
int softHd3DOsdServive = (softHdPlugin && softHdPlugin->Service(OSD1_3DMODE_SERVICE, 0));
if (!softHd3DOsdServive)
tell(1, "Can't find softhddevice 3D OsdMode Service 1.1, 3D detection will not work!");
}
cAmbiThread::~cAmbiThread()
{
bob.close();
}
//***************************************************************************
// Stop Thread
//***************************************************************************
void cAmbiThread::Stop()
{
loopActive = false;
waitCondition.Broadcast(); // wakeup the thread
Cancel(3); // wait up to 3 seconds for thread was stopping
}
//***************************************************************************
// Action
//***************************************************************************
void cAmbiThread::Action()
{
MsTime wait = 0;
MsTime lastPing = 0;
MsTime lastBoarderDetect = 0;
int lastPingResult = na;
cMutexLock lock(&mutex);
tell(0, "boblight Thread started (pid=%d)", getpid());
loopActive = true;
bob.open();
while (loopActive && Running())
{
MsTime start = msNow();
if(ShutdownHandler.IsUserInactive() || softhddeviceNotDetached() == fail)
{
bob.close();
waitCondition.TimedWait(mutex, 1000);
continue;
}
// Reduce load, just ping every second
if(start - lastPing > 1000) {
lastPing = start;
lastPingResult = bob.ping();
}
// Softhddevice is not detached, work...
if(lastPingResult == success) {
if(cfg.dirty > 0) {
cfg.dirty = 0;
bob.sendOptions();
}
switch(cfg.viewMode)
{
case vmAtmo:
if (grabImage() == success)
{
if(start - lastBoarderDetect > 5000) {
lastBoarderDetect = start;
detectCineBars();
getOsd3DMode();
}
putData();
MsTime elapsed = msNow() - start;
wait = 1000 / cfg.frequence - elapsed;
tell(3, "sleeping %ldms (%d Hz)", wait, cfg.frequence);
}
else
{
wait = 10000; // retry softhd grab every 10 seconds
}
break;
case vmBlack:
case vmFixedCol:
putData();
wait = 500; // less load on fixed color or black
break;
case vmDetached:
bob.close();
wait = 1000;
break;
default:
break;
}
}
else { // Connection lost, reconnect
wait = 10000;
switch(cfg.viewMode)
{
case vmDetached:
bob.close();
break;
default:
bob.close();
bob.open();
wait = 5000;
}
}
waitCondition.TimedWait(mutex, wait); // wait time in ms
}
bob.close();
loopActive = false;
tell(0, "boblight thread ended (pid=%d)", getpid());
}
int cAmbiThread::softhddeviceNotDetached()
{
int reply_code = 0;
cString reply_msg;
reply_msg = softHdPlugin->SVDRPCommand("STAT", "", reply_code);
if(910 == reply_code)
{
tell(1, "Softhddevice NOT detached: %d", reply_code);
return success;
}
else
{
tell(1, "Softhddevice detached: %d", reply_code);
return fail;
}
}
//***************************************************************************
// Grab Image
//***************************************************************************
int cAmbiThread::grabImage()
{
SoftHDDevice_AtmoGrabService_v1_1_t req;
free(image);
image = 0;
// grab image at sofhddevice
req.width = 64;
req.height = 64;
req.img = 0;
if (!softHdPlugin->Service(ATMO1_GRAB_SERVICE, &req) || !req.img)
return fail;
tell(3, "Got image with %dx%d pixel; %d bytes", req.width, req.height, req.size);
image = (Pixel*)req.img;
imageSize = req.size;
imageWidth = req.width;
imageHeight = req.height;
return success;
}
int cAmbiThread::getOsd3DMode()
{
if(osd3dChanged) return osd3DMode;
switch(cfg.osd3DMode) {
case osdAuto:
int tempOsd3DMode;
SoftHDDevice_Osd3DModeService_v1_1_t req;
req.GetMode = true;
if (!softHdPlugin->Service(OSD1_3DMODE_SERVICE, &req)) {
tempOsd3DMode = osdOff;
cfg.osd3DMode = osdOff;
}
// (0=off, 1=SBS, 2=Top Bottom)
switch (req.Mode) {
case 0:
tempOsd3DMode = osdOff;
break;
case 1:
tempOsd3DMode = osdHSBS;
break;
case 2:
tempOsd3DMode = osdHOU;
break;
default:
tempOsd3DMode = osdOff;
}
if(tempOsd3DMode != osd3DMode) {
osd3dChanged = true;
osd3DMode = tempOsd3DMode;
}
break;
case osdOff:
case osdHSBS:
case osdHOU:
if(osd3DMode != cfg.osd3DMode) {
osd3dChanged = true;
osd3DMode = cfg.osd3DMode;
}
default:
break;
}
return osd3DMode;
}
//***************************************************************************
// Detect Cine Bars
//***************************************************************************
int cAmbiThread::detectCineBars()
{
/*
Annahme: Wenn im mittleren oberen und unterem Drittel des Bildes alle aufeinander folgenden Pixel schwarz sind
haben wir einen Horizontalen Balken.
|0|x x|0|
|y|0 0|y|
|y|0 0|y|
|0|x x|0|
*/
if (cfg.detectCineBars == cbNone || barsChanged) {
return done;
}
Pixel* p;
const int xOffset = imageWidth / 4;
const int yOffset = imageHeight / 4;
int tempxBarHeight = 0;
int tempyBarWidth = 0;
if (cfg.detectCineBars == cbHorizontal || cfg.detectCineBars == cbBoth) {
// check for xBar
for (int y = 0; y < yOffset; ++y) {
int row = imageWidth * y;
int xBarCount = 0;
for (int x = xOffset; x < imageWidth - xOffset; ++x) {
p = &image[row + x];
if(p->isBlack(cfg.cineBarsThreshold)) {
++xBarCount;
}
}
if(xBarCount == imageWidth - (2*xOffset)){
++tempxBarHeight;
}
else {
break;
}
}
}
if (cfg.detectCineBars == cbVertical || cfg.detectCineBars == cbBoth) {
// check for yBar
for (int x = 0; x < xOffset; ++x) {
int yBarCount = 0;
for (int y = yOffset; y < imageHeight - yOffset; ++y) {
int row = imageWidth * y;
p = &image[row + x];
if(p->isBlack(cfg.cineBarsThreshold)) {
++yBarCount;
}
}
if(yBarCount == imageHeight - (2*yOffset) ){
++tempyBarWidth;
}
else {
break;
}
}
}
if (tempxBarHeight != lastxBarHeight || tempyBarWidth != lastyBarWidth) {
barsChanged = true;
xBarHeight = tempxBarHeight;
yBarWidth = tempyBarWidth;
}
lastxBarHeight = tempxBarHeight;
lastyBarWidth = tempyBarWidth;
if(barsChanged) tell(1, "V2 Black border detection horBar: %d verBar: %d", xBarHeight, yBarWidth);
return done;
}
//***************************************************************************
// Put Data
//***************************************************************************
int cAmbiThread::putData()
{
if (cfg.viewMode == vmFixedCol)
{
int pFixedCol[3] = {cfg.fixedR, cfg.fixedG, cfg.fixedB};
bob.writePix(pFixedCol);
}
else if (cfg.viewMode == vmBlack) {
int pFixedCol[3] = {0,0,0};
bob.writePix(pFixedCol);
}
else if(cfg.viewMode == vmAtmo) {
int row = 0;
Pixel* p;
int scanWidth = 0;
int scanHeight = 0;
for (int y = 0; y < imageHeight; y++) {
// skip horizontal cinebars
if(y < xBarHeight || y > imageHeight - xBarHeight) continue;
if (osd3DMode == osdHOU && y > (imageHeight/2) ) continue; // (0=off, 1=SBS, 2=Top Bottom)
int rgb[3];
row = imageWidth * y;
for (int x = 0; x < imageWidth; x++) {
// skip vertical cinebars
if(x < yBarWidth || x > imageWidth - yBarWidth) continue;
if (osd3DMode == osdHSBS && x > (imageWidth/2) ) continue; // (0=off, 1=SBS, 2=Top Bottom)
p = &image[row + x];
rgb[0] = p->r;
rgb[1] = p->g;
rgb[2] = p->b;
bob.writeColor(rgb, x - yBarWidth, y - xBarHeight);
scanWidth = x - yBarWidth;
scanHeight = y- xBarHeight;
}
}
if (barsChanged || osd3dChanged) {
bob.setScanRange(scanWidth, scanHeight);
if (barsChanged) barsChanged = false;
if (osd3dChanged) osd3dChanged = false;
}
}
bob.send();
return success;
}