From 3fde9964ce31948ebf2a11f1a0eae77d876600ee Mon Sep 17 00:00:00 2001 From: John Koleszar Date: Mon, 20 Jun 2011 17:09:29 -0400 Subject: [PATCH] vpxenc: add quantizer histogram display Add the --q-hist=n option, which displays a histogram with n buckets for the quantizer selected on each frame. Change-Id: I59b020c26b0acae0b938685081d9932bd98df5c9 --- vpxenc.c | 165 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 164 insertions(+), 1 deletion(-) diff --git a/vpxenc.c b/vpxenc.c index 89cdef01e..264a719ea 100644 --- a/vpxenc.c +++ b/vpxenc.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "vpx/vpx_encoder.h" #if USE_POSIX_MMAP #include @@ -909,12 +910,14 @@ static const arg_def_t framerate = ARG_DEF(NULL, "fps", 1, "Stream frame rate (rate/scale)"); static const arg_def_t use_ivf = ARG_DEF(NULL, "ivf", 0, "Output IVF (default is WebM)"); +static const arg_def_t q_hist_n = ARG_DEF(NULL, "q-hist", 1, + "Show quantizer histogram (n-buckets)"); static const arg_def_t *main_args[] = { &debugmode, &outputfile, &codecarg, &passes, &pass_arg, &fpf_name, &limit, &deadline, &best_dl, &good_dl, &rt_dl, - &verbosearg, &psnrarg, &use_ivf, + &verbosearg, &psnrarg, &use_ivf, &q_hist_n, NULL }; @@ -1107,6 +1110,149 @@ static void usage_exit() exit(EXIT_FAILURE); } + +#define HIST_BAR_MAX 40 +struct hist_bucket +{ + int low, high, count; +}; + + +static int merge_hist_buckets(struct hist_bucket *bucket, + int *buckets_, + int max_buckets) +{ + int small_bucket = 0, merge_bucket = INT_MAX, big_bucket=0; + int buckets = *buckets_; + int i; + + /* Find the extrema for this list of buckets */ + big_bucket = small_bucket = 0; + for(i=0; i < buckets; i++) + { + if(bucket[i].count < bucket[small_bucket].count) + small_bucket = i; + if(bucket[i].count > bucket[big_bucket].count) + big_bucket = i; + } + + /* If we have too many buckets, merge the smallest with an ajacent + * bucket. + */ + while(buckets > max_buckets) + { + int last_bucket = buckets - 1; + + // merge the small bucket with an adjacent one. + if(small_bucket == 0) + merge_bucket = 1; + else if(small_bucket == last_bucket) + merge_bucket = last_bucket - 1; + else if(bucket[small_bucket - 1].count < bucket[small_bucket + 1].count) + merge_bucket = small_bucket - 1; + else + merge_bucket = small_bucket + 1; + + assert(abs(merge_bucket - small_bucket) <= 1); + assert(small_bucket < buckets); + assert(big_bucket < buckets); + assert(merge_bucket < buckets); + + if(merge_bucket < small_bucket) + { + bucket[merge_bucket].high = bucket[small_bucket].high; + bucket[merge_bucket].count += bucket[small_bucket].count; + } + else + { + bucket[small_bucket].high = bucket[merge_bucket].high; + bucket[small_bucket].count += bucket[merge_bucket].count; + merge_bucket = small_bucket; + } + + assert(bucket[merge_bucket].low != bucket[merge_bucket].high); + + buckets--; + + /* Remove the merge_bucket from the list, and find the new small + * and big buckets while we're at it + */ + big_bucket = small_bucket = 0; + for(i=0; i < buckets; i++) + { + if(i > merge_bucket) + bucket[i] = bucket[i+1]; + + if(bucket[i].count < bucket[small_bucket].count) + small_bucket = i; + if(bucket[i].count > bucket[big_bucket].count) + big_bucket = i; + } + + } + + *buckets_ = buckets; + return bucket[big_bucket].count; +} + + +static void show_histogram(const struct hist_bucket *bucket, + int buckets, + int total, + int scale) +{ + int i; + + for(i=0; i