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 | 7768 | uint32_t ff_aac_ac_map_process(AACArithState *state, int reset, int N) | |
26 | { | ||
27 | float ratio; | ||
28 |
2/2✓ Branch 0 taken 467 times.
✓ Branch 1 taken 7301 times.
|
7768 | if (reset) { |
29 | 467 | memset(state->last, 0, sizeof(state->last)); | |
30 | 467 | state->last_len = N; | |
31 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7301 times.
|
7301 | } else if (state->last_len != N) { |
32 | int i; | ||
33 | uint8_t last[512 /* 2048 / 4 */]; | ||
34 | ✗ | memcpy(last, state->last, sizeof(last)); | |
35 | |||
36 | ✗ | ratio = state->last_len / (float)N; | |
37 | ✗ | for (i = 0; i < N/2; i++) { | |
38 | ✗ | int k = (int)(i * ratio); | |
39 | ✗ | state->last[i] = last[k]; | |
40 | } | ||
41 | |||
42 | ✗ | for (; i < FF_ARRAY_ELEMS(state->last); i++) | |
43 | ✗ | state->last[i] = 0; | |
44 | |||
45 | ✗ | state->last_len = N; | |
46 | } | ||
47 | |||
48 | 7768 | state->cur[3] = 0; | |
49 | 7768 | state->cur[2] = 0; | |
50 | 7768 | state->cur[1] = 0; | |
51 | 7768 | state->cur[0] = 1; | |
52 | |||
53 | 7768 | state->state_pre = state->last[0] << 12; | |
54 | 7768 | return state->last[0] << 12; | |
55 | } | ||
56 | |||
57 | 1881867 | uint32_t ff_aac_ac_get_context(AACArithState *state, uint32_t c, int i, int N) | |
58 | { | ||
59 | 1881867 | c = state->state_pre >> 8; | |
60 | 1881867 | c = c + (state->last[i + 1] << 8); | |
61 | 1881867 | c = (c << 4); | |
62 | 1881867 | c += state->cur[1]; | |
63 | |||
64 | 1881867 | state->state_pre = c; | |
65 | |||
66 |
2/2✓ Branch 0 taken 1850843 times.
✓ Branch 1 taken 31024 times.
|
1881867 | if (i > 3 && |
67 |
2/2✓ Branch 0 taken 1065119 times.
✓ Branch 1 taken 785724 times.
|
1850843 | ((state->cur[3] + state->cur[2] + state->cur[1]) < 5)) |
68 | 1065119 | return c + 0x10000; | |
69 | |||
70 | 816748 | return c; | |
71 | } | ||
72 | |||
73 | 1928594 | uint32_t ff_aac_ac_get_pk(uint32_t c) | |
74 | { | ||
75 | 1928594 | int i_min = -1; | |
76 | int i, j; | ||
77 | 1928594 | int i_max = FF_ARRAY_ELEMS(ff_aac_ac_lookup_m) - 1; | |
78 |
2/2✓ Branch 0 taken 17565347 times.
✓ Branch 1 taken 730824 times.
|
18296171 | while ((i_max - i_min) > 1) { |
79 | 17565347 | i = i_min + ((i_max - i_min) / 2); | |
80 | 17565347 | j = ff_aac_ac_hash_m[i]; | |
81 |
2/2✓ Branch 0 taken 8903763 times.
✓ Branch 1 taken 8661584 times.
|
17565347 | if (c < (j >> 8)) |
82 | 8903763 | i_max = i; | |
83 |
2/2✓ Branch 0 taken 7463814 times.
✓ Branch 1 taken 1197770 times.
|
8661584 | else if (c > (j >> 8)) |
84 | 7463814 | i_min = i; | |
85 | else | ||
86 | 1197770 | return (j & 0xFF); | |
87 | } | ||
88 | 730824 | return ff_aac_ac_lookup_m[i_max]; | |
89 | } | ||
90 | |||
91 | 1874099 | void ff_aac_ac_update_context(AACArithState *state, int idx, | |
92 | uint16_t a, uint16_t b) | ||
93 | { | ||
94 | 1874099 | state->cur[0] = a + b + 1; | |
95 |
2/2✓ Branch 0 taken 2751 times.
✓ Branch 1 taken 1871348 times.
|
1874099 | if (state->cur[0] > 0xF) |
96 | 2751 | state->cur[0] = 0xF; | |
97 | |||
98 | 1874099 | state->cur[3] = state->cur[2]; | |
99 | 1874099 | state->cur[2] = state->cur[1]; | |
100 | 1874099 | state->cur[1] = state->cur[0]; | |
101 | |||
102 | 1874099 | state->last[idx] = state->cur[0]; | |
103 | 1874099 | } | |
104 | |||
105 | /* Initialize AC */ | ||
106 | 7768 | void ff_aac_ac_init(AACArith *ac, GetBitContext *gb) | |
107 | { | ||
108 | 7768 | ac->low = 0; | |
109 | 7768 | ac->high = UINT16_MAX; | |
110 | 7768 | ac->val = get_bits(gb, 16); | |
111 | 7768 | } | |
112 | |||
113 | 1967553 | uint16_t ff_aac_ac_decode(AACArith *ac, GetBitContext *gb, | |
114 | const uint16_t *cdf, uint16_t cdf_len) | ||
115 | { | ||
116 | 1967553 | int val = ac->val; | |
117 | 1967553 | int low = ac->low; | |
118 | 1967553 | int high = ac->high; | |
119 | |||
120 | int sym; | ||
121 | 1967553 | int rng = high - low + 1; | |
122 | 1967553 | int c = ((((int)(val - low + 1)) << 14) - ((int)1)); | |
123 | |||
124 | 1967553 | const uint16_t *p = cdf - 1; | |
125 | |||
126 | /* One for each possible CDF length in the spec */ | ||
127 |
2/5✗ Branch 0 not taken.
✓ Branch 1 taken 38959 times.
✓ Branch 2 taken 1928594 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1967553 | switch (cdf_len) { |
128 | ✗ | case 2: | |
129 | ✗ | if ((p[1] * rng) > c) | |
130 | ✗ | p += 1; | |
131 | ✗ | break; | |
132 | 38959 | case 4: | |
133 |
2/2✓ Branch 0 taken 17073 times.
✓ Branch 1 taken 21886 times.
|
38959 | if ((p[2] * rng) > c) |
134 | 17073 | p += 2; | |
135 |
2/2✓ Branch 0 taken 17078 times.
✓ Branch 1 taken 21881 times.
|
38959 | if ((p[1] * rng) > c) |
136 | 17078 | p += 1; | |
137 | 38959 | break; | |
138 | 1928594 | case 17: | |
139 | /* First check if the current probability is even met at all */ | ||
140 |
2/2✓ Branch 0 taken 1097206 times.
✓ Branch 1 taken 831388 times.
|
1928594 | if ((p[1] * rng) <= c) |
141 | 1097206 | break; | |
142 | 831388 | p += 1; | |
143 |
2/2✓ Branch 0 taken 3325552 times.
✓ Branch 1 taken 831388 times.
|
4156940 | for (int i = 8; i >= 1; i >>= 1) |
144 |
2/2✓ Branch 0 taken 1076155 times.
✓ Branch 1 taken 2249397 times.
|
3325552 | if ((p[i] * rng) > c) |
145 | 1076155 | p += i; | |
146 | 831388 | break; | |
147 | ✗ | case 27: | |
148 | ✗ | if ((p[16] * rng) > c) | |
149 | ✗ | p += 16; | |
150 | ✗ | if ((p[8] * rng) > c) | |
151 | ✗ | p += 8; | |
152 | ✗ | if (p != (cdf - 1 + 24)) | |
153 | ✗ | if ((p[4] * rng) > c) | |
154 | ✗ | p += 4; | |
155 | ✗ | if ((p[2] * rng) > c) | |
156 | ✗ | p += 2; | |
157 | |||
158 | ✗ | if (p != (cdf - 1 + 24 + 2)) | |
159 | ✗ | if ((p[1] * rng) > c) | |
160 | ✗ | p += 1; | |
161 | ✗ | break; | |
162 | 1967553 | default: | |
163 | /* This should never happen */ | ||
164 | av_assert2(0); | ||
165 | } | ||
166 | |||
167 | 1967553 | sym = (int)((ptrdiff_t)(p - cdf)) + 1; | |
168 |
2/2✓ Branch 0 taken 858337 times.
✓ Branch 1 taken 1109216 times.
|
1967553 | if (sym) |
169 | 858337 | high = low + ((rng * cdf[sym - 1]) >> 14) - 1; | |
170 | 1967553 | low += (rng * cdf[sym]) >> 14; | |
171 | |||
172 | /* This loop could be done faster */ | ||
173 | while (1) { | ||
174 |
2/2✓ Branch 0 taken 4206020 times.
✓ Branch 1 taken 1223737 times.
|
5429757 | if (high < 32768) { |
175 | ; | ||
176 |
2/2✓ Branch 0 taken 1168857 times.
✓ Branch 1 taken 3037163 times.
|
4206020 | } else if (low >= 32768) { |
177 | 1168857 | val -= 32768; | |
178 | 1168857 | low -= 32768; | |
179 | 1168857 | high -= 32768; | |
180 |
4/4✓ Branch 0 taken 1811450 times.
✓ Branch 1 taken 1225713 times.
✓ Branch 2 taken 1069610 times.
✓ Branch 3 taken 741840 times.
|
3037163 | } else if (low >= 16384 && high < 49152) { |
181 | 1069610 | val -= 16384; | |
182 | 1069610 | low -= 16384; | |
183 | 1069610 | high -= 16384; | |
184 | } else { | ||
185 | break; | ||
186 | } | ||
187 | 3462204 | low += low; | |
188 | 3462204 | high += high + 1; | |
189 | 3462204 | val = (val << 1) | get_bits1(gb); | |
190 | }; | ||
191 | |||
192 | 1967553 | ac->low = low; | |
193 | 1967553 | ac->high = high; | |
194 | 1967553 | ac->val = val; | |
195 | |||
196 | 1967553 | return sym; | |
197 | } | ||
198 | |||
199 | 7768 | void ff_aac_ac_finish(AACArithState *state, int offset, int N) | |
200 | { | ||
201 | int i; | ||
202 | |||
203 |
2/2✓ Branch 0 taken 2103117 times.
✓ Branch 1 taken 7768 times.
|
2110885 | for (i = offset; i < N/2; i++) |
204 | 2103117 | state->last[i] = 1; | |
205 | |||
206 |
2/2✓ Branch 0 taken 7768 times.
✓ Branch 1 taken 7768 times.
|
15536 | for (; i < FF_ARRAY_ELEMS(state->last); i++) |
207 | 7768 | state->last[i] = 0; | |
208 | 7768 | } | |
209 |