Coverage Report

Created: 2026-02-14 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvpx/vp8/encoder/encodemv.c
Line
Count
Source
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
 *  that can be found in the LICENSE file in the root of the source
6
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
9
 */
10
11
#include "vp8/common/common.h"
12
#include "encodemv.h"
13
#include "vp8/common/entropymode.h"
14
#include "vp8/common/systemdependent.h"
15
#include "vpx_ports/system_state.h"
16
17
#include <math.h>
18
19
static void encode_mvcomponent(vp8_writer *const w, const int v,
20
1.01M
                               const struct mv_context *mvc) {
21
1.01M
  const vp8_prob *p = mvc->prob;
22
1.01M
  const int x = v < 0 ? -v : v;
23
24
1.01M
  if (x < mvnum_short) { /* Small */
25
297k
    vp8_write(w, 0, p[mvpis_short]);
26
297k
    vp8_treed_write(w, vp8_small_mvtree, p + MVPshort, x, 3);
27
28
297k
    if (!x) return; /* no sign bit */
29
714k
  } else {          /* Large */
30
714k
    int i = 0;
31
32
714k
    vp8_write(w, 1, p[mvpis_short]);
33
34
2.14M
    do {
35
2.14M
      vp8_write(w, (x >> i) & 1, p[MVPbits + i]);
36
2.14M
    } while (++i < 3);
37
38
714k
    i = mvlong_width - 1; /* Skip bit 3, which is sometimes implicit */
39
40
4.28M
    do {
41
4.28M
      vp8_write(w, (x >> i) & 1, p[MVPbits + i]);
42
4.28M
    } while (--i > 3);
43
44
714k
    if (x & 0xFFF0) vp8_write(w, (x >> 3) & 1, p[MVPbits + 3]);
45
714k
  }
46
47
857k
  vp8_write(w, v < 0, p[MVPsign]);
48
857k
}
49
#if 0
50
static int max_mv_r = 0;
51
static int max_mv_c = 0;
52
#endif
53
void vp8_encode_motion_vector(vp8_writer *w, const MV *mv,
54
505k
                              const MV_CONTEXT *mvc) {
55
#if 0
56
    {
57
        if (abs(mv->row >> 1) > max_mv_r)
58
        {
59
            FILE *f = fopen("maxmv.stt", "a");
60
            max_mv_r = abs(mv->row >> 1);
61
            fprintf(f, "New Mv Row Max %6d\n", (mv->row >> 1));
62
63
            if ((abs(mv->row) / 2) != max_mv_r)
64
                fprintf(f, "MV Row conversion error %6d\n", abs(mv->row) / 2);
65
66
            fclose(f);
67
        }
68
69
        if (abs(mv->col >> 1) > max_mv_c)
70
        {
71
            FILE *f = fopen("maxmv.stt", "a");
72
            fprintf(f, "New Mv Col Max %6d\n", (mv->col >> 1));
73
            max_mv_c = abs(mv->col >> 1);
74
            fclose(f);
75
        }
76
    }
77
#endif
78
79
505k
  encode_mvcomponent(w, mv->row >> 1, &mvc[0]);
80
505k
  encode_mvcomponent(w, mv->col >> 1, &mvc[1]);
81
505k
}
82
83
static unsigned int cost_mvcomponent(const int v,
84
80.6M
                                     const struct mv_context *mvc) {
85
80.6M
  const vp8_prob *p = mvc->prob;
86
80.6M
  const int x = v;
87
80.6M
  unsigned int cost;
88
89
80.6M
  if (x < mvnum_short) {
90
629k
    cost = vp8_cost_zero(p[mvpis_short]) +
91
629k
           vp8_treed_cost(vp8_small_mvtree, p + MVPshort, x, 3);
92
93
629k
    if (!x) return cost;
94
79.9M
  } else {
95
79.9M
    int i = 0;
96
79.9M
    cost = vp8_cost_one(p[mvpis_short]);
97
98
239M
    do {
99
239M
      cost += vp8_cost_bit(p[MVPbits + i], (x >> i) & 1);
100
101
239M
    } while (++i < 3);
102
103
79.9M
    i = mvlong_width - 1; /* Skip bit 3, which is sometimes implicit */
104
105
479M
    do {
106
479M
      cost += vp8_cost_bit(p[MVPbits + i], (x >> i) & 1);
107
108
479M
    } while (--i > 3);
109
110
79.9M
    if (x & 0xFFF0) cost += vp8_cost_bit(p[MVPbits + 3], (x >> 3) & 1);
111
79.9M
  }
112
113
80.5M
  return cost; /* + vp8_cost_bit( p [MVPsign], v < 0); */
114
80.6M
}
115
116
void vp8_build_component_cost_table(int *mvcost[2], const MV_CONTEXT *mvc,
117
40.6k
                                    int mvc_flag[2]) {
118
40.6k
  int i = 1;
119
40.6k
  unsigned int cost0 = 0;
120
40.6k
  unsigned int cost1 = 0;
121
122
40.6k
  vpx_clear_system_state();
123
124
40.6k
  i = 1;
125
126
40.6k
  if (mvc_flag[0]) {
127
39.4k
    mvcost[0][0] = cost_mvcomponent(0, &mvc[0]);
128
129
40.3M
    do {
130
40.3M
      cost0 = cost_mvcomponent(i, &mvc[0]);
131
132
40.3M
      mvcost[0][i] = cost0 + vp8_cost_zero(mvc[0].prob[MVPsign]);
133
40.3M
      mvcost[0][-i] = cost0 + vp8_cost_one(mvc[0].prob[MVPsign]);
134
40.3M
    } while (++i <= mv_max);
135
39.4k
  }
136
137
40.6k
  i = 1;
138
139
40.6k
  if (mvc_flag[1]) {
140
39.3k
    mvcost[1][0] = cost_mvcomponent(0, &mvc[1]);
141
142
40.2M
    do {
143
40.2M
      cost1 = cost_mvcomponent(i, &mvc[1]);
144
145
40.2M
      mvcost[1][i] = cost1 + vp8_cost_zero(mvc[1].prob[MVPsign]);
146
40.2M
      mvcost[1][-i] = cost1 + vp8_cost_one(mvc[1].prob[MVPsign]);
147
40.2M
    } while (++i <= mv_max);
148
39.3k
  }
149
40.6k
}
150
151
/* Motion vector probability table update depends on benefit.
152
 * Small correction allows for the fact that an update to an MV probability
153
 * may have benefit in subsequent frames as well as the current one.
154
 */
