2 * Copyright (c) 2014 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
17 #include "./rate_hist.h"
20 #define HIST_BAR_MAX 40
33 struct hist_bucket bucket[RATE_BINS];
37 struct rate_hist *init_rate_histogram(const vpx_codec_enc_cfg_t *cfg,
38 const vpx_rational_t *fps) {
40 struct rate_hist *hist = malloc(sizeof(*hist));
42 // Determine the number of samples in the buffer. Use the file's framerate
43 // to determine the number of frames in rc_buf_sz milliseconds, with an
44 // adjustment (5/4) to account for alt-refs
45 hist->samples = cfg->rc_buf_sz * 5 / 4 * fps->num / fps->den / 1000;
47 // prevent division by zero
48 if (hist->samples == 0) hist->samples = 1;
53 hist->pts = calloc(hist->samples, sizeof(*hist->pts));
54 hist->sz = calloc(hist->samples, sizeof(*hist->sz));
55 for (i = 0; i < RATE_BINS; i++) {
56 hist->bucket[i].low = INT_MAX;
57 hist->bucket[i].high = 0;
58 hist->bucket[i].count = 0;
64 void destroy_rate_histogram(struct rate_hist *hist) {
72 void update_rate_histogram(struct rate_hist *hist,
73 const vpx_codec_enc_cfg_t *cfg,
74 const vpx_codec_cx_pkt_t *pkt) {
77 int64_t avg_bitrate = 0;
79 const int64_t now = pkt->data.frame.pts * 1000 *
80 (uint64_t)cfg->g_timebase.num /
81 (uint64_t)cfg->g_timebase.den;
83 int idx = hist->frames++ % hist->samples;
85 hist->sz[idx] = (int)pkt->data.frame.sz;
87 if (now < cfg->rc_buf_initial_sz) return;
89 if (!cfg->rc_target_bitrate) return;
93 /* Sum the size over the past rc_buf_sz ms */
94 for (i = hist->frames; i > 0 && hist->frames - i < hist->samples; i--) {
95 const int i_idx = (i - 1) % hist->samples;
97 then = hist->pts[i_idx];
98 if (now - then > cfg->rc_buf_sz) break;
99 sum_sz += hist->sz[i_idx];
102 if (now == then) return;
104 avg_bitrate = sum_sz * 8 * 1000 / (now - then);
105 idx = (int)(avg_bitrate * (RATE_BINS / 2) / (cfg->rc_target_bitrate * 1000));
106 if (idx < 0) idx = 0;
107 if (idx > RATE_BINS - 1) idx = RATE_BINS - 1;
108 if (hist->bucket[idx].low > avg_bitrate)
109 hist->bucket[idx].low = (int)avg_bitrate;
110 if (hist->bucket[idx].high < avg_bitrate)
111 hist->bucket[idx].high = (int)avg_bitrate;
112 hist->bucket[idx].count++;
116 static int merge_hist_buckets(struct hist_bucket *bucket, int max_buckets,
118 int small_bucket = 0, merge_bucket = INT_MAX, big_bucket = 0;
119 int buckets = *num_buckets;
122 /* Find the extrema for this list of buckets */
123 big_bucket = small_bucket = 0;
124 for (i = 0; i < buckets; i++) {
125 if (bucket[i].count < bucket[small_bucket].count) small_bucket = i;
126 if (bucket[i].count > bucket[big_bucket].count) big_bucket = i;
129 /* If we have too many buckets, merge the smallest with an adjacent
132 while (buckets > max_buckets) {
133 int last_bucket = buckets - 1;
135 /* merge the small bucket with an adjacent one. */
136 if (small_bucket == 0)
138 else if (small_bucket == last_bucket)
139 merge_bucket = last_bucket - 1;
140 else if (bucket[small_bucket - 1].count < bucket[small_bucket + 1].count)
141 merge_bucket = small_bucket - 1;
143 merge_bucket = small_bucket + 1;
145 assert(abs(merge_bucket - small_bucket) <= 1);
146 assert(small_bucket < buckets);
147 assert(big_bucket < buckets);
148 assert(merge_bucket < buckets);
150 if (merge_bucket < small_bucket) {
151 bucket[merge_bucket].high = bucket[small_bucket].high;
152 bucket[merge_bucket].count += bucket[small_bucket].count;
154 bucket[small_bucket].high = bucket[merge_bucket].high;
155 bucket[small_bucket].count += bucket[merge_bucket].count;
156 merge_bucket = small_bucket;
159 assert(bucket[merge_bucket].low != bucket[merge_bucket].high);
163 /* Remove the merge_bucket from the list, and find the new small
164 * and big buckets while we're at it
166 big_bucket = small_bucket = 0;
167 for (i = 0; i < buckets; i++) {
168 if (i > merge_bucket) bucket[i] = bucket[i + 1];
170 if (bucket[i].count < bucket[small_bucket].count) small_bucket = i;
171 if (bucket[i].count > bucket[big_bucket].count) big_bucket = i;
175 *num_buckets = buckets;
176 return bucket[big_bucket].count;
179 static void show_histogram(const struct hist_bucket *bucket, int buckets,
180 int total, int scale) {
181 const char *pat1, *pat2;
184 switch ((int)(log(bucket[buckets - 1].high) / log(10)) + 1) {
211 pat1 = "%12d %10s: ";
212 pat2 = "%12d-%10d: ";
216 for (i = 0; i < buckets; i++) {
221 pct = (float)(100.0 * bucket[i].count / total);
222 len = HIST_BAR_MAX * bucket[i].count / scale;
223 if (len < 1) len = 1;
224 assert(len <= HIST_BAR_MAX);
226 if (bucket[i].low == bucket[i].high)
227 fprintf(stderr, pat1, bucket[i].low, "");
229 fprintf(stderr, pat2, bucket[i].low, bucket[i].high);
231 for (j = 0; j < HIST_BAR_MAX; j++) fprintf(stderr, j < len ? "=" : " ");
232 fprintf(stderr, "\t%5d (%6.2f%%)\n", bucket[i].count, pct);
236 void show_q_histogram(const int counts[64], int max_buckets) {
237 struct hist_bucket bucket[64];
243 for (i = 0; i < 64; i++) {
245 bucket[buckets].low = bucket[buckets].high = i;
246 bucket[buckets].count = counts[i];
252 fprintf(stderr, "\nQuantizer Selection:\n");
253 scale = merge_hist_buckets(bucket, max_buckets, &buckets);
254 show_histogram(bucket, buckets, total, scale);
257 void show_rate_histogram(struct rate_hist *hist, const vpx_codec_enc_cfg_t *cfg,
262 for (i = 0; i < RATE_BINS; i++) {
263 if (hist->bucket[i].low == INT_MAX) continue;
264 hist->bucket[buckets++] = hist->bucket[i];
267 fprintf(stderr, "\nRate (over %dms window):\n", cfg->rc_buf_sz);
268 scale = merge_hist_buckets(hist->bucket, max_buckets, &buckets);
269 show_histogram(hist->bucket, buckets, hist->total, scale);