Coverage Report

Created: 2026-05-30 06:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/dav1d/src/getbits.c
Line
Count
Source
1
/*
2
 * Copyright © 2018, VideoLAN and dav1d authors
3
 * Copyright © 2018, Two Orioles, LLC
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions are met:
8
 *
9
 * 1. Redistributions of source code must retain the above copyright notice, this
10
 *    list of conditions and the following disclaimer.
11
 *
12
 * 2. Redistributions in binary form must reproduce the above copyright notice,
13
 *    this list of conditions and the following disclaimer in the documentation
14
 *    and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 */
27
28
#include "config.h"
29
30
#include <limits.h>
31
32
#include "common/intops.h"
33
34
#include "src/getbits.h"
35
36
void dav1d_init_get_bits(GetBits *const c, const uint8_t *const data,
37
                         const size_t sz)
38
78.8k
{
39
78.8k
    assert(sz);
40
78.8k
    c->ptr = c->ptr_start = data;
41
78.8k
    c->ptr_end = &c->ptr_start[sz];
42
78.8k
    c->state = 0;
43
78.8k
    c->bits_left = 0;
44
78.8k
    c->error = 0;
45
78.8k
}
46
47
3.45M
unsigned dav1d_get_bit(GetBits *const c) {
48
3.45M
    if (!c->bits_left) {
49
583k
        if (c->ptr >= c->ptr_end) {
50
165
            c->error = 1;
51
583k
        } else {
52
583k
            const unsigned state = *c->ptr++;
53
583k
            c->bits_left = 7;
54
583k
            c->state = (uint64_t) state << 57;
55
583k
            return state >> 7;
56
583k
        }
57
583k
    }
58
59
2.87M
    const uint64_t state = c->state;
60
2.87M
    c->bits_left--;
61
2.87M
    c->state = state << 1;
62
2.87M
    return (unsigned) (state >> 63);
63
3.45M
}
64
65
1.20M
static inline void refill(GetBits *const c, const int n) {
66
1.20M
    assert(c->bits_left >= 0 && c->bits_left < 32);
67
1.20M
    unsigned state = 0;
68
1.29M
    do {
69
1.29M
        if (c->ptr >= c->ptr_end) {
70
253
            c->error = 1;
71
253
            if (state) break;
72
238
            return;
73
253
        }
74
1.29M
        state = (state << 8) | *c->ptr++;
75
1.29M
        c->bits_left += 8;
76
1.29M
    } while (n > c->bits_left);
77
1.20M
    c->state |= (uint64_t) state << (64 - c->bits_left);
78
1.20M
}
79
80
#define GET_BITS(name, type, type64)            \
81
2.54M
type name(GetBits *const c, const int n) {      \
82
2.54M
    assert(n > 0 && n <= 32);                   \
83
2.54M
    /* Unsigned cast avoids refill after eob */ \
84
2.54M
    if ((unsigned) n > (unsigned) c->bits_left) \
85
2.54M
        refill(c, n);                           \
86
2.54M
    const uint64_t state = c->state;            \
87
2.54M
    c->bits_left -= n;                          \
88
2.54M
    c->state = state << n;                      \
89
2.54M
    return (type) ((type64) state >> (64 - n)); \
90
2.54M
}
dav1d_get_bits
Line
Count
Source
81
2.38M
type name(GetBits *const c, const int n) {      \
82
2.38M
    assert(n > 0 && n <= 32);                   \
83
2.38M
    /* Unsigned cast avoids refill after eob */ \
84
2.38M
    if ((unsigned) n > (unsigned) c->bits_left) \
85
2.38M
        refill(c, n);                           \
86
2.38M
    const uint64_t state = c->state;            \
87
2.38M
    c->bits_left -= n;                          \
88
2.38M
    c->state = state << n;                      \
89
2.38M
    return (type) ((type64) state >> (64 - n)); \
90
2.38M
}
dav1d_get_sbits
Line
Count
Source
81
163k
type name(GetBits *const c, const int n) {      \
82
163k
    assert(n > 0 && n <= 32);                   \
83
163k
    /* Unsigned cast avoids refill after eob */ \
84
163k
    if ((unsigned) n > (unsigned) c->bits_left) \
85
163k
        refill(c, n);                           \
86
163k
    const uint64_t state = c->state;            \
87
163k
    c->bits_left -= n;                          \
88
163k
    c->state = state << n;                      \
89
163k
    return (type) ((type64) state >> (64 - n)); \
90
163k
}
91
92
GET_BITS(dav1d_get_bits,  unsigned, uint64_t)
93
GET_BITS(dav1d_get_sbits, int,      int64_t)
94
95
74.9k
unsigned dav1d_get_uleb128(GetBits *const c) {
96
74.9k
    uint64_t val = 0;
97
74.9k
    unsigned i = 0, more;
98
99
78.5k
    do {
100
78.5k
        const int v = dav1d_get_bits(c, 8);
101
78.5k
        more = v & 0x80;
102
78.5k
        val |= ((uint64_t) (v & 0x7F)) << i;
103
78.5k
        i += 7;
104
78.5k
    } while (more && i < 56);
105
106
74.9k
    if (val > UINT32_MAX || more) {
107
243
        c->error = 1;
108
243
        return 0;
109
243
    }
110
111
74.6k
    return (unsigned) val;
112
74.9k
}
113
114
18.9k
unsigned dav1d_get_uniform(GetBits *const c, const unsigned max) {
115
    // Output in range [0..max-1]
116
    // max must be > 1, or else nothing is read from the bitstream
117
18.9k
    assert(max > 1);
118
18.9k
    const int l = ulog2(max) + 1;
119
18.9k
    assert(l > 1);
120
18.9k
    const unsigned m = (1U << l) - max;
121
18.9k
    const unsigned v = dav1d_get_bits(c, l - 1);
122
18.9k
    return v < m ? v : (v << 1) - m + dav1d_get_bit(c);
123
18.9k
}
124
125
44
unsigned dav1d_get_vlc(GetBits *const c) {
126
44
    if (dav1d_get_bit(c))
127
14
        return 0;
128
129
30
    int n_bits = 0;
130
309
    do {
131
309
        if (++n_bits == 32)
132
0
            return UINT32_MAX;
133
309
    } while (!dav1d_get_bit(c));
134
135
30
    return ((1U << n_bits) - 1) + dav1d_get_bits(c, n_bits);
136
30
}
137
138
static unsigned get_bits_subexp_u(GetBits *const c, const unsigned ref,
139
                                  const unsigned n)
140
631k
{
141
631k
    unsigned v = 0;
142
143
678k
    for (int i = 0;; i++) {
144
678k
        const int b = i ? 3 + i - 1 : 3;
145
146
678k
        if (n < v + 3 * (1 << b)) {
147
2.13k
            v += dav1d_get_uniform(c, n - v + 1);
148
2.13k
            break;
149
2.13k
        }
150
151
676k
        if (!dav1d_get_bit(c)) {
152
629k
            v += dav1d_get_bits(c, b);
153
629k
            break;
154
629k
        }
155
156
46.6k
        v += 1 << b;
157
46.6k
    }
158
159
631k
    return ref * 2 <= n ? inv_recenter(ref, v) : n - inv_recenter(n - ref, v);
160
631k
}
161
162
631k
int dav1d_get_bits_subexp(GetBits *const c, const int ref, const unsigned n) {
163
631k
    return (int) get_bits_subexp_u(c, ref + (1 << n), 2 << n) - (1 << n);
164
631k
}