Bug Summary

File:src/super_tone_rx.c
Warning:line 317, column 24
The right operand of '>' is a garbage value

Annotated Source Code

1/*
2 * SpanDSP - a series of DSP components for telephony
3 *
4 * super_tone_rx.c - Flexible telephony supervisory tone detection.
5 *
6 * Written by Steve Underwood <steveu@coppice.org>
7 *
8 * Copyright (C) 2003 Steve Underwood
9 *
10 * All rights reserved.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 2.1,
14 * as published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26/*! \file */
27
28#if defined(HAVE_CONFIG_H1)
29#include "config.h"
30#endif
31
32#include <stdlib.h>
33#include <string.h>
34#include <stdio.h>
35#include <fcntl.h>
36#include <ctype.h>
37#include <time.h>
38#include <inttypes.h>
39#if defined(HAVE_TGMATH_H1)
40#include <tgmath.h>
41#endif
42#if defined(HAVE_MATH_H1)
43#include <math.h>
44#endif
45#include "floating_fudge.h"
46
47#include "spandsp/telephony.h"
48#include "spandsp/alloc.h"
49#include "spandsp/fast_convert.h"
50#include "spandsp/complex.h"
51#include "spandsp/vector_float.h"
52#include "spandsp/complex_vector_float.h"
53#include "spandsp/tone_detect.h"
54#include "spandsp/tone_generate.h"
55#include "spandsp/super_tone_rx.h"
56
57#include "spandsp/private/super_tone_rx.h"
58
59#if defined(SPANDSP_USE_FIXED_POINT)
60//static const int detection_threshold = energy_threshold_dbm0(SUPER_TONE_BINS, -42);
61static const int detection_threshold = 16439 /* -42dBm0
62//static const int tone_twist = 4;
63static const int tone_twist = 4; /* 6dB &/
64//static const int tone_to_total_energy = SUPER_TONE_BINS*64;
65static const int tone_to_total_energy = 64; /* -3dB */
66#else
67//static const float detection_threshold = energy_threshold_dbm0(SUPER_TONE_BINS, -42);
68static const float detection_threshold = 2104205.6f; /* -42dBm0 */
69//static const float tone_twist = db_to_power_ratio(6.0f);
70static const float tone_twist = 3.981f; /* 6dB */
71//static const float tone_to_total_energy = SUPER_TONE_BINS*db_to_power_ratio(-3.0f);
72static const float tone_to_total_energy = 1.995f; /* 3dB */
73#endif
74
75static int add_super_tone_freq(super_tone_rx_descriptor_t *desc, int freq)
76{
77 int i;
78
79 if (freq == 0)
80 return -1;
81 /*endif*/
82 /* Look for an existing frequency */
83 for (i = 0; i < desc->used_frequencies; i++)
84 {
85 if (desc->pitches[i][0] == freq)
86 return desc->pitches[i][1];
87 /*endif*/
88 }
89 /*endfor*/
90 /* Look for an existing tone which is very close. We may need to merge
91 the detectors. */
92 for (i = 0; i < desc->used_frequencies; i++)
93 {
94 if ((desc->pitches[i][0] - 10) <= freq && freq <= (desc->pitches[i][0] + 10))
95 {
96 /* Merge these two */
97 desc->pitches[desc->used_frequencies][0] = freq;
98 desc->pitches[desc->used_frequencies][1] = i;
99 make_goertzel_descriptor(&desc->desc[desc->pitches[i][1]], (float) (freq + desc->pitches[i][0])/2, SUPER_TONE_BINS128);
100 desc->used_frequencies++;
101 return desc->pitches[i][1];
102 }
103 /*endif*/
104 }
105 /*endfor*/
106 desc->pitches[i][0] = freq;
107 desc->pitches[i][1] = desc->monitored_frequencies;
108 if (desc->monitored_frequencies%5 == 0)
109 {
110 desc->desc = (goertzel_descriptor_t *) span_realloc(desc->desc, (desc->monitored_frequencies + 5)*sizeof(goertzel_descriptor_t));
111 }
112 /*endif*/
113 make_goertzel_descriptor(&desc->desc[desc->monitored_frequencies++], (float) freq, SUPER_TONE_BINS128);
114 desc->used_frequencies++;
115 return desc->pitches[i][1];
116}
117/*- End of function --------------------------------------------------------*/
118
119SPAN_DECLARE(int)__attribute__((visibility("default"))) int super_tone_rx_add_tone(super_tone_rx_descriptor_t *desc)
120{
121 if (desc->tones%5 == 0)
122 {
123 desc->tone_list = (super_tone_rx_segment_t **) span_realloc(desc->tone_list, (desc->tones + 5)*sizeof(super_tone_rx_segment_t *));
124 desc->tone_segs = (int *) span_realloc(desc->tone_segs, (desc->tones + 5)*sizeof(int));
125 }
126 /*endif*/
127 desc->tone_list[desc->tones] = NULL((void*)0);
128 desc->tone_segs[desc->tones] = 0;
129 desc->tones++;
130 return desc->tones - 1;
131}
132/*- End of function --------------------------------------------------------*/
133
134SPAN_DECLARE(int)__attribute__((visibility("default"))) int super_tone_rx_add_element(super_tone_rx_descriptor_t *desc,
135 int tone,
136 int f1,
137 int f2,
138 int min,
139 int max)
140{
141 int step;
142
143 step = desc->tone_segs[tone];
144 if (step%5 == 0)
145 {
146 desc->tone_list[tone] = (super_tone_rx_segment_t *) span_realloc(desc->tone_list[tone], (step + 5)*sizeof(super_tone_rx_segment_t));
147 }
148 /*endif*/
149 desc->tone_list[tone][step].f1 = add_super_tone_freq(desc, f1);
150 desc->tone_list[tone][step].f2 = add_super_tone_freq(desc, f2);
151 desc->tone_list[tone][step].min_duration = min*8;
152 desc->tone_list[tone][step].max_duration = (max == 0) ? 0x7FFFFFFF : max*8;
153 desc->tone_segs[tone]++;
154 return step;
155}
156/*- End of function --------------------------------------------------------*/
157
158static int test_cadence(super_tone_rx_segment_t *pattern,
159 int steps,
160 super_tone_rx_segment_t *test,
161 int rotation)
162{
163 int i;
164 int j;
165
166 if (rotation >= 0)
167 {
168 /* Check only for the sustaining of a tone in progress. This means
169 we only need to check each block if the latest step is compatible
170 with the tone template. */
171 j = 0;
172 if (steps < 0)
173 {
174 /* A -ve value for steps indicates we just changed step, and need to
175 check the last one ended within spec. If we don't do this
176 extra test a low duration segment might be accepted as OK. */
177 steps = -steps;
178 j = (rotation + steps - 2)%steps;
179 if (pattern[j].f1 != test[8].f1 || pattern[j].f2 != test[8].f2)
180 return 0;
181 /*endif*/
182 if (pattern[j].min_duration > test[8].min_duration*SUPER_TONE_BINS128
183 ||
184 pattern[j].max_duration < test[8].min_duration*SUPER_TONE_BINS128)
185 {
186 return 0;
187 }
188 /*endif*/
189 }
190 /*endif*/
191 if (steps)
192 j = (rotation + steps - 1)%steps;
193 /*endif*/
194 if (pattern[j].f1 != test[9].f1 || pattern[j].f2 != test[9].f2)
195 return 0;
196 /*endif*/
197 if (pattern[j].max_duration < test[9].min_duration*SUPER_TONE_BINS128)
198 return 0;
199 /*endif*/
200 }
201 else
202 {
203 /* Look for a complete template match. */
204 for (i = 0; i < steps; i++)
205 {
206 j = i + 10 - steps;
207 if (pattern[i].f1 != test[j].f1 || pattern[i].f2 != test[j].f2)
208 return 0;
209 /*endif*/
210 if (pattern[i].min_duration > test[j].min_duration*SUPER_TONE_BINS128
211 ||
212 pattern[i].max_duration < test[j].min_duration*SUPER_TONE_BINS128)
213 {
214 return 0;
215 }
216 /*endif*/
217 }
218 /*endfor*/
219 }
220 /*endif*/
221 return 1;
222}
223/*- End of function --------------------------------------------------------*/
224
225SPAN_DECLARE(super_tone_rx_descriptor_t *)__attribute__((visibility("default"))) super_tone_rx_descriptor_t
*
super_tone_rx_make_descriptor(super_tone_rx_descriptor_t *desc)
226{
227 if (desc == NULL((void*)0))
228 {
229 if ((desc = (super_tone_rx_descriptor_t *) span_alloc(sizeof(*desc))) == NULL((void*)0))
230 return NULL((void*)0);
231 /*endif*/
232 }
233 /*endif*/
234 desc->tone_list = NULL((void*)0);
235 desc->tone_segs = NULL((void*)0);
236
237 desc->used_frequencies = 0;
238 desc->monitored_frequencies = 0;
239 desc->desc = NULL((void*)0);
240 desc->tones = 0;
241 return desc;
242}
243/*- End of function --------------------------------------------------------*/
244
245SPAN_DECLARE(int)__attribute__((visibility("default"))) int super_tone_rx_free_descriptor(super_tone_rx_descriptor_t *desc)
246{
247 int i;
248
249 if (desc)
250 {
251 for (i = 0; i < desc->tones; i++)
252 {
253 if (desc->tone_list[i])
254 span_free(desc->tone_list[i]);
255 /*endif*/
256 }
257 /*endfor*/
258 if (desc->tone_list)
259 span_free(desc->tone_list);
260 /*endif*/
261 if (desc->tone_segs)
262 span_free(desc->tone_segs);
263 /*endif*/
264 if (desc->desc)
265 span_free(desc->desc);
266 /*endif*/
267 span_free(desc);
268 }
269 /*endif*/
270 return 0;
271}
272/*- End of function --------------------------------------------------------*/
273
274SPAN_DECLARE(void)__attribute__((visibility("default"))) void super_tone_rx_tone_callback(super_tone_rx_state_t *s,
275 span_tone_report_func_t callback,
276 void *user_data)
277{
278 s->tone_callback = callback;
279 s->callback_data = user_data;
280}
281/*- End of function --------------------------------------------------------*/
282
283static void super_tone_chunk(super_tone_rx_state_t *s)
284{
285 int i;
286 int j;
287 int k1;
288 int k2;
289#if defined(SPANDSP_USE_FIXED_POINT)
290 int32_t res[SUPER_TONE_BINS128/2];
291#else
292 float res[SUPER_TONE_BINS128/2];
293#endif
294
295 if (s->energy < detection_threshold)
1
Taking false branch
296 {
297 /* The total energy is too low to be considered a tone detection. */
298 k1 = -1;
299 k2 = -1;
300 for (i = 0; i < s->desc->monitored_frequencies; i++)
301 goertzel_reset(&s->state[i]);
302 /*endfor*/
303 }
304 else
305 {
306 if (s->desc->monitored_frequencies < 2)
2
Assuming the condition is false
3
Taking false branch
307 {
308 k1 =
309 k2 = 0;
310 }
311 else
312 {
313 /* Find our two best monitored frequencies, which also have adequate energy. */
314 for (i = 0; i < s->desc->monitored_frequencies; i++)
4
Loop condition is true. Entering loop body
5
Assuming the condition is false
6
Loop condition is false. Execution continues on line 317
315 res[i] = goertzel_result(&s->state[i]);
316 /*endfor*/
317 if (res[0] > res[1])
7
The right operand of '>' is a garbage value
318 {
319 k1 = 0;
320 k2 = 1;
321 }
322 else
323 {
324 k1 = 1;
325 k2 = 0;
326 }
327 /*endif*/
328 for (j = 2; j < s->desc->monitored_frequencies; j++)
329 {
330 if (res[j] >= res[k1])
331 {
332 k2 = k1;
333 k1 = j;
334 }
335 else if (res[j] >= res[k2])
336 {
337 k2 = j;
338 }
339 /*endif*/
340 }
341 /*endfor*/
342 if ((res[k1] + res[k2]) < tone_to_total_energy*s->energy)
343 {
344 k1 = -1;
345 k2 = -1;
346 }
347 else if (res[k1] > tone_twist*res[k2])
348 {
349 k2 = -1;
350 }
351 else if (k2 < k1)
352 {
353 j = k1;
354 k1 = k2;
355 k2 = j;
356 }
357 /*endif*/
358 }
359 /*endif*/
360 }
361 /*endif*/
362 /* See if this differs from last time. */
363 if (k1 != s->segments[10].f1 || k2 != s->segments[10].f2)
364 {
365 /* It is different, but this might just be a transitional quirk, or
366 a one shot hiccup (eg due to noise). Only if this same thing is
367 seen a second time should we change state. */
368 s->segments[10].f1 = k1;
369 s->segments[10].f2 = k2;
370 /* While things are hopping around, consider this a continuance of the
371 previous state. */
372 s->segments[9].min_duration++;
373 }
374 else
375 {
376 if (k1 != s->segments[9].f1 || k2 != s->segments[9].f2)
377 {
378 if (s->detected_tone >= 0)
379 {
380 /* Test for the continuance of the existing tone pattern, based on our new knowledge of an
381 entire segment length. */
382 if (!test_cadence(s->desc->tone_list[s->detected_tone], -s->desc->tone_segs[s->detected_tone], s->segments, s->rotation++))
383 {
384 s->detected_tone = -1;
385 s->tone_callback(s->callback_data, s->detected_tone, -10, 0);
386 }
387 /*endif*/
388 }
389 /*endif*/
390 if (s->segment_callback)
391 {
392 s->segment_callback(s->callback_data,
393 s->segments[9].f1,
394 s->segments[9].f2,
395 s->segments[9].min_duration*SUPER_TONE_BINS128/8);
396 }
397 /*endif*/
398 memmove(&s->segments[0], &s->segments[1], 9*sizeof(s->segments[0]));
399 s->segments[9].f1 = k1;
400 s->segments[9].f2 = k2;
401 s->segments[9].min_duration = 1;
402 }
403 else
404 {
405 /* This is a continuance of the previous state */
406 if (s->detected_tone >= 0)
407 {
408 /* Test for the continuance of the existing tone pattern. We must do this here, so we can sense the
409 discontinuance of the tone on an excessively long segment. */
410 if (!test_cadence(s->desc->tone_list[s->detected_tone], s->desc->tone_segs[s->detected_tone], s->segments, s->rotation))
411 {
412 s->detected_tone = -1;
413 s->tone_callback(s->callback_data, s->detected_tone, -10, 0);
414 }
415 /*endif*/
416 }
417 /*endif*/
418 s->segments[9].min_duration++;
419 }
420 /*endif*/
421 }
422 /*endif*/
423 if (s->detected_tone < 0)
424 {
425 /* Test for the start of any of the monitored tone patterns */
426 for (j = 0; j < s->desc->tones; j++)
427 {
428 if (test_cadence(s->desc->tone_list[j], s->desc->tone_segs[j], s->segments, -1))
429 {
430 s->detected_tone = j;
431 s->rotation = 0;
432 s->tone_callback(s->callback_data, s->detected_tone, -10, 0);
433 break;
434 }
435 /*endif*/
436 }
437 /*endfor*/
438 }
439 /*endif*/
440#if defined(SPANDSP_USE_FIXED_POINT)
441 s->energy = 0;
442#else
443 s->energy = 0.0f;
444#endif
445}
446/*- End of function --------------------------------------------------------*/
447
448SPAN_DECLARE(int)__attribute__((visibility("default"))) int super_tone_rx(super_tone_rx_state_t *s, const int16_t amp[], int samples)
449{
450 int i;
451 int x;
452 int sample;
453#if defined(SPANDSP_USE_FIXED_POINT)
454 int16_t xamp;
455#else
456 float xamp;
457#endif
458
459 x = 0;
460 for (sample = 0; sample < samples; sample += x)
461 {
462 for (i = 0; i < s->desc->monitored_frequencies; i++)
463 x = goertzel_update(&s->state[i], &amp[sample], samples - sample);
464 /*endfor*/
465 for (i = 0; i < x; i++)
466 {
467 xamp = goertzel_preadjust_amp(amp[sample + i])((float) amp[sample + i]);
468#if defined(SPANDSP_USE_FIXED_POINT)
469 s->energy += ((int32_t) xamp*xamp);
470#else
471 s->energy += xamp*xamp;
472#endif
473 }
474 /*endfor*/
475 if (s->state[0].current_sample >= SUPER_TONE_BINS128)
476 {
477 /* We have finished a Goertzel block. */
478 super_tone_chunk(s);
479 }
480 /*endif*/
481 }
482 /*endfor*/
483 return samples;
484}
485/*- End of function --------------------------------------------------------*/
486
487SPAN_DECLARE(int)__attribute__((visibility("default"))) int super_tone_rx_fillin(super_tone_rx_state_t *s, int samples)
488{
489 /* TODO: Roll the detector forward without a state change */
490 return 0;
491}
492/*- End of function --------------------------------------------------------*/
493
494SPAN_DECLARE(void)__attribute__((visibility("default"))) void super_tone_rx_segment_callback(super_tone_rx_state_t *s,
495 tone_segment_func_t callback)
496{
497 s->segment_callback = callback;
498}
499/*- End of function --------------------------------------------------------*/
500
501SPAN_DECLARE(super_tone_rx_state_t *)__attribute__((visibility("default"))) super_tone_rx_state_t * super_tone_rx_init(super_tone_rx_state_t *s,
502 super_tone_rx_descriptor_t *desc,
503 span_tone_report_func_t callback,
504 void *user_data)
505{
506 int i;
507
508 if (desc == NULL((void*)0))
509 return NULL((void*)0);
510 /*endif*/
511 if (callback == NULL((void*)0))
512 return NULL((void*)0);
513 /*endif*/
514 if (s == NULL((void*)0))
515 {
516 if ((s = (super_tone_rx_state_t *) span_alloc(sizeof(*s) + desc->monitored_frequencies*sizeof(goertzel_state_t))) == NULL((void*)0))
517 return NULL((void*)0);
518 /*endif*/
519 }
520 /*endif*/
521
522 for (i = 0; i < 11; i++)
523 {
524 s->segments[i].f1 = -1;
525 s->segments[i].f2 = -1;
526 s->segments[i].min_duration = 0;
527 }
528 /*endfor*/
529 s->segment_callback = NULL((void*)0);
530 s->tone_callback = callback;
531 s->callback_data = user_data;
532 if (desc)
533 s->desc = desc;
534 /*endif*/
535 s->detected_tone = -1;
536#if defined(SPANDSP_USE_FIXED_POINT)
537 s->energy = 0;
538#else
539 s->energy = 0.0f;
540#endif
541 for (i = 0; i < desc->monitored_frequencies; i++)
542 goertzel_init(&s->state[i], &s->desc->desc[i]);
543 /*endfor*/
544 return s;
545}
546/*- End of function --------------------------------------------------------*/
547
548SPAN_DECLARE(int)__attribute__((visibility("default"))) int super_tone_rx_release(super_tone_rx_state_t *s)
549{
550 return 0;
551}
552/*- End of function --------------------------------------------------------*/
553
554SPAN_DECLARE(int)__attribute__((visibility("default"))) int super_tone_rx_free(super_tone_rx_state_t *s)
555{
556 if (s)
557 span_free(s);
558 /*endif*/
559 return 0;
560}
561/*- End of function --------------------------------------------------------*/
562/*- End of file ------------------------------------------------------------*/