📄 brhist.c
字号:
/* * Bitrate histogram source file * * Copyright (c) 2000 Mark Taylor * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. *//* $Id: brhist.c,v 1.46 2005/02/25 01:21:54 robert Exp $ */#ifdef HAVE_CONFIG_H#include <config.h>#endif#ifdef BRHIST/* basic #define's */#ifndef BRHIST_WIDTH# define BRHIST_WIDTH 14#endif#ifndef BRHIST_RES# define BRHIST_RES 14#endif/* #includes */#ifdef STDC_HEADERS# include <stdlib.h># include <string.h>#else# ifndef HAVE_STRCHR# define strchr index# define strrchr rindex# endifchar *strchr(), *strrchr();# ifndef HAVE_MEMCPY# define memcpy(d, s, n) bcopy ((s), (d), (n))# define memmove(d, s, n) bcopy ((s), (d), (n))# endif#endif#if defined(HAVE_NCURSES_TERMCAP_H)# include <ncurses/termcap.h>#elif defined(HAVE_TERMCAP_H)# include <termcap.h>#elif defined(HAVE_TERMCAP)# include <curses.h># if !defined(__bsdi__)# include <term.h># endif#endif#include "brhist.h"#ifdef WITH_DMALLOC#include <dmalloc.h>#endif/* Structure holding all data related to the Console I/O * may be this should be a more global frontend structure. So it * makes sense to print all files instead with * printf ( "blah\n") with printf ( "blah%s\n", Console_IO.str_clreoln ); */Console_IO_t Console_IO;static struct { int vbr_bitrate_min_index; int vbr_bitrate_max_index; int kbps[BRHIST_WIDTH]; int hist_printed_lines; char bar_asterisk[512 + 1]; /* buffer filled up with a lot of '*' to print a bar */ char bar_percent[512 + 1]; /* buffer filled up with a lot of '%' to print a bar */ char bar_coded[512 + 1]; /* buffer filled up with a lot of ' ' to print a bar */ char bar_space[512 + 1]; /* buffer filled up with a lot of ' ' to print a bar */} brhist;static intcalculate_index(const int *const array, const int len, const int value){ int i; for (i = 0; i < len; i++) if (array[i] == value) return i; return -1;}intbrhist_init(const lame_global_flags * gf, const int bitrate_kbps_min, const int bitrate_kbps_max){#ifdef HAVE_TERMCAP char term_buff[2048]; /* see 1) */ const char *term_name; char *tp; char tc[10]; int val;#endif /* setup basics of brhist I/O channels */ Console_IO.disp_width = 80; Console_IO.disp_height = 25; brhist.hist_printed_lines = 0; Console_IO.Console_fp = stderr; Console_IO.Error_fp = stderr; Console_IO.Report_fp = stderr; setvbuf(Console_IO.Console_fp, Console_IO.Console_buff, _IOFBF, sizeof(Console_IO.Console_buff));/* setvbuf ( Console_IO.Error_fp , NULL , _IONBF, 0 ); */#if defined(_WIN32) && !defined(__CYGWIN__) Console_IO.Console_Handle = GetStdHandle(STD_ERROR_HANDLE);#endif strcpy(Console_IO.str_up, "\033[A");#ifndef RH_HIST /* some internal checks */ if (bitrate_kbps_min > bitrate_kbps_max) { fprintf(Console_IO.Error_fp, "lame internal error: VBR min %d kbps > VBR max %d kbps.\n", bitrate_kbps_min, bitrate_kbps_max); return -1; } if (bitrate_kbps_min < 8 || bitrate_kbps_max > 320) { fprintf(Console_IO.Error_fp, "lame internal error: VBR min %d kbps or VBR max %d kbps out of range.\n", bitrate_kbps_min, bitrate_kbps_max); return -1; }#endif /* initialize histogramming data structure */ lame_bitrate_kbps(gf, brhist.kbps); brhist.vbr_bitrate_min_index = calculate_index(brhist.kbps, BRHIST_WIDTH, bitrate_kbps_min); brhist.vbr_bitrate_max_index = calculate_index(brhist.kbps, BRHIST_WIDTH, bitrate_kbps_max); if (brhist.vbr_bitrate_min_index >= BRHIST_WIDTH || brhist.vbr_bitrate_max_index >= BRHIST_WIDTH) { fprintf(Console_IO.Error_fp, "lame internal error: VBR min %d kbps or VBR max %d kbps not allowed.\n", bitrate_kbps_min, bitrate_kbps_max); return -1; } memset(brhist.bar_asterisk, '*', sizeof(brhist.bar_asterisk) - 1); memset(brhist.bar_percent, '%', sizeof(brhist.bar_percent) - 1); memset(brhist.bar_space, '-', sizeof(brhist.bar_space) - 1); memset(brhist.bar_coded, '-', sizeof(brhist.bar_space) - 1);#ifdef HAVE_TERMCAP /* try to catch additional information about special console sequences */ if ((term_name = getenv("TERM")) == NULL) { fprintf(Console_IO.Error_fp, "LAME: Can't get \"TERM\" environment string.\n"); return -1; } if (tgetent(term_buff, term_name) != 1) { fprintf(Console_IO.Error_fp, "LAME: Can't find termcap entry for terminal \"%s\"\n", term_name); return -1; } val = tgetnum("co"); if (val >= 40 && val <= 512) Console_IO.disp_width = val; val = tgetnum("li"); if (val >= 16 && val <= 256) Console_IO.disp_height = val; *(tp = tc) = '\0'; tp = tgetstr("up", &tp); if (tp != NULL) strcpy(Console_IO.str_up, tp); *(tp = tc) = '\0'; tp = tgetstr("ce", &tp); if (tp != NULL) strcpy(Console_IO.str_clreoln, tp); *(tp = tc) = '\0'; tp = tgetstr("md", &tp); if (tp != NULL) strcpy(Console_IO.str_emph, tp); *(tp = tc) = '\0'; tp = tgetstr("me", &tp); if (tp != NULL) strcpy(Console_IO.str_norm, tp);#endif /* HAVE_TERMCAP */ return 0;}static intdigits(unsigned number){ int ret = 1; if (number >= 100000000) { ret += 8; number /= 100000000; } if (number >= 10000) { ret += 4; number /= 10000; } if (number >= 100) { ret += 2; number /= 100; } if (number >= 10) { ret += 1; } return ret;}static voidbrhist_disp_line(const lame_global_flags * gf, int i, int br_hist_TOT, int br_hist_LR, int full, int frames){ char brppt[14]; /* [%] and max. 10 characters for kbps */ int barlen_TOT; int barlen_LR; int ppt = 0; int res = digits(frames) + 3 + 4 + 1; if (full != 0) { /* some problems when br_hist_TOT \approx br_hist_LR: You can't see that there are still MS frames */ barlen_TOT = (br_hist_TOT * (Console_IO.disp_width - res) + full - 1) / full; /* round up */ barlen_LR = (br_hist_LR * (Console_IO.disp_width - res) + full - 1) / full; /* round up */ } else { barlen_TOT = barlen_LR = 0; } if (frames > 0) ppt = (1000 * br_hist_TOT + frames / 2) / frames; /* round nearest */ sprintf(brppt, " [%*i]", digits(frames), br_hist_TOT); if (Console_IO.str_clreoln[0]) /* ClearEndOfLine available */ fprintf(Console_IO.Console_fp, "\n%3d%s %.*s%.*s%s", brhist.kbps[i], brppt, barlen_LR, brhist.bar_percent, barlen_TOT - barlen_LR, brhist.bar_asterisk, Console_IO.str_clreoln); else fprintf(Console_IO.Console_fp, "\n%3d%s %.*s%.*s%*s", brhist.kbps[i], brppt, barlen_LR, brhist.bar_percent, barlen_TOT - barlen_LR, brhist.bar_asterisk, Console_IO.disp_width - res - barlen_TOT, ""); brhist.hist_printed_lines++;}#ifdef RH_HISTstatic voidprogress_line(const lame_global_flags * gf, int full, int frames){ char rst[20] = "\0"; int barlen_TOT = 0, barlen_COD = 0, barlen_RST = 0; int res = 1; int df = 0, hour, min, sec; int fsize = lame_get_framesize(gf); int srate = lame_get_out_samplerate(gf); if (full < frames) { full = frames; } if (srate > 0) { df = full - frames; df *= fsize; df /= srate; } hour = df / 3600; df -= hour * 3600;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -