]> git.sesse.net Git - vlc/blob - modules/video_filter/atmo/AtmoLiveView.cpp
98631ce819dc156167bd7be0c9278acbf7efc8be
[vlc] / modules / video_filter / atmo / AtmoLiveView.cpp
1 /*
2  * AtmoLiveView.cpp:  this effect outputs colors as result of a picture
3  * content (most complex effect) see thread.c of the linux VDR version -
4  * to fully understand what happens here..
5  *
6  * See the README.txt file for copyright information and how to reach the author(s).
7  *
8  * $Id$
9  */
10 #include "AtmoDefs.h"
11 #include "AtmoLiveView.h"
12 #include "AtmoOutputFilter.h"
13 #include "AtmoTools.h"
14
15 #if defined(_ATMO_VLC_PLUGIN_)
16 #  include <vlc_common.h>
17 #else
18 #  include "AtmoGdiDisplayCaptureInput.h"
19 #endif
20
21 #include "AtmoExternalCaptureInput.h"
22
23
24
25 #if defined(_ATMO_VLC_PLUGIN_)
26
27 CAtmoLiveView::CAtmoLiveView(CAtmoDynData *pAtmoDynData) :
28                CThread(pAtmoDynData->getAtmoFilter())
29 {
30     this->m_pAtmoDynData    = pAtmoDynData;
31 }
32
33 #else
34
35 CAtmoLiveView::CAtmoLiveView(CAtmoDynData *pAtmoDynData)
36 {
37     this->m_pAtmoDynData  = pAtmoDynData;
38 }
39
40 #endif
41
42
43 CAtmoLiveView::~CAtmoLiveView(void)
44 {
45 }
46
47
48 DWORD CAtmoLiveView::Execute(void)
49 {
50 #if defined(_ATMO_VLC_PLUGIN_)
51     mtime_t ticks;
52     mtime_t t;
53     mtime_t packet_time;
54 #else
55     DWORD ticks;
56     DWORD t;
57     DWORD packet_time;
58 #endif
59     int i_frame_counter = -1;
60
61     pColorPacket ColorPacket;
62     pColorPacket PreviousPacket = NULL;
63
64     CAtmoConnection *pAtmoConnection = this->m_pAtmoDynData->getAtmoConnection();
65     if((pAtmoConnection == NULL) || (pAtmoConnection->isOpen() == ATMO_FALSE)) return 0;
66
67     CAtmoConfig *pAtmoConfig = this->m_pAtmoDynData->getAtmoConfig();
68
69     /*
70        this object does post processing of the pixel data
71        like jump /scenechange detection fading over the colors
72     */
73     CAtmoOutputFilter *filter = new CAtmoOutputFilter( this->m_pAtmoDynData->getAtmoConfig() );
74     CAtmoPacketQueue *pPacketQueue = this->m_pAtmoDynData->getLivePacketQueue();
75
76     int frameDelay = pAtmoConfig->getLiveView_FrameDelay();
77
78 #if defined(_ATMO_VLC_PLUGIN_)
79     /*
80      because time function of vlc are working with us values instead of ms
81     */
82     frameDelay = frameDelay * 1000;
83 #endif
84
85     /*
86       wait for the first frame to go in sync with the other thread
87     */
88     t = get_time;
89
90     if( pPacketQueue->WaitForNextPacket(3000) )
91     {
92         if( frameDelay > 0 )
93             do_sleep( frameDelay );
94 #if defined(_ATMO_VLC_PLUGIN_)
95         msg_Dbg( m_pAtmoThread, "First Packet got %d ms", (get_time - t) / 1000  );
96 #endif
97     }
98
99     while(this->m_bTerminated == ATMO_FALSE)
100     {
101         i_frame_counter++;
102         if(i_frame_counter == 50) i_frame_counter = 0;
103
104         /* grab current Packet from InputQueue (working as FIFO)! */
105 #if defined(_ATMO_VLC_PLUGIN_)
106         ColorPacket = pPacketQueue->GetNextPacket(get_time - frameDelay, (i_frame_counter == 0), m_pAtmoThread, packet_time);
107 #else
108         ColorPacket = pPacketQueue->GetNextPacket(get_time - frameDelay, (i_frame_counter == 0), packet_time);
109 #endif
110         if(ColorPacket)
111         {
112             /*
113               create a packet copy - for later reuse if the input is slower than 25fps
114             */
115             if(PreviousPacket && (PreviousPacket->numColors == ColorPacket->numColors))
116                 CopyColorPacket(ColorPacket, PreviousPacket)
117             else {
118                 delete (char *)PreviousPacket;
119                 DupColorPacket(PreviousPacket, ColorPacket )
120             }
121         } else {
122             /*
123               packet queue was empty for the given point of time
124             */
125             if(i_frame_counter == 0)
126             {
127 #if defined(_ATMO_VLC_PLUGIN_)
128                 msg_Dbg( m_pAtmoThread, "wait for delayed packet..." );
129 #endif
130                 t = get_time;
131                 if( pPacketQueue->WaitForNextPacket(200) )
132                 {
133                     if( frameDelay > 0 )
134                         do_sleep( frameDelay );
135 #if defined(_ATMO_VLC_PLUGIN_)
136                     msg_Dbg( m_pAtmoThread, "got delayed packet %d ms", (mdate() - t) / 1000  );
137 #endif
138                     continue;
139                 }
140             }
141             /*
142               reuse previous color packet
143             */
144             DupColorPacket(ColorPacket, PreviousPacket)
145         }
146
147         ticks = get_time;
148
149         if(ColorPacket)
150         {
151             /* pass it through the outputfilters! */
152             // Info Filtering will possible free the colorpacket and alloc a new one!
153             ColorPacket = filter->Filtering(ColorPacket);
154
155             /* apply gamma correction - only if the hardware isnt capable doing this */
156             ColorPacket = CAtmoTools::ApplyGamma(pAtmoConfig, ColorPacket);
157
158             /*
159             apply white calibration - only if it is not
160             done by the hardware
161             */
162             if(pAtmoConfig->isUseSoftwareWhiteAdj())
163                 ColorPacket = CAtmoTools::WhiteCalibration(pAtmoConfig, ColorPacket);
164
165             /* send color data to the the hardware... */
166             pAtmoConnection->SendData(ColorPacket);
167
168             delete (char *)ColorPacket;
169         }
170
171         /*
172             calculate RunTime of thread abbove (doesn't work well - so
173             this threads comes out of sync with Image producer and the
174             framerate (25fps) drifts away
175         */
176 #if defined(_ATMO_VLC_PLUGIN_)
177         ticks = ((mdate() - ticks) + 999)/1000;
178 #else
179         ticks = GetTickCount() - ticks;
180 #endif
181         if(ticks < 40)
182         {
183             if( ThreadSleep( 40 - ticks ) == ATMO_FALSE )
184                 break;
185         }
186     }
187
188 #if defined(_ATMO_VLC_PLUGIN_)
189     msg_Dbg( m_pAtmoThread, "DWORD CAtmoLiveView::Execute(void) terminates");
190     pPacketQueue->ShowQueueStatus( m_pAtmoThread );
191 #endif
192
193     delete (char *)PreviousPacket;
194
195     delete filter;
196     return 0;
197 }
198