| 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] = FFMIN(a + b + 1, 0xF); | |
| 95 | 1874099 | state->cur[3] = state->cur[2]; | |
| 96 | 1874099 | state->cur[2] = state->cur[1]; | |
| 97 | 1874099 | state->cur[1] = state->cur[0]; | |
| 98 | |||
| 99 | 1874099 | state->last[idx] = state->cur[0]; | |
| 100 | 1874099 | } | |
| 101 | |||
| 102 | /* Initialize AC */ | ||
| 103 | 7768 | void ff_aac_ac_init(AACArith *ac, GetBitContext *gb) | |
| 104 | { | ||
| 105 | 7768 | ac->low = 0; | |
| 106 | 7768 | ac->high = UINT16_MAX; | |
| 107 | 7768 | ac->val = get_bits(gb, 16); | |
| 108 | 7768 | } | |
| 109 | |||
| 110 | 1967553 | uint16_t ff_aac_ac_decode(AACArith *ac, GetBitContext *gb, | |
| 111 | const uint16_t *cdf, uint16_t cdf_len) | ||
| 112 | { | ||
| 113 | 1967553 | int val = ac->val; | |
| 114 | 1967553 | int low = ac->low; | |
| 115 | 1967553 | int high = ac->high; | |
| 116 | |||
| 117 | int sym; | ||
| 118 | 1967553 | int rng = high - low + 1; | |
| 119 | 1967553 | int c = ((((int)(val - low + 1)) << 14) - ((int)1)); | |
| 120 | |||
| 121 | 1967553 | 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 38959 times.
✓ Branch 2 taken 1928594 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1967553 | switch (cdf_len) { |
| 125 | ✗ | case 2: | |
| 126 | ✗ | if ((p[1] * rng) > c) | |
| 127 | ✗ | p += 1; | |
| 128 | ✗ | break; | |
| 129 | 38959 | case 4: | |
| 130 |
2/2✓ Branch 0 taken 17073 times.
✓ Branch 1 taken 21886 times.
|
38959 | if ((p[2] * rng) > c) |
| 131 | 17073 | p += 2; | |
| 132 |
2/2✓ Branch 0 taken 17078 times.
✓ Branch 1 taken 21881 times.
|
38959 | if ((p[1] * rng) > c) |
| 133 | 17078 | p += 1; | |
| 134 | 38959 | break; | |
| 135 | 1928594 | case 17: | |
| 136 | /* First check if the current probability is even met at all */ | ||
| 137 |
2/2✓ Branch 0 taken 1097206 times.
✓ Branch 1 taken 831388 times.
|
1928594 | if ((p[1] * rng) <= c) |
| 138 | 1097206 | break; | |
| 139 | 831388 | p += 1; | |
| 140 |
2/2✓ Branch 0 taken 3325552 times.
✓ Branch 1 taken 831388 times.
|
4156940 | for (int i = 8; i >= 1; i >>= 1) |
| 141 |
2/2✓ Branch 0 taken 1076155 times.
✓ Branch 1 taken 2249397 times.
|
3325552 | if ((p[i] * rng) > c) |
| 142 | 1076155 | p += i; | |
| 143 | 831388 | 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 | 1967553 | default: | |
| 160 | /* This should never happen */ | ||
| 161 | av_assert2(0); | ||
| 162 | } | ||
| 163 | |||
| 164 | 1967553 | sym = (int)((ptrdiff_t)(p - cdf)) + 1; | |
| 165 |
2/2✓ Branch 0 taken 858337 times.
✓ Branch 1 taken 1109216 times.
|
1967553 | if (sym) |
| 166 | 858337 | high = low + ((rng * cdf[sym - 1]) >> 14) - 1; | |
| 167 | 1967553 | low += (rng * cdf[sym]) >> 14; | |
| 168 | |||
| 169 | /* This loop could be done faster */ | ||
| 170 | while (1) { | ||
| 171 |
2/2✓ Branch 0 taken 4206020 times.
✓ Branch 1 taken 1223737 times.
|
5429757 | if (high < 32768) { |
| 172 | ; | ||
| 173 |
2/2✓ Branch 0 taken 1168857 times.
✓ Branch 1 taken 3037163 times.
|
4206020 | } else if (low >= 32768) { |
| 174 | 1168857 | val -= 32768; | |
| 175 | 1168857 | low -= 32768; | |
| 176 | 1168857 | high -= 32768; | |
| 177 |
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) { |
| 178 | 1069610 | val -= 16384; | |
| 179 | 1069610 | low -= 16384; | |
| 180 | 1069610 | high -= 16384; | |
| 181 | } else { | ||
| 182 | break; | ||
| 183 | } | ||
| 184 | 3462204 | low += low; | |
| 185 | 3462204 | high += high + 1; | |
| 186 | 3462204 | val = (val << 1) | get_bits1(gb); | |
| 187 | }; | ||
| 188 | |||
| 189 | 1967553 | ac->low = low; | |
| 190 | 1967553 | ac->high = high; | |
| 191 | 1967553 | ac->val = val; | |
| 192 | |||
| 193 | 1967553 | return sym; | |
| 194 | } | ||
| 195 | |||
| 196 | 7768 | void ff_aac_ac_finish(AACArithState *state, int offset, int N) | |
| 197 | { | ||
| 198 | int i; | ||
| 199 | |||
| 200 |
2/2✓ Branch 0 taken 2103117 times.
✓ Branch 1 taken 7768 times.
|
2110885 | for (i = offset; i < N/2; i++) |
| 201 | 2103117 | state->last[i] = 1; | |
| 202 | |||
| 203 |
2/2✓ Branch 0 taken 7768 times.
✓ Branch 1 taken 7768 times.
|
15536 | for (; i < FF_ARRAY_ELEMS(state->last); i++) |
| 204 | 7768 | state->last[i] = 0; | |
| 205 | 7768 | } | |
| 206 |