FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vorbis.c
Date: 2026-04-24 19:58:39
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 989 unsigned int ff_vorbis_nth_root(unsigned int x, unsigned int n)
42 {
43 989 unsigned int ret = 0, i, j;
44
45 do {
46 19016 ++ret;
47
2/2
✓ Branch 0 taken 14806 times.
✓ Branch 1 taken 19016 times.
33822 for (i = 0, j = ret; i < n - 1; i++)
48 14806 j *= ret;
49
2/2
✓ Branch 0 taken 18027 times.
✓ Branch 1 taken 989 times.
19016 } while (j <= x);
50
51 989 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 2250 int ff_vorbis_len2vlc(uint8_t *bits, uint32_t *codes, unsigned num)
60 {
61 2250 uint32_t exit_at_level[33] = { 404 };
62 unsigned i, j, p, code;
63
64
3/4
✓ Branch 0 taken 15932 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13682 times.
✓ Branch 3 taken 2250 times.
15932 for (p = 0; (p < num) && (bits[p] == 0); ++p)
65 ;
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2250 times.
2250 if (p == num)
67 return 0;
68
69 2250 codes[p] = 0;
70
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2250 times.
2250 if (bits[p] > 32)
71 return AVERROR_INVALIDDATA;
72
2/2
✓ Branch 0 taken 7706 times.
✓ Branch 1 taken 2250 times.
9956 for (i = 0; i < bits[p]; ++i)
73 7706 exit_at_level[i+1] = 1u << i;
74
75 2250 ++p;
76
77
2/4
✓ Branch 0 taken 2250 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2250 times.
2250 for (i = p; (i < num) && (bits[i] == 0); ++i)
78 ;
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2250 times.
2250 if (i == num)
80 return 0;
81
82
2/2
✓ Branch 0 taken 430085 times.
✓ Branch 1 taken 2250 times.
432335 for (; p < num; ++p) {
83
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 430085 times.
430085 if (bits[p] > 32)
84 return AVERROR_INVALIDDATA;
85
2/2
✓ Branch 0 taken 7798 times.
✓ Branch 1 taken 422287 times.
430085 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 836868 times.
✗ Branch 1 not taken.
836868 for (i = bits[p]; i > 0; --i)
89
2/2
✓ Branch 0 taken 422287 times.
✓ Branch 1 taken 414581 times.
836868 if (exit_at_level[i])
90 422287 break;
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 422287 times.
422287 if (!i) // overspecified tree
92 return AVERROR_INVALIDDATA;
93 422287 code = exit_at_level[i];
94 422287 exit_at_level[i] = 0;
95 // construct code (append 0s to end) and introduce new exits
96
2/2
✓ Branch 0 taken 414581 times.
✓ Branch 1 taken 422287 times.
836868 for (j = i + 1 ;j <= bits[p]; ++j)
97 414581 exit_at_level[j] = code + (1u << (j - 1));
98 422287 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 72000 times.
✓ Branch 1 taken 2250 times.
74250 for (p = 1; p < 33; p++)
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 72000 times.
72000 if (exit_at_level[p])
104 return AVERROR_INVALIDDATA;
105
106 2250 return 0;
107 }
108
109 99 int ff_vorbis_ready_floor1_list(void *logctx,
110 vorbis_floor1_entry *list, int values)
111 {
112 int i;
113 99 list[0].sort = 0;
114 99 list[1].sort = 1;
115
2/2
✓ Branch 0 taken 1815 times.
✓ Branch 1 taken 99 times.
1914 for (i = 2; i < values; i++) {
116 int j;
117 1815 list[i].low = 0;
118 1815 list[i].high = 1;
119 1815 list[i].sort = i;
120
2/2
✓ Branch 0 taken 20940 times.
✓ Branch 1 taken 1815 times.
22755 for (j = 2; j < i; j++) {
121 20940 int tmp = list[j].x;
122
2/2
✓ Branch 0 taken 14534 times.
✓ Branch 1 taken 6406 times.
20940 if (tmp < list[i].x) {
123
2/2
✓ Branch 0 taken 2650 times.
✓ Branch 1 taken 11884 times.
14534 if (tmp > list[list[i].low].x)
124 2650 list[i].low = j;
125 } else {
126
2/2
✓ Branch 0 taken 2496 times.
✓ Branch 1 taken 3910 times.
6406 if (tmp < list[list[i].high].x)
127 2496 list[i].high = j;
128 }
129 }
130 }
131
2/2
✓ Branch 0 taken 1914 times.
✓ Branch 1 taken 99 times.
2013 for (i = 0; i < values - 1; i++) {
132 int j;
133
2/2
✓ Branch 0 taken 24669 times.
✓ Branch 1 taken 1914 times.
26583 for (j = i + 1; j < values; j++) {
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24669 times.
24669 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 8221 times.
✓ Branch 1 taken 16448 times.
24669 if (list[list[i].sort].x > list[list[j].sort].x) {
140 8221 int tmp = list[i].sort;
141 8221 list[i].sort = list[j].sort;
142 8221 list[j].sort = tmp;
143 }
144 }
145 }
146 99 return 0;
147 }
148
149 78291 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 78291 int err = -adx;
154 78291 x -= x1 - 1;
155 78291 buf += x1 - 1;
156
2/2
✓ Branch 0 taken 6385882 times.
✓ Branch 1 taken 78291 times.
6464173 while (++x < 0) {
157 6385882 err += ady;
158
2/2
✓ Branch 0 taken 659038 times.
✓ Branch 1 taken 5726844 times.
6385882 if (err >= 0) {
159 659038 err += ady - adx;
160 659038 y += sy;
161 659038 buf[x++] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
162 }
163 6385882 buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
164 }
165
2/2
✓ Branch 0 taken 65531 times.
✓ Branch 1 taken 12760 times.
78291 if (x <= 0) {
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65531 times.
65531 if (err + ady >= 0)
167 y += sy;
168 65531 buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
169 }
170 78291 }
171
172 130219 static void render_line(int x0, int y0, int x1, int y1, float *buf)
173 {
174 130219 int dy = y1 - y0;
175 130219 int adx = x1 - x0;
176 130219 int ady = FFABS(dy);
177
2/2
✓ Branch 0 taken 69015 times.
✓ Branch 1 taken 61204 times.
130219 int sy = dy < 0 ? -1 : 1;
178 130219 buf[x0] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y0)];
179
2/2
✓ Branch 0 taken 78291 times.
✓ Branch 1 taken 51928 times.
130219 if (ady*2 <= adx) { // optimized common case
180 78291 render_line_unrolled(x0, y0, x1, sy, ady, adx, buf);
181 } else {
182 51928 int base = dy / adx;
183 51928 int x = x0;
184 51928 int y = y0;
185 51928 int err = -adx;
186 51928 ady -= FFABS(base) * adx;
187
2/2
✓ Branch 0 taken 599026 times.
✓ Branch 1 taken 51928 times.
650954 while (++x < x1) {
188 599026 y += base;
189 599026 err += ady;
190
2/2
✓ Branch 0 taken 291097 times.
✓ Branch 1 taken 307929 times.
599026 if (err >= 0) {
191 291097 err -= adx;
192 291097 y += sy;
193 }
194 599026 buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
195 }
196 }
197 130219 }
198
199 10299 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 10299 lx = 0;
205 10299 ly = y_list[0] * multiplier;
206
1/2
✓ Branch 0 taken 249188 times.
✗ Branch 1 not taken.
249188 for (i = 1; i < values; i++) {
207 249188 int pos = list[i].sort;
208
2/2
✓ Branch 0 taken 130219 times.
✓ Branch 1 taken 118969 times.
249188 if (flag[pos]) {
209 130219 int x1 = list[pos].x;
210 130219 int y1 = y_list[pos] * multiplier;
211
1/2
✓ Branch 0 taken 130219 times.
✗ Branch 1 not taken.
130219 if (lx < samples)
212 130219 render_line(lx, ly, FFMIN(x1,samples), y1, out);
213 130219 lx = x1;
214 130219 ly = y1;
215 }
216
2/2
✓ Branch 0 taken 10299 times.
✓ Branch 1 taken 238889 times.
249188 if (lx >= samples)
217 10299 break;
218 }
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10299 times.
10299 if (lx < samples)
220 render_line(lx, ly, samples, ly, out);
221 10299 }
222