| 1 |
/****************************************************************************** |
| 2 |
* |
| 3 |
* Copyright 2022 Google LLC |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at: |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
******************************************************************************/ |
| 18 |
|
| 19 |
#include "attdet.h" |
| 20 |
|
| 21 |
|
| 22 |
/** |
| 23 |
* Time domain attack detector |
| 24 |
*/ |
| 25 |
bool lc3_attdet_run(enum lc3_dt dt, enum lc3_srate sr, |
| 26 |
int nbytes, struct lc3_attdet_analysis *attdet, const int16_t *x) |
| 27 |
{ |
| 28 |
/* --- Check enabling --- */ |
| 29 |
|
| 30 |
const int nbytes_ranges[][LC3_NUM_SRATE - LC3_SRATE_32K][2] = { |
| 31 |
[LC3_DT_7M5 - LC3_DT_7M5] = { { 61, 149 }, { 75, 149 } }, |
| 32 |
[LC3_DT_10M - LC3_DT_7M5] = { { 81, INT_MAX }, { 100, INT_MAX } }, |
| 33 |
}; |
| 34 |
|
| 35 |
if (dt < LC3_DT_7M5 || sr < LC3_SRATE_32K || lc3_hr(sr) || |
| 36 |
nbytes < nbytes_ranges[dt - LC3_DT_7M5][sr - LC3_SRATE_32K][0] || |
| 37 |
nbytes > nbytes_ranges[dt - LC3_DT_7M5][sr - LC3_SRATE_32K][1] ) |
| 38 |
return 0; |
| 39 |
|
| 40 |
/* --- Filtering & Energy calculation --- */ |
| 41 |
|
| 42 |
int nblk = 4 - (dt == LC3_DT_7M5); |
| 43 |
int32_t e[4]; |
| 44 |
|
| 45 |
for (int i = 0; i < nblk; i++) { |
| 46 |
e[i] = 0; |
| 47 |
|
| 48 |
if (sr == LC3_SRATE_32K) { |
| 49 |
int16_t xn2 = (x[-4] + x[-3]) >> 1; |
| 50 |
int16_t xn1 = (x[-2] + x[-1]) >> 1; |
| 51 |
int16_t xn, xf; |
| 52 |
|
| 53 |
for (int j = 0; j < 40; j++, x += 2, xn2 = xn1, xn1 = xn) { |
| 54 |
xn = (x[0] + x[1]) >> 1; |
| 55 |
xf = (3 * xn - 4 * xn1 + 1 * xn2) >> 3; |
| 56 |
e[i] += (xf * xf) >> 5; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
else { |
| 61 |
int16_t xn2 = (x[-6] + x[-5] + x[-4]) >> 2; |
| 62 |
int16_t xn1 = (x[-3] + x[-2] + x[-1]) >> 2; |
| 63 |
int16_t xn, xf; |
| 64 |
|
| 65 |
for (int j = 0; j < 40; j++, x += 3, xn2 = xn1, xn1 = xn) { |
| 66 |
xn = (x[0] + x[1] + x[2]) >> 2; |
| 67 |
xf = (3 * xn - 4 * xn1 + 1 * xn2) >> 3; |
| 68 |
e[i] += (xf * xf) >> 5; |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/* --- Attack detection --- |
| 74 |
* The attack block `p_att` is defined as the normative value + 1, |
| 75 |
* in such way, it will be initialized to 0 */ |
| 76 |
|
| 77 |
int p_att = 0; |
| 78 |
int32_t a[4]; |
| 79 |
|
| 80 |
for (int i = 0; i < nblk; i++) { |
| 81 |
a[i] = LC3_MAX(attdet->an1 >> 2, attdet->en1); |
| 82 |
attdet->en1 = e[i], attdet->an1 = a[i]; |
| 83 |
|
| 84 |
if ((e[i] >> 3) > a[i] + (a[i] >> 4)) |
| 85 |
p_att = i + 1; |
| 86 |
} |
| 87 |
|
| 88 |
int att = attdet->p_att >= 1 + (nblk >> 1) || p_att > 0; |
| 89 |
attdet->p_att = p_att; |
| 90 |
|
| 91 |
return att; |
| 92 |
} |