2014-06-03 11:51:30 +00:00
|
|
|
#ifndef ENCODERSTATE_H_
|
|
|
|
#define ENCODERSTATE_H_
|
|
|
|
/*****************************************************************************
|
|
|
|
* This file is part of Kvazaar HEVC encoder.
|
|
|
|
*
|
2015-02-23 11:18:48 +00:00
|
|
|
* Copyright (C) 2013-2015 Tampere University of Technology and others (see
|
2014-06-03 11:51:30 +00:00
|
|
|
* COPYING file).
|
|
|
|
*
|
2015-02-23 11:18:48 +00:00
|
|
|
* 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.
|
2014-06-03 11:51:30 +00:00
|
|
|
*
|
2015-02-23 11:18:48 +00:00
|
|
|
* 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.
|
2014-06-03 11:51:30 +00:00
|
|
|
*
|
2015-02-23 11:18:48 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with Kvazaar. If not, see <http://www.gnu.org/licenses/>.
|
2014-06-03 11:51:30 +00:00
|
|
|
****************************************************************************/
|
|
|
|
|
2015-12-17 11:42:57 +00:00
|
|
|
/**
|
|
|
|
* \ingroup Control
|
2014-06-03 11:51:30 +00:00
|
|
|
* \file
|
2015-12-17 11:42:57 +00:00
|
|
|
* Top level of the encoder implementation.
|
2014-06-03 11:51:30 +00:00
|
|
|
*/
|
|
|
|
|
2016-03-30 09:41:37 +00:00
|
|
|
#include "global.h" // IWYU pragma: keep
|
2014-06-03 11:51:30 +00:00
|
|
|
|
2014-06-05 12:54:58 +00:00
|
|
|
#include "videoframe.h"
|
2014-06-03 11:51:30 +00:00
|
|
|
#include "encoder.h"
|
2014-06-05 12:54:58 +00:00
|
|
|
#include "image.h"
|
2014-06-03 11:51:30 +00:00
|
|
|
#include "bitstream.h"
|
|
|
|
#include "cabac.h"
|
|
|
|
#include "tables.h"
|
|
|
|
#include "scalinglist.h"
|
|
|
|
#include "threadqueue.h"
|
2014-06-05 12:54:58 +00:00
|
|
|
#include "imagelist.h"
|
2014-06-03 11:51:30 +00:00
|
|
|
|
2014-06-04 14:45:46 +00:00
|
|
|
// Submodules
|
|
|
|
// Functions to obtain geometry information from LCU
|
|
|
|
#include "encoder_state-geometry.h"
|
|
|
|
// Constructors/destructors
|
|
|
|
#include "encoder_state-ctors_dtors.h"
|
|
|
|
// Functions writing bitstream parts
|
|
|
|
#include "encoder_state-bitstream.h"
|
|
|
|
|
|
|
|
|
2014-06-03 11:51:30 +00:00
|
|
|
typedef enum {
|
|
|
|
ENCODER_STATE_TYPE_INVALID = 'i',
|
|
|
|
ENCODER_STATE_TYPE_MAIN = 'M',
|
|
|
|
ENCODER_STATE_TYPE_SLICE = 'S',
|
|
|
|
ENCODER_STATE_TYPE_TILE = 'T',
|
|
|
|
ENCODER_STATE_TYPE_WAVEFRONT_ROW = 'W',
|
|
|
|
} encoder_state_type;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2014-06-26 08:17:32 +00:00
|
|
|
double cur_lambda_cost; //!< \brief Lambda for SSE
|
|
|
|
double cur_lambda_cost_sqrt; //!< \brief Lambda for SAD and SATD
|
2014-06-03 11:51:30 +00:00
|
|
|
|
|
|
|
int32_t frame;
|
|
|
|
int32_t poc; /*!< \brief picture order count */
|
2015-03-25 14:00:20 +00:00
|
|
|
int8_t gop_offset; /*!< \brief offset in the gop structure */
|
2014-06-03 11:51:30 +00:00
|
|
|
|
|
|
|
int8_t QP; //!< \brief Quantization parameter
|
2015-03-03 10:21:45 +00:00
|
|
|
double QP_factor; //!< \brief Quantization factor
|
2014-06-03 11:51:30 +00:00
|
|
|
|
|
|
|
//Current picture available references
|
2015-03-04 14:13:57 +00:00
|
|
|
image_list_t *ref;
|
2014-06-03 11:51:30 +00:00
|
|
|
int8_t ref_list;
|
2015-03-19 10:24:52 +00:00
|
|
|
|
|
|
|
struct {
|
|
|
|
int32_t poc;
|
|
|
|
int8_t list;
|
|
|
|
int8_t idx;
|
|
|
|
} refmap[16];
|
2014-06-03 11:51:30 +00:00
|
|
|
|
2015-04-30 10:30:02 +00:00
|
|
|
bool is_idr_frame;
|
2014-06-03 11:51:30 +00:00
|
|
|
uint8_t pictype;
|
2015-09-08 10:57:15 +00:00
|
|
|
enum kvz_slice_type slicetype;
|
2015-04-17 14:31:22 +00:00
|
|
|
|
|
|
|
//! Total number of bits written.
|
|
|
|
uint64_t total_bits_coded;
|
2015-04-29 11:24:33 +00:00
|
|
|
|
|
|
|
//! Number of bits written in the current GOP.
|
|
|
|
uint64_t cur_gop_bits_coded;
|
|
|
|
|
2015-05-04 09:20:41 +00:00
|
|
|
//! Number of bits targeted for the current GOP.
|
2015-05-06 11:08:18 +00:00
|
|
|
double cur_gop_target_bits;
|
|
|
|
|
|
|
|
// Parameters used in rate control
|
|
|
|
double rc_alpha;
|
|
|
|
double rc_beta;
|
2015-05-04 09:20:41 +00:00
|
|
|
|
2015-03-04 11:48:58 +00:00
|
|
|
} encoder_state_config_global_t;
|
2014-06-03 11:51:30 +00:00
|
|
|
|
|
|
|
typedef struct {
|
2014-06-05 12:54:58 +00:00
|
|
|
//Current sub-frame
|
2015-03-04 11:17:33 +00:00
|
|
|
videoframe_t *frame;
|
2014-06-03 11:51:30 +00:00
|
|
|
|
|
|
|
int32_t id;
|
|
|
|
|
|
|
|
//Tile: offset in LCU for current encoder_state in global coordinates
|
|
|
|
int32_t lcu_offset_x;
|
|
|
|
int32_t lcu_offset_y;
|
|
|
|
|
|
|
|
//Position of the first element in tile scan in global coordinates
|
|
|
|
int32_t lcu_offset_in_ts;
|
|
|
|
|
|
|
|
//Buffer for search
|
|
|
|
//order by row of (LCU_WIDTH * cur_pic->width_in_lcu) pixels
|
|
|
|
yuv_t *hor_buf_search;
|
|
|
|
//order by column of (LCU_WIDTH * encoder_state->height_in_lcu) pixels (there is no more extra pixel, since we can use a negative index)
|
|
|
|
yuv_t *ver_buf_search;
|
|
|
|
|
2016-03-08 16:31:54 +00:00
|
|
|
// The bottom post-deblocking, pre-SAO pixels of every WPP-row.
|
2014-06-03 11:51:30 +00:00
|
|
|
yuv_t *hor_buf_before_sao;
|
|
|
|
|
2015-03-17 09:13:48 +00:00
|
|
|
//Jobs for each individual LCU of a wavefront row.
|
2015-03-04 14:28:56 +00:00
|
|
|
threadqueue_job_t **wf_jobs;
|
2015-03-04 11:48:58 +00:00
|
|
|
} encoder_state_config_tile_t;
|
2014-06-03 11:51:30 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int32_t id;
|
|
|
|
|
|
|
|
//Global coordinates
|
|
|
|
int32_t start_in_ts;
|
|
|
|
int32_t end_in_ts;
|
|
|
|
|
|
|
|
//Global coordinates
|
|
|
|
int32_t start_in_rs;
|
|
|
|
int32_t end_in_rs;
|
2015-03-04 11:48:58 +00:00
|
|
|
} encoder_state_config_slice_t;
|
2014-06-03 11:51:30 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
//Row in tile coordinates of the wavefront
|
|
|
|
int32_t lcu_offset_y;
|
2015-03-04 11:48:58 +00:00
|
|
|
} encoder_state_config_wfrow_t;
|
2014-06-03 11:51:30 +00:00
|
|
|
|
|
|
|
typedef struct lcu_order_element {
|
|
|
|
//This it used for leaf of the encoding tree. All is relative to the tile.
|
|
|
|
int id;
|
|
|
|
int index;
|
2015-03-04 10:25:42 +00:00
|
|
|
struct encoder_state_t *encoder_state;
|
2015-03-04 11:32:11 +00:00
|
|
|
vector2d_t position;
|
|
|
|
vector2d_t position_px; //Top-left
|
|
|
|
vector2d_t size;
|
2014-06-03 11:51:30 +00:00
|
|
|
int first_column;
|
|
|
|
int first_row;
|
|
|
|
int last_column;
|
|
|
|
int last_row;
|
|
|
|
|
|
|
|
struct lcu_order_element *above;
|
|
|
|
struct lcu_order_element *below;
|
|
|
|
struct lcu_order_element *left;
|
|
|
|
struct lcu_order_element *right;
|
2015-03-04 11:49:59 +00:00
|
|
|
} lcu_order_element_t;
|
2014-06-03 11:51:30 +00:00
|
|
|
|
2015-03-04 10:25:42 +00:00
|
|
|
typedef struct encoder_state_t {
|
2015-03-04 10:26:57 +00:00
|
|
|
const encoder_control_t *encoder_control;
|
2014-06-03 11:51:30 +00:00
|
|
|
encoder_state_type type;
|
|
|
|
|
|
|
|
//List of children, the last item of this list is a pseudo-encoder with encoder_control = NULL
|
|
|
|
//Use for (i = 0; encoder_state->children[i].encoder_control; ++i) {
|
2015-03-04 10:25:42 +00:00
|
|
|
struct encoder_state_t *children;
|
|
|
|
struct encoder_state_t *parent;
|
2014-06-03 11:51:30 +00:00
|
|
|
|
2014-06-05 07:09:25 +00:00
|
|
|
//Pointer to the encoder_state of the previous frame
|
2015-03-04 10:25:42 +00:00
|
|
|
struct encoder_state_t *previous_encoder_state;
|
2014-06-05 07:09:25 +00:00
|
|
|
|
2015-03-04 11:48:58 +00:00
|
|
|
encoder_state_config_global_t *global;
|
|
|
|
encoder_state_config_tile_t *tile;
|
|
|
|
encoder_state_config_slice_t *slice;
|
|
|
|
encoder_state_config_wfrow_t *wfrow;
|
2014-06-03 11:51:30 +00:00
|
|
|
|
|
|
|
int is_leaf; //A leaf encoder state is one which should encode LCUs...
|
2015-03-04 11:49:59 +00:00
|
|
|
lcu_order_element_t *lcu_order;
|
2014-06-03 11:51:30 +00:00
|
|
|
uint32_t lcu_order_count;
|
|
|
|
|
2015-03-04 11:20:18 +00:00
|
|
|
bitstream_t stream;
|
2015-03-04 11:27:32 +00:00
|
|
|
cabac_data_t cabac;
|
2015-06-17 11:17:30 +00:00
|
|
|
|
2015-06-18 06:10:47 +00:00
|
|
|
/**
|
|
|
|
* \brief Indicates that this encoder state is ready for encoding the
|
2015-08-26 08:50:27 +00:00
|
|
|
* next frame i.e. kvz_encoder_next_frame has been called.
|
2015-06-18 06:10:47 +00:00
|
|
|
*/
|
|
|
|
int prepared;
|
|
|
|
|
2015-06-17 11:17:30 +00:00
|
|
|
/**
|
|
|
|
* \brief Indicates that the previous frame has been encoded and the
|
|
|
|
* encoded data written and the encoding the next frame has not been
|
|
|
|
* started yet.
|
|
|
|
*/
|
|
|
|
int frame_done;
|
|
|
|
|
2014-06-19 08:23:01 +00:00
|
|
|
uint32_t stats_bitstream_length; //Bitstream length written in bytes
|
2014-06-16 07:22:59 +00:00
|
|
|
|
2014-06-05 07:09:25 +00:00
|
|
|
//Jobs to wait for
|
2015-03-04 14:28:56 +00:00
|
|
|
threadqueue_job_t * tqj_recon_done; //Reconstruction is done
|
|
|
|
threadqueue_job_t * tqj_bitstream_written; //Bitstream is written
|
2015-03-04 10:25:42 +00:00
|
|
|
} encoder_state_t;
|
2014-06-03 11:51:30 +00:00
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
void kvz_encode_one_frame(encoder_state_t *state);
|
2014-06-03 11:51:30 +00:00
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
void kvz_encoder_next_frame(encoder_state_t *state);
|
2014-06-03 11:51:30 +00:00
|
|
|
|
2014-06-03 12:25:16 +00:00
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
void kvz_encode_coding_tree(encoder_state_t *state, uint16_t x_ctb,
|
2014-06-03 11:51:30 +00:00
|
|
|
uint16_t y_ctb, uint8_t depth);
|
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
void kvz_encode_last_significant_xy(encoder_state_t *state,
|
2014-06-03 11:51:30 +00:00
|
|
|
uint8_t lastpos_x, uint8_t lastpos_y,
|
|
|
|
uint8_t width, uint8_t height,
|
|
|
|
uint8_t type, uint8_t scan);
|
2015-08-26 08:50:27 +00:00
|
|
|
void kvz_encode_coeff_nxn(encoder_state_t *state, coeff_t *coeff, uint8_t width,
|
2014-06-03 11:51:30 +00:00
|
|
|
uint8_t type, int8_t scan_mode, int8_t tr_skip);
|
2015-08-26 08:50:27 +00:00
|
|
|
void kvz_encode_transform_coeff(encoder_state_t *state, int32_t x_cu, int32_t y_cu,
|
2014-06-03 11:51:30 +00:00
|
|
|
int8_t depth, int8_t tr_depth, uint8_t parent_coeff_u, uint8_t parent_coeff_v);
|
2015-03-04 10:26:57 +00:00
|
|
|
void encode_block_residual(const encoder_control_t * const encoder,
|
2014-06-03 11:51:30 +00:00
|
|
|
uint16_t x_ctb, uint16_t y_ctb, uint8_t depth);
|
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
int kvz_encoder_state_match_children_of_previous_frame(encoder_state_t * const state);
|
2014-06-05 07:09:25 +00:00
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
coeff_scan_order_t kvz_get_scan_order(int8_t cu_type, int intra_mode, int depth);
|
2014-06-03 11:51:30 +00:00
|
|
|
|
2015-09-09 07:01:02 +00:00
|
|
|
void kvz_encoder_get_ref_lists(const encoder_state_t *const state,
|
|
|
|
int ref_list_len_out[2],
|
|
|
|
int ref_list_poc_out[2][16]);
|
2015-05-14 15:33:57 +00:00
|
|
|
|
2014-06-03 11:51:30 +00:00
|
|
|
static const uint8_t g_group_idx[32] = {
|
|
|
|
0, 1, 2, 3, 4, 4, 5, 5, 6, 6,
|
|
|
|
6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
|
|
|
|
8, 8, 8, 8, 9, 9, 9, 9, 9, 9,
|
|
|
|
9, 9 };
|
|
|
|
|
|
|
|
static const uint8_t g_min_in_group[10] = {
|
|
|
|
0, 1, 2, 3, 4, 6, 8, 12, 16, 24 };
|
|
|
|
|
|
|
|
|
|
|
|
#define C1FLAG_NUMBER 8 // maximum number of largerThan1 flag coded in one chunk
|
|
|
|
#define C2FLAG_NUMBER 1 // maximum number of largerThan2 flag coded in one chunk
|
|
|
|
|
|
|
|
//Get the data for vertical buffer position at the left of LCU identified by the position in pixel
|
|
|
|
#define OFFSET_VER_BUF(position_x, position_y, cur_pic, i) ((position_y) + i + ((position_x)/LCU_WIDTH - 1) * (cur_pic)->height)
|
|
|
|
#define OFFSET_VER_BUF_C(position_x, position_y, cur_pic, i) ((position_y/2) + i + ((position_x)/LCU_WIDTH - 1) * (cur_pic)->height / 2)
|
|
|
|
|
|
|
|
//Get the data for horizontal buffer position at the top of LCU identified by the position in pixel
|
|
|
|
#define OFFSET_HOR_BUF(position_x, position_y, cur_pic, i) ((position_x) + i + ((position_y)/LCU_WIDTH - 1) * (cur_pic)->width)
|
|
|
|
#define OFFSET_HOR_BUF_C(position_x, position_y, cur_pic, i) ((position_x/2) + i + ((position_y)/LCU_WIDTH - 1) * (cur_pic)->width / 2)
|
|
|
|
|
2015-12-17 11:42:57 +00:00
|
|
|
/** @} */
|
2014-06-03 11:51:30 +00:00
|
|
|
|
|
|
|
#endif //ENCODERSTATE_H_
|