12 #ifndef AOM_AV1_ENCODER_TPL_MODEL_H_
13 #define AOM_AV1_ENCODER_TPL_MODEL_H_
23 struct AV1_SEQ_CODING_TOOLS;
29 #include "config/aom_config.h"
31 #include "aom_scale/yv12config.h"
33 #include "av1/common/mv.h"
34 #include "av1/common/scale.h"
37 #include "av1/encoder/ratectrl.h"
39 static INLINE BLOCK_SIZE convert_length_to_bsize(
int length) {
41 case 64:
return BLOCK_64X64;
42 case 32:
return BLOCK_32X32;
43 case 16:
return BLOCK_16X16;
44 case 8:
return BLOCK_8X8;
45 case 4:
return BLOCK_4X4;
47 assert(0 &&
"Invalid block size for tpl model");
52 typedef struct AV1TplRowMultiThreadSync {
53 #if CONFIG_MULTITHREAD
55 pthread_mutex_t *mutex_;
56 pthread_cond_t *cond_;
61 int *num_finished_cols;
69 int num_threads_working;
70 } AV1TplRowMultiThreadSync;
72 typedef struct AV1TplRowMultiThreadInfo {
74 void (*sync_read_ptr)(AV1TplRowMultiThreadSync *tpl_mt_sync,
int r,
int c);
75 void (*sync_write_ptr)(AV1TplRowMultiThreadSync *tpl_mt_sync,
int r,
int c,
77 } AV1TplRowMultiThreadInfo;
90 #define MAX_TPL_FRAME_IDX (2 * MAX_LAG_BUFFERS)
93 #define MAX_LENGTH_TPL_FRAME_STATS (MAX_TPL_FRAME_IDX + REF_FRAMES + 1)
94 #define TPL_DEP_COST_SCALE_LOG2 4
96 #define TPL_EPSILON 0.0000001
98 typedef struct TplTxfmStats {
100 double abs_coeff_sum[256];
101 double abs_coeff_mean[256];
102 int txfm_block_count;
106 typedef struct TplDepStats {
113 int64_t cmp_recrf_dist[2];
116 int64_t pred_error[INTER_REFS_PER_FRAME];
122 int32_t cmp_recrf_rate[2];
123 int_mv mv[INTER_REFS_PER_FRAME];
124 int8_t ref_frame_index[2];
127 typedef struct TplDepFrame {
129 TplDepStats *tpl_stats_ptr;
132 int ref_map_index[REF_FRAMES];
139 uint32_t frame_display_index;
198 struct scale_factors
sf;
232 #if CONFIG_BITRATE_ACCURACY || CONFIG_RATECTRL_LOG
233 #define VBR_RC_INFO_MAX_FRAMES 500
236 #if CONFIG_BITRATE_ACCURACY
244 double total_bit_budget;
245 int show_frame_count;
247 int gop_showframe_count;
248 double gop_bit_budget;
249 double scale_factors[FRAME_UPDATE_TYPES];
251 double mv_scale_factors[FRAME_UPDATE_TYPES];
256 int q_index_list_ready;
257 int q_index_list[VBR_RC_INFO_MAX_FRAMES];
261 double qstep_ratio_list[VBR_RC_INFO_MAX_FRAMES];
263 #if CONFIG_THREE_PASS
264 TplTxfmStats txfm_stats_list[VBR_RC_INFO_MAX_FRAMES];
265 FRAME_UPDATE_TYPE update_type_list[VBR_RC_INFO_MAX_FRAMES];
266 int gop_start_idx_list[VBR_RC_INFO_MAX_FRAMES];
267 int gop_length_list[VBR_RC_INFO_MAX_FRAMES];
269 int total_frame_count;
274 static INLINE
void vbr_rc_reset_gop_data(VBR_RATECTRL_INFO *vbr_rc_info) {
275 vbr_rc_info->q_index_list_ready = 0;
276 av1_zero(vbr_rc_info->q_index_list);
279 void av1_vbr_rc_init(VBR_RATECTRL_INFO *vbr_rc_info,
double total_bit_budget,
280 int show_frame_count);
282 int av1_vbr_rc_frame_coding_idx(
const VBR_RATECTRL_INFO *vbr_rc_info,
285 void av1_vbr_rc_append_tpl_info(VBR_RATECTRL_INFO *vbr_rc_info,
286 const struct TPL_INFO *tpl_info);
288 void av1_vbr_rc_set_gop_bit_budget(VBR_RATECTRL_INFO *vbr_rc_info,
289 int gop_showframe_count);
291 void av1_vbr_rc_compute_q_indices(
int base_q_index,
int frame_count,
292 const double *qstep_ratio_list,
303 void av1_vbr_rc_update_q_index_list(VBR_RATECTRL_INFO *vbr_rc_info,
329 double av1_vbr_rc_info_estimate_gop_bitrate(
331 const double *update_type_scale_factors,
int frame_count,
332 const FRAME_UPDATE_TYPE *update_type_list,
const double *qstep_ratio_list,
333 const TplTxfmStats *stats_list,
int *q_index_list,
334 double *estimated_bitrate_byframe);
357 int av1_vbr_rc_info_estimate_base_q(
359 const double *update_type_scale_factors,
int frame_count,
360 const FRAME_UPDATE_TYPE *update_type_list,
const double *qstep_ratio_list,
361 const TplTxfmStats *stats_list,
int *q_index_list,
362 double *estimated_bitrate_byframe);
366 #if CONFIG_RD_COMMAND
370 RD_OPTION_SET_Q_RDMULT
373 typedef struct RD_COMMAND {
374 RD_OPTION option_ls[MAX_LENGTH_TPL_FRAME_STATS];
375 int q_index_ls[MAX_LENGTH_TPL_FRAME_STATS];
376 int rdmult_ls[MAX_LENGTH_TPL_FRAME_STATS];
381 void av1_read_rd_command(
const char *filepath, RD_COMMAND *rd_command);
392 void av1_setup_tpl_buffers(
struct AV1_PRIMARY *
const ppi,
394 int height,
int byte_alignment,
int lag_in_frames);
412 void av1_tpl_preload_rc_estimate(
415 int av1_tpl_ptr_pos(
int mi_row,
int mi_col,
int stride, uint8_t right_shift);
417 void av1_init_tpl_stats(
TplParams *
const tpl_data);
419 int av1_tpl_stats_ready(
const TplParams *tpl_data,
int gf_frame_index);
421 void av1_tpl_rdmult_setup(
struct AV1_COMP *cpi);
424 BLOCK_SIZE sb_size,
int mi_row,
int mi_col);
426 void av1_mc_flow_dispenser_row(
struct AV1_COMP *cpi,
428 int mi_row, BLOCK_SIZE bsize, TX_SIZE tx_size);
442 double av1_exponential_entropy(
double q_step,
double b);
457 double av1_laplace_entropy(
double q_step,
double b,
double zero_bin_ratio);
476 double av1_laplace_estimate_frame_rate(
int q_index,
int block_count,
477 const double *abs_coeff_mean,
486 void av1_init_tpl_txfm_stats(TplTxfmStats *tpl_txfm_stats);
496 void av1_accumulate_tpl_txfm_stats(
const TplTxfmStats *sub_stats,
497 TplTxfmStats *accumulated_stats);
507 void av1_record_tpl_txfm_block(TplTxfmStats *tpl_txfm_stats,
508 const tran_low_t *coeff);
518 void av1_tpl_txfm_stats_update_abs_coeff_mean(TplTxfmStats *txfm_stats);
535 double av1_estimate_coeff_entropy(
double q_step,
double b,
536 double zero_bin_ratio,
int qcoeff);
550 double av1_estimate_txfm_block_entropy(
int q_index,
551 const double *abs_coeff_mean,
552 int *qcoeff_arr,
int coeff_num);
555 int64_t av1_delta_rate_cost(int64_t delta_rate, int64_t recrf_dist,
556 int64_t srcrf_dist,
int pix_num);
573 int av1_get_overlap_area(
int row_a,
int col_a,
int row_b,
int col_b,
int width,
585 int av1_tpl_get_q_index(
const TplParams *tpl_data,
int gf_frame_index,
595 double av1_tpl_get_frame_importance(
const TplParams *tpl_data,
608 double av1_tpl_get_qstep_ratio(
const TplParams *tpl_data,
int gf_frame_index);
618 int av1_get_q_index_from_qstep_ratio(
int leaf_qindex,
double qstep_ratio,
635 int_mv av1_compute_mv_difference(
const TplDepFrame *tpl_frame,
int row,
int col,
636 int step,
int tpl_stride,
int right_shift);
645 double av1_tpl_compute_frame_mv_entropy(
const TplDepFrame *tpl_frame,
646 uint8_t right_shift);
648 #if CONFIG_RATECTRL_LOG
650 int coding_frame_count;
654 int q_index_list[VBR_RC_INFO_MAX_FRAMES];
655 double qstep_ratio_list[VBR_RC_INFO_MAX_FRAMES];
656 FRAME_UPDATE_TYPE update_type_list[VBR_RC_INFO_MAX_FRAMES];
659 TplTxfmStats txfm_stats_list[VBR_RC_INFO_MAX_FRAMES];
662 double est_coeff_rate_list[VBR_RC_INFO_MAX_FRAMES];
665 double act_rate_list[VBR_RC_INFO_MAX_FRAMES];
666 double act_coeff_rate_list[VBR_RC_INFO_MAX_FRAMES];
669 static INLINE
void rc_log_init(RATECTRL_LOG *rc_log) { av1_zero(*rc_log); }
671 static INLINE
void rc_log_frame_stats(RATECTRL_LOG *rc_log,
int coding_index,
672 const TplTxfmStats *txfm_stats) {
673 rc_log->txfm_stats_list[coding_index] = *txfm_stats;
676 static INLINE
void rc_log_frame_encode_param(RATECTRL_LOG *rc_log,
678 double qstep_ratio,
int q_index,
679 FRAME_UPDATE_TYPE update_type) {
680 rc_log->qstep_ratio_list[coding_index] = qstep_ratio;
681 rc_log->q_index_list[coding_index] = q_index;
682 rc_log->update_type_list[coding_index] = update_type;
683 const TplTxfmStats *txfm_stats = &rc_log->txfm_stats_list[coding_index];
684 rc_log->est_coeff_rate_list[coding_index] = 0;
685 if (txfm_stats->ready) {
686 rc_log->est_coeff_rate_list[coding_index] = av1_laplace_estimate_frame_rate(
687 q_index, txfm_stats->txfm_block_count, txfm_stats->abs_coeff_mean,
688 txfm_stats->coeff_num);
692 static INLINE
void rc_log_frame_entropy(RATECTRL_LOG *rc_log,
int coding_index,
694 double act_coeff_rate) {
695 rc_log->act_rate_list[coding_index] = act_rate;
696 rc_log->act_coeff_rate_list[coding_index] = act_coeff_rate;
699 static INLINE
void rc_log_record_chunk_info(RATECTRL_LOG *rc_log,
701 int coding_frame_count) {
702 rc_log->base_q_index = base_q_index;
703 rc_log->coding_frame_count = coding_frame_count;
706 static INLINE
void rc_log_show(
const RATECTRL_LOG *rc_log) {
707 printf(
"= chunk 1\n");
708 printf(
"coding_frame_count %d base_q_index %d\n", rc_log->coding_frame_count,
709 rc_log->base_q_index);
710 printf(
"= frame %d\n", rc_log->coding_frame_count);
711 for (
int coding_idx = 0; coding_idx < rc_log->coding_frame_count;
714 "coding_idx %d update_type %d q %d qstep_ratio %f est_coeff_rate %f "
715 "act_coeff_rate %f act_rate %f\n",
716 coding_idx, rc_log->update_type_list[coding_idx],
717 rc_log->q_index_list[coding_idx], rc_log->qstep_ratio_list[coding_idx],
718 rc_log->est_coeff_rate_list[coding_idx],
719 rc_log->act_coeff_rate_list[coding_idx],
720 rc_log->act_rate_list[coding_idx]);
enum aom_bit_depth aom_bit_depth_t
Bit depth for codecThis enumeration determines the bit depth of the codec.
int av1_tpl_setup_stats(struct AV1_COMP *cpi, int gop_eval, const struct EncodeFrameParams *const frame_params)
Implements temporal dependency modelling for a GOP (GF/ARF group) and selects between 16 and 32 frame...
Describes look ahead buffer operations.
Top level encoder structure.
Definition: encoder.h:2759
Top level primary encoder structure.
Definition: encoder.h:2469
Params related to MB_MODE_INFO arrays and related info.
Definition: av1_common_int.h:501
contains per-frame encoding parameters decided upon by av1_encode_strategy() and passed down to av1_e...
Definition: encoder.h:3482
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:146
const YV12_BUFFER_CONFIG * src_ref_frame[INTER_REFS_PER_FRAME]
Definition: tpl_model.h:210
struct scale_factors sf
Definition: tpl_model.h:198
int ready
Definition: tpl_model.h:150
TplDepFrame tpl_stats_buffer[MAX_LENGTH_TPL_FRAME_STATS]
Definition: tpl_model.h:167
uint8_t tpl_bsize_1d
Definition: tpl_model.h:160
AV1TplRowMultiThreadSync tpl_mt_sync
Definition: tpl_model.h:223
TplDepFrame * tpl_frame
Definition: tpl_model.h:193
int border_in_pixels
Definition: tpl_model.h:228
TplDepStats * tpl_stats_pool[MAX_LAG_BUFFERS]
Definition: tpl_model.h:174
TplTxfmStats * txfm_stats_list
Definition: tpl_model.h:182
YV12_BUFFER_CONFIG tpl_rec_pool[MAX_LAG_BUFFERS]
Definition: tpl_model.h:188
uint8_t tpl_stats_block_mis_log2
Definition: tpl_model.h:155
int frame_idx
Definition: tpl_model.h:203
const YV12_BUFFER_CONFIG * ref_frame[INTER_REFS_PER_FRAME]
Definition: tpl_model.h:217
Encoder's parameters related to the current coding block.
Definition: block.h:849
YV12 frame buffer data structure.
Definition: yv12config.h:39