155
3.65M
#define MV_PROB_UPDATE_CORRECTION -1
156
157
3.65M
static void calc_prob(vp8_prob *p, const unsigned int ct[2]) {
158
3.65M
  const unsigned int tot = ct[0] + ct[1];
159
160
3.65M
  if (tot) {
161
1.09M
    const vp8_prob x = ((ct[0] * 255) / tot) & ~1u;
162
1.09M
    *p = x ? x : 1;
163
1.09M
  }
164
3.65M
}
165
166
static void update(vp8_writer *const w, const unsigned int ct[2],
167
                   vp8_prob *const cur_p, const vp8_prob new_p,
168
3.65M
                   const vp8_prob update_p, int *updated) {
169
3.65M
  const int cur_b = vp8_cost_branch(ct, *cur_p);
170
3.65M
  const int new_b = vp8_cost_branch(ct, new_p);
171
3.65M
  const int cost =
172
3.65M
      7 + MV_PROB_UPDATE_CORRECTION +
173
3.65M
      ((vp8_cost_one(update_p) - vp8_cost_zero(update_p) + 128) >> 8);
174
175
3.65M
  if (cur_b - new_b > cost) {
176
18.4k
    *cur_p = new_p;
177
18.4k
    vp8_write(w, 1, update_p);
178
18.4k
    vp8_write_literal(w, new_p >> 1, 7);
179
18.4k
    *updated = 1;
180
181
18.4k
  } else
182
3.64M
    vp8_write(w, 0, update_p);
183
3.65M
}
184
185
static void write_component_probs(vp8_writer *const w,
186
                                  struct mv_context *cur_mvc,
187
                                  const struct mv_context *default_mvc_,
188
                                  const struct mv_context *update_mvc,
189
                                  const unsigned int events[MVvals],
190
192k
                                  unsigned int rc, int *updated) {
191
192k
  vp8_prob *Pcur = cur_mvc->prob;
192
192k
  const vp8_prob *default_mvc = default_mvc_->prob;
193
192k
  const vp8_prob *Pupdate = update_mvc->prob;
194
192k
  unsigned int is_short_ct[2], sign_ct[2];
195
196
192k
  unsigned int bit_ct[mvlong_width][2];
197
198
192k
  unsigned int short_ct[mvnum_short];
199
192k
  unsigned int short_bct[mvnum_short - 1][2];
200
201
192k
  vp8_prob Pnew[MVPcount];
202
203
192k
  (void)rc;
204
192k
  vp8_copy_array(Pnew, default_mvc, MVPcount);
205
206
192k
  vp8_zero(is_short_ct);
207
192k
  vp8_zero(sign_ct);
208
192k
  vp8_zero(bit_ct);
209
192k
  vp8_zero(short_ct);
210
192k
  vp8_zero(short_bct);
211
212
  /* j=0 */
213
192k
  {
214
192k
    const int c = events[mv_max];
215
216
192k
    is_short_ct[0] += c; /* Short vector */
217
192k
    short_ct[0] += c;    /* Magnitude distribution */
218
192k
  }
219
220
  /* j: 1 ~ mv_max (1023) */
221
192k
  {
222
192k
    int j = 1;
223
224
197M
    do {
225
197M
      const int c1 = events[mv_max + j]; /* positive */
226
197M
      const int c2 = events[mv_max - j]; /* negative */
227
197M
      const int c = c1 + c2;
228
197M
      int a = j;
229
230
197M
      sign_ct[0] += c1;
231
197M
      sign_ct[1] += c2;
232
233
197M
      if (a < mvnum_short) {
234
1.34M
        is_short_ct[0] += c; /* Short vector */
235
1.34M
        short_ct[a] += c;    /* Magnitude distribution */
236
195M
      } else {
237
195M
        int k = mvlong_width - 1;
238
195M
        is_short_ct[1] += c; /* Long vector */
239
240
        /*  bit 3 not always encoded. */
241
1.95G
        do {
242
1.95G
          bit_ct[k][(a >> k) & 1] += c;
243
244
1.95G
        } while (--k >= 0);
245
195M
      }
246
197M
    } while (++j <= mv_max);
247
192k
  }
248
249
192k
  calc_prob(Pnew + mvpis_short, is_short_ct);
250
251
192k
  calc_prob(Pnew + MVPsign, sign_ct);
252
253
192k
  {
254
192k
    vp8_prob p[mvnum_short - 1]; /* actually only need branch ct */
255
192k
    int j = 0;
256
257
192k
    vp8_tree_probs_from_distribution(8, vp8_small_mvencodings, vp8_small_mvtree,
258
192k
                                     p, short_bct, short_ct, 256, 1);
259
260
1.34M
    do {
261
1.34M
      calc_prob(Pnew + MVPshort + j, short_bct[j]);
262
263
1.34M
    } while (++j < mvnum_short - 1);
264
192k
  }
265
266
192k
  {
267
192k
    int j = 0;
268
269
1.92M
    do {
270
1.92M
      calc_prob(Pnew + MVPbits + j, bit_ct[j]);
271
272
1.92M
    } while (++j < mvlong_width);
273
192k
  }
274
275
192k
  update(w, is_short_ct, Pcur + mvpis_short, Pnew[mvpis_short], *Pupdate++,
276
192k
         updated);
277
278
192k
  update(w, sign_ct, Pcur + MVPsign, Pnew[MVPsign], *Pupdate++, updated);
279
280
192k
  {
281
192k
    const vp8_prob *const new_p = Pnew + MVPshort;
282
192k
    vp8_prob *const cur_p = Pcur + MVPshort;
283
284
192k
    int j = 0;
285
286
1.34M
    do {
287
1.34M
      update(w, short_bct[j], cur_p + j, new_p[j], *Pupdate++, updated);
288
289
1.34M
    } while (++j < mvnum_short - 1);
290
192k
  }
291
292
192k
  {
293
192k
    const vp8_prob *const new_p = Pnew + MVPbits;
294
192k
    vp8_prob *const cur_p = Pcur + MVPbits;
295
296
192k
    int j = 0;
297
298
1.92M
    do {
299
1.92M
      update(w, bit_ct[j], cur_p + j, new_p[j], *Pupdate++, updated);
300
301
1.92M
    } while (++j < mvlong_width);
302
192k
  }
303
192k
}
304
305
96.2k
void vp8_write_mvprobs(VP8_COMP *cpi) {
306
96.2k
  vp8_writer *const w = cpi->bc;
307
96.2k
  MV_CONTEXT *mvc = cpi->common.fc.mvc;
308
96.2k
  int flags[2] = { 0, 0 };
309
96.2k
  write_component_probs(w, &mvc[0], &vp8_default_mv_context[0],
310
96.2k
                        &vp8_mv_update_probs[0], cpi->mb.MVcount[0], 0,
311
96.2k
                        &flags[0]);
312
96.2k
  write_component_probs(w, &mvc[1], &vp8_default_mv_context[1],
313
96.2k
                        &vp8_mv_update_probs[1], cpi->mb.MVcount[1], 1,
314
96.2k
                        &flags[1]);
315
316
96.2k
  if (flags[0] || flags[1]) {
317
4.95k
    vp8_build_component_cost_table(
318
4.95k
        cpi->mb.mvcost, (const MV_CONTEXT *)cpi->common.fc.mvc, flags);
319
4.95k
  }
320
96.2k
}