Review Board 1.7.16


dsp.c ast_dsp_call_progress use local short variable in loop, plus other cleanup

Review Request #2107 - Created Sept. 12, 2012 and submitted

Alec Davis
trunk
Reviewers
asterisk-dev
Asterisk
janitor cleanup. No functional change.

1). ast_dsp_call_progress: use 'short samp' instead of s[x] inside loop.
    apply same casting as other _init, dsp->energy = (int32_t) samp * (int32_t) samp

2). ast_dtmf_detect_init: move repeated setting of s->energy to outside of loop.
    do goertzel_init loop first before setting s->lasthit and s->current_hit, consistant with ast_dsp_digitreset()

3). ast_mf_detect_init:
    do goertzel_init loop first before setting s->hits[] and s->current_hit, consistant with ast_dsp_digitreset()

4). Don't chain init different variables, as the type may change
compiles.

Changes between revision 2 and 3

1 2 3
1 2 3

  1. branches/1.8/main/dsp.c: Loading...
  2. trunk/main/dsp.c: Loading...
branches/1.8/main/dsp.c
Diff Revision 2 Diff Revision 3 - File Reverted
[20] 477 lines
[+20] [+] static void ast_tone_detect_init(tone_detect_state_t *s, int freq, int duration, int amp)
478
{
478
{
479
	ast_tone_detect_init(&s->cng_tone_state, FAX_TONE_CNG_FREQ, FAX_TONE_CNG_DURATION, FAX_TONE_CNG_DB);
479
	ast_tone_detect_init(&s->cng_tone_state, FAX_TONE_CNG_FREQ, FAX_TONE_CNG_DURATION, FAX_TONE_CNG_DB);
480
	ast_tone_detect_init(&s->ced_tone_state, FAX_TONE_CED_FREQ, FAX_TONE_CED_DURATION, FAX_TONE_CED_DB);
480
	ast_tone_detect_init(&s->ced_tone_state, FAX_TONE_CED_FREQ, FAX_TONE_CED_DURATION, FAX_TONE_CED_DB);
481
}
481
}
482

    
   
482

   
483
static void ast_dtmf_detect_init(dtmf_detect_state_t *s)
483
static void ast_dtmf_detect_init (dtmf_detect_state_t *s)
484
{
484
{
485
	int i;
485
	int i;
486

    
   
486

   
487
	for (i = 0; i < 4; i++) {
487
	s->lasthit = 0;

    
   
488
	s->current_hit = 0;

    
   
489
	for (i = 0;  i < 4;  i++) {
488
		goertzel_init(&s->row_out[i], dtmf_row[i], DTMF_GSIZE);
490
		goertzel_init(&s->row_out[i], dtmf_row[i], DTMF_GSIZE);
489
		goertzel_init(&s->col_out[i], dtmf_col[i], DTMF_GSIZE);
491
		goertzel_init(&s->col_out[i], dtmf_col[i], DTMF_GSIZE);
490
	}

   
491
	s->lasthit = s->current_hit = 0;

   
492
	s->energy = 0.0;
492
		s->energy = 0.0;

    
   
493
	}
493
	s->current_sample = 0;
494
	s->current_sample = 0;
494
	s->hits = 0;
495
	s->hits = 0;
495
	s->misses = 0;
496
	s->misses = 0;
496

    
   
497

   
497
	s->hits_to_begin = DTMF_HITS_TO_BEGIN;
498
	s->hits_to_begin = DTMF_HITS_TO_BEGIN;
498
	s->misses_to_end = DTMF_MISSES_TO_END;
499
	s->misses_to_end = DTMF_MISSES_TO_END;
499
}
500
}
500

    
   
501

   
501
static void ast_mf_detect_init(mf_detect_state_t *s)
502
static void ast_mf_detect_init (mf_detect_state_t *s)
502
{
503
{
503
	int i;
504
	int i;
504

    
   
505
	s->hits[0] = s->hits[1] = s->hits[2] = s->hits[3] = s->hits[4] = 0;
505
	for (i = 0; i < 6; i++) {
506
	for (i = 0;  i < 6;  i++) {
506
		goertzel_init(&s->tone_out[i], mf_tones[i], MF_GSIZE);
507
		goertzel_init (&s->tone_out[i], mf_tones[i], MF_GSIZE);
507
	}
508
	}
508
	s->hits[4] = s->hits[3] = s->hits[2] = s->hits[1] = s->hits[0] = s->current_hit = 0;

   
509
	s->current_sample = 0;
509
	s->current_sample = 0;

    
   
510
	s->current_hit = 0;
510
}
511
}
511

    
   
