timestatus.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. /* $Id: timestatus.c,v 1.32 2001/03/11 11:24:25 aleidinger Exp $ */
  22. #ifdef HAVE_CONFIG_H
  23. # include <config.h>
  24. #endif
  25. /* Hope it works now, otherwise complain or flame ;-)
  26. */
  27. #if 1
  28. # define SPEED_CHAR "x" /* character x */
  29. # define SPEED_MULT 1.
  30. #else
  31. # define SPEED_CHAR "%%"
  32. # define SPEED_MULT 100.
  33. #endif
  34. #include <assert.h>
  35. #include <time.h>
  36. #include "lame.h"
  37. #include "main.h"
  38. #include "lametime.h"
  39. #include "timestatus.h"
  40. #if defined(BRHIST)
  41. # include "brhist.h"
  42. #endif
  43. #ifdef WITH_DMALLOC
  44. #include <dmalloc.h>
  45. #endif
  46. typedef struct {
  47. double start_time; // start time of converting [s]
  48. double elapsed_time; // current time - start time [s]
  49. double estimated_time; // estimated total duration time [s]
  50. double speed_index; // speed relative to realtime coding [100%]
  51. } timestatus_t;
  52. /*
  53. * Calculates from the input (see below) the following values:
  54. * - total estimated time
  55. * - a speed index
  56. */
  57. static void ts_calc_times (
  58. timestatus_t* const tstime, // tstime->elapsed_time: elapsed time
  59. const int sample_freq, // sample frequency [Hz/kHz]
  60. const int frameNum, // Number of the current Frame
  61. const int totalframes, // total umber of Frames
  62. const int framesize ) // Size of a frame [bps/kbps]
  63. {
  64. assert ( sample_freq >= 8000 && sample_freq <= 48000 );
  65. if ( frameNum > 0 && tstime->elapsed_time > 0 ) {
  66. tstime->estimated_time = tstime->elapsed_time * totalframes / frameNum;
  67. tstime->speed_index = framesize * frameNum / (sample_freq * tstime->elapsed_time);
  68. } else {
  69. tstime->estimated_time = 0.;
  70. tstime->speed_index = 0.;
  71. }
  72. }
  73. /* Decomposes a given number of seconds into a easy to read hh:mm:ss format
  74. * padded with an additional character
  75. */
  76. static void ts_time_decompose ( const unsigned long time_in_sec, const char padded_char )
  77. {
  78. const unsigned long hour = time_in_sec / 3600;
  79. const unsigned int min = time_in_sec / 60 % 60;
  80. const unsigned int sec = time_in_sec % 60;
  81. if ( hour == 0 )
  82. fprintf ( stderr, " %2u:%02u%c", min, sec, padded_char );
  83. else if ( hour < 100 )
  84. fprintf ( stderr, "%2lu:%02u:%02u%c", hour, min, sec, padded_char );
  85. else
  86. fprintf ( stderr, "%6lu h%c", hour, padded_char );
  87. }
  88. void timestatus ( const int samp_rate,
  89. const int frameNum,
  90. const int totalframes,
  91. const int framesize )
  92. {
  93. static timestatus_t real_time;
  94. static timestatus_t proc_time;
  95. int percent;
  96. static int init = 0; /* What happens here? A work around instead of a bug fix ??? */
  97. if ( frameNum == 0 ) {
  98. real_time.start_time = GetRealTime ();
  99. proc_time.start_time = GetCPUTime ();
  100. }
  101. real_time.elapsed_time = GetRealTime () - real_time.start_time;
  102. proc_time.elapsed_time = GetCPUTime () - proc_time.start_time;
  103. if ( frameNum == 0 && init == 0 ) {
  104. fprintf ( stderr,
  105. "\r"
  106. " Frame | CPU time/estim | REAL time/estim | play/CPU | ETA \n"
  107. " 0/ ( 0%%)| 0:00/ : | 0:00/ : | " SPEED_CHAR "| : \r"
  108. /* , Console_IO.str_clreoln, Console_IO.str_clreoln */ );
  109. init = 1;
  110. return;
  111. }
  112. /* reset init counter for next time we are called with frameNum==0 */
  113. if (frameNum > 0)
  114. init = 0;
  115. ts_calc_times ( &real_time, samp_rate, frameNum, totalframes, framesize );
  116. ts_calc_times ( &proc_time, samp_rate, frameNum, totalframes, framesize );
  117. if ( frameNum < totalframes ) {
  118. percent = (int) (100. * frameNum / totalframes + 0.5 );
  119. } else {
  120. percent = 100;
  121. }
  122. fprintf ( stderr, "\r%6i/%-6i", frameNum, totalframes );
  123. fprintf ( stderr, percent < 100 ? " (%2d%%)|" : "(%3.3d%%)|", percent );
  124. ts_time_decompose ( (unsigned long)proc_time.elapsed_time , '/' );
  125. ts_time_decompose ( (unsigned long)proc_time.estimated_time, '|' );
  126. ts_time_decompose ( (unsigned long)real_time.elapsed_time , '/' );
  127. ts_time_decompose ( (unsigned long)real_time.estimated_time, '|' );
  128. fprintf ( stderr, proc_time.speed_index <= 1. ?
  129. "%9.4f" SPEED_CHAR "|" : "%#9.5g" SPEED_CHAR "|",
  130. SPEED_MULT * proc_time.speed_index );
  131. ts_time_decompose ( (unsigned long)(real_time.estimated_time - real_time.elapsed_time), ' ' );
  132. fflush ( stderr );
  133. }
  134. void timestatus_finish ( void )
  135. {
  136. fprintf ( stderr, "\n" );
  137. fflush ( stderr );
  138. }
  139. void timestatus_klemm ( const lame_global_flags* const gfp )
  140. {
  141. static double last_time = 0.;
  142. if ( !silent )
  143. if ( gfp->frameNum == 0 ||
  144. gfp->frameNum == 9 ||
  145. GetRealTime () - last_time >= update_interval ||
  146. GetRealTime () - last_time < 0 ) {
  147. #ifdef BRHIST
  148. brhist_jump_back();
  149. #endif
  150. timestatus ( lame_get_out_samplerate( gfp ),
  151. gfp->frameNum,
  152. gfp->totalframes,
  153. gfp->framesize );
  154. #ifdef BRHIST
  155. if ( brhist ) {
  156. brhist_disp ( gfp );
  157. }
  158. #endif
  159. last_time = GetRealTime (); /* from now! disp_time seconds */
  160. }
  161. }
  162. /* these functions are used in get_audio.c */
  163. void decoder_progress ( const lame_global_flags* const gfp, const mp3data_struct* const mp3data )
  164. {
  165. static int last;
  166. fprintf ( stderr, "\rFrame#%6i/%-6i %3i kbps",
  167. mp3data->framenum, mp3data->totalframes, mp3data->bitrate );
  168. // Programmed with a single frame hold delay
  169. // Attention: static data
  170. // MP2 Playback is still buggy.
  171. // "'00' subbands 4-31 in intensity_stereo, bound==4"
  172. // is this really intensity_stereo or is it MS stereo?
  173. if ( mp3data->mode == JOINT_STEREO ) {
  174. int curr = mp3data->mode_ext;
  175. fprintf ( stderr, " %s %c" ,
  176. curr&2 ? last&2 ? " MS " : "LMSR" : last&2 ? "LMSR" : "L R",
  177. curr&1 ? last&1 ? 'I' : 'i' : last&1 ? 'i' : ' ' );
  178. last = curr;
  179. } else {
  180. fprintf ( stderr, " " );
  181. last = 0;
  182. }
  183. // fprintf ( stderr, "%s", Console_IO.str_clreoln );
  184. fprintf ( stderr, " \b\b\b\b\b\b\b\b" );
  185. fflush ( stderr );
  186. }
  187. void decoder_progress_finish ( const lame_global_flags* const gfp )
  188. {
  189. fprintf ( stderr, "\n" );
  190. }