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_COMP;
22 struct EncodeFrameParams;
23 struct EncodeFrameInput;
24 
25 #include "av1/encoder/encoder.h"
26 
27 static INLINE BLOCK_SIZE convert_length_to_bsize(int length) {
28  switch (length) {
29  case 64: return BLOCK_64X64;
30  case 32: return BLOCK_32X32;
31  case 16: return BLOCK_16X16;
32  case 8: return BLOCK_8X8;
33  case 4: return BLOCK_4X4;
34  default:
35  assert(0 && "Invalid block size for tpl model");
36  return BLOCK_16X16;
37  }
38 }
39 
40 typedef struct AV1TplRowMultiThreadSync {
41 #if CONFIG_MULTITHREAD
42  // Synchronization objects for top-right dependency.
43  pthread_mutex_t *mutex_;
44  pthread_cond_t *cond_;
45 #endif
46  // Buffer to store the macroblock whose encoding is complete.
47  // num_finished_cols[i] stores the number of macroblocks which finished
48  // encoding in the ith macroblock row.
49  int *num_finished_cols;
50  // Number of extra macroblocks of the top row to be complete for encoding
51  // of the current macroblock to start. A value of 1 indicates top-right
52  // dependency.
53  int sync_range;
54  // Number of macroblock rows.
55  int rows;
56  // Number of threads processing the current tile.
57  int num_threads_working;
58 } AV1TplRowMultiThreadSync;
59 
60 typedef struct AV1TplRowMultiThreadInfo {
61  // Row synchronization related function pointers.
62  void (*sync_read_ptr)(AV1TplRowMultiThreadSync *tpl_mt_sync, int r, int c);
63  void (*sync_write_ptr)(AV1TplRowMultiThreadSync *tpl_mt_sync, int r, int c,
64  int cols);
65 } AV1TplRowMultiThreadInfo;
66 
67 // TODO(jingning): This needs to be cleaned up next.
68 
69 // TPL stats buffers are prepared for every frame in the GOP,
70 // including (internal) overlays and (internal) arfs.
71 // In addition, frames in the lookahead that are outside of the GOP
72 // are also used.
73 // Thus it should use
74 // (gop_length) + (# overlays) + (MAX_LAG_BUFFERS - gop_len) =
75 // MAX_LAG_BUFFERS + (# overlays)
76 // 2 * MAX_LAG_BUFFERS is therefore a safe estimate.
77 // TODO(bohanli): test setting it to 1.5 * MAX_LAG_BUFFER
78 #define MAX_TPL_FRAME_IDX (2 * MAX_LAG_BUFFERS)
79 // The first REF_FRAMES + 1 buffers are reserved.
80 // tpl_data->tpl_frame starts after REF_FRAMES + 1
81 #define MAX_LENGTH_TPL_FRAME_STATS (MAX_TPL_FRAME_IDX + REF_FRAMES + 1)
82 #define MAX_TPL_EXTEND (MAX_LAG_BUFFERS - MAX_GF_INTERVAL)
83 #define TPL_DEP_COST_SCALE_LOG2 4
84 
85 typedef struct TplTxfmStats {
86  double abs_coeff_sum[256]; // Assume we are using 16x16 transform block
87  int txfm_block_count;
88 } TplTxfmStats;
89 
90 typedef struct TplDepStats {
91  int64_t intra_cost;
92  int64_t inter_cost;
93  int64_t srcrf_dist;
94  int64_t recrf_dist;
95  int64_t cmp_recrf_dist[2];
96  int64_t srcrf_rate;
97  int64_t recrf_rate;
98  int64_t cmp_recrf_rate[2];
99  int64_t mc_dep_rate;
100  int64_t mc_dep_dist;
101  int_mv mv[INTER_REFS_PER_FRAME];
102  int ref_frame_index[2];
103  int64_t pred_error[INTER_REFS_PER_FRAME];
104 } TplDepStats;
105 
106 typedef struct TplDepFrame {
107  uint8_t is_valid;
108  TplDepStats *tpl_stats_ptr;
109  const YV12_BUFFER_CONFIG *gf_picture;
110  YV12_BUFFER_CONFIG *rec_picture;
111  int ref_map_index[REF_FRAMES];
112  int stride;
113  int width;
114  int height;
115  int mi_rows;
116  int mi_cols;
117  int base_rdmult;
118  uint32_t frame_display_index;
119  double abs_coeff_sum[256]; // Assume we are using 16x16 transform block
120  double abs_coeff_mean[256];
121  int coeff_num; // number of coefficients in a transform block
122  int txfm_block_count;
123 } TplDepFrame;
124 
129 typedef struct TplParams {
134 
138  uint8_t tpl_bsize_1d;
139 
145  TplDepFrame tpl_stats_buffer[MAX_LENGTH_TPL_FRAME_STATS];
146 
152  TplDepStats *tpl_stats_pool[MAX_LAG_BUFFERS];
153 
159 
163  TplDepFrame *tpl_frame;
164 
168  struct scale_factors sf;
169 
174 
180  const YV12_BUFFER_CONFIG *src_ref_frame[INTER_REFS_PER_FRAME];
181 
187  const YV12_BUFFER_CONFIG *ref_frame[INTER_REFS_PER_FRAME];
188 
193  AV1TplRowMultiThreadSync tpl_mt_sync;
194 
199 
204 } TplParams;
205 
214 void av1_setup_tpl_buffers(AV1_COMMON *const cm, TplParams *const tpl_data,
215  int lag_in_frames);
216 
229 int av1_tpl_setup_stats(struct AV1_COMP *cpi, int gop_eval,
230  const struct EncodeFrameParams *const frame_params,
231  const struct EncodeFrameInput *const frame_input);
232 
235 int av1_tpl_ptr_pos(int mi_row, int mi_col, int stride, uint8_t right_shift);
236 
237 void av1_init_tpl_stats(TplParams *const tpl_data);
238 
239 void av1_tpl_rdmult_setup(struct AV1_COMP *cpi);
240 
241 void av1_tpl_rdmult_setup_sb(struct AV1_COMP *cpi, MACROBLOCK *const x,
242  BLOCK_SIZE sb_size, int mi_row, int mi_col);
243 
244 void av1_mc_flow_dispenser_row(struct AV1_COMP *cpi,
245  TplTxfmStats *tpl_txfm_stats, MACROBLOCK *x,
246  int mi_row, BLOCK_SIZE bsize, TX_SIZE tx_size);
247 
260 double av1_exponential_entropy(double q_step, double b);
261 
275 double av1_laplace_entropy(double q_step, double b, double zero_bin_ratio);
276 
294 double av1_laplace_estimate_frame_rate(int q_index, int block_count,
295  const double *abs_coeff_mean,
296  int coeff_num);
297 
306 void av1_tpl_stats_init_txfm_stats(TplDepFrame *tpl_frame, int coeff_num);
307 
309 #ifdef __cplusplus
310 } // extern "C"
311 #endif
312 
313 #endif // AOM_AV1_ENCODER_TPL_MODEL_H_
int border_in_pixels
Definition: tpl_model.h:198
uint8_t tpl_bsize_1d
Definition: tpl_model.h:138
Params related to temporal dependency model.
Definition: tpl_model.h:129
const YV12_BUFFER_CONFIG * ref_frame[INTER_REFS_PER_FRAME]
Definition: tpl_model.h:187
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:2726
TplDepFrame tpl_stats_buffer[MAX_LENGTH_TPL_FRAME_STATS]
Definition: tpl_model.h:145
AV1TplRowMultiThreadSync tpl_mt_sync
Definition: tpl_model.h:193
struct scale_factors sf
Definition: tpl_model.h:168
TplDepStats * tpl_stats_pool[MAX_LAG_BUFFERS]
Definition: tpl_model.h:152
YV12_BUFFER_CONFIG tpl_rec_pool[MAX_LAG_BUFFERS]
Definition: tpl_model.h:158
YV12 frame buffer data structure.
Definition: yv12config.h:38
Input frames and last input frame.
Definition: encoder.h:2714
int frame_idx
Definition: tpl_model.h:173
Top level encoder structure.
Definition: encoder.h:2095
Declares top-level encoder structures and functions.
Top level common structure used by both encoder and decoder.
Definition: av1_common_int.h:723
int skip_tpl_setup_stats
Definition: tpl_model.h:203
Encoder's parameters related to the current coding block.
Definition: block.h:846
TplDepFrame * tpl_frame
Definition: tpl_model.h:163
uint8_t tpl_stats_block_mis_log2
Definition: tpl_model.h:133
const YV12_BUFFER_CONFIG * src_ref_frame[INTER_REFS_PER_FRAME]
Definition: tpl_model.h:180