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