~ubuntu-branches/ubuntu/intrepid/transcode/intrepid-proposed

« back to all changes in this revision

Viewing changes to libmp3lame/timestatus.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T Chen
  • Date: 2005-10-10 19:12:47 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20051010191247-uq4j6t947df71v7m
Tags: 2:1.0.1-0.0ubuntu1
* New upstream release.
* debian/:
  + Remove 03_rescale.c and 04_average.c dpatches, applied upstream.
  + Force amd64 to link against the correct _pic libs, fixing FTBFS.
  + Disable mjpegtools support on ppc until post-Breezy (FTBFS).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *      time status related function source file
3
 
 *
4
 
 *      Copyright (c) 1999 Mark Taylor
5
 
 *
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.
10
 
 *
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.
15
 
 *
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.
20
 
 */
21
 
 
22
 
#include "lame.h"
23
 
#include "timestatus.h"
24
 
#include "lametime.h"
25
 
#include "util.h"
26
 
#ifdef BRHIST
27
 
#include "brhist.h"
28
 
#endif
29
 
#include <assert.h>
30
 
#include <time.h>
31
 
 
32
 
#ifdef __unix__
33
 
   #include <sys/time.h>
34
 
   #include <unistd.h>
35
 
#endif
36
 
 
37
 
 
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
44
 
#elif defined(HZ)
45
 
/* Older BSD systems */
46
 
# define TS_CLOCKS_PER_SEC HZ
47
 
#else
48
 
# error no suitable value for TS_CLOCKS_PER_SEC
49
 
#endif
50
 
 
51
 
 
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)
55
 
 
56
 
/*********************************************************/
57
 
/* ts_real_time: real time elapsed in seconds            */
58
 
/*********************************************************/
59
 
 
60
 
FLOAT ts_real_time (long frame) 
61
 
{
62
 
#ifdef __unix__
63
 
 
64
 
   static long double  initial_time;
65
 
   long double         current_time;
66
 
   struct timeval      t;
67
 
 
68
 
   gettimeofday (&t, NULL);
69
 
 
70
 
   current_time = t.tv_sec + 1.e-6l * t.tv_usec;
71
 
   if (frame == 0)
72
 
       initial_time = current_time;
73
 
 
74
 
   return (FLOAT)(current_time - initial_time);
75
 
 
76
 
#else
77
 
 
78
 
   static time_t  initial_time;
79
 
   time_t         current_time;
80
 
 
81
 
   time (&current_time);
82
 
 
83
 
   if (frame == 0)
84
 
       initial_time = current_time;
85
 
 
86
 
   return (FLOAT) difftime (current_time, initial_time);
87
 
 
88
 
#endif
89
 
}
90
 
 
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;
97
 
  clock_t current_time;
98
 
 
99
 
#if ( defined(_MSC_VER) || defined(__BORLANDC__) ) 
100
 
 
101
 
  { static HANDLE hProcess;
102
 
    FILETIME Ignored1, Ignored2, KernelTime, UserTime;
103
 
 
104
 
    if ( frame==0 ) {
105
 
      hProcess = GetCurrentProcess();
106
 
    }
107
 
        
108
 
    /* GetProcessTimes() always fails under Win9x */
109
 
    if (GetProcessTimes(hProcess, &Ignored1, &Ignored2, &KernelTime, &UserTime)) {
110
 
      LARGE_INTEGER Kernel;
111
 
      LARGE_INTEGER User;
112
 
 
113
 
      Kernel.LowPart  = KernelTime.dwLowDateTime;
114
 
      Kernel.HighPart = KernelTime.dwHighDateTime;
115
 
      User.LowPart    = UserTime.dwLowDateTime;
116
 
      User.HighPart   = UserTime.dwHighDateTime;
117
 
 
118
 
      current_time = (clock_t)((FLOAT)(Kernel.QuadPart + User.QuadPart) * TS_CLOCKS_PER_SEC / 10000000);
119
 
    } else {
120
 
      current_time = clock();
121
 
        }
122
 
  }
123
 
#else
124
 
  current_time = clock();
125
 
#endif
126
 
 
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);
135
 
    }
136
 
  }
137
 
  previous_time = current_time;
138
 
  
139
 
  current_time /= TS_CLOCKS_PER_TIC; /* convert process time to tics */
140
 
 
141
 
  if (frame == 0) {
142
 
    initial_tictime = current_time;
143
 
  }
144
 
 
145
 
  return (FLOAT)((FLOAT)(current_time - initial_tictime) * TS_SECS_PER_TIC);
146
 
}
147
 
 
148
 
#undef TS_SECS_PER_TIC
149
 
#undef TS_CLOCKS_PER_TIC
150
 
#undef TS_CLOCKS_PER_SEC
151
 
 
152
 
typedef struct ts_times {
153
 
  FLOAT so_far;
154
 
  FLOAT estimated;
155
 
  FLOAT speed;
156
 
  FLOAT eta;
157
 
} ts_times;
158
 
 
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)
163
 
