Bug Summary

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