v42bis_tests.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * v42bis_tests.c
  5. *
  6. * Written by Steve Underwood <steveu@coppice.org>
  7. *
  8. * Copyright (C) 2005 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. /* THIS IS A WORK IN PROGRESS. IT IS NOT FINISHED. */
  26. /*! \page v42bis_tests_page V.42bis tests
  27. \section v42bis_tests_page_sec_1 What does it do?
  28. These tests compress the contents of a file specified on the command line, writing
  29. the compressed data to v42bis_tests.v42bis. They then read back the contents of the
  30. compressed file, decompress, and write the results to v42bis_tests.out. The contents
  31. of this file should exactly match the original file.
  32. */
  33. #if defined(HAVE_CONFIG_H)
  34. #include "config.h"
  35. #endif
  36. #include <stdlib.h>
  37. #include <stdio.h>
  38. #include <fcntl.h>
  39. #include <string.h>
  40. #include <unistd.h>
  41. #include <errno.h>
  42. #include <ctype.h>
  43. #include <assert.h>
  44. #include "spandsp.h"
  45. #define COMPRESSED_FILE_NAME "v42bis_tests.v42bis"
  46. #define DECOMPRESSED_FILE_NAME "v42bis_tests.out"
  47. int in_octets_to_date = 0;
  48. int out_octets_to_date = 0;
  49. static void frame_handler(void *user_data, const uint8_t *buf, int len)
  50. {
  51. int ret;
  52. if ((ret = write((intptr_t) user_data, buf, len)) != len)
  53. fprintf(stderr, "Write error %d/%d\n", ret, errno);
  54. out_octets_to_date += len;
  55. }
  56. static void data_handler(void *user_data, const uint8_t *buf, int len)
  57. {
  58. int ret;
  59. if ((ret = write((intptr_t) user_data, buf, len)) != len)
  60. fprintf(stderr, "Write error %d/%d\n", ret, errno);
  61. out_octets_to_date += len;
  62. }
  63. int main(int argc, char *argv[])
  64. {
  65. int len;
  66. v42bis_state_t *state_a;
  67. v42bis_state_t *state_b;
  68. uint8_t buf[1024];
  69. int in_fd;
  70. int v42bis_fd;
  71. int out_fd;
  72. int do_compression;
  73. bool do_decompression;
  74. bool stutter_compression;
  75. int stutter_time;
  76. int seg;
  77. int opt;
  78. time_t now;
  79. const char *argv0;
  80. const char *original_file;
  81. const char *compressed_file;
  82. const char *decompressed_file;
  83. argv0 = argv[0];
  84. do_compression = false;
  85. do_decompression = false;
  86. stutter_compression = false;
  87. while ((opt = getopt(argc, argv, "cds")) != -1)
  88. {
  89. switch (opt)
  90. {
  91. case 'c':
  92. do_compression = true;
  93. break;
  94. case 'd':
  95. do_decompression = true;
  96. break;
  97. case 's':
  98. stutter_compression = true;
  99. break;
  100. default:
  101. //usage();
  102. exit(2);
  103. break;
  104. }
  105. }
  106. argc -= optind;
  107. argv += optind;
  108. if (argc < 1)
  109. {
  110. fprintf(stderr, "Usage: %s [-c] [-d] [-s] <in-file> [<out-file>]\n", argv0);
  111. exit(2);
  112. }
  113. if (do_compression)
  114. {
  115. original_file = argv[0];
  116. compressed_file = COMPRESSED_FILE_NAME;
  117. }
  118. else
  119. {
  120. original_file = NULL;
  121. compressed_file = argv[0];
  122. }
  123. decompressed_file = (argc > 1) ? argv[1] : DECOMPRESSED_FILE_NAME;
  124. if (do_compression)
  125. {
  126. stutter_time = rand() & 0x3FF;
  127. if ((in_fd = open(argv[0], O_RDONLY)) < 0)
  128. {
  129. fprintf(stderr, "Error opening file '%s'.\n", original_file);
  130. exit(2);
  131. }
  132. if ((v42bis_fd = open(compressed_file, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0)
  133. {
  134. fprintf(stderr, "Error opening file '%s'.\n", compressed_file);
  135. exit(2);
  136. }
  137. time(&now);
  138. state_a = v42bis_init(NULL, 3, 512, 6, frame_handler, (void *) (intptr_t) v42bis_fd, 512, data_handler, NULL, 512);
  139. span_log_set_level(v42bis_get_logging_state(state_a), SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW);
  140. span_log_set_tag(v42bis_get_logging_state(state_a), "V.42bis");
  141. //v42bis_compression_control(state_a, V42BIS_COMPRESSION_MODE_ALWAYS);
  142. in_octets_to_date = 0;
  143. out_octets_to_date = 0;
  144. while ((len = read(in_fd, buf, 1024)) > 0)
  145. {
  146. seg = 0;
  147. if (stutter_compression)
  148. {
  149. while ((len - seg) >= stutter_time)
  150. {
  151. if (v42bis_compress(state_a, buf + seg, stutter_time))
  152. {
  153. fprintf(stderr, "Bad return code from compression\n");
  154. exit(2);
  155. }
  156. v42bis_compress_flush(state_a);
  157. seg += stutter_time;
  158. stutter_time = rand() & 0x3FF;
  159. }
  160. }
  161. if (v42bis_compress(state_a, buf + seg, len - seg))
  162. {
  163. fprintf(stderr, "Bad return code from compression\n");
  164. exit(2);
  165. }
  166. in_octets_to_date += len;
  167. }
  168. v42bis_compress_flush(state_a);
  169. printf("%d bytes compressed to %d bytes in %lds\n", in_octets_to_date, out_octets_to_date, time(NULL) - now);
  170. close(in_fd);
  171. close(v42bis_fd);
  172. }
  173. if (do_decompression)
  174. {
  175. /* Now open the files for the decompression. */
  176. if ((v42bis_fd = open(compressed_file, O_RDONLY)) < 0)
  177. {
  178. fprintf(stderr, "Error opening file '%s'.\n", compressed_file);
  179. exit(2);
  180. }
  181. if ((out_fd = open(decompressed_file, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0)
  182. {
  183. fprintf(stderr, "Error opening file '%s'.\n", decompressed_file);
  184. exit(2);
  185. }
  186. time(&now);
  187. state_b = v42bis_init(NULL, 3, 512, 6, frame_handler, (void *) (intptr_t) v42bis_fd, 512, data_handler, (void *) (intptr_t) out_fd, 512);
  188. span_log_set_level(v42bis_get_logging_state(state_b), SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW);
  189. span_log_set_tag(v42bis_get_logging_state(state_b), "V.42bis");
  190. in_octets_to_date = 0;
  191. out_octets_to_date = 0;
  192. while ((len = read(v42bis_fd, buf, 1024)) > 0)
  193. {
  194. if (v42bis_decompress(state_b, buf, len))
  195. {
  196. fprintf(stderr, "Bad return code from decompression\n");
  197. exit(2);
  198. }
  199. in_octets_to_date += len;
  200. }
  201. v42bis_decompress_flush(state_b);
  202. printf("%d bytes decompressed to %d bytes in %lds\n", in_octets_to_date, out_octets_to_date, time(NULL) - now);
  203. close(v42bis_fd);
  204. close(out_fd);
  205. }
  206. return 0;
  207. }
  208. /*- End of function --------------------------------------------------------*/
  209. /*- End of file ------------------------------------------------------------*/