Bug Summary

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