t38_decode.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * t38_decode.c
  5. *
  6. * Written by Steve Underwood <steveu@coppice.org>
  7. *
  8. * Copyright (C) 2010 Steve Underwood
  9. *
  10. * All rights reserved.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2, as
  14. * published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. /*! \file */
  26. #if defined(HAVE_CONFIG_H)
  27. #include "config.h"
  28. #endif
  29. #include <stdlib.h>
  30. #include <inttypes.h>
  31. #include <stdio.h>
  32. #include <fcntl.h>
  33. #include <string.h>
  34. #include <assert.h>
  35. #include <errno.h>
  36. #include <time.h>
  37. #if !defined(_WIN32)
  38. #include <unistd.h>
  39. #endif
  40. #include "udptl.h"
  41. #include "spandsp.h"
  42. #include "spandsp-sim.h"
  43. #include "fax_utils.h"
  44. #include "pcap_parse.h"
  45. #define INPUT_FILE_NAME "t38.pcap"
  46. #define INPUT_TIFF_FILE_NAME "../test-data/itu/fax/itutests.tif"
  47. #define OUTPUT_TIFF_FILE_NAME "t38pcap.tif"
  48. #define OUTPUT_WAVE_FILE_NAME "t38_decode2.wav"
  49. #define SAMPLES_PER_CHUNK 160
  50. static t38_core_state_t *t38_core;
  51. static t38_terminal_state_t *t38_terminal_state;
  52. static t38_gateway_state_t *t38_gateway_state;
  53. static fax_state_t *fax_state;
  54. static struct timeval now;
  55. static SNDFILE *wave_handle;
  56. static int log_audio;
  57. static int use_transmit_on_idle;
  58. static int done = false;
  59. static int started = false;
  60. static int64_t current = 0;
  61. static int phase_b_handler(void *user_data, int result)
  62. {
  63. int ch;
  64. t30_state_t *s;
  65. char tag[20];
  66. ch = 'A';
  67. s = (t30_state_t *) user_data;
  68. snprintf(tag, sizeof(tag), "%c: Phase B", ch);
  69. printf("%c: Phase B handler on channel %c - (0x%X) %s\n", ch, ch, result, t30_frametype(result));
  70. fax_log_rx_parameters(s, tag);
  71. return T30_ERR_OK;
  72. }
  73. /*- End of function --------------------------------------------------------*/
  74. static int phase_d_handler(void *user_data, int result)
  75. {
  76. int ch;
  77. t30_state_t *s;
  78. char tag[20];
  79. ch = 'A';
  80. s = (t30_state_t *) user_data;
  81. snprintf(tag, sizeof(tag), "%c: Phase D", ch);
  82. printf("%c: Phase D handler on channel %c - (0x%X) %s\n", ch, ch, result, t30_frametype(result));
  83. fax_log_page_transfer_statistics(s, tag);
  84. fax_log_tx_parameters(s, tag);
  85. fax_log_rx_parameters(s, tag);
  86. return T30_ERR_OK;
  87. }
  88. /*- End of function --------------------------------------------------------*/
  89. static void phase_e_handler(void *user_data, int result)
  90. {
  91. int ch;
  92. t30_stats_t t;
  93. t30_state_t *s;
  94. char tag[20];
  95. ch = 'A';
  96. s = (t30_state_t *) user_data;
  97. snprintf(tag, sizeof(tag), "%c: Phase E", ch);
  98. printf("%c: Phase E handler on channel %c - (%d) %s\n", ch, ch, result, t30_completion_code_to_str(result));
  99. fax_log_final_transfer_statistics(s, tag);
  100. fax_log_tx_parameters(s, tag);
  101. fax_log_rx_parameters(s, tag);
  102. t30_get_transfer_statistics(s, &t);
  103. }
  104. /*- End of function --------------------------------------------------------*/
  105. static int tx_packet_handler(t38_core_state_t *s, void *user_data, const uint8_t *buf, int len, int count)
  106. {
  107. return 0;
  108. }
  109. /*- End of function --------------------------------------------------------*/
  110. static int t38_terminal_timing_update(void *user_data, struct timeval *ts)
  111. {
  112. t30_state_t *t30;
  113. logging_state_t *logging;
  114. int samples;
  115. int partial;
  116. int64_t when;
  117. int64_t diff;
  118. memcpy(&now, ts, sizeof(now));
  119. when = ts->tv_sec*1000000LL + ts->tv_usec;
  120. if (current == 0)
  121. {
  122. if (started)
  123. current = when;
  124. return 0;
  125. }
  126. diff = when - current;
  127. samples = diff/125LL;
  128. while (samples > 0)
  129. {
  130. partial = (samples > SAMPLES_PER_CHUNK) ? SAMPLES_PER_CHUNK : samples;
  131. //fprintf(stderr, "Update time by %d samples\n", partial);
  132. logging = t38_terminal_get_logging_state(t38_terminal_state);
  133. span_log_bump_samples(logging, partial);
  134. logging = t38_core_get_logging_state(t38_core);
  135. span_log_bump_samples(logging, partial);
  136. t30 = t38_terminal_get_t30_state(t38_terminal_state);
  137. logging = t30_get_logging_state(t30);
  138. span_log_bump_samples(logging, partial);
  139. t38_terminal_send_timeout(t38_terminal_state, partial);
  140. current = when;
  141. samples -= partial;
  142. }
  143. return 0;
  144. }
  145. /*- End of function --------------------------------------------------------*/
  146. static int t38_gateway_timing_update(void *user_data, struct timeval *ts)
  147. {
  148. t30_state_t *t30;
  149. logging_state_t *logging;
  150. int samples;
  151. int partial;
  152. int64_t when;
  153. int64_t diff;
  154. int16_t t38_amp[SAMPLES_PER_CHUNK];
  155. int16_t t30_amp[SAMPLES_PER_CHUNK];
  156. int16_t out_amp[2*SAMPLES_PER_CHUNK];
  157. int t38_len;
  158. int t30_len;
  159. int outframes;
  160. int i;
  161. memcpy(&now, ts, sizeof(now));
  162. when = ts->tv_sec*1000000LL + ts->tv_usec;
  163. if (current == 0)
  164. {
  165. if (started)
  166. current = when;
  167. return 0;
  168. }
  169. diff = when - current;
  170. samples = diff/125LL;
  171. while (samples > 0)
  172. {
  173. partial = (samples > SAMPLES_PER_CHUNK) ? SAMPLES_PER_CHUNK : samples;
  174. //fprintf(stderr, "Update time by %d samples\n", partial);
  175. logging = t38_gateway_get_logging_state(t38_gateway_state);
  176. span_log_bump_samples(logging, partial);
  177. logging = t38_core_get_logging_state(t38_core);
  178. span_log_bump_samples(logging, partial);
  179. logging = fax_get_logging_state(fax_state);
  180. span_log_bump_samples(logging, partial);
  181. t30 = fax_get_t30_state(fax_state);
  182. logging = t30_get_logging_state(t30);
  183. span_log_bump_samples(logging, partial);
  184. memset(out_amp, 0, sizeof(out_amp));
  185. t30_len = fax_tx(fax_state, t30_amp, partial);
  186. if (!use_transmit_on_idle)
  187. {
  188. /* The receive side always expects a full block of samples, but the
  189. transmit side may not be sending any when it doesn't need to. We
  190. may need to pad with some silence. */
  191. if (t30_len < partial)
  192. {
  193. memset(t30_amp + t30_len, 0, sizeof(int16_t)*(partial - t30_len));
  194. t30_len = partial;
  195. }
  196. }
  197. if (log_audio)
  198. {
  199. for (i = 0; i < t30_len; i++)
  200. out_amp[2*i + 1] = t30_amp[i];
  201. }
  202. if (t38_gateway_rx(t38_gateway_state, t30_amp, t30_len))
  203. break;
  204. t38_len = t38_gateway_tx(t38_gateway_state, t38_amp, partial);
  205. if (!use_transmit_on_idle)
  206. {
  207. if (t38_len < partial)
  208. {
  209. memset(t38_amp + t38_len, 0, sizeof(int16_t)*(partial - t38_len));
  210. t38_len = partial;
  211. }
  212. }
  213. if (log_audio)
  214. {
  215. for (i = 0; i < t38_len; i++)
  216. out_amp[2*i] = t38_amp[i];
  217. }
  218. if (fax_rx(fax_state, t38_amp, partial))
  219. break;
  220. if (log_audio)
  221. {
  222. outframes = sf_writef_short(wave_handle, out_amp, partial);
  223. if (outframes != partial)
  224. break;
  225. }
  226. if (done)
  227. break;
  228. current = when;
  229. samples -= partial;
  230. }
  231. return 0;
  232. }
  233. /*- End of function --------------------------------------------------------*/
  234. static int ifp_handler(void *user_data, const uint8_t msg[], int len, int seq_no)
  235. {
  236. int i;
  237. started = true;
  238. printf("%5d >>> ", seq_no);
  239. for (i = 0; i < len; i++)
  240. printf("%02X ", msg[i]);
  241. printf("\n");
  242. t38_core_rx_ifp_packet(t38_core, msg, len, seq_no);
  243. return 0;
  244. }
  245. /*- End of function --------------------------------------------------------*/
  246. static int process_packet(void *user_data, const uint8_t *pkt, int len)
  247. {
  248. static udptl_state_t *state = NULL;
  249. if (state == NULL)
  250. state = udptl_init(NULL, UDPTL_ERROR_CORRECTION_REDUNDANCY, 3, 3, ifp_handler, NULL);
  251. udptl_rx_packet(state, pkt, len);
  252. return 0;
  253. }
  254. /*- End of function --------------------------------------------------------*/
  255. static uint32_t parse_inet_addr(const char *s)
  256. {
  257. int i;
  258. uint32_t a;
  259. uint32_t b;
  260. uint32_t c;
  261. uint32_t d;
  262. a = 0;
  263. b = 0;
  264. c = 0;
  265. d = 0;
  266. i = sscanf(s, "%" PRIu32 ".%" PRIu32 ".%" PRIu32 ".%" PRIu32, &a, &b, &c, &d);
  267. switch (i)
  268. {
  269. case 4:
  270. c = (c << 8) | d;
  271. case 3:
  272. b = (b << 16) | c;
  273. case 2:
  274. a = (a << 24) | b;
  275. }
  276. return a;
  277. }
  278. /*- End of function --------------------------------------------------------*/
  279. int main(int argc, char *argv[])
  280. {
  281. t30_state_t *t30;
  282. logging_state_t *logging;
  283. const char *input_file_name;
  284. const char *input_tiff_file_name;
  285. int t38_version;
  286. int caller;
  287. int use_ecm;
  288. int use_tep;
  289. int options;
  290. int supported_modems;
  291. int fill_removal;
  292. int opt;
  293. int t38_terminal_operation;
  294. uint32_t src_addr;
  295. uint16_t src_port;
  296. uint32_t dest_addr;
  297. uint16_t dest_port;
  298. caller = false;
  299. use_ecm = false;
  300. t38_version = 0;
  301. options = 0;
  302. input_file_name = INPUT_FILE_NAME;
  303. input_tiff_file_name = INPUT_TIFF_FILE_NAME;
  304. fill_removal = false;
  305. use_tep = false;
  306. use_transmit_on_idle = true;
  307. supported_modems = T30_SUPPORT_V27TER | T30_SUPPORT_V29 | T30_SUPPORT_V17;
  308. t38_terminal_operation = true;
  309. log_audio = false;
  310. src_addr = 0;
  311. src_port = 0;
  312. dest_addr = 0;
  313. dest_port = 0;
  314. while ((opt = getopt(argc, argv, "cD:d:eFGi:lm:oS:s:T:tv:")) != -1)
  315. {
  316. switch (opt)
  317. {
  318. case 'c':
  319. caller = true;
  320. break;
  321. case 'D':
  322. dest_addr = parse_inet_addr(optarg);
  323. break;
  324. case 'd':
  325. dest_port = atoi(optarg);
  326. break;
  327. case 'e':
  328. use_ecm = true;
  329. break;
  330. case 'F':
  331. fill_removal = true;
  332. break;
  333. case 'G':
  334. t38_terminal_operation = false;
  335. break;
  336. case 'i':
  337. input_file_name = optarg;
  338. break;
  339. case 'l':
  340. log_audio = true;
  341. break;
  342. case 'm':
  343. supported_modems = atoi(optarg);
  344. break;
  345. case 'o':
  346. options = atoi(optarg);
  347. break;
  348. case 'S':
  349. src_addr = parse_inet_addr(optarg);
  350. break;
  351. case 's':
  352. src_port = atoi(optarg);
  353. break;
  354. case 'T':
  355. input_tiff_file_name = optarg;
  356. break;
  357. case 't':
  358. use_tep = true;
  359. break;
  360. case 'v':
  361. t38_version = atoi(optarg);
  362. break;
  363. default:
  364. //usage();
  365. exit(2);
  366. break;
  367. }
  368. }
  369. printf("Using T.38 version %d\n", t38_version);
  370. if (t38_terminal_operation)
  371. {
  372. if ((t38_terminal_state = t38_terminal_init(NULL, caller, tx_packet_handler, NULL)) == NULL)
  373. {
  374. fprintf(stderr, "Cannot start the T.38 channel\n");
  375. exit(2);
  376. }
  377. t30 = t38_terminal_get_t30_state(t38_terminal_state);
  378. t38_core = t38_terminal_get_t38_core_state(t38_terminal_state);
  379. t38_set_t38_version(t38_core, t38_version);
  380. t38_terminal_set_config(t38_terminal_state, options);
  381. t38_terminal_set_tep_mode(t38_terminal_state, use_tep);
  382. t38_terminal_set_fill_bit_removal(t38_terminal_state, fill_removal);
  383. logging = t38_terminal_get_logging_state(t38_terminal_state);
  384. span_log_set_level(logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
  385. span_log_set_tag(logging, "T.38");
  386. logging = t38_core_get_logging_state(t38_core);
  387. span_log_set_level(logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
  388. span_log_set_tag(logging, "T.38");
  389. logging = t30_get_logging_state(t30);
  390. span_log_set_level(logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
  391. span_log_set_tag(logging, "T.38");
  392. t30_set_supported_modems(t30, supported_modems);
  393. t30_set_tx_ident(t30, "11111111");
  394. t30_set_tx_nsf(t30, (const uint8_t *) "\x50\x00\x00\x00Spandsp\x00", 12);
  395. if (caller)
  396. t30_set_tx_file(t30, input_tiff_file_name, -1, -1);
  397. else
  398. t30_set_rx_file(t30, OUTPUT_TIFF_FILE_NAME, -1);
  399. t30_set_phase_b_handler(t30, phase_b_handler, (void *) t30);
  400. t30_set_phase_d_handler(t30, phase_d_handler, (void *) t30);
  401. t30_set_phase_e_handler(t30, phase_e_handler, (void *) t30);
  402. t30_set_ecm_capability(t30, use_ecm);
  403. t30_set_supported_compressions(t30,
  404. T4_COMPRESSION_T4_1D
  405. | T4_COMPRESSION_T4_2D
  406. | T4_COMPRESSION_T6
  407. | T4_COMPRESSION_T85
  408. | T4_COMPRESSION_T85_L0
  409. | T4_COMPRESSION_T42_T81
  410. | T4_COMPRESSION_COLOUR);
  411. t30_set_supported_bilevel_resolutions(t30,
  412. T4_RESOLUTION_R8_STANDARD
  413. | T4_RESOLUTION_R8_FINE
  414. | T4_RESOLUTION_R8_SUPERFINE
  415. | T4_RESOLUTION_R16_SUPERFINE
  416. | T4_RESOLUTION_200_100
  417. | T4_RESOLUTION_200_200
  418. | T4_RESOLUTION_200_400
  419. | T4_RESOLUTION_300_300
  420. | T4_RESOLUTION_300_600
  421. | T4_RESOLUTION_400_400
  422. | T4_RESOLUTION_400_800
  423. | T4_RESOLUTION_600_600
  424. | T4_RESOLUTION_600_1200
  425. | T4_RESOLUTION_1200_1200);
  426. t30_set_supported_colour_resolutions(t30,
  427. T4_RESOLUTION_100_100
  428. | T4_RESOLUTION_200_200
  429. | T4_RESOLUTION_300_300
  430. | T4_RESOLUTION_400_400
  431. | T4_RESOLUTION_600_600
  432. | T4_RESOLUTION_1200_1200);
  433. if (pcap_scan_pkts(input_file_name, src_addr, src_port, dest_addr, dest_port, t38_terminal_timing_update, process_packet, NULL))
  434. exit(2);
  435. /* Push the time along, to flush out any remaining activity from the application. */
  436. now.tv_sec += 60;
  437. t38_terminal_timing_update(NULL, &now);
  438. }
  439. else
  440. {
  441. wave_handle = NULL;
  442. if (log_audio)
  443. {
  444. if ((wave_handle = sf_open_telephony_write(OUTPUT_WAVE_FILE_NAME, 2)) == NULL)
  445. {
  446. fprintf(stderr, " Cannot create audio file '%s'\n", OUTPUT_WAVE_FILE_NAME);
  447. exit(2);
  448. }
  449. }
  450. if ((t38_gateway_state = t38_gateway_init(NULL, tx_packet_handler, NULL)) == NULL)
  451. {
  452. fprintf(stderr, "Cannot start the T.38 channel\n");
  453. exit(2);
  454. }
  455. t38_core = t38_gateway_get_t38_core_state(t38_gateway_state);
  456. t38_gateway_set_transmit_on_idle(t38_gateway_state, use_transmit_on_idle);
  457. t38_set_t38_version(t38_core, t38_version);
  458. t38_gateway_set_ecm_capability(t38_gateway_state, use_ecm);
  459. logging = t38_gateway_get_logging_state(t38_gateway_state);
  460. span_log_set_level(logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
  461. span_log_set_tag(logging, "T.38");
  462. logging = t38_core_get_logging_state(t38_core);
  463. span_log_set_level(logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
  464. span_log_set_tag(logging, "T.38");
  465. if ((fax_state = fax_init(NULL, caller)) == NULL)
  466. {
  467. fprintf(stderr, "Cannot start FAX\n");
  468. exit(2);
  469. }
  470. t30 = fax_get_t30_state(fax_state);
  471. fax_set_transmit_on_idle(fax_state, use_transmit_on_idle);
  472. fax_set_tep_mode(fax_state, use_tep);
  473. t30_set_supported_modems(t30, supported_modems);
  474. t30_set_tx_ident(t30, "22222222");
  475. t30_set_tx_nsf(t30, (const uint8_t *) "\x50\x00\x00\x00Spandsp\x00", 12);
  476. if (caller)
  477. t30_set_tx_file(t30, input_tiff_file_name, -1, -1);
  478. else
  479. t30_set_rx_file(t30, OUTPUT_TIFF_FILE_NAME, -1);
  480. t30_set_phase_b_handler(t30, phase_b_handler, (void *) t30);
  481. t30_set_phase_d_handler(t30, phase_d_handler, (void *) t30);
  482. t30_set_phase_e_handler(t30, phase_e_handler, (void *) t30);
  483. t30_set_ecm_capability(t30, use_ecm);
  484. t30_set_supported_compressions(t30,
  485. T4_COMPRESSION_T4_1D
  486. | T4_COMPRESSION_T4_2D
  487. | T4_COMPRESSION_T6
  488. | T4_COMPRESSION_T85
  489. | T4_COMPRESSION_T85_L0
  490. | T4_COMPRESSION_T42_T81
  491. | T4_COMPRESSION_COLOUR);
  492. t30_set_supported_bilevel_resolutions(t30,
  493. T4_RESOLUTION_R8_STANDARD
  494. | T4_RESOLUTION_R8_FINE
  495. | T4_RESOLUTION_R8_SUPERFINE
  496. | T4_RESOLUTION_R16_SUPERFINE
  497. | T4_RESOLUTION_200_100
  498. | T4_RESOLUTION_200_200
  499. | T4_RESOLUTION_200_400
  500. | T4_RESOLUTION_300_300
  501. | T4_RESOLUTION_300_600
  502. | T4_RESOLUTION_400_400
  503. | T4_RESOLUTION_400_800
  504. | T4_RESOLUTION_600_600
  505. | T4_RESOLUTION_600_1200
  506. | T4_RESOLUTION_1200_1200);
  507. t30_set_supported_colour_resolutions(t30,
  508. T4_RESOLUTION_100_100
  509. | T4_RESOLUTION_200_200
  510. | T4_RESOLUTION_300_300
  511. | T4_RESOLUTION_400_400
  512. | T4_RESOLUTION_600_600
  513. | T4_RESOLUTION_1200_1200);
  514. logging = fax_get_logging_state(fax_state);
  515. span_log_set_level(logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
  516. span_log_set_tag(logging, "FAX ");
  517. logging = t30_get_logging_state(t30);
  518. span_log_set_level(logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
  519. span_log_set_tag(logging, "FAX ");
  520. if (pcap_scan_pkts(input_file_name, src_addr, src_port, dest_addr, dest_port, t38_gateway_timing_update, process_packet, NULL))
  521. exit(2);
  522. /* Push the time along, to flush out any remaining activity from the application. */
  523. now.tv_sec += 60;
  524. t38_gateway_timing_update(NULL, &now);
  525. fax_release(fax_state);
  526. t38_gateway_release(t38_gateway_state);
  527. if (log_audio)
  528. {
  529. if (sf_close_telephony(wave_handle))
  530. {
  531. fprintf(stderr, " Cannot close audio file '%s'\n", OUTPUT_WAVE_FILE_NAME);
  532. exit(2);
  533. }
  534. }
  535. }
  536. }
  537. /*- End of function --------------------------------------------------------*/
  538. /*- End of file ------------------------------------------------------------*/