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 TplDepStats {
86  int64_t intra_cost;
87  int64_t inter_cost;
88  int64_t srcrf_dist;
89  int64_t recrf_dist;
90  int64_t cmp_recrf_dist[2];
91  int64_t srcrf_rate;
92  int64_t recrf_rate;
93  int64_t cmp_recrf_rate[2];
94  int64_t mc_dep_rate;
95  int64_t mc_dep_dist;
96  int_mv mv[INTER_REFS_PER_FRAME];
97  int ref_frame_index[2];
98  int64_t pred_error[INTER_REFS_PER_FRAME];
99 } TplDepStats;
100 
101 typedef struct TplDepFrame {
102  uint8_t is_valid;
103  TplDepStats *tpl_stats_ptr;
104  const YV12_BUFFER_CONFIG *gf_picture;
105  YV12_BUFFER_CONFIG *rec_picture;
106  int ref_map_index[REF_FRAMES];
107  int stride;
108  int width;
109  int height;
110  int mi_rows;
111  int mi_cols;
112  int base_rdmult;
113  uint32_t frame_display_index;
114 } TplDepFrame;
115 
120 typedef struct TplParams {
125 
129  uint8_t tpl_bsize_1d;
130 
136  TplDepFrame tpl_stats_buffer[MAX_LENGTH_TPL_FRAME_STATS];
137 
143  TplDepStats *tpl_stats_pool[MAX_LAG_BUFFERS];
144 
150 
154  TplDepFrame *tpl_frame;
155 
159  struct scale_factors sf;
160 
165 
171  const YV12_BUFFER_CONFIG *src_ref_frame[INTER_REFS_PER_FRAME];
172 
178  const YV12_BUFFER_CONFIG *ref_frame[INTER_REFS_PER_FRAME];
179 
184  AV1TplRowMultiThreadSync tpl_mt_sync;
185 
190 
195 } TplParams;
196 
209 int av1_tpl_setup_stats(struct AV1_COMP *cpi, int gop_eval,
210  const struct EncodeFrameParams *const frame_params,
211  const struct EncodeFrameInput *const frame_input);
212 
215 int av1_tpl_ptr_pos(int mi_row, int mi_col, int stride, uint8_t right_shift);
216 
217 void av1_init_tpl_stats(TplParams *const tpl_data);
218 
219 void av1_tpl_rdmult_setup(struct AV1_COMP *cpi);
220 
221 void av1_tpl_rdmult_setup_sb(struct AV1_COMP *cpi, MACROBLOCK *const x,
222  BLOCK_SIZE sb_size, int mi_row, int mi_col);
223 
224 void av1_mc_flow_dispenser_row(struct AV1_COMP *cpi, MACROBLOCK *x, int mi_row,
225  BLOCK_SIZE bsize, TX_SIZE tx_size);
226 
239 double av1_exponential_entropy(double q_step, double b);
240 
254 double av1_laplace_entropy(double q_step, double b, double zero_bin_ratio);
255 
273 double av1_laplace_estimate_frame_rate(int q_index, int block_count,
274  const double *abs_coeff_mean,
275  int coeff_num);
277 #ifdef __cplusplus
278 } // extern "C"
279 #endif
280 
281 #endif // AOM_AV1_ENCODER_TPL_MODEL_H_
Declares top-level encoder structures and functions.
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...
Top level encoder structure.
Definition: encoder.h:2042
Input frames and last input frame.
Definition: encoder.h:2663
contains per-frame encoding parameters decided upon by av1_encode_strategy() and passed down to av1_e...
Definition: encoder.h:2675
Params related to temporal dependency model.
Definition: tpl_model.h:120
const YV12_BUFFER_CONFIG * src_ref_frame[INTER_REFS_PER_FRAME]
Definition: tpl_model.h:171
struct scale_factors sf
Definition: tpl_model.h:159
TplDepFrame tpl_stats_buffer[MAX_LENGTH_TPL_FRAME_STATS]
Definition: tpl_model.h:136
uint8_t tpl_bsize_1d
Definition: tpl_model.h:129
AV1TplRowMultiThreadSync tpl_mt_sync
Definition: tpl_model.h:184
TplDepFrame * tpl_frame
Definition: tpl_model.h:154
int border_in_pixels
Definition: tpl_model.h:189
TplDepStats * tpl_stats_pool[MAX_LAG_BUFFERS]
Definition: tpl_model.h:143
YV12_BUFFER_CONFIG tpl_rec_pool[MAX_LAG_BUFFERS]
Definition: tpl_model.h:149
uint8_t tpl_stats_block_mis_log2
Definition: tpl_model.h:124
int frame_idx
Definition: tpl_model.h:164
int skip_tpl_setup_stats
Definition: tpl_model.h:194
const YV12_BUFFER_CONFIG * ref_frame[INTER_REFS_PER_FRAME]
Definition: tpl_model.h:178
Encoder's parameters related to the current coding block.
Definition: block.h:829
YV12 frame buffer data structure.
Definition: yv12config.h:38