/src/faac/libfaac/sbr_analysis.c
Line | Count | Source |
1 | | /* |
2 | | * FAAC - Freeware Advanced Audio Coder |
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 "sbr.h" |
17 | | #include "sbr_analysis.h" |
18 | | #include "sbr_internal.h" |
19 | | #include "util.h" |
20 | | #include <string.h> |
21 | | |
22 | | /* Envelope border in QMF slots, for binning the per-slot energies. The first |
23 | | * envelope also takes slots before tEnv[0] and the last the ones after its |
24 | | * end, so no slot's energy is dropped. Computed on demand: an array of them |
25 | | * is vectorized into more code than the whole analysis it serves. */ |
26 | | static inline int env_border(const SignalAnalysis *sa, int e, int num_slots) |
27 | 94.3k | { |
28 | 94.3k | if (e == 0) return 0; |
29 | 74.1k | if (e == sa->numEnvelopes) return num_slots; |
30 | 54.0k | return sa->tEnv[e] * num_slots / SBR_NUM_TIME_SLOTS; |
31 | 74.1k | } |
32 | | |
33 | | /* Multi-pass signal analysis: transient detection, temporal grid selection, |
34 | | * and subband energy accumulation. */ |
35 | | void SbrAnalyze(SignalAnalysis *sa, float *fullPtrs[], int nch, const bool *isLfe, int numSamples, struct SBRInfo *sbr) |
36 | 3.82k | { |
37 | | #if MAX_CHANNELS < 6 |
38 | | (void)isLfe; |
39 | | #endif |
40 | 3.82k | int num_slots = numSamples / SBR_QMF_BANDS_64; |
41 | 3.82k | float *workspace = sbr->workspace[0]; |
42 | | |
43 | 3.82k | float frameStrength = 0.0f; |
44 | | |
45 | | /* The strongest non-LFE transient selects the shared envelope grid. */ |
46 | 21.9k | for (int ch = 0; ch < nch; ch++) { |
47 | 18.1k | #if MAX_CHANNELS >= 6 |
48 | 18.1k | if (isLfe[ch]) continue; |
49 | 16.3k | #endif |
50 | 16.3k | float smax = 0.0f, ssum = 0.0f; |
51 | 539k | for (int slot = 0; slot < num_slots; slot++) { |
52 | | /* The analyzed frame starts SBR_ANALYSIS_DELAY back, in the saved history. */ |
53 | 522k | int pos = slot * SBR_QMF_BANDS_64 - SBR_ANALYSIS_DELAY; |
54 | 522k | const float * restrict p_in = (pos < 0) ? sbr->ch[ch].qmfOvl64 + SBR_QMF_HIST_LEN + pos |
55 | 522k | : fullPtrs[ch] + pos; |
56 | 522k | float stot = 0.0f; |
57 | 8.88M | for (int n = 0; n < SBR_QMF_BANDS_64; n += 4) { |
58 | 8.36M | float v0 = p_in[0], v1 = p_in[1], v2 = p_in[2], v3 = p_in[3]; |
59 | 8.36M | stot += v0 * v0 + v1 * v1 + v2 * v2 + v3 * v3; |
60 | 8.36M | p_in += 4; |
61 | 8.36M | } |
62 | | |
63 | 522k | if (stot > smax) { |
64 | 13.1k | smax = stot; |
65 | 13.1k | } |
66 | 522k | ssum += stot; |
67 | 522k | } |
68 | | |
69 | 16.3k | float strength = smax * (float)num_slots / (ssum + SBR_ENERGY_FLOOR); |
70 | 16.3k | if (strength > frameStrength) { |
71 | 2.77k | frameStrength = strength; |
72 | 2.77k | } |
73 | 16.3k | } |
74 | | |
75 | 3.82k | if (frameStrength > (sbr->rich ? SBR_TRANSIENT_THRESH_RICH : SBR_TRANSIENT_THRESH_DEFAULT)) { |
76 | 1.59k | const int innerSbr = SBR_NUM_TIME_SLOTS / 2; |
77 | 1.59k | sa->numEnvelopes = 2; |
78 | 1.59k | sa->frameClass = SBR_FRAME_CLASS_VARFIX; |
79 | 1.59k | sa->tEnv[0] = 0; |
80 | 1.59k | sa->tEnv[1] = innerSbr; |
81 | 1.59k | sa->tEnv[2] = SBR_NUM_TIME_SLOTS; |
82 | 2.22k | } else { |
83 | 2.22k | int ne = sbr->numEnvFixFix; |
84 | 2.22k | sa->numEnvelopes = ne; |
85 | 2.22k | sa->frameClass = SBR_FRAME_CLASS_FIXFIX; |
86 | 10.9k | for (int e = 0; e <= ne; e++) |
87 | 8.73k | sa->tEnv[e] = e * SBR_NUM_TIME_SLOTS / ne; |
88 | 2.22k | } |
89 | | |
90 | | /* Slots analyzed per envelope, for power normalization: the multiples of |
91 | | * the decimation factor below each border. */ |
92 | 13.5k | for (int e = 0; e < sa->numEnvelopes; e++) { |
93 | 9.70k | int n = (env_border(sa, e + 1, num_slots) + FAAC_SBR_DECIMATION - 1) / FAAC_SBR_DECIMATION |
94 | 9.70k | - (env_border(sa, e, num_slots) + FAAC_SBR_DECIMATION - 1) / FAAC_SBR_DECIMATION; |
95 | 9.70k | sa->envSampled[e] = n < 1 ? 1 : n; |
96 | 9.70k | } |
97 | | |
98 | 3.82k | if (sbr) { |
99 | | #if FAAC_ENCODER_PS |
100 | | if (sbr->is_he_v2) { |
101 | | float *right = sbr->workspace[1]; |
102 | | memcpy(workspace, sbr->ch[0].qmfOvl64, SBR_QMF_HIST_LEN * sizeof(float)); |
103 | | memcpy(workspace + SBR_QMF_HIST_LEN, fullPtrs[0], numSamples * sizeof(float)); |
104 | | memcpy(right, sbr->ch[1].qmfOvl64, SBR_QMF_HIST_LEN * sizeof(float)); |
105 | | memcpy(right + SBR_QMF_HIST_LEN, fullPtrs[1], numSamples * sizeof(float)); |
106 | | memset(sa->ch[0].bandE, 0, sizeof(sa->ch[0].bandE)); |
107 | | memset(sa->psE, 0, sizeof(sa->psE)); |
108 | | memset(sa->psCross, 0, sizeof(sa->psCross)); |
109 | | memset(sa->psCrossIm, 0, sizeof(sa->psCrossIm)); |
110 | | float (*low)[3][44][2] = sa->psLow; |
111 | | for (int ch = 0; ch < 2; ch++) |
112 | | for (int k = 0; k < 3; k++) |
113 | | memcpy(low[ch][k], sbr->psHybrid.history[ch][k], sizeof(sbr->psHybrid.history[ch][k])); |
114 | | for (int slot = 0; slot < num_slots + 6; slot++) { |
115 | | float lr[64], li[64], rr[64], ri[64]; |
116 | | int sampled = slot < num_slots && slot % FAAC_SBR_DECIMATION == 0; |
117 | | SbrQmfAnalysisComplex(sbr, workspace + slot * 64, lr, li, 0, sampled ? 64 : 3); |
118 | | SbrQmfAnalysisComplex(sbr, right + slot * 64, rr, ri, 0, sampled ? 64 : 3); |
119 | | for (int k = 0; k < 3; k++) { |
120 | | low[0][k][slot+6][0]=lr[k]; low[0][k][slot+6][1]=li[k]; |
121 | | low[1][k][slot+6][0]=rr[k]; low[1][k][slot+6][1]=ri[k]; |
122 | | } |
123 | | if (!sampled) continue; |
124 | | int e = 0; |
125 | | while (e + 1 < sa->numEnvelopes && |
126 | | slot >= env_border(sa, e + 1, num_slots)) e++; |
127 | | int p = slot * 4 / num_slots; |
128 | | static const unsigned char edges[13] = {3,4,5,6,7,8,9,11,14,18,23,35,64}; |
129 | | int band = 8; |
130 | | for (int k = 0; k < 64; k++) { |
131 | | /* The mono carrier preserves mean stereo energy. */ |
132 | | sa->ch[0].bandE[e][k] += |
133 | | 0.5f * (lr[k]*lr[k]+li[k]*li[k]+rr[k]*rr[k]+ri[k]*ri[k]); |
134 | | if (k < 3) continue; |
135 | | /* Band edges are monotonic in k. */ |
136 | | while (k >= edges[band-7]) band++; |
137 | | sa->psE[0][p][band] += lr[k]*lr[k]+li[k]*li[k]; |
138 | | sa->psE[1][p][band] += rr[k]*rr[k]+ri[k]*ri[k]; |
139 | | sa->psCross[p][band] += lr[k]*rr[k]+li[k]*ri[k]; |
140 | | sa->psCrossIm[p][band] += li[k]*rr[k]-lr[k]*ri[k]; |
141 | | } |
142 | | } |
143 | | PsHybridAnalyze(&sbr->psHybrid, low, sa->psE, sa->psCross, sa->psCrossIm, num_slots); |
144 | | for (int ch = 0; ch < 2; ch++) |
145 | | for (int k = 0; k < 3; k++) |
146 | | memcpy(sbr->psHybrid.history[ch][k], low[ch][k] + num_slots, |
147 | | sizeof(sbr->psHybrid.history[ch][k])); |
148 | | return; |
149 | | } |
150 | | #endif |
151 | | /* Pass 2: subband analysis, accumulating QMF band energy per envelope. |
152 | | * Only [kx, k2) feeds the quantizer, so skip bands below kx. */ |
153 | 3.82k | int kx = sbr->kx; |
154 | 3.82k | int kEnd = sbr->k2; |
155 | 21.9k | for (int ch = 0; ch < nch; ch++) { |
156 | 18.1k | #if MAX_CHANNELS >= 6 |
157 | 18.1k | if (isLfe[ch]) continue; |
158 | 16.3k | #endif |
159 | 16.3k | memset(sa->ch[ch].bandE, 0, (size_t)sa->numEnvelopes * sizeof(sa->ch[ch].bandE[0])); |
160 | | |
161 | 16.3k | memcpy(workspace, sbr->ch[ch].qmfOvl64, SBR_QMF_HIST_LEN * sizeof(float)); |
162 | 16.3k | memcpy(workspace + SBR_QMF_HIST_LEN, fullPtrs[ch], numSamples * sizeof(float)); |
163 | | |
164 | 53.8k | for (int e = 0; e < sa->numEnvelopes; e++) { |
165 | 37.4k | int end = env_border(sa, e + 1, num_slots); |
166 | 560k | for (int slot = env_border(sa, e, num_slots); slot < end; slot++) { |
167 | | #if FAAC_SBR_DECIMATION > 1 |
168 | | if (slot % FAAC_SBR_DECIMATION != 0) continue; |
169 | | #endif |
170 | 522k | SbrQmfAnalysis(sbr, workspace + slot * SBR_QMF_BANDS_64, sa->ch[ch].bandE[e], kx, kEnd); |
171 | 522k | } |
172 | 37.4k | } |
173 | 16.3k | } |
174 | 3.82k | } |
175 | 3.82k | } |