Coverage Report

Created: 2026-04-01 07:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/vorbis.c
Line
Count
Source
1
/**
2
 * @file
3
 * Common code for Vorbis I encoder and decoder
4
 * @author Denes Balatoni  ( dbalatoni programozo hu )
5
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22
23
/**
24
 * @file
25
 * Common code for Vorbis I encoder and decoder
26
 * @author Denes Balatoni  ( dbalatoni programozo hu )
27
 */
28
29
#include "libavutil/common.h"
30
#include "libavutil/error.h"
31
#include "libavutil/log.h"
32
#include "libavutil/macros.h"
33
34
#include "vorbis.h"
35
#include "vorbis_data.h"
36
37
38
/* Helper functions */
39
40
// x^(1/n)
41
unsigned int ff_vorbis_nth_root(unsigned int x, unsigned int n)
42
2.06k
{
43
2.06k
    unsigned int ret = 0, i, j;
44
45
383k
    do {
46
383k
        ++ret;
47
422k
        for (i = 0, j = ret; i < n - 1; i++)
48
39.1k
            j *= ret;
49
383k
    } while (j <= x);
50
51
2.06k
    return ret - 1;
52
2.06k
}
53
54
// Generate vlc codes from vorbis huffman code lengths
55
56
// the two bits[p] > 32 checks should be redundant, all calling code should
57
// already ensure that, but since it allows overwriting the stack it seems
58
// reasonable to check redundantly.
59
int ff_vorbis_len2vlc(uint8_t *bits, uint32_t *codes, unsigned num)
60
19.3k
{
61
19.3k
    uint32_t exit_at_level[33] = { 404 };
62
19.3k
    unsigned i, j, p, code;
63
64
18.9M
    for (p = 0; (p < num) && (bits[p] == 0); ++p)
65
18.9M
        ;
66
19.3k
    if (p == num)
67
2.31k
        return 0;
68
69
17.0k
    codes[p] = 0;
70
17.0k
    if (bits[p] > 32)
71
0
        return AVERROR_INVALIDDATA;
72
259k
    for (i = 0; i < bits[p]; ++i)
73
241k
        exit_at_level[i+1] = 1u << i;
74
75
17.0k
    ++p;
76
77
233k
    for (i = p; (i < num) && (bits[i] == 0); ++i)
78
216k
        ;
79
17.0k
    if (i == num)
80
15.8k
        return 0;
81
82
1.20M
    for (; p < num; ++p) {
83
1.20M
        if (bits[p] > 32)
84
0
             return AVERROR_INVALIDDATA;
85
1.20M
        if (bits[p] == 0)
86
815k
             continue;
87
        // find corresponding exit(node which the tree can grow further from)
88
784k
        for (i = bits[p]; i > 0; --i)
89
783k
            if (exit_at_level[i])
90
386k
                break;
91
387k
        if (!i) // overspecified tree
92
522
             return AVERROR_INVALIDDATA;
93
386k
        code = exit_at_level[i];
94
386k
        exit_at_level[i] = 0;
95
        // construct code (append 0s to end) and introduce new exits
96
783k
        for (j = i + 1 ;j <= bits[p]; ++j)
97
396k
            exit_at_level[j] = code + (1u << (j - 1));
98
386k
        codes[p] = code;
99
386k
    }
100
101
    //no exits should be left (underspecified tree - ie. unused valid vlcs - not allowed by SPEC)
102
9.51k
    for (p = 1; p < 33; p++)
103
9.24k
        if (exit_at_level[p])
104
411
            return AVERROR_INVALIDDATA;
105
106
274
    return 0;
107
685
}
108
109
int ff_vorbis_ready_floor1_list(void *logctx,
110
                                vorbis_floor1_entry *list, int values)
