void ff_aac_search_for_tns(AACEncContext *s, SingleChannelElement *sce)
{
    TemporalNoiseShaping *tns = &sce->tns;
    int w, g, w2, prev_end_sfb = 0, count = 0;
    const int is8 = sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE;
    const int tns_max_order = is8 ? 7 : s->profile == FF_PROFILE_AAC_LOW ? 12 : TNS_MAX_ORDER;

    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
        int order = 0, filters = 1;
        int sfb_start = 0, sfb_len = 0;
        int coef_start = 0, coef_len = 0;
        float energy = 0.0f, threshold = 0.0f;
        double coefs[MAX_LPC_ORDER][MAX_LPC_ORDER] = {{0}};
        for (g = 0;  g < sce->ics.num_swb; g++) {
            if (!sfb_start && w*16+g > TNS_LOW_LIMIT && w*16+g > prev_end_sfb) {
                sfb_start = w*16+g;
                coef_start =  sce->ics.swb_offset[sfb_start];
            }
            if (sfb_start) {
                for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
                    FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
                    if (!sfb_len && band->energy < band->threshold*1.3f) {
                        sfb_len = (w+w2)*16+g - sfb_start;
                        prev_end_sfb = sfb_start + sfb_len;
                        coef_len = sce->ics.swb_offset[sfb_start + sfb_len] - coef_start;
                        break;
                    }
                    energy += band->energy;
                    threshold += band->threshold;
                }
                if (!sfb_len) {
                    sfb_len = (w+sce->ics.group_len[w])*16+g - sfb_start;
                    coef_len = sce->ics.swb_offset[sfb_start + sfb_len] - coef_start;
                }
            }
        }

        if (sfb_len <= 0 || coef_len <= 0)
            continue;
        if (coef_start + coef_len > 1024)
            coef_len = 1024 - coef_start;

        /* LPC */
        order = ff_lpc_calc_levinsion(&s->lpc, &sce->coeffs[coef_start], coef_len,
                                      coefs, 0, tns_max_order, ORDER_METHOD_LOG);

        if (energy > threshold) {
            int direction = 0;
            tns->n_filt[w] = filters++;
            for (g = 0; g < tns->n_filt[w]; g++) {
                process_tns_coeffs(tns, coefs[order], &order, w, g);
                tns->order[w][g]     = order;
                tns->length[w][g]    = sfb_len;
                tns->direction[w][g] = direction;
            }
            count++;
        }
    }

    sce->tns.present = !!count;
}