512

   
512
static void ast_digit_detect_init(digit_detect_state_t *s, int mf)
513
static void ast_digit_detect_init(digit_detect_state_t *s, int mf)
513
{
514
{
514
	s->current_digits = 0;
515
	s->current_digits = 0;
[+20] [20] 23 lines
[+20] [+] static int tone_detect(struct ast_dsp *dsp, tone_detect_state_t *s, int16_t *amp, int samples)
538
	if (s->squelch && s->mute_samples > 0) {
539
	if (s->squelch && s->mute_samples > 0) {
539
		mute.end = (s->mute_samples < samples) ? s->mute_samples : samples;
540
		mute.end = (s->mute_samples < samples) ? s->mute_samples : samples;
540
		s->mute_samples -= mute.end;
541
		s->mute_samples -= mute.end;
541
	}
542
	}
542

    
   
543

   
543
	for (start = 0; start < samples; start = end) {
544
	for (start = 0;  start < samples;  start = end) {
544
		/* Process in blocks. */
545
		/* Process in blocks. */
545
		limit = samples - start;
546
		limit = samples - start;
546
		if (limit > s->samples_pending) {
547
		if (limit > s->samples_pending) {
547
			limit = s->samples_pending;
548
			limit = s->samples_pending;
548
		}
549
		}
[+20] [20] 143 lines
[+20] [+] static int dtmf_detect(struct ast_dsp *dsp, digit_detect_state_t *s, int16_t amp[], int samples, int squelch, int relax)
692
		/* We are at the end of a DTMF detection block */
693
		/* We are at the end of a DTMF detection block */
693
		/* Find the peak row and the peak column */
694
		/* Find the peak row and the peak column */
694
		row_energy[0] = goertzel_result (&s->td.dtmf.row_out[0]);
695
		row_energy[0] = goertzel_result (&s->td.dtmf.row_out[0]);
695
		col_energy[0] = goertzel_result (&s->td.dtmf.col_out[0]);
696
		col_energy[0] = goertzel_result (&s->td.dtmf.col_out[0]);
696

    
   
697

   
697
		for (best_row = best_col = 0, i = 1; i < 4; i++) {
698
		for (best_row = best_col = 0, i = 1;  i < 4;  i++) {
698
			row_energy[i] = goertzel_result (&s->td.dtmf.row_out[i]);
699
			row_energy[i] = goertzel_result (&s->td.dtmf.row_out[i]);
699
			if (row_energy[i] > row_energy[best_row]) {
700
			if (row_energy[i] > row_energy[best_row]) {
700
				best_row = i;
701
				best_row = i;
701
			}
702
			}
702
			col_energy[i] = goertzel_result (&s->td.dtmf.col_out[i]);
703
			col_energy[i] = goertzel_result (&s->td.dtmf.col_out[i]);
[+20] [20] 6 lines
[+20] static int dtmf_detect(struct ast_dsp *dsp, digit_detect_state_t *s, int16_t amp[], int samples, int squelch, int relax)
709
		if (row_energy[best_row] >= DTMF_THRESHOLD && 
710
		if (row_energy[best_row] >= DTMF_THRESHOLD && 
710
		    col_energy[best_col] >= DTMF_THRESHOLD &&
711
		    col_energy[best_col] >= DTMF_THRESHOLD &&
711
		    col_energy[best_col] < row_energy[best_row] * DTMF_REVERSE_TWIST &&
712
		    col_energy[best_col] < row_energy[best_row] * DTMF_REVERSE_TWIST &&
712
		    col_energy[best_col] * DTMF_NORMAL_TWIST > row_energy[best_row]) {
713
		    col_energy[best_col] * DTMF_NORMAL_TWIST > row_energy[best_row]) {
713
			/* Relative peak test */
714
			/* Relative peak test */
714
			for (i = 0; i < 4; i++) {
715
			for (i = 0;  i < 4;  i++) {
715
				if ((i != best_col &&
716
				if ((i != best_col &&
716
				    col_energy[i] * DTMF_RELATIVE_PEAK_COL > col_energy[best_col]) ||
717
				    col_energy[i] * DTMF_RELATIVE_PEAK_COL > col_energy[best_col]) ||
717
				    (i != best_row 
718
				    (i != best_row 
718
				     && row_energy[i] * DTMF_RELATIVE_PEAK_ROW > row_energy[best_row])) {
719
				     && row_energy[i] * DTMF_RELATIVE_PEAK_ROW > row_energy[best_row])) {
719
					break;
720
					break;
[+20] [20] 141 lines
[+20] [+] static int mf_detect(struct ast_dsp *dsp, digit_detect_state_t *s, int16_t amp[],
861
		mute.end = (s->td.mf.mute_samples < samples) ? s->td.mf.mute_samples : samples;
862
		mute.end = (s->td.mf.mute_samples < samples) ? s->td.mf.mute_samples : samples;
862
		s->td.mf.mute_samples -= mute.end;
863
		s->td.mf.mute_samples -= mute.end;
863
	}
864
	}
864

    
   
865

   
865
	hit = 0;
866
	hit = 0;
866
	for (sample = 0; sample < samples; sample = limit) {
867
	for (sample = 0;  sample < samples;  sample = limit) {
867
		/* 80 is optimised to meet the MF specs. */
868
		/* 80 is optimised to meet the MF specs. */
868
		/* XXX So then why is MF_GSIZE defined as 120? */
869
		/* XXX So then why is MF_GSIZE defined as 120? */
869
		if ((samples - sample) >= (MF_GSIZE - s->td.mf.current_sample)) {
870
		if ((samples - sample) >= (MF_GSIZE - s->td.mf.current_sample)) {
870
			limit = sample + (MF_GSIZE - s->td.mf.current_sample);
871
			limit = sample + (MF_GSIZE - s->td.mf.current_sample);
871
		} else {
872
		} else {
872
			limit = samples;
873
			limit = samples;
873
		}
874
		}
874
		/* The following unrolled loop takes only 35% (rough estimate) of the 
875
		/* The following unrolled loop takes only 35% (rough estimate) of the 
875
		   time of a rolled loop on the machine on which it was developed */
876
		   time of a rolled loop on the machine on which it was developed */
876
		for (j = sample; j < limit; j++) {
877
		for (j = sample;  j < limit;  j++) {
877
			/* With GCC 2.95, the following unrolled code seems to take about 35%
878
			/* With GCC 2.95, the following unrolled code seems to take about 35%
878
			   (rough estimate) as long as a neat little 0-3 loop */
879
			   (rough estimate) as long as a neat little 0-3 loop */
879
			samp = amp[j];
880
			samp = amp[j];
880
			goertzel_sample(s->td.mf.tone_out, samp);
881
			goertzel_sample(s->td.mf.tone_out, samp);
881
			goertzel_sample(s->td.mf.tone_out + 1, samp);
882
			goertzel_sample(s->td.mf.tone_out + 1, samp);
[+20] [20] 93 lines
[+20] static int mf_detect(struct ast_dsp *dsp, digit_detect_state_t *s, int16_t amp[],
975
			}
976
			}
976
			mute.end = limit + MF_GSIZE;
977
			mute.end = limit + MF_GSIZE;
977
		}
978
		}
978

    
   
979

   
979
		/* Reinitialise the detector for the next block */
980
		/* Reinitialise the detector for the next block */
980
		for (i = 0; i < 6; i++)
981
		for (i = 0;  i < 6;  i++)
981
			goertzel_reset(&s->td.mf.tone_out[i]);
982
			goertzel_reset(&s->td.mf.tone_out[i]);
982
		s->td.mf.current_sample = 0;
983
		s->td.mf.current_sample = 0;
983
	}
984
	}
984

    
   
985

   
985
	if (squelch && mute.end) {
986
	if (squelch && mute.end) {
[+20] [20] 30 lines
[+20] [+] static inline int pair_there(float p1, float p2, float i1, float i2, float e)
1016
	return 1;
1017
	return 1;
1017
}
1018
}
1018

    
   
1019

   
1019
static int __ast_dsp_call_progress(struct ast_dsp *dsp, short *s, int len)
1020
static int __ast_dsp_call_progress(struct ast_dsp *dsp, short *s, int len)
1020
{
1021
{
1021
	short samp;

   
1022
	int x;
1022
	int x;
1023
	int y;
1023
	int y;
1024
	int pass;
1024
	int pass;
1025
	int newstate = DSP_TONE_STATE_SILENCE;
1025
	int newstate = DSP_TONE_STATE_SILENCE;
1026
	int res = 0;
1026
	int res = 0;
1027
	while (len) {
1027
	while (len) {
1028
		/* Take the lesser of the number of samples we need and what we have */
1028
		/* Take the lesser of the number of samples we need and what we have */
1029
		pass = len;
1029
		pass = len;
1030
		if (pass > dsp->gsamp_size - dsp->gsamps) {
1030
		if (pass > dsp->gsamp_size - dsp->gsamps) {
1031
			pass = dsp->gsamp_size - dsp->gsamps;
1031
			pass = dsp->gsamp_size - dsp->gsamps;
1032
		}
1032
		}
1033
		for (x = 0; x < pass; x++) {
1033
		for (x = 0; x < pass; x++) {
1034
			samp = s[x];

   
1035
			dsp->genergy += (int32_t) samp * (int32_t) samp;

   
1036
			for (y = 0; y < dsp->freqcount; y++) {
1034
			for (y = 0; y < dsp->freqcount; y++) {
1037
				goertzel_sample(&dsp->freqs[y], samp);
1035
				goertzel_sample(&dsp->freqs[y], s[x]);
1038
			}
1036
			}

    
   
1037
			dsp->genergy += s[x] * s[x];
1039
		}
1038
		}
1040
		s += pass;
1039
		s += pass;
1041
		dsp->gsamps += pass;
1040
		dsp->gsamps += pass;
1042
		len -= pass;
1041
		len -= pass;
1043
		if (dsp->gsamps == dsp->gsamp_size) {
1042
		if (dsp->gsamps == dsp->gsamp_size) {
[+20] [20] 597 lines
[+20] [+] void ast_dsp_digitreset(struct ast_dsp *dsp)
1641
	
1640
	
1642
	dsp->dtmf_began = 0;
1641
	dsp->dtmf_began = 0;
1643
	if (dsp->digitmode & DSP_DIGITMODE_MF) {
1642
	if (dsp->digitmode & DSP_DIGITMODE_MF) {
1644
		mf_detect_state_t *s = &dsp->digit_state.td.mf;
1643
		mf_detect_state_t *s = &dsp->digit_state.td.mf;
1645
		/* Reinitialise the detector for the next block */
1644
		/* Reinitialise the detector for the next block */
1646
		for (i = 0; i < 6; i++) {
1645
		for (i = 0;  i < 6;  i++) {
1647
			goertzel_reset(&s->tone_out[i]);
1646
			goertzel_reset(&s->tone_out[i]);
1648
		}
1647
		}
1649
		s->hits[4] = s->hits[3] = s->hits[2] = s->hits[1] = s->hits[0] = s->current_hit = 0;
1648
		s->hits[4] = s->hits[3] = s->hits[2] = s->hits[1] = s->hits[0] = s->current_hit = 0;
1650
		s->current_sample = 0;
1649
		s->current_sample = 0;
1651
	} else {
1650
	} else {
1652
		dtmf_detect_state_t *s = &dsp->digit_state.td.dtmf;
1651
		dtmf_detect_state_t *s = &dsp->digit_state.td.dtmf;
1653
		/* Reinitialise the detector for the next block */
1652
		/* Reinitialise the detector for the next block */
1654
		for (i = 0; i < 4; i++) {
1653
		for (i = 0;  i < 4;  i++) {
1655
			goertzel_reset(&s->row_out[i]);
1654
			goertzel_reset(&s->row_out[i]);
1656
			goertzel_reset(&s->col_out[i]);
1655
			goertzel_reset(&s->col_out[i]);
1657
		}
1656
		}
1658
		s->lasthit = s->current_hit = 0;
1657
		s->lasthit = s->current_hit = 0;
1659
		s->energy = 0.0;
1658
		s->energy = 0.0;
[+20] [20] 123 lines
trunk/main/dsp.c
Diff Revision 2 Diff Revision 3
 
  1. branches/1.8/main/dsp.c: Loading...
  2. trunk/main/dsp.c: Loading...

https://reviewboard.asterisk.org/ runs on a server provided by Digium, Inc. and uses bandwidth donated to the open source Asterisk community by API Digital Communications in Huntsville, AL USA.
Please report problems with this site to asteriskteam@digium.com.