111
11.7k
{
112
11.7k
    int i;
113
11.7k
    list[0].sort = 0;
114
11.7k
    list[1].sort = 1;
115
16.1k
    for (i = 2; i < values; i++) {
116
4.35k
        int j;
117
4.35k
        list[i].low  = 0;
118
4.35k
        list[i].high = 1;
119
4.35k
        list[i].sort = i;
120
30.5k
        for (j = 2; j < i; j++) {
121
26.2k
            int tmp = list[j].x;
122
26.2k
            if (tmp < list[i].x) {
123
11.4k
                if (tmp > list[list[i].low].x)
124
970
                    list[i].low  =  j;
125
14.7k
            } else {
126
14.7k
                if (tmp < list[list[i].high].x)
127
3.60k
                    list[i].high = j;
128
14.7k
            }
129
26.2k
        }
130
4.35k
    }
131
23.6k
    for (i = 0; i < values - 1; i++) {
132
12.8k
        int j;
133
29.3k
        for (j = i + 1; j < values; j++) {
134
17.3k
            if (list[i].x == list[j].x) {
135
925
                av_log(logctx, AV_LOG_ERROR,
136
925
                       "Duplicate value found in floor 1 X coordinates\n");
137
925
                return AVERROR_INVALIDDATA;
138
925
            }
139
16.4k
            if (list[list[i].sort].x > list[list[j].sort].x) {
140
1.18k
                int tmp = list[i].sort;
141
1.18k
                list[i].sort = list[j].sort;
142
1.18k
                list[j].sort = tmp;
143
1.18k
            }
144
16.4k
        }
145
12.8k
    }
146
10.8k
    return 0;
147
11.7k
}
148
149
static inline void render_line_unrolled(intptr_t x, int y, int x1,
150
                                        intptr_t sy, int ady, int adx,
151
                                        float *buf)
152
106k
{
153
106k
    int err = -adx;
154
106k
    x -= x1 - 1;
155
106k
    buf += x1 - 1;
156
10.9M
    while (++x < 0) {
157
10.8M
        err += ady;
158
10.8M
        if (err >= 0) {
159
1.72M
            err += ady - adx;
160
1.72M
            y   += sy;
161
1.72M
            buf[x++] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
162
1.72M
        }
163
10.8M
        buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
164
10.8M
    }
165
106k
    if (x <= 0) {
166
49.0k
        if (err + ady >= 0)
167
0
            y += sy;
168
49.0k
        buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
169
49.0k
    }
170
106k
}
171
172
static void render_line(int x0, int y0, int x1, int y1, float *buf)
173
366k
{
174
366k
    int dy  = y1 - y0;
175
366k
    int adx = x1 - x0;
176
366k
    int ady = FFABS(dy);
177
366k
    int sy  = dy < 0 ? -1 : 1;
178
366k
    buf[x0] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y0)];
179
366k
    if (ady*2 <= adx) { // optimized common case
180
106k
        render_line_unrolled(x0, y0, x1, sy, ady, adx, buf);
181
260k
    } else {
182
260k
        int base  = dy / adx;
183
260k
        int x     = x0;
184
260k
        int y     = y0;
185
260k
        int err   = -adx;
186
260k
        ady -= FFABS(base) * adx;
187
4.30M
        while (++x < x1) {
188
4.04M
            y += base;
189
4.04M
            err += ady;
190
4.04M
            if (err >= 0) {
191
2.48M
                err -= adx;
192
2.48M
                y   += sy;
193
2.48M
            }
194
4.04M
            buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
195
4.04M
        }
196
260k
    }
197
366k
}
198
199
void ff_vorbis_floor1_render_list(vorbis_floor1_entry * list, int values,
200
                                  uint16_t *y_list, int *flag,
201
                                  int multiplier, float *out, int samples)
202
366k
{
203
366k
    int lx, ly, i;
204
366k
    lx = 0;
205
366k
    ly = y_list[0] * multiplier;
206
419k
    for (i = 1; i < values; i++) {
207
419k
        int pos = list[i].sort;
208
419k
        if (flag[pos]) {
209
366k
            int x1 = list[pos].x;
210
366k
            int y1 = y_list[pos] * multiplier;
211
366k
            if (lx < samples)
212
366k
                render_line(lx, ly, FFMIN(x1,samples), y1, out);
213
366k
            lx = x1;
214
366k
            ly = y1;
215
366k
        }
216
419k
        if (lx >= samples)
217
366k
            break;
218
419k
    }
219
366k
    if (lx < samples)
220
0
        render_line(lx, ly, samples, ly, out);
221
366k
}