Debian Codegraph @

samples/qtrecompress/recwnd.cpp

raw
1 #include "recwnd.h"
2 #include "recwnd.moc"
3
4 #include "recompressor.h"
5
6 #include <qprogressbar.h>
7 #include <qpushbutton.h>
8 #include <qlabel.h>
9 #include <qtabwidget.h>
10
11 #if QT_VERSION > 220
12 //#include <qthread.h>
13 #endif
14
15 #include <stdio.h>
16
17 Progress::Progress(unsigned int size)
18 :m_pValues(0), m_pValuesFlags(0), m_uiPos(0),
19 m_uiSize(size), m_bValidMax(false)
20 {
21 }
22
23 Progress::~Progress()
24 {
25 delete[] m_pValues;
26 delete[] m_pValuesFlags;
27 }
28
29 void Progress::insert(unsigned int v, int flags)
30 {
31 if (!m_pValues)
32 {
33 m_pValues = new unsigned int[m_uiSize];
34 m_pValuesFlags = new int[m_uiSize];
35 memset(m_pValues, 0, sizeof(unsigned int) * m_uiSize);
36 }
37
38 if (m_pValues[m_uiPos] == m_uiMax)
39 m_bValidMax = false;
40
41 m_pValues[m_uiPos] = v;
42 m_pValuesFlags[m_uiPos] = flags;
43 m_uiPos = (m_uiPos + 1) % m_uiSize;
44 // cout << "Insert " << v << ", " << flags << " p: " << m_uiPos << endl;
45 }
46
47 unsigned int Progress::getMax()
48 {
49 if (!m_bValidMax)
50 {
51 m_uiMax = 0;
52 for (unsigned i = 0; i < m_uiSize; i++)
53 if (m_pValues[i] > m_uiMax)
54 m_uiMax = m_pValues[i];
55 m_bValidMax = true;
56 }
57 return m_uiMax;
58 }
59
60
61 /*
62 * Constructs a RecWindow which is a child of 'parent', with the
63 * name 'name' and widget flags set to 'f'
64 *
65 * The dialog will by default be modeless, unless you set 'modal' to
66 * TRUE to construct a modal dialog.
67 */
68 RecWindow::RecWindow( QWidget* parent, RecKernel* kern )
69 : RecWnd_p( parent, "Record window", true, 0 ), kernel(kern),
70 m_Progress(PROGRESS_SIZE)
71 {
72 progress = 1;
73 elapsed = estimated = 1;
74 fsize = 0;
75 curVideoFrame = 0;
76 curAudioSample = 0;
77 estimated = 0;
78 fsize = 0;
79 videoSize = audioSize = 0;
80 totalAudioSamples = totalVideoFrames = 0;
81 kernel->setCallback(this);
82 if (kernel->startRecompress())
83 accept();
84 }
85
86 /*
87 * Destroys the object and frees any allocated resources
88 */
89 RecWindow::~RecWindow()
90 {
91 // no need to delete child widgets, Qt does it all for us
92 }
93
94 /*
95 * public slot
96 */
97 void RecWindow::cancelProcess()
98 {
99 kernel->stopRecompress();
100 accept();
101 }
102 /*
103 * public slot
104 */
105 void RecWindow::pauseProcess()
106 {
107 if (kernel->pauseRecompress() != 0)
108 m_pPause->setText( tr( "&Resume" ) );
109 else
110 m_pPause->setText( tr( "&Pause" ) );
111 }
112 /*
113 * public slot
114 */
115 void RecWindow::currentChanged(QWidget* w)
116 {
117 #if QT_VERSION >= 300
118 //cout << "INDEX " << m_pTabWidget->indexOf(w) << endl;
119 #endif
120 }
121
122
123 void RecWindow::finished()
124 {
125 //QThread::postEvent(this, new QEvent(QEvent::Type(QEvent::User+1)));
126 qApp->postEvent(this, new QEvent(QEvent::Type(QEvent::User+1)));
127 #if QT_VERSION > 220
128 qApp->wakeUpGuiThread();
129 #endif
130 }
131
132 void RecWindow::setNewState(double progress, double elapsed, int64_t fsize)
133 {
134 this->progress=progress;
135 this->elapsed=elapsed;
136 this->estimated = (progress > 0.0001) ? elapsed / progress : 0.0;
137 this->fsize=fsize;
138
139 //QThread::postEvent(this, new QEvent(QEvent::User));
140 qApp->postEvent(this, new QEvent(QEvent::User));
141 #if QT_VERSION > 220
142 qApp->wakeUpGuiThread();
143 #endif
144 //printf("seupdate2\n");
145 /*
146 if (progress > 0) qApp->sendPostedEvents();
147
148 #if QT_VERSION >= 300
149 qApp->flush();
150 #else
151 qApp->flushX();
152 #endif
153 */
154 }
155
156 void RecWindow::setTotal(framepos_t vtotal, framepos_t atotal, double vtm, double atm)
157 {
158 //printf("SETTOTAL vt:%d at:%d vtm:%f atm%f\n", vtotal, atotal, vtm, atm);
159 totalVideoFrames = vtotal;
160 totalAudioSamples = atotal;
161 startVideoTime = vtm - 1.0;
162 startAudioTime = vtm - 1.0;
163 }
164
165 void RecWindow::addVideo(framepos_t vframe, unsigned int vsize, double vtm, bool keyframe)
166 {
167 curVideoFrame = vframe;
168 curVideoTime = vtm;
169 videoSize += vsize;
170 m_Progress.insert(vsize, keyframe);
171 }
172
173 void RecWindow::addAudio(framepos_t asample, unsigned int asize, double atime)
174 {
175 curAudioSample = asample;
176 audioSize += asize;
177 curAudioTime = atime;
178 }
179
180 bool RecWindow::event(QEvent* e)
181 {
182 if (e->type()==QEvent::User)
183 {
184 //printf("update1\n");
185 return update();
186 }
187 if (e->type()==QEvent::Type(QEvent::User+1))
188 {
189 m_pPause->hide();
190 m_pStop->setText( tr( "&Close" ) );
191 update();
192 //accept();
193 #if QT_VERSION > 220
194 qApp->wakeUpGuiThread();
195 #endif
196 //return true;
197 }
198 return QWidget::event(e);
199 }
200
201 static inline char* sprintfTimeString(char* buf, double tm)
202 {
203 sprintf(buf, "%d:%.02d:%.02d", int(tm/3600),
204 int(tm/60)%60, int(tm)%60);
205 return buf;
206 }
207
208 bool RecWindow::update()
209 {
210 m_pProgress->setProgress(int(progress*1000));
211
212 #if QT_VERSION > 220
213 if (m_pTabWidget->currentPageIndex() == 1)
214 return updateGraphs();
215 #endif
216
217 char s[256];
218 char buf[20];
219 double proj = fsize / progress;
220 for (int i = 0; i < LAST_LABEL; i++)
221 {
222 switch (i)
223 {
224 case CURRENT_VIDEO_FRAME:
225 sprintf(s, "%d/%d", curVideoFrame, totalVideoFrames);
226 break;
227 case CURRENT_AUDIO_SAMPLE:
228 sprintf(s, "%d/%d", curAudioSample, totalAudioSamples);
229 break;
230 case VIDEO_DATA:
231 sprintf(s, "%d KB (%d kbps)", int(videoSize/1024LL),
232 int(videoSize * 8 / 1000LL/(curVideoTime - startVideoTime)));
233 break;
234 case AUDIO_DATA:
235 //printf("SURA %f SA %f lsize %lld\n", curAudioTime, startAudioTime, audioSize);
236 sprintf(s, "%d KB (%d kbps)", int(audioSize/1024LL),
237 int(audioSize * 8 / 1000LL/(curAudioTime - startAudioTime)));
238 break;
239 case CURRENT_FILE_SIZE:
240 sprintf(s, "%" PRId64, fsize);
241 break;
242 case PROJECTED_FILE_SIZE:
243 if (proj > (10 * 1024 * 1024))
244 sprintf(s, "%.3f MB", proj/(1024*1024LL));
245 else
246 sprintf(s, "%d KB", int(proj/1024LL));
247 break;
248 case VIDEO_RENDERING_RATE:
249 sprintf(s, "%.3f fps", curVideoFrame / elapsed);
250 break;
251 case TIME_ELAPSED:
252 sprintfTimeString(s, elapsed);
253 break;
254 case TOTAL_TIME_ESTIMATED:
255 sprintfTimeString(s, estimated);
256 break;
257 }
258 m_pText[i]->setText(s);
259 }
260 return true;
261 }
262
263 bool RecWindow::updateGraphs()
264 {
265 // for this moment just some experimental code
266 //cout << "******MAX " << m_Progress.getMax() << endl;
267 char pcElapsed[20];
268 char pcEstimated[20];
269 sprintfTimeString(pcElapsed, elapsed);
270 sprintfTimeString(pcEstimated, estimated);
271
272 printf("%3.2f\%%, elapsed: %s, remaining: %s, "
273 "estimated file size: %d KB\n",
274 progress*100, pcElapsed, pcEstimated,
275 int(fsize/progress/1024LL));
276
277 #if 0
278 char s[1024];
279 static int call=0;
280 int iHeight;
281 int iWidth;
282 int iMaxFrame=0;
283 int i;
284 call+=10;
285 m_pProgress->lock();
286
287 // 2 because we have buffer of 50 frames
288 2*m_pProgress->buffer_occupancies[(m_pProgress->buffer_head+m_pProgress->history_size-1)%m_pProgress->history_size]
289 );
290
291 QPainter* qp = new QPainter(Frame3_2);
292 iHeight=Frame3_2->height();
293 iWidth=Frame3_2->width();
294
295 iMaxFrame= m_pProgress->getMax()
296
297 if (iMaxFrame<10)
298 iMaxFrame=10;
299 else if (iMaxFrame<100)
300 iMaxFrame=100;
301 else if (iMaxFrame<1000)
302 iMaxFrame=1000;
303 else if (iMaxFrame<10000)
304 iMaxFrame=10000;
305 else if (iMaxFrame<100000)
306 iMaxFrame=100000;
307 else iMaxFrame=1000000;
308 if(iMaxFrame<1000)
309 sprintf(s, tr( "%d bytes" ), iMaxFrame);
310 else if(iMaxFrame<1000000)
311 sprintf(s, tr( "%d KB" ), iMaxFrame/1000);
312 else
313 sprintf(s, tr( "%d MB" ), iMaxFrame/1000000);
314 MaxFrameSizeText->setText(s);
315 for(i=iWidth; i>=0; i--)
316 {
317 int index = i-iWidth;
318 if(index<-m_pProgress->video_frame_len)
319 {
320 qp->setPen(QColor(0,0,0));
321 qp->moveTo(i, iHeight);
322 qp->lineTo(i, 0);
323 continue;
324 }
325 index += m_pProgress->video_frame_head;
326 while(index<0)
327 index += m_pProgress->history_size;
328 index %= m_pProgress->history_size;
329 float t=(m_pProgress->video_frame_sizes[index] & ~0x40000000)/(float)iMaxFrame;
330 if(t>1)t=1;
331 if(m_pProgress->video_frame_sizes[index] & 0x40000000)
332 qp->setPen(QColor(255, 0, 0));
333 else
334 qp->setPen(QColor(0, 255, 0));
335 qp->moveTo(i, iHeight);
336 qp->lineTo(i, iHeight*(1-t));
337 if(t<1)
338 {
339 qp->setPen(QColor(0, 0, 0));
340 qp->lineTo(i, 0);
341 }
342 }
343 finish:
344 m_pProgress->unlock();
345 #endif
346 return true;
347 }