2015-03-13 12:23:54 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* This file is part of Kvazaar HEVC encoder.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013-2015 Tampere University of Technology and others (see
|
|
|
|
* COPYING file).
|
|
|
|
*
|
|
|
|
* Kvazaar is free software: you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2.1 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* Kvazaar is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with Kvazaar. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "rate_control.h"
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
2016-04-01 14:14:23 +00:00
|
|
|
#include "encoder.h"
|
|
|
|
#include "kvazaar.h"
|
|
|
|
|
|
|
|
|
2015-03-13 12:23:54 +00:00
|
|
|
static const int SMOOTHING_WINDOW = 40;
|
|
|
|
|
2015-05-06 11:08:18 +00:00
|
|
|
/**
|
|
|
|
* \brief Update alpha and beta parameters.
|
|
|
|
* \param state the main encoder state
|
|
|
|
*
|
|
|
|
* Sets global->rc_alpha and global->rc_beta of the encoder state.
|
|
|
|
*/
|
|
|
|
static void update_rc_parameters(encoder_state_t * state)
|
|
|
|
{
|
|
|
|
const encoder_control_t * const encoder = state->encoder_control;
|
|
|
|
|
|
|
|
const double pixels_per_picture = encoder->in.width * encoder->in.height;
|
|
|
|
const double bpp = state->stats_bitstream_length * 8 / pixels_per_picture;
|
|
|
|
const double log_bpp = log(bpp);
|
|
|
|
|
|
|
|
const double alpha_old = state->global->rc_alpha;
|
|
|
|
const double beta_old = state->global->rc_beta;
|
|
|
|
// lambda computed from real bpp
|
|
|
|
const double lambda_comp = CLIP(0.1, 10000, alpha_old * pow(bpp, beta_old));
|
|
|
|
// lambda used in encoding
|
|
|
|
const double lambda_real = state->global->cur_lambda_cost;
|
|
|
|
const double lambda_log_ratio = log(lambda_real) - log(lambda_comp);
|
|
|
|
|
|
|
|
const double alpha = alpha_old + 0.1 * lambda_log_ratio * alpha_old;
|
|
|
|
state->global->rc_alpha = CLIP(0.05, 20, alpha);
|
|
|
|
|
|
|
|
const double beta = beta_old + 0.05 * lambda_log_ratio * CLIP(-5, 1, log_bpp);
|
|
|
|
state->global->rc_beta = CLIP(-3, -0.1, beta);
|
|
|
|
}
|
|
|
|
|
2015-03-13 12:23:54 +00:00
|
|
|
/**
|
2015-05-04 09:20:41 +00:00
|
|
|
* \brief Allocate bits for the current GOP.
|
2015-03-13 12:23:54 +00:00
|
|
|
* \param state the main encoder state
|
2015-05-04 09:20:41 +00:00
|
|
|
*
|
|
|
|
* If GOPs are not used, allocates bits for a single picture.
|
|
|
|
*
|
|
|
|
* Sets the cur_gop_target_bits of the encoder state.
|
2015-03-13 12:23:54 +00:00
|
|
|
*/
|
2015-05-04 09:20:41 +00:00
|
|
|
static void gop_allocate_bits(encoder_state_t * const state)
|
2015-03-13 12:23:54 +00:00
|
|
|
{
|
|
|
|
const encoder_control_t * const encoder = state->encoder_control;
|
|
|
|
|
|
|
|
// At this point, total_bits_coded of the current state contains the
|
|
|
|
// number of bits written encoder->owf frames before the current frame.
|
2016-02-18 14:48:21 +00:00
|
|
|
uint64_t bits_coded = state->global->total_bits_coded;
|
2015-05-04 09:20:41 +00:00
|
|
|
int pictures_coded = MAX(0, state->global->frame - encoder->owf);
|
2015-03-13 12:23:54 +00:00
|
|
|
|
2015-05-04 09:20:41 +00:00
|
|
|
int gop_offset = (state->global->gop_offset - encoder->owf) % MAX(1, encoder->cfg->gop_len);
|
|
|
|
// Only take fully coded GOPs into account.
|
|
|
|
if (encoder->cfg->gop_len > 0 && gop_offset != encoder->cfg->gop_len - 1) {
|
|
|
|
// Subtract number of bits in the partially coded GOP.
|
|
|
|
bits_coded -= state->global->cur_gop_bits_coded;
|
|
|
|
// Subtract number of pictures in the partially coded GOP.
|
|
|
|
pictures_coded -= gop_offset + 1;
|
|
|
|
}
|
2015-03-13 12:23:54 +00:00
|
|
|
|
2015-05-04 09:20:41 +00:00
|
|
|
double gop_target_bits =
|
2015-05-27 12:41:45 +00:00
|
|
|
(encoder->target_avg_bppic * (pictures_coded + SMOOTHING_WINDOW) - bits_coded)
|
2015-05-04 09:20:41 +00:00
|
|
|
* MAX(1, encoder->cfg->gop_len) / SMOOTHING_WINDOW;
|
|
|
|
state->global->cur_gop_target_bits = MAX(200, gop_target_bits);
|
|
|
|
}
|
|
|
|
|
2015-05-06 11:08:18 +00:00
|
|
|
/**
|
|
|
|
* Allocate bits for the current picture.
|
|
|
|
* \param state the main encoder state
|
|
|
|
* \return target number of bits
|
|
|
|
*/
|
2015-05-06 11:08:18 +00:00
|
|
|
static double pic_allocate_bits(const encoder_state_t * const state)
|
|
|
|
{
|
2015-05-06 11:08:18 +00:00
|
|
|
const encoder_control_t * const encoder = state->encoder_control;
|
|
|
|
|
|
|
|
if (encoder->cfg->gop_len <= 0) {
|
|
|
|
return state->global->cur_gop_target_bits;
|
|
|
|
}
|
|
|
|
|
2015-05-27 12:41:45 +00:00
|
|
|
const double pic_weight = encoder->gop_layer_weights[
|
|
|
|
encoder->cfg->gop[state->global->gop_offset].layer - 1];
|
2015-05-06 11:08:18 +00:00
|
|
|
double pic_target_bits = state->global->cur_gop_target_bits * pic_weight;
|
|
|
|
return MAX(100, pic_target_bits);
|
2015-05-06 11:08:18 +00:00
|
|
|
}
|
|
|
|
|
2015-05-04 09:20:41 +00:00
|
|
|
/**
|
|
|
|
* \brief Select a lambda value for encoding the next picture
|
|
|
|
* \param state the main encoder state
|
|
|
|
* \return lambda for the next picture
|
2015-05-29 11:00:51 +00:00
|
|
|
*
|
|
|
|
* Rate control must be enabled (i.e. cfg->target_bitrate > 0) when this
|
|
|
|
* function is called.
|
2015-05-04 09:20:41 +00:00
|
|
|
*/
|
2015-08-26 08:50:27 +00:00
|
|
|
double kvz_select_picture_lambda(encoder_state_t * const state)
|
2015-05-04 09:20:41 +00:00
|
|
|
{
|
|
|
|
const encoder_control_t * const encoder = state->encoder_control;
|
|
|
|
|
2015-05-29 11:00:51 +00:00
|
|
|
assert(encoder->cfg->target_bitrate > 0);
|
2015-05-04 09:20:41 +00:00
|
|
|
|
2015-08-12 15:12:04 +00:00
|
|
|
if (state->global->frame > encoder->owf) {
|
2015-05-29 11:00:51 +00:00
|
|
|
// At least one frame has been written.
|
2015-05-06 11:08:18 +00:00
|
|
|
update_rc_parameters(state);
|
|
|
|
}
|
|
|
|
|
2016-05-27 12:53:55 +00:00
|
|
|
if (encoder->cfg->gop_len == 0 ||
|
|
|
|
state->global->gop_offset == 0 ||
|
|
|
|
state->global->frame == 0)
|
|
|
|
{
|
2015-05-29 11:00:51 +00:00
|
|
|
// A new GOP begins at this frame.
|
2015-05-04 09:20:41 +00:00
|
|
|
gop_allocate_bits(state);
|
|
|
|
} else {
|
|
|
|
state->global->cur_gop_target_bits =
|
|
|
|
state->previous_encoder_state->global->cur_gop_target_bits;
|
|
|
|
}
|
|
|
|
|
2015-03-13 12:23:54 +00:00
|
|
|
// TODO: take the picture headers into account
|
2015-05-06 11:08:18 +00:00
|
|
|
const double target_bits_current_picture = pic_allocate_bits(state);
|
|
|
|
const double target_bits_per_pixel =
|
2015-05-27 12:41:45 +00:00
|
|
|
target_bits_current_picture / encoder->in.pixels_per_pic;
|
2015-05-06 11:08:18 +00:00
|
|
|
const double lambda =
|
|
|
|
state->global->rc_alpha * pow(target_bits_per_pixel, state->global->rc_beta);
|
2015-05-04 09:20:41 +00:00
|
|
|
return CLIP(0.1, 10000, lambda);
|
|
|
|
}
|
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
int8_t kvz_lambda_to_QP(const double lambda)
|
2015-05-04 09:20:41 +00:00
|
|
|
{
|
2015-05-29 11:00:51 +00:00
|
|
|
const int8_t qp = 4.2005 * log(lambda) + 13.7223 + 0.5;
|
2015-05-04 09:20:41 +00:00
|
|
|
return CLIP(0, 51, qp);
|
2015-03-13 12:23:54 +00:00
|
|
|
}
|
2015-05-29 11:00:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Select a lambda value according to current QP value
|
|
|
|
* \param state the main encoder state
|
|
|
|
* \return lambda for the next picture
|
|
|
|
*
|
|
|
|
* This function should be used to select lambda when rate control is
|
|
|
|
* disabled.
|
|
|
|
*/
|
2015-08-26 08:50:27 +00:00
|
|
|
double kvz_select_picture_lambda_from_qp(encoder_state_t const * const state)
|
2015-05-29 11:00:51 +00:00
|
|
|
{
|
|
|
|
const int gop_len = state->encoder_control->cfg->gop_len;
|
2015-11-02 12:10:10 +00:00
|
|
|
const int intra_period = state->encoder_control->cfg->intra_period;
|
|
|
|
const int keyframe_period = gop_len > 0 ? gop_len : intra_period;
|
|
|
|
|
|
|
|
double lambda = pow(2.0, (state->global->QP - 12) / 3.0);
|
2015-05-29 11:00:51 +00:00
|
|
|
|
2015-09-08 10:57:15 +00:00
|
|
|
if (state->global->slicetype == KVZ_SLICE_I) {
|
2015-11-02 12:10:10 +00:00
|
|
|
lambda *= 0.57;
|
|
|
|
|
|
|
|
// Reduce lambda for I-frames according to the number of references.
|
|
|
|
if (keyframe_period == 0) {
|
|
|
|
lambda *= 0.5;
|
|
|
|
} else {
|
|
|
|
lambda *= 1.0 - CLIP(0.0, 0.5, 0.05 * (keyframe_period - 1));
|
|
|
|
}
|
2015-05-29 11:00:51 +00:00
|
|
|
} else if (gop_len > 0) {
|
2015-11-02 12:10:10 +00:00
|
|
|
lambda *= state->global->QP_factor;
|
2015-05-29 11:00:51 +00:00
|
|
|
} else {
|
2015-11-02 12:10:10 +00:00
|
|
|
lambda *= 0.4624;
|
2015-05-29 11:00:51 +00:00
|
|
|
}
|
|
|
|
|
2015-11-02 12:10:10 +00:00
|
|
|
// Increase lambda if not key-frame.
|
|
|
|
if (keyframe_period > 0 && state->global->poc % keyframe_period != 0) {
|
|
|
|
lambda *= CLIP(2.0, 4.0, (state->global->QP - 12) / 6.0);
|
|
|
|
}
|
|
|
|
|
2015-05-29 11:00:51 +00:00
|
|
|
return lambda;
|
|
|
|
}
|