+#if CONFIG_RATE_CTRL
+static void init_rq_history(RATE_QINDEX_HISTORY *rq_history) {
+ rq_history->recode_count = 0;
+ rq_history->q_index_high = 255;
+ rq_history->q_index_low = 0;
+}
+
+static void update_rq_history(RATE_QINDEX_HISTORY *rq_history, int target_bits,
+ int actual_bits, int q_index) {
+ rq_history->q_index_history[rq_history->recode_count] = q_index;
+ rq_history->rate_history[rq_history->recode_count] = actual_bits;
+ if (actual_bits <= target_bits) {
+ rq_history->q_index_high = q_index;
+ }
+ if (actual_bits >= target_bits) {
+ rq_history->q_index_low = q_index;
+ }
+ rq_history->recode_count += 1;
+}
+
+static int guess_q_index_from_model(const RATE_QSTEP_MODEL *rq_model,
+ int target_bits) {
+ // The model predicts bits as follows.
+ // target_bits = bias - ratio * log2(q_step)
+ // Given the target_bits, we compute the q_step as follows.
+ double q_step;
+ assert(rq_model->ratio > 0);
+ q_step = pow(2.0, (rq_model->bias - target_bits) / rq_model->ratio);
+ // TODO(angiebird): Make this function support highbitdepth.
+ return vp9_convert_q_to_qindex(q_step, VPX_BITS_8);
+}
+
+static int guess_q_index_linear(int prev_q_index, int target_bits,
+ int actual_bits, int gap) {
+ int q_index = prev_q_index;
+ if (actual_bits < target_bits) {
+ q_index -= gap;
+ q_index = VPXMAX(q_index, 0);
+ } else {
+ q_index += gap;
+ q_index = VPXMIN(q_index, 255);
+ }
+ return q_index;
+}
+
+static double get_bits_percent_diff(int target_bits, int actual_bits) {
+ double diff;
+ target_bits = VPXMAX(target_bits, 1);
+ diff = abs(target_bits - actual_bits) * 1. / target_bits;
+ return diff * 100;
+}
+
+static int rq_model_predict_q_index(const RATE_QSTEP_MODEL *rq_model,
+ const RATE_QINDEX_HISTORY *rq_history,
+ int target_bits) {
+ int q_index = 128;
+ if (rq_history->recode_count > 0) {
+ const int actual_bits =
+ rq_history->rate_history[rq_history->recode_count - 1];
+ const int prev_q_index =
+ rq_history->q_index_history[rq_history->recode_count - 1];
+ const double percent_diff = get_bits_percent_diff(target_bits, actual_bits);
+ if (percent_diff > 50) {
+ // Binary search.
+ // When the actual_bits and target_bits are far apart, binary search
+ // q_index is faster.
+ q_index = (rq_history->q_index_low + rq_history->q_index_high) / 2;
+ } else {
+ if (rq_model->ready) {
+ q_index = guess_q_index_from_model(rq_model, target_bits);
+ } else {
+ // TODO(angiebird): Find a better way to set the gap.
+ q_index =
+ guess_q_index_linear(prev_q_index, target_bits, actual_bits, 20);
+ }
+ }
+ } else {
+ if (rq_model->ready) {
+ q_index = guess_q_index_from_model(rq_model, target_bits);
+ }
+ }
+
+ assert(rq_history->q_index_low <= rq_history->q_index_high);
+ if (q_index <= rq_history->q_index_low) {
+ q_index = rq_history->q_index_low + 1;
+ }
+ if (q_index >= rq_history->q_index_high) {
+ q_index = rq_history->q_index_high - 1;
+ }
+ return q_index;
+}
+
+static void rq_model_update(const RATE_QINDEX_HISTORY *rq_history,
+ int target_bits, RATE_QSTEP_MODEL *rq_model) {
+ const int recode_count = rq_history->recode_count;
+ const double delta = 0.00001;
+ if (recode_count >= 2) {
+ const int q_index1 = rq_history->q_index_history[recode_count - 2];
+ const int q_index2 = rq_history->q_index_history[recode_count - 1];
+ const int r1 = rq_history->rate_history[recode_count - 2];
+ const int r2 = rq_history->rate_history[recode_count - 1];
+ int valid = 0;
+ // lower q_index should yield higher bit rate
+ if (q_index1 < q_index2) {
+ valid = r1 > r2;
+ } else if (q_index1 > q_index2) {
+ valid = r1 < r2;
+ }
+ // Only update the model when the q_index and rate behave normally.
+ if (valid) {
+ // Fit the ratio and bias of rq_model based on last two recode histories.
+ const double s1 = vp9_convert_qindex_to_q(q_index1, VPX_BITS_8);
+ const double s2 = vp9_convert_qindex_to_q(q_index2, VPX_BITS_8);
+ if (fabs(log2(s1) - log2(s2)) > delta) {
+ rq_model->ratio = (r2 - r1) / (log2(s1) - log2(s2));
+ rq_model->bias = r1 + (rq_model->ratio) * log2(s1);
+ if (rq_model->ratio > delta && rq_model->bias > delta) {
+ rq_model->ready = 1;
+ }
+ }
+ }
+ } else if (recode_count == 1) {
+ if (rq_model->ready) {
+ // Update the ratio only when the initial model exists and we only have
+ // one recode history.
+ const int prev_q = rq_history->q_index_history[recode_count - 1];
+ const double prev_q_step = vp9_convert_qindex_to_q(prev_q, VPX_BITS_8);
+ if (fabs(log2(prev_q_step)) > delta) {
+ const int actual_bits = rq_history->rate_history[recode_count - 1];
+ rq_model->ratio =
+ rq_model->ratio + (target_bits - actual_bits) / log2(prev_q_step);
+ }
+ }
+ }
+}
+#endif // CONFIG_RATE_CTRL
+
+static void encode_with_recode_loop(VP9_COMP *cpi, size_t *size, uint8_t *dest
+#if CONFIG_RATE_CTRL
+ ,
+ RATE_QINDEX_HISTORY *rq_history
+#endif // CONFIG_RATE_CTRL
+) {