Coverage Report

Created: 2025-10-28 07:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libavif/ext/aom/aom_dsp/bitwriter_buffer.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2016, 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
#include <assert.h>
13
#include <limits.h>
14
#include <stdlib.h>
15
16
#include "config/aom_config.h"
17
18
#include "aom_dsp/bitwriter_buffer.h"
19
#include "aom_dsp/recenter.h"
20
#include "aom_ports/bitops.h"
21
22
82.5k
int aom_wb_is_byte_aligned(const struct aom_write_bit_buffer *wb) {
23
82.5k
  return (wb->bit_offset % CHAR_BIT == 0);
24
82.5k
}
25
26
237k
uint32_t aom_wb_bytes_written(const struct aom_write_bit_buffer *wb) {
27
237k
  return wb->bit_offset / CHAR_BIT + (wb->bit_offset % CHAR_BIT > 0);
28
237k
}
29
30
12.0M
void aom_wb_write_bit(struct aom_write_bit_buffer *wb, int bit) {
31
12.0M
  const int off = (int)wb->bit_offset;
32
12.0M
  const int p = off / CHAR_BIT;
33
12.0M
  const int q = CHAR_BIT - 1 - off % CHAR_BIT;
34
12.0M
  if (q == CHAR_BIT - 1) {
35
    // Zero next char and write bit
36
1.62M
    wb->bit_buffer[p] = bit << q;
37
10.4M
  } else {
38
10.4M
    wb->bit_buffer[p] &= ~(1 << q);
39
10.4M
    wb->bit_buffer[p] |= bit << q;
40
10.4M
  }
41
12.0M
  wb->bit_offset = off + 1;
42
12.0M
}
43
44
172k
static void overwrite_bit(struct aom_write_bit_buffer *wb, int bit) {
45
  // Do not zero bytes but overwrite exisiting values
46
172k
  const int off = (int)wb->bit_offset;
47
172k
  const int p = off / CHAR_BIT;
48
172k
  const int q = CHAR_BIT - 1 - off % CHAR_BIT;
49
172k
  wb->bit_buffer[p] &= ~(1 << q);
50
172k
  wb->bit_buffer[p] |= bit << q;
51
172k
  wb->bit_offset = off + 1;
52
172k
}
53
54
1.80M
void aom_wb_write_literal(struct aom_write_bit_buffer *wb, int data, int bits) {
55
1.80M
  assert(bits <= 31);
56
1.80M
  int bit;
57
9.92M
  for (bit = bits - 1; bit >= 0; bit--) aom_wb_write_bit(wb, (data >> bit) & 1);
58
1.80M
}
59
60
void aom_wb_write_unsigned_literal(struct aom_write_bit_buffer *wb,
61
0
                                   uint32_t data, int bits) {
62
0
  assert(bits <= 32);
63
0
  int bit;
64
0
  for (bit = bits - 1; bit >= 0; bit--) aom_wb_write_bit(wb, (data >> bit) & 1);
65
0
}
66
67
void aom_wb_overwrite_literal(struct aom_write_bit_buffer *wb, int data,
68
88.8k
                              int bits) {
69
88.8k
  int bit;
70
261k
  for (bit = bits - 1; bit >= 0; bit--) overwrite_bit(wb, (data >> bit) & 1);
71
88.8k
}
72
73
void aom_wb_write_inv_signed_literal(struct aom_write_bit_buffer *wb, int data,
74
0
                                     int bits) {
75
0
  aom_wb_write_literal(wb, data, bits + 1);
76
0
}
77
78
0
void aom_wb_write_uvlc(struct aom_write_bit_buffer *wb, uint32_t v) {
79
0
  assert(v != UINT32_MAX);
80
0
  ++v;
81
0
  const int leading_zeroes = get_msb(v);
82
0
  aom_wb_write_literal(wb, 0, leading_zeroes);
83
0
  aom_wb_write_unsigned_literal(wb, v, leading_zeroes + 1);
84
0
}
85
86
static void wb_write_primitive_quniform(struct aom_write_bit_buffer *wb,
87
0
                                        uint16_t n, uint16_t v) {
88
0
  if (n <= 1) return;
89
0
  const int l = get_msb(n) + 1;
90
0
  const int m = (1 << l) - n;
91
0
  if (v < m) {
92
0
    aom_wb_write_literal(wb, v, l - 1);
93
0
  } else {
94
0
    aom_wb_write_literal(wb, m + ((v - m) >> 1), l - 1);
95
0
    aom_wb_write_bit(wb, (v - m) & 1);
96
0
  }
97
0
}
98
99
static void wb_write_primitive_subexpfin(struct aom_write_bit_buffer *wb,
100
0
                                         uint16_t n, uint16_t k, uint16_t v) {
101
0
  int i = 0;
102
0
  int mk = 0;
103
0
  while (1) {
104
0
    int b = (i ? k + i - 1 : k);
105
0
    int a = (1 << b);
106
0
    if (n <= mk + 3 * a) {
107
0
      wb_write_primitive_quniform(wb, n - mk, v - mk);
108
0
      break;
109
0
    } else {
110
0
      int t = (v >= mk + a);
111
0
      aom_wb_write_bit(wb, t);
112
0
      if (t) {
113
0
        i = i + 1;
114
0
        mk += a;
115
0
      } else {
116
0
        aom_wb_write_literal(wb, v - mk, b);
117
0
        break;
118
0
      }
119
0
    }
120
0
  }
121
0
}
122
123
static void wb_write_primitive_refsubexpfin(struct aom_write_bit_buffer *wb,
124
                                            uint16_t n, uint16_t k,
125
0
                                            uint16_t ref, uint16_t v) {
126
0
  wb_write_primitive_subexpfin(wb, n, k, recenter_finite_nonneg(n, ref, v));
127
0
}
128
129
void aom_wb_write_signed_primitive_refsubexpfin(struct aom_write_bit_buffer *wb,
130
                                                uint16_t n, uint16_t k,
131
0
                                                int16_t ref, int16_t v) {
132
0
  ref += n - 1;
133
0
  v += n - 1;
134
0
  const uint16_t scaled_n = (n << 1) - 1;
135
0
  wb_write_primitive_refsubexpfin(wb, scaled_n, k, ref, v);
136
0
}