Coverage Report

Created: 2026-09-07 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvpx/vp9/common/vp9_pred_common.c
Line
Count
Source
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_common.h"
13
#include "vp9/common/vp9_pred_common.h"
14
#include "vp9/common/vp9_seg_common.h"
15
16
531k
int vp9_compound_reference_allowed(const VP9_COMMON *cm) {
17
531k
  int i;
18
1.45M
  for (i = 1; i < REFS_PER_FRAME; ++i)
19
1.02M
    if (cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1]) return 1;
20
21
433k
  return 0;
22
531k
}
23
24
37.7k
void vp9_setup_compound_reference_mode(VP9_COMMON *cm) {
25
37.7k
  if (cm->ref_frame_sign_bias[LAST_FRAME] ==
26
37.7k
      cm->ref_frame_sign_bias[GOLDEN_FRAME]) {
27
22.8k
    cm->comp_fixed_ref = ALTREF_FRAME;
28
22.8k
    cm->comp_var_ref[0] = LAST_FRAME;
29
22.8k
    cm->comp_var_ref[1] = GOLDEN_FRAME;
30
22.8k
  } else if (cm->ref_frame_sign_bias[LAST_FRAME] ==
31
14.8k
             cm->ref_frame_sign_bias[ALTREF_FRAME]) {
32
8.96k
    cm->comp_fixed_ref = GOLDEN_FRAME;
33
8.96k
    cm->comp_var_ref[0] = LAST_FRAME;
34
8.96k
    cm->comp_var_ref[1] = ALTREF_FRAME;
35
8.96k
  } else {
36
5.86k
    cm->comp_fixed_ref = LAST_FRAME;
37
5.86k
    cm->comp_var_ref[0] = GOLDEN_FRAME;
38
5.86k
    cm->comp_var_ref[1] = ALTREF_FRAME;
39
5.86k
  }
40
37.7k
}
41
42
int vp9_get_reference_mode_context(const VP9_COMMON *cm,
43
14.6M
                                   const MACROBLOCKD *xd) {
44
14.6M
  int ctx;
45
14.6M
  const MODE_INFO *const above_mi = xd->above_mi;
46
14.6M
  const MODE_INFO *const left_mi = xd->left_mi;
47
14.6M
  const int has_above = !!above_mi;
48
14.6M
  const int has_left = !!left_mi;
49
  // Note:
50
  // The mode info data structure has a one element border above and to the
51
  // left of the entries corresponding to real macroblocks.
52
  // The prediction flags in these dummy entries are initialized to 0.
53
14.6M
  if (has_above && has_left) {  // both edges available
54
14.3M
    if (!has_second_ref(above_mi) && !has_second_ref(left_mi))
55
      // neither edge uses comp pred (0/1)
56
6.16M
      ctx = (above_mi->ref_frame[0] == cm->comp_fixed_ref) ^
57
6.16M
            (left_mi->ref_frame[0] == cm->comp_fixed_ref);
58
8.15M
    else if (!has_second_ref(above_mi))
59
      // one of two edges uses comp pred (2/3)
60
2.95M
      ctx = 2 + (above_mi->ref_frame[0] == cm->comp_fixed_ref ||
61
2.30M
                 !is_inter_block(above_mi));
62
5.19M
    else if (!has_second_ref(left_mi))
63
      // one of two edges uses comp pred (2/3)
64
2.89M
      ctx = 2 + (left_mi->ref_frame[0] == cm->comp_fixed_ref ||
65
2.24M
                 !is_inter_block(left_mi));
66
2.30M
    else  // both edges use comp pred (4)
67
2.30M
      ctx = 4;
68
14.3M
  } else if (has_above || has_left) {  // one edge available
69
341k
    const MODE_INFO *edge_mi = has_above ? above_mi : left_mi;
70
71
341k
    if (!has_second_ref(edge_mi))
72
      // edge does not use comp pred (0/1)
73
263k
      ctx = edge_mi->ref_frame[0] == cm->comp_fixed_ref;
74
77.8k
    else
75
      // edge uses comp pred (3)
76
77.8k
      ctx = 3;
77
341k
  } else {  // no edges available (1)
78
9.95k
    ctx = 1;
79
9.95k
  }
80
14.6M
  assert(ctx >= 0 && ctx < COMP_INTER_CONTEXTS);
81
14.6M
  return ctx;
82
14.6M
}
83
84
// Returns a context number for the given MB prediction signal
85
int vp9_get_pred_context_comp_ref_p(const VP9_COMMON *cm,
86
10.0M
                                    const MACROBLOCKD *xd) {
87
10.0M
  int pred_context;
88
10.0M
  const MODE_INFO *const above_mi = xd->above_mi;
89
10.0M
  const MODE_INFO *const left_mi = xd->left_mi;
90
10.0M
  const int above_in_image = !!above_mi;
91
10.0M
  const int left_in_image = !!left_mi;
92
93
  // Note:
94
  // The mode info data structure has a one element border above and to the
95
  // left of the entries corresponding to real macroblocks.
96
  // The prediction flags in these dummy entries are initialized to 0.
97
10.0M
  const int fix_ref_idx = cm->ref_frame_sign_bias[cm->comp_fixed_ref];
98
10.0M
  const int var_ref_idx = !fix_ref_idx;
99
100
10.0M
  if (above_in_image && left_in_image) {  // both edges available
101
9.61M
    const int above_intra = !is_inter_block(above_mi);
102
9.61M
    const int left_intra = !is_inter_block(left_mi);
103
104
9.61M
    if (above_intra && left_intra) {  // intra/intra (2)
105
341k
      pred_context = 2;
106
9.27M
    } else if (above_intra || left_intra) {  // intra/inter
107
1.36M
      const MODE_INFO *edge_mi = above_intra ? left_mi : above_mi;
108
109
1.36M
      if (!has_second_ref(edge_mi))  // single pred (1/3)
110
669k
        pred_context = 1 + 2 * (edge_mi->ref_frame[0] != cm->comp_var_ref[1]);
111
698k
      else  // comp pred (1/3)
112
698k
        pred_context =
113
698k
            1 + 2 * (edge_mi->ref_frame[var_ref_idx] != cm->comp_var_ref[1]);
114
7.90M
    } else {  // inter/inter
115
7.90M
      const int l_sg = !has_second_ref(left_mi);
116
7.90M
      const int a_sg = !has_second_ref(above_mi);
117
7.90M
      const MV_REFERENCE_FRAME vrfa =
118
7.90M
          a_sg ? above_mi->ref_frame[0] : above_mi->ref_frame[var_ref_idx];
119
7.90M
      const MV_REFERENCE_FRAME vrfl =
120
7.90M
          l_sg ? left_mi->ref_frame[0] : left_mi->ref_frame[var_ref_idx];
121
122
7.90M
      if (vrfa == vrfl && cm->comp_var_ref[1] == vrfa) {
123
1.20M
        pred_context = 0;
124
6.69M
      } else if (l_sg && a_sg) {  // single/single
125
3.06M
        if ((vrfa == cm->comp_fixed_ref && vrfl == cm->comp_var_ref[0]) ||
126
2.73M
            (vrfl == cm->comp_fixed_ref && vrfa == cm->comp_var_ref[0]))
127
648k
          pred_context = 4;
128
2.41M
        else if (vrfa == vrfl)
129
1.34M
          pred_context = 3;
130
1.06M
        else
131
1.06M
          pred_context = 1;
132
3.63M
      } else if (l_sg || a_sg) {  // single/comp
133
600k
        const MV_REFERENCE_FRAME vrfc = l_sg ? vrfa : vrfl;
134
600k
        const MV_REFERENCE_FRAME rfs = a_sg ? vrfa : vrfl;
135
600k
        if (vrfc == cm->comp_var_ref[1] && rfs != cm->comp_var_ref[1])
136
169k
          pred_context = 1;
137
430k
        else if (rfs == cm->comp_var_ref[1] && vrfc != cm->comp_var_ref[1])
138
65.0k
          pred_context = 2;
139
365k
        else
140
365k
          pred_context = 4;
141
3.03M
      } else if (vrfa == vrfl) {  // comp/comp
142
1.62M
        pred_context = 4;
143
1.62M
      } else {
144
1.40M
        pred_context = 2;
145
1.40M
      }
146
7.90M
    }
147
9.61M
  } else if (above_in_image || left_in_image) {  // one edge available
148
425k
    const MODE_INFO *edge_mi = above_in_image ? above_mi : left_mi;
149
150
425k
    if (!is_inter_block(edge_mi)) {
151
21.9k
      pred_context = 2;
152
403k
    } else {
153
403k
      if (has_second_ref(edge_mi))
154
351k
        pred_context =
155
351k
            4 * (edge_mi->ref_frame[var_ref_idx] != cm->comp_var_ref[1]);
156
52.1k
      else
157
52.1k
        pred_context = 3 * (edge_mi->ref_frame[0] != cm->comp_var_ref[1]);
158
403k
    }
159
425k
  } else {  // no edges available (2)
160
18.7k
    pred_context = 2;
161
18.7k
  }
162
10.0M
  assert(pred_context >= 0 && pred_context < REF_CONTEXTS);
163
164
10.0M
  return pred_context;
165
10.0M
}
166
167
23.6M
int vp9_get_pred_context_single_ref_p1(const MACROBLOCKD *xd) {
168
23.6M
  int pred_context;
169
23.6M
  const MODE_INFO *const above_mi = xd->above_mi;
170
23.6M
  const MODE_INFO *const left_mi = xd->left_mi;
171
23.6M
  const int has_above = !!above_mi;
172
23.6M
  const int has_left = !!left_mi;
173
  // Note:
174
  // The mode info data structure has a one element border above and to the
175
  // left of the entries corresponding to real macroblocks.
176
  // The prediction flags in these dummy entries are initialized to 0.
177
23.6M
  if (has_above && has_left) {  // both edges available
178
15.8M
    const int above_intra = !is_inter_block(above_mi);
179
15.8M
    const int left_intra = !is_inter_block(left_mi);
180
181
15.8M
    if (above_intra && left_intra) {  // intra/intra
182
1.77M
      pred_context = 2;
183
14.0M
    } else if (above_intra || left_intra) {  // intra/inter or inter/intra
184
2.26M
      const MODE_INFO *edge_mi = above_intra ? left_mi : above_mi;
185
2.26M
      if (!has_second_ref(edge_mi))
186
1.63M
        pred_context = 4 * (edge_mi->ref_frame[0] == LAST_FRAME);
187
636k
      else
188
636k
        pred_context = 1 + (edge_mi->ref_frame[0] == LAST_FRAME ||
189
431k
                            edge_mi->ref_frame[1] == LAST_FRAME);
190
11.7M
    } else {  // inter/inter
191
11.7M
      const int above_has_second = has_second_ref(above_mi);
192
11.7M
      const int left_has_second = has_second_ref(left_mi);
193
11.7M
      const MV_REFERENCE_FRAME above0 = above_mi->ref_frame[0];
194
11.7M
      const MV_REFERENCE_FRAME above1 = above_mi->ref_frame[1];
195
11.7M
      const MV_REFERENCE_FRAME left0 = left_mi->ref_frame[0];
196
11.7M
      const MV_REFERENCE_FRAME left1 = left_mi->ref_frame[1];
197
198
11.7M
      if (above_has_second && left_has_second) {
199
1.51M
        pred_context = 1 + (above0 == LAST_FRAME || above1 == LAST_FRAME ||
200
930k
                            left0 == LAST_FRAME || left1 == LAST_FRAME);
201
10.2M
      } else if (above_has_second || left_has_second) {
202
4.42M
        const MV_REFERENCE_FRAME rfs = !above_has_second ? above0 : left0;
203
4.42M
        const MV_REFERENCE_FRAME crf1 = above_has_second ? above0 : left0;
204
4.42M
        const MV_REFERENCE_FRAME crf2 = above_has_second ? above1 : left1;
205
206
4.42M
        if (rfs == LAST_FRAME)
207
2.37M
          pred_context = 3 + (crf1 == LAST_FRAME || crf2 == LAST_FRAME);
208
2.05M
        else
209
2.05M
          pred_context = (crf1 == LAST_FRAME || crf2 == LAST_FRAME);
210
5.85M
      } else {
211
5.85M
        pred_context = 2 * (above0 == LAST_FRAME) + 2 * (left0 == LAST_FRAME);
212
5.85M
      }
213
11.7M
    }
214
15.8M
  } else if (has_above || has_left) {  // one edge available
215
5.95M
    const MODE_INFO *edge_mi = has_above ? above_mi : left_mi;
216
5.95M
    if (!is_inter_block(edge_mi)) {  // intra
217
1.64M
      pred_context = 2;
218
4.30M
    } else {  // inter
219
4.30M
      if (!has_second_ref(edge_mi))
220
4.25M
        pred_context = 4 * (edge_mi->ref_frame[0] == LAST_FRAME);
221
50.3k
      else
222
50.3k
        pred_context = 1 + (edge_mi->ref_frame[0] == LAST_FRAME ||
223
38.1k
                            edge_mi->ref_frame[1] == LAST_FRAME);
224
4.30M
    }
225
5.95M
  } else {  // no edges available
226
1.89M
    pred_context = 2;
227
1.89M
  }
228
229
23.6M
  assert(pred_context >= 0 && pred_context < REF_CONTEXTS);
230
23.6M
  return pred_context;
231
23.6M
}
232
233
14.8M
int vp9_get_pred_context_single_ref_p2(const MACROBLOCKD *xd) {
234
14.8M
  int pred_context;
235
14.8M
  const MODE_INFO *const above_mi = xd->above_mi;
236
14.8M
  const MODE_INFO *const left_mi = xd->left_mi;
237
14.8M
  const int has_above = !!above_mi;
238
14.8M
  const int has_left = !!left_mi;
239
240
  // Note:
241
  // The mode info data structure has a one element border above and to the
242
  // left of the entries corresponding to real macroblocks.
243
  // The prediction flags in these dummy entries are initialized to 0.
244
14.8M
  if (has_above && has_left) {  // both edges available
245
8.87M
    const int above_intra = !is_inter_block(above_mi);
246
8.87M
    const int left_intra = !is_inter_block(left_mi);
247
248
8.87M
    if (above_intra && left_intra) {  // intra/intra
249
1.66M
      pred_context = 2;
250
7.21M
    } else if (above_intra || left_intra) {  // intra/inter or inter/intra
251
1.35M
      const MODE_INFO *edge_mi = above_intra ? left_mi : above_mi;
252
1.35M
      if (!has_second_ref(edge_mi)) {
253
1.13M
        if (edge_mi->ref_frame[0] == LAST_FRAME)
254
544k
          pred_context = 3;
255
592k
        else
256
592k
          pred_context = 4 * (edge_mi->ref_frame[0] == GOLDEN_FRAME);
257
1.13M
      } else {
258
221k
        pred_context = 1 + 2 * (edge_mi->ref_frame[0] == GOLDEN_FRAME ||
259
150k
                                edge_mi->ref_frame[1] == GOLDEN_FRAME);
260
221k
      }
261
5.85M
    } else {  // inter/inter
262
5.85M
      const int above_has_second = has_second_ref(above_mi);
263
5.85M
      const int left_has_second = has_second_ref(left_mi);
264
5.85M
      const MV_REFERENCE_FRAME above0 = above_mi->ref_frame[0];
265
5.85M
      const MV_REFERENCE_FRAME above1 = above_mi->ref_frame[1];
266
5.85M
      const MV_REFERENCE_FRAME left0 = left_mi->ref_frame[0];
267
5.85M
      const MV_REFERENCE_FRAME left1 = left_mi->ref_frame[1];
268
269
5.85M
      if (above_has_second && left_has_second) {
270
416k
        if (above0 == left0 && above1 == left1)
271
281k
          pred_context =
272
281k
              3 * (above0 == GOLDEN_FRAME || above1 == GOLDEN_FRAME ||
273
156k
                   left0 == GOLDEN_FRAME || left1 == GOLDEN_FRAME);
274
135k
        else
275
135k
          pred_context = 2;
276
5.43M
      } else if (above_has_second || left_has_second) {
277
2.38M
        const MV_REFERENCE_FRAME rfs = !above_has_second ? above0 : left0;
278
2.38M
        const MV_REFERENCE_FRAME crf1 = above_has_second ? above0 : left0;
279
2.38M
        const MV_REFERENCE_FRAME crf2 = above_has_second ? above1 : left1;
280
281
2.38M
        if (rfs == GOLDEN_FRAME)
282
229k
          pred_context = 3 + (crf1 == GOLDEN_FRAME || crf2 == GOLDEN_FRAME);
283
2.15M
        else if (rfs == ALTREF_FRAME)
284
517k
          pred_context = crf1 == GOLDEN_FRAME || crf2 == GOLDEN_FRAME;
285
1.64M
        else
286
1.64M
          pred_context = 1 + 2 * (crf1 == GOLDEN_FRAME || crf2 == GOLDEN_FRAME);
287
3.04M
      } else {
288
3.04M
        if (above0 == LAST_FRAME && left0 == LAST_FRAME) {
289
1.01M
          pred_context = 3;
290
2.03M
        } else if (above0 == LAST_FRAME || left0 == LAST_FRAME) {
291
801k
          const MV_REFERENCE_FRAME edge0 =
292
801k
              (above0 == LAST_FRAME) ? left0 : above0;
293
801k
          pred_context = 4 * (edge0 == GOLDEN_FRAME);
294
1.23M
        } else {
295
1.23M
          pred_context =
296
1.23M
              2 * (above0 == GOLDEN_FRAME) + 2 * (left0 == GOLDEN_FRAME);
297
1.23M
        }
298
3.04M
      }
299
5.85M
    }
300
8.87M
  } else if (has_above || has_left) {  // one edge available
301
4.51M
    const MODE_INFO *edge_mi = has_above ? above_mi : left_mi;
302
303
4.51M
    if (!is_inter_block(edge_mi) ||
304
3.00M
        (edge_mi->ref_frame[0] == LAST_FRAME && !has_second_ref(edge_mi)))
305
3.09M
      pred_context = 2;
306
1.41M
    else if (!has_second_ref(edge_mi))
307
1.39M
      pred_context = 4 * (edge_mi->ref_frame[0] == GOLDEN_FRAME);
308
25.4k
    else
309
25.4k
      pred_context = 3 * (edge_mi->ref_frame[0] == GOLDEN_FRAME ||
310
19.5k
                          edge_mi->ref_frame[1] == GOLDEN_FRAME);
311
4.51M
  } else {  // no edges available (2)
312
1.46M
    pred_context = 2;
313
1.46M
  }
314
14.8M
  assert(pred_context >= 0 && pred_context < REF_CONTEXTS);
315
14.8M
  return pred_context;
316
14.8M
}