19a19cd78SMatthias Ringwald /******************************************************************************
29a19cd78SMatthias Ringwald *
34930cef6SMatthias Ringwald * Copyright 2022 Google LLC
49a19cd78SMatthias Ringwald *
59a19cd78SMatthias Ringwald * Licensed under the Apache License, Version 2.0 (the "License");
69a19cd78SMatthias Ringwald * you may not use this file except in compliance with the License.
79a19cd78SMatthias Ringwald * You may obtain a copy of the License at:
89a19cd78SMatthias Ringwald *
99a19cd78SMatthias Ringwald * http://www.apache.org/licenses/LICENSE-2.0
109a19cd78SMatthias Ringwald *
119a19cd78SMatthias Ringwald * Unless required by applicable law or agreed to in writing, software
129a19cd78SMatthias Ringwald * distributed under the License is distributed on an "AS IS" BASIS,
139a19cd78SMatthias Ringwald * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
149a19cd78SMatthias Ringwald * See the License for the specific language governing permissions and
159a19cd78SMatthias Ringwald * limitations under the License.
169a19cd78SMatthias Ringwald *
179a19cd78SMatthias Ringwald ******************************************************************************/
189a19cd78SMatthias Ringwald
199a19cd78SMatthias Ringwald #include "attdet.h"
209a19cd78SMatthias Ringwald
219a19cd78SMatthias Ringwald
229a19cd78SMatthias Ringwald /**
239a19cd78SMatthias Ringwald * Time domain attack detector
249a19cd78SMatthias Ringwald */
lc3_attdet_run(enum lc3_dt dt,enum lc3_srate sr,int nbytes,struct lc3_attdet_analysis * attdet,const int16_t * x)259a19cd78SMatthias Ringwald bool lc3_attdet_run(enum lc3_dt dt, enum lc3_srate sr,
264930cef6SMatthias Ringwald int nbytes, struct lc3_attdet_analysis *attdet, const int16_t *x)
279a19cd78SMatthias Ringwald {
289a19cd78SMatthias Ringwald /* --- Check enabling --- */
299a19cd78SMatthias Ringwald
30*6897da5cSDirk Helbig const int nbytes_ranges[][LC3_NUM_SRATE - LC3_SRATE_32K][2] = {
31*6897da5cSDirk Helbig [LC3_DT_7M5 - LC3_DT_7M5] = { { 61, 149 }, { 75, 149 } },
32*6897da5cSDirk Helbig [LC3_DT_10M - LC3_DT_7M5] = { { 81, INT_MAX }, { 100, INT_MAX } },
339a19cd78SMatthias Ringwald };
349a19cd78SMatthias Ringwald
35*6897da5cSDirk Helbig if (dt < LC3_DT_7M5 || sr < LC3_SRATE_32K || lc3_hr(sr) ||
36*6897da5cSDirk Helbig nbytes < nbytes_ranges[dt - LC3_DT_7M5][sr - LC3_SRATE_32K][0] ||
37*6897da5cSDirk Helbig nbytes > nbytes_ranges[dt - LC3_DT_7M5][sr - LC3_SRATE_32K][1] )
389a19cd78SMatthias Ringwald return 0;
399a19cd78SMatthias Ringwald
409a19cd78SMatthias Ringwald /* --- Filtering & Energy calculation --- */
419a19cd78SMatthias Ringwald
429a19cd78SMatthias Ringwald int nblk = 4 - (dt == LC3_DT_7M5);
434930cef6SMatthias Ringwald int32_t e[4];
449a19cd78SMatthias Ringwald
459a19cd78SMatthias Ringwald for (int i = 0; i < nblk; i++) {
469a19cd78SMatthias Ringwald e[i] = 0;
479a19cd78SMatthias Ringwald
489a19cd78SMatthias Ringwald if (sr == LC3_SRATE_32K) {
494930cef6SMatthias Ringwald int16_t xn2 = (x[-4] + x[-3]) >> 1;
504930cef6SMatthias Ringwald int16_t xn1 = (x[-2] + x[-1]) >> 1;
514930cef6SMatthias Ringwald int16_t xn, xf;
529a19cd78SMatthias Ringwald
539a19cd78SMatthias Ringwald for (int j = 0; j < 40; j++, x += 2, xn2 = xn1, xn1 = xn) {
544930cef6SMatthias Ringwald xn = (x[0] + x[1]) >> 1;
554930cef6SMatthias Ringwald xf = (3 * xn - 4 * xn1 + 1 * xn2) >> 3;
564930cef6SMatthias Ringwald e[i] += (xf * xf) >> 5;
579a19cd78SMatthias Ringwald }
589a19cd78SMatthias Ringwald }
599a19cd78SMatthias Ringwald
609a19cd78SMatthias Ringwald else {
614930cef6SMatthias Ringwald int16_t xn2 = (x[-6] + x[-5] + x[-4]) >> 2;
624930cef6SMatthias Ringwald int16_t xn1 = (x[-3] + x[-2] + x[-1]) >> 2;
634930cef6SMatthias Ringwald int16_t xn, xf;
649a19cd78SMatthias Ringwald
659a19cd78SMatthias Ringwald for (int j = 0; j < 40; j++, x += 3, xn2 = xn1, xn1 = xn) {
664930cef6SMatthias Ringwald xn = (x[0] + x[1] + x[2]) >> 2;
674930cef6SMatthias Ringwald xf = (3 * xn - 4 * xn1 + 1 * xn2) >> 3;
684930cef6SMatthias Ringwald e[i] += (xf * xf) >> 5;
699a19cd78SMatthias Ringwald }
709a19cd78SMatthias Ringwald }
719a19cd78SMatthias Ringwald }
729a19cd78SMatthias Ringwald
739a19cd78SMatthias Ringwald /* --- Attack detection ---
749a19cd78SMatthias Ringwald * The attack block `p_att` is defined as the normative value + 1,
759a19cd78SMatthias Ringwald * in such way, it will be initialized to 0 */
769a19cd78SMatthias Ringwald
779a19cd78SMatthias Ringwald int p_att = 0;
784930cef6SMatthias Ringwald int32_t a[4];
799a19cd78SMatthias Ringwald
809a19cd78SMatthias Ringwald for (int i = 0; i < nblk; i++) {
814930cef6SMatthias Ringwald a[i] = LC3_MAX(attdet->an1 >> 2, attdet->en1);
829a19cd78SMatthias Ringwald attdet->en1 = e[i], attdet->an1 = a[i];
839a19cd78SMatthias Ringwald
844930cef6SMatthias Ringwald if ((e[i] >> 3) > a[i] + (a[i] >> 4))
859a19cd78SMatthias Ringwald p_att = i + 1;
869a19cd78SMatthias Ringwald }
879a19cd78SMatthias Ringwald
889a19cd78SMatthias Ringwald int att = attdet->p_att >= 1 + (nblk >> 1) || p_att > 0;
899a19cd78SMatthias Ringwald attdet->p_att = p_att;
909a19cd78SMatthias Ringwald
919a19cd78SMatthias Ringwald return att;
929a19cd78SMatthias Ringwald }
93