| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * AAC encoder TNS | ||
| 3 | * Copyright (C) 2015 Rostislav Pehlivanov | ||
| 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 | /** | ||
| 23 | * @file | ||
| 24 | * AAC encoder temporal noise shaping | ||
| 25 | * @author Rostislav Pehlivanov ( atomnuker gmail com ) | ||
| 26 | */ | ||
| 27 | |||
| 28 | #include "libavutil/libm.h" | ||
| 29 | #include "aacenc.h" | ||
| 30 | #include <float.h> | ||
| 31 | #include "aacenc_tns.h" | ||
| 32 | #include "aactab.h" | ||
| 33 | #include "aacenc_utils.h" | ||
| 34 | #include "lpc_functions.h" | ||
| 35 | |||
| 36 | /* Could be set to 3 to save an additional bit at the cost of little quality */ | ||
| 37 | #define TNS_Q_BITS 4 | ||
| 38 | |||
| 39 | /* Coefficient resolution in short windows */ | ||
| 40 | #define TNS_Q_BITS_IS8 4 | ||
| 41 | |||
| 42 | /* We really need the bits we save here elsewhere */ | ||
| 43 | #define TNS_ENABLE_COEF_COMPRESSION | ||
| 44 | |||
| 45 | /* Apple-derived TNS: weighted-spectrum predictor, accepted only if the measured | ||
| 46 | * post-quantization prediction gain clears a block-type-dependent bar (Apple RE). */ | ||
| 47 | #define TNS_PREDGAIN_GATE 1.4f /* first gate: predicted LPC gain */ | ||
| 48 | #define TNS_PG_C1_LONG 1.4f /* min measured gain, long blocks */ | ||
| 49 | #define TNS_PG_C1_SHORT 3.2f /* min measured gain, short blocks */ | ||
| 50 | #define TNS_PG_CLAMP 6.0f /* upper bound: poles near unit circle → noise blowup */ | ||
| 51 | #define TNS_WEIGHT_FLOOR 0.01f /* per-bin masking floor for the weighted spectrum */ | ||
| 52 | |||
| 53 | 112 | static inline int compress_coeffs(int *coef, int order, int c_bits) | |
| 54 | { | ||
| 55 | int i; | ||
| 56 |
1/2✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
|
112 | const int low_idx = c_bits ? 4 : 2; |
| 57 |
1/2✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
|
112 | const int shift_val = c_bits ? 8 : 4; |
| 58 |
1/2✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
|
112 | const int high_idx = c_bits ? 11 : 5; |
| 59 | #ifndef TNS_ENABLE_COEF_COMPRESSION | ||
| 60 | return 0; | ||
| 61 | #endif /* TNS_ENABLE_COEF_COMPRESSION */ | ||
| 62 |
2/2✓ Branch 0 taken 563 times.
✓ Branch 1 taken 87 times.
|
650 | for (i = 0; i < order; i++) |
| 63 |
4/4✓ Branch 0 taken 184 times.
✓ Branch 1 taken 379 times.
✓ Branch 2 taken 25 times.
✓ Branch 3 taken 159 times.
|
563 | if (coef[i] >= low_idx && coef[i] <= high_idx) |
| 64 | 25 | return 0; | |
| 65 |
2/2✓ Branch 0 taken 522 times.
✓ Branch 1 taken 87 times.
|
609 | for (i = 0; i < order; i++) |
| 66 |
2/2✓ Branch 0 taken 147 times.
✓ Branch 1 taken 375 times.
|
522 | coef[i] -= (coef[i] > high_idx) ? shift_val : 0; |
| 67 | 87 | return 1; | |
| 68 | } | ||
| 69 | |||
| 70 | /** Encode TNS data. */ | ||
| 71 | 9458 | void ff_aac_encode_tns_info(AACEncContext *s, SingleChannelElement *sce) | |
| 72 | { | ||
| 73 | 9458 | TemporalNoiseShaping *tns = &sce->tns; | |
| 74 | 9458 | int i, w, filt, coef_compress = 0, coef_len; | |
| 75 | 9458 | const int is8 = sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE; | |
| 76 | 9458 | const int c_bits = is8 ? TNS_Q_BITS_IS8 == 4 : TNS_Q_BITS == 4; | |
| 77 | |||
| 78 |
2/2✓ Branch 0 taken 9380 times.
✓ Branch 1 taken 78 times.
|
9458 | if (!sce->tns.present) |
| 79 | 9380 | return; | |
| 80 | |||
| 81 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 78 times.
|
156 | for (i = 0; i < sce->ics.num_windows; i++) { |
| 82 | 78 | put_bits(&s->pb, 2 - is8, sce->tns.n_filt[i]); | |
| 83 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
|
78 | if (!tns->n_filt[i]) |
| 84 | ✗ | continue; | |
| 85 | 78 | put_bits(&s->pb, 1, c_bits); | |
| 86 |
2/2✓ Branch 0 taken 156 times.
✓ Branch 1 taken 78 times.
|
234 | for (filt = 0; filt < tns->n_filt[i]; filt++) { |
| 87 | 156 | put_bits(&s->pb, 6 - 2 * is8, tns->length[i][filt]); | |
| 88 | 156 | put_bits(&s->pb, 5 - 2 * is8, tns->order[i][filt]); | |
| 89 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 112 times.
|
156 | if (!tns->order[i][filt]) |
| 90 | 44 | continue; | |
| 91 | 112 | put_bits(&s->pb, 1, tns->direction[i][filt]); | |
| 92 | 112 | coef_compress = compress_coeffs(tns->coef_idx[i][filt], | |
| 93 | tns->order[i][filt], c_bits); | ||
| 94 | 112 | put_bits(&s->pb, 1, coef_compress); | |
| 95 | 112 | coef_len = c_bits + 3 - coef_compress; | |
| 96 |
2/2✓ Branch 0 taken 672 times.
✓ Branch 1 taken 112 times.
|
784 | for (w = 0; w < tns->order[i][filt]; w++) |
| 97 | 672 | put_bits(&s->pb, coef_len, tns->coef_idx[i][filt][w]); | |
| 98 | } | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | /* Cap the TNS band range at the first PNS band to avoid TNS+PNS conflicts. */ | ||
| 103 | 4255 | static int tns_max_nonpns(const SingleChannelElement *sce, int mmm) | |
| 104 | { | ||
| 105 |
2/2✓ Branch 0 taken 4633 times.
✓ Branch 1 taken 4255 times.
|
8888 | for (int w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) |
| 106 |
2/2✓ Branch 0 taken 166496 times.
✓ Branch 1 taken 4633 times.
|
171129 | for (int g = 0; g < mmm; g++) |
| 107 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 166496 times.
|
166496 | if (sce->band_type[w*16+g] == NOISE_BT) { mmm = g; break; } |
| 108 | 4255 | return mmm; | |
| 109 | } | ||
| 110 | |||
| 111 | /* Apply TNS filter */ | ||
| 112 | 2106 | void ff_aac_apply_tns(AACEncContext *s, SingleChannelElement *sce) | |
| 113 | { | ||
| 114 | 2106 | TemporalNoiseShaping *tns = &sce->tns; | |
| 115 | 2106 | IndividualChannelStream *ics = &sce->ics; | |
| 116 | int w, filt, m, i, top, order, bottom, start, end, size, inc; | ||
| 117 | 2106 | const int mmm = tns_max_nonpns(sce, FFMIN(ics->tns_max_bands, ics->max_sfb)); | |
| 118 | float lpc[TNS_MAX_ORDER]; | ||
| 119 | |||
| 120 | /* TNS predicts from the post-M/S and post-I/S coefficients. */ | ||
| 121 | float hist[1024]; | ||
| 122 | 2106 | memcpy(hist, sce->coeffs, sizeof(hist)); | |
| 123 | |||
| 124 |
2/2✓ Branch 0 taken 2421 times.
✓ Branch 1 taken 2106 times.
|
4527 | for (w = 0; w < ics->num_windows; w++) { |
| 125 | 2421 | bottom = ics->num_swb; | |
| 126 |
2/2✓ Branch 0 taken 156 times.
✓ Branch 1 taken 2421 times.
|
2577 | for (filt = 0; filt < tns->n_filt[w]; filt++) { |
| 127 | int b0, e0; | ||
| 128 | 156 | top = bottom; | |
| 129 | 156 | bottom = FFMAX(0, top - tns->length[w][filt]); | |
| 130 | 156 | order = tns->order[w][filt]; | |
| 131 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 112 times.
|
156 | if (order == 0) |
| 132 | 44 | continue; | |
| 133 | |||
| 134 | // tns_decode_coef | ||
| 135 | 112 | compute_lpc_coefs(tns->coef[w][filt], 0, order, lpc, 0, 0, 0, NULL); | |
| 136 | |||
| 137 | 112 | b0 = FFMIN(bottom, mmm); | |
| 138 | 112 | e0 = FFMIN( top, mmm); | |
| 139 | 112 | start = ics->swb_offset[b0]; | |
| 140 | 112 | end = ics->swb_offset[e0]; | |
| 141 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
|
112 | if ((size = end - start) <= 0) |
| 142 | ✗ | continue; | |
| 143 |
2/2✓ Branch 0 taken 35 times.
✓ Branch 1 taken 77 times.
|
112 | if (tns->direction[w][filt]) { |
| 144 | 35 | inc = -1; | |
| 145 | 35 | start = end - 1; | |
| 146 | } else { | ||
| 147 | 77 | inc = 1; | |
| 148 | } | ||
| 149 | 112 | start += w * 128; | |
| 150 | |||
| 151 | /* AR filter */ | ||
| 152 |
2/2✓ Branch 0 taken 28756 times.
✓ Branch 1 taken 112 times.
|
28868 | for (m = 0; m < size; m++, start += inc) { |
| 153 |
2/2✓ Branch 0 taken 170184 times.
✓ Branch 1 taken 28756 times.
|
198940 | for (i = 1; i <= FFMIN(m, order); i++) { |
| 154 | 170184 | sce->coeffs[start] += lpc[i-1]*hist[start - i*inc]; | |
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | } | ||
| 159 | } | ||
| 160 | 2106 | } | |
| 161 | |||
| 162 | /* | ||
| 163 | * c_bits - 1 if 4 bit coefficients, 0 if 3 bit coefficients | ||
| 164 | */ | ||
| 165 | 186 | static inline void quantize_coefs(double *coef, int *idx, float *lpc, int order, | |
| 166 | int c_bits) | ||
| 167 | { | ||
| 168 | int i; | ||
| 169 | 186 | const float *quant_arr = ff_tns_tmp2_map[c_bits]; | |
| 170 |
2/2✓ Branch 0 taken 1137 times.
✓ Branch 1 taken 186 times.
|
1323 | for (i = 0; i < order; i++) { |
| 171 |
1/2✓ Branch 0 taken 1137 times.
✗ Branch 1 not taken.
|
1137 | idx[i] = quant_array_idx(coef[i], quant_arr, c_bits ? 16 : 8); |
| 172 | 1137 | lpc[i] = quant_arr[idx[i]]; | |
| 173 | } | ||
| 174 | 186 | } | |
| 175 | |||
| 176 | /* | ||
| 177 | * 3 bits per coefficient with 8 short windows | ||
| 178 | */ | ||
| 179 | /* Short blocks, pooled per group: one filter per scalefactor group so the | ||
| 180 | * shared sf sees uniform residuals (per-window filters caused silent | ||
| 181 | * sub-windows); all-or-none accept. */ | ||
| 182 | 43 | static void search_for_tns_short_pooled(AACEncContext *s, SingleChannelElement *sce) | |
| 183 | { | ||
| 184 | 43 | TemporalNoiseShaping *tns = &sce->tns; | |
| 185 |
1/2✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
|
43 | const int mmm = tns_max_nonpns(sce, FFMIN(sce->ics.tns_max_bands, sce->ics.max_sfb ? sce->ics.max_sfb : sce->ics.num_swb)); |
| 186 | 43 | const int sfb_start = av_clip(tns_min_sfb[1][s->samplerate_index], 0, mmm); | |
| 187 | 43 | const int sfb_end = av_clip(sce->ics.num_swb, 0, mmm); | |
| 188 | 43 | const int c_bits = TNS_Q_BITS_IS8 == 4; | |
| 189 | 43 | int count = 0; | |
| 190 | 43 | FFPsyBand *const psy_bands = &s->psy.ch[s->cur_channel].psy_bands[0]; | |
| 191 | |||
| 192 | 43 | memset(tns, 0, sizeof(*tns)); | |
| 193 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
|
43 | if (sfb_end - sfb_start <= 0) |
| 194 | ✗ | return; | |
| 195 | 43 | const int c_lo = sce->ics.swb_offset[sfb_start]; | |
| 196 | 43 | const int c_hi = sce->ics.swb_offset[sfb_end]; | |
| 197 | 43 | const int clen = c_hi - c_lo; | |
| 198 | 43 | const int ord_g = 7; | |
| 199 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
|
43 | if (clen <= 2*ord_g) |
| 200 | ✗ | return; | |
| 201 | |||
| 202 |
2/2✓ Branch 0 taken 165 times.
✓ Branch 1 taken 43 times.
|
208 | for (int wh = 0; wh < sce->ics.num_windows; wh += sce->ics.group_len[wh]) { |
| 203 | 165 | int gl = sce->ics.group_len[wh]; | |
| 204 | double coefs[MAX_LPC_ORDER]; | ||
| 205 | float pooled[1024], lpc_q[TNS_MAX_ORDER]; | ||
| 206 | float gain, gmin; | ||
| 207 | 165 | int ok = 1; | |
| 208 | |||
| 209 | /* per-window weighted spectra, concatenated over the group */ | ||
| 210 |
2/2✓ Branch 0 taken 344 times.
✓ Branch 1 taken 165 times.
|
509 | for (int w2 = 0; w2 < gl; w2++) { |
| 211 | 344 | int w = wh + w2; | |
| 212 | 344 | float maxrms = 0.0f, floorrms; | |
| 213 |
2/2✓ Branch 0 taken 3752 times.
✓ Branch 1 taken 344 times.
|
4096 | for (int g = sfb_start; g < sfb_end; g++) { |
| 214 | 3752 | int s0 = sce->ics.swb_offset[g], s1 = sce->ics.swb_offset[g+1]; | |
| 215 |
2/4✓ Branch 0 taken 3752 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3752 times.
✗ Branch 3 not taken.
|
3752 | float rms = sqrtf(FFMAX(psy_bands[w*16 + g].threshold, 0.0f) / FFMAX(s1 - s0, 1)); |
| 216 |
2/2✓ Branch 0 taken 2968 times.
✓ Branch 1 taken 784 times.
|
3752 | maxrms = FFMAX(maxrms, rms); |
| 217 | } | ||
| 218 |
1/2✓ Branch 0 taken 344 times.
✗ Branch 1 not taken.
|
344 | floorrms = FFMAX(maxrms * TNS_WEIGHT_FLOOR, 1e-9f); |
| 219 |
2/2✓ Branch 0 taken 3752 times.
✓ Branch 1 taken 344 times.
|
4096 | for (int g = sfb_start; g < sfb_end; g++) { |
| 220 | 3752 | int s0 = sce->ics.swb_offset[g], s1 = sce->ics.swb_offset[g+1]; | |
| 221 |
2/4✓ Branch 0 taken 3752 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3752 times.
✗ Branch 3 not taken.
|
3752 | float rms = sqrtf(FFMAX(psy_bands[w*16 + g].threshold, 0.0f) / FFMAX(s1 - s0, 1)); |
| 222 |
2/2✓ Branch 0 taken 3528 times.
✓ Branch 1 taken 224 times.
|
3752 | float wgt = 1.0f / FFMAX(rms, floorrms); |
| 223 |
2/2✓ Branch 0 taken 39392 times.
✓ Branch 1 taken 3752 times.
|
43144 | for (int k = s0; k < s1; k++) |
| 224 | 39392 | pooled[w2*clen + (k - c_lo)] = sce->coeffs[w*128 + k] * wgt; | |
| 225 | } | ||
| 226 | } | ||
| 227 | |||
| 228 | 165 | gain = ff_lpc_calc_ref_coefs_f(&s->lpc, pooled, clen*gl, ord_g, coefs, 0); | |
| 229 |
5/6✓ Branch 0 taken 163 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 142 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 21 times.
|
165 | if (!isfinite(gain) || gain < TNS_PREDGAIN_GATE || gain > TNS_PG_CLAMP) |
| 230 | 144 | continue; | |
| 231 |
2/2✓ Branch 0 taken 147 times.
✓ Branch 1 taken 21 times.
|
168 | for (int i = 0; i < ord_g; i++) |
| 232 | 147 | coefs[i] = -coefs[i]; | |
| 233 | |||
| 234 | 21 | quantize_coefs(coefs, tns->coef_idx[wh][0], tns->coef[wh][0], ord_g, c_bits); | |
| 235 | 21 | compute_lpc_coefs(tns->coef[wh][0], 0, ord_g, lpc_q, 0, 0, 0, NULL); | |
| 236 | |||
| 237 | /* every window must clear the measured post-quantization bar */ | ||
| 238 | 21 | gmin = FLT_MAX; | |
| 239 |
2/2✓ Branch 0 taken 27 times.
✓ Branch 1 taken 21 times.
|
48 | for (int w2 = 0; w2 < gl; w2++) { |
| 240 | 27 | const float *msrc = pooled + w2*clen; | |
| 241 | 27 | float orig_e = 0.0f, filt_e = 0.0f; | |
| 242 |
2/2✓ Branch 0 taken 3068 times.
✓ Branch 1 taken 27 times.
|
3095 | for (int m = 0; m < clen; m++) { |
| 243 | 3068 | float acc = msrc[m]; | |
| 244 |
2/2✓ Branch 0 taken 20720 times.
✓ Branch 1 taken 3068 times.
|
23788 | for (int i = 1; i <= FFMIN(m, ord_g); i++) |
| 245 | 20720 | acc += lpc_q[i-1] * msrc[m - i]; | |
| 246 | 3068 | orig_e += msrc[m]*msrc[m]; | |
| 247 | 3068 | filt_e += acc*acc; | |
| 248 | } | ||
| 249 |
4/6✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 22 times.
✗ Branch 5 not taken.
|
27 | gmin = FFMIN(gmin, orig_e / FFMAX(filt_e, 1e-9f)); |
| 250 | } | ||
| 251 | { | ||
| 252 | /* accept Schmitt, run-scoped: hard entry / easy hold inside | ||
| 253 | * short runs (anti-gravel); isolated frames use the base bar */ | ||
| 254 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 14 times.
|
21 | int in_run = s->nmr ? s->nmr->prev_was_short : 0; |
| 255 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 14 times.
|
21 | int prev_on = s->nmr ? s->nmr->tns8_prev[s->cur_channel & 15] : 0; |
| 256 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
21 | float bar = TNS_PG_C1_SHORT * (!in_run ? 1.0f : prev_on ? 0.5f : 1.8f); |
| 257 |
1/2✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
|
21 | if (gmin < bar) |
| 258 | 21 | ok = 0; | |
| 259 | } | ||
| 260 | |||
| 261 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if (ok) { |
| 262 | ✗ | for (int w2 = 0; w2 < gl; w2++) { | |
| 263 | ✗ | int w = wh + w2; | |
| 264 | ✗ | tns->n_filt[w] = 1; | |
| 265 | ✗ | tns->length[w][0] = sfb_end - sfb_start; | |
| 266 | ✗ | tns->order[w][0] = ord_g; | |
| 267 | ✗ | tns->direction[w][0] = 0; | |
| 268 | ✗ | if (w2) { | |
| 269 | ✗ | memcpy(tns->coef_idx[w][0], tns->coef_idx[wh][0], sizeof(tns->coef_idx[w][0])); | |
| 270 | ✗ | memcpy(tns->coef[w][0], tns->coef[wh][0], sizeof(tns->coef[w][0])); | |
| 271 | } | ||
| 272 | ✗ | count++; | |
| 273 | } | ||
| 274 | } | ||
| 275 | } | ||
| 276 | 43 | sce->tns.present = !!count; | |
| 277 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 38 times.
|
43 | if (s->nmr) |
| 278 | 5 | s->nmr->tns8_prev[s->cur_channel & 15] = !!count; | |
| 279 | } | ||
| 280 | |||
| 281 | 2106 | void ff_aac_search_for_tns(AACEncContext *s, SingleChannelElement *sce) | |
| 282 | { | ||
| 283 | 2106 | TemporalNoiseShaping *tns = &sce->tns; | |
| 284 | 2106 | int w, count = 0; | |
| 285 | 2106 | const int mmm = tns_max_nonpns(sce, FFMIN(sce->ics.tns_max_bands, sce->ics.max_sfb)); | |
| 286 | 2106 | const int is8 = sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE; | |
| 287 | 2106 | const int c_bits = is8 ? TNS_Q_BITS_IS8 == 4 : TNS_Q_BITS == 4; | |
| 288 | 2106 | const int sfb_start = av_clip(tns_min_sfb[is8][s->samplerate_index], 0, mmm); | |
| 289 | 2106 | const int sfb_end = av_clip(sce->ics.num_swb, 0, mmm); | |
| 290 |
2/2✓ Branch 0 taken 45 times.
✓ Branch 1 taken 2061 times.
|
2106 | const int order = is8 ? 7 : 12; |
| 291 |
2/2✓ Branch 0 taken 2060 times.
✓ Branch 1 taken 46 times.
|
4166 | const int slant = sce->ics.window_sequence[0] == LONG_STOP_SEQUENCE ? 1 : |
| 292 |
2/2✓ Branch 0 taken 46 times.
✓ Branch 1 taken 2014 times.
|
2060 | sce->ics.window_sequence[0] == LONG_START_SEQUENCE ? 0 : 2; |
| 293 | 2106 | const int sfb_len = sfb_end - sfb_start; | |
| 294 | 2106 | const int coef_len = sce->ics.swb_offset[sfb_end] - sce->ics.swb_offset[sfb_start]; | |
| 295 |
3/4✓ Branch 0 taken 2061 times.
✓ Branch 1 taken 45 times.
✓ Branch 2 taken 2061 times.
✗ Branch 3 not taken.
|
2106 | const int n_filt = is8 ? 1 : order != TNS_MAX_ORDER ? 2 : 3; |
| 296 | 2106 | const int ord_g = order / n_filt; | |
| 297 | |||
| 298 | /* Apple's accept bar (minimum measured prediction gain): higher on short blocks, | ||
| 299 | * where a weak filter's shaped-noise tail spreads across the 50% overlap. */ | ||
| 300 |
2/2✓ Branch 0 taken 45 times.
✓ Branch 1 taken 2061 times.
|
2106 | const float c1 = is8 ? TNS_PG_C1_SHORT : TNS_PG_C1_LONG; |
| 301 | 2106 | FFPsyBand *const psy_bands = &s->psy.ch[s->cur_channel].psy_bands[0]; | |
| 302 | |||
| 303 |
3/4✓ Branch 0 taken 1944 times.
✓ Branch 1 taken 162 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1944 times.
|
2106 | if (coef_len <= 0 || sfb_len <= 0) { |
| 304 | 162 | sce->tns.present = 0; | |
| 305 | 205 | return; | |
| 306 | } | ||
| 307 |
2/2✓ Branch 0 taken 43 times.
✓ Branch 1 taken 1901 times.
|
1944 | if (is8) { |
| 308 | 43 | search_for_tns_short_pooled(s, sce); | |
| 309 | 43 | return; | |
| 310 | } | ||
| 311 | |||
| 312 | /* time-domain window length backing one coding window: a long MDCT block is | ||
| 313 | * fed 2048 windowed samples (current 1024 + overlap), each short block 256. */ | ||
| 314 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1901 times.
|
1901 | const int tlen = is8 ? 256 : 2048; |
| 315 | |||
| 316 | 1901 | float mgain[8] = {0}; | |
| 317 | |||
| 318 |
2/2✓ Branch 0 taken 1901 times.
✓ Branch 1 taken 1901 times.
|
3802 | for (w = 0; w < sce->ics.num_windows; w++) { |
| 319 | 1901 | int filt, any = 0; | |
| 320 | |||
| 321 | /* The filter gets ran in the direction of the signal's *temporal* energy, | ||
| 322 | * so the quantization noise stays in the loud masked part rather than spilling | ||
| 323 | * into the quiet part. */ | ||
| 324 | 1901 | const float *tw = sce->ret_buf + w*tlen; | |
| 325 | 1901 | float e_early = 0.0f, e_late = 0.0f; | |
| 326 | int ti; | ||
| 327 |
2/2✓ Branch 0 taken 1946624 times.
✓ Branch 1 taken 1901 times.
|
1948525 | for (ti = 0; ti < tlen/2; ti++) |
| 328 | 1946624 | e_early += tw[ti]*tw[ti]; | |
| 329 |
2/2✓ Branch 0 taken 1946624 times.
✓ Branch 1 taken 1901 times.
|
1948525 | for (; ti < tlen; ti++) |
| 330 | 1946624 | e_late += tw[ti]*tw[ti]; | |
| 331 | 1901 | const int tdir = e_early > e_late; | |
| 332 | |||
| 333 | /* Walk the frequency regions exactly as the decoder does: filter 0 is the | ||
| 334 | * topmost band region, each subsequent filter covers the next region down, | ||
| 335 | * clamped to mmm. Each filter gets its own LPC over its own region. */ | ||
| 336 | 1901 | int top_sfb = sce->ics.num_swb; | |
| 337 |
2/2✓ Branch 0 taken 3802 times.
✓ Branch 1 taken 1901 times.
|
5703 | for (filt = 0; filt < n_filt; filt++) { |
| 338 | double coefs[MAX_LPC_ORDER]; | ||
| 339 | float wspec[1024], tmp[1024], lpc_q[TNS_MAX_ORDER]; | ||
| 340 | 9505 | int len_sfb = (filt == n_filt - 1) ? sfb_len - filt*(sfb_len/n_filt) | |
| 341 |
2/2✓ Branch 0 taken 1901 times.
✓ Branch 1 taken 1901 times.
|
3802 | : sfb_len/n_filt; |
| 342 | 3802 | int bot_sfb = FFMAX(0, top_sfb - len_sfb); | |
| 343 | 3802 | int g_lo = FFMIN(bot_sfb, mmm), g_hi = FFMIN(top_sfb, mmm); | |
| 344 | 3802 | int c_lo = sce->ics.swb_offset[g_lo]; | |
| 345 | 3802 | int c_hi = sce->ics.swb_offset[g_hi]; | |
| 346 | 3802 | int clen = c_hi - c_lo; | |
| 347 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 3724 times.
|
3802 | const int dir = slant != 2 ? slant : tdir; |
| 348 | 3802 | float gain, orig_e = 0.0f, filt_e = 0.0f; | |
| 349 | int m, i, g, inc, st; | ||
| 350 | |||
| 351 | 3802 | tns->length[w][filt] = len_sfb; | |
| 352 | 3802 | tns->order[w][filt] = 0; /* default: region carries no filter */ | |
| 353 | 3802 | top_sfb = bot_sfb; | |
| 354 | |||
| 355 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3802 times.
|
3802 | if (clen <= 2*ord_g) /* too short for a stable order-ord_g LPC */ |
| 356 | 3690 | continue; | |
| 357 | |||
| 358 | /* Fit LPC on the perceptually-weighted spectrum X/sqrt(thr), floored | ||
| 359 | * to avoid a near-zero threshold blowing up a single bin (Apple). */ | ||
| 360 | { | ||
| 361 | 3802 | float maxrms = 0.0f, floorrms; | |
| 362 | int k; | ||
| 363 |
2/2✓ Branch 0 taken 33051 times.
✓ Branch 1 taken 3802 times.
|
36853 | for (g = g_lo; g < g_hi; g++) { |
| 364 | 33051 | int s0 = sce->ics.swb_offset[g], s1 = sce->ics.swb_offset[g+1]; | |
| 365 |
1/2✓ Branch 0 taken 33051 times.
✗ Branch 1 not taken.
|
33051 | float rms = sqrtf(FFMAX(psy_bands[w*16 + g].threshold, 0.0f) / |
| 366 |
1/2✓ Branch 0 taken 33051 times.
✗ Branch 1 not taken.
|
33051 | FFMAX(s1 - s0, 1)); |
| 367 |
2/2✓ Branch 0 taken 20588 times.
✓ Branch 1 taken 12463 times.
|
33051 | maxrms = FFMAX(maxrms, rms); |
| 368 | } | ||
| 369 |
1/2✓ Branch 0 taken 3802 times.
✗ Branch 1 not taken.
|
3802 | floorrms = FFMAX(maxrms * TNS_WEIGHT_FLOOR, 1e-9f); |
| 370 |
2/2✓ Branch 0 taken 33051 times.
✓ Branch 1 taken 3802 times.
|
36853 | for (g = g_lo; g < g_hi; g++) { |
| 371 | 33051 | int s0 = sce->ics.swb_offset[g], s1 = sce->ics.swb_offset[g+1]; | |
| 372 |
1/2✓ Branch 0 taken 33051 times.
✗ Branch 1 not taken.
|
33051 | float rms = sqrtf(FFMAX(psy_bands[w*16 + g].threshold, 0.0f) / |
| 373 |
1/2✓ Branch 0 taken 33051 times.
✗ Branch 1 not taken.
|
33051 | FFMAX(s1 - s0, 1)); |
| 374 |
2/2✓ Branch 0 taken 30558 times.
✓ Branch 1 taken 2493 times.
|
33051 | float wgt = 1.0f / FFMAX(rms, floorrms); |
| 375 |
2/2✓ Branch 0 taken 993864 times.
✓ Branch 1 taken 33051 times.
|
1026915 | for (k = s0; k < s1; k++) |
| 376 | 993864 | wspec[k - c_lo] = sce->coeffs[w*128 + k] * wgt; | |
| 377 | } | ||
| 378 | /* Short blocks: unwindowed fit; Hann window zeros the edges of the | ||
| 379 | * tiny region, wrecking the LPC. Long blocks keep the window. */ | ||
| 380 | 3802 | gain = ff_lpc_calc_ref_coefs_f(&s->lpc, wspec, clen, ord_g, coefs, !is8); | |
| 381 | } | ||
| 382 | /* Reject below the first gate and above the clamp (poles near unit circle). */ | ||
| 383 |
5/6✓ Branch 0 taken 3802 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 454 times.
✓ Branch 3 taken 3348 times.
✓ Branch 4 taken 289 times.
✓ Branch 5 taken 165 times.
|
3802 | if (!isfinite(gain) || gain < TNS_PREDGAIN_GATE || gain > TNS_PG_CLAMP) |
| 384 | 3637 | continue; | |
| 385 | /* Negate: ff_lpc_calc_ref_coefs_f sign convention is opposite to what | ||
| 386 | * ff_aac_apply_tns's MA filter needs; fed unnegated, it anti-whitens. */ | ||
| 387 |
2/2✓ Branch 0 taken 990 times.
✓ Branch 1 taken 165 times.
|
1155 | for (i = 0; i < ord_g; i++) |
| 388 | 990 | coefs[i] = -coefs[i]; | |
| 389 | |||
| 390 | /* Quantize, then build the decoder's direct-form LPC. */ | ||
| 391 | 165 | quantize_coefs(coefs, tns->coef_idx[w][filt], tns->coef[w][filt], | |
| 392 | ord_g, c_bits); | ||
| 393 | 165 | compute_lpc_coefs(tns->coef[w][filt], 0, ord_g, lpc_q, 0, 0, 0, NULL); | |
| 394 | |||
| 395 | /* Apply the quantized filter to the weighted spectrum and measure gain. */ | ||
| 396 | 165 | const float *msrc = wspec; | |
| 397 |
2/2✓ Branch 0 taken 53 times.
✓ Branch 1 taken 112 times.
|
165 | inc = dir ? -1 : 1; |
| 398 |
2/2✓ Branch 0 taken 53 times.
✓ Branch 1 taken 112 times.
|
165 | st = dir ? clen - 1 : 0; |
| 399 |
2/2✓ Branch 0 taken 41380 times.
✓ Branch 1 taken 165 times.
|
41545 | for (m = 0; m < clen; m++) { |
| 400 | 41380 | int idx = st + m*inc; | |
| 401 | 41380 | float acc = msrc[idx]; | |
| 402 |
2/2✓ Branch 0 taken 244815 times.
✓ Branch 1 taken 41380 times.
|
286195 | for (i = 1; i <= FFMIN(m, ord_g); i++) |
| 403 | 244815 | acc += lpc_q[i-1] * msrc[idx - i*inc]; | |
| 404 | 41380 | tmp[idx] = acc; | |
| 405 | } | ||
| 406 |
2/2✓ Branch 0 taken 41380 times.
✓ Branch 1 taken 165 times.
|
41545 | for (m = 0; m < clen; m++) { |
| 407 | 41380 | orig_e += msrc[m]*msrc[m]; | |
| 408 | 41380 | filt_e += tmp[m]*tmp[m]; | |
| 409 | } | ||
| 410 |
1/2✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
|
165 | filt_e = FFMAX(filt_e, 1e-9f); |
| 411 | |||
| 412 | /* Keep only if measured post-quantization gain clears C1 (Apple's outcome gate). */ | ||
| 413 |
2/2✓ Branch 0 taken 53 times.
✓ Branch 1 taken 112 times.
|
165 | if (orig_e < c1*filt_e) |
| 414 | 53 | continue; | |
| 415 | |||
| 416 | 112 | tns->order[w][filt] = ord_g; | |
| 417 | 112 | tns->direction[w][filt] = dir; | |
| 418 | 112 | mgain[w] = orig_e / filt_e; | |
| 419 | 112 | any = 1; | |
| 420 | } | ||
| 421 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 1823 times.
|
1901 | tns->n_filt[w] = any ? n_filt : 0; |
| 422 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 1823 times.
|
1901 | if (any) |
| 423 | 78 | count++; | |
| 424 | } | ||
| 425 | |||
| 426 | /* per-window path: group-uniformity gate (mismatched whitening within a | ||
| 427 | * shared-sf group silences sub-windows) */ | ||
| 428 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1901 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1901 | if (is8 && count) { |
| 429 | ✗ | const float gspread = 2.0f; | |
| 430 | ✗ | count = 0; | |
| 431 | ✗ | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { | |
| 432 | ✗ | int gl = sce->ics.group_len[w], drop = 0; | |
| 433 | ✗ | float gmin = FLT_MAX, gmax = 0.0f; | |
| 434 | ✗ | for (int w2 = w; w2 < w + gl; w2++) { | |
| 435 | ✗ | if (!tns->n_filt[w2] || mgain[w2] <= 0.0f) { drop = 1; break; } | |
| 436 | ✗ | gmin = FFMIN(gmin, mgain[w2]); | |
| 437 | ✗ | gmax = FFMAX(gmax, mgain[w2]); | |
| 438 | } | ||
| 439 | ✗ | if (!drop && gmax > gspread * gmin) | |
| 440 | ✗ | drop = 1; | |
| 441 | ✗ | for (int w2 = w; w2 < w + gl; w2++) { | |
| 442 | ✗ | if (drop) { | |
| 443 | ✗ | tns->n_filt[w2] = 0; | |
| 444 | ✗ | for (int f2 = 0; f2 < n_filt; f2++) | |
| 445 | ✗ | tns->order[w2][f2] = 0; | |
| 446 | ✗ | } else if (tns->n_filt[w2]) { | |
| 447 | ✗ | count++; | |
| 448 | } | ||
| 449 | } | ||
| 450 | } | ||
| 451 | } | ||
| 452 | 1901 | sce->tns.present = !!count; | |
| 453 | } | ||
| 454 |