TUT HEVC Encoder
encoderstate.h
Go to the documentation of this file.
1#ifndef ENCODERSTATE_H_
2#define ENCODERSTATE_H_
3/*****************************************************************************
4 * This file is part of Kvazaar HEVC encoder.
5 *
6 * Copyright (c) 2021, Tampere University, ITU/ISO/IEC, project contributors
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without modification,
10 * are permitted provided that the following conditions are met:
11 *
12 * * Redistributions of source code must retain the above copyright notice, this
13 * list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above copyright notice, this
16 * list of conditions and the following disclaimer in the documentation and/or
17 * other materials provided with the distribution.
18 *
19 * * Neither the name of the Tampere University or ITU/ISO/IEC nor the names of its
20 * contributors may be used to endorse or promote products derived from
21 * this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
27 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND ON
30 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
33 ****************************************************************************/
34
41#include "bitstream.h"
42#include "cabac.h"
43#include "cu.h"
44#include "encoder.h"
45#include "global.h" // IWYU pragma: keep
46#include "image.h"
47#include "imagelist.h"
48#include "kvazaar.h"
49#include "tables.h"
50#include "threadqueue.h"
51#include "videoframe.h"
52#include "extras/crypto.h"
53
54struct kvz_rc_data;
55
56typedef enum {
63
64
65typedef struct lcu_stats_t {
67 uint32_t bits;
68
69 uint32_t pixels;
70
72 double weight;
73
75
77 double lambda;
78
80
82 double rc_alpha;
83
85 double rc_beta;
86 double distortion;
87 int i_cost;
88
89 int8_t qp;
90 int8_t adjust_qp;
91 uint8_t skipped;
93
94
104 double lambda;
105
106 int32_t num;
107 int32_t poc;
108 int8_t gop_offset;
109 int32_t irap_poc;
116 int8_t QP;
118 double QP_factor;
119
122 int8_t ref_list;
123
125 uint8_t ref_LX[2][16];
127 uint8_t ref_LX_size[2];
128
130 uint8_t pictype;
132
135
138
141
144
147
148 // Parameters used in rate control
149 double rc_alpha;
150 double rc_beta;
151
157
163 bool done;
164
171
172 pthread_mutex_t rc_lock;
173
175
177
181 double *aq_offsets;
182
184
189 double icost;
192
193 double *c_para;
194 double *k_para;
196
198 //Current sub-frame
200
201 int32_t id;
202
203 //Tile: offset in LCU for current encoder_state in global coordinates
206
207 //Tile: offset in pixels
208 int32_t offset_x;
209 int32_t offset_y;
210
211 //Position of the first element in tile scan in global coordinates
213
214 // This is a buffer for the non-loopfiltered bottom pixels of every LCU-row
215 // in the tile. They are packed such that each LCU-row index maps to the
216 // y-coordinate.
218 // This is a buffer for the non-loopfiltered rightmost pixels of every
219 // LCU-column. They are packed such that each LCU-column index maps to the
220 // x-coordinate.
222
223 // This is a buffer for the deblocked bottom pixels of every LCU in the
224 // tile. They are packed such that each LCU-row index maps to the
225 // y-coordinate.
227
228 // This is a buffer for the deblocked right pixels of every LCU in the
229 // tile. They are packed such that each LCU-column index maps to the
230 // x-coordinate.
232
233 //Jobs for each individual LCU of a wavefront row.
235
237
239 int32_t id;
240
241 //Global coordinates
242 int32_t start_in_ts;
243 int32_t end_in_ts;
244
245 //Global coordinates
246 int32_t start_in_rs;
247 int32_t end_in_rs;
249
251 //Row in tile coordinates of the wavefront
254
255typedef struct lcu_order_element {
256 //This it used for leaf of the encoding tree. All is relative to the tile.
257 int id;
258 int index;
267
273
274typedef struct encoder_state_t {
277
278 //List of children, the last item of this list is a pseudo-encoder with encoder_control = NULL
279 //Use for (i = 0; encoder_state->children[i].encoder_control; ++i) {
282
283 //Pointer to the encoder_state of the previous frame
285
290
291 int is_leaf; //A leaf encoder state is one which should encode LCUs...
294
298
299 // Crypto stuff
300 crypto_handle_t *crypto_hdl;
302
303 uint32_t stats_bitstream_length; //Bitstream length written in bytes
304
306 double lambda;
310 int8_t qp;
311
316
328 int8_t last_qp;
329
334
335 //Jobs to wait for
336 threadqueue_job_t * tqj_recon_done; //Reconstruction is done
337 threadqueue_job_t * tqj_bitstream_written; //Bitstream is written
338
339 //Constraint structure
341
342
344
346
348
349
351
352coeff_scan_order_t kvz_get_scan_order(int8_t cu_type, int intra_mode, int depth);
353
354void kvz_encoder_create_ref_lists(const encoder_state_t *const state);
355
356lcu_stats_t* kvz_get_lcu_stats(encoder_state_t *state, int lcu_x, int lcu_y);
357
358
359int kvz_get_cu_ref_qp(const encoder_state_t *state, int x, int y, int last_qp);
360
365{
366 const int32_t frame = state->frame->num;
367 const int32_t vps_period = state->encoder_control->cfg.vps_period;
368
369 return (vps_period > 0 && frame % vps_period == 0) ||
370 (vps_period >= 0 && frame == 0);
371}
372
373
384static INLINE bool is_last_cu_in_qg(const encoder_state_t *state, int x, int y, int depth)
385{
386 if (state->frame->max_qp_delta_depth < 0) return false;
387
388 const int cu_width = LCU_WIDTH >> depth;
389 const int qg_width = LCU_WIDTH >> state->frame->max_qp_delta_depth;
390 const int right = x + cu_width;
391 const int bottom = y + cu_width;
392 return (right % qg_width == 0 || right >= state->tile->frame->width) &&
393 (bottom % qg_width == 0 || bottom >= state->tile->frame->height);
394}
395
396
397static const uint8_t g_group_idx[32] = {
398 0, 1, 2, 3, 4, 4, 5, 5, 6, 6,
399 6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
400 8, 8, 8, 8, 9, 9, 9, 9, 9, 9,
401 9, 9 };
402
403static const uint8_t g_min_in_group[10] = {
404 0, 1, 2, 3, 4, 6, 8, 12, 16, 24 };
405
406
407#define C1FLAG_NUMBER 8 // maximum number of largerThan1 flag coded in one chunk
408#define C2FLAG_NUMBER 1 // maximum number of largerThan2 flag coded in one chunk
409
410//Get the data for vertical buffer position at the left of LCU identified by the position in pixel
411#define OFFSET_VER_BUF(position_x, position_y, cur_pic, i) ((position_y) + i + ((position_x)/LCU_WIDTH - 1) * (cur_pic)->height)
412#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)
413
414//Get the data for horizontal buffer position at the top of LCU identified by the position in pixel
415#define OFFSET_HOR_BUF(position_x, position_y, cur_pic, i) ((position_x) + i + ((position_y)/LCU_WIDTH - 1) * (cur_pic)->width)
416#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)
417
420#endif //ENCODERSTATE_H_
Appending bits into an Annex-B coded bitstream.
Coding bins using CABAC.
Coding Unit data structure and related functions.
lcu_coeff_t
Definition: cu.h:296
Initialization of encoder_control_t.
struct encoder_state_config_slice_t encoder_state_config_slice_t
struct lcu_stats_t lcu_stats_t
int kvz_get_cu_ref_qp(const encoder_state_t *state, int x, int y, int last_qp)
Definition: encoderstate.c:1785
int kvz_encoder_state_match_children_of_previous_frame(encoder_state_t *const state)
Definition: encoderstate.c:65
encoder_state_type
Definition: encoderstate.h:56
@ ENCODER_STATE_TYPE_WAVEFRONT_ROW
Definition: encoderstate.h:61
@ ENCODER_STATE_TYPE_TILE
Definition: encoderstate.h:60
@ ENCODER_STATE_TYPE_MAIN
Definition: encoderstate.h:58
@ ENCODER_STATE_TYPE_INVALID
Definition: encoderstate.h:57
@ ENCODER_STATE_TYPE_SLICE
Definition: encoderstate.h:59
struct encoder_state_t encoder_state_t
struct lcu_order_element lcu_order_element_t
static bool is_last_cu_in_qg(const encoder_state_t *state, int x, int y, int depth)
Returns true if the CU is the last CU in its containing quantization group.
Definition: encoderstate.h:384
void kvz_encoder_create_ref_lists(const encoder_state_t *const state)
Generate reference picture lists.
Definition: encoderstate.c:1065
static const uint8_t g_group_idx[32]
Definition: encoderstate.h:397
struct encoder_state_config_frame_t encoder_state_config_frame_t
void kvz_encoder_prepare(encoder_state_t *state)
Prepare the encoder state for encoding the next frame.
Definition: encoderstate.c:1690
static bool encoder_state_must_write_vps(const encoder_state_t *state)
Whether the parameter sets should be written with the current frame.
Definition: encoderstate.h:364
struct encoder_state_config_tile_t encoder_state_config_tile_t
void kvz_encode_one_frame(encoder_state_t *const state, kvz_picture *frame)
Definition: encoderstate.c:1661
static const uint8_t g_min_in_group[10]
Definition: encoderstate.h:403
coeff_scan_order_t kvz_get_scan_order(int8_t cu_type, int intra_mode, int depth)
Definition: encoderstate.c:1761
lcu_stats_t * kvz_get_lcu_stats(encoder_state_t *state, int lcu_x, int lcu_y)
Definition: encoderstate.c:1777
struct encoder_state_config_wfrow_t encoder_state_config_wfrow_t
Header that is included in every other header.
#define INLINE
Definition: global.h:240
#define LCU_WIDTH
spec: CtbSizeY
Definition: global.h:153
A reference counted YUV pixel buffer.
Container for a list of reference pictures.
This file defines the public API of Kvazaar when used as a library.
kvz_slice_type
Definition: kvazaar.h:602
A stream of bits.
Definition: bitstream.h:50
Definition: cabac.h:54
Definition: encoder.h:49
kvz_config cfg
Configuration.
Definition: encoder.h:61
Definition: encoderstate.h:95
int8_t QP
Frame-level quantization parameter.
Definition: encoderstate.h:116
struct encoder_state_t const * previous_layer_state
Definition: encoderstate.h:176
double * aq_offsets
Calculated adaptive QP offset for each LCU.
Definition: encoderstate.h:181
double remaining_weight
Definition: encoderstate.h:190
double cur_gop_target_bits
Number of bits targeted for the current GOP.
Definition: encoderstate.h:143
double cur_pic_target_bits
Number of bits targeted for the current picture.
Definition: encoderstate.h:146
int32_t poc
Picture order count.
Definition: encoderstate.h:107
double rc_beta
Definition: encoderstate.h:150
uint8_t pictype
Definition: encoderstate.h:130
int8_t gop_offset
Offset in the gop structure.
Definition: encoderstate.h:108
lcu_stats_t * lcu_stats
Information about the coded LCUs.
Definition: encoderstate.h:170
double QP_factor
quantization factor
Definition: encoderstate.h:118
bool prepared
Indicates that this encoder state is ready for encoding the next frame i.e.
Definition: encoderstate.h:156
uint64_t cur_frame_bits_coded
Number of bits written in the current frame.
Definition: encoderstate.h:140
enum kvz_slice_type slicetype
Definition: encoderstate.h:131
int32_t num
Frame number.
Definition: encoderstate.h:106
int8_t ref_list
Definition: encoderstate.h:122
image_list_t * ref
Current pictures available for references.
Definition: encoderstate.h:121
pthread_mutex_t rc_lock
Definition: encoderstate.h:172
double lambda
Frame-level lambda.
Definition: encoderstate.h:104
struct kvz_rc_data * new_ratecontrol
Definition: encoderstate.h:174
uint64_t cur_gop_bits_coded
Number of bits written in the current GOP.
Definition: encoderstate.h:137
uint8_t ref_LX[2][16]
L0 and L1 reference index list.
Definition: encoderstate.h:125
uint64_t total_bits_coded
Total number of bits written.
Definition: encoderstate.h:134
double rc_alpha
Definition: encoderstate.h:149
double * k_para
Definition: encoderstate.h:194
bool done
Indicates that the previous frame has been encoded and the encoded data written and the encoding the ...
Definition: encoderstate.h:163
bool first_nal
Whether next NAL is the first NAL in the access unit.
Definition: encoderstate.h:188
double i_bits_left
Definition: encoderstate.h:191
double * c_para
Definition: encoderstate.h:193
int32_t irap_poc
POC of the associated IRAP picture.
Definition: encoderstate.h:109
double icost
Definition: encoderstate.h:189
bool is_irap
Definition: encoderstate.h:129
uint8_t ref_LX_size[2]
L0 reference index list size.
Definition: encoderstate.h:127
int8_t max_qp_delta_depth
Definition: encoderstate.h:183
Definition: encoderstate.h:238
int32_t end_in_rs
Definition: encoderstate.h:247
int32_t start_in_rs
Definition: encoderstate.h:246
int32_t start_in_ts
Definition: encoderstate.h:242
int32_t id
Definition: encoderstate.h:239
int32_t end_in_ts
Definition: encoderstate.h:243
Definition: encoderstate.h:197
int32_t lcu_offset_in_ts
Definition: encoderstate.h:212
yuv_t * hor_buf_before_sao
Definition: encoderstate.h:226
int32_t id
Definition: encoderstate.h:201
int32_t lcu_offset_x
Definition: encoderstate.h:204
yuv_t * ver_buf_search
Definition: encoderstate.h:221
int32_t lcu_offset_y
Definition: encoderstate.h:205
yuv_t * ver_buf_before_sao
Definition: encoderstate.h:231
threadqueue_job_t ** wf_jobs
Definition: encoderstate.h:234
yuv_t * hor_buf_search
Definition: encoderstate.h:217
int32_t offset_y
Definition: encoderstate.h:209
int32_t offset_x
Definition: encoderstate.h:208
videoframe_t * frame
Definition: encoderstate.h:199
Definition: encoderstate.h:250
int32_t lcu_offset_y
Definition: encoderstate.h:252
Definition: encoderstate.h:274
threadqueue_job_t * tqj_bitstream_written
Definition: encoderstate.h:337
int8_t qp
Quantization parameter for the current LCU.
Definition: encoderstate.h:310
lcu_order_element_t * lcu_order
Definition: encoderstate.h:292
double lambda_sqrt
Lambda for SAD and SATD.
Definition: encoderstate.h:308
bool must_code_qp_delta
Whether a QP delta value must be coded for the current LCU.
Definition: encoderstate.h:315
uint32_t crypto_prev_pos
Definition: encoderstate.h:301
bitstream_t stream
Definition: encoderstate.h:295
encoder_state_config_slice_t * slice
Definition: encoderstate.h:288
cabac_data_t cabac
Definition: encoderstate.h:296
lcu_coeff_t * coeff
Coeffs for the LCU.
Definition: encoderstate.h:333
uint32_t lcu_order_count
Definition: encoderstate.h:293
uint32_t stats_bitstream_length
Definition: encoderstate.h:303
cabac_data_t search_cabac
Definition: encoderstate.h:297
encoder_state_config_frame_t * frame
Definition: encoderstate.h:286
int8_t last_qp
QP value of the last CU in the last coded quantization group.
Definition: encoderstate.h:328
struct encoder_state_t * previous_encoder_state
Definition: encoderstate.h:284
crypto_handle_t * crypto_hdl
Definition: encoderstate.h:300
void * constraint
Definition: encoderstate.h:340
double lambda
Lambda for SSE.
Definition: encoderstate.h:306
encoder_state_config_wfrow_t * wfrow
Definition: encoderstate.h:289
threadqueue_job_t * tqj_recon_done
Definition: encoderstate.h:336
const encoder_control_t * encoder_control
Definition: encoderstate.h:275
int is_leaf
Definition: encoderstate.h:291
encoder_state_config_tile_t * tile
Definition: encoderstate.h:287
struct encoder_state_t * children
Definition: encoderstate.h:280
encoder_state_type type
Definition: encoderstate.h:276
struct encoder_state_t * parent
Definition: encoderstate.h:281
Struct which contains array of picture structs.
Definition: imagelist.h:50
int32_t vps_period
How often the VPS, SPS and PPS are re-sent.
Definition: kvazaar.h:300
Struct which contains all picture data.
Definition: kvazaar.h:504
Definition: rate_control.h:46
Definition: encoderstate.h:255
int last_row
Definition: encoderstate.h:266
int first_row
Definition: encoderstate.h:264
struct lcu_order_element * below
Definition: encoderstate.h:269
struct encoder_state_t * encoder_state
Definition: encoderstate.h:259
struct lcu_order_element * right
Definition: encoderstate.h:271
struct lcu_order_element * left
Definition: encoderstate.h:270
vector2d_t size
Definition: encoderstate.h:262
int first_column
Definition: encoderstate.h:263
vector2d_t position
Definition: encoderstate.h:260
int index
Definition: encoderstate.h:258
int last_column
Definition: encoderstate.h:265
vector2d_t position_px
Definition: encoderstate.h:261
struct lcu_order_element * above
Definition: encoderstate.h:268
int id
Definition: encoderstate.h:257
Definition: encoderstate.h:65
double original_weight
Definition: encoderstate.h:74
double weight
Weight of the LCU for rate control.
Definition: encoderstate.h:72
double rc_alpha
Rate control alpha parameter.
Definition: encoderstate.h:82
int8_t qp
Definition: encoderstate.h:89
double lambda
Lambda value which was used for this LCU.
Definition: encoderstate.h:77
int i_cost
Definition: encoderstate.h:87
uint32_t bits
Number of bits that were spent.
Definition: encoderstate.h:67
double distortion
Definition: encoderstate.h:86
double adjust_lambda
Definition: encoderstate.h:79
int8_t adjust_qp
Definition: encoderstate.h:90
uint8_t skipped
Definition: encoderstate.h:91
uint32_t pixels
Definition: encoderstate.h:69
double rc_beta
Rate control beta parameter.
Definition: encoderstate.h:85
Definition: threadqueue.c:127
Definition: cu.h:121
Struct which contains all picture data.
Definition: videoframe.h:50
int32_t width
Luma pixel array width.
Definition: videoframe.h:54
int32_t height
Luma pixel array height.
Definition: videoframe.h:55
Definition: image.h:54
Various tables.
coeff_scan_order_t
Definition: tables.h:93
Container for worker tasks.
Container for the frame currently being encoded.