Bug Summary

File:super_tone_rx.c
Warning:line 187, column 13
Array subscript is undefined

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;
19
'j' declared without an initial value
159
160 if (rotation >= 0)
20
Taking true branch
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 if (steps < 0)
21
Assuming 'steps' is >= 0
22
Taking false branch
166 {
167 /* A -ve value for steps indicates we just changed step, and need to
168 check the last one ended within spec. If we don't do this
169 extra test a low duration segment might be accepted as OK. */
170 steps = -steps;
171 j = (rotation + steps - 2)%steps;
172 if (pattern[j].f1 != test[8].f1 || pattern[j].f2 != test[8].f2)
173 return 0;
174 /*endif*/
175 if (pattern[j].min_duration > test[8].min_duration*SUPER_TONE_BINS128
176 ||
177 pattern[j].max_duration < test[8].min_duration*SUPER_TONE_BINS128)
178 {
179 return 0;
180 }
181 /*endif*/
182 }
183 /*endif*/
184 if (steps)
23
Taking false branch
185 j = (rotation + steps - 1)%steps;
186 /*endif*/
187 if (pattern[j].f1 != test[9].f1 || pattern[j].f2 != test[9].f2)
24
Array subscript is undefined
188 return 0;
189 /*endif*/
190 if (pattern[j].max_duration < test[9].min_duration*SUPER_TONE_BINS128)
191 return 0;
192 /*endif*/
193 }
194 else
195 {
196 /* Look for a complete template match. */
197 for (i = 0; i < steps; i++)
198 {
199 j = i + 10 - steps;
200 if (pattern[i].f1 != test[j].f1 || pattern[i].f2 != test[j].f2)
201 return 0;
202 /*endif*/
203 if (pattern[i].min_duration > test[j].min_duration*SUPER_TONE_BINS128
204 ||
205 pattern[i].max_duration < test[j].min_duration*SUPER_TONE_BINS128)
206 {
207 return 0;
208 }
209 /*endif*/
210 }
211 /*endfor*/
212 }
213 /*endif*/
214 return 1;
215}
216/*- End of function --------------------------------------------------------*/
217
218SPAN_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)
219{
220 if (desc == NULL((void*)0))
221 {
222 if ((desc = (super_tone_rx_descriptor_t *) span_alloc(sizeof(*desc))) == NULL((void*)0))
223 return NULL((void*)0);
224 /*endif*/
225 }
226 /*endif*/
227 desc->tone_list = NULL((void*)0);
228 desc->tone_segs = NULL((void*)0);
229
230 desc->used_frequencies = 0;
231 desc->monitored_frequencies = 0;
232 desc->desc = NULL((void*)0);
233 desc->tones = 0;
234 return desc;
235}
236/*- End of function --------------------------------------------------------*/
237
238SPAN_DECLARE(int)__attribute__((visibility("default"))) int super_tone_rx_free_descriptor(super_tone_rx_descriptor_t *desc)
239{
240 int i;
241
242 if (desc)
243 {
244 for (i = 0; i < desc->tones; i++)
245 {
246 if (desc->tone_list[i])
247 span_free(desc->tone_list[i]);
248 /*endif*/
249 }
250 /*endfor*/
251 if (desc->tone_list)
252 span_free(desc->tone_list);
253 /*endif*/
254 if (desc->tone_segs)
255 span_free(desc->tone_segs);
256 /*endif*/
257 if (desc->desc)
258 span_free(desc->desc);
259 /*endif*/
260 span_free(desc);
261 }
262 /*endif*/
263 return 0;
264}
265/*- End of function --------------------------------------------------------*/
266
267SPAN_DECLARE(void)__attribute__((visibility("default"))) void super_tone_rx_tone_callback(super_tone_rx_state_t *s,
268 span_tone_report_func_t callback,
269 void *user_data)
270{
271 s->tone_callback = callback;
272 s->callback_data = user_data;
273}
274/*- End of function --------------------------------------------------------*/
275
276static void super_tone_chunk(super_tone_rx_state_t *s)
277{
278 int i;
279 int j;
280 int k1;
281 int k2;
282#if defined(SPANDSP_USE_FIXED_POINT)
283 int32_t res[SUPER_TONE_BINS128/2];
284#else
285 float res[SUPER_TONE_BINS128/2];
286#endif
287
288 for (i = 0; i < s->desc->monitored_frequencies; i++)
13
Loop condition is false. Execution continues on line 292
289 res[i] = goertzel_result(&s->state[i]);
290 /*endfor*/
291 /* Find our two best monitored frequencies, which also have adequate energy. */
292 if (s->energy < DETECTION_THRESHOLD2104205.6f)
14
Taking true branch
293 {
294 k1 = -1;
295 k2 = -1;
296 }
297 else
298 {
299 if (res[0] > res[1])
300 {
301 k1 = 0;
302 k2 = 1;
303 }
304 else
305 {
306 k1 = 1;
307 k2 = 0;
308 }
309 /*endif*/
310 for (j = 2; j < s->desc->monitored_frequencies; j++)
311 {
312 if (res[j] >= res[k1])
313 {
314 k2 = k1;
315 k1 = j;
316 }
317 else if (res[j] >= res[k2])
318 {
319 k2 = j;
320 }
321 /*endif*/
322 }
323 /*endfor*/
324 if ((res[k1] + res[k2]) < TONE_TO_TOTAL_ENERGY1.995f*s->energy)
325 {
326 k1 = -1;
327 k2 = -1;
328 }
329 else if (res[k1] > TONE_TWIST3.981f*res[k2])
330 {
331 k2 = -1;
332 }
333 else if (k2 < k1)
334 {
335 j = k1;
336 k1 = k2;
337 k2 = j;
338 }
339 /*endif*/
340 }
341 /*endif*/
342 /* See if this differs from last time. */
343 if (k1 != s->segments[10].f1 || k2 != s->segments[10].f2)
15
Taking false branch
344 {
345 /* It is different, but this might just be a transitional quirk, or
346 a one shot hiccup (eg due to noise). Only if this same thing is
347 seen a second time should we change state. */
348 s->segments[10].f1 = k1;
349 s->segments[10].f2 = k2;
350 /* While things are hopping around, consider this a continuance of the
351 previous state. */
352 s->segments[9].min_duration++;
353 }
354 else
355 {
356 if (k1 != s->segments[9].f1 || k2 != s->segments[9].f2)
16
Taking false branch
357 {
358 if (s->detected_tone >= 0)
359 {
360 /* Test for the continuance of the existing tone pattern, based on our new knowledge of an
361 entire segment length. */
362 if (!test_cadence(s->desc->tone_list[s->detected_tone], -s->desc->tone_segs[s->detected_tone], s->segments, s->rotation++))
363 {
364 s->detected_tone = -1;
365 s->tone_callback(s->callback_data, s->detected_tone, -10, 0);
366 }
367 /*endif*/
368 }
369 /*endif*/
370 if (s->segment_callback)
371 {
372 s->segment_callback(s->callback_data,
373 s->segments[9].f1,
374 s->segments[9].f2,
375 s->segments[9].min_duration*SUPER_TONE_BINS128/8);
376 }
377 /*endif*/
378 memmove(&s->segments[0], &s->segments[1], 9*sizeof(s->segments[0]));
379 s->segments[9].f1 = k1;
380 s->segments[9].f2 = k2;
381 s->segments[9].min_duration = 1;
382 }
383 else
384 {
385 /* This is a continuance of the previous state */
386 if (s->detected_tone >= 0)
17
Taking true branch
387 {
388 /* Test for the continuance of the existing tone pattern. We must do this here, so we can sense the
389 discontinuance of the tone on an excessively long segment. */
390 if (!test_cadence(s->desc->tone_list[s->detected_tone], s->desc->tone_segs[s->detected_tone], s->segments, s->rotation))
18
Calling 'test_cadence'
391 {
392 s->detected_tone = -1;
393 s->tone_callback(s->callback_data, s->detected_tone, -10, 0);
394 }
395 /*endif*/
396 }
397 /*endif*/
398 s->segments[9].min_duration++;
399 }
400 /*endif*/
401 }
402 /*endif*/
403 if (s->detected_tone < 0)
404 {
405 /* Test for the start of any of the monitored tone patterns */
406 for (j = 0; j < s->desc->tones; j++)
407 {
408 if (test_cadence(s->desc->tone_list[j], s->desc->tone_segs[j], s->segments, -1))
409 {
410 s->detected_tone = j;
411 s->rotation = 0;
412 s->tone_callback(s->callback_data, s->detected_tone, -10, 0);
413 break;
414 }
415 /*endif*/
416 }
417 /*endfor*/
418 }
419 /*endif*/
420#if defined(SPANDSP_USE_FIXED_POINT)
421 s->energy = 0;
422#else
423 s->energy = 0.0f;
424#endif
425}
426/*- End of function --------------------------------------------------------*/
427
428SPAN_DECLARE(int)__attribute__((visibility("default"))) int super_tone_rx(super_tone_rx_state_t *s, const int16_t amp[], int samples)
429{
430 int i;
431 int x;
432 int sample;
433#if defined(SPANDSP_USE_FIXED_POINT)
434 int16_t xamp;
435#else
436 float xamp;
437#endif
438
439 x = 0;
440 for (sample = 0; sample < samples; sample += x)
1
Assuming 'sample' is < 'samples'
2
Loop condition is true. Entering loop body
8
Loop condition is true. Entering loop body
441 {
442 for (i = 0; i < s->desc->monitored_frequencies; i++)
3
Assuming the condition is false
4
Loop condition is false. Execution continues on line 445
9
Loop condition is false. Execution continues on line 445
443 x = goertzel_update(&s->state[i], amp + sample, samples - sample);
444 /*endfor*/
445 for (i = 0; i < x; i++)
5
Loop condition is false. Execution continues on line 455
10
Loop condition is false. Execution continues on line 455
446 {
447 xamp = goertzel_preadjust_amp(amp[sample + i])((float) amp[sample + i]);
448#if defined(SPANDSP_USE_FIXED_POINT)
449 s->energy += ((int32_t) xamp*xamp);
450#else
451 s->energy += xamp*xamp;
452#endif
453 }
454 /*endfor*/
455 if (s->state[0].current_sample >= SUPER_TONE_BINS128)
6
Assuming the condition is true
7
Taking true branch
11
Taking true branch
456 {
457 /* We have finished a Goertzel block. */
458 super_tone_chunk(s);
12
Calling 'super_tone_chunk'
459#if defined(SPANDSP_USE_FIXED_POINT)
460 s->energy = 0;
461#else
462 s->energy = 0.0f;
463#endif
464 }
465 /*endif*/
466 }
467 /*endfor*/
468 return samples;
469}
470/*- End of function --------------------------------------------------------*/
471
472SPAN_DECLARE(int)__attribute__((visibility("default"))) int super_tone_rx_fillin(super_tone_rx_state_t *s, int samples)
473{
474 /* TODO: Roll the detector forward without a state change */
475 return 0;
476}
477/*- End of function --------------------------------------------------------*/
478
479SPAN_DECLARE(void)__attribute__((visibility("default"))) void super_tone_rx_segment_callback(super_tone_rx_state_t *s,
480 tone_segment_func_t callback)
481{
482 s->segment_callback = callback;
483}
484/*- End of function --------------------------------------------------------*/
485
486SPAN_DECLARE(super_tone_rx_state_t *)__attribute__((visibility("default"))) super_tone_rx_state_t * super_tone_rx_init(super_tone_rx_state_t *s,
487 super_tone_rx_descriptor_t *desc,
488 span_tone_report_func_t callback,
489 void *user_data)
490{
491 int i;
492
493 if (desc == NULL((void*)0))
494 return NULL((void*)0);
495 /*endif*/
496 if (callback == NULL((void*)0))
497 return NULL((void*)0);
498 /*endif*/
499 if (s == NULL((void*)0))
500 {
501 if ((s = (super_tone_rx_state_t *) span_alloc(sizeof(*s) + desc->monitored_frequencies*sizeof(goertzel_state_t))) == NULL((void*)0))
502 return NULL((void*)0);
503 /*endif*/
504 }
505 /*endif*/
506
507 for (i = 0; i < 11; i++)
508 {
509 s->segments[i].f1 = -1;
510 s->segments[i].f2 = -1;
511 s->segments[i].min_duration = 0;
512 }
513 /*endfor*/
514 s->segment_callback = NULL((void*)0);
515 s->tone_callback = callback;
516 s->callback_data = user_data;
517 if (desc)
518 s->desc = desc;
519 /*endif*/
520 s->detected_tone = -1;
521#if defined(SPANDSP_USE_FIXED_POINT)
522 s->energy = 0;
523#else
524 s->energy = 0.0f;
525#endif
526 for (i = 0; i < desc->monitored_frequencies; i++)
527 goertzel_init(&s->state[i], &s->desc->desc[i]);
528 /*endfor*/
529 return s;
530}
531/*- End of function --------------------------------------------------------*/
532
533SPAN_DECLARE(int)__attribute__((visibility("default"))) int super_tone_rx_release(super_tone_rx_state_t *s)
534{
535 return 0;
536}
537/*- End of function --------------------------------------------------------*/
538
539SPAN_DECLARE(int)__attribute__((visibility("default"))) int super_tone_rx_free(super_tone_rx_state_t *s)
540{
541 if (s)
542 span_free(s);
543 /*endif*/
544 return 0;
545}
546/*- End of function --------------------------------------------------------*/
547/*- End of file ------------------------------------------------------------*/