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