{
164
 
  if (frame > 0) {
165
 
    tstime->estimated = tstime->so_far * frames / frame;
166
 
    if (samp_rate * tstime->estimated > 0) {
167
 
      tstime->speed = frames * framesize / (samp_rate * tstime->estimated);
168
 
    } else {
169
 
      tstime->speed = 0;
170
 
    }
171
 
    tstime->eta = tstime->estimated - tstime->so_far;
172
 
  } else {
173
 
    tstime->estimated = 0;
174
 
    tstime->speed = 0;
175
 
    tstime->eta = 0;
176
 
  }
177
 
}
178
 
 
179
 
/*********************************************************/
180
 
/* timestatus: display encoding process time information */
181
 
/*********************************************************/
182
 
 
183
 
static void  ts_time_decompose ( const unsigned long time_in_sec, const char padded_char )
184
 
{
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;
188
 
 
189
 
    if ( hour == 0 )
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 );
193
 
    else
194
 
        fprintf ( stderr,         "%6lu h%c", hour,           padded_char );
195
 
}
196
 
 
197
 
void timestatus ( unsigned long samp_rate, 
198
 
                  unsigned long frameNum,
199
 
                  unsigned long totalframes,
200
 
                  int           framesize )
201
 
{
202
 
    ts_times  real_time;
203
 
    ts_times  process_time;
204
 
    int       percent;
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 ??? */
208
 
 
209
 
    real_time   .so_far = ts_real_time    (frameNum);
210
 
    process_time.so_far = ts_process_time (frameNum);
211
 
 
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 );
215
 
        return;
216
 
    }  
217
 
 
218
 
    ts_calc_times ( &real_time   , samp_rate, frameNum, totalframes, framesize );
219
 
    ts_calc_times ( &process_time, samp_rate, frameNum, totalframes, framesize );
220
 
 
221
 
    if ( frameNum < totalframes - dropped_frames ) {
222
 
        percent = (int) (100.0 * frameNum / (totalframes - dropped_frames) + 0.5 );
223
 
    } else {
224
 
        percent = 100;
225
 
    }
226
 
 
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      , ' ' );
235
 
    fflush ( stderr );
236
 
}
237
 
 
238
 
 
239
 
void timestatus_finish(void)
240
 
{
241
 
  fprintf(stderr, "\n");
242
 
  fflush(stderr);
243
 
}
244
 
 
245
 
 
246
 
void timestatus_klemm(lame_global_flags *gfp)
247
 
{
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, 
255
 
                         gfp -> frameNum, 
256
 
                         gfp -> totalframes, 
257
 
                         gfp -> framesize );
258
 
 
259
 
#ifdef BRHIST
260
 
            if ( gfp -> brhist_disp )
261
 
                brhist_disp ( gfp );
262
 
#endif
263
 
            gfc -> last_time = GetRealTime ();  /* from now! disp_time seconds */
264
 
        }
265
 
    }
266
 
}
267
 
 
268
 
 
269
 
 
270
 
 
271
 
#if defined LIBSNDFILE || defined LAMESNDFILE
272
 
 
273
 
/* these functions are used in get_audio.c */
274
 
 
275
 
void decoder_progress ( lame_global_flags* gfp, mp3data_struct *mp3data )
276
 
{
277
 
    lame_internal_flags *gfc=gfp->internal_flags;
278
 
#if 0
279
 
    static int  last_total = -1;
280
 
    static int  last_kbps  = -1;
281
 
    static int  last_frame = -1;
282
 
 
283
 
    if ( (gfp -> frameNum & 255 ) == 1 ) {
284
 
        fprintf ( stderr, "\rFrame#%6lu/%-6lu %3u kbps        ", gfp -> frameNum, gfp -> totalframes, gfp -> brate );
285
 
        last_frame = -1;
286
 
    } 
287
 
    else if ( last_kbps != gfp -> brate ) {
288
 
        fprintf ( stderr, "\rFrame#%6lu/%-6lu %3u", gfp -> frameNum, gfp -> totalframes, gfp -> brate );
289
 
        last_frame = -1;
290
 
    } 
291
 
    else if ( last_total != gfp -> totalframes ) {
292
 
        fprintf ( stderr, "\rFrame#%6lu/%-6lu", gfp -> frameNum, gfp -> totalframes );
293
 
        last_frame = -1;
294
 
    } 
295
 
    else {
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 );
300
 
        else
301
 
            fprintf ( stderr, "\rFrame#%6lu", gfp -> frameNum ),
302
 
        last_frame = gfp -> frameNum;
303
 
    }
304
 
              
305
 
    last_total = gfp -> totalframes;
306
 
    last_kbps  = gfp -> brate;
307
 
#endif
308
 
 
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" );
314
 
 
315
 
}
316
 
 
317
 
void decoder_progress_finish ( lame_global_flags* gfp )
318
 
{
319
 
    fprintf ( stderr, "\n" );
320
 
}
321
 
 
322
 
#endif
323
 
 
324
 
 
325
 
 
326
 
 
327
 
 
328
 
 
329