12#ifndef AOM_AV1_ENCODER_TPL_MODEL_H_
13#define AOM_AV1_ENCODER_TPL_MODEL_H_
23struct AV1_SEQ_CODING_TOOLS;
30#include "config/aom_config.h"
32#include "aom_scale/yv12config.h"
33#include "aom_util/aom_pthread.h"
35#include "av1/common/mv.h"
36#include "av1/common/scale.h"
39#include "av1/encoder/ratectrl.h"
41static inline BLOCK_SIZE convert_length_to_bsize(
int 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;
49 assert(0 &&
"Invalid block size for tpl model");
54typedef struct AV1TplRowMultiThreadSync {
57 pthread_mutex_t *mutex_;
58 pthread_cond_t *cond_;
63 int *num_finished_cols;
71 int num_threads_working;
72} AV1TplRowMultiThreadSync;
74typedef struct AV1TplRowMultiThreadInfo {
80 pthread_mutex_t *mutex_;
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,
86} AV1TplRowMultiThreadInfo;
99#define MAX_TPL_FRAME_IDX (2 * MAX_LAG_BUFFERS)
102#define MAX_LENGTH_TPL_FRAME_STATS (MAX_TPL_FRAME_IDX + REF_FRAMES + 1)
103#define TPL_DEP_COST_SCALE_LOG2 4
105#define TPL_EPSILON 0.0000001
107typedef struct TplTxfmStats {
109 double abs_coeff_sum[256];
110 double abs_coeff_mean[256];
111 int txfm_block_count;
123typedef struct TplDepStats {
130 int64_t cmp_recrf_dist[2];
133 int64_t pred_error[INTER_REFS_PER_FRAME];
139 int32_t cmp_recrf_rate[2];
140 int_mv mv[INTER_REFS_PER_FRAME];
141 int8_t ref_frame_index[2];
144typedef struct TplDepFrame {
146 TplDepStats *tpl_stats_ptr;
149 int ref_map_index[REF_FRAMES];
156 uint32_t frame_display_index;
217 struct scale_factors
sf;
255#if CONFIG_BITRATE_ACCURACY || CONFIG_RATECTRL_LOG
256#define VBR_RC_INFO_MAX_FRAMES 500
259#if CONFIG_BITRATE_ACCURACY
267 double total_bit_budget;
268 int show_frame_count;
270 int gop_showframe_count;
271 double gop_bit_budget;
272 double scale_factors[FRAME_UPDATE_TYPES];
274 double mv_scale_factors[FRAME_UPDATE_TYPES];
279 int q_index_list_ready;
280 int q_index_list[VBR_RC_INFO_MAX_FRAMES];
284 double qstep_ratio_list[VBR_RC_INFO_MAX_FRAMES];
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];
292 int total_frame_count;
297static 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);
302void av1_vbr_rc_init(VBR_RATECTRL_INFO *vbr_rc_info,
double total_bit_budget,
303 int show_frame_count);
305int av1_vbr_rc_frame_coding_idx(
const VBR_RATECTRL_INFO *vbr_rc_info,
308void av1_vbr_rc_append_tpl_info(VBR_RATECTRL_INFO *vbr_rc_info,
309 const struct TPL_INFO *tpl_info);
311void av1_vbr_rc_set_gop_bit_budget(VBR_RATECTRL_INFO *vbr_rc_info,
312 int gop_showframe_count);
314void av1_vbr_rc_compute_q_indices(
int base_q_index,
int frame_count,
315 const double *qstep_ratio_list,
326void av1_vbr_rc_update_q_index_list(VBR_RATECTRL_INFO *vbr_rc_info,
352double av1_vbr_rc_info_estimate_gop_bitrate(
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);
380int av1_vbr_rc_info_estimate_base_q(
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);
393 RD_OPTION_SET_Q_RDMULT
396typedef 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];
404void av1_read_rd_command(
const char *filepath, RD_COMMAND *rd_command);
415void av1_setup_tpl_buffers(
struct AV1_PRIMARY *
const ppi,
417 int height,
int byte_alignment,
int lag_in_frames);
419static 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;
432static inline bool tpl_alloc_temp_buffers(TplBuffers *tpl_tmp_buffers,
433 uint8_t tpl_bsize_1d) {
435 const int tpl_block_pels = tpl_bsize_1d * tpl_bsize_1d;
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));
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);
474void av1_tpl_preload_rc_estimate(
477int av1_tpl_ptr_pos(
int mi_row,
int mi_col,
int stride, uint8_t right_shift);
479void av1_init_tpl_stats(
TplParams *
const tpl_data);
481int av1_tpl_stats_ready(
const TplParams *tpl_data,
int gf_frame_index);
483void av1_tpl_rdmult_setup(
struct AV1_COMP *cpi);
486 BLOCK_SIZE sb_size,
int mi_row,
int mi_col);
488void av1_mc_flow_dispenser_row(
struct AV1_COMP *cpi,
489 TplTxfmStats *tpl_txfm_stats,
491 int mi_row, BLOCK_SIZE bsize, TX_SIZE tx_size);
505double av1_exponential_entropy(
double q_step,
double b);
520double av1_laplace_entropy(
double q_step,
double b,
double zero_bin_ratio);
539double av1_laplace_estimate_frame_rate(
int q_index,
int block_count,
540 const double *abs_coeff_mean,
549void av1_init_tpl_txfm_stats(TplTxfmStats *tpl_txfm_stats);
551#if CONFIG_BITRATE_ACCURACY
560void av1_accumulate_tpl_txfm_stats(
const TplTxfmStats *sub_stats,
561 TplTxfmStats *accumulated_stats);
571void av1_record_tpl_txfm_block(TplTxfmStats *tpl_txfm_stats,
572 const tran_low_t *coeff);
582void av1_tpl_txfm_stats_update_abs_coeff_mean(TplTxfmStats *txfm_stats);
600double av1_estimate_coeff_entropy(
double q_step,
double b,
601 double zero_bin_ratio,
int qcoeff);
604int64_t av1_delta_rate_cost(int64_t delta_rate, int64_t recrf_dist,
605 int64_t srcrf_dist,
int pix_num);
622int av1_get_overlap_area(
int row_a,
int col_a,
int row_b,
int col_b,
int width,
634int av1_tpl_get_q_index(
const TplParams *tpl_data,
int gf_frame_index,
644double av1_tpl_get_frame_importance(
const TplParams *tpl_data,
657double av1_tpl_get_qstep_ratio(
const TplParams *tpl_data,
int gf_frame_index);
667int av1_get_q_index_from_qstep_ratio(
int leaf_qindex,
double qstep_ratio,
684int_mv av1_compute_mv_difference(
const TplDepFrame *tpl_frame,
int row,
int col,
685 int step,
int tpl_stride,
int right_shift);
694double av1_tpl_compute_frame_mv_entropy(
const TplDepFrame *tpl_frame,
695 uint8_t right_shift);
697#if CONFIG_RATECTRL_LOG
699 int coding_frame_count;
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];
708 TplTxfmStats txfm_stats_list[VBR_RC_INFO_MAX_FRAMES];
711 double est_coeff_rate_list[VBR_RC_INFO_MAX_FRAMES];
714 double act_rate_list[VBR_RC_INFO_MAX_FRAMES];
715 double act_coeff_rate_list[VBR_RC_INFO_MAX_FRAMES];
718static inline void rc_log_init(RATECTRL_LOG *rc_log) { av1_zero(*rc_log); }
720static 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;
725static inline void rc_log_frame_encode_param(RATECTRL_LOG *rc_log,
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);
741static inline void rc_log_frame_entropy(RATECTRL_LOG *rc_log,
int coding_index,
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;
748static inline void rc_log_record_chunk_info(RATECTRL_LOG *rc_log,
750 int coding_frame_count) {
751 rc_log->base_q_index = base_q_index;
752 rc_log->coding_frame_count = coding_frame_count;
755static 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;
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]);
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:2873
Top level primary encoder structure.
Definition encoder.h:2577
Params related to MB_MODE_INFO arrays and related info.
Definition av1_common_int.h:508
contains per-frame encoding parameters decided upon by av1_encode_strategy() and passed down to av1_e...
Definition encoder.h:3687
Data related to the current GF/ARF group and the individual frames within the group.
Definition firstpass.h:339
Params related to temporal dependency model.
Definition tpl_model.h:165
const YV12_BUFFER_CONFIG * src_ref_frame[INTER_REFS_PER_FRAME]
Definition tpl_model.h:229
struct scale_factors sf
Definition tpl_model.h:217
int ready
Definition tpl_model.h:169
TplDepFrame tpl_stats_buffer[MAX_LENGTH_TPL_FRAME_STATS]
Definition tpl_model.h:186
uint8_t tpl_bsize_1d
Definition tpl_model.h:179
AV1TplRowMultiThreadSync tpl_mt_sync
Definition tpl_model.h:242
TplDepFrame * tpl_frame
Definition tpl_model.h:212
int border_in_pixels
Definition tpl_model.h:247
TplDepStats * tpl_stats_pool[MAX_LAG_BUFFERS]
Definition tpl_model.h:193
TplTxfmStats * txfm_stats_list
Definition tpl_model.h:201
YV12_BUFFER_CONFIG tpl_rec_pool[MAX_LAG_BUFFERS]
Definition tpl_model.h:207
uint8_t tpl_stats_block_mis_log2
Definition tpl_model.h:174
int frame_idx
Definition tpl_model.h:222
double r0_adjust_factor
Definition tpl_model.h:252
const YV12_BUFFER_CONFIG * ref_frame[INTER_REFS_PER_FRAME]
Definition tpl_model.h:236
Encoder's parameters related to the current coding block.
Definition block.h:878
YV12 frame buffer data structure.
Definition yv12config.h:46