Blame frontend/brhist.c

Packit 47f805
/*
Packit 47f805
 *	Bitrate histogram source file
Packit 47f805
 *
Packit 47f805
 *	Copyright (c) 2000 Mark Taylor
Packit 47f805
 *
Packit 47f805
 * This library is free software; you can redistribute it and/or
Packit 47f805
 * modify it under the terms of the GNU Library General Public
Packit 47f805
 * License as published by the Free Software Foundation; either
Packit 47f805
 * version 2 of the License, or (at your option) any later version.
Packit 47f805
 *
Packit 47f805
 * This library is distributed in the hope that it will be useful,
Packit 47f805
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 47f805
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
Packit 47f805
 * Library General Public License for more details.
Packit 47f805
 *
Packit 47f805
 * You should have received a copy of the GNU Library General Public
Packit 47f805
 * License along with this library; if not, write to the
Packit 47f805
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit 47f805
 * Boston, MA 02111-1307, USA.
Packit 47f805
 */
Packit 47f805
Packit 47f805
/* $Id: brhist.c,v 1.58 2013/06/11 08:41:31 rbrito Exp $ */
Packit 47f805
Packit 47f805
#ifdef HAVE_CONFIG_H
Packit 47f805
#include <config.h>
Packit 47f805
#endif
Packit 47f805
Packit 47f805
/* basic #define's */
Packit 47f805
Packit 47f805
#ifndef BRHIST_WIDTH
Packit 47f805
# define BRHIST_WIDTH    14
Packit 47f805
#endif
Packit 47f805
#ifndef BRHIST_RES
Packit 47f805
# define BRHIST_RES      14
Packit 47f805
#endif
Packit 47f805
Packit 47f805
Packit 47f805
/* #includes */
Packit 47f805
Packit 47f805
#ifdef STDC_HEADERS
Packit 47f805
# include <stdio.h>
Packit 47f805
# include <stdlib.h>
Packit 47f805
# include <string.h>
Packit 47f805
#endif
Packit 47f805
Packit 47f805
#include "brhist.h"
Packit 47f805
#include "console.h"
Packit 47f805
Packit 47f805
#ifdef WITH_DMALLOC
Packit 47f805
#include <dmalloc.h>
Packit 47f805
#endif
Packit 47f805
Packit 47f805
Packit 47f805
/* Structure holding all data related to the Console I/O
Packit 47f805
 * may be this should be a more global frontend structure. So it
Packit 47f805
 * makes sense to print all files instead with
Packit 47f805
 * printf ( "blah\n") with printf ( "blah%s\n", Console_IO.str_clreoln );
Packit 47f805
 */
