#include <stdarg.h>
#include <string.h>
#include <limits.h>
+#include <assert.h>
#include "vpx/vpx_encoder.h"
#if USE_POSIX_MMAP
#include <sys/types.h>
"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
};
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<buckets; i++)
+ {
+ int len;
+ int j;
+ float pct;
+
+ pct = 100.0 * (float)bucket[i].count / (float)total;
+ len = HIST_BAR_MAX * bucket[i].count / scale;
+ if(len < 1)
+ len = 1;
+ assert(len <= HIST_BAR_MAX);
+
+ if(bucket[i].low == bucket[i].high)
+ fprintf(stderr, "%4d : ", bucket[i].low);
+ else
+ fprintf(stderr, "%4d-%2d: ", bucket[i].low, bucket[i].high);
+
+ for(j=0; j<HIST_BAR_MAX; j++)
+ fprintf(stderr, j<len?"=":" ");
+ fprintf(stderr, "\t%5d (%6.2f%%)\n",bucket[i].count,pct);
+ }
+}
+
+
+static void show_q_histogram(const int counts[64], int max_buckets)
+{
+ struct hist_bucket bucket[64];
+ int buckets = 0;
+ int total = 0;
+ int scale;
+ int i;
+
+
+ for(i=0; i<64; i++)
+ {
+ if(counts[i])
+ {
+ bucket[buckets].low = bucket[buckets].high = i;
+ bucket[buckets].count = counts[i];
+ buckets++;
+ total += counts[i];
+ }
+ }
+
+ fprintf(stderr, "Quantizer Selection:\n");
+ scale = merge_hist_buckets(bucket, &buckets, max_buckets);
+ show_histogram(bucket, buckets, total, scale);
+}
+
+
#define ARG_CTRL_CNT_MAX 10
int main(int argc, const char **argv_)
double psnr_totals[4] = {0, 0, 0, 0};
int psnr_count = 0;
stereo_format_t stereo_fmt = STEREO_FORMAT_MONO;
+ int counts[64]={0};
+ int show_q_hist_buckets=0;
exec_name = argv_[0];
ebml.last_pts_ms = -1;
out_fn = arg.val;
else if (arg_match(&arg, &debugmode, argi))
ebml.debug = 1;
+ else if (arg_match(&arg, &q_hist_n, argi))
+ show_q_hist_buckets = arg_parse_uint(&arg);
else
argj++;
}
vpx_usec_timer_mark(&timer);
cx_time += vpx_usec_timer_elapsed(&timer);
ctx_exit_on_error(&encoder, "Failed to encode frame");
+
+ if(cfg.g_pass != VPX_RC_FIRST_PASS)
+ {
+ int q;
+
+ vpx_codec_control(&encoder, VP8E_GET_LAST_QUANTIZER_64, &q);
+ ctx_exit_on_error(&encoder, "Failed to read quantizer");
+ counts[q]++;
+ }
+
got_data = 0;
while ((pkt = vpx_codec_get_cx_data(&encoder, &iter)))
break;
}
+ if (show_q_hist_buckets)
+ show_q_histogram(counts, show_q_hist_buckets);
+
vpx_img_free(&raw);
free(argv);
return EXIT_SUCCESS;