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 struct ThreadData;
28 struct TPL_INFO;
29
30 #include "config/aom_config.h"
31
32 #include "aom_scale/yv12config.h"
33 #include "aom_util/aom_pthread.h"
34
35 #include "av1/common/mv.h"
36 #include "av1/common/scale.h"
37 #include "av1/encoder/block.h"
38 #include "av1/encoder/lookahead.h"
39 #include "av1/encoder/ratectrl.h"
40
convert_length_to_bsize(int length)41 static inline BLOCK_SIZE convert_length_to_bsize(int length) {
42 switch (length) {
43 case 64: return BLOCK_64X64;
44 case 32: return BLOCK_32X32;
45 case 16: return BLOCK_16X16;
46 case 8: return BLOCK_8X8;
47 case 4: return BLOCK_4X4;
48 default:
49 assert(0 && "Invalid block size for tpl model");
50 return BLOCK_16X16;
51 }
52 }
53
54 typedef struct AV1TplRowMultiThreadSync {
55 #if CONFIG_MULTITHREAD
56 // Synchronization objects for top-right dependency.
57 pthread_mutex_t *mutex_;
58 pthread_cond_t *cond_;
59 #endif
60 // Buffer to store the macroblock whose encoding is complete.
61 // num_finished_cols[i] stores the number of macroblocks which finished
62 // encoding in the ith macroblock row.
63 int *num_finished_cols;
64 // Number of extra macroblocks of the top row to be complete for encoding
65 // of the current macroblock to start. A value of 1 indicates top-right
66 // dependency.
67 int sync_range;
68 // Number of macroblock rows.
69 int rows;
70 // Number of threads processing the current tile.
71 int num_threads_working;
72 } AV1TplRowMultiThreadSync;
73
74 typedef struct AV1TplRowMultiThreadInfo {
75 // Initialized to false, set to true by the worker thread that encounters an
76 // error in order to abort the processing of other worker threads.
77 bool tpl_mt_exit;
78 #if CONFIG_MULTITHREAD
79 // Mutex lock object used for error handling.
80 pthread_mutex_t *mutex_;
81 #endif
82 // Row synchronization related function pointers.
83 void (*sync_read_ptr)(AV1TplRowMultiThreadSync *tpl_mt_sync, int r, int c);
84 void (*sync_write_ptr)(AV1TplRowMultiThreadSync *tpl_mt_sync, int r, int c,
85 int cols);
86 } AV1TplRowMultiThreadInfo;
87
88 // TODO(jingning): This needs to be cleaned up next.
89
90 // TPL stats buffers are prepared for every frame in the GOP,
91 // including (internal) overlays and (internal) arfs.
92 // In addition, frames in the lookahead that are outside of the GOP
93 // are also used.
94 // Thus it should use
95 // (gop_length) + (# overlays) + (MAX_LAG_BUFFERS - gop_len) =
96 // MAX_LAG_BUFFERS + (# overlays)
97 // 2 * MAX_LAG_BUFFERS is therefore a safe estimate.
98 // TODO(bohanli): test setting it to 1.5 * MAX_LAG_BUFFER
99 #define MAX_TPL_FRAME_IDX (2 * MAX_LAG_BUFFERS)
100 // The first REF_FRAMES + 1 buffers are reserved.
101 // tpl_data->tpl_frame starts after REF_FRAMES + 1
102 #define MAX_LENGTH_TPL_FRAME_STATS (MAX_TPL_FRAME_IDX + REF_FRAMES + 1)
103 #define TPL_DEP_COST_SCALE_LOG2 4
104
105 #define TPL_EPSILON 0.0000001
106
107 typedef struct TplTxfmStats {
108 int ready; // Whether abs_coeff_mean is ready
109 double abs_coeff_sum[256]; // Assume we are using 16x16 transform block
110 double abs_coeff_mean[256];
111 int txfm_block_count;
112 int coeff_num;
113 } TplTxfmStats;
114
115 typedef struct {
116 uint8_t *predictor8;
117 int16_t *src_diff;
118 tran_low_t *coeff;
119 tran_low_t *qcoeff;
120 tran_low_t *dqcoeff;
121 } TplBuffers;
122
123 typedef struct TplDepStats {
124 int64_t srcrf_sse;
125 int64_t srcrf_dist;
126 int64_t recrf_sse;
127 int64_t recrf_dist;
128 int64_t intra_sse;
129 int64_t intra_dist;
130 int64_t cmp_recrf_dist[2];
131 int64_t mc_dep_rate;
132 int64_t mc_dep_dist;
133 int64_t pred_error[INTER_REFS_PER_FRAME];
134 int32_t intra_cost;
135 int32_t inter_cost;
136 int32_t srcrf_rate;
137 int32_t recrf_rate;
138 int32_t intra_rate;
139 int32_t cmp_recrf_rate[2];
140 int_mv mv[INTER_REFS_PER_FRAME];
141 int8_t ref_frame_index[2];
142 } TplDepStats;
143
144 typedef struct TplDepFrame {
145 uint8_t is_valid;
146 TplDepStats *tpl_stats_ptr;
147 const YV12_BUFFER_CONFIG *gf_picture;
148 YV12_BUFFER_CONFIG *rec_picture;
149 int ref_map_index[REF_FRAMES];
150 int stride;
151 int width;
152 int height;
153 int mi_rows;
154 int mi_cols;
155 int base_rdmult;
156 uint32_t frame_display_index;
157 // When set, SAD metric is used for intra and inter mode decision.
158 int use_pred_sad;
159 } TplDepFrame;
160
161 /*!\endcond */
162 /*!
163 * \brief Params related to temporal dependency model.
164 */
165 typedef struct TplParams {
166 /*!
167 * Whether the tpl stats is ready.
168 */
169 int ready;
170
171 /*!
172 * Block granularity of tpl score storage.
173 */
174 uint8_t tpl_stats_block_mis_log2;
175
176 /*!
177 * Tpl motion estimation block 1d size. tpl_bsize_1d >= 16.
178 */
179 uint8_t tpl_bsize_1d;
180
181 /*!
182 * Buffer to store the frame level tpl information for each frame in a gf
183 * group. tpl_stats_buffer[i] stores the tpl information of ith frame in a gf
184 * group
185 */
186 TplDepFrame tpl_stats_buffer[MAX_LENGTH_TPL_FRAME_STATS];
187
188 /*!
189 * Buffer to store tpl stats at block granularity.
190 * tpl_stats_pool[i][j] stores the tpl stats of jth block of ith frame in a gf
191 * group.
192 */
193 TplDepStats *tpl_stats_pool[MAX_LAG_BUFFERS];
194
195 /*!
196 * Pointer to the buffer which stores tpl transform stats per frame.
197 * txfm_stats_list[i] stores the TplTxfmStats of the ith frame in a gf group.
198 * Memory is allocated dynamically for MAX_LENGTH_TPL_FRAME_STATS frames when
199 * tpl is enabled.
200 */
201 TplTxfmStats *txfm_stats_list;
202
203 /*!
204 * Buffer to store tpl reconstructed frame.
205 * tpl_rec_pool[i] stores the reconstructed frame of ith frame in a gf group.
206 */
207 YV12_BUFFER_CONFIG tpl_rec_pool[MAX_LAG_BUFFERS];
208
209 /*!
210 * Pointer to tpl_stats_buffer.
211 */
212 TplDepFrame *tpl_frame;
213
214 /*!
215 * Scale factors for the current frame.
216 */
217 struct scale_factors sf;
218
219 /*!
220 * GF group index of the current frame.
221 */
222 int frame_idx;
223
224 /*!
225 * Array of pointers to the frame buffers holding the source frame.
226 * src_ref_frame[i] stores the pointer to the source frame of the ith
227 * reference frame type.
228 */
229 const YV12_BUFFER_CONFIG *src_ref_frame[INTER_REFS_PER_FRAME];
230
231 /*!
232 * Array of pointers to the frame buffers holding the tpl reconstructed frame.
233 * ref_frame[i] stores the pointer to the tpl reconstructed frame of the ith
234 * reference frame type.
235 */
236 const YV12_BUFFER_CONFIG *ref_frame[INTER_REFS_PER_FRAME];
237
238 /*!
239 * Parameters related to synchronization for top-right dependency in row based
240 * multi-threading of tpl
241 */
242 AV1TplRowMultiThreadSync tpl_mt_sync;
243
244 /*!
245 * Frame border for tpl frame.
246 */
247 int border_in_pixels;
248
249 /*!
250 * Factor to adjust r0 if TPL uses a subset of frames in the gf group.
251 */
252 double r0_adjust_factor;
253 } TplParams;
254
255 #if CONFIG_BITRATE_ACCURACY || CONFIG_RATECTRL_LOG
256 #define VBR_RC_INFO_MAX_FRAMES 500
257 #endif // CONFIG_BITRATE_ACCURACY || CONFIG_RATECTRL_LOG
258
259 #if CONFIG_BITRATE_ACCURACY
260
261 /*!
262 * \brief This structure stores information needed for bitrate accuracy
263 * experiment.
264 */
265 typedef struct {
266 int ready;
267 double total_bit_budget; // The total bit budget of the entire video
268 int show_frame_count; // Number of show frames in the entire video
269
270 int gop_showframe_count; // The number of show frames in the current gop
271 double gop_bit_budget; // The bitbudget for the current gop
272 double scale_factors[FRAME_UPDATE_TYPES]; // Scale factors to improve the
273 // budget estimation
274 double mv_scale_factors[FRAME_UPDATE_TYPES]; // Scale factors to improve
275 // MV entropy estimation
276
277 // === Below this line are GOP related data that will be updated per GOP ===
278 int base_q_index; // Stores the base q index.
279 int q_index_list_ready;
280 int q_index_list[VBR_RC_INFO_MAX_FRAMES]; // q indices for the current
281 // GOP
282
283 // Array to store qstep_ratio for each frame in a GOP
284 double qstep_ratio_list[VBR_RC_INFO_MAX_FRAMES];
285
286 #if CONFIG_THREE_PASS
287 TplTxfmStats txfm_stats_list[VBR_RC_INFO_MAX_FRAMES];
288 FRAME_UPDATE_TYPE update_type_list[VBR_RC_INFO_MAX_FRAMES];
289 int gop_start_idx_list[VBR_RC_INFO_MAX_FRAMES];
290 int gop_length_list[VBR_RC_INFO_MAX_FRAMES];
291 int cur_gop_idx;
292 int total_frame_count;
293 int gop_count;
294 #endif // CONFIG_THREE_PASS
295 } VBR_RATECTRL_INFO;
296
vbr_rc_reset_gop_data(VBR_RATECTRL_INFO * vbr_rc_info)297 static inline void vbr_rc_reset_gop_data(VBR_RATECTRL_INFO *vbr_rc_info) {
298 vbr_rc_info->q_index_list_ready = 0;
299 av1_zero(vbr_rc_info->q_index_list);
300 }
301
302 void av1_vbr_rc_init(VBR_RATECTRL_INFO *vbr_rc_info, double total_bit_budget,
303 int show_frame_count);
304
305 int av1_vbr_rc_frame_coding_idx(const VBR_RATECTRL_INFO *vbr_rc_info,
306 int gf_frame_index);
307
308 void av1_vbr_rc_append_tpl_info(VBR_RATECTRL_INFO *vbr_rc_info,
309 const struct TPL_INFO *tpl_info);
310
311 void av1_vbr_rc_set_gop_bit_budget(VBR_RATECTRL_INFO *vbr_rc_info,
312 int gop_showframe_count);
313
314 void av1_vbr_rc_compute_q_indices(int base_q_index, int frame_count,
315 const double *qstep_ratio_list,
316 aom_bit_depth_t bit_depth, int *q_index_list);
317
318 /*!\brief Update q_index_list in vbr_rc_info based on tpl stats
319 *
320 * \param[out] vbr_rc_info Rate control info for BITRATE_ACCURACY
321 * experiment
322 * \param[in] tpl_data TPL struct
323 * \param[in] gf_group GOP struct
324 * \param[in] bit_depth bit depth
325 */
326 void av1_vbr_rc_update_q_index_list(VBR_RATECTRL_INFO *vbr_rc_info,
327 const TplParams *tpl_data,
328 const struct GF_GROUP *gf_group,
329 aom_bit_depth_t bit_depth);
330 /*
331 *!\brief Compute the number of bits needed to encode a GOP
332 *
333 * \param[in] base_q_index base layer q_index
334 * \param[in] bit_depth bit depth
335 * \param[in] update_type_scale_factors array of scale factors for each
336 * update_type
337 * \param[in] frame_count size of update_type_list,
338 * qstep_ratio_list stats_list,
339 * q_index_list and
340 * estimated_bitrate_byframe
341 * \param[in] update_type_list array of update_type, one per frame
342 * \param[in] qstep_ratio_list array of qstep_ratio, one per frame
343 * \param[in] stats_list array of transform stats, one per
344 * 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
347 * bitrate
348 *
349 * \return The estimated GOP bitrate.
350 *
351 */
352 double av1_vbr_rc_info_estimate_gop_bitrate(
353 int base_q_index, aom_bit_depth_t bit_depth,
354 const double *update_type_scale_factors, int frame_count,
355 const FRAME_UPDATE_TYPE *update_type_list, const double *qstep_ratio_list,
356 const TplTxfmStats *stats_list, int *q_index_list,
357 double *estimated_bitrate_byframe);
358
359 /*!\brief Estimate the optimal base q index for a GOP.
360 *
361 * This function uses a binary search to find base layer q index to
362 * achieve the specified bit budget.
363 *
364 * \param[in] bit_budget target bit budget
365 * \param[in] bit_depth bit depth
366 * \param[in] update_type_scale_factors array of scale factors for each
367 * update_type
368 * \param[in] frame_count size of update_type_list, qstep_ratio_list
369 * stats_list, q_index_list and
370 * estimated_bitrate_byframe
371 * \param[in] update_type_list array of update_type, one per frame
372 * \param[in] qstep_ratio_list array of qstep_ratio, one per frame
373 * \param[in] stats_list array of transform stats, one per frame
374 * \param[out] q_index_list array of q_index, one per frame
375 * \param[out] estimated_bitrate_byframe Array to keep track of frame
376 * bitrate
377 *
378 * \return Returns the optimal base q index to use.
379 */
380 int av1_vbr_rc_info_estimate_base_q(
381 double bit_budget, aom_bit_depth_t bit_depth,
382 const double *update_type_scale_factors, int frame_count,
383 const FRAME_UPDATE_TYPE *update_type_list, const double *qstep_ratio_list,
384 const TplTxfmStats *stats_list, int *q_index_list,
385 double *estimated_bitrate_byframe);
386
387 #endif // CONFIG_BITRATE_ACCURACY
388
389 #if CONFIG_RD_COMMAND
390 typedef enum {
391 RD_OPTION_NONE,
392 RD_OPTION_SET_Q,
393 RD_OPTION_SET_Q_RDMULT
394 } RD_OPTION;
395
396 typedef struct RD_COMMAND {
397 RD_OPTION option_ls[MAX_LENGTH_TPL_FRAME_STATS];
398 int q_index_ls[MAX_LENGTH_TPL_FRAME_STATS];
399 int rdmult_ls[MAX_LENGTH_TPL_FRAME_STATS];
400 int frame_count;
401 int frame_index;
402 } RD_COMMAND;
403
404 void av1_read_rd_command(const char *filepath, RD_COMMAND *rd_command);
405 #endif // CONFIG_RD_COMMAND
406
407 /*!\brief Allocate buffers used by tpl model
408 *
409 * \param[in] Top-level encode/decode structure
410 * \param[in] lag_in_frames number of lookahead frames
411 *
412 * \param[out] tpl_data tpl data structure
413 */
414
415 void av1_setup_tpl_buffers(struct AV1_PRIMARY *const ppi,
416 CommonModeInfoParams *const mi_params, int width,
417 int height, int byte_alignment, int lag_in_frames);
418
tpl_dealloc_temp_buffers(TplBuffers * tpl_tmp_buffers)419 static inline void tpl_dealloc_temp_buffers(TplBuffers *tpl_tmp_buffers) {
420 aom_free(tpl_tmp_buffers->predictor8);
421 tpl_tmp_buffers->predictor8 = NULL;
422 aom_free(tpl_tmp_buffers->src_diff);
423 tpl_tmp_buffers->src_diff = NULL;
424 aom_free(tpl_tmp_buffers->coeff);
425 tpl_tmp_buffers->coeff = NULL;
426 aom_free(tpl_tmp_buffers->qcoeff);
427 tpl_tmp_buffers->qcoeff = NULL;
428 aom_free(tpl_tmp_buffers->dqcoeff);
429 tpl_tmp_buffers->dqcoeff = NULL;
430 }
431
tpl_alloc_temp_buffers(TplBuffers * tpl_tmp_buffers,uint8_t tpl_bsize_1d)432 static inline bool tpl_alloc_temp_buffers(TplBuffers *tpl_tmp_buffers,
433 uint8_t tpl_bsize_1d) {
434 // Number of pixels in a tpl block
435 const int tpl_block_pels = tpl_bsize_1d * tpl_bsize_1d;
436
437 // Allocate temporary buffers used in mode estimation.
438 tpl_tmp_buffers->predictor8 = (uint8_t *)aom_memalign(
439 32, tpl_block_pels * 2 * sizeof(*tpl_tmp_buffers->predictor8));
440 tpl_tmp_buffers->src_diff = (int16_t *)aom_memalign(
441 32, tpl_block_pels * sizeof(*tpl_tmp_buffers->src_diff));
442 tpl_tmp_buffers->coeff = (tran_low_t *)aom_memalign(
443 32, tpl_block_pels * sizeof(*tpl_tmp_buffers->coeff));
444 tpl_tmp_buffers->qcoeff = (tran_low_t *)aom_memalign(
445 32, tpl_block_pels * sizeof(*tpl_tmp_buffers->qcoeff));
446 tpl_tmp_buffers->dqcoeff = (tran_low_t *)aom_memalign(
447 32, tpl_block_pels * sizeof(*tpl_tmp_buffers->dqcoeff));
448
449 if (!(tpl_tmp_buffers->predictor8 && tpl_tmp_buffers->src_diff &&
450 tpl_tmp_buffers->coeff && tpl_tmp_buffers->qcoeff &&
451 tpl_tmp_buffers->dqcoeff)) {
452 tpl_dealloc_temp_buffers(tpl_tmp_buffers);
453 return false;
454 }
455 return true;
456 }
457
458 /*!\brief Implements temporal dependency modelling for a GOP (GF/ARF
459 * group) and selects between 16 and 32 frame GOP structure.
460 *
461 *\ingroup tpl_modelling
462 *
463 * \param[in] cpi Top - level encoder instance structure
464 * \param[in] gop_eval Flag if it is in the GOP length decision stage
465 * \param[in] frame_params Per frame encoding parameters
466 *
467 * \return Indicates whether or not we should use a longer GOP length.
468 */
469 int av1_tpl_setup_stats(struct AV1_COMP *cpi, int gop_eval,
470 const struct EncodeFrameParams *const frame_params);
471
472 /*!\cond */
473
474 void av1_tpl_preload_rc_estimate(
475 struct AV1_COMP *cpi, const struct EncodeFrameParams *const frame_params);
476
477 int av1_tpl_ptr_pos(int mi_row, int mi_col, int stride, uint8_t right_shift);
478
479 void av1_init_tpl_stats(TplParams *const tpl_data);
480
481 int av1_tpl_stats_ready(const TplParams *tpl_data, int gf_frame_index);
482
483 void av1_tpl_rdmult_setup(struct AV1_COMP *cpi);
484
485 void av1_tpl_rdmult_setup_sb(struct AV1_COMP *cpi, MACROBLOCK *const x,
486 BLOCK_SIZE sb_size, int mi_row, int mi_col);
487
488 void av1_mc_flow_dispenser_row(struct AV1_COMP *cpi,
489 TplTxfmStats *tpl_txfm_stats,
490 TplBuffers *tpl_tmp_buffers, MACROBLOCK *x,
491 int mi_row, BLOCK_SIZE bsize, TX_SIZE tx_size);
492
493 /*!\brief Compute the entropy of an exponential probability distribution
494 * function (pdf) subjected to uniform quantization.
495 *
496 * pdf(x) = b*exp(-b*x)
497 *
498 *\ingroup tpl_modelling
499 *
500 * \param[in] q_step quantizer step size
501 * \param[in] b parameter of exponential distribution
502 *
503 * \return entropy cost
504 */
505 double av1_exponential_entropy(double q_step, double b);
506
507 /*!\brief Compute the entropy of a Laplace probability distribution
508 * function (pdf) subjected to non-uniform quantization.
509 *
510 * pdf(x) = 0.5*b*exp(-0.5*b*|x|)
511 *
512 *\ingroup tpl_modelling
513 *
514 * \param[in] q_step quantizer step size for non-zero bins
515 * \param[in] b parameter of Laplace distribution
516 * \param[in] zero_bin_ratio zero bin's size is zero_bin_ratio * q_step
517 *
518 * \return entropy cost
519 */
520 double av1_laplace_entropy(double q_step, double b, double zero_bin_ratio);
521
522 /*!\brief Compute the frame rate using transform block stats
523 *
524 * Assume each position i in the transform block is of Laplace distribution
525 * with mean absolute deviation abs_coeff_mean[i]
526 *
527 * Then we can use av1_laplace_entropy() to compute the expected frame
528 * rate.
529 *
530 *\ingroup tpl_modelling
531 *
532 * \param[in] q_index quantizer index
533 * \param[in] block_count number of transform blocks
534 * \param[in] abs_coeff_mean array of mean absolute deviation
535 * \param[in] coeff_num number of coefficients per transform block
536 *
537 * \return expected frame rate
538 */
539 double av1_laplace_estimate_frame_rate(int q_index, int block_count,
540 const double *abs_coeff_mean,
541 int coeff_num);
542
543 /*
544 *!\brief Init TplTxfmStats
545 *
546 * \param[in] tpl_txfm_stats a structure for storing transform stats
547 *
548 */
549 void av1_init_tpl_txfm_stats(TplTxfmStats *tpl_txfm_stats);
550
551 #if CONFIG_BITRATE_ACCURACY
552 /*
553 *!\brief Accumulate TplTxfmStats
554 *
555 * \param[in] sub_stats a structure for storing sub transform stats
556 * \param[out] accumulated_stats a structure for storing accumulated
557 *transform stats
558 *
559 */
560 void av1_accumulate_tpl_txfm_stats(const TplTxfmStats *sub_stats,
561 TplTxfmStats *accumulated_stats);
562
563 /*
564 *!\brief Record a transform block into TplTxfmStats
565 *
566 * \param[in] tpl_txfm_stats A structure for storing transform stats
567 * \param[out] coeff An array of transform coefficients. Its size
568 * should equal to tpl_txfm_stats.coeff_num.
569 *
570 */
571 void av1_record_tpl_txfm_block(TplTxfmStats *tpl_txfm_stats,
572 const tran_low_t *coeff);
573
574 /*
575 *!\brief Update abs_coeff_mean and ready of txfm_stats
576 * If txfm_block_count > 0, this function will use abs_coeff_sum and
577 * txfm_block_count to compute abs_coeff_mean. Moreover, reday flag
578 * will be set to one.
579 *
580 * \param[in] txfm_stats A structure for storing transform stats
581 */
582 void av1_tpl_txfm_stats_update_abs_coeff_mean(TplTxfmStats *txfm_stats);
583 #endif // CONFIG_BITRATE_ACCURACY
584
585 /*!\brief Estimate coefficient entropy using Laplace dsitribution
586 *
587 *\ingroup tpl_modelling
588 *
589 * This function is equivalent to -log2(laplace_prob()), where laplace_prob()
590 *is defined in tpl_model_test.cc
591 *
592 * \param[in] q_step quantizer step size without any scaling
593 * \param[in] b mean absolute deviation of Laplace
594 *distribution \param[in] zero_bin_ratio zero bin's size is zero_bin_ratio
595 ** q_step \param[in] qcoeff quantized coefficient
596 *
597 * \return estimated coefficient entropy
598 *
599 */
600 double av1_estimate_coeff_entropy(double q_step, double b,
601 double zero_bin_ratio, int qcoeff);
602
603 // TODO(angiebird): Add doxygen description here.
604 int64_t av1_delta_rate_cost(int64_t delta_rate, int64_t recrf_dist,
605 int64_t srcrf_dist, int pix_num);
606
607 /*!\brief Compute the overlap area between two blocks with the same size
608 *
609 *\ingroup tpl_modelling
610 *
611 * If there is no overlap, this function should return zero.
612 *
613 * \param[in] row_a row position of the first block
614 * \param[in] col_a column position of the first block
615 * \param[in] row_b row position of the second block
616 * \param[in] col_b column position of the second block
617 * \param[in] width width shared by the two blocks
618 * \param[in] height height shared by the two blocks
619 *
620 * \return overlap area of the two blocks
621 */
622 int av1_get_overlap_area(int row_a, int col_a, int row_b, int col_b, int width,
623 int height);
624
625 /*!\brief Get current frame's q_index from tpl stats and leaf_qindex
626 *
627 * \param[in] tpl_data TPL struct
628 * \param[in] gf_frame_index current frame index in the GOP
629 * \param[in] leaf_qindex q index of leaf frame
630 * \param[in] bit_depth bit depth
631 *
632 * \return q_index
633 */
634 int av1_tpl_get_q_index(const TplParams *tpl_data, int gf_frame_index,
635 int leaf_qindex, aom_bit_depth_t bit_depth);
636
637 /*!\brief Compute the frame importance from TPL stats
638 *
639 * \param[in] tpl_data TPL struct
640 * \param[in] gf_frame_index current frame index in the GOP
641 *
642 * \return frame_importance
643 */
644 double av1_tpl_get_frame_importance(const TplParams *tpl_data,
645 int gf_frame_index);
646
647 /*!\brief Compute the ratio between arf q step and the leaf q step based on
648 * TPL stats
649 *
650 * \param[in] tpl_data TPL struct
651 * \param[in] gf_frame_index current frame index in the GOP
652 * \param[in] leaf_qindex q index of leaf frame
653 * \param[in] bit_depth bit depth
654 *
655 * \return qstep_ratio
656 */
657 double av1_tpl_get_qstep_ratio(const TplParams *tpl_data, int gf_frame_index);
658
659 /*!\brief Find a q index whose step size is near qstep_ratio * leaf_qstep
660 *
661 * \param[in] leaf_qindex q index of leaf frame
662 * \param[in] qstep_ratio step ratio between target q index and
663 * leaf q index \param[in] bit_depth bit depth
664 *
665 * \return q_index
666 */
667 int av1_get_q_index_from_qstep_ratio(int leaf_qindex, double qstep_ratio,
668 aom_bit_depth_t bit_depth);
669
670 /*!\brief Improve the motion vector estimation by taking neighbors into
671 * account.
672 *
673 * Use the upper and left neighbor block as the reference MVs.
674 * Compute the minimum difference between current MV and reference MV.
675 *
676 * \param[in] tpl_frame Tpl frame struct
677 * \param[in] row Current row
678 * \param[in] col Current column
679 * \param[in] step Step parameter for av1_tpl_ptr_pos
680 * \param[in] tpl_stride Stride parameter for av1_tpl_ptr_pos
681 * \param[in] right_shift Right shift parameter for
682 * av1_tpl_ptr_pos
683 */
684 int_mv av1_compute_mv_difference(const TplDepFrame *tpl_frame, int row, int col,
685 int step, int tpl_stride, int right_shift);
686
687 /*!\brief Compute the entropy of motion vectors for a single frame.
688 *
689 * \param[in] tpl_frame TPL frame struct
690 * \param[in] right_shift right shift value for step
691 *
692 * \return Bits used by the motion vectors for one frame.
693 */
694 double av1_tpl_compute_frame_mv_entropy(const TplDepFrame *tpl_frame,
695 uint8_t right_shift);
696
697 #if CONFIG_RATECTRL_LOG
698 typedef struct {
699 int coding_frame_count;
700 int base_q_index;
701
702 // Encode decision
703 int q_index_list[VBR_RC_INFO_MAX_FRAMES];
704 double qstep_ratio_list[VBR_RC_INFO_MAX_FRAMES];
705 FRAME_UPDATE_TYPE update_type_list[VBR_RC_INFO_MAX_FRAMES];
706
707 // Frame stats
708 TplTxfmStats txfm_stats_list[VBR_RC_INFO_MAX_FRAMES];
709
710 // Estimated encode results
711 double est_coeff_rate_list[VBR_RC_INFO_MAX_FRAMES];
712
713 // Actual encode results
714 double act_rate_list[VBR_RC_INFO_MAX_FRAMES];
715 double act_coeff_rate_list[VBR_RC_INFO_MAX_FRAMES];
716 } RATECTRL_LOG;
717
rc_log_init(RATECTRL_LOG * rc_log)718 static inline void rc_log_init(RATECTRL_LOG *rc_log) { av1_zero(*rc_log); }
719
rc_log_frame_stats(RATECTRL_LOG * rc_log,int coding_index,const TplTxfmStats * txfm_stats)720 static inline void rc_log_frame_stats(RATECTRL_LOG *rc_log, int coding_index,
721 const TplTxfmStats *txfm_stats) {
722 rc_log->txfm_stats_list[coding_index] = *txfm_stats;
723 }
724
rc_log_frame_encode_param(RATECTRL_LOG * rc_log,int coding_index,double qstep_ratio,int q_index,FRAME_UPDATE_TYPE update_type)725 static inline void rc_log_frame_encode_param(RATECTRL_LOG *rc_log,
726 int coding_index,
727 double qstep_ratio, int q_index,
728 FRAME_UPDATE_TYPE update_type) {
729 rc_log->qstep_ratio_list[coding_index] = qstep_ratio;
730 rc_log->q_index_list[coding_index] = q_index;
731 rc_log->update_type_list[coding_index] = update_type;
732 const TplTxfmStats *txfm_stats = &rc_log->txfm_stats_list[coding_index];
733 rc_log->est_coeff_rate_list[coding_index] = 0;
734 if (txfm_stats->ready) {
735 rc_log->est_coeff_rate_list[coding_index] = av1_laplace_estimate_frame_rate(
736 q_index, txfm_stats->txfm_block_count, txfm_stats->abs_coeff_mean,
737 txfm_stats->coeff_num);
738 }
739 }
740
rc_log_frame_entropy(RATECTRL_LOG * rc_log,int coding_index,double act_rate,double act_coeff_rate)741 static inline void rc_log_frame_entropy(RATECTRL_LOG *rc_log, int coding_index,
742 double act_rate,
743 double act_coeff_rate) {
744 rc_log->act_rate_list[coding_index] = act_rate;
745 rc_log->act_coeff_rate_list[coding_index] = act_coeff_rate;
746 }
747
rc_log_record_chunk_info(RATECTRL_LOG * rc_log,int base_q_index,int coding_frame_count)748 static inline void rc_log_record_chunk_info(RATECTRL_LOG *rc_log,
749 int base_q_index,
750 int coding_frame_count) {
751 rc_log->base_q_index = base_q_index;
752 rc_log->coding_frame_count = coding_frame_count;
753 }
754
rc_log_show(const RATECTRL_LOG * rc_log)755 static inline void rc_log_show(const RATECTRL_LOG *rc_log) {
756 printf("= chunk 1\n");
757 printf("coding_frame_count %d base_q_index %d\n", rc_log->coding_frame_count,
758 rc_log->base_q_index);
759 printf("= frame %d\n", rc_log->coding_frame_count);
760 for (int coding_idx = 0; coding_idx < rc_log->coding_frame_count;
761 coding_idx++) {
762 printf(
763 "coding_idx %d update_type %d q %d qstep_ratio %f est_coeff_rate %f "
764 "act_coeff_rate %f act_rate %f\n",
765 coding_idx, rc_log->update_type_list[coding_idx],
766 rc_log->q_index_list[coding_idx], rc_log->qstep_ratio_list[coding_idx],
767 rc_log->est_coeff_rate_list[coding_idx],
768 rc_log->act_coeff_rate_list[coding_idx],
769 rc_log->act_rate_list[coding_idx]);
770 }
771 }
772 #endif // CONFIG_RATECTRL_LOG
773
774 /*!\endcond */
775 #ifdef __cplusplus
776 } // extern "C"
777 #endif
778
779 #endif // AOM_AV1_ENCODER_TPL_MODEL_H_
780