]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
More fixes from Coverity scans
authorSteve Underwood <steveu@coppice.org>
Wed, 30 Apr 2014 18:56:53 +0000 (02:56 +0800)
committerSteve Underwood <steveu@coppice.org>
Wed, 30 Apr 2014 18:56:53 +0000 (02:56 +0800)
libs/spandsp/src/dds_float.c
libs/spandsp/src/spandsp/dds.h
libs/spandsp/src/spandsp/g711.h

index 4acaa1eaf96035d0f51b63948971e31a35aa53e8..45e5411f9c95a465fb30d927fd57c762b2eeea18 100644 (file)
@@ -2100,6 +2100,12 @@ static const float sine_table[SINELEN] =
     -0.00306796f
 };
 
+SPAN_DECLARE(float) dds_phase_to_radians(uint32_t phase)
+{
+    return phase*2.0f*3.1415926f/(65536.0f*65536.0f);
+}
+/*- End of function --------------------------------------------------------*/
+
 SPAN_DECLARE(int32_t) dds_phase_ratef(float frequency)
 {
     return (int32_t) (frequency*65536.0f*65536.0f/SAMPLE_RATE);
@@ -2124,44 +2130,45 @@ SPAN_DECLARE(float) dds_scaling_dbovf(float level)
 }
 /*- End of function --------------------------------------------------------*/
 
-SPAN_DECLARE(void) dds_advancef(uint32_t *phase_acc, int32_t phase_rate)
+static __inline__ float dds_lookupx(uint32_t phase)
 {
-    *phase_acc += phase_rate;
+    return sine_table[phase >> (32 - SLENK)];
 }
 /*- End of function --------------------------------------------------------*/
 
-SPAN_DECLARE(float) ddsf(uint32_t *phase_acc, int32_t phase_rate)
+SPAN_DECLARE(float) dds_lookupf(uint32_t phase)
 {
-    float amp;
+    return dds_lookupx(phase);
+}
+/*- End of function --------------------------------------------------------*/
 
-    amp = sine_table[*phase_acc >> (32 - SLENK)];
-    *phase_acc += phase_rate;
-    return amp;
+SPAN_DECLARE(float) dds_offsetf(uint32_t phase_acc, int32_t phase_offset)
+{
+    return dds_lookupx(phase_acc + phase_offset);
 }
 /*- End of function --------------------------------------------------------*/
 
-SPAN_DECLARE(float) dds_lookupf(uint32_t phase)
+SPAN_DECLARE(void) dds_advancef(uint32_t *phase_acc, int32_t phase_rate)
 {
-    return sine_table[phase >> (32 - SLENK)];
+    *phase_acc += phase_rate;
 }
 /*- End of function --------------------------------------------------------*/
 
-SPAN_DECLARE(float) dds_modf(uint32_t *phase_acc, int32_t phase_rate, float scale, int32_t phase)
+SPAN_DECLARE(float) ddsf(uint32_t *phase_acc, int32_t phase_rate)
 {
     float amp;
 
-    amp = sine_table[*(phase_acc + phase) >> (32 - SLENK)]*scale;
+    amp = dds_lookupx(*phase_acc);
     *phase_acc += phase_rate;
     return amp;
 }
 /*- End of function --------------------------------------------------------*/
 
-SPAN_DECLARE(complexf_t) dds_complexf(uint32_t *phase_acc, int32_t phase_rate)
+SPAN_DECLARE(float) dds_modf(uint32_t *phase_acc, int32_t phase_rate, float scale, int32_t phase)
 {
-    complexf_t amp;
+    float amp;
 
-    amp = complex_setf(sine_table[(*phase_acc + (1 << 30)) >> (32 - SLENK)],
-                       sine_table[*phase_acc >> (32 - SLENK)]);
+    amp = dds_lookupx(*phase_acc + phase)*scale;
     *phase_acc += phase_rate;
     return amp;
 }
@@ -2169,8 +2176,17 @@ SPAN_DECLARE(complexf_t) dds_complexf(uint32_t *phase_acc, int32_t phase_rate)
 
 SPAN_DECLARE(complexf_t) dds_lookup_complexf(uint32_t phase)
 {
-    return complex_setf(sine_table[(phase + (1 << 30)) >> (32 - SLENK)],
-                        sine_table[phase >> (32 - SLENK)]);
+    return complex_setf(dds_lookupx(phase + (1 << 30)), dds_lookupx(phase));
+}
+/*- End of function --------------------------------------------------------*/
+
+SPAN_DECLARE(complexf_t) dds_complexf(uint32_t *phase_acc, int32_t phase_rate)
+{
+    complexf_t amp;
+
+    amp = complex_setf(dds_lookupx(*phase_acc + (1 << 30)), dds_lookupx(*phase_acc));
+    *phase_acc += phase_rate;
+    return amp;
 }
 /*- End of function --------------------------------------------------------*/
 
