]> granicus.if.org Git - libvpx/blob - rate_hist.c
Merge "Sample points to reduce encode overhead."
[libvpx] / rate_hist.c
1 /*
2  *  Copyright (c) 2014 The WebM project authors. All Rights Reserved.
3  *
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.
9  */
10
11 #include <assert.h>
12 #include <stdlib.h>
13 #include <limits.h>
14 #include <stdio.h>
15 #include <math.h>
16
17 #include "./rate_hist.h"
18
19 #define RATE_BINS 100
20 #define HIST_BAR_MAX 40
21
22 struct hist_bucket {
23   int low;
24   int high;
25   int count;
26 };
27
28 struct rate_hist {
29   int64_t *pts;
30   int *sz;
31   int samples;
32   int frames;
33   struct hist_bucket bucket[RATE_BINS];
34   int total;
35 };
36
37 struct rate_hist *init_rate_histogram(const vpx_codec_enc_cfg_t *cfg,
38                                       const vpx_rational_t *fps) {
39   int i;
40   struct rate_hist *hist = malloc(sizeof(*hist));
41
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;
46
47   // prevent division by zero
48   if (hist->samples == 0) hist->samples = 1;
49
50   hist->frames = 0;
51   hist->total = 0;
52
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;
59   }
60
61   return hist;
62 }
63
64 void destroy_rate_histogram(struct rate_hist *hist) {
65   if (hist) {
66     free(hist->pts);
67     free(hist->sz);
68     free(hist);
69   }
70 }
71
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) {
75   int i;
76   int64_t then = 0;
77   int64_t avg_bitrate = 0;
78   int64_t sum_sz = 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;
82
83   int idx = hist->frames++ % hist->samples;
84   hist->pts[idx] = now;
85   hist->sz[idx] = (int)pkt->data.frame.sz;
86
87   if (now < cfg->rc_buf_initial_sz) return;
88
89   if (!cfg->rc_target_bitrate) return;
90
91   then = now;
92
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;
96
97     then = hist->pts[i_idx];
98     if (now - then > cfg->rc_buf_sz) break;
99     sum_sz += hist->sz[i_idx];
100   }
101
102   if (now == then) return;
103
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++;
113   hist->total++;
114 }
115
116 static int merge_hist_buckets(struct hist_bucket *bucket, int max_buckets,
117                               int *num_buckets) {
118   int small_bucket = 0, merge_bucket = INT_MAX, big_bucket = 0;
119   int buckets = *num_buckets;
120   int i;
121
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;
127   }
128
129   /* If we have too many buckets, merge the smallest with an adjacent
130    * bucket.
131    */
132   while (buckets > max_buckets) {
133     int last_bucket = buckets - 1;
134
135     /* merge the small bucket with an adjacent one. */
136     if (small_bucket == 0)
137       merge_bucket = 1;
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;
142     else
143       merge_bucket = small_bucket + 1;
144
145     assert(abs(merge_bucket - small_bucket) <= 1);
146     assert(small_bucket < buckets);
147     assert(big_bucket < buckets);
148     assert(merge_bucket < buckets);
149
150     if (merge_bucket < small_bucket) {
151       bucket[merge_bucket].high = bucket[small_bucket].high;
152       bucket[merge_bucket].count += bucket[small_bucket].count;
153     } else {
154       bucket[small_bucket].high = bucket[merge_bucket].high;
155       bucket[small_bucket].count += bucket[merge_bucket].count;
156       merge_bucket = small_bucket;
157     }
158
159     assert(bucket[merge_bucket].low != bucket[merge_bucket].high);
160
161     buckets--;
162
163     /* Remove the merge_bucket from the list, and find the new small
164      * and big buckets while we're at it
165      */
166     big_bucket = small_bucket = 0;
167     for (i = 0; i < buckets; i++) {
168       if (i > merge_bucket) bucket[i] = bucket[i + 1];
169
170       if (bucket[i].count < bucket[small_bucket].count) small_bucket = i;
171       if (bucket[i].count > bucket[big_bucket].count) big_bucket = i;
172     }
173   }
174
175   *num_buckets = buckets;
176   return bucket[big_bucket].count;
177 }
178
179 static void show_histogram(const struct hist_bucket *bucket, int buckets,
180                            int total, int scale) {
181   const char *pat1, *pat2;
182   int i;
183
184   switch ((int)(log(bucket[buckets - 1].high) / log(10)) + 1) {
185     case 1:
186     case 2:
187       pat1 = "%4d %2s: ";
188       pat2 = "%4d-%2d: ";
189       break;
190     case 3:
191       pat1 = "%5d %3s: ";
192       pat2 = "%5d-%3d: ";
193       break;
194     case 4:
195       pat1 = "%6d %4s: ";
196       pat2 = "%6d-%4d: ";
197       break;
198     case 5:
199       pat1 = "%7d %5s: ";
200       pat2 = "%7d-%5d: ";
201       break;
202     case 6:
203       pat1 = "%8d %6s: ";
204       pat2 = "%8d-%6d: ";
205       break;
206     case 7:
207       pat1 = "%9d %7s: ";
208       pat2 = "%9d-%7d: ";
209       break;
210     default:
211       pat1 = "%12d %10s: ";
212       pat2 = "%12d-%10d: ";
213       break;
214   }
215
216   for (i = 0; i < buckets; i++) {
217     int len;
218     int j;
219     float pct;
220
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);
225
226     if (bucket[i].low == bucket[i].high)
227       fprintf(stderr, pat1, bucket[i].low, "");
228     else
229       fprintf(stderr, pat2, bucket[i].low, bucket[i].high);
230
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);
233   }
234 }
235
236 void show_q_histogram(const int counts[64], int max_buckets) {
237   struct hist_bucket bucket[64];
238   int buckets = 0;
239   int total = 0;
240   int scale;
241   int i;
242
243   for (i = 0; i < 64; i++) {
244     if (counts[i]) {
245       bucket[buckets].low = bucket[buckets].high = i;
246       bucket[buckets].count = counts[i];
247       buckets++;
248       total += counts[i];
249     }
250   }
251
252   fprintf(stderr, "\nQuantizer Selection:\n");
253   scale = merge_hist_buckets(bucket, max_buckets, &buckets);
254   show_histogram(bucket, buckets, total, scale);
255 }
256
257 void show_rate_histogram(struct rate_hist *hist, const vpx_codec_enc_cfg_t *cfg,
258                          int max_buckets) {
259   int i, scale;
260   int buckets = 0;
261
262   for (i = 0; i < RATE_BINS; i++) {
263     if (hist->bucket[i].low == INT_MAX) continue;
264     hist->bucket[buckets++] = hist->bucket[i];
265   }
266
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);
270 }