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 
35 /**
36  * \ingroup Control
37  * \file
38  * Top level of the encoder implementation.
39  */
40 
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 
54 struct kvz_rc_data;
55 
56 typedef enum {
57   ENCODER_STATE_TYPE_INVALID = 'i',
58   ENCODER_STATE_TYPE_MAIN = 'M',
59   ENCODER_STATE_TYPE_SLICE = 'S',
60   ENCODER_STATE_TYPE_TILE = 'T',
61   ENCODER_STATE_TYPE_WAVEFRONT_ROW = 'W',
62 } encoder_state_type;
63 
64 
65 typedef struct lcu_stats_t {
66   //! \brief Number of bits that were spent
67   uint32_t bits;
68 
69   uint32_t pixels;
70 
71   //! \brief Weight of the LCU for rate control
72   double weight;
73 
74   double original_weight;
75 
76   //! \brief Lambda value which was used for this LCU
77   double lambda;
78 
79   double adjust_lambda;
80 
81   //! \brief Rate control alpha parameter
82   double rc_alpha;
83 
84   //! \brief Rate control beta parameter
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;
92 } lcu_stats_t;
93 
94 
95 typedef struct encoder_state_config_frame_t {
96   /**
97    * \brief Frame-level lambda.
98    *
99    * Use state->lambda or state->lambda_sqrt for cost computations.
100    *
101    * \see encoder_state_t::lambda
102    * \see encoder_state_t::lambda_sqrt
103    */
104   double lambda;
105 
106   int32_t num;       /*!< \brief Frame number */
107   int32_t poc;       /*!< \brief Picture order count */
108   int8_t gop_offset; /*!< \brief Offset in the gop structure */
109   int32_t irap_poc;  /*!< \brief POC of the associated IRAP picture */
110 
111   /**
112    * \brief Frame-level quantization parameter
113    *
114    * \see encoder_state_t::qp
115    */
116   int8_t QP;
117   //! \brief quantization factor
118   double QP_factor;
119 
120   //! Current pictures available for references
121   image_list_t *ref;
122   int8_t ref_list;
123 
124   //! L0 and L1 reference index list
125   uint8_t ref_LX[2][16];
126   //! L0 reference index list size
127   uint8_t ref_LX_size[2];
128 
129   bool is_irap;
130   uint8_t pictype;
131   enum kvz_slice_type slicetype;
132 
133   //! Total number of bits written.
134   uint64_t total_bits_coded;
135 
136   //! Number of bits written in the current GOP.
137   uint64_t cur_gop_bits_coded;
138 
139   //! Number of bits written in the current frame.
140   uint64_t cur_frame_bits_coded;
141 
142   //! Number of bits targeted for the current GOP.
143   double cur_gop_target_bits;
144 
145   //! Number of bits targeted for the current picture.
146   double cur_pic_target_bits;
147 
148   // Parameters used in rate control
149   double rc_alpha;
150   double rc_beta;
151 
152   /**
153    * \brief Indicates that this encoder state is ready for encoding the
154    * next frame i.e. kvz_encoder_prepare has been called.
155    */
156   bool prepared;
157 
158   /**
159    * \brief Indicates that the previous frame has been encoded and the
160    * encoded data written and the encoding the next frame has not been
161    * started yet.
162    */
163   bool done;
164 
165   /**
166    * \brief Information about the coded LCUs.
167    *
168    * Used for rate control.
169    */
170   lcu_stats_t *lcu_stats;
171 
172   pthread_mutex_t rc_lock;
173 
174   struct kvz_rc_data *new_ratecontrol;
175 
176   struct encoder_state_t const *previous_layer_state;
177 
178   /**
179   * \brief Calculated adaptive QP offset for each LCU.
180   */
181   double *aq_offsets;
182 
183   /**
184    * \brief Whether next NAL is the first NAL in the access unit.
185    */
186   bool first_nal;
187   double icost;
188   double remaining_weight;
189   double i_bits_left;
190 
191   double *c_para;
192   double *k_para;
193 } encoder_state_config_frame_t;
194 
195 typedef struct encoder_state_config_tile_t {
196   //Current sub-frame
197   videoframe_t *frame;
198 
199   int32_t id;
200 
201   //Tile: offset in LCU for current encoder_state in global coordinates
202   int32_t lcu_offset_x;
203   int32_t lcu_offset_y;
204 
205   //Tile: offset in pixels
206   int32_t offset_x;
207   int32_t offset_y;
208 
209   //Position of the first element in tile scan in global coordinates
210   int32_t lcu_offset_in_ts;
211 
212   // This is a buffer for the non-loopfiltered bottom pixels of every LCU-row
213   // in the tile. They are packed such that each LCU-row index maps to the
214   // y-coordinate.
215   yuv_t *hor_buf_search;
216   // This is a buffer for the non-loopfiltered rightmost pixels of every
217   // LCU-column. They are packed such that each LCU-column index maps to the
218   // x-coordinate.
219   yuv_t *ver_buf_search;
220 
221   // This is a buffer for the deblocked bottom pixels of every LCU in the
222   // tile. They are packed such that each LCU-row index maps to the
223   // y-coordinate.
224   yuv_t *hor_buf_before_sao;
225 
226   // This is a buffer for the deblocked right pixels of every LCU in the
227   // tile. They are packed such that each LCU-column index maps to the
228   // x-coordinate.
229   yuv_t *ver_buf_before_sao;
230 
231   //Jobs for each individual LCU of a wavefront row.
232   threadqueue_job_t **wf_jobs;
233 
234 } encoder_state_config_tile_t;
235 
236 typedef struct encoder_state_config_slice_t {
237   int32_t id;
238 
239   //Global coordinates
240   int32_t start_in_ts;
241   int32_t end_in_ts;
242 
243   //Global coordinates
244   int32_t start_in_rs;
245   int32_t end_in_rs;
246 } encoder_state_config_slice_t;
247 
248 typedef struct encoder_state_config_wfrow_t {
249   //Row in tile coordinates of the wavefront
250   int32_t lcu_offset_y;
251 } encoder_state_config_wfrow_t;
252 
253 typedef struct lcu_order_element {
254   //This it used for leaf of the encoding tree. All is relative to the tile.
255   int id;
256   int index;
257   struct encoder_state_t *encoder_state;
258   vector2d_t position;
259   vector2d_t position_px; //Top-left
260   vector2d_t size;
261   int first_column;
262   int first_row;
263   int last_column;
264   int last_row;
265 
266   struct lcu_order_element *above;
267   struct lcu_order_element *below;
268   struct lcu_order_element *left;
269   struct lcu_order_element *right;
270 } lcu_order_element_t;
271 
272 typedef struct encoder_state_t {
273   const encoder_control_t *encoder_control;
274   encoder_state_type type;
275 
276   //List of children, the last item of this list is a pseudo-encoder with encoder_control = NULL
277   //Use for (i = 0; encoder_state->children[i].encoder_control; ++i) {
278   struct encoder_state_t *children;
279   struct encoder_state_t *parent;
280 
281   //Pointer to the encoder_state of the previous frame
282   struct encoder_state_t *previous_encoder_state;
283 
284   encoder_state_config_frame_t  *frame;
285   encoder_state_config_tile_t   *tile;
286   encoder_state_config_slice_t  *slice;
287   encoder_state_config_wfrow_t  *wfrow;
288 
289   int is_leaf; //A leaf encoder state is one which should encode LCUs...
290   lcu_order_element_t *lcu_order;
291   uint32_t lcu_order_count;
292 
293   bitstream_t stream;
294   cabac_data_t cabac;
295 
296   // Crypto stuff
297   crypto_handle_t *crypto_hdl;
298   uint32_t crypto_prev_pos;
299 
300   uint32_t stats_bitstream_length; //Bitstream length written in bytes
301 
302   //! \brief Lambda for SSE
303   double lambda;
304   //! \brief Lambda for SAD and SATD
305   double lambda_sqrt;
306   //! \brief Quantization parameter for the current LCU
307   int8_t qp;
308 
309   /**
310    * \brief Whether a QP delta value must be coded for the current LCU.
311    */
312   bool must_code_qp_delta;
313 
314   /**
315    * \brief QP value of the last CU in the last coded quantization group.
316    *
317    * A quantization group is a square of width
318    * (LCU_WIDTH >> encoder_control->max_qp_delta_depth). All CUs of in the
319    * same quantization group share the QP predictor value, but may have
320    * different QP values.
321    *
322    * Set to the frame QP at the beginning of a wavefront row or a tile and
323    * updated when the last CU of a quantization group is coded.
324    */
325   int8_t last_qp;
326 
327   /**
328    * \brief Coeffs for the LCU.
329    */
330   lcu_coeff_t *coeff;
331 
332   //Jobs to wait for
333   threadqueue_job_t * tqj_recon_done; //Reconstruction is done
334   threadqueue_job_t * tqj_bitstream_written; //Bitstream is written
335 
336   //Constraint structure
337   void * constraint;
338 
339 
340 } encoder_state_t;
341 
342 void kvz_encode_one_frame(encoder_state_t * const state, kvz_picture* frame);
343 
344 void kvz_encoder_prepare(encoder_state_t *state);
345 
346 
347 int kvz_encoder_state_match_children_of_previous_frame(encoder_state_t * const state);
348 
349 coeff_scan_order_t kvz_get_scan_order(int8_t cu_type, int intra_mode, int depth);
350 
351 void kvz_encoder_create_ref_lists(const encoder_state_t *const state);
352 
353 lcu_stats_t* kvz_get_lcu_stats(encoder_state_t *state, int lcu_x, int lcu_y);
354 
355 
356 int kvz_get_cu_ref_qp(const encoder_state_t *state, int x, int y, int last_qp);
357 
358 /**
359  * Whether the parameter sets should be written with the current frame.
360  */
encoder_state_must_write_vps(const encoder_state_t * state)361 static INLINE bool encoder_state_must_write_vps(const encoder_state_t *state)
362 {
363   const int32_t frame = state->frame->num;
364   const int32_t vps_period = state->encoder_control->cfg.vps_period;
365 
366   return (vps_period >  0 && frame % vps_period == 0) ||
367          (vps_period >= 0 && frame == 0);
368 }
369 
370 
371 /**
372  * \brief Returns true if the CU is the last CU in its containing
373  * quantization group.
374  *
375  * \param state   encoder state
376  * \param x       x-coordinate of the left edge of the CU
377  * \param y       y-cooradinate of the top edge of the CU
378  * \param depth   depth in the CU tree
379  * \return true, if it's the last CU in its QG, otherwise false
380  */
is_last_cu_in_qg(const encoder_state_t * state,int x,int y,int depth)381 static INLINE bool is_last_cu_in_qg(const encoder_state_t *state, int x, int y, int depth)
382 {
383   if (state->encoder_control->max_qp_delta_depth < 0) return false;
384 
385   const int cu_width = LCU_WIDTH >> depth;
386   const int qg_width = LCU_WIDTH >> state->encoder_control->max_qp_delta_depth;
387   const int right  = x + cu_width;
388   const int bottom = y + cu_width;
389   return (right % qg_width == 0 || right >= state->tile->frame->width) &&
390          (bottom % qg_width == 0 || bottom >= state->tile->frame->height);
391 }
392 
393 
394 static const uint8_t g_group_idx[32] = {
395   0, 1, 2, 3, 4, 4, 5, 5, 6, 6,
396   6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
397   8, 8, 8, 8, 9, 9, 9, 9, 9, 9,
398   9, 9 };
399 
400 static const uint8_t g_min_in_group[10] = {
401   0, 1, 2, 3, 4, 6, 8, 12, 16, 24 };
402 
403 
404 #define C1FLAG_NUMBER 8 // maximum number of largerThan1 flag coded in one chunk
405 #define C2FLAG_NUMBER 1 // maximum number of largerThan2 flag coded in one chunk
406 
407 //Get the data for vertical buffer position at the left of LCU identified by the position in pixel
408 #define OFFSET_VER_BUF(position_x, position_y, cur_pic, i) ((position_y) + i + ((position_x)/LCU_WIDTH - 1) * (cur_pic)->height)
409 #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)
410 
411 //Get the data for horizontal buffer position at the top of LCU identified by the position in pixel
412 #define OFFSET_HOR_BUF(position_x, position_y, cur_pic, i) ((position_x) + i + ((position_y)/LCU_WIDTH - 1) * (cur_pic)->width)
413 #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)
414 
415 /** @} */
416 
417 #endif //ENCODERSTATE_H_
418