xref: /aosp_15_r20/external/libaom/stats/rate_hist.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #include "stats/rate_hist.h"
13*77c1e3ccSAndroid Build Coastguard Worker 
14*77c1e3ccSAndroid Build Coastguard Worker #include <assert.h>
15*77c1e3ccSAndroid Build Coastguard Worker #include <limits.h>
16*77c1e3ccSAndroid Build Coastguard Worker #include <math.h>
17*77c1e3ccSAndroid Build Coastguard Worker #include <stdint.h>
18*77c1e3ccSAndroid Build Coastguard Worker #include <stdio.h>
19*77c1e3ccSAndroid Build Coastguard Worker #include <stdlib.h>
20*77c1e3ccSAndroid Build Coastguard Worker 
21*77c1e3ccSAndroid Build Coastguard Worker #define RATE_BINS 100
22*77c1e3ccSAndroid Build Coastguard Worker #define HIST_BAR_MAX 40
23*77c1e3ccSAndroid Build Coastguard Worker 
24*77c1e3ccSAndroid Build Coastguard Worker struct hist_bucket {
25*77c1e3ccSAndroid Build Coastguard Worker   int low;
26*77c1e3ccSAndroid Build Coastguard Worker   int high;
27*77c1e3ccSAndroid Build Coastguard Worker   int count;
28*77c1e3ccSAndroid Build Coastguard Worker };
29*77c1e3ccSAndroid Build Coastguard Worker 
30*77c1e3ccSAndroid Build Coastguard Worker struct rate_hist {
31*77c1e3ccSAndroid Build Coastguard Worker   int64_t *pts;
32*77c1e3ccSAndroid Build Coastguard Worker   int *sz;
33*77c1e3ccSAndroid Build Coastguard Worker   int samples;
34*77c1e3ccSAndroid Build Coastguard Worker   int frames;
35*77c1e3ccSAndroid Build Coastguard Worker   struct hist_bucket bucket[RATE_BINS];
36*77c1e3ccSAndroid Build Coastguard Worker   int total;
37*77c1e3ccSAndroid Build Coastguard Worker };
38*77c1e3ccSAndroid Build Coastguard Worker 
init_rate_histogram(const aom_codec_enc_cfg_t * cfg,const aom_rational_t * fps)39*77c1e3ccSAndroid Build Coastguard Worker struct rate_hist *init_rate_histogram(const aom_codec_enc_cfg_t *cfg,
40*77c1e3ccSAndroid Build Coastguard Worker                                       const aom_rational_t *fps) {
41*77c1e3ccSAndroid Build Coastguard Worker   int i;
42*77c1e3ccSAndroid Build Coastguard Worker   struct rate_hist *hist = calloc(1, sizeof(*hist));
43*77c1e3ccSAndroid Build Coastguard Worker 
44*77c1e3ccSAndroid Build Coastguard Worker   if (hist == NULL || cfg == NULL || fps == NULL || fps->num == 0 ||
45*77c1e3ccSAndroid Build Coastguard Worker       fps->den == 0) {
46*77c1e3ccSAndroid Build Coastguard Worker     goto fail;
47*77c1e3ccSAndroid Build Coastguard Worker   }
48*77c1e3ccSAndroid Build Coastguard Worker 
49*77c1e3ccSAndroid Build Coastguard Worker   // Determine the number of samples in the buffer. Use the file's framerate
50*77c1e3ccSAndroid Build Coastguard Worker   // to determine the number of frames in rc_buf_sz milliseconds, with an
51*77c1e3ccSAndroid Build Coastguard Worker   // adjustment (5/4) to account for alt-refs
52*77c1e3ccSAndroid Build Coastguard Worker   hist->samples =
53*77c1e3ccSAndroid Build Coastguard Worker       (int)((int64_t)cfg->rc_buf_sz * 5 / 4 * fps->num / fps->den / 1000);
54*77c1e3ccSAndroid Build Coastguard Worker 
55*77c1e3ccSAndroid Build Coastguard Worker   // prevent division by zero
56*77c1e3ccSAndroid Build Coastguard Worker   if (hist->samples == 0) hist->samples = 1;
57*77c1e3ccSAndroid Build Coastguard Worker 
58*77c1e3ccSAndroid Build Coastguard Worker   hist->frames = 0;
59*77c1e3ccSAndroid Build Coastguard Worker   hist->total = 0;
60*77c1e3ccSAndroid Build Coastguard Worker 
61*77c1e3ccSAndroid Build Coastguard Worker   hist->pts = calloc(hist->samples, sizeof(*hist->pts));
62*77c1e3ccSAndroid Build Coastguard Worker   hist->sz = calloc(hist->samples, sizeof(*hist->sz));
63*77c1e3ccSAndroid Build Coastguard Worker   if (hist->pts == NULL || hist->sz == NULL) goto fail;
64*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < RATE_BINS; i++) {
65*77c1e3ccSAndroid Build Coastguard Worker     hist->bucket[i].low = INT_MAX;
66*77c1e3ccSAndroid Build Coastguard Worker     hist->bucket[i].high = 0;
67*77c1e3ccSAndroid Build Coastguard Worker     hist->bucket[i].count = 0;
68*77c1e3ccSAndroid Build Coastguard Worker   }
69*77c1e3ccSAndroid Build Coastguard Worker 
70*77c1e3ccSAndroid Build Coastguard Worker   return hist;
71*77c1e3ccSAndroid Build Coastguard Worker 
72*77c1e3ccSAndroid Build Coastguard Worker fail:
73*77c1e3ccSAndroid Build Coastguard Worker   fprintf(stderr,
74*77c1e3ccSAndroid Build Coastguard Worker           "Warning: Unable to allocate buffers required for "
75*77c1e3ccSAndroid Build Coastguard Worker           "show_rate_histogram().\n"
76*77c1e3ccSAndroid Build Coastguard Worker           "Continuing without rate histogram feature...\n");
77*77c1e3ccSAndroid Build Coastguard Worker   destroy_rate_histogram(hist);
78*77c1e3ccSAndroid Build Coastguard Worker   return NULL;
79*77c1e3ccSAndroid Build Coastguard Worker }
80*77c1e3ccSAndroid Build Coastguard Worker 
destroy_rate_histogram(struct rate_hist * hist)81*77c1e3ccSAndroid Build Coastguard Worker void destroy_rate_histogram(struct rate_hist *hist) {
82*77c1e3ccSAndroid Build Coastguard Worker   if (hist) {
83*77c1e3ccSAndroid Build Coastguard Worker     free(hist->pts);
84*77c1e3ccSAndroid Build Coastguard Worker     free(hist->sz);
85*77c1e3ccSAndroid Build Coastguard Worker     free(hist);
86*77c1e3ccSAndroid Build Coastguard Worker   }
87*77c1e3ccSAndroid Build Coastguard Worker }
88*77c1e3ccSAndroid Build Coastguard Worker 
update_rate_histogram(struct rate_hist * hist,const aom_codec_enc_cfg_t * cfg,const aom_codec_cx_pkt_t * pkt)89*77c1e3ccSAndroid Build Coastguard Worker void update_rate_histogram(struct rate_hist *hist,
90*77c1e3ccSAndroid Build Coastguard Worker                            const aom_codec_enc_cfg_t *cfg,
91*77c1e3ccSAndroid Build Coastguard Worker                            const aom_codec_cx_pkt_t *pkt) {
92*77c1e3ccSAndroid Build Coastguard Worker   int i;
93*77c1e3ccSAndroid Build Coastguard Worker   int64_t then = 0;
94*77c1e3ccSAndroid Build Coastguard Worker   int64_t avg_bitrate = 0;
95*77c1e3ccSAndroid Build Coastguard Worker   int64_t sum_sz = 0;
96*77c1e3ccSAndroid Build Coastguard Worker   const int64_t now = pkt->data.frame.pts * 1000 *
97*77c1e3ccSAndroid Build Coastguard Worker                       (uint64_t)cfg->g_timebase.num /
98*77c1e3ccSAndroid Build Coastguard Worker                       (uint64_t)cfg->g_timebase.den;
99*77c1e3ccSAndroid Build Coastguard Worker 
100*77c1e3ccSAndroid Build Coastguard Worker   int idx;
101*77c1e3ccSAndroid Build Coastguard Worker 
102*77c1e3ccSAndroid Build Coastguard Worker   if (hist == NULL || cfg == NULL || pkt == NULL) return;
103*77c1e3ccSAndroid Build Coastguard Worker 
104*77c1e3ccSAndroid Build Coastguard Worker   idx = hist->frames++ % hist->samples;
105*77c1e3ccSAndroid Build Coastguard Worker   hist->pts[idx] = now;
106*77c1e3ccSAndroid Build Coastguard Worker   hist->sz[idx] = (int)pkt->data.frame.sz;
107*77c1e3ccSAndroid Build Coastguard Worker 
108*77c1e3ccSAndroid Build Coastguard Worker   if (now < cfg->rc_buf_initial_sz) return;
109*77c1e3ccSAndroid Build Coastguard Worker 
110*77c1e3ccSAndroid Build Coastguard Worker   if (!cfg->rc_target_bitrate) return;
111*77c1e3ccSAndroid Build Coastguard Worker 
112*77c1e3ccSAndroid Build Coastguard Worker   then = now;
113*77c1e3ccSAndroid Build Coastguard Worker 
114*77c1e3ccSAndroid Build Coastguard Worker   /* Sum the size over the past rc_buf_sz ms */
115*77c1e3ccSAndroid Build Coastguard Worker   for (i = hist->frames; i > 0 && hist->frames - i < hist->samples; i--) {
116*77c1e3ccSAndroid Build Coastguard Worker     const int i_idx = (i - 1) % hist->samples;
117*77c1e3ccSAndroid Build Coastguard Worker 
118*77c1e3ccSAndroid Build Coastguard Worker     then = hist->pts[i_idx];
119*77c1e3ccSAndroid Build Coastguard Worker     if (now - then > cfg->rc_buf_sz) break;
120*77c1e3ccSAndroid Build Coastguard Worker     sum_sz += hist->sz[i_idx];
121*77c1e3ccSAndroid Build Coastguard Worker   }
122*77c1e3ccSAndroid Build Coastguard Worker 
123*77c1e3ccSAndroid Build Coastguard Worker   if (now == then) return;
124*77c1e3ccSAndroid Build Coastguard Worker 
125*77c1e3ccSAndroid Build Coastguard Worker   avg_bitrate = sum_sz * 8 * 1000 / (now - then);
126*77c1e3ccSAndroid Build Coastguard Worker   idx = (int)(avg_bitrate * (RATE_BINS / 2) / (cfg->rc_target_bitrate * 1000));
127*77c1e3ccSAndroid Build Coastguard Worker   if (idx < 0) idx = 0;
128*77c1e3ccSAndroid Build Coastguard Worker   if (idx > RATE_BINS - 1) idx = RATE_BINS - 1;
129*77c1e3ccSAndroid Build Coastguard Worker   if (hist->bucket[idx].low > avg_bitrate)
130*77c1e3ccSAndroid Build Coastguard Worker     hist->bucket[idx].low = (int)avg_bitrate;
131*77c1e3ccSAndroid Build Coastguard Worker   if (hist->bucket[idx].high < avg_bitrate)
132*77c1e3ccSAndroid Build Coastguard Worker     hist->bucket[idx].high = (int)avg_bitrate;
133*77c1e3ccSAndroid Build Coastguard Worker   hist->bucket[idx].count++;
134*77c1e3ccSAndroid Build Coastguard Worker   hist->total++;
135*77c1e3ccSAndroid Build Coastguard Worker }
136*77c1e3ccSAndroid Build Coastguard Worker 
merge_hist_buckets(struct hist_bucket * bucket,int max_buckets,int * num_buckets)137*77c1e3ccSAndroid Build Coastguard Worker static int merge_hist_buckets(struct hist_bucket *bucket, int max_buckets,
138*77c1e3ccSAndroid Build Coastguard Worker                               int *num_buckets) {
139*77c1e3ccSAndroid Build Coastguard Worker   int small_bucket = 0, merge_bucket = INT_MAX, big_bucket = 0;
140*77c1e3ccSAndroid Build Coastguard Worker   int buckets;
141*77c1e3ccSAndroid Build Coastguard Worker   int i;
142*77c1e3ccSAndroid Build Coastguard Worker 
143*77c1e3ccSAndroid Build Coastguard Worker   assert(bucket != NULL);
144*77c1e3ccSAndroid Build Coastguard Worker   assert(num_buckets != NULL);
145*77c1e3ccSAndroid Build Coastguard Worker 
146*77c1e3ccSAndroid Build Coastguard Worker   buckets = *num_buckets;
147*77c1e3ccSAndroid Build Coastguard Worker 
148*77c1e3ccSAndroid Build Coastguard Worker   /* Find the extrema for this list of buckets */
149*77c1e3ccSAndroid Build Coastguard Worker   big_bucket = small_bucket = 0;
150*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < buckets; i++) {
151*77c1e3ccSAndroid Build Coastguard Worker     if (bucket[i].count < bucket[small_bucket].count) small_bucket = i;
152*77c1e3ccSAndroid Build Coastguard Worker     if (bucket[i].count > bucket[big_bucket].count) big_bucket = i;
153*77c1e3ccSAndroid Build Coastguard Worker   }
154*77c1e3ccSAndroid Build Coastguard Worker 
155*77c1e3ccSAndroid Build Coastguard Worker   /* If we have too many buckets, merge the smallest with an adjacent
156*77c1e3ccSAndroid Build Coastguard Worker    * bucket.
157*77c1e3ccSAndroid Build Coastguard Worker    */
158*77c1e3ccSAndroid Build Coastguard Worker   while (buckets > max_buckets) {
159*77c1e3ccSAndroid Build Coastguard Worker     int last_bucket = buckets - 1;
160*77c1e3ccSAndroid Build Coastguard Worker 
161*77c1e3ccSAndroid Build Coastguard Worker     /* merge the small bucket with an adjacent one. */
162*77c1e3ccSAndroid Build Coastguard Worker     if (small_bucket == 0)
163*77c1e3ccSAndroid Build Coastguard Worker       merge_bucket = 1;
164*77c1e3ccSAndroid Build Coastguard Worker     else if (small_bucket == last_bucket)
165*77c1e3ccSAndroid Build Coastguard Worker       merge_bucket = last_bucket - 1;
166*77c1e3ccSAndroid Build Coastguard Worker     else if (bucket[small_bucket - 1].count < bucket[small_bucket + 1].count)
167*77c1e3ccSAndroid Build Coastguard Worker       merge_bucket = small_bucket - 1;
168*77c1e3ccSAndroid Build Coastguard Worker     else
169*77c1e3ccSAndroid Build Coastguard Worker       merge_bucket = small_bucket + 1;
170*77c1e3ccSAndroid Build Coastguard Worker 
171*77c1e3ccSAndroid Build Coastguard Worker     assert(abs(merge_bucket - small_bucket) <= 1);
172*77c1e3ccSAndroid Build Coastguard Worker     assert(small_bucket < buckets);
173*77c1e3ccSAndroid Build Coastguard Worker     assert(big_bucket < buckets);
174*77c1e3ccSAndroid Build Coastguard Worker     assert(merge_bucket < buckets);
175*77c1e3ccSAndroid Build Coastguard Worker 
176*77c1e3ccSAndroid Build Coastguard Worker     if (merge_bucket < small_bucket) {
177*77c1e3ccSAndroid Build Coastguard Worker       bucket[merge_bucket].high = bucket[small_bucket].high;
178*77c1e3ccSAndroid Build Coastguard Worker       bucket[merge_bucket].count += bucket[small_bucket].count;
179*77c1e3ccSAndroid Build Coastguard Worker     } else {
180*77c1e3ccSAndroid Build Coastguard Worker       bucket[small_bucket].high = bucket[merge_bucket].high;
181*77c1e3ccSAndroid Build Coastguard Worker       bucket[small_bucket].count += bucket[merge_bucket].count;
182*77c1e3ccSAndroid Build Coastguard Worker       merge_bucket = small_bucket;
183*77c1e3ccSAndroid Build Coastguard Worker     }
184*77c1e3ccSAndroid Build Coastguard Worker 
185*77c1e3ccSAndroid Build Coastguard Worker     assert(bucket[merge_bucket].low != bucket[merge_bucket].high);
186*77c1e3ccSAndroid Build Coastguard Worker 
187*77c1e3ccSAndroid Build Coastguard Worker     buckets--;
188*77c1e3ccSAndroid Build Coastguard Worker 
189*77c1e3ccSAndroid Build Coastguard Worker     /* Remove the merge_bucket from the list, and find the new small
190*77c1e3ccSAndroid Build Coastguard Worker      * and big buckets while we're at it
191*77c1e3ccSAndroid Build Coastguard Worker      */
192*77c1e3ccSAndroid Build Coastguard Worker     big_bucket = small_bucket = 0;
193*77c1e3ccSAndroid Build Coastguard Worker     for (i = 0; i < buckets; i++) {
194*77c1e3ccSAndroid Build Coastguard Worker       if (i > merge_bucket) bucket[i] = bucket[i + 1];
195*77c1e3ccSAndroid Build Coastguard Worker 
196*77c1e3ccSAndroid Build Coastguard Worker       if (bucket[i].count < bucket[small_bucket].count) small_bucket = i;
197*77c1e3ccSAndroid Build Coastguard Worker       if (bucket[i].count > bucket[big_bucket].count) big_bucket = i;
198*77c1e3ccSAndroid Build Coastguard Worker     }
199*77c1e3ccSAndroid Build Coastguard Worker   }
200*77c1e3ccSAndroid Build Coastguard Worker 
201*77c1e3ccSAndroid Build Coastguard Worker   *num_buckets = buckets;
202*77c1e3ccSAndroid Build Coastguard Worker   return bucket[big_bucket].count;
203*77c1e3ccSAndroid Build Coastguard Worker }
204*77c1e3ccSAndroid Build Coastguard Worker 
show_histogram(const struct hist_bucket * bucket,int buckets,int total,int scale)205*77c1e3ccSAndroid Build Coastguard Worker static void show_histogram(const struct hist_bucket *bucket, int buckets,
206*77c1e3ccSAndroid Build Coastguard Worker                            int total, int scale) {
207*77c1e3ccSAndroid Build Coastguard Worker   int width1, width2;
208*77c1e3ccSAndroid Build Coastguard Worker   int i;
209*77c1e3ccSAndroid Build Coastguard Worker 
210*77c1e3ccSAndroid Build Coastguard Worker   if (!buckets) return;
211*77c1e3ccSAndroid Build Coastguard Worker   assert(bucket != NULL);
212*77c1e3ccSAndroid Build Coastguard Worker   assert(buckets > 0);
213*77c1e3ccSAndroid Build Coastguard Worker 
214*77c1e3ccSAndroid Build Coastguard Worker   switch ((int)(log(bucket[buckets - 1].high) / log(10)) + 1) {
215*77c1e3ccSAndroid Build Coastguard Worker     case 1:
216*77c1e3ccSAndroid Build Coastguard Worker     case 2:
217*77c1e3ccSAndroid Build Coastguard Worker       width1 = 4;
218*77c1e3ccSAndroid Build Coastguard Worker       width2 = 2;
219*77c1e3ccSAndroid Build Coastguard Worker       break;
220*77c1e3ccSAndroid Build Coastguard Worker     case 3:
221*77c1e3ccSAndroid Build Coastguard Worker       width1 = 5;
222*77c1e3ccSAndroid Build Coastguard Worker       width2 = 3;
223*77c1e3ccSAndroid Build Coastguard Worker       break;
224*77c1e3ccSAndroid Build Coastguard Worker     case 4:
225*77c1e3ccSAndroid Build Coastguard Worker       width1 = 6;
226*77c1e3ccSAndroid Build Coastguard Worker       width2 = 4;
227*77c1e3ccSAndroid Build Coastguard Worker       break;
228*77c1e3ccSAndroid Build Coastguard Worker     case 5:
229*77c1e3ccSAndroid Build Coastguard Worker       width1 = 7;
230*77c1e3ccSAndroid Build Coastguard Worker       width2 = 5;
231*77c1e3ccSAndroid Build Coastguard Worker       break;
232*77c1e3ccSAndroid Build Coastguard Worker     case 6:
233*77c1e3ccSAndroid Build Coastguard Worker       width1 = 8;
234*77c1e3ccSAndroid Build Coastguard Worker       width2 = 6;
235*77c1e3ccSAndroid Build Coastguard Worker       break;
236*77c1e3ccSAndroid Build Coastguard Worker     case 7:
237*77c1e3ccSAndroid Build Coastguard Worker       width1 = 9;
238*77c1e3ccSAndroid Build Coastguard Worker       width2 = 7;
239*77c1e3ccSAndroid Build Coastguard Worker       break;
240*77c1e3ccSAndroid Build Coastguard Worker     default:
241*77c1e3ccSAndroid Build Coastguard Worker       width1 = 12;
242*77c1e3ccSAndroid Build Coastguard Worker       width2 = 10;
243*77c1e3ccSAndroid Build Coastguard Worker       break;
244*77c1e3ccSAndroid Build Coastguard Worker   }
245*77c1e3ccSAndroid Build Coastguard Worker 
246*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < buckets; i++) {
247*77c1e3ccSAndroid Build Coastguard Worker     int len;
248*77c1e3ccSAndroid Build Coastguard Worker     int j;
249*77c1e3ccSAndroid Build Coastguard Worker     float pct;
250*77c1e3ccSAndroid Build Coastguard Worker 
251*77c1e3ccSAndroid Build Coastguard Worker     pct = (float)(100.0 * bucket[i].count / total);
252*77c1e3ccSAndroid Build Coastguard Worker     len = HIST_BAR_MAX * bucket[i].count / scale;
253*77c1e3ccSAndroid Build Coastguard Worker     if (len < 1) len = 1;
254*77c1e3ccSAndroid Build Coastguard Worker     assert(len <= HIST_BAR_MAX);
255*77c1e3ccSAndroid Build Coastguard Worker 
256*77c1e3ccSAndroid Build Coastguard Worker     if (bucket[i].low == bucket[i].high)
257*77c1e3ccSAndroid Build Coastguard Worker       fprintf(stderr, "%*d %*s: ", width1, bucket[i].low, width2, "");
258*77c1e3ccSAndroid Build Coastguard Worker     else
259*77c1e3ccSAndroid Build Coastguard Worker       fprintf(stderr, "%*d-%*d: ", width1, bucket[i].low, width2,
260*77c1e3ccSAndroid Build Coastguard Worker               bucket[i].high);
261*77c1e3ccSAndroid Build Coastguard Worker 
262*77c1e3ccSAndroid Build Coastguard Worker     for (j = 0; j < HIST_BAR_MAX; j++) fprintf(stderr, j < len ? "=" : " ");
263*77c1e3ccSAndroid Build Coastguard Worker     fprintf(stderr, "\t%5d (%6.2f%%)\n", bucket[i].count, pct);
264*77c1e3ccSAndroid Build Coastguard Worker   }
265*77c1e3ccSAndroid Build Coastguard Worker }
266*77c1e3ccSAndroid Build Coastguard Worker 
show_q_histogram(const int counts[64],int max_buckets)267*77c1e3ccSAndroid Build Coastguard Worker void show_q_histogram(const int counts[64], int max_buckets) {
268*77c1e3ccSAndroid Build Coastguard Worker   struct hist_bucket bucket[64];
269*77c1e3ccSAndroid Build Coastguard Worker   int buckets = 0;
270*77c1e3ccSAndroid Build Coastguard Worker   int total = 0;
271*77c1e3ccSAndroid Build Coastguard Worker   int scale;
272*77c1e3ccSAndroid Build Coastguard Worker   int i;
273*77c1e3ccSAndroid Build Coastguard Worker 
274*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < 64; i++) {
275*77c1e3ccSAndroid Build Coastguard Worker     if (counts[i]) {
276*77c1e3ccSAndroid Build Coastguard Worker       bucket[buckets].low = bucket[buckets].high = i;
277*77c1e3ccSAndroid Build Coastguard Worker       bucket[buckets].count = counts[i];
278*77c1e3ccSAndroid Build Coastguard Worker       buckets++;
279*77c1e3ccSAndroid Build Coastguard Worker       total += counts[i];
280*77c1e3ccSAndroid Build Coastguard Worker     }
281*77c1e3ccSAndroid Build Coastguard Worker   }
282*77c1e3ccSAndroid Build Coastguard Worker 
283*77c1e3ccSAndroid Build Coastguard Worker   fprintf(stderr, "\nQuantizer Selection:\n");
284*77c1e3ccSAndroid Build Coastguard Worker   scale = merge_hist_buckets(bucket, max_buckets, &buckets);
285*77c1e3ccSAndroid Build Coastguard Worker   show_histogram(bucket, buckets, total, scale);
286*77c1e3ccSAndroid Build Coastguard Worker }
287*77c1e3ccSAndroid Build Coastguard Worker 
show_rate_histogram(struct rate_hist * hist,const aom_codec_enc_cfg_t * cfg,int max_buckets)288*77c1e3ccSAndroid Build Coastguard Worker void show_rate_histogram(struct rate_hist *hist, const aom_codec_enc_cfg_t *cfg,
289*77c1e3ccSAndroid Build Coastguard Worker                          int max_buckets) {
290*77c1e3ccSAndroid Build Coastguard Worker   int i, scale;
291*77c1e3ccSAndroid Build Coastguard Worker   int buckets = 0;
292*77c1e3ccSAndroid Build Coastguard Worker 
293*77c1e3ccSAndroid Build Coastguard Worker   if (hist == NULL || cfg == NULL) return;
294*77c1e3ccSAndroid Build Coastguard Worker 
295*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < RATE_BINS; i++) {
296*77c1e3ccSAndroid Build Coastguard Worker     if (hist->bucket[i].low == INT_MAX) continue;
297*77c1e3ccSAndroid Build Coastguard Worker     hist->bucket[buckets++] = hist->bucket[i];
298*77c1e3ccSAndroid Build Coastguard Worker   }
299*77c1e3ccSAndroid Build Coastguard Worker 
300*77c1e3ccSAndroid Build Coastguard Worker   fprintf(stderr, "\nRate (over %dms window):\n", cfg->rc_buf_sz);
301*77c1e3ccSAndroid Build Coastguard Worker   scale = merge_hist_buckets(hist->bucket, max_buckets, &buckets);
302*77c1e3ccSAndroid Build Coastguard Worker   show_histogram(hist->bucket, buckets, hist->total, scale);
303*77c1e3ccSAndroid Build Coastguard Worker }
304