rate_hist.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. #include <assert.h>
  11. #include <stdlib.h>
  12. #include <limits.h>
  13. #include <stdio.h>
  14. #include <math.h>
  15. #include "./rate_hist.h"
  16. #define RATE_BINS 100
  17. #define HIST_BAR_MAX 40
  18. struct hist_bucket {
  19. int low;
  20. int high;
  21. int count;
  22. };
  23. struct rate_hist {
  24. int64_t *pts;
  25. int *sz;
  26. int samples;
  27. int frames;
  28. struct hist_bucket bucket[RATE_BINS];
  29. int total;
  30. };
  31. struct rate_hist *init_rate_histogram(const vpx_codec_enc_cfg_t *cfg,
  32. const vpx_rational_t *fps) {
  33. int i;
  34. struct rate_hist *hist = calloc(1, sizeof(*hist));
  35. if (hist == NULL || cfg == NULL || fps == NULL || fps->num == 0 ||
  36. fps->den == 0) {
  37. destroy_rate_histogram(hist);
  38. return NULL;
  39. }
  40. // Determine the number of samples in the buffer. Use the file's framerate
  41. // to determine the number of frames in rc_buf_sz milliseconds, with an
  42. // adjustment (5/4) to account for alt-refs
  43. hist->samples = cfg->rc_buf_sz * 5 / 4 * fps->num / fps->den / 1000;
  44. // prevent division by zero
  45. if (hist->samples == 0) hist->samples = 1;
  46. hist->frames = 0;
  47. hist->total = 0;
  48. hist->pts = calloc(hist->samples, sizeof(*hist->pts));
  49. hist->sz = calloc(hist->samples, sizeof(*hist->sz));
  50. for (i = 0; i < RATE_BINS; i++) {
  51. hist->bucket[i].low = INT_MAX;
  52. hist->bucket[i].high = 0;
  53. hist->bucket[i].count = 0;
  54. }
  55. return hist;
  56. }
  57. void destroy_rate_histogram(struct rate_hist *hist) {
  58. if (hist) {
  59. free(hist->pts);
  60. free(hist->sz);
  61. free(hist);
  62. }
  63. }
  64. void update_rate_histogram(struct rate_hist *hist,
  65. const vpx_codec_enc_cfg_t *cfg,
  66. const vpx_codec_cx_pkt_t *pkt) {
  67. int i;
  68. int64_t then = 0;
  69. int64_t avg_bitrate = 0;
  70. int64_t sum_sz = 0;
  71. const int64_t now = pkt->data.frame.pts * 1000 *
  72. (uint64_t)cfg->g_timebase.num /
  73. (uint64_t)cfg->g_timebase.den;
  74. int idx;
  75. if (hist == NULL || cfg == NULL || pkt == NULL) return;
  76. idx = hist->frames++ % hist->samples;
  77. hist->pts[idx] = now;
  78. hist->sz[idx] = (int)pkt->data.frame.sz;
  79. if (now < cfg->rc_buf_initial_sz) return;
  80. if (!cfg->rc_target_bitrate) return;
  81. then = now;
  82. /* Sum the size over the past rc_buf_sz ms */
  83. for (i = hist->frames; i > 0 && hist->frames - i < hist->samples; i--) {
  84. const int i_idx = (i - 1) % hist->samples;
  85. then = hist->pts[i_idx];
  86. if (now - then > cfg->rc_buf_sz) break;
  87. sum_sz += hist->sz[i_idx];
  88. }
  89. if (now == then) return;
  90. avg_bitrate = sum_sz * 8 * 1000 / (now - then);
  91. idx = (int)(avg_bitrate * (RATE_BINS / 2) / (cfg->rc_target_bitrate * 1000));
  92. if (idx < 0) idx = 0;
  93. if (idx > RATE_BINS - 1) idx = RATE_BINS - 1;
  94. if (hist->bucket[idx].low > avg_bitrate)
  95. hist->bucket[idx].low = (int)avg_bitrate;
  96. if (hist->bucket[idx].high < avg_bitrate)
  97. hist->bucket[idx].high = (int)avg_bitrate;
  98. hist->bucket[idx].count++;
  99. hist->total++;
  100. }
  101. static int merge_hist_buckets(struct hist_bucket *bucket, int max_buckets,
  102. int *num_buckets) {
  103. int small_bucket = 0, merge_bucket = INT_MAX, big_bucket = 0;
  104. int buckets;
  105. int i;
  106. assert(bucket != NULL);
  107. assert(num_buckets != NULL);
  108. buckets = *num_buckets;
  109. /* Find the extrema for this list of buckets */
  110. big_bucket = small_bucket = 0;
  111. for (i = 0; i < buckets; i++) {
  112. if (bucket[i].count < bucket[small_bucket].count) small_bucket = i;
  113. if (bucket[i].count > bucket[big_bucket].count) big_bucket = i;
  114. }
  115. /* If we have too many buckets, merge the smallest with an adjacent
  116. * bucket.
  117. */
  118. while (buckets > max_buckets) {
  119. int last_bucket = buckets - 1;
  120. /* merge the small bucket with an adjacent one. */
  121. if (small_bucket == 0)
  122. merge_bucket = 1;
  123. else if (small_bucket == last_bucket)
  124. merge_bucket = last_bucket - 1;
  125. else if (bucket[small_bucket - 1].count < bucket[small_bucket + 1].count)
  126. merge_bucket = small_bucket - 1;
  127. else
  128. merge_bucket = small_bucket + 1;
  129. assert(abs(merge_bucket - small_bucket) <= 1);
  130. assert(small_bucket < buckets);
  131. assert(big_bucket < buckets);
  132. assert(merge_bucket < buckets);
  133. if (merge_bucket < small_bucket) {
  134. bucket[merge_bucket].high = bucket[small_bucket].high;
  135. bucket[merge_bucket].count += bucket[small_bucket].count;
  136. } else {
  137. bucket[small_bucket].high = bucket[merge_bucket].high;
  138. bucket[small_bucket].count += bucket[merge_bucket].count;
  139. merge_bucket = small_bucket;
  140. }
  141. assert(bucket[merge_bucket].low != bucket[merge_bucket].high);
  142. buckets--;
  143. /* Remove the merge_bucket from the list, and find the new small
  144. * and big buckets while we're at it
  145. */
  146. big_bucket = small_bucket = 0;
  147. for (i = 0; i < buckets; i++) {
  148. if (i > merge_bucket) bucket[i] = bucket[i + 1];
  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. *num_buckets = buckets;
  154. return bucket[big_bucket].count;
  155. }
  156. static void show_histogram(const struct hist_bucket *bucket, int buckets,
  157. int total, int scale) {
  158. const char *pat1, *pat2;
  159. int i;
  160. assert(bucket != NULL);
  161. switch ((int)(log(bucket[buckets - 1].high) / log(10)) + 1) {
  162. case 1:
  163. case 2:
  164. pat1 = "%4d %2s: ";
  165. pat2 = "%4d-%2d: ";
  166. break;
  167. case 3:
  168. pat1 = "%5d %3s: ";
  169. pat2 = "%5d-%3d: ";
  170. break;
  171. case 4:
  172. pat1 = "%6d %4s: ";
  173. pat2 = "%6d-%4d: ";
  174. break;
  175. case 5:
  176. pat1 = "%7d %5s: ";
  177. pat2 = "%7d-%5d: ";
  178. break;
  179. case 6:
  180. pat1 = "%8d %6s: ";
  181. pat2 = "%8d-%6d: ";
  182. break;
  183. case 7:
  184. pat1 = "%9d %7s: ";
  185. pat2 = "%9d-%7d: ";
  186. break;
  187. default:
  188. pat1 = "%12d %10s: ";
  189. pat2 = "%12d-%10d: ";
  190. break;
  191. }
  192. for (i = 0; i < buckets; i++) {
  193. int len;
  194. int j;
  195. float pct;
  196. pct = (float)(100.0 * bucket[i].count / total);
  197. len = HIST_BAR_MAX * bucket[i].count / scale;
  198. if (len < 1) len = 1;
  199. assert(len <= HIST_BAR_MAX);
  200. if (bucket[i].low == bucket[i].high)
  201. fprintf(stderr, pat1, bucket[i].low, "");
  202. else
  203. fprintf(stderr, pat2, bucket[i].low, bucket[i].high);
  204. for (j = 0; j < HIST_BAR_MAX; j++) fprintf(stderr, j < len ? "=" : " ");
  205. fprintf(stderr, "\t%5d (%6.2f%%)\n", bucket[i].count, pct);
  206. }
  207. }
  208. void show_q_histogram(const int counts[64], int max_buckets) {
  209. struct hist_bucket bucket[64];
  210. int buckets = 0;
  211. int total = 0;
  212. int scale;
  213. int i;
  214. for (i = 0; i < 64; i++) {
  215. if (counts[i]) {
  216. bucket[buckets].low = bucket[buckets].high = i;
  217. bucket[buckets].count = counts[i];
  218. buckets++;
  219. total += counts[i];
  220. }
  221. }
  222. fprintf(stderr, "\nQuantizer Selection:\n");
  223. scale = merge_hist_buckets(bucket, max_buckets, &buckets);
  224. show_histogram(bucket, buckets, total, scale);
  225. }
  226. void show_rate_histogram(struct rate_hist *hist, const vpx_codec_enc_cfg_t *cfg,
  227. int max_buckets) {
  228. int i, scale;
  229. int buckets = 0;
  230. if (hist == NULL || cfg == NULL) return;
  231. for (i = 0; i < RATE_BINS; i++) {
  232. if (hist->bucket[i].low == INT_MAX) continue;
  233. hist->bucket[buckets++] = hist->bucket[i];
  234. }
  235. fprintf(stderr, "\nRate (over %dms window):\n", cfg->rc_buf_sz);
  236. scale = merge_hist_buckets(hist->bucket, max_buckets, &buckets);
  237. show_histogram(hist->bucket, buckets, hist->total, scale);
  238. }