57
58
#define SAMPLES_PER_CHUNK 160
83
} fallback_sequence[] =
85
{14400, T30_MODEM_V17_14400, T30_SUPPORT_V17, DISBIT6},
86
{12000, T30_MODEM_V17_12000, T30_SUPPORT_V17, (DISBIT6 | DISBIT4)},
87
{ 9600, T30_MODEM_V17_9600, T30_SUPPORT_V17, (DISBIT6 | DISBIT3)},
88
{ 9600, T30_MODEM_V29_9600, T30_SUPPORT_V29, DISBIT3},
89
{ 7200, T30_MODEM_V17_7200, T30_SUPPORT_V17, (DISBIT6 | DISBIT4 | DISBIT3)},
90
{ 7200, T30_MODEM_V29_7200, T30_SUPPORT_V29, (DISBIT4 | DISBIT3)},
91
{ 4800, T30_MODEM_V27TER_4800, T30_SUPPORT_V27TER, DISBIT4},
92
{ 2400, T30_MODEM_V27TER_2400, T30_SUPPORT_V27TER, 0},
59
96
int decode_test = FALSE;
63
99
t30_state_t t30_dummy;
64
100
t4_state_t t4_state;
65
101
int t4_up = FALSE;
103
hdlc_rx_state_t hdlcrx;
105
int fast_trained = FAX_NONE;
107
uint8_t ecm_data[256][260];
108
int16_t ecm_len[256];
110
int line_encoding = T4_COMPRESSION_ITU_T4_2D;
111
int x_resolution = T4_X_RESOLUTION_R8;
112
int y_resolution = T4_Y_RESOLUTION_STANDARD;
113
int image_width = 1728;
114
int octets_per_ecm_frame = 256;
115
int error_correcting_mode = FALSE;
116
int current_fallback = 0;
67
118
static void print_frame(const char *io, const uint8_t *fr, int frlen)
84
135
if (t35_decode(&fr[3], frlen - 3, &country, &vendor, &model))
87
printf("The remote was made in '%s'\n", country);
138
fprintf(stderr, "The remote was made in '%s'\n", country);
89
printf("The remote was made by '%s'\n", vendor);
140
fprintf(stderr, "The remote was made by '%s'\n", vendor);
91
printf("The remote is a '%s'\n", model);
142
fprintf(stderr, "The remote is a '%s'\n", model);
95
146
/*- End of function --------------------------------------------------------*/
148
static int find_fallback_entry(int dcs_code)
152
/* The table is short, and not searched often, so a brain-dead linear scan seems OK */
153
for (i = 0; fallback_sequence[i].bit_rate; i++)
155
if (fallback_sequence[i].dcs_code == dcs_code)
158
if (fallback_sequence[i].bit_rate == 0)
162
/*- End of function --------------------------------------------------------*/
164
static int check_rx_dcs(const uint8_t *msg, int len)
166
static const int widths[3][4] =
168
{ 864, 1024, 1216, -1}, /* R4 resolution - no longer used in recent versions of T.30 */
169
{1728, 2048, 2432, -1}, /* R8 resolution */
170
{3456, 4096, 4864, -1} /* R16 resolution */
172
uint8_t dcs_frame[T30_MAX_DIS_DTC_DCS_LEN];
174
/* Check DCS frame from remote */
177
printf("Short DCS frame\n");
181
/* Make a local copy of the message, padded to the maximum possible length with zeros. This allows
182
us to simply pick out the bits, without worrying about whether they were set from the remote side. */
183
if (len > T30_MAX_DIS_DTC_DCS_LEN)
185
memcpy(dcs_frame, msg, T30_MAX_DIS_DTC_DCS_LEN);
189
memcpy(dcs_frame, msg, len);
190
if (len < T30_MAX_DIS_DTC_DCS_LEN)
191
memset(dcs_frame + len, 0, T30_MAX_DIS_DTC_DCS_LEN - len);
194
octets_per_ecm_frame = (dcs_frame[6] & DISBIT4) ? 256 : 64;
195
if ((dcs_frame[8] & DISBIT1))
196
y_resolution = T4_Y_RESOLUTION_SUPERFINE;
197
else if (dcs_frame[4] & DISBIT7)
198
y_resolution = T4_Y_RESOLUTION_FINE;
200
y_resolution = T4_Y_RESOLUTION_STANDARD;
201
image_width = widths[(dcs_frame[8] & DISBIT3) ? 2 : 1][dcs_frame[5] & (DISBIT2 | DISBIT1)];
203
/* Check which compression we will use. */
204
if ((dcs_frame[6] & DISBIT7))
205
line_encoding = T4_COMPRESSION_ITU_T6;
206
else if ((dcs_frame[4] & DISBIT8))
207
line_encoding = T4_COMPRESSION_ITU_T4_2D;
209
line_encoding = T4_COMPRESSION_ITU_T4_1D;
210
printf("Selected compression %d\n", line_encoding);
212
if ((current_fallback = find_fallback_entry(dcs_frame[4] & (DISBIT6 | DISBIT5 | DISBIT4 | DISBIT3))) < 0)
213
printf("Remote asked for a modem standard we do not support\n");
214
error_correcting_mode = ((dcs_frame[6] & DISBIT3) != 0);
216
#if defined(ENABLE_V17)
217
//v17_rx_restart(&v17, fallback_sequence[fallback_entry].bit_rate, FALSE);
221
/*- End of function --------------------------------------------------------*/
97
223
static void hdlc_accept(void *user_data, int ok, const uint8_t *msg, int len)
101
231
/* Special conditions */
104
234
case PUTBIT_CARRIER_UP:
105
fprintf(stderr, "Slow carrier up\n");
235
fprintf(stderr, "HDLC carrier up\n");
107
237
case PUTBIT_CARRIER_DOWN:
108
fprintf(stderr, "Slow carrier down\n");
238
fprintf(stderr, "HDLC carrier down\n");
110
240
case PUTBIT_FRAMING_OK:
241
fprintf(stderr, "HDLC framing OK\n");
111
243
case PUTBIT_ABORT:
112
244
/* Just ignore these */
121
if (msg[0] != 0xFF || !(msg[1] == 0x03 || msg[1] == 0x13))
123
fprintf(stderr, "Bad frame header - %02x %02x", msg[0], msg[1]);
126
print_frame("HDLC: ", msg, len);
255
if (msg[0] != 0xFF || !(msg[1] == 0x03 || msg[1] == 0x13))
257
fprintf(stderr, "Bad frame header - %02x %02x", msg[0], msg[1]);
260
print_frame("HDLC: ", msg, len);
261
type = msg[2] & 0xFE;
268
/* Just store the actual image data, and record its length */
269
memcpy(&ecm_data[frame_no][0], &msg[4], len - 4);
270
ecm_len[frame_no] = (int16_t) (len - 4);
274
check_rx_dcs(msg, len);
280
fprintf(stderr, "Bad HDLC frame ");
281
for (i = 0; i < len; i++)
282
fprintf(stderr, " %02x", msg[i]);
283
fprintf(stderr, "\n");
128
286
/*- End of function --------------------------------------------------------*/
130
288
static void t4_begin(void)
132
t4_rx_set_rx_encoding(&t4_state, T4_COMPRESSION_ITU_T4_2D);
133
t4_rx_set_x_resolution(&t4_state, T4_X_RESOLUTION_R8);
134
t4_rx_set_y_resolution(&t4_state, T4_Y_RESOLUTION_STANDARD);
135
t4_rx_set_image_width(&t4_state, 1728);
292
t4_rx_set_rx_encoding(&t4_state, line_encoding);
293
t4_rx_set_x_resolution(&t4_state, x_resolution);
294
t4_rx_set_y_resolution(&t4_state, y_resolution);
295
t4_rx_set_image_width(&t4_state, image_width);
137
297
t4_rx_start_page(&t4_state);
300
for (i = 0; i < 256; i++)
140
303
/*- End of function --------------------------------------------------------*/
142
305
static void t4_end(void)
144
307
t4_stats_t stats;
148
312
t4_rx_end_page(&t4_state);
149
313
t4_get_transfer_statistics(&t4_state, &stats);
150
printf("Pages = %d\n", stats.pages_transferred);
151
printf("Image size = %dx%d\n", stats.width, stats.length);
152
printf("Image resolution = %dx%d\n", stats.x_resolution, stats.y_resolution);
153
printf("Bad rows = %d\n", stats.bad_rows);
154
printf("Longest bad row run = %d\n", stats.longest_bad_row_run);
314
fprintf(stderr, "Pages = %d\n", stats.pages_transferred);
315
fprintf(stderr, "Image size = %dx%d\n", stats.width, stats.length);
316
fprintf(stderr, "Image resolution = %dx%d\n", stats.x_resolution, stats.y_resolution);
317
fprintf(stderr, "Bad rows = %d\n", stats.bad_rows);
318
fprintf(stderr, "Longest bad row run = %d\n", stats.longest_bad_row_run);
319
for (i = 0; i < 256; i++)
320
printf("%d", (ecm_len[i] < 0) ? 0 : 1);
157
324
/*- End of function --------------------------------------------------------*/
326
static void v21_put_bit(void *user_data, int bit)
330
/* Special conditions */
333
case PUTBIT_TRAINING_FAILED:
334
fprintf(stderr, "V.21 Training failed\n");
336
case PUTBIT_TRAINING_SUCCEEDED:
337
fprintf(stderr, "V.21 Training succeeded\n");
340
case PUTBIT_CARRIER_UP:
341
fprintf(stderr, "V.21 Carrier up\n");
343
case PUTBIT_CARRIER_DOWN:
344
fprintf(stderr, "V.21 Carrier down\n");
348
fprintf(stderr, "V.21 Eh!\n");
353
if (fast_trained == FAX_NONE)
354
hdlc_rx_put_bit(&hdlcrx, bit);
355
//printf("V.21 Rx bit %d - %d\n", rx_bits++, bit);
357
/*- End of function --------------------------------------------------------*/
159
359
#if defined(ENABLE_V17)
160
360
static void v17_put_bit(void *user_data, int bit)
166
364
/* Special conditions */
169
367
case PUTBIT_TRAINING_FAILED:
170
//printf("V.17 Training failed\n");
368
fprintf(stderr, "V.17 Training failed\n");
172
370
case PUTBIT_TRAINING_SUCCEEDED:
173
printf("V.17 Training succeeded\n");
371
fprintf(stderr, "V.17 Training succeeded\n");
372
fast_trained = FAX_V17_RX;
176
375
case PUTBIT_CARRIER_UP:
177
//printf("V.17 Carrier up\n");
376
fprintf(stderr, "V.17 Carrier up\n");
179
378
case PUTBIT_CARRIER_DOWN:
180
//printf("V.17 Carrier down\n");
379
fprintf(stderr, "V.17 Carrier down\n");
381
if (fast_trained == FAX_V17_RX)
382
fast_trained = FAX_NONE;
184
printf("V.17 Eh!\n");
385
fprintf(stderr, "V.17 Eh!\n");
190
end_of_page = t4_rx_putbit(&t4_state, bit);
194
printf("End of page detected\n");
390
if (error_correcting_mode)
392
hdlc_rx_put_bit(&hdlcrx, bit);
396
if (t4_rx_put_bit(&t4_state, bit))
399
fprintf(stderr, "End of page detected\n");
196
402
//printf("V.17 Rx bit %d - %d\n", rx_bits++, bit);
201
407
static void v29_put_bit(void *user_data, int bit)
207
411
/* Special conditions */
210
414
case PUTBIT_TRAINING_FAILED:
211
//printf("V.29 Training failed\n");
415
//fprintf(stderr, "V.29 Training failed\n");
213
417
case PUTBIT_TRAINING_SUCCEEDED:
214
printf("V.29 Training succeeded\n");
418
fprintf(stderr, "V.29 Training succeeded\n");
419
fast_trained = FAX_V29_RX;
217
422
case PUTBIT_CARRIER_UP:
218
//printf("V.29 Carrier up\n");
423
//fprintf(stderr, "V.29 Carrier up\n");
220
425
case PUTBIT_CARRIER_DOWN:
221
//printf("V.29 Carrier down\n");
426
//fprintf(stderr, "V.29 Carrier down\n");
428
if (fast_trained == FAX_V29_RX)
429
fast_trained = FAX_NONE;
225
printf("V.29 Eh!\n");
432
fprintf(stderr, "V.29 Eh!\n");
231
end_of_page = t4_rx_put_bit(&t4_state, bit);
235
printf("End of page detected\n");
438
if (error_correcting_mode)
440
hdlc_rx_put_bit(&hdlcrx, bit);
444
if (t4_rx_put_bit(&t4_state, bit))
447
fprintf(stderr, "End of page detected\n");
237
450
//printf("V.29 Rx bit %d - %d\n", rx_bits++, bit);
248
461
case PUTBIT_TRAINING_FAILED:
249
//printf("V.27ter Training failed\n");
462
//fprintf(stderr, "V.27ter Training failed\n");
251
464
case PUTBIT_TRAINING_SUCCEEDED:
252
printf("V.27ter Training succeeded\n");
465
fprintf(stderr, "V.27ter Training succeeded\n");
466
fast_trained = FAX_V27TER_RX;
255
469
case PUTBIT_CARRIER_UP:
256
//printf("V.27ter Carrier up\n");
470
//fprintf(stderr, "V.27ter Carrier up\n");
258
472
case PUTBIT_CARRIER_DOWN:
259
//printf("V.27ter Carrier down\n");
473
//fprintf(stderr, "V.27ter Carrier down\n");
474
if (fast_trained == FAX_V27TER_RX)
475
fast_trained = FAX_NONE;
262
printf("V.27ter Eh!\n");
478
fprintf(stderr, "V.27ter Eh!\n");
268
printf("V.27ter Rx bit %d - %d\n", rx_bits++, bit);
484
if (error_correcting_mode)
486
hdlc_rx_put_bit(&hdlcrx, bit);
490
if (t4_rx_put_bit(&t4_state, bit))
493
fprintf(stderr, "End of page detected\n");
496
//printf("V.27ter Rx bit %d - %d\n", rx_bits++, bit);
270
498
/*- End of function --------------------------------------------------------*/
272
500
int main(int argc, char *argv[])
274
hdlc_rx_state_t hdlcrx;
275
502
fsk_rx_state_t fsk;
276
503
#if defined(ENABLE_V17)
277
504
v17_rx_state_t v17;
298
525
span_log_init(&t30_dummy.logging, SPAN_LOG_FLOW, NULL);
299
526
span_log_set_protocol(&t30_dummy.logging, "T.30");
301
hdlc_rx_init(&hdlcrx, FALSE, FALSE, 1, hdlc_accept, NULL);
302
fsk_rx_init(&fsk, &preset_fsk_specs[FSK_V21CH2], TRUE, 0, (put_bit_func_t) hdlc_rx_put_bit, &hdlcrx);
528
hdlc_rx_init(&hdlcrx, FALSE, TRUE, 5, hdlc_accept, NULL);
529
fsk_rx_init(&fsk, &preset_fsk_specs[FSK_V21CH2], TRUE, 0, v21_put_bit, NULL);
303
530
#if defined(ENABLE_V17)
304
531
v17_rx_init(&v17, 14400, v17_put_bit, NULL);
306
533
v29_rx_init(&v29, 9600, v29_put_bit, NULL);
307
534
v27ter_rx_init(&v27ter, 4800, v27ter_put_bit, NULL);
308
fsk_rx_signal_cutoff(&fsk, -45.0);
535
fsk_rx_signal_cutoff(&fsk, -45.5);
309
536
#if defined(ENABLE_V17)
310
v17_rx_signal_cutoff(&v17, -45.0);
537
v17_rx_signal_cutoff(&v17, -45.5);
312
v29_rx_signal_cutoff(&v29, -45.0);
539
v29_rx_signal_cutoff(&v29, -45.5);
313
540
v27ter_rx_signal_cutoff(&v27ter, -40.0);
315
//span_log_init(&v29.logging, SPAN_LOG_FLOW, NULL);
316
//span_log_set_protocol(&v29.logging, "V.29");
317
//span_log_set_level(&v29.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_FLOW);
542
#if defined(ENABLE_V17)
543
span_log_init(&v17.logging, SPAN_LOG_FLOW, NULL);
544
span_log_set_protocol(&v17.logging, "V.17");
545
span_log_set_level(&v17.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_FLOW);
319
548
if (t4_rx_init(&t4_state, "fax_decode.tif", T4_COMPRESSION_ITU_T4_2D))
321
printf("Failed to init\n");
550
fprintf(stderr, "Failed to init\n");