Coverage Report

Created: 2026-09-13 06:34

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
478k
{
39
478k
    assert(sz);
40
478k
    c->ptr = c->ptr_start = data;
41
478k
    c->ptr_end = &c->ptr_start[sz];
42
478k
    c->state = 0;
43
478k
    c->bits_left = 0;
44
478k
    c->error = 0;
45
478k
}
46
47
19.9M
unsigned dav1d_get_bit(GetBits *const c) {
48
19.9M
    if (!c->bits_left) {
49
3.27M
        if (c->ptr >= c->ptr_end) {
50
1.45k
            c->error = 1;
51
3.27M
        } else {
52
3.27M
            const unsigned state = *c->ptr++;
53
3.27M
            c->bits_left = 7;
54
3.27M
            c->state = (uint64_t) state << 57;
55
3.27M
            return state >> 7;
56
3.27M
        }
57
3.27M
    }
58
59
16.6M
    const uint64_t state = c->state;
60
16.6M
    c->bits_left--;
61
16.6M
    c->state = state << 1;
62
16.6M
    return (unsigned) (state >> 63);
63
19.9M
}
64
65
6.42M
static inline void refill(GetBits *const c, const int n) {
66
6.42M
    assert(c->bits_left >= 0 && c->bits_left < 32);
67
6.42M
    unsigned state = 0;
68
6.76M
    do {
69
6.76M
        if (c->ptr >= c->ptr_end) {
70
2.75k
            c->error = 1;
71
2.75k
            if (state) break;
72
2.49k
            return;
73
2.75k
        }
74
6.75M
        state = (state << 8) | *c->ptr++;
75
6.75M
        c->bits_left += 8;
76
6.75M
    } while (n > c->bits_left);
77
6.42M
    c->state |= (uint64_t) state << (64 - c->bits_left);
78
6.42M
}
79
80
#define GET_BITS(name, type, type64)            \
81
12.8M
type name(GetBits *const c, const int n) {      \
82
12.8M
    assert(n > 0 && n <= 32);                   \
83
12.8M
    /* Unsigned cast avoids refill after eob */ \
84
12.8M
    if ((unsigned) n > (unsigned) c->bits_left) \
85
12.8M
        refill(c, n);                           \
86
12.8M
    const uint64_t state = c->state;            \
87
12.8M
    c->bits_left -= n;                          \
88
12.8M
    c->state = state << n;                      \
89
12.8M
    return (type) ((type64) state >> (64 - n)); \
90
12.8M
}
dav1d_get_bits
Line
Count
Source
81
11.6M
type name(GetBits *const c, const int n) {      \
82
11.6M
    assert(n > 0 && n <= 32);                   \
83
11.6M
    /* Unsigned cast avoids refill after eob */ \
84
11.6M
    if ((unsigned) n > (unsigned) c->bits_left) \
85
11.6M
        refill(c, n);                           \
86
11.6M
    const uint64_t state = c->state;            \
87
11.6M
    c->bits_left -= n;                          \
88
11.6M
    c->state = state << n;                      \
89
11.6M
    return (type) ((type64) state >> (64 - n)); \
90
11.6M
}
dav1d_get_sbits
Line
Count
Source
81
1.19M
type name(GetBits *const c, const int n) {      \
82
1.19M
    assert(n > 0 && n <= 32);                   \
83
1.19M
    /* Unsigned cast avoids refill after eob */ \
84
1.19M
    if ((unsigned) n > (unsigned) c->bits_left) \
85
1.19M
        refill(c, n);                           \
86
1.19M
    const uint64_t state = c->state;            \
87
1.19M
    c->bits_left -= n;                          \
88
1.19M
    c->state = state << n;                      \
89
1.19M
    return (type) ((type64) state >> (64 - n)); \
90
1.19M
}
91
92
GET_BITS(dav1d_get_bits,  unsigned, uint64_t)
93
GET_BITS(dav1d_get_sbits, int,      int64_t)
94
95
394k
unsigned dav1d_get_uleb128(GetBits *const c) {
96
394k
    uint64_t val = 0;
97
394k
    unsigned i = 0, more;
98
99
400k
    do {
100
400k
        const int v = dav1d_get_bits(c, 8);
101
400k
        more = v & 0x80;
102
400k
        val |= ((uint64_t) (v & 0x7F)) << i;
103
400k
        i += 7;
104
400k
    } while (more && i < 56);
105
106
394k
    if (val > UINT32_MAX || more) {
107
465
        c->error = 1;
108
465
        return 0;
109
465
    }
110
111
393k
    return (unsigned) val;
112
394k
}
113
114
135k
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
135k
    assert(max > 1);
118
135k
    const int l = ulog2(max) + 1;
119
135k
    assert(l > 1);
120
135k
    const unsigned m = (1U << l) - max;
121
135k
    const unsigned v = dav1d_get_bits(c, l - 1);
122
135k
    return v < m ? v : (v << 1) - m + dav1d_get_bit(c);
123
135k
}
124
125
2.44k
unsigned dav1d_get_vlc(GetBits *const c) {
126
2.44k
    if (dav1d_get_bit(c))
127
187
        return 0;
128
129
2.25k
    int n_bits = 0;
130
9.06k
    do {
131
9.06k
        if (++n_bits == 32)
132
25
            return UINT32_MAX;
133
9.06k
    } while (!dav1d_get_bit(c));
134
135
2.23k
    return ((1U << n_bits) - 1) + dav1d_get_bits(c, n_bits);
136
2.25k
}
137
138
static unsigned get_bits_subexp_u(GetBits *const c, const unsigned ref,
139
                                  const unsigned n)
140
2.54M
{
141
2.54M
    unsigned v = 0;
142
143
3.05M
    for (int i = 0;; i++) {
144
3.05M
        const int b = i ? 3 + i - 1 : 3;
145
146
3.05M
        if (n < v + 3 * (1 << b)) {
147
20.4k
            v += dav1d_get_uniform(c, n - v + 1);
148
20.4k
            break;
149
20.4k
        }
150
151
3.03M
        if (!dav1d_get_bit(c)) {
152
2.52M
            v += dav1d_get_bits(c, b);
153
2.52M
            break;
154
2.52M
        }
155
156
502k
        v += 1 << b;
157
502k
    }
158
159
2.54M
    return ref * 2 <= n ? inv_recenter(ref, v) : n - inv_recenter(n - ref, v);
160
2.54M
}
161
162
2.54M
int dav1d_get_bits_subexp(GetBits *const c, const int ref, const unsigned n) {
163
2.54M
    return (int) get_bits_subexp_u(c, ref + (1 << n), 2 << n) - (1 << n);
164
2.54M
}