AOMedia AV1 Codec
tpl_model.h
1 /*
2  * Copyright (c) 2019, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #ifndef AOM_AV1_ENCODER_TPL_MODEL_H_
13 #define AOM_AV1_ENCODER_TPL_MODEL_H_
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
21 struct AV1_PRIMARY;
22 struct AV1_COMP;
23 struct AV1_SEQ_CODING_TOOLS;
24 struct EncodeFrameParams;
25 struct EncodeFrameInput;
26 struct GF_GROUP;
27 
28 #include "config/aom_config.h"
29 
30 #include "aom_scale/yv12config.h"
31 
32 #include "av1/common/mv.h"
33 #include "av1/common/scale.h"
34 #include "av1/encoder/block.h"
35 #include "av1/encoder/lookahead.h"
36 #include "av1/encoder/ratectrl.h"
37 
38 static INLINE BLOCK_SIZE convert_length_to_bsize(int length) {
39  switch (length) {
40  case 64: return BLOCK_64X64;
41  case 32: return BLOCK_32X32;
42  case 16: return BLOCK_16X16;
43  case 8: return BLOCK_8X8;
44  case 4: return BLOCK_4X4;
45  default:
46  assert(0 && "Invalid block size for tpl model");
47  return BLOCK_16X16;
48  }
49 }
50 
51 typedef struct AV1TplRowMultiThreadSync {
52 #if CONFIG_MULTITHREAD
53  // Synchronization objects for top-right dependency.
54  pthread_mutex_t *mutex_;
55  pthread_cond_t *cond_;
56 #endif
57  // Buffer to store the macroblock whose encoding is complete.
58  // num_finished_cols[i] stores the number of macroblocks which finished
59  // encoding in the ith macroblock row.
60  int *num_finished_cols;
61  // Number of extra macroblocks of the top row to be complete for encoding
62  // of the current macroblock to start. A value of 1 indicates top-right
63  // dependency.
64  int sync_range;
65  // Number of macroblock rows.
66  int rows;
67  // Number of threads processing the current tile.
68  int num_threads_working;
69 } AV1TplRowMultiThreadSync;
70 
71 typedef struct AV1TplRowMultiThreadInfo {
72  // Row synchronization related function pointers.
73  void (*sync_read_ptr)(AV1TplRowMultiThreadSync *tpl_mt_sync, int r, int c);
74  void (*sync_write_ptr)(AV1TplRowMultiThreadSync *tpl_mt_sync, int r, int c,
75  int cols);
76 } AV1TplRowMultiThreadInfo;
77 
78 // TODO(jingning): This needs to be cleaned up next.
79 
80 // TPL stats buffers are prepared for every frame in the GOP,
81 // including (internal) overlays and (internal) arfs.
82 // In addition, frames in the lookahead that are outside of the GOP
83 // are also used.
84 // Thus it should use
85 // (gop_length) + (# overlays) + (MAX_LAG_BUFFERS - gop_len) =
86 // MAX_LAG_BUFFERS + (# overlays)
87 // 2 * MAX_LAG_BUFFERS is therefore a safe estimate.
88 // TODO(bohanli): test setting it to 1.5 * MAX_LAG_BUFFER
89 #define MAX_TPL_FRAME_IDX (2 * MAX_LAG_BUFFERS)
90 // The first REF_FRAMES + 1 buffers are reserved.
91 // tpl_data->tpl_frame starts after REF_FRAMES + 1
92 #define MAX_LENGTH_TPL_FRAME_STATS (MAX_TPL_FRAME_IDX + REF_FRAMES + 1)
93 #define TPL_DEP_COST_SCALE_LOG2 4
94 
95 #define TPL_EPSILON 0.0000001
96 
97 typedef struct TplTxfmStats {
98  double abs_coeff_sum[256]; // Assume we are using 16x16 transform block
99  int txfm_block_count;
100  int coeff_num;
101 } TplTxfmStats;
102 
103 typedef struct TplDepStats {
104  int64_t intra_cost;
105  int64_t inter_cost;
106  int64_t srcrf_dist;
107  int64_t recrf_dist;
108  int64_t cmp_recrf_dist[2];
109  int64_t srcrf_rate;
110  int64_t recrf_rate;
111  int64_t srcrf_sse;
112  int64_t cmp_recrf_rate[2];
113  int64_t mc_dep_rate;
114  int64_t mc_dep_dist;
115  int_mv mv[INTER_REFS_PER_FRAME];
116  int ref_frame_index[2];
117  int64_t pred_error[INTER_REFS_PER_FRAME];
118 } TplDepStats;
119 
120 typedef struct TplDepFrame {
121  uint8_t is_valid;
122  TplDepStats *tpl_stats_ptr;
123  const YV12_BUFFER_CONFIG *gf_picture;
124  YV12_BUFFER_CONFIG *rec_picture;
125  int ref_map_index[REF_FRAMES];
126  int stride;
127  int width;
128  int height;
129  int mi_rows;
130  int mi_cols;
131  int base_rdmult;
132  uint32_t frame_display_index;
133 } TplDepFrame;
134 
139 typedef struct TplParams {
143  int ready;
144 
149 
153  uint8_t tpl_bsize_1d;
154 
160  TplDepFrame tpl_stats_buffer[MAX_LENGTH_TPL_FRAME_STATS];
161 
167  TplDepStats *tpl_stats_pool[MAX_LAG_BUFFERS];
168 
173  TplTxfmStats txfm_stats_list[MAX_LENGTH_TPL_FRAME_STATS];
174 
180 
184  TplDepFrame *tpl_frame;
185 
189  struct scale_factors sf;
190 
195 
201  const YV12_BUFFER_CONFIG *src_ref_frame[INTER_REFS_PER_FRAME];
202 
208  const YV12_BUFFER_CONFIG *ref_frame[INTER_REFS_PER_FRAME];
209 
214  AV1TplRowMultiThreadSync tpl_mt_sync;
215 
220 
221 #if CONFIG_BITRATE_ACCURACY
222  /*
223  * Estimated and actual GOP bitrate.
224  */
225  double estimated_gop_bitrate;
226  double actual_gop_bitrate;
227 #endif
228 } TplParams;
229 
230 #if CONFIG_BITRATE_ACCURACY
231 
235 typedef struct {
236  double keyframe_bitrate;
237  double total_bit_budget; // The total bit budget of the entire video
238  int show_frame_count; // Number of show frames in the entire video
239 
240  int gop_showframe_count; // The number of show frames in the current gop
241  double gop_bit_budget; // The bitbudget for the current gop
242  double scale_factors[FRAME_UPDATE_TYPES]; // Scale factors to improve the
243  // budget estimation
244  double mv_scale_factors[FRAME_UPDATE_TYPES]; // Scale factors to improve
245  // MV entropy estimation
246 
247  // === Below this line are GOP related data that will be updated per GOP ===
248  int q_index_list_ready;
249  int q_index_list[MAX_LENGTH_TPL_FRAME_STATS]; // q indices for the current
250  // GOP
251  // Arrays to store frame level bitrate accuracy data.
252  double estimated_bitrate_byframe[MAX_LENGTH_TPL_FRAME_STATS];
253  double estimated_mv_bitrate_byframe[MAX_LENGTH_TPL_FRAME_STATS];
254  int actual_bitrate_byframe[MAX_LENGTH_TPL_FRAME_STATS];
255  int actual_mv_bitrate_byframe[MAX_LENGTH_TPL_FRAME_STATS];
256  int actual_coeff_bitrate_byframe[MAX_LENGTH_TPL_FRAME_STATS];
257 } VBR_RATECTRL_INFO;
258 
259 static INLINE void vbr_rc_reset_gop_data(VBR_RATECTRL_INFO *vbr_rc_info) {
260  vbr_rc_info->q_index_list_ready = 0;
261  av1_zero(vbr_rc_info->q_index_list);
262  av1_zero(vbr_rc_info->estimated_bitrate_byframe);
263  av1_zero(vbr_rc_info->estimated_mv_bitrate_byframe);
264  av1_zero(vbr_rc_info->actual_bitrate_byframe);
265  av1_zero(vbr_rc_info->actual_mv_bitrate_byframe);
266  av1_zero(vbr_rc_info->actual_coeff_bitrate_byframe);
267 }
268 
269 static INLINE void vbr_rc_init(VBR_RATECTRL_INFO *vbr_rc_info,
270  double total_bit_budget, int show_frame_count) {
271  vbr_rc_info->total_bit_budget = total_bit_budget;
272  vbr_rc_info->show_frame_count = show_frame_count;
273  vbr_rc_info->keyframe_bitrate = 0;
274  const double scale_factors[FRAME_UPDATE_TYPES] = { 1.2, 1.2, 1.2, 1.2,
275  1.2, 1.2, 1.2 };
276  const double mv_scale_factors[FRAME_UPDATE_TYPES] = { 5.0, 5.0, 5.0, 5.0,
277  5.0, 5.0, 5.0 };
278  memcpy(vbr_rc_info->scale_factors, scale_factors,
279  sizeof(scale_factors[0]) * FRAME_UPDATE_TYPES);
280  memcpy(vbr_rc_info->mv_scale_factors, mv_scale_factors,
281  sizeof(mv_scale_factors[0]) * FRAME_UPDATE_TYPES);
282 
283  vbr_rc_reset_gop_data(vbr_rc_info);
284 }
285 
286 static INLINE void vbr_rc_set_gop_bit_budget(VBR_RATECTRL_INFO *vbr_rc_info,
287  int gop_showframe_count) {
288  vbr_rc_info->gop_showframe_count = gop_showframe_count;
289  vbr_rc_info->gop_bit_budget = vbr_rc_info->total_bit_budget *
290  gop_showframe_count /
291  vbr_rc_info->show_frame_count;
292 }
293 
294 static INLINE void vbr_rc_set_keyframe_bitrate(VBR_RATECTRL_INFO *vbr_rc_info,
295  double keyframe_bitrate) {
296  vbr_rc_info->keyframe_bitrate = keyframe_bitrate;
297 }
298 
299 static INLINE void vbr_rc_info_log(const VBR_RATECTRL_INFO *vbr_rc_info,
300  int gf_frame_index, int gf_group_size,
301  int *update_type) {
302  // Add +2 here because this is the last frame this method is called at.
303  if (gf_frame_index + 2 >= gf_group_size) {
304  printf(
305  "\ni, \test_bitrate, \test_mv_bitrate, \tact_bitrate, "
306  "\tact_mv_bitrate, \tact_coeff_bitrate, \tq, \tupdate_type\n");
307  for (int i = 0; i < gf_group_size; i++) {
308  printf("%d, \t%f, \t%f, \t%d, \t%d, \t%d, \t%d, \t%d\n", i,
309  vbr_rc_info->estimated_bitrate_byframe[i],
310  vbr_rc_info->estimated_mv_bitrate_byframe[i],
311  vbr_rc_info->actual_bitrate_byframe[i],
312  vbr_rc_info->actual_mv_bitrate_byframe[i],
313  vbr_rc_info->actual_coeff_bitrate_byframe[i],
314  vbr_rc_info->q_index_list[i], update_type[i]);
315  }
316  }
317 }
318 
319 #endif // CONFIG_BITRATE_ACCURACY
320 
321 #if CONFIG_RD_COMMAND
322 typedef enum {
323  RD_OPTION_NONE,
324  RD_OPTION_SET_Q,
325  RD_OPTION_SET_Q_RDMULT
326 } RD_OPTION;
327 
328 typedef struct RD_COMMAND {
329  RD_OPTION option_ls[MAX_LENGTH_TPL_FRAME_STATS];
330  int q_index_ls[MAX_LENGTH_TPL_FRAME_STATS];
331  int rdmult_ls[MAX_LENGTH_TPL_FRAME_STATS];
332  int frame_count;
333  int frame_index;
334 } RD_COMMAND;
335 
336 void av1_read_rd_command(const char *filepath, RD_COMMAND *rd_command);
337 #endif // CONFIG_RD_COMMAND
338 
347 void av1_setup_tpl_buffers(struct AV1_PRIMARY *const ppi,
348  CommonModeInfoParams *const mi_params, int width,
349  int height, int byte_alignment, int lag_in_frames);
350 
363 int av1_tpl_setup_stats(struct AV1_COMP *cpi, int gop_eval,
364  const struct EncodeFrameParams *const frame_params,
365  const struct EncodeFrameInput *const frame_input);
366 
369 void av1_tpl_preload_rc_estimate(
370  struct AV1_COMP *cpi, const struct EncodeFrameParams *const frame_params);
371 
372 int av1_tpl_ptr_pos(int mi_row, int mi_col, int stride, uint8_t right_shift);
373 
374 void av1_init_tpl_stats(TplParams *const tpl_data);
375 
376 int av1_tpl_stats_ready(const TplParams *tpl_data, int gf_frame_index);
377 
378 void av1_tpl_rdmult_setup(struct AV1_COMP *cpi);
379 
380 void av1_tpl_rdmult_setup_sb(struct AV1_COMP *cpi, MACROBLOCK *const x,
381  BLOCK_SIZE sb_size, int mi_row, int mi_col);
382 
383 void av1_mc_flow_dispenser_row(struct AV1_COMP *cpi,
384  TplTxfmStats *tpl_txfm_stats, MACROBLOCK *x,
385  int mi_row, BLOCK_SIZE bsize, TX_SIZE tx_size);
386 
399 double av1_exponential_entropy(double q_step, double b);
400 
414 double av1_laplace_entropy(double q_step, double b, double zero_bin_ratio);
415 
433 double av1_laplace_estimate_frame_rate(int q_index, int block_count,
434  const double *abs_coeff_mean,
435  int coeff_num);
436 
437 /*
438  *!\brief Compute the number of bits needed to encode a GOP
439  *
440  * \param[in] q_index_list array of q_index, one per frame
441  * \param[in] frame_count number of frames in the GOP
442  * \param[in] stats array of transform stats, one per frame
443  * \param[in] stats_valid_list List indicates whether transform stats
444  * exists
445  * \param[out] bitrate_byframe_list Array to keep track of frame bitrate
446  *
447  * \return The estimated GOP bitrate.
448  *
449  */
450 double av1_estimate_gop_bitrate(const int *q_index_list, const int frame_count,
451  const TplTxfmStats *stats,
452  const int *stats_valid_list,
453  double *bitrate_byframe_list);
454 
455 /*
456  *!\brief Init TplTxfmStats
457  *
458  * \param[in] tpl_txfm_stats a structure for storing transform stats
459  *
460  */
461 void av1_init_tpl_txfm_stats(TplTxfmStats *tpl_txfm_stats);
462 
463 /*
464  *!\brief Accumulate TplTxfmStats
465  *
466  * \param[in] sub_stats a structure for storing sub transform stats
467  * \param[out] accumulated_stats a structure for storing accumulated transform
468  *stats
469  *
470  */
471 void av1_accumulate_tpl_txfm_stats(const TplTxfmStats *sub_stats,
472  TplTxfmStats *accumulated_stats);
473 
474 /*
475  *!\brief Record a transform block into TplTxfmStats
476  *
477  * \param[in] tpl_txfm_stats A structure for storing transform stats
478  * \param[out] coeff An array of transform coefficients. Its size
479  * should equal to tpl_txfm_stats.coeff_num.
480  *
481  */
482 void av1_record_tpl_txfm_block(TplTxfmStats *tpl_txfm_stats,
483  const tran_low_t *coeff);
484 
500 double av1_estimate_coeff_entropy(double q_step, double b,
501  double zero_bin_ratio, int qcoeff);
502 
515 double av1_estimate_txfm_block_entropy(int q_index,
516  const double *abs_coeff_mean,
517  int *qcoeff_arr, int coeff_num);
518 
519 // TODO(angiebird): Add doxygen description here.
520 int64_t av1_delta_rate_cost(int64_t delta_rate, int64_t recrf_dist,
521  int64_t srcrf_dist, int pix_num);
522 
538 int av1_get_overlap_area(int row_a, int col_a, int row_b, int col_b, int width,
539  int height);
540 
561 int av1_q_mode_estimate_base_q(const struct GF_GROUP *gf_group,
562  const TplTxfmStats *txfm_stats_list,
563  const int *stats_valid_list, double bit_budget,
564  int gf_frame_index, double arf_qstep_ratio,
565  aom_bit_depth_t bit_depth, double scale_factor,
566  int *q_index_list,
567  double *estimated_bitrate_byframe);
568 
578 int av1_tpl_get_q_index(const TplParams *tpl_data, int gf_frame_index,
579  int leaf_qindex, aom_bit_depth_t bit_depth);
580 
591 double av1_tpl_get_qstep_ratio(const TplParams *tpl_data, int gf_frame_index);
592 
602 int av1_get_q_index_from_qstep_ratio(int leaf_qindex, double qstep_ratio,
603  aom_bit_depth_t bit_depth);
604 
605 #if CONFIG_BITRATE_ACCURACY
606 
615 void av1_vbr_rc_update_q_index_list(VBR_RATECTRL_INFO *vbr_rc_info,
616  const TplParams *tpl_data,
617  const struct GF_GROUP *gf_group,
618  int gf_frame_index,
619  aom_bit_depth_t bit_depth);
620 
631 double av1_tpl_compute_mv_bits(const TplParams *tpl_data, int gf_group_size,
632  int gf_frame_index, int gf_update_type,
633  VBR_RATECTRL_INFO *vbr_rc_info);
634 #endif // CONFIG_BITRATE_ACCURACY
635 
648 int_mv av1_compute_mv_difference(const TplDepFrame *tpl_frame, int row, int col,
649  int step, int tpl_stride, int right_shift);
650 
658 double av1_tpl_compute_frame_mv_entropy(const TplDepFrame *tpl_frame,
659  uint8_t right_shift);
660 
662 #ifdef __cplusplus
663 } // extern "C"
664 #endif
665 
666 #endif // AOM_AV1_ENCODER_TPL_MODEL_H_
int border_in_pixels
Definition: tpl_model.h:219
uint8_t tpl_bsize_1d
Definition: tpl_model.h:153
TplTxfmStats txfm_stats_list[MAX_LENGTH_TPL_FRAME_STATS]
Definition: tpl_model.h:173
int ready
Definition: tpl_model.h:143
Data related to the current GF/ARF group and the individual frames within the group.
Definition: firstpass.h:344
Params related to temporal dependency model.
Definition: tpl_model.h:139
Top level primary encoder structure.
Definition: encoder.h:2290
const YV12_BUFFER_CONFIG * ref_frame[INTER_REFS_PER_FRAME]
Definition: tpl_model.h:208
int av1_tpl_setup_stats(struct AV1_COMP *cpi, int gop_eval, const struct EncodeFrameParams *const frame_params, const struct EncodeFrameInput *const frame_input)
Implements temporal dependency modelling for a GOP (GF/ARF group) and selects between 16 and 32 frame...
contains per-frame encoding parameters decided upon by av1_encode_strategy() and passed down to av1_e...
Definition: encoder.h:3200
Params related to MB_MODE_INFO arrays and related info.
Definition: av1_common_int.h:505
TplDepFrame tpl_stats_buffer[MAX_LENGTH_TPL_FRAME_STATS]
Definition: tpl_model.h:160
AV1TplRowMultiThreadSync tpl_mt_sync
Definition: tpl_model.h:214
struct scale_factors sf
Definition: tpl_model.h:189
TplDepStats * tpl_stats_pool[MAX_LAG_BUFFERS]
Definition: tpl_model.h:167
YV12_BUFFER_CONFIG tpl_rec_pool[MAX_LAG_BUFFERS]
Definition: tpl_model.h:179
Describes look ahead buffer operations.
YV12 frame buffer data structure.
Definition: yv12config.h:38
Input frames and last input frame.
Definition: encoder.h:3188
int frame_idx
Definition: tpl_model.h:194
Top level encoder structure.
Definition: encoder.h:2557
enum aom_bit_depth aom_bit_depth_t
Bit depth for codecThis enumeration determines the bit depth of the codec.
Encoder's parameters related to the current coding block.
Definition: block.h:854
TplDepFrame * tpl_frame
Definition: tpl_model.h:184
uint8_t tpl_stats_block_mis_log2
Definition: tpl_model.h:148
const YV12_BUFFER_CONFIG * src_ref_frame[INTER_REFS_PER_FRAME]
Definition: tpl_model.h:201