/proc/self/cwd/external/faac/libfaad/tns.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 | | #include "sfb_tables.h" |
18 | | |
19 | | /* TNS_MAX_BANDS (ISO/IEC 14496-3 Table 4.139), indexed by sampling_frequency_index. */ |
20 | | static const uint8_t tns_max_bands_long[12] = { 31, 31, 34, 40, 42, 51, 46, 46, 42, 42, 42, 39 }; |
21 | | static const uint8_t tns_max_bands_short[12] = { 9, 9, 10, 14, 14, 14, 14, 14, 14, 14, 14, 14 }; |
22 | | |
23 | | static int tns_max_bands_for(int sr_idx, bool is_short) |
24 | 1.58k | { |
25 | 1.58k | if (sr_idx < 0 || sr_idx > 11) sr_idx = 4; |
26 | 1.58k | return is_short ? tns_max_bands_short[sr_idx] : tns_max_bands_long[sr_idx]; |
27 | 1.58k | } |
28 | | |
29 | | #ifndef M_PI |
30 | | #define M_PI 3.14159265358979323846 |
31 | | #endif |
32 | | |
33 | | static void tns_ar_filter(float * restrict spec, int length, int dir, const float * restrict lpc, int order) |
34 | 474 | { |
35 | 474 | if (length <= 0 || order <= 0) return; |
36 | | |
37 | 474 | if (!dir) { |
38 | 19.1k | for (int i = 0; i < length; i++) { |
39 | 18.8k | float sum = spec[i]; |
40 | 18.8k | int limit = (i < order) ? i : order; |
41 | 134k | for (int j = 1; j <= limit; j++) { |
42 | 116k | sum -= lpc[j - 1] * spec[i - j]; |
43 | 116k | } |
44 | 18.8k | spec[i] = sum; |
45 | 18.8k | } |
46 | 253 | } else { |
47 | 17.8k | for (int i = length - 1; i >= 0; i--) { |
48 | 17.6k | float sum = spec[i]; |
49 | 17.6k | int limit = (length - 1 - i < order) ? (length - 1 - i) : order; |
50 | 116k | for (int j = 1; j <= limit; j++) { |
51 | 99.0k | sum -= lpc[j - 1] * spec[i + j]; |
52 | 99.0k | } |
53 | 17.6k | spec[i] = sum; |
54 | 17.6k | } |
55 | 221 | } |
56 | 474 | } |
57 | | |
58 | | void apply_tns(ICSInfo *ics, float *spec) |
59 | 16.0k | { |
60 | 16.0k | #ifndef FAAD_DISABLE_TNS |
61 | 16.0k | if (!ics->tns_data_present) return; |
62 | | |
63 | 16.0k | bool is_short = (ics->window_sequence == EIGHT_SHORT_SEQUENCE); |
64 | 1.58k | int tns_max_bands = tns_max_bands_for(ics->sample_rate_index, is_short); |
65 | | |
66 | 1.58k | #ifndef FAAD_DISABLE_DAB |
67 | 1.58k | int short_len = (ics->frame_len_long == 960) ? 120 : 128; |
68 | | #else |
69 | | int short_len = 128; |
70 | | #endif |
71 | 6.99k | for (int w = 0; w < ics->num_windows; w++) { |
72 | 5.41k | float *window_spec = spec + w * short_len; |
73 | 5.41k | int limit = ics->max_sfb < tns_max_bands ? ics->max_sfb : tns_max_bands; |
74 | 5.41k | int bottom = ics->num_sfbs; |
75 | | |
76 | 7.79k | for (int f = 0; f < ics->tns_n_filt[w]; f++) { |
77 | 2.38k | int order = ics->tns_order[w][f]; |
78 | 2.38k | int length = ics->tns_length[w][f]; |
79 | 2.38k | int dir = ics->tns_direction[w][f]; |
80 | | |
81 | | /* Filter regions stack downwards from the top of the sfb table; |
82 | | * only afterwards is each clipped to max_sfb / TNS_MAX_BANDS. */ |
83 | 2.38k | int top = bottom; |
84 | 2.38k | bottom = (top > length) ? (top - length) : 0; |
85 | 2.38k | if (order == 0) continue; |
86 | | |
87 | 1.77k | int start_line = ics->sfb_offsets[bottom < limit ? bottom : limit]; |
88 | 1.77k | int end_line = ics->sfb_offsets[top < limit ? top : limit]; |
89 | 1.77k | int num_lines = end_line - start_line; |
90 | | |
91 | 1.77k | if (num_lines <= 0) continue; |
92 | | |
93 | | /* Reflection coefficients to LPC by Levinson-Durbin step-down. */ |
94 | 474 | float rc[TNS_MAX_ORDER]; |
95 | 474 | float lpc[TNS_MAX_ORDER]; |
96 | 474 | float lpc_tmp[TNS_MAX_ORDER]; |
97 | | /* ยง4.6.9.3: the quantizer is asymmetric, one more step on the |
98 | | * negative side: iqfac = (2^(bits-1) -/+ 0.5) / (pi/2). */ |
99 | 474 | static const float tns_rc_lut[2][16] = { |
100 | 474 | { -0.342020143f, -0.642787610f, -0.866025404f, -0.984807753f, -0.984807753f, -0.866025404f, -0.642787610f, -0.342020143f, 0.000000000f, 0.433883739f, 0.781831482f, 0.974927912f, 0.974927912f, 0.781831482f, 0.433883739f, 0.000000000f }, |
101 | 474 | { -0.995734176f, -0.961825643f, -0.895163291f, -0.798017227f, -0.673695644f, -0.526432163f, -0.361241666f, -0.183749518f, 0.000000000f, 0.207911691f, 0.406736643f, 0.587785252f, 0.743144825f, 0.866025404f, 0.951056516f, 0.994521895f }, |
102 | 474 | }; |
103 | | |
104 | 474 | int res_idx = ics->tns_coef_res[w] ? 1 : 0; |
105 | 3.20k | for (int i = 0; i < order; i++) { |
106 | 2.72k | int8_t val = ics->tns_coef[w][f][i]; |
107 | 2.72k | rc[i] = tns_rc_lut[res_idx][(val >= -8 && val <= 7) ? (val + 8) : 8]; |
108 | 2.72k | } |
109 | | |
110 | 3.20k | for (int m = 0; m < order; m++) { |
111 | 2.72k | lpc[m] = rc[m]; |
112 | 10.4k | for (int i = 0; i < m; i++) { |
113 | 7.71k | lpc_tmp[i] = lpc[i] + rc[m] * lpc[m - 1 - i]; |
114 | 7.71k | } |
115 | 10.4k | for (int i = 0; i < m; i++) { |
116 | 7.71k | lpc[i] = lpc_tmp[i]; |
117 | 7.71k | } |
118 | 2.72k | } |
119 | | |
120 | 474 | tns_ar_filter(window_spec + start_line, num_lines, dir, lpc, order); |
121 | 474 | } |
122 | 5.41k | } |
123 | | #else |
124 | | (void)ics; (void)spec; |
125 | | #endif |
126 | 1.58k | } |