/src/aom/av1/encoder/tpl_model.h
Line | Count | Source (jump to first uncovered line) |
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 | | |
19 | | /*!\cond */ |
20 | | |
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 | 0 | static INLINE BLOCK_SIZE convert_length_to_bsize(int length) { |
39 | 0 | switch (length) { |
40 | 0 | case 64: return BLOCK_64X64; |
41 | 0 | case 32: return BLOCK_32X32; |
42 | 0 | case 16: return BLOCK_16X16; |
43 | 0 | case 8: return BLOCK_8X8; |
44 | 0 | case 4: return BLOCK_4X4; |
45 | 0 | default: |
46 | 0 | assert(0 && "Invalid block size for tpl model"); |
47 | 0 | return BLOCK_16X16; |
48 | 0 | } |
49 | 0 | } Unexecuted instantiation: av1_cx_iface.c:convert_length_to_bsize Unexecuted instantiation: av1_quantize.c:convert_length_to_bsize Unexecuted instantiation: bitstream.c:convert_length_to_bsize Unexecuted instantiation: encodemv.c:convert_length_to_bsize Unexecuted instantiation: encoder.c:convert_length_to_bsize Unexecuted instantiation: encoder_utils.c:convert_length_to_bsize Unexecuted instantiation: encodetxb.c:convert_length_to_bsize Unexecuted instantiation: ethread.c:convert_length_to_bsize Unexecuted instantiation: firstpass.c:convert_length_to_bsize Unexecuted instantiation: global_motion_facade.c:convert_length_to_bsize Unexecuted instantiation: level.c:convert_length_to_bsize Unexecuted instantiation: lookahead.c:convert_length_to_bsize Unexecuted instantiation: mcomp.c:convert_length_to_bsize Unexecuted instantiation: mv_prec.c:convert_length_to_bsize Unexecuted instantiation: palette.c:convert_length_to_bsize Unexecuted instantiation: pass2_strategy.c:convert_length_to_bsize Unexecuted instantiation: pickcdef.c:convert_length_to_bsize Unexecuted instantiation: picklpf.c:convert_length_to_bsize Unexecuted instantiation: pickrst.c:convert_length_to_bsize Unexecuted instantiation: ratectrl.c:convert_length_to_bsize Unexecuted instantiation: rd.c:convert_length_to_bsize Unexecuted instantiation: rdopt.c:convert_length_to_bsize Unexecuted instantiation: segmentation.c:convert_length_to_bsize Unexecuted instantiation: speed_features.c:convert_length_to_bsize Unexecuted instantiation: superres_scale.c:convert_length_to_bsize Unexecuted instantiation: svc_layercontext.c:convert_length_to_bsize Unexecuted instantiation: temporal_filter.c:convert_length_to_bsize Unexecuted instantiation: thirdpass.c:convert_length_to_bsize Unexecuted instantiation: tokenize.c:convert_length_to_bsize Unexecuted instantiation: tpl_model.c:convert_length_to_bsize Unexecuted instantiation: tx_search.c:convert_length_to_bsize Unexecuted instantiation: txb_rdopt.c:convert_length_to_bsize Unexecuted instantiation: intra_mode_search.c:convert_length_to_bsize Unexecuted instantiation: var_based_part.c:convert_length_to_bsize Unexecuted instantiation: av1_noise_estimate.c:convert_length_to_bsize Unexecuted instantiation: aq_complexity.c:convert_length_to_bsize Unexecuted instantiation: aq_cyclicrefresh.c:convert_length_to_bsize Unexecuted instantiation: aq_variance.c:convert_length_to_bsize Unexecuted instantiation: allintra_vis.c:convert_length_to_bsize Unexecuted instantiation: compound_type.c:convert_length_to_bsize Unexecuted instantiation: context_tree.c:convert_length_to_bsize Unexecuted instantiation: encodeframe.c:convert_length_to_bsize Unexecuted instantiation: encodeframe_utils.c:convert_length_to_bsize Unexecuted instantiation: encodemb.c:convert_length_to_bsize Unexecuted instantiation: encode_strategy.c:convert_length_to_bsize Unexecuted instantiation: global_motion.c:convert_length_to_bsize Unexecuted instantiation: gop_structure.c:convert_length_to_bsize Unexecuted instantiation: interp_search.c:convert_length_to_bsize Unexecuted instantiation: motion_search_facade.c:convert_length_to_bsize Unexecuted instantiation: partition_search.c:convert_length_to_bsize Unexecuted instantiation: partition_strategy.c:convert_length_to_bsize Unexecuted instantiation: nonrd_pickmode.c:convert_length_to_bsize |
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 | 0 | #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 | 0 | #define MAX_LENGTH_TPL_FRAME_STATS (MAX_TPL_FRAME_IDX + REF_FRAMES + 1) |
93 | 0 | #define TPL_DEP_COST_SCALE_LOG2 4 |
94 | | |
95 | 0 | #define TPL_EPSILON 0.0000001 |
96 | | |
97 | | typedef struct TplTxfmStats { |
98 | | int ready; // Whether abs_coeff_mean is ready |
99 | | double abs_coeff_sum[256]; // Assume we are using 16x16 transform block |
100 | | double abs_coeff_mean[256]; |
101 | | int txfm_block_count; |
102 | | int coeff_num; |
103 | | } TplTxfmStats; |
104 | | |
105 | | typedef struct TplDepStats { |
106 | | int64_t intra_cost; |
107 | | int64_t inter_cost; |
108 | | int64_t srcrf_dist; |
109 | | int64_t recrf_dist; |
110 | | int64_t cmp_recrf_dist[2]; |
111 | | int64_t srcrf_rate; |
112 | | int64_t recrf_rate; |
113 | | int64_t srcrf_sse; |
114 | | int64_t cmp_recrf_rate[2]; |
115 | | int64_t mc_dep_rate; |
116 | | int64_t mc_dep_dist; |
117 | | int_mv mv[INTER_REFS_PER_FRAME]; |
118 | | int ref_frame_index[2]; |
119 | | int64_t pred_error[INTER_REFS_PER_FRAME]; |
120 | | } TplDepStats; |
121 | | |
122 | | typedef struct TplDepFrame { |
123 | | uint8_t is_valid; |
124 | | TplDepStats *tpl_stats_ptr; |
125 | | const YV12_BUFFER_CONFIG *gf_picture; |
126 | | YV12_BUFFER_CONFIG *rec_picture; |
127 | | int ref_map_index[REF_FRAMES]; |
128 | | int stride; |
129 | | int width; |
130 | | int height; |
131 | | int mi_rows; |
132 | | int mi_cols; |
133 | | int base_rdmult; |
134 | | uint32_t frame_display_index; |
135 | | } TplDepFrame; |
136 | | |
137 | | /*!\endcond */ |
138 | | /*! |
139 | | * \brief Params related to temporal dependency model. |
140 | | */ |
141 | | typedef struct TplParams { |
142 | | /*! |
143 | | * Whether the tpl stats is ready. |
144 | | */ |
145 | | int ready; |
146 | | |
147 | | /*! |
148 | | * Block granularity of tpl score storage. |
149 | | */ |
150 | | uint8_t tpl_stats_block_mis_log2; |
151 | | |
152 | | /*! |
153 | | * Tpl motion estimation block 1d size. tpl_bsize_1d >= 16. |
154 | | */ |
155 | | uint8_t tpl_bsize_1d; |
156 | | |
157 | | /*! |
158 | | * Buffer to store the frame level tpl information for each frame in a gf |
159 | | * group. tpl_stats_buffer[i] stores the tpl information of ith frame in a gf |
160 | | * group |
161 | | */ |
162 | | TplDepFrame tpl_stats_buffer[MAX_LENGTH_TPL_FRAME_STATS]; |
163 | | |
164 | | /*! |
165 | | * Buffer to store tpl stats at block granularity. |
166 | | * tpl_stats_pool[i][j] stores the tpl stats of jth block of ith frame in a gf |
167 | | * group. |
168 | | */ |
169 | | TplDepStats *tpl_stats_pool[MAX_LAG_BUFFERS]; |
170 | | |
171 | | /*! |
172 | | * Buffer to store tpl transform stats per frame. |
173 | | * txfm_stats_list[i] stores the TplTxfmStats of the ith frame in a gf group. |
174 | | */ |
175 | | TplTxfmStats txfm_stats_list[MAX_LENGTH_TPL_FRAME_STATS]; |
176 | | |
177 | | /*! |
178 | | * Buffer to store tpl reconstructed frame. |
179 | | * tpl_rec_pool[i] stores the reconstructed frame of ith frame in a gf group. |
180 | | */ |
181 | | YV12_BUFFER_CONFIG tpl_rec_pool[MAX_LAG_BUFFERS]; |
182 | | |
183 | | /*! |
184 | | * Pointer to tpl_stats_buffer. |
185 | | */ |
186 | | TplDepFrame *tpl_frame; |
187 | | |
188 | | /*! |
189 | | * Scale factors for the current frame. |
190 | | */ |
191 | | struct scale_factors sf; |
192 | | |
193 | | /*! |
194 | | * GF group index of the current frame. |
195 | | */ |
196 | | int frame_idx; |
197 | | |
198 | | /*! |
199 | | * Array of pointers to the frame buffers holding the source frame. |
200 | | * src_ref_frame[i] stores the pointer to the source frame of the ith |
201 | | * reference frame type. |
202 | | */ |
203 | | const YV12_BUFFER_CONFIG *src_ref_frame[INTER_REFS_PER_FRAME]; |
204 | | |
205 | | /*! |
206 | | * Array of pointers to the frame buffers holding the tpl reconstructed frame. |
207 | | * ref_frame[i] stores the pointer to the tpl reconstructed frame of the ith |
208 | | * reference frame type. |
209 | | */ |
210 | | const YV12_BUFFER_CONFIG *ref_frame[INTER_REFS_PER_FRAME]; |
211 | | |
212 | | /*! |
213 | | * Parameters related to synchronization for top-right dependency in row based |
214 | | * multi-threading of tpl |
215 | | */ |
216 | | AV1TplRowMultiThreadSync tpl_mt_sync; |
217 | | |
218 | | /*! |
219 | | * Frame border for tpl frame. |
220 | | */ |
221 | | int border_in_pixels; |
222 | | |
223 | | } TplParams; |
224 | | |
225 | | #if CONFIG_BITRATE_ACCURACY |
226 | | /*! |
227 | | * \brief This structure stores information needed for bitrate accuracy |
228 | | * experiment. |
229 | | */ |
230 | | typedef struct { |
231 | | double total_bit_budget; // The total bit budget of the entire video |
232 | | int show_frame_count; // Number of show frames in the entire video |
233 | | |
234 | | int gop_showframe_count; // The number of show frames in the current gop |
235 | | double gop_bit_budget; // The bitbudget for the current gop |
236 | | double scale_factors[FRAME_UPDATE_TYPES]; // Scale factors to improve the |
237 | | // budget estimation |
238 | | double mv_scale_factors[FRAME_UPDATE_TYPES]; // Scale factors to improve |
239 | | // MV entropy estimation |
240 | | |
241 | | // === Below this line are GOP related data that will be updated per GOP === |
242 | | int base_q_index; // Stores the base q index. |
243 | | int q_index_list_ready; |
244 | | int q_index_list[MAX_LENGTH_TPL_FRAME_STATS]; // q indices for the current |
245 | | // GOP |
246 | | // Arrays to store frame level bitrate accuracy data. |
247 | | double estimated_bitrate_byframe[MAX_LENGTH_TPL_FRAME_STATS]; |
248 | | double estimated_mv_bitrate_byframe[MAX_LENGTH_TPL_FRAME_STATS]; |
249 | | int actual_bitrate_byframe[MAX_LENGTH_TPL_FRAME_STATS]; |
250 | | int actual_mv_bitrate_byframe[MAX_LENGTH_TPL_FRAME_STATS]; |
251 | | int actual_coeff_bitrate_byframe[MAX_LENGTH_TPL_FRAME_STATS]; |
252 | | |
253 | | // Array to store qstep_ratio for each frame in a GOP |
254 | | double qstep_ratio_list[MAX_LENGTH_TPL_FRAME_STATS]; |
255 | | } VBR_RATECTRL_INFO; |
256 | | |
257 | | static INLINE void vbr_rc_reset_gop_data(VBR_RATECTRL_INFO *vbr_rc_info) { |
258 | | vbr_rc_info->q_index_list_ready = 0; |
259 | | av1_zero(vbr_rc_info->q_index_list); |
260 | | av1_zero(vbr_rc_info->estimated_bitrate_byframe); |
261 | | av1_zero(vbr_rc_info->estimated_mv_bitrate_byframe); |
262 | | av1_zero(vbr_rc_info->actual_bitrate_byframe); |
263 | | av1_zero(vbr_rc_info->actual_mv_bitrate_byframe); |
264 | | av1_zero(vbr_rc_info->actual_coeff_bitrate_byframe); |
265 | | } |
266 | | |
267 | | void av1_vbr_rc_init(VBR_RATECTRL_INFO *vbr_rc_info, double total_bit_budget, |
268 | | int show_frame_count); |
269 | | void av1_vbr_rc_set_gop_bit_budget(VBR_RATECTRL_INFO *vbr_rc_info, |
270 | | int gop_showframe_count); |
271 | | |
272 | | static INLINE void vbr_rc_info_log(const VBR_RATECTRL_INFO *vbr_rc_info, |
273 | | int gf_frame_index, int gf_group_size, |
274 | | FRAME_UPDATE_TYPE *update_type) { |
275 | | // Add +2 here because this is the last frame this method is called at. |
276 | | if (gf_frame_index + 2 >= gf_group_size) { |
277 | | printf( |
278 | | "\ni, \test_bitrate, \test_mv_bitrate, \tact_bitrate, " |
279 | | "\tact_mv_bitrate, \tact_coeff_bitrate, \tq, \tupdate_type\n"); |
280 | | for (int i = 0; i < gf_group_size; i++) { |
281 | | printf("%d, \t%f, \t%f, \t%d, \t%d, \t%d, \t%d, \t%d\n", i, |
282 | | vbr_rc_info->estimated_bitrate_byframe[i], |
283 | | vbr_rc_info->estimated_mv_bitrate_byframe[i], |
284 | | vbr_rc_info->actual_bitrate_byframe[i], |
285 | | vbr_rc_info->actual_mv_bitrate_byframe[i], |
286 | | vbr_rc_info->actual_coeff_bitrate_byframe[i], |
287 | | vbr_rc_info->q_index_list[i], update_type[i]); |
288 | | } |
289 | | } |
290 | | } |
291 | | |
292 | | /*!\brief Update q_index_list in vbr_rc_info based on tpl stats |
293 | | * |
294 | | * \param[out] vbr_rc_info Rate control info for BITRATE_ACCURACY |
295 | | * experiment |
296 | | * \param[in] tpl_data TPL struct |
297 | | * \param[in] gf_group GOP struct |
298 | | * \param[in] bit_depth bit depth |
299 | | */ |
300 | | void av1_vbr_rc_update_q_index_list(VBR_RATECTRL_INFO *vbr_rc_info, |
301 | | const TplParams *tpl_data, |
302 | | const struct GF_GROUP *gf_group, |
303 | | aom_bit_depth_t bit_depth); |
304 | | /* |
305 | | *!\brief Compute the number of bits needed to encode a GOP |
306 | | * |
307 | | * \param[in] base_q_index base layer q_index |
308 | | * \param[in] bit_depth bit depth |
309 | | * \param[in] update_type_scale_factors array of scale factors for each |
310 | | * update_type |
311 | | * \param[in] frame_count size of update_type_list, qstep_ratio_list |
312 | | * stats_list, q_index_list and |
313 | | * estimated_bitrate_byframe |
314 | | * \param[in] update_type_list array of update_type, one per frame |
315 | | * \param[in] qstep_ratio_list array of qstep_ratio, one per frame |
316 | | * \param[in] stats_list array of transform stats, one per frame |
317 | | * \param[out] q_index_list array of q_index, one per frame |
318 | | * \param[out] estimated_bitrate_byframe Array to keep track of frame bitrate |
319 | | * |
320 | | * \return The estimated GOP bitrate. |
321 | | * |
322 | | */ |
323 | | double av1_vbr_rc_info_estimate_gop_bitrate( |
324 | | int base_q_index, aom_bit_depth_t bit_depth, |
325 | | const double *update_type_scale_factors, int frame_count, |
326 | | const FRAME_UPDATE_TYPE *update_type_list, const double *qstep_ratio_list, |
327 | | const TplTxfmStats *stats_list, int *q_index_list, |
328 | | double *estimated_bitrate_byframe); |
329 | | |
330 | | /*!\brief Estimate the optimal base q index for a GOP. |
331 | | * |
332 | | * This function uses a binary search to find base layer q index to |
333 | | * achieve the specified bit budget. |
334 | | * |
335 | | * \param[in] bit_budget target bit budget |
336 | | * \param[in] bit_depth bit depth |
337 | | * \param[in] update_type_scale_factors array of scale factors for each |
338 | | * update_type |
339 | | * \param[in] frame_count size of update_type_list, qstep_ratio_list |
340 | | * stats_list, q_index_list and |
341 | | * estimated_bitrate_byframe |
342 | | * \param[in] update_type_list array of update_type, one per frame |
343 | | * \param[in] qstep_ratio_list array of qstep_ratio, one per frame |
344 | | * \param[in] stats_list array of transform stats, one per frame |
345 | | * \param[out] q_index_list array of q_index, one per frame |
346 | | * \param[out] estimated_bitrate_byframe Array to keep track of frame bitrate |
347 | | * |
348 | | * \return Returns the optimal base q index to use. |
349 | | */ |
350 | | int av1_vbr_rc_info_estimate_base_q( |
351 | | double bit_budget, aom_bit_depth_t bit_depth, |
352 | | const double *update_type_scale_factors, int frame_count, |
353 | | const FRAME_UPDATE_TYPE *update_type_list, const double *qstep_ratio_list, |
354 | | const TplTxfmStats *stats_list, int *q_index_list, |
355 | | double *estimated_bitrate_byframe); |
356 | | |
357 | | /*!\brief For a GOP, calculate the bits used by motion vectors. |
358 | | * |
359 | | * \param[in] tpl_data TPL struct |
360 | | * \param[in] gf_group GOP struct |
361 | | * \param[in] vbr_rc_info Rate control info struct |
362 | | * |
363 | | * \return Bits used by the motion vectors for the GOP. |
364 | | */ |
365 | | double av1_tpl_compute_mv_bits(const TplParams *tpl_data, |
366 | | const GF_GROUP *gf_group, |
367 | | VBR_RATECTRL_INFO *vbr_rc_info); |
368 | | #endif // CONFIG_BITRATE_ACCURACY |
369 | | |
370 | | #if CONFIG_RD_COMMAND |
371 | | typedef enum { |
372 | | RD_OPTION_NONE, |
373 | | RD_OPTION_SET_Q, |
374 | | RD_OPTION_SET_Q_RDMULT |
375 | | } RD_OPTION; |
376 | | |
377 | | typedef struct RD_COMMAND { |
378 | | RD_OPTION option_ls[MAX_LENGTH_TPL_FRAME_STATS]; |
379 | | int q_index_ls[MAX_LENGTH_TPL_FRAME_STATS]; |
380 | | int rdmult_ls[MAX_LENGTH_TPL_FRAME_STATS]; |
381 | | int frame_count; |
382 | | int frame_index; |
383 | | } RD_COMMAND; |
384 | | |
385 | | void av1_read_rd_command(const char *filepath, RD_COMMAND *rd_command); |
386 | | #endif // CONFIG_RD_COMMAND |
387 | | |
388 | | /*!\brief Allocate buffers used by tpl model |
389 | | * |
390 | | * \param[in] Top-level encode/decode structure |
391 | | * \param[in] lag_in_frames number of lookahead frames |
392 | | * |
393 | | * \param[out] tpl_data tpl data structure |
394 | | */ |
395 | | |
396 | | void av1_setup_tpl_buffers(struct AV1_PRIMARY *const ppi, |
397 | | CommonModeInfoParams *const mi_params, int width, |
398 | | int height, int byte_alignment, int lag_in_frames); |
399 | | |
400 | | /*!\brief Implements temporal dependency modelling for a GOP (GF/ARF |
401 | | * group) and selects between 16 and 32 frame GOP structure. |
402 | | * |
403 | | *\ingroup tpl_modelling |
404 | | * |
405 | | * \param[in] cpi Top - level encoder instance structure |
406 | | * \param[in] gop_eval Flag if it is in the GOP length decision stage |
407 | | * \param[in] frame_params Per frame encoding parameters |
408 | | * |
409 | | * \return Indicates whether or not we should use a longer GOP length. |
410 | | */ |
411 | | int av1_tpl_setup_stats(struct AV1_COMP *cpi, int gop_eval, |
412 | | const struct EncodeFrameParams *const frame_params); |
413 | | |
414 | | /*!\cond */ |
415 | | |
416 | | void av1_tpl_preload_rc_estimate( |
417 | | struct AV1_COMP *cpi, const struct EncodeFrameParams *const frame_params); |
418 | | |
419 | | int av1_tpl_ptr_pos(int mi_row, int mi_col, int stride, uint8_t right_shift); |
420 | | |
421 | | void av1_init_tpl_stats(TplParams *const tpl_data); |
422 | | |
423 | | int av1_tpl_stats_ready(const TplParams *tpl_data, int gf_frame_index); |
424 | | |
425 | | void av1_tpl_rdmult_setup(struct AV1_COMP *cpi); |
426 | | |
427 | | void av1_tpl_rdmult_setup_sb(struct AV1_COMP *cpi, MACROBLOCK *const x, |
428 | | BLOCK_SIZE sb_size, int mi_row, int mi_col); |
429 | | |
430 | | void av1_mc_flow_dispenser_row(struct AV1_COMP *cpi, |
431 | | TplTxfmStats *tpl_txfm_stats, MACROBLOCK *x, |
432 | | int mi_row, BLOCK_SIZE bsize, TX_SIZE tx_size); |
433 | | |
434 | | /*!\brief Compute the entropy of an exponential probability distribution |
435 | | * function (pdf) subjected to uniform quantization. |
436 | | * |
437 | | * pdf(x) = b*exp(-b*x) |
438 | | * |
439 | | *\ingroup tpl_modelling |
440 | | * |
441 | | * \param[in] q_step quantizer step size |
442 | | * \param[in] b parameter of exponential distribution |
443 | | * |
444 | | * \return entropy cost |
445 | | */ |
446 | | double av1_exponential_entropy(double q_step, double b); |
447 | | |
448 | | /*!\brief Compute the entropy of a Laplace probability distribution |
449 | | * function (pdf) subjected to non-uniform quantization. |
450 | | * |
451 | | * pdf(x) = 0.5*b*exp(-0.5*b*|x|) |
452 | | * |
453 | | *\ingroup tpl_modelling |
454 | | * |
455 | | * \param[in] q_step quantizer step size for non-zero bins |
456 | | * \param[in] b parameter of Laplace distribution |
457 | | * \param[in] zero_bin_ratio zero bin's size is zero_bin_ratio * q_step |
458 | | * |
459 | | * \return entropy cost |
460 | | */ |
461 | | double av1_laplace_entropy(double q_step, double b, double zero_bin_ratio); |
462 | | |
463 | | /*!\brief Compute the frame rate using transform block stats |
464 | | * |
465 | | * Assume each position i in the transform block is of Laplace distribution |
466 | | * with mean absolute deviation abs_coeff_mean[i] |
467 | | * |
468 | | * Then we can use av1_laplace_entropy() to compute the expected frame |
469 | | * rate. |
470 | | * |
471 | | *\ingroup tpl_modelling |
472 | | * |
473 | | * \param[in] q_index quantizer index |
474 | | * \param[in] block_count number of transform blocks |
475 | | * \param[in] abs_coeff_mean array of mean absolute deviation |
476 | | * \param[in] coeff_num number of coefficients per transform block |
477 | | * |
478 | | * \return expected frame rate |
479 | | */ |
480 | | double av1_laplace_estimate_frame_rate(int q_index, int block_count, |
481 | | const double *abs_coeff_mean, |
482 | | int coeff_num); |
483 | | |
484 | | /* |
485 | | *!\brief Init TplTxfmStats |
486 | | * |
487 | | * \param[in] tpl_txfm_stats a structure for storing transform stats |
488 | | * |
489 | | */ |
490 | | void av1_init_tpl_txfm_stats(TplTxfmStats *tpl_txfm_stats); |
491 | | |
492 | | /* |
493 | | *!\brief Accumulate TplTxfmStats |
494 | | * |
495 | | * \param[in] sub_stats a structure for storing sub transform stats |
496 | | * \param[out] accumulated_stats a structure for storing accumulated transform |
497 | | *stats |
498 | | * |
499 | | */ |
500 | | void av1_accumulate_tpl_txfm_stats(const TplTxfmStats *sub_stats, |
501 | | TplTxfmStats *accumulated_stats); |
502 | | |
503 | | /* |
504 | | *!\brief Record a transform block into TplTxfmStats |
505 | | * |
506 | | * \param[in] tpl_txfm_stats A structure for storing transform stats |
507 | | * \param[out] coeff An array of transform coefficients. Its size |
508 | | * should equal to tpl_txfm_stats.coeff_num. |
509 | | * |
510 | | */ |
511 | | void av1_record_tpl_txfm_block(TplTxfmStats *tpl_txfm_stats, |
512 | | const tran_low_t *coeff); |
513 | | |
514 | | /* |
515 | | *!\brief Update abs_coeff_mean and ready of txfm_stats |
516 | | * If txfm_block_count > 0, this function will use abs_coeff_sum and |
517 | | * txfm_block_count to compute abs_coeff_mean. Moreover, reday flag |
518 | | * will be set to one. |
519 | | * |
520 | | * \param[in] txfm_stats A structure for storing transform stats |
521 | | */ |
522 | | void av1_tpl_txfm_stats_update_abs_coeff_mean(TplTxfmStats *txfm_stats); |
523 | | |
524 | | /*!\brief Estimate coefficient entropy using Laplace dsitribution |
525 | | * |
526 | | *\ingroup tpl_modelling |
527 | | * |
528 | | * This function is equivalent to -log2(laplace_prob()), where laplace_prob() is |
529 | | * defined in tpl_model_test.cc |
530 | | * |
531 | | * \param[in] q_step quantizer step size without any scaling |
532 | | * \param[in] b mean absolute deviation of Laplace distribution |
533 | | * \param[in] zero_bin_ratio zero bin's size is zero_bin_ratio * q_step |
534 | | * \param[in] qcoeff quantized coefficient |
535 | | * |
536 | | * \return estimated coefficient entropy |
537 | | * |
538 | | */ |
539 | | double av1_estimate_coeff_entropy(double q_step, double b, |
540 | | double zero_bin_ratio, int qcoeff); |
541 | | |
542 | | /*!\brief Estimate entropy of a transform block using Laplace dsitribution |
543 | | * |
544 | | *\ingroup tpl_modelling |
545 | | * |
546 | | * \param[in] q_index quantizer index |
547 | | * \param[in] abs_coeff_mean array of mean absolute deviations |
548 | | * \param[in] qcoeff_arr array of quantized coefficients |
549 | | * \param[in] coeff_num number of coefficients per transform block |
550 | | * |
551 | | * \return estimated transform block entropy |
552 | | * |
553 | | */ |
554 | | double av1_estimate_txfm_block_entropy(int q_index, |
555 | | const double *abs_coeff_mean, |
556 | | int *qcoeff_arr, int coeff_num); |
557 | | |
558 | | // TODO(angiebird): Add doxygen description here. |
559 | | int64_t av1_delta_rate_cost(int64_t delta_rate, int64_t recrf_dist, |
560 | | int64_t srcrf_dist, int pix_num); |
561 | | |
562 | | /*!\brief Compute the overlap area between two blocks with the same size |
563 | | * |
564 | | *\ingroup tpl_modelling |
565 | | * |
566 | | * If there is no overlap, this function should return zero. |
567 | | * |
568 | | * \param[in] row_a row position of the first block |
569 | | * \param[in] col_a column position of the first block |
570 | | * \param[in] row_b row position of the second block |
571 | | * \param[in] col_b column position of the second block |
572 | | * \param[in] width width shared by the two blocks |
573 | | * \param[in] height height shared by the two blocks |
574 | | * |
575 | | * \return overlap area of the two blocks |
576 | | */ |
577 | | int av1_get_overlap_area(int row_a, int col_a, int row_b, int col_b, int width, |
578 | | int height); |
579 | | |
580 | | /*!\brief Get current frame's q_index from tpl stats and leaf_qindex |
581 | | * |
582 | | * \param[in] tpl_data TPL struct |
583 | | * \param[in] gf_frame_index current frame index in the GOP |
584 | | * \param[in] leaf_qindex q index of leaf frame |
585 | | * \param[in] bit_depth bit depth |
586 | | * |
587 | | * \return q_index |
588 | | */ |
589 | | int av1_tpl_get_q_index(const TplParams *tpl_data, int gf_frame_index, |
590 | | int leaf_qindex, aom_bit_depth_t bit_depth); |
591 | | |
592 | | /*!\brief Compute the frame importance from TPL stats |
593 | | * |
594 | | * \param[in] tpl_data TPL struct |
595 | | * \param[in] gf_frame_index current frame index in the GOP |
596 | | * |
597 | | * \return frame_importance |
598 | | */ |
599 | | double av1_tpl_get_frame_importance(const TplParams *tpl_data, |
600 | | int gf_frame_index); |
601 | | |
602 | | /*!\brief Compute the ratio between arf q step and the leaf q step based on TPL |
603 | | * stats |
604 | | * |
605 | | * \param[in] tpl_data TPL struct |
606 | | * \param[in] gf_frame_index current frame index in the GOP |
607 | | * \param[in] leaf_qindex q index of leaf frame |
608 | | * \param[in] bit_depth bit depth |
609 | | * |
610 | | * \return qstep_ratio |
611 | | */ |
612 | | double av1_tpl_get_qstep_ratio(const TplParams *tpl_data, int gf_frame_index); |
613 | | |
614 | | /*!\brief Find a q index whose step size is near qstep_ratio * leaf_qstep |
615 | | * |
616 | | * \param[in] leaf_qindex q index of leaf frame |
617 | | * \param[in] qstep_ratio step ratio between target q index and leaf |
618 | | * q index |
619 | | * \param[in] bit_depth bit depth |
620 | | * |
621 | | * \return q_index |
622 | | */ |
623 | | int av1_get_q_index_from_qstep_ratio(int leaf_qindex, double qstep_ratio, |
624 | | aom_bit_depth_t bit_depth); |
625 | | |
626 | | /*!\brief Improve the motion vector estimation by taking neighbors into account. |
627 | | * |
628 | | * Use the upper and left neighbor block as the reference MVs. |
629 | | * Compute the minimum difference between current MV and reference MV. |
630 | | * |
631 | | * \param[in] tpl_frame Tpl frame struct |
632 | | * \param[in] row Current row |
633 | | * \param[in] col Current column |
634 | | * \param[in] step Step parameter for av1_tpl_ptr_pos |
635 | | * \param[in] tpl_stride Stride parameter for av1_tpl_ptr_pos |
636 | | * \param[in] right_shift Right shift parameter for av1_tpl_ptr_pos |
637 | | */ |
638 | | int_mv av1_compute_mv_difference(const TplDepFrame *tpl_frame, int row, int col, |
639 | | int step, int tpl_stride, int right_shift); |
640 | | |
641 | | /*!\brief Compute the entropy of motion vectors for a single frame. |
642 | | * |
643 | | * \param[in] tpl_frame TPL frame struct |
644 | | * \param[in] right_shift right shift value for step |
645 | | * |
646 | | * \return Bits used by the motion vectors for one frame. |
647 | | */ |
648 | | double av1_tpl_compute_frame_mv_entropy(const TplDepFrame *tpl_frame, |
649 | | uint8_t right_shift); |
650 | | |
651 | | /*!\endcond */ |
652 | | #ifdef __cplusplus |
653 | | } // extern "C" |
654 | | #endif |
655 | | |
656 | | #endif // AOM_AV1_ENCODER_TPL_MODEL_H_ |