2014-01-24 10:37:15 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* This file is part of Kvazaar HEVC encoder.
|
2014-02-21 13:00:20 +00:00
|
|
|
*
|
2021-10-07 08:32:59 +00:00
|
|
|
* Copyright (c) 2021, Tampere University, ITU/ISO/IEC, project contributors
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
* are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer in the documentation and/or
|
|
|
|
* other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* * Neither the name of the Tampere University or ITU/ISO/IEC nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived from
|
|
|
|
* this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND ON
|
|
|
|
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
* INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
|
2014-01-24 10:37:15 +00:00
|
|
|
****************************************************************************/
|
|
|
|
|
2013-09-18 09:16:03 +00:00
|
|
|
#include "encoder.h"
|
|
|
|
|
2017-04-19 12:47:47 +00:00
|
|
|
// This define is required for M_PI on Windows.
|
|
|
|
#define _USE_MATH_DEFINES
|
|
|
|
#include <math.h>
|
2012-06-05 11:01:47 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2013-09-18 09:16:03 +00:00
|
|
|
|
2016-01-25 10:08:27 +00:00
|
|
|
#include "cfg.h"
|
2018-02-16 12:51:35 +00:00
|
|
|
#include "gop.h"
|
2020-11-05 17:47:47 +00:00
|
|
|
#include "rdo.h"
|
2016-09-28 20:05:21 +00:00
|
|
|
#include "strategyselector.h"
|
2018-02-15 07:58:42 +00:00
|
|
|
#include "kvz_math.h"
|
2020-09-25 16:18:56 +00:00
|
|
|
#include "fast_coeff_cost.h"
|
2012-06-05 11:01:47 +00:00
|
|
|
|
2017-04-19 12:47:47 +00:00
|
|
|
/**
|
|
|
|
* \brief Strength of QP adjustments when using adaptive QP for 360 video.
|
|
|
|
*
|
|
|
|
* Determined empirically.
|
|
|
|
*/
|
|
|
|
static const double ERP_AQP_STRENGTH = 3.0;
|
|
|
|
|
|
|
|
|
2015-05-27 12:41:45 +00:00
|
|
|
static int encoder_control_init_gop_layer_weights(encoder_control_t * const);
|
2014-05-08 10:00:23 +00:00
|
|
|
|
2017-07-17 09:03:02 +00:00
|
|
|
static unsigned cfg_num_threads(void)
|
2015-06-30 06:14:31 +00:00
|
|
|
{
|
2017-07-17 09:03:02 +00:00
|
|
|
if (kvz_g_hardware_flags.logical_cpu_count == 0) {
|
|
|
|
// Default to 4 if we don't know the number of CPUs.
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
return kvz_g_hardware_flags.logical_cpu_count;
|
2015-06-30 06:14:31 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 09:03:02 +00:00
|
|
|
|
|
|
|
static int get_max_parallelism(const encoder_control_t *const encoder)
|
2015-06-30 06:14:31 +00:00
|
|
|
{
|
2017-07-17 09:03:02 +00:00
|
|
|
const int width_lcu = CEILDIV(encoder->cfg.width, LCU_WIDTH);
|
|
|
|
const int height_lcu = CEILDIV(encoder->cfg.height, LCU_WIDTH);
|
|
|
|
const int wpp_limit = MIN(height_lcu, CEILDIV(width_lcu, 2));
|
|
|
|
const int par_frames = encoder->cfg.owf + 1;
|
2015-06-30 06:14:31 +00:00
|
|
|
|
2017-07-17 09:03:02 +00:00
|
|
|
int parallelism = 0;
|
2015-06-30 06:14:31 +00:00
|
|
|
|
2017-07-17 09:03:02 +00:00
|
|
|
if (encoder->cfg.intra_period == 1) {
|
|
|
|
int threads_per_frame;
|
|
|
|
if (encoder->cfg.wpp) {
|
|
|
|
// Usually limited by width because starting to code a CTU requires
|
|
|
|
// that the next two CTUs in the row above have been completed.
|
|
|
|
threads_per_frame = wpp_limit;
|
2016-09-27 20:14:57 +00:00
|
|
|
} else {
|
2017-07-17 09:03:02 +00:00
|
|
|
// One thread for each tile.
|
|
|
|
threads_per_frame = encoder->cfg.tiles_width_count *
|
|
|
|
encoder->cfg.tiles_height_count;
|
2016-09-27 20:14:57 +00:00
|
|
|
}
|
2017-07-17 09:03:02 +00:00
|
|
|
// Divide by two since all frames cannot achieve the maximum
|
|
|
|
// parallelism all the time.
|
|
|
|
parallelism = par_frames * threads_per_frame / 2;
|
|
|
|
|
2015-06-30 06:14:31 +00:00
|
|
|
} else {
|
2017-07-17 09:03:02 +00:00
|
|
|
if (encoder->cfg.wpp) {
|
|
|
|
const int last_diagonal = (width_lcu - 1) + (height_lcu - 1) * 2;
|
|
|
|
|
|
|
|
// Index of a diagonal. The diagonal contains CTUs whose coordinates
|
|
|
|
// satisfy x + 2*y == diagonal. We start the sum from the longest
|
|
|
|
// diagonal.
|
|
|
|
int diagonal = CEILDIV(last_diagonal, 2);
|
|
|
|
|
|
|
|
// Difference between diagonal indices in consecutive frames.
|
|
|
|
const int frame_delay = 1 + encoder->max_inter_ref_lcu.right +
|
|
|
|
2 * encoder->max_inter_ref_lcu.down;
|
|
|
|
int step = frame_delay;
|
|
|
|
int direction = -1;
|
|
|
|
|
|
|
|
// Compute number of threads for each parallel frame.
|
|
|
|
for (int num_frames = 0; num_frames < par_frames; num_frames++) {
|
|
|
|
if (diagonal < 0 || diagonal > last_diagonal) {
|
|
|
|
// No room for more threads.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count number of CTUs on the diagonal.
|
|
|
|
if (diagonal < MIN(2 * height_lcu, width_lcu)) {
|
|
|
|
parallelism += 1 + diagonal / 2;
|
|
|
|
} else {
|
|
|
|
parallelism += MIN(
|
|
|
|
wpp_limit,
|
|
|
|
height_lcu + CEILDIV(width_lcu, 2) - 1 - CEILDIV(diagonal, 2)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
diagonal += direction * step;
|
|
|
|
step += frame_delay;
|
|
|
|
direction = -direction;
|
|
|
|
}
|
2016-09-27 20:14:57 +00:00
|
|
|
|
2017-07-17 09:03:02 +00:00
|
|
|
} else {
|
|
|
|
parallelism = encoder->cfg.tiles_width_count *
|
|
|
|
encoder->cfg.tiles_height_count;
|
2016-09-27 20:14:57 +00:00
|
|
|
}
|
2015-06-30 06:14:31 +00:00
|
|
|
}
|
2016-09-28 20:05:21 +00:00
|
|
|
|
2017-07-17 09:03:02 +00:00
|
|
|
return parallelism;
|
2016-09-28 20:05:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-19 12:47:47 +00:00
|
|
|
/**
|
|
|
|
* \brief Return weight for 360 degree ERP video
|
|
|
|
*
|
|
|
|
* Returns the scaling factor of area from equirectangular projection to
|
|
|
|
* spherical surface.
|
|
|
|
*
|
|
|
|
* \param y y-coordinate of the pixel
|
|
|
|
* \param h height of the picture
|
|
|
|
*/
|
|
|
|
static double ws_weight(int y, int h)
|
|
|
|
{
|
|
|
|
return cos((y - 0.5 * h + 0.5) * (M_PI / h));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Update ROI QPs for 360 video with equirectangular projection.
|
|
|
|
*
|
|
|
|
* Writes updated ROI parameters to encoder->cfg.roi.
|
|
|
|
*
|
|
|
|
* \param encoder encoder control
|
|
|
|
* \param orig_roi original delta QPs or NULL
|
|
|
|
* \param orig_width width of orig_roi
|
|
|
|
* \param orig_height height of orig_roi
|
|
|
|
*/
|
|
|
|
static void init_erp_aqp_roi(encoder_control_t* encoder,
|
|
|
|
int8_t *orig_roi,
|
|
|
|
int32_t orig_width,
|
|
|
|
int32_t orig_height)
|
|
|
|
{
|
|
|
|
// Update ROI with WS-PSNR delta QPs.
|
|
|
|
int height = encoder->in.height_in_lcu;
|
|
|
|
int width = orig_roi ? orig_width : 1;
|
|
|
|
|
|
|
|
int frame_height = encoder->in.real_height;
|
|
|
|
|
|
|
|
encoder->cfg.roi.width = width;
|
|
|
|
encoder->cfg.roi.height = height;
|
|
|
|
encoder->cfg.roi.dqps = calloc(width * height, sizeof(orig_roi[0]));
|
|
|
|
|
|
|
|
double total_weight = 0.0;
|
|
|
|
for (int y = 0; y < frame_height; y++) {
|
|
|
|
total_weight += ws_weight(y, frame_height);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int y_lcu = 0; y_lcu < height; y_lcu++) {
|
|
|
|
int y_orig = LCU_WIDTH * y_lcu;
|
|
|
|
int lcu_height = MIN(LCU_WIDTH, frame_height - y_orig);
|
|
|
|
|
|
|
|
double lcu_weight = 0.0;
|
|
|
|
for (int y = y_orig; y < y_orig + lcu_height; y++) {
|
|
|
|
lcu_weight += ws_weight(y, frame_height);
|
|
|
|
}
|
|
|
|
// Normalize.
|
|
|
|
lcu_weight = (lcu_weight * frame_height) / (total_weight * lcu_height);
|
|
|
|
|
|
|
|
int8_t qp_delta = round(-ERP_AQP_STRENGTH * log2(lcu_weight));
|
|
|
|
|
|
|
|
if (orig_roi) {
|
|
|
|
// If a ROI array already exists, we copy the existing values to the
|
|
|
|
// new array while adding qp_delta to each.
|
|
|
|
int y_roi = y_lcu * orig_height / height;
|
|
|
|
for (int x = 0; x < width; x++) {
|
|
|
|
encoder->cfg.roi.dqps[x + y_lcu * width] =
|
|
|
|
CLIP(-51, 51, orig_roi[x + y_roi * width] + qp_delta);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Otherwise, simply write qp_delta to the ROI array.
|
|
|
|
encoder->cfg.roi.dqps[y_lcu] = qp_delta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-16 08:22:40 +00:00
|
|
|
static int8_t* derive_chroma_QP_mapping_table(const kvz_config* const cfg, int i)
|
|
|
|
{
|
|
|
|
const int MAX_QP = 63;
|
|
|
|
|
|
|
|
int8_t qpInVal[16], qpOutVal[16];
|
|
|
|
int8_t* table = calloc(MAX_QP + 1, sizeof(int8_t));
|
|
|
|
|
|
|
|
|
|
|
|
const int qpBdOffsetC = (cfg->input_bitdepth - 8) * 6;
|
|
|
|
const int numPtsInCQPTableMinus1 = cfg->qp_table_length_minus1[i];
|
|
|
|
|
|
|
|
qpInVal[0] = cfg->qp_table_start_minus26[i] + 26;
|
|
|
|
qpOutVal[0] = qpInVal[0];
|
|
|
|
for (int j = 0; j <= cfg->qp_table_length_minus1[i]; j++)
|
|
|
|
{
|
|
|
|
qpInVal[j + 1] = qpInVal[j] + cfg->delta_qp_in_val_minus1[i][j] + 1;
|
|
|
|
qpOutVal[j + 1] = qpOutVal[j] + cfg->delta_qp_out_val[i][j];
|
|
|
|
}
|
|
|
|
|
|
|
|
table[qpInVal[0]] = qpOutVal[0];
|
|
|
|
for (int k = qpInVal[0] - 1; k >= -qpBdOffsetC; k--)
|
|
|
|
{
|
|
|
|
table[k] = CLIP(-qpBdOffsetC, MAX_QP, table[k + 1] - 1);
|
|
|
|
}
|
|
|
|
for (int j = 0; j <= numPtsInCQPTableMinus1; j++)
|
|
|
|
{
|
|
|
|
int sh = (cfg->delta_qp_in_val_minus1[i][j] + 1) >> 1;
|
|
|
|
for (int k = qpInVal[j] + 1, m = 1; k <= qpInVal[j + 1]; k++, m++)
|
|
|
|
{
|
|
|
|
table[k] = table[qpInVal[j]] + ((qpOutVal[j + 1] - qpOutVal[j]) * m + sh) / (cfg->delta_qp_in_val_minus1[i][j] + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int k = qpInVal[numPtsInCQPTableMinus1 + 1] + 1; k <= MAX_QP; k++)
|
|
|
|
{
|
|
|
|
table[k] = CLIP(-qpBdOffsetC, MAX_QP, table[k - 1] + 1);
|
|
|
|
}
|
2021-05-24 19:23:45 +00:00
|
|
|
|
2021-03-16 08:22:40 +00:00
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-30 06:14:31 +00:00
|
|
|
/**
|
|
|
|
* \brief Allocate and initialize an encoder control structure.
|
|
|
|
*
|
|
|
|
* \param cfg encoder configuration
|
|
|
|
* \return initialized encoder control or NULL on failure
|
|
|
|
*/
|
2017-02-05 09:59:21 +00:00
|
|
|
encoder_control_t* kvz_encoder_control_init(const kvz_config *const cfg)
|
|
|
|
{
|
2015-06-30 06:14:31 +00:00
|
|
|
encoder_control_t *encoder = NULL;
|
|
|
|
|
2014-01-31 18:34:50 +00:00
|
|
|
if (!cfg) {
|
|
|
|
fprintf(stderr, "Config object must not be null!\n");
|
2015-06-30 06:14:31 +00:00
|
|
|
goto init_failed;
|
2014-01-31 18:34:50 +00:00
|
|
|
}
|
2015-06-30 06:14:31 +00:00
|
|
|
|
|
|
|
// Make sure that the parameters make sense.
|
2015-08-26 08:50:27 +00:00
|
|
|
if (!kvz_config_validate(cfg)) {
|
2015-06-30 06:14:31 +00:00
|
|
|
goto init_failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
encoder = calloc(1, sizeof(encoder_control_t));
|
|
|
|
if (!encoder) {
|
|
|
|
fprintf(stderr, "Failed to allocate encoder control.\n");
|
|
|
|
goto init_failed;
|
|
|
|
}
|
|
|
|
|
2017-02-05 09:59:21 +00:00
|
|
|
// Take a copy of the config.
|
|
|
|
memcpy(&encoder->cfg, cfg, sizeof(encoder->cfg));
|
|
|
|
// Set fields that are not copied to NULL.
|
|
|
|
encoder->cfg.cqmfile = NULL;
|
|
|
|
encoder->cfg.tiles_width_split = NULL;
|
|
|
|
encoder->cfg.tiles_height_split = NULL;
|
|
|
|
encoder->cfg.slice_addresses_in_ts = NULL;
|
2020-09-25 16:18:56 +00:00
|
|
|
encoder->cfg.fast_coeff_table_fn = NULL;
|
2017-02-05 09:59:21 +00:00
|
|
|
|
|
|
|
if (encoder->cfg.gop_len > 0) {
|
|
|
|
if (encoder->cfg.gop_lowdelay) {
|
2018-02-16 12:51:35 +00:00
|
|
|
if (encoder->cfg.gop_len == 4 && encoder->cfg.ref_frames == 4) {
|
|
|
|
memcpy(encoder->cfg.gop, kvz_gop_lowdelay4, sizeof(kvz_gop_lowdelay4));
|
|
|
|
} else {
|
|
|
|
kvz_config_process_lp_gop(&encoder->cfg);
|
|
|
|
}
|
2017-02-05 09:59:21 +00:00
|
|
|
}
|
2020-02-14 18:15:48 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 08:56:44 +00:00
|
|
|
if( encoder->cfg.intra_qp_offset_auto ) {
|
2021-03-04 16:08:59 +00:00
|
|
|
// Limit offset to -3 since HM/VTM seems to use it even for 32 frame gop
|
|
|
|
encoder->cfg.intra_qp_offset = encoder->cfg.gop_len > 1 ? MAX(-(int8_t)kvz_math_ceil_log2( encoder->cfg.gop_len ) + 1, -3) : 0;
|
2020-03-31 08:56:44 +00:00
|
|
|
}
|
|
|
|
|
2020-02-14 18:15:48 +00:00
|
|
|
// Disable GOP and QP offset for all-intra coding
|
|
|
|
if (encoder->cfg.intra_period == 1) {
|
|
|
|
encoder->cfg.gop_len = 0;
|
|
|
|
encoder->cfg.intra_qp_offset = 0;
|
2017-02-05 09:59:21 +00:00
|
|
|
}
|
|
|
|
|
2018-02-15 07:58:42 +00:00
|
|
|
encoder->poc_lsb_bits = MAX(4, kvz_math_ceil_log2(encoder->cfg.gop_len * 2 + 1));
|
|
|
|
|
2017-07-17 09:03:02 +00:00
|
|
|
encoder->max_inter_ref_lcu.right = 1;
|
|
|
|
encoder->max_inter_ref_lcu.down = 1;
|
|
|
|
|
|
|
|
int max_threads = encoder->cfg.threads;
|
|
|
|
if (max_threads < 0) {
|
|
|
|
max_threads = cfg_num_threads();
|
|
|
|
}
|
|
|
|
max_threads = MAX(1, max_threads);
|
|
|
|
|
2015-06-30 06:14:31 +00:00
|
|
|
// Need to set owf before initializing threadqueue.
|
2017-02-06 11:00:25 +00:00
|
|
|
if (encoder->cfg.owf < 0) {
|
2017-07-17 09:03:02 +00:00
|
|
|
int best_parallelism = 0;
|
|
|
|
|
|
|
|
for (encoder->cfg.owf = 0; true; encoder->cfg.owf++) {
|
|
|
|
int parallelism = get_max_parallelism(encoder);
|
|
|
|
|
|
|
|
if (parallelism <= best_parallelism) {
|
|
|
|
// No improvement over previous OWF.
|
|
|
|
encoder->cfg.owf--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
best_parallelism = parallelism;
|
|
|
|
if (parallelism >= max_threads) {
|
|
|
|
// Cannot have more parallelism than there are threads.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add two frames so that we have frames ready to be coded when one is
|
|
|
|
// completed.
|
|
|
|
encoder->cfg.owf += 2;
|
|
|
|
|
2017-02-06 11:00:25 +00:00
|
|
|
fprintf(stderr, "--owf=auto value set to %d.\n", encoder->cfg.owf);
|
2015-06-30 06:14:31 +00:00
|
|
|
}
|
2017-07-17 09:03:02 +00:00
|
|
|
|
|
|
|
if (encoder->cfg.threads < 0) {
|
|
|
|
encoder->cfg.threads = MIN(max_threads, get_max_parallelism(encoder));
|
|
|
|
fprintf(stderr, "--threads=auto value set to %d.\n", encoder->cfg.threads);
|
|
|
|
}
|
|
|
|
|
2017-02-05 09:59:21 +00:00
|
|
|
if (encoder->cfg.source_scan_type != KVZ_INTERLACING_NONE) {
|
2016-01-25 17:05:10 +00:00
|
|
|
// If using interlaced coding with OWF, the OWF has to be an even number
|
|
|
|
// to ensure that the pair of fields will be output for the same picture.
|
2017-02-06 11:00:25 +00:00
|
|
|
if (encoder->cfg.owf % 2 == 1) {
|
|
|
|
encoder->cfg.owf += 1;
|
2016-01-25 17:05:10 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-30 06:14:31 +00:00
|
|
|
|
2017-07-20 12:23:18 +00:00
|
|
|
encoder->threadqueue = kvz_threadqueue_init(encoder->cfg.threads);
|
|
|
|
if (!encoder->threadqueue) {
|
2015-06-30 06:14:31 +00:00
|
|
|
fprintf(stderr, "Could not initialize threadqueue.\n");
|
|
|
|
goto init_failed;
|
2014-05-13 09:28:15 +00:00
|
|
|
}
|
2015-06-30 06:14:31 +00:00
|
|
|
|
2015-08-11 05:42:09 +00:00
|
|
|
encoder->bitdepth = KVZ_BIT_DEPTH;
|
2015-06-30 06:14:31 +00:00
|
|
|
|
2017-02-05 09:59:21 +00:00
|
|
|
encoder->chroma_format = KVZ_FORMAT2CSP(encoder->cfg.input_format);
|
2016-08-16 16:03:21 +00:00
|
|
|
|
2016-01-22 10:44:04 +00:00
|
|
|
// Interlacing
|
2017-02-05 09:59:21 +00:00
|
|
|
encoder->in.source_scan_type = (int8_t)encoder->cfg.source_scan_type;
|
|
|
|
encoder->vui.field_seq_flag = encoder->cfg.source_scan_type != 0;
|
|
|
|
encoder->vui.frame_field_info_present_flag = encoder->cfg.source_scan_type != 0;
|
2015-06-30 06:14:31 +00:00
|
|
|
|
2014-01-31 18:34:50 +00:00
|
|
|
// Initialize the scaling list
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_scalinglist_init(&encoder->scaling_list);
|
2015-06-30 06:14:31 +00:00
|
|
|
|
2014-04-15 12:20:26 +00:00
|
|
|
// CQM
|
2018-11-15 07:33:02 +00:00
|
|
|
if (cfg->scaling_list == KVZ_SCALING_LIST_CUSTOM && cfg->cqmfile) {
|
2016-10-31 02:33:14 +00:00
|
|
|
FILE* cqmfile = fopen(cfg->cqmfile, "rb");
|
2014-04-15 12:20:26 +00:00
|
|
|
if (cqmfile) {
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_scalinglist_parse(&encoder->scaling_list, cqmfile);
|
2014-04-15 12:20:26 +00:00
|
|
|
fclose(cqmfile);
|
2016-10-31 02:33:14 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Could not open CQM file.\n");
|
|
|
|
goto init_failed;
|
2014-04-15 12:20:26 +00:00
|
|
|
}
|
2018-11-15 07:33:02 +00:00
|
|
|
} else if (cfg->scaling_list == KVZ_SCALING_LIST_DEFAULT) {
|
|
|
|
// Enable scaling lists if default lists are used
|
|
|
|
encoder->scaling_list.enable = 1;
|
|
|
|
encoder->scaling_list.use_default_list = 1;
|
2014-04-15 12:20:26 +00:00
|
|
|
}
|
2018-11-15 07:33:02 +00:00
|
|
|
|
2020-09-25 16:18:56 +00:00
|
|
|
if (cfg->fast_coeff_table_fn) {
|
|
|
|
FILE *fast_coeff_table_f = fopen(cfg->fast_coeff_table_fn, "rb");
|
|
|
|
if (fast_coeff_table_f == NULL) {
|
|
|
|
fprintf(stderr, "Could not open fast coeff table file.\n");
|
|
|
|
goto init_failed;
|
|
|
|
}
|
|
|
|
if (kvz_fast_coeff_table_parse(&encoder->fast_coeff_table, fast_coeff_table_f) != 0) {
|
|
|
|
fprintf(stderr, "Failed to parse fast coeff table, using default\n");
|
|
|
|
kvz_fast_coeff_use_default_table(&encoder->fast_coeff_table);
|
|
|
|
}
|
|
|
|
fclose(fast_coeff_table_f);
|
|
|
|
} else {
|
|
|
|
kvz_fast_coeff_use_default_table(&encoder->fast_coeff_table);
|
|
|
|
}
|
|
|
|
|
2020-11-03 11:23:19 +00:00
|
|
|
if (cfg->fastrd_sampling_on || cfg->fastrd_accuracy_check_on) {
|
2020-11-05 17:47:47 +00:00
|
|
|
if (cfg->fastrd_learning_outdir_fn == NULL) {
|
2020-11-03 11:23:19 +00:00
|
|
|
fprintf(stderr, "No output file defined for Fast RD sampling or accuracy check.\n");
|
|
|
|
goto init_failed;
|
|
|
|
}
|
2020-11-05 17:47:47 +00:00
|
|
|
if (kvz_init_rdcost_outfiles(cfg->fastrd_learning_outdir_fn) != 0) {
|
2020-11-03 11:23:19 +00:00
|
|
|
goto init_failed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_scalinglist_process(&encoder->scaling_list, encoder->bitdepth);
|
2016-10-31 02:33:14 +00:00
|
|
|
|
2017-02-05 09:59:21 +00:00
|
|
|
kvz_encoder_control_input_init(encoder, encoder->cfg.width, encoder->cfg.height);
|
2015-05-27 12:41:45 +00:00
|
|
|
|
2017-02-05 09:59:21 +00:00
|
|
|
if (encoder->cfg.framerate_num != 0) {
|
|
|
|
double framerate = encoder->cfg.framerate_num / (double)encoder->cfg.framerate_denom;
|
2017-02-06 11:00:25 +00:00
|
|
|
encoder->target_avg_bppic = encoder->cfg.target_bitrate / framerate;
|
2016-01-14 20:08:35 +00:00
|
|
|
} else {
|
2017-02-05 09:59:21 +00:00
|
|
|
encoder->target_avg_bppic = encoder->cfg.target_bitrate / encoder->cfg.framerate;
|
2016-01-14 20:08:35 +00:00
|
|
|
}
|
2015-05-27 12:41:45 +00:00
|
|
|
encoder->target_avg_bpp = encoder->target_avg_bppic / encoder->in.pixels_per_pic;
|
|
|
|
|
2018-02-15 07:19:43 +00:00
|
|
|
if (encoder->cfg.target_bitrate > 0 &&
|
|
|
|
!encoder_control_init_gop_layer_weights(encoder))
|
|
|
|
{
|
2015-06-30 06:14:31 +00:00
|
|
|
goto init_failed;
|
2015-05-27 12:41:45 +00:00
|
|
|
}
|
2016-01-14 09:48:35 +00:00
|
|
|
|
2017-04-19 12:47:47 +00:00
|
|
|
if (cfg->erp_aqp) {
|
|
|
|
init_erp_aqp_roi(encoder,
|
|
|
|
cfg->roi.dqps,
|
|
|
|
cfg->roi.width,
|
|
|
|
cfg->roi.height);
|
|
|
|
|
|
|
|
} else if (cfg->roi.dqps) {
|
|
|
|
// Copy delta QP array for ROI coding.
|
2017-02-05 09:59:21 +00:00
|
|
|
const size_t roi_size = encoder->cfg.roi.width * encoder->cfg.roi.height;
|
|
|
|
encoder->cfg.roi.dqps = calloc(roi_size, sizeof(cfg->roi.dqps[0]));
|
|
|
|
memcpy(encoder->cfg.roi.dqps,
|
|
|
|
cfg->roi.dqps,
|
|
|
|
roi_size * sizeof(*cfg->roi.dqps));
|
2017-04-19 12:47:47 +00:00
|
|
|
|
2017-02-05 09:59:21 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 09:00:28 +00:00
|
|
|
// NOTE: When tr_depth_inter is equal to 0, the transform is still split
|
|
|
|
// for SMP and AMP partition units.
|
|
|
|
encoder->tr_depth_inter = 0;
|
2017-06-07 09:52:16 +00:00
|
|
|
|
2020-02-07 14:04:23 +00:00
|
|
|
if (encoder->cfg.target_bitrate > 0 || encoder->cfg.roi.dqps || encoder->cfg.set_qp_in_cu || encoder->cfg.vaq) {
|
2017-05-08 07:54:06 +00:00
|
|
|
encoder->max_qp_delta_depth = 0;
|
|
|
|
} else {
|
|
|
|
encoder->max_qp_delta_depth = -1;
|
|
|
|
}
|
|
|
|
|
2014-04-02 09:31:12 +00:00
|
|
|
//Tiles
|
2017-02-05 09:59:21 +00:00
|
|
|
encoder->tiles_enable = encoder->cfg.tiles_width_count > 1 ||
|
|
|
|
encoder->cfg.tiles_height_count > 1;
|
2015-06-30 06:14:31 +00:00
|
|
|
|
2014-05-01 12:10:22 +00:00
|
|
|
{
|
2014-04-02 09:31:12 +00:00
|
|
|
const int num_ctbs = encoder->in.width_in_lcu * encoder->in.height_in_lcu;
|
|
|
|
|
|
|
|
//Temporary pointers to allow encoder fields to be const
|
|
|
|
int32_t *tiles_col_width, *tiles_row_height, *tiles_ctb_addr_rs_to_ts, *tiles_ctb_addr_ts_to_rs, *tiles_tile_id, *tiles_col_bd, *tiles_row_bd;
|
2015-06-30 06:14:31 +00:00
|
|
|
|
2017-02-05 09:59:21 +00:00
|
|
|
if (encoder->cfg.tiles_width_count > encoder->in.width_in_lcu) {
|
2014-05-01 05:11:31 +00:00
|
|
|
fprintf(stderr, "Too many tiles (width)!\n");
|
2015-06-30 06:14:31 +00:00
|
|
|
goto init_failed;
|
|
|
|
|
2017-02-05 09:59:21 +00:00
|
|
|
} else if (encoder->cfg.tiles_height_count > encoder->in.height_in_lcu) {
|
2014-05-01 05:11:31 +00:00
|
|
|
fprintf(stderr, "Too many tiles (height)!\n");
|
2015-06-30 06:14:31 +00:00
|
|
|
goto init_failed;
|
2014-05-01 05:11:31 +00:00
|
|
|
}
|
2015-06-30 06:14:31 +00:00
|
|
|
|
2014-04-02 09:31:12 +00:00
|
|
|
//Will be (perhaps) changed later
|
|
|
|
encoder->tiles_uniform_spacing_flag = 1;
|
2015-06-30 06:14:31 +00:00
|
|
|
|
|
|
|
encoder->tiles_col_width = tiles_col_width =
|
2017-02-06 11:00:25 +00:00
|
|
|
MALLOC(int32_t, encoder->cfg.tiles_width_count);
|
2015-06-30 06:14:31 +00:00
|
|
|
encoder->tiles_row_height = tiles_row_height =
|
2017-02-06 11:00:25 +00:00
|
|
|
MALLOC(int32_t, encoder->cfg.tiles_height_count);
|
2015-06-30 06:14:31 +00:00
|
|
|
|
|
|
|
encoder->tiles_col_bd = tiles_col_bd =
|
2017-02-06 11:00:25 +00:00
|
|
|
MALLOC(int32_t, encoder->cfg.tiles_width_count + 1);
|
2015-07-06 07:48:19 +00:00
|
|
|
encoder->tiles_row_bd = tiles_row_bd =
|
2017-02-06 11:00:25 +00:00
|
|
|
MALLOC(int32_t, encoder->cfg.tiles_height_count + 1);
|
2015-06-30 06:14:31 +00:00
|
|
|
|
|
|
|
encoder->tiles_ctb_addr_rs_to_ts = tiles_ctb_addr_rs_to_ts =
|
|
|
|
MALLOC(int32_t, num_ctbs);
|
|
|
|
encoder->tiles_ctb_addr_ts_to_rs = tiles_ctb_addr_ts_to_rs =
|
|
|
|
MALLOC(int32_t, num_ctbs);
|
|
|
|
encoder->tiles_tile_id = tiles_tile_id =
|
|
|
|
MALLOC(int32_t, num_ctbs);
|
|
|
|
|
|
|
|
if (!tiles_col_width ||
|
|
|
|
!tiles_row_height ||
|
|
|
|
!tiles_row_bd ||
|
|
|
|
!tiles_col_bd ||
|
|
|
|
!tiles_ctb_addr_rs_to_ts ||
|
|
|
|
!tiles_ctb_addr_ts_to_rs ||
|
|
|
|
!tiles_tile_id) {
|
|
|
|
goto init_failed;
|
|
|
|
}
|
|
|
|
|
2014-04-02 09:31:12 +00:00
|
|
|
//(6-3) and (6-4) in ITU-T Rec. H.265 (04/2013)
|
2017-02-05 09:59:21 +00:00
|
|
|
if (!cfg->tiles_width_split) {
|
2017-02-06 11:00:25 +00:00
|
|
|
for (int i = 0; i < encoder->cfg.tiles_width_count; ++i) {
|
|
|
|
tiles_col_width[i] =
|
|
|
|
(i+1) * encoder->in.width_in_lcu / encoder->cfg.tiles_width_count -
|
|
|
|
i * encoder->in.width_in_lcu / encoder->cfg.tiles_width_count;
|
2014-04-02 09:31:12 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int32_t last_pos_in_px = 0;
|
2017-02-06 11:00:25 +00:00
|
|
|
tiles_col_width[encoder->cfg.tiles_width_count - 1] = encoder->in.width_in_lcu;
|
|
|
|
for (int i = 0; i < encoder->cfg.tiles_width_count - 1; ++i) {
|
2014-04-02 09:31:12 +00:00
|
|
|
int32_t column_width_in_lcu = (cfg->tiles_width_split[i] - last_pos_in_px) / LCU_WIDTH;
|
|
|
|
last_pos_in_px = cfg->tiles_width_split[i];
|
|
|
|
tiles_col_width[i] = column_width_in_lcu;
|
2017-02-06 11:00:25 +00:00
|
|
|
tiles_col_width[encoder->cfg.tiles_width_count - 1] -= column_width_in_lcu;
|
2014-04-02 09:31:12 +00:00
|
|
|
}
|
|
|
|
encoder->tiles_uniform_spacing_flag = 0;
|
|
|
|
}
|
2015-06-30 06:14:31 +00:00
|
|
|
|
2017-02-05 09:59:21 +00:00
|
|
|
if (!cfg->tiles_height_split) {
|
2017-02-06 11:00:25 +00:00
|
|
|
for (int i = 0; i < encoder->cfg.tiles_height_count; ++i) {
|
|
|
|
tiles_row_height[i] = ((i+1) * encoder->in.height_in_lcu) / encoder->cfg.tiles_height_count -
|
|
|
|
i * encoder->in.height_in_lcu / encoder->cfg.tiles_height_count;
|
2014-04-02 09:31:12 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int32_t last_pos_in_px = 0;
|
2017-02-06 11:00:25 +00:00
|
|
|
tiles_row_height[encoder->cfg.tiles_height_count - 1] = encoder->in.height_in_lcu;
|
|
|
|
for (int i = 0; i < encoder->cfg.tiles_height_count - 1; ++i) {
|
2014-04-02 09:31:12 +00:00
|
|
|
int32_t row_height_in_lcu = (cfg->tiles_height_split[i] - last_pos_in_px) / LCU_WIDTH;
|
|
|
|
last_pos_in_px = cfg->tiles_height_split[i];
|
|
|
|
tiles_row_height[i] = row_height_in_lcu;
|
2017-02-06 11:00:25 +00:00
|
|
|
tiles_row_height[encoder->cfg.tiles_height_count - 1] -= row_height_in_lcu;
|
2014-04-02 09:31:12 +00:00
|
|
|
}
|
|
|
|
encoder->tiles_uniform_spacing_flag = 0;
|
|
|
|
}
|
2015-06-30 06:14:31 +00:00
|
|
|
|
2014-04-02 09:31:12 +00:00
|
|
|
//(6-5) in ITU-T Rec. H.265 (04/2013)
|
|
|
|
tiles_col_bd[0] = 0;
|
2017-02-06 11:00:25 +00:00
|
|
|
for (int i = 0; i < encoder->cfg.tiles_width_count; ++i) {
|
2014-04-02 09:31:12 +00:00
|
|
|
tiles_col_bd[i+1] = tiles_col_bd[i] + tiles_col_width[i];
|
|
|
|
}
|
2015-06-30 06:14:31 +00:00
|
|
|
|
2014-04-02 09:31:12 +00:00
|
|
|
//(6-6) in ITU-T Rec. H.265 (04/2013)
|
|
|
|
tiles_row_bd[0] = 0;
|
2017-02-06 11:00:25 +00:00
|
|
|
for (int i = 0; i < encoder->cfg.tiles_height_count; ++i) {
|
2014-04-02 09:31:12 +00:00
|
|
|
tiles_row_bd[i+1] = tiles_row_bd[i] + tiles_row_height[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
//(6-7) in ITU-T Rec. H.265 (04/2013)
|
|
|
|
//j == ctbAddrRs
|
2017-02-06 11:00:25 +00:00
|
|
|
for (int j = 0; j < num_ctbs; ++j) {
|
2014-04-24 06:41:05 +00:00
|
|
|
int tileX = 0, tileY = 0;
|
2014-04-02 09:31:12 +00:00
|
|
|
int tbX = j % encoder->in.width_in_lcu;
|
|
|
|
int tbY = j / encoder->in.width_in_lcu;
|
2015-06-30 06:14:31 +00:00
|
|
|
|
2017-02-06 11:00:25 +00:00
|
|
|
for (int i = 0; i < encoder->cfg.tiles_width_count; ++i) {
|
2014-04-02 09:31:12 +00:00
|
|
|
if (tbX >= tiles_col_bd[i]) tileX = i;
|
|
|
|
}
|
2015-06-30 06:14:31 +00:00
|
|
|
|
2017-02-06 11:00:25 +00:00
|
|
|
for (int i = 0; i < encoder->cfg.tiles_height_count; ++i) {
|
2014-04-02 09:31:12 +00:00
|
|
|
if (tbY >= tiles_row_bd[i]) tileY = i;
|
|
|
|
}
|
2015-06-30 06:14:31 +00:00
|
|
|
|
2014-04-02 09:31:12 +00:00
|
|
|
tiles_ctb_addr_rs_to_ts[j] = 0;
|
2017-02-06 11:00:25 +00:00
|
|
|
for (int i = 0; i < tileX; ++i) {
|
2014-04-02 09:31:12 +00:00
|
|
|
tiles_ctb_addr_rs_to_ts[j] += tiles_row_height[tileY] * tiles_col_width[i];
|
|
|
|
}
|
2017-02-06 11:00:25 +00:00
|
|
|
for (int i = 0; i < tileY; ++i) {
|
2014-04-02 09:31:12 +00:00
|
|
|
tiles_ctb_addr_rs_to_ts[j] += encoder->in.width_in_lcu * tiles_row_height[i];
|
|
|
|
}
|
2015-06-30 06:14:31 +00:00
|
|
|
tiles_ctb_addr_rs_to_ts[j] += (tbY - tiles_row_bd[tileY]) * tiles_col_width[tileX] +
|
2014-04-02 09:31:12 +00:00
|
|
|
tbX - tiles_col_bd[tileX];
|
|
|
|
}
|
2015-06-30 06:14:31 +00:00
|
|
|
|
2014-04-02 09:31:12 +00:00
|
|
|
//(6-8) in ITU-T Rec. H.265 (04/2013)
|
|
|
|
//Make reverse map from tile scan to raster scan
|
2017-02-06 11:00:25 +00:00
|
|
|
for (int j = 0; j < num_ctbs; ++j) {
|
2014-04-02 09:31:12 +00:00
|
|
|
tiles_ctb_addr_ts_to_rs[tiles_ctb_addr_rs_to_ts[j]] = j;
|
|
|
|
}
|
2015-06-30 06:14:31 +00:00
|
|
|
|
2014-04-02 09:31:12 +00:00
|
|
|
//(6-9) in ITU-T Rec. H.265 (04/2013)
|
2017-02-06 11:00:25 +00:00
|
|
|
int tileIdx = 0;
|
|
|
|
for (int j = 0; j < encoder->cfg.tiles_height_count; ++j) {
|
|
|
|
for (int i = 0; i < encoder->cfg.tiles_width_count; ++i) {
|
|
|
|
for (int y = tiles_row_bd[j]; y < tiles_row_bd[j+1]; ++y) {
|
|
|
|
for (int x = tiles_col_bd[i]; x < tiles_col_bd[i+1]; ++x) {
|
2014-04-02 09:31:12 +00:00
|
|
|
tiles_tile_id[tiles_ctb_addr_rs_to_ts[y * encoder->in.width_in_lcu + x]] = tileIdx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++tileIdx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-05 09:59:21 +00:00
|
|
|
if (encoder->cfg.slices & KVZ_SLICES_WPP) {
|
2017-01-31 13:44:23 +00:00
|
|
|
// Each WPP row will be put into a dependent slice.
|
|
|
|
encoder->pps.dependent_slice_segments_enabled_flag = 1;
|
|
|
|
}
|
|
|
|
|
2014-05-05 13:17:22 +00:00
|
|
|
//Slices
|
2017-02-05 09:59:21 +00:00
|
|
|
if (encoder->cfg.slices & KVZ_SLICES_TILES) {
|
2017-01-31 13:44:23 +00:00
|
|
|
// Configure a single independent slice per tile.
|
|
|
|
|
|
|
|
int *slice_addresses_in_ts;
|
2017-02-06 11:00:25 +00:00
|
|
|
encoder->slice_count = encoder->cfg.tiles_width_count * encoder->cfg.tiles_height_count;
|
2017-01-31 13:44:23 +00:00
|
|
|
encoder->slice_addresses_in_ts = slice_addresses_in_ts = MALLOC(int, encoder->slice_count);
|
|
|
|
|
|
|
|
int slice_id = 0;
|
2017-02-06 11:00:25 +00:00
|
|
|
for (int tile_row = 0; tile_row < encoder->cfg.tiles_height_count; ++tile_row) {
|
|
|
|
for (int tile_col = 0; tile_col < encoder->cfg.tiles_width_count; ++tile_col) {
|
2017-01-31 13:44:23 +00:00
|
|
|
int x = tiles_col_bd[tile_col];
|
|
|
|
int y = tiles_row_bd[tile_row];
|
|
|
|
int rs = y * encoder->in.width_in_lcu + x;
|
|
|
|
int ts = tiles_ctb_addr_rs_to_ts[rs];
|
|
|
|
slice_addresses_in_ts[slice_id] = ts;
|
|
|
|
slice_id += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2014-05-05 13:17:22 +00:00
|
|
|
int *slice_addresses_in_ts;
|
2017-02-05 09:59:21 +00:00
|
|
|
encoder->slice_count = encoder->cfg.slice_count;
|
2014-05-05 13:17:22 +00:00
|
|
|
if (encoder->slice_count == 0) {
|
|
|
|
encoder->slice_count = 1;
|
2015-06-30 06:14:31 +00:00
|
|
|
|
|
|
|
encoder->slice_addresses_in_ts = slice_addresses_in_ts =
|
|
|
|
MALLOC(int, encoder->slice_count);
|
|
|
|
if (!slice_addresses_in_ts) goto init_failed;
|
|
|
|
|
2014-05-05 13:17:22 +00:00
|
|
|
slice_addresses_in_ts[0] = 0;
|
2015-06-30 06:14:31 +00:00
|
|
|
|
2014-05-05 13:17:22 +00:00
|
|
|
} else {
|
2015-06-30 06:14:31 +00:00
|
|
|
encoder->slice_addresses_in_ts = slice_addresses_in_ts =
|
|
|
|
MALLOC(int, encoder->slice_count);
|
|
|
|
if (!slice_addresses_in_ts) goto init_failed;
|
|
|
|
|
2017-02-05 09:59:21 +00:00
|
|
|
if (!cfg->slice_addresses_in_ts) {
|
2014-05-05 13:17:22 +00:00
|
|
|
slice_addresses_in_ts[0] = 0;
|
2017-02-06 11:00:25 +00:00
|
|
|
for (int i = 1; i < encoder->slice_count; ++i) {
|
2014-05-05 13:17:22 +00:00
|
|
|
slice_addresses_in_ts[i] = encoder->in.width_in_lcu * encoder->in.height_in_lcu * i / encoder->slice_count;
|
|
|
|
}
|
|
|
|
} else {
|
2017-02-06 11:00:25 +00:00
|
|
|
for (int i = 0; i < encoder->slice_count; ++i) {
|
2017-02-05 09:59:21 +00:00
|
|
|
slice_addresses_in_ts[i] = cfg->slice_addresses_in_ts[i];
|
2014-05-05 13:17:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-30 06:14:31 +00:00
|
|
|
|
2015-09-10 10:27:50 +00:00
|
|
|
#ifdef _DEBUG_PRINT_THREADING_INFO
|
2014-04-22 12:49:39 +00:00
|
|
|
printf("Tiles columns width:");
|
2017-02-06 11:00:25 +00:00
|
|
|
for (int i = 0; i < encoder->cfg.tiles_width_count; ++i) {
|
2014-04-22 12:49:39 +00:00
|
|
|
printf(" %d", encoder->tiles_col_width[i]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
printf("Tiles row height:");
|
2017-02-06 11:00:25 +00:00
|
|
|
for (int i = 0; i < encoder->cfg.tiles_height_count; ++i) {
|
2014-04-22 12:49:39 +00:00
|
|
|
printf(" %d", encoder->tiles_row_height[i]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
//Print tile index map
|
2017-02-06 11:00:25 +00:00
|
|
|
for (int y = 0; y < encoder->in.height_in_lcu; ++y) {
|
|
|
|
for (int x = 0; x < encoder->in.width_in_lcu; ++x) {
|
2014-05-05 13:17:22 +00:00
|
|
|
const int lcu_id_rs = y * encoder->in.width_in_lcu + x;
|
|
|
|
const int lcu_id_ts = encoder->tiles_ctb_addr_rs_to_ts[lcu_id_rs];
|
2015-08-26 08:50:27 +00:00
|
|
|
const char slice_start = kvz_lcu_at_slice_start(encoder, lcu_id_ts) ? '|' : ' ';
|
|
|
|
const char slice_end = kvz_lcu_at_slice_end(encoder, lcu_id_ts) ? '|' : ' ';
|
2015-06-30 06:14:31 +00:00
|
|
|
|
2014-05-05 13:17:22 +00:00
|
|
|
printf("%c%03d%c", slice_start, encoder->tiles_tile_id[lcu_id_ts], slice_end);
|
2014-04-22 12:49:39 +00:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
2014-05-05 13:17:22 +00:00
|
|
|
printf("\n");
|
2017-02-06 11:00:25 +00:00
|
|
|
if (encoder->cfg.wpp) {
|
2014-05-05 13:17:22 +00:00
|
|
|
printf("Wavefront Parallel Processing: enabled\n");
|
|
|
|
} else {
|
|
|
|
printf("Wavefront Parallel Processing: disabled\n");
|
|
|
|
}
|
|
|
|
printf("\n");
|
2015-09-14 09:43:28 +00:00
|
|
|
#endif //KVZ_DEBUG
|
2014-04-02 09:31:12 +00:00
|
|
|
}
|
2015-05-15 11:04:56 +00:00
|
|
|
|
2020-03-20 07:04:00 +00:00
|
|
|
for( size_t i = 0; i < KVZ_MAX_GOP_LAYERS; i++ )
|
2020-03-17 15:57:18 +00:00
|
|
|
{
|
|
|
|
if( encoder->cfg.pu_depth_inter.min[i] < 0 || cfg->pu_depth_inter.max[i] < 0 ) continue;
|
|
|
|
assert( WITHIN( encoder->cfg.pu_depth_inter.min[i], PU_DEPTH_INTER_MIN, PU_DEPTH_INTER_MAX ) );
|
|
|
|
assert( WITHIN( encoder->cfg.pu_depth_inter.max[i], PU_DEPTH_INTER_MIN, PU_DEPTH_INTER_MAX ) );
|
2017-02-06 11:00:25 +00:00
|
|
|
|
2020-03-17 15:57:18 +00:00
|
|
|
if( encoder->cfg.pu_depth_intra.min[i] < 0 || cfg->pu_depth_intra.max[i] < 0 ) continue;
|
|
|
|
assert( WITHIN( encoder->cfg.pu_depth_intra.min[i], PU_DEPTH_INTRA_MIN, PU_DEPTH_INTRA_MAX ) );
|
|
|
|
assert( WITHIN( encoder->cfg.pu_depth_intra.max[i], PU_DEPTH_INTRA_MIN, PU_DEPTH_INTRA_MAX ) );
|
|
|
|
}
|
2017-02-06 11:00:25 +00:00
|
|
|
// Disable in-loop filters, sign hiding and transform skip when using
|
|
|
|
// lossless coding.
|
|
|
|
if (encoder->cfg.lossless) {
|
|
|
|
encoder->cfg.deblock_enable = false;
|
2017-08-11 08:57:09 +00:00
|
|
|
encoder->cfg.sao_type = false;
|
2017-02-06 11:00:25 +00:00
|
|
|
encoder->cfg.signhide_enable = false;
|
|
|
|
encoder->cfg.trskip_enable = false;
|
|
|
|
}
|
2016-01-14 20:08:35 +00:00
|
|
|
|
|
|
|
// If fractional framerate is set, use that instead of the floating point framerate.
|
2017-02-05 09:59:21 +00:00
|
|
|
if (encoder->cfg.framerate_num != 0) {
|
2016-01-14 20:08:35 +00:00
|
|
|
encoder->vui.timing_info_present_flag = 1;
|
2017-02-05 09:59:21 +00:00
|
|
|
encoder->vui.num_units_in_tick = encoder->cfg.framerate_denom;
|
|
|
|
encoder->vui.time_scale = encoder->cfg.framerate_num;
|
|
|
|
if (encoder->cfg.source_scan_type != KVZ_INTERLACING_NONE) {
|
2016-01-28 19:04:52 +00:00
|
|
|
// when field_seq_flag=1, the time_scale and num_units_in_tick refer to
|
|
|
|
// field rate rather than frame rate.
|
|
|
|
encoder->vui.time_scale *= 2;
|
|
|
|
}
|
2016-01-14 20:08:35 +00:00
|
|
|
}
|
|
|
|
|
2017-02-05 09:59:21 +00:00
|
|
|
if (encoder->cfg.vps_period >= 0) {
|
2017-02-06 11:00:25 +00:00
|
|
|
encoder->cfg.vps_period = encoder->cfg.vps_period * encoder->cfg.intra_period;
|
2015-10-15 12:11:12 +00:00
|
|
|
} else {
|
2017-02-06 11:00:25 +00:00
|
|
|
encoder->cfg.vps_period = -1;
|
2015-10-15 12:11:12 +00:00
|
|
|
}
|
2015-05-15 11:04:56 +00:00
|
|
|
|
2021-03-16 08:22:40 +00:00
|
|
|
for (int i = 0; i < cfg->num_used_table; i++) {
|
|
|
|
encoder->qp_map[i] = derive_chroma_QP_mapping_table(cfg, i);
|
|
|
|
}
|
|
|
|
|
2015-06-30 06:14:31 +00:00
|
|
|
return encoder;
|
|
|
|
|
|
|
|
init_failed:
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_encoder_control_free(encoder);
|
2015-06-30 06:14:31 +00:00
|
|
|
return NULL;
|
2014-04-17 12:42:20 +00:00
|
|
|
}
|
2014-01-31 18:34:50 +00:00
|
|
|
|
2015-06-30 06:14:31 +00:00
|
|
|
/**
|
|
|
|
* \brief Free an encoder control structure.
|
|
|
|
*/
|
2017-02-05 09:59:21 +00:00
|
|
|
void kvz_encoder_control_free(encoder_control_t *const encoder)
|
|
|
|
{
|
2015-06-30 06:14:31 +00:00
|
|
|
if (!encoder) return;
|
|
|
|
|
2014-05-05 13:17:22 +00:00
|
|
|
//Slices
|
|
|
|
FREE_POINTER(encoder->slice_addresses_in_ts);
|
2015-06-30 06:14:31 +00:00
|
|
|
|
2014-04-02 09:31:12 +00:00
|
|
|
//Tiles
|
2014-05-01 12:10:22 +00:00
|
|
|
FREE_POINTER(encoder->tiles_col_width);
|
|
|
|
FREE_POINTER(encoder->tiles_row_height);
|
2015-06-30 06:14:31 +00:00
|
|
|
|
2014-05-01 12:10:22 +00:00
|
|
|
FREE_POINTER(encoder->tiles_col_bd);
|
|
|
|
FREE_POINTER(encoder->tiles_row_bd);
|
2015-06-30 06:14:31 +00:00
|
|
|
|
2014-05-01 12:10:22 +00:00
|
|
|
FREE_POINTER(encoder->tiles_ctb_addr_rs_to_ts);
|
|
|
|
FREE_POINTER(encoder->tiles_ctb_addr_ts_to_rs);
|
2015-06-30 06:14:31 +00:00
|
|
|
|
2014-05-01 12:10:22 +00:00
|
|
|
FREE_POINTER(encoder->tiles_tile_id);
|
2015-06-30 06:14:31 +00:00
|
|
|
|
2017-02-05 09:59:21 +00:00
|
|
|
FREE_POINTER(encoder->cfg.roi.dqps);
|
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_scalinglist_destroy(&encoder->scaling_list);
|
2015-06-30 06:14:31 +00:00
|
|
|
|
2017-07-20 12:23:18 +00:00
|
|
|
kvz_threadqueue_free(encoder->threadqueue);
|
|
|
|
encoder->threadqueue = NULL;
|
2021-03-16 08:22:40 +00:00
|
|
|
for (int i = 0; i < encoder->cfg.num_used_table; i++) {
|
|
|
|
if (encoder->qp_map[i]) FREE_POINTER(encoder->qp_map[i]);
|
|
|
|
}
|
2014-05-13 09:28:15 +00:00
|
|
|
|
2020-11-05 17:47:47 +00:00
|
|
|
kvz_close_rdcost_outfiles();
|
2020-11-03 11:23:19 +00:00
|
|
|
|
2015-06-30 06:14:31 +00:00
|
|
|
free(encoder);
|
2014-04-17 12:42:20 +00:00
|
|
|
}
|
2014-01-31 18:34:50 +00:00
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
void kvz_encoder_control_input_init(encoder_control_t * const encoder,
|
2015-08-18 16:11:08 +00:00
|
|
|
const int32_t width, int32_t height)
|
2014-05-07 07:56:16 +00:00
|
|
|
{
|
2015-08-18 16:11:08 +00:00
|
|
|
// Halve for interlaced content
|
|
|
|
if (encoder->in.source_scan_type != 0) height /= 2;
|
|
|
|
|
2014-05-07 07:56:16 +00:00
|
|
|
encoder->in.width = width;
|
|
|
|
encoder->in.height = height;
|
|
|
|
encoder->in.real_width = width;
|
|
|
|
encoder->in.real_height = height;
|
|
|
|
|
|
|
|
// If input dimensions are not divisible by the smallest block size, add
|
|
|
|
// pixels to the dimensions, so that they are. These extra pixels will be
|
|
|
|
// compressed along with the real ones but they will be cropped out before
|
|
|
|
// rendering.
|
|
|
|
if (encoder->in.width % CU_MIN_SIZE_PIXELS) {
|
|
|
|
encoder->in.width += CU_MIN_SIZE_PIXELS - (width % CU_MIN_SIZE_PIXELS);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (encoder->in.height % CU_MIN_SIZE_PIXELS) {
|
|
|
|
encoder->in.height += CU_MIN_SIZE_PIXELS - (height % CU_MIN_SIZE_PIXELS);
|
|
|
|
}
|
|
|
|
|
|
|
|
encoder->in.height_in_lcu = encoder->in.height / LCU_WIDTH;
|
|
|
|
encoder->in.width_in_lcu = encoder->in.width / LCU_WIDTH;
|
|
|
|
|
|
|
|
// Add one extra LCU when image not divisible by LCU_WIDTH
|
|
|
|
if (encoder->in.height_in_lcu * LCU_WIDTH < height) {
|
|
|
|
encoder->in.height_in_lcu++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (encoder->in.width_in_lcu * LCU_WIDTH < width) {
|
|
|
|
encoder->in.width_in_lcu++;
|
|
|
|
}
|
|
|
|
|
2015-05-27 12:41:45 +00:00
|
|
|
encoder->in.pixels_per_pic = encoder->in.width * encoder->in.height;
|
2014-05-07 07:56:16 +00:00
|
|
|
|
|
|
|
|
2015-09-14 09:43:28 +00:00
|
|
|
#ifdef KVZ_DEBUG
|
2014-05-07 07:56:16 +00:00
|
|
|
if (width != encoder->in.width || height != encoder->in.height) {
|
|
|
|
printf("Picture buffer has been extended to be a multiple of the smallest block size:\r\n");
|
|
|
|
printf(" Width = %d (%d), Height = %d (%d)\r\n", width, encoder->in.width, height,
|
|
|
|
encoder->in.height);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-05-27 12:41:45 +00:00
|
|
|
/**
|
|
|
|
* \brief Initialize GOP layer weights.
|
|
|
|
* \return 1 on success, 0 on failure.
|
|
|
|
*
|
|
|
|
* Selects appropriate weights for layers according to the target bpp.
|
2020-02-14 16:58:47 +00:00
|
|
|
* Only GOP structures with exactly four layers are supported with the.
|
|
|
|
* exception of experimental GOP 16.
|
2015-05-27 12:41:45 +00:00
|
|
|
*/
|
|
|
|
static int encoder_control_init_gop_layer_weights(encoder_control_t * const encoder)
|
|
|
|
{
|
|
|
|
|
2017-02-05 09:59:21 +00:00
|
|
|
kvz_gop_config const * const gop = encoder->cfg.gop;
|
|
|
|
const int8_t gop_len = encoder->cfg.gop_len;
|
2015-05-27 12:41:45 +00:00
|
|
|
|
|
|
|
int num_layers = 0;
|
|
|
|
for (int i = 0; i < gop_len; ++i) {
|
|
|
|
num_layers = MAX(gop[i].layer, num_layers);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (num_layers) {
|
|
|
|
case 0:
|
2016-05-25 11:08:06 +00:00
|
|
|
case 1:
|
2018-11-06 07:38:17 +00:00
|
|
|
encoder->gop_layer_weights[0] = 1;
|
2015-05-27 12:41:45 +00:00
|
|
|
break;
|
|
|
|
|
2016-05-25 11:08:06 +00:00
|
|
|
// Use the first layers of the 4-layer weights.
|
|
|
|
case 2:
|
2015-11-02 10:22:25 +00:00
|
|
|
case 3:
|
2016-05-25 11:08:06 +00:00
|
|
|
|
2015-05-27 12:41:45 +00:00
|
|
|
case 4:
|
2017-02-05 09:59:21 +00:00
|
|
|
if (encoder->cfg.gop_lowdelay) {
|
2016-05-25 11:08:06 +00:00
|
|
|
// These weights are based on http://doi.org/10.1109/TIP.2014.2336550
|
|
|
|
// They are meant for lp-g4d3r4t1 gop, but work ok for others.
|
|
|
|
if (encoder->target_avg_bpp <= 0.05) {
|
|
|
|
encoder->gop_layer_weights[0] = 14;
|
|
|
|
encoder->gop_layer_weights[1] = 3;
|
|
|
|
encoder->gop_layer_weights[2] = 2;
|
|
|
|
encoder->gop_layer_weights[3] = 1;
|
|
|
|
} else if (encoder->target_avg_bpp <= 0.1) {
|
|
|
|
encoder->gop_layer_weights[0] = 12;
|
|
|
|
encoder->gop_layer_weights[1] = 3;
|
|
|
|
encoder->gop_layer_weights[2] = 2;
|
|
|
|
encoder->gop_layer_weights[3] = 1;
|
|
|
|
} else if (encoder->target_avg_bpp <= 0.2) {
|
|
|
|
encoder->gop_layer_weights[0] = 10;
|
|
|
|
encoder->gop_layer_weights[1] = 3;
|
|
|
|
encoder->gop_layer_weights[2] = 2;
|
|
|
|
encoder->gop_layer_weights[3] = 1;
|
|
|
|
} else {
|
|
|
|
encoder->gop_layer_weights[0] = 6;
|
|
|
|
encoder->gop_layer_weights[1] = 3;
|
|
|
|
encoder->gop_layer_weights[2] = 2;
|
|
|
|
encoder->gop_layer_weights[3] = 1;
|
|
|
|
}
|
2015-05-27 12:41:45 +00:00
|
|
|
} else {
|
2016-05-25 11:08:06 +00:00
|
|
|
// These weights are from http://doi.org/10.1109/TIP.2014.2336550
|
|
|
|
if (encoder->target_avg_bpp <= 0.05) {
|
|
|
|
encoder->gop_layer_weights[0] = 30;
|
|
|
|
encoder->gop_layer_weights[1] = 8;
|
|
|
|
encoder->gop_layer_weights[2] = 4;
|
|
|
|
encoder->gop_layer_weights[3] = 1;
|
|
|
|
} else if (encoder->target_avg_bpp <= 0.1) {
|
|
|
|
encoder->gop_layer_weights[0] = 25;
|
|
|
|
encoder->gop_layer_weights[1] = 7;
|
|
|
|
encoder->gop_layer_weights[2] = 4;
|
|
|
|
encoder->gop_layer_weights[3] = 1;
|
|
|
|
} else if (encoder->target_avg_bpp <= 0.2) {
|
|
|
|
encoder->gop_layer_weights[0] = 20;
|
|
|
|
encoder->gop_layer_weights[1] = 6;
|
|
|
|
encoder->gop_layer_weights[2] = 4;
|
|
|
|
encoder->gop_layer_weights[3] = 1;
|
|
|
|
} else {
|
|
|
|
encoder->gop_layer_weights[0] = 15;
|
|
|
|
encoder->gop_layer_weights[1] = 5;
|
|
|
|
encoder->gop_layer_weights[2] = 4;
|
|
|
|
encoder->gop_layer_weights[3] = 1;
|
|
|
|
}
|
2015-05-27 12:41:45 +00:00
|
|
|
}
|
|
|
|
break;
|
2020-03-19 09:13:43 +00:00
|
|
|
case 5:
|
|
|
|
if(!encoder->cfg.gop_lowdelay) {
|
|
|
|
// These are obtained by running HM with RA GOP 16 collecting the ratio of bits spent for each
|
|
|
|
// layer from the CTC sequences and then fitting power curve
|
|
|
|
encoder->gop_layer_weights[0] = 13.0060187535 * pow(encoder->target_avg_bpp, -0.3727651453);
|
|
|
|
encoder->gop_layer_weights[1] = 7.3654107392 * pow(encoder->target_avg_bpp, -0.0854329266);
|
|
|
|
encoder->gop_layer_weights[2] = 3.6563990701 * pow(encoder->target_avg_bpp, -0.0576990493);
|
|
|
|
encoder->gop_layer_weights[3] = 2.1486937288 * pow(encoder->target_avg_bpp, -0.0155389471);
|
|
|
|
encoder->gop_layer_weights[4] = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fprintf(stderr, "Unsupported amount of layers (%d) for lowdelay GOP\n", num_layers);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
2015-05-27 12:41:45 +00:00
|
|
|
default:
|
2020-02-14 16:58:47 +00:00
|
|
|
if (!encoder->cfg.gop_lowdelay && encoder->cfg.gop_len == 16) {
|
|
|
|
fprintf(stdout,
|
|
|
|
"Rate control: Using experimental weights for GOP layers (%d)\n",
|
|
|
|
num_layers);
|
|
|
|
for (int i = 0; i < MAX_GOP_LAYERS; ++i) {
|
|
|
|
encoder->gop_layer_weights[i] = (i == 0) ? 10 : 2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Unsupported number of GOP layers (%d)\n", num_layers);
|
|
|
|
return 0;
|
|
|
|
}
|
2015-05-27 12:41:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Normalize weights so that the sum of weights in a GOP is one.
|
|
|
|
double sum_weights = 0;
|
|
|
|
for (int i = 0; i < gop_len; ++i) {
|
|
|
|
sum_weights += encoder->gop_layer_weights[gop[i].layer - 1];
|
|
|
|
}
|
|
|
|
for (int i = 0; i < num_layers; ++i) {
|
|
|
|
encoder->gop_layer_weights[i] /= sum_weights;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|