forked from ambilight-4-mediaportal/AtmoWin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAtmoExternalCaptureInput.cpp
More file actions
279 lines (241 loc) · 9.1 KB
/
Copy pathAtmoExternalCaptureInput.cpp
File metadata and controls
279 lines (241 loc) · 9.1 KB
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
/*
* AtmoExternalCaptureInput.cpp: Datasource which gets its data via a COM object call
* or some other external method.
*
*
* See the README.txt file for copyright information and how to reach the author(s).
*
* $Id: 7e8d3cfb9910397d39efd9a1a0af4a52cf063a8d $
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "AtmoExternalCaptureInput.h"
#include "AtmoTools.h"
#if defined(_ATMO_VLC_PLUGIN_)
#ifndef INT64_C
#define INT64_C(c) c ## LL
#endif
CAtmoExternalCaptureInput::CAtmoExternalCaptureInput(CAtmoDynData *pAtmoDynData) :
CAtmoInput(pAtmoDynData)
{
vlc_cond_init( &m_WakeupCond );
vlc_mutex_init( &m_WakeupLock );
m_pCurrentFramePixels = NULL;
m_pLog = pAtmoDynData->getAtmoFilter();
}
#else
CAtmoExternalCaptureInput::CAtmoExternalCaptureInput(CAtmoDynData *pAtmoDynData) :
CAtmoInput(pAtmoDynData)
{
m_hWakeupEvent = CreateEvent(NULL,0,0,NULL);
InitializeCriticalSection( &m_BufferLock );
m_pCurrentFramePixels = NULL;
}
#endif
CAtmoExternalCaptureInput::~CAtmoExternalCaptureInput(void)
{
/* if there is still an unprocessed bufferpicture do kill it */
#if defined(_ATMO_VLC_PLUGIN_)
vlc_mutex_lock( &m_WakeupLock );
free( m_pCurrentFramePixels );
vlc_mutex_unlock( &m_WakeupLock );
vlc_cond_destroy( &m_WakeupCond );
vlc_mutex_destroy( &m_WakeupLock );
#else
EnterCriticalSection( &m_BufferLock );
free( m_pCurrentFramePixels );
LeaveCriticalSection( &m_BufferLock );
CloseHandle(m_hWakeupEvent);
DeleteCriticalSection( &m_BufferLock );
#endif
}
ATMO_BOOL CAtmoExternalCaptureInput::Open()
{
this->Run();
return ATMO_TRUE;
}
// Closes the input-device.
// Returns true if the input-device was closed successfully.
ATMO_BOOL CAtmoExternalCaptureInput::Close(void)
{
this->Terminate();
return ATMO_TRUE;
}
/*
this method will be called from another thread or possible the COM Server to feed
new pixeldata into the calculation process it doest just the following:
1: check if last buffer was allready processed (!m_pCurrentFramePixels)
2. copy the bitmap info structure into the threads own one
3. alloc memory for frame
4. copy sourcepixeldata into own buffer...
5. let the thread wake up and return imediately to the caller
so that the real videoout wouldn't be stop for a too long time
*/
void CAtmoExternalCaptureInput::DeliverNewSourceDataPaket(BITMAPINFOHEADER *bmpInfoHeader,void *pixelData)
{
/*
normaly we should protect this area of code by critical_section or a mutex,
but I think we can omit this here because the timing this method is called
is really slow (in terms of the speed of a modern computer?)
so it's nearly impossible that two frames are delivert in the same time
the test needs and malloc needs...
*/
#if defined(_ATMO_VLC_PLUGIN_)
// msg_Dbg( m_pLog, "DeliverNewSourceDataPaket start...");
vlc_mutex_lock( &m_WakeupLock );
#else
EnterCriticalSection( &m_BufferLock );
#endif
if( !m_pCurrentFramePixels )
{
// Last Frame was processed... take this one...
memcpy(&m_CurrentFrameHeader,bmpInfoHeader,bmpInfoHeader->biSize);
int PixelDataSize = m_CurrentFrameHeader.biHeight * m_CurrentFrameHeader.biWidth;
switch(m_CurrentFrameHeader.biBitCount) {
case 8: /* PixelDataSize = PixelDataSize; */ break;
case 16: PixelDataSize = PixelDataSize * 2; break;
case 24: PixelDataSize = PixelDataSize * 3; break;
case 32: PixelDataSize = PixelDataSize * 4; break;
}
m_pCurrentFramePixels = malloc(PixelDataSize);
memcpy(m_pCurrentFramePixels,pixelData,PixelDataSize);
}
#if defined(_ATMO_VLC_PLUGIN_)
vlc_cond_signal( &m_WakeupCond );
vlc_mutex_unlock( &m_WakeupLock );
// msg_Dbg( m_pLog, "DeliverNewSourceDataPaket done.");
#else
SetEvent(m_hWakeupEvent);
LeaveCriticalSection( &m_BufferLock );
#endif
}
/*
the real thread Method which is processing the pixeldata into the hardware channel
values - which are used by the thread AtmoLiveView...
*/
#if defined (_ATMO_VLC_PLUGIN_)
DWORD CAtmoExternalCaptureInput::Execute(void)
{
while ((this->m_bTerminated == ATMO_FALSE)) {
vlc_mutex_lock( &m_WakeupLock );
vlc_cond_timedwait(&m_WakeupCond, &m_WakeupLock, mdate() + 75000 );
/* DeliverNewSourceDataPaket delivered new work for me... get it! */
if(m_pCurrentFramePixels)
CalcColors(); // read picture and calculate colors
vlc_mutex_unlock( &m_WakeupLock );
}
msg_Dbg( m_pLog, "DWORD CAtmoExternalCaptureInput::Execute(void) bailed out?");
return 0;
}
#else
DWORD CAtmoExternalCaptureInput::Execute(void) {
HANDLE handles[2];
handles[0] = this->m_hTerminateEvent;
handles[1] = m_hWakeupEvent;
while (this->m_bTerminated == ATMO_FALSE) {
DWORD event = WaitForMultipleObjects(2, &handles[0], FALSE, INFINITE);
if(event == WAIT_OBJECT_0) {
// Terminate Thread Event was set... say good bye...!
break;
}
if(event == (WAIT_OBJECT_0+1)) {
EnterCriticalSection( &m_BufferLock );
if(m_pCurrentFramePixels)
CalcColors(); // read picture and calculate colors
LeaveCriticalSection( &m_BufferLock );
}
}
return 0;
}
#endif
void CAtmoExternalCaptureInput::CalcColors()
{
// take data from m_CurrentFrameHeader and m_pCurrentFramePixels .. process for atmo ...
tHSVColor HSV_Img[IMAGE_SIZE];
tRGBColor pixelColor;
int srcIndex,index = 0;
memset(&HSV_Img,0,sizeof(HSV_Img));
// msg_Dbg( m_pLog, "CalcColors start...");
if((m_CurrentFrameHeader.biWidth == CAP_WIDTH) && (m_CurrentFrameHeader.biHeight == CAP_HEIGHT))
{
// HSVI = HSV Image allready in right format just copy the easiest task
// und weiterverarbeiten lassen
#ifdef _ATMO_VLC_PLUGIN_
if(m_CurrentFrameHeader.biCompression == VLC_FOURCC('H','S','V','I'))
#else
if(m_CurrentFrameHeader.biCompression == MakeDword('H','S','V','I'))
#endif
{
memcpy( &HSV_Img, m_pCurrentFramePixels, CAP_WIDTH * CAP_HEIGHT * sizeof(tHSVColor));
}
else if(m_CurrentFrameHeader.biCompression == BI_RGB)
{
if(m_CurrentFrameHeader.biBitCount == 16)
{
unsigned short *buffer = (unsigned short *)m_pCurrentFramePixels;
for(int y=0;y<CAP_HEIGHT;y++)
{
srcIndex = y * CAP_WIDTH;
for(int x=0;x<CAP_WIDTH;x++)
{
pixelColor.b = (buffer[srcIndex] & 31) << 3;
pixelColor.g = ((buffer[srcIndex] >> 5) & 31) << 3;
pixelColor.r = ((buffer[srcIndex] >> 10) & 63) << 2;
srcIndex++;
HSV_Img[index++] = RGB2HSV(pixelColor);
}
}
}
else if(m_CurrentFrameHeader.biBitCount == 24)
{
for(int y=0;y<CAP_HEIGHT;y++)
{
srcIndex = y * (CAP_WIDTH*3);
for(int x=0;x<CAP_WIDTH;x++)
{
pixelColor.b = ((unsigned char *)m_pCurrentFramePixels)[srcIndex++];
pixelColor.g = ((unsigned char *)m_pCurrentFramePixels)[srcIndex++];
pixelColor.r = ((unsigned char *)m_pCurrentFramePixels)[srcIndex++];
HSV_Img[index++] = RGB2HSV(pixelColor);
}
}
}
else if(m_CurrentFrameHeader.biBitCount == 32)
{
for(int y=0;y<CAP_HEIGHT;y++)
{
srcIndex = y * (CAP_WIDTH*4);
for(int x=0;x<CAP_WIDTH;x++)
{
pixelColor.b = ((unsigned char *)m_pCurrentFramePixels)[srcIndex++];
pixelColor.g = ((unsigned char *)m_pCurrentFramePixels)[srcIndex++];
pixelColor.r = ((unsigned char *)m_pCurrentFramePixels)[srcIndex++];
srcIndex++;
HSV_Img[index++] = RGB2HSV(pixelColor);
}
}
}
}
}
/*
else {
if the image color format wasn't recognized - the output
will be black (memset)
}
*/
/* remove the source buffer */
free( m_pCurrentFramePixels );
m_pCurrentFramePixels = NULL;
/*
now convert the pixeldata into one RGB trippel for each channel,
this is done by some very sophisticated methods and statistics ...
the only thing I know - the pixel priority is controled by some
gradients for each edge of the picture
(sorry I don't know how it exactly works because the formulas
are done by some one else...)
*/
//msg_Dbg( m_pLog, "CalcColors ende AddPacket...");
m_pAtmoDynData->getLivePacketQueue()->AddPacket( m_pAtmoColorCalculator->AnalyzeHSV( HSV_Img ) );
//msg_Dbg( m_pLog, "CalcColors ende AddPacket...done.");
}