~ubuntu-branches/ubuntu/hardy/avidemux/hardy

« back to all changes in this revision

Viewing changes to avidemux/ADM_userInterfaces/ADM_QT4/ADM_dialog/Q_encoding.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Matvey Kozhev
  • Date: 2007-12-18 13:53:04 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20071218135304-cdqec2lg2bglyz15
Tags: 1:2.4~preview3-0.0ubuntu1
* Upload to Ubuntu. (LP: #163287, LP: #126572)
* debian/changelog: re-added Ubuntu releases.
* debian/control:
  - Require debhelper >= 5.0.51 (for dh_icons) and imagemagick.
  - Build-depend on libsdl1.2-dev instead of libsdl-dev.
  - Build against newer libx264-dev. (LP: #138854)
  - Removed libamrnb-dev, not in Ubuntu yet.
* debian/rules:
  - Install all icon sizes, using convert (upstream installs none).
  - Added missing calls to dh_installmenu, dh_installman, dh_icons and
    dh_desktop.
* debian/menu, debian/avidemux-qt.menu:
  - Corrected package and executable names.
* debian/avidemux-common.install: Install icons.
* debian/avidemux.common.manpages: Install man/avidemux.1.
* debian/links, debian/avidemux-cli.links, debian/avidemux-gtk.links:
  - Link manpages to avidemux.1.gz.
* debian/install, debian/avidemux-qt.install, debian/avidemux-gtk.desktop,
  debian/avidemux-qt.desktop: Install desktop files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
    copyright            : (C) 2001 by mean
 
3
    email                : fixounet@free.fr
 
4
 ***************************************************************************/
 
5
 
 
6
/***************************************************************************
 
7
 *                                                                         *
 
8
 *   This program is free software; you can redistribute it and/or modify  *
 
9
 *   it under the terms of the GNU General Public License as published by  *
 
10
 *   the Free Software Foundation; either version 2 of the License, or     *
 
11
 *   (at your option) any later version.                                   *
 
12
 *                                                                         *
 
13
 ***************************************************************************/
 
14
#include "config.h"
 
15
#include <stdio.h>
 
16
#include <stdlib.h>
 
17
#include <math.h>
 
18
 
 
19
#define Ui_Dialog Ui_encodingDialog
 
20
#include "ui_encoding.h"
 
21
#undef Ui_Dialog 
 
22
 
 
23
#include "default.h"
 
24
#include "prefs.h"
 
25
#include "ADM_assert.h"
 
26
#include "ADM_osSupport/ADM_misc.h"
 
27
#include "DIA_working.h"
 
28
#include "DIA_encoding.h"
 
29
#include "ADM_toolkit/toolkit.hxx"
 
30
#include "ADM_libraries/ADM_utilities/avidemutils.h"
 
31
 
 
32
extern void UI_purge(void);
 
33
static int stopReq=0;
 
34
class encodingWindow : public QDialog
 
35
{
 
36
     Q_OBJECT
 
37
 
 
38
 public:
 
39
     encodingWindow();
 
40
     Ui_encodingDialog ui;
 
41
 public slots:
 
42
        void buttonPressed(void ) { printf("StopReq\n");stopReq=1;}
 
43
 
 
44
        void priorityChanged(int priorityLevel)
 
45
        {
 
46
        #ifndef ADM_WIN32
 
47
                if (getuid() != 0)
 
48
                {
 
49
                        ui.comboBoxPriority->disconnect(SIGNAL(currentIndexChanged(int)));
 
50
                        ui.comboBoxPriority->setCurrentIndex(2);
 
51
                        connect(ui.checkBoxShutdown, SIGNAL(currentIndexChanged(int)), this, SLOT(priorityChanged(int)));
 
52
 
 
53
                        GUI_Error_HIG(QT_TR_NOOP("Privileges Required"), QT_TR_NOOP( "Root privileges are required to perform this operation."));
 
54
 
 
55
                        return;
 
56
                }
 
57
        #endif
 
58
 
 
59
                setpriority(PRIO_PROCESS, 0, ADM_getNiceValue(priorityLevel));
 
60
        }
 
61
 
 
62
        void shutdownChanged(int state)
 
63
        {
 
64
        #ifndef ADM_WIN32
 
65
                if (getuid() != 0)
 
66
                {
 
67
                        ui.checkBoxShutdown->disconnect(SIGNAL(stateChanged(int)));
 
68
                        ui.checkBoxShutdown->setCheckState(Qt::Unchecked);
 
69
                        connect(ui.checkBoxShutdown, SIGNAL(stateChanged(int)), this, SLOT(shutdownChanged(int)));
 
70
 
 
71
                        GUI_Error_HIG(QT_TR_NOOP("Privileges Required"), QT_TR_NOOP( "Root privileges are required to perform this operation."));
 
72
                }
 
73
        #endif
 
74
        }
 
75
 
 
76
 
 
77
 private slots:
 
78
 private:
 
79
};
 
80
 
 
81
 
 
82
encodingWindow::encodingWindow()     : QDialog()
 
83
 {
 
84
        ui.setupUi(this);
 
85
 
 
86
#ifndef ADM_WIN32
 
87
        //check for root privileges
 
88
        if (getuid() == 0)
 
89
        {
 
90
                // set priority to normal, regardless of preferences
 
91
                ui.comboBoxPriority->setCurrentIndex(2);
 
92
        }
 
93
#endif
 
94
 
 
95
        connect(ui.checkBoxShutdown, SIGNAL(stateChanged(int)), this, SLOT(shutdownChanged(int)));
 
96
        connect( (ui.pushButton),SIGNAL(pressed()),this,SLOT(buttonPressed()));
 
97
        connect(ui.comboBoxPriority, SIGNAL(currentIndexChanged(int)), this, SLOT(priorityChanged(int)));
 
98
 
 
99
        // set priority
 
100
        uint32_t priority;
 
101
 
 
102
        prefs->get(PRIORITY_ENCODING,&priority);        
 
103
 
 
104
#ifndef ADM_WIN32
 
105
        // check for root privileges
 
106
        if (getuid() == 0)
 
107
        {
 
108
                ui.comboBoxPriority->setCurrentIndex(priority);
 
109
        }
 
110
#else
 
111
        ui.comboBoxPriority->setCurrentIndex(priority);
 
112
#endif
 
113
 }
 
114
//*******************************************
 
115
#define WIDGET(x) (window->ui.x)
 
116
#define WRITEM(x,y) window->ui.x->setText(y)
 
117
#define WRITE(x) WRITEM(x,string)
 
118
/*************************************/
 
119
static char string[80];
 
120
static encodingWindow *window=NULL;
 
121
DIA_encoding::DIA_encoding( uint32_t fps1000 )
 
122
{
 
123
uint32_t useTray=0;
 
124
 
 
125
 
 
126
        ADM_assert(window==NULL);
 
127
        stopReq=0;
 
128
        _lastnb=0;
 
129
        _totalSize=0;
 
130
        _audioSize=0;
 
131
        _videoSize=0;
 
132
        _current=0;
 
133
        window=new encodingWindow();
 
134
        setFps(fps1000);
 
135
                _originalPriority=getpriority(PRIO_PROCESS, 0);
 
136
        _lastTime=0;
 
137
        _lastFrame=0;
 
138
        _fps_average=0;
 
139
        _total=1000;
 
140
 
 
141
         window->setModal(TRUE);
 
142
         window->show();
 
143
 
 
144
}
 
145
/**
 
146
    \fn setFps(uint32_t fps)
 
147
    \brief Memorize fps, it will be used later for bitrate computation
 
148
*/
 
149
 
 
150
void DIA_encoding::setFps(uint32_t fps)
 
151
{
 
152
        _roundup=(uint32_t )floor( (fps+999)/1000);
 
153
        _fps1000=fps;
 
154
        ADM_assert(_roundup<MAX_BR_SLOT);
 
155
        memset(_bitrate,0,sizeof(_bitrate));
 
156
        _bitrate_sum=0;
 
157
        _average_bitrate=0;
 
158
        
 
159
}
 
160
 
 
161
void DIA_stop( void)
 
162
{
 
163
        printf("Stop request\n");
 
164
        stopReq=1;
 
165
}
 
166
DIA_encoding::~DIA_encoding( )
 
167
{
 
168
        bool shutdownRequired = (window->ui.checkBoxShutdown->checkState() == Qt::Checked);
 
169
 
 
170
        setpriority(PRIO_PROCESS, 0, _originalPriority);
 
171
 
 
172
        if(window) delete window;
 
173
        window=NULL;
 
174
 
 
175
        if (shutdownRequired && !stopReq)
 
176
        {
 
177
                DIA_working *work=new DIA_working(QT_TR_NOOP("Shutting down"));
 
178
                bool performShutdown=true;
 
179
 
 
180
                for(int i = 0; i <= 30; i++)
 
181
                {
 
182
                        if (work->isAlive())
 
183
                        {
 
184
                                GUI_Sleep(1000);
 
185
                                work->update(i, 30);
 
186
                        }
 
187
                        else
 
188
                        {
 
189
                                performShutdown=false;
 
190
                                break;
 
191
                        }
 
192
                }
 
193
 
 
194
                if (performShutdown && shutdown())
 
195
                {
 
196
                        GUI_Sleep(5000);
 
197
                }
 
198
 
 
199
                delete work;
 
200
        }
 
201
}
 
202
/**
 
203
    \fn setPhasis(const char *n)
 
204
    \brief Display parameters as phasis
 
205
*/
 
206
 
 
207
void DIA_encoding::setPhasis(const char *n)
 
208
{
 
209
          ADM_assert(window);
 
210
          WRITEM(labelPhasis,n);
 
211
 
 
212
}
 
213
/**
 
214
    \fn setAudioCodec(const char *n)
 
215
    \brief Display parameters as audio codec
 
216
*/
 
217
 
 
218
void DIA_encoding::setAudioCodec(const char *n)
 
219
{
 
220
          ADM_assert(window);
 
221
          WRITEM(labelAudCodec,n);
 
222
}
 
223
/**
 
224
    \fn setCodec(const char *n)
 
225
    \brief Display parameters as video codec
 
226
*/
 
227
 
 
228
void DIA_encoding::setCodec(const char *n)
 
229
{
 
230
          ADM_assert(window);
 
231
          WRITEM(labelVidCodec,n);
 
232
}
 
233
/**
 
234
    \fn setBitrate(uint32_t br,uint32_t globalbr)
 
235
    \brief Display parameters as instantaneous bitrate and average bitrate
 
236
*/
 
237
 
 
238
void DIA_encoding::setBitrate(uint32_t br,uint32_t globalbr)
 
239
{
 
240
          ADM_assert(window);
 
241
          snprintf(string,79,"%lu kB/s",br,globalbr);
 
242
          WRITE(labelVidBitrate);
 
243
 
 
244
}
 
245
/**
 
246
    \fn reset(void)
 
247
    \brief Reset everything, used for 2pass
 
248
*/
 
249
 
 
250
void DIA_encoding::reset(void)
 
251
{
 
252
          ADM_assert(window);
 
253
          _totalSize=0;
 
254
          _videoSize=0;
 
255
          _current=0;
 
256
}
 
257
/**
 
258
    \fn setContainer(const char *container)
 
259
    \brief Display parameter as container field
 
260
*/
 
261
 
 
262
void DIA_encoding::setContainer(const char *container)
 
263
{
 
264
        ADM_assert(window);
 
265
        WRITEM(labelContainer,container);
 
266
}
 
267
#define  ETA_SAMPLE_PERIOD 60000 //Use last n millis to calculate ETA
 
268
#define  GUI_UPDATE_RATE 500  
 
269
/**
 
270
    \fn setFrame(uint32_t nb,uint32_t size, uint32_t quant,uint32_t total)
 
271
    \brief Recompute and update everything concering video
 
272
*/
 
273
 
 
274
void DIA_encoding::setFrame(uint32_t nb,uint32_t size, uint32_t quant,uint32_t total)
 
275
{
 
276
          _total=total;
 
277
          _videoSize+=size;
 
278
          if(nb < _lastnb || _lastnb == 0) // restart ?
 
279
           {
 
280
                _lastnb = nb;
 
281
                clock.reset();
 
282
                _lastTime=clock.getElapsedMS();
 
283
                _lastFrame=0;
 
284
                _fps_average=0;
 
285
                _videoSize=size;
 
286
    
 
287
                _nextUpdate = _lastTime + GUI_UPDATE_RATE;
 
288
                _nextSampleStartTime=_lastTime + ETA_SAMPLE_PERIOD;
 
289
                _nextSampleStartFrame=0;
 
290
          } 
 
291
          _lastnb = nb;
 
292
          _current=nb%_roundup;
 
293
          _bitrate[_current].size=size;
 
294
          _bitrate[_current].quant=quant;
 
295
}
 
296
/**
 
297
    \fn updateUI(void)
 
298
    \brief Recompute and update all fields, especially ETA
 
299
*/
 
300
 
 
301
void DIA_encoding::updateUI(void)
 
302
{
 
303
uint32_t tim;
 
304
 
 
305
           ADM_assert(window);
 
306
           //
 
307
           //   nb/total=timestart/totaltime -> total time =timestart*total/nb
 
308
           //
 
309
           //
 
310
           
 
311
           UI_purge();
 
312
          if(!_lastnb) return;
 
313
          
 
314
          tim=clock.getElapsedMS();
 
315
          if(_lastTime > tim) return;
 
316
          if( tim < _nextUpdate) return ; 
 
317
          _nextUpdate = tim+GUI_UPDATE_RATE;
 
318
  
 
319
          snprintf(string,79,"%lu",_lastnb);
 
320
          WIDGET(labelFrame)->setText(string);
 
321
 
 
322
          snprintf(string,79,"%lu",_total);
 
323
          WIDGET(labelTotalFrame)->setText(string);
 
324
 
 
325
                  snprintf(string,79,"%lu",_total);
 
326
          WIDGET(labelTotalFrame)->setText(string);
 
327
 
 
328
          // Average bitrate  on the last second
 
329
          uint32_t sum=0,aquant=0,gsum;
 
330
          for(int i=0;i<_roundup;i++)
 
331
          {
 
332
            sum+=_bitrate[i].size;
 
333
            aquant+=_bitrate[i].quant;
 
334
          }
 
335
          
 
336
          aquant/=_roundup;
 
337
 
 
338
          sum=(sum*8)/1000;
 
339
 
 
340
          // Now compute global average bitrate
 
341
          float whole=_videoSize,second;
 
342
            second=_lastnb;
 
343
            second/=_fps1000;
 
344
            second*=1000;
 
345
           
 
346
          whole/=second;
 
347
          whole/=1000;
 
348
          whole*=8;
 
349
      
 
350
          gsum=(uint32_t)whole;
 
351
 
 
352
          setBitrate(sum,gsum);
 
353
          setQuantIn(aquant);
 
354
 
 
355
          // compute fps
 
356
          uint32_t deltaFrame, deltaTime;
 
357
          deltaTime=tim-_lastTime;
 
358
          deltaFrame=_lastnb-_lastFrame;
 
359
 
 
360
          _fps_average    =(float)( deltaFrame*1000.0F / deltaTime ); 
 
361
 
 
362
          snprintf(string,79,"%.2f",_fps_average);
 
363
          WIDGET(labelFps)->setText(string);
 
364
  
 
365
          uint32_t   hh,mm,ss;
 
366
  
 
367
            double framesLeft=(_total-_lastnb);
 
368
 
 
369
                        ms2time(tim,&hh,&mm,&ss);
 
370
                        snprintf(string,79,"%02d:%02d:%02d",hh,mm,ss);
 
371
                        WIDGET(labelElapsed)->setText(string);
 
372
 
 
373
            WIDGET(labelETA)->setText(ms2timedisplay((uint32_t) floor(0.5 + deltaTime * framesLeft / deltaFrame)));
 
374
  
 
375
           // Check if we should move on to the next sample period
 
376
          if (tim >= _nextSampleStartTime + ETA_SAMPLE_PERIOD ) {
 
377
            _lastTime=_nextSampleStartTime;
 
378
            _lastFrame=_nextSampleStartFrame;
 
379
            _nextSampleStartTime=tim;
 
380
            _nextSampleStartFrame=0;
 
381
          } else if (tim >= _nextSampleStartTime && _nextSampleStartFrame == 0 ) {
 
382
            // Store current point for use later as the next sample period.
 
383
            //
 
384
            _nextSampleStartTime=tim;
 
385
            _nextSampleStartFrame=_lastnb;
 
386
          }
 
387
          // update progress bar
 
388
            float f=_lastnb*100;
 
389
            f=f/_total;
 
390
            WIDGET(progressBar)->setValue((int)f);
 
391
          
 
392
        _totalSize=_audioSize+_videoSize;
 
393
        setSize(_totalSize>>20);
 
394
        setAudioSizeIn((_audioSize>>20));
 
395
        setVideoSizeIn((_videoSize>>20));
 
396
        UI_purge();
 
397
 
 
398
}
 
399
/**
 
400
    \fn setQuantIn(int size)
 
401
    \brief display parameter as quantizer
 
402
*/
 
403
 
 
404
void DIA_encoding::setQuantIn(int size)
 
405
{
 
406
          ADM_assert(window);
 
407
          sprintf(string,"%lu",size);
 
408
          WRITE(labelQz);
 
409
 
 
410
}
 
411
/**
 
412
    \fn setSize(int size)
 
413
    \brief display parameter as total size
 
414
*/
 
415
 
 
416
void DIA_encoding::setSize(int size)
 
417
{
 
418
          ADM_assert(window);
 
419
          sprintf(string,"%lu MB",size);
 
420
          WRITE(labelTotalSize);
 
421
 
 
422
}
 
423
/**
 
424
    \fn setAudioSizeIn(int size)
 
425
    \brief display parameter as audio size
 
426
*/
 
427
 
 
428
void DIA_encoding::setAudioSizeIn(int size)
 
429
{
 
430
          ADM_assert(window);
 
431
          sprintf(string,"%lu MB",size);
 
432
          WRITE(labelAudioSize);
 
433
 
 
434
}
 
435
/**
 
436
    \fn setVideoSizeIn(int size)
 
437
    \brief display parameter as video size
 
438
*/
 
439
 
 
440
void DIA_encoding::setVideoSizeIn(int size)
 
441
{
 
442
          ADM_assert(window);
 
443
          sprintf(string,"%lu MB",size);
 
444
          WRITE(labelVideoSize);
 
445
 
 
446
}
 
447
/**
 
448
    \fn setAudioSize( uint32_t size)
 
449
    \brief set the total audio size as per parameter
 
450
*/
 
451
 
 
452
void DIA_encoding::setAudioSize(uint32_t size)
 
453
{
 
454
      _audioSize=size;
 
455
}
 
456
/**
 
457
    \fn isAlive( void )
 
458
    \brief return 0 if the window was killed or cancel button press, 1 otherwisearchForward
 
459
*/
 
460
uint8_t DIA_encoding::isAlive( void )
 
461
{
 
462
        updateUI();
 
463
 
 
464
        if(stopReq)
 
465
        {
 
466
          if(GUI_Alternate((char*)QT_TR_NOOP("The encoding is paused. Do you want to resume or abort?"),
 
467
                              (char*)QT_TR_NOOP("Resume"),(char*)QT_TR_NOOP("Abort")))
 
468
                 {
 
469
                         stopReq=0;
 
470
                 }
 
471
        }
 
472
 
 
473
        if(!stopReq) return 1;          
 
474
 
 
475
        return 0;
 
476
}
 
477
 
 
478
//********************************************
 
479
//EOF