rate_hist.c 7.3 KB

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