| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
#include "asterisk.h" |
| 38 |
|
| 39 |
#include <math.h> |
| 40 |
|
| 41 |
#include "asterisk/config.h" |
| 42 |
#include "asterisk/module.h" |
| 43 |
#include "asterisk/plc.h" |
| 44 |
|
| 45 |
#if !defined(FALSE) |
| 46 |
#define FALSE 0 |
| 47 |
#endif |
| 48 |
#if !defined(TRUE) |
| 49 |
#define TRUE (!FALSE) |
| 50 |
#endif |
| 51 |
|
| 52 |
#if !defined(INT16_MAX) |
| 53 |
#define INT16_MAX (32767) |
| 54 |
#define INT16_MIN (-32767-1) |
| 55 |
#endif |
| 56 |
|
| 57 |
|
| 58 |
#define ATTENUATION_INCREMENT 0.0025 |
| 59 |
|
| 60 |
#define ms_to_samples(t) (((t)*DEFAULT_SAMPLE_RATE)/1000) |
| 61 |
|
| 62 |
static inline int16_t fsaturate(double damp) |
| 63 |
{ |
| 64 |
if (damp > 32767.0) |
| 65 |
return INT16_MAX; |
| 66 |
if (damp < -32768.0) |
| 67 |
return INT16_MIN; |
| 68 |
return (int16_t) rint(damp); |
| 69 |
} |
| 70 |
|
| 71 |
static void save_history(plc_state_t *s, int16_t *buf, int len) |
| 72 |
{ |
| 73 |
if (len >= PLC_HISTORY_LEN) { |
| 74 |
|
| 75 |
memcpy(s->history, buf + len - PLC_HISTORY_LEN, sizeof(int16_t) * PLC_HISTORY_LEN); |
| 76 |
s->buf_ptr = 0; |
| 77 |
return; |
| 78 |
} |
| 79 |
if (s->buf_ptr + len > PLC_HISTORY_LEN) { |
| 80 |
|
| 81 |
memcpy(s->history + s->buf_ptr, buf, sizeof(int16_t) * (PLC_HISTORY_LEN - s->buf_ptr)); |
| 82 |
len -= (PLC_HISTORY_LEN - s->buf_ptr); |
| 83 |
memcpy(s->history, buf + (PLC_HISTORY_LEN - s->buf_ptr), sizeof(int16_t)*len); |
| 84 |
s->buf_ptr = len; |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
memcpy(s->history + s->buf_ptr, buf, sizeof(int16_t)*len); |
| 89 |
s->buf_ptr += len; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
static void normalise_history(plc_state_t *s) |
| 95 |
{ |
| 96 |
int16_t tmp[PLC_HISTORY_LEN]; |
| 97 |
|
| 98 |
if (s->buf_ptr == 0) |
| 99 |
return; |
| 100 |
memcpy(tmp, s->history, sizeof(int16_t)*s->buf_ptr); |
| 101 |
memmove(s->history, s->history + s->buf_ptr, sizeof(int16_t) * (PLC_HISTORY_LEN - s->buf_ptr)); |
| 102 |
memcpy(s->history + PLC_HISTORY_LEN - s->buf_ptr, tmp, sizeof(int16_t) * s->buf_ptr); |
| 103 |
s->buf_ptr = 0; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
static int __inline__ amdf_pitch(int min_pitch, int max_pitch, int16_t amp[], int len) |
| 109 |
{ |
| 110 |
int i; |
| 111 |
int j; |
| 112 |
int acc; |
| 113 |
int min_acc; |
| 114 |
int pitch; |
| 115 |
|
| 116 |
pitch = min_pitch; |
| 117 |
min_acc = INT_MAX; |
| 118 |
for (i = max_pitch; i <= min_pitch; i++) { |
| 119 |
acc = 0; |
| 120 |
for (j = 0; j < len; j++) |
| 121 |
acc += abs(amp[i + j] - amp[j]); |
| 122 |
if (acc < min_acc) { |
| 123 |
min_acc = acc; |
| 124 |
pitch = i; |
| 125 |
} |
| 126 |
} |
| 127 |
return pitch; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
int plc_rx(plc_state_t *s, int16_t amp[], int len) |
| 133 |
{ |
| 134 |
int i; |
| 135 |
int pitch_overlap; |
| 136 |
float old_step; |
| 137 |
float new_step; |
| 138 |
float old_weight; |
| 139 |
float new_weight; |
| 140 |
float gain; |
| 141 |
|
| 142 |
if (s->missing_samples) { |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
pitch_overlap = s->pitch >> 2; |
| 149 |
if (pitch_overlap > len) |
| 150 |
pitch_overlap = len; |
| 151 |
gain = 1.0 - s->missing_samples*ATTENUATION_INCREMENT; |
| 152 |
if (gain < 0.0) |
| 153 |
gain = 0.0; |
| 154 |
new_step = 1.0/pitch_overlap; |
| 155 |
old_step = new_step*gain; |
| 156 |
new_weight = new_step; |
| 157 |
old_weight = (1.0 - new_step)*gain; |
| 158 |
for (i = 0; i < pitch_overlap; i++) { |
| 159 |
amp[i] = fsaturate(old_weight * s->pitchbuf[s->pitch_offset] + new_weight * amp[i]); |
| 160 |
if (++s->pitch_offset >= s->pitch) |
| 161 |
s->pitch_offset = 0; |
| 162 |
new_weight += new_step; |
| 163 |
old_weight -= old_step; |
| 164 |
if (old_weight < 0.0) |
| 165 |
old_weight = 0.0; |
| 166 |
} |
| 167 |
s->missing_samples = 0; |
| 168 |
} |
| 169 |
save_history(s, amp, len); |
| 170 |
return len; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
int plc_fillin(plc_state_t *s, int16_t amp[], int len) |
| 176 |
{ |
| 177 |
int i; |
| 178 |
int pitch_overlap; |
| 179 |
float old_step; |
| 180 |
float new_step; |
| 181 |
float old_weight; |
| 182 |
float new_weight; |
| 183 |
float gain; |
| 184 |
int orig_len; |
| 185 |
|
| 186 |
orig_len = len; |
| 187 |
if (s->missing_samples == 0) { |
| 188 |
|
| 189 |
|
| 190 |
normalise_history(s); |
| 191 |
s->pitch = amdf_pitch(PLC_PITCH_MIN, PLC_PITCH_MAX, s->history + PLC_HISTORY_LEN - CORRELATION_SPAN - PLC_PITCH_MIN, CORRELATION_SPAN); |
| 192 |
|
| 193 |
pitch_overlap = s->pitch >> 2; |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
for (i = 0; i < s->pitch - pitch_overlap; i++) |
| 198 |
s->pitchbuf[i] = s->history[PLC_HISTORY_LEN - s->pitch + i]; |
| 199 |
|
| 200 |
new_step = 1.0/pitch_overlap; |
| 201 |
new_weight = new_step; |
| 202 |
for ( ; i < s->pitch; i++) { |
| 203 |
s->pitchbuf[i] = s->history[PLC_HISTORY_LEN - s->pitch + i] * (1.0 - new_weight) + s->history[PLC_HISTORY_LEN - 2 * s->pitch + i]*new_weight; |
| 204 |
new_weight += new_step; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
gain = 1.0; |
| 213 |
new_step = 1.0 / pitch_overlap; |
| 214 |
old_step = new_step; |
| 215 |
new_weight = new_step; |
| 216 |
old_weight = 1.0 - new_step; |
| 217 |
for (i = 0; i < pitch_overlap; i++) { |
| 218 |
amp[i] = fsaturate(old_weight * s->history[PLC_HISTORY_LEN - 1 - i] + new_weight * s->pitchbuf[i]); |
| 219 |
new_weight += new_step; |
| 220 |
old_weight -= old_step; |
| 221 |
if (old_weight < 0.0) |
| 222 |
old_weight = 0.0; |
| 223 |
} |
| 224 |
s->pitch_offset = i; |
| 225 |
} else { |
| 226 |
gain = 1.0 - s->missing_samples*ATTENUATION_INCREMENT; |
| 227 |
i = 0; |
| 228 |
} |
| 229 |
for ( ; gain > 0.0 && i < len; i++) { |
| 230 |
amp[i] = s->pitchbuf[s->pitch_offset] * gain; |
| 231 |
gain -= ATTENUATION_INCREMENT; |
| 232 |
if (++s->pitch_offset >= s->pitch) |
| 233 |
s->pitch_offset = 0; |
| 234 |
} |
| 235 |
for ( ; i < len; i++) |
| 236 |
amp[i] = 0; |
| 237 |
s->missing_samples += orig_len; |
| 238 |
save_history(s, amp, len); |
| 239 |
return len; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
plc_state_t *plc_init(plc_state_t *s) |
| 245 |
{ |
| 246 |
memset(s, 0, sizeof(*s)); |
| 247 |
return s; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
static int reload_module(void) |
| 253 |
{ |
| 254 |
struct ast_variable *var; |
| 255 |
struct ast_flags config_flags = { 0 }; |
| 256 |
struct ast_config *cfg = ast_config_load("codecs.conf", config_flags); |
| 257 |
|
| 258 |
if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID) { |
| 259 |
return 0; |
| 260 |
} |
| 261 |
|
| 262 |
for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) { |
| 263 |
if (!strcasecmp(var->name, "genericplc")) { |
| 264 |
ast_set2_flag64(&ast_options, ast_true(var->value), AST_OPT_FLAG_GENERIC_PLC); |
| 265 |
} else if (!strcasecmp(var->name, "genericplc_on_equal_codecs")) { |
| 266 |
ast_set2_flag64(&ast_options, ast_true(var->value), AST_OPT_FLAG_GENERIC_PLC_ON_EQUAL_CODECS); |
| 267 |
} |
| 268 |
} |
| 269 |
ast_config_destroy(cfg); |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
if (!ast_opt_generic_plc) { |
| 275 |
ast_set2_flag64(&ast_options, 0, AST_OPT_FLAG_GENERIC_PLC_ON_EQUAL_CODECS); |
| 276 |
} |
| 277 |
|
| 278 |
return 0; |
| 279 |
} |
| 280 |
|
| 281 |
static int load_module(void) |
| 282 |
{ |
| 283 |
reload_module(); |
| 284 |
|
| 285 |
return AST_MODULE_LOAD_SUCCESS; |
| 286 |
} |
| 287 |
|
| 288 |
static int unload_module(void) |
| 289 |
{ |
| 290 |
return 0; |
| 291 |
} |
| 292 |
|
| 293 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "PLC", |
| 294 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 295 |
.load = load_module, |
| 296 |
.unload = unload_module, |
| 297 |
.reload = reload_module, |
| 298 |
.load_pri = AST_MODPRI_CORE, |
| 299 |
.requires = "extconfig", |
| 300 |
); |