]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
Improved handling of page lengths in the T.85 decoder
authorSteve Underwood <steveu@haswell.coppice.org>
Sun, 8 Sep 2013 09:19:00 +0000 (17:19 +0800)
committerSteve Underwood <steveu@haswell.coppice.org>
Sun, 8 Sep 2013 09:19:00 +0000 (17:19 +0800)
libs/spandsp/src/t85_decode.c
libs/spandsp/src/v17rx.c
libs/spandsp/src/v27ter_rx.c

index 82bbecf013a4853f833526c68404270c1405c413..dda2a4053dd0698500ebc4bdbfef042c9db47aba 100644 (file)
@@ -271,11 +271,16 @@ SPAN_DECLARE(bool) t85_analyse_header(uint32_t *width, uint32_t *length, const u
         return false;
     *width = pack_32(&data[6]);
     *length = pack_32(&data[10]);
+    if ((data[19] & T85_VLENGTH))
+    {
+        /* TODO: scan for a true length, if the initial one just says 0xFFFFFFFF */
+        /* There should be an image length sequence terminating the image later on. */
+    }
     return true;
 }
 /*- End of function --------------------------------------------------------*/
 
-static int check_bih(t85_decode_state_t *s)
+static int extract_bih(t85_decode_state_t *s)
 {
     /* Check that the fixed parameters have the values they are expected to be
        fixed at - see T.85/Table 1 */
@@ -303,50 +308,68 @@ static int check_bih(t85_decode_state_t *s)
         return T4_DECODE_INVALID_DATA;
     }
     /* P - Number of bit planes */
-    if (s->buffer[2] < s->min_bit_planes  ||  s->buffer[2] > s->max_bit_planes)
-    {
-        span_log(&s->logging, SPAN_LOG_FLOW, "BIH invalid. %d bit planes. Should be %d to %d.\n", s->buffer[2], s->min_bit_planes, s->max_bit_planes);
-        s->end_of_data = 2;
-        return T4_DECODE_INVALID_DATA;
-    }
     s->bit_planes = s->buffer[2];
     s->current_bit_plane = 0;
     /* Now look at the stuff which actually counts in a T.85 header. */
     /* XD - Horizontal image size at layer D */
     s->xd = pack_32(&s->buffer[4]);
+    /* YD - Vertical image size at layer D */
+    s->yd = pack_32(&s->buffer[8]);
+    /* L0 - Rows per stripe, at the lowest resolution */
+    s->l0 = pack_32(&s->buffer[12]);
+    /* MX - Maximum horizontal offset allowed for AT pixel */
+    s->mx = s->buffer[16];
+    /* Options byte */
+    s->options = s->buffer[19];
+
+    if (s->bit_planes < s->min_bit_planes  ||  s->bit_planes > s->max_bit_planes)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "BIH invalid. %d bit planes. Should be %d to %d.\n", s->bit_planes, s->min_bit_planes, s->max_bit_planes);
+        s->end_of_data = 2;
+        return T4_DECODE_INVALID_DATA;
+    }
     if (s->xd == 0  ||  (s->max_xd  &&  s->xd > s->max_xd))
     {
         span_log(&s->logging, SPAN_LOG_FLOW, "BIH invalid. Width is %" PRIu32 "\n", s->xd);
         s->end_of_data = 2;
         return T4_DECODE_INVALID_DATA;
     }
-    /* YD - Vertical image size at layer D */
-    s->yd = pack_32(&s->buffer[8]);
-    if (s->yd == 0  ||  (s->max_yd  &&  s->yd > s->max_yd))
+    if (s->yd == 0)
     {
         span_log(&s->logging, SPAN_LOG_FLOW, "BIH invalid. Length is %" PRIu32 "\n", s->yd);
         s->end_of_data = 2;
         return T4_DECODE_INVALID_DATA;
     }
