../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
108 / 118
91.5%
Branches:
67 / 82
81.7%
Functions:
6 / 6
100.0%

libavcodec/vorbis.c
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 1231 unsigned int ff_vorbis_nth_root(unsigned int x, unsigned int n)
42 {
43 1231 unsigned int ret = 0, i, j;
44
45 do {
46 25361 ++ret;
47
2/2
✓ Branch 5 → 4 taken 18817 times.
✓ Branch 5 → 6 taken 25361 times.
44178 for (i = 0, j = ret; i < n - 1; i++)
48 18817 j *= ret;
49
2/2
✓ Branch 6 → 7 taken 24130 times.
✓ Branch 6 → 8 taken 1231 times.
25361 } while (j <= x);
50
51 1231 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 2818 int ff_vorbis_len2vlc(uint8_t *bits, uint32_t *codes, unsigned num)
60 {
61 2818 uint32_t exit_at_level[33] = { 404 };
62 unsigned i, j, p, code;
63
64
3/4
✓ Branch 4 → 5 taken 19943 times.
✗ Branch 4 → 6 not taken.
✓ Branch 5 → 3 taken 17125 times.
✓ Branch 5 → 6 taken 2818 times.
19943 for (p = 0; (p < num) && (bits[p] == 0); ++p)
65 ;
66
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 2818 times.
2818 if (p == num)
67 ✗ return 0;
68
69 2818 codes[p] = 0;
70
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 2818 times.
2818 if (bits[p] > 32)
71 ✗ return AVERROR_INVALIDDATA;
72
2/2
✓ Branch 12 → 11 taken 9748 times.
✓ Branch 12 → 13 taken 2818 times.
12566 for (i = 0; i < bits[p]; ++i)
73 9748 exit_at_level[i+1] = 1u << i;
74
75 2818 ++p;
76
77
2/4
✓ Branch 15 → 16 taken 2818 times.
✗ Branch 15 → 17 not taken.
✗ Branch 16 → 14 not taken.
✓ Branch 16 → 17 taken 2818 times.
2818 for (i = p; (i < num) && (bits[i] == 0); ++i)
78 ;
79
1/2
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 19 taken 2818 times.
2818 if (i == num)
80 ✗ return 0;
81
82
2/2
✓ Branch 36 → 20 taken 561588 times.
✓ Branch 36 → 37 taken 2818 times.
564406 for (; p < num; ++p) {
83
1/2
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 561588 times.
561588 if (bits[p] > 32)
84 ✗ return AVERROR_INVALIDDATA;
85
2/2
✓ Branch 22 → 23 taken 7798 times.
✓ Branch 22 → 24 taken 553790 times.
561588 if (bits[p] == 0)
86 7798 continue;
87 // find corresponding exit(node which the tree can grow further from)
88
1/2
✓ Branch 28 → 25 taken 1097832 times.
✗ Branch 28 → 29 not taken.
1097832 for (i = bits[p]; i > 0; --i)
89
2/2
✓ Branch 25 → 26 taken 553790 times.
✓ Branch 25 → 27 taken 544042 times.
1097832 if (exit_at_level[i])
90 553790 break;
91
1/2
✗ Branch 29 → 30 not taken.
✓ Branch 29 → 31 taken 553790 times.
553790 if (!i) // overspecified tree
92 ✗ return AVERROR_INVALIDDATA;
93 553790 code = exit_at_level[i];
94 553790 exit_at_level[i] = 0;
95 // construct code (append 0s to end) and introduce new exits
96
2/2
✓ Branch 33 → 32 taken 544042 times.
✓ Branch 33 → 34 taken 553790 times.
1097832 for (j = i + 1 ;j <= bits[p]; ++j)
97 544042 exit_at_level[j] = code + (1u << (j - 1));
98 553790 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 41 → 38 taken 90176 times.
✓ Branch 41 → 42 taken 2818 times.
92994 for (p = 1; p < 33; p++)
103
1/2
✗ Branch 38 → 39 not taken.
✓ Branch 38 → 40 taken 90176 times.
90176 if (exit_at_level[p])
104 ✗ return AVERROR_INVALIDDATA;
105
106 2818 return 0;
107 }
108
109 125 int ff_vorbis_ready_floor1_list(void *logctx,
110 vorbis_floor1_entry *list, int values)
111 {
112 int i;
113 125 list[0].sort = 0;
114 125 list[1].sort = 1;
115
2/2
✓ Branch 12 → 3 taken 2248 times.
✓ Branch 12 → 13 taken 125 times.
2373 for (i = 2; i < values; i++) {
116 int j;
117 2248 list[i].low = 0;
118 2248 list[i].high = 1;
119 2248 list[i].sort = i;
120
2/2
✓ Branch 10 → 4 taken 25731 times.
✓ Branch 10 → 11 taken 2248 times.
27979 for (j = 2; j < i; j++) {
121 25731 int tmp = list[j].x;
122
2/2
✓ Branch 4 → 5 taken 17700 times.
✓ Branch 4 → 7 taken 8031 times.
25731 if (tmp < list[i].x) {
123
2/2
✓ Branch 5 → 6 taken 3239 times.
✓ Branch 5 → 9 taken 14461 times.
17700 if (tmp > list[list[i].low].x)
124 3239 list[i].low = j;
125 } else {
126
2/2
✓ Branch 7 → 8 taken 3087 times.
✓ Branch 7 → 9 taken 4944 times.
8031 if (tmp < list[list[i].high].x)
127 3087 list[i].high = j;
128 }
129 }
130 }
131
2/2
✓ Branch 23 → 14 taken 2373 times.
✓ Branch 23 → 24 taken 125 times.
2498 for (i = 0; i < values - 1; i++) {
132 int j;
133
2/2
✓ Branch 21 → 15 taken 30352 times.
✓ Branch 21 → 22 taken 2373 times.
32725 for (j = i + 1; j < values; j++) {
134
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 18 taken 30352 times.
30352 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 18 → 19 taken 10279 times.
✓ Branch 18 → 20 taken 20073 times.
30352 if (list[list[i].sort].x > list[list[j].sort].x) {
140 10279 int tmp = list[i].sort;
141 10279 list[i].sort = list[j].sort;
142 10279 list[j].sort = tmp;
143 }
144 }
145 }
146 125 return 0;
147 }
148
149 80566 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 80566 int err = -adx;
154 80566 x -= x1 - 1;
155 80566 buf += x1 - 1;
156
2/2
✓ Branch 6 → 3 taken 6567234 times.
✓ Branch 6 → 7 taken 80566 times.
6647800 while (++x < 0) {
157 6567234 err += ady;
158
2/2
✓ Branch 3 → 4 taken 696974 times.
✓ Branch 3 → 5 taken 5870260 times.
6567234 if (err >= 0) {
159 696974 err += ady - adx;
160 696974 y += sy;
161 696974 buf[x++] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
162 }
163 6567234 buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
164 }
165
2/2
✓ Branch 7 → 8 taken 67394 times.
✓ Branch 7 → 11 taken 13172 times.
80566 if (x <= 0) {
166
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 67394 times.
67394 if (err + ady >= 0)
167 ✗ y += sy;
168 67394 buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
169 }
170 80566 }
171
172 134743 static void render_line(int x0, int y0, int x1, int y1, float *buf)
173 {
174 134743 int dy = y1 - y0;
175 134743 int adx = x1 - x0;
176 134743 int ady = FFABS(dy);
177
2/2
✓ Branch 2 → 3 taken 71385 times.
✓ Branch 2 → 4 taken 63358 times.
134743 int sy = dy < 0 ? -1 : 1;
178 134743 buf[x0] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y0)];
179
2/2
✓ Branch 5 → 6 taken 80566 times.
✓ Branch 5 → 7 taken 54177 times.
134743 if (ady*2 <= adx) { // optimized common case
180 80566 render_line_unrolled(x0, y0, x1, sy, ady, adx, buf);
181 } else {
182 54177 int base = dy / adx;
183 54177 int x = x0;
184 54177 int y = y0;
185 54177 int err = -adx;
186 54177 ady -= FFABS(base) * adx;
187
2/2
✓ Branch 11 → 8 taken 642535 times.
✓ Branch 11 → 12 taken 54177 times.
696712 while (++x < x1) {
188 642535 y += base;
189 642535 err += ady;
190
2/2
✓ Branch 8 → 9 taken 313352 times.
✓ Branch 8 → 10 taken 329183 times.
642535 if (err >= 0) {
191 313352 err -= adx;
192 313352 y += sy;
193 }
194 642535 buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
195 }
196 }
197 134743 }
198
199 10777 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 10777 lx = 0;
205 10777 ly = y_list[0] * multiplier;
206
1/2
✓ Branch 10 → 3 taken 257643 times.
✗ Branch 10 → 11 not taken.
257643 for (i = 1; i < values; i++) {
207 257643 int pos = list[i].sort;
208
2/2
✓ Branch 3 → 4 taken 134743 times.
✓ Branch 3 → 7 taken 122900 times.
257643 if (flag[pos]) {
209 134743 int x1 = list[pos].x;
210 134743 int y1 = y_list[pos] * multiplier;
211
1/2
✓ Branch 4 → 5 taken 134743 times.
✗ Branch 4 → 6 not taken.
134743 if (lx < samples)
212 134743 render_line(lx, ly, FFMIN(x1,samples), y1, out);
213 134743 lx = x1;
214 134743 ly = y1;
215 }
216
2/2
✓ Branch 7 → 8 taken 10777 times.
✓ Branch 7 → 9 taken 246866 times.
257643 if (lx >= samples)
217 10777 break;
218 }
219
1/2
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 10777 times.
10777 if (lx < samples)
220 ✗ render_line(lx, ly, samples, ly, out);
221 10777 }
222