Coverage Report

Created: 2025-08-28 07:12

/src/libvpx/vp9/common/vp9_mvref_common.c
Line
Count
Source (jump to first uncovered line)
1
2
/*
3
 *  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
4
 *
5
 *  Use of this source code is governed by a BSD-style license
6
 *  that can be found in the LICENSE file in the root of the source
7
 *  tree. An additional intellectual property rights grant can be found
8
 *  in the file PATENTS.  All contributing project authors may
9
 *  be found in the AUTHORS file in the root of the source tree.
10
 */
11
12
#include "vp9/common/vp9_mvref_common.h"
13
14
// This function searches the neighborhood of a given MB/SB
15
// to try and find candidate reference vectors.
16
static void find_mv_refs_idx(const VP9_COMMON *cm, const MACROBLOCKD *xd,
17
                             MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
18
                             int_mv *mv_ref_list, int block, int mi_row,
19
32.0M
                             int mi_col, uint8_t *mode_context) {
20
32.0M
  const int *ref_sign_bias = cm->ref_frame_sign_bias;
21
32.0M
  int i, refmv_count = 0;
22
32.0M
  const POSITION *const mv_ref_search = mv_ref_blocks[mi->sb_type];
23
32.0M
  int different_ref_found = 0;
24
32.0M
  int context_counter = 0;
25
32.0M
  const MV_REF *const prev_frame_mvs =
26
32.0M
      cm->use_prev_frame_mvs
27
32.0M
          ? cm->prev_frame->mvs + mi_row * cm->mi_cols + mi_col
28
32.0M
          : NULL;
29
32.0M
  const TileInfo *const tile = &xd->tile;
30
31
  // Blank the reference vector list
32
32.0M
  memset(mv_ref_list, 0, sizeof(*mv_ref_list) * MAX_MV_REF_CANDIDATES);
33
34
  // The nearest 2 blocks are treated differently
35
  // if the size < 8x8 we get the mv from the bmi substructure,
36
  // and we also need to keep a mode count.
37
94.7M
  for (i = 0; i < 2; ++i) {
38
64.0M
    const POSITION *const mv_ref = &mv_ref_search[i];
39
64.0M
    if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
40
53.3M
      const MODE_INFO *const candidate_mi =
41
53.3M
          xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
42
      // Keep counts for entropy encoding.
43
53.3M
      context_counter += mode_2_counter[candidate_mi->mode];
44
53.3M
      different_ref_found = 1;
45
46
53.3M
      if (candidate_mi->ref_frame[0] == ref_frame)
47
9.53M
        ADD_MV_REF_LIST(get_sub_block_mv(candidate_mi, 0, mv_ref->col, block),
48
53.3M
                        refmv_count, mv_ref_list, Done);
49
43.8M
      else if (candidate_mi->ref_frame[1] == ref_frame)
50
0
        ADD_MV_REF_LIST(get_sub_block_mv(candidate_mi, 1, mv_ref->col, block),
51
53.3M
                        refmv_count, mv_ref_list, Done);
52
53.3M
    }
53
64.0M
  }
54
55
  // Check the rest of the neighbors in much the same way
56
  // as before except we don't need to keep track of sub blocks or
57
  // mode counts.
58
204M
  for (; i < MVREF_NEIGHBOURS; ++i) {
59
176M
    const POSITION *const mv_ref = &mv_ref_search[i];
60
176M
    if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
61
111M
      const MODE_INFO *const candidate_mi =
62
111M
          xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
63
111M
      different_ref_found = 1;
64
65
111M
      if (candidate_mi->ref_frame[0] == ref_frame)
66
13.8M
        ADD_MV_REF_LIST(candidate_mi->mv[0], refmv_count, mv_ref_list, Done);
67
97.7M
      else if (candidate_mi->ref_frame[1] == ref_frame)
68
0
        ADD_MV_REF_LIST(candidate_mi->mv[1], refmv_count, mv_ref_list, Done);
69
111M
    }
70
176M
  }
71
72
  // Check the last frame's mode and mv info.
73
28.0M
  if (cm->use_prev_frame_mvs) {
74
28.0M
    if (prev_frame_mvs->ref_frame[0] == ref_frame) {
75
3.80M
      ADD_MV_REF_LIST(prev_frame_mvs->mv[0], refmv_count, mv_ref_list, Done);
76
24.2M
    } else if (prev_frame_mvs->ref_frame[1] == ref_frame) {
77
0
      ADD_MV_REF_LIST(prev_frame_mvs->mv[1], refmv_count, mv_ref_list, Done);
78
0
    }
79
28.0M
  }
80
81
  // Since we couldn't find 2 mvs from the same reference frame
82
  // go back through the neighbors and find motion vectors from
83
  // different reference frames.
84
26.8M
  if (different_ref_found) {
85
199M
    for (i = 0; i < MVREF_NEIGHBOURS; ++i) {
86
179M
      const POSITION *mv_ref = &mv_ref_search[i];
87
179M
      if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
88
124M
        const MODE_INFO *const candidate_mi =
89
124M
            xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
90
91
        // If the candidate is INTRA we don't want to consider its mv.
92
124M
        IF_DIFF_REF_FRAME_ADD_MV(candidate_mi, ref_frame, ref_sign_bias,
93
124M
                                 refmv_count, mv_ref_list, Done);
94
124M
      }
95
179M
    }
96
25.7M
  }
97
98
  // Since we still don't have a candidate we'll try the last frame.