-    /* L0 - Rows per stripe, at the lowest resolution */
-    s->l0 = pack_32(&s->buffer[12]);
+    if (s->max_yd)
+    {
+        if ((s->options & T85_VLENGTH))
+        {
+            if (s->yd > s->max_yd)
+                s->yd = s->max_yd;
+        }
+        else
+        {
+            if (s->yd > s->max_yd)
+            {
+                span_log(&s->logging, SPAN_LOG_FLOW, "BIH invalid. Length is %" PRIu32 "\n", s->yd);
+                s->end_of_data = 2;
+                return T4_DECODE_INVALID_DATA;
+            }
+        }
+    }
     if (s->l0 == 0)
     {
         span_log(&s->logging, SPAN_LOG_FLOW, "BIH invalid. L0 is %" PRIu32 "\n", s->l0);
         s->end_of_data = 2;
         return T4_DECODE_INVALID_DATA;
     }
-    /* MX - Maximum horizontal offset allowed for AT pixel */
-    s->mx = s->buffer[16];
     if (s->mx > 127)
     {
         span_log(&s->logging, SPAN_LOG_FLOW, "BIH invalid. MX is %d\n", s->mx);
         s->end_of_data = 2;
         return T4_DECODE_INVALID_DATA;
     }
-    /* Options byte */
-    s->options = s->buffer[19];
-    if ((s->options & 0x97))
+    if ((s->options & ~(T85_LRLTWO | T85_VLENGTH | T85_TPBON)))
     {
         span_log(&s->logging, SPAN_LOG_FLOW, "BIH invalid. Options are 0x%X\n", s->options);
         s->end_of_data = 2;
@@ -413,7 +436,7 @@ SPAN_DECLARE(int) t85_decode_put(t85_decode_state_t *s, const uint8_t data[], si
         cnt = i;
         if (s->bie_len < 20)
             return T4_DECODE_MORE_DATA;
-        if ((ret = check_bih(s)) != T4_DECODE_OK)
+        if ((ret = extract_bih(s)) != T4_DECODE_OK)
             return ret;
         /* Set up the two/three row buffer */
         bytes_per_row = (s->xd + 7) >> 3;
@@ -690,8 +713,8 @@ SPAN_DECLARE(int) t85_decode_put(t85_decode_state_t *s, const uint8_t data[], si
             cnt += decode_pscd(s, data + cnt, len - cnt);
             if (s->interrupt)
                 return T4_DECODE_INTERRUPT;
-            /* We should only have stopped processing PSCD if
-               we ran out of data, or hit a T82_ESC */
+            /* We should only have stopped processing PSCD if we ran out of data,
+               or hit a T82_ESC */
             if (cnt < len  &&  data[cnt] != T82_ESC)
             {
                 s->end_of_data = 2;
index bfc701a597afdc963db4fd20bd8998026e0c1231..85d72d0ed626c780839f8126653cb8abd9908937 100644 (file)
@@ -905,12 +905,11 @@ static void process_half_baud(v17_rx_state_t *s, const complexf_t *sample)
         tune_equalizer(s, &z, target);
         if (++s->training_count >= V17_TRAINING_SEG_2_LEN - 48)
         {
+            s->training_error = FP_SCALE(0.0f);
 #if defined(SPANDSP_USE_FIXED_POINTx)
-            s->training_error = 0;
             s->carrier_track_i = 100;
             s->carrier_track_p = 500000;
 #else
-            s->training_error = 0.0f;
             s->carrier_track_i = 100.0f;
             s->carrier_track_p = 500000.0f;
 #endif
index 228c5e4d773a0629a943941c39e110d4e0d9d9b0..3451d660265a49a31bb2d3b91f93336856e78314 100644 (file)
@@ -178,8 +178,13 @@ SPAN_DECLARE(int) v27ter_rx_equalizer_state(v27ter_rx_state_t *s, complexi16_t *
 SPAN_DECLARE(int) v27ter_rx_equalizer_state(v27ter_rx_state_t *s, complexf_t **coeffs)
 #endif
 {
+#if defined(SPANDSP_USE_FIXED_POINT)
+    *coeffs = NULL;
+    return 0;
+#else
     *coeffs = s->eq_coeff;
     return V27TER_EQUALIZER_LEN;
+#endif
 }
 /*- End of function --------------------------------------------------------*/