brhist.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * Bitrate histogram source file
  3. *
  4. * Copyright (c) 2000 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$ */
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. /* basic #define's */
  26. #ifndef BRHIST_WIDTH
  27. # define BRHIST_WIDTH 14
  28. #endif
  29. #ifndef BRHIST_RES
  30. # define BRHIST_RES 14
  31. #endif
  32. /* #includes */
  33. #ifdef STDC_HEADERS
  34. # include <stdio.h>
  35. # include <stdlib.h>
  36. # include <string.h>
  37. #endif
  38. #include "brhist.h"
  39. #include "console.h"
  40. #ifdef WITH_DMALLOC
  41. #include <dmalloc.h>
  42. #endif
  43. /* Structure holding all data related to the Console I/O
  44. * may be this should be a more global frontend structure. So it
  45. * makes sense to print all files instead with
  46. * printf ( "blah\n") with printf ( "blah%s\n", Console_IO.str_clreoln );
  47. */
  48. extern Console_IO_t Console_IO;
  49. static struct brhist_struct {
  50. int vbr_bitrate_min_index;
  51. int vbr_bitrate_max_index;
  52. int kbps[BRHIST_WIDTH];
  53. int hist_printed_lines;
  54. char bar_asterisk[512 + 1]; /* buffer filled up with a lot of '*' to print a bar */
  55. char bar_percent[512 + 1]; /* buffer filled up with a lot of '%' to print a bar */
  56. char bar_coded[512 + 1]; /* buffer filled up with a lot of ' ' to print a bar */
  57. char bar_space[512 + 1]; /* buffer filled up with a lot of ' ' to print a bar */
  58. } brhist;
  59. static int
  60. calculate_index(const int *const array, const int len, const int value)
  61. {
  62. int i;
  63. for (i = 0; i < len; i++)
  64. if (array[i] == value)
  65. return i;
  66. return -1;
  67. }
  68. int
  69. brhist_init(const lame_global_flags * gf, const int bitrate_kbps_min, const int bitrate_kbps_max)
  70. {
  71. brhist.hist_printed_lines = 0;
  72. /* initialize histogramming data structure */
  73. lame_bitrate_kbps(gf, brhist.kbps);
  74. brhist.vbr_bitrate_min_index = calculate_index(brhist.kbps, BRHIST_WIDTH, bitrate_kbps_min);
  75. brhist.vbr_bitrate_max_index = calculate_index(brhist.kbps, BRHIST_WIDTH, bitrate_kbps_max);
  76. if (brhist.vbr_bitrate_min_index >= BRHIST_WIDTH ||
  77. brhist.vbr_bitrate_max_index >= BRHIST_WIDTH) {
  78. error_printf("lame internal error: VBR min %d kbps or VBR max %d kbps not allowed.\n",
  79. bitrate_kbps_min, bitrate_kbps_max);
  80. return -1;
  81. }
  82. memset(brhist.bar_asterisk, '*', sizeof(brhist.bar_asterisk) - 1);
  83. memset(brhist.bar_percent, '%', sizeof(brhist.bar_percent) - 1);
  84. memset(brhist.bar_space, '-', sizeof(brhist.bar_space) - 1);
  85. memset(brhist.bar_coded, '-', sizeof(brhist.bar_space) - 1);
  86. return 0;
  87. }
  88. static int
  89. digits(unsigned number)
  90. {
  91. int ret = 1;
  92. if (number >= 100000000) {
  93. ret += 8;
  94. number /= 100000000;
  95. }
  96. if (number >= 10000) {
  97. ret += 4;
  98. number /= 10000;
  99. }
  100. if (number >= 100) {
  101. ret += 2;
  102. number /= 100;
  103. }
  104. if (number >= 10) {
  105. ret += 1;
  106. }
  107. return ret;
  108. }
  109. static void
  110. brhist_disp_line(int i, int br_hist_TOT, int br_hist_LR, int full, int frames)
  111. {
  112. char brppt[14]; /* [%] and max. 10 characters for kbps */
  113. int barlen_TOT;
  114. int barlen_LR;
  115. int res = digits(frames) + 3 + 4 + 1;
  116. if (full != 0) {
  117. /* some problems when br_hist_TOT \approx br_hist_LR: You can't see that there are still MS frames */
  118. barlen_TOT = (br_hist_TOT * (Console_IO.disp_width - res) + full - 1) / full; /* round up */
  119. barlen_LR = (br_hist_LR * (Console_IO.disp_width - res) + full - 1) / full; /* round up */
  120. }
  121. else {
  122. barlen_TOT = barlen_LR = 0;
  123. }
  124. sprintf(brppt, " [%*i]", digits(frames), br_hist_TOT);
  125. if (Console_IO.str_clreoln[0]) /* ClearEndOfLine available */
  126. console_printf("\n%3d%s %.*s%.*s%s",
  127. brhist.kbps[i], brppt,
  128. barlen_LR, brhist.bar_percent,
  129. barlen_TOT - barlen_LR, brhist.bar_asterisk, Console_IO.str_clreoln);
  130. else
  131. console_printf("\n%3d%s %.*s%.*s%*s",
  132. brhist.kbps[i], brppt,
  133. barlen_LR, brhist.bar_percent,
  134. barlen_TOT - barlen_LR, brhist.bar_asterisk,
  135. Console_IO.disp_width - res - barlen_TOT, "");
  136. brhist.hist_printed_lines++;
  137. }
  138. static void
  139. progress_line(const lame_global_flags * gf, int full, int frames)
  140. {
  141. char rst[20] = "\0";
  142. int barlen_TOT = 0, barlen_COD = 0, barlen_RST = 0;
  143. int res = 1;
  144. float time_in_sec = 0;
  145. unsigned int hour, min, sec;
  146. int fsize = lame_get_framesize(gf);
  147. int srate = lame_get_out_samplerate(gf);
  148. if (full < frames) {
  149. full = frames;
  150. }
  151. if (srate > 0) {
  152. time_in_sec = (float)(full - frames);
  153. time_in_sec *= fsize;
  154. time_in_sec /= srate;
  155. }
  156. hour = (unsigned int)(time_in_sec / 3600);
  157. time_in_sec -= hour * 3600;
  158. min = (unsigned int)(time_in_sec / 60);
  159. time_in_sec -= min * 60;
  160. sec = (unsigned int)time_in_sec;
  161. if (full != 0) {
  162. if (hour > 0) {
  163. sprintf(rst, "%*u:%02u:%02u", digits(hour), hour, min, sec);
  164. res += digits(hour) + 1 + 5;
  165. }
  166. else {
  167. sprintf(rst, "%02u:%02u", min, sec);
  168. res += 5;
  169. }
  170. /* some problems when br_hist_TOT \approx br_hist_LR: You can't see that there are still MS frames */
  171. barlen_TOT = (full * (Console_IO.disp_width - res) + full - 1) / full; /* round up */
  172. barlen_COD = (frames * (Console_IO.disp_width - res) + full - 1) / full; /* round up */
  173. barlen_RST = barlen_TOT - barlen_COD;
  174. if (barlen_RST == 0) {
  175. sprintf(rst, "%.*s", res - 1, brhist.bar_coded);
  176. }
  177. }
  178. else {
  179. barlen_TOT = barlen_COD = barlen_RST = 0;
  180. }
  181. if (Console_IO.str_clreoln[0]) { /* ClearEndOfLine available */
  182. console_printf("\n%.*s%s%.*s%s",
  183. barlen_COD, brhist.bar_coded,
  184. rst, barlen_RST, brhist.bar_space, Console_IO.str_clreoln);
  185. }
  186. else {
  187. console_printf("\n%.*s%s%.*s%*s",
  188. barlen_COD, brhist.bar_coded,
  189. rst, barlen_RST, brhist.bar_space, Console_IO.disp_width - res - barlen_TOT,
  190. "");
  191. }
  192. brhist.hist_printed_lines++;
  193. }
  194. static int
  195. stats_value(double x)
  196. {
  197. if (x > 0.0) {
  198. console_printf(" %5.1f", x);
  199. return 6;
  200. }
  201. return 0;
  202. }
  203. static int
  204. stats_head(double x, const char *txt)
  205. {
  206. if (x > 0.0) {
  207. console_printf(txt);
  208. return 6;
  209. }
  210. return 0;
  211. }
  212. static void
  213. stats_line(double *stat)
  214. {
  215. int n = 1;
  216. console_printf("\n kbps ");
  217. n += 12;
  218. n += stats_head(stat[1], " mono");
  219. n += stats_head(stat[2], " IS ");
  220. n += stats_head(stat[3], " LR ");
  221. n += stats_head(stat[4], " MS ");
  222. console_printf(" %% ");
  223. n += 6;
  224. n += stats_head(stat[5], " long ");
  225. n += stats_head(stat[6], "switch");
  226. n += stats_head(stat[7], " short");
  227. n += stats_head(stat[8], " mixed");
  228. n += console_printf(" %%");
  229. if (Console_IO.str_clreoln[0]) { /* ClearEndOfLine available */
  230. console_printf("%s", Console_IO.str_clreoln);
  231. }
  232. else {
  233. console_printf("%*s", Console_IO.disp_width - n, "");
  234. }
  235. brhist.hist_printed_lines++;
  236. n = 1;
  237. console_printf("\n %5.1f ", stat[0]);
  238. n += 12;
  239. n += stats_value(stat[1]);
  240. n += stats_value(stat[2]);
  241. n += stats_value(stat[3]);
  242. n += stats_value(stat[4]);
  243. console_printf(" ");
  244. n += 6;
  245. n += stats_value(stat[5]);
  246. n += stats_value(stat[6]);
  247. n += stats_value(stat[7]);
  248. n += stats_value(stat[8]);
  249. if (Console_IO.str_clreoln[0]) { /* ClearEndOfLine available */
  250. console_printf("%s", Console_IO.str_clreoln);
  251. }
  252. else {
  253. console_printf("%*s", Console_IO.disp_width - n, "");
  254. }
  255. brhist.hist_printed_lines++;
  256. }
  257. /* Yes, not very good */
  258. #define LR 0
  259. #define MS 2
  260. void
  261. brhist_disp(const lame_global_flags * gf)
  262. {
  263. int i, lines_used = 0;
  264. int br_hist[BRHIST_WIDTH]; /* how often a frame size was used */
  265. int br_sm_hist[BRHIST_WIDTH][4]; /* how often a special frame size/stereo mode commbination was used */
  266. int st_mode[4];
  267. int bl_type[6];
  268. int frames; /* total number of encoded frames */
  269. int most_often; /* usage count of the most often used frame size, but not smaller than Console_IO.disp_width-BRHIST_RES (makes this sense?) and 1 */
  270. double sum = 0.;
  271. double stat[9] = { 0 };
  272. int st_frames = 0;
  273. brhist.hist_printed_lines = 0; /* printed number of lines for the brhist functionality, used to skip back the right number of lines */
  274. lame_bitrate_stereo_mode_hist(gf, br_sm_hist);
  275. lame_bitrate_hist(gf, br_hist);
  276. lame_stereo_mode_hist(gf, st_mode);
  277. lame_block_type_hist(gf, bl_type);
  278. frames = most_often = 0;
  279. for (i = 0; i < BRHIST_WIDTH; i++) {
  280. frames += br_hist[i];
  281. sum += br_hist[i] * brhist.kbps[i];
  282. if (most_often < br_hist[i])
  283. most_often = br_hist[i];
  284. if (br_hist[i])
  285. ++lines_used;
  286. }
  287. for (i = 0; i < BRHIST_WIDTH; i++) {
  288. int show = br_hist[i];
  289. show = show && (lines_used > 1);
  290. if (show || (i >= brhist.vbr_bitrate_min_index && i <= brhist.vbr_bitrate_max_index))
  291. brhist_disp_line(i, br_hist[i], br_sm_hist[i][LR], most_often, frames);
  292. }
  293. for (i = 0; i < 4; i++) {
  294. st_frames += st_mode[i];
  295. }
  296. if (frames > 0) {
  297. stat[0] = sum / frames;
  298. stat[1] = 100. * (frames - st_frames) / frames;
  299. }
  300. if (st_frames > 0) {
  301. stat[2] = 0.0;
  302. stat[3] = 100. * st_mode[LR] / st_frames;
  303. stat[4] = 100. * st_mode[MS] / st_frames;
  304. }
  305. if (bl_type[5] > 0) {
  306. stat[5] = 100. * bl_type[0] / bl_type[5];
  307. stat[6] = 100. * (bl_type[1] + bl_type[3]) / bl_type[5];
  308. stat[7] = 100. * bl_type[2] / bl_type[5];
  309. stat[8] = 100. * bl_type[4] / bl_type[5];
  310. }
  311. progress_line(gf, lame_get_totalframes(gf), frames);
  312. stats_line(stat);
  313. }
  314. void
  315. brhist_jump_back(void)
  316. {
  317. console_up(brhist.hist_printed_lines);
  318. brhist.hist_printed_lines = 0;
  319. }
  320. /*
  321. * 1)
  322. *
  323. * Taken from Termcap_Manual.html:
  324. *
  325. * With the Unix version of termcap, you must allocate space for the description yourself and pass
  326. * the address of the space as the argument buffer. There is no way you can tell how much space is
  327. * needed, so the convention is to allocate a buffer 2048 characters long and assume that is
  328. * enough. (Formerly the convention was to allocate 1024 characters and assume that was enough.
  329. * But one day, for one kind of terminal, that was not enough.)
  330. */