@@ -2178,8 +2194,8 @@ SPAN_DECLARE(complexf_t) dds_complex_modf(uint32_t *phase_acc, int32_t phase_rat
 {
     complexf_t amp;
 
-    amp = complex_setf(sine_table[(*phase_acc + phase + (1 << 30)) >> (32 - SLENK)]*scale,
-                       sine_table[(*phase_acc + phase) >> (32 - SLENK)]*scale);
+    amp = complex_setf(dds_lookupx(*phase_acc + phase + (1 << 30))*scale,
+                       dds_lookupx(*phase_acc + phase)*scale);
     *phase_acc += phase_rate;
     return amp;
 }
index c4b8b6bb134f5a20f10e663b874cb4b2eb0f909b..2364f7ce0193efc76c95cb84c94c602758c92b07 100644 (file)
@@ -36,6 +36,12 @@ extern "C"
 {
 #endif
 
+/*! \brief Convert a 32 bit phase angle to an angle in radians, between 0 and 2*PI
+    \param phase The angle to convert.
+    \return The angle in radians.
+*/
+SPAN_DECLARE(float) dds_phase_to_radians(uint32_t phase);
+
 /*! \brief Find the phase rate value to achieve a particular frequency.
     \param frequency The desired frequency, in Hz.
     \return The phase rate which while achieve the desired frequency.
@@ -221,6 +227,13 @@ SPAN_DECLARE(float) ddsf(uint32_t *phase_acc, int32_t phase_rate);
 */
 SPAN_DECLARE(float) dds_lookupf(uint32_t phase);
 
+/*! \brief Lookup the floating point value of a particular phase offset from an accumulated phase.
+    \param phase_acc The accumulated phase.
+    \param phase_offset The phase offset.
+    \return The signal amplitude.
+*/
+SPAN_DECLARE(float) dds_offsetf(uint32_t phase_acc, int32_t phase_offset);
+
 /*! \brief Generate a floating point tone sample, with modulation.
     \param phase_acc A pointer to a phase accumulator value.
     \param phase_rate The phase increment to be applied.
index 438861651e0c4f47f73a105aa12accaea10d802d..f722c6067c1082b3ee333bfa0f47c956df39fcd1 100644 (file)
@@ -137,15 +137,15 @@ static __inline__ uint8_t linear_to_ulaw(int linear)
     }
 
     seg = top_bit(linear | 0xFF) - 7;
-
-    /*
-     * Combine the sign, segment, quantization bits,
-     * and complement the code word.
-     */
     if (seg >= 8)
+    {
         u_val = (uint8_t) (0x7F ^ mask);
+    }
     else
+    {
+        /* Combine the sign, segment, quantization bits, and complement the code word. */
         u_val = (uint8_t) (((seg << 4) | ((linear >> (seg + 3)) & 0xF)) ^ mask);
+    }
 #if defined(G711_ULAW_ZEROTRAP)
     /* Optional ITU trap */
     if (u_val == 0)
@@ -201,13 +201,14 @@ static __inline__ int16_t ulaw_to_linear(uint8_t ulaw)
 */
 static __inline__ uint8_t linear_to_alaw(int linear)
 {
+    uint8_t a_val;
     int mask;
     int seg;
 
     if (linear >= 0)
     {
         /* Sign (bit 7) bit = 1 */
-        mask = G711_ALAW_AMI_MASK | 0x80;
+        mask = 0x80 | G711_ALAW_AMI_MASK;
     }
     else
     {
@@ -220,16 +221,14 @@ static __inline__ uint8_t linear_to_alaw(int linear)
     seg = top_bit(linear | 0xFF) - 7;
     if (seg >= 8)
     {
-        if (linear >= 0)
-        {
-            /* Out of range. Return maximum value. */
-            return (uint8_t) (0x7F ^ mask);
-        }
-        /* We must be just a tiny step below zero */
-        return (uint8_t) mask;
+        a_val = (uint8_t) (0x7F ^ mask);
+    }
+    else
+    {
+        /* Combine the sign, segment, and quantization bits. */
+        a_val = (uint8_t) (((seg << 4) | ((linear >> ((seg)  ?  (seg + 3)  :  4)) & 0x0F)) ^ mask);
     }
-    /* Combine the sign, segment, and quantization bits. */
-    return (uint8_t) (((seg << 4) | ((linear >> ((seg)  ?  (seg + 3)  :  4)) & 0x0F)) ^ mask);
+    return a_val;
 }
 /*- End of function --------------------------------------------------------*/