99
21.5M
  if (cm->use_prev_frame_mvs) {
100
21.5M
    if (prev_frame_mvs->ref_frame[0] != ref_frame &&
101
21.5M
        prev_frame_mvs->ref_frame[0] > INTRA_FRAME) {
102
4.05M
      int_mv mv = prev_frame_mvs->mv[0];
103
4.05M
      if (ref_sign_bias[prev_frame_mvs->ref_frame[0]] !=
104
4.05M
          ref_sign_bias[ref_frame]) {
105
0
        mv.as_mv.row *= -1;
106
0
        mv.as_mv.col *= -1;
107
0
      }
108
4.05M
      ADD_MV_REF_LIST(mv, refmv_count, mv_ref_list, Done);
109
4.05M
    }
110
111
19.7M
    if (prev_frame_mvs->ref_frame[1] > INTRA_FRAME &&
112
19.7M
        prev_frame_mvs->ref_frame[1] != ref_frame &&
113
19.7M
        prev_frame_mvs->mv[1].as_int != prev_frame_mvs->mv[0].as_int) {
114
0
      int_mv mv = prev_frame_mvs->mv[1];
115
0
      if (ref_sign_bias[prev_frame_mvs->ref_frame[1]] !=
116
0
          ref_sign_bias[ref_frame]) {
117
0
        mv.as_mv.row *= -1;
118
0
        mv.as_mv.col *= -1;
119
0
      }
120
0
      ADD_MV_REF_LIST(mv, refmv_count, mv_ref_list, Done);
121
0
    }
122
19.7M
  }
123
124
32.0M
Done:
125
126
32.0M
  mode_context[ref_frame] = counter_to_context[context_counter];
127
128
  // Clamp vectors
129
96.0M
  for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i)
130
64.0M
    clamp_mv_ref(&mv_ref_list[i].as_mv, xd);
131
32.0M
}
132
133
void vp9_find_mv_refs(const VP9_COMMON *cm, const MACROBLOCKD *xd,
134
                      MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
135
                      int_mv *mv_ref_list, int mi_row, int mi_col,
136
14.4M
                      uint8_t *mode_context) {
137
14.4M
  find_mv_refs_idx(cm, xd, mi, ref_frame, mv_ref_list, -1, mi_row, mi_col,
138
14.4M
                   mode_context);
139
14.4M
}
140
141
void vp9_find_best_ref_mvs(MACROBLOCKD *xd, int allow_hp, int_mv *mvlist,
142
14.4M
                           int_mv *nearest_mv, int_mv *near_mv) {
143
14.4M
  int i;
144
  // Make sure all the candidates are properly clamped etc
145
43.2M
  for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i) {
146
28.8M
    lower_mv_precision(&mvlist[i].as_mv, allow_hp);
147
28.8M
    clamp_mv2(&mvlist[i].as_mv, xd);
148
28.8M
  }
149
14.4M
  *nearest_mv = mvlist[0];
150
14.4M
  *near_mv = mvlist[1];
151
14.4M
}
152
153
void vp9_append_sub8x8_mvs_for_idx(VP9_COMMON *cm, MACROBLOCKD *xd, int block,
154
                                   int ref, int mi_row, int mi_col,
155
                                   int_mv *nearest_mv, int_mv *near_mv,
156
17.5M
                                   uint8_t *mode_context) {
157
17.5M
  int_mv mv_list[MAX_MV_REF_CANDIDATES];
158
17.5M
  MODE_INFO *const mi = xd->mi[0];
159
17.5M
  b_mode_info *bmi = mi->bmi;
160
17.5M
  int n;
161
162
17.5M
  assert(MAX_MV_REF_CANDIDATES == 2);
163
164
17.5M
  find_mv_refs_idx(cm, xd, mi, mi->ref_frame[ref], mv_list, block, mi_row,
165
17.5M
                   mi_col, mode_context);
166
167
17.5M
  near_mv->as_int = 0;
168
17.5M
  switch (block) {
169
6.11M
    case 0:
170
6.11M
      nearest_mv->as_int = mv_list[0].as_int;
171
6.11M
      near_mv->as_int = mv_list[1].as_int;
172
6.11M
      break;
173
4.47M
    case 1:
174
8.76M
    case 2:
175
8.76M
      nearest_mv->as_int = bmi[0].as_mv[ref].as_int;
176
14.1M
      for (n = 0; n < MAX_MV_REF_CANDIDATES; ++n)
177
12.5M
        if (nearest_mv->as_int != mv_list[n].as_int) {
178
7.22M
          near_mv->as_int = mv_list[n].as_int;
179
7.22M
          break;
180
7.22M
        }
181
8.76M
      break;
182
2.69M
    case 3: {
183
2.69M
      int_mv candidates[2 + MAX_MV_REF_CANDIDATES];
184
2.69M
      candidates[0] = bmi[1].as_mv[ref];
185
2.69M
      candidates[1] = bmi[0].as_mv[ref];
186
2.69M
      candidates[2] = mv_list[0];
187
2.69M
      candidates[3] = mv_list[1];
188
189
2.69M
      nearest_mv->as_int = bmi[2].as_mv[ref].as_int;
190
6.24M
      for (n = 0; n < 2 + MAX_MV_REF_CANDIDATES; ++n)
191
5.97M
        if (nearest_mv->as_int != candidates[n].as_int) {
192
2.42M
          near_mv->as_int = candidates[n].as_int;
193
2.42M
          break;
194
2.42M
        }
195
2.69M
      break;
196
4.47M
    }
197
0
    default: assert(0 && "Invalid block index.");
198
17.5M
  }
199
17.5M
}