Skip to content

Refactor handle_inter_mode and separate single and compound mode searches - #5239

Draft
kslu-aom wants to merge 6 commits into
AOMediaCodec:av2-encfrom
kslu-aom:av2enc-refactor2
Draft

Refactor handle_inter_mode and separate single and compound mode searches#5239
kslu-aom wants to merge 6 commits into
AOMediaCodec:av2-encfrom
kslu-aom:av2enc-refactor2

Conversation

@kslu-aom

@kslu-aom kslu-aom commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Reduce the search loop nesting by using a helper function for single and another for compound. The order of nested loops has been changed, so that a common search loop for MV precision is moved to higher level and remains in handle_inter_mode, while the other loops are moved to the helper functions. The double search loops for compound DRL and for BAWP are flattened as one single loop. Some pruning logic of skip_newmv are refactored using an inline function.

There is an ongoing effort to further unnest the loops inside handle_compound_inter_prediction.

Evaluation based on aba7683 with 33 frames

Speed 0 (1 point remaining for each set)

+---------+--------+--------+--------+--------+----------+----------+
| Summary |   Y    |   U    |   V    |  YUV   | Enc-time | Dec-time |
+---------+--------+--------+--------+--------+----------+----------+
| A1      | -0.05% | +0.00% | +0.28% | -0.03% | 99.4%    | 99.0%    |
| A2      | -0.03% | +0.04% | -0.20% | -0.03% | 100.4%   | 100.7%   |
+---------+--------+--------+--------+--------+----------+----------+

Speed 1

+---------+--------+--------+--------+--------+----------+----------+
| Summary |   Y    |   U    |   V    |  YUV   | Enc-time | Dec-time |
+---------+--------+--------+--------+--------+----------+----------+
| A1      | -0.01% | -0.02% | -0.10% | -0.02% | 100.8%   | 100.1%   |
| A2      | +0.01% | -0.20% | +0.02% | -0.00% |  99.2%   |  98.7%   |
+---------+--------+--------+--------+--------+----------+----------+

STATS_CHANGED

Combine ref_mv_id_1 and ref_mv_id_0 loops into a single flat_idx loop in handle_inter_mode.

TAG=agy
CONV=f3d7c114-6aba-4bcd-85bc-35cff99055d5
@kslu-aom
kslu-aom changed the base branch from main to av2-enc August 10, 2026 20:38
@kslu-aom

Copy link
Copy Markdown
Contributor Author

Please review this large scale refactoring and let me know. I can do another iteration based on the comments and suggestions, and mark it as ready once I have rebased it with new test results.

@kslu-aom
kslu-aom requested a review from jingninghan August 10, 2026 20:45
@jingninghan
jingninghan requested a review from jianle973 August 10, 2026 20:59
@yeqing-wu

Copy link
Copy Markdown
Contributor

Thanks Lester for helping refactor the code. I’ll review it today. With so many internal and external code reviews, I still prefer that we review the code carefully to identify issues as early as possible.

@yunqingwang1

Copy link
Copy Markdown
Contributor

@kslu-aom Thanks for the refactoring. Regarding "The double search loops for compound DRL and for BAWP are flattened as one single loop", separating single and compound mode search is needed to achieve this loop flattening?

@kslu-aom

Copy link
Copy Markdown
Contributor Author

@kslu-aom Thanks for the refactoring. Regarding "The double search loops for compound DRL and for BAWP are flattened as one single loop", separating single and compound mode search is needed to achieve this loop flattening?

No, it is a separate cleanup with the intention to reduce the nesting.

@yunqingwang1

Copy link
Copy Markdown
Contributor

Looks good - thanks.

@yeqing-wu yeqing-wu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Lester. Hare are my comments.

Comment thread av2/encoder/rdopt.c Outdated
for (i = 0; i < is_comp_pred + 1; ++i) {
bawp_off_mv[i].as_int = cur_mv[i].as_int;
}
set_precision_set(cm, xd, mbmi, bsize, 0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not need ref_mv_idx inside set_precision_set(), thus please help clean it up so that it can avoid one input parameter

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread av2/encoder/rdopt.c Outdated
mbmi->motion_mode = SIMPLE_TRANSLATION;
mbmi->ref_mv_idx[1] = ref_mv_idx[1];
mbmi->ref_mv_idx[0] = ref_mv_idx[0];
set_mv_precision(mbmi, mbmi->max_mv_precision);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the code in 5248 (mbmi->pb_mv_precision = pb_mv_precision;), I think set_mv_precision() call cite is redundant

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check other places since inside set_mv_precision(), it only has one line code to assign the mv precision, which is similar as mbmi->pb_mv_precision = pb_mv_precision;

void set_mv_precision(MB_MODE_INFO *mbmi, MvSubpelPrecision precision) {
  mbmi->pb_mv_precision = precision;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed a few of these.

Comment thread av2/encoder/rdopt.c Outdated
mbmi->ref_mv_idx[1] = 0;
mbmi->ref_mv_idx[0] = ref_mv_idx0;
int ref_mv_idx_type = av2_ref_mv_idx_type(mbmi, ref_mv_idx);
set_mv_precision(mbmi, mbmi->max_mv_precision);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the code in 5248 (mbmi->pb_mv_precision = pb_mv_precision;), I think set_mv_precision() call cite is redundant

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

Comment thread av2/encoder/rdopt.c Outdated
int_mv bawp_off_mv[2];
int64_t bawp_off_newmv_ret_val = 0;
int bawp_off_rate_mv = 0;
for (i = 0; i < is_comp_pred + 1; ++i) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_comp_pred is hardcoded to 0, thus I think we do not need this for loop. Meanwhile, as the function name indicates it is for single, I think we also do not need to is_comp_pred inside this function.

const int is_comp_pred = 0;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread av2/encoder/rdopt.c Outdated
if (mbmi->bawp_flag[0] >= 1) {
bawp_off_mv[1].as_int = -1;
assert(is_comp_pred == 0);
for (i = 0; i < is_comp_pred + 1; ++i) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread av2/encoder/rdopt.c Outdated
end_timing(cpi, handle_newmv_time);
#endif
bawp_off_rate_mv = rate_mv;
for (i = 0; i < is_comp_pred + 1; ++i) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread av2/encoder/rdopt.c
int prune_modes_based_on_tpl, int ref_match_found_in_above_nb,
int ref_match_found_in_left_nb,
PruneInfoFromTpl *inter_cost_info_from_tpl, int jmvd_scaling_factor_num,
PREDICTION_MODE best_ref_mode) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future, we need to think about how to refactor the code to avoid enormous parameter lists. Not for this PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will try to clean up some parameters in the next iteration

Comment thread av2/encoder/rdopt.c
av2_zero(cwp_search_mask);

const int total_ref_mv_idx = ref_set[1] * ref_set[0];
for (int flat_idx = 0; flat_idx < total_ref_mv_idx; ++flat_idx) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5209–5390 (single) vs 5422–5609 (compound), has lots of duplicate near-identical code. We can refactor it later, but add a TODO so that we would not forget this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. I have added a TODO

Comment thread av2/encoder/rdopt.c Outdated
bawp_off_mv[i].as_int = cur_mv[i].as_int;
}

int bawp_eanbled = cm->features.enable_bawp &&

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo, should be 'bawp_enabled'. Even it is from the existing code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread av2/encoder/rdopt.c Outdated

int rate_mv = 0;
int64_t newmv_ret_val = 0;
int i;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Loose int i; C-style declaration, Move to for (int i = 0; ...) at each use

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@yeqing-wu yeqing-wu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Lester. LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants