FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vorbis.c
Date: 2021-09-22 05:58:22
Exec Total Coverage
Lines: 108 118 91.5%
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
31 #include "avcodec.h"
32 #include "vorbis.h"
33
34
35 /* Helper functions */
36
37 // x^(1/n)
38 899 unsigned int ff_vorbis_nth_root(unsigned int x, unsigned int n)
39 {
40 899 unsigned int ret = 0, i, j;
41
42 do {
43 18210 ++ret;
44
2/2
✓ Branch 0 taken 13592 times.
✓ Branch 1 taken 18210 times.
31802 for (i = 0, j = ret; i < n - 1; i++)
45 13592 j *= ret;
46
2/2
✓ Branch 0 taken 17311 times.
✓ Branch 1 taken 899 times.
18210 } while (j <= x);
47
48 899 return ret - 1;
49 }
50
51 // Generate vlc codes from vorbis huffman code lengths
52
53 // the two bits[p] > 32 checks should be redundant, all calling code should
54 // already ensure that, but since it allows overwriting the stack it seems
55 // reasonable to check redundantly.
56 1947 int ff_vorbis_len2vlc(uint8_t *bits, uint32_t *codes, unsigned num)
57 {
58 1947 uint32_t exit_at_level[33] = { 404 };
59 unsigned i, j, p, code;
60
61
3/4
✓ Branch 0 taken 13305 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11358 times.
✓ Branch 3 taken 1947 times.
13305 for (p = 0; (p < num) && (bits[p] == 0); ++p)
62 ;
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1947 times.
1947 if (p == num)
64 return 0;
65
66 1947 codes[p] = 0;
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1947 times.
1947 if (bits[p] > 32)
68 return AVERROR_INVALIDDATA;
69
2/2
✓ Branch 0 taken 6732 times.
✓ Branch 1 taken 1947 times.
8679 for (i = 0; i < bits[p]; ++i)
70 6732 exit_at_level[i+1] = 1u << i;
71
72 1947 ++p;
73
74
2/4
✓ Branch 0 taken 1947 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1947 times.
1947 for (i = p; (i < num) && (bits[i] == 0); ++i)
75 ;
76
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1947 times.
1947 if (i == num)
77 return 0;
78
79
2/2
✓ Branch 0 taken 395150 times.
✓ Branch 1 taken 1947 times.
397097 for (; p < num; ++p) {
80
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 395150 times.
395150 if (bits[p] > 32)
81 return AVERROR_INVALIDDATA;
82
2/2
✓ Branch 0 taken 7798 times.
✓ Branch 1 taken 387352 times.
395150 if (bits[p] == 0)
83 7798 continue;
84 // find corresponding exit(node which the tree can grow further from)
85
1/2
✓ Branch 0 taken 767972 times.
✗ Branch 1 not taken.
767972 for (i = bits[p]; i > 0; --i)
86
2/2
✓ Branch 0 taken 387352 times.
✓ Branch 1 taken 380620 times.
767972 if (exit_at_level[i])
87 387352 break;
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 387352 times.
387352 if (!i) // overspecified tree
89 return AVERROR_INVALIDDATA;
90 387352 code = exit_at_level[i];
91 387352 exit_at_level[i] = 0;
92 // construct code (append 0s to end) and introduce new exits
93
2/2
✓ Branch 0 taken 380620 times.
✓ Branch 1 taken 387352 times.
767972 for (j = i + 1 ;j <= bits[p]; ++j)
94 380620 exit_at_level[j] = code + (1u << (j - 1));
95 387352 codes[p] = code;
96 }
97
98 //no exits should be left (underspecified tree - ie. unused valid vlcs - not allowed by SPEC)
99
2/2
✓ Branch 0 taken 62304 times.
✓ Branch 1 taken 1947 times.
64251 for (p = 1; p < 33; p++)
100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62304 times.
62304 if (exit_at_level[p])
101 return AVERROR_INVALIDDATA;
102
103 1947 return 0;
104 }
105
106 81 int ff_vorbis_ready_floor1_list(AVCodecContext *avctx,
107 vorbis_floor1_entry *list, int values)
108 {
109 int i;
110 81 list[0].sort = 0;
111 81 list[1].sort = 1;
112
2/2
✓ Branch 0 taken 1521 times.
✓ Branch 1 taken 81 times.
1602 for (i = 2; i < values; i++) {
113 int j;
114 1521 list[i].low = 0;
115 1521 list[i].high = 1;
116 1521 list[i].sort = i;
117
2/2
✓ Branch 0 taken 17652 times.
✓ Branch 1 taken 1521 times.
19173 for (j = 2; j < i; j++) {
118 17652 int tmp = list[j].x;
119
2/2
✓ Branch 0 taken 12368 times.
✓ Branch 1 taken 5284 times.
17652 if (tmp < list[i].x) {
120
2/2
✓ Branch 0 taken 2248 times.
✓ Branch 1 taken 10120 times.
12368 if (tmp > list[list[i].low].x)
121 2248 list[i].low = j;
122 } else {
123
2/2
✓ Branch 0 taken 2086 times.
✓ Branch 1 taken 3198 times.
5284 if (tmp < list[list[i].high].x)
124 2086 list[i].high = j;
125 }
126 }
127 }
128
2/2
✓ Branch 0 taken 1602 times.
✓ Branch 1 taken 81 times.
1683 for (i = 0; i < values - 1; i++) {
129 int j;
130
2/2
✓ Branch 0 taken 20775 times.
✓ Branch 1 taken 1602 times.
22377 for (j = i + 1; j < values; j++) {
131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20775 times.
20775 if (list[i].x == list[j].x) {
132 av_log(avctx, AV_LOG_ERROR,
133 "Duplicate value found in floor 1 X coordinates\n");
134 return AVERROR_INVALIDDATA;
135 }
136
2/2
✓ Branch 0 taken 6805 times.
✓ Branch 1 taken 13970 times.
20775 if (list[list[i].sort].x > list[list[j].sort].x) {
137 6805 int tmp = list[i].sort;
138 6805 list[i].sort = list[j].sort;
139 6805 list[j].sort = tmp;
140 }
141 }
142 }
143 81 return 0;
144 }
145
146 78227 static inline void render_line_unrolled(intptr_t x, int y, int x1,
147 intptr_t sy, int ady, int adx,
148 float *buf)
149 {
150 78227 int err = -adx;
151 78227 x -= x1 - 1;
152 78227 buf += x1 - 1;
153
2/2
✓ Branch 0 taken 6379130 times.
✓ Branch 1 taken 78227 times.
6457357 while (++x < 0) {
154 6379130 err += ady;
155
2/2
✓ Branch 0 taken 657502 times.
✓ Branch 1 taken 5721628 times.
6379130 if (err >= 0) {
156 657502 err += ady - adx;
157 657502 y += sy;
158 657502 buf[x++] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
159 }
160 6379130 buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
161 }
162
2/2
✓ Branch 0 taken 65489 times.
✓ Branch 1 taken 12738 times.
78227 if (x <= 0) {
163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65489 times.
65489 if (err + ady >= 0)
164 y += sy;
165 65489 buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
166 }
167 78227 }
168
169 130087 static void render_line(int x0, int y0, int x1, int y1, float *buf)
170 {
171 130087 int dy = y1 - y0;
172 130087 int adx = x1 - x0;
173 130087 int ady = FFABS(dy);
174
2/2
✓ Branch 0 taken 68934 times.
✓ Branch 1 taken 61153 times.
130087 int sy = dy < 0 ? -1 : 1;
175 130087 buf[x0] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y0)];
176
2/2
✓ Branch 0 taken 78227 times.
✓ Branch 1 taken 51860 times.
130087 if (ady*2 <= adx) { // optimized common case
177 78227 render_line_unrolled(x0, y0, x1, sy, ady, adx, buf);
178 } else {
179 51860 int base = dy / adx;
180 51860 int x = x0;
181 51860 int y = y0;
182 51860 int err = -adx;
183 51860 ady -= FFABS(base) * adx;
184
2/2
✓ Branch 0 taken 597632 times.
✓ Branch 1 taken 51860 times.
649492 while (++x < x1) {
185 597632 y += base;
186 597632 err += ady;
187
2/2
✓ Branch 0 taken 290398 times.
✓ Branch 1 taken 307234 times.
597632 if (err >= 0) {
188 290398 err -= adx;
189 290398 y += sy;
190 }
191 597632 buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
192 }
193 }
194 130087 }
195
196 10281 void ff_vorbis_floor1_render_list(vorbis_floor1_entry * list, int values,
197 uint16_t *y_list, int *flag,
198 int multiplier, float *out, int samples)
199 {
200 int lx, ly, i;
201 10281 lx = 0;
202 10281 ly = y_list[0] * multiplier;
203
1/2
✓ Branch 0 taken 248893 times.
✗ Branch 1 not taken.
248893 for (i = 1; i < values; i++) {
204 248893 int pos = list[i].sort;
205
2/2
✓ Branch 0 taken 130087 times.
✓ Branch 1 taken 118806 times.
248893 if (flag[pos]) {
206 130087 int x1 = list[pos].x;
207 130087 int y1 = y_list[pos] * multiplier;
208
1/2
✓ Branch 0 taken 130087 times.
✗ Branch 1 not taken.
130087 if (lx < samples)
209 130087 render_line(lx, ly, FFMIN(x1,samples), y1, out);
210 130087 lx = x1;
211 130087 ly = y1;
212 }
213
2/2
✓ Branch 0 taken 10281 times.
✓ Branch 1 taken 238612 times.
248893 if (lx >= samples)
214 10281 break;
215 }
216
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10281 times.
10281 if (lx < samples)
217 render_line(lx, ly, samples, ly, out);
218 10281 }
219