2
* time status related function source file
4
* Copyright (c) 1999 Mark Taylor
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Library General Public
8
* License as published by the Free Software Foundation; either
9
* version 2 of the License, or (at your option) any later version.
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Library General Public License for more details.
16
* You should have received a copy of the GNU Library General Public
17
* License along with this library; if not, write to the
18
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19
* Boston, MA 02111-1307, USA.
23
#include "timestatus.h"
38
#if defined(CLOCKS_PER_SEC)
39
/* ANSI/ISO systems */
40
# define TS_CLOCKS_PER_SEC CLOCKS_PER_SEC
41
#elif defined(CLK_TCK)
42
/* Non-standard systems */
43
# define TS_CLOCKS_PER_SEC CLK_TCK
45
/* Older BSD systems */
46
# define TS_CLOCKS_PER_SEC HZ
48
# error no suitable value for TS_CLOCKS_PER_SEC
52
/* divide process time by TS_CLOCKS_PER_TIC to reduce overflow threshold */
53
#define TS_CLOCKS_PER_TIC ((CLOCKS_PER_SEC + 63) / 64)
54
#define TS_SECS_PER_TIC ((FLOAT) TS_CLOCKS_PER_TIC / TS_CLOCKS_PER_SEC)
56
/*********************************************************/
57
/* ts_real_time: real time elapsed in seconds */
58
/*********************************************************/
60
FLOAT ts_real_time (long frame)
64
static long double initial_time;
65
long double current_time;
68
gettimeofday (&t, NULL);
70
current_time = t.tv_sec + 1.e-6l * t.tv_usec;
72
initial_time = current_time;
74
return (FLOAT)(current_time - initial_time);
78
static time_t initial_time;
84
initial_time = current_time;
86
return (FLOAT) difftime (current_time, initial_time);
91
/*********************************************************/
92
/* ts_process_time: process time elapsed in seconds */
93
/*********************************************************/
94
FLOAT ts_process_time(long frame) {
95
static clock_t initial_tictime;
96
static clock_t previous_time;
99
#if ( defined(_MSC_VER) || defined(__BORLANDC__) )
101
{ static HANDLE hProcess;
102
FILETIME Ignored1, Ignored2, KernelTime, UserTime;
105
hProcess = GetCurrentProcess();
108
/* GetProcessTimes() always fails under Win9x */
109
if (GetProcessTimes(hProcess, &Ignored1, &Ignored2, &KernelTime, &UserTime)) {
110
LARGE_INTEGER Kernel;
113
Kernel.LowPart = KernelTime.dwLowDateTime;
114
Kernel.HighPart = KernelTime.dwHighDateTime;
115
User.LowPart = UserTime.dwLowDateTime;
116
User.HighPart = UserTime.dwHighDateTime;
118
current_time = (clock_t)((FLOAT)(Kernel.QuadPart + User.QuadPart) * TS_CLOCKS_PER_SEC / 10000000);
120
current_time = clock();
124
current_time = clock();
127
if( current_time < previous_time ) {
128
/* adjust initial_tictime for wrapped time */
129
/* whether clock_t is signed or unsigned */
130
initial_tictime -= ((previous_time / TS_CLOCKS_PER_TIC)
131
+ (current_time - previous_time) / TS_CLOCKS_PER_TIC);
132
if( current_time < 0 ) { /* adjust if clock_t is signed */
133
initial_tictime -= -(((clock_t) 1 << ((int)sizeof(clock_t) * 8 - 1))
134
/ TS_CLOCKS_PER_TIC);
137
previous_time = current_time;
139
current_time /= TS_CLOCKS_PER_TIC; /* convert process time to tics */
142
initial_tictime = current_time;
145
return (FLOAT)((FLOAT)(current_time - initial_tictime) * TS_SECS_PER_TIC);
148
#undef TS_SECS_PER_TIC
149
#undef TS_CLOCKS_PER_TIC
150
#undef TS_CLOCKS_PER_SEC
152
typedef struct ts_times {
159
/*********************************************************/
160
/* ts_calc_times: calculate time info (eta, speed, etc.) */
161
/*********************************************************/
162
void ts_calc_times(ts_times *tstime, int samp_rate, long frame, long frames,int framesize)
165
tstime->estimated = tstime->so_far * frames / frame;
166
if (samp_rate * tstime->estimated > 0) {
167
tstime->speed = frames * framesize / (samp_rate * tstime->estimated);
171
tstime->eta = tstime->estimated - tstime->so_far;
173
tstime->estimated = 0;
179
/*********************************************************/
180
/* timestatus: display encoding process time information */
181
/*********************************************************/
183
static void ts_time_decompose ( const unsigned long time_in_sec, const char padded_char )
185
unsigned long hour = time_in_sec / 3600;
186
unsigned int min = time_in_sec / 60 % 60;
187
unsigned int sec = time_in_sec % 60;
190
fprintf ( stderr, " %2u:%02u%c", min, sec, padded_char );
191
else if ( hour < 100 )
192
fprintf ( stderr, "%2lu:%02u:%02u%c", hour, min, sec, padded_char );
194
fprintf ( stderr, "%6lu h%c", hour, padded_char );
197
void timestatus ( unsigned long samp_rate,
198
unsigned long frameNum,
199
unsigned long totalframes,
203
ts_times process_time;
205
unsigned dropped_frames = samp_rate <= 16000 || samp_rate == 32000 ? 2 : 1;
206
/* ugly and nasty work around, only obscuring another bug? */
207
/* values of 1...3 are possible, why ??? */
209
real_time .so_far = ts_real_time (frameNum);
210
process_time.so_far = ts_process_time (frameNum);
212
if ( frameNum == 0 ) {
213
fputs ( " Frame | CPU time/estim | REAL time/estim | play/CPU | ETA \n"
214
" 0/ ( 0%)| 0:00/ : | 0:00/ : | . x| : \r", stderr );
218
ts_calc_times ( &real_time , samp_rate, frameNum, totalframes, framesize );
219
ts_calc_times ( &process_time, samp_rate, frameNum, totalframes, framesize );
221
if ( frameNum < totalframes - dropped_frames ) {
222
percent = (int) (100.0 * frameNum / (totalframes - dropped_frames) + 0.5 );
227
fprintf ( stderr, "\r%6ld/%-6ld", frameNum, totalframes - dropped_frames );
228
fprintf ( stderr, percent < 100 ? " (%2d%%)|" : "(%3.3d%%)|", percent );
229
ts_time_decompose ( (unsigned long)process_time.so_far , '/' );
230
ts_time_decompose ( (unsigned long)process_time.estimated, '|' );
231
ts_time_decompose ( (unsigned long)real_time .so_far , '/' );
232
ts_time_decompose ( (unsigned long)real_time .estimated, '|' );
233
fprintf ( stderr, process_time.speed <= 9999.9999 ? "%9.4fx|" : "%9.3ex|", process_time.speed );
234
ts_time_decompose ( (unsigned long)real_time .eta , ' ' );
239
void timestatus_finish(void)
241
fprintf(stderr, "\n");
246
void timestatus_klemm(lame_global_flags *gfp)
248
lame_internal_flags *gfc=gfp->internal_flags;
249
if (! gfp -> silent) {
250
if ( gfp -> frameNum == 0 ||
251
gfp -> frameNum == 10 ||
252
( GetRealTime () - gfc -> last_time >= gfp -> update_interval ||
253
GetRealTime () < gfp -> update_interval )) {
254
timestatus ( gfp -> out_samplerate,
260
if ( gfp -> brhist_disp )
263
gfc -> last_time = GetRealTime (); /* from now! disp_time seconds */
271
#if defined LIBSNDFILE || defined LAMESNDFILE
273
/* these functions are used in get_audio.c */
275
void decoder_progress ( lame_global_flags* gfp, mp3data_struct *mp3data )
277
lame_internal_flags *gfc=gfp->internal_flags;
279
static int last_total = -1;
280
static int last_kbps = -1;
281
static int last_frame = -1;
283
if ( (gfp -> frameNum & 255 ) == 1 ) {
284
fprintf ( stderr, "\rFrame#%6lu/%-6lu %3u kbps ", gfp -> frameNum, gfp -> totalframes, gfp -> brate );
287
else if ( last_kbps != gfp -> brate ) {
288
fprintf ( stderr, "\rFrame#%6lu/%-6lu %3u", gfp -> frameNum, gfp -> totalframes, gfp -> brate );
291
else if ( last_total != gfp -> totalframes ) {
292
fprintf ( stderr, "\rFrame#%6lu/%-6lu", gfp -> frameNum, gfp -> totalframes );
296
if ( last_frame > 0 && last_frame/10 == gfp -> frameNum/10 )
297
fprintf ( stderr, "\b%lu", gfp -> frameNum % 10 );
298
else if ( last_frame > 0 && last_frame/100 == gfp -> frameNum/100 )
299
fprintf ( stderr, "\b\b%02lu", gfp -> frameNum % 100 );
301
fprintf ( stderr, "\rFrame#%6lu", gfp -> frameNum ),
302
last_frame = gfp -> frameNum;
305
last_total = gfp -> totalframes;
306
last_kbps = gfp -> brate;
309
fprintf(stderr, "\rFrame#%6i/%-6i %3i kbps ",mp3data->framenum,
310
mp3data->totalframes, mp3data->bitrate );
311
if (mp3data->mode==MPG_MD_JOINT_STEREO)
312
fprintf ( stderr, ".... %s ...." ,
313
MPG_MD_MS_LR==mp3data->mode_ext ? "M " : " S" );
317
void decoder_progress_finish ( lame_global_flags* gfp )
319
fprintf ( stderr, "\n" );