timestatus.c

Go to the documentation of this file.
00001 /*
00002  *      time status related function source file
00003  *
00004  *      Copyright (c) 1999 Mark Taylor
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the
00018  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019  * Boston, MA 02111-1307, USA.
00020  */
00021 
00022 /* $Id: timestatus.c,v 1.44 2007/07/24 17:46:09 bouvigne Exp $ */
00023 
00024 #ifdef HAVE_CONFIG_H
00025 # include <config.h>
00026 #endif
00027 
00028 
00029 /* Hope it works now, otherwise complain or flame ;-)
00030  */
00031 
00032 
00033 #if 1
00034 # define SPEED_CHAR     "x" /* character x */
00035 # define SPEED_MULT     1.
00036 #else
00037 # define SPEED_CHAR     "%%"
00038 # define SPEED_MULT     100.
00039 #endif
00040 
00041 #include <assert.h>
00042 #include <time.h>
00043 #include <string.h>
00044 
00045 #include "lame.h"
00046 #include "main.h"
00047 #include "lametime.h"
00048 #include "timestatus.h"
00049 
00050 #if defined(BRHIST)
00051 # include "brhist.h"
00052 #endif
00053 #include "console.h"
00054 
00055 #ifdef WITH_DMALLOC
00056 #include <dmalloc.h>
00057 #endif
00058 
00059 typedef struct {
00060     double  last_time;       /* result of last call to clock */
00061     double  elapsed_time;    /* total time */
00062     double  estimated_time;  /* estimated total duration time [s] */
00063     double  speed_index;     /* speed relative to realtime coding [100%] */
00064 } timestatus_t;
00065 
00066 /*
00067  *  Calculates from the input (see below) the following values:
00068  *    - total estimated time
00069  *    - a speed index
00070  */
00071 
00072 static void
00073 ts_calc_times(timestatus_t * const tstime, /* tstime->elapsed_time: elapsed time */
00074               const int sample_freq, /* sample frequency [Hz/kHz]  */
00075               const int frameNum, /* Number of the current Frame */
00076               const int totalframes, /* total umber of Frames */
00077               const int framesize)
00078 {                       /* Size of a frame [bps/kbps] */
00079     assert(sample_freq >= 8000 && sample_freq <= 48000);
00080 
00081     if (frameNum > 0 && tstime->elapsed_time > 0) {
00082         tstime->estimated_time = tstime->elapsed_time * totalframes / frameNum;
00083         tstime->speed_index = framesize * frameNum / (sample_freq * tstime->elapsed_time);
00084     }
00085     else {
00086         tstime->estimated_time = 0.;
00087         tstime->speed_index = 0.;
00088     }
00089 }
00090 
00091 /* Decomposes a given number of seconds into a easy to read hh:mm:ss format
00092  * padded with an additional character
00093  */
00094 
00095 static void
00096 ts_time_decompose(const unsigned long time_in_sec, const char padded_char)
00097 {
00098     const unsigned long hour = time_in_sec / 3600;
00099     const unsigned int min = time_in_sec / 60 % 60;
00100     const unsigned int sec = time_in_sec % 60;
00101 
00102     if (hour == 0)
00103         console_printf("   %2u:%02u%c", min, sec, padded_char);
00104     else if (hour < 100)
00105         console_printf("%2lu:%02u:%02u%c", hour, min, sec, padded_char);
00106     else
00107         console_printf("%6lu h%c", hour, padded_char);
00108 }
00109 
00110 static void
00111 timestatus(const lame_global_flags * const gfp)
00112 {
00113     static timestatus_t real_time;
00114     static timestatus_t proc_time;
00115     int     percent;
00116     static int init = 0;     /* What happens here? A work around instead of a bug fix ??? */
00117     double  tmx, delta;
00118     int samp_rate     = lame_get_out_samplerate(gfp)
00119       , frameNum      = lame_get_frameNum(gfp)
00120       , totalframes   = lame_get_totalframes(gfp)
00121       , framesize     = lame_get_framesize(gfp)
00122       ;
00123 
00124     if (totalframes < frameNum) {
00125         totalframes = frameNum;
00126     }
00127     if (frameNum == 0) {
00128         real_time.last_time = GetRealTime();
00129         proc_time.last_time = GetCPUTime();
00130         real_time.elapsed_time = 0;
00131         proc_time.elapsed_time = 0;
00132     }
00133 
00134     /* we need rollover protection for GetCPUTime, and maybe GetRealTime(): */
00135     tmx = GetRealTime();
00136     delta = tmx - real_time.last_time;
00137     if (delta < 0)
00138         delta = 0;      /* ignore, clock has rolled over */
00139     real_time.elapsed_time += delta;
00140     real_time.last_time = tmx;
00141 
00142 
00143     tmx = GetCPUTime();
00144     delta = tmx - proc_time.last_time;
00145     if (delta < 0)
00146         delta = 0;      /* ignore, clock has rolled over */
00147     proc_time.elapsed_time += delta;
00148     proc_time.last_time = tmx;
00149 
00150     if (frameNum == 0 && init == 0) {
00151         console_printf("\r"
00152                        "    Frame          |  CPU time/estim | REAL time/estim | play/CPU |    ETA \n"
00153                        "     0/       ( 0%%)|    0:00/     :  |    0:00/     :  |         "
00154                        SPEED_CHAR "|     :  \r"
00155                        /* , Console_IO.str_clreoln, Console_IO.str_clreoln */ );
00156         init = 1;
00157         return;
00158     }
00159     /* reset init counter for next time we are called with frameNum==0 */
00160     if (frameNum > 0)
00161         init = 0;
00162 
00163     ts_calc_times(&real_time, samp_rate, frameNum, totalframes, framesize);
00164     ts_calc_times(&proc_time, samp_rate, frameNum, totalframes, framesize);
00165 
00166     if (frameNum < totalframes) {
00167         percent = (int) (100. * frameNum / totalframes + 0.5);
00168     }
00169     else {
00170         percent = 100;
00171     }
00172 
00173     console_printf("\r%6i/%-6i", frameNum, totalframes);
00174     console_printf(percent < 100 ? " (%2d%%)|" : "(%3.3d%%)|", percent);
00175     ts_time_decompose((unsigned long) proc_time.elapsed_time, '/');
00176     ts_time_decompose((unsigned long) proc_time.estimated_time, '|');
00177     ts_time_decompose((unsigned long) real_time.elapsed_time, '/');
00178     ts_time_decompose((unsigned long) real_time.estimated_time, '|');
00179     console_printf(proc_time.speed_index <= 1. ?
00180                    "%9.4f" SPEED_CHAR "|" : "%#9.5g" SPEED_CHAR "|",
00181                    SPEED_MULT * proc_time.speed_index);
00182     ts_time_decompose((unsigned long) (real_time.estimated_time - real_time.elapsed_time), ' ');
00183 }
00184 
00185 static void
00186 timestatus_finish(void)
00187 {
00188     console_printf("\n");
00189 }
00190 
00191 
00192 void
00193 encoder_progress_begin( lame_global_flags const* gf
00194                       , char              const* inPath
00195                       , char              const* outPath
00196                       )
00197 {
00198     if (silent < 10) {
00199         lame_print_config(gf); /* print useful information about options being used */
00200 
00201         console_printf("Encoding %s%s to %s\n",
00202                        strcmp(inPath, "-") ? inPath : "<stdin>",
00203                        strlen(inPath) + strlen(outPath) < 66 ? "" : "\n     ",
00204                        strcmp(outPath, "-") ? outPath : "<stdout>");
00205 
00206         console_printf("Encoding as %g kHz ", 1.e-3 * lame_get_out_samplerate(gf));
00207 
00208         {
00209             static const char *mode_names[2][4] = {
00210                 {"stereo", "j-stereo", "dual-ch", "single-ch"},
00211                 {"stereo", "force-ms", "dual-ch", "single-ch"}
00212             };
00213             const char *appendix = "";
00214 
00215             switch (lame_get_VBR(gf)) {
00216             case vbr_mt:
00217             case vbr_rh:
00218             case vbr_mtrh:
00219                 appendix = "ca. ";
00220                 console_printf("VBR(q=%i)", lame_get_VBR_q(gf));
00221                 break;
00222             case vbr_abr:
00223                 console_printf("average %d kbps", lame_get_VBR_mean_bitrate_kbps(gf));
00224                 break;
00225             default:
00226                 console_printf("%3d kbps", lame_get_brate(gf));
00227                 break;
00228             }
00229             console_printf(" %s MPEG-%u%s Layer III (%s%gx) qval=%i\n",
00230                            mode_names[lame_get_force_ms(gf)][lame_get_mode(gf)],
00231                            2 - lame_get_version(gf),
00232                            lame_get_out_samplerate(gf) < 16000 ? ".5" : "",
00233                            appendix,
00234                            0.1 * (int) (10. * lame_get_compression_ratio(gf) + 0.5),
00235                            lame_get_quality(gf));
00236         }
00237 
00238         if (silent <= -10) {
00239             lame_print_internals(gf);
00240         }
00241     }
00242 }
00243 
00244 void
00245 encoder_progress( lame_global_flags const* gf )
00246 {
00247     if (silent <= 0) {
00248         int const frames = lame_get_frameNum(gf);
00249         if (update_interval <= 0) {     /*  most likely --disptime x not used */
00250             if ((frames % 100) != 0) {  /*  true, most of the time */
00251                 return;
00252             }
00253         }
00254         else {
00255             static double last_time = 0.0;
00256             if (frames != 0 && frames != 9) {
00257                 double const act = GetRealTime();
00258                 double const dif = act - last_time;
00259                 if (dif >= 0 && dif < update_interval) {
00260                     return;
00261                 }
00262             }
00263             last_time = GetRealTime(); /* from now! disp_time seconds */
00264         }
00265 #ifdef BRHIST
00266         if (brhist) {
00267             brhist_jump_back();
00268         }
00269 #endif
00270         timestatus(gf);
00271 #ifdef BRHIST
00272         if (brhist) {
00273             brhist_disp(gf);
00274         }
00275 #endif
00276         console_flush();
00277     }
00278 }
00279 
00280 void
00281 encoder_progress_end( lame_global_flags const* gf )
00282 {
00283     if (silent <= 0) {
00284 #ifdef BRHIST
00285         if (brhist) {
00286             brhist_jump_back();
00287         }
00288 #endif
00289         timestatus(gf);
00290 #ifdef BRHIST
00291         if (brhist) {
00292             brhist_disp(gf);
00293         }
00294         brhist_disp_total(gf);
00295 #endif
00296         timestatus_finish();
00297     }
00298 }
00299 
00300 
00301 /* these functions are used in get_audio.c */
00302 
00303 void
00304 decoder_progress(const mp3data_struct * const mp3data)
00305 {
00306     static int last;
00307     console_printf("\rFrame#%6i/%-6i %3i kbps",
00308                    mp3data->framenum, mp3data->totalframes, mp3data->bitrate);
00309 
00310     /* Programmed with a single frame hold delay */
00311     /* Attention: static data */
00312 
00313     /* MP2 Playback is still buggy. */
00314     /* "'00' subbands 4-31 in intensity_stereo, bound==4" */
00315     /* is this really intensity_stereo or is it MS stereo? */
00316 
00317     if (mp3data->mode == JOINT_STEREO) {
00318         int     curr = mp3data->mode_ext;
00319         console_printf("  %s  %c",
00320                        curr & 2 ? last & 2 ? " MS " : "LMSR" : last & 2 ? "LMSR" : "L  R",
00321                        curr & 1 ? last & 1 ? 'I' : 'i' : last & 1 ? 'i' : ' ');
00322         last = curr;
00323     }
00324     else {
00325         console_printf("         ");
00326         last = 0;
00327     }
00328 /*    console_printf ("%s", Console_IO.str_clreoln ); */
00329     console_printf("        \b\b\b\b\b\b\b\b");
00330 }
00331 
00332 void
00333 decoder_progress_finish()
00334 {
00335     console_printf("\n");
00336 }

Generated on Sun Dec 2 11:34:18 2007 for LAME by  doxygen 1.5.2