コード例 #1
0
ファイル: t4_rx.c プロジェクト: AbrahamJewowich/FreeSWITCH
static int rx_put_bits(t4_state_t *s, uint32_t bit_string, int quantity)
{
    int bits;
    int old_a0;

    /* We decompress bit by bit, as the data stream is received. We need to
       scan continuously for EOLs, so we might as well work this way. */
    s->line_image_size += quantity;
    s->t4_t6_rx.rx_bitstream |= (bit_string << s->t4_t6_rx.rx_bits);
    /* The longest item we need to scan for is 13 bits long (a 2D EOL), so we
       need a minimum of 13 bits in the buffer to proceed with any bit stream
       analysis. */
    if ((s->t4_t6_rx.rx_bits += quantity) < 13)
        return FALSE;
    if (s->t4_t6_rx.consecutive_eols)
    {
        /* Check if the image has already terminated. */
        if (s->t4_t6_rx.consecutive_eols >= EOLS_TO_END_ANY_RX_PAGE)
            return TRUE;
        /* Check if the image hasn't even started. */
        if (s->t4_t6_rx.consecutive_eols < 0)
        {
            /* We are waiting for the very first EOL (1D or 2D only). */
            /* We need to take this bit by bit, as the EOL could be anywhere,
               and any junk could preceed it. */
            while ((s->t4_t6_rx.rx_bitstream & 0xFFF) != 0x800)
            {
                s->t4_t6_rx.rx_bitstream >>= 1;
                if (--s->t4_t6_rx.rx_bits < 13)
                    return FALSE;
            }
            /* We have an EOL, so now the page begins and we can proceed to
               process the bit stream as image data. */
            s->t4_t6_rx.consecutive_eols = 0;
            if (s->line_encoding == T4_COMPRESSION_ITU_T4_1D)
            {
                s->row_is_2d = FALSE;
                force_drop_rx_bits(s, 12);
            }
            else
            {
                s->row_is_2d = !(s->t4_t6_rx.rx_bitstream & 0x1000);
                force_drop_rx_bits(s, 13);
            }
        }
    }

    while (s->t4_t6_rx.rx_bits >= 13)
    {
        /* We need to check for EOLs bit by bit through the whole stream. If
           we just try looking between code words, we will miss an EOL when a bit
           error has throw the code words completely out of step. The can mean
           recovery takes many lines, and the image gets really messed up. */
        /* Although EOLs are not inserted at the end of each row of a T.6 image,
           they are still perfectly valid, and can terminate an image. */
        if ((s->t4_t6_rx.rx_bitstream & 0x0FFF) == 0x0800)
        {
            STATE_TRACE("EOL\n");
            if (s->row_len == 0)
            {
                /* A zero length row - i.e. 2 consecutive EOLs - is distinctly
                   the end of page condition. That's all we actually get on a
                   T.6 page. However, there are a minimum of 6 EOLs at the end of
                   any T.4 page. We can look for more than 2 EOLs in case bit
                   errors simulate the end of page condition at the wrong point.
                   Such robust checking is irrelevant for a T.6 page, as it should
                   be error free. */
                /* Note that for a T.6 page we should get here on the very first
                   EOL, as the row length should be zero at that point. Therefore
                   we should count up both EOLs, unless there is some bogus partial
                   row ahead of them. */
                s->t4_t6_rx.consecutive_eols++;
                if (s->line_encoding == T4_COMPRESSION_ITU_T6)
                {
                    if (s->t4_t6_rx.consecutive_eols >= EOLS_TO_END_T6_RX_PAGE)
                    {
                        s->t4_t6_rx.consecutive_eols = EOLS_TO_END_ANY_RX_PAGE;
                        return TRUE;
                    }
                }
                else
                {
                    if (s->t4_t6_rx.consecutive_eols >= EOLS_TO_END_T4_RX_PAGE)
                    {
                        s->t4_t6_rx.consecutive_eols = EOLS_TO_END_ANY_RX_PAGE;
                        return TRUE;
                    }
                }
            }
            else
            {
                /* The EOLs are not back-to-back, so they are not part of the
                   end of page condition. */
                if (s->t4_t6_rx.run_length > 0)
                    add_run_to_row(s);
                s->t4_t6_rx.consecutive_eols = 0;
                if (put_decoded_row(s))
                    return TRUE;
                update_row_bit_info(s);
            }
            if (s->line_encoding == T4_COMPRESSION_ITU_T4_2D)
            {
                s->row_is_2d = !(s->t4_t6_rx.rx_bitstream & 0x1000);
                force_drop_rx_bits(s, 13);
            }
            else
            {
                force_drop_rx_bits(s, 12);
            }
            s->t4_t6_rx.its_black = FALSE;
            s->t4_t6_rx.black_white = 0;
            s->t4_t6_rx.run_length = 0;
            s->row_len = 0;
            continue;
        }
        if (s->t4_t6_rx.rx_skip_bits)
        {
            /* We are clearing out the remaining bits of the last code word we
               absorbed. */
            s->t4_t6_rx.rx_skip_bits--;
            s->t4_t6_rx.rx_bits--;
            s->t4_t6_rx.rx_bitstream >>= 1;
            continue;
        }
        if (s->row_is_2d  &&  s->t4_t6_rx.black_white == 0)
        {
            bits = s->t4_t6_rx.rx_bitstream & 0x7F;
            STATE_TRACE("State %d, %d - ",
                        t4_2d_table[bits].state,
                        t4_2d_table[bits].width);
            if (s->row_len >= s->image_width)
            {
                drop_rx_bits(s, t4_2d_table[bits].width);
                continue;
            }
            if (s->t4_t6_rx.a_cursor)
            {
                /* Move past a0, always staying on the current colour */
                for (  ;  s->t4_t6_rx.b1 <= s->t4_t6_rx.a0;  s->t4_t6_rx.b_cursor += 2)
                    s->t4_t6_rx.b1 += (s->ref_runs[s->t4_t6_rx.b_cursor] + s->ref_runs[s->t4_t6_rx.b_cursor + 1]);
            }
            switch (t4_2d_table[bits].state)
            {
            case S_Horiz:
                STATE_TRACE("Horiz %d %d %d\n",
                            s->image_width,
                            s->t4_t6_rx.a0,
                            s->t4_t6_rx.a_cursor);
                /* We now need to extract a white/black or black/white pair of runs, using the 1D
                   method. If the first of the pair takes us exactly to the end of the row, there
                   should still be a zero length element for the second of the pair. */
                s->t4_t6_rx.its_black = s->t4_t6_rx.a_cursor & 1;
                s->t4_t6_rx.black_white = 2;
                break;
            case S_Vert:
                STATE_TRACE("Vert[%d] %d %d %d %d\n",
                            t4_2d_table[bits].param,
                            s->image_width,
                            s->t4_t6_rx.a0,
                            s->t4_t6_rx.b1,
                            s->t4_t6_rx.run_length);
                old_a0 = s->t4_t6_rx.a0;
                s->t4_t6_rx.a0 = s->t4_t6_rx.b1 + t4_2d_table[bits].param;
                /* We need to check if a bad or malicious image is failing to move forward along the row.
                   Going back is obviously bad. We also need to avoid a stall on the spot, except for the
                   special case of the start of the row. Zero movement as the very first element in the
                   row is perfectly normal. */
                if (s->t4_t6_rx.a0 <= old_a0)
                {
                    if (s->t4_t6_rx.a0 < old_a0  ||  s->t4_t6_rx.b_cursor > 1)
                    {
                        /* Undo the update we just started, and carry on as if this code does not exist */
                        /* TODO: we really should record that something wasn't right at this point. */
                        s->t4_t6_rx.a0 = old_a0;
                        break;
    		        }
	            }
                s->t4_t6_rx.run_length += (s->t4_t6_rx.a0 - old_a0);
                add_run_to_row(s);
                /* We need to move one step in one direction or the other, to change to the
                   opposite colour */
                if (t4_2d_table[bits].param >= 0)
                {
                    s->t4_t6_rx.b1 += s->ref_runs[s->t4_t6_rx.b_cursor++];
                }
                else
                {
                    if (s->t4_t6_rx.b_cursor)
                        s->t4_t6_rx.b1 -= s->ref_runs[--s->t4_t6_rx.b_cursor];
                }
                break;
            case S_Pass:
                STATE_TRACE("Pass %d %d %d %d %d\n",
                            s->image_width,
                            s->t4_t6_rx.a0,
                            s->t4_t6_rx.b1,
                            s->ref_runs[s->t4_t6_rx.b_cursor],
                            s->ref_runs[s->t4_t6_rx.b_cursor + 1]);
                s->t4_t6_rx.b1 += s->ref_runs[s->t4_t6_rx.b_cursor++];
                old_a0 = s->t4_t6_rx.a0;
                s->t4_t6_rx.a0 = s->t4_t6_rx.b1;
                s->t4_t6_rx.run_length += (s->t4_t6_rx.a0 - old_a0);
                s->t4_t6_rx.b1 += s->ref_runs[s->t4_t6_rx.b_cursor++];
                break;
            case S_Ext:
                /* We do not currently handle any kind of extension */
                STATE_TRACE("Ext %d %d %d 0x%x\n",
                            s->image_width,
                            s->t4_t6_rx.a0,
                            ((s->t4_t6_rx.rx_bitstream >> t4_2d_table[bits].width) & 0x7),
                            s->t4_t6_rx.rx_bitstream);
                /* TODO: The uncompressed option should be implemented. */
                break;
            case S_Null:
                STATE_TRACE("Null\n");
                break;
            default:
                STATE_TRACE("Unexpected T.4 state\n");
                span_log(&s->logging, SPAN_LOG_WARNING, "Unexpected T.4 state %d\n", t4_2d_table[bits].state);
                break;
            }
            drop_rx_bits(s, t4_2d_table[bits].width);
        }
コード例 #2
0
static int put_decoded_row(t4_t6_decode_state_t *s)
{
    static const int msbmask[9] =
    {
        0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff
    };
    uint32_t i;
    uint32_t *p;
    int fudge;
    int x;
    int j;
    int row_pos;

    if (s->run_length)
        add_run_to_row(s);
    update_row_bit_info(s);
#if defined(T4_STATE_DEBUGGING)
    /* Dump the runs of black and white for analysis */
    {
        int total;

        total = 0;
        for (x = 0;  x < s->b_cursor;  x++)
            total += s->ref_runs[x];
        printf("Ref (%d)", total);
        for (x = 0;  x < s->b_cursor;  x++)
            printf(" %" PRIu32, s->ref_runs[x]);
        printf("\n");
        total = 0;
        for (x = 0;  x < s->a_cursor;  x++)
            total += s->cur_runs[x];
        printf("Cur (%d)", total);
        for (x = 0;  x < s->a_cursor;  x++)
            printf(" %" PRIu32, s->cur_runs[x]);
        printf("\n");
    }
#endif
    row_pos = 0;
    if (s->row_len == s->image_width)
    {
        STATE_TRACE("%d Good row - %d %s\n", s->image_length, s->row_len, (s->row_is_2d)  ?  "2D"  :  "1D");
        if (s->curr_bad_row_run)
        {
            if (s->curr_bad_row_run > s->longest_bad_row_run)
                s->longest_bad_row_run = s->curr_bad_row_run;
            s->curr_bad_row_run = 0;
        }
        /* Convert the runs to a bit image of the row */
        /* White/black/white... runs, always starting with white. That means the first run could be
           zero length. */
        for (x = 0, fudge = 0;  x < s->a_cursor;  x++, fudge ^= 0xFF)
        {
            i = s->cur_runs[x];
            if ((int) i >= s->pixels)
            {
                s->pixel_stream = (s->pixel_stream << s->pixels) | (msbmask[s->pixels] & fudge);
                for (i += (8 - s->pixels);  i >= 8;  i -= 8)
                {
                    s->pixels = 8;
                    s->row_buf[row_pos++] = (uint8_t) s->pixel_stream;
                    s->pixel_stream = fudge;
                }
            }
            s->pixel_stream = (s->pixel_stream << i) | (msbmask[i] & fudge);
            s->pixels -= i;
        }
        s->image_length++;
    }
    else
    {
        STATE_TRACE("%d Bad row - %d %s\n", s->image_length, s->row_len, (s->row_is_2d)  ?  "2D"  :  "1D");
        /* Try to clean up the bad runs, and produce something reasonable as the reference
           row for the next row. Use a copy of the previous good row as the actual current
           row. If the row only fell apart near the end, reusing it might be the best
           solution. */
        for (j = 0, fudge = 0;  j < s->a_cursor  &&  fudge < s->image_width;  j++)
            fudge += s->cur_runs[j];
        if (fudge < s->image_width)
        {
            /* Try to pad with white, and avoid black, to minimise mess on the image. */
            if ((s->a_cursor & 1))
            {
                /* We currently finish in white. We could extend that, but it is probably of
                   the right length. Changing it would only further mess up what happens in the
                   next row. It seems better to add a black spot, and an extra white run. */
                s->cur_runs[s->a_cursor++] = 1;
                fudge++;
                if (fudge < s->image_width)
                    s->cur_runs[s->a_cursor++] = s->image_width - fudge;
            }
            else
            {
                /* We currently finish on black, so we add an extra white run to fill out the line. */
                s->cur_runs[s->a_cursor++] = s->image_width - fudge;
            }
        }
        else
        {
            /* Trim the last element to align with the proper image width */
            s->cur_runs[s->a_cursor] += (s->image_width - fudge);
        }
        /* Ensure there is a previous line to copy from. */
        /* Reuse the previous row as the current one. If this is the first row
           the row buffer will already contain a suitable white row */
        s->image_length++;
        s->bad_rows++;
        s->curr_bad_row_run++;
    }

    /* Pad the row as it becomes the reference row, so there are no odd runs to pick up if we
       step off the end of the list. */
    s->cur_runs[s->a_cursor] = 0;
    s->cur_runs[s->a_cursor + 1] = 0;

    /* Swap the buffers */
    p = s->cur_runs;
    s->cur_runs = s->ref_runs;
    s->ref_runs = p;

    s->b_cursor = 1;
    s->a_cursor = 0;
    s->b1 = s->ref_runs[0];
    s->a0 = 0;

    s->run_length = 0;
    if (s->row_write_handler)
        return s->row_write_handler(s->row_write_user_data, s->row_buf, s->bytes_per_row);

    return 0;
}