FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vorbis.c
Date: 2025-06-01 09:29:47
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 969 unsigned int ff_vorbis_nth_root(unsigned int x, unsigned int n)
42 {
43 969 unsigned int ret = 0, i, j;
44
45 do {
46 18828 ++ret;
47
2/2
✓ Branch 0 taken 14538 times.
✓ Branch 1 taken 18828 times.
33366 for (i = 0, j = ret; i < n - 1; i++)
48 14538 j *= ret;
49
2/2
✓ Branch 0 taken 17859 times.
✓ Branch 1 taken 969 times.
18828 } while (j <= x);
50
51 969 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 2180 int ff_vorbis_len2vlc(uint8_t *bits, uint32_t *codes, unsigned num)
60 {
61 2180 uint32_t exit_at_level[33] = { 404 };
62 unsigned i, j, p, code;
63
64
3/4
✓ Branch 0 taken 15318 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13138 times.
✓ Branch 3 taken 2180 times.
15318 for (p = 0; (p < num) && (bits[p] == 0); ++p)
65 ;
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2180 times.
2180 if (p == num)
67 return 0;
68
69 2180 codes[p] = 0;
70
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2180 times.
2180 if (bits[p] > 32)
71 return AVERROR_INVALIDDATA;
72
2/2
✓ Branch 0 taken 7470 times.
✓ Branch 1 taken 2180 times.
9650 for (i = 0; i < bits[p]; ++i)
73 7470 exit_at_level[i+1] = 1u << i;
74
75 2180 ++p;
76
77
2/4
✓ Branch 0 taken 2180 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2180 times.
2180 for (i = p; (i < num) && (bits[i] == 0); ++i)
78 ;
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2180 times.
2180 if (i == num)
80 return 0;
81
82
2/2
✓ Branch 0 taken 422543 times.
✓ Branch 1 taken 2180 times.
424723 for (; p < num; ++p) {
83
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 422543 times.
422543 if (bits[p] > 32)
84 return AVERROR_INVALIDDATA;
85
2/2
✓ Branch 0 taken 7798 times.
✓ Branch 1 taken 414745 times.
422543 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 822020 times.
✗ Branch 1 not taken.
822020 for (i = bits[p]; i > 0; --i)
89
2/2
✓ Branch 0 taken 414745 times.
✓ Branch 1 taken 407275 times.
822020 if (exit_at_level[i])
90 414745 break;
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 414745 times.
414745 if (!i) // overspecified tree
92 return AVERROR_INVALIDDATA;
93 414745 code = exit_at_level[i];
94 414745 exit_at_level[i] = 0;
95 // construct code (append 0s to end) and introduce new exits
96
2/2
✓ Branch 0 taken 407275 times.
✓ Branch 1 taken 414745 times.
822020 for (j = i + 1 ;j <= bits[p]; ++j)
97 407275 exit_at_level[j] = code + (1u << (j - 1));
98 414745 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 69760 times.
✓ Branch 1 taken 2180 times.
71940 for (p = 1; p < 33; p++)
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 69760 times.
69760 if (exit_at_level[p])
104 return AVERROR_INVALIDDATA;
105
106 2180 return 0;
107 }
108
109 95 int ff_vorbis_ready_floor1_list(void *logctx,
110 vorbis_floor1_entry *list, int values)
111 {
112 int i;
113 95 list[0].sort = 0;
114 95 list[1].sort = 1;
115
2/2
✓ Branch 0 taken 1747 times.
✓ Branch 1 taken 95 times.
1842 for (i = 2; i < values; i++) {
116 int j;
117 1747 list[i].low = 0;
118 1747 list[i].high = 1;
119 1747 list[i].sort = i;
120
2/2
✓ Branch 0 taken 20196 times.
✓ Branch 1 taken 1747 times.
21943 for (j = 2; j < i; j++) {
121 20196 int tmp = list[j].x;
122
2/2
✓ Branch 0 taken 14042 times.
✓ Branch 1 taken 6154 times.
20196 if (tmp < list[i].x) {
123
2/2
✓ Branch 0 taken 2558 times.
✓ Branch 1 taken 11484 times.
14042 if (tmp > list[list[i].low].x)
124 2558 list[i].low = j;
125 } else {
126
2/2
✓ Branch 0 taken 2404 times.
✓ Branch 1 taken 3750 times.
6154 if (tmp < list[list[i].high].x)
127 2404 list[i].high = j;
128 }
129 }
130 }
131
2/2
✓ Branch 0 taken 1842 times.
✓ Branch 1 taken 95 times.
1937 for (i = 0; i < values - 1; i++) {
132 int j;
133
2/2
✓ Branch 0 taken 23785 times.
✓ Branch 1 taken 1842 times.
25627 for (j = i + 1; j < values; j++) {
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23785 times.
23785 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 7901 times.
✓ Branch 1 taken 15884 times.
23785 if (list[list[i].sort].x > list[list[j].sort].x) {
140 7901 int tmp = list[i].sort;
141 7901 list[i].sort = list[j].sort;
142 7901 list[j].sort = tmp;
143 }
144 }
145 }
146 95 return 0;
147 }
148
149 78261 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 78261 int err = -adx;
154 78261 x -= x1 - 1;
155 78261 buf += x1 - 1;
156
2/2
✓ Branch 0 taken 6382630 times.
✓ Branch 1 taken 78261 times.
6460891 while (++x < 0) {
157 6382630 err += ady;
158
2/2
✓ Branch 0 taken 658272 times.
✓ Branch 1 taken 5724358 times.
6382630 if (err >= 0) {
159 658272 err += ady - adx;
160 658272 y += sy;
161 658272 buf[x++] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
162 }
163 6382630 buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
164 }
165
2/2
✓ Branch 0 taken 65513 times.
✓ Branch 1 taken 12748 times.
78261 if (x <= 0) {
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65513 times.
65513 if (err + ady >= 0)
167 y += sy;
168 65513 buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
169 }
170 78261 }
171
172 130157 static void render_line(int x0, int y0, int x1, int y1, float *buf)
173 {
174 130157 int dy = y1 - y0;
175 130157 int adx = x1 - x0;
176 130157 int ady = FFABS(dy);
177
2/2
✓ Branch 0 taken 68977 times.
✓ Branch 1 taken 61180 times.
130157 int sy = dy < 0 ? -1 : 1;
178 130157 buf[x0] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y0)];
179
2/2
✓ Branch 0 taken 78261 times.
✓ Branch 1 taken 51896 times.
130157 if (ady*2 <= adx) { // optimized common case
180 78261 render_line_unrolled(x0, y0, x1, sy, ady, adx, buf);
181 } else {
182 51896 int base = dy / adx;
183 51896 int x = x0;
184 51896 int y = y0;
185 51896 int err = -adx;
186 51896 ady -= FFABS(base) * adx;
187
2/2
✓ Branch 0 taken 598516 times.
✓ Branch 1 taken 51896 times.
650412 while (++x < x1) {
188 598516 y += base;
189 598516 err += ady;
190
2/2
✓ Branch 0 taken 290755 times.
✓ Branch 1 taken 307761 times.
598516 if (err >= 0) {
191 290755 err -= adx;
192 290755 y += sy;
193 }
194 598516 buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
195 }
196 }
197 130157 }
198
199 10291 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 10291 lx = 0;
205 10291 ly = y_list[0] * multiplier;
206
1/2
✓ Branch 0 taken 249044 times.
✗ Branch 1 not taken.
249044 for (i = 1; i < values; i++) {
207 249044 int pos = list[i].sort;
208
2/2
✓ Branch 0 taken 130157 times.
✓ Branch 1 taken 118887 times.
249044 if (flag[pos]) {
209 130157 int x1 = list[pos].x;
210 130157 int y1 = y_list[pos] * multiplier;
211
1/2
✓ Branch 0 taken 130157 times.
✗ Branch 1 not taken.
130157 if (lx < samples)
212 130157 render_line(lx, ly, FFMIN(x1,samples), y1, out);
213 130157 lx = x1;
214 130157 ly = y1;
215 }
216
2/2
✓ Branch 0 taken 10291 times.
✓ Branch 1 taken 238753 times.
249044 if (lx >= samples)
217 10291 break;
218 }
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10291 times.
10291 if (lx < samples)
220 render_line(lx, ly, samples, ly, out);
221 10291 }
222