/proc/self/cwd/external/faac/libfaad/dequant.c
Line | Count | Source |
1 | | /* |
2 | | * FAAD - Freeware Advanced Audio Decoder |
3 | | * Copyright (C) 2026 Nils Schimmelmann |
4 | | * |
5 | | * This library is free software; you can redistribute it and/or |
6 | | * modify it under the terms of the GNU Lesser General Public |
7 | | * License as published by the Free Software Foundation; either |
8 | | * version 2.1 of the License, or (at your option) any later version. |
9 | | * |
10 | | * This library is distributed in the hope that it will be useful, |
11 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | | * Lesser General Public License for more details. |
14 | | */ |
15 | | |
16 | | #include "faad_internal.h" |
17 | | |
18 | | void apply_pns(ICSInfo *ics, float *spec, uint32_t *pns_seed) |
19 | 16.1k | { |
20 | 16.1k | int window_offset = 0; |
21 | 16.1k | uint32_t seed = *pns_seed; |
22 | | |
23 | 66.1k | for (int g = 0; g < ics->num_window_groups && g < 8; g++) { |
24 | 50.0k | int max_sfb = ics->max_sfb < ics->num_sfbs ? ics->max_sfb : ics->num_sfbs; |
25 | 50.0k | if (max_sfb > MAX_SFB) max_sfb = MAX_SFB; |
26 | 151k | for (int sfb = 0; sfb < max_sfb; sfb++) { |
27 | 100k | if (ics->sfb_cb[g][sfb] == 13) { |
28 | | /* ยง4.6.13.3: the band's summed energy is 2^(noise_nrg/2), with |
29 | | * no SF_OFFSET -- noise_nrg is not a scalefactor. */ |
30 | 10.0k | int nrg = ics->scalefactors[g][sfb]; |
31 | 10.0k | if (nrg < -120) nrg = -120; |
32 | 10.0k | if (nrg > 120) nrg = 120; |
33 | 10.0k | float scale = powf(2.0f, 0.25f * (float)nrg); |
34 | | |
35 | 10.0k | #ifndef FAAD_DISABLE_DAB |
36 | 10.0k | int short_len = (ics->frame_len_long == 960) ? 120 : 128; |
37 | 10.0k | int long_len = (ics->frame_len_long == 960) ? 960 : 1024; |
38 | | #else |
39 | | int short_len = 128; |
40 | | int long_len = 1024; |
41 | | #endif |
42 | 10.0k | int start_k = ics->sfb_offsets[sfb]; |
43 | 10.0k | int end_k = ics->sfb_offsets[sfb + 1]; |
44 | 10.0k | if (start_k >= long_len) continue; |
45 | 10.0k | if (end_k > long_len) end_k = long_len; |
46 | 10.0k | int len = end_k - start_k; |
47 | | |
48 | 24.3k | for (int w = 0; w < ics->window_group_length[g]; w++) { |
49 | 14.3k | int win_offset_k = (window_offset + w) * short_len + start_k; |
50 | 14.3k | if (win_offset_k < 0 || win_offset_k + len > long_len) continue; |
51 | 14.3k | float * restrict ptr = spec + win_offset_k; |
52 | 14.3k | float energy = 0.0f; |
53 | | |
54 | 183k | for (int k = 0; k < len; k++) { |
55 | 169k | seed = (seed * 1664525U) + 1013904223U; |
56 | 169k | float noise = ((float)(int32_t)seed) * (1.0f / 2147483648.0f); |
57 | 169k | ptr[k] = noise; |
58 | 169k | energy += noise * noise; |
59 | 169k | } |
60 | | |
61 | 14.3k | if (energy > 0.0f) { |
62 | 14.3k | float norm = scale / sqrtf(energy); |
63 | 183k | for (int k = 0; k < len; k++) { |
64 | 169k | ptr[k] *= norm; |
65 | 169k | } |
66 | 14.3k | } |
67 | 14.3k | } |
68 | 10.0k | } |
69 | 100k | } |
70 | 50.0k | window_offset += ics->window_group_length[g]; |
71 | 50.0k | } |
72 | 16.1k | *pns_seed = seed; |
73 | 16.1k | } |