FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vorbis.c
Date: 2026-07-22 00:55:30
Exec Total Coverage
Lines: 108 118 91.5%
Functions: 6 6 100.0%
Branches: 67 82 81.7%

Line Branch Exec 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 1085 unsigned int ff_vorbis_nth_root(unsigned int x, unsigned int n)
42 {
43 1085 unsigned int ret = 0, i, j;
44
45 do {
46 20008 ++ret;
47
2/2
✓ Branch 0 taken 16190 times.
✓ Branch 1 taken 20008 times.
36198 for (i = 0, j = ret; i < n - 1; i++)
48 16190 j *= ret;
49
2/2
✓ Branch 0 taken 18923 times.
✓ Branch 1 taken 1085 times.
20008 } while (j <= x);
50
51 1085 return ret - 1;
52 }
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 2540 int ff_vorbis_len2vlc(uint8_t *bits, uint32_t *codes, unsigned num)
60 {
61 2540 uint32_t exit_at_level[33] = { 404 };
62 unsigned i, j, p, code;
63
64
3/4
✓ Branch 0 taken 18336 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15796 times.
✓ Branch 3 taken 2540 times.
18336 for (p = 0; (p < num) && (bits[p] == 0); ++p)
65 ;
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2540 times.
2540 if (p == num)
67 return 0;
68
69 2540 codes[p] = 0;
70
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2540 times.
2540 if (bits[p] > 32)
71 return AVERROR_INVALIDDATA;
72
2/2
✓ Branch 0 taken 8584 times.
✓ Branch 1 taken 2540 times.
11124 for (i = 0; i < bits[p]; ++i)
73 8584 exit_at_level[i+1] = 1u << i;
74
75 2540 ++p;
76
77
2/4
✓ Branch 0 taken 2540 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2540 times.
2540 for (i = p; (i < num) && (bits[i] == 0); ++i)
78 ;
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2540 times.
2540 if (i == num)
80 return 0;
81
82
2/2
✓ Branch 0 taken 455655 times.
✓ Branch 1 taken 2540 times.
458195 for (; p < num; ++p) {
83
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 455655 times.
455655 if (bits[p] > 32)
84 return AVERROR_INVALIDDATA;
85
2/2
✓ Branch 0 taken 7798 times.
✓ Branch 1 taken 447857 times.
455655 if (bits[p] == 0)
86 7798 continue;
87 // find corresponding exit(node which the tree can grow further from)
88
1/2
✓ Branch 0 taken 887130 times.
✗ Branch 1 not taken.
887130 for (i = bits[p]; i > 0; --i)
89
2/2
✓ Branch 0 taken 447857 times.
✓ Branch 1 taken 439273 times.
887130 if (exit_at_level[i])
90 447857 break;
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 447857 times.
447857 if (!i) // overspecified tree
92 return AVERROR_INVALIDDATA;
93 447857 code = exit_at_level[i];
94 447857 exit_at_level[i] = 0;
95 // construct code (append 0s to end) and introduce new exits
96
2/2
✓ Branch 0 taken 439273 times.
✓ Branch 1 taken 447857 times.
887130 for (j = i + 1 ;j <= bits[p]; ++j)
97 439273 exit_at_level[j] = code + (1u << (j - 1));
98 447857 codes[p] = code;
99 }
100
101 //no exits should be left (underspecified tree - ie. unused valid vlcs - not allowed by SPEC)
102
2/2
✓ Branch 0 taken 81280 times.
✓ Branch 1 taken 2540 times.
83820 for (p = 1; p < 33; p++)
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81280 times.
81280 if (exit_at_level[p])
104 return AVERROR_INVALIDDATA;
105
106 2540 return 0;
107 }
108
109 115 int ff_vorbis_ready_floor1_list(void *logctx,
110 vorbis_floor1_entry *list, int values)
111 {
112 int i;
113 115 list[0].sort = 0;
114 115 list[1].sort = 1;
115
2/2
✓ Branch 0 taken 2081 times.
✓ Branch 1 taken 115 times.
2196 for (i = 2; i < values; i++) {
116 int j;
117 2081 list[i].low = 0;
118 2081 list[i].high = 1;
119 2081 list[i].sort = i;
120
2/2
✓ Branch 0 taken 23886 times.
✓ Branch 1 taken 2081 times.
25967 for (j = 2; j < i; j++) {
121 23886 int tmp = list[j].x;
122
2/2
✓ Branch 0 taken 16482 times.
✓ Branch 1 taken 7404 times.
23886 if (tmp < list[i].x) {
123
2/2
✓ Branch 0 taken 3012 times.
✓ Branch 1 taken 13470 times.
16482 if (tmp > list[list[i].low].x)
124 3012 list[i].low = j;
125 } else {
126
2/2
✓ Branch 0 taken 2858 times.
✓ Branch 1 taken 4546 times.
7404 if (tmp < list[list[i].high].x)
127 2858 list[i].high = j;
128 }
129 }
130 }
131
2/2
✓ Branch 0 taken 2196 times.
✓ Branch 1 taken 115 times.
2311 for (i = 0; i < values - 1; i++) {
132 int j;
133
2/2
✓ Branch 0 taken 28163 times.
✓ Branch 1 taken 2196 times.
30359 for (j = i + 1; j < values; j++) {
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28163 times.
28163 if (list[i].x == list[j].x) {
135 av_log(logctx, AV_LOG_ERROR,
136 "Duplicate value found in floor 1 X coordinates\n");
137 return AVERROR_INVALIDDATA;
138 }
139
2/2
✓ Branch 0 taken 9485 times.
✓ Branch 1 taken 18678 times.
28163 if (list[list[i].sort].x > list[list[j].sort].x) {
140 9485 int tmp = list[i].sort;
141 9485 list[i].sort = list[j].sort;
142 9485 list[j].sort = tmp;
143 }
144 }
145 }
146 115 return 0;
147 }
148
149 80568 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 {
153 80568 int err = -adx;
154 80568 x -= x1 - 1;
155 80568 buf += x1 - 1;
156
2/2
✓ Branch 0 taken 6567082 times.
✓ Branch 1 taken 80568 times.
6647650 while (++x < 0) {
157 6567082 err += ady;
158
2/2
✓ Branch 0 taken 696924 times.
✓ Branch 1 taken 5870158 times.
6567082 if (err >= 0) {
159 696924 err += ady - adx;
160 696924 y += sy;
161 696924 buf[x++] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
162 }
163 6567082 buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
164 }
165
2/2
✓ Branch 0 taken 67392 times.
✓ Branch 1 taken 13176 times.
80568 if (x <= 0) {
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67392 times.
67392 if (err + ady >= 0)
167 y += sy;
168 67392 buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
169 }
170 80568 }
171
172 134736 static void render_line(int x0, int y0, int x1, int y1, float *buf)
173 {
174 134736 int dy = y1 - y0;
175 134736 int adx = x1 - x0;
176 134736 int ady = FFABS(dy);
177
2/2
✓ Branch 0 taken 71378 times.
✓ Branch 1 taken 63358 times.
134736 int sy = dy < 0 ? -1 : 1;
178 134736 buf[x0] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y0)];
179
2/2
✓ Branch 0 taken 80568 times.
✓ Branch 1 taken 54168 times.
134736 if (ady*2 <= adx) { // optimized common case
180 80568 render_line_unrolled(x0, y0, x1, sy, ady, adx, buf);
181 } else {
182 54168 int base = dy / adx;
183 54168 int x = x0;
184 54168 int y = y0;
185 54168 int err = -adx;
186 54168 ady -= FFABS(base) * adx;
187
2/2
✓ Branch 0 taken 642234 times.
✓ Branch 1 taken 54168 times.
696402 while (++x < x1) {
188 642234 y += base;
189 642234 err += ady;
190
2/2
✓ Branch 0 taken 313233 times.
✓ Branch 1 taken 329001 times.
642234 if (err >= 0) {
191 313233 err -= adx;
192 313233 y += sy;
193 }
194 642234 buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
195 }
196 }
197 134736 }
198
199 10774 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 {
203 int lx, ly, i;
204 10774 lx = 0;
205 10774 ly = y_list[0] * multiplier;
206
1/2
✓ Branch 0 taken 257622 times.
✗ Branch 1 not taken.
257622 for (i = 1; i < values; i++) {
207 257622 int pos = list[i].sort;
208
2/2
✓ Branch 0 taken 134736 times.
✓ Branch 1 taken 122886 times.
257622 if (flag[pos]) {
209 134736 int x1 = list[pos].x;
210 134736 int y1 = y_list[pos] * multiplier;
211
1/2
✓ Branch 0 taken 134736 times.
✗ Branch 1 not taken.
134736 if (lx < samples)
212 134736 render_line(lx, ly, FFMIN(x1,samples), y1, out);
213 134736 lx = x1;
214 134736 ly = y1;
215 }
216
2/2
✓ Branch 0 taken 10774 times.
✓ Branch 1 taken 246848 times.
257622 if (lx >= samples)
217 10774 break;
218 }
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10774 times.
10774 if (lx < samples)
220 render_line(lx, ly, samples, ly, out);
221 10774 }
222