FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aac/aacdec_ac.c
Date: 2026-08-15 14:54:27
Exec Total Coverage
Lines: 102 120 85.0%
Functions: 7 7 100.0%
Branches: 44 63 69.8%

Line Branch Exec Source
1 /*
2 * AAC definitions and structures
3 * Copyright (c) 2024 Lynne
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "libavcodec/aactab.h"
23 #include "aacdec_ac.h"
24
25 19988 uint32_t ff_aac_ac_map_process(AACArithState *state, int reset, int N)
26 {
27 float ratio;
28
2/2
✓ Branch 0 taken 2191 times.
✓ Branch 1 taken 17797 times.
19988 if (reset) {
29 2191 memset(state->last, 0, sizeof(state->last));
30 2191 state->last_len = N;
31
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 17795 times.
17797 } else if (state->last_len != N) {
32 int i;
33 uint8_t last[512 /* 2048 / 4 */];
34 2 memcpy(last, state->last, sizeof(last));
35
36 2 ratio = state->last_len / (float)N;
37
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 2 times.
130 for (i = 0; i < N/2; i++) {
38 128 int k = (int)(i * ratio);
39 128 state->last[i] = last[k];
40 }
41
42
2/2
✓ Branch 0 taken 898 times.
✓ Branch 1 taken 2 times.
900 for (; i < FF_ARRAY_ELEMS(state->last); i++)
43 898 state->last[i] = 0;
44
45 2 state->last_len = N;
46 }
47
48 19988 state->cur[3] = 0;
49 19988 state->cur[2] = 0;
50 19988 state->cur[1] = 0;
51 19988 state->cur[0] = 1;
52
53 19988 state->state_pre = state->last[0] << 12;
54 19988 return state->last[0] << 12;
55 }
56
57 3773419 uint32_t ff_aac_ac_get_context(AACArithState *state, uint32_t c, int i, int N)
58 {
59 3773419 c = state->state_pre >> 8;
60 3773419 c = c + (state->last[i + 1] << 8);
61 3773419 c = (c << 4);
62 3773419 c += state->cur[1];
63
64 3773419 state->state_pre = c;
65
66
2/2
✓ Branch 0 taken 3693744 times.
✓ Branch 1 taken 79675 times.
3773419 if (i > 3 &&
67
2/2
✓ Branch 0 taken 2270143 times.
✓ Branch 1 taken 1423601 times.
3693744 ((state->cur[3] + state->cur[2] + state->cur[1]) < 5))
68 2270143 return c + 0x10000;
69
70 1503276 return c;
71 }
72
73 4024691 uint32_t ff_aac_ac_get_pk(uint32_t c)
74 {
75 4024691 int i_min = -1;
76 int i, j;
77 4024691 int i_max = FF_ARRAY_ELEMS(ff_aac_ac_lookup_m) - 1;
78
2/2
✓ Branch 0 taken 36712539 times.
✓ Branch 1 taken 1468300 times.
38180839 while ((i_max - i_min) > 1) {
79 36712539 i = i_min + ((i_max - i_min) / 2);
80 36712539 j = ff_aac_ac_hash_m[i];
81
2/2
✓ Branch 0 taken 18328308 times.
✓ Branch 1 taken 18384231 times.
36712539 if (c < (j >> 8))
82 18328308 i_max = i;
83
2/2
✓ Branch 0 taken 15827840 times.
✓ Branch 1 taken 2556391 times.
18384231 else if (c > (j >> 8))
84 15827840 i_min = i;
85 else
86 2556391 return (j & 0xFF);
87 }
88 1468300 return ff_aac_ac_lookup_m[i_max];
89 }
90
91 3758095 void ff_aac_ac_update_context(AACArithState *state, int idx,
92 uint16_t a, uint16_t b)
93 {
94 3758095 state->cur[0] = FFMIN(a + b + 1, 0xF);
95 3758095 state->cur[3] = state->cur[2];
96 3758095 state->cur[2] = state->cur[1];
97 3758095 state->cur[1] = state->cur[0];
98
99 3758095 state->last[idx] = state->cur[0];
100 3758095 }
101
102 /* Initialize AC */
103 19976 void ff_aac_ac_init(AACArith *ac, GetBitContext *gb)
104 {
105 19976 ac->low = 0;
106 19976 ac->high = UINT16_MAX;
107 19976 ac->val = get_bits(gb, 16);
108 19976 }
109
110 4260639 uint16_t ff_aac_ac_decode(AACArith *ac, GetBitContext *gb,
111 const uint16_t *cdf, uint16_t cdf_len)
112 {
113 4260639 int val = ac->val;
114 4260639 int low = ac->low;
115 4260639 int high = ac->high;
116
117 int sym;
118 4260639 int rng = high - low + 1;
119 4260639 int c = ((((int)(val - low + 1)) << 14) - ((int)1));
120
121 4260639 const uint16_t *p = cdf - 1;
122
123 /* One for each possible CDF length in the spec */
124
2/5
✗ Branch 0 not taken.
✓ Branch 1 taken 235948 times.
✓ Branch 2 taken 4024691 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
4260639 switch (cdf_len) {
125 case 2:
126 if ((p[1] * rng) > c)
127 p += 1;
128 break;
129 235948 case 4:
130
2/2
✓ Branch 0 taken 109545 times.
✓ Branch 1 taken 126403 times.
235948 if ((p[2] * rng) > c)
131 109545 p += 2;
132
2/2
✓ Branch 0 taken 109962 times.
✓ Branch 1 taken 125986 times.
235948 if ((p[1] * rng) > c)
133 109962 p += 1;
134 235948 break;
135 4024691 case 17:
136 /* First check if the current probability is even met at all */
137
2/2
✓ Branch 0 taken 2341624 times.
✓ Branch 1 taken 1683067 times.
4024691 if ((p[1] * rng) <= c)
138 2341624 break;
139 1683067 p += 1;
140
2/2
✓ Branch 0 taken 6732268 times.
✓ Branch 1 taken 1683067 times.
8415335 for (int i = 8; i >= 1; i >>= 1)
141
2/2
✓ Branch 0 taken 2726704 times.
✓ Branch 1 taken 4005564 times.
6732268 if ((p[i] * rng) > c)
142 2726704 p += i;
143 1683067 break;
144 case 27:
145 if ((p[16] * rng) > c)
146 p += 16;
147 if ((p[8] * rng) > c)
148 p += 8;
149 if (p != (cdf - 1 + 24))
150 if ((p[4] * rng) > c)
151 p += 4;
152 if ((p[2] * rng) > c)
153 p += 2;
154
155 if (p != (cdf - 1 + 24 + 2))
156 if ((p[1] * rng) > c)
157 p += 1;
158 break;
159 4260639 default:
160 /* This should never happen */
161 av_assert2(0);
162 }
163
164 4260639 sym = (int)((ptrdiff_t)(p - cdf)) + 1;
165
2/2
✓ Branch 0 taken 1851451 times.
✓ Branch 1 taken 2409188 times.
4260639 if (sym)
166 1851451 high = low + ((rng * cdf[sym - 1]) >> 14) - 1;
167 4260639 low += (rng * cdf[sym]) >> 14;
168
169 /* This loop could be done faster */
170 while (1) {
171
2/2
✓ Branch 0 taken 8681124 times.
✓ Branch 1 taken 2450936 times.
11132060 if (high < 32768) {
172 ;
173
2/2
✓ Branch 0 taken 2339607 times.
✓ Branch 1 taken 6341517 times.
8681124 } else if (low >= 32768) {
174 2339607 val -= 32768;
175 2339607 low -= 32768;
176 2339607 high -= 32768;
177
4/4
✓ Branch 0 taken 3744253 times.
✓ Branch 1 taken 2597264 times.
✓ Branch 2 taken 2080878 times.
✓ Branch 3 taken 1663375 times.
6341517 } else if (low >= 16384 && high < 49152) {
178 2080878 val -= 16384;
179 2080878 low -= 16384;
180 2080878 high -= 16384;
181 } else {
182 break;
183 }
184 6871421 low += low;
185 6871421 high += high + 1;
186 6871421 val = (val << 1) | get_bits1(gb);
187 };
188
189 4260639 ac->low = low;
190 4260639 ac->high = high;
191 4260639 ac->val = val;
192
193 4260639 return sym;
194 }
195
196 19988 void ff_aac_ac_finish(AACArithState *state, int offset, int N)
197 {
198 int i;
199
200
2/2
✓ Branch 0 taken 4425713 times.
✓ Branch 1 taken 19988 times.
4445701 for (i = offset; i < N/2; i++)
201 4425713 state->last[i] = 1;
202
203
2/2
✓ Branch 0 taken 2070036 times.
✓ Branch 1 taken 19988 times.
2090024 for (; i < FF_ARRAY_ELEMS(state->last); i++)
204 2070036 state->last[i] = 0;
205 19988 }
206