Packit 47f805
Packit 47f805
extern Console_IO_t Console_IO;
Packit 47f805
Packit 47f805
static struct brhist_struct {
Packit 47f805
    int     vbr_bitrate_min_index;
Packit 47f805
    int     vbr_bitrate_max_index;
Packit 47f805
    int     kbps[BRHIST_WIDTH];
Packit 47f805
    int     hist_printed_lines;
Packit 47f805
    char    bar_asterisk[512 + 1]; /* buffer filled up with a lot of '*' to print a bar     */
Packit 47f805
    char    bar_percent[512 + 1]; /* buffer filled up with a lot of '%' to print a bar     */
Packit 47f805
    char    bar_coded[512 + 1]; /* buffer filled up with a lot of ' ' to print a bar     */
Packit 47f805
    char    bar_space[512 + 1]; /* buffer filled up with a lot of ' ' to print a bar     */
Packit 47f805
} brhist;
Packit 47f805
Packit 47f805
static int
Packit 47f805
calculate_index(const int *const array, const int len, const int value)
Packit 47f805
{
Packit 47f805
    int     i;
Packit 47f805
Packit 47f805
    for (i = 0; i < len; i++)
Packit 47f805
        if (array[i] == value)
Packit 47f805
            return i;
Packit 47f805
    return -1;
Packit 47f805
}
Packit 47f805
Packit 47f805
int
Packit 47f805
brhist_init(const lame_global_flags * gf, const int bitrate_kbps_min, const int bitrate_kbps_max)
Packit 47f805
{
Packit 47f805
    brhist.hist_printed_lines = 0;
Packit 47f805
Packit 47f805
    /* initialize histogramming data structure */
Packit 47f805
    lame_bitrate_kbps(gf, brhist.kbps);
Packit 47f805
    brhist.vbr_bitrate_min_index = calculate_index(brhist.kbps, BRHIST_WIDTH, bitrate_kbps_min);
Packit 47f805
    brhist.vbr_bitrate_max_index = calculate_index(brhist.kbps, BRHIST_WIDTH, bitrate_kbps_max);
Packit 47f805
Packit 47f805
    if (brhist.vbr_bitrate_min_index >= BRHIST_WIDTH ||
Packit 47f805
        brhist.vbr_bitrate_max_index >= BRHIST_WIDTH) {
Packit 47f805
        error_printf("lame internal error: VBR min %d kbps or VBR max %d kbps not allowed.\n",
Packit 47f805
                     bitrate_kbps_min, bitrate_kbps_max);
Packit 47f805
        return -1;
Packit 47f805
    }
Packit 47f805
Packit 47f805
    memset(brhist.bar_asterisk, '*', sizeof(brhist.bar_asterisk) - 1);
Packit 47f805
    memset(brhist.bar_percent, '%', sizeof(brhist.bar_percent) - 1);
Packit 47f805
    memset(brhist.bar_space, '-', sizeof(brhist.bar_space) - 1);
Packit 47f805
    memset(brhist.bar_coded, '-', sizeof(brhist.bar_space) - 1);
Packit 47f805
Packit 47f805
    return 0;
Packit 47f805
}
Packit 47f805
Packit 47f805
static int
Packit 47f805
digits(unsigned number)
Packit 47f805
{
Packit 47f805
    int     ret = 1;
Packit 47f805
Packit 47f805
    if (number >= 100000000) {
Packit 47f805
        ret += 8;
Packit 47f805
        number /= 100000000;
Packit 47f805
    }
Packit 47f805
    if (number >= 10000) {
Packit 47f805
        ret += 4;
Packit 47f805
        number /= 10000;
Packit 47f805
    }
Packit 47f805
    if (number >= 100) {
Packit 47f805
        ret += 2;
Packit 47f805
        number /= 100;
Packit 47f805
    }
Packit 47f805
    if (number >= 10) {
Packit 47f805
        ret += 1;
Packit 47f805
    }
Packit 47f805
Packit 47f805
    return ret;
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
static void
Packit 47f805
brhist_disp_line(int i, int br_hist_TOT, int br_hist_LR, int full, int frames)
Packit 47f805
{
Packit 47f805
    char    brppt[14];       /* [%] and max. 10 characters for kbps */
Packit 47f805
    int     barlen_TOT;
Packit 47f805
    int     barlen_LR;
Packit 47f805
    int     res = digits(frames) + 3 + 4 + 1;
Packit 47f805
Packit 47f805
    if (full != 0) {
Packit 47f805
        /* some problems when br_hist_TOT \approx br_hist_LR: You can't see that there are still MS frames */
Packit 47f805
        barlen_TOT = (br_hist_TOT * (Console_IO.disp_width - res) + full - 1) / full; /* round up */
Packit 47f805
        barlen_LR = (br_hist_LR * (Console_IO.disp_width - res) + full - 1) / full; /* round up */
Packit 47f805
    }
Packit 47f805
    else {
Packit 47f805
        barlen_TOT = barlen_LR = 0;
Packit 47f805
    }
Packit 47f805
Packit 47f805
    sprintf(brppt, " [%*i]", digits(frames), br_hist_TOT);
Packit 47f805
Packit 47f805
    if (Console_IO.str_clreoln[0]) /* ClearEndOfLine available */
Packit 47f805
        console_printf("\n%3d%s %.*s%.*s%s",
Packit 47f805
                       brhist.kbps[i], brppt,
Packit 47f805
                       barlen_LR, brhist.bar_percent,
Packit 47f805
                       barlen_TOT - barlen_LR, brhist.bar_asterisk, Console_IO.str_clreoln);
Packit 47f805
    else
Packit 47f805
        console_printf("\n%3d%s %.*s%.*s%*s",
Packit 47f805
                       brhist.kbps[i], brppt,
Packit 47f805
                       barlen_LR, brhist.bar_percent,
Packit 47f805
                       barlen_TOT - barlen_LR, brhist.bar_asterisk,
Packit 47f805
                       Console_IO.disp_width - res - barlen_TOT, "");
Packit 47f805
Packit 47f805
    brhist.hist_printed_lines++;
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
static void
Packit 47f805
progress_line(const lame_global_flags * gf, int full, int frames)
Packit 47f805
{
Packit 47f805
    char    rst[20] = "\0";
Packit 47f805
    int     barlen_TOT = 0, barlen_COD = 0, barlen_RST = 0;
Packit 47f805
    int     res = 1;
Packit 47f805
    float   time_in_sec = 0;
Packit 47f805
    unsigned int hour, min, sec;
Packit 47f805
    int     fsize = lame_get_framesize(gf);
Packit 47f805
    int     srate = lame_get_out_samplerate(gf);
Packit 47f805
Packit 47f805
    if (full < frames) {
Packit 47f805
        full = frames;
Packit 47f805
    }
Packit 47f805
    if (srate > 0) {
Packit 47f805
        time_in_sec = (float)(full - frames);
Packit 47f805
        time_in_sec *= fsize;
Packit 47f805
        time_in_sec /= srate;
Packit 47f805
    }
Packit 47f805
    hour = (unsigned int)(time_in_sec / 3600);
Packit 47f805
    time_in_sec -= hour * 3600;
Packit 47f805
    min = (unsigned int)(time_in_sec / 60);
Packit 47f805
    time_in_sec -= min * 60;
Packit 47f805
    sec = (unsigned int)time_in_sec;
Packit 47f805
    if (full != 0) {
Packit 47f805
        if (hour > 0) {
Packit 47f805
            sprintf(rst, "%*u:%02u:%02u", digits(hour), hour, min, sec);
Packit 47f805
            res += digits(hour) + 1 + 5;
Packit 47f805
        }
Packit 47f805
        else {
Packit 47f805
            sprintf(rst, "%02u:%02u", min, sec);
Packit 47f805
            res += 5;
Packit 47f805
        }
Packit 47f805
        /* some problems when br_hist_TOT \approx br_hist_LR: You can't see that there are still MS frames */
Packit 47f805
        barlen_TOT = (full * (Console_IO.disp_width - res) + full - 1) / full; /* round up */
Packit 47f805
        barlen_COD = (frames * (Console_IO.disp_width - res) + full - 1) / full; /* round up */
Packit 47f805
        barlen_RST = barlen_TOT - barlen_COD;
Packit 47f805
        if (barlen_RST == 0) {
Packit 47f805
            sprintf(rst, "%.*s", res - 1, brhist.bar_coded);
Packit 47f805
        }
Packit 47f805
    }
Packit 47f805
    else {
Packit 47f805
        barlen_TOT = barlen_COD = barlen_RST = 0;
Packit 47f805
    }
Packit 47f805
    if (Console_IO.str_clreoln[0]) { /* ClearEndOfLine available */
Packit 47f805
        console_printf("\n%.*s%s%.*s%s",
Packit 47f805
                       barlen_COD, brhist.bar_coded,
Packit 47f805
                       rst, barlen_RST, brhist.bar_space, Console_IO.str_clreoln);
Packit 47f805
    }
Packit 47f805
    else {
Packit 47f805
        console_printf("\n%.*s%s%.*s%*s",
Packit 47f805
                       barlen_COD, brhist.bar_coded,
Packit 47f805
                       rst, barlen_RST, brhist.bar_space, Console_IO.disp_width - res - barlen_TOT,
Packit 47f805
                       "");
Packit 47f805
    }
Packit 47f805
    brhist.hist_printed_lines++;
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
static int
Packit 47f805
stats_value(double x)
Packit 47f805
{
Packit 47f805
    if (x > 0.0) {
Packit 47f805
        console_printf(" %5.1f", x);
Packit 47f805
        return 6;
Packit 47f805
    }
Packit 47f805
    return 0;
Packit 47f805
}
Packit 47f805
Packit 47f805
static int
Packit 47f805
stats_head(double x, const char *txt)
Packit 47f805
{
Packit 47f805
    if (x > 0.0) {
Packit 47f805
        console_printf(txt);
Packit 47f805
        return 6;
Packit 47f805
    }
Packit 47f805
    return 0;
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
static void
Packit 47f805
stats_line(double *stat)
Packit 47f805
{
Packit 47f805
    int     n = 1;
Packit 47f805
    console_printf("\n   kbps     ");
Packit 47f805
    n += 12;
Packit 47f805
    n += stats_head(stat[1], "  mono");
Packit 47f805
    n += stats_head(stat[2], "   IS ");
Packit 47f805
    n += stats_head(stat[3], "   LR ");
Packit 47f805
    n += stats_head(stat[4], "   MS ");
Packit 47f805
    console_printf(" %%    ");
Packit 47f805
    n += 6;
Packit 47f805
    n += stats_head(stat[5], " long ");
Packit 47f805
    n += stats_head(stat[6], "switch");
Packit 47f805
    n += stats_head(stat[7], " short");
Packit 47f805
    n += stats_head(stat[8], " mixed");
Packit 47f805
    n += console_printf(" %%");
Packit 47f805
    if (Console_IO.str_clreoln[0]) { /* ClearEndOfLine available */
Packit 47f805
        console_printf("%s", Console_IO.str_clreoln);
Packit 47f805
    }
Packit 47f805
    else {
Packit 47f805
        console_printf("%*s", Console_IO.disp_width - n, "");
Packit 47f805
    }
Packit 47f805
    brhist.hist_printed_lines++;
Packit 47f805
Packit 47f805
    n = 1;
Packit 47f805
    console_printf("\n  %5.1f     ", stat[0]);
Packit 47f805
    n += 12;
Packit 47f805
    n += stats_value(stat[1]);
Packit 47f805
    n += stats_value(stat[2]);
Packit 47f805
    n += stats_value(stat[3]);
Packit 47f805
    n += stats_value(stat[4]);
Packit 47f805
    console_printf("      ");
Packit 47f805
    n += 6;
Packit 47f805
    n += stats_value(stat[5]);
Packit 47f805
    n += stats_value(stat[6]);
Packit 47f805
    n += stats_value(stat[7]);
Packit 47f805
    n += stats_value(stat[8]);
Packit 47f805
    if (Console_IO.str_clreoln[0]) { /* ClearEndOfLine available */
Packit 47f805
        console_printf("%s", Console_IO.str_clreoln);
Packit 47f805
    }
Packit 47f805
    else {
Packit 47f805
        console_printf("%*s", Console_IO.disp_width - n, "");
Packit 47f805
    }
Packit 47f805
    brhist.hist_printed_lines++;
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
/* Yes, not very good */
Packit 47f805
#define LR  0
Packit 47f805
#define MS  2
Packit 47f805
Packit 47f805
void
Packit 47f805
brhist_disp(const lame_global_flags * gf)
Packit 47f805
{
Packit 47f805
    int     i, lines_used = 0;
Packit 47f805
    int     br_hist[BRHIST_WIDTH]; /* how often a frame size was used */
Packit 47f805
    int     br_sm_hist[BRHIST_WIDTH][4]; /* how often a special frame size/stereo mode commbination was used */
Packit 47f805
    int     st_mode[4];
Packit 47f805
    int     bl_type[6];
Packit 47f805
    int     frames;          /* total number of encoded frames */
Packit 47f805
    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 */
Packit 47f805
    double  sum = 0.;
Packit 47f805
Packit 47f805
    double  stat[9] = { 0 };
Packit 47f805
    int     st_frames = 0;
Packit 47f805
Packit 47f805
Packit 47f805
    brhist.hist_printed_lines = 0; /* printed number of lines for the brhist functionality, used to skip back the right number of lines */
Packit 47f805
Packit 47f805
    lame_bitrate_stereo_mode_hist(gf, br_sm_hist);
Packit 47f805
    lame_bitrate_hist(gf, br_hist);
Packit 47f805
    lame_stereo_mode_hist(gf, st_mode);
Packit 47f805
    lame_block_type_hist(gf, bl_type);
Packit 47f805
Packit 47f805
    frames = most_often = 0;
Packit 47f805
    for (i = 0; i < BRHIST_WIDTH; i++) {
Packit 47f805
        frames += br_hist[i];
Packit 47f805
        sum += br_hist[i] * brhist.kbps[i];
Packit 47f805
        if (most_often < br_hist[i])
Packit 47f805
            most_often = br_hist[i];
Packit 47f805
        if (br_hist[i])
Packit 47f805
            ++lines_used;
Packit 47f805
    }
Packit 47f805
Packit 47f805
    for (i = 0; i < BRHIST_WIDTH; i++) {
Packit 47f805
        int     show = br_hist[i];
Packit 47f805
        show = show && (lines_used > 1);
Packit 47f805
        if (show || (i >= brhist.vbr_bitrate_min_index && i <= brhist.vbr_bitrate_max_index))
Packit 47f805
            brhist_disp_line(i, br_hist[i], br_sm_hist[i][LR], most_often, frames);
Packit 47f805
    }
Packit 47f805
    for (i = 0; i < 4; i++) {
Packit 47f805
        st_frames += st_mode[i];
Packit 47f805
    }
Packit 47f805
    if (frames > 0) {
Packit 47f805
        stat[0] = sum / frames;
Packit 47f805
        stat[1] = 100. * (frames - st_frames) / frames;
Packit 47f805
    }
Packit 47f805
    if (st_frames > 0) {
Packit 47f805
        stat[2] = 0.0;
Packit 47f805
        stat[3] = 100. * st_mode[LR] / st_frames;
Packit 47f805
        stat[4] = 100. * st_mode[MS] / st_frames;
Packit 47f805
    }
Packit 47f805
    if (bl_type[5] > 0) {
Packit 47f805
        stat[5] = 100. * bl_type[0] / bl_type[5];
Packit 47f805
        stat[6] = 100. * (bl_type[1] + bl_type[3]) / bl_type[5];
Packit 47f805
        stat[7] = 100. * bl_type[2] / bl_type[5];
Packit 47f805
        stat[8] = 100. * bl_type[4] / bl_type[5];
Packit 47f805
    }
Packit 47f805
    progress_line(gf, lame_get_totalframes(gf), frames);
Packit 47f805
    stats_line(stat);
Packit 47f805
}
Packit 47f805
Packit 47f805
void
Packit 47f805
brhist_jump_back(void)
Packit 47f805
{
Packit 47f805
    console_up(brhist.hist_printed_lines);
Packit 47f805
    brhist.hist_printed_lines = 0;
Packit 47f805
}
Packit 47f805
Packit 47f805
/*
Packit 47f805
 * 1)
Packit 47f805
 *
Packit 47f805
 * Taken from Termcap_Manual.html:
Packit 47f805
 *
Packit 47f805
 * With the Unix version of termcap, you must allocate space for the description yourself and pass
Packit 47f805
 * the address of the space as the argument buffer. There is no way you can tell how much space is
Packit 47f805
 * needed, so the convention is to allocate a buffer 2048 characters long and assume that is
Packit 47f805
 * enough.  (Formerly the convention was to allocate 1024 characters and assume that was enough.
Packit 47f805
 * But one day, for one kind of terminal, that was not enough.)
Packit 47f805
 */
Packit 47f805
